Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MPI_Comm -> C++
[simgrid.git] / src / smpi / colls / reduce-flat-tree.cpp
1 /* Copyright (c) 2013-2014. 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 "colls_private.h"
8 //#include <star-reduction.c>
9
10 int
11 smpi_coll_tuned_reduce_flat_tree(void *sbuf, void *rbuf, int count,
12                                  MPI_Datatype dtype, MPI_Op op,
13                                  int root, MPI_Comm comm)
14 {
15   int i, tag = COLL_TAG_REDUCE;
16   int size;
17   int rank;
18   MPI_Aint extent;
19   char *origin = 0;
20   char *inbuf;
21   MPI_Status status;
22
23   rank = comm->rank();
24   size = comm->size();
25
26   /* If not root, send data to the root. */
27   extent = smpi_datatype_get_extent(dtype);
28
29   if (rank != root) {
30     smpi_mpi_send(sbuf, count, dtype, root, tag, comm);
31     return 0;
32   }
33
34   /* Root receives and reduces messages.  Allocate buffer to receive
35      messages. */
36
37   if (size > 1)
38     origin = (char *) smpi_get_tmp_recvbuffer(count * extent);
39
40
41   /* Initialize the receive buffer. */
42   if (rank == (size - 1))
43     smpi_mpi_sendrecv(sbuf, count, dtype, rank, tag,
44                  rbuf, count, dtype, rank, tag, comm, &status);
45   else
46     smpi_mpi_recv(rbuf, count, dtype, size - 1, tag, comm, &status);
47
48   /* Loop receiving and calling reduction function (C or Fortran). */
49
50   for (i = size - 2; i >= 0; --i) {
51     if (rank == i)
52       inbuf = static_cast<char*>(sbuf);
53     else {
54       smpi_mpi_recv(origin, count, dtype, i, tag, comm, &status);
55       inbuf = origin;
56     }
57
58     /* Call reduction function. */
59     smpi_op_apply(op, inbuf, rbuf, &count, &dtype);
60
61   }
62
63   if (origin)
64     smpi_free_tmp_buffer(origin);
65
66   /* All done */
67   return 0;
68 }