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"
8 //#include <star-reduction.c>
10 int reduce_arrival_pattern_aware_segment_size_in_byte = 8192;
13 #define HEADER_SIZE 1024
21 /* Non-topology-specific pipelined linear-reduce function */
22 int reduce__arrival_pattern_aware(const void *buf, void *rbuf,
24 MPI_Datatype datatype,
28 int rank = comm->rank();
29 int tag = -COLL_TAG_REDUCE;
33 MPI_Status temp_status_array[MAX_NODE];
35 int size = comm->size();
40 int flag_array[MAX_NODE];
41 int already_received[MAX_NODE];
43 int header_buf[HEADER_SIZE];
44 char temp_buf[MAX_NODE];
47 datatype->extent(&lb, &extent);
49 /* source and destination */
52 /* segment is segment size in number of elements (not bytes) */
53 int segment = reduce_arrival_pattern_aware_segment_size_in_byte / extent;
56 int pipe_length = count / segment;
58 /* use for buffer offset for sending and receiving data = segment size in byte */
59 int increment = segment * extent;
61 /* if the input size is not divisible by segment size =>
62 the small remainder will be done with native implementation */
63 int remainder = count % segment;
66 /* value == 0 means root has not send data (or header) to the node yet */
67 for (i = 0; i < MAX_NODE; i++) {
68 already_received[i] = 0;
71 unsigned char* tmp_buf = smpi_get_tmp_sendbuffer(count * extent);
73 Request::sendrecv(buf, count, datatype, rank, tag, rbuf, count, datatype, rank,
78 /* when a message is smaller than a block size => no pipeline */
79 if (count <= segment) {
84 while (sent_count < (size - 1)) {
86 for (i = 1; i < size; i++) {
87 if (already_received[i] == 0) {
88 Request::iprobe(i, MPI_ANY_TAG, comm, &flag_array[i], MPI_STATUSES_IGNORE);
89 simgrid::s4u::this_actor::sleep_for(0.0001);
94 /* recv 1-byte message */
95 for (i = 0; i < size; i++) {
99 /* 1-byte message arrive */
100 if ((flag_array[i] == 1) && (already_received[i] == 0)) {
101 Request::recv(temp_buf, 1, MPI_CHAR, i, tag, comm, &status);
102 header_buf[header_index] = i;
107 //printf("root send to %d recv from %d : data = ",to,from);
109 for (i=0;i<=header_index;i++) {
110 printf("%d ",header_buf[i]);
114 /* will receive in the next step */
115 already_received[i] = 1;
119 /* send header followed by receive and reduce data */
120 if (header_index != 0) {
121 header_buf[header_index] = -1;
123 from = header_buf[header_index - 1];
125 Request::send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
126 Request::recv(tmp_buf, count, datatype, from, tag, comm, &status);
127 if(op!=MPI_OP_NULL) op->apply( tmp_buf, rbuf, &count, datatype);
136 /* send 1-byte message to root */
137 Request::send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
139 /* wait for header and data, forward when required */
140 Request::recv(header_buf, HEADER_SIZE, MPI_INT, MPI_ANY_SOURCE, tag, comm,
142 // Request::recv(buf,count,datatype,MPI_ANY_SOURCE,tag,comm,&status);
144 /* search for where it is */
146 while (rank != header_buf[myordering]) {
151 if (header_buf[myordering + 1] != -1) {
152 Request::send(header_buf, HEADER_SIZE, MPI_INT, header_buf[myordering + 1],
155 //printf("node %d ordering %d\n",rank,myordering);
157 /* receive, reduce, and forward data */
160 if (myordering == 0) {
161 if (header_buf[myordering + 1] == -1) {
164 to = header_buf[myordering + 1];
166 Request::send(rbuf, count, datatype, to, tag, comm);
169 /* recv, reduce, send */
171 if (header_buf[myordering + 1] == -1) {
174 to = header_buf[myordering + 1];
176 from = header_buf[myordering - 1];
177 Request::recv(tmp_buf, count, datatype, from, tag, comm, &status);
178 if(op!=MPI_OP_NULL) op->apply( tmp_buf, rbuf, &count, datatype);
179 Request::send(rbuf, count, datatype, to, tag, comm);
185 // printf("node %d start\n",rank);
187 auto* send_request_array = new MPI_Request[size + pipe_length];
188 auto* recv_request_array = new MPI_Request[size + pipe_length];
189 auto* send_status_array = new MPI_Status[size + pipe_length];
190 auto* recv_status_array = new MPI_Status[size + pipe_length];
195 int will_send[MAX_NODE];
196 for (i = 0; i < MAX_NODE; i++)
199 /* loop until all data are received (sent) */
200 while (sent_count < (size - 1)) {
202 for (k = 0; k < 1; k++) {
203 for (i = 1; i < size; i++) {
206 if ((already_received[i] == 0) && (will_send[i] == 0)) {
207 Request::iprobe(i, MPI_ANY_TAG, comm, &flag_array[i],
208 &temp_status_array[i]);
209 if (flag_array[i] == 1) {
211 Request::recv(&temp_buf[i], 1, MPI_CHAR, i, tag, comm,
213 //printf("recv from %d\n",i);
218 } /* end of probing */
222 /* recv 1-byte message */
223 for (i = 1; i < size; i++) {
226 /* message arrived in this round (put in the header) */
227 if ((will_send[i] == 1) && (already_received[i] == 0)) {
228 header_buf[header_index] = i;
232 /* will send in the next step */
233 already_received[i] = 1;
237 /* send header followed by data */
238 if (header_index != 0) {
239 header_buf[header_index] = -1;
243 Request::send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
245 /* recv data - pipeline */
246 from = header_buf[header_index - 1];
247 for (i = 0; i < pipe_length; i++) {
248 Request::recv(tmp_buf + (i * increment), segment, datatype, from, tag,
250 if(op!=MPI_OP_NULL) op->apply( tmp_buf + (i * increment),
251 (char *)rbuf + (i * increment), &segment, datatype);
254 } /* while loop (sent_count < size-1 ) */
260 /* send 1-byte message to root */
261 Request::send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
264 /* wait for header forward when required */
265 request=Request::irecv(header_buf, HEADER_SIZE, MPI_INT, MPI_ANY_SOURCE, tag, comm);
266 Request::wait(&request, MPI_STATUS_IGNORE);
268 /* search for where it is */
271 while (rank != header_buf[myordering]) {
275 /* send header when required */
276 if (header_buf[myordering + 1] != -1) {
277 Request::send(header_buf, HEADER_SIZE, MPI_INT, header_buf[myordering + 1],
281 /* (receive, reduce), and send data */
282 if (header_buf[myordering + 1] == -1) {
285 to = header_buf[myordering + 1];
289 if (myordering == 0) {
290 for (i = 0; i < pipe_length; i++) {
291 send_request_array[i]= Request::isend((char *)rbuf + (i * increment), segment, datatype, to, tag, comm);
293 Request::waitall((pipe_length), send_request_array, send_status_array);
296 /* receive, reduce, and send */
298 from = header_buf[myordering - 1];
299 for (i = 0; i < pipe_length; i++) {
300 recv_request_array[i]=Request::irecv(tmp_buf + (i * increment), segment, datatype, from, tag, comm);
302 for (i = 0; i < pipe_length; i++) {
303 Request::wait(&recv_request_array[i], MPI_STATUS_IGNORE);
304 if(op!=MPI_OP_NULL) op->apply( tmp_buf + (i * increment), (char *)rbuf + (i * increment),
306 send_request_array[i]=Request::isend((char *)rbuf + (i * increment), segment, datatype, to, tag, comm);
308 Request::waitall((pipe_length), send_request_array, send_status_array);
312 delete[] send_request_array;
313 delete[] recv_request_array;
314 delete[] send_status_array;
315 delete[] recv_status_array;
317 //printf("node %d done\n",rank);
321 /* if root is not zero send root after finished
322 this can be modified to make it faster by using logical src, dst.
326 Request::send(rbuf, count, datatype, root, tag, comm);
327 } else if (rank == root) {
328 Request::recv(rbuf, count, datatype, 0, tag, comm, &status);
333 /* when count is not divisible by block size, use default BCAST for the remainder */
334 if ((remainder != 0) && (count > segment)) {
335 reduce__default((char*)buf + (pipe_length * increment), (char*)rbuf + (pipe_length * increment),
336 remainder, datatype, op, root, comm);
339 smpi_free_tmp_buffer(tmp_buf);