Why is my Strassen's Matrix Multiplication slow?

前端 未结 2 646
温柔的废话
温柔的废话 2020-12-28 19:33

I wrote two Matrix Multiplications programs in C++: Regular MM (source), and Strassen\'s MM (source), both of which operate on square matrices of sizes 2^k x 2^k(in other wo

2条回答
  •  不思量自难忘°
    2020-12-28 20:15

    So, there may be more problems that this, but your first problem is that you're using arrays of pointers to arrays. And since you're using array sizes that are powers of 2, this is an especially big performance hit over allocating the elements contiguously and using integer division to fold the long array of numbers into rows.

    Anyway, that's my first guess as to a problem. As I said, there may be more, and I'll add to this answer as I discover them.

    Edit: This likely only contributes a small amount to the problem. The problem is likely the one Luchian Grigore refers to involving cache line contention issues with powers of two.

    I verified that my concern is valid for the naive algorithm. The time for the naive algorithm goes down by almost 50% if the array is contiguous instead. Here is the code for this (using a SquareMatrix class that is C++11 dependent) on pastebin.

提交回复
热议问题