Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f7321d6028e01303a38bcd1ca9fd42baabe6993f
[simgrid.git] / src / smpi / sample / reduce.c
1 /* Copyright (c) 2007, 2009, 2010. 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 root = 0;
14   int value = 1;
15   int sum = -99;
16
17   double start_timer;
18
19
20   MPI_Init(&argc, &argv);
21   MPI_Comm_size(MPI_COMM_WORLD, &size);
22   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
23
24   start_timer = MPI_Wtime();
25
26   printf("rank %d has value %d\n", rank, value);
27   MPI_Reduce(&value, &sum, 1, MPI_INT, MPI_SUM, root, MPI_COMM_WORLD);
28   if (rank == root) {
29     printf("On root: sum=%d\n", sum);
30     printf("Elapsed time=%lf s\n", MPI_Wtime() - start_timer);
31   }
32   MPI_Finalize();
33   return 0;
34 }