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. */
14 #include "smpi_coll_private.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_coll, smpi,
17 "Logging specific to SMPI (coll)");
28 typedef struct s_proc_tree * proc_tree_t;
33 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) {
54 xbt_free(tree->child );
59 * Build the tree depending on a process rank (index) and the group size (extent)
60 * @param index the rank of the calling process
61 * @param extent the total number of processes
63 static void build_tree(int index, int extent, proc_tree_t* tree) {
64 int places = (*tree)->PROCTREE_A * index;
69 for(i = 1; i <= (*tree)->PROCTREE_A; i++) {
71 ch = (*tree)->PROCTREE_A * index + i + (*tree)->root;
74 (*tree)->child[i - 1] = ch;
75 (*tree)->numChildren++;
78 if(index == (*tree)->root) {
82 pr = (index - 1) / (*tree)->PROCTREE_A;
90 static void tree_bcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm, proc_tree_t tree) {
91 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
93 MPI_Request* requests;
95 rank = smpi_comm_rank(comm);
96 /* wait for data from my parent in the tree */
98 DEBUG3("<%d> tree_bcast(): i am not root: recv from %d, tag=%d)",
99 rank, tree->parent, system_tag + rank);
100 smpi_mpi_recv(buf, count, datatype, tree->parent, system_tag + rank, comm, MPI_STATUS_IGNORE);
102 requests = xbt_new(MPI_Request, tree->numChildren);
103 DEBUG2("<%d> creates %d requests (1 per child)", rank, tree->numChildren);
104 /* iniates sends to ranks lower in the tree */
105 for(i = 0; i < tree->numChildren; i++) {
106 if(tree->child[i] == -1) {
107 requests[i] = MPI_REQUEST_NULL;
109 DEBUG3("<%d> send to <%d>, tag=%d", rank, tree->child[i], system_tag + tree->child[i]);
110 requests[i] = smpi_isend_init(buf, count, datatype, tree->child[i], system_tag + tree->child[i], comm);
113 smpi_mpi_startall(tree->numChildren, requests);
114 smpi_mpi_waitall(tree->numChildren, requests, MPI_STATUS_IGNORE);
121 static void tree_antibcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm, proc_tree_t tree) {
122 int system_tag = 999; // used negative int but smpi_create_request() declares this illegal (to be checked)
124 MPI_Request* requests;
126 rank = smpi_comm_rank(comm);
127 // everyone sends to its parent, except root.
129 DEBUG3("<%d> tree_antibcast(): i am not root: send to %d, tag=%d)",
130 rank, tree->parent, system_tag + rank);
131 smpi_mpi_send(buf, count, datatype, tree->parent, system_tag + rank, comm);
133 //every one receives as many messages as it has children
134 requests = xbt_new(MPI_Request, tree->numChildren);
135 DEBUG2("<%d> creates %d requests (1 per child)", rank, tree->numChildren);
136 for(i = 0; i < tree->numChildren; i++) {
137 if(tree->child[i] == -1) {
138 requests[i] = MPI_REQUEST_NULL;
140 DEBUG3("<%d> recv from <%d>, tag=%d", rank, tree->child[i], system_tag + tree->child[i]);
141 requests[i] = smpi_irecv_init(buf, count, datatype, tree->child[i], system_tag + tree->child[i], comm);
144 smpi_mpi_startall(tree->numChildren, requests);
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_irecv_init(&((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_isend_init(&((char*)sendbuf)[i * sendextent], sendcount, sendtype, i, system_tag, comm);
220 /* Wait for them all.*/
221 smpi_mpi_startall(count, requests);
222 DEBUG2("<%d> wait for %d requests", rank, count);
223 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
230 * Alltoall basic_linear
232 int smpi_coll_tuned_alltoall_basic_linear(void *sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
233 int system_tag = 888;
234 int i, rank, size, err, count;
235 MPI_Aint lb, sendinc, recvinc;
236 MPI_Request *requests;
239 rank = smpi_comm_rank(comm);
240 size = smpi_comm_size(comm);
241 DEBUG1("<%d> algorithm alltoall_basic_linear() called.", rank);
242 err = smpi_datatype_extent(sendtype, &lb, &sendinc);
243 err = smpi_datatype_extent(recvtype, &lb, &recvinc);
244 sendinc *= sendcount;
245 recvinc *= recvcount;
246 /* simple optimization */
247 err = smpi_datatype_copy(&((char*)sendbuf)[rank * sendinc], sendcount, sendtype, &((char*)recvbuf)[rank * recvinc], recvcount, recvtype);
248 if(err == MPI_SUCCESS && size > 1) {
249 /* Initiate all send/recv to/from others. */
250 requests = xbt_new(MPI_Request, 2 * (size - 1));
251 /* Post all receives first -- a simple optimization */
253 for(i = (rank + 1) % size; i != rank; i = (i + 1) % size) {
254 requests[count] = smpi_irecv_init(&((char*)recvbuf)[i * recvinc], recvcount, recvtype, i, system_tag, comm);
257 /* Now post all sends in reverse order
258 * - We would like to minimize the search time through message queue
259 * when messages actually arrive in the order in which they were posted.
260 * TODO: check the previous assertion
262 for(i = (rank + size - 1) % size; i != rank; i = (i + size - 1) % size ) {
263 requests[count] = smpi_isend_init(&((char*)sendbuf)[i * sendinc], sendcount, sendtype, i, system_tag, comm);
266 /* Wait for them all.*/
267 smpi_mpi_startall(count, requests);
268 DEBUG2("<%d> wait for %d requests", rank, count);
269 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);
278 * this algorithm performs size steps (1<=s<=size) and
279 * at each step s, a process p sends iand receive to.from a unique distinct remote process
280 * size=5 : s=1: 4->0->1, 0->1->2, 1->2->3, ...
281 * s=2: 3->0->2, 4->1->3, 0->2->4, 1->3->0 , 2->4->1
283 * Openmpi calls this routine when the message size sent to each rank is greater than 3000 bytes
285 int smpi_coll_tuned_alltoall_pairwise(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
286 int system_tag = 999;
287 int rank, size, step, sendto, recvfrom, sendsize, recvsize;
289 rank = smpi_comm_rank(comm);
290 size = smpi_comm_size(comm);
291 DEBUG1("<%d> algorithm alltoall_pairwise() called.", rank);
292 sendsize = smpi_datatype_size(sendtype);
293 recvsize = smpi_datatype_size(recvtype);
294 /* Perform pairwise exchange - starting from 1 so the local copy is last */
295 for(step = 1; step < size + 1; step++) {
296 /* who do we talk to in this step? */
297 sendto = (rank+step)%size;
298 recvfrom = (rank+size-step)%size;
299 /* send and receive */
300 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);
305 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) {
306 int system_tag = 889;
307 int i, rank, size, err, count;
308 MPI_Aint lb, sendextent, recvextent;
309 MPI_Request* requests;
312 rank = smpi_comm_rank(comm);
313 size = smpi_comm_size(comm);
314 DEBUG1("<%d> algorithm basic_alltoallv() called.", rank);
315 err = smpi_datatype_extent(sendtype, &lb, &sendextent);
316 err = smpi_datatype_extent(recvtype, &lb, &recvextent);
317 /* Local copy from self */
318 err = smpi_datatype_copy(&((char*)sendbuf)[senddisps[rank] * sendextent], sendcounts[rank], sendtype, &((char*)recvbuf)[recvdisps[rank] * recvextent], recvcounts[rank], recvtype);
319 if(err == MPI_SUCCESS && size > 1) {
320 /* Initiate all send/recv to/from others. */
321 requests = xbt_new(MPI_Request, 2 * (size - 1));
323 /* Create all receives that will be posted first */
324 for(i = 0; i < size; ++i) {
325 if(i == rank || recvcounts[i] == 0) {
326 DEBUG3("<%d> skip request creation [src = %d, recvcounts[src] = %d]", rank, i, recvcounts[i]);
329 requests[count] = smpi_irecv_init(&((char*)recvbuf)[recvdisps[i] * recvextent], recvcounts[i], recvtype, i, system_tag, comm);
332 /* Now create all sends */
333 for(i = 0; i < size; ++i) {
334 if(i == rank || sendcounts[i] == 0) {
335 DEBUG3("<%d> skip request creation [dst = %d, sendcounts[dst] = %d]", rank, i, sendcounts[i]);
338 requests[count] = smpi_isend_init(&((char*)sendbuf)[senddisps[i] * sendextent], sendcounts[i], sendtype, i, system_tag, comm);
341 /* Wait for them all.*/
342 smpi_mpi_startall(count, requests);
343 DEBUG2("<%d> wait for %d requests", rank, count);
344 smpi_mpi_waitall(count, requests, MPI_STATUS_IGNORE);