procesamiento paralelo - mpi - tipos y topologías procesamiento paralelo mpi - tipos y topologías...

Post on 25-Sep-2018

236 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Procesamiento ParaleloMPI - Tipos y Topologías

Javier Iparraguirre

Universidad Tecnológica Nacional, Facultad Regional Bahía Blanca11 de Abril 461, Bahía Blanca, Argentina

jiparraguirre@frbb.utn.edu.arhttp://www.frbb.utn.edu.ar/hpc/

9 de junio de 2016

Tipos de datos derivados

Tipos de datos primitivos

Datos derivados

• Se pueden definir estructuras de datos basados en lostipos primitivos de MPI.

• Los tipos de datos primitivos son contiguos.• Con tipos de datos derivados podemos definir estructuras

de datos no-contiguas y se pueden tratar como contiguasde una forma conveniente.

Formas de crear datos derivados

• Contiguous• Vector• Indexed• Struct

MPI_Type_contiguous

• Provee un nuevo tipo que consiste en múltiples capas deun tipo existente.

• MPI_Type_contiguous (count,oldtype,&newtype)

MPI_Type_vector y MPI_Type_hvector

• Similar al anterior pero permite saltos (stride) regulares enlos desplazamientos.

• MPI_Type_vector y MPI_Type_hvector son idénticas con lasalvedad que en uno el salto se especifica en bytes.

• MPI_Type_vector (count, blocklength, stride,oldtype,&newtype)

MPI_Type_indexed y MPI_Type_hindexed

• Arreglo de tipos de datos primitivos.• MPI_Type_indexed y MPI_Type_hindexed son idénticas

con la salvedad que en uno el salto se especifica en bytes.• MPI_Type_indexed (count, blocklens[], offsets[],

old_type, &newtype)

MPI_Type_struct

• El nuevo tipo de datos se forma de acuerdo al mapacompuesto de datos primitivos.

• MPI_Type_struct (count, blocklens[], offsets[],old_types, &newtype)

MPI_Type_extent

• Retorna el tamaño en bytes de un tipo dado. Usado en lasfunciones que requieren tamaños de los tipos.

• MPI_Type_extent (datatype, &extent)

MPI_Type_commit

• Toma memoria para alojar al nuevo tipo de datos en elsistema. Requerido para todos los tipos de datosderivados.

• MPI_Type_commit( &datatype)

MPI_Type_free

• Devuelve la memoria tomada para el nuevo tipo de datos.Mandatorio en todos los programas, especialmente dentrode bucles.

• MPI_Type_free (&datatype)

Ejemplo de datos contiguos

Ejemplo de datos contiguos

# inc lude " mpi . h "# inc lude < s t d i o . h># de f ine SIZE 4i n t main ( i n t argc , char ∗argv [ ] ){

i n t numtasks , rank , source =0 , tag =1 , i ;f l o a t a [ SIZE ] [ SIZE ] = { 1 . 0 , 2 .0 , 3 .0 , 4 .0 ,

5 .0 , 6 .0 , 7 .0 , 8 .0 ,9 .0 , 10.0 , 11.0 , 12.0 ,13.0 , 14.0 , 15.0 , 1 6 . 0 } ;

f l o a t b [ SIZE ] ;MPI_Status s t a t ;MPI_Datatype rowtype ;MPI_ In i t (& argc ,& argv ) ;MPI_Comm_rank (MPI_COMM_WORLD, &rank ) ;MPI_Comm_size (MPI_COMM_WORLD, &numtasks ) ;MPI_Type_contiguous ( SIZE , MPI_FLOAT, &rowtype ) ;MPI_Type_commit (& rowtype ) ;i f ( numtasks == SIZE ) {

i f ( rank == 0) {f o r ( i =0; i <numtasks ; i ++)

MPI_Send(&a [ i ] [ 0 ] , 1 , rowtype , i , tag , MPI_COMM_WORLD) ;}MPI_Recv ( b , SIZE , MPI_FLOAT, source , tag , MPI_COMM_WORLD, &s t a t ) ;p r i n t f ( " rank= %d b= %3.1 f %3.1 f %3.1 f %3.1 f \ n " ,

rank , b [ 0 ] , b [ 1 ] , b [ 2 ] , b [ 3 ] ) ;}e lse

