cs470/570 lecture 5 introduction to openmp compute pi example openmp directives and options
TRANSCRIPT
![Page 1: CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options](https://reader036.vdocuments.net/reader036/viewer/2022082817/56649e205503460f94b0c5c3/html5/thumbnails/1.jpg)
CS470/570 Lecture 5
• Introduction to OpenMP• Compute Pi example• OpenMP directives and options
![Page 2: CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options](https://reader036.vdocuments.net/reader036/viewer/2022082817/56649e205503460f94b0c5c3/html5/thumbnails/2.jpg)
Introduction to OpenMP
• A set of compiler directives and library routines to support parallel programming in a share memory architecture
• Can be used with C, C++ and ForTran• A program created with OpenMP starts with a
master thread• Various directives cause the program to
created and execute multiple threads
![Page 3: CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options](https://reader036.vdocuments.net/reader036/viewer/2022082817/56649e205503460f94b0c5c3/html5/thumbnails/3.jpg)
Introduction to OpenMP
Master Thread
AdditionalThreadsExecute
New Threads created with #pragma omp parallel
Threads joinedOnly master thread remains
![Page 4: CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options](https://reader036.vdocuments.net/reader036/viewer/2022082817/56649e205503460f94b0c5c3/html5/thumbnails/4.jpg)
Introduction to OpenMp
• Directives have the following form– #pragma omp construct [clauses]
• Directives are applied to a structured block of code– A structured block has one entry point (at the top)
and one exit point (at the bottom)
![Page 5: CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options](https://reader036.vdocuments.net/reader036/viewer/2022082817/56649e205503460f94b0c5c3/html5/thumbnails/5.jpg)
Introduction to OpenMp
• Supports explicit parallelism• Programmer needs to specify where parallel
threads should be used• Programmer needs to think about
synchronization issues
![Page 6: CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options](https://reader036.vdocuments.net/reader036/viewer/2022082817/56649e205503460f94b0c5c3/html5/thumbnails/6.jpg)
Simple Example
• Sequential algorithm to calculate pi– Find the area under the curve 4/(1+x2) between
0 and 1.– Example of numerical integration
• Parallelization of the algorithm using OpenMP
![Page 7: CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options](https://reader036.vdocuments.net/reader036/viewer/2022082817/56649e205503460f94b0c5c3/html5/thumbnails/7.jpg)
Compute Pi
![Page 8: CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options](https://reader036.vdocuments.net/reader036/viewer/2022082817/56649e205503460f94b0c5c3/html5/thumbnails/8.jpg)
Sequential Algorithm to Computer Pi
int main(int argc, char *argv[]) { double midpoint, width, pi, sum; int numIntervals, I; numIntervals = atoi(argv[1]); width = 1.0 / numIntervals; pi = 0.0; for(i = 0; i < numIntervals; i++) { midpoint = (i + 0.5) * width; sum = sum +4.0 / (1.0 + midpoint * midpoint); } pi = width*sum; printf("%1.20f \n", pi);}
![Page 9: CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options](https://reader036.vdocuments.net/reader036/viewer/2022082817/56649e205503460f94b0c5c3/html5/thumbnails/9.jpg)
Parallelized Algorithm to Computer Pi
int main(int argc, char *argv[]) {double midpoint, width, pi, sum;
int numIntervals, I; numIntervals = atoi(argv[1]); width = 1.0 / numIntervals; pi = 0.0;#pragma omp parallel for default(shared) private(i, midpoint)reduction(+:sum) for(i = 0; i < numIntervals; i++) { midpoint = (i + 0.5) * width; sum = sum +4.0 / (1.0 + midpoint * midpoint); } pi = width*sum; printf("%1.20f \n", pi);}
![Page 10: CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options](https://reader036.vdocuments.net/reader036/viewer/2022082817/56649e205503460f94b0c5c3/html5/thumbnails/10.jpg)
OpenMP Directives
• #pragma omp parallel• #pragma omp for• #pragma omp sections
![Page 11: CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options](https://reader036.vdocuments.net/reader036/viewer/2022082817/56649e205503460f94b0c5c3/html5/thumbnails/11.jpg)
#pragma omp parallelExample
void printId(int myId) {printf("%d\n", myId);
}
int main(int argc, char *argv[]) {
#pragma omp parallel{
printId(omp_get_thread_num());}
}
![Page 12: CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options](https://reader036.vdocuments.net/reader036/viewer/2022082817/56649e205503460f94b0c5c3/html5/thumbnails/12.jpg)
#pragma omp forint main(int argc, char *argv[]) {
int i;#pragma omp parallel for num_threads(4)for (i = 0; i < 10; i++) {
printf("%d %d\n",omp_get_thread_num(),i);}
}
![Page 13: CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options](https://reader036.vdocuments.net/reader036/viewer/2022082817/56649e205503460f94b0c5c3/html5/thumbnails/13.jpg)
#pragma omp sectionsint main(int argc, char *argv[]) {
#pragma omp parallel num_threads(4){
#pragma omp sections {
#pragma omp section{printf("%d\n",omp_get_thread_num());}
#pragma omp section{printf("%d\n",omp_get_thread_num());}
#pragma omp section{printf("%d\n",omp_get_thread_num());}
#pragma omp section{printf("%d\n",omp_get_thread_num());}
}}
}