1 / 8

PP Lab

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 {

tarala
Download Presentation

PP Lab

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PP Lab Open MP programming Lab 10and 11

  2. 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.

  3. 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

  4. Clauses of loop directive • Private • Shared • Firstprivate • Lastprivate • Reduction • Schedule • Ordered • Nowait

  5. 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); }

  6. 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

  7. 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); } }

  8. 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);}

More Related