I am trying to use CUDA textures for 2D interpolation, but I am having trouble with tex2D() function.
I am initializing my cuda texture object with this code:
// Allocate CUDA array in device memory
cudaChannelFormatDesc channelDesc =
cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat);
cudaArray_t cuArray;
cudaMallocArray(&cuArray, &channelDesc, width, height);
// Set pitch of the source (the width in memory in bytes o
const size_t spitch = width*sizeof(float);
// Copy data located at address h_data in host memory to device memory
cudaMemcpy2DToArray(cuArray, 0, 0, h_data, spitch, width * sizeof(float),
height, cudaMemcpyHostToDevice);
// Specify texture
struct cudaResourceDesc resDesc;
memset(&resDesc, 0, sizeof(resDesc));
resDesc.resType = cudaResourceTypeArray;
resDesc.res.array.array = cuArray;
// Specify texture object parameters
struct cudaTextureDesc texDesc;
memset(&texDesc, 0, sizeof(texDesc));
texDesc.addressMode[0] = cudaAddressModeWrap;
texDesc.addressMode[1] = cudaAddressModeWrap;
texDesc.filterMode = cudaFilterModeLinear;
texDesc.readMode = cudaReadModeElementType;
texDesc.normalizedCoords = 1;
// Create texture object
texObj = 0;
cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL);
cudaFreeArray(cuArray);
where h_data is a 1D array that has the information of the mathematical function I want to interpolate.
I tried to interpolate f(x,y)=x in a rectangle [0,10]x[0,20], with a grid of 32 x 128 points. Since I use normalized coordinates in the texture, the transformation of coordinates should be
[0,10]x[0,20] → [0,31]x[0,127] → [0,1]x[0,1]
If the grid of points is defined by
for(int i=0; i<32; i++){
for(int j=0; j<128; j++){
h_data[32*j+i]=10*i/31.0;
}
}
Then, to use texture interpolation in the interior points of the grid, one should do
__global__ void testTexture(cudaTextureObject_t tex, float* d_output){
for(int i=1; i<31; i++){
for(int j=1; j<127; j++){
float x=i/31.0;
float y=j/127.0;
d_output[32*j+i]=tex2D<float>(tex,x,y);
}
}
}
But doing this I get a systematic error of 0.0155 in every single point. What is happening?