Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix bug while counting created requests in alltoallv.
[simgrid.git] / src / smpi / smpi_coll.c
1 /* $Id$tag */
2
3 /* smpi_coll.c -- various optimized routing for collectives                   */
4
5 /* Copyright (c) 2009 Stephane Genaud.                                        */
6 /* All rights reserved.                                                       */
7
8 /* This program is free software; you can redistribute it and/or modify it
9  *  * under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <assert.h>
14
15 #include "private.h"
16 #include "smpi_coll_private.h"
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_coll, smpi,
19                                 "Logging specific to SMPI (coll)");
20
21 struct s_proc_tree {
22   int PROCTREE_A;
23   int numChildren;
24   int * child;
25   int parent;
26   int me;
27   int root;
28   int isRoot;
29 };
30 typedef struct s_proc_tree * proc_tree_t;
31
32 /**
33  * alloc and init
34  **/
35 static proc_tree_t alloc_tree(int arity) {
36   proc_tree_t tree;
37   int i;
38
39   tree = xbt_new(struct s_proc_tree, 1);
40   tree->PROCTREE_A = arity;
41   tree->isRoot = 0; 
42   tree->numChildren = 0;
43   tree->child = xbt_new(int, arity);
44   for(i = 0; i < arity; i++) {
45     tree->child[i] = -1;
46   }
47   tree->root = -1;
48   tree->parent = -1;
49   return tree;
50 }
51
52 /**
53  * free
54  **/
55 static void free_tree(proc_tree_t tree) {
56   xbt_free(tree->child );
57   xbt_free(tree);
58 }
59
60 /**
61  * Build the tree depending on a process rank (index) and the group size (extent)
62  * @param index the rank of the calling process
63  * @param extent the total number of processes
64  **/
65 static void build_tree(int index, int extent, proc_tree_t* tree) {
66   int places = (*tree)->PROCTREE_A * index;
67   int i, ch, pr;
68
69   (*tree)->me = index;
70   (*tree)->root = 0 ;
71   for(i = 1; i <= (*tree)->PROCTREE_A; i++) {
72     ++places;
73     ch = (*tree)->PROCTREE_A * index + i + (*tree)->root;
74     ch %= extent;
75     if(places < extent) {
76       (*tree)->child[i - 1] = ch;
77       (*tree)->numChildren++;
78     }
79   }
80   if(index == (*tree)->root) {
81     (*tree)->isRoot = 1;
82   } else {
83     (*tree)->isRoot = 0;
84     pr = (index - 1) / (*tree)->PROCTREE_A;
85     (*tree)->parent = pr;
86   }
87 }
88
89 /**
90  * bcast
91  **/
92 static void tree_bcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm, proc_tree_t tree) {
93   int system_tag = 999;  // used negative int but smpi_create_request() declares this illegal (to be checked)
94   int rank, i;
95   MPI_Request* requests;
96
97   rank = smpi_comm_rank(comm);
98   /* wait for data from my parent in the tree */
99   if(!tree->isRoot) {
100     DEBUG3("<%d> tree_bcast(): i am not root: recv from %d, tag=%d)",
101            rank, tree->parent, system_tag + rank);
102     smpi_mpi_recv(buf, count, datatype, tree->parent, system_tag + rank, comm, MPI_STATUS_IGNORE);
103   }
104   requests = xbt_new(MPI_Request, tree->numChildren);
105   DEBUG2("<%d> creates %d requests (1 per child)\n", rank, tree->numChildren);
106   /* iniates sends to ranks lower in the tree */
107   for(i = 0; i < tree->numChildren; i++) {
108     if(tree->child[i] == -1) {
109       requests[i] = MPI_REQUEST_NULL;
110     } else {
111       DEBUG3("<%d> send to <%d>, tag=%d", rank, tree->child[i], system_tag + tree->child[i]);
112       requests[i] = smpi_mpi_isend(buf, count, datatype, tree->child[i], system_tag + tree->child[i], comm);
113     }
114   }
115   smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
116   xbt_free(requests);
117 }
118
119 /**
120  * anti-bcast
121  **/
122 static void tree_antibcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm, proc_tree_t tree) {
123   int system_tag = 999;  // used negative int but smpi_create_request() declares this illegal (to be checked)
124   int rank, i;
125   MPI_Request* requests;
126
127   rank = smpi_comm_rank(comm);
128   // everyone sends to its parent, except root.
129   if(!tree->isRoot) {
130     DEBUG3("<%d> tree_antibcast(): i am not root: send to %d, tag=%d)",
131            rank, tree->parent, system_tag + rank);
132     smpi_mpi_send(buf, count, datatype, tree->parent, system_tag + rank, comm);
133   }
134   //every one receives as many messages as it has children
135   requests = xbt_new(MPI_Request, tree->numChildren);
136   DEBUG2("<%d> creates %d requests (1 per child)\n", rank, tree->numChildren);
137   for(i = 0; i < tree->numChildren; i++) {
138     if(tree->child[i] == -1) {
139       requests[i] = MPI_REQUEST_NULL;
140     } else {
141       DEBUG3("<%d> recv from <%d>, tag=%d", rank, tree->child[i], system_tag + tree->child[i]);
142       requests[i] = smpi_mpi_irecv(buf, count, datatype, tree->child[i], system_tag + tree->child[i], comm);
143     }
144   }
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   DEBUG0("coll:tuned:alltoall_intra_bruck ** NOT IMPLEMENTED YET**");
186   return MPI_SUCCESS;
187 }
188
189 /**
190  * Alltoall basic_linear
191  **/
192 int smpi_coll_tuned_alltoall_basic_linear(void *sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
193   int system_tag = 888;
194   int i, rank, size, err, count;
195   MPI_Aint lb, sendinc, recvinc;
196   MPI_Request *requests;
197
198   /* Initialize. */
199   rank = smpi_comm_rank(comm);
200   size = smpi_comm_size(comm);
201   DEBUG1("<%d> algorithm alltoall_basic_linear() called.", rank);
202   err = smpi_datatype_extent(sendtype, &lb, &sendinc);
203   err = smpi_datatype_extent(recvtype, &lb, &recvinc);
204   sendinc *= sendcount;
205   recvinc *= recvcount;
206   /* simple optimization */
207   err = smpi_datatype_copy(&((char*)sendbuf)[rank * sendinc], sendcount, sendtype, &((char*)recvbuf)[rank * recvinc], recvcount, recvtype);
208   if(err == MPI_SUCCESS && size > 1) {
209     /* Initiate all send/recv to/from others. */
210     requests = xbt_new(MPI_Request, 2 * (size - 1));
211     /* Post all receives first -- a simple optimization */
212     count = 0;
213     for(i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
214       requests[count] = smpi_mpi_irecv(&((char*)recvbuf)[i * recvinc], recvcount, recvtype, i, system_tag, comm);
215       count++;
216     }
217     /* Now post all sends in reverse order 
218      *   - We would like to minimize the search time through message queue
219      *     when messages actually arrive in the order in which they were posted.
220      * TODO: check the previous assertion
221      */
222     for(i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size ) {
223       requests[count] = smpi_mpi_isend(&((char*)sendbuf)[i * sendinc], sendcount, sendtype, i, system_tag, comm);
224       count++;
225     }
226     /* Wait for them all.  If there's an error, note that we don't
227      * care what the error was -- just that there *was* an error.  The
228      * PML will finish all requests, even if one or more of them fail.
229      * i.e., by the end of this call, all the requests are free-able.
230      * So free them anyway -- even if there was an error, and return
231      * the error after we free everything.
232      */
233     DEBUG2("<%d> wait for %d requests", rank, count);
234     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
235     xbt_free(requests);
236   }
237   return err;
238 }
239
240 /**
241  * Alltoall pairwise
242  *
243  * this algorithm performs size steps (1<=s<=size) and
244  * at each step s, a process p sends iand receive to.from a unique distinct remote process
245  * size=5 : s=1:  4->0->1, 0->1->2, 1->2->3, ... 
246  *          s=2:  3->0->2, 4->1->3, 0->2->4, 1->3->0 , 2->4->1
247  *          .... 
248  * Openmpi calls this routine when the message size sent to each rank is greater than 3000 bytes
249  **/
250 int smpi_coll_tuned_alltoall_pairwise(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
251   int system_tag = 999;
252   int rank, size, step, sendto, recvfrom, sendsize, recvsize;
253
254   rank = smpi_comm_rank(comm);
255   size = smpi_comm_size(comm);
256   DEBUG1("<%d> algorithm alltoall_pairwise() called.", rank);
257   sendsize = smpi_datatype_size(sendtype);
258   recvsize = smpi_datatype_size(recvtype);
259   /* Perform pairwise exchange - starting from 1 so the local copy is last */
260   for(step = 1; step < size + 1; step++) {
261     /* who do we talk to in this step? */
262     sendto  = (rank+step)%size;
263     recvfrom = (rank+size-step)%size;
264     /* send and receive */
265     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);
266   }
267   return MPI_SUCCESS;
268 }
269
270 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) {
271   int system_tag = 889;
272   int i, rank, size, err, count;
273   MPI_Aint lb, sendextent, recvextent;
274   MPI_Request* requests;
275
276   /* Initialize. */
277   rank = smpi_comm_rank(comm);
278   size = smpi_comm_size(comm);
279   DEBUG1("<%d> algorithm basic_alltoallv() called.", rank);
280   err = smpi_datatype_extent(sendtype, &lb, &sendextent);
281   err = smpi_datatype_extent(recvtype, &lb, &recvextent);
282   /* Local copy from self */
283   err = smpi_datatype_copy(&((char*)sendbuf)[senddisps[rank] * sendextent], sendcounts[rank], sendtype, &((char*)recvbuf)[recvdisps[rank] * recvextent], recvcounts[rank], recvtype);
284   if(err == MPI_SUCCESS && size > 1) {
285     /* Initiate all send/recv to/from others. */
286     requests = xbt_new(MPI_Request, 2 * (size - 1));
287     count = 0;
288     /* Create all receives that will be posted first */
289     for(i = 0; i < size; ++i) {
290       if(i == rank || recvcounts[i] == 0) {
291         DEBUG3("<%d> skip request creation [src = %d, recvcounts[src] = %d]", rank, i, recvcounts[i]);
292         continue;
293       }
294       requests[count] = smpi_mpi_irecv(&((char*)recvbuf)[recvdisps[i] * recvextent], recvcounts[i], recvtype, i, system_tag, comm);
295       count++;
296     }
297     /* Now create all sends  */
298     for(i = 0; i < size; ++i) {
299       if(i == rank || sendcounts[i] == 0) {
300         DEBUG3("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]", rank, i, sendcounts[i]);
301         continue;
302       }
303       requests[count] = smpi_mpi_isend(&((char*)sendbuf)[senddisps[i] * sendextent], sendcounts[i], sendtype, i, system_tag, comm);
304       count++;
305     }
306     /* Wait for them all.  If there's an error, note that we don't
307      * care what the error was -- just that there *was* an error.  The
308      * PML will finish all requests, even if one or more of them fail.
309      * i.e., by the end of this call, all the requests are free-able.
310      * So free them anyway -- even if there was an error, and return
311      * the error after we free everything.
312      */
313     DEBUG2("<%d> wait for %d requests", rank, count);
314     smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
315     xbt_free(requests);
316   }
317   return err;
318 }