1 /* Copyright (c) 2013-2020. 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 static int bcast_NTSL_segment_size_in_byte = 8192;
11 #define HEADER_SIZE 1024
16 /* Non-topology-specific pipelined linear-bcast function */
17 int bcast__arrival_pattern_aware(void *buf, int count,
18 MPI_Datatype datatype, int root,
21 int tag = -COLL_TAG_BCAST;
25 MPI_Status temp_status_array[MAX_NODE];
32 int flag_array[MAX_NODE];
33 int already_sent[MAX_NODE];
34 int to_clean[MAX_NODE];
35 int header_buf[HEADER_SIZE];
36 char temp_buf[MAX_NODE];
39 extent = datatype->get_extent();
50 /* segment is segment size in number of elements (not bytes) */
51 int segment = bcast_NTSL_segment_size_in_byte / extent;
52 segment = segment == 0 ? 1 :segment;
54 int pipe_length = count / segment;
56 /* use for buffer offset for sending and receiving data = segment size in byte */
57 int increment = segment * extent;
59 /* if the input size is not divisible by segment size =>
60 the small remainder will be done with native implementation */
61 int remainder = count % segment;
63 /* if root is not zero send to rank zero first
64 this can be modified to make it faster by using logical src, dst.
68 Request::send(buf, count, datatype, 0, tag, comm);
69 } else if (rank == 0) {
70 Request::recv(buf, count, datatype, root, tag, comm, &status);
74 /* value == 0 means root has not send data (or header) to the node yet */
75 for (i = 0; i < MAX_NODE; i++) {
80 /* when a message is smaller than a block size => no pipeline */
81 if (count <= segment) {
85 while (sent_count < (size - 1)) {
86 for (i = 1; i < size; i++) {
87 Request::iprobe(i, MPI_ANY_TAG, comm, &flag_array[i],
92 /* recv 1-byte message */
93 for (i = 1; i < size; i++) {
96 if ((flag_array[i] == 1) && (already_sent[i] == 0)) {
97 Request::recv(temp_buf, 1, MPI_CHAR, i, tag, comm, &status);
98 header_buf[header_index] = i;
102 /* will send in the next step */
107 /* send header followed by data */
108 if (header_index != 0) {
109 header_buf[header_index] = -1;
111 Request::send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
112 Request::send(buf, count, datatype, to, tag, comm);
115 /* randomly MPI_Send to one */
117 /* search for the first node that never received data before */
118 for (i = 1; i < size; i++) {
119 if (already_sent[i] == 0) {
122 Request::send(header_buf, HEADER_SIZE, MPI_INT, i, tag, comm);
123 Request::send(buf, count, datatype, i, tag, comm);
138 /* send 1-byte message to root */
139 Request::send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
141 /* wait for header and data, forward when required */
142 Request::recv(header_buf, HEADER_SIZE, MPI_INT, MPI_ANY_SOURCE, tag, comm,
144 Request::recv(buf, count, datatype, MPI_ANY_SOURCE, tag, comm, &status);
146 /* search for where it is */
148 while (rank != header_buf[myordering]) {
152 /* send header followed by data */
153 if (header_buf[myordering + 1] != -1) {
154 Request::send(header_buf, HEADER_SIZE, MPI_INT, header_buf[myordering + 1],
156 Request::send(buf, count, datatype, header_buf[myordering + 1], tag, comm);
162 auto* send_request_array = new MPI_Request[size + pipe_length];
163 auto* recv_request_array = new MPI_Request[size + pipe_length];
164 auto* send_status_array = new MPI_Status[size + pipe_length];
165 auto* recv_status_array = new MPI_Status[size + pipe_length];
168 //double start2 = MPI_Wtime();
171 while (sent_count < (size - 1)) {
173 //start = MPI_Wtime();
174 for (i = 1; i < size; i++) {
175 Request::iprobe(i, MPI_ANY_TAG, comm, &flag_array[i],
176 &temp_status_array[i]);
178 //total = MPI_Wtime() - start;
180 //printf("Iprobe time = %.2f\n",total);
184 /* recv 1-byte message */
185 for (i = 1; i < size; i++) {
187 if ((flag_array[i] == 1) && (already_sent[i] == 0)) {
188 Request::recv(&temp_buf[i], 1, MPI_CHAR, i, tag, comm,
190 header_buf[header_index] = i;
194 /* will send in the next step */
198 //total = MPI_Wtime() - start;
200 //printf("Recv 1-byte time = %.2f\n",total);
203 if (header_index != 0) {
204 printf("header index = %d node = ",header_index);
205 for (i=0;i<header_index;i++) {
206 printf("%d ",header_buf[i]);
212 /* send header followed by data */
213 if (header_index != 0) {
214 header_buf[header_index] = -1;
217 //start = MPI_Wtime();
220 Request::send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
222 //total = MPI_Wtime() - start;
224 //printf("\tSend header to %d time = %.2f\n",to,total);
226 //start = MPI_Wtime();
228 /* send data - non-pipeline case */
231 //if (header_index == 1) {
232 Request::send(buf, count, datatype, to, tag, comm);
236 /* send data - pipeline */
238 for (i = 0; i < pipe_length; i++) {
239 Request::send((char *)buf + (i * increment), segment, datatype, to, tag, comm);
241 //Request::waitall((pipe_length), send_request_array, send_status_array);
243 //total = MPI_Wtime() - start;
245 //printf("\tSend data to %d time = %.2f\n",to,total);
251 /* randomly MPI_Send to one node */
253 /* search for the first node that never received data before */
254 for (i = 1; i < size; i++) {
255 if (already_sent[i] == 0) {
260 //start = MPI_Wtime();
261 Request::send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
263 /* still need to chop data so that we can use the same non-root code */
264 for (j = 0; j < pipe_length; j++) {
265 Request::send((char *)buf + (j * increment), segment, datatype, to, tag,
269 //Request::send(buf,count,datatype,to,tag,comm);
270 //Request::wait(&request,MPI_STATUS_IGNORE);
272 //total = MPI_Wtime() - start;
274 //printf("SEND TO SINGLE node %d time = %.2f\n",i,total);
287 for(i=0; i<size; i++)
288 if(to_clean[i]!=0)Request::recv(&temp_buf[i], 1, MPI_CHAR, i, tag, comm,
290 //total = MPI_Wtime() - start2;
292 //printf("Node zero iter = %d time = %.2f\n",iteration,total);
298 /* send 1-byte message to root */
299 Request::send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
301 /* wait for header forward when required */
302 request = Request::irecv(header_buf, HEADER_SIZE, MPI_INT, MPI_ANY_SOURCE, tag, comm);
303 Request::wait(&request, MPI_STATUS_IGNORE);
305 /* search for where it is */
307 while (rank != header_buf[myordering]) {
311 /* send header when required */
312 if (header_buf[myordering + 1] != -1) {
313 Request::send(header_buf, HEADER_SIZE, MPI_INT, header_buf[myordering + 1],
320 //if (header_buf[1] == -1) {
321 request = Request::irecv(buf, count, datatype, 0, tag, comm);
322 Request::wait(&request, MPI_STATUS_IGNORE);
323 //printf("\t\tnode %d ordering = %d receive data from root\n",rank,myordering);
325 for (i = 0; i < pipe_length; i++) {
326 recv_request_array[i] = Request::irecv((char *)buf + (i * increment), segment, datatype, MPI_ANY_SOURCE,
332 if (header_buf[myordering + 1] != -1) {
333 for (i = 0; i < pipe_length; i++) {
334 Request::wait(&recv_request_array[i], MPI_STATUS_IGNORE);
335 send_request_array[i] = Request::isend((char *)buf + (i * increment), segment, datatype,
336 header_buf[myordering + 1], tag, comm);
338 Request::waitall((pipe_length), send_request_array, send_status_array);
340 Request::waitall(pipe_length, recv_request_array, recv_status_array);
345 delete[] send_request_array;
346 delete[] recv_request_array;
347 delete[] send_status_array;
348 delete[] recv_status_array;
351 /* when count is not divisible by block size, use default BCAST for the remainder */
352 if ((remainder != 0) && (count > segment)) {
353 XBT_WARN("MPI_bcast_arrival_pattern_aware use default MPI_bcast.");
354 colls::bcast((char*)buf + (pipe_length * increment), remainder, datatype, root, comm);