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;
271 MPI_Aint lb = 0, sendext = 0, recvext = 0;
272 MPI_Request *requests;
275 rank = smpi_comm_rank(comm);
276 size = smpi_comm_size(comm);
277 XBT_DEBUG("<%d> algorithm alltoall_basic_linear() called.", rank);
278 err = smpi_datatype_extent(sendtype, &lb, &sendext);
279 err = smpi_datatype_extent(recvtype, &lb, &recvext);
280 /* simple optimization */
281 err = smpi_datatype_copy(sendbuf + rank * sendcount * sendext, sendcount,
283 recvbuf + rank * recvcount * recvext, recvcount,
285 if (err == MPI_SUCCESS && size > 1) {
286 /* Initiate all send/recv to/from others. */
287 requests = xbt_new(MPI_Request, 2 * (size - 1));
288 /* Post all receives first -- a simple optimization */
290 for (i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
292 smpi_irecv_init(recvbuf + i * recvcount * recvext, recvcount,
293 recvtype, i, system_tag, comm);
296 /* Now post all sends in reverse order
297 * - We would like to minimize the search time through message queue
298 * when messages actually arrive in the order in which they were posted.
299 * TODO: check the previous assertion
301 for (i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size) {
303 smpi_isend_init(sendbuf + i * sendcount * sendext, sendcount,
304 sendtype, i, system_tag, comm);
307 /* Wait for them all. */
308 smpi_mpi_startall(count, requests);
309 XBT_DEBUG("<%d> wait for %d requests", rank, count);
310 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
319 * this algorithm performs size steps (1<=s<=size) and
320 * at each step s, a process p sends iand receive to.from a unique distinct remote process
321 * size=5 : s=1: 4->0->1, 0->1->2, 1->2->3, ...
322 * s=2: 3->0->2, 4->1->3, 0->2->4, 1->3->0 , 2->4->1
324 * Openmpi calls this routine when the message size sent to each rank is greater than 3000 bytes
326 int smpi_coll_tuned_alltoall_pairwise(void *sendbuf, int sendcount,
327 MPI_Datatype sendtype, void *recvbuf,
328 int recvcount, MPI_Datatype recvtype,
331 int system_tag = 999;
332 int rank, size, step, sendto, recvfrom, sendsize, recvsize;
334 rank = smpi_comm_rank(comm);
335 size = smpi_comm_size(comm);
336 XBT_DEBUG("<%d> algorithm alltoall_pairwise() called.", rank);
337 sendsize = smpi_datatype_size(sendtype);
338 recvsize = smpi_datatype_size(recvtype);
339 /* Perform pairwise exchange - starting from 1 so the local copy is last */
340 for (step = 1; step < size + 1; step++) {
341 /* who do we talk to in this step? */
342 sendto = (rank + step) % size;
343 recvfrom = (rank + size - step) % size;
344 /* send and receive */
345 smpi_mpi_sendrecv(&((char *) sendbuf)[sendto * sendsize * sendcount],
346 sendcount, sendtype, sendto, system_tag,
347 &((char *) recvbuf)[recvfrom * recvsize * recvcount],
348 recvcount, recvtype, recvfrom, system_tag, comm,
354 int smpi_coll_basic_alltoallv(void *sendbuf, int *sendcounts,
355 int *senddisps, MPI_Datatype sendtype,
356 void *recvbuf, int *recvcounts,
357 int *recvdisps, MPI_Datatype recvtype,
360 int system_tag = 889;
361 int i, rank, size, err, count;
362 MPI_Aint lb = 0, sendext = 0, recvext = 0;
363 MPI_Request *requests;
366 rank = smpi_comm_rank(comm);
367 size = smpi_comm_size(comm);
368 XBT_DEBUG("<%d> algorithm basic_alltoallv() called.", rank);
369 err = smpi_datatype_extent(sendtype, &lb, &sendext);
370 err = smpi_datatype_extent(recvtype, &lb, &recvext);
371 /* Local copy from self */
373 smpi_datatype_copy(sendbuf + senddisps[rank] * sendext, sendcounts[rank],
375 recvbuf + recvdisps[rank] * recvext, recvcounts[rank],
377 if (err == MPI_SUCCESS && size > 1) {
378 /* Initiate all send/recv to/from others. */
379 requests = xbt_new(MPI_Request, 2 * (size - 1));
381 /* Create all receives that will be posted first */
382 for (i = 0; i < size; ++i) {
383 if (i == rank || recvcounts[i] == 0) {
385 ("<%d> skip request creation [src = %d, recvcounts[src] = %d]",
386 rank, i, recvcounts[i]);
390 smpi_irecv_init(recvbuf + recvdisps[i] * recvext, recvcounts[i],
391 recvtype, i, system_tag, comm);
394 /* Now create all sends */
395 for (i = 0; i < size; ++i) {
396 if (i == rank || sendcounts[i] == 0) {
398 ("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]",
399 rank, i, sendcounts[i]);
403 smpi_isend_init(sendbuf + senddisps[i] * sendext, sendcounts[i],
404 sendtype, i, system_tag, comm);
407 /* Wait for them all. */
408 smpi_mpi_startall(count, requests);
409 XBT_DEBUG("<%d> wait for %d requests", rank, count);
410 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);