HP-MPI User's Guide (11th Edition)

Example applications
communicator.c
Appendix A 249
communicator.c
This C example shows how to make a copy of the default communicator
MPI_COMM_WORLD using MPI_Comm_dup.
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int
main(argc, argv)
int argc;
char *argv[];
{
int rank, size, data;
MPI_Status status;
MPI_Comm libcomm;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (size != 2) {
if ( ! rank) printf("communicator: must have two
processes\n");
MPI_Finalize();
exit(0);
}
MPI_Comm_dup(MPI_COMM_WORLD, &libcomm);
if (rank == 0) {
data = 12345;
MPI_Send(&data, 1, MPI_INT, 1, 5,
MPI_COMM_WORLD);
data = 6789;
MPI_Send(&data, 1, MPI_INT, 1, 5, libcomm);
} else {
MPI_Recv(&data, 1, MPI_INT, 0, 5, libcomm,
&status);
printf("received libcomm data = %d\n", data);
MPI_Recv(&data, 1, MPI_INT, 0, 5, MPI_COMM_WORLD,
&status);
printf("received data = %d\n", data);
}
MPI_Comm_free(&libcomm);
MPI_Finalize();
return(0);
}