Logo AND Algorithmique Numérique Distribuée

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