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

Example applications
ping_pong.c
Appendix A226
* Page-align buffers and displace them in the cache to avoid
collisions.
*/
buf = (char *) malloc(nbytes + 524288 + (ALIGN - 1));
if (buf == 0) {
MPI_Abort(MPI_COMM_WORLD, MPI_ERR_BUFFER);
exit(1);
}
buf = (char *) ((((unsigned long) buf) + (ALIGN - 1)) &
~(ALIGN - 1));
if (rank == 1) buf += 524288;
memset(buf, 0, nbytes);
/*
* Ping-pong.
*/
if (rank == 0) {
printf("ping-pong %d bytes ...\n", nbytes);
/*
* warm-up loop
*/
for (i = 0; i < 5; i++) {
MPI_Send(buf, nbytes, MPI_CHAR, 1, 1,
MPI_COMM_WORLD);
MPI_Recv(buf, nbytes, MPI_CHAR,1, 1,
MPI_COMM_WORLD,
&status);
}
/*
* timing loop
*/
start = MPI_Wtime();
for (i = 0; i < NLOOPS; i++) {
#ifdef CHECK
for (j = 0; j < nbytes; j++) buf[j] = (char)
(j + i);
#endif
MPI_Send(buf, nbytes, MPI_CHAR,1, 1000 + i,
MPI_COMM_WORLD);
#ifdef CHECK
memset(buf, 0, nbytes);
#endif
MPI_Recv(buf, nbytes, MPI_CHAR,1, 2000 + i,
MPI_COMM_WORLD,&status);