what are mpi and openmp? - programming language lab

18
Introduction to MPI and OpenMP myson @ postech.ac.kr CSE700-PL @ POSTECH Programming Language Laboratory – p.1/18

Upload: others

Post on 12-Feb-2022

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: What are MPI and OpenMP? - Programming Language Lab

Introduction to

MPI and OpenMP

myson @ postech.ac.kr

CSE700-PL @ POSTECH

Programming Language Laboratory – p.1/18

Page 2: What are MPI and OpenMP? - Programming Language Lab

Outline

MPI and OpenMPDefinitionCharacteristicsFlow models

Examples

Compiling and Execution

Resources

Programming Language Laboratory – p.2/18

Page 3: What are MPI and OpenMP? - Programming Language Lab

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: What are MPI and OpenMP? - Programming Language Lab

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: What are MPI and OpenMP? - Programming Language Lab

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: What are MPI and OpenMP? - Programming Language Lab

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: What are MPI and OpenMP? - Programming Language Lab

OpenMP Flow Model

Directives (C/C++) - #pragma omp directives [clauses]

directives - parallel, for, single, etc.

Programming Language Laboratory – p.7/18

Page 8: What are MPI and OpenMP? - Programming Language Lab

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: What are MPI and OpenMP? - Programming Language Lab

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: What are MPI and OpenMP? - Programming Language Lab

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: What are MPI and OpenMP? - Programming Language Lab

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: What are MPI and OpenMP? - Programming Language Lab

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: What are MPI and OpenMP? - Programming Language Lab

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: What are MPI and OpenMP? - Programming Language Lab

Which is better?

Programming Language Laboratory – p.14/18

Page 15: What are MPI and OpenMP? - Programming Language Lab

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: What are MPI and OpenMP? - Programming Language Lab

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: What are MPI and OpenMP? - Programming Language Lab

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: What are MPI and OpenMP? - Programming Language Lab

End

Any Questions. . . ?

Programming Language Laboratory – p.18/18