HTML version of Scripted Foils prepared 11 November 1996

Foil 5 Hello World:C Example of Send and Receive

From CPS615-Completion of MPI foilset and Application to Jacobi Iteration in 2D Delivered Lectures of CPS615 Basic Simulation Track for Computational Science -- 7 November 96. by Geoffrey C. Fox *
Secs 257.7
1 #include "mpi.h"
2 main( int argc, char **argv )
3 {
  • char message[20];
  • int i, rank, size, tag=137; # Any value of type allowed
  • MPI_Status status;
  • MPI_Init (&argc, &argv);
  • MPI_Comm_size(MPI_COMM_WORLD, &size); # Number of Processors
  • MPI_Comm_rank(MPI_COMM_WORLD, &rank); # Who is this processor
  • if( rank == 0 ) { # We are on "root" -- Processor 0
    • strcpy(message,"Hello MPI World"); # Generate message
    • for(i=1; i<size; i++) # Send message to size-1 other processors
    • MPI_Send(message, strlen(message)+1, MPI_CHAR, i, tag, MPI_COMM_WORLD);
  • }
  • else
    • MPI_Recv(message,20, MPI_CHAR, 0, tag, MPI_COMM_WORLD, &status);
  • printf("This is a message from node %d saying %s\n", rank, message);
  • MPI_Finalize();
4 }

Table Font Size


© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Fri Aug 15 1997