让我们回顾一下文档的内容cudaMemcpy3D
:
范围字段定义元素中传输区域的尺寸。如果 CUDA 数组参与复制,则根据该数组的元素定义范围。如果没有 CUDA 数组参与复制,则范围在 unsigned char 的元素中定义。
以及类似的cudaMalloc3DArray
注释文档:
所有值都在元素中指定
因此,您需要为两个调用形成的范围需要在元素中具有第一个维度(因为其中一个分配cudaMemcpy3D
是一个数组)。
但是您的代码中可能存在不同的问题,因为您正在d_volumeMem
使用cudaMalloc
. cudaMemcpy3D
预计线性源内存已分配有兼容的间距。您的代码只是使用大小的线性分配
SIZE_X*SIZE_Y*SIZE_Z*sizeof(float)
现在,您选择的尺寸可能会为您正在使用的硬件产生兼容的间距,但不能保证它会这样做。我也建议使用cudaMalloc3D
分配线性源内存。围绕您的小代码片段构建的扩展演示可能如下所示:
#include <cstdio>
typedef float VolumeType;
const size_t SIZE_X = 8;
const size_t SIZE_Y = 8;
const size_t SIZE_Z = 8;
const size_t width = sizeof(VolumeType) * SIZE_X;
texture<VolumeType, cudaTextureType3D, cudaReadModeElementType> tex;
__global__ void testKernel(VolumeType * output, int dimx, int dimy, int dimz)
{
int tidx = threadIdx.x + blockIdx.x * blockDim.x;
int tidy = threadIdx.y + blockIdx.y * blockDim.y;
int tidz = threadIdx.z + blockIdx.z * blockDim.z;
float x = float(tidx)+0.5f;
float y = float(tidy)+0.5f;
float z = float(tidz)+0.5f;
size_t oidx = tidx + tidy*dimx + tidz*dimx*dimy;
output[oidx] = tex3D(tex, x, y, z);
}
inline void gpuAssert(cudaError_t code, char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
template<typename T>
void init(char * devPtr, size_t pitch, int width, int height, int depth)
{
size_t slicePitch = pitch * height;
int v = 0;
for (int z = 0; z < depth; ++z) {
char * slice = devPtr + z * slicePitch;
for (int y = 0; y < height; ++y) {
T * row = (T *)(slice + y * pitch);
for (int x = 0; x < width; ++x) {
row[x] = T(v++);
}
}
}
}
int main(void)
{
VolumeType *h_volumeMem, *d_output, *h_output;
cudaExtent volumeSizeBytes = make_cudaExtent(width, SIZE_Y, SIZE_Z);
cudaPitchedPtr d_volumeMem;
gpuErrchk(cudaMalloc3D(&d_volumeMem, volumeSizeBytes));
size_t size = d_volumeMem.pitch * SIZE_Y * SIZE_Z;
h_volumeMem = (VolumeType *)malloc(size);
init<VolumeType>((char *)h_volumeMem, d_volumeMem.pitch, SIZE_X, SIZE_Y, SIZE_Z);
gpuErrchk(cudaMemcpy(d_volumeMem.ptr, h_volumeMem, size, cudaMemcpyHostToDevice));
cudaArray * d_volumeArray;
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<VolumeType>();
cudaExtent volumeSize = make_cudaExtent(SIZE_X, SIZE_Y, SIZE_Z);
gpuErrchk( cudaMalloc3DArray(&d_volumeArray, &channelDesc, volumeSize) );
cudaMemcpy3DParms copyParams = {0};
copyParams.srcPtr = d_volumeMem;
copyParams.dstArray = d_volumeArray;
copyParams.extent = volumeSize;
copyParams.kind = cudaMemcpyDeviceToDevice;
gpuErrchk( cudaMemcpy3D(©Params) );
tex.normalized = false;
tex.filterMode = cudaFilterModeLinear;
tex.addressMode[0] = cudaAddressModeWrap;
tex.addressMode[1] = cudaAddressModeWrap;
tex.addressMode[2] = cudaAddressModeWrap;
gpuErrchk(cudaBindTextureToArray(tex, d_volumeArray, channelDesc));
size_t osize = 64 * sizeof(VolumeType);
gpuErrchk(cudaMalloc((void**)&d_output, osize));
testKernel<<<1,dim3(4,4,4)>>>(d_output,4,4,4);
gpuErrchk(cudaPeekAtLastError());
h_output = (VolumeType *)malloc(osize);
gpuErrchk(cudaMemcpy(h_output, d_output, osize, cudaMemcpyDeviceToHost));
for(int i=0; i<64; i++)
fprintf(stdout, "%d %f\n", i, h_output[i]);
return 0;
}
您可以自己确认纹理读取的输出与主机上的原始源内存匹配。