简体   繁体   中英

how to use 2d array of variable size to work with Eigen?

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.

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, eg: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM