I have a 3D image. I need to copy that image to cuda's GLOBAL M开发者_StackOverflow社区EMORY by the use of pointers. Currently I am doing as the following.. In the following implementation the array is a linear 1D array.
float *image = new float[noOfVoxels];
readImage(image) //one D linear array
int sizef = noOfVoxels*sizeof(float);
float *devI;
cudaMalloc((void**)&devI, sizef);
cudaMemcpy(devI, image,sizef, cudaMemcpyHostToDevice);
How can I allocate a 3D array in device memory??
3D array
float image[][][];
How are you planning to access the data once it's on the GPU?
If you're doing lots of random accesses and would benefit from spatial locality, then you should use cudaMalloc3D and bind it to a 3D texture.
If you're doing predictable, coalesced accesses, then linear memory indexing as you have it now is great.
Note that the memory of your PC is not in 3D. It's just the matter of visualization, so you can convert your 3D image into a single pointer. So why don't you keep the 3D image in the form on a single pointer on Host side.
accessing image3D[i][j][z] is same as image3D[ i*cols+j + rows*cols*z];
Now feed the singled-pointer image3D to CUDA.
You are best off using cudaMallocPitch()
. It still allocates memory as a single chunk i.e 1d which you must access by converting between 3d subcripts and a 1d index, but the benefit is that it allocates the memory in such a way as to optimize the alignment of the data types.
Alternatively cudaMalloc3D() will also return a pointer to pitched device memory
精彩评论