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-2009 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.
19 * Additional copyrights may follow
22 #include "../colls_private.h"
23 #include "../coll_tuned_topo.h"
28 int smpi_coll_tuned_ompi_reduce_generic( void* sendbuf, void* recvbuf, int original_count,
29 MPI_Datatype datatype, MPI_Op op,
30 int root, MPI_Comm comm,
31 ompi_coll_tree_t* tree, int count_by_segment,
32 int max_outstanding_reqs );
34 * This is a generic implementation of the reduce protocol. It used the tree
35 * provided as an argument and execute all operations using a segment of
36 * count times a datatype.
37 * For the last communication it will update the count in order to limit
38 * the number of datatype to the original count (original_count)
40 * Note that for non-commutative operations we cannot save memory copy
41 * for the first block: thus we must copy sendbuf to accumbuf on intermediate
42 * to keep the optimized loop happy.
44 int smpi_coll_tuned_ompi_reduce_generic( void* sendbuf, void* recvbuf, int original_count,
45 MPI_Datatype datatype, MPI_Op op,
46 int root, MPI_Comm comm,
47 ompi_coll_tree_t* tree, int count_by_segment,
48 int max_outstanding_reqs )
50 char *inbuf[2] = {NULL, NULL}, *inbuf_free[2] = {NULL, NULL};
51 char *accumbuf = NULL, *accumbuf_free = NULL;
52 char *local_op_buffer = NULL, *sendtmpbuf = NULL;
53 ptrdiff_t extent, lower_bound, segment_increment;
54 MPI_Request reqs[2] = {MPI_REQUEST_NULL, MPI_REQUEST_NULL};
55 int num_segments, line, ret, segindex, i, rank;
56 int recvcount, prevcount, inbi;
59 * Determine number of segments and number of elements
62 datatype->extent(&lower_bound, &extent);
63 num_segments = (original_count + count_by_segment - 1) / count_by_segment;
64 segment_increment = count_by_segment * extent;
66 sendtmpbuf = (char*) sendbuf;
67 if( sendbuf == MPI_IN_PLACE ) {
68 sendtmpbuf = (char *)recvbuf;
71 XBT_DEBUG( "coll:tuned:reduce_generic count %d, msg size %ld, segsize %ld, max_requests %d", original_count, (unsigned long)(num_segments * segment_increment), (unsigned long)segment_increment, max_outstanding_reqs);
75 /* non-leaf nodes - wait for children to send me data & forward up
77 if( tree->tree_nextsize > 0 ) {
78 ptrdiff_t true_extent, real_segment_size;
79 true_extent=datatype->get_extent();
81 /* handle non existant recv buffer (i.e. its NULL) and
82 protect the recv buffer on non-root nodes */
83 accumbuf = (char*)recvbuf;
84 if( (NULL == accumbuf) || (root != rank) ) {
85 /* Allocate temporary accumulator buffer. */
86 accumbuf_free = (char*)smpi_get_tmp_sendbuffer(true_extent +
87 (original_count - 1) * extent);
88 if (accumbuf_free == NULL) {
89 line = __LINE__; ret = -1; goto error_hndl;
91 accumbuf = accumbuf_free - lower_bound;
94 /* If this is a non-commutative operation we must copy
95 sendbuf to the accumbuf, in order to simplfy the loops */
96 if ((op != MPI_OP_NULL && not op->is_commutative())) {
97 Datatype::copy((char*)sendtmpbuf, original_count, datatype, (char*)accumbuf, original_count, datatype);
99 /* Allocate two buffers for incoming segments */
100 real_segment_size = true_extent + (count_by_segment - 1) * extent;
101 inbuf_free[0] = (char*) smpi_get_tmp_recvbuffer(real_segment_size);
102 if( inbuf_free[0] == NULL ) {
103 line = __LINE__; ret = -1; goto error_hndl;
105 inbuf[0] = inbuf_free[0] - lower_bound;
106 /* if there is chance to overlap communication -
107 allocate second buffer */
108 if( (num_segments > 1) || (tree->tree_nextsize > 1) ) {
109 inbuf_free[1] = (char*) smpi_get_tmp_recvbuffer(real_segment_size);
110 if( inbuf_free[1] == NULL ) {
111 line = __LINE__; ret = -1; goto error_hndl;
113 inbuf[1] = inbuf_free[1] - lower_bound;
116 /* reset input buffer index and receive count */
119 /* for each segment */
120 for( segindex = 0; segindex <= num_segments; segindex++ ) {
121 prevcount = recvcount;
122 /* recvcount - number of elements in current segment */
123 recvcount = count_by_segment;
124 if( segindex == (num_segments-1) )
125 recvcount = original_count - count_by_segment * segindex;
128 for( i = 0; i < tree->tree_nextsize; i++ ) {
130 * We try to overlap communication:
131 * either with next segment or with the next child
133 /* post irecv for current segindex on current child */
134 if( segindex < num_segments ) {
135 void* local_recvbuf = inbuf[inbi];
137 /* for the first step (1st child per segment) and
138 * commutative operations we might be able to irecv
139 * directly into the accumulate buffer so that we can
140 * reduce(op) this with our sendbuf in one step as
141 * ompi_op_reduce only has two buffer pointers,
142 * this avoids an extra memory copy.
144 * BUT if the operation is non-commutative or
145 * we are root and are USING MPI_IN_PLACE this is wrong!
147 if( (op==MPI_OP_NULL || op->is_commutative()) &&
148 !((MPI_IN_PLACE == sendbuf) && (rank == tree->tree_root)) ) {
149 local_recvbuf = accumbuf + segindex * segment_increment;
153 reqs[inbi]=Request::irecv(local_recvbuf, recvcount, datatype,
155 COLL_TAG_REDUCE, comm
158 /* wait for previous req to complete, if any.
159 if there are no requests reqs[inbi ^1] will be
161 /* wait on data from last child for previous segment */
162 Request::waitall( 1, &reqs[inbi ^ 1],
163 MPI_STATUSES_IGNORE );
164 local_op_buffer = inbuf[inbi ^ 1];
166 /* our first operation is to combine our own [sendbuf] data
167 * with the data we recvd from down stream (but only
168 * the operation is commutative and if we are not root and
169 * not using MPI_IN_PLACE)
172 if( (op==MPI_OP_NULL || op->is_commutative())&&
173 !((MPI_IN_PLACE == sendbuf) && (rank == tree->tree_root)) ) {
174 local_op_buffer = sendtmpbuf + segindex * segment_increment;
177 /* apply operation */
178 if(op!=MPI_OP_NULL) op->apply( local_op_buffer,
179 accumbuf + segindex * segment_increment,
180 &recvcount, datatype );
181 } else if ( segindex > 0 ) {
182 void* accumulator = accumbuf + (segindex-1) * segment_increment;
183 if( tree->tree_nextsize <= 1 ) {
184 if( (op==MPI_OP_NULL || op->is_commutative()) &&
185 !((MPI_IN_PLACE == sendbuf) && (rank == tree->tree_root)) ) {
186 local_op_buffer = sendtmpbuf + (segindex-1) * segment_increment;
189 if(op!=MPI_OP_NULL) op->apply( local_op_buffer, accumulator, &prevcount,
192 /* all reduced on available data this step (i) complete,
193 * pass to the next process unless you are the root.
195 if (rank != tree->tree_root) {
196 /* send combined/accumulated data to parent */
197 Request::send( accumulator, prevcount,
198 datatype, tree->tree_prev,
203 /* we stop when segindex = number of segments
204 (i.e. we do num_segment+1 steps for pipelining */
205 if (segindex == num_segments) break;
208 /* update input buffer index */
210 } /* end of for each child */
211 } /* end of for each segment */
214 smpi_free_tmp_buffer(inbuf_free[0]);
215 smpi_free_tmp_buffer(inbuf_free[1]);
216 smpi_free_tmp_buffer(accumbuf_free);
220 Depending on the value of max_outstanding_reqs and
221 the number of segments we have two options:
222 - send all segments using blocking send to the parent, or
223 - avoid overflooding the parent nodes by limiting the number of
224 outstanding requests to max_oustanding_reqs.
225 TODO/POSSIBLE IMPROVEMENT: If there is a way to determine the eager size
226 for the current communication, synchronization should be used only
227 when the message/segment size is smaller than the eager size.
231 /* If the number of segments is less than a maximum number of oustanding
232 requests or there is no limit on the maximum number of outstanding
233 requests, we send data to the parent using blocking send */
234 if ((0 == max_outstanding_reqs) ||
235 (num_segments <= max_outstanding_reqs)) {
238 while ( original_count > 0) {
239 if (original_count < count_by_segment) {
240 count_by_segment = original_count;
242 Request::send((char*)sendbuf +
243 segindex * segment_increment,
244 count_by_segment, datatype,
249 original_count -= count_by_segment;
253 /* Otherwise, introduce flow control:
254 - post max_outstanding_reqs non-blocking synchronous send,
255 - for remaining segments
256 - wait for a ssend to complete, and post the next one.
257 - wait for all outstanding sends to complete.
262 MPI_Request* sreq = NULL;
264 sreq = (MPI_Request*) calloc( max_outstanding_reqs,
265 sizeof(MPI_Request ) );
266 if (NULL == sreq) { line = __LINE__; ret = -1; goto error_hndl; }
268 /* post first group of requests */
269 for (segindex = 0; segindex < max_outstanding_reqs; segindex++) {
270 sreq[segindex]=Request::isend((char*)sendbuf +
271 segindex * segment_increment,
272 count_by_segment, datatype,
276 original_count -= count_by_segment;
280 while ( original_count > 0 ) {
281 /* wait on a posted request to complete */
282 Request::wait(&sreq[creq], MPI_STATUS_IGNORE);
283 sreq[creq] = MPI_REQUEST_NULL;
285 if( original_count < count_by_segment ) {
286 count_by_segment = original_count;
288 sreq[creq]=Request::isend((char*)sendbuf +
289 segindex * segment_increment,
290 count_by_segment, datatype,
294 creq = (creq + 1) % max_outstanding_reqs;
296 original_count -= count_by_segment;
299 /* Wait on the remaining request to complete */
300 Request::waitall( max_outstanding_reqs, sreq,
301 MPI_STATUSES_IGNORE );
310 error_hndl: /* error handler */
311 XBT_DEBUG("ERROR_HNDL: node %d file %s line %d error %d\n",
312 rank, __FILE__, line, ret );
313 if( inbuf_free[0] != NULL ) free(inbuf_free[0]);
314 if( inbuf_free[1] != NULL ) free(inbuf_free[1]);
315 if( accumbuf_free != NULL ) free(accumbuf);
319 /* Attention: this version of the reduce operations does not
321 - non-commutative operations
322 - segment sizes which are not multiplies of the extent of the datatype
323 meaning that at least one datatype must fit in the segment !
327 int Coll_reduce_ompi_chain::reduce( void *sendbuf, void *recvbuf, int count,
328 MPI_Datatype datatype,
333 uint32_t segsize=64*1024;
334 int segcount = count;
336 int fanout = comm->size()/2;
338 XBT_DEBUG("coll:tuned:reduce_intra_chain rank %d fo %d ss %5d", comm->rank(), fanout, segsize);
341 * Determine number of segments and number of elements
344 typelng = datatype->size();
346 COLL_TUNED_COMPUTED_SEGCOUNT( segsize, typelng, segcount );
348 return smpi_coll_tuned_ompi_reduce_generic( sendbuf, recvbuf, count, datatype,
350 ompi_coll_tuned_topo_build_chain(fanout, comm, root),
355 int Coll_reduce_ompi_pipeline::reduce( void *sendbuf, void *recvbuf,
356 int count, MPI_Datatype datatype,
362 int segcount = count;
364 // COLL_TUNED_UPDATE_PIPELINE( comm, tuned_module, root );
367 * Determine number of segments and number of elements
370 const double a2 = 0.0410 / 1024.0; /* [1/B] */
371 const double b2 = 9.7128;
372 const double a4 = 0.0033 / 1024.0; /* [1/B] */
373 const double b4 = 1.6761;
374 typelng= datatype->size();
375 int communicator_size = comm->size();
376 size_t message_size = typelng * count;
378 if (communicator_size > (a2 * message_size + b2)) {
381 }else if (communicator_size > (a4 * message_size + b4)) {
389 XBT_DEBUG("coll:tuned:reduce_intra_pipeline rank %d ss %5d",
390 comm->rank(), segsize);
392 COLL_TUNED_COMPUTED_SEGCOUNT( segsize, typelng, segcount );
394 return smpi_coll_tuned_ompi_reduce_generic( sendbuf, recvbuf, count, datatype,
396 ompi_coll_tuned_topo_build_chain( 1, comm, root),
400 int Coll_reduce_ompi_binary::reduce( void *sendbuf, void *recvbuf,
401 int count, MPI_Datatype datatype,
406 int segcount = count;
412 * Determine number of segments and number of elements
415 typelng=datatype->size();
420 XBT_DEBUG("coll:tuned:reduce_intra_binary rank %d ss %5d",
421 comm->rank(), segsize);
423 COLL_TUNED_COMPUTED_SEGCOUNT( segsize, typelng, segcount );
425 return smpi_coll_tuned_ompi_reduce_generic( sendbuf, recvbuf, count, datatype,
427 ompi_coll_tuned_topo_build_tree(2, comm, root),
431 int Coll_reduce_ompi_binomial::reduce( void *sendbuf, void *recvbuf,
432 int count, MPI_Datatype datatype,
438 int segcount = count;
441 const double a1 = 0.6016 / 1024.0; /* [1/B] */
442 const double b1 = 1.3496;
444 // COLL_TUNED_UPDATE_IN_ORDER_BMTREE( comm, tuned_module, root );
447 * Determine number of segments and number of elements
450 typelng= datatype->size();
451 int communicator_size = comm->size();
452 size_t message_size = typelng * count;
453 if (((communicator_size < 8) && (message_size < 20480)) ||
454 (message_size < 2048) || (count <= 1)) {
457 } else if (communicator_size > (a1 * message_size + b1)) {
462 XBT_DEBUG("coll:tuned:reduce_intra_binomial rank %d ss %5d",
463 comm->rank(), segsize);
464 COLL_TUNED_COMPUTED_SEGCOUNT( segsize, typelng, segcount );
466 return smpi_coll_tuned_ompi_reduce_generic( sendbuf, recvbuf, count, datatype,
468 ompi_coll_tuned_topo_build_in_order_bmtree(comm, root),
473 * reduce_intra_in_order_binary
475 * Function: Logarithmic reduce operation for non-commutative operations.
476 * Acecpts: same as MPI_Reduce()
477 * Returns: MPI_SUCCESS or error code
479 int Coll_reduce_ompi_in_order_binary::reduce( void *sendbuf, void *recvbuf,
481 MPI_Datatype datatype,
487 int rank, size, io_root;
488 int segcount = count;
489 void *use_this_sendbuf = NULL, *use_this_recvbuf = NULL;
494 XBT_DEBUG("coll:tuned:reduce_intra_in_order_binary rank %d ss %5d",
498 * Determine number of segments and number of elements
501 typelng=datatype->size();
502 COLL_TUNED_COMPUTED_SEGCOUNT( segsize, typelng, segcount );
504 /* An in-order binary tree must use root (size-1) to preserve the order of
505 operations. Thus, if root is not rank (size - 1), then we must handle
506 1. MPI_IN_PLACE option on real root, and
507 2. we must allocate temporary recvbuf on rank (size - 1).
508 Note that generic function must be careful not to switch order of
509 operations for non-commutative ops.
512 use_this_sendbuf = sendbuf;
513 use_this_recvbuf = recvbuf;
514 if (io_root != root) {
518 ext=datatype->get_extent();
519 text=datatype->get_extent();
521 if ((root == rank) && (MPI_IN_PLACE == sendbuf)) {
522 tmpbuf = (char *) smpi_get_tmp_sendbuffer(text + (count - 1) * ext);
523 if (NULL == tmpbuf) {
524 return MPI_ERR_INTERN;
527 (char*)recvbuf, count, datatype,
528 (char*)tmpbuf, count, datatype);
529 use_this_sendbuf = tmpbuf;
530 } else if (io_root == rank) {
531 tmpbuf = (char *) smpi_get_tmp_recvbuffer(text + (count - 1) * ext);
532 if (NULL == tmpbuf) {
533 return MPI_ERR_INTERN;
535 use_this_recvbuf = tmpbuf;
539 /* Use generic reduce with in-order binary tree topology and io_root */
540 ret = smpi_coll_tuned_ompi_reduce_generic( use_this_sendbuf, use_this_recvbuf, count, datatype,
542 ompi_coll_tuned_topo_build_in_order_bintree(comm),
544 if (MPI_SUCCESS != ret) { return ret; }
547 if (io_root != root) {
549 /* Receive result from rank io_root to recvbuf */
550 Request::recv(recvbuf, count, datatype, io_root,
551 COLL_TAG_REDUCE, comm,
553 if (MPI_IN_PLACE == sendbuf) {
554 smpi_free_tmp_buffer(use_this_sendbuf);
557 } else if (io_root == rank) {
558 /* Send result from use_this_recvbuf to root */
559 Request::send(use_this_recvbuf, count, datatype, root,
562 smpi_free_tmp_buffer(use_this_recvbuf);
570 * Linear functions are copied from the BASIC coll module
571 * they do not segment the message and are simple implementations
572 * but for some small number of nodes and/or small data sizes they
573 * are just as fast as tuned/tree based segmenting operations
574 * and as such may be selected by the decision functions
575 * These are copied into this module due to the way we select modules
576 * in V1. i.e. in V2 we will handle this differently and so will not
577 * have to duplicate code.
578 * GEF Oct05 after asking Jeff.
581 /* copied function (with appropriate renaming) starts here */
586 * Function: - reduction using O(N) algorithm
587 * Accepts: - same as MPI_Reduce()
588 * Returns: - MPI_SUCCESS or error code
592 Coll_reduce_ompi_basic_linear::reduce(void *sbuf, void *rbuf, int count,
599 ptrdiff_t true_extent, lb, extent;
600 char *free_buffer = NULL;
601 char *pml_buffer = NULL;
602 char *inplace_temp = NULL;
610 XBT_DEBUG("coll:tuned:reduce_intra_basic_linear rank %d", rank);
612 /* If not root, send data to the root. */
615 Request::send(sbuf, count, dtype, root,
621 /* see discussion in ompi_coll_basic_reduce_lin_intra about
622 extent and true extent */
623 /* for reducing buffer allocation lengths.... */
625 dtype->extent(&lb, &extent);
626 true_extent = dtype->get_extent();
628 if (MPI_IN_PLACE == sbuf) {
630 inplace_temp = (char*)smpi_get_tmp_recvbuffer(true_extent + (count - 1) * extent);
631 if (NULL == inplace_temp) {
634 rbuf = inplace_temp - lb;
638 free_buffer = (char*)smpi_get_tmp_recvbuffer(true_extent + (count - 1) * extent);
639 pml_buffer = free_buffer - lb;
642 /* Initialize the receive buffer. */
644 if (rank == (size - 1)) {
645 Datatype::copy((char*)sbuf, count, dtype,(char*)rbuf, count, dtype);
647 Request::recv(rbuf, count, dtype, size - 1,
648 COLL_TAG_REDUCE, comm,
652 /* Loop receiving and calling reduction function (C or Fortran). */
654 for (i = size - 2; i >= 0; --i) {
658 Request::recv(pml_buffer, count, dtype, i,
659 COLL_TAG_REDUCE, comm,
664 /* Perform the reduction */
665 if(op!=MPI_OP_NULL) op->apply( inbuf, rbuf, &count, dtype);
668 if (NULL != inplace_temp) {
669 Datatype::copy(inplace_temp, count, dtype,(char*)sbuf
671 smpi_free_tmp_buffer(inplace_temp);
673 if (NULL != free_buffer) {
674 smpi_free_tmp_buffer(free_buffer);
681 /* copied function (with appropriate renaming) ends here */