From: lkcl Date: Mon, 30 Oct 2023 06:51:06 +0000 (+0000) Subject: (no commit message) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=78747e8;p=libreriscv.git --- diff --git a/openpower/sv/cookbook/remap_matrix.mdwn b/openpower/sv/cookbook/remap_matrix.mdwn index e321914c9..68ba24a49 100644 --- a/openpower/sv/cookbook/remap_matrix.mdwn +++ b/openpower/sv/cookbook/remap_matrix.mdwn @@ -1,11 +1,15 @@ -# SVP64/Vector+1 REMAP Worked Example: Matrix Multiply +# SVP64 REMAP Worked Example: Matrix Multiply - +Links + +* [Online matrix calculator](https://matrix.reshish.com/multCalculation.php) +* [[sv/remap]] +* 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 @@ -54,16 +58,18 @@ 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 +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 | @@ -75,9 +81,11 @@ Calculations: | 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 | @@ -86,11 +94,13 @@ For the algorithm, assign indeces to matrices as follows: 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 @@ -104,6 +114,7 @@ Index list: 3 | 1 | 3 4 | 3 | 3 5 | 5 | 3 +``` The issue with this algorithm is that the result matrix element is the same @@ -119,13 +130,16 @@ element (and thus register) changes with every multiply-add operation. 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 @@ -139,6 +153,7 @@ Index list: 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 @@ -149,9 +164,9 @@ register. * 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)* @@ -170,7 +185,7 @@ be possible. ### 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: @@ -235,7 +250,9 @@ SPRs directly. Going back to the assembler instruction used to setup the shape for matrix multiply: +``` svshape 2, 2, 3, 0, 0 +``` breakdown: @@ -248,8 +265,3 @@ breakdown: ## Appendix - -### Links - -- [Online matrix calculator](https://matrix.reshish.com/multCalculation.php) -- [LibreSOC matrix multiply REMAP]()