array of structs with arrays inside

Hi,

its possible to create an variable Array of structures. And in these structures should be also 1 variable length array.

I want to build up a tree, on which each node has to save an array.

Currently its all implemented in 1 array which is difficult to access, because of the many “sublevels”

Thx in advance

TT

has anybody an idea why this code doesnt work on sm_13?

device

__global__ void multi_array_kernel( int** arrays )

{

	int* ar = arrays[threadIdx.x];

	for(int i = 0; i < 10;i++)

	{

		ar[i] = 666;

	}

}

on host

int N = 20;

	int** h_array = new int*[N];

	int* d_array;

	cudaMalloc((void**)&d_array,sizeof(int*) * N);

	for(int i = 0; i < N; i++)

	{

		cudaMalloc((void**)h_array[i], 10 * sizeof(int));

	}

	cudaMemcpy(d_array, h_array, sizeof(int*) * N, cudaMemcpyHostToDevice);

	multi_array_kernel<1,N>(d_array);

	for(int i = 0; i < N; i++)

	{

		cudaFree(h_array[i]);

	}

	cudaFree(d_array);

	free(h_array);

error message

(102): error: operand types are incompatible ("void (*)(int **)" and "int")

(102): error: operand types are incompatible ("int" and "int *")

line 102: is the kernel call

even with an empty kernel i get this message

is this possible or not?

You have some incorrect pointer usage in the host code. d_array is a device pointer to an array of int. h_array is a host pointer to an array of device pointers. They cannot be used interchangeably, and d_array cannot be passed to your kernel function. Further to that, you seem to be assuming that sizeof(int) is the same as sizeof(int *). On 64 bit host platforms, that isn’t true.

HM I dont understand your post.

Why do you think iam assuming that sizeof(int) == sizeof(int*) ?

Is there a way to get it work? I though there is a way because in the forum its mentioned that you have to have to create each “layer” separate to create a structure which consist of pointers of pointers.

thanks for help

It might be worth doing a bit of reading up on pointers in C/C++ then, because until you can understand it, you are probably going to struggle in solving this problem.

Because that is what this code implies:

cudaMemcpy(d_array, h_array, sizeof(int*) * N, cudaMemcpyHostToDevice);

d_array is a device pointer to an array of int. h_array is a host array of device pointers (int *). The types and sizes of those two arrays are not necessarily equivalent.

Yes there is. You might find this post useful. Otherwise I am not sure I can explain it differently.

Yes you are right!!

Yes there is. You might find this post useful. Otherwise I am not sure I can explain it differently.

I want to have an array of pointers which each an array of int but the sizes of theses arrays are not equal and not constant so they can change. So construct one big array 1d/2d doesnt help. because if array[i] becomes double in size. I have to shift all the data behind the array[i]. Also i become so big(1024+) that I cannot create simple arrays. And if I have the right understanding your code is only using one array for all the same size atoms.

in c++ i would do something like this:

int arrayofmatrices** = new int*[N];

for(int i = 0; i < N; i++)

		*arrayofmatrices[i] = new int[rand()%100];