p r i n t f ( " Must spec i f y %d processors . Terminat ing . \ n " , SIZE ) ;

MPI_Type_free (& rowtype ) ;MPI_Final ize ( ) ;r e t u r n 0 ;

}

Ejemplo de datos contiguos

rank= 0 b= 1.0 2.0 3.0 4.0rank= 1 b= 5.0 6.0 7.0 8.0rank= 2 b= 9.0 10.0 11.0 12.0rank= 3 b= 13.0 14.0 15.0 16.0

Ejemplo de vectores

Ejemplo de vectores

# inc lude " mpi . h "# inc lude < s t d i o . h># de f ine SIZE 4i n t main ( i n t argc , char ∗argv [ ] ){

i n t numtasks , rank , source =0 , tag =1 , i ;f l o a t a [ SIZE ] [ SIZE ] = { 1 . 0 , 2 .0 , 3 .0 , 4 .0 ,

5 .0 , 6 .0 , 7 .0 , 8 .0 ,9 .0 , 10.0 , 11.0 , 12.0 ,13.0 , 14.0 , 15.0 , 1 6 . 0 } ;

f l o a t b [ SIZE ] ;MPI_Status s t a t ;MPI_Datatype columntype ;MPI_ In i t (& argc ,& argv ) ;MPI_Comm_rank (MPI_COMM_WORLD, &rank ) ;MPI_Comm_size (MPI_COMM_WORLD, &numtasks ) ;MPI_Type_vector ( SIZE , 1 , SIZE , MPI_FLOAT, &columntype ) ;MPI_Type_commit (& columntype ) ;i f ( numtasks == SIZE ) {

i f ( rank == 0) {f o r ( i =0; i <numtasks ; i ++)

MPI_Send(&a [ 0 ] [ i ] , 1 , columntype , i , tag , MPI_COMM_WORLD) ;}MPI_Recv ( b , SIZE , MPI_FLOAT, source , tag , MPI_COMM_WORLD, &s t a t ) ;p r i n t f ( " rank= %d b= %3.1 f %3.1 f %3.1 f %3.1 f \ n " ,

rank , b [ 0 ] , b [ 1 ] , b [ 2 ] , b [ 3 ] ) ;}e lse

p r i n t f ( " Must spec i f y %d processors . Terminat ing . \ n " , SIZE ) ;MPI_Type_free (& columntype ) ;MPI_Final ize ( ) ;r e t u r n 0 ;

}

Ejemplo de vectores

rank= 0 b= 1.0 5.0 9.0 13.0rank= 1 b= 2.0 6.0 10.0 14.0rank= 2 b= 3.0 7.0 11.0 15.0rank= 3 b= 4.0 8.0 12.0 16.0

Grupos y comunicadores

Grupos y comunicadores

• Un grupo es un conjunto ordenado de nodos.• Cada nodo en un grupo es asociado a un ID entero en el

rango.• Si el grupo des de N elementos, el rango va desde 0 hasta

N-1.• En MPI el grupo se representa como un objeto. Se accede

a través de una referencia.• El grupo siempre es asociado con un objeto comunicador.

Mensajes con grupos y comunicadores

• Todos los mensajes MPI deben especificar uncomunicador.

• Por defecto, usamos MPI_COMM_WORLD.• Desde el punto de vista del programador, el grupo y el

comunicador son uno.• Las rutinas de grupo se usan para especificar que

procesos deben ser usados para construir uncomunicador.

Grupos y comunicadores

Propósitos de grupos y comunicadores

• Permite organizar tareas en grupos.• Permite comunicaciones colectivas en un subgrupo de

tareas.• Es la base para implementar topologías definidas por el

usuario.• Provee un mecanismo para realizar comunicaciones

seguras.

