Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d1984b0a2656cc088566e102bd70c061911fe6bd
[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 #include "smpi_coll_private.h"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_coll, smpi,
17                                 "Logging specific to SMPI (coll)");
18
19 struct s_proc_tree {
20   int PROCTREE_A;
21   int numChildren;
22   int * child;
23   int parent;
24   int me;
25   int root;
26   int isRoot;
27 };
28 typedef struct s_proc_tree * proc_tree_t;
29
30 /**
31  * alloc and init
32  **/
33 static proc_tree_t alloc_tree(int arity) {
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   xbt_free(tree->child );
55   xbt_free(tree);
56 }
57
58 /**
59  * Build the tree depending on a process rank (index) and the group size (extent)
60  * @param index the rank of the calling process
61  * @param extent the total number of processes
62  **/
63 static void build_tree(int index, int extent, proc_tree_t* tree) {
64   int places = (*tree)->PROCTREE_A * index;
65   int i, ch, pr;
66
67   (*tree)->me = index;
68   (*tree)->root = 0 ;
69   for(i = 1; i <= (*tree)->PROCTREE_A; i++) {
70     ++places;
71     ch = (*tree)->PROCTREE_A * index + i + (*tree)->root;
72     ch %= extent;
73     if(places < extent) {
74       (*tree)->child[i - 1] = ch;
75       (*tree)->numChildren++;
76     }
77   }
78   if(index == (*tree)->root) {
79     (*tree)->isRoot = 1;
80   } else {
81     (*tree)->isRoot = 0;
82     pr = (index - 1) / (*tree)->PROCTREE_A;
83     (*tree)->parent = pr;
84   }
85 }
86
87 /**
88  * bcast
89  **/
90 static void tree_bcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm, proc_tree_t tree) {
91   int system_tag = 999;  // used negative int but smpi_create_request() declares this illegal (to be checked)
92   int rank, i;
93   MPI_Request* requests;
94
95   rank = smpi_comm_rank(comm);
96   /* wait for data from my parent in the tree */
97   if(!tree->isRoot) {
98     DEBUG3("<%d> tree_bcast(): i am not root: recv from %d, tag=%d)",
99            rank, tree->parent, system_tag + rank);
100     smpi_mpi_recv(buf, count, datatype, tree->parent, system_tag + rank, comm, MPI_STATUS_IGNORE);
101   }
102   requests = xbt_new(MPI_Request, tree->numChildren);
103   DEBUG2("<%d> creates %d requests (1 per child)", rank, tree->numChildren);
104   /* iniates sends to ranks lower in the tree */
105   for(i = 0; i < tree->numChildren; i++) {
106     if(tree->child[i] == -1) {
107       requests[i] = MPI_REQUEST_NULL;
108     } else {
109       DEBUG3("<%d> send to <%d>, tag=%d", rank, tree->child[i], system_tag + tree->child[i]);
110       requests[i] = smpi_isend_init(buf, count, datatype, tree->child[i], system_tag + tree->child[i], comm);
111     }
112   }
113   smpi_mpi_startall(tree->numChildren, requests);
114   smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
115   xbt_free(requests);
116 }
117
118 /**
119  * anti-bcast
120  **/
121 static void tree_antibcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm, proc_tree_t tree) {
122   int system_tag = 999;  // used negative int but smpi_create_request() declares this illegal (to be checked)
123   int rank, i;
124   MPI_Request* requests;
125
126   rank = smpi_comm_rank(comm);
127   // everyone sends to its parent, except root.
128   if(!tree->isRoot) {
129     DEBUG3("<%d> tree_antibcast(): i am not root: send to %d, tag=%d)",
130            rank, tree->parent, system_tag + rank);
131     smpi_mpi_send(buf, count, datatype, tree->parent, system_tag + rank, comm);
132   }
133   //every one receives as many messages as it has children
134   requests = xbt_new(MPI_Request, tree->numChildren);
135   DEBUG2("<%d> creates %d requests (1 per child)", rank, tree->numChildren);
136   for(i = 0; i < tree->numChildren; i++) {
137     if(tree->child[i] == -1) {
138       requests[i] = MPI_REQUEST_NULL;
139     } else {
140       DEBUG3("<%d> recv from <%d>, tag=%d", rank, tree->child[i], system_tag + tree->child[i]);
141       requests[i] = smpi_irecv_init(buf, count, datatype, tree->child[i], system_tag + tree->child[i], comm);
142     }
143   }
144   smpi_mpi_startall(tree->numChildren, requests);
145   smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
146   xbt_free(requests);
147 }
148
149 /**
150  * bcast with a binary, ternary, or whatever tree ..
151  **/
152 void nary_tree_bcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm, int arity) {
153   proc_tree_t tree = alloc_tree(arity);
154   int rank, size;
155
156   rank = smpi_comm_rank(comm);
157   size = smpi_comm_size(comm);
158   build_tree(rank, size, &tree);
159   tree_bcast(buf, count, datatype, root, comm, tree);
160   free_tree(tree);
161 }
162
163 /**
164  * barrier with a binary, ternary, or whatever tree ..
165  **/
166 void nary_tree_barrier(MPI_Comm comm, int arity) {
167   proc_tree_t tree = alloc_tree( arity );
168   int rank, size;
169   char dummy='$';
170
171   rank = smpi_comm_rank(comm);
172   size = smpi_comm_size(comm);
173   build_tree(rank, size, &tree);
174   tree_antibcast(&dummy, 1, MPI_CHAR, 0, comm, tree);
175   tree_bcast(&dummy, 1, MPI_CHAR, 0, comm, tree);
176   free_tree(tree);
177 }
178
179 /**
180  * Alltoall Bruck
181  *
182  * Openmpi calls this routine when the message size sent to each rank < 2000 bytes and size < 12
183  **/
184 int smpi_coll_tuned_alltoall_bruck(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
185   int system_tag = 777;
186   int i, rank, size, err, count;
187   MPI_Aint lb, sendextent, recvextent;
188   MPI_Request* requests;
189
190   // FIXME: check implementation
191   rank = smpi_comm_rank(comm);
192   size = smpi_comm_size(comm);
193   DEBUG1("<%d> algorithm alltoall_bruck() called.", rank);
194   err = smpi_datatype_extent(sendtype, &lb, &sendextent);
195   err = smpi_datatype_extent(recvtype, &lb, &recvextent);
196   /* Local copy from self */
197   err = smpi_datatype_copy(&((char*)sendbuf)[rank * sendextent], sendcount, sendtype, &((char*)recvbuf)[rank * recvextent], recvcount, recvtype);
198   if(err == MPI_SUCCESS && size > 1) {
199     /* Initiate all send/recv to/from others. */
200     requests = xbt_new(MPI_Request, 2 * (size - 1));
201     count = 0;
202     /* Create all receives that will be posted first */
203     for(i = 0; i < size; ++i) {
204       if(i == rank) {
205         DEBUG3("<%d> skip request creation [src = %d, recvcount = %d]", rank, i, recvcount);
206         continue;
207       }
208       requests[count] = smpi_irecv_init(&((char*)recvbuf)[i * recvextent], recvcount, recvtype, i, system_tag, comm);
209       count++;
210     }
211     /* Now create all sends  */
212     for(i = 0; i < size; ++i) {
213       if(i == rank) {
214         DEBUG3("<%d> skip request creation [dst = %d, sendcount = %d]", rank, i, sendcount);
215         continue;
216       }
217       requests[count] = smpi_isend_init(&((char*)sendbuf)[i * sendextent], sendcount, sendtype, i, system_tag, comm);
218       count++;
219     }
220     /* Wait for them all.*/
221     smpi_mpi_startall(count, requests);
222     DEBUG2("<%d> wait for %d requests", rank, count);
223     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
224     xbt_free(requests);
225   }
226   return MPI_SUCCESS;
227 }
228
229 /**
230  * Alltoall basic_linear
231  **/
232 int smpi_coll_tuned_alltoall_basic_linear(void *sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
233   int system_tag = 888;
234   int i, rank, size, err, count;
235   MPI_Aint lb, sendinc, recvinc;
236   MPI_Request *requests;
237
238   /* Initialize. */
239   rank = smpi_comm_rank(comm);
240   size = smpi_comm_size(comm);
241   DEBUG1("<%d> algorithm alltoall_basic_linear() called.", rank);
242   err = smpi_datatype_extent(sendtype, &lb, &sendinc);
243   err = smpi_datatype_extent(recvtype, &lb, &recvinc);
244   sendinc *= sendcount;
245   recvinc *= recvcount;
246   /* simple optimization */
247   err = smpi_datatype_copy(&((char*)sendbuf)[rank * sendinc], sendcount, sendtype, &((char*)recvbuf)[rank * recvinc], recvcount, recvtype);
248   if(err == MPI_SUCCESS && size > 1) {
249     /* Initiate all send/recv to/from others. */
250     requests = xbt_new(MPI_Request, 2 * (size - 1));
251     /* Post all receives first -- a simple optimization */
252     count = 0;
253     for(i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
254       requests[count] = smpi_irecv_init(&((char*)recvbuf)[i * recvinc], recvcount, recvtype, i, system_tag, comm);
255       count++;
256     }
257     /* Now post all sends in reverse order
258      *   - We would like to minimize the search time through message queue
259      *     when messages actually arrive in the order in which they were posted.
260      * TODO: check the previous assertion
261      */
262     for(i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size ) {
263       requests[count] = smpi_isend_init(&((char*)sendbuf)[i * sendinc], sendcount, sendtype, i, system_tag, comm);
264       count++;
265     }
266     /* Wait for them all.*/
267     smpi_mpi_startall(count, requests);
268     DEBUG2("<%d> wait for %d requests", rank, count);
269     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
270     xbt_free(requests);
271   }
272   return err;
273 }
274
275 /**
276  * Alltoall pairwise
277  *
278  * this algorithm performs size steps (1<=s<=size) and
279  * at each step s, a process p sends iand receive to.from a unique distinct remote process
280  * size=5 : s=1:  4->0->1, 0->1->2, 1->2->3, ...
281  *          s=2:  3->0->2, 4->1->3, 0->2->4, 1->3->0 , 2->4->1
282  *          ....
283  * Openmpi calls this routine when the message size sent to each rank is greater than 3000 bytes
284  **/
285 int smpi_coll_tuned_alltoall_pairwise(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
286   int system_tag = 999;
287   int rank, size, step, sendto, recvfrom, sendsize, recvsize;
288
289   rank = smpi_comm_rank(comm);
290   size = smpi_comm_size(comm);
291   DEBUG1("<%d> algorithm alltoall_pairwise() called.", rank);
292   sendsize = smpi_datatype_size(sendtype);
293   recvsize = smpi_datatype_size(recvtype);
294   /* Perform pairwise exchange - starting from 1 so the local copy is last */
295   for(step = 1; step < size + 1; step++) {
296     /* who do we talk to in this step? */
297     sendto  = (rank+step)%size;
298     recvfrom = (rank+size-step)%size;
299     /* send and receive */
300     smpi_mpi_sendrecv(&((char*)sendbuf)[sendto * sendsize * sendcount], sendcount, sendtype, sendto, system_tag, &((char*)recvbuf)[recvfrom * recvsize * recvcount], recvcount, recvtype, recvfrom, system_tag, comm, MPI_STATUS_IGNORE);
301   }
302   return MPI_SUCCESS;
303 }
304
305 int smpi_coll_basic_alltoallv(void* sendbuf, int* sendcounts, int* senddisps, MPI_Datatype sendtype, void* recvbuf, int *recvcounts, int* recvdisps, MPI_Datatype recvtype, MPI_Comm comm) {
306   int system_tag = 889;
307   int i, rank, size, err, count;
308   MPI_Aint lb, sendextent, recvextent;
309   MPI_Request* requests;
310
311   /* Initialize. */
312   rank = smpi_comm_rank(comm);
313   size = smpi_comm_size(comm);
314   DEBUG1("<%d> algorithm basic_alltoallv() called.", rank);
315   err = smpi_datatype_extent(sendtype, &lb, &sendextent);
316   err = smpi_datatype_extent(recvtype, &lb, &recvextent);
317   /* Local copy from self */
318   err = smpi_datatype_copy(&((char*)sendbuf)[senddisps[rank] * sendextent], sendcounts[rank], sendtype, &((char*)recvbuf)[recvdisps[rank] * recvextent], recvcounts[rank], recvtype);
319   if(err == MPI_SUCCESS && size > 1) {
320     /* Initiate all send/recv to/from others. */
321     requests = xbt_new(MPI_Request, 2 * (size - 1));
322     count = 0;
323     /* Create all receives that will be posted first */
324     for(i = 0; i < size; ++i) {
325       if(i == rank || recvcounts[i] == 0) {
326         DEBUG3("<%d> skip request creation [src = %d, recvcounts[src] = %d]", rank, i, recvcounts[i]);
327         continue;
328       }
329       requests[count] = smpi_irecv_init(&((char*)recvbuf)[recvdisps[i] * recvextent], recvcounts[i], recvtype, i, system_tag, comm);
330       count++;
331     }
332     /* Now create all sends  */
333     for(i = 0; i < size; ++i) {
334       if(i == rank || sendcounts[i] == 0) {
335         DEBUG3("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]", rank, i, sendcounts[i]);
336         continue;
337       }
338       requests[count] = smpi_isend_init(&((char*)sendbuf)[senddisps[i] * sendextent], sendcounts[i], sendtype, i, system_tag, comm);
339       count++;
340     }
341     /* Wait for them all.*/
342     smpi_mpi_startall(count, requests);
343     DEBUG2("<%d> wait for %d requests", rank, count);
344     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
345     xbt_free(requests);
346   }
347   return err;
348 }