c:期望一个声明

发布于 01-14 16:42 字数 1884 浏览 1 评论 0原文

当我编译下面的代码时,出现错误:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘦怹2025-01-21 16:42:47

您不能像您所做的

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{ 
//...

那样在 if 和 else 之间放置单独的语句,即没有 if 就没有单独的 else 语句。但该语句

__syncthreads();

将 if-else 语句拆分为两个单独的语句。

You may not place a separate statement between if and else like you are doing

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{ 
//...

That is there is no separate statement else without if. But this statement

__syncthreads();

splits the if-else statement into two separate statements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文