Consideraciones de programación para grupos ycomunicadores

• Los grupos y comunicadores son estructuras dinámicas ydeben ser creadas y destruidas en tiempo de ejecución.

• Los procesos pueden estar en más de un grupo.• Hay 40 directivas MPI relacionadas con grupos y

comunicadores.

Uso típico grupos y comunicadores

1 Extraer MPI_COMM_WORLD usando MPI_Comm_group2 Crear un sub-grupo usando MPI_Group_incl3 Crear un nuevo comunicador para el nuevo grupo usando

MPI_Comm_create4 Determinar el nuevo rango en el nuevo comunicador

usando MPI_Comm_rank5 Pasar mensajes usando MPI6 Cando se termina el trabajo, liberar recursos usando

MPI_Comm_free y MPI_Group_free

Ejemplo de grupos

# inc lude " mpi . h "# de f ine NPROCS 8i n t main ( i n t argc , char ∗argv [ ] ){

i n t rank , new_rank , sendbuf , recvbuf , numtasks ,ranks1 [ 4 ] = { 0 , 1 , 2 , 3 } , ranks2 [ 4 ] = { 4 , 5 , 6 , 7 } ;

MPI_Group or ig_group , new_group ;MPI_Comm new_comm; MPI_ In i t (& argc ,& argv ) ;MPI_Comm_rank (MPI_COMM_WORLD, &rank ) ;MPI_Comm_size (MPI_COMM_WORLD, &numtasks ) ;i f ( numtasks != NPROCS) {

p r i n t f ( " Must spec i f y MP_PROCS= %d . Terminat ing . \ n " ,NPROCS) ;MPI_Final ize ( ) ;e x i t ( 0 ) ;

}sendbuf = rank ;/∗ Ex t rac t the o r i g i n a l group handle ∗ /MPI_Comm_group (MPI_COMM_WORLD, &or ig_group ) ;/∗ Div ide tasks i n t o two d i s t i n c t groups based upon rank ∗ /i f ( rank < NPROCS/ 2 ) {

MPI_Group_incl ( or ig_group , NPROCS/2 , ranks1 , &new_group ) ;}e lse {

MPI_Group_incl ( or ig_group , NPROCS/2 , ranks2 , &new_group ) ;}/∗ Create new new communicator and then perform c o l l e c t i v e communications ∗ /MPI_Comm_create (MPI_COMM_WORLD, new_group , &new_comm ) ;MPI_Allreduce (&sendbuf , &recvbuf , 1 , MPI_INT , MPI_SUM, new_comm ) ;MPI_Group_rank ( new_group , &new_rank ) ;p r i n t f ( " rank= %d newrank= %d recvbuf= %d \ n " , rank , new_rank , recvbuf ) ;MPI_Final ize ( ) ;r e t u r n 0 ;

}

Ejemplo de grupos

rank= 4 newrank= 0 recvbuf= 22rank= 6 newrank= 2 recvbuf= 22rank= 7 newrank= 3 recvbuf= 22rank= 1 newrank= 1 recvbuf= 6rank= 3 newrank= 3 recvbuf= 6rank= 5 newrank= 1 recvbuf= 22rank= 0 newrank= 0 recvbuf= 6rank= 2 newrank= 2 recvbuf= 6

Topologías virtuales

Topologías virtuales

• En MPI se pueden mapear procesos a una formageométrica.

• Las dos principales topologías son grilla cartesiana ygrafos (Cartesian, Graph).

• Las topologías son virtuales, no hay relación con laestructura física de la máquina.

• Las topologías se construyen sobre grupos ycomunicadores.

• Deben ser programadas por el desarrollador.

Ventajas

• Conveniencia.• Eficiencia en las comunicaciones.

Ejemplo de topologías virtuales

Contacto

¡Muchas gracias!

¿Preguntas?jiparraguirre@frbb.utn.edu.ar

Referencias

• MPI Tutorial, Blaise Barney, Lawrence Livermore NationalLaboratoryhttps://computing.llnl.gov/tutorials/mpi/

top related