root/orte/test/mpi/ring.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 #include <stdlib.h>
   2 #include <stdio.h>
   3 #include <mpi.h>
   4 
   5 #define SIZE 20
   6 #define POS 10
   7 #define INITIAL_VALUE 10
   8 
   9 int main(int argc, char *argv[])
  10 {
  11     int i, rank, size, next, prev, tag = 201;
  12     int array_size = SIZE;
  13     int pos = POS;
  14     int *send_array;
  15     int *recv_array;
  16 
  17     MPI_Init(&argc, &argv);
  18     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  19     MPI_Comm_size(MPI_COMM_WORLD, &size);
  20 
  21     fprintf(stderr, "Rank %d has cleared MPI_Init\n", rank);
  22 
  23     next = (rank + 1) % size;
  24     prev = (rank + size - 1) % size;
  25     send_array = malloc(sizeof(int) * SIZE);
  26     recv_array = malloc(sizeof(int) * SIZE);
  27 
  28     for (i = 0; i < array_size; ++i) {
  29         send_array[i] = 17;
  30         recv_array[i] = -1;
  31     }
  32 
  33     if (0 == rank) {
  34         send_array[pos] = INITIAL_VALUE;
  35         MPI_Send(send_array, array_size, MPI_INT, next, tag,
  36                  MPI_COMM_WORLD);
  37     }
  38 
  39     while (1) {
  40         recv_array[pos] = -1;
  41         MPI_Recv(recv_array, array_size, MPI_INT, prev, tag,
  42                  MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  43         send_array[pos] = recv_array[pos];
  44         if (rank == 0) {
  45             --send_array[pos];
  46         }
  47         MPI_Send(send_array, array_size, MPI_INT, next, tag, MPI_COMM_WORLD);
  48         if (0 == send_array[pos]) {
  49             break;
  50         }
  51     }
  52 
  53     if (rank == 0) {
  54         MPI_Recv(recv_array, array_size, MPI_INT, prev, tag,
  55                  MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  56     }
  57 
  58     fprintf(stderr, "Rank %d has completed ring\n", rank);
  59     MPI_Barrier(MPI_COMM_WORLD);
  60     fprintf(stderr, "Rank %d has completed MPI_Barrier\n", rank);
  61     MPI_Finalize();
  62     return 0;
  63 }

/* [<][>][^][v][top][bottom][index][help] */