1 #include "colls_private.h"
2 //#include <star-reduction.c>
4 int reduce_arrival_pattern_aware_segment_size_in_byte = 8192;
7 #define HEADER_SIZE 1024
14 /* Non-topology-specific pipelined linear-reduce function */
15 int smpi_coll_tuned_reduce_arrival_pattern_aware(void *buf, void *rbuf,
17 MPI_Datatype datatype,
21 int rank = smpi_comm_rank(comm);
22 int tag = -COLL_TAG_REDUCE;
25 MPI_Request *send_request_array;
26 MPI_Request *recv_request_array;
27 MPI_Status *send_status_array;
28 MPI_Status *recv_status_array;
30 MPI_Status temp_status_array[MAX_NODE];
32 int size = smpi_comm_size(comm);
37 int flag_array[MAX_NODE];
38 int already_received[MAX_NODE];
40 int header_buf[HEADER_SIZE];
41 char temp_buf[MAX_NODE];
44 smpi_datatype_extent(datatype, &lb, &extent);
46 /* source and destination */
49 /* segment is segment size in number of elements (not bytes) */
50 int segment = reduce_arrival_pattern_aware_segment_size_in_byte / extent;
53 int pipe_length = count / segment;
55 /* use for buffer offset for sending and receiving data = segment size in byte */
56 int increment = segment * extent;
58 /* if the input size is not divisible by segment size =>
59 the small remainder will be done with native implementation */
60 int remainder = count % segment;
63 /* value == 0 means root has not send data (or header) to the node yet */
64 for (i = 0; i < MAX_NODE; i++) {
65 already_received[i] = 0;
69 tmp_buf = (char *) xbt_malloc(count * extent);
71 smpi_mpi_sendrecv(buf, count, datatype, rank, tag, rbuf, count, datatype, rank,
76 /* when a message is smaller than a block size => no pipeline */
77 if (count <= segment) {
82 while (sent_count < (size - 1)) {
84 for (i = 1; i < size; i++) {
85 if (already_received[i] == 0) {
86 smpi_mpi_iprobe(i, MPI_ANY_TAG, comm, &flag_array[i],
88 simcall_process_sleep(0.0001);
93 /* recv 1-byte message */
94 for (i = 0; i < size; i++) {
98 /* 1-byte message arrive */
99 if ((flag_array[i] == 1) && (already_received[i] == 0)) {
100 smpi_mpi_recv(temp_buf, 1, MPI_CHAR, i, tag, comm, &status);
101 header_buf[header_index] = i;
106 //printf("root send to %d recv from %d : data = ",to,from);
108 for (i=0;i<=header_index;i++) {
109 printf("%d ",header_buf[i]);
113 /* will receive in the next step */
114 already_received[i] = 1;
118 /* send header followed by receive and reduce data */
119 if (header_index != 0) {
120 header_buf[header_index] = -1;
122 from = header_buf[header_index - 1];
124 smpi_mpi_send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
125 smpi_mpi_recv(tmp_buf, count, datatype, from, tag, comm, &status);
126 smpi_op_apply(op, tmp_buf, rbuf, &count, &datatype);
135 /* send 1-byte message to root */
136 smpi_mpi_send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
138 /* wait for header and data, forward when required */
139 smpi_mpi_recv(header_buf, HEADER_SIZE, MPI_INT, MPI_ANY_SOURCE, tag, comm,
141 // smpi_mpi_recv(buf,count,datatype,MPI_ANY_SOURCE,tag,comm,&status);
143 /* search for where it is */
145 while (rank != header_buf[myordering]) {
150 if (header_buf[myordering + 1] != -1) {
151 smpi_mpi_send(header_buf, HEADER_SIZE, MPI_INT, header_buf[myordering + 1],
154 //printf("node %d ordering %d\n",rank,myordering);
156 /* receive, reduce, and forward data */
159 if (myordering == 0) {
160 if (header_buf[myordering + 1] == -1) {
163 to = header_buf[myordering + 1];
165 smpi_mpi_send(rbuf, count, datatype, to, tag, comm);
168 /* recv, reduce, send */
170 if (header_buf[myordering + 1] == -1) {
173 to = header_buf[myordering + 1];
175 from = header_buf[myordering - 1];
176 smpi_mpi_recv(tmp_buf, count, datatype, header_buf[myordering - 1], tag,
178 smpi_op_apply(op, tmp_buf, rbuf, &count, &datatype);
179 smpi_mpi_send(rbuf, count, datatype, to, tag, comm);
185 // printf("node %d start\n",rank);
188 (MPI_Request *) xbt_malloc((size + pipe_length) * sizeof(MPI_Request));
190 (MPI_Request *) xbt_malloc((size + pipe_length) * sizeof(MPI_Request));
192 (MPI_Status *) xbt_malloc((size + pipe_length) * sizeof(MPI_Status));
194 (MPI_Status *) xbt_malloc((size + pipe_length) * sizeof(MPI_Status));
199 int will_send[MAX_NODE];
200 for (i = 0; i < MAX_NODE; i++)
203 /* loop until all data are received (sent) */
204 while (sent_count < (size - 1)) {
206 for (k = 0; k < 1; k++) {
207 for (i = 1; i < size; i++) {
210 if ((already_received[i] == 0) && (will_send[i] == 0)) {
211 smpi_mpi_iprobe(i, MPI_ANY_TAG, comm, &flag_array[i],
212 &temp_status_array[i]);
213 if (flag_array[i] == 1) {
215 smpi_mpi_recv(&temp_buf[i], 1, MPI_CHAR, i, tag, comm,
217 //printf("recv from %d\n",i);
222 } /* end of probing */
226 /* recv 1-byte message */
227 for (i = 1; i < size; i++) {
230 /* message arrived in this round (put in the header) */
231 if ((will_send[i] == 1) && (already_received[i] == 0)) {
232 header_buf[header_index] = i;
236 /* will send in the next step */
237 already_received[i] = 1;
241 /* send header followed by data */
242 if (header_index != 0) {
243 header_buf[header_index] = -1;
247 smpi_mpi_send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
249 /* recv data - pipeline */
250 from = header_buf[header_index - 1];
251 for (i = 0; i < pipe_length; i++) {
252 smpi_mpi_recv(tmp_buf + (i * increment), segment, datatype, from, tag,
254 smpi_op_apply(op, tmp_buf + (i * increment),
255 (char *)rbuf + (i * increment), &segment, &datatype);
258 } /* while loop (sent_count < size-1 ) */
264 /* send 1-byte message to root */
265 smpi_mpi_send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
268 /* wait for header forward when required */
269 request=smpi_mpi_irecv(header_buf, HEADER_SIZE, MPI_INT, MPI_ANY_SOURCE, tag, comm);
270 smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
272 /* search for where it is */
275 while (rank != header_buf[myordering]) {
279 /* send header when required */
280 if (header_buf[myordering + 1] != -1) {
281 smpi_mpi_send(header_buf, HEADER_SIZE, MPI_INT, header_buf[myordering + 1],
285 /* (receive, reduce), and send data */
286 if (header_buf[myordering + 1] == -1) {
289 to = header_buf[myordering + 1];
293 if (myordering == 0) {
294 for (i = 0; i < pipe_length; i++) {
295 send_request_array[i]= smpi_mpi_isend((char *)rbuf + (i * increment), segment, datatype, to, tag, comm);
297 smpi_mpi_waitall((pipe_length), send_request_array, send_status_array);
300 /* receive, reduce, and send */
302 from = header_buf[myordering - 1];
303 for (i = 0; i < pipe_length; i++) {
304 recv_request_array[i]=smpi_mpi_irecv(tmp_buf + (i * increment), segment, datatype, from, tag, comm);
306 for (i = 0; i < pipe_length; i++) {
307 smpi_mpi_wait(&recv_request_array[i], MPI_STATUS_IGNORE);
308 smpi_op_apply(op, tmp_buf + (i * increment), (char *)rbuf + (i * increment),
309 &segment, &datatype);
310 send_request_array[i]=smpi_mpi_isend((char *)rbuf + (i * increment), segment, datatype, to, tag, comm);
312 smpi_mpi_waitall((pipe_length), send_request_array, send_status_array);
319 free(send_request_array);
320 free(recv_request_array);
321 free(send_status_array);
322 free(recv_status_array);
324 //printf("node %d done\n",rank);
328 /* if root is not zero send root after finished
329 this can be modified to make it faster by using logical src, dst.
333 smpi_mpi_send(rbuf, count, datatype, root, tag, comm);
334 } else if (rank == root) {
335 smpi_mpi_recv(rbuf, count, datatype, 0, tag, comm, &status);
340 /* when count is not divisible by block size, use default BCAST for the remainder */
341 if ((remainder != 0) && (count > segment)) {
342 smpi_mpi_reduce((char *)buf + (pipe_length * increment),
343 (char *)rbuf + (pipe_length * increment), remainder, datatype, op, root,