我在主机上执行以下代码时遇到问题,并且不确定哪里出错了。我的理解是,我需要分配足够的内存来存储 textData 的内容,该内容在设备上的大小为 textLength,我使用 sizeof(char) * textLength 进行 malloc,这应该给我足够的空间,然后我使用 cudaMemcpy 复制所有从 textData 到设备上分配的空间的值。我的异常读取“访问冲突读取位置 0x000000000501f80400”,这是设备上我的 cuTextArray 的内存地址。
texture<char, cudaTextureType1D, cudaReadModeElementType> textDataRef;
int textLength = 10000000
cudaArray *cuTextArray;
checkCudaErrors (cudaMalloc(&cuTextArray, sizeof(char)*textLength));
checkCudaErrors(cudaMemcpy(cuTextArray, textData, sizeof(char)*textLength, cudaMemcpyHostToDevice));
checkCudaErrors(cudaBindTextureToArray(textDataRef, cuTextArray, textDataRef.channelDesc));
我无法使用 cudaMallocArray,因为我的数据对于缓冲区来说太大 - 最大为 8192 字节
此处出现异常,将纹理绑定(bind)到 cuda_runtime.h 内的代码数组片段时似乎崩溃了:
最佳答案
事实证明,我可以通过使用带有引用的标准 char 指针和不同的绑定(bind)调用来使其工作,从而远离整个数组的废话。极其简单的事情,有点奇怪,它不容易回答。
//Allocate Memory
checkCudaErrors( cudaMalloc(&d_textData, sizeof(char) * textLength) );
//Copy Host To Device
checkCudaErrors( cudaMemcpy(d_textData, textData, sizeof(char)* textLength, cudaMemcpyHostToDevice));
//Bind reference to block of memory
checkCudaErrors ( cudaBindTexture(NULL,&texTextRef,d_textData,&texTextRef.channelDesc, sizeof(char) * textLength ) );
使用查找值
tex1Dfetch(texTextRef,k+index)
关于cudaMalloc运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29817755/