1 /* Copyright (c) 2013-2017. 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 Coll_bcast_NTSB::bcast(void *buf, int count, MPI_Datatype datatype,
13 int root, MPI_Comm comm)
15 int tag = COLL_TAG_BCAST;
20 MPI_Request *send_request_array;
21 MPI_Request *recv_request_array;
22 MPI_Status *send_status_array;
23 MPI_Status *recv_status_array;
26 extent = datatype->get_extent();
31 /* source node and destination nodes (same through out the functions) */
32 int from = (rank - 1) / 2;
33 int to_left = rank * 2 + 1;
34 int to_right = rank * 2 + 2;
40 /* segment is segment size in number of elements (not bytes) */
41 int segment = bcast_NTSB_segment_size_in_byte / extent;
42 segment = segment == 0 ? 1 :segment;
44 int pipe_length = count / segment;
46 /* use for buffer offset for sending and receiving data = segment size in byte */
47 int increment = segment * extent;
49 /* if the input size is not divisible by segment size =>
50 the small remainder will be done with native implementation */
51 int remainder = count % segment;
53 /* if root is not zero send to rank zero first */
56 Request::send(buf, count, datatype, 0, tag, comm);
57 } else if (rank == 0) {
58 Request::recv(buf, count, datatype, root, tag, comm, &status);
62 /* when a message is smaller than a block size => no pipeline */
63 if (count <= segment) {
67 /* case root has only a left child */
69 Request::send(buf, count, datatype, to_left, tag, comm);
71 /* case root has both left and right children */
73 Request::send(buf, count, datatype, to_left, tag, comm);
74 Request::send(buf, count, datatype, to_right, tag, comm);
78 /* case: leaf ==> receive only */
79 else if (to_left == -1) {
80 Request::recv(buf, count, datatype, from, tag, comm, &status);
83 /* case: intermidiate node with only left child ==> relay message */
84 else if (to_right == -1) {
85 Request::recv(buf, count, datatype, from, tag, comm, &status);
86 Request::send(buf, count, datatype, to_left, tag, comm);
89 /* case: intermidiate node with both left and right children ==> relay message */
91 Request::recv(buf, count, datatype, from, tag, comm, &status);
92 Request::send(buf, count, datatype, to_left, tag, comm);
93 Request::send(buf, count, datatype, to_right, tag, comm);
101 (MPI_Request *) xbt_malloc(2 * (size + pipe_length) * sizeof(MPI_Request));
103 (MPI_Request *) xbt_malloc((size + pipe_length) * sizeof(MPI_Request));
105 (MPI_Status *) xbt_malloc(2 * (size + pipe_length) * sizeof(MPI_Status));
107 (MPI_Status *) xbt_malloc((size + pipe_length) * sizeof(MPI_Status));
113 /* case root has only a left child */
114 if (to_right == -1) {
115 for (i = 0; i < pipe_length; i++) {
116 send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
119 Request::waitall((pipe_length), send_request_array, send_status_array);
121 /* case root has both left and right children */
123 for (i = 0; i < pipe_length; i++) {
124 send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
126 send_request_array[i + pipe_length] = Request::isend((char *) buf + (i * increment), segment, datatype, to_right,
129 Request::waitall((2 * pipe_length), send_request_array, send_status_array);
133 /* case: leaf ==> receive only */
134 else if (to_left == -1) {
135 for (i = 0; i < pipe_length; i++) {
136 recv_request_array[i] = Request::irecv((char *) buf + (i * increment), segment, datatype, from,
139 Request::waitall((pipe_length), recv_request_array, recv_status_array);
142 /* case: intermidiate node with only left child ==> relay message */
143 else if (to_right == -1) {
144 for (i = 0; i < pipe_length; i++) {
145 recv_request_array[i] = Request::irecv((char *) buf + (i * increment), segment, datatype, from,
148 for (i = 0; i < pipe_length; i++) {
149 Request::wait(&recv_request_array[i], &status);
150 send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
153 Request::waitall(pipe_length, send_request_array, send_status_array);
156 /* case: intermidiate node with both left and right children ==> relay message */
158 for (i = 0; i < pipe_length; i++) {
159 recv_request_array[i] = Request::irecv((char *) buf + (i * increment), segment, datatype, from,
162 for (i = 0; i < pipe_length; i++) {
163 Request::wait(&recv_request_array[i], &status);
164 send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
166 send_request_array[i + pipe_length] = Request::isend((char *) buf + (i * increment), segment, datatype, to_right,
169 Request::waitall((2 * pipe_length), send_request_array, send_status_array);
172 free(send_request_array);
173 free(recv_request_array);
174 free(send_status_array);
175 free(recv_status_array);
178 /* when count is not divisible by block size, use default BCAST for the remainder */
179 if ((remainder != 0) && (count > segment)) {
180 XBT_WARN("MPI_bcast_NTSB use default MPI_bcast.");
181 Colls::bcast((char *) buf + (pipe_length * increment), remainder, datatype,