Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Part B | CS 61C Fall 2023

Part B

In this part, you will implement a matrix multiplication function and file operations to read pictures of handwritten digits. Then you will use your math functions from the previous

part to determine what digit is in the picture.

If you are curious how the machine learning algorithm works, you can expand the Neural Networks section below. This is optional and not required to finish the project.

Optional: Neural Networks

Task 6: Matrix Multiplication

Conceptual Overview: Storing Matrices

A matrix is a 2-dimensional array of integers. In this project, matrices will be stored as an   integer array in row-major order. Row-major order means we store each row of the matrix consecutively in memory as a 1-dimensional integer array.

Conceptual Overview: Matrix Multiplication

The matrix multiplication function takes in two integer matrices A (dimension n × m) and B (dimension m × k) and outputs an integer matrix C (dimension n × k).

To calculate the entry at row i, column jof C, take the dot product of the ith row of A and  the jth column of B. Note that this can be done by calling the dot function with the proper strides.

For example, in the above diagram, we are computing the entry in row 1, column 1 of C by taking the dot product of the 1st row of A and the 1st row of B.

In the above diagram, we are computing the entry in row 2, column 2 of C. Note that we are changing the pointer to the start of the array in order to access later rows and columns.

Your Task

Fill in thematmul function in src/matmul.s.

matmul: Task 6.

Arguments

a0

int *

A pointer to the start of the first matrix A (stored as an integer array in row-major order).

a1

int

The number of rows (height) of the first matrix A.

a2

int

The number of columns (width) of the first matrix A.

a3

int *

A pointer to the start of the second matrix B (stored as an integer array in row-major order).

a4

int

The number of rows (height) of the second matrix B.

a5

int

The number of columns (width) of the second matrix B.

a6

int *

A pointer to the start of an integer array where the result C should be stored. You can assume this memory has

been allocated (but is uninitialized) and has enough space to store C.

Return

values

None

If the input is malformed in the following ways, put the appropriate return code into a0 and run j exit to quit the program.

Return

code

Exception

38

The height or width of either matrix is less than 1.

38

The number of columns (width) of the first matrix A is not equal to the number of rows (height) of the second matrix B.

Testing and debugging

To test your function, in your local terminal, run bash test.sh test_matmul.

To debug your function, in your Venus terminal, run cd /vmfs/test-src, then run a VDB command to start the debugger:

vdb test_matmul_length_1.s

vdb test_matmul_negative_dim_m0_x.s

vdb test_matmul_negative_dim_m0_y.s

vdb test_matmul_negative_dim_m1_x.s

vdb test_matmul_negative_dim_m1_y.s

vdb test_matmul_nonsquare_1.s

vdb test_matmul_nonsquare_2.s

vdb test_matmul_square.s

vdb test_matmul_unmatched_dims.s

vdb test_matmul_zero_dim_m0.s

vdb test_matmul_zero_dim_m1.s

Debugging advice:

Since you'll need to call the dot function in matmul, make sure to follow calling

convention! See the calling convention appendix for more details. In particular, as soon as you call dot, the dot function is allowed to change all the t0-t6 and a1-a7 registers, so when the dot function returns, you need to assume that those registers contain garbage.

· You can use the functions described in the calling convention appendix to debug calling convention errors.

· Watch the debugging videos!

Task 7: Read Matrix

Conceptual Overview: Matrix Files

Remember from Task 6 that matrices are stored in memory as an integer array in row- major order.

Matrices are stored in files as a consecutive sequence of 4-byte integers. The first and

second integers in the file indicate the number of rows and columns in the matrix,

respectively. The rest of the integers store the elements in the matrix in row-major order.

All the matrix files end in a .bin file extension and are in the tests folder. To view matrix     files, you can run xxd -e matrix_file.bin, replacing matrix_file.bin with the matrix file you want to read.

Reading matrix files

In your local terminal (not the Venus) terminal, navigate to the tests folder (e.g. cd tests), then navigate to the folder that contains the files you want to read. In this example, we'll  cd read-matrix-1 to check the first test.

ls to see the files in this directory. There should be one file, input.bin. Run xxd -e

input.bin to see the contents of this file. The output should look something like this:

00000000: 00000003 00000003 00000001 00000002 ................

00000010: 00000003 00000004 00000005 00000006 ................

00000020: 00000007 00000008 00000009 ............

The left-most column indexes the bytes in the file (e.g. the third row starts at the 0x20th

byte of the file). The dots on the right display the bytes in the file as ASCII, but these bytes don't correspond to printable ASCII characters so only dot placeholders appear.

The actual contents of the file are listed in 4-byte blocks, 4 per row. The first row has the    numbers 3 (row count), 3 (column count), 1 (first element), and 2 (second element). This is a 3x3 matrix with elements [1, 2, 3, 4, 5, 6, 7, 8, 9].

Your Task

Fill in the read_matrix function in src/read_matrix.s. This function should do the following:

1. Open the file with read permissions. The filepathis provided as an argument (a0).

2. Read the number of rows and columns from the file (remember: these are the first    two integers in the file). Store these integers in memory at the provided pointers (a1 for rows and a2 for columns).

3. Allocate space on the heap to store the matrix. (Hint: Use the number of rows and columns from the previous step to determine how much space to allocate.)

4. Read the matrix from the file to the memory allocated in the previous step. 5. Close the file.

6. Return a pointer to the matrix in memory.

read_matrix: Task 7.

Arguments

a0

char

*

A pointer to the filename string.

a1

int *

A pointer to an integer which will contain the number of rows. You can assume this points to allocated memory.

a2

int *

A pointer to an integer which will contain the number of

columns. You can assume this points to allocated

memory.

Return

values

a0

int *

A pointer to the matrix in memory.

If the input is malformed in the following ways, put the appropriate return code into a0 and run j exit to quit the program.

Return

code

Exception

26

malloc returns an error.

27

fopen returns an error.

28

fclose returns an error.

29

fread does not read the correct number of bytes.

To implement this function, you will need to call some utility functions. A complete set of function definitions can be found in the appendix. The relevant function definitions for     this task are provided below (expand the section to see them).

Task 7: Relevant Function Definitions

Testing and debugging

To test your function,in your local terminal, run bash test.sh test_read_matrix.

To debug your function, in your Venus terminal, run cd /vmfs/test-src, then run a VDB command to start the debugger:

vdb test_read_1.s

vdb test_read_2.s

vdb test_read_3.s

vdb test_read_fail_fclose.s

vdb test_read_fail_fopen.s

vdb test_read_fail_fread.s