I am trying to write a conditional statement in my opencl kernel. The code is in the image. If I remove the inner conditional statements the code runs fine, but otherwise I get the following error:
<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);
^
OpenCL uses C's scoping rules. This means that if you declare a variable inside a { ... }
block, the variable only exists within this block.
So a correct way to write the type of code you are attempting to write is:
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. */
// …
}
Make sure to remove the float
type specifier inside your if
/else if
blocks to make those statements assignments. If you don't, you just end up declaring new variables with the same name as the one declared outside those blocks. (This is called shadowing and usually a bad idea - in this case, the outer variable would never actually be updated.)