Search code examples
cfor-loopmatrix-multiplicationpseudocode

What is a "for to by" loop?


I am trying to do this matrix multiplication assignment for my parallel programming class, but I don't understand what my professor means with this "for to by" loop he has in the pseudocode. More specifically, why is the "by" there and how do I translate it into code? I am using C.

Here is the loop that he wants me to code as a part of this assignment: variables A, B, and C are 2D arrays that I defined. n is the size of the two n by n matrices being multiplied together, and I do not know what the variable s is supposed to be after the "by" statement.

//Loop 3
for it = 1 to n by s do
for kt = 1 to n by s do
for jt = 1 to n by s do
 for i = it to min(it+s-1, n) do
  for k = kt to min(kt+s-1, n) do
   for j = jt to min(jt+s-1, n) do
    C[i,j] = C[i,j] + A[i,k] x B[k,j]
   endfor
  endfor
 endfor
endfor
endfor
endfor

Solution

  • by is the amount to increment the iteration variable each time. In C you would use it += s. This is called the step or stride.

    for (int it = 1; it <= n; it += s) { // for it = 1 to n by s do
        // body of loop
    }
    

    If you leave out by s it's assumed to be incrementing by 1, and you would use it++.

    Note that in C array indexes start at 0, not 1. So the loop would actually be

    for (int it = 0; it < n; it += s)