Your program is perfectly syntactically correct OpenMP code according to current OpenMP standards (for example, it compiles unmodified using GCC 4.7.1), except that x must be declared private (which is not syntactic, but rather a semantic error). Unfortunately, Microsoft Visual C ++ implements the very old OpenMP specification (2.0 of March 2002), which allows the use of the following statements in the atomic construct:
x binop = expr
x ++
++ x
x -
--x
Later versions included x = x binop expr, but MSVC was permanently stuck in OpenMP version 2.0 even in VS2012. In comparison, the current version of OpenMP is 3.1, and we expect 4.0 to appear in the coming months.
In OpenMP 2.0, your statement should look like this:
#pragma omp atomic sum += 4.0/(1.+ x*x);
But as already noted, it would be better (and generally faster) to use the abbreviation:
#pragma omp parallel for private(x) reduction(+:sum) for (i=0; i<num_steps; i++) { x = (i + .5)*step; sum = sum + 4.0/(1.+ x*x); }
(you can also write sum += 4.0/(1.+ x*x); )
source share