Lab 01: “Level 1 BLAS” in C: A First Pass

Version: Current as of 2021-02-10 22:03:45

Instructions:

– Written portions of this assignment are submitted via Canvas. Unless specified otherwise, the written portion of the assignment is to be completed using LaTeX. All derivations, images, graphs, and tables are to be included in this document. Handwritten solutions will receive zero credit.

– Code portions of this assignment are submitted via code.vt.edu. Source code must be in the private repository, to which the CMDA 3634 staff must have access.

Deliverables: For this assignment, you are to submit the following:

1. (Canvas) <pid> Lab01.pdf: A PDF file, renderd by pdflatex (the file generated by Overleaf is sufficient) containing the answers to the questions requiring written answers. Your submission should not exceed 2 pages.

2. (code.vt.edu) The source files required to compile and run your solutions to the lab and the tex and image files for your report, in the appropriate directories.

Collaboration: This assignment is to be completed by yourself, however, you make seek assistance from your classmates. In your submission you must indicate who you assisted and from whom you received assistance.

Honor Code: By submitting this assignment, you acknowledge that you have adhered to the Virginia Tech Honor Code and attest to the following:

I have neither given nor received unauthorized assistance on this assignment. The work I am presenting is ultimately my own.


Background

Basic Linear Algebra Subprograms, or BLAS, are a standard library of Fortran (!) routines that form the funda-mental backbone of most numerical computing code, including many libraries that numerical Python depends on. BLAS libraries are often highly tuned for specific computing systems as optimal performance depends on specific knowledge of the hardware. Vendors provide their own BLAS-equivalent libraries to help performance on their hardware. You may have heard of the Intel Math Kernel Library (MKL), as an example.

      BLAS is decomposed into three levels: Level 1 contains Vector operations, Level 2 contains Matrix-Vector operations, and Level 3 contains Matrix-Matrix operations. In the next two labs, we will implement a small number of Level 1 BLAS functions: nrm (norm), dot (inner product), and axpy.


Resources

More on BLAS:

https://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms

http://www.netlib.org/blas/

– SAXPY: http://www.netlib.org/lapack/explore-html/d8/daf/saxpy_8f_source.html

https://www.openblas.net/, https://software.intel.com/en-us/mkl

More on C programming:

https://www.geeksforgeeks.org/c-programming-language/

– Recommended texts


Tasks

1. Setup your lab environment.

● In your class git repository, create the following “standard” directory structure:

   

Be sure to put submitted files in the correct location.

Be mindful of proper git discipline. Follow the class programming style guide.

2. Implement a main program and the following functions (used to answer the questions below) in C.

You may use floats or doubles for floating-point valued variables.

(a) Create a C source file called vector.c.

(b) Defifine a struct named Vector, a new C data type for 3-vectors, using stack arrays.

(c) Write the following functions, which must take Vector, by pointer, for their vector arguments. Func-tions should return 0 if they run successfully and 1 if they produce an error.

     i. initialize: initialized the provided vector to 0

     ii. inner product: returns the inner (dot) product of two vectors

     iii. norm: returns the Euclidian norm of a vector

     iv. normalize: modifies a given vector to have length1, in place

     v. axpy: returns the result of the operation z = α ∗ x + y

     vi. print: prints the contents of a vector in a useful way

Hint: You may start the implementation by passing the vectors by copy (how we do it before Lecture 9), and convert to pass-by-pointer (how we do it in Lecture 9), if that is an easier pathway.

(d) Write a main program that uses the above functions to answer the questions below.

(e) Compile your program so that the binary executable is named vector lab01.

3. Answer the following questions in a LaTeX document.

(a) What command did you run to compile your program?

(b) For the scalars α = 0.45 and β = 0.65 and vectors,

use your program to compute the following values:



Include the text of your output (the listings package will be helpful here) and be sure that it indicates which questions the output corresponds to.

(c) How would you need to modify your code work for vectors of length 10? Do not perform these modifications.

(d) In what way did you make your print function “useful”?

(e) What error checking did it make sense to provide?

(f) Other than the instructor or TAs, who did you give assistance to or receive assistance from on this assignment?

4. Submit your results.

Upload a PDF of your report to Canvas.

Push your source code and LaTeX files to code.vt.edu.

Examine your assignment repository on code.vt.edu to be sure that all of your materials have been correctly submitted.