1 /* Copyright (c) 2013-2017. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
9 * University Research and Technology
10 * Corporation. All rights reserved.
11 * Copyright (c) 2004-2006 The University of Tennessee and The University
12 * of Tennessee Research Foundation. All rights
14 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
15 * University of Stuttgart. All rights reserved.
16 * Copyright (c) 2004-2005 The Regents of the University of California.
17 * All rights reserved.
18 * Copyright (c) 2008 Sun Microsystems, Inc. All rights reserved.
20 * Additional copyrights may follow
23 #include "../colls_private.h"
24 #include "../coll_tuned_topo.h"
28 * Barrier is ment to be a synchronous operation, as some BTLs can mark
29 * a request done before its passed to the NIC and progress might not be made
30 * elsewhere we cannot allow a process to exit the barrier until its last
31 * [round of] sends are completed.
33 * It is last round of sends rather than 'last' individual send as each pair of
34 * peers can use different channels/devices/btls and the receiver of one of
35 * these sends might be forced to wait as the sender
36 * leaves the collective and does not make progress until the next mpi call
41 * Simple double ring version of barrier
43 * synchronous gurantee made by last ring of sends are synchronous
48 int Coll_barrier_ompi_doublering::barrier(MPI_Comm comm
58 XBT_DEBUG("ompi_coll_tuned_barrier_ompi_doublering rank %d", rank);
60 left = ((rank-1+size)%size);
61 right = ((rank+1)%size);
63 if (rank > 0) { /* receive message from the left */
64 Request::recv((void*)NULL, 0, MPI_BYTE, left,
65 COLL_TAG_BARRIER, comm,
69 /* Send message to the right */
70 Request::send((void*)NULL, 0, MPI_BYTE, right,
74 /* root needs to receive from the last node */
76 Request::recv((void*)NULL, 0, MPI_BYTE, left,
77 COLL_TAG_BARRIER, comm,
81 /* Allow nodes to exit */
82 if (rank > 0) { /* post Receive from left */
83 Request::recv((void*)NULL, 0, MPI_BYTE, left,
84 COLL_TAG_BARRIER, comm,
88 /* send message to the right one */
89 Request::send((void*)NULL, 0, MPI_BYTE, right,
93 /* rank 0 post receive from the last node */
95 Request::recv((void*)NULL, 0, MPI_BYTE, left,
96 COLL_TAG_BARRIER, comm,
106 * To make synchronous, uses sync sends and sync sendrecvs
109 int Coll_barrier_ompi_recursivedoubling::barrier(MPI_Comm comm
112 int rank, size, adjsize;
118 "ompi_coll_tuned_barrier_ompi_recursivedoubling rank %d",
121 /* do nearest power of 2 less than size calc */
122 for( adjsize = 1; adjsize <= size; adjsize <<= 1 );
125 /* if size is not exact power of two, perform an extra step */
126 if (adjsize != size) {
127 if (rank >= adjsize) {
128 /* send message to lower ranked node */
129 remote = rank - adjsize;
130 Request::sendrecv(NULL, 0, MPI_BYTE, remote,
132 NULL, 0, MPI_BYTE, remote,
134 comm, MPI_STATUS_IGNORE);
136 } else if (rank < (size - adjsize)) {
138 /* receive message from high level rank */
139 Request::recv((void*)NULL, 0, MPI_BYTE, rank+adjsize,
140 COLL_TAG_BARRIER, comm,
146 /* exchange messages */
147 if ( rank < adjsize ) {
149 while ( mask < adjsize ) {
150 remote = rank ^ mask;
152 if (remote >= adjsize) continue;
154 /* post receive from the remote node */
155 Request::sendrecv(NULL, 0, MPI_BYTE, remote,
157 NULL, 0, MPI_BYTE, remote,
159 comm, MPI_STATUS_IGNORE);
163 /* non-power of 2 case */
164 if (adjsize != size) {
165 if (rank < (size - adjsize)) {
166 /* send enter message to higher ranked node */
167 remote = rank + adjsize;
168 Request::send((void*)NULL, 0, MPI_BYTE, remote,
181 * To make synchronous, uses sync sends and sync sendrecvs
184 int Coll_barrier_ompi_bruck::barrier(MPI_Comm comm
188 int distance, to, from;
193 "ompi_coll_tuned_barrier_ompi_bruck rank %d", rank);
195 /* exchange data with rank-2^k and rank+2^k */
196 for (distance = 1; distance < size; distance <<= 1) {
197 from = (rank + size - distance) % size;
198 to = (rank + distance) % size;
200 /* send message to lower ranked node */
201 Request::sendrecv(NULL, 0, MPI_BYTE, to,
203 NULL, 0, MPI_BYTE, from,
205 comm, MPI_STATUS_IGNORE);
214 * To make synchronous, uses sync sends and sync sendrecvs
216 /* special case for two processes */
217 int Coll_barrier_ompi_two_procs::barrier(MPI_Comm comm
222 remote = comm->rank();
224 "ompi_coll_tuned_barrier_ompi_two_procs rank %d", remote);
225 remote = (remote + 1) & 0x1;
227 Request::sendrecv(NULL, 0, MPI_BYTE, remote,
229 NULL, 0, MPI_BYTE, remote,
231 comm, MPI_STATUS_IGNORE);
232 return (MPI_SUCCESS);
237 * Linear functions are copied from the BASIC coll module
238 * they do not segment the message and are simple implementations
239 * but for some small number of nodes and/or small data sizes they
240 * are just as fast as tuned/tree based segmenting operations
241 * and as such may be selected by the decision functions
242 * These are copied into this module due to the way we select modules
243 * in V1. i.e. in V2 we will handle this differently and so will not
244 * have to duplicate code.
245 * GEF Oct05 after asking Jeff.
248 /* copied function (with appropriate renaming) starts here */
250 int Coll_barrier_ompi_basic_linear::barrier(MPI_Comm comm)
253 int size = comm->size();
254 int rank = comm->rank();
256 /* All non-root send & receive zero-length message. */
259 Request::send (NULL, 0, MPI_BYTE, 0,
263 Request::recv (NULL, 0, MPI_BYTE, 0,
265 comm, MPI_STATUS_IGNORE);
268 /* The root collects and broadcasts the messages. */
271 MPI_Request* requests;
273 requests = (MPI_Request*)malloc( size * sizeof(MPI_Request) );
274 for (i = 1; i < size; ++i) {
275 requests[i] = Request::irecv(NULL, 0, MPI_BYTE, MPI_ANY_SOURCE,
276 COLL_TAG_BARRIER, comm
279 Request::waitall( size-1, requests+1, MPI_STATUSES_IGNORE );
281 for (i = 1; i < size; ++i) {
282 requests[i] = Request::isend(NULL, 0, MPI_BYTE, i,
287 Request::waitall( size-1, requests+1, MPI_STATUSES_IGNORE );
296 /* copied function (with appropriate renaming) ends here */
299 * Another recursive doubling type algorithm, but in this case
300 * we go up the tree and back down the tree.
302 int Coll_barrier_ompi_tree::barrier(MPI_Comm comm)
304 int rank, size, depth;
310 "ompi_coll_tuned_barrier_ompi_tree %d",
313 /* Find the nearest power of 2 of the communicator size. */
314 for(depth = 1; depth < size; depth <<= 1 );
316 for (jump=1; jump<depth; jump<<=1) {
317 partner = rank ^ jump;
318 if (!(partner & (jump-1)) && partner < size) {
319 if (partner > rank) {
320 Request::recv (NULL, 0, MPI_BYTE, partner,
321 COLL_TAG_BARRIER, comm,
323 } else if (partner < rank) {
324 Request::send (NULL, 0, MPI_BYTE, partner,
332 for (jump = depth; jump>0; jump>>=1) {
333 partner = rank ^ jump;
334 if (!(partner & (jump-1)) && partner < size) {
335 if (partner > rank) {
336 Request::send (NULL, 0, MPI_BYTE, partner,
339 } else if (partner < rank) {
340 Request::recv (NULL, 0, MPI_BYTE, partner,
341 COLL_TAG_BARRIER, comm,