Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make smx_file_t, surf_file_t and msg_file_t
[simgrid.git] / src / smpi / smpi_coll.c
1 /* smpi_coll.c -- various optimized routing for collectives                   */
2
3 /* Copyright (c) 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <assert.h>
12
13 #include "private.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_coll, smpi,
16                                 "Logging specific to SMPI (coll)");
17
18 struct s_proc_tree {
19   int PROCTREE_A;
20   int numChildren;
21   int *child;
22   int parent;
23   int me;
24   int root;
25   int isRoot;
26 };
27 typedef struct s_proc_tree *proc_tree_t;
28
29 /**
30  * alloc and init
31  **/
32 static proc_tree_t alloc_tree(int arity)
33 {
34   proc_tree_t tree;
35   int i;
36
37   tree = xbt_new(struct s_proc_tree, 1);
38   tree->PROCTREE_A = arity;
39   tree->isRoot = 0;
40   tree->numChildren = 0;
41   tree->child = xbt_new(int, arity);
42   for (i = 0; i < arity; i++) {
43     tree->child[i] = -1;
44   }
45   tree->root = -1;
46   tree->parent = -1;
47   return tree;
48 }
49
50 /**
51  * free
52  **/
53 static void free_tree(proc_tree_t tree)
54 {
55   xbt_free(tree->child);
56   xbt_free(tree);
57 }
58
59 /**
60  * Build the tree depending on a process rank (index) and the group size (extent)
61  * @param root the rank of the tree root
62  * @param rank the rank of the calling process
63  * @param size the total number of processes
64  **/
65 static void build_tree(int root, int rank, int size, proc_tree_t * tree)
66 {
67   int index = (rank - root + size) % size;
68   int firstChildIdx = index * (*tree)->PROCTREE_A + 1;
69   int i;
70
71   (*tree)->me = rank;
72   (*tree)->root = root;
73
74   for (i = 0; i < (*tree)->PROCTREE_A && firstChildIdx + i < size; i++) {
75     (*tree)->child[i] = (firstChildIdx + i + root) % size;
76     (*tree)->numChildren++;
77   }
78   if (rank == root) {
79     (*tree)->isRoot = 1;
80   } else {
81     (*tree)->isRoot = 0;
82     (*tree)->parent = (((index - 1) / (*tree)->PROCTREE_A) + root) % size;
83   }
84 }
85
86 /**
87  * bcast
88  **/
89 static void tree_bcast(void *buf, int count, MPI_Datatype datatype,
90                        MPI_Comm comm, proc_tree_t tree)
91 {
92   int system_tag = 999;         // used negative int but smpi_create_request() declares this illegal (to be checked)
93   int rank, i;
94   MPI_Request *requests;
95
96   rank = smpi_comm_rank(comm);
97   /* wait for data from my parent in the tree */
98   if (!tree->isRoot) {
99     XBT_DEBUG("<%d> tree_bcast(): i am not root: recv from %d, tag=%d)",
100            rank, tree->parent, system_tag + rank);
101     smpi_mpi_recv(buf, count, datatype, tree->parent, system_tag + rank,
102                   comm, MPI_STATUS_IGNORE);
103   }
104   requests = xbt_new(MPI_Request, tree->numChildren);
105   XBT_DEBUG("<%d> creates %d requests (1 per child)", rank,
106          tree->numChildren);
107   /* iniates sends to ranks lower in the tree */
108   for (i = 0; i < tree->numChildren; i++) {
109     if (tree->child[i] == -1) {
110       requests[i] = MPI_REQUEST_NULL;
111     } else {
112       XBT_DEBUG("<%d> send to <%d>, tag=%d", rank, tree->child[i],
113              system_tag + tree->child[i]);
114       requests[i] =
115           smpi_isend_init(buf, count, datatype, tree->child[i],
116                           system_tag + tree->child[i], comm);
117     }
118   }
119   smpi_mpi_startall(tree->numChildren, requests);
120   smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
121   xbt_free(requests);
122 }
123
124 /**
125  * anti-bcast
126  **/
127 static void tree_antibcast(void *buf, int count, MPI_Datatype datatype,
128                            MPI_Comm comm, proc_tree_t tree)
129 {
130   int system_tag = 999;         // used negative int but smpi_create_request() declares this illegal (to be checked)
131   int rank, i;
132   MPI_Request *requests;
133
134   rank = smpi_comm_rank(comm);
135   // everyone sends to its parent, except root.
136   if (!tree->isRoot) {
137     XBT_DEBUG("<%d> tree_antibcast(): i am not root: send to %d, tag=%d)",
138            rank, tree->parent, system_tag + rank);
139     smpi_mpi_send(buf, count, datatype, tree->parent, system_tag + rank,
140                   comm);
141   }
142   //every one receives as many messages as it has children
143   requests = xbt_new(MPI_Request, tree->numChildren);
144   XBT_DEBUG("<%d> creates %d requests (1 per child)", rank,
145          tree->numChildren);
146   for (i = 0; i < tree->numChildren; i++) {
147     if (tree->child[i] == -1) {
148       requests[i] = MPI_REQUEST_NULL;
149     } else {
150       XBT_DEBUG("<%d> recv from <%d>, tag=%d", rank, tree->child[i],
151              system_tag + tree->child[i]);
152       requests[i] =
153           smpi_irecv_init(buf, count, datatype, tree->child[i],
154                           system_tag + tree->child[i], comm);
155     }
156   }
157   smpi_mpi_startall(tree->numChildren, requests);
158   smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
159   xbt_free(requests);
160 }
161
162 /**
163  * bcast with a binary, ternary, or whatever tree ..
164  **/
165 void nary_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root,
166                      MPI_Comm comm, int arity)
167 {
168   proc_tree_t tree = alloc_tree(arity);
169   int rank, size;
170
171   rank = smpi_comm_rank(comm);
172   size = smpi_comm_size(comm);
173   build_tree(root, rank, size, &tree);
174   tree_bcast(buf, count, datatype, comm, tree);
175   free_tree(tree);
176 }
177
178 /**
179  * barrier with a binary, ternary, or whatever tree ..
180  **/
181 void nary_tree_barrier(MPI_Comm comm, int arity)
182 {
183   proc_tree_t tree = alloc_tree(arity);
184   int rank, size;
185   char dummy = '$';
186
187   rank = smpi_comm_rank(comm);
188   size = smpi_comm_size(comm);
189   build_tree(0, rank, size, &tree);
190   tree_antibcast(&dummy, 1, MPI_CHAR, comm, tree);
191   tree_bcast(&dummy, 1, MPI_CHAR, comm, tree);
192   free_tree(tree);
193 }
194
195 /**
196  * Alltoall Bruck
197  *
198  * Openmpi calls this routine when the message size sent to each rank < 2000 bytes and size < 12
199  * FIXME: uh, check smpi_pmpi again, but this routine is called for > 12, not
200  * less...
201  **/
202 int smpi_coll_tuned_alltoall_bruck(void *sendbuf, int sendcount,
203                                    MPI_Datatype sendtype, void *recvbuf,
204                                    int recvcount, MPI_Datatype recvtype,
205                                    MPI_Comm comm)
206 {
207   int system_tag = 777;
208   int i, rank, size, err, count;
209   MPI_Aint lb;
210   MPI_Aint sendext = 0;
211   MPI_Aint recvext = 0;
212   MPI_Request *requests;
213
214   // FIXME: check implementation
215   rank = smpi_comm_rank(comm);
216   size = smpi_comm_size(comm);
217   XBT_DEBUG("<%d> algorithm alltoall_bruck() called.", rank);
218   err = smpi_datatype_extent(sendtype, &lb, &sendext);
219   err = smpi_datatype_extent(recvtype, &lb, &recvext);
220   /* Local copy from self */
221   err =
222       smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext, 
223                          sendcount, sendtype, 
224                          (char *)recvbuf + rank * recvcount * recvext,
225                          recvcount, recvtype);
226   if (err == MPI_SUCCESS && size > 1) {
227     /* Initiate all send/recv to/from others. */
228     requests = xbt_new(MPI_Request, 2 * (size - 1));
229     count = 0;
230     /* Create all receives that will be posted first */
231     for (i = 0; i < size; ++i) {
232       if (i == rank) {
233         XBT_DEBUG("<%d> skip request creation [src = %d, recvcount = %d]",
234                rank, i, recvcount);
235         continue;
236       }
237       requests[count] =
238           smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount,
239                           recvtype, i, system_tag, comm);
240       count++;
241     }
242     /* Now create all sends  */
243     for (i = 0; i < size; ++i) {
244       if (i == rank) {
245         XBT_DEBUG("<%d> skip request creation [dst = %d, sendcount = %d]",
246                rank, i, sendcount);
247         continue;
248       }
249       requests[count] =
250           smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
251                           sendtype, i, system_tag, comm);
252       count++;
253     }
254     /* Wait for them all. */
255     smpi_mpi_startall(count, requests);
256     XBT_DEBUG("<%d> wait for %d requests", rank, count);
257     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
258     xbt_free(requests);
259   }
260   return MPI_SUCCESS;
261 }
262
263 /**
264  * Alltoall basic_linear
265  **/
266 int smpi_coll_tuned_alltoall_basic_linear(void *sendbuf, int sendcount,
267                                           MPI_Datatype sendtype,
268                                           void *recvbuf, int recvcount,
269                                           MPI_Datatype recvtype,
270                                           MPI_Comm comm)
271 {
272   int system_tag = 888;
273   int i, rank, size, err, count;
274   MPI_Aint lb = 0, sendext = 0, recvext = 0;
275   MPI_Request *requests;
276
277   /* Initialize. */
278   rank = smpi_comm_rank(comm);
279   size = smpi_comm_size(comm);
280   XBT_DEBUG("<%d> algorithm alltoall_basic_linear() called.", rank);
281   err = smpi_datatype_extent(sendtype, &lb, &sendext);
282   err = smpi_datatype_extent(recvtype, &lb, &recvext);
283   /* simple optimization */
284   err = smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext, 
285                            sendcount, sendtype, 
286                            (char *)recvbuf + rank * recvcount * recvext, 
287                            recvcount, recvtype);
288   if (err == MPI_SUCCESS && size > 1) {
289     /* Initiate all send/recv to/from others. */
290     requests = xbt_new(MPI_Request, 2 * (size - 1));
291     /* Post all receives first -- a simple optimization */
292     count = 0;
293     for (i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
294       requests[count] =
295           smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount, 
296                           recvtype, i, system_tag, comm);
297       count++;
298     }
299     /* Now post all sends in reverse order
300      *   - We would like to minimize the search time through message queue
301      *     when messages actually arrive in the order in which they were posted.
302      * TODO: check the previous assertion
303      */
304     for (i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size) {
305       requests[count] =
306           smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
307                           sendtype, i, system_tag, comm);
308       count++;
309     }
310     /* Wait for them all. */
311     smpi_mpi_startall(count, requests);
312     XBT_DEBUG("<%d> wait for %d requests", rank, count);
313     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
314     xbt_free(requests);
315   }
316   return err;
317 }
318
319 /**
320  * Alltoall pairwise
321  *
322  * this algorithm performs size steps (1<=s<=size) and
323  * at each step s, a process p sends iand receive to.from a unique distinct remote process
324  * size=5 : s=1:  4->0->1, 0->1->2, 1->2->3, ...
325  *          s=2:  3->0->2, 4->1->3, 0->2->4, 1->3->0 , 2->4->1
326  *          ....
327  * Openmpi calls this routine when the message size sent to each rank is greater than 3000 bytes
328  **/
329 int smpi_coll_tuned_alltoall_pairwise(void *sendbuf, int sendcount,
330                                       MPI_Datatype sendtype, void *recvbuf,
331                                       int recvcount, MPI_Datatype recvtype,
332                                       MPI_Comm comm)
333 {
334   int system_tag = 999;
335   int rank, size, step, sendto, recvfrom, sendsize, recvsize;
336
337   rank = smpi_comm_rank(comm);
338   size = smpi_comm_size(comm);
339   XBT_DEBUG("<%d> algorithm alltoall_pairwise() called.", rank);
340   sendsize = smpi_datatype_size(sendtype);
341   recvsize = smpi_datatype_size(recvtype);
342   /* Perform pairwise exchange - starting from 1 so the local copy is last */
343   for (step = 1; step < size + 1; step++) {
344     /* who do we talk to in this step? */
345     sendto = (rank + step) % size;
346     recvfrom = (rank + size - step) % size;
347     /* send and receive */
348     smpi_mpi_sendrecv(&((char *) sendbuf)[sendto * sendsize * sendcount],
349                       sendcount, sendtype, sendto, system_tag,
350                       &((char *) recvbuf)[recvfrom * recvsize * recvcount],
351                       recvcount, recvtype, recvfrom, system_tag, comm,
352                       MPI_STATUS_IGNORE);
353   }
354   return MPI_SUCCESS;
355 }
356
357 int smpi_coll_basic_alltoallv(void *sendbuf, int *sendcounts,
358                               int *senddisps, MPI_Datatype sendtype,
359                               void *recvbuf, int *recvcounts,
360                               int *recvdisps, MPI_Datatype recvtype,
361                               MPI_Comm comm)
362 {
363   int system_tag = 889;
364   int i, rank, size, err, count;
365   MPI_Aint lb = 0, sendext = 0, recvext = 0;
366   MPI_Request *requests;
367
368   /* Initialize. */
369   rank = smpi_comm_rank(comm);
370   size = smpi_comm_size(comm);
371   XBT_DEBUG("<%d> algorithm basic_alltoallv() called.", rank);
372   err = smpi_datatype_extent(sendtype, &lb, &sendext);
373   err = smpi_datatype_extent(recvtype, &lb, &recvext);
374   /* Local copy from self */
375   err =
376       smpi_datatype_copy((char *)sendbuf + senddisps[rank] * sendext, 
377                          sendcounts[rank], sendtype,
378                          (char *)recvbuf + recvdisps[rank] * recvext, 
379                          recvcounts[rank], recvtype);
380   if (err == MPI_SUCCESS && size > 1) {
381     /* Initiate all send/recv to/from others. */
382     requests = xbt_new(MPI_Request, 2 * (size - 1));
383     count = 0;
384     /* Create all receives that will be posted first */
385     for (i = 0; i < size; ++i) {
386       if (i == rank || recvcounts[i] == 0) {
387         XBT_DEBUG
388             ("<%d> skip request creation [src = %d, recvcounts[src] = %d]",
389              rank, i, recvcounts[i]);
390         continue;
391       }
392       requests[count] =
393           smpi_irecv_init((char *)recvbuf + recvdisps[i] * recvext, 
394                           recvcounts[i], recvtype, i, system_tag, comm);
395       count++;
396     }
397     /* Now create all sends  */
398     for (i = 0; i < size; ++i) {
399       if (i == rank || sendcounts[i] == 0) {
400         XBT_DEBUG
401             ("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]",
402              rank, i, sendcounts[i]);
403         continue;
404       }
405       requests[count] =
406           smpi_isend_init((char *)sendbuf + senddisps[i] * sendext, 
407                           sendcounts[i], sendtype, i, system_tag, comm);
408       count++;
409     }
410     /* Wait for them all. */
411     smpi_mpi_startall(count, requests);
412     XBT_DEBUG("<%d> wait for %d requests", rank, count);
413     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
414     xbt_free(requests);
415   }
416   return err;
417 }