问题
I am trying to test the CUDA 1D texture with a piece of simple code. It is quite straightforward: first allocates a global memory, then bind it to a texture reference; access the texture from within a kernel by tex1D(); print out the value returned by the texture fetch.
The code is as follows:
#include "cuda.h"
#include "cuda_runtime.h"
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
texture<float, cudaTextureType1D, cudaReadModeElementType> texX;
__global__ void kernel(float *X)
{
int i = threadIdx.x ;
if ( i >= 128 ) return;
printf("%.3f\t%.3f\n", tex1D( texX, i*1.0 ), X[i] );
}
int main()
{
float *devX;
vector<float> X(128, 3.1415926 );
cudaMalloc( &devX, 128 * sizeof(float) );
cudaMemcpy( devX, &X[0], 128*sizeof(float), cudaMemcpyDefault );
cudaDeviceSynchronize();
cudaBindTexture( (size_t)0, texX, devX, 128 * sizeof(float) );
cudaDeviceSynchronize();
kernel<<<1,128>>>( devX );
cudaDeviceSynchronize();
cout<<endl;
cout<< cudaGetErrorString( cudaGetLastError() ) <<endl;
}
But all I got was like this:
0.000 3.142
0.000 3.142
...
0.000 3.142
no error
Could anyone explain why?
回答1:
You need to use tex1Dfetch()
since your texture is bound to linear memory:
printf("%.3f\t%.3f\n", tex1Dfetch( texX, i ), X[i] );
回答2:
From the " CUDA C Programming Guide"
tex1D is used to fetch from the CUDA array specified by the one-dimensional texture object ...
tex1Dfetch is used to fetch from the region of linear memory specified by the one-dimension texture ...
So, if you use cudaMalloc to malloc linear memory but not cuda array, u should select tex1Dfetch
来源:https://stackoverflow.com/questions/12409581/cuda-1d-texture-fetch-always-return-0