1 /* Copyright (c) 2013-2019. 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_arrival_pattern_aware_wait_segment_size_in_byte = 8192;
11 #ifndef BCAST_ARRIVAL_PATTERN_AWARE_HEADER_SIZE
12 #define BCAST_ARRIVAL_PATTERN_AWARE_HEADER_SIZE 1024
15 #ifndef BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE
16 #define BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE 128
20 /* Non-topology-specific pipelined linear-bcast function */
21 int Coll_bcast_arrival_pattern_aware_wait::bcast(void *buf, int count,
22 MPI_Datatype datatype,
23 int root, MPI_Comm comm)
28 MPI_Status temp_status_array[BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE];
32 int tag = -COLL_TAG_BCAST;
33 int will_send[BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE];
37 int flag_array[BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE];
38 int already_sent[BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE];
40 int header_buf[BCAST_ARRIVAL_PATTERN_AWARE_HEADER_SIZE];
41 char temp_buf[BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE];
43 int max_node = BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE;
44 int header_size = BCAST_ARRIVAL_PATTERN_AWARE_HEADER_SIZE;
47 extent = datatype->get_extent();
49 /* source and destination */
58 /* segment is segment size in number of elements (not bytes) */
59 int segment = bcast_arrival_pattern_aware_wait_segment_size_in_byte / extent;
60 segment = segment == 0 ? 1 :segment;
62 int pipe_length = count / segment;
64 /* use for buffer offset for sending and receiving data = segment size in byte */
65 int increment = segment * extent;
67 /* if the input size is not divisible by segment size =>
68 the small remainder will be done with native implementation */
69 int remainder = count % segment;
71 /* if root is not zero send to rank zero first
72 this can be modified to make it faster by using logical src, dst.
76 Request::send(buf, count, datatype, 0, tag, comm);
77 } else if (rank == 0) {
78 Request::recv(buf, count, datatype, root, tag, comm, &status);
83 /* value == 0 means root has not send data (or header) to the node yet */
84 for (i = 0; i < max_node; i++) {
88 /* when a message is smaller than a block size => no pipeline */
89 if (count <= segment) {
94 /* start pipeline bcast */
96 MPI_Request* send_request_array = new MPI_Request[size + pipe_length];
97 MPI_Request* recv_request_array = new MPI_Request[size + pipe_length];
98 MPI_Status* send_status_array = new MPI_Status[size + pipe_length];
99 MPI_Status* recv_status_array = new MPI_Status[size + pipe_length];
106 for (i = 0; i < BCAST_ARRIVAL_PATTERN_AWARE_MAX_NODE; i++)
108 while (sent_count < (size - 1)) {
111 /* loop k times to let more processes arrive before start sending data */
112 for (k = 0; k < 3; k++) {
113 for (i = 1; i < size; i++) {
114 if ((already_sent[i] == 0) && (will_send[i] == 0)) {
115 Request::iprobe(i, MPI_ANY_TAG, comm, &flag_array[i],
116 &temp_status_array[i]);
117 if (flag_array[i] == 1) {
119 Request::recv(&temp_buf[i], 1, MPI_CHAR, i, tag, comm,
129 /* recv 1-byte message */
130 for (i = 1; i < size; i++) {
132 if ((will_send[i] == 1) && (already_sent[i] == 0)) {
133 header_buf[header_index] = i;
137 /* will send in the next step */
142 /* send header followed by data */
143 if (header_index != 0) {
144 header_buf[header_index] = -1;
148 Request::send(header_buf, header_size, MPI_INT, to, tag, comm);
150 /* send data - pipeline */
151 for (i = 0; i < pipe_length; i++) {
152 send_request_array[i] = Request::isend((char *)buf + (i * increment), segment, datatype, to, tag, comm);
154 Request::waitall((pipe_length), send_request_array, send_status_array);
158 /* end - send header followed by data */
159 /* randomly MPI_Send to one node */
160 /* this part has been commented out - performance-wise */
162 /* search for the first node that never received data before */
163 for (i = 0; i < size; i++) {
166 if (already_sent[i] == 0) {
171 Request::send(header_buf, header_size, MPI_INT, to, tag, comm);
173 /* still need to chop data so that we can use the same non-root code */
174 for (j = 0; j < pipe_length; j++) {
175 Request::send((char *)buf + (j * increment), segment, datatype, to, tag, comm);
180 } /* end - while (send_count < size-1) loop */
187 /* send 1-byte message to root */
188 Request::send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
190 /* wait for header forward when required */
191 request = Request::irecv(header_buf, header_size, MPI_INT, MPI_ANY_SOURCE, tag, comm);
192 Request::wait(&request, MPI_STATUS_IGNORE);
194 /* search for where it is */
196 while (rank != header_buf[myordering]) {
200 to = header_buf[myordering + 1];
201 if (myordering == 0) {
204 from = header_buf[myordering - 1];
207 /* send header when required */
209 Request::send(header_buf, header_size, MPI_INT, to, tag, comm);
214 for (i = 0; i < pipe_length; i++) {
215 recv_request_array[i] = Request::irecv((char *)buf + (i * increment), segment, datatype, from, tag, comm);
220 for (i = 0; i < pipe_length; i++) {
221 Request::wait(&recv_request_array[i], MPI_STATUS_IGNORE);
222 send_request_array[i] = Request::isend((char *)buf + (i * increment), segment, datatype, to, tag, comm);
224 Request::waitall((pipe_length), send_request_array, send_status_array);
229 Request::waitall((pipe_length), recv_request_array, recv_status_array);
233 delete[] send_request_array;
234 delete[] recv_request_array;
235 delete[] send_status_array;
236 delete[] recv_status_array;
239 /* when count is not divisible by block size, use default BCAST for the remainder */
240 if ((remainder != 0) && (count > segment)) {
241 XBT_WARN("MPI_bcast_arrival_pattern_aware_wait use default MPI_bcast.");
242 Colls::bcast((char *)buf + (pipe_length * increment), remainder, datatype, root, comm);