introduction to mpi and openmp - electrical & computer ...courses/ee8218/mpi_openmp.pdf ·...
TRANSCRIPT
![Page 1: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/1.jpg)
Introduction to
MPI and OpenMP
myson @ postech.ac.kr
CSE700-PL @ POSTECH
Programming Language Laboratory – p.1/18
![Page 2: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/2.jpg)
Outline
MPI and OpenMPDefinitionCharacteristicsFlow models
Examples
Compiling and Execution
Resources
Programming Language Laboratory – p.2/18
![Page 3: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/3.jpg)
What are MPI and OpenMP?
Message Passing Interface (MPI)- MPI is a library specification for message-passing,proposed as a standard by a broadly based committeeof vendors, implementors, and users.
Programming Language Laboratory – p.3/18
![Page 4: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/4.jpg)
What are MPI and OpenMP?
Open Multi Processing (OpenMP)- OpenMP is a specification for a set of compilerdirectives, library routines, and environment variablesthat can be used to specify shared memory parallelismin Fortran and C/C++ programs.
Programming Language Laboratory – p.4/18
![Page 5: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/5.jpg)
MPI vs. OpenMP
MPI OpenMP
Distributed memory model Shared memory model
on Distributed network on Multi-core processors
Message based Directive based
Flexible and expressive Easier to program and debug
Programming Language Laboratory – p.5/18
![Page 6: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/6.jpg)
MPI Flow Model
Message Passing - Send and Receive
a message, size, type, source, dest,tag, communicator, status
Programming Language Laboratory – p.6/18
![Page 7: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/7.jpg)
OpenMP Flow Model
Directives (C/C++) - #pragma omp directives [clauses]
directives - parallel, for, single, etc.
Programming Language Laboratory – p.7/18
![Page 8: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/8.jpg)
A Simple Example
A serial program
#include<stdio.h>
#define PID 0
main(){
int i;
printf("Greetings from process %d!/n", PID);
}
Greetings from process 0
Programming Language Laboratory – p.8/18
![Page 9: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/9.jpg)
A Simple Example(cont.)
A parallel program using MPI (cont.)
#include<mpi.h>
main(int argc, char** argv){...
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
Parallel Region
MPI_Finalize();
}
Programming Language Laboratory – p.9/18
![Page 10: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/10.jpg)
A Simple Example(cont.)
A parallel program using MPI
if ( my_rank != 0){
sprintf(message,
"Greetings from process %d!", my_rank);
dest = 0;
MPI_Send(message, strlen(message)+1, MPI_CHAR,
dest, tag, MPI_COMM_WORLD);
} else{ /* my_rank = 0 */
for (source = 1; source < p; source++){
MPI_Recv(message, 100, MPI_CHAR, source, tag,
MPI_COMM_WORLD, &status);
printf("%s/n", message);
}
}
Programming Language Laboratory – p.10/18
![Page 11: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/11.jpg)
A Simple Example(cont.)
A parallel program using MPI (cont.)
Greetings from process 1
Greetings from process 2
Greetings from process 3
Programming Language Laboratory – p.11/18
![Page 12: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/12.jpg)
A Simple Example(cont.)
A parallel program using OpenMP
#include<stdio.h>
#include<omp.h>
main(){
int id;
#pragma omp parallel
{
id = omp_get_thread_num();
printf("Greetings from process %d!/n", id);
}
}
Programming Language Laboratory – p.12/18
![Page 13: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/13.jpg)
A Simple Example(cont.)
A parallel program using OpenMP (cont.)
Greetings from process 1
Greetings from process 0
Greetings from process 2
Greetings from process 3
Programming Language Laboratory – p.13/18
![Page 14: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/14.jpg)
Which is better?
Programming Language Laboratory – p.14/18
![Page 15: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/15.jpg)
Compiling
GCC and MPICH2 for MPI
GCC-4.2 with library libgomp for OpenMP
MPImpicc -o example.out example.c
OpenMPgcc-4.2 -o example.out example.c -fopenmp
Programming Language Laboratory – p.15/18
![Page 16: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/16.jpg)
Execution
∼/.mpd.conf for MPI executionvi(or emacs) ∼/.mpd.confsecretword=<your secretword>chmod 600 ∼/.mpd.conf
MPI (using multi-core processors)mpdbootmpiexec -n #processes ./example.outmpdallexit
OpenMP./example.out
Programming Language Laboratory – p.16/18
![Page 17: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/17.jpg)
Resources
Machine (Plquad: plquad.postech.ac.kr)Intel Core 2 Quad Q6600 (quad-core)1G DDR RAMIf you want to use it, email the instructors.
Materials - resource tab on the course web-pageMPI & OpenMP install guidesMPI & OpenMP tutorials...
Programming Language Laboratory – p.17/18
![Page 18: Introduction to MPI and OpenMP - Electrical & Computer ...courses/ee8218/mpi_openmp.pdf · Introduction to MPI and OpenMP myson @ postech.ac.kr ... GCC and MPICH2 for MPI GCC-4.2](https://reader031.vdocuments.net/reader031/viewer/2022021601/5cdf322e88c993f4758b8104/html5/thumbnails/18.jpg)
End
Any Questions. . . ?
Programming Language Laboratory – p.18/18