1

Why is adding a sparse and a full matrix slower than adding two full matrices? The following code demonstrates consistent superior performance for hFullAddFull.

I_FULL = 600;
J_FULL = 10000;
FULL_COUNT = I_FULL*J_FULL;
NON_ZERO_ELEMENT_COUNT = 1000;

nonZeroIdxs = randsample(FULL_COUNT, NON_ZERO_ELEMENT_COUNT);
mat_Sp = spalloc(I_FULL, J_FULL, NON_ZERO_ELEMENT_COUNT);
mat_Sp(nonZeroIdxs) = 0.5;
mat_Full = full(mat_Sp);

otherMat_Full = rand(I_FULL, J_FULL);

hFullAddSp = @()otherMat_Full+mat_Sp;
hFullAddFull = @()otherMat_Full+mat_Full;

timeit(hFullAddSp)
timeit(hFullAddFull)

For me this is important as the addition occurs within a critical piece of code that is called 10,000s of times, so the small performance decrease for the sparse addition is critical. I would rather keep my code with the sparse type as input to the calculation and having the final matrix as a full matrix. Surely the 1000 elements of the full matrix can simply be modified? What is going on underneath here? Is there a way to make the sparse/full addition faster? Would a mex implementation be faster?

On my machine hFullAddFull is 30% faster.

rnoodle
  • 534
  • 2
  • 5
  • 21
  • This question has the same outcome https://stackoverflow.com/questions/35875823/adding-a-sparse-vector-to-a-dense-vector-in-matlab – rnoodle Apr 20 '18 at 00:17
  • Surely if the number of sparse elements are small, then adding them to the full matrix in-place would be efficient? – rnoodle Apr 20 '18 at 11:30
  • 1
    Honestly, it might just be considered an unimportant case and not optimized as much as other operations. It is indeed possible, as suggest in the linked question, that the sparse matrix is converted to a full matrix before the operation. What happens internally can only be guessed at. – Cris Luengo Apr 20 '18 at 12:35
  • @Cris Luengo Agree, but multiplication doesn't seem to suffer this performance impediment when used in the same context?! – rnoodle Apr 20 '18 at 12:56
  • Right. But those are different cases, no? They would need different code. As a company, you need to decide whether it's worth the investment to write good code for each case or to write cheaper, slower code. They maybe decided that sparse+sparse and full+full are important, and sparse+full is not. But again, it's all just guesswork from here. :) – Cris Luengo Apr 20 '18 at 13:04
  • "I would rather keep my code with the sparse type as input to the calculation and having the final matrix as a full matrix." But you also want efficiency, which your post indicates should use all fulls and avoid the overhead of sparse-to-full operations. One thing that *may* help is embedding the calculation in a function with an output and input with the same name for the full matrix in an attempt to do memory in-place updating. Extreme guess on my part though. – TroyHaskin Apr 21 '18 at 07:37

0 Answers0