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. */
7 #include "../colls_private.hpp"
9 int bcast_NTSB_segment_size_in_byte = 8192;
12 int bcast__NTSB(void *buf, int count, MPI_Datatype datatype,
13 int root, MPI_Comm comm)
15 int tag = COLL_TAG_BCAST;
21 extent = datatype->get_extent();
26 /* source node and destination nodes (same through out the functions) */
27 int from = (rank - 1) / 2;
28 int to_left = rank * 2 + 1;
29 int to_right = rank * 2 + 2;
35 /* segment is segment size in number of elements (not bytes) */
36 int segment = bcast_NTSB_segment_size_in_byte / extent;
37 segment = segment == 0 ? 1 :segment;
39 int pipe_length = count / segment;
41 /* use for buffer offset for sending and receiving data = segment size in byte */
42 int increment = segment * extent;
44 /* if the input size is not divisible by segment size =>
45 the small remainder will be done with native implementation */
46 int remainder = count % segment;
48 /* if root is not zero send to rank zero first */
51 Request::send(buf, count, datatype, 0, tag, comm);
52 } else if (rank == 0) {
53 Request::recv(buf, count, datatype, root, tag, comm, &status);
57 /* when a message is smaller than a block size => no pipeline */
58 if (count <= segment) {
62 /* case root has only a left child */
64 Request::send(buf, count, datatype, to_left, tag, comm);
66 /* case root has both left and right children */
68 Request::send(buf, count, datatype, to_left, tag, comm);
69 Request::send(buf, count, datatype, to_right, tag, comm);
73 /* case: leaf ==> receive only */
74 else if (to_left == -1) {
75 Request::recv(buf, count, datatype, from, tag, comm, &status);
78 /* case: intermediate node with only left child ==> relay message */
79 else if (to_right == -1) {
80 Request::recv(buf, count, datatype, from, tag, comm, &status);
81 Request::send(buf, count, datatype, to_left, tag, comm);
84 /* case: intermediate node with both left and right children ==> relay message */
86 Request::recv(buf, count, datatype, from, tag, comm, &status);
87 Request::send(buf, count, datatype, to_left, tag, comm);
88 Request::send(buf, count, datatype, to_right, tag, comm);
95 auto* send_request_array = new MPI_Request[2 * (size + pipe_length)];
96 auto* recv_request_array = new MPI_Request[size + pipe_length];
97 auto* send_status_array = new MPI_Status[2 * (size + pipe_length)];
98 auto* recv_status_array = new MPI_Status[size + pipe_length];
102 /* case root has only a left child */
103 if (to_right == -1) {
104 for (i = 0; i < pipe_length; i++) {
105 send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
108 Request::waitall((pipe_length), send_request_array, send_status_array);
110 /* case root has both left and right children */
112 for (i = 0; i < pipe_length; i++) {
113 send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
115 send_request_array[i + pipe_length] = Request::isend((char *) buf + (i * increment), segment, datatype, to_right,
118 Request::waitall((2 * pipe_length), send_request_array, send_status_array);
122 /* case: leaf ==> receive only */
123 else if (to_left == -1) {
124 for (i = 0; i < pipe_length; i++) {
125 recv_request_array[i] = Request::irecv((char *) buf + (i * increment), segment, datatype, from,
128 Request::waitall((pipe_length), recv_request_array, recv_status_array);
131 /* case: intermediate node with only left child ==> relay message */
132 else if (to_right == -1) {
133 for (i = 0; i < pipe_length; i++) {
134 recv_request_array[i] = Request::irecv((char *) buf + (i * increment), segment, datatype, from,
137 for (i = 0; i < pipe_length; i++) {
138 Request::wait(&recv_request_array[i], &status);
139 send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
142 Request::waitall(pipe_length, send_request_array, send_status_array);
145 /* case: intermediate node with both left and right children ==> relay message */
147 for (i = 0; i < pipe_length; i++) {
148 recv_request_array[i] = Request::irecv((char *) buf + (i * increment), segment, datatype, from,
151 for (i = 0; i < pipe_length; i++) {
152 Request::wait(&recv_request_array[i], &status);
153 send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
155 send_request_array[i + pipe_length] = Request::isend((char *) buf + (i * increment), segment, datatype, to_right,
158 Request::waitall((2 * pipe_length), send_request_array, send_status_array);
161 delete[] send_request_array;
162 delete[] recv_request_array;
163 delete[] send_status_array;
164 delete[] recv_status_array;
167 if ((remainder != 0) && (count > segment)) {
168 XBT_INFO("MPI_bcast_NTSB: count is not divisible by block size, use default MPI_bcast for remainder.");
169 colls::bcast((char*)buf + (pipe_length * increment), remainder, datatype, root, comm);