|
| 1 | +#include <stdlib.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | +#include <stdbool.h> |
| 5 | +#include <unistd.h> |
| 6 | +#include <mpi.h> |
| 7 | + |
| 8 | +#define ORTE_IOF_BASE_MSG_MAX 2048 |
| 9 | + |
| 10 | +int main(int argc, char *argv[]) |
| 11 | +{ |
| 12 | + int i, rank, size, next, prev, tag = 201; |
| 13 | + int pos, msgsize, nbytes; |
| 14 | + bool done; |
| 15 | + char *msg; |
| 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 | + msg = malloc(ORTE_IOF_BASE_MSG_MAX); |
| 26 | + pos = 0; |
| 27 | + nbytes = 0; |
| 28 | + |
| 29 | + if (0 == rank) { |
| 30 | + while (0 != (msgsize = read(0, msg, ORTE_IOF_BASE_MSG_MAX))) { |
| 31 | + fprintf(stderr, "Rank %d: sending blob %d\n", rank, pos); |
| 32 | + if (msgsize > 0) { |
| 33 | + MPI_Bcast(msg, ORTE_IOF_BASE_MSG_MAX, MPI_BYTE, 0, MPI_COMM_WORLD); |
| 34 | + } |
| 35 | + ++pos; |
| 36 | + nbytes += msgsize; |
| 37 | + } |
| 38 | + fprintf(stderr, "Rank %d: sending termination blob %d\n", rank, pos); |
| 39 | + memset(msg, 0, ORTE_IOF_BASE_MSG_MAX); |
| 40 | + MPI_Bcast(msg, ORTE_IOF_BASE_MSG_MAX, MPI_BYTE, 0, MPI_COMM_WORLD); |
| 41 | + MPI_Barrier(MPI_COMM_WORLD); |
| 42 | + } else { |
| 43 | + while (1) { |
| 44 | + MPI_Bcast(msg, ORTE_IOF_BASE_MSG_MAX, MPI_BYTE, 0, MPI_COMM_WORLD); |
| 45 | + fprintf(stderr, "Rank %d: recvd blob %d\n", rank, pos); |
| 46 | + ++pos; |
| 47 | + done = true; |
| 48 | + for (i=0; i < ORTE_IOF_BASE_MSG_MAX; i++) { |
| 49 | + if (0 != msg[i]) { |
| 50 | + done = false; |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | + if (done) { |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + fprintf(stderr, "Rank %d: recv done\n", rank); |
| 59 | + MPI_Barrier(MPI_COMM_WORLD); |
| 60 | + } |
| 61 | + |
| 62 | + fprintf(stderr, "Rank %d has completed bcast\n", rank); |
| 63 | + MPI_Finalize(); |
| 64 | + return 0; |
| 65 | +} |
0 commit comments