1 /* Copyright (c) 2013-2022. 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. */
8 * implemented by Pitch Patarasuk, 07/01/2007
10 #include "../colls_private.hpp"
11 //#include <star-reduction.c>
15 This function performs all-reduce operation as follow.
16 1) binomial_tree reduce inside each SMP node
17 2) reduce-scatter -inter between root of each SMP node
18 3) allgather - inter between root of each SMP node
19 4) binomial_tree bcast inside each SMP node
21 namespace simgrid::smpi {
22 int allreduce__smp_rsag_rab(const void *sbuf, void *rbuf, int count,
23 MPI_Datatype dtype, MPI_Op op,
27 int tag = COLL_TAG_ALLREDUCE;
30 if(comm->get_leaders_comm()==MPI_COMM_NULL){
34 if (comm->is_uniform()){
35 num_core = comm->get_intra_comm()->size();
38 comm_size = comm->size();
40 if((comm_size&(comm_size-1)))
41 throw std::invalid_argument(
42 "allreduce smp rsag rab algorithm can't be used with non power of two number of processes!");
46 extent = dtype->get_extent();
47 unsigned char* tmp_buf = smpi_get_tmp_sendbuffer(count * extent);
49 int intra_rank, inter_rank;
50 intra_rank = rank % num_core;
51 inter_rank = rank / num_core;
53 int inter_comm_size = (comm_size + num_core - 1) / num_core;
55 Request::sendrecv(sbuf, count, dtype, rank, tag,
56 rbuf, count, dtype, rank, tag, comm, &status);
58 // SMP_binomial_reduce
60 while (mask < num_core) {
61 if ((mask & intra_rank) == 0) {
62 int src = (inter_rank * num_core) + (intra_rank | mask);
63 // if (src < ((inter_rank + 1) * num_core)) {
64 if (src < comm_size) {
65 Request::recv(tmp_buf, count, dtype, src, tag, comm, &status);
66 if(op!=MPI_OP_NULL) op->apply( tmp_buf, rbuf, &count, dtype);
70 int dst = (inter_rank * num_core) + (intra_rank & (~mask));
71 Request::send(rbuf, count, dtype, dst, tag, comm);
78 // INTER: reduce-scatter
79 if (intra_rank == 0) {
81 int base_offset, send_base_offset, recv_base_offset, recv_chunk;
82 int curr_count, i, recv_offset, send_offset;
86 recv_chunk = extent * count / (comm_size / num_core);
89 curr_count = count / 2;
92 while (mask < (comm_size / num_core)) {
93 int dst = inter_rank ^ mask;
97 if (inter_rank & mask) {
98 recv_base_offset = base_offset + curr_count;
99 send_base_offset = base_offset;
100 base_offset = recv_base_offset;
104 recv_base_offset = base_offset;
105 send_base_offset = base_offset + curr_count;
107 send_offset = send_base_offset * extent;
108 recv_offset = recv_base_offset * extent;
110 Request::sendrecv((char *)rbuf + send_offset, curr_count, dtype, (dst * num_core), tag,
111 tmp_buf, curr_count, dtype, (dst * num_core), tag,
114 if(op!=MPI_OP_NULL) op->apply( tmp_buf, (char *)rbuf + recv_offset, &curr_count, dtype);
123 int size = (comm_size / num_core) / 2;
126 while (mask < (comm_size / num_core)) {
127 if (inter_rank & mask) {
138 // destination pair for both send and recv
139 int dst = inter_rank ^ mask;
142 send_base_offset = base_offset;
143 if (inter_rank & mask) {
144 recv_base_offset = base_offset - i;
147 recv_base_offset = base_offset + i;
149 send_offset = send_base_offset * recv_chunk;
150 recv_offset = recv_base_offset * recv_chunk;
152 Request::sendrecv((char *)rbuf + send_offset, curr_count, dtype, (dst * num_core), tag,
153 (char *)rbuf + recv_offset, curr_count, dtype, (dst * num_core), tag,
165 // intra SMP binomial bcast
167 int num_core_in_current_smp = num_core;
168 if (inter_rank == (inter_comm_size - 1)) {
169 num_core_in_current_smp = comm_size - (inter_rank * num_core);
172 while (mask < num_core_in_current_smp) {
173 if (intra_rank & mask) {
174 int src = (inter_rank * num_core) + (intra_rank - mask);
175 Request::recv(rbuf, count, dtype, src, tag, comm, &status);
184 int dst = (inter_rank * num_core) + (intra_rank + mask);
185 if (dst < comm_size) {
186 Request::send(rbuf, count, dtype, dst, tag, comm);
192 smpi_free_tmp_buffer(tmp_buf);
195 } // namespace simgrid::smpi