15
Tutorial sobre MPI Temario 1 MPI Introducción Procesos Envío y recepción simple Envío y recepción no tan simple Comunicación colectiva Herramientas de depuración y evaluación

MPI - Escuela Técnica Superior de Ingeniería de Sistemas ... · % mpirun –n 9 –f helloWorld.txt helloWorld. 4406-1:4. 4406-2:4. 4406-1 % mpirun –f hola.txt –n 5 hola : -n

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: MPI - Escuela Técnica Superior de Ingeniería de Sistemas ... · % mpirun –n 9 –f helloWorld.txt helloWorld. 4406-1:4. 4406-2:4. 4406-1 % mpirun –f hola.txt –n 5 hola : -n

Tutorial sobre MPI Temario 1

• MPI

• Introducción

• Procesos

• Envío y recepción simple

• Envío y recepción no tan simple

• Comunicación colectiva

• Herramientas de depuración y evaluación

Page 2: MPI - Escuela Técnica Superior de Ingeniería de Sistemas ... · % mpirun –n 9 –f helloWorld.txt helloWorld. 4406-1:4. 4406-2:4. 4406-1 % mpirun –f hola.txt –n 5 hola : -n

arqAva MPI (Introducción) 2

MPI: Message Passing Interface – 1994 MPI Forum [Nov/92]

“Ejecución de aplicaciones paralelas distribuidas en ordenadores heterogéneos”

maestro

esclavo1 esclavo3esclavo2 esclavo4

Cada uno con su dirIP

• Biblioteca “mpi.h”MPI_Send,MPI_Recv,-------------

M E1 E2 E3 E4

• ImplementaciónMPICHLAM/MPIIBM, …

MPI-2

Page 3: MPI - Escuela Técnica Superior de Ingeniería de Sistemas ... · % mpirun –n 9 –f helloWorld.txt helloWorld. 4406-1:4. 4406-2:4. 4406-1 % mpirun –f hola.txt –n 5 hola : -n

>= 0 => MPI_SUCCESS, significado concreto< 0 => un error ( ... MPI_ERR_ARG ...)

arqAva MPI (Procesos) 3

int MPI_Comm_size(…, int *numProcesos);

int MPI_Comm_rank(…, int *yo);

int MPI_Finalize();

int MPI_Init( ....... ); /* Inicia MPI */

• Creación estática de procesos (según implementación “mpirun”)

01

32

4

MPI_COMM_WORLD

2

5

2

Page 4: MPI - Escuela Técnica Superior de Ingeniería de Sistemas ... · % mpirun –n 9 –f helloWorld.txt helloWorld. 4406-1:4. 4406-2:4. 4406-1 % mpirun –f hola.txt –n 5 hola : -n

arqAva MPI (Procesos) 4

• helloWorld paralelo#include “mpi.h”int main (int argc,

char *argv[]) {int yo, numProcesos;

MPI_Init (&argc, &argv);MPI_Comm_size (MPI_COMM_WORLD, &numProcesos);MPI_Comm_rank (MPI_COMM_WORLD, &yo);if (yo == 0) printf (“Creados %d procesos\n”, numProcesos);printf (“Hola, soy el proceso %d\n”, yo);MPI_Finalize();

}

% mpirun –n 5 helloWorld

% mpirun –n 9 –f helloWorld.txt helloWorld

4406-1:44406-2:4

4406-1

% mpirun –f hola.txt –n 5 hola : -n 4 helloWorld

4406-1:44406-2:4

4406-14406-2?

Page 5: MPI - Escuela Técnica Superior de Ingeniería de Sistemas ... · % mpirun –n 9 –f helloWorld.txt helloWorld. 4406-1:4. 4406-2:4. 4406-1 % mpirun –f hola.txt –n 5 hola : -n

arqAva MPI (Envío y Recepción Simple) 5

• Enviar y recibir arrays de datos simples (int, byte, ...) Bloqueante

