Logo AND Algorithmique Numérique Distribuée

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