-# SVP64/Vector+1 REMAP Worked Example: Matrix Multiply
+# SVP64 REMAP Worked Example: Matrix Multiply
-<https://bugs.libre-soc.org/show_bug.cgi?id=701>
+Links
+
+* [Online matrix calculator](https://matrix.reshish.com/multCalculation.php)
+* [[sv/remap]]
+* <https://bugs.libre-soc.org/show_bug.cgi?id=701>
TODO: Include screenshots
One of the most powerful and versatile modes of the REMAP engine (a part of
-the SVP64/Vector+1 feature set) is the ability to perform matrix
+the SVP64 feature set) is the ability to perform matrix
multiplication with all elements within a scalar register file.
This is done by converting the index used to iterate over the operand and
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
+This for-loop uses the indices 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]
-
+```
Calculations:
+```
| 1 2 3 | | 6 7 | = | (1*6 + 2*8 + 3*10) (1*7 + 2*9 3*11) |
| 3 4 5 | * | 8 9 | | (3*6 + 4*8 + 5*10) (3*7 + 4*9 5*11) |
| 10 11 |
| 1 2 3 | | 6 7 | = | 52 58 |
| 3 4 5 | * | 8 9 | | 100 112 |
| 10 11 |
+```
For the algorithm, assign indeces to matrices as follows:
+```
Index | 0 1 2 3 4 5 |
Mat X | 1 2 3 3 4 5 |
Index | 0 1 2 3 |
Mat Z | 52 58 100 112 |
+```
(Start with the first row, then assign index left-to-right, top-to-bottom.)
Index list:
+```
Mat X | Mat Y | Mat Z
0 | 0 | 0
1 | 2 | 0
3 | 1 | 3
4 | 3 | 3
5 | 5 | 3
+```
The issue with this algorithm is that the result matrix element is the same
The code:
+```
for i in range(mat_X_num_rows):
for j in range(0, mat_X_num_cols): # or mat_Y_num_rows
for k in range(0, mat_Y_num_cols):
mat_Z[i][k] += mat_X[i][j] * mat_Y[j][k]
+```
Index list:
+```
Mat X | Mat Y | Mat Z
0 | 0 | 0
0 | 1 | 1
2 | 5 | 1
5 | 4 | 2
5 | 5 | 3
+```
The index for the result matrix changes with every operation, and thus the
consecutive multiply-add instruction doesn't depend on the previous write
* SVP64 assembler example:
[unit test](https://git.libre-soc.org/?p=openpower-isa.git;a=blob;f=src/openpower/decoder/isa/test_caller_svp64_matrix.py;hb=30f2d8a8e92ad2939775f19e6a0f387499e9842b#l56)
* SVREMAP and SVSHAPE instructions defined in:
-[[/openpower/sv/rfc/ls009|LS009 RFC]]
+[[sv/rfc/ls009]
* Multiple-Add Low Doubleword instruction pseudo-code (OpenPOWER ISA 3.0C
-Book I, section 3.3.9): [[/openpower/isa/fixedarith|]]
+Book I, section 3.3.9): [[openpower/isa/fixedarith]]
*(Need to check if first arg of svremap correct, then one shown works with
ISACaller)*
### SHAPE Remapping SPRs
-* See [[openpower/sv/remap]] for the full break down of SPRs SHAPE0-3.
+* See [[sv/remap]] for the full break down of SPRs SHAPE0-3.
For Matrix Multiply, SHAPE0 SPR is used:
Going back to the assembler instruction used to setup the shape for matrix
multiply:
+```
svshape 2, 2, 3, 0, 0
+```
breakdown:
## Appendix
-
-### Links
-
-- [Online matrix calculator](https://matrix.reshish.com/multCalculation.php)
-- [LibreSOC matrix multiply REMAP]()