After creating a new project with VS, First right-click the project, the attribute--c/c++--language, and the OpenMP support option in the right dialog box is;
The program code is:
#include <stdio.h>#include<time.h>#include<omp.h>Static LongNum_steps =100000;DoubleStep,pi;voidMain () {clock_t T1=clock (); inti; DoubleX,sum =0.0; Step=1.0/(Double) num_steps;#pragmaOMP parallel for Reduction (+: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, spents:%d\n", pi,t2-t1);}
The code above uses the protocol rduction (op:list), so that each thread will have a private list, and when all the threads ' list calculation is complete, the private list value of each thread will be the OP protocol as a list value. If you do not make a return, it may result in a different run.
The Open MP calculates the value of π