Is there any way in CUDA to explicitly declare whether a pointer points to constant or shared memory space as opposed to the global device space?
Apparently nvcc tries to figure it out, but cannot always come to a conclusion. In CUDA 1.0, it silently assumed global device space. In CUDA 1.1beta, it warns about the situation and then assume global device space. This is clearly a step in the right direction. Unfortunately, even if you are not using any constant or shared data, these ambiguities arise, and I still gets lots of warnings, which now obfuscate any real problems.
So there are 2 problem:
(1) I can’t use constant or shared space when I want/need to.
(2) I can’t always make the compiler shut up even when I am using global space
If there’s a way to specify to qualify the type of the referenced data, that would solve both problems.
Below is small example. You can see the various combinations of problem by changing the two “#if 0” statements to “#if 1”.
Thanks for any insight.
#include <stdio.h>
#include <cuda.h>
#define CUDA_ERROR \
{ \
cudaError_t error; \
error = cudaGetLastError(); \
fprintf(stderr, "Cuda Error (%s:%d): %s\n", \
__FILE__, __LINE__, \
cudaGetErrorString(error)); \
}
typedef struct {
unsigned short a;
} type1;
typedef struct {
type1 **b;
type1 *c[1];
} type2;
#if 0
__device__ type1 table[2] = {
#else
__device__ __constant__ type1 table[2] = {
#endif
{0x5601},
{0x5601}
};
__device__ unsigned short test;
__device__ type2 glob;
__global__
void sample()
{
type2 *foo;
foo = &glob;
#if 1
for (int i=0; i<1; i++)
foo->c[i] = &table[0];
#else
foo->c[0] = &table[0];
#endif
foo->b = &(foo->c[0]);
test = (*foo->b)->a;
return;
}
int main()
{
int deviceCount;
cudaGetDeviceCount(&deviceCount);
if (deviceCount == 0) {
fprintf(stderr, "There is no device.\n");
exit(1);
}
int dev;
for (dev = 0; dev < deviceCount; ++dev) {
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, dev);
if (deviceProp.major >= 1)
break;
}
if (dev == deviceCount) {
fprintf(stderr, "There is no device supporting CUDA.\n");
exit(1);
}
else
{
cudaSetDevice(dev);
fprintf(stderr, "CUDA device is set.\n");
}
CUDA_ERROR;
// lauch grid
dim3 dimBlock(1, 1, 1);
dim3 dimGrid(1,1,1);
sample<<<dimGrid, dimBlock>>>();
CUDA_ERROR;
sample<<<dimGrid, dimBlock>>>();
CUDA_ERROR;
sample<<<dimGrid, dimBlock>>>();
CUDA_ERROR;
sample<<<dimGrid, dimBlock>>>();
CUDA_ERROR;
sample<<<dimGrid, dimBlock>>>();
CUDA_ERROR;
return 0;
}