问题
I have two cuda kernels that are run, one after the other:
__global__
void calculate_histo(const float* const d_logLuminance,
unsigned int* d_histogram,
float min_logLum,
float lumRange,
int numBins,
int num_elements){
extern __shared__ float sdata[];
int tid = threadIdx.x;
int bid = blockIdx.x;
int gid = tid * blockDim.x + bid;
// load input into __shared__ memory
if(gid < num_elements)
{
sdata[tid] = d_logLuminance[gid];
__syncthreads();
//compute bin value of input
int bin = static_cast <int> (floor((d_logLuminance[gid]-min_logLum)/ lumRange * numBins));
//increment histogram at bin value
atomicAdd(&(d_histogram[bin]), 1);
}
}
__global__
void blelloch_scan(unsigned int* const d_cdf, unsigned int* d_histogram, int numBins) {
extern __shared__ unsigned int sdata[];// allocated on invocation
int thid = threadIdx.x;
//printf("%i \n", thid);
//printf("%i \n", d_histogram[thid]);
int offset = 1;
sdata[2*thid] = d_histogram[2*thid]; // load input into shared memory
sdata[2*thid+1] = d_histogram[2*thid+1];
// build sum in place up the tree
for (int d = numBins>>1; d > 0; d >>= 1) {
__syncthreads();
if (thid < d) {
int ai = offset*(2*thid+1)-1;
int bi = offset*(2*thid+2)-1;
sdata[bi] += sdata[ai];
}
offset *= 2;
}
if (thid == 0) { sdata[numBins - 1] = 0; } // clear the last element
// traverse down tree & build scan
for (int d = 1; d < numBins; d *= 2) {
offset >>= 1;
__syncthreads();
if (thid < d) {
int ai = offset*(2*thid+1)-1;
int bi = offset*(2*thid+2)-1;
float t = sdata[ai];
sdata[ai] = sdata[bi];
sdata[bi] += t;
}
__syncthreads();
d_cdf[2*thid] = sdata[2*thid]; // write results to device memory
d_cdf[2*thid+1] = sdata[2*thid+1];
}
}
They both use shared memory. The second has an unsigned int array as the shared memory. The first has a float array. I thought I should be able to reuse the same variable name, sdata, for both arrays, since shared memory is cleared after each kernel launch, but I'm getting the error:
declaration is incompatible with previous 'sdata'
If I use different variable names for each kernel, that seems to solve the problem. Anyone know why I can't reuse the same variable name?
回答1:
CUDA is just following the rule of the standard C language. Quoting the Kernighan and Ritchie "The C Programming Language" book:
An external variable must be defined, exactly once, outside of any function; this sets aside storage for it. The variable must also be declared in each function that wants to access it; this states the type of the variable. [...] Definition refers to the place where the variable is created or assigned storage; declaration refers to places where the nature of the variable is stated but no storage is allocated.
Somewhere in your program you should have something like
extern __shared__ unsigned int sdata[];
At that location, you are creating a pointer, named sdata
, to an unsigned int
. Inside the __global__
functions you are declaring the type of sdata
, so that the __global__
function can be aware of it. In the
kernel<<<blocks,threads,numbytes_for_shared>>>(...);
launch, you are allocating space of the array pointed to by sdata
.
来源:https://stackoverflow.com/questions/19338679/cuda-reuse-of-shared-memory-variable-name