1 /* Copyright (c) 2013-2023. 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"
8 //#include <star-reduction.c>
10 namespace simgrid::smpi {
11 // this requires that count >= NP
12 int allreduce__rab2(const void *sbuff, void *rbuff,
13 int count, MPI_Datatype dtype,
14 MPI_Op op, MPI_Comm comm)
18 int nbytes, send_size, s_offset, r_offset;
20 #ifdef MPICH2_REDUCTION
21 MPI_User_function * uop = MPIR_Op_table[op % 16 - 1];
23 MPI_User_function *uop;
25 op_ptr = MPIR_ToPointer(op);
30 nprocs = comm->size();
33 s_extent = dtype->get_extent();
40 send_size = (count + nprocs) / nprocs;
41 nbytes = send_size * s_extent;
43 unsigned char* send = smpi_get_tmp_sendbuffer(s_extent * send_size * nprocs);
44 unsigned char* recv = smpi_get_tmp_recvbuffer(s_extent * send_size * nprocs);
45 unsigned char* tmp = smpi_get_tmp_sendbuffer(nbytes);
47 memcpy(send, sbuff, s_extent * count);
49 colls::alltoall(send, send_size, dtype, recv, send_size, dtype, comm);
51 memcpy(tmp, recv, nbytes);
53 for (i = 1, s_offset = nbytes; i < nprocs; i++, s_offset = i * nbytes)
54 if (op != MPI_OP_NULL)
55 op->apply(recv + s_offset, tmp, &send_size, dtype);
57 colls::allgather(tmp, send_size, dtype, recv, send_size, dtype, comm);
58 memcpy(rbuff, recv, count * s_extent);
60 smpi_free_tmp_buffer(recv);
61 smpi_free_tmp_buffer(tmp);
62 smpi_free_tmp_buffer(send);
64 const void* send = sbuff;
65 send_size = count / nprocs;
66 nbytes = send_size * s_extent;
67 r_offset = rank * nbytes;
69 unsigned char* recv = smpi_get_tmp_recvbuffer(s_extent * send_size * nprocs);
71 colls::alltoall(send, send_size, dtype, recv, send_size, dtype, comm);
73 memcpy((char *) rbuff + r_offset, recv, nbytes);
75 for (i = 1, s_offset = nbytes; i < nprocs; i++, s_offset = i * nbytes)
76 if (op != MPI_OP_NULL)
77 op->apply(recv + s_offset, static_cast<char*>(rbuff) + r_offset, &send_size, dtype);
79 colls::allgather((char*)rbuff + r_offset, send_size, dtype, rbuff, send_size, dtype, comm);
80 smpi_free_tmp_buffer(recv);
85 } // namespace simgrid::smpi