Skip to content

Commit

Permalink
Add a faster way to loop for matrix-matrix multiplication
Browse files Browse the repository at this point in the history
  • Loading branch information
yungyuc committed Mar 22, 2023
1 parent f9cdf5c commit 2a04c93
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions nsd/05cache/code/03_matrix_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,44 @@ Matrix multiply_indirect(Matrix const & mat1, Matrix const & mat2)
return ret;
}

/*
* Indirect naive matrix matrix multiplication but loop in a different order
* (i,j,k). It is much faster than the original order (i,k,j), but I do not
* know why (yet)!
*/
Matrix multiply_indirect_order1(Matrix const & mat1, Matrix const & mat2)
{
validate_multiplication(mat1, mat2);

Matrix ret(mat1.nrow(), mat2.ncol());

StopWatch sw;

for (size_t i=0; i<ret.nrow(); ++i)
{
for (size_t j=0; j<ret.ncol(); ++j)
{
ret(i,j) = 0;
}
}

for (size_t i=0; i<mat1.nrow(); ++i)
{
for (size_t j=0; j<mat1.ncol(); ++j)
{
for (size_t k=0; k<mat2.ncol(); ++k)
{
ret(i,k) += mat1(i,j) * mat2(j,k);
}
}
}

ret.elapsed() = sw.lap();
ret.nflo() = calc_nflo(mat1, mat2);

return ret;
}

/*
* Direct naive matrix matrix multiplication.
*/
Expand Down Expand Up @@ -536,6 +574,10 @@ Matrix time_tile(
{
runner = multiply_indirect;
}
else if ("indirect_order1" == tag)
{
runner = multiply_indirect_order1;
}
else if ("direct" == tag)
{
runner = multiply_direct;
Expand Down Expand Up @@ -579,6 +621,7 @@ int main(int argc, char ** argv)
Matrix mat_gold = time_tile("mkl", nullptr, mat1, mat2);

time_tile("indirect", &mat_gold, mat1, mat2);
time_tile("indirect_order1", &mat_gold, mat1, mat2);
time_tile("direct", &mat_gold, mat1, mat2);

time_tile<32>("", &mat_gold, mat1, mat2);
Expand Down

0 comments on commit 2a04c93

Please sign in to comment.