| 1 | /******************************************************************************
|
|---|
| 2 | * FILE: mpi_ringtopo.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * MPI tutorial example code: Non-Blocking Send/Receive
|
|---|
| 5 | * AUTHOR: Blaise Barney
|
|---|
| 6 | * LAST REVISED: 04/02/05
|
|---|
| 7 | ******************************************************************************/
|
|---|
| 8 | #include "mpi.h"
|
|---|
| 9 | #include <stdio.h>
|
|---|
| 10 | #include <stdlib.h>
|
|---|
| 11 |
|
|---|
| 12 | int main (int argc, char *argv[])
|
|---|
| 13 | {
|
|---|
| 14 | int numtasks, rank, next, prev, buf[2], tag1=1, tag2=2;
|
|---|
| 15 | MPI_Request reqs[4];
|
|---|
| 16 | MPI_Status stats[4];
|
|---|
| 17 |
|
|---|
| 18 | MPI_Init(&argc,&argv);
|
|---|
| 19 | MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
|
|---|
| 20 | MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
|---|
| 21 |
|
|---|
| 22 | prev = rank-1;
|
|---|
| 23 | next = rank+1;
|
|---|
| 24 | if (rank == 0) prev = numtasks - 1;
|
|---|
| 25 | if (rank == (numtasks - 1)) next = 0;
|
|---|
| 26 |
|
|---|
| 27 | MPI_Irecv(&buf[0], 1, MPI_INT, prev, tag1, MPI_COMM_WORLD, &reqs[0]);
|
|---|
| 28 | MPI_Irecv(&buf[1], 1, MPI_INT, next, tag2, MPI_COMM_WORLD, &reqs[1]);
|
|---|
| 29 |
|
|---|
| 30 | MPI_Isend(&rank, 1, MPI_INT, prev, tag1, MPI_COMM_WORLD, &reqs[2]);
|
|---|
| 31 | MPI_Isend(&rank, 1, MPI_INT, next, tag1, MPI_COMM_WORLD, &reqs[3]);
|
|---|
| 32 |
|
|---|
| 33 | MPI_Waitall(4, reqs, stats);
|
|---|
| 34 | printf("Task %d communicated with tasks %d & %d\n",rank,prev,next);
|
|---|
| 35 |
|
|---|
| 36 | MPI_Finalize();
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|