HP-MPI User's Guide (11th Edition)
Example applications
ping_pong_ring.c (Windows)
Appendix A238
int rank, size;
int root;
int partner;
MPI_Status status;
char *buf, *obuf;
char myhost[MPI_MAX_PROCESSOR_NAME];
int len;
char str[1024];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Get_processor_name(myhost, &len);
if (size < 2) {
if ( ! rank) printf("rping: must have two+ processes\n");
MPI_Finalize();
exit(0);
}
nbytes = (argc > 1) ? atoi(argv[1]) : 0;
if (nbytes < 0) nbytes = 0;
/*
* Page-align buffers and displace them in the cache to avoid
collisions.
*/
buf = (char *) malloc(nbytes + 524288 + (ALIGN - 1));
obuf = buf;
if (buf == 0) {
MPI_Abort(MPI_COMM_WORLD, MPI_ERR_BUFFER);
exit(1);
}
buf = (char *) ((((unsigned long) buf) + (ALIGN - 1)) &
~(ALIGN - 1));
if (rank > 0) buf += 524288;
memset(buf, 0, nbytes);
/*
* Ping-pong.
*/
for (root=0; root<size; root++) {
if (rank == root) {
partner = (root + 1) % size;
sprintf(str, "[%d:%s] ping-pong %d bytes ...\n",
root, myhost, nbytes);
/*
* warm-up loop
*/
for (i = 0; i < 5; i++) {
SEND(1);
RECV(1);
}
/*
* timing loop
*/