Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void lab4(int k)
- {
- int num_threads=k;
- static long num_steps=100000000;
- double sum,pi,x,step;
- float start=omp_get_wtime();
- omp_set_num_threads(num_threads);
- pi=0.0;
- #pragma omp parallel
- {
- int i;
- int tid;
- tid=omp_get_thread_num();
- double x;
- double sum=0.0;
- step=1.0/(double)num_steps;
- for(i=tid;i<num_steps;i=i+num_threads)
- {
- x=(i+0.5)*step;
- sum=sum+4.0/(1+x*x);
- }
- #pragma omp critical
- {
- pi=pi+sum*step;
- }
- }
- float end=omp_get_wtime();
- float secs=end-start;
- printf("pi=%f time=%f threads=%d\n",pi,secs,num_threads);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement