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

Example applications
ping_pong_ring.c (HP-UX and Linux)
Appendix A232
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
*/
start = MPI_Wtime();
for (i = 0; i < NLOOPS; i++) {
SETBUF();
SEND(1000 + i);
CLRBUF();
RECV(2000 + i);
CHKBUF();
}
stop = MPI_Wtime();
sprintf(&str[strlen(str)],
"%d bytes: %.2f usec/msg\n", nbytes,
(stop - start) / NLOOPS / 2 * 1024 * 1024);