Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0a45358480212855c959850f4da27fed3f800745
[simgrid.git] / src / smpi / colls / allreduce / allreduce-rab2.cpp
1 /* Copyright (c) 2013-2019. 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.hpp"
8 //#include <star-reduction.c>
9
10 namespace simgrid{
11 namespace smpi{
12 // this requires that count >= NP
13 int Coll_allreduce_rab2::allreduce(const void *sbuff, void *rbuff,
14                                    int count, MPI_Datatype dtype,
15                                    MPI_Op op, MPI_Comm comm)
16 {
17   MPI_Aint s_extent;
18   int i, rank, nprocs;
19   int nbytes, send_size, s_offset, r_offset;
20   void *recv, *send, *tmp;
21   /*
22      #ifdef MPICH2_REDUCTION
23      MPI_User_function * uop = MPIR_Op_table[op % 16 - 1];
24      #else
25      MPI_User_function *uop;
26      MPIR_OP *op_ptr;
27      op_ptr = MPIR_ToPointer(op);
28      uop  = op_ptr->op;
29      #endif
30    */
31   rank = comm->rank();
32   nprocs = comm->size();
33
34
35   s_extent = dtype->get_extent();
36
37   // uneven count
38   if (count % nprocs) {
39     if (count < nprocs)
40       send_size = nprocs;
41     else
42       send_size = (count + nprocs) / nprocs;
43     nbytes = send_size * s_extent;
44
45     send = (void *) smpi_get_tmp_sendbuffer(s_extent * send_size * nprocs);
46     recv = (void *) smpi_get_tmp_recvbuffer(s_extent * send_size * nprocs);
47     tmp = (void *) smpi_get_tmp_sendbuffer(nbytes);
48
49     memcpy(send, sbuff, s_extent * count);
50
51     Colls::alltoall(send, send_size, dtype, recv, send_size, dtype, comm);
52
53     memcpy(tmp, recv, nbytes);
54
55     for (i = 1, s_offset = nbytes; i < nprocs; i++, s_offset = i * nbytes)
56       if(op!=MPI_OP_NULL) op->apply( (char *) recv + s_offset, tmp, &send_size, dtype);
57
58     Colls::allgather(tmp, send_size, dtype, recv, send_size, dtype, comm);
59     memcpy(rbuff, recv, count * s_extent);
60
61     smpi_free_tmp_buffer(recv);
62     smpi_free_tmp_buffer(tmp);
63     smpi_free_tmp_buffer(send);
64   } else {
65     send = const_cast<void*>(sbuff);
66     send_size = count / nprocs;
67     nbytes = send_size * s_extent;
68     r_offset = rank * nbytes;
69
70     recv = (void *) smpi_get_tmp_recvbuffer(s_extent * send_size * nprocs);
71
72     Colls::alltoall(send, send_size, dtype, recv, send_size, dtype, comm);
73
74     memcpy((char *) rbuff + r_offset, recv, nbytes);
75
76     for (i = 1, s_offset = nbytes; i < nprocs; i++, s_offset = i * nbytes)
77       if(op!=MPI_OP_NULL) op->apply( (char *) recv + s_offset, (char *) rbuff + r_offset,
78                      &send_size, dtype);
79
80     Colls::allgather((char *) rbuff + r_offset, send_size, dtype, rbuff, send_size,
81                   dtype, comm);
82     smpi_free_tmp_buffer(recv);
83   }
84
85   return MPI_SUCCESS;
86 }
87 }
88 }