Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / src / smpi / colls / bcast / bcast-arrival-pattern-aware.cpp
1 /* Copyright (c) 2013-2019. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "../colls_private.hpp"
8
9 static int bcast_NTSL_segment_size_in_byte = 8192;
10
11 #define HEADER_SIZE 1024
12 #define MAX_NODE 1024
13
14 namespace simgrid{
15 namespace smpi{
16 /* Non-topology-specific pipelined linear-bcast function */
17 int Coll_bcast_arrival_pattern_aware::bcast(void *buf, int count,
18                                                 MPI_Datatype datatype, int root,
19                                                 MPI_Comm comm)
20 {
21   int tag = -COLL_TAG_BCAST;
22   MPI_Status status;
23   MPI_Request request;
24   MPI_Request *send_request_array;
25   MPI_Request *recv_request_array;
26   MPI_Status *send_status_array;
27   MPI_Status *recv_status_array;
28
29   MPI_Status temp_status_array[MAX_NODE];
30
31   int rank, size;
32   int i, j;
33
34   int sent_count;
35   int header_index;
36   int flag_array[MAX_NODE];
37   int already_sent[MAX_NODE];
38   int to_clean[MAX_NODE];
39   int header_buf[HEADER_SIZE];
40   char temp_buf[MAX_NODE];
41
42   MPI_Aint extent;
43   extent = datatype->get_extent();
44
45   /* destination */
46   int to;
47
48
49
50   rank = comm->rank();
51   size = comm->size();
52
53
54   /* segment is segment size in number of elements (not bytes) */
55   int segment = bcast_NTSL_segment_size_in_byte / extent;
56   segment =  segment == 0 ? 1 :segment;
57   /* pipeline length */
58   int pipe_length = count / segment;
59
60   /* use for buffer offset for sending and receiving data = segment size in byte */
61   int increment = segment * extent;
62
63   /* if the input size is not divisible by segment size =>
64      the small remainder will be done with native implementation */
65   int remainder = count % segment;
66
67   /* if root is not zero send to rank zero first
68      this can be modified to make it faster by using logical src, dst.
69    */
70   if (root != 0) {
71     if (rank == root) {
72       Request::send(buf, count, datatype, 0, tag, comm);
73     } else if (rank == 0) {
74       Request::recv(buf, count, datatype, root, tag, comm, &status);
75     }
76   }
77
78   /* value == 0 means root has not send data (or header) to the node yet */
79   for (i = 0; i < MAX_NODE; i++) {
80     already_sent[i] = 0;
81     to_clean[i] = 0;
82   }
83
84   /* when a message is smaller than a block size => no pipeline */
85   if (count <= segment) {
86     if (rank == 0) {
87       sent_count = 0;
88
89       while (sent_count < (size - 1)) {
90         for (i = 1; i < size; i++) {
91           Request::iprobe(i, MPI_ANY_TAG, comm, &flag_array[i],
92                      MPI_STATUSES_IGNORE);
93         }
94
95         header_index = 0;
96         /* recv 1-byte message */
97         for (i = 1; i < size; i++) {
98
99           /* message arrive */
100           if ((flag_array[i] == 1) && (already_sent[i] == 0)) {
101             Request::recv(temp_buf, 1, MPI_CHAR, i, tag, comm, &status);
102             header_buf[header_index] = i;
103             header_index++;
104             sent_count++;
105
106             /* will send in the next step */
107             already_sent[i] = 1;
108           }
109         }
110
111         /* send header followed by data */
112         if (header_index != 0) {
113           header_buf[header_index] = -1;
114           to = header_buf[0];
115           Request::send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
116           Request::send(buf, count, datatype, to, tag, comm);
117         }
118
119         /* randomly MPI_Send to one */
120         else {
121           /* search for the first node that never received data before */
122           for (i = 1; i < size; i++) {
123             if (already_sent[i] == 0) {
124               header_buf[0] = i;
125               header_buf[1] = -1;
126               Request::send(header_buf, HEADER_SIZE, MPI_INT, i, tag, comm);
127               Request::send(buf, count, datatype, i, tag, comm);
128               already_sent[i] = 1;
129               sent_count++;
130               break;
131             }
132           }
133         }
134
135
136       }                         /* while loop */
137     }
138
139     /* non-root */
140     else {
141
142       /* send 1-byte message to root */
143       Request::send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
144
145       /* wait for header and data, forward when required */
146       Request::recv(header_buf, HEADER_SIZE, MPI_INT, MPI_ANY_SOURCE, tag, comm,
147                &status);
148       Request::recv(buf, count, datatype, MPI_ANY_SOURCE, tag, comm, &status);
149
150       /* search for where it is */
151       int myordering = 0;
152       while (rank != header_buf[myordering]) {
153         myordering++;
154       }
155
156       /* send header followed by data */
157       if (header_buf[myordering + 1] != -1) {
158         Request::send(header_buf, HEADER_SIZE, MPI_INT, header_buf[myordering + 1],
159                  tag, comm);
160         Request::send(buf, count, datatype, header_buf[myordering + 1], tag, comm);
161       }
162     }
163   }
164   /* pipeline bcast */
165   else {
166     send_request_array =
167         (MPI_Request *) xbt_malloc((size + pipe_length) * sizeof(MPI_Request));
168     recv_request_array =
169         (MPI_Request *) xbt_malloc((size + pipe_length) * sizeof(MPI_Request));
170     send_status_array =
171         (MPI_Status *) xbt_malloc((size + pipe_length) * sizeof(MPI_Status));
172     recv_status_array =
173         (MPI_Status *) xbt_malloc((size + pipe_length) * sizeof(MPI_Status));
174
175     if (rank == 0) {
176       //double start2 = MPI_Wtime();
177       sent_count = 0;
178       //int iteration = 0;
179       while (sent_count < (size - 1)) {
180         //iteration++;
181         //start = MPI_Wtime();
182         for (i = 1; i < size; i++) {
183           Request::iprobe(i, MPI_ANY_TAG, comm, &flag_array[i],
184                      &temp_status_array[i]);
185         }
186         //total = MPI_Wtime() - start;
187         //total *= 1000;
188         //printf("Iprobe time = %.2f\n",total);
189         header_index = 0;
190
191         MPI_Wtime();
192         /* recv 1-byte message */
193         for (i = 1; i < size; i++) {
194           /* message arrive */
195           if ((flag_array[i] == 1) && (already_sent[i] == 0)) {
196             Request::recv(&temp_buf[i], 1, MPI_CHAR, i, tag, comm,
197                      &status);
198             header_buf[header_index] = i;
199             header_index++;
200             sent_count++;
201
202             /* will send in the next step */
203             already_sent[i] = 1;
204           }
205         }
206         //total = MPI_Wtime() - start;
207         //total *= 1000;
208         //printf("Recv 1-byte time = %.2f\n",total);
209
210         /*
211            if (header_index != 0) {
212            printf("header index = %d node = ",header_index);
213            for (i=0;i<header_index;i++) {
214            printf("%d ",header_buf[i]);
215            }
216            printf("\n");
217            }
218          */
219
220         /* send header followed by data */
221         if (header_index != 0) {
222           header_buf[header_index] = -1;
223           to = header_buf[0];
224
225           //start = MPI_Wtime();
226
227           /* send header */
228           Request::send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
229
230           //total = MPI_Wtime() - start;
231           //total *= 1000;
232           //printf("\tSend header to %d time = %.2f\n",to,total);
233
234           //start = MPI_Wtime();
235
236           /* send data - non-pipeline case */
237
238           if (0 == 1) {
239             //if (header_index == 1) {
240             Request::send(buf, count, datatype, to, tag, comm);
241           }
242
243
244           /* send data - pipeline */
245           else {
246             for (i = 0; i < pipe_length; i++) {
247               Request::send((char *)buf + (i * increment), segment, datatype, to, tag, comm);
248             }
249             //Request::waitall((pipe_length), send_request_array, send_status_array);
250           }
251           //total = MPI_Wtime() - start;
252           //total *= 1000;
253           //printf("\tSend data to %d time = %.2f\n",to,total);
254
255         }
256
257
258
259         /* randomly MPI_Send to one node */
260         else {
261           /* search for the first node that never received data before */
262           for (i = 1; i < size; i++) {
263             if (already_sent[i] == 0) {
264               header_buf[0] = i;
265               header_buf[1] = -1;
266               to = i;
267
268               //start = MPI_Wtime();
269               Request::send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
270
271               /* still need to chop data so that we can use the same non-root code */
272               for (j = 0; j < pipe_length; j++) {
273                 Request::send((char *)buf + (j * increment), segment, datatype, to, tag,
274                          comm);
275               }
276
277               //Request::send(buf,count,datatype,to,tag,comm);
278               //Request::wait(&request,MPI_STATUS_IGNORE);
279
280               //total = MPI_Wtime() - start;
281               //total *= 1000;
282               //printf("SEND TO SINGLE node %d time = %.2f\n",i,total);
283
284
285               already_sent[i] = 1;
286               to_clean[i]=1;
287               sent_count++;
288               break;
289             }
290           }
291         }
292
293       }                         /* while loop */
294
295       for(i=0; i<size; i++)
296         if(to_clean[i]!=0)Request::recv(&temp_buf[i], 1, MPI_CHAR, i, tag, comm,
297                      &status);
298       //total = MPI_Wtime() - start2;
299       //total *= 1000;
300       //printf("Node zero iter = %d time = %.2f\n",iteration,total);
301     }
302
303     /* rank 0 */
304     /* none root */
305     else {
306       /* send 1-byte message to root */
307       Request::send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
308
309       /* wait for header forward when required */
310       request = Request::irecv(header_buf, HEADER_SIZE, MPI_INT, MPI_ANY_SOURCE, tag, comm);
311       Request::wait(&request, MPI_STATUS_IGNORE);
312
313       /* search for where it is */
314       int myordering = 0;
315       while (rank != header_buf[myordering]) {
316         myordering++;
317       }
318
319       /* send header when required */
320       if (header_buf[myordering + 1] != -1) {
321         Request::send(header_buf, HEADER_SIZE, MPI_INT, header_buf[myordering + 1],
322                  tag, comm);
323       }
324
325       /* receive data */
326
327       if (0 == -1) {
328         //if (header_buf[1] == -1) {
329         request = Request::irecv(buf, count, datatype, 0, tag, comm);
330         Request::wait(&request, MPI_STATUS_IGNORE);
331         //printf("\t\tnode %d ordering = %d receive data from root\n",rank,myordering);
332       } else {
333         for (i = 0; i < pipe_length; i++) {
334           recv_request_array[i] = Request::irecv((char *)buf + (i * increment), segment, datatype, MPI_ANY_SOURCE,
335                                                  tag, comm);
336         }
337       }
338
339       /* send data */
340       if (header_buf[myordering + 1] != -1) {
341         for (i = 0; i < pipe_length; i++) {
342           Request::wait(&recv_request_array[i], MPI_STATUS_IGNORE);
343           send_request_array[i] = Request::isend((char *)buf + (i * increment), segment, datatype,
344                     header_buf[myordering + 1], tag, comm);
345         }
346         Request::waitall((pipe_length), send_request_array, send_status_array);
347       }else{
348           Request::waitall(pipe_length, recv_request_array, recv_status_array);
349           }
350
351     }
352
353     free(send_request_array);
354     free(recv_request_array);
355     free(send_status_array);
356     free(recv_status_array);
357   }                             /* end pipeline */
358
359   /* when count is not divisible by block size, use default BCAST for the remainder */
360   if ((remainder != 0) && (count > segment)) {
361     XBT_WARN("MPI_bcast_arrival_pattern_aware use default MPI_bcast.");
362     Colls::bcast((char *)buf + (pipe_length * increment), remainder, datatype, root, comm);
363   }
364
365   return MPI_SUCCESS;
366 }
367
368 }
369 }