CS 61C Fall 2023

CS 61C Fall 2023 Calendar Extensions Staff Policies Resources Quick Links
Project 2: CS61Classify Part A
Task 6: Matrix Multiplication
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 j of 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.
Conceptual Overview: Storing Matrices
Conceptual Overview: Matrix Multiplication
Testing and debugging Task 7: Read Matrix
Task 8: Write Matrix
Task 9: Classify
Task 10: Partner/Feedback Form
Submission and Grading Appendix: Function Definitions Appendix: Calling Convention
¡ì Your Task
Fill in the matmul function in src/matmul.s. matmul: Task 6.
A pointer to the start of the first matrix A (stored as an integer array in row-major order).
The number of rows (height) of the first matrix A. The number of columns (width) of the first matrix A.
A pointer to the start of the second matrix B (stored as an integer array in row-major order).
The number of rows (height) of the second matrix B. The number of columns (width) of the second matrix B.
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
The height or width of either matrix is less than 1.
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 3×3 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. Openthefilewithreadpermissions.Thefilepathisprovidedasanargument(a0).
2. Readthenumberofrowsandcolumnsfromthefile(remember:thesearethefirsttwointegersinthefile).Storetheseintegers in memory at the provided pointers (a1 for rows and a2 for columns).
3. Allocatespaceontheheaptostorethematrix.(Hint:Usethenumberofrowsandcolumnsfromtheprevioussteptodetermine how much space to allocate.)
4. Readthematrixfromthefiletothememoryallocatedinthepreviousstep.
5. Closethefile.
6. Returnapointertothematrixinmemory.
read_matrix: Task 7.
A pointer to the filename string.
A pointer to an integer which will contain the number of rows. You can assume this points to allocated memory.
A pointer to an integer which will contain the number of columns. You can assume this points to allocated memory.
malloc returns an error.
fopen returns an error.
fclose returns an error.
fread does not read the correct number of bytes.
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
26 27 28 29
Return values
A pointer to the matrix in memory.
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
vdb test_read_fail_malloc.s
As a reminder, you can use the functions described in the calling convention appendix to debug calling convention errors. We also have debugging videos that may help you debug these errors.
¡ì Task 8: Write Matrix
Fill in the write_matrix function in src/write_matrix.s. This function should do the following:
1. Openthefilewithwritepermissions.Thefilepathisprovidedasanargument.
2. Writethenumberofrowsandcolumnstothefile.(Hint:Thefwritefunctionexpectsapointertodatainmemory,soyou should first store the data to memory, and then pass a pointer to the data to fwrite.)
3. Writethedatatothefile.
4. Closethefile.
write_matrix: Task 8.
A pointer to the filename string.
A pointer to the matrix in memory (stored as an integer array). The number of rows in the matrix.
The number of columns in the matrix.
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
fopen returns an error.
fwrite does not write the correct number of bytes. fclose returns an error.
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 8: Relevant Function Definitions
¡ì Testing and debugging
To test your function, in your local terminal, run bash test.sh test_write_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_write_1.s
vdb test_write_fail_fclose.s
vdb test_write_fail_fopen.s
vdb test_write_fail_fwrite.s
As a reminder, you can use the functions described in the calling convention appendix to debug calling convention errors. We also have debugging videos that may help you debug these errors.
¡ì Task 9: Classify
Recall the neural net that we’re trying to create. In this task, you will use functions from the previous tasks in order to complete the classification function, located in src/classify.s.
Fill in the classify function in src/classify.s. This function should do the following:
1. Readthreematricesm0,m1,andinputfromfiles.Theirfilepathsareprovidedasarguments.Youwillneedtoallocatespacefor
the pointer arguments to read_matrix, since that function is expecting a pointer to allocated memory.
2. Computeh=matmul(m0,input).Youwillprobablyneedtomallocspacetofith.
3. Computeh=relu(h).Rememberthatreluisperformedin-place.
4. Computeo=matmul(m1,h)andwritetheresultingmatrixtotheoutputfile.Theoutputfilepathisprovidedasanargument.
5. Computeandreturnargmax(o).Iftheprintargumentissetto0,thenalsoprintoutargmax(o)andanewlinecharacter.
6. Freeanydatayouallocatedwithmalloc.Thisincludesanyheapblocksallocatedfromcallingread_matrix.
7. Remembertoputthereturnvalue,argmax(o),intheappropriateregisterbeforereturning.
classify: Task 9.
Return values
a1[1] = *(a1 + 4) a1[2] = *(a1 + 8) a1[3] = *(a1 + 12) a1[4] = *(a1 + 16) a2
argc (the number of arguments provided)
argv, a pointer to an array of argument strings (char *)
A pointer to the filepath string of the first matrix file m0.
A pointer to the filepath string of the second matrix file m1.
A pointer to the filepath string of the input matrix file input.
A pointer to the filepath string of the output file.
If set to 0, print out the classification. Otherwise, do not print anything. The classification (see above).
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
malloc returns an error.
There are an incorrect number of command line arguments. Note that there are 5 arguments to the program,
because a1[0] is reserved for the name of the program.
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 9: Relevant Function Definitions
¡ì Testing and debugging
To test your function, in your local terminal, run bash test.sh test_classify.
To debug your function, in your Venus terminal, run cd /vmfs/test-src, then run a VDB command to start the debugger:
vdb test_classify_1_silent.s ../tests/classify-1/m0.bin ../tests/classify-1/m1.bin ../tests/classify-1/input.bin ../tests/classify-1/outp
vdb test_classify_2_print.s ../tests/classify-2/m0.bin ../tests/classify-2/m1.bin ../tests/classify-2/input.bin ../tests/classify-2/outpu
vdb test_classify_3_print.s ../tests/classify-3/m0.bin ../tests/classify-3/m1.bin ../tests/classify-3/input.bin ../tests/classify-3/outpu
vdb test_classify_fail_malloc.s ../tests/classify-1/m0.bin ../tests/classify-1/m1.bin ../tests/classify-1/input.bin ../tests/classify-1/o
vdb test_classify_not_enough_args.s
Once you have classify running, you can run bash test.sh test_chain. This runs your classification function twice to make sure you properly followed calling convention.
As a reminder, you can use the functions described in the calling convention appendix to debug calling convention errors. We also have debugging videos that may help you debug these errors.
To debug the chain test, run cd /vmfs/test-src, then run a VDB command to start the debugger: vdb ../tests/chain-1/chain.s
¡ì Task 10: Partner/Feedback Form
Congratulations on finishing the project! We’d love to hear your feedback on what can be improved for future semesters.
Please fill out this short form , where you can offer your thoughts on the project and (if applicable) your partnership. Any feedback you provide won’t affect your grade, so feel free to be honest and constructive.
¡ì Submission and Grading
Submit your code to the Project 2B assignment on Gradescope.
To ensure the autograder runs correctly, do not add any .import statements to the starter code. Also, make sure there are no ecall instructions in your code.
浙大学霸代写 加微信 cstutorcs