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
199 * FIXME: uh, check smpi_pmpi again, but this routine is called for > 12, not
202 int smpi_coll_tuned_alltoall_bruck(void *sendbuf, int sendcount,
203 MPI_Datatype sendtype, void *recvbuf,
204 int recvcount, MPI_Datatype recvtype,
207 int system_tag = 777;
208 int i, rank, size, err, count;
210 MPI_Aint sendext = 0;
211 MPI_Aint recvext = 0;
212 MPI_Request *requests;
214 // FIXME: check implementation
215 rank = smpi_comm_rank(comm);
216 size = smpi_comm_size(comm);
217 XBT_DEBUG("<%d> algorithm alltoall_bruck() called.", rank);
218 err = smpi_datatype_extent(sendtype, &lb, &sendext);
219 err = smpi_datatype_extent(recvtype, &lb, &recvext);
220 /* Local copy from self */
222 smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext,
224 (char *)recvbuf + rank * recvcount * recvext,
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 * recvcount * recvext, 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 * sendcount * sendext, 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;
274 MPI_Aint lb = 0, sendext = 0, recvext = 0;
275 MPI_Request *requests;
278 rank = smpi_comm_rank(comm);
279 size = smpi_comm_size(comm);
280 XBT_DEBUG("<%d> algorithm alltoall_basic_linear() called.", rank);
281 err = smpi_datatype_extent(sendtype, &lb, &sendext);
282 err = smpi_datatype_extent(recvtype, &lb, &recvext);
283 /* simple optimization */
284 err = smpi_datatype_copy((char *)sendbuf + rank * sendcount * sendext,
286 (char *)recvbuf + rank * recvcount * recvext,
287 recvcount, recvtype);
288 if (err == MPI_SUCCESS && size > 1) {
289 /* Initiate all send/recv to/from others. */
290 requests = xbt_new(MPI_Request, 2 * (size - 1));
291 /* Post all receives first -- a simple optimization */
293 for (i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
295 smpi_irecv_init((char *)recvbuf + i * recvcount * recvext, recvcount,
296 recvtype, i, system_tag, comm);
299 /* Now post all sends in reverse order
300 * - We would like to minimize the search time through message queue
301 * when messages actually arrive in the order in which they were posted.
302 * TODO: check the previous assertion
304 for (i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size) {
306 smpi_isend_init((char *)sendbuf + i * sendcount * sendext, sendcount,
307 sendtype, i, system_tag, comm);
310 /* Wait for them all. */
311 smpi_mpi_startall(count, requests);
312 XBT_DEBUG("<%d> wait for %d requests", rank, count);
313 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
322 * this algorithm performs size steps (1<=s<=size) and
323 * at each step s, a process p sends iand receive to.from a unique distinct remote process
324 * size=5 : s=1: 4->0->1, 0->1->2, 1->2->3, ...
325 * s=2: 3->0->2, 4->1->3, 0->2->4, 1->3->0 , 2->4->1
327 * Openmpi calls this routine when the message size sent to each rank is greater than 3000 bytes
329 int smpi_coll_tuned_alltoall_pairwise(void *sendbuf, int sendcount,
330 MPI_Datatype sendtype, void *recvbuf,
331 int recvcount, MPI_Datatype recvtype,
334 int system_tag = 999;
335 int rank, size, step, sendto, recvfrom, sendsize, recvsize;
337 rank = smpi_comm_rank(comm);
338 size = smpi_comm_size(comm);
339 XBT_DEBUG("<%d> algorithm alltoall_pairwise() called.", rank);
340 sendsize = smpi_datatype_size(sendtype);
341 recvsize = smpi_datatype_size(recvtype);
342 /* Perform pairwise exchange - starting from 1 so the local copy is last */
343 for (step = 1; step < size + 1; step++) {
344 /* who do we talk to in this step? */
345 sendto = (rank + step) % size;
346 recvfrom = (rank + size - step) % size;
347 /* send and receive */
348 smpi_mpi_sendrecv(&((char *) sendbuf)[sendto * sendsize * sendcount],
349 sendcount, sendtype, sendto, system_tag,
350 &((char *) recvbuf)[recvfrom * recvsize * recvcount],
351 recvcount, recvtype, recvfrom, system_tag, comm,
357 int smpi_coll_basic_alltoallv(void *sendbuf, int *sendcounts,
358 int *senddisps, MPI_Datatype sendtype,
359 void *recvbuf, int *recvcounts,
360 int *recvdisps, MPI_Datatype recvtype,
363 int system_tag = 889;
364 int i, rank, size, err, count;
365 MPI_Aint lb = 0, sendext = 0, recvext = 0;
366 MPI_Request *requests;
369 rank = smpi_comm_rank(comm);
370 size = smpi_comm_size(comm);
371 XBT_DEBUG("<%d> algorithm basic_alltoallv() called.", rank);
372 err = smpi_datatype_extent(sendtype, &lb, &sendext);
373 err = smpi_datatype_extent(recvtype, &lb, &recvext);
374 /* Local copy from self */
376 smpi_datatype_copy((char *)sendbuf + senddisps[rank] * sendext,
377 sendcounts[rank], sendtype,
378 (char *)recvbuf + recvdisps[rank] * recvext,
379 recvcounts[rank], recvtype);
380 if (err == MPI_SUCCESS && size > 1) {
381 /* Initiate all send/recv to/from others. */
382 requests = xbt_new(MPI_Request, 2 * (size - 1));
384 /* Create all receives that will be posted first */
385 for (i = 0; i < size; ++i) {
386 if (i == rank || recvcounts[i] == 0) {
388 ("<%d> skip request creation [src = %d, recvcounts[src] = %d]",
389 rank, i, recvcounts[i]);
393 smpi_irecv_init((char *)recvbuf + recvdisps[i] * recvext,
394 recvcounts[i], recvtype, i, system_tag, comm);
397 /* Now create all sends */
398 for (i = 0; i < size; ++i) {
399 if (i == rank || sendcounts[i] == 0) {
401 ("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]",
402 rank, i, sendcounts[i]);
406 smpi_isend_init((char *)sendbuf + senddisps[i] * sendext,
407 sendcounts[i], sendtype, i, system_tag, comm);
410 /* Wait for them all. */
411 smpi_mpi_startall(count, requests);
412 XBT_DEBUG("<%d> wait for %d requests", rank, count);
413 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);