Explorer les Livres électroniques
Catégories
Explorer les Livres audio
Catégories
Explorer les Magazines
Catégories
Explorer les Documents
Catégories
MPI
• Présentation de MPI
• Schéma général d’un programme MPI
• Exécution d’un programme C quelques commandes Unix
• Quelques fonctions MPI
• Exemples de programmes
• Communication
• Calcul de P
• Gestion des groupes
• Gestion des communicateurs
• Projets de TP
Architectures parallèles, M. Eleuldj, Département Génie Informatique, EMI, octobre 2014 1
Présentation de MPI (Message Passing Interface)
p2 p5
p0
p4
p1 p3 p6
Communicateur A Communicateur B
Architectures parallèles, M. Eleuldj, Département Génie Informatique, EMI, octobre 2014 3
Exécution d’un programme C
/* fichier hello.c*/
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Compilation:
$gcc hello.c (ou gcc –o hello.exe hello.c)
Exécution:
$./a (ou /home/……./a.exe ou ./hello)
Résultat:
$ Hello World!
Architectures parallèles, M. Eleuldj, Département Génie Informatique, EMI, octobre 2014 4
Quelques commandes Unix
$pwd
$ls -l
$hostname
$mpirun -n 1 hostname (ou mpiexec -np 1 hostname)
$mpirun -n 7 hostname
$mpirun hostname
$mpirun -n 5 hello
$mpirun -n 12 hello
$mpirun -machinefile machine.txt -n 4 hostname
Architectures parallèles, M. Eleuldj, Département Génie Informatique, EMI, octobre 2014 5
Quelques fonctions MPI (1/2)
int MPI_Init(int *argc, char ***argv)
int MPI_Finalize()
int MPI_Comm_rank(MPI_Comm comm, int *rank)
int MPI_Comm_size(MPI_Comm comm, int *size)
int MPI_Send(const void *buf, int count, MPI_Datatype datatype, int dest,
int tag, MPI_Comm comm)
int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag,
MPI_Comm comm, MPI_Status *status)
int MPI_Bcast(void *buffer, int count, MPI_Datatype datatype, int root,
MPI_Comm comm)
int MPI_Comm_group(MPI_Comm comm, MPI_Group *group)
int MPI_Group_incl(MPI_Group group, int n, const int ranks[],
MPI_Group *newgroup)
int MPI_Comm_create(MPI_Comm comm, MPI_Group group,
MPI_Comm *newcomm)
Architectures parallèles, M. Eleuldj, Département Génie Informatique, EMI, octobre 2014 6
Quelques fonctions MPI (2/2)
int MPI_Reduce(void *sendbuf, void *recvbuf, int count,
MPI_Datatype datatype, MPI_Op op, int root,MPI_Comm comm)
Int MPI_Allreduce( void *sendbuf, void *recvbuf, int count,
MPI_Datatype datatype, MPI_Op op, MPI_Comm comm )
Opérations :
MPI_MAX : maximum MPI_MIN : minimum
MPI_SUM : sum MPI_PROD : product
MPI_LAND : logical and MPI_BAND : bit-wise and
MPI_LOR : logical or MPI_BOR : bit-wise or
MPI_LXOR: logical xor MPI_BXOR : bit-wise xor
MPI_MAXLOC : max value + location MPI_MINLOC : min value + location
p1 p2 p3
Architectures parallèles, M. Eleuldj, Département Génie Informatique, EMI, octobre 2014 9
Programme de communication (1/3)
/* programme helloMPI.c*/
#include <stdio.h>
#include <string.h>
#include "mpi.h"
if (my_rank !=0){
/* create message */
sprintf(message, "Hello MPI World from process %d!", my_rank);
dest = 0; /* use strlen+1 so that '\0' get transmitted */
MPI_Send(message, strlen(message)+1, MPI_CHAR,dest,
tag,MPI_COMM_WORLD);
}
Architectures parallèles, M. Eleuldj, Département Génie Informatique, EMI, octobre 2014 11
Programme de communication (3/3)
else{
printf("Hello MPI World From process 0: Num processes: %d\n",p);
for (source = 1; source < p; source++) {
MPI_Recv(message, 100, MPI_CHAR, source, tag,
MPI_COMM_WORLD, &status);
printf("%s\n",message);
}
}
Hypoyhèse :
num_procs = 3 (p0, p1 et p2)
num_intervals =10 donc h = 0,1 1
1
Architectures parallèles, M. Eleuldj, Département Génie Informatique, EMI, octobre 2014 15
Programme du calcul de P (1/5)
/* fichier calculPi.c */
#include <mpi.h>
#include <stdio.h>
#include <string.h>
void calc_pi(int rank, int num_procs){
int i;
int num_intervals;
double h;
double mypi;
double pi;
double sum;
double x;
/* set number of intervals to calculate */
if (rank == 0) {
num_intervals = 1000;
}
Architectures parallèles, M. Eleuldj, Département Génie Informatique, EMI, octobre 2014 16
Programme du calcul de P (2/5)
/* tell other tasks how many intervals */
MPI_Bcast(&num_intervals, 1, MPI_INT, 0, MPI_COMM_WORLD);
/* calculate PI */
calc_pi(my_rank, num_procs);
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
sendbuf = rank;
MPI_Finalize();
}
Architectures parallèles, M. Eleuldj, Département Génie Informatique, EMI, octobre 2014 24
Algorithme de gestion des communicateurs
mon_rang identificateur du processus
P nombre total des processus
si (mon_rang < NBPROCS 0) alors
inclure(mon_rang,groupe1)
Sinon
inclure (mon_rang,groupe2)
recevoir(message,source)
écrire(message)
a) Compilation
$mpicc -o groupe.exe groupe.c
b) Exécution
$mpirun groupe.exe