本文介绍了绑定CUDA纹理读数为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从纹理读取值,并将其写入回全局内存。我相信写作的部分作品,beause我可以把内核中的常量值,我可以看到他们的输出:

I try to read values from a texture and write them back to global memory.I am sure the writing part works, beause I can put constant values in the kernel and I can see them in the output:

__global__ void
bartureKernel( float* g_odata, int width, int height)
{
    unsigned int x = blockIdx.x*blockDim.x + threadIdx.x;
    unsigned int y = blockIdx.y*blockDim.y + threadIdx.y;

    if(x < width && y < height) {
            unsigned int idx = (y*width + x);
            g_odata[idx] = tex2D(texGrad, (float)x, (float)y).x;

    }
}

我想使用的纹理是2D浮纹双通道,所以我把它定义为:

The texture I want to use is a 2D float texture with two channels, so I defined it as:

texture<float2, 2, cudaReadModeElementType> texGrad;

而code的调用内核初始化一些恒定的非零值纹理:

And the code which calls the kernel initializes the texture with some constant non-zero values:

float* d_data_grad = NULL;

cudaMalloc((void**) &d_data_grad, gradientSize * sizeof(float));
CHECK_CUDA_ERROR;

texGrad.addressMode[0] = cudaAddressModeClamp;
texGrad.addressMode[1] = cudaAddressModeClamp;
texGrad.filterMode = cudaFilterModeLinear;
texGrad.normalized = false;

cudaMemset(d_data_grad, 50, gradientSize * sizeof(float));
CHECK_CUDA_ERROR;

cudaBindTexture(NULL, texGrad, d_data_grad, cudaCreateChannelDesc<float2>(), gradientSize * sizeof(float));

float* d_data_barture = NULL;
cudaMalloc((void**) &d_data_barture, outputSize * sizeof(float));
CHECK_CUDA_ERROR;

dim3 dimBlock(8, 8, 1);
dim3 dimGrid( ((width-1) / dimBlock.x)+1, ((height-1) / dimBlock.y)+1, 1);

bartureKernel<<< dimGrid, dimBlock, 0 >>>( d_data_barture, width, height);

我知道,纹理字节设置为所有50并没有太大的意义在花车的背景下,但它至少应该给我一些非零值阅读。

I know, setting the texture bytes to all "50" doesn't make much sense in the context of floats, but it should at least give me some non-zero values to read.

我只能读取零,但...

I can only read zeros though...

推荐答案

正在使用 cudaBindTexture 来你的纹理绑定到被分配的内存 cudaMalloc 。在内核使用的是 tex2D 函数读取纹理值。这就是为什么它是阅读零。

You are using cudaBindTexture to bind your texture to the memory allocated by cudaMalloc. In the kernel you are using tex2D function to read values from the texture. That is why it is reading zeros.

如果您绑定的质感,使用 cudaBindTexture ,它是使用 tex1Dfetch 在内核中读取线性内存。

If you bind texture to linear memory using cudaBindTexture, it is read using tex1Dfetch inside the kernel.

tex2D 仅用于从那些被绑定到纹理读间距线性内存(这是由分配使用cud​​aMallocPitch )使用功能 cudaBindTexture2D ,或者那些被绑定为 cudaArray 使用功能 cudaBindTextureToArray

tex2D is used to read only from those textures which are bound to pitch linear memory ( which is allocated by cudaMallocPitch ) using the function cudaBindTexture2D, or those textures which are bound to cudaArray using the function cudaBindTextureToArray

下面是基本表,休息可以从节目指南阅读:

Here is the basic table, rest you can read from the programming guide:

存储器类型 ----------------- 分配使用 ------------ ----- 绑定使用 ------------ 读取内核通过

Memory Type----------------- Allocated Using-----------------Bound Using-----------------------Read In The Kernel By

线性Memory...................<$c$c>cudaMalloc........................<$c$c>cudaBindTexture.............................<$c$c>tex1Dfetch

Linear Memory...................cudaMalloc........................cudaBindTexture.............................tex1Dfetch

间距线Memory.........<$c$c>cudaMallocPitch.............<$c$c>cudaBindTexture2D........................<$c$c>tex2D

Pitch Linear Memory.........cudaMallocPitch.............cudaBindTexture2D........................tex2D

cudaArray............................<$c$c>cudaMallocArray.............<$c$c>cudaBindTextureToArray.............<$c$c>tex1D tex2D

cudaArray............................cudaMallocArray.............cudaBindTextureToArray.............tex1D or tex2D

3D cudaArray......................<$c$c>cudaMalloc3DArray........<$c$c>cudaBindTextureToArray.............<$c$c>tex3D

3D cudaArray......................cudaMalloc3DArray........cudaBindTextureToArray.............tex3D

这篇关于绑定CUDA纹理读数为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 04:22