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_isend_init(buf, count, datatype, tree->child[i], system_tag + tree->child[i], comm);
115 smpi_mpi_startall(tree->numChildren, requests);
116 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
123 static void tree_antibcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm, proc_tree_t tree) {
124 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
126 MPI_Request* requests;
128 rank = smpi_comm_rank(comm);
129 // everyone sends to its parent, except root.
131 DEBUG3("<%d> tree_antibcast(): i am not root: send to %d, tag=%d)",
132 rank, tree->parent, system_tag + rank);
133 smpi_mpi_send(buf, count, datatype, tree->parent, system_tag + rank, comm);
135 //every one receives as many messages as it has children
136 requests = xbt_new(MPI_Request, tree->numChildren);
137 DEBUG2("<%d> creates %d requests (1 per child)", rank, tree->numChildren);
138 for(i = 0; i < tree->numChildren; i++) {
139 if(tree->child[i] == -1) {
140 requests[i] = MPI_REQUEST_NULL;
142 DEBUG3("<%d> recv from <%d>, tag=%d", rank, tree->child[i], system_tag + tree->child[i]);
143 requests[i] = smpi_irecv_init(buf, count, datatype, tree->child[i], system_tag + tree->child[i], comm);
146 smpi_mpi_startall(tree->numChildren, requests);
147 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
152 * bcast with a binary, ternary, or whatever tree ..
154 void nary_tree_bcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm, int arity) {
155 proc_tree_t tree = alloc_tree(arity);
158 rank = smpi_comm_rank(comm);
159 size = smpi_comm_size(comm);
160 build_tree(rank, size, &tree);
161 tree_bcast(buf, count, datatype, root, comm, tree);
166 * barrier with a binary, ternary, or whatever tree ..
168 void nary_tree_barrier(MPI_Comm comm, int arity) {
169 proc_tree_t tree = alloc_tree( arity );
173 rank = smpi_comm_rank(comm);
174 size = smpi_comm_size(comm);
175 build_tree(rank, size, &tree);
176 tree_antibcast(&dummy, 1, MPI_CHAR, 0, comm, tree);
177 tree_bcast(&dummy, 1, MPI_CHAR, 0, comm, tree);
184 * Openmpi calls this routine when the message size sent to each rank < 2000 bytes and size < 12
186 int smpi_coll_tuned_alltoall_bruck(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
187 int system_tag = 777;
188 int i, rank, size, err, count;
189 MPI_Aint lb, sendextent, recvextent;
190 MPI_Request* requests;
192 // FIXME: check implementation
193 rank = smpi_comm_rank(comm);
194 size = smpi_comm_size(comm);
195 DEBUG1("<%d> algorithm alltoall_bruck() called.", rank);
196 err = smpi_datatype_extent(sendtype, &lb, &sendextent);
197 err = smpi_datatype_extent(recvtype, &lb, &recvextent);
198 /* Local copy from self */
199 err = smpi_datatype_copy(&((char*)sendbuf)[rank * sendextent], sendcount, sendtype, &((char*)recvbuf)[rank * recvextent], recvcount, recvtype);
200 if(err == MPI_SUCCESS && size > 1) {
201 /* Initiate all send/recv to/from others. */
202 requests = xbt_new(MPI_Request, 2 * (size - 1));
204 /* Create all receives that will be posted first */
205 for(i = 0; i < size; ++i) {
207 DEBUG3("<%d> skip request creation [src = %d, recvcount = %d]", rank, i, recvcount);
210 requests[count] = smpi_irecv_init(&((char*)recvbuf)[i * recvextent], recvcount, recvtype, i, system_tag, comm);
213 /* Now create all sends */
214 for(i = 0; i < size; ++i) {
216 DEBUG3("<%d> skip request creation [dst = %d, sendcount = %d]", rank, i, sendcount);
219 requests[count] = smpi_isend_init(&((char*)sendbuf)[i * sendextent], sendcount, sendtype, i, system_tag, comm);
222 /* Wait for them all.*/
223 smpi_mpi_startall(count, requests);
224 DEBUG2("<%d> wait for %d requests", rank, count);
225 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
232 * Alltoall basic_linear
234 int smpi_coll_tuned_alltoall_basic_linear(void *sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
235 int system_tag = 888;
236 int i, rank, size, err, count;
237 MPI_Aint lb, sendinc, recvinc;
238 MPI_Request *requests;
241 rank = smpi_comm_rank(comm);
242 size = smpi_comm_size(comm);
243 DEBUG1("<%d> algorithm alltoall_basic_linear() called.", rank);
244 err = smpi_datatype_extent(sendtype, &lb, &sendinc);
245 err = smpi_datatype_extent(recvtype, &lb, &recvinc);
246 sendinc *= sendcount;
247 recvinc *= recvcount;
248 /* simple optimization */
249 err = smpi_datatype_copy(&((char*)sendbuf)[rank * sendinc], sendcount, sendtype, &((char*)recvbuf)[rank * recvinc], recvcount, recvtype);
250 if(err == MPI_SUCCESS && size > 1) {
251 /* Initiate all send/recv to/from others. */
252 requests = xbt_new(MPI_Request, 2 * (size - 1));
253 /* Post all receives first -- a simple optimization */
255 for(i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
256 requests[count] = smpi_irecv_init(&((char*)recvbuf)[i * recvinc], recvcount, recvtype, i, system_tag, comm);
259 /* Now post all sends in reverse order
260 * - We would like to minimize the search time through message queue
261 * when messages actually arrive in the order in which they were posted.
262 * TODO: check the previous assertion
264 for(i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size ) {
265 requests[count] = smpi_isend_init(&((char*)sendbuf)[i * sendinc], sendcount, sendtype, i, system_tag, comm);
268 /* Wait for them all.*/
269 smpi_mpi_startall(count, requests);
270 DEBUG2("<%d> wait for %d requests", rank, count);
271 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
280 * this algorithm performs size steps (1<=s<=size) and
281 * at each step s, a process p sends iand receive to.from a unique distinct remote process
282 * size=5 : s=1: 4->0->1, 0->1->2, 1->2->3, ...
283 * s=2: 3->0->2, 4->1->3, 0->2->4, 1->3->0 , 2->4->1
285 * Openmpi calls this routine when the message size sent to each rank is greater than 3000 bytes
287 int smpi_coll_tuned_alltoall_pairwise(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
288 int system_tag = 999;
289 int rank, size, step, sendto, recvfrom, sendsize, recvsize;
291 rank = smpi_comm_rank(comm);
292 size = smpi_comm_size(comm);
293 DEBUG1("<%d> algorithm alltoall_pairwise() called.", rank);
294 sendsize = smpi_datatype_size(sendtype);
295 recvsize = smpi_datatype_size(recvtype);
296 /* Perform pairwise exchange - starting from 1 so the local copy is last */
297 for(step = 1; step < size + 1; step++) {
298 /* who do we talk to in this step? */
299 sendto = (rank+step)%size;
300 recvfrom = (rank+size-step)%size;
301 /* send and receive */
302 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);
307 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) {
308 int system_tag = 889;
309 int i, rank, size, err, count;
310 MPI_Aint lb, sendextent, recvextent;
311 MPI_Request* requests;
314 rank = smpi_comm_rank(comm);
315 size = smpi_comm_size(comm);
316 DEBUG1("<%d> algorithm basic_alltoallv() called.", rank);
317 err = smpi_datatype_extent(sendtype, &lb, &sendextent);
318 err = smpi_datatype_extent(recvtype, &lb, &recvextent);
319 /* Local copy from self */
320 err = smpi_datatype_copy(&((char*)sendbuf)[senddisps[rank] * sendextent], sendcounts[rank], sendtype, &((char*)recvbuf)[recvdisps[rank] * recvextent], recvcounts[rank], recvtype);
321 if(err == MPI_SUCCESS && size > 1) {
322 /* Initiate all send/recv to/from others. */
323 requests = xbt_new(MPI_Request, 2 * (size - 1));
325 /* Create all receives that will be posted first */
326 for(i = 0; i < size; ++i) {
327 if(i == rank || recvcounts[i] == 0) {
328 DEBUG3("<%d> skip request creation [src = %d, recvcounts[src] = %d]", rank, i, recvcounts[i]);
331 requests[count] = smpi_irecv_init(&((char*)recvbuf)[recvdisps[i] * recvextent], recvcounts[i], recvtype, i, system_tag, comm);
334 /* Now create all sends */
335 for(i = 0; i < size; ++i) {
336 if(i == rank || sendcounts[i] == 0) {
337 DEBUG3("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]", rank, i, sendcounts[i]);
340 requests[count] = smpi_isend_init(&((char*)sendbuf)[senddisps[i] * sendextent], sendcounts[i], sendtype, i, system_tag, comm);
343 /* Wait for them all.*/
344 smpi_mpi_startall(count, requests);
345 DEBUG2("<%d> wait for %d requests", rank, count);
346 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);