1 /* Copyright (c) 2013-2014. 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"
27 int smpi_coll_tuned_ompi_reduce_generic( void* sendbuf, void* recvbuf, int original_count,
28 MPI_Datatype datatype, MPI_Op op,
29 int root, MPI_Comm comm,
30 ompi_coll_tree_t* tree, int count_by_segment,
31 int max_outstanding_reqs );
33 * This is a generic implementation of the reduce protocol. It used the tree
34 * provided as an argument and execute all operations using a segment of
35 * count times a datatype.
36 * For the last communication it will update the count in order to limit
37 * the number of datatype to the original count (original_count)
39 * Note that for non-commutative operations we cannot save memory copy
40 * for the first block: thus we must copy sendbuf to accumbuf on intermediate
41 * to keep the optimized loop happy.
43 int smpi_coll_tuned_ompi_reduce_generic( void* sendbuf, void* recvbuf, int original_count,
44 MPI_Datatype datatype, MPI_Op op,
45 int root, MPI_Comm comm,
46 ompi_coll_tree_t* tree, int count_by_segment,
47 int max_outstanding_reqs )
49 char *inbuf[2] = {NULL, NULL}, *inbuf_free[2] = {NULL, NULL};
50 char *accumbuf = NULL, *accumbuf_free = NULL;
51 char *local_op_buffer = NULL, *sendtmpbuf = NULL;
52 ptrdiff_t extent, lower_bound, segment_increment;
53 MPI_Request reqs[2] = {MPI_REQUEST_NULL, MPI_REQUEST_NULL};
54 int num_segments, line, ret, segindex, i, rank;
55 int recvcount, prevcount, inbi;
58 * Determine number of segments and number of elements
61 smpi_datatype_extent( datatype, &lower_bound, &extent);
62 num_segments = (original_count + count_by_segment - 1) / count_by_segment;
63 segment_increment = count_by_segment * extent;
65 sendtmpbuf = (char*) sendbuf;
66 if( sendbuf == MPI_IN_PLACE ) {
67 sendtmpbuf = (char *)recvbuf;
70 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);
72 rank = smpi_comm_rank(comm);
74 /* non-leaf nodes - wait for children to send me data & forward up
76 if( tree->tree_nextsize > 0 ) {
77 ptrdiff_t true_extent, real_segment_size;
78 true_extent=smpi_datatype_get_extent( datatype);
80 /* handle non existant recv buffer (i.e. its NULL) and
81 protect the recv buffer on non-root nodes */
82 accumbuf = (char*)recvbuf;
83 if( (NULL == accumbuf) || (root != rank) ) {
84 /* Allocate temporary accumulator buffer. */
85 accumbuf_free = (char*)malloc(true_extent +
86 (original_count - 1) * extent);
87 if (accumbuf_free == NULL) {
88 line = __LINE__; ret = -1; goto error_hndl;
90 accumbuf = accumbuf_free - lower_bound;
93 /* If this is a non-commutative operation we must copy
94 sendbuf to the accumbuf, in order to simplfy the loops */
95 if (!smpi_op_is_commute(op)) {
97 (char*)sendtmpbuf, original_count, datatype,
98 (char*)accumbuf, original_count, datatype);
100 /* Allocate two buffers for incoming segments */
101 real_segment_size = true_extent + (count_by_segment - 1) * extent;
102 inbuf_free[0] = (char*) malloc(real_segment_size);
103 if( inbuf_free[0] == NULL ) {
104 line = __LINE__; ret = -1; goto error_hndl;
106 inbuf[0] = inbuf_free[0] - lower_bound;
107 /* if there is chance to overlap communication -
108 allocate second buffer */
109 if( (num_segments > 1) || (tree->tree_nextsize > 1) ) {
110 inbuf_free[1] = (char*) malloc(real_segment_size);
111 if( inbuf_free[1] == NULL ) {
112 line = __LINE__; ret = -1; goto error_hndl;
114 inbuf[1] = inbuf_free[1] - lower_bound;
117 /* reset input buffer index and receive count */
120 /* for each segment */
121 for( segindex = 0; segindex <= num_segments; segindex++ ) {
122 prevcount = recvcount;
123 /* recvcount - number of elements in current segment */
124 recvcount = count_by_segment;
125 if( segindex == (num_segments-1) )
126 recvcount = original_count - count_by_segment * segindex;
129 for( i = 0; i < tree->tree_nextsize; i++ ) {
131 * We try to overlap communication:
132 * either with next segment or with the next child
134 /* post irecv for current segindex on current child */
135 if( segindex < num_segments ) {
136 void* local_recvbuf = inbuf[inbi];
138 /* for the first step (1st child per segment) and
139 * commutative operations we might be able to irecv
140 * directly into the accumulate buffer so that we can
141 * reduce(op) this with our sendbuf in one step as
142 * ompi_op_reduce only has two buffer pointers,
143 * this avoids an extra memory copy.
145 * BUT if the operation is non-commutative or
146 * we are root and are USING MPI_IN_PLACE this is wrong!
148 if( (smpi_op_is_commute(op)) &&
149 !((MPI_IN_PLACE == sendbuf) && (rank == tree->tree_root)) ) {
150 local_recvbuf = accumbuf + segindex * segment_increment;
154 reqs[inbi]=smpi_mpi_irecv(local_recvbuf, recvcount, datatype,
156 COLL_TAG_REDUCE, comm
159 /* wait for previous req to complete, if any.
160 if there are no requests reqs[inbi ^1] will be
162 /* wait on data from last child for previous segment */
163 smpi_mpi_waitall( 1, &reqs[inbi ^ 1],
164 MPI_STATUSES_IGNORE );
165 local_op_buffer = inbuf[inbi ^ 1];
167 /* our first operation is to combine our own [sendbuf] data
168 * with the data we recvd from down stream (but only
169 * the operation is commutative and if we are not root and
170 * not using MPI_IN_PLACE)
173 if( (smpi_op_is_commute(op)) &&
174 !((MPI_IN_PLACE == sendbuf) && (rank == tree->tree_root)) ) {
175 local_op_buffer = sendtmpbuf + segindex * segment_increment;
178 /* apply operation */
179 smpi_op_apply(op, local_op_buffer,
180 accumbuf + segindex * segment_increment,
181 &recvcount, &datatype );
182 } else if ( segindex > 0 ) {
183 void* accumulator = accumbuf + (segindex-1) * segment_increment;
184 if( tree->tree_nextsize <= 1 ) {
185 if( (smpi_op_is_commute(op)) &&
186 !((MPI_IN_PLACE == sendbuf) && (rank == tree->tree_root)) ) {
187 local_op_buffer = sendtmpbuf + (segindex-1) * segment_increment;
190 smpi_op_apply(op, local_op_buffer, accumulator, &prevcount,
193 /* all reduced on available data this step (i) complete,
194 * pass to the next process unless you are the root.
196 if (rank != tree->tree_root) {
197 /* send combined/accumulated data to parent */
198 smpi_mpi_send( accumulator, prevcount,
199 datatype, tree->tree_prev,
204 /* we stop when segindex = number of segments
205 (i.e. we do num_segment+1 steps for pipelining */
206 if (segindex == num_segments) break;
209 /* update input buffer index */
211 } /* end of for each child */
212 } /* end of for each segment */
215 if( inbuf_free[0] != NULL) free(inbuf_free[0]);
216 if( inbuf_free[1] != NULL) free(inbuf_free[1]);
217 if( accumbuf_free != NULL ) free(accumbuf_free);
221 Depending on the value of max_outstanding_reqs and
222 the number of segments we have two options:
223 - send all segments using blocking send to the parent, or
224 - avoid overflooding the parent nodes by limiting the number of
225 outstanding requests to max_oustanding_reqs.
226 TODO/POSSIBLE IMPROVEMENT: If there is a way to determine the eager size
227 for the current communication, synchronization should be used only
228 when the message/segment size is smaller than the eager size.
232 /* If the number of segments is less than a maximum number of oustanding
233 requests or there is no limit on the maximum number of outstanding
234 requests, we send data to the parent using blocking send */
235 if ((0 == max_outstanding_reqs) ||
236 (num_segments <= max_outstanding_reqs)) {
239 while ( original_count > 0) {
240 if (original_count < count_by_segment) {
241 count_by_segment = original_count;
243 smpi_mpi_send((char*)sendbuf +
244 segindex * segment_increment,
245 count_by_segment, datatype,
250 original_count -= count_by_segment;
254 /* Otherwise, introduce flow control:
255 - post max_outstanding_reqs non-blocking synchronous send,
256 - for remaining segments
257 - wait for a ssend to complete, and post the next one.
258 - wait for all outstanding sends to complete.
263 MPI_Request* sreq = NULL;
265 sreq = (MPI_Request*) calloc( max_outstanding_reqs,
266 sizeof(MPI_Request ) );
267 if (NULL == sreq) { line = __LINE__; ret = -1; goto error_hndl; }
269 /* post first group of requests */
270 for (segindex = 0; segindex < max_outstanding_reqs; segindex++) {
271 sreq[segindex]=smpi_mpi_isend((char*)sendbuf +
272 segindex * segment_increment,
273 count_by_segment, datatype,
277 original_count -= count_by_segment;
281 while ( original_count > 0 ) {
282 /* wait on a posted request to complete */
283 smpi_mpi_wait(&sreq[creq], MPI_STATUS_IGNORE);
284 sreq[creq] = MPI_REQUEST_NULL;
286 if( original_count < count_by_segment ) {
287 count_by_segment = original_count;
289 sreq[creq]=smpi_mpi_isend((char*)sendbuf +
290 segindex * segment_increment,
291 count_by_segment, datatype,
295 creq = (creq + 1) % max_outstanding_reqs;
297 original_count -= count_by_segment;
300 /* Wait on the remaining request to complete */
301 smpi_mpi_waitall( max_outstanding_reqs, sreq,
302 MPI_STATUSES_IGNORE );
311 error_hndl: /* error handler */
312 XBT_DEBUG("ERROR_HNDL: node %d file %s line %d error %d\n",
313 rank, __FILE__, line, ret );
314 if( inbuf_free[0] != NULL ) free(inbuf_free[0]);
315 if( inbuf_free[1] != NULL ) free(inbuf_free[1]);
316 if( accumbuf_free != NULL ) free(accumbuf);
320 /* Attention: this version of the reduce operations does not
322 - non-commutative operations
323 - segment sizes which are not multiplies of the extent of the datatype
324 meaning that at least one datatype must fit in the segment !
327 int smpi_coll_tuned_reduce_ompi_chain( void *sendbuf, void *recvbuf, int count,
328 MPI_Datatype datatype,
333 uint32_t segsize=64*1024;
334 int segcount = count;
336 int fanout = smpi_comm_size(comm)/2;
338 XBT_DEBUG("coll:tuned:reduce_intra_chain rank %d fo %d ss %5d", smpi_comm_rank(comm), fanout, segsize);
341 * Determine number of segments and number of elements
344 typelng = smpi_datatype_size( datatype);
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 smpi_coll_tuned_reduce_ompi_pipeline( 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= smpi_datatype_size( datatype);
375 int communicator_size = smpi_comm_size(comm);
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 smpi_comm_rank(comm), 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 smpi_coll_tuned_reduce_ompi_binary( 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=smpi_datatype_size( datatype );
420 XBT_DEBUG("coll:tuned:reduce_intra_binary rank %d ss %5d",
421 smpi_comm_rank(comm), 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 smpi_coll_tuned_reduce_ompi_binomial( 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= smpi_datatype_size( datatype);
451 int communicator_size = smpi_comm_size(comm);
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 smpi_comm_rank(comm), 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 smpi_coll_tuned_reduce_ompi_in_order_binary( 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;
492 rank = smpi_comm_rank(comm);
493 size = smpi_comm_size(comm);
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=smpi_datatype_size( datatype);
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=smpi_datatype_get_extent(datatype);
519 text=smpi_datatype_get_extent(datatype);
521 if ((root == rank) && (MPI_IN_PLACE == sendbuf)) {
522 tmpbuf = (char *) malloc(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 *) malloc(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 smpi_mpi_recv(recvbuf, count, datatype, io_root,
551 COLL_TAG_REDUCE, comm,
553 if (MPI_IN_PLACE == sendbuf) {
554 free(use_this_sendbuf);
557 } else if (io_root == rank) {
558 /* Send result from use_this_recvbuf to root */
559 smpi_mpi_send(use_this_recvbuf, count, datatype, root,
562 free(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 smpi_coll_tuned_reduce_ompi_basic_linear(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;
607 rank = smpi_comm_rank(comm);
608 size = smpi_comm_size(comm);
610 XBT_DEBUG("coll:tuned:reduce_intra_basic_linear rank %d", rank);
612 /* If not root, send data to the root. */
615 smpi_mpi_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 smpi_datatype_extent(dtype, &lb, &extent);
626 true_extent = smpi_datatype_get_extent(dtype);
628 if (MPI_IN_PLACE == sbuf) {
630 inplace_temp = (char*)malloc(true_extent + (count - 1) * extent);
631 if (NULL == inplace_temp) {
634 rbuf = inplace_temp - lb;
638 free_buffer = (char*)malloc(true_extent + (count - 1) * extent);
639 pml_buffer = free_buffer - lb;
642 /* Initialize the receive buffer. */
644 if (rank == (size - 1)) {
645 smpi_datatype_copy((char*)sbuf, count, dtype,(char*)rbuf, count, dtype);
647 smpi_mpi_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 smpi_mpi_recv(pml_buffer, count, dtype, i,
659 COLL_TAG_REDUCE, comm,
664 /* Perform the reduction */
665 smpi_op_apply(op, inbuf, rbuf, &count, &dtype);
668 if (NULL != inplace_temp) {
669 smpi_datatype_copy(inplace_temp, count, dtype,(char*)sbuf
673 if (NULL != free_buffer) {
681 /* copied function (with appropriate renaming) ends here */