MPI Pack

De Wiki de Calcul Québec
Aller à : Navigation, rechercher
Cette page est une traduction de la page MPI Pack et la traduction est complétée à 100 % et à jour.

Autres langues :anglais 100% • ‎français 100%

This example shows how to use the functions MPI_Pack amd MPI_Unpack to tranfer many variables in a unique communication.

Each process identifies its rank. Process 0 then packs the three variables to be sent: a, b, n. The first two are of type real and the last one of type integer. The variable buffer is a descriptor that contains a list of memory position for the objects that are transferred. The variable position is only useful when the packet is constructed to remember where we are. Following that, process 0 sends everything to all other processes. Once the latter receive the message, they extract the desired information. Before exiting the program the parameters' values are displayed to make sure that the transfer was successful.

In Fortran

File : pack.f
!--------------------------------------------------------
! This example has been taken from:
!        A User's Guide to MPI, Peter S. Pacheco (1998)
!
! Author: Steve Allen
!         Centre de calcul scientifique
!         Université de Sherbrooke
!
! Last revision: September 2005
!--------------------------------------------------------
     Program Example_Pack
      include "mpif.h"
 
      integer            :: myrank, ierr
      integer            :: position
      integer            :: n
      real               :: a, b
      character(len=100) :: buffer
 
      call MPI_INIT(ierr)
      call MPI_COMM_RANK(MPI_COMM_WORLD, myrank, ierr)
 
      if (myrank .eq. 0) then
         n = 4
         a = 1.
         b = 2.
         print*,'Rank=',myrank,' a=',a,' b=',b,' n=',n
         position = 0
 
    ! packing
         call MPI_PACK(a,1,MPI_REAL,buffer,100,position,MPI_COMM_WORLD,ierr)
         call MPI_PACK(b,1,MPI_REAL,buffer,100,position,MPI_COMM_WORLD,ierr)
         call MPI_PACK(n,1,MPI_INTEGER,buffer,100,position,MPI_COMM_WORLD,ierr)
 
    ! communication
         call MPI_BCAST(buffer,100,MPI_PACKED,0,MPI_COMM_WORLD,ierr)
 
      else
 
    ! communication
         call MPI_BCAST(buffer,100,MPI_PACKED,0,MPI_COMM_WORLD,ierr)
         position = 0
 
    ! unpacking
         call MPI_UNPACK(buffer,100,position,a,1,MPI_REAL,MPI_COMM_WORLD,ierr)
         call MPI_UNPACK(buffer,100,position,b,1,MPI_REAL,MPI_COMM_WORLD,ierr)
         call MPI_UNPACK(buffer,100,position,n,1,MPI_INTEGER,MPI_COMM_WORLD,ierr)
         print*,'Rank=',myrank,' a=',a,' b=',b,' n=',n
      endif
 
      call MPI_FINALIZE(ierr)
      end program Example_Pack


In C

File : pack.c
/*--------------------------------------------------------  
 This example has been taken from:
         A User's Guide to MPI, Peter S. Pacheco (1998)
 
 Author: Steve Allen
         Centre de calcul scientifique
         Université de Sherbrooke
 
 Last revision: September 2005
--------------------------------------------------------*/
 
#include <stdio.h>
#include "mpi.h"
 
int main(int argc,char** argv)
{
   int     myrank;
   int     position;
   int     n;
   float   a, b;
   char    buffer[100];
 
   MPI_Init(&argc, &argv);
   MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
 
   if (myrank == 0){
      n = 4;
      a = 1.;
      b = 2.;
      printf("Rank=%d, a= %f, b= %f, and n=%d\n", myrank, a, b, n);
      position = 0;
 
    // packing
      MPI_Pack(&a, 1, MPI_FLOAT, buffer, 100, &position, MPI_COMM_WORLD);
      MPI_Pack(&b, 1, MPI_FLOAT, buffer, 100, &position, MPI_COMM_WORLD);
      MPI_Pack(&n, 1, MPI_INT, buffer, 100, &position, MPI_COMM_WORLD);
 
    // communication
      MPI_Bcast(buffer, 100, MPI_PACKED, 0, MPI_COMM_WORLD);
 
   } else {
 
    // communication
      MPI_Bcast(buffer, 100, MPI_PACKED, 0, MPI_COMM_WORLD);
      position = 0;
 
    // unpacking
      MPI_Unpack(buffer, 100, &position, &a, 1, MPI_FLOAT, MPI_COMM_WORLD);
      MPI_Unpack(buffer, 100, &position, &b, 1, MPI_FLOAT, MPI_COMM_WORLD);
      MPI_Unpack(buffer, 100, &position, &n, 1, MPI_INT, MPI_COMM_WORLD);
      printf("Rank=%d, a= %f, b=%f, and n=%d\n", myrank, a, b, n);
   }
 
   MPI_Finalize();
}


Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Ressources de Calcul Québec
Outils
Partager