PI open MP : approche SPMD #include static long etapes = 100000; double pas; #define NB_THREADS 2 void main () { int i; double x, pi, sum[NB_THREADS]; pas = 1.0/(double) etapes; omp_set_num_threads(NB_THREADS) #pragma omp parallel { double x; int id; id = omp_get_thread_num(); for (i=id, sum[id]=0.0;i< etapes; i=i+NB_THREADS){ x = ((double)i+0.5)*pas; sum[id] += 4.0/(1.0+x*x); } } for(i=0, pi=0.0;i static long etapes = 100000; double pas; #define NB_THREADS 2 void main () { int i; double x, pi, sum[NB_THREADS]; pas = 1.0/(double) etapes; omp_set_num_threads(NB_THREADS) #pragma omp parallel { double x; int id; id = omp_get_thread_num(); sum[id] = 0; #pragma omp for for (i=0;i< etapes; i++){ x = ((double)i+0.5)*pas; sum[id] += 4.0/(1.0+x*x); } } for(i=0, pi=0.0;i static long etapes = 100000; double pas; #define NB_THREADS 2 void main () { int i; double x, sum, pi=0.0; pas = 1.0/(double) etapes; omp_set_num_threads(NB_THREADS) #pragma omp parallel private (x, sum) { id = omp_get_thread_num(); for (i=id,sum=0.0;i< etapes;i=i+NB_THREADS){ x = ((double)i+0.5)*pas; sum += 4.0/(1.0+x*x); } #pragma omp critical pi += sum } Programme PI OpenMP : For parallèle + réduction #include static long etapes = 100000; double pas; #define NB_THREADS 2 void main () { int i; double x, pi, sum = 0.0; pas = 1.0/(double) etapes; omp_set_num_threads(NB_THREADS) #pragma omp parallel for reduction(+:sum) private(x) for (i=1;i<= etapes; i++){ x = ((double)i-0.5)*pas; sum = sum + 4.0/(1.0+x*x); } pi = pas * sum; }