1 /* Copyright (c) 2013-2019. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "../colls_private.hpp"
10 //#include <star-reduction.c>
13 int Coll_reduce_binomial::reduce(void *sendbuf, void *recvbuf, int count,
14 MPI_Datatype datatype, MPI_Op op, int root,
19 int mask, relrank, source;
21 int tag = COLL_TAG_REDUCE;
24 MPI_Aint true_lb, true_extent;
28 comm_size = comm->size();
30 extent = datatype->get_extent();
32 tmp_buf = (void *) smpi_get_tmp_sendbuffer(count * extent);
33 int is_commutative = (op==MPI_OP_NULL || op->is_commutative());
41 relrank = (rank - lroot + comm_size) % comm_size;
43 datatype->extent(&true_lb, &true_extent);
45 /* adjust for potential negative lower bound in datatype */
46 tmp_buf = (void *)((char*)tmp_buf - true_lb);
48 /* If I'm not the root, then my recvbuf may not be valid, therefore
49 I have to allocate a temporary one */
51 recvbuf = (void*)smpi_get_tmp_recvbuffer(count * std::max(extent, true_extent));
52 recvbuf = (void *)((char*)recvbuf - true_lb);
54 if ((rank != root) || (sendbuf != MPI_IN_PLACE)) {
55 Datatype::copy(sendbuf, count, datatype, recvbuf,count, datatype);
58 while (mask < comm_size) {
60 if ((mask & relrank) == 0) {
61 source = (relrank | mask);
62 if (source < comm_size) {
63 source = (source + lroot) % comm_size;
64 Request::recv(tmp_buf, count, datatype, source, tag, comm, &status);
67 if(op!=MPI_OP_NULL) op->apply( tmp_buf, recvbuf, &count, datatype);
69 if(op!=MPI_OP_NULL) op->apply( recvbuf, tmp_buf, &count, datatype);
70 Datatype::copy(tmp_buf, count, datatype,recvbuf, count, datatype);
74 dst = ((relrank & (~mask)) + lroot) % comm_size;
75 Request::send(recvbuf, count, datatype, dst, tag, comm);
81 if (not is_commutative && (root != 0)) {
83 Request::send(recvbuf, count, datatype, root,tag, comm);
84 }else if (rank == root){
85 Request::recv(recvbuf, count, datatype, 0, tag, comm, &status);
90 smpi_free_tmp_buffer(recvbuf);
92 smpi_free_tmp_buffer(tmp_buf);