Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-MPI User's Guide > Appendix A Example applications

communicator.c

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

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);
}

communicator output

The output from running the communicator executable is shown below. The application was run with -np = 2.

received libcomm data = 6789
received data = 12345
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1979-2007 Hewlett-Packard Development Company, L.P.