OpenCL:错误使用未声明的标识符

OpenCL: error use of undeclared identifier

我正在尝试在我的 opencl 内核中编写条件语句。代码在图片中。如果我删除内部条件语句,代码运行正常,否则我会收到以下错误:

<program source>:157:23: warning: unused variable 'pyramid_1'
     float pyramid_1 = (largest_0 - largest_1) * distance_bw_x_coord * half_diff / 2 / 3;
           ^
<program source>:158:23: warning: unused variable 'pyramid_2'
     float pyramid_2 = (smallest_1 - smallest_0) * distance_bw_x_coord * half_diff /2 / 3;
           ^
<program source>:161:23: warning: unused variable 'pyramid_1'
     float pyramid_1 = distance_bw_x_coord * (largest_1 - smallest_1) * half_diff / 3;
           ^
<program source>:162:23: warning: unused variable 'pyramid_2'
     float pyramid_2 = half_diff * (largest_0 - smallest_1) * distance_bw_x_coord / 2 / 3;
           ^
<program source>:165:23: warning: unused variable 'pyramid_1'
     float pyramid_1 = distance_bw_x_coord * (largest_1 - largest_0) * half_diff / 3;
           ^
<program source>:166:23: warning: unused variable 'pyramid_2'
     float pyramid_2 = half_diff * (smallest_1 - smallest_0) * distance_bw_x_coord / 2 / 3;
           ^
<program source>:169:23: warning: unused variable 'pyramid_1'
     float pyramid_1 = (largest_0 - largest_1) * distance_bw_x_coord * half_diff / 2 / 3;
           ^
<program source>:170:23: warning: unused variable 'pyramid_2'
     float pyramid_2 = distance_bw_x_coord * (smallest_0 - smallest_0) * half_diff / 3;
           ^
<program source>:173:23: warning: unused variable 'pyramid_1'
     float pyramid_1 = half_diff * (largest_0 - smallest_0) * distance_bw_x_coord / 2 / 3;
           ^
<program source>:174:23: warning: unused variable 'pyramid_2'
     float pyramid_2 = distance_bw_x_coord * (largest_1 - smallest_1) * half_diff / 3;
           ^
<program source>:177:23: warning: unused variable 'pyramid_1'
     float pyramid_1 = distance_bw_x_coord * (largest_1 - largest_0) * half_diff / 3;
           ^
<program source>:178:23: warning: unused variable 'pyramid_2'
     float pyramid_2 = distance_bw_x_coord * (smallest_0 - smallest_1) * half_diff / 3;
           ^
<program source>:180:78: error: use of undeclared identifier 'pyramid_1'
     total_area += trapezoid_prism_vol + 2 (triangular_prism_vol + pyramid_1 + pyramid_2);    
                                                                   ^
<program source>:180:90: error: use of undeclared identifier 'pyramid_2'
     total_area += trapezoid_prism_vol + 2 (triangular_prism_vol + pyramid_1 + pyramid_2);    
                                                                               ^

This is the code

OpenCL 使用 C 的作用域规则。这意味着如果您在 { ... } 块中声明一个变量,则该变量仅存在于该块中。

因此,编写您尝试编写的代码类型的正确方法是:

if (whatever)
{
    float pyramid_1; // declare the variable here
    if (condition1) {
        pyramid_1 = expression1; // assign a value here…
    }
    else if (condition2) {
    {
        pyramid_1 = expression2; // …and here…
    }
    // etc.

    /* You can use the result of computing pyramid_1's value here, as
     * we're still inside the same { } block it was declared in. */

    // …
}

确保删除 if/else if 块中的 float 类型说明符以生成这些语句 assignments。如果不这样做,您最终会声明新变量 ,其名称 与在这些块外声明的变量同名。 (这称为阴影,通常是个坏主意 - 在这种情况下,外部变量实际上永远不会更新。)