Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / src / smpi / colls / allreduce / allreduce-smp-binomial.cpp
1 /* Copyright (c) 2013-2023. 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 /* IMPLEMENTED BY PITCH PATARASUK
9    Non-topology-specific (however, number of cores/node need to be changed)
10    all-reduce operation designed for smp clusters
11    It uses 2-layer communication: binomial for both intra-communication
12    inter-communication*/
13
14 /* ** NOTE **
15    Use -DMPICH2 if this code does not compile.
16    MPICH1 code also work on MPICH2 on our cluster and the performance are similar.
17    This code assume commutative and associative reduce operator (MPI_SUM, MPI_MAX, etc).
18 */
19
20 //#include <star-reduction.c>
21
22 /*
23 This function performs all-reduce operation as follow.
24 1) binomial_tree reduce inside each SMP node
25 2) binomial_tree reduce intra-communication between root of each SMP node
26 3) binomial_tree bcast intra-communication between root of each SMP node
27 4) binomial_tree bcast inside each SMP node
28 */
29 namespace simgrid::smpi {
30 int allreduce__smp_binomial(const void *send_buf, void *recv_buf,
31                             int count, MPI_Datatype dtype,
32                             MPI_Op op, MPI_Comm comm)
33 {
34   int comm_size, rank;
35   int tag = COLL_TAG_ALLREDUCE;
36   int mask, src, dst;
37
38   if(comm->get_leaders_comm()==MPI_COMM_NULL){
39     comm->init_smp();
40   }
41   int num_core=1;
42   if (comm->is_uniform()){
43     num_core = comm->get_intra_comm()->size();
44   }
45   MPI_Status status;
46
47   comm_size=comm->size();
48   rank=comm->rank();
49   MPI_Aint extent, lb;
50   dtype->extent(&lb, &extent);
51   unsigned char* tmp_buf = smpi_get_tmp_sendbuffer(count * extent);
52
53   /* compute intra and inter ranking */
54   int intra_rank, inter_rank;
55   intra_rank = rank % num_core;
56   inter_rank = rank / num_core;
57
58   /* size of processes participate in intra communications =>
59      should be equal to number of machines */
60   int inter_comm_size = (comm_size + num_core - 1) / num_core;
61
62   /* copy input buffer to output buffer */
63   Request::sendrecv(send_buf, count, dtype, rank, tag,
64                recv_buf, count, dtype, rank, tag, comm, &status);
65
66   /* start binomial reduce intra communication inside each SMP node */
67   mask = 1;
68   while (mask < num_core) {
69     if ((mask & intra_rank) == 0) {
70       src = (inter_rank * num_core) + (intra_rank | mask);
71       if (src < comm_size) {
72         Request::recv(tmp_buf, count, dtype, src, tag, comm, &status);
73         if(op!=MPI_OP_NULL) op->apply( tmp_buf, recv_buf, &count, dtype);
74       }
75     } else {
76       dst = (inter_rank * num_core) + (intra_rank & (~mask));
77       Request::send(recv_buf, count, dtype, dst, tag, comm);
78       break;
79     }
80     mask <<= 1;
81   }
82
83   /* start binomial reduce inter-communication between each SMP nodes:
84      each node only have one process that can communicate to other nodes */
85   if (intra_rank == 0) {
86     mask = 1;
87     while (mask < inter_comm_size) {
88       if ((mask & inter_rank) == 0) {
89         src = (inter_rank | mask) * num_core;
90         if (src < comm_size) {
91           Request::recv(tmp_buf, count, dtype, src, tag, comm, &status);
92           if(op!=MPI_OP_NULL) op->apply( tmp_buf, recv_buf, &count, dtype);
93         }
94       } else {
95         dst = (inter_rank & (~mask)) * num_core;
96         Request::send(recv_buf, count, dtype, dst, tag, comm);
97         break;
98       }
99       mask <<= 1;
100     }
101   }
102
103   /* start binomial broadcast inter-communication between each SMP nodes:
104      each node only have one process that can communicate to other nodes */
105   if (intra_rank == 0) {
106     mask = 1;
107     while (mask < inter_comm_size) {
108       if (inter_rank & mask) {
109         src = (inter_rank - mask) * num_core;
110         Request::recv(recv_buf, count, dtype, src, tag, comm, &status);
111         break;
112       }
113       mask <<= 1;
114     }
115     mask >>= 1;
116
117     while (mask > 0) {
118       if (inter_rank < inter_comm_size) {
119         dst = (inter_rank + mask) * num_core;
120         if (dst < comm_size) {
121           Request::send(recv_buf, count, dtype, dst, tag, comm);
122         }
123       }
124       mask >>= 1;
125     }
126   }
127
128   /* start binomial broadcast intra-communication inside each SMP nodes */
129   int num_core_in_current_smp = num_core;
130   if (inter_rank == (inter_comm_size - 1)) {
131     num_core_in_current_smp = comm_size - (inter_rank * num_core);
132   }
133   mask = 1;
134   while (mask < num_core_in_current_smp) {
135     if (intra_rank & mask) {
136       src = (inter_rank * num_core) + (intra_rank - mask);
137       Request::recv(recv_buf, count, dtype, src, tag, comm, &status);
138       break;
139     }
140     mask <<= 1;
141   }
142   mask >>= 1;
143
144   while (mask > 0) {
145     dst = (inter_rank * num_core) + (intra_rank + mask);
146     if (dst < comm_size) {
147       Request::send(recv_buf, count, dtype, dst, tag, comm);
148     }
149     mask >>= 1;
150   }
151
152   smpi_free_tmp_buffer(tmp_buf);
153   return MPI_SUCCESS;
154 }
155 } // namespace simgrid::smpi