Bernardo
Bernardo

Reputation: 541

CUDA 3D matrix index

I have one matrix in CUDA which is 356x896x60.

In my program I am coding the three spatial coordinates like this:

voxel[threadIdx.x]= indexx+indexy*(DETECTOR_X_DIM)+indexz*DETECTOR_X_DIM*DETECTOR_Y_DIM;

Is this right? I have seen some topics on the matter that use x*dimx*dimy + y*dimx + z
So I am not really sure what is the right method

Upvotes: 1

Views: 1202

Answers (1)

jmsu
jmsu

Reputation: 2053

Depends how your matrix elements are laid out in memory. You are using x + y*dimx +z*dimx*dimy which is valid if your values were mapped to memory with something like:

index = 0;
for (z = 0; z<dimz; ++z)
    for(y = 0; y<dimy; ++y)
        for(x = 0; x<dimx; ++x) {
            matrix[index] = value;
            index++;
        }

If you imagine a cube made of many small cubes you walk along each line of cubes by changing x, then you switch lines with y and the layers of cubes with z.

Where did you see x*dimx*dimy + y*dimx + z? I can't think of a 3D matrix where you can use it correctly unless dimx=dimz. I don't think it is correct.

Upvotes: 3

Related Questions