/
barrierC.c
42 lines (32 loc) · 1.72 KB
/
barrierC.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/** -*- fill-column:110 -*-
@file barrierC.c
@author Mitch Richling <https://www.mitchr.me/>
@Copyright Copyright 1999 by Mitch Richling. All rights reserved.
@brief Demonstrate MPI_Barrier.@EOL
@Keywords LAM MPICH OpenMPI MPI
@Std C89 MPI1
This simple little program is closely related to the C bindings "hello world" program. It is
intend to illustrate the MPI_Barrier function for application synchronization.
Not all platforms allow console I/O on all MPI processes. On such platforms, this program can
fail.
*/
#include <unistd.h> /* UNIX std stf POSIX */
#include <stdio.h> /* I/O lib C89 */
#include <mpi.h> /* MPI Std MPI */
int main(int argc, char *argv[]) {
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
printf("I am %d of %d (sleep for %d seconds)\n", rank, size, rank/2+1);
/* Sleep for a moment to help demo things.. */
sleep(rank/2+1);
/* The MPI_Barrier function provides for simple synchronization across a communication group. Every process
in the group will "stop" on the barrier call until EVERY process gets to the call. When they all reach
the barrier, all processes start up again. */
printf("I am %d of %d. Waiting for barrier...)\n", rank, size);
MPI_Barrier(MPI_COMM_WORLD);
printf("I am %d of %d. Past barrier. Gonna exit now...)\n", rank, size);
MPI_Finalize();
return 0;
} /* end func main */