2

Im developing a small cuda lib, stuck by this annoying tex ref issue.

This is the sample code from Cuda C Programming Guide, Page43~44:

texture<float, cudaTextureType2D,cudaReadModeElementType> texRef;
textureReference* texRefPtr;
cudaGetTextureReference(&texRefPtr, "texRef");
cudaChannelFormatDesc channelDesc;
cudaGetChannelDesc(&channelDesc, cuArray);
cudaBindTextureToArray(texRef, cuArray, &channelDesc);

When i execute it, the line cudaGetTextureReference(...) returns the error code cudaErrorInvalidTexture. I couldn't find many other samples about cudaGetTextureReference on the internet, most of them are following the exact same procedure as the sample code above.

Frustrated, i tried the high-level API afterwards:

texture<float, cudaTextureType2D,cudaReadModeElementType> texRef;
cudaBindTextureToArray(texRef, cuArray);

same problem. If read from that texture in the kernel, zero values are all i get.

My full toy test code, 100% reproduce-rate: (win7, cuda 5.0)

texture<float, cudaTextureType2D, cudaReadModeElementType> texRef;

int main ()
{
    const textureReference *tref = NULL;
    checkSuccess( cudaGetTextureReference( &tref, "texRef" ) );
    pauseConsole();
    return 0;
}

Any insight for this problem would be greatly appreciated. Thanks

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Defd
  • 470
  • 3
  • 16

2 Answers2

5

Use of a string naming a variable as the symbol paramater was removed in CUDA 5.0.

Stated in CUDA Toolkit Reference Manual 5.0, Section 5.20.2.8 /Note.

Do the following instead:

cudaGetTextureReference( &tref, &texRef );
sgarizvi
  • 16,623
  • 9
  • 64
  • 98
3

Pass the symbol directly, not as a string. The string look method is a deprecated version of the API that was removed completely in CUDA 5. Your texture lookup should be written as

cudaGetTextureReference( &tref, &texRef );
talonmies
  • 70,661
  • 34
  • 192
  • 269
  • This looks unfriendly with c++ syntax. I get this: no suitable conversion function from "texture" to "const void *" exists. Can you elaborate a little more about the details? – Defd Mar 14 '13 at 11:54
  • 1
    @Defd: sorry, made a very small typo. The call requires a reference to the texture, not its value. I usually put a disclaimer if I haven't actually tried to compile and run any code I post. This time I didn't..... – talonmies Mar 14 '13 at 14:13
  • thank you for the fast clarification. Unfortunately stackoverflow only allows me to select one correct answer. sgar91's answer looks more canonical. I think it's better for the sake of a QA site like this. Anyway, thanks and kudos for you. – Defd Mar 15 '13 at 03:39