-
/************************************************************************
- * This file has been written as a sample solution to an exercise in a
+ * This file has been written as a sample solution to an exercise in a
* course given at the Edinburgh Parallel Computing Centre. It is made
* freely available with the understanding that every copy of this file
* must include this header and that EPCC takes no responsibility for
* the use of the enclosed teaching material.
*
- * Author: Joel Malard
+ * Author: Joel Malard
*
* Contact: epcc-tec@epcc.ed.ac.uk
*
- * Purpose: A program to try out non-blocking point-to-point
+ * Purpose: A program to try out non-blocking point-to-point
* communications.
*
* Contents: C source code.
*
************************************************************************/
-#include <stdio.h>
-#include <mpi.h>
-#define tag_right 201
-#define tag_left 102
+#include <stdio.h>
+#include <mpi.h>
+
+#define TAG_RIGHT 123
int main (int argc, char *argv[])
{
-int ierror, rank, my_rank, size;
-int right, left;
-int other, sum, i;
-int message;
+ int ierror, rank, size;
+ int right, left, count, i;
+ int message, sum;
-MPI_Status send_status;
-MPI_Status recv_status;
-MPI_Request request;
+ MPI_Status send_status;
+ MPI_Status recv_status;
+ MPI_Request request;
- MPI_Init(&argc, &argv);
+ MPI_Init(&argc, &argv);
+ MPI_Comm_rank(MPI_COMM_WORLD, &rank);
+ MPI_Comm_size(MPI_COMM_WORLD, &size);
- MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
- MPI_Comm_size(MPI_COMM_WORLD, &size);
+ count = 1; /* only sending a single MPI_INT */
+ right = (rank + 1) % size;
+ left = (rank - 1 + size) % size; /* the '+ size' ensures 'left >= 0'. */
- right = my_rank + 1;
- if (right == size) right = 0;
+ sum = 0;
+ message = rank;
- left = my_rank - 1;
- if (left == -1) left = size-1;
+ for (i=0; i < size; i++) {
+ /* Issend because? */
+ MPI_Issend(&message, count, MPI_INT, right, TAG_RIGHT,
+ MPI_COMM_WORLD, &request);
- sum = 0;
- message = my_rank;
+ MPI_Recv(&message, count, MPI_INT, left, TAG_RIGHT,
+ MPI_COMM_WORLD, &recv_status);
- for( i = 0; i < size; i++) {
+ /* Wait because? */
+ MPI_Wait(&request, &send_status);
- MPI_Issend(&message, 1, MPI_INT, right, tag_right,
- MPI_COMM_WORLD, &request);
-
- MPI_Recv(&other, 1, MPI_INT, left, tag_right,
- MPI_COMM_WORLD, &recv_status);
-
- MPI_Wait(&request, &send_status);
-
- sum = sum + other;
- message = other;
-
- }
+ sum += message;
+ }
- printf ("PE%d:\tSum = %d\n", my_rank, sum);
-
- MPI_Finalize();
+ printf ("PE%d:\tSum = %d\n", rank, sum);
+ MPI_Finalize();
}
-