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);
^
This is the code
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.)
Related
I am a freshman in openmp. I have some trouble in a 3d sum, and I don't know how to improve my code. Here's the code I want to improve in openmp. My aim is to speed up the calculation of this 3d sum. What should I add in my code according to the rules of openmp?
I add #pragma omp parallel for reduction(+:integral) in my code. But an error happens which says the initialization of 'for' is not correct. This is the information of this error:enter image description here I am a chinese, so the language of my IDE is chinese. I use Visual Studio 2019.
#include<omp.h>
#include<stdio.h>
#include<math.h>
int main()
{
double a = 0.3291;
double d_title = 2.414;
double b = 3.8037;
double c = 4086;
double nu_start = 0;
double mu_start = 0;
double z_start = 0;
double step_nu = 2 * 3.1415926 / 100;
double step_mu = 3.1415926 / 100;
double step_z = 0;
double nu = 0;
double mu = 0;
double z = 0;
double integral=0;
double d_uv = 0;
int i = 0;
int j = 0;
int k = 0;
#pragma omp parallel for default(none) shared(a, d_title, b, c, nu_start, mu_start, z_start, step_nu, step_mu) private( j,k,mu, nu, step_z, z, d_uv) reduction(+:integral)
for (i = 0; i < 100; i++)
{
mu = mu_start + (i + 1) * step_mu;
for (j = 0; j < 100; j++)
{
nu = nu_start + (j + 1) * step_nu;
for (k = 0; k < 500; k++)
{
d_uv = (sin(mu) * sin(mu) * cos(nu) * cos(nu) + sin(mu) * sin(mu) * (a * sin(nu) - d_title * cos(nu)) * (a * sin(nu) - d_title * cos(nu)) + b * b * cos(mu) * cos(mu)) / (c * c);
step_z = 20 / (d_uv * 500);
z = z_start + (k + 1) * step_z;
integral = integral + sin(mu) * (1 - 3 * sin(mu) * sin(mu) * cos(nu) * cos(nu)) * exp(-d_uv * z) * log(1 + z * z) * step_z * step_mu * step_nu;
}
}
}
double out = 0;
out = integral / (c * c);
return 0;
}
Solutions (UPDATE: It is an answer to the original question:)
To do the least typing you just have to add the following line before for(int i=..)
#pragma omp parallel for private( mu, nu, step_z, z, d_uv) reduction(+:integral)
Here you define which variables have to be private to avoid data race. Note that variables are shared by default, so variable integral also shared, but all threads update its value, which is a data race. To avoid it, you have 2 possibilities: use atomic operation, or a much better option is to use use reduction (add reduction(+:integral) clause).
As you mentioned that you are beginner in OpenMP it is recommended to use default(none) clause in the #pragma omp parallel for directive, so you have to explicitly define sharing attributes. If you forget a variable you will get an error, so you have to consider all variables involved in your parallel region and can think about possible data races:
#pragma omp parallel for default(none) shared(a, d_title, b, c, nu_start, mu_start, z_start, step_nu, step_mu) private( mu, nu, step_z, z, d_uv) reduction(+:integral)
Generally, it is recommended to define your variables in their minimum required scope, so variables defined inside the for loop to parallelize will be private. In this case you just have to add #pragma omp parallel for reduction(+:integral) before your outermost for loop, so your code will be:
#pragma omp parallel for reduction(+:integral)
for (int i = 0; i < 100; i++)
{
double mu = mu_start + (i + 1) * step_mu;
for (int j = 0; j < 100; j++)
{
//int id = omp_get_thread_num();
double nu = nu_start + (j + 1) * step_nu;
for (int k = 0; k < 500; k++)
{
double d_uv = (sin(mu) * sin(mu) * cos(nu) * cos(nu) + sin(mu) * sin(mu) * (a * sin(nu) - d_title * cos(nu)) * (a * sin(nu) - d_title * cos(nu)) + b * b * cos(mu) * cos(mu)) / (c * c);
double step_z = 20 / (d_uv * 500);
double z = z_start + (k + 1) * step_z;
//int id = omp_get_thread_num();
integral = integral + sin(mu) * (1 - 3 * sin(mu) * sin(mu) * cos(nu) * cos(nu)) * exp(-d_uv * z) * log(1 + z * z) * step_z * step_mu * step_nu;
}
}
}
Runtimes: 44 ms (1 thread) and 11 ms (4 threads) on my computer (g++ -O3 -mavx2 -fopenmp).
Can anyone help me to calculate dew point from the temperature in both (°C or °F) with the relative humidity. I have searched lot on Google but not found any appropriate formula to calculate this. I am using the below formula but it gives wrong values in Fahrenheit unit.
float Temp = [value floatValue];
float Humi = [[data humidity] floatValue];
float Td = Temp - ((100 - Humi)/3.6);
return Td;
I have the temperature (°C or °F) and humidity and need help in formula to calculate dew point in objective C.
This should do the job. Below Code only take Celsius and Fahrenheit in account. To use other units of temperature, modify code accordingly;
-(float) findDewPointWithHumidity:(float) humidity forTemperature:(float) temperature isCelsius: (BOOL) isCelsius isReturnTypeCelcius: (BOOL) isReturnTypeCelcius
{
float temp;
if(isCelsius){
temp = temperature;
} else {
temp = (temperature - 32) / 1.8;
}
float humi = 34;
float ans = (temp - (14.55 + 0.114 * temp) * (1 - (0.01 * humi)) - pow(((2.5 + 0.007 * temp) * (1 - (0.01 * humi))),3) - (15.9 + 0.117 * temp) * pow((1 - (0.01 * humi)), 14));
if(isReturnTypeCelsius){
return ans; // returns dew Point in Celsius
}
return (temperature - 32) / 1.8; // returns dew Point in Fahrenheit
}
It works perfectly
-(float) findDewPointWithHumidity:(float) humi forTemperature:(float) temperature isCelsius: (BOOL) isCelsius isReturnTypeCelcius: (BOOL) isReturnTypeCelcius
{
float temp;
if(isCelsius){
temp = temperature;
} else {
temp = (temperature - 32) / 1.8;
}
float ans = (temp - (14.55 + 0.114 * temp) * (1 - (0.01 * humi)) - pow(((2.5 + 0.007 * temp) * (1 - (0.01 * humi))),3) - (15.9 + 0.117 * temp) * pow((1 - (0.01 * humi)), 14));
if(isReturnTypeCelcius){
return ans;
}
float value = ans*(9.0/5.0);
return value+32.0;
}
I am assigning a value to var with calculations but it shows syntax error i have seen all the brackets still it gives error here is my code .
appDelegate.Same_Vaccination_Cost_Treatment_Annually_With_Guarantee=((appDelegate.Same_Vaccination_Cost_Treatment_Annually_No_Guarantee
- 5000) / appDelegate.Same_Vaccination_Cost_Treatment_Annually_No_Guarantee) *
((appDelegate.Cost_Treatment_SevereEI *
appDelegate.Same_Vaccination_SevereEI_Annually) +
(appDelegate.Cost_Treatment_ModerateEI *
appDelegate.Same_Vaccination_ModerateEI_Annually) +
(appDelegate.Cost_Treatment_MildEI *
appDelegate.Same_Vaccination_MildEI_Annually)))) *
appDelegate.Same_Vaccination_Horses_Per_Premise;
As per Your code:-
float Same_Vaccination_Cost_Treatment_Annually_With_Guarantee;
float Cost_Treatment_SevereEI;
float Same_Vaccination_SevereEI_Annually;
float Cost_Treatment_MildEI;
float Same_Vaccination_MildEI_Annually;
float Same_Vaccination_Horses_Per_Premise;
float Same_Vaccination_Cost_Treatment_Annually_No_Guarantee;
float Cost_Treatment_ModerateEI;
float Same_Vaccination_ModerateEI_Annually;
Same_Vaccination_Cost_Treatment_Annually_With_Guarantee=(((Same_Vaccination_Cost_Treatment_Annually_No_Guarantee - 5000) / Same_Vaccination_Cost_Treatment_Annually_No_Guarantee) * (((Cost_Treatment_SevereEI * Same_Vaccination_SevereEI_Annually) + (Cost_Treatment_ModerateEI * Same_Vaccination_ModerateEI_Annually) + (Cost_Treatment_MildEI * Same_Vaccination_MildEI_Annually)) * Same_Vaccination_Horses_Per_Premise));
and you can also put Method like:-
Same_Vaccination_Cost_Treatment_Annually_With_Guarantee=((Same_Vaccination_Cost_Treatment_Annually_No_Guarantee - 5000) / Same_Vaccination_Cost_Treatment_Annually_No_Guarantee) * ((Cost_Treatment_SevereEI * Same_Vaccination_SevereEI_Annually) + (Cost_Treatment_ModerateEI * Same_Vaccination_ModerateEI_Annually) + (Cost_Treatment_MildEI * Same_Vaccination_MildEI_Annually)) * Same_Vaccination_Horses_Per_Premise;
If you are using exactly same
appDelegate.Same_Vaccination_Cost_Treatment_Annually_With_Guarantee=((appDelegate.Same_Vaccination_Cost_Treatment_Annually_No_Guarantee - 5000) / appDelegate.Same_Vaccination_Cost_Treatment_Annually_No_Guarantee) * ((appDelegate.Cost_Treatment_SevereEI * appDelegate.Same_Vaccination_SevereEI_Annually) + (appDelegate.Cost_Treatment_ModerateEI * appDelegate.Same_Vaccination_ModerateEI_Annually) + (appDelegate.Cost_Treatment_MildEI * appDelegate.Same_Vaccination_MildEI_Annually)))) * appDelegate.Same_Vaccination_Horses_Per_Premise;
then you have 2 braces extra
try
appDelegate.Same_Vaccination_Cost_Treatment_Annually_With_Guarantee=((appDelegate.Same_Vaccination_Cost_Treatment_Annually_No_Guarantee - 5000) / appDelegate.Same_Vaccination_Cost_Treatment_Annually_No_Guarantee) * ((appDelegate.Cost_Treatment_SevereEI * appDelegate.Same_Vaccination_SevereEI_Annually) + (appDelegate.Cost_Treatment_ModerateEI * appDelegate.Same_Vaccination_ModerateEI_Annually) + (appDelegate.Cost_Treatment_MildEI * appDelegate.Same_Vaccination_MildEI_Annually)) * appDelegate.Same_Vaccination_Horses_Per_Premise;
appDelegate.Same_Vaccination_Cost_Treatment_Annually_With_Guarantee=((appDelegate.Same_Vaccination_Cost_Treatment_Annually_No_Guarantee - 5000) / appDelegate.Same_Vaccination_Cost_Treatment_Annually_No_Guarantee) * ((appDelegate.Cost_Treatment_SevereEI * appDelegate.Same_Vaccination_SevereEI_Annually) + (appDelegate.Cost_Treatment_ModerateEI * appDelegate.Same_Vaccination_ModerateEI_Annually) + (appDelegate.Cost_Treatment_MildEI * appDelegate.Same_Vaccination_MildEI_Annually)) * appDelegate.Same_Vaccination_Horses_Per_Premise;
This will work. You were using too many brackets.
I have three CGpoint and I would like calculate the angle.
I drawn a little schema :
I tried with this code :
CGPoint u ;
u.x = 0;
u.y = - middleRectY;
CGPoint v ;
v.x = x1 - middelRectX;
v.y = y1 - middleRectY;
// formule = u.v / ( ||u|| * ||v||)
double cosa = (double)((u.x * v.x + u.y * v.y)) / sqrt(u.x * u.x + u.y * u.y) * sqrt(v.x * v.x + v.y * v.y);
// angle en degré
double angle = (180.0 / M_PI) * acos(cosa);
// Signe de l'angle
int sign = (u.x * v.y - u.y * v.x) > 0 ? 1 : -1;
rectYellow.transform = CGAffineTransformMakeRotation(angle*sign);
But my function return "nan" :/
Thx :)
I found the problem !
It just a probleme of parenthesis :
double cosa = ((u.x * v.x) + (u.y * v.y)) / (sqrt((u.x * u.x) + (u.y * u.y)) * sqrt((v.x * v.x) + (v.y * v.y)));
I don't understand why ?
Because the parentheses aren't necessary for multiplication ...
I have the following code, but in this line of code I have warning x[i] = (rhs[i] - x[i - 1]) / b;, compiler is telling me that rhs[i] is a garbage value. Why it's happend? And how to remove this warning?
double* getFirstControlPoints(double* rhs, const int n) {
double *x;
x = (double*)malloc(n * sizeof(double));
double *tmp; // Temp workspace.
tmp = (double*)malloc(n * sizeof(double));
double b = 2.0;
x[0] = rhs[0] / b;
for (int i = 1; i < n; i++) // Decomposition and forward substitution.
{
tmp[i] = 1 / b;
b = (i < n - 1 ? 4.0 : 3.5) - tmp[i];
x[i] = (rhs[i] - x[i - 1]) / b; //The left operand of '-' is a garbage value
}
for (int i = 1; i < n; i++) {
x[n - i - 1] -= tmp[n - i] * x[n - i]; // Backsubstitution.
}
free(tmp);
return x;
}
All compiler warnings and calling getFirstControlPoints you may see on screenshots.
You need a check to make sure you have at least 4 points in the points array because this loop (line 333):
for (NSInteger i = 1 ; i < n - 1 ; ++i) {
// initialisation stuff
}
will not execute at all for n = 0, 1, 2.
Assume that points has 3 objects in it, At line 311 you set n to the count - 1 i.e. n == 2
Then the loop condition is i < 2 - 1 i.e. i < 1.
I think you need the loop condition to be i < n
if points.count is 0 or 1 you are facing some problems, because then, n is -1 or 0, and you access rhs[n-1]; and you malloc n* bytes;
maybe that can be the problem. that you put some rangechecks int o the code?