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"
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)
14 int tag = COLL_TAG_BCAST;
20 extent = datatype->get_extent();
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;
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;
38 int pipe_length = count / segment;
40 /* use for buffer offset for sending and receiving data = segment size in byte */
41 int increment = segment * extent;
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;
47 /* if root is not zero send to rank zero first */
50 Request::send(buf, count, datatype, 0, tag, comm);
51 } else if (rank == 0) {
52 Request::recv(buf, count, datatype, root, tag, comm, &status);
56 /* when a message is smaller than a block size => no pipeline */
57 if (count <= segment) {
61 /* case root has only a left child */
63 Request::send(buf, count, datatype, to_left, tag, comm);
65 /* case root has both left and right children */
67 Request::send(buf, count, datatype, to_left, tag, comm);
68 Request::send(buf, count, datatype, to_right, tag, comm);
72 /* case: leaf ==> receive only */
73 else if (to_left == -1) {
74 Request::recv(buf, count, datatype, from, tag, comm, &status);
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);
83 /* case: intermediate node with both left and right children ==> relay message */
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);
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];
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,
107 Request::waitall((pipe_length), send_request_array, send_status_array);
109 /* case root has both left and right children */
111 for (i = 0; i < pipe_length; i++) {
112 send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
114 send_request_array[i + pipe_length] = Request::isend((char *) buf + (i * increment), segment, datatype, to_right,
117 Request::waitall((2 * pipe_length), send_request_array, send_status_array);
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,
127 Request::waitall((pipe_length), recv_request_array, recv_status_array);
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,
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,
141 Request::waitall(pipe_length, send_request_array, send_status_array);
144 /* case: intermediate node with both left and right children ==> relay message */
146 for (i = 0; i < pipe_length; i++) {
147 recv_request_array[i] = Request::irecv((char *) buf + (i * increment), segment, datatype, from,
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,
154 send_request_array[i + pipe_length] = Request::isend((char *) buf + (i * increment), segment, datatype, to_right,
157 Request::waitall((2 * pipe_length), send_request_array, send_status_array);
160 delete[] send_request_array;
161 delete[] recv_request_array;
162 delete[] send_status_array;
163 delete[] recv_status_array;
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);
174 } // namespace simgrid::smpi