Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
another allreduce ported
[simgrid.git] / src / smpi / colls / bcast-arrival-pattern-aware.c
1 #include "colls.h"
2
3 static int bcast_NTSL_segment_size_in_byte = 8192;
4
5 #define HEADER_SIZE 1024
6 #define MAX_NODE 1024
7
8 /* Non-topology-specific pipelined linear-bcast function */
9 int smpi_coll_tuned_bcast_arrival_pattern_aware(void *buf, int count,
10                                                 MPI_Datatype datatype, int root,
11                                                 MPI_Comm comm)
12 {
13   int tag = 50;
14   MPI_Status status;
15   MPI_Request request;
16   MPI_Request *send_request_array;
17   MPI_Request *recv_request_array;
18   MPI_Status *send_status_array;
19   MPI_Status *recv_status_array;
20
21   MPI_Status temp_status_array[MAX_NODE];
22
23   int rank, size;
24   int i, j;
25
26   int sent_count;
27   int header_index;
28   int flag_array[MAX_NODE];
29   int already_sent[MAX_NODE];
30
31   int header_buf[HEADER_SIZE];
32   char temp_buf[MAX_NODE];
33
34   MPI_Aint extent;
35   MPI_Type_extent(datatype, &extent);
36
37   /* destination */
38   int to;
39
40
41
42   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
43   MPI_Comm_size(MPI_COMM_WORLD, &size);
44
45
46   /* segment is segment size in number of elements (not bytes) */
47   int segment = bcast_NTSL_segment_size_in_byte / extent;
48
49   /* pipeline length */
50   int pipe_length = count / segment;
51
52   /* use for buffer offset for sending and receiving data = segment size in byte */
53   int increment = segment * extent;
54
55   /* if the input size is not divisible by segment size => 
56      the small remainder will be done with native implementation */
57   int remainder = count % segment;
58
59   /* if root is not zero send to rank zero first
60      this can be modified to make it faster by using logical src, dst.
61    */
62   if (root != 0) {
63     if (rank == root) {
64       MPI_Send(buf, count, datatype, 0, tag, comm);
65     } else if (rank == 0) {
66       MPI_Recv(buf, count, datatype, root, tag, comm, &status);
67     }
68   }
69
70   /* value == 0 means root has not send data (or header) to the node yet */
71   for (i = 0; i < MAX_NODE; i++) {
72     already_sent[i] = 0;
73   }
74
75   /* when a message is smaller than a block size => no pipeline */
76   if (count <= segment) {
77     if (rank == 0) {
78       sent_count = 0;
79
80       while (sent_count < (size - 1)) {
81         for (i = 1; i < size; i++) {
82           MPI_Iprobe(i, MPI_ANY_TAG, MPI_COMM_WORLD, &flag_array[i],
83                      MPI_STATUSES_IGNORE);
84         }
85
86         header_index = 0;
87         /* recv 1-byte message */
88         for (i = 1; i < size; i++) {
89
90           /* message arrive */
91           if ((flag_array[i] == 1) && (already_sent[i] == 0)) {
92             MPI_Recv(temp_buf, 1, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
93             header_buf[header_index] = i;
94             header_index++;
95             sent_count++;
96
97             /* will send in the next step */
98             already_sent[i] = 1;
99           }
100         }
101
102         /* send header followed by data */
103         if (header_index != 0) {
104           header_buf[header_index] = -1;
105           to = header_buf[0];
106           MPI_Send(header_buf, HEADER_SIZE, MPI_INT, to, tag, comm);
107           MPI_Send(buf, count, datatype, to, tag, comm);
108         }
109
110         /* randomly MPI_Send to one */
111         else {
112           /* search for the first node that never received data before */
113           for (i = 1; i < size; i++) {
114             if (already_sent[i] == 0) {
115               header_buf[0] = i;
116               header_buf[1] = -1;
117               MPI_Send(header_buf, HEADER_SIZE, MPI_INT, i, tag, comm);
118               MPI_Send(buf, count, datatype, i, tag, comm);
119               already_sent[i] = 1;
120               sent_count++;
121               break;
122             }
123           }
124         }
125
126
127       }                         /* while loop */
128     }
129
130     /* non-root */
131     else {
132
133       /* send 1-byte message to root */
134       MPI_Send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
135
136       /* wait for header and data, forward when required */
137       MPI_Recv(header_buf, HEADER_SIZE, MPI_INT, MPI_ANY_SOURCE, tag, comm,
138                &status);
139       MPI_Recv(buf, count, datatype, MPI_ANY_SOURCE, tag, comm, &status);
140
141       /* search for where it is */
142       int myordering = 0;
143       while (rank != header_buf[myordering]) {
144         myordering++;
145       }
146
147       /* send header followed by data */
148       if (header_buf[myordering + 1] != -1) {
149         MPI_Send(header_buf, HEADER_SIZE, MPI_INT, header_buf[myordering + 1],
150                  tag, comm);
151         MPI_Send(buf, count, datatype, header_buf[myordering + 1], tag, comm);
152       }
153     }
154   }
155   /* pipeline bcast */
156   else {
157     send_request_array =
158         (MPI_Request *) malloc((size + pipe_length) * sizeof(MPI_Request));
159     recv_request_array =
160         (MPI_Request *) malloc((size + pipe_length) * sizeof(MPI_Request));
161     send_status_array =
162         (MPI_Status *) malloc((size + pipe_length) * sizeof(MPI_Status));
163     recv_status_array =
164         (MPI_Status *) malloc((size + pipe_length) * sizeof(MPI_Status));
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           MPI_Iprobe(i, MPI_ANY_TAG, MPI_COMM_WORLD, &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             MPI_Recv(&temp_buf[i], 1, MPI_CHAR, i, tag, MPI_COMM_WORLD,
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           MPI_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             MPI_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               MPI_Send((char *)buf + (i * increment), segment, datatype, to, tag, comm);
239             }
240             //MPI_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               MPI_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                 MPI_Send((char *)buf + (j * increment), segment, datatype, to, tag,
265                          comm);
266               }
267
268               //MPI_Send(buf,count,datatype,to,tag,comm);
269               //MPI_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               sent_count++;
278               break;
279             }
280           }
281         }
282
283       }                         /* while loop */
284
285       //total = MPI_Wtime() - start2;
286       //total *= 1000;
287       //printf("Node zero iter = %d time = %.2f\n",iteration,total);
288     }
289
290     /* rank 0 */
291     /* none root */
292     else {
293       /* send 1-byte message to root */
294       MPI_Send(temp_buf, 1, MPI_CHAR, 0, tag, comm);
295
296       /* wait for header forward when required */
297       MPI_Irecv(header_buf, HEADER_SIZE, MPI_INT, MPI_ANY_SOURCE, tag, comm,
298                 &request);
299       MPI_Wait(&request, MPI_STATUS_IGNORE);
300
301       /* search for where it is */
302       int myordering = 0;
303       while (rank != header_buf[myordering]) {
304         myordering++;
305       }
306
307       /* send header when required */
308       if (header_buf[myordering + 1] != -1) {
309         MPI_Send(header_buf, HEADER_SIZE, MPI_INT, header_buf[myordering + 1],
310                  tag, comm);
311       }
312
313       /* receive data */
314
315       if (0 == -1) {
316         //if (header_buf[1] == -1) {
317         MPI_Irecv(buf, count, datatype, 0, tag, comm, &request);
318         MPI_Wait(&request, MPI_STATUS_IGNORE);
319         //printf("\t\tnode %d ordering = %d receive data from root\n",rank,myordering);
320       } else {
321         for (i = 0; i < pipe_length; i++) {
322           MPI_Irecv((char *)buf + (i * increment), segment, datatype, MPI_ANY_SOURCE,
323                     tag, comm, &recv_request_array[i]);
324         }
325       }
326
327       /* send data */
328       if (header_buf[myordering + 1] != -1) {
329         for (i = 0; i < pipe_length; i++) {
330           MPI_Wait(&recv_request_array[i], MPI_STATUS_IGNORE);
331           MPI_Isend((char *)buf + (i * increment), segment, datatype,
332                     header_buf[myordering + 1], tag, comm,
333                     &send_request_array[i]);
334         }
335         MPI_Waitall((pipe_length), send_request_array, send_status_array);
336       }
337
338     }
339
340     free(send_request_array);
341     free(recv_request_array);
342     free(send_status_array);
343     free(recv_status_array);
344   }                             /* end pipeline */
345
346   /* when count is not divisible by block size, use default BCAST for the remainder */
347   if ((remainder != 0) && (count > segment)) {
348     MPI_Bcast((char *)buf + (pipe_length * increment), remainder, datatype, root, comm);
349   }
350
351   return MPI_SUCCESS;
352 }