Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / smpi / colls / bcast / bcast-NTSB.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
9 int bcast_NTSB_segment_size_in_byte = 8192;
10 namespace simgrid::smpi {
11 int bcast__NTSB(void *buf, int count, MPI_Datatype datatype,
12                 int root, MPI_Comm comm)
13 {
14   int tag = COLL_TAG_BCAST;
15   MPI_Status status;
16   int rank, size;
17   int i;
18
19   MPI_Aint extent;
20   extent = datatype->get_extent();
21
22   rank = comm->rank();
23   size = comm->size();
24
25   /* source node and destination nodes (same through out the functions) */
26   int from = (rank - 1) / 2;
27   int to_left = rank * 2 + 1;
28   int to_right = rank * 2 + 2;
29   if (to_left >= size)
30     to_left = -1;
31   if (to_right >= size)
32     to_right = -1;
33
34   /* segment is segment size in number of elements (not bytes) */
35   int segment = bcast_NTSB_segment_size_in_byte / extent;
36   segment =  segment == 0 ? 1 :segment;
37   /* pipeline length */
38   int pipe_length = count / segment;
39
40   /* use for buffer offset for sending and receiving data = segment size in byte */
41   int increment = segment * extent;
42
43   /* if the input size is not divisible by segment size =>
44      the small remainder will be done with native implementation */
45   int remainder = count % segment;
46
47   /* if root is not zero send to rank zero first */
48   if (root != 0) {
49     if (rank == root) {
50       Request::send(buf, count, datatype, 0, tag, comm);
51     } else if (rank == 0) {
52       Request::recv(buf, count, datatype, root, tag, comm, &status);
53     }
54   }
55
56   /* when a message is smaller than a block size => no pipeline */
57   if (count <= segment) {
58
59     /* case: root */
60     if (rank == 0) {
61       /* case root has only a left child */
62       if (to_right == -1) {
63         Request::send(buf, count, datatype, to_left, tag, comm);
64       }
65       /* case root has both left and right children */
66       else {
67         Request::send(buf, count, datatype, to_left, tag, comm);
68         Request::send(buf, count, datatype, to_right, tag, comm);
69       }
70     }
71
72     /* case: leaf ==> receive only */
73     else if (to_left == -1) {
74       Request::recv(buf, count, datatype, from, tag, comm, &status);
75     }
76
77     /* case: intermediate node with only left child ==> relay message */
78     else if (to_right == -1) {
79       Request::recv(buf, count, datatype, from, tag, comm, &status);
80       Request::send(buf, count, datatype, to_left, tag, comm);
81     }
82
83     /* case: intermediate node with both left and right children ==> relay message */
84     else {
85       Request::recv(buf, count, datatype, from, tag, comm, &status);
86       Request::send(buf, count, datatype, to_left, tag, comm);
87       Request::send(buf, count, datatype, to_right, tag, comm);
88     }
89     return MPI_SUCCESS;
90   }
91   // pipelining
92   else {
93
94     auto* send_request_array = new MPI_Request[2 * (size + pipe_length)];
95     auto* recv_request_array = new MPI_Request[size + pipe_length];
96     auto* send_status_array  = new MPI_Status[2 * (size + pipe_length)];
97     auto* recv_status_array  = new MPI_Status[size + pipe_length];
98
99     /* case: root */
100     if (rank == 0) {
101       /* case root has only a left child */
102       if (to_right == -1) {
103         for (i = 0; i < pipe_length; i++) {
104           send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
105                     tag + i, comm);
106         }
107         Request::waitall((pipe_length), send_request_array, send_status_array);
108       }
109       /* case root has both left and right children */
110       else {
111         for (i = 0; i < pipe_length; i++) {
112           send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
113                     tag + i, comm);
114           send_request_array[i + pipe_length] = Request::isend((char *) buf + (i * increment), segment, datatype, to_right,
115                     tag + i, comm);
116         }
117         Request::waitall((2 * pipe_length), send_request_array, send_status_array);
118       }
119     }
120
121     /* case: leaf ==> receive only */
122     else if (to_left == -1) {
123       for (i = 0; i < pipe_length; i++) {
124         recv_request_array[i] = Request::irecv((char *) buf + (i * increment), segment, datatype, from,
125                   tag + i, comm);
126       }
127       Request::waitall((pipe_length), recv_request_array, recv_status_array);
128     }
129
130     /* case: intermediate node with only left child ==> relay message */
131     else if (to_right == -1) {
132       for (i = 0; i < pipe_length; i++) {
133         recv_request_array[i] = Request::irecv((char *) buf + (i * increment), segment, datatype, from,
134                   tag + i, comm);
135       }
136       for (i = 0; i < pipe_length; i++) {
137         Request::wait(&recv_request_array[i], &status);
138         send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
139                   tag + i, comm);
140       }
141       Request::waitall(pipe_length, send_request_array, send_status_array);
142
143     }
144     /* case: intermediate node with both left and right children ==> relay message */
145     else {
146       for (i = 0; i < pipe_length; i++) {
147         recv_request_array[i] = Request::irecv((char *) buf + (i * increment), segment, datatype, from,
148                   tag + i, comm);
149       }
150       for (i = 0; i < pipe_length; i++) {
151         Request::wait(&recv_request_array[i], &status);
152         send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
153                   tag + i, comm);
154         send_request_array[i + pipe_length] = Request::isend((char *) buf + (i * increment), segment, datatype, to_right,
155                   tag + i, comm);
156       }
157       Request::waitall((2 * pipe_length), send_request_array, send_status_array);
158     }
159
160     delete[] send_request_array;
161     delete[] recv_request_array;
162     delete[] send_status_array;
163     delete[] recv_status_array;
164   }                             /* end pipeline */
165
166   if ((remainder != 0) && (count > segment)) {
167     XBT_INFO("MPI_bcast_NTSB: count is not divisible by block size, use default MPI_bcast for remainder.");
168     colls::bcast((char*)buf + (pipe_length * increment), remainder, datatype, root, comm);
169   }
170
171   return MPI_SUCCESS;
172 }
173
174 } // namespace simgrid::smpi