Allocating memory for structure of 2D arrays on device

Hi everyone,

I want to allocate memory on device for structure of 2D array. The structure and memory allocation code is as follows:

struct Cell
{
unsigned short** R;
unsigned short** G;
unsigned short** B;
unsigned short** Count;
};

Cell* BGM;

size_BGM = WidthHeightsizeof(unsigned short);

cudaMalloc((void **)((BGM->R)), 4sizeof(unsigned short));
for (int j=0; j<4; j++) cudaMalloc((void **)(&(BGM->R[j])), size_BGM);
cudaMalloc((void **)((BGM->G)), 4sizeof(unsigned short));
for (int j=0; j<4; j++) cudaMalloc((void **)(&(BGM->G[j])), size_BGM);
cudaMalloc((void **)((BGM->B)), 4sizeof(unsigned short));
for (int j=0; j<4; j++) cudaMalloc((void **)(&(BGM->B[j])), size_BGM);
cudaMalloc((void **)((BGM->Count)), 4sizeof(unsigned short));
for (int j=0; j<4; j++) cudaMalloc((void **)(&(BGM->Count[j])), size_BGM);

But when I pass pointer BGM to cuda kernel and access using BGM->R[i][j], it gives “Warning: Cannot tell what pointer points to, assuming global memory space”, and on execution gives segmentation fault.

Any help would be highly appreciated as I’ve got stuck with this.

Hi everyone,

I want to allocate memory on device for structure of 2D array. The structure and memory allocation code is as follows:

struct Cell
{
unsigned short** R;
unsigned short** G;
unsigned short** B;
unsigned short** Count;
};

Cell* BGM;

size_BGM = WidthHeightsizeof(unsigned short);

cudaMalloc((void **)((BGM->R)), 4sizeof(unsigned short));
for (int j=0; j<4; j++) cudaMalloc((void **)(&(BGM->R[j])), size_BGM);
cudaMalloc((void **)((BGM->G)), 4sizeof(unsigned short));
for (int j=0; j<4; j++) cudaMalloc((void **)(&(BGM->G[j])), size_BGM);
cudaMalloc((void **)((BGM->B)), 4sizeof(unsigned short));
for (int j=0; j<4; j++) cudaMalloc((void **)(&(BGM->B[j])), size_BGM);
cudaMalloc((void **)((BGM->Count)), 4sizeof(unsigned short));
for (int j=0; j<4; j++) cudaMalloc((void **)(&(BGM->Count[j])), size_BGM);

But when I pass pointer BGM to cuda kernel and access using BGM->R[i][j], it gives “Warning: Cannot tell what pointer points to, assuming global memory space”, and on execution gives segmentation fault.

Any help would be highly appreciated as I’ve got stuck with this.

Every pointer you supply to cudaMalloc() must be a pointer in host memory. Right now you are mixing host and device pointers all through that code.

Every pointer you supply to cudaMalloc() must be a pointer in host memory. Right now you are mixing host and device pointers all through that code.