I have been trying store a 2D array in texture memory and read from it via cudaBindTexture2D but the value returned is 0, but I'm not sure if this is the right use of cudaBindTexture2D, and tex2D();
I made a pretty simple code to try it out :
#include <cuda.h>
#include <stdio.h>
#include <stdlib.h>
texture<uint, cudaTextureType2D, cudaReadModeElementType> tex;
__global__
void texture2DTest(int *x){
*x = tex2D(tex,0,0);
}
void initTable(int textureTable[][9]){
int i=0;
int j=0;
for(i=0; i<10; i++){
for(j=0; j<9; j++){
textureTable[i][j]=0;
}
}
textureTable[0][0] = 12;
}
int main (int argc, char ** argv){
int textureTable[10][9];
int *d_x;
int x=2;
size_t pitch;
initTable(textureTable);
cudaMalloc(&d_x, sizeof(int));
cudaMemcpy(d_x, &x, sizeof(int), cudaMemcpyHostToDevice);
cudaMallocPitch( (void**)textureTable,&pitch, 9, 10);
cudaChannelFormatDesc desc = cudaCreateChannelDesc<uint>();
cudaBindTexture2D(NULL, tex, textureTable, desc, 9, 10, pitch) ;
texture2DTest<<<1,1>>>(d_x);
cudaThreadSynchronize();
cudaMemcpy(&x,d_x, sizeof(int), cudaMemcpyDeviceToHost);
printf(" \n %d \n",x);
cudaUnbindTexture(tex);
return 0;
}
Thank you.