208164f353c2726a2362573b2b887a00bd5c5390
[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 of rows from matrix X (2) and number of columns from matrix Y (2).
40
41 For the algorithm, assign indeces to matrices as follows:
42
43 Index | 0 1 2 3 4 5 |
44 Mat X | 1 2 3 3 4 5 |
45
46 Index | 0 1 2 3 4 5 |
47 Mat Y | 6 7 8 9 10 11 |
48
49 Index | 0 1 2 3 |
50 Mat Z | 52 58 100 112 |
51
52 The method usually taught in linear algebra course to students is the following:
53
54 1. Start with the first row of the first matrix, and first column of the second matrix.
55 2. Multiply each element in the row by each element in the column, and sum the result.
56 3. Store result in the first row, first column entry.
57
58 Equation: (TODO)
59
60 A(i, j) * B(j, k) = C(i, k)
61
62 Calculations:
63
64 | 1 2 3 | | 6 7 | = | (1*6 + 2*8 + 3*10) (1*7 + 2*9 3*11) |
65 | 3 4 5 | * | 8 9 | | (3*6 + 4*8 + 5*10) (3*7 + 4*9 5*11) |
66 | 10 11 |
67
68 | 1 2 3 | | 6 7 | = | ( 6 + 16 + 30) ( 7 + 18 + 33) |
69 | 3 4 5 | * | 8 9 | | (18 + 32 + 50) (21 + 36 + 55) |
70 | 10 11 |
71
72 | 1 2 3 | | 6 7 | = | 52 58 |
73 | 3 4 5 | * | 8 9 | | 100 112 |
74 | 10 11 |
75
76 ## Appendix
77
78 ### Links
79
80 - [Online matrix calculator](https://matrix.reshish.com/multCalculation.php)
81 - [LibreSOC matrix multiply REMAP]()