我有两个 cuda 内核一个接一个地运行:
__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];
}
}
他们都使用共享内存。第二个有一个无符号整数数组作为共享内存。第一个有一个浮点数组。我认为我应该能够为两个数组重用相同的变量名 sdata,因为共享内存在每次内核启动后都会被清除,但我得到了错误:
declaration is incompatible with previous 'sdata'
如果我为每个内核使用不同的变量名,那似乎可以解决问题。任何人都知道为什么我不能重用相同的变量名?