80 likes | 230 Views
PP Lab. Open MP programming Lab 10 and 11. THE LOOP directive. The for directive splits the for-loop so that each thread in the current team handles a different portion of the loop. A simple example. #include < omp.h > #include " stdafx.h " void main() { #pragma omp parallel {
E N D
PP Lab Open MP programming Lab 10and 11
THE LOOP directive • The for directive splits the for-loop so that each thread in the current team handles a different portion of the loop.
A simple example #include <omp.h> #include "stdafx.h" void main() { #pragma omp parallel { #pragma omp for for(intn=0; n<10; ++n) { printf("thread# %d says %d\n", omp_get_thread_num(),n); } } } Combined syntax: #pragma omp parallel for
Clauses of loop directive • Private • Shared • Firstprivate • Lastprivate • Reduction • Schedule • Ordered • Nowait
Example using private and shared #include <stdio.h> #include <math.h> #include <omp.h> intmain() { inti, sum=0; #pragma omp parallel private(i) shared(sum) // every thread has a different i, sum is a global variable { #pragma omp for for (i=1; i<=10; ++i) { #pragma ompatomic //this directive guarantees mutual exclusion on the shared variable, only one thread can write to sum at //a time, threads execute atomically in this region sum += i; } } printf(“The sum of 1 through 10 is %d\n", sum); }
printf("using the barrier now\n\n"); #pragma omp parallel {i=0; #pragma omp for firstprivate(i) lastprivate(i) for(n=0; n<10; ++n) {printf("thread# %d says %d, this is my iteration# %d\n\n", omp_get_thread_num(),n,i); i++;} printf("thread# %d says i'm done\n\n",omp_get_thread_num()); #pragma omp single printf("value of i is %d\n\n",i); } } #include <omp.h> #include "stdafx.h" void main() { int n, i; printf("not using the barrier\n\n"); #pragma omp parallel { i=0; #pragma omp for firstprivate(i) nowait for(n=0; n<10; ++n) {printf("thread# %d says %d, this is my iteration# %d\n\n", omp_get_thread_num(),n,i); i++;} printf("thread# %d says i'm done\n\n",omp_get_thread_num()); } Example using firstprivate, lastprivate and nowait
Example using schedule #include <omp.h> #include "stdafx.h" void main() { #pragma omp parallel { #pragma omp single printf("static shedule:\n"); #pragma omp for schedule(static,5) for(int n=0; n<23; ++n) printf("thread# %d says %d\n", omp_get_thread_num(),n); #pragma omp single printf("dynamic shedule:\n"); #pragma omp for schedule(dynamic,5) for(int n=0; n<23; ++n) printf("thread# %d says %d\n", omp_get_thread_num(),n); } }
Example using reduction #include <omp.h> #include <stdio.h> #include <stdlib.h> #define N 1000 int main (intargc, char *argv[]) { double a[N]; double sum=0.0; inti,n,tid; #pragma omp parallel shared(a) private(i) {tid=omp_get_thread_num(); #pragma omp single printf("Number of threads = %d\n",omp_get_num_threads()); #pragma omp for for(i=0;i<N;i++) a[i]=i+1; #pragma omp for reduction(+:sum) for(i=0;i<N; i++) sum=sum+a[i]; }/* sums of all threads are added up into one variable */ printf("Sum = %2.1f\n",sum); return(0);}