406fda611bf3235f1537902c23b2ad590b790835
[libreriscv.git] / openpower / sv / cookbook / remap_matrix.mdwn
1 # SVP64/Vector+1 REMAP Worked Example: Matrix Multiply
2
3 <https://bugs.libre-soc.org/show_bug.cgi?id=701>
4
5 TODO: Include screenshots
6
7 One of the most powerful and versatile modes of the REMAP engine (a part of
8 the SVP64/Vector+1 feature set) is the ability to perform matrix
9 multiplication with all elements within a scalar register file.
10
11 This is done by converting the index used to iterate over the operand and
12 result matrices to the actual index inside the scalar register file.
13
14 ## Worked example - manual (conventional method)
15
16 The matrix multiply looks like this:
17
18 mat_X * mat_Y = mat_Z
19
20 When multiplying non-square matrices (rows != columns), to determine the
21 dimension of the result when matrix X has `a` rows and `b` columns and matrix
22 Y has `b` rows and `c` columns:
23
24 X_axb * Y_bxc = Z_axc
25
26 The result matrix will have number of rows of the first matrix, and number of
27 columns of the second matrix.
28
29
30 For this example, the following values will be used for the operand matrices X
31 and Y, result Z shown for completeness.
32
33 X =| 1 2 3 | Y = | 6 7 | Z = | 52 58 |
34 | 3 4 5 | | 8 9 | | 100 112 |
35 | 10 11 |
36
37 Matrix X has 2 rows, 3 columns (2x3), and matrix Y has 3 rows, 2 columns.
38
39 To determine the final dimensions of the resultant matrix Z, take the number
40 of rows from matrix X (2) and number of columns from matrix Y (2).
41
42 For the algorithm, assign indeces to matrices as follows:
43
44 Index | 0 1 2 3 4 5 |
45 Mat X | 1 2 3 3 4 5 |
46
47 Index | 0 1 2 3 4 5 |
48 Mat Y | 6 7 8 9 10 11 |
49
50 Index | 0 1 2 3 |
51 Mat Z | 52 58 100 112 |
52
53 (Start with the first row, then assign index left-to-right, top-to-bottom.)
54
55 The method usually taught in linear algebra course to students is the
56 following:
57
58 1. Start with the first row of the first matrix, and first column of the
59 second matrix.
60 2. Multiply each element in the row by each element in the column, and sum
61 with the current value of the result matrix element (multiply-add-accumulate).
62 Store result in the first row, first column entry.
63 3. Move to the next column of the second matrix, and next column of the result
64 matrix. If there are no more columns in the second matrix, go back to first
65 column (second matrix), and move to next row (first and result matrices).
66 If there are no more rows left, result matrix has been fully computed.
67 4. Repeat step 2.
68 4. Move to the next row of the first matrix, and next column of the second matrix.
69
70 This for-loop uses the indeces as shown above
71
72 for i in range(mat_X_num_rows):
73 for k in range(0, mat_Y_num_cols):
74 for j in range(0, mat_X_num_cols): # or mat_Y_num_rows
75 mat_Z[i][k] += mat_X[i][j] * mat_Y[j][k]
76
77
78 Calculations:
79
80 | 1 2 3 | | 6 7 | = | (1*6 + 2*8 + 3*10) (1*7 + 2*9 3*11) |
81 | 3 4 5 | * | 8 9 | | (3*6 + 4*8 + 5*10) (3*7 + 4*9 5*11) |
82 | 10 11 |
83
84 | 1 2 3 | | 6 7 | = | ( 6 + 16 + 30) ( 7 + 18 + 33) |
85 | 3 4 5 | * | 8 9 | | (18 + 32 + 50) (21 + 36 + 55) |
86 | 10 11 |
87
88 | 1 2 3 | | 6 7 | = | 52 58 |
89 | 3 4 5 | * | 8 9 | | 100 112 |
90 | 10 11 |
91
92
93
94 ## Appendix
95
96 ### Links
97
98 - [Online matrix calculator](https://matrix.reshish.com/multCalculation.php)
99 - [LibreSOC matrix multiply REMAP]()