MPI-Beispiel ring.c


C-Programm
ring.c:
/* MPI-Beispiel
   R0 sendet zu R1
   R1 empfängt und sendet zu R2
   R2 empfängt und sendet zu R3

   ... empfängt und sendet zu R0
   R0 empfängt und sendet zu R1
   usw.
   
   http://www.tnotes.de/MpiBeispielRing
   
   Compile: mpicc -o ring ring.c
   Start:   mpiexec -n 6 ./ring  
   
   @author Andreas Tobola
   @since 06.12.2008
*/

#include "mpi.h"
#include <stdio.h>
#include <string.h>

int main(int argc, char** argv)
{
  int size, rank, prank, nrank;
  char sdata[] = "Wir spielen mit MPI!";
  char rdata[30];
  int  data_size;
  int i;  
  MPI_Status stat;

  data_size = sizeof(sdata);
 
  MPI_Init(&argc, &argv);
  MPI_Comm_size( MPI_COMM_WORLD, &size );
  MPI_Comm_rank( MPI_COMM_WORLD, &rank );


  // Calcualte previous prozess rank
  prank = rank-1;
  if (prank==-1)
    prank = size-1;
 
  // Calcualte next prozess rank
  nrank = (rank+1) % size;

  // If we are the first (rank 0), first token has to be send
  if (rank==0)
  {
    sleep(2);
    MPI_Send(sdata, data_size, MPI_CHAR, 1, 1,  MPI_COMM_WORLD );
    printf ("R%d sent data to R%d.\n", rank, nrank );
  }

 
  for(i=0; i<10; i++)
  {
   
    // Wait and receive data form prank
    printf ("R%d is waiting for R%d...\n", rank, prank);
    memset(rdata, 0 , 30);
    MPI_Recv(rdata, data_size, MPI_CHAR, prank       , 1, MPI_COMM_WORLD,  &stat);
   
    // Print and forward data to nrank
    printf( "R%d received from R%d: %s\n", rank, prank, rdata);
    usleep(100000);
    MPI_Send(rdata, data_size, MPI_CHAR, nrank , 1, MPI_COMM_WORLD );
    printf ("R%d sent data to R%d.\n", rank, nrank );
   
   }

  //printf( "Finalize %d/%d\n", rank, size );
  MPI_Finalize();
  return 0;
}


Kompilieren
mpicc ring.c -o ring


Mit vier Prozessen auf einem Rechner testen
mpiexec -n 4 ring


Ausgabe
R1 is waiting for R0...
R2 is waiting for R1...
R3 is waiting for R2...
R0 sent data to R1.
R0 is waiting for R3...
R1 received from R0: Wir spielen mit MPI!
R1 sent data to R2.
R1 is waiting for R0...
R2 received from R1: Wir spielen mit MPI!
R2 sent data to R3.
R2 is waiting for R1...
R3 received from R2: Wir spielen mit MPI!
R0 received from R3: Wir spielen mit MPI!
R3 sent data to R0.
R3 is waiting for R2...
R0 sent data to R1.
R0 is waiting for R3...
R1 received from R0: Wir spielen mit MPI!
R2 received from R1: Wir spielen mit MPI!
R1 sent data to R2.
R1 is waiting for R0...
R2 sent data to R3.
R2 is waiting for R1...
R3 received from R2: Wir spielen mit MPI!
R0 received from R3: Wir spielen mit MPI!
R3 sent data to R0.
R3 is waiting for R2...
R0 sent data to R1.
R0 is waiting for R3...
R1 received from R0: Wir spielen mit MPI!
R2 received from R1: Wir spielen mit MPI!
R1 sent data to R2.
R1 is waiting for R0...
R2 sent data to R3.
R2 is waiting for R1...
R3 received from R2: Wir spielen mit MPI!
R0 received from R3: Wir spielen mit MPI!
R3 sent data to R0.
R3 is waiting for R2...
...



Mit drei Rechnern testen
mpiexec  -n 1 -host amygdala ~/code/mpi/04_ring/ring : -n 1 -host IBM ~/mpi/ring : -n 1 -host Medusa ~/mpi/ring


R2 is waiting for R1...
R1 is waiting for R0...
R0 sent data to R1.
R0 is waiting for R2...
R1 received from R0: Wir spielen mit MPI!
R2 received from R1: Wir spielen mit MPI!
R1 sent data to R2.
R1 is waiting for R0...
R0 received from R2: Wir spielen mit MPI!
R2 sent data to R0.
R2 is waiting for R1...
...





CategoryStudiumSE
Siehe auch Mpi

There are no comments on this page. [Add comment]

Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki