How to use texture mem? The code from programming guide failed on my laptop

Is the texture available on nv device? how about on emulator?

my laptop does not have a nv device, so I use “nvcc -deviceemu xxx.cu”

my code is:

global void kernel(int*);

texture<int, 1> tex;

int main(void)

{

int x[4]={1,2,3,4}, y[4];

int *h_x=&x[0], *h_y=&y[0];

int *d_x, *d_y;

cudaMalloc((void**) &d_x, 4*sizeof(int));

cudaMalloc((void**) &d_y, 4*sizeof(int));

cudaArray* cu_array;

// Allocate array

cudaChannelFormatDesc description = cudaCreateChannelDesc<int>();

cudaMallocArray(&cu_array, &description, 4, 1);

// Copy image data to array

cudaMemcpy(cu_array, h_x, 4*sizeof(int), cudaMemcpyHostToDevice);

// Bind the array to the texture

cudaBindTextureToArray(tex, cu_array);

int dimBlock=4;

kernel<<< 1, dimBlock >>>(d_y);

cudaMemcpy(h_y, d_y, 4*sizeof(int), cudaMemcpyDeviceToHost);



int i;

printf("\nby gpu:\n");

for(i = 0; i < 4; i++)

	printf("%f ", h_y[i]);

printf("\n");

cudaUnbindTexture(tex);

//free(h_data); free(h_indices); free(h_x); free(h_y);

//cudaFree(d_data); cudaFree(d_indices); cudaFree(d_x); cudaFree(d_y);

return 0;

}

global void kernel(int* odata)

{

unsigned int x = threadIdx.x;

int c = tex1D(tex, x);

odata = c;

}

however, the output is 0 0 0 0. what i expect is 1 2 3 4

is here any error? but i can succeed compiling it.

You cannot copy data to or from array using cudaMemcpy.