1

I've been trying to implement a program for matrix multiplication of multiple of 4 (ex: 4x4, 8x8, 16x16, etc.) and while I my program works for 4x4 matrix, it only fills the first 4 row of every other matrix (which are filled with the proper value).

My guess it's because I don't have the proper algorithm implemented, so I made some search and I came upon that link:

http://www.mathcs.emory.edu/~cheung/Courses/561/Syllabus/90-parallel/SIMD.html

enter image description here

While I understand the first step of the algorithm, I would appreciate if someone could explain to me why the final row of 12 18 18 in the example becomes 13 19 19? I know these are the good values for the result matrix, but I seem to be missing a step and I guess if I could understand it I could come up with a proper algorithm to solve my problem.

I can provide the code I have so far by personal inbox if anyone is interested, but basically it's an adaptation of this algorithm: https://codereview.stackexchange.com/questions/101144/simd-matrix-multiplication

1 Answers1

1

The diagram seems to have a number of mistakes. Firstly, the top right entry is a 5 in Step 3 but a 1 everywhere else, without explanation. Secondly, pairs of numbers are multiplied that are not part of the straightforward definition of matrix multiplication. Unless there is something amazingly clever going on here that is being glossed over, I think it's just plain wrong, and what they're trying to show is this:

In step 1, the blue 2 is multiplied by the top row of the second matrix, and the result added to the top row of the result, i.e. 2 * [3, 6, 2] = [6, 12, 4]. In the second step, the blue circle should move right one column to the 3, which is multiplied by the new red row and again added to the first step, i.e. 3 * [2, 2, 4] = [6, 6, 12], making the top row of the result [12, 18, 16]. Finally in step 3, the blue circle moves right again, giving 1 * [1, 1, 3] = [1, 1, 3], which is added to give [13, 19, 19]. Then the blue circle drops to the second row and it repeats.

In other words, to summarize the algorithm in pseudocode, it's something like:

For i = 0...height_of_first_matrix-1:
 For j = 0...width_of_first_matrix-1:

        multiply left_matrix[i,j] by j-th row of right matrix and add to i-th row of result

Hope that makes sense!

biohacker
  • 126
  • 2