Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / smpi / colls / reduce-ompi.c
1 /* Copyright (c) 2013-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /*
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
13  *                         reserved.
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  *
19  * Additional copyrights may follow
20  */
21
22 #include "colls_private.h"
23 #include "coll_tuned_topo.h"
24
25
26
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 );
32 /**
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)
38  *
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.
42  */
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 )
48 {
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;
56
57     /**
58      * Determine number of segments and number of elements
59      * sent per operation
60      */
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;
64
65     sendtmpbuf = (char*) sendbuf; 
66     if( sendbuf == MPI_IN_PLACE ) { 
67         sendtmpbuf = (char *)recvbuf; 
68     }
69
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);
71
72     rank = smpi_comm_rank(comm);
73
74     /* non-leaf nodes - wait for children to send me data & forward up 
75        (if needed) */
76     if( tree->tree_nextsize > 0 ) {
77         ptrdiff_t true_extent, real_segment_size;
78         true_extent=smpi_datatype_get_extent( datatype);
79
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*)smpi_get_tmp_sendbuffer(true_extent +
86                                           (original_count - 1) * extent);
87             if (accumbuf_free == NULL) { 
88                 line = __LINE__; ret = -1; goto error_hndl; 
89             }
90             accumbuf = accumbuf_free - lower_bound;
91         } 
92
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)) {
96             smpi_datatype_copy(
97                                                 (char*)sendtmpbuf, original_count, datatype,
98                                                 (char*)accumbuf, original_count, datatype);
99         }
100         /* Allocate two buffers for incoming segments */
101         real_segment_size = true_extent + (count_by_segment - 1) * extent;
102         inbuf_free[0] = (char*) smpi_get_tmp_recvbuffer(real_segment_size);
103         if( inbuf_free[0] == NULL ) { 
104             line = __LINE__; ret = -1; goto error_hndl; 
105         }
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*) smpi_get_tmp_recvbuffer(real_segment_size);
111             if( inbuf_free[1] == NULL ) { 
112                 line = __LINE__; ret = -1; goto error_hndl;
113             }
114             inbuf[1] = inbuf_free[1] - lower_bound;
115         } 
116
117         /* reset input buffer index and receive count */
118         inbi = 0;
119         recvcount = 0;
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;
127
128             /* for each child */
129             for( i = 0; i < tree->tree_nextsize; i++ ) {
130                 /**
131                  * We try to overlap communication:
132                  * either with next segment or with the next child
133                  */
134                 /* post irecv for current segindex on current child */
135                 if( segindex < num_segments ) {
136                     void* local_recvbuf = inbuf[inbi];
137                     if( 0 == i ) {
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.
144                          *
145                          * BUT if the operation is non-commutative or 
146                          * we are root and are USING MPI_IN_PLACE this is wrong!
147                          */
148                         if( (smpi_op_is_commute(op)) &&
149                             !((MPI_IN_PLACE == sendbuf) && (rank == tree->tree_root)) ) {
150                             local_recvbuf = accumbuf + segindex * segment_increment;
151                         }
152                     }
153
154                     reqs[inbi]=smpi_mpi_irecv(local_recvbuf, recvcount, datatype,
155                                              tree->tree_next[i], 
156                                              COLL_TAG_REDUCE, comm
157                                              );
158                 }
159                 /* wait for previous req to complete, if any.
160                    if there are no requests reqs[inbi ^1] will be 
161                    MPI_REQUEST_NULL. */
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];
166                 if( i > 0 ) {
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)
171                      */
172                     if( 1 == i ) {
173                         if( (smpi_op_is_commute(op)) && 
174                             !((MPI_IN_PLACE == sendbuf) && (rank == tree->tree_root)) ) {
175                             local_op_buffer = sendtmpbuf + segindex * segment_increment;
176                         }
177                     }
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;
188                         }
189                     }
190                     smpi_op_apply(op, local_op_buffer, accumulator, &prevcount, 
191                                    &datatype );
192
193                     /* all reduced on available data this step (i) complete, 
194                      * pass to the next process unless you are the root.
195                      */
196                     if (rank != tree->tree_root) {
197                         /* send combined/accumulated data to parent */
198                         smpi_mpi_send( accumulator, prevcount, 
199                                                   datatype, tree->tree_prev, 
200                                                   COLL_TAG_REDUCE,
201                                                   comm);
202                     }
203
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;
207                 }
208
209                 /* update input buffer index */
210                 inbi = inbi ^ 1;
211             } /* end of for each child */
212         } /* end of for each segment */
213
214         /* clean up */
215         smpi_free_tmp_buffer(inbuf_free[0]);
216         smpi_free_tmp_buffer(inbuf_free[1]);
217         smpi_free_tmp_buffer(accumbuf_free);
218     }
219
220     /* leaf nodes 
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.
229     */
230     else {
231
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)) {
237             
238             segindex = 0;
239             while ( original_count > 0) {
240                 if (original_count < count_by_segment) {
241                     count_by_segment = original_count;
242                 }
243                 smpi_mpi_send((char*)sendbuf + 
244                                          segindex * segment_increment,
245                                          count_by_segment, datatype,
246                                          tree->tree_prev, 
247                                          COLL_TAG_REDUCE,
248                                          comm) ;
249                 segindex++;
250                 original_count -= count_by_segment;
251             }
252         }
253
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.
259         */
260         else {
261
262             int creq = 0;
263             MPI_Request* sreq = NULL;
264
265             sreq = (MPI_Request*) calloc( max_outstanding_reqs,
266                                               sizeof(MPI_Request ) );
267             if (NULL == sreq) { line = __LINE__; ret = -1; goto error_hndl; }
268
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,
274                                           tree->tree_prev, 
275                                           COLL_TAG_REDUCE,
276                                           comm);
277                 original_count -= count_by_segment;
278             }
279
280             creq = 0;
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;
285
286                 if( original_count < count_by_segment ) {
287                     count_by_segment = original_count;
288                 }
289                 sreq[creq]=smpi_mpi_isend((char*)sendbuf + 
290                                           segindex * segment_increment, 
291                                           count_by_segment, datatype, 
292                                           tree->tree_prev, 
293                                           COLL_TAG_REDUCE,
294                                           comm );
295                 creq = (creq + 1) % max_outstanding_reqs;
296                 segindex++;
297                 original_count -= count_by_segment;
298             }
299
300             /* Wait on the remaining request to complete */
301             smpi_mpi_waitall( max_outstanding_reqs, sreq, 
302                                          MPI_STATUSES_IGNORE );
303
304             /* free requests */
305             free(sreq);
306         }
307     }
308     free(tree);
309     return MPI_SUCCESS;
310
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);
317     return ret;
318 }
319
320 /* Attention: this version of the reduce operations does not
321    work for:
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 !
325 */
326
327 int smpi_coll_tuned_reduce_ompi_chain( void *sendbuf, void *recvbuf, int count,
328                                         MPI_Datatype datatype, 
329                                         MPI_Op  op, int root, 
330                                         MPI_Comm  comm
331                                         )
332 {
333     uint32_t segsize=64*1024;
334     int segcount = count;
335     size_t typelng;
336     int fanout = smpi_comm_size(comm)/2;
337
338     XBT_DEBUG("coll:tuned:reduce_intra_chain rank %d fo %d ss %5d", smpi_comm_rank(comm), fanout, segsize);
339
340     /**
341      * Determine number of segments and number of elements
342      * sent per operation
343      */
344     typelng = smpi_datatype_size( datatype);
345     
346     COLL_TUNED_COMPUTED_SEGCOUNT( segsize, typelng, segcount );
347
348     return smpi_coll_tuned_ompi_reduce_generic( sendbuf, recvbuf, count, datatype, 
349                                            op, root, comm,
350                                            ompi_coll_tuned_topo_build_chain(fanout, comm, root), 
351                                            segcount, 0 );
352 }
353
354
355 int smpi_coll_tuned_reduce_ompi_pipeline( void *sendbuf, void *recvbuf,
356                                            int count, MPI_Datatype datatype,
357                                            MPI_Op  op, int root,
358                                            MPI_Comm  comm  )
359 {
360
361     uint32_t segsize;
362     int segcount = count;
363     size_t typelng;
364 //    COLL_TUNED_UPDATE_PIPELINE( comm, tuned_module, root );
365
366     /**
367      * Determine number of segments and number of elements
368      * sent per operation
369      */
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; 
377
378     if (communicator_size > (a2 * message_size + b2)) {
379         // Pipeline_1K 
380         segsize = 1024;
381     }else if (communicator_size > (a4 * message_size + b4)) {
382         // Pipeline_32K 
383         segsize = 32*1024;
384     } else {
385         // Pipeline_64K 
386         segsize = 64*1024;
387     }
388
389     XBT_DEBUG("coll:tuned:reduce_intra_pipeline rank %d ss %5d",
390                  smpi_comm_rank(comm), segsize);
391
392     COLL_TUNED_COMPUTED_SEGCOUNT( segsize, typelng, segcount );
393
394     return smpi_coll_tuned_ompi_reduce_generic( sendbuf, recvbuf, count, datatype, 
395                                            op, root, comm,
396                                            ompi_coll_tuned_topo_build_chain( 1, comm, root), 
397                                            segcount, 0);
398 }
399
400 int smpi_coll_tuned_reduce_ompi_binary( void *sendbuf, void *recvbuf,
401                                          int count, MPI_Datatype datatype,
402                                          MPI_Op  op, int root,
403                                          MPI_Comm  comm)
404 {
405     uint32_t segsize;
406     int segcount = count;
407     size_t typelng;
408
409
410
411     /**
412      * Determine number of segments and number of elements
413      * sent per operation
414      */
415     typelng=smpi_datatype_size( datatype );
416
417         // Binary_32K 
418     segsize = 32*1024;
419
420     XBT_DEBUG("coll:tuned:reduce_intra_binary rank %d ss %5d",
421                  smpi_comm_rank(comm), segsize);
422
423     COLL_TUNED_COMPUTED_SEGCOUNT( segsize, typelng, segcount );
424
425     return smpi_coll_tuned_ompi_reduce_generic( sendbuf, recvbuf, count, datatype, 
426                                            op, root, comm, 
427                                            ompi_coll_tuned_topo_build_tree(2, comm, root), 
428                                            segcount, 0);
429 }
430
431 int smpi_coll_tuned_reduce_ompi_binomial( void *sendbuf, void *recvbuf,
432                                            int count, MPI_Datatype datatype,
433                                            MPI_Op  op, int root,
434                                            MPI_Comm  comm)
435 {
436
437     uint32_t segsize=0;
438     int segcount = count;
439     size_t typelng;
440
441     const double a1 =  0.6016 / 1024.0; /* [1/B] */
442     const double b1 =  1.3496;
443
444 //    COLL_TUNED_UPDATE_IN_ORDER_BMTREE( comm, tuned_module, root );
445
446     /**
447      * Determine number of segments and number of elements
448      * sent per operation
449      */
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)) {
455         /* Binomial_0K */
456         segsize = 0;
457     } else if (communicator_size > (a1 * message_size + b1)) {
458         // Binomial_1K 
459         segsize = 1024;
460     }
461
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 );
465
466     return smpi_coll_tuned_ompi_reduce_generic( sendbuf, recvbuf, count, datatype, 
467                                            op, root, comm, 
468                                            ompi_coll_tuned_topo_build_in_order_bmtree(comm, root), 
469                                            segcount, 0);
470 }
471
472 /*
473  * reduce_intra_in_order_binary 
474  * 
475  * Function:      Logarithmic reduce operation for non-commutative operations.
476  * Acecpts:       same as MPI_Reduce()
477  * Returns:       MPI_SUCCESS or error code
478  */
479 int smpi_coll_tuned_reduce_ompi_in_order_binary( void *sendbuf, void *recvbuf,
480                                                   int count, 
481                                                   MPI_Datatype datatype,
482                                                   MPI_Op  op, int root,
483                                                   MPI_Comm  comm)
484 {
485     uint32_t segsize=0;
486     int ret;
487     int rank, size, io_root;
488     int segcount = count;
489     void *use_this_sendbuf = NULL, *use_this_recvbuf = NULL;
490     size_t typelng;
491
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",
495                  rank, segsize);
496
497     /**
498      * Determine number of segments and number of elements
499      * sent per operation
500      */
501     typelng=smpi_datatype_size( datatype);
502     COLL_TUNED_COMPUTED_SEGCOUNT( segsize, typelng, segcount );
503
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.
510     */
511     io_root = size - 1;
512     use_this_sendbuf = sendbuf;
513     use_this_recvbuf = recvbuf;
514     if (io_root != root) {
515         ptrdiff_t text, ext;
516         char *tmpbuf = NULL;
517     
518         ext=smpi_datatype_get_extent(datatype);
519         text=smpi_datatype_get_extent(datatype);
520
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;
525             }
526             smpi_datatype_copy (
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;
534             }
535             use_this_recvbuf = tmpbuf;
536         }
537     }
538
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,
541                                           op, io_root, comm, 
542                                           ompi_coll_tuned_topo_build_in_order_bintree(comm), 
543                                           segcount, 0 );
544     if (MPI_SUCCESS != ret) { return ret; }
545
546     /* Clean up */
547     if (io_root != root) {
548         if (root == rank) {
549             /* Receive result from rank io_root to recvbuf */
550             smpi_mpi_recv(recvbuf, count, datatype, io_root,
551                                     COLL_TAG_REDUCE, comm,
552                                     MPI_STATUS_IGNORE);
553             if (MPI_IN_PLACE == sendbuf) {
554               smpi_free_tmp_buffer(use_this_sendbuf);
555             }
556           
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,
560                                     COLL_TAG_REDUCE,
561                                     comm);
562             smpi_free_tmp_buffer(use_this_recvbuf);
563         }
564     }
565
566     return MPI_SUCCESS;
567 }
568
569 /*
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.
579  */
580
581 /* copied function (with appropriate renaming) starts here */
582
583 /*
584  *  reduce_lin_intra
585  *
586  *  Function:   - reduction using O(N) algorithm
587  *  Accepts:    - same as MPI_Reduce()
588  *  Returns:    - MPI_SUCCESS or error code
589  */
590
591 int
592 smpi_coll_tuned_reduce_ompi_basic_linear(void *sbuf, void *rbuf, int count,
593                                           MPI_Datatype dtype,
594                                           MPI_Op op,
595                                           int root,
596                                           MPI_Comm comm)
597 {
598     int i, rank, size;
599     ptrdiff_t true_extent, lb, extent;
600     char *free_buffer = NULL;
601     char *pml_buffer = NULL;
602     char *inplace_temp = NULL;
603     char *inbuf;
604
605     /* Initialize */
606
607     rank = smpi_comm_rank(comm);
608     size = smpi_comm_size(comm);
609
610     XBT_DEBUG("coll:tuned:reduce_intra_basic_linear rank %d", rank);
611
612     /* If not root, send data to the root. */
613
614     if (rank != root) {
615         smpi_mpi_send(sbuf, count, dtype, root,
616                                 COLL_TAG_REDUCE,
617                                 comm);
618         return MPI_SUCCESS;
619     }
620
621     /* see discussion in ompi_coll_basic_reduce_lin_intra about 
622        extent and true extent */
623     /* for reducing buffer allocation lengths.... */
624
625     smpi_datatype_extent(dtype, &lb, &extent);
626     true_extent = smpi_datatype_get_extent(dtype);
627
628     if (MPI_IN_PLACE == sbuf) {
629         sbuf = rbuf;
630         inplace_temp = (char*)smpi_get_tmp_recvbuffer(true_extent + (count - 1) * extent);
631         if (NULL == inplace_temp) {
632             return -1;
633         }
634         rbuf = inplace_temp - lb;
635     }
636
637     if (size > 1) {
638         free_buffer = (char*)smpi_get_tmp_recvbuffer(true_extent + (count - 1) * extent);
639         pml_buffer = free_buffer - lb;
640     }
641
642     /* Initialize the receive buffer. */
643
644     if (rank == (size - 1)) {
645         smpi_datatype_copy((char*)sbuf, count, dtype,(char*)rbuf, count, dtype);
646     } else {
647         smpi_mpi_recv(rbuf, count, dtype, size - 1,
648                                 COLL_TAG_REDUCE, comm,
649                                 MPI_STATUS_IGNORE);
650     }
651
652     /* Loop receiving and calling reduction function (C or Fortran). */
653
654     for (i = size - 2; i >= 0; --i) {
655         if (rank == i) {
656             inbuf = (char*)sbuf;
657         } else {
658             smpi_mpi_recv(pml_buffer, count, dtype, i,
659                                     COLL_TAG_REDUCE, comm,
660                                     MPI_STATUS_IGNORE);
661             inbuf = pml_buffer;
662         }
663
664         /* Perform the reduction */
665         smpi_op_apply(op, inbuf, rbuf, &count, &dtype);
666     }
667
668     if (NULL != inplace_temp) {
669         smpi_datatype_copy(inplace_temp, count, dtype,(char*)sbuf
670                                                   ,count , dtype);
671         smpi_free_tmp_buffer(inplace_temp);
672     }
673     if (NULL != free_buffer) {
674         smpi_free_tmp_buffer(free_buffer);
675     }
676
677     /* All done */
678     return MPI_SUCCESS;
679 }
680
681 /* copied function (with appropriate renaming) ends here */
682
683