/****************************************************************************** * FILE: mpi_helloBsend.c * DESCRIPTION: * MPI tutorial example code: Simple hello world program that uses blocking * send/receive routines. * Original Source: https://computing.llnl.gov/tutorials/mpi/exercise.html#Exercise1 * How to Compile and Run @euclide: * scp mpi_helloBsend.c epl655u03@euclid.cyi.ac.cy:~/epl372 * ssh epl655u03@euclid.cyi.ac.cy * [epl655u03@euclid epl372]module load goolf/1.6.10 * [epl655u03@euclid epl372]rm *.out * [epl655u03@euclid epl372]mpicc mpi_helloBsend.c -o hello.out * [epl655u03@euclid epl372]sbatch mpirun.sh * [epl655u03@euclid epl372]vi hello.output.out Hello from task 8 on e02! Hello from task 0 on e01! MASTER: Number of MPI tasks is: 16 Hello from task 1 on e01! Hello from task 2 on e01! Hello from task 3 on e01! Hello from task 4 on e01! Hello from task 5 on e01! Hello from task 6 on e01! Hello from task 7 on e01! Hello from task 9 on e02! Hello from task 10 on e02! Hello from task 11 on e02! Hello from task 12 on e02! Hello from task 13 on e02! Hello from task 15 on e02! Hello from task 14 on e02! Task 3 is partner with 11 Task 11 is partner with 3 Task 2 is partner with 10 Task 10 is partner with 2 Task 7 is partner with 15 Task 15 is partner with 7 Task 1 is partner with 9 Task 9 is partner with 1 Task 0 is partner with 8 Task 8 is partner with 0 Task 13 is partner with 5 Task 5 is partner with 13 Task 6 is partner with 14 Task 14 is partner with 6 Task 4 is partner with 12 Task 12 is partner with 4 ******************************************************************************/ #include "mpi.h" #include #include #define MASTER 0 int main (int argc, char *argv[]) { int numtasks, taskid, len; char hostname[MPI_MAX_PROCESSOR_NAME]; int partner, message; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &numtasks); MPI_Comm_rank(MPI_COMM_WORLD,&taskid); MPI_Get_processor_name(hostname, &len); printf ("Hello from task %d on %s!\n", taskid, hostname); if (taskid == MASTER) printf("MASTER: Number of MPI tasks is: %d\n",numtasks); /* determine partner and then send/receive with partner */ if (taskid < numtasks/2) { partner = numtasks/2 + taskid; MPI_Send(&taskid, 1, MPI_INT, partner, 1, MPI_COMM_WORLD); MPI_Recv(&message, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &status); } else if (taskid >= numtasks/2) { partner = taskid - numtasks/2; MPI_Recv(&message, 1, MPI_INT, partner, 1, MPI_COMM_WORLD, &status); MPI_Send(&taskid, 1, MPI_INT, partner, 1, MPI_COMM_WORLD); } /* print partner info and exit*/ printf("Task %d is partner with %d\n",taskid,message); MPI_Finalize(); }