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"
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 Coll_reduce_arrival_pattern_aware::reduce(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],
90 simcall_process_sleep(0.0001);
95 /* recv 1-byte message */
96 for (i = 0; i < size; i++) {
100 /* 1-byte message arrive */
101 if ((flag_array[i] == 1) && (already_received[i] == 0)) {
102 Request::recv(temp_buf, 1, MPI_CHAR, i, tag, comm, &status);
103 header_buf[header_index] = i;
108 //printf("root send to %d recv from %d : data = ",to,from);
110 for (i=0;i<=header_index;i++) {
111 printf("%d ",header_buf[i]);
115 /* will receive in the next step */
116 already_received[i] = 1;
120 /* send header followed by receive and reduce data */
121 if (header_index != 0) {
122 header_buf[header_index] = -1;
124 from = header_buf[header_index - 1];
126 Request::send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
127 Request::recv(tmp_buf, count, datatype, from, tag, comm, &status);
128 if(op!=MPI_OP_NULL) op->apply( tmp_buf, rbuf, &count, datatype);
137 /* send 1-byte message to root */
138 Request::send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
140 /* wait for header and data, forward when required */
141 Request::recv(header_buf, HEADER_SIZE, MPI_INT, MPI_ANY_SOURCE, tag, comm,
143 // Request::recv(buf,count,datatype,MPI_ANY_SOURCE,tag,comm,&status);
145 /* search for where it is */
147 while (rank != header_buf[myordering]) {
152 if (header_buf[myordering + 1] != -1) {
153 Request::send(header_buf, HEADER_SIZE, MPI_INT, header_buf[myordering + 1],
156 //printf("node %d ordering %d\n",rank,myordering);
158 /* receive, reduce, and forward data */
161 if (myordering == 0) {
162 if (header_buf[myordering + 1] == -1) {
165 to = header_buf[myordering + 1];
167 Request::send(rbuf, count, datatype, to, tag, comm);
170 /* recv, reduce, send */
172 if (header_buf[myordering + 1] == -1) {
175 to = header_buf[myordering + 1];
177 from = header_buf[myordering - 1];
178 Request::recv(tmp_buf, count, datatype, from, tag, comm, &status);
179 if(op!=MPI_OP_NULL) op->apply( tmp_buf, rbuf, &count, datatype);
180 Request::send(rbuf, count, datatype, to, tag, comm);
186 // printf("node %d start\n",rank);
188 MPI_Request* send_request_array = new MPI_Request[size + pipe_length];
189 MPI_Request* recv_request_array = new MPI_Request[size + pipe_length];
190 MPI_Status* send_status_array = new MPI_Status[size + pipe_length];
191 MPI_Status* recv_status_array = new MPI_Status[size + pipe_length];
196 int will_send[MAX_NODE];
197 for (i = 0; i < MAX_NODE; i++)
200 /* loop until all data are received (sent) */
201 while (sent_count < (size - 1)) {
203 for (k = 0; k < 1; k++) {
204 for (i = 1; i < size; i++) {
207 if ((already_received[i] == 0) && (will_send[i] == 0)) {
208 Request::iprobe(i, MPI_ANY_TAG, comm, &flag_array[i],
209 &temp_status_array[i]);
210 if (flag_array[i] == 1) {
212 Request::recv(&temp_buf[i], 1, MPI_CHAR, i, tag, comm,
214 //printf("recv from %d\n",i);
219 } /* end of probing */
223 /* recv 1-byte message */
224 for (i = 1; i < size; i++) {
227 /* message arrived in this round (put in the header) */
228 if ((will_send[i] == 1) && (already_received[i] == 0)) {
229 header_buf[header_index] = i;
233 /* will send in the next step */
234 already_received[i] = 1;
238 /* send header followed by data */
239 if (header_index != 0) {
240 header_buf[header_index] = -1;
244 Request::send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
246 /* recv data - pipeline */
247 from = header_buf[header_index - 1];
248 for (i = 0; i < pipe_length; i++) {
249 Request::recv(tmp_buf + (i * increment), segment, datatype, from, tag,
251 if(op!=MPI_OP_NULL) op->apply( tmp_buf + (i * increment),
252 (char *)rbuf + (i * increment), &segment, datatype);
255 } /* while loop (sent_count < size-1 ) */
261 /* send 1-byte message to root */
262 Request::send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
265 /* wait for header forward when required */
266 request=Request::irecv(header_buf, HEADER_SIZE, MPI_INT, MPI_ANY_SOURCE, tag, comm);
267 Request::wait(&request, MPI_STATUS_IGNORE);
269 /* search for where it is */
272 while (rank != header_buf[myordering]) {
276 /* send header when required */
277 if (header_buf[myordering + 1] != -1) {
278 Request::send(header_buf, HEADER_SIZE, MPI_INT, header_buf[myordering + 1],
282 /* (receive, reduce), and send data */
283 if (header_buf[myordering + 1] == -1) {
286 to = header_buf[myordering + 1];
290 if (myordering == 0) {
291 for (i = 0; i < pipe_length; i++) {
292 send_request_array[i]= Request::isend((char *)rbuf + (i * increment), segment, datatype, to, tag, comm);
294 Request::waitall((pipe_length), send_request_array, send_status_array);
297 /* receive, reduce, and send */
299 from = header_buf[myordering - 1];
300 for (i = 0; i < pipe_length; i++) {
301 recv_request_array[i]=Request::irecv(tmp_buf + (i * increment), segment, datatype, from, tag, comm);
303 for (i = 0; i < pipe_length; i++) {
304 Request::wait(&recv_request_array[i], MPI_STATUS_IGNORE);
305 if(op!=MPI_OP_NULL) op->apply( tmp_buf + (i * increment), (char *)rbuf + (i * increment),
307 send_request_array[i]=Request::isend((char *)rbuf + (i * increment), segment, datatype, to, tag, comm);
309 Request::waitall((pipe_length), send_request_array, send_status_array);
313 delete[] send_request_array;
314 delete[] recv_request_array;
315 delete[] send_status_array;
316 delete[] recv_status_array;
318 //printf("node %d done\n",rank);
322 /* if root is not zero send root after finished
323 this can be modified to make it faster by using logical src, dst.
327 Request::send(rbuf, count, datatype, root, tag, comm);
328 } else if (rank == root) {
329 Request::recv(rbuf, count, datatype, 0, tag, comm, &status);
334 /* when count is not divisible by block size, use default BCAST for the remainder */
335 if ((remainder != 0) && (count > segment)) {
336 Coll_reduce_default::reduce((char*)buf + (pipe_length * increment), (char*)rbuf + (pipe_length * increment),
337 remainder, datatype, op, root, comm);
340 smpi_free_tmp_buffer(tmp_buf);