gpt4 book ai didi

cuda - 新的 CUDA 纹理对象——在 2D 情况下获取错误数据

转载 作者:行者123 更新时间:2023-12-02 00:09:19 24 4
gpt4 key购买 nike

在 CUDA 5.0 中,NVIDIA 添加了一个“纹理对象”(cudaTextureObject_t),使纹理更容易使用。以前,需要将纹理定义为全局变量。

我关注了 this NVIDIA example关于使用 cudaTextureObject_t .它适用于一维情况。我试图扩展这个例子来处理 2D 倾斜内存:

#define WIDTH 6
#define HEIGHT 2
int width = WIDTH; int height = HEIGHT;
float h_buffer[12] = {1,2,3,4,5,6,7,8,9,10,11,12};
float* d_buffer;
size_t pitch;
cudaMallocPitch(&d_buffer, &pitch, sizeof(float)*width, height);
cudaMemcpy2D(d_buffer, pitch, &h_buffer, sizeof(float)*width, sizeof(float)*width, height, cudaMemcpyHostToDevice);
printf("pitch = %d \n", pitch);

//CUDA 5 texture objects: https://developer.nvidia.com/content/cuda-pro-tip-kepler-texture-objects-improve-performance-and-flexibility
cudaResourceDesc resDesc;
memset(&resDesc, 0, sizeof(resDesc));
resDesc.resType = cudaResourceTypePitch2D;
resDesc.res.pitch2D.devPtr = d_buffer;
resDesc.res.pitch2D.pitchInBytes = pitch;
resDesc.res.pitch2D.width = width;
resDesc.res.pitch2D.height = height;
resDesc.res.pitch2D.desc.f = cudaChannelFormatKindFloat;
resDesc.res.pitch2D.desc.x = 32; // bits per channel
resDesc.res.pitch2D.desc.y = 32;
cudaTextureDesc texDesc;
memset(&texDesc, 0, sizeof(texDesc));
texDesc.readMode = cudaReadModeElementType;
cudaTextureObject_t tex;
cudaCreateTextureObject(&tex, &resDesc, &texDesc, NULL);

为了查看数据是否确实可以通过纹理缓存访问,我在这个内核中打印了几个字节:
__global__ void printGpu_tex(cudaTextureObject_t tex) {
int tidx = blockIdx.x * blockDim.x + threadIdx.x;
int tidy = blockIdx.y * blockDim.y + threadIdx.y;
if(tidx < WIDTH && tidy < HEIGHT){
float x = tex2D<float>(tex, tidy, tidx);
printf("tex2D<float>(tex, %d, %d) = %f \n", tidy, tidx, x);
}
}

我希望它的输出是“1,2,3,...,12”。但是,它打印“1,7,7,7,...3,9,...”:
tex2D<float>(tex, 0, 0) = 1.000000 
tex2D<float>(tex, 0, 1) = 7.000000
tex2D<float>(tex, 0, 2) = 7.000000
tex2D<float>(tex, 0, 3) = 7.000000
tex2D<float>(tex, 0, 4) = 7.000000
tex2D<float>(tex, 0, 5) = 7.000000
tex2D<float>(tex, 1, 0) = 3.000000
tex2D<float>(tex, 1, 1) = 9.000000
tex2D<float>(tex, 1, 2) = 9.000000
tex2D<float>(tex, 1, 3) = 9.000000
tex2D<float>(tex, 1, 4) = 9.000000
tex2D<float>(tex, 1, 5) = 9.000000

验证 d_buffer数据设置正确,我还为原始数据制作了“打印内核” d_buffer不使用纹理缓存的数组:
__global__ void printGpu_vanilla(float* d_buffer, int pitch) {
int tidx = blockIdx.x * blockDim.x + threadIdx.x;
int tidy = blockIdx.y * blockDim.y + threadIdx.y;
if(tidx < WIDTH && tidy < HEIGHT){
float x = d_buffer[tidy*pitch + tidx];
printf("d_buffer[%d][%d] = %f \n", tidy, tidx, x);
}
}

输出看起来不错(与纹理缓存版本不同):
d_buffer[0][0] = 1.000000 
d_buffer[0][2] = 2.000000
d_buffer[0][3] = 3.000000
d_buffer[0][4] = 4.000000
d_buffer[0][5] = 5.000000
d_buffer[0][5] = 6.000000
d_buffer[1][0] = 7.000000
d_buffer[1][6] = 8.000000
d_buffer[1][7] = 9.000000
d_buffer[1][8] = 10.000000
d_buffer[1][9] = 11.000000
d_buffer[1][5] = 12.000000

关于纹理缓存版本可能出现什么问题的任何想法?

下载:
  • Working sample code using cudaTextureObject_t for 1D textures
  • Broken sample code using cudaTextureObject_t for 2D textures (如上所述)
  • 最佳答案

    您的 cudaChannelFormatDescresDesc.res.pitch2D.desc错了:y应该是 0 .

    设置 FormatDesc正确使用 CreateChannelDesc<>()函数如 resDesc.res.pitch2D.desc = cudaCreateChannelDesc<float>();而不是手动设置。
    resDesc.res.pitch2D.desc.y = 32float2 有效质地。

    关于cuda - 新的 CUDA 纹理对象——在 2D 情况下获取错误数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16380883/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com