Reputation: 844
I am doing it in C++11 or newer for application involving matrix-matrix multiplication running on single machine.
The size of the 2d matrix needs to be determined during runtime for matrices of different sizes. So it cant be hard-coded as global variable.
Here is a list of potential options I can think of:
1. std::array
2. std::vector
3. statically-allocated array on stack
4. dynamically-allocated array on heap(with new?)
5. use Eigen built-in matrix representation from the beginning(avoid casting back and forth when using option 1- option 4 )
Follow-up: what if the size of the matrices can be fitted into stack? I am thinking about option 3 since it is faster than option 4 when there are multiple memory allocations are needed.
Upvotes: 0
Views: 216
Reputation: 317
Statically allocated array works best since memory allocation is relatively expensive. In practice, it is still okay to use a heap-allocated array for that purpose if you can reuse it without needing further freeing and allocation.
Also, if you want memory access performance, you should also care about CPU caches. You will have more chances of performance gain if you have a matrix that is cache line aligned this is to prevent a write to one data variable invalidating the cache line that also contains another variable used by a different thread.
Memory access pattern should also be taken with care, e.g: organizing data according to how it is traversed in various stages of a program in order to maximize the locality of reference.
In general, you should still do benchmarks and compare your results with other methods.
Upvotes: 1