We've successfully used the following post to help create structs that contain basic types like int *. Textures provide a nice performance boost for read-only arrays. We use many of them, which makes the argument lists for the kernels and the kernel sub-functions long and complicated. We'd like to embed the Textures in structures to reduce the argument length and complexity.
Copying a struct containing pointers to CUDA device
Here's a snippet representing the code methodology we use. It compiles, but crashes at run-time.
// Initialize texture description
memset(&textureDescription, 0, sizeof(textureDescription));
textureDescription.readMode = cudaReadModeElementType;
// Create Texture from variable
cudaTextureObject_t texture = 0;
cudaResourceDesc resource;
memset(&resource, 0, sizeof(resource));
resource.resType = cudaResourceTypeLinear;
resource.res.linear.devPtr = intArray;
resource.res.linear.desc.f = cudaChannelFormatKindSigned;
resource.res.linear.desc.x = 32; // bits per channel
resource.res.linear.sizeInBytes = count*sizeof(int);
cudaCreateTextureObject(&texture, resource, &textureDescription, NULL);
// These declarations are in the .h file
typedef struct SampleStructure {
cudaTextureObject_t texture;
} SampleStructure;
SampleStructure *structureHost;
SampleStructure *structureDevice;
// Create host and device structures
structureHost = (SampleStructure *)malloc(sizeof(SampleStructure));
cudaMalloc(&structureDevice, sizeof(SampleStructure));
// Assign the texture object to the host structure
structureHost->texture = texture;
// Copy the host structure to Global Memory
cudaMemcpy(structureDevice, structureHost, sizeof(SampleStructure), cudaMemcpyHostToDevice));
// Pass Texture and Texture-embedded-in-structure to kernel
kenerl<<<1,1>>>(texture, structureDevice);
...
__global__ void
kernel(cudaTextureObject_t texture, SampleStructure *structureDevice) {
value = tex1Dfetch<int>(texture, index); // Runs successfully at runtime
value = tex1Dfetch<int>(structureDevice->texture, index); // Crashes at runtime
}
When using the "texture" variable in the kernel code (or sub-function), it runs correctly. When using "structureDevice->texture" instead, it crashes at run-time.
Can someone show a simple code showing how to successfully embed a texture object in a struct that's passed to a kernel and runs without crashing? Or can someone point out where the mistake might be in the code that we've presented?