3 /* smpi_coll.c -- various optimized routing for collectives */
5 /* Copyright (c) 2009 Stephane Genaud. */
6 /* All rights reserved. */
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. */
16 #include "smpi_coll_private.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_coll, smpi,
19 "Logging specific to SMPI (coll)");
30 typedef struct s_proc_tree * proc_tree_t;
35 static proc_tree_t alloc_tree(int arity) {
39 tree = xbt_new(struct s_proc_tree, 1);
40 tree->PROCTREE_A = arity;
42 tree->numChildren = 0;
43 tree->child = xbt_new(int, arity);
44 for(i = 0; i < arity; i++) {
55 static void free_tree(proc_tree_t tree) {
56 xbt_free(tree->child );
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
65 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;
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, 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)
95 MPI_Request* requests;
97 rank = smpi_comm_rank(comm);
98 /* wait for data from my parent in the tree */
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);
104 requests = xbt_new(MPI_Request, tree->numChildren);
105 DEBUG2("<%d> creates %d requests (1 per child)", 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;
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);
115 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
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)
125 MPI_Request* requests;
127 rank = smpi_comm_rank(comm);
128 // everyone sends to its parent, except root.
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);
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)", rank, tree->numChildren);
137 for(i = 0; i < tree->numChildren; i++) {
138 if(tree->child[i] == -1) {
139 requests[i] = MPI_REQUEST_NULL;
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);
145 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
150 * bcast with a binary, ternary, or whatever tree ..
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);
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);
164 * barrier with a binary, ternary, or whatever tree ..
166 void nary_tree_barrier(MPI_Comm comm, int arity) {
167 proc_tree_t tree = alloc_tree( arity );
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);
182 * Openmpi calls this routine when the message size sent to each rank < 2000 bytes and size < 12
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 int system_tag = 777;
186 int i, rank, size, err, count;
187 MPI_Aint lb, sendextent, recvextent;
188 MPI_Request* requests;
190 // FIXME: check implementation
191 rank = smpi_comm_rank(comm);
192 size = smpi_comm_size(comm);
193 DEBUG1("<%d> algorithm alltoall_bruck() called.", rank);
194 err = smpi_datatype_extent(sendtype, &lb, &sendextent);
195 err = smpi_datatype_extent(recvtype, &lb, &recvextent);
196 /* Local copy from self */
197 err = smpi_datatype_copy(&((char*)sendbuf)[rank * sendextent], sendcount, sendtype, &((char*)recvbuf)[rank * recvextent], recvcount, recvtype);
198 if(err == MPI_SUCCESS && size > 1) {
199 /* Initiate all send/recv to/from others. */
200 requests = xbt_new(MPI_Request, 2 * (size - 1));
202 /* Create all receives that will be posted first */
203 for(i = 0; i < size; ++i) {
205 DEBUG3("<%d> skip request creation [src = %d, recvcount = %d]", rank, i, recvcount);
208 requests[count] = smpi_mpi_irecv(&((char*)recvbuf)[i * recvextent], recvcount, recvtype, i, system_tag, comm);
211 /* Now create all sends */
212 for(i = 0; i < size; ++i) {
214 DEBUG3("<%d> skip request creation [dst = %d, sendcount = %d]", rank, i, sendcount);
217 requests[count] = smpi_mpi_isend(&((char*)sendbuf)[i * sendextent], sendcount, sendtype, i, system_tag, comm);
220 /* Wait for them all. If there's an error, note that we don't
221 * care what the error was -- just that there *was* an error. The
222 * PML will finish all requests, even if one or more of them fail.
223 * i.e., by the end of this call, all the requests are free-able.
224 * So free them anyway -- even if there was an error, and return
225 * the error after we free everything.
227 DEBUG2("<%d> wait for %d requests", rank, count);
228 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
235 * Alltoall basic_linear
237 int smpi_coll_tuned_alltoall_basic_linear(void *sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
238 int system_tag = 888;
239 int i, rank, size, err, count;
240 MPI_Aint lb, sendinc, recvinc;
241 MPI_Request *requests;
244 rank = smpi_comm_rank(comm);
245 size = smpi_comm_size(comm);
246 DEBUG1("<%d> algorithm alltoall_basic_linear() called.", rank);
247 err = smpi_datatype_extent(sendtype, &lb, &sendinc);
248 err = smpi_datatype_extent(recvtype, &lb, &recvinc);
249 sendinc *= sendcount;
250 recvinc *= recvcount;
251 /* simple optimization */
252 err = smpi_datatype_copy(&((char*)sendbuf)[rank * sendinc], sendcount, sendtype, &((char*)recvbuf)[rank * recvinc], recvcount, recvtype);
253 if(err == MPI_SUCCESS && size > 1) {
254 /* Initiate all send/recv to/from others. */
255 requests = xbt_new(MPI_Request, 2 * (size - 1));
256 /* Post all receives first -- a simple optimization */
258 for(i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
259 requests[count] = smpi_mpi_irecv(&((char*)recvbuf)[i * recvinc], recvcount, recvtype, i, system_tag, comm);
262 /* Now post all sends in reverse order
263 * - We would like to minimize the search time through message queue
264 * when messages actually arrive in the order in which they were posted.
265 * TODO: check the previous assertion
267 for(i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size ) {
268 requests[count] = smpi_mpi_isend(&((char*)sendbuf)[i * sendinc], sendcount, sendtype, i, system_tag, comm);
271 /* Wait for them all. If there's an error, note that we don't
272 * care what the error was -- just that there *was* an error. The
273 * PML will finish all requests, even if one or more of them fail.
274 * i.e., by the end of this call, all the requests are free-able.
275 * So free them anyway -- even if there was an error, and return
276 * the error after we free everything.
278 DEBUG2("<%d> wait for %d requests", rank, count);
279 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
288 * this algorithm performs size steps (1<=s<=size) and
289 * at each step s, a process p sends iand receive to.from a unique distinct remote process
290 * size=5 : s=1: 4->0->1, 0->1->2, 1->2->3, ...
291 * s=2: 3->0->2, 4->1->3, 0->2->4, 1->3->0 , 2->4->1
293 * Openmpi calls this routine when the message size sent to each rank is greater than 3000 bytes
295 int smpi_coll_tuned_alltoall_pairwise(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
296 int system_tag = 999;
297 int rank, size, step, sendto, recvfrom, sendsize, recvsize;
299 rank = smpi_comm_rank(comm);
300 size = smpi_comm_size(comm);
301 DEBUG1("<%d> algorithm alltoall_pairwise() called.", rank);
302 sendsize = smpi_datatype_size(sendtype);
303 recvsize = smpi_datatype_size(recvtype);
304 /* Perform pairwise exchange - starting from 1 so the local copy is last */
305 for(step = 1; step < size + 1; step++) {
306 /* who do we talk to in this step? */
307 sendto = (rank+step)%size;
308 recvfrom = (rank+size-step)%size;
309 /* send and receive */
310 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);
315 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) {
316 int system_tag = 889;
317 int i, rank, size, err, count;
318 MPI_Aint lb, sendextent, recvextent;
319 MPI_Request* requests;
322 rank = smpi_comm_rank(comm);
323 size = smpi_comm_size(comm);
324 DEBUG1("<%d> algorithm basic_alltoallv() called.", rank);
325 err = smpi_datatype_extent(sendtype, &lb, &sendextent);
326 err = smpi_datatype_extent(recvtype, &lb, &recvextent);
327 /* Local copy from self */
328 err = smpi_datatype_copy(&((char*)sendbuf)[senddisps[rank] * sendextent], sendcounts[rank], sendtype, &((char*)recvbuf)[recvdisps[rank] * recvextent], recvcounts[rank], recvtype);
329 if(err == MPI_SUCCESS && size > 1) {
330 /* Initiate all send/recv to/from others. */
331 requests = xbt_new(MPI_Request, 2 * (size - 1));
333 /* Create all receives that will be posted first */
334 for(i = 0; i < size; ++i) {
335 if(i == rank || recvcounts[i] == 0) {
336 DEBUG3("<%d> skip request creation [src = %d, recvcounts[src] = %d]", rank, i, recvcounts[i]);
339 requests[count] = smpi_mpi_irecv(&((char*)recvbuf)[recvdisps[i] * recvextent], recvcounts[i], recvtype, i, system_tag, comm);
342 /* Now create all sends */
343 for(i = 0; i < size; ++i) {
344 if(i == rank || sendcounts[i] == 0) {
345 DEBUG3("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]", rank, i, sendcounts[i]);
348 requests[count] = smpi_mpi_isend(&((char*)sendbuf)[senddisps[i] * sendextent], sendcounts[i], sendtype, i, system_tag, comm);
351 /* Wait for them all. If there's an error, note that we don't
352 * care what the error was -- just that there *was* an error. The
353 * PML will finish all requests, even if one or more of them fail.
354 * i.e., by the end of this call, all the requests are free-able.
355 * So free them anyway -- even if there was an error, and return
356 * the error after we free everything.
358 DEBUG2("<%d> wait for %d requests", rank, count);
359 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);