I'm working through the OMP tutorial on youtube where you are to write parallel code to compute an approximation to pi.
I'm getting an invalid controlling predicate error at the for loop line. Usually (other times this questions has been asked here) this is due to OMP being picky about for loops, it only accepts a basic structure.
I don't think my code is violating at OMP rules, and it's very similar to the code in the link (he has a typo in his, at the top, num+staps should be num_steps).
Does anyone know what's going on?
Note: to compile, you must add -fopenmp flag.
#include <iostream>
#include "omp.h"
using namespace std;
/* omp parallel for loop solution for computing pi as
pi = int_[0,1] 4/(1+x^2) dx */
int main()
{
int reqThreads = 2;
double panels = 10000000;
double pi = 3.141592653589793238462643;
double clockstart = omp_get_wtime();
double hx = 1.0/panels;
double sum = 0.0;
int i = 0;
#pragma omp parallel
{
double xThread = 0.0;
#pragma omp for reduction(+:sum)
for (i = 0; i < panels; i++)
{
xThread = (i+0.5)*hx;
sum += 4/(1 + xThread*xThread);
}
}
double clockstop = omp_get_wtime();
cout << "parallel code" << endl;
cout << "pi error " << pi - hx*sum << endl;
cout << "time elapsed: " << clockstop-clockstart << endl;
return 0;
}