Multidimensional array, cudaMalloc

Hello all,

I am trying to write a toy-code in CUDA. Part of this code allocates a struct (lets call it ftest) in GPU and does some multidimensional array allocation.

This code works fine in emulation mode, but when i try to run it on the GPU i get an allocation error from cudaMalloc.

[codebox]struct ftest

{

 float** ff;

};

    ftest *f;

ierr=cudaMalloc( (void**)&f,sizeof(ftest));

if (ierr!=cudaSuccess) Stop("Error 1");

ierr=cudaMalloc( (void**)&(f->ff),5*sizeof(float*));

if (ierr!=cudaSuccess) Stop("Error 2");[/codebox]

The error appears on the second malloc statement.

I think the reason is because f->ff already resides on device, whereas cudaMalloc requires a variable residing on host?

So if that is true, how can i bypass it?

If I allocate locally (ie. malloc on tempff** on host)and then memcpy (tempff to f->ff) will the pointers be valid?

Thanks in advance,

Giannis

I believe your conclusions are correct. As for a work-around… well… “How I Learned to Stop Worrying and Love Pointer Arithmetic”

I’m working on a code which uses 3D arrays on the CPU side. First thing on the GPU side has been to flatten all of these out. Remember, on the GPU, each layer of indirection is going to take a memory access hit, since the pointer arrays aren’t going to be cached.