#include #include "mpi.h" /* * The program sends an integer to a slave process. The slave * increases the value by one and then sends it back. * The program should be used with two processes. */ int main(int argc, char *argv[]) { int message, length, source, dest, tag; int my_rank; int n_procs; /* number of processes */ MPI_Status status; MPI_Init(&argc, &argv); /* Start up MPI */ /* Find out the number of n_processes and my rank */ MPI_Comm_size(MPI_COMM_WORLD, &n_procs); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); tag = 1; length = 1; /* Length of message */ if (my_rank == 0) { /* I'm the master process */ printf("Number of processes = %d\n", n_procs); dest = 1; /* Send to the other process */ message = 1; /* Just send one int */ /* Send message to slave */ MPI_Send(&message, length, MPI_INT, dest, tag, MPI_COMM_WORLD); printf("After MPI_Send\n"); source = 1; /* Receive message from slave */ MPI_Recv(&message, length, MPI_INT, source, tag, MPI_COMM_WORLD, &status); printf("After MPI_Recv, message = %d\n", message); } else { /* I'm the slave process */ source = 0; /* Receive message from master */ MPI_Recv(&message, length, MPI_INT, source, tag, MPI_COMM_WORLD, &status); dest = 0; /* Send to the other process */ message++; /* Increase message */ /* Send message to master */ MPI_Send(&message, length, MPI_INT, dest, tag, MPI_COMM_WORLD); } MPI_Finalize(); /* Shut down MPI */ return 0; }