2D array problem...

Hi!

I make this topic because I’m having a problem trying to implement a 2D array so that I can use it on the device.

The problem goes like this:

I need to use this structure: struct neighbor{
float3 neighbor1; --> coordinates of the vertex corresponding to the row index;
float3 neighbor2; --> the coordinates of the vertex2 and vertex3 wich makes a triangle;
float3 neighbor3;
};

so that in each member of the matrix has a structure like that.

I think that implementing it on the host it’s not that difficult, but the problem is how to get it to the device after…

Thanks in advance to anyone that replies.

Why is that gonna be a problem?

If you cudaMemcpy() this to the device it should work.

like:

``````neighbour * h_neighbour = new h_neighbour[NUM_NEIGHBOURS];

//fill h_neighbour with valid data

neighbour * d_neighbours = 0;

cudaMalloc(d_neighbours, sizeof(neighbour) * NUM_NEIGHBOURS);

cudaMemcpy(d_neighbour, h_neighbour, sizeof(neighbour) * NUM_NEIGHBOURS, cudaMemcpyHostToDevice);
``````

Hi edisongustavo!

the main problem, that I’m not figuring out is how to fill the 2D array with data, since i’m declaring it as 1D array “cudaMalloc(d_neighbours, sizeof(neighbour) * NUM_NEIGHBOURS);” the array will have 2903 rows and 16 columns, and I don’t know hot put this on the memory.

Obrigado pelo post na mesma! Or thank you for the post (if you don’t know portuguese)

You can view a 2D array like this:

A B C
D E F
G H I

Or like this, which it most probably looks like in memory:

A B C D E F G H I

I’m not 100% sure I understand your problem correctly, but this may help.

Hi kristleifur

I’ve already saw this, and I think it will be part of the solution, the main problem, and of course it is very simple (only I can’t figure it out) is how to access it like a matrix. How can I access it using the indexes so I can store in the matrix like this:

for(int i=0;i<numvertex;i++){

``````	             for(int j=0;j<16;j++){

matrix[i+j].neighbor1=make_float3(x,y,z);

^^^

the problem is here

}

}``````

You can make a 1D array look like a 2D array like kristle said, inside the 2nd for you can access it like this:

``````for (int i = 0; i < rows; ++i) {

for (int j = 0; j < cols; ++j) {

unsigned int index = i * cols + j;
``````

This wikipedia article explains this kind of access:

http://en.wikipedia.org/wiki/Row-major_order

PS.: Sim, eu entendo portuguÃªs, sou brasileiro :)