-
-
Save nazt/4e40fe8b7c152007b0e5 to your computer and use it in GitHub Desktop.
piomp.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <omp.h> | |
| static long num_steps = 99999999; | |
| double step,start = 0.0,stop = 0.0; | |
| #define NUM_THREADS 8 | |
| int main () | |
| { | |
| double pi; | |
| step = 1.0/(double) num_steps; | |
| omp_set_num_threads(NUM_THREADS); | |
| #pragma omp parallel | |
| { | |
| int i, id,nthrds,nthreads = 0; double x, sum; | |
| id = omp_get_thread_num(); | |
| nthrds = omp_get_num_threads(); | |
| if (id == 0) nthreads = nthrds; | |
| printf("HELLO FROM THREAD %d\n", id); | |
| start = omp_get_wtime(); | |
| for (i=id, sum=0.0;i< num_steps; i=i+NUM_THREADS) | |
| { | |
| x = (i+0.5)*step; | |
| sum += 4.0/(1.0+x*x); | |
| } | |
| sum = sum*step; | |
| #pragma omp atomic | |
| pi += sum ; | |
| } | |
| stop = omp_get_wtime(); | |
| printf("pi = %f",pi); | |
| printf("time = %f",stop-start); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment