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
runcommand associated with thegenerateVectorsexecutable: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_vectorhelper, which:- Examines the extension of
<filename_prefix>(.txt,.dat, or .h5). - Dispatches to the matching I/O routine (plain‐text, binary, or HDF5).
- Writes
xto<filename_prefix>_x.<ext>andyto<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 vectorsxandyrespectively.
- 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
runcommand associated with thevectorSumexecutable: -
YAML parsing
The program uses
yaml-cppto read all required parameters fromconfig.yml. -
Reading input vectors
It calls the
read_vectorhelper, which checks the file extension and reads the data into twostd::vector<double>objects. -
Choosing the implementation
Based on the
implementationfield in the YAML config, it creates an instance of eitherVectorSumDefaultorVectorSumGSL. Both derive fromVectorSumInterface. -
Computing the sum
The chosen implementation's
compute_summethod is called with the vectorsx,y, scalara, and an output vectord.- If
implementationisdefault, it uses a simple loop to compute \(d = a \cdot x + y\). - If
implementationisgsl, it usesgsl_vector_axpbyfor an in-place BLAS-optimized update.
- If
-
Writing the result
The program calls the
write_vectorhelper to save the output vectordto the specified file in the chosen format.