4

I do not use any matrix library, but instead plain std::vector for my matrix data.

To fill it with 2D data I use this code:

data[iy + dataPointsY * ix] = value;

I would like to know is this is correct or if it must be the other way (ix first). To my understanding fftw needs 'Row-major Format'. Since I use it the formula should be according to row-major format.

Chris Drew
  • 14,926
  • 3
  • 34
  • 54
Matthias Pospiech
  • 3,130
  • 18
  • 55
  • 76
  • 4
    The key point is, they way you store the data should be exactly the way you retrieve. – phoxis Nov 04 '16 at 15:26
  • sure, but I know that other libs rely on row.major format. Since I use it I should order the data according to it. – Matthias Pospiech Nov 04 '16 at 15:27
  • If you are trying to encode the 2d indexing in a linear format which some other library expects, then you have to follow exactly the format specified by that library. Like MPI functions would require all the data elements of a matrix should be adjacent in memory. – phoxis Nov 04 '16 at 15:29
  • Also, if you traverse the matrix column wise, (that is top to bottom for one single column), then you should consider storing it in a column-major format, as in that case the elements would be adjacent, allowing large datasets being traversed in lesser page faults. – phoxis Nov 04 '16 at 15:30
  • The question is not whether this format is "correct". The question is what format does the fftw library expect. – Chris Drew Nov 04 '16 at 15:31
  • Let's look at how you access your data to find out: `data[y + Y * x]`. Your `y`s are sequential in memory and your `x`s are `Y` elements appart. I.e. for `data` to be row-major, `y` needs to index the column and `x` the row. – BeyelerStudios Nov 04 '16 at 15:46
  • Recommend wrapping that indexing math one way or another. If you keep typing it over and over sooner or later there will be a sucktastic and hard-to-spot typo. – user4581301 Nov 04 '16 at 17:24
  • This was an example. In most of my own code this is an inline function. – Matthias Pospiech Nov 04 '16 at 19:34

1 Answers1

3

Assuming you want row major format for fftw, what you want is:

data[ix + iy*dataPointsY]

The point of row-major is, when the combined index increased by 1, the corresponding row index would be same (assuming not overflowing to the next row).

double m[4][4];
mp = (double*)m;
mp[1+2*3] == m[2][1]; //true
mp[2+2*3] == m[2][2]; //true
mp[2+2*3] == m[3][1]; //false

In general, there's no "right" way to store a matrix. Row major format is also called "C-style" matrix, while column major is called "fortran-style" matrix. The naming is due to different multidimensional array indexing scheme between the two language.

Kh40tiK
  • 2,276
  • 19
  • 29