0

Consider as an example the matrix

X(a,b) = [a b
          a a]

I would like to perform some relatively intensive matrix algebra computations with X, update the values of a and b and then repeat.

I can see two ways of storing the entries of X:

1) As numbers (i.e. floats). Then after our matrix algebra operation we update all the values in X to the correct values of a and b.

2) As pointers to a and b, so that after updating them, the entries of X are automatically updated.

Now, I initially thought method (2) was the way to go as it skips out the updating step. However I believe that using method (1) allows a better use of the cache when doing for example matrix multiplication in parallel (although I am no expert so please correct me if I'm wrong).

My hypothesis is that for unexpensive matrix computations you should use method (2) and there will be some threshold as the computation becomes more complex that you should switch to (1).

I imagine this is not too uncommon a problem and my question is which is the optimal method to use for general matrices X?

rwolst
  • 12,904
  • 16
  • 54
  • 75

1 Answers1

0

Neither approach sounds particularly hard to implement. The simplest answer is make a test calculation, try both, and benchmark them. Take the faster one. Depending on what sort of operations you're doing (matrix multiplication, inversion, etc?) you can potentially reduce the computation by simplifying the operations given the assumptions you can make about your matrix structure. But I can't speak to that any more deeply since I'm not sure what types of operations you're doing.

But from experience, with a matrix that size, you probably won't see a performance difference. With larger matrices, you will, since the CPU's cache starts to fill. In that case, doing things like separating multiplication and addition operations, pointer indexes, and passing inputs as const enable the compiler to make significant performance enhancements.

See Optimized matrix multiplication in C and Cache friendly method to multiply two matrices

Community
  • 1
  • 1
WakkaDojo
  • 431
  • 3
  • 7