1 /* smpi_coll.c -- various optimized routing for collectives */
3 /* Copyright (c) 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_coll, smpi,
16 "Logging specific to SMPI (coll)");
27 typedef struct s_proc_tree *proc_tree_t;
32 static proc_tree_t alloc_tree(int arity)
37 tree = xbt_new(struct s_proc_tree, 1);
38 tree->PROCTREE_A = arity;
40 tree->numChildren = 0;
41 tree->child = xbt_new(int, arity);
42 for (i = 0; i < arity; i++) {
53 static void free_tree(proc_tree_t tree)
55 xbt_free(tree->child);
60 * Build the tree depending on a process rank (index) and the group size (extent)
61 * @param index the rank of the calling process
62 * @param extent the total number of processes
64 static void build_tree(int index, int extent, proc_tree_t * tree)
66 int places = (*tree)->PROCTREE_A * index;
71 for (i = 1; i <= (*tree)->PROCTREE_A; i++) {
73 ch = (*tree)->PROCTREE_A * index + i + (*tree)->root;
75 if (places < extent) {
76 (*tree)->child[i - 1] = ch;
77 (*tree)->numChildren++;
80 if (index == (*tree)->root) {
84 pr = (index - 1) / (*tree)->PROCTREE_A;
92 static void tree_bcast(void *buf, int count, MPI_Datatype datatype,
93 int root, MPI_Comm comm, proc_tree_t tree)
95 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
97 MPI_Request *requests;
99 rank = smpi_comm_rank(comm);
100 /* wait for data from my parent in the tree */
102 XBT_DEBUG("<%d> tree_bcast(): i am not root: recv from %d, tag=%d)",
103 rank, tree->parent, system_tag + rank);
104 smpi_mpi_recv(buf, count, datatype, tree->parent, system_tag + rank,
105 comm, MPI_STATUS_IGNORE);
107 requests = xbt_new(MPI_Request, tree->numChildren);
108 XBT_DEBUG("<%d> creates %d requests (1 per child)", rank,
110 /* iniates sends to ranks lower in the tree */
111 for (i = 0; i < tree->numChildren; i++) {
112 if (tree->child[i] == -1) {
113 requests[i] = MPI_REQUEST_NULL;
115 XBT_DEBUG("<%d> send to <%d>, tag=%d", rank, tree->child[i],
116 system_tag + tree->child[i]);
118 smpi_isend_init(buf, count, datatype, tree->child[i],
119 system_tag + tree->child[i], comm);
122 smpi_mpi_startall(tree->numChildren, requests);
123 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
130 static void tree_antibcast(void *buf, int count, MPI_Datatype datatype,
131 int root, MPI_Comm comm, proc_tree_t tree)
133 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
135 MPI_Request *requests;
137 rank = smpi_comm_rank(comm);
138 // everyone sends to its parent, except root.
140 XBT_DEBUG("<%d> tree_antibcast(): i am not root: send to %d, tag=%d)",
141 rank, tree->parent, system_tag + rank);
142 smpi_mpi_send(buf, count, datatype, tree->parent, system_tag + rank,
145 //every one receives as many messages as it has children
146 requests = xbt_new(MPI_Request, tree->numChildren);
147 XBT_DEBUG("<%d> creates %d requests (1 per child)", rank,
149 for (i = 0; i < tree->numChildren; i++) {
150 if (tree->child[i] == -1) {
151 requests[i] = MPI_REQUEST_NULL;
153 XBT_DEBUG("<%d> recv from <%d>, tag=%d", rank, tree->child[i],
154 system_tag + tree->child[i]);
156 smpi_irecv_init(buf, count, datatype, tree->child[i],
157 system_tag + tree->child[i], comm);
160 smpi_mpi_startall(tree->numChildren, requests);
161 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
166 * bcast with a binary, ternary, or whatever tree ..
168 void nary_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root,
169 MPI_Comm comm, int arity)
171 proc_tree_t tree = alloc_tree(arity);
174 rank = smpi_comm_rank(comm);
175 size = smpi_comm_size(comm);
176 build_tree(rank, size, &tree);
177 tree_bcast(buf, count, datatype, root, comm, tree);
182 * barrier with a binary, ternary, or whatever tree ..
184 void nary_tree_barrier(MPI_Comm comm, int arity)
186 proc_tree_t tree = alloc_tree(arity);
190 rank = smpi_comm_rank(comm);
191 size = smpi_comm_size(comm);
192 build_tree(rank, size, &tree);
193 tree_antibcast(&dummy, 1, MPI_CHAR, 0, comm, tree);
194 tree_bcast(&dummy, 1, MPI_CHAR, 0, comm, tree);
201 * Openmpi calls this routine when the message size sent to each rank < 2000 bytes and size < 12
203 int smpi_coll_tuned_alltoall_bruck(void *sendbuf, int sendcount,
204 MPI_Datatype sendtype, void *recvbuf,
205 int recvcount, MPI_Datatype recvtype,
208 int system_tag = 777;
209 int i, rank, size, err, count;
211 MPI_Aint sendextent = 0;
212 MPI_Aint recvextent = 0;
213 MPI_Request *requests;
215 // FIXME: check implementation
216 rank = smpi_comm_rank(comm);
217 size = smpi_comm_size(comm);
218 XBT_DEBUG("<%d> algorithm alltoall_bruck() called.", rank);
219 err = smpi_datatype_extent(sendtype, &lb, &sendextent);
220 err = smpi_datatype_extent(recvtype, &lb, &recvextent);
221 /* Local copy from self */
223 smpi_datatype_copy(&((char *) sendbuf)[rank * sendextent], sendcount,
224 sendtype, &((char *) recvbuf)[rank * recvextent],
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));
230 /* Create all receives that will be posted first */
231 for (i = 0; i < size; ++i) {
233 XBT_DEBUG("<%d> skip request creation [src = %d, recvcount = %d]",
238 smpi_irecv_init(&((char *) recvbuf)[i * recvextent], recvcount,
239 recvtype, i, system_tag, comm);
242 /* Now create all sends */
243 for (i = 0; i < size; ++i) {
245 XBT_DEBUG("<%d> skip request creation [dst = %d, sendcount = %d]",
250 smpi_isend_init(&((char *) sendbuf)[i * sendextent], sendcount,
251 sendtype, i, system_tag, comm);
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);
264 * Alltoall basic_linear
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,
272 int system_tag = 888;
273 int i, rank, size, err, count;
275 MPI_Aint sendinc = 0;
276 MPI_Aint recvinc = 0;
277 MPI_Request *requests;
280 rank = smpi_comm_rank(comm);
281 size = smpi_comm_size(comm);
282 XBT_DEBUG("<%d> algorithm alltoall_basic_linear() called.", rank);
283 err = smpi_datatype_extent(sendtype, &lb, &sendinc);
284 err = smpi_datatype_extent(recvtype, &lb, &recvinc);
285 sendinc *= sendcount;
286 recvinc *= recvcount;
287 /* simple optimization */
289 smpi_datatype_copy(&((char *) sendbuf)[rank * sendinc], sendcount,
290 sendtype, &((char *) recvbuf)[rank * recvinc],
291 recvcount, recvtype);
292 if (err == MPI_SUCCESS && size > 1) {
293 /* Initiate all send/recv to/from others. */
294 requests = xbt_new(MPI_Request, 2 * (size - 1));
295 /* Post all receives first -- a simple optimization */
297 for (i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
299 smpi_irecv_init(&((char *) recvbuf)[i * recvinc], recvcount,
300 recvtype, i, system_tag, comm);
303 /* Now post all sends in reverse order
304 * - We would like to minimize the search time through message queue
305 * when messages actually arrive in the order in which they were posted.
306 * TODO: check the previous assertion
308 for (i = (rank + size - 1) % size; i != rank;
309 i = (i + size - 1) % size) {
311 smpi_isend_init(&((char *) sendbuf)[i * sendinc], sendcount,
312 sendtype, i, system_tag, comm);
315 /* Wait for them all. */
316 smpi_mpi_startall(count, requests);
317 XBT_DEBUG("<%d> wait for %d requests", rank, count);
318 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
327 * this algorithm performs size steps (1<=s<=size) and
328 * at each step s, a process p sends iand receive to.from a unique distinct remote process
329 * size=5 : s=1: 4->0->1, 0->1->2, 1->2->3, ...
330 * s=2: 3->0->2, 4->1->3, 0->2->4, 1->3->0 , 2->4->1
332 * Openmpi calls this routine when the message size sent to each rank is greater than 3000 bytes
334 int smpi_coll_tuned_alltoall_pairwise(void *sendbuf, int sendcount,
335 MPI_Datatype sendtype, void *recvbuf,
336 int recvcount, MPI_Datatype recvtype,
339 int system_tag = 999;
340 int rank, size, step, sendto, recvfrom, sendsize, recvsize;
342 rank = smpi_comm_rank(comm);
343 size = smpi_comm_size(comm);
344 XBT_DEBUG("<%d> algorithm alltoall_pairwise() called.", rank);
345 sendsize = smpi_datatype_size(sendtype);
346 recvsize = smpi_datatype_size(recvtype);
347 /* Perform pairwise exchange - starting from 1 so the local copy is last */
348 for (step = 1; step < size + 1; step++) {
349 /* who do we talk to in this step? */
350 sendto = (rank + step) % size;
351 recvfrom = (rank + size - step) % size;
352 /* send and receive */
353 smpi_mpi_sendrecv(&((char *) sendbuf)[sendto * sendsize * sendcount],
354 sendcount, sendtype, sendto, system_tag,
355 &((char *) recvbuf)[recvfrom * recvsize * recvcount],
356 recvcount, recvtype, recvfrom, system_tag, comm,
362 int smpi_coll_basic_alltoallv(void *sendbuf, int *sendcounts,
363 int *senddisps, MPI_Datatype sendtype,
364 void *recvbuf, int *recvcounts,
365 int *recvdisps, MPI_Datatype recvtype,
368 int system_tag = 889;
369 int i, rank, size, err, count;
371 MPI_Aint sendextent = 0;
372 MPI_Aint recvextent = 0;
373 MPI_Request *requests;
376 rank = smpi_comm_rank(comm);
377 size = smpi_comm_size(comm);
378 XBT_DEBUG("<%d> algorithm basic_alltoallv() called.", rank);
379 err = smpi_datatype_extent(sendtype, &lb, &sendextent);
380 err = smpi_datatype_extent(recvtype, &lb, &recvextent);
381 /* Local copy from self */
383 smpi_datatype_copy(&((char *) sendbuf)[senddisps[rank] * sendextent],
384 sendcounts[rank], sendtype,
385 &((char *) recvbuf)[recvdisps[rank] * recvextent],
386 recvcounts[rank], recvtype);
387 if (err == MPI_SUCCESS && size > 1) {
388 /* Initiate all send/recv to/from others. */
389 requests = xbt_new(MPI_Request, 2 * (size - 1));
391 /* Create all receives that will be posted first */
392 for (i = 0; i < size; ++i) {
393 if (i == rank || recvcounts[i] == 0) {
395 ("<%d> skip request creation [src = %d, recvcounts[src] = %d]",
396 rank, i, recvcounts[i]);
400 smpi_irecv_init(&((char *) recvbuf)[recvdisps[i] * recvextent],
401 recvcounts[i], recvtype, i, system_tag, comm);
404 /* Now create all sends */
405 for (i = 0; i < size; ++i) {
406 if (i == rank || sendcounts[i] == 0) {
408 ("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]",
409 rank, i, sendcounts[i]);
413 smpi_isend_init(&((char *) sendbuf)[senddisps[i] * sendextent],
414 sendcounts[i], sendtype, i, system_tag, comm);
417 /* Wait for them all. */
418 smpi_mpi_startall(count, requests);
419 XBT_DEBUG("<%d> wait for %d requests", rank, count);
420 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);