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 root the rank of the tree root
62 * @param rank the rank of the calling process
63 * @param size the total number of processes
65 static void build_tree(int root, int rank, int size, proc_tree_t * tree)
67 int index = (rank - root + size) % size;
68 int firstChildIdx = index * (*tree)->PROCTREE_A + 1;
74 for (i = 0; i < (*tree)->PROCTREE_A && firstChildIdx + i < size; i++) {
75 (*tree)->child[i] = (firstChildIdx + i + root) % size;
76 (*tree)->numChildren++;
82 (*tree)->parent = (((index - 1) / (*tree)->PROCTREE_A) + root) % size;
89 static void tree_bcast(void *buf, int count, MPI_Datatype datatype,
90 MPI_Comm comm, proc_tree_t tree)
92 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
94 MPI_Request *requests;
96 rank = smpi_comm_rank(comm);
97 /* wait for data from my parent in the tree */
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);
104 requests = xbt_new(MPI_Request, tree->numChildren);
105 XBT_DEBUG("<%d> creates %d requests (1 per child)", rank,
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;
112 XBT_DEBUG("<%d> send to <%d>, tag=%d", rank, tree->child[i],
113 system_tag + tree->child[i]);
115 smpi_isend_init(buf, count, datatype, tree->child[i],
116 system_tag + tree->child[i], comm);
119 smpi_mpi_startall(tree->numChildren, requests);
120 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
127 static void tree_antibcast(void *buf, int count, MPI_Datatype datatype,
128 MPI_Comm comm, proc_tree_t tree)
130 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
132 MPI_Request *requests;
134 rank = smpi_comm_rank(comm);
135 // everyone sends to its parent, except root.
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,
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,
146 for (i = 0; i < tree->numChildren; i++) {
147 if (tree->child[i] == -1) {
148 requests[i] = MPI_REQUEST_NULL;
150 XBT_DEBUG("<%d> recv from <%d>, tag=%d", rank, tree->child[i],
151 system_tag + tree->child[i]);
153 smpi_irecv_init(buf, count, datatype, tree->child[i],
154 system_tag + tree->child[i], comm);
157 smpi_mpi_startall(tree->numChildren, requests);
158 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
163 * bcast with a binary, ternary, or whatever tree ..
165 void nary_tree_bcast(void *buf, int count, MPI_Datatype datatype, int root,
166 MPI_Comm comm, int arity)
168 proc_tree_t tree = alloc_tree(arity);
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);
179 * barrier with a binary, ternary, or whatever tree ..
181 void nary_tree_barrier(MPI_Comm comm, int arity)
183 proc_tree_t tree = alloc_tree(arity);
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);
198 * Openmpi calls this routine when the message size sent to each rank < 2000 bytes and size < 12
200 int smpi_coll_tuned_alltoall_bruck(void *sendbuf, int sendcount,
201 MPI_Datatype sendtype, void *recvbuf,
202 int recvcount, MPI_Datatype recvtype,
205 int system_tag = 777;
206 int i, rank, size, err, count;
208 MPI_Aint sendextent = 0;
209 MPI_Aint recvextent = 0;
210 MPI_Request *requests;
212 // FIXME: check implementation
213 rank = smpi_comm_rank(comm);
214 size = smpi_comm_size(comm);
215 XBT_DEBUG("<%d> algorithm alltoall_bruck() called.", rank);
216 err = smpi_datatype_extent(sendtype, &lb, &sendextent);
217 err = smpi_datatype_extent(recvtype, &lb, &recvextent);
218 /* Local copy from self */
220 smpi_datatype_copy(&((char *) sendbuf)[rank * sendextent], sendcount,
221 sendtype, &((char *) recvbuf)[rank * recvextent],
222 recvcount, recvtype);
223 if (err == MPI_SUCCESS && size > 1) {
224 /* Initiate all send/recv to/from others. */
225 requests = xbt_new(MPI_Request, 2 * (size - 1));
227 /* Create all receives that will be posted first */
228 for (i = 0; i < size; ++i) {
230 XBT_DEBUG("<%d> skip request creation [src = %d, recvcount = %d]",
235 smpi_irecv_init(&((char *) recvbuf)[i * recvextent], recvcount,
236 recvtype, i, system_tag, comm);
239 /* Now create all sends */
240 for (i = 0; i < size; ++i) {
242 XBT_DEBUG("<%d> skip request creation [dst = %d, sendcount = %d]",
247 smpi_isend_init(&((char *) sendbuf)[i * sendextent], sendcount,
248 sendtype, i, system_tag, comm);
251 /* Wait for them all. */
252 smpi_mpi_startall(count, requests);
253 XBT_DEBUG("<%d> wait for %d requests", rank, count);
254 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
261 * Alltoall basic_linear
263 int smpi_coll_tuned_alltoall_basic_linear(void *sendbuf, int sendcount,
264 MPI_Datatype sendtype,
265 void *recvbuf, int recvcount,
266 MPI_Datatype recvtype,
269 int system_tag = 888;
270 int i, rank, size, err, count;
272 MPI_Aint sendinc = 0;
273 MPI_Aint recvinc = 0;
274 MPI_Request *requests;
277 rank = smpi_comm_rank(comm);
278 size = smpi_comm_size(comm);
279 XBT_DEBUG("<%d> algorithm alltoall_basic_linear() called.", rank);
280 err = smpi_datatype_extent(sendtype, &lb, &sendinc);
281 err = smpi_datatype_extent(recvtype, &lb, &recvinc);
282 sendinc *= sendcount;
283 recvinc *= recvcount;
284 /* simple optimization */
286 smpi_datatype_copy(&((char *) sendbuf)[rank * sendinc], sendcount,
287 sendtype, &((char *) recvbuf)[rank * recvinc],
288 recvcount, recvtype);
289 if (err == MPI_SUCCESS && size > 1) {
290 /* Initiate all send/recv to/from others. */
291 requests = xbt_new(MPI_Request, 2 * (size - 1));
292 /* Post all receives first -- a simple optimization */
294 for (i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
296 smpi_irecv_init(&((char *) recvbuf)[i * recvinc], recvcount,
297 recvtype, i, system_tag, comm);
300 /* Now post all sends in reverse order
301 * - We would like to minimize the search time through message queue
302 * when messages actually arrive in the order in which they were posted.
303 * TODO: check the previous assertion
305 for (i = (rank + size - 1) % size; i != rank;
306 i = (i + size - 1) % size) {
308 smpi_isend_init(&((char *) sendbuf)[i * sendinc], sendcount,
309 sendtype, i, system_tag, comm);
312 /* Wait for them all. */
313 smpi_mpi_startall(count, requests);
314 XBT_DEBUG("<%d> wait for %d requests", rank, count);
315 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
324 * this algorithm performs size steps (1<=s<=size) and
325 * at each step s, a process p sends iand receive to.from a unique distinct remote process
326 * size=5 : s=1: 4->0->1, 0->1->2, 1->2->3, ...
327 * s=2: 3->0->2, 4->1->3, 0->2->4, 1->3->0 , 2->4->1
329 * Openmpi calls this routine when the message size sent to each rank is greater than 3000 bytes
331 int smpi_coll_tuned_alltoall_pairwise(void *sendbuf, int sendcount,
332 MPI_Datatype sendtype, void *recvbuf,
333 int recvcount, MPI_Datatype recvtype,
336 int system_tag = 999;
337 int rank, size, step, sendto, recvfrom, sendsize, recvsize;
339 rank = smpi_comm_rank(comm);
340 size = smpi_comm_size(comm);
341 XBT_DEBUG("<%d> algorithm alltoall_pairwise() called.", rank);
342 sendsize = smpi_datatype_size(sendtype);
343 recvsize = smpi_datatype_size(recvtype);
344 /* Perform pairwise exchange - starting from 1 so the local copy is last */
345 for (step = 1; step < size + 1; step++) {
346 /* who do we talk to in this step? */
347 sendto = (rank + step) % size;
348 recvfrom = (rank + size - step) % size;
349 /* send and receive */
350 smpi_mpi_sendrecv(&((char *) sendbuf)[sendto * sendsize * sendcount],
351 sendcount, sendtype, sendto, system_tag,
352 &((char *) recvbuf)[recvfrom * recvsize * recvcount],
353 recvcount, recvtype, recvfrom, system_tag, comm,
359 int smpi_coll_basic_alltoallv(void *sendbuf, int *sendcounts,
360 int *senddisps, MPI_Datatype sendtype,
361 void *recvbuf, int *recvcounts,
362 int *recvdisps, MPI_Datatype recvtype,
365 int system_tag = 889;
366 int i, rank, size, err, count;
368 MPI_Aint sendextent = 0;
369 MPI_Aint recvextent = 0;
370 MPI_Request *requests;
373 rank = smpi_comm_rank(comm);
374 size = smpi_comm_size(comm);
375 XBT_DEBUG("<%d> algorithm basic_alltoallv() called.", rank);
376 err = smpi_datatype_extent(sendtype, &lb, &sendextent);
377 err = smpi_datatype_extent(recvtype, &lb, &recvextent);
378 /* Local copy from self */
380 smpi_datatype_copy(&((char *) sendbuf)[senddisps[rank] * sendextent],
381 sendcounts[rank], sendtype,
382 &((char *) recvbuf)[recvdisps[rank] * recvextent],
383 recvcounts[rank], recvtype);
384 if (err == MPI_SUCCESS && size > 1) {
385 /* Initiate all send/recv to/from others. */
386 requests = xbt_new(MPI_Request, 2 * (size - 1));
388 /* Create all receives that will be posted first */
389 for (i = 0; i < size; ++i) {
390 if (i == rank || recvcounts[i] == 0) {
392 ("<%d> skip request creation [src = %d, recvcounts[src] = %d]",
393 rank, i, recvcounts[i]);
397 smpi_irecv_init(&((char *) recvbuf)[recvdisps[i] * recvextent],
398 recvcounts[i], recvtype, i, system_tag, comm);
401 /* Now create all sends */
402 for (i = 0; i < size; ++i) {
403 if (i == rank || sendcounts[i] == 0) {
405 ("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]",
406 rank, i, sendcounts[i]);
410 smpi_isend_init(&((char *) sendbuf)[senddisps[i] * sendextent],
411 sendcounts[i], sendtype, i, system_tag, comm);
414 /* Wait for them all. */
415 smpi_mpi_startall(count, requests);
416 XBT_DEBUG("<%d> wait for %d requests", rank, count);
417 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);