After creating a project with vs, you must right-click the project and choose Properties -- C/C ++ -- language. In the dialog box on the right, select YES For OpenMP;
ProgramCodeIs:
# Include<Stdio. h>
# Include<Time. h>
# Include<OMP. h>
Static long num_steps= 100000;
Double step, PI;
Void main ()
{
Clock_t T1 = clock ();
Int I;
Double X, sum = 0.0;
Step = 1.0/(double) num_steps;
# Pragma OMP parallel for ction (+: Sum)
For (I = 0; I <num_steps; I ++)
{
X = (I + 0.5) * step;
Sum = sum + 4.0/(1.0 + x * X );
Printf ("% d", omp_get_thread_num ());
}
Pi = Step * sum;
Clock_t t2 = clock ();
Printf ("Pi = % F, time: % d \ n", PI, t2-t1 );
}
The above Code uses the Protocol rduction (OP: List); in this way, each thread will have a private list. After the list calculation of all threads is complete, the private list value of each thread is normalized as a list value by op.