From: Andrey Miroshnikov Date: Tue, 17 Oct 2023 10:13:28 +0000 (+0000) Subject: Matrix example X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=d559ce5ac28615cc5dbbb2cb57a66a2374f341ad;p=libreriscv.git Matrix example --- diff --git a/openpower/sv/cookbook/remap_matrix.mdwn b/openpower/sv/cookbook/remap_matrix.mdwn index 208164f35..406fda611 100644 --- a/openpower/sv/cookbook/remap_matrix.mdwn +++ b/openpower/sv/cookbook/remap_matrix.mdwn @@ -36,7 +36,8 @@ and Y, result Z shown for completeness. Matrix X has 2 rows, 3 columns (2x3), and matrix Y has 3 rows, 2 columns. -To determine the final dimensions of the resultant matrix Z, take the number of rows from matrix X (2) and number of columns from matrix Y (2). +To determine the final dimensions of the resultant matrix Z, take the number +of rows from matrix X (2) and number of columns from matrix Y (2). For the algorithm, assign indeces to matrices as follows: @@ -49,15 +50,30 @@ For the algorithm, assign indeces to matrices as follows: Index | 0 1 2 3 | Mat Z | 52 58 100 112 | -The method usually taught in linear algebra course to students is the following: +(Start with the first row, then assign index left-to-right, top-to-bottom.) -1. Start with the first row of the first matrix, and first column of the second matrix. -2. Multiply each element in the row by each element in the column, and sum the result. -3. Store result in the first row, first column entry. +The method usually taught in linear algebra course to students is the +following: -Equation: (TODO) +1. Start with the first row of the first matrix, and first column of the +second matrix. +2. Multiply each element in the row by each element in the column, and sum +with the current value of the result matrix element (multiply-add-accumulate). +Store result in the first row, first column entry. +3. Move to the next column of the second matrix, and next column of the result +matrix. If there are no more columns in the second matrix, go back to first +column (second matrix), and move to next row (first and result matrices). +If there are no more rows left, result matrix has been fully computed. +4. Repeat step 2. +4. Move to the next row of the first matrix, and next column of the second matrix. + +This for-loop uses the indeces as shown above + + for i in range(mat_X_num_rows): + for k in range(0, mat_Y_num_cols): + for j in range(0, mat_X_num_cols): # or mat_Y_num_rows + mat_Z[i][k] += mat_X[i][j] * mat_Y[j][k] - A(i, j) * B(j, k) = C(i, k) Calculations: @@ -73,6 +89,8 @@ Calculations: | 3 4 5 | * | 8 9 | | 100 112 | | 10 11 | + + ## Appendix ### Links