int vector[N];----------MPI_Send (vector, …

P2, ...)----------

P1

int tabla[M];----------MPI_Recv (tabla, …

P1, ...)----------

P2

int MPI_Send(void *buffer, int cuantos, MPI_Datatype tipo,int destino, int etiqueta, MPI_Comm grupo)

MPI_INT,MPI_FLOAT, …

MPI_COMM_WORLD

0..MPI_TAG_UB

Page 6: MPI - Escuela Técnica Superior de Ingeniería de Sistemas ... · % mpirun –n 9 –f helloWorld.txt helloWorld. 4406-1:4. 4406-2:4. 4406-1 % mpirun –f hola.txt –n 5 hola : -n

arqAva MPI (Envío y Recepción Simple) 6

• Enviar y recibir arrays de datos simples (int, byte, ...) Bloqueanteint MPI_Recv(void *buffer,int cuantos, MPI_Datatype tipo,

int origen, int etiqueta, MPI_Comm grupo,

MPI_Status *estado)

estado.MPI_SOURCEestado.MPI_TAG

MPI_ANY_SOURCE

MPI_ANY_TAG

int MPI_Get_count( MPI_Status *estado, MPI_Datatype tipo,

int *cuantos)

Page 7: MPI - Escuela Técnica Superior de Ingeniería de Sistemas ... · % mpirun –n 9 –f helloWorld.txt helloWorld. 4406-1:4. 4406-2:4. 4406-1 % mpirun –f hola.txt –n 5 hola : -n

arqAva MPI (Envío y Recepción Simple) 7• Ejemplo de uso: psendrec.c

#include <stdio.h>#include <unistd.h>

#include “mpi.h"

#define N 3#define VECES 5

void esclavo(void) {...}void maestro(void) {...}

int main( int argc, char *argv[] ) {int yo;MPI_Init (&argc, &argv);MPI_Comm_rank (MPI_COMM_WORLD, &yo);if (yo == 0) maestro();else esclavo();MPI_Finalize();return 0;

}

Page 8: MPI - Escuela Técnica Superior de Ingeniería de Sistemas ... · % mpirun –n 9 –f helloWorld.txt helloWorld. 4406-1:4. 4406-2:4. 4406-1 % mpirun –f hola.txt –n 5 hola : -n

arqAva MPI (Envío y Recepción Simple) 8• Ejemplo de uso: psendrec.c

void maestro (void) {

int i, j, vector[N];

for (i=0; i<VECES; i++) {printf ("M: envia => ");for (j=0; j<N; j++) {vector[j] = i*N+j;printf("%d ", vector[j]);

}printf ("\n");MPI_Send (vector, N, MPI_INT, 1, 1, MPI_COMM_WORLD);

}}

esclavo

Page 9: MPI - Escuela Técnica Superior de Ingeniería de Sistemas ... · % mpirun –n 9 –f helloWorld.txt helloWorld. 4406-1:4. 4406-2:4. 4406-1 % mpirun –f hola.txt –n 5 hola : -n

arqAva MPI (Envío y Recepción Simple) 9• Ejemplo de uso: psendrec.c

void esclavo(void) {

int i, j,tabla[N], n;MPI_Status estado;

sleep(2);for (i=0; i<VECES; i++) {MPI_Recv (tabla, N, MPI_INT, 0, 1,

MPI_COMM_WORLD, &estado);MPI_Get_count (&estado, MPI_INT, &n);printf ("E: recibe => ");for (j=0; j<N; j++) printf("%d ", tabla[j]);printf (" de tid = %d eti = %d longi = %d\n",

estado.MPI_SOURCE, estado.MPI_TAG, n);}

}

maestro

Page 10: MPI - Escuela Técnica Superior de Ingeniería de Sistemas ... · % mpirun –n 9 –f helloWorld.txt helloWorld. 4406-1:4. 4406-2:4. 4406-1 % mpirun –f hola.txt –n 5 hola : -n

arqAva MPI (Envío y Recepción No Tan Simple) 10

