acco93
acco93

Reputation: 138

Large matrix creation in c++

I have to create the following global matrix

float m[20][2000][1024][200]

that will occupy approximately 26 GB of RAM and this is not a problem.

I'm able to do it in GNU\Linux operating systems. How can I do it in Windows? Why does Windows impose this very annoying array dimension limitation?

One solution could be to allocate a single array in the heap and compute the 1d-index but I would prefer if it was the compiler to do it.

It is a dense matrix used in a dynamic programming algorithm and for performance purposes I would prefer contiguous memory for caching.

Any idea?

== UPDATE==

I'd like to post a solution that I eventually found. A possibility is as follows:

float (*m)[2000][1024][200];
m = malloc (20 * sizeof *m);

Memory is contiguous and could be accessed using the matrix access way.

Upvotes: 2

Views: 1595

Answers (1)

One solution could be to allocate a single array and compute the 1d-index but I would prefer if it was the compiler to do it.

That is actually the good idea. Of course that array data should be allocated into heap (using operator new in C++, or malloc or calloc in C). and computing the offset from various indexes is easy.

You'll make that array some abstract data type; with C++ you might define your own class MyMatrix (but follow the rule of five) and you might build it above existing containers.

You probably should find a good existing matrix library. Some of them might even have optimization taking advantage of specific hardware (e.g. OpenMP or OpenCL based).

See also this answer for a C approach.

for performance purposes I would prefer contiguous memory for caching.

Such caching considerations only matter for the most inner loops of your computation. See also this.

Upvotes: 4

Related Questions