Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4b594e3a22febaac869df53f0eec27770f441112
[simgrid.git] / examples / smpi / bcast.c
1 /* Copyright (c) 2009. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include <mpi.h>
9
10 int main(int argc, char **argv)
11 {
12   int size, rank;
13   int value = 3;
14   double start_timer;
15   int quiet = 0;
16
17   MPI_Init(&argc, &argv);
18   MPI_Comm_size(MPI_COMM_WORLD, &size);
19   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
20
21   if (argc > 1 && !strcmp(argv[1], "-q"))
22     quiet = 1;
23
24   start_timer = MPI_Wtime();
25
26   if (0 == rank) {
27     value = 17;
28   }
29   printf("node %d has value %d before broadcast\n", rank, value);
30   MPI_Bcast(&value, 1, MPI_INT, 0, MPI_COMM_WORLD);
31   printf("node %d has value %d after broadcast\n", rank, value);
32
33   MPI_Barrier(MPI_COMM_WORLD);
34   if (0 == rank && !quiet)
35     printf("Elapsed time on rank %d: %lf s\n", rank,
36            MPI_Wtime() - start_timer);
37   MPI_Finalize();
38   return 0;
39 }