How to use textures and arrays in CUDA?

How to use arrays of Textures in CUDA

  • I was trying to use an array of textures in CUDA but I'm getting an "invalid texture reference" error. Anyone has any idea how to implement array of textures? #include<iostream> #include<cstdio> #include<cstdlib> #define MAX_TEXTURE 134217728 #define MAX_LENGTH 268435456 using namespace std; texture<char, 1, cudaReadModeElementType> texref[2]; int main(){ // host variable char *text; // device variable char *dev_a; text = (char*)malloc(MAX_LENGTH*sizeof(char)); // any initial value for(int i=0; i<MAX_LENGTH; i++) text[i] = 'a'; cudaMalloc((void**)&dev_a, MAX_LENGTH*sizeof(char)); cudaMemcpy(dev_a, text, MAX_LENGTH*sizeof(char), cudaMemcpyHostToDevice); cudaError_t err = cudaBindTexture(0, texref[0], dev_a, MAX_TEXTURE*sizeof(char)); cout << cudaGetErrorString(err) << endl; size_t offset = MAX_TEXTURE; err = cudaBindTexture(&offset, texref[1], dev_a, MAX_TEXTURE*sizeof(char)); cout << cudaGetErrorString(err) << endl; cudaFree(dev_a); free(text); return 0; }

  • Answer:

    This is not possible. If available to you (Kepler or Maxwell gpu and cuda 5.0 or later), you should try http://devblogs.nvidia.com/parallelforall/cuda-pro-tip-kepler-texture-objects-improve-performance-and-flexibility/. In my code I faked the array by using a function to access various texture references with something similar to the following (in a header file): texture<char, 1, cudaReadModeElementType> tex0; texture<char, 1, cudaReadModeElementType> tex1; // ... texture<char, 1, cudaReadModeElementType> getTexture( int id ) { if( id == 0 ) return tex0; else if( id == 1 ) return tex1; // ... } As far as I know there is no way to avoid listing all the texture<char, 1, cudaReadModeElementType> texX; explicitly and be aware that texture references are declared at file scope. Probably you can improve my code snippet by using preprocessor macros...

Thiago Alexandre at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

I don't believe there is a way to have an array of texture references. You could have an array of texture objects if you are using hardware which supports it, but to the best of my (admittedly limited) knowledge, there is no way to do what you are asking about.

talonmies

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.