Code overview
This page describes, step by step, how the different components of Task 3 work together to generate input vectors, select an algorithm, compute the sum \(d = a\,x + y\), and write the result.
Generating Vectors¶
-
Invoke the executable
From the terminal , use the
run
command associated with thegenerateVectors
executable:where
<N>
is the number of elements in each vector and<filename_prefix>
is the prefix for the output files.<filename_prefix>
is a path and file‐name prefix (for example,data/input/vector.txt
).
-
What happens inside
- The program allocates two
std::vector<double>
of lengthN
. - It fills both vectors with a constant value (set in code).
-
It calls the
write_vector
helper, which:- Examines the extension of
<filename_prefix>
(.txt
,.dat
, or .h5
). - Dispatches to the matching I/O routine (plain‐text, binary, or HDF5).
- Writes
x
to<filename_prefix>_x.<ext>
andy
to<filename_prefix>_y.<ext>
.
- Examines the extension of
- The program allocates two
-
Result
- Two files are created:
<filename_prefix>_x.<ext>
and<filename_prefix>_y.<ext>
, containing the vectorsx
andy
respectively.
- Two files are created:
Preparing the configuration¶
Before running the sum, you must create or edit a YAML file (for example, config/config.yml) with these fields:
config/config.yml | |
---|---|
input.vector_x
,input.vector_y
: paths to the two vectors.N
: number of elements (must match how you generated the vectors).a
: scalar multiplier in the sum.output.path
: directory where the result will be saved.output.prefix
: prefix for the output file.output.format
: format for the output file (can betxt
,dat
, orh5
).implementation
: selects the algorithm (see next section).
Computing the sum¶
-
Invoke the executable
From the terminal, use the
run
command associated with thevectorSum
executable: -
YAML parsing
The program uses
yaml-cpp
to read all required parameters fromconfig.yml
. -
Reading input vectors
It calls the
read_vector
helper, which checks the file extension and reads the data into twostd::vector<double>
objects. -
Choosing the implementation
Based on the
implementation
field in the YAML config, it creates an instance of eitherVectorSumDefault
orVectorSumGSL
. Both derive fromVectorSumInterface
. -
Computing the sum
The chosen implementation's
compute_sum
method is called with the vectorsx
,y
, scalara
, and an output vectord
.- If
implementation
isdefault
, it uses a simple loop to compute \(d = a \cdot x + y\). - If
implementation
isgsl
, it usesgsl_vector_axpby
for an in-place BLAS-optimized update.
- If
-
Writing the result
The program calls the
write_vector
helper to save the output vectord
to the specified file in the chosen format.