1 /* Copyright (c) 2013-2020. 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 /* 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
13 The communication are done in a pipeline fashion */
15 /* this is a default segment size for pipelining,
16 but it is typically passed as a command line argument */
17 int allreduce_smp_binomial_pipeline_segment_size = 4096;
20 This code is modified from allreduce-smp-binomial.c by wrapping the code with pipeline effect as follow
21 for (loop over pipelength) {
22 smp-binomial main code;
27 Use -DMPICH2 if this code does not compile.
28 MPICH1 code also work on MPICH2 on our cluster and the performance are similar.
29 This code assume commutative and associative reduce operator (MPI_SUM, MPI_MAX, etc).
33 This fucntion performs all-reduce operation as follow. ** in a pipeline fashion **
34 1) binomial_tree reduce inside each SMP node
35 2) binomial_tree reduce intra-communication between root of each SMP node
36 3) binomial_tree bcast intra-communication between root of each SMP node
37 4) binomial_tree bcast inside each SMP node
41 int allreduce__smp_binomial_pipeline(const void *send_buf,
42 void *recv_buf, int count,
44 MPI_Op op, MPI_Comm comm)
47 int tag = COLL_TAG_ALLREDUCE;
50 if(comm->get_leaders_comm()==MPI_COMM_NULL){
54 if (comm->is_uniform()){
55 num_core = comm->get_intra_comm()->size();
58 comm_size = comm->size();
61 extent = dtype->get_extent();
62 unsigned char* tmp_buf = smpi_get_tmp_sendbuffer(count * extent);
64 int intra_rank, inter_rank;
65 intra_rank = rank % num_core;
66 inter_rank = rank / num_core;
71 int pcount = allreduce_smp_binomial_pipeline_segment_size;
76 /* size of processes participate in intra communications =>
77 should be equal to number of machines */
78 int inter_comm_size = (comm_size + num_core - 1) / num_core;
80 /* copy input buffer to output buffer */
81 Request::sendrecv(send_buf, count, dtype, rank, tag,
82 recv_buf, count, dtype, rank, tag, comm, &status);
84 /* compute pipe length */
86 pipelength = count / pcount;
88 /* pipelining over pipelength (+3 is because we have 4 stages:
89 reduce-intra, reduce-inter, bcast-inter, bcast-intra */
90 for (phase = 0; phase < pipelength + 3; phase++) {
92 /* start binomial reduce intra communication inside each SMP node */
93 if (phase < pipelength) {
95 while (mask < num_core) {
96 if ((mask & intra_rank) == 0) {
97 src = (inter_rank * num_core) + (intra_rank | mask);
98 if (src < comm_size) {
99 recv_offset = phase * pcount * extent;
100 Request::recv(tmp_buf, pcount, dtype, src, tag, comm, &status);
101 if(op!=MPI_OP_NULL) op->apply( tmp_buf, (char *)recv_buf + recv_offset, &pcount, dtype);
104 send_offset = phase * pcount * extent;
105 dst = (inter_rank * num_core) + (intra_rank & (~mask));
106 Request::send((char *)recv_buf + send_offset, pcount, dtype, dst, tag, comm);
113 /* start binomial reduce inter-communication between each SMP nodes:
114 each node only have one process that can communicate to other nodes */
115 if ((phase > 0) && (phase < (pipelength + 1))) {
116 if (intra_rank == 0) {
119 while (mask < inter_comm_size) {
120 if ((mask & inter_rank) == 0) {
121 src = (inter_rank | mask) * num_core;
122 if (src < comm_size) {
123 recv_offset = (phase - 1) * pcount * extent;
124 Request::recv(tmp_buf, pcount, dtype, src, tag, comm, &status);
125 if(op!=MPI_OP_NULL) op->apply( tmp_buf, (char *)recv_buf + recv_offset, &pcount, dtype);
128 dst = (inter_rank & (~mask)) * num_core;
129 send_offset = (phase - 1) * pcount * extent;
130 Request::send((char *)recv_buf + send_offset, pcount, dtype, dst, tag, comm);
138 /* start binomial broadcast inter-communication between each SMP nodes:
139 each node only have one process that can communicate to other nodes */
140 if ((phase > 1) && (phase < (pipelength + 2))) {
141 if (intra_rank == 0) {
143 while (mask < inter_comm_size) {
144 if (inter_rank & mask) {
145 src = (inter_rank - mask) * num_core;
146 recv_offset = (phase - 2) * pcount * extent;
147 Request::recv((char *)recv_buf + recv_offset, pcount, dtype, src, tag, comm,
156 if (inter_rank < inter_comm_size) {
157 dst = (inter_rank + mask) * num_core;
158 if (dst < comm_size) {
159 //printf("Node %d send to node %d when mask is %d\n", rank, dst, mask);
160 send_offset = (phase - 2) * pcount * extent;
161 Request::send((char *)recv_buf + send_offset, pcount, dtype, dst, tag, comm);
169 /* start binomial broadcast intra-communication inside each SMP nodes */
171 int num_core_in_current_smp = num_core;
172 if (inter_rank == (inter_comm_size - 1)) {
173 num_core_in_current_smp = comm_size - (inter_rank * num_core);
176 while (mask < num_core_in_current_smp) {
177 if (intra_rank & mask) {
178 src = (inter_rank * num_core) + (intra_rank - mask);
179 recv_offset = (phase - 3) * pcount * extent;
180 Request::recv((char *)recv_buf + recv_offset, pcount, dtype, src, tag, comm,
189 dst = (inter_rank * num_core) + (intra_rank + mask);
190 if (dst < comm_size) {
191 send_offset = (phase - 3) * pcount * extent;
192 Request::send((char *)recv_buf + send_offset, pcount, dtype, dst, tag, comm);
199 smpi_free_tmp_buffer(tmp_buf);