int MPI_Iprobe(int origen, int etiqueta, MPI_Comm grupo, int *hayMsj, MPI_Status *estado)

• Enviar y recibir arrays de datos simples No Bloqueante

• Enviar y recibir datos no homogéneos

Crear tipos => Algo tedioso

Hacer otras cosas

NO

int MPI_Recv(void *buffer,… MPI_Status *estado)

SI

Page 11: MPI - Escuela Técnica Superior de Ingeniería de Sistemas ... · % mpirun –n 9 –f helloWorld.txt helloWorld. 4406-1:4. 4406-2:4. 4406-1 % mpirun –f hola.txt –n 5 hola : -n

arqAva MPI (Comunicación colectiva) 11

int MPI_Bcast(void *buffer, int cuantos,MPI_Datatype tipo, int emisor,MPI_Comm grupo)

cuenta.0

cuenta.1 cuenta.Ncuenta.2

“Ala”“Ala”

“Ala”

void maestro () { void esclavo () {char palabra[4]; char texto[4];

---------- ----------MPI_Bcast ( MPI_Bcast (palabra, 4, MPI_CHAR, texto, 4, MPI_CHAR,0, MPI_COMM_WORLD); 0, MPI_COMM_WORLD);

---------- ----------

? tagSend+Recv

Page 12: MPI - Escuela Técnica Superior de Ingeniería de Sistemas ... · % mpirun –n 9 –f helloWorld.txt helloWorld. 4406-1:4. 4406-2:4. 4406-1 % mpirun –f hola.txt –n 5 hola : -n

arqAva MPI (Comunicación colectiva) 12

• Otras llamadas interesantes:

int MPI_Barrier(MPI_Comm grupo);

int MPI_Gather(void *vOrg, int nOrg, MPI_Datatype tOrg,void *vDst, int nDst, MPI_Datatype tDst,int receptor, MPI_Comm grupo);

int MPI_Scatter(void *vOrg, int nOrg, MPI_Datatype tOrg,void *vDst, int nDst, MPI_Datatype tDst,int emisor, MPI_Comm grupo);

int MPI_Reduce(void *vOrg, void *vDst, int nOrg,MPI_Datatype tDatoOrg, int operacion,int receptor, MPI_Comm grupo);

Page 13: MPI - Escuela Técnica Superior de Ingeniería de Sistemas ... · % mpirun –n 9 –f helloWorld.txt helloWorld. 4406-1:4. 4406-2:4. 4406-1 % mpirun –f hola.txt –n 5 hola : -n

arqAva Herramientas de depuración y monitorización 13

• Medición de tiempos de ejecución• Depuración• Monitorización

#include <sys/time.h>

struct timeval t0, tf, tiempo;

/* Inicialización */

gettimeofday (&t0, NULL);

/* ejecucion del programa paralelo */

gettimeofday (&tf, NULL);timersub (&tf, &t0, &tiempo);printf (“Tiempo => %ld:%ld seg:micro\n”,

tiempo.tv_sec, tiempo.tv_usec);

Evitar E/S

Page 14: MPI - Escuela Técnica Superior de Ingeniería de Sistemas ... · % mpirun –n 9 –f helloWorld.txt helloWorld. 4406-1:4. 4406-2:4. 4406-1 % mpirun –f hola.txt –n 5 hola : -n

arqAva Herramientas de depuración y monitorización 14

%mpirun –dbg=ddd –np 2 psendrec

printf fflush(stdout) setbuf(stdout, NULL)

maestro esclavo

Page 15: MPI - Escuela Técnica Superior de Ingeniería de Sistemas ... · % mpirun –n 9 –f helloWorld.txt helloWorld. 4406-1:4. 4406-2:4. 4406-1 % mpirun –f hola.txt –n 5 hola : -n

arqAva Herramientas de depuración y monitorización 15

• Monitorización (totalview) para MPI, openMP, … www.etnus.com

FIN