c:期望一个声明
当我编译下面的代码时,出现错误:
(line 64) expected a statement
(line 71) warning: parsing restarts here after previous syntax error.
第 64 行对应于对应的 else
if (num_bins < blockDim.x * gridDim.x)
(我在下面的代码中注释了确切的行)。
在以前的帖子中,错误是 if 后面有一个不必要的分号,而我没有。此外,我所有的括号都匹配。是什么原因导致此错误以及如何修复它?
__global__ void histogram_shared_kernel(unsigned int *input, unsigned int *bins,
unsigned int num_elements,
unsigned int num_bins) {
__shared__ unsigned int shBins[num_bins];
int tx = blockDim.x * blockIdx.x + threadIdx.x;
int stride = blockDim.x * gridDim.x; //stride amount = total number of threads
//all threads handle blockDim.x * gridDim.x amount of elements
//initialize all threads to zero
//if the number of bins < number of threads, we just need an if statment
if (num_bins < blockDim.x * gridDim.x){
if (tx < num_elements){
shBins[tx] = 0;
} //end if
} //end if
__syncthreads();
//if the number of bins > number of threads, we need an if statment and a for loop
else{ //corresponds to line 64
if (tx < num_elements){
for (int bin = tx; bin < size; bin = bin + blockDim.x){
shBins[tx] = 0;
}
}
}//end else
__syncthreads();
//end initialization phase
//begin computation
while (tx < num_elements){
atomicAdd( &shBins[input[tx]], 1); //increment the corresponding bin by 1 (the 2nd arg)
tx += stride;
} //end filling in shBins
//wait for all other threads to finish
__syncthreads();
//now, merge results of each privatized copy
if (tx < num_bins){
atomicAdd(&(bins[tx]), shBins[tx]);
}//end if
}//end kernel
When I compile the below code, I get the error:
(line 64) expected a statement
(line 71) warning: parsing restarts here after previous syntax error.
line 64 corresponds to the else corresponding to the
if (num_bins < blockDim.x * gridDim.x)
(I commented the exact line in the code below ).
in previous posts, the error was an unnecessary semicolon after the if, which I do not have. further, all my brackets match. What is causing this error and how can I fix it?
__global__ void histogram_shared_kernel(unsigned int *input, unsigned int *bins,
unsigned int num_elements,
unsigned int num_bins) {
__shared__ unsigned int shBins[num_bins];
int tx = blockDim.x * blockIdx.x + threadIdx.x;
int stride = blockDim.x * gridDim.x; //stride amount = total number of threads
//all threads handle blockDim.x * gridDim.x amount of elements
//initialize all threads to zero
//if the number of bins < number of threads, we just need an if statment
if (num_bins < blockDim.x * gridDim.x){
if (tx < num_elements){
shBins[tx] = 0;
} //end if
} //end if
__syncthreads();
//if the number of bins > number of threads, we need an if statment and a for loop
else{ //corresponds to line 64
if (tx < num_elements){
for (int bin = tx; bin < size; bin = bin + blockDim.x){
shBins[tx] = 0;
}
}
}//end else
__syncthreads();
//end initialization phase
//begin computation
while (tx < num_elements){
atomicAdd( &shBins[input[tx]], 1); //increment the corresponding bin by 1 (the 2nd arg)
tx += stride;
} //end filling in shBins
//wait for all other threads to finish
__syncthreads();
//now, merge results of each privatized copy
if (tx < num_bins){
atomicAdd(&(bins[tx]), shBins[tx]);
}//end if
}//end kernel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您不能像您所做的
那样在 if 和 else 之间放置单独的语句,即没有 if 就没有单独的 else 语句。但该语句
将 if-else 语句拆分为两个单独的语句。
You may not place a separate statement between if and else like you are doing
That is there is no separate statement else without if. But this statement
splits the if-else statement into two separate statements.