Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In-place conversion between row major and column major storage in three dimensions

Tags:

c++

arrays

I have a program that receives three-dimensional data as flat arrays in row-major (a.k.a. "C") order as input.

I need to pass these to a library that expects the same three-dimensional data in column-major (a.k.a. "Fortran") order.
Preprocessing the arrays outside of my program is not an option.

Transforming the data while copying is no problem except for performance - there are quite a few arrays of several million elements each, and the allocation and copying is my major bottleneck - so I would like to do the transformation in-place and see if that helps.

However, I have been unable to work out the maths behind this transformation, and my googling has been less than helpful.
Is there an efficient way to perform this transformation in-place?

like image 912
molbdnilo Avatar asked Dec 05 '25 08:12

molbdnilo


1 Answers

An in-place transformation (if possible) would copy all the elements of these big arrays anyway, thus it won't be cache-friendly.
Each allocation will be done once for a big array (and its subsequent long transformation) and if you have to deal with a stream of such arrays you could reuse old ones in order to avoid alloc/free repetitions.

I would simply recommend to load the data in the predictible/cache-friendly row-major order and rely on the store-buffer machinery to deal with the column-major store anti-pattern to the second (allocated) array.

like image 196
prog-fh Avatar answered Dec 07 '25 22:12

prog-fh