Sparse matrix construction is slow

Issue #252 closed
Chun Xun Lin created an issue

Hi, I am using blaze to create a sparse matrix from a set of tuples in a file. I follow Wiki to append elements row by row (reserve, append,finalize) but it is very slow. Is there anything wrong with my sparse matrix construction?

The code is here: https://bitbucket.org/clin99/image_reader/src/master/

Thanks!

Comments (3)

  1. Klaus Iglberger

    Hi Chun!

    The problem is the call to .reserve() within the for loop. Every call to .reserve() causes a memory allocation and with that an additional copy operation for all the data that has already been added to the matrix. You can significantly speed up the setup of the sparse matrix if you use a different constructor:

    int main()
    {
      // ...
    
      blaze::CompressedMatrix<double> mat(num_rows, num_cols, row_count);
    
      // Construct the sparse  matrix from tuples
      auto it = tuples.begin();
      for(auto i=0u; i<row_count.size(); i++) {
        if(row_count[i] > 0) {
          for(auto j=0u; j<row_count[i]; j++) {
            auto [r, c, v] = *it;
            mat.append(r, c, v);
            it ++;
          }
        }
        mat.finalize(i);
      }
    }
    

    This will configure the CompressedMatrix in a single step. There is exactly one memory allocation and no addition copy operations. I hope this helps,

    Best regards,

    Klaus!

  2. Chun Xun Lin reporter

    Hi Klaus,

    I have tried your solution and now the construction is fast! Thanks for your answering! 👍

    Best regards,

    Chun-Xun

  3. Log in to comment