Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling large matrices in C++

Tags:

c++

c++11

matrix

I am using large matrices of doubles in C++. I need to get rows or columns from these matrices and pass them to a function. What is the fastest way I can do this?

  1. One way is to write a function that returns a copy of the desired row or column as an std::vector.
  2. Another way is to pass the whole thing as a reference and modify the function to be able to read the desired values.

Are there any other options? Which one do you recommend?

BTW, how do you recommend I store the data in the matrix class? I am using std::vector< std::vector< double > > right now.

EDIT

I mus have mentioned that the matrices might have more that two dimensions. So using boost or arma::mat here is out of the question. Although, I am using armadillo in other parts of the library.

like image 224
Eliad Avatar asked Feb 10 '23 22:02

Eliad


1 Answers

If a variable number of dimensions above 2 is a key requirement, take a look at boost's multidimensional array library. It has efficient (copying free) "views" you can use to reference lower-dimensional "slices" of the full matrix.

The details of what's "fastest" for this sort of thing depends an awful lot on what exactly you're doing, and how the access patterns/working set "footprint" fit to your HW's various levels of cache and memory latency; in practice it can be worth copying to more compact representations to get more cache coherent access, in preference to making sparse strided accesses which just waste a lot of a cache line. Alternatives are Morton-order accessing schemes which can at least amortize "bad axis" effects over all axes. Only your own benchmarking on your own code and use-cases on your HW can really answer that though.

(Note that I wouldn't use Boost.MultiArray for 2 dimensional arrays - there are faster, better options for linear algebra / image processing applications - but for 3+ it's worth considering.)

like image 66
timday Avatar answered Feb 13 '23 03:02

timday