Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ok, I stop trying to please sonar.
[simgrid.git] / src / smpi / colls / reduce / reduce-ompi.cpp
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 namespace simgrid{
26 namespace smpi{
27
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 );
33 /**
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)
39  *
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.
43  */
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 )
49 {
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;
57
58     /**
59      * Determine number of segments and number of elements
60      * sent per operation
61      */
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;
65
66     sendtmpbuf = (char*) sendbuf; 
67     if( sendbuf == MPI_IN_PLACE ) { 
68         sendtmpbuf = (char *)recvbuf; 
69     }
70
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);
72
73     rank = comm->rank();
74
75     /* non-leaf nodes - wait for children to send me data & forward up 
76        (if needed) */
77     if( tree->tree_nextsize > 0 ) {
78         ptrdiff_t true_extent, real_segment_size;
79         true_extent=datatype->get_extent();
80
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; 
90             }
91             accumbuf = accumbuf_free - lower_bound;
92         } 
93
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 && !op->is_commutative())) {
97             Datatype::copy(
98                                                 (char*)sendtmpbuf, original_count, datatype,
99                                                 (char*)accumbuf, original_count, datatype);
100         }
101         /* Allocate two buffers for incoming segments */
102         real_segment_size = true_extent + (count_by_segment - 1) * extent;
103         inbuf_free[0] = (char*) smpi_get_tmp_recvbuffer(real_segment_size);
104         if( inbuf_free[0] == NULL ) { 
105             line = __LINE__; ret = -1; goto error_hndl; 
106         }
107         inbuf[0] = inbuf_free[0] - lower_bound;
108         /* if there is chance to overlap communication -
109            allocate second buffer */
110         if( (num_segments > 1) || (tree->tree_nextsize > 1) ) {
111             inbuf_free[1] = (char*) smpi_get_tmp_recvbuffer(real_segment_size);
112             if( inbuf_free[1] == NULL ) { 
113                 line = __LINE__; ret = -1; goto error_hndl;
114             }
115             inbuf[1] = inbuf_free[1] - lower_bound;
116         } 
117
118         /* reset input buffer index and receive count */
119         inbi = 0;
120         recvcount = 0;
121         /* for each segment */
122         for( segindex = 0; segindex <= num_segments; segindex++ ) {
123             prevcount = recvcount;
124             /* recvcount - number of elements in current segment */
125             recvcount = count_by_segment;
126             if( segindex == (num_segments-1) )
127                 recvcount = original_count - count_by_segment * segindex;
128
129             /* for each child */
130             for( i = 0; i < tree->tree_nextsize; i++ ) {
131                 /**
132                  * We try to overlap communication:
133                  * either with next segment or with the next child
134                  */
135                 /* post irecv for current segindex on current child */
136                 if( segindex < num_segments ) {
137                     void* local_recvbuf = inbuf[inbi];
138                     if( 0 == i ) {
139                         /* for the first step (1st child per segment) and 
140                          * commutative operations we might be able to irecv 
141                          * directly into the accumulate buffer so that we can 
142                          * reduce(op) this with our sendbuf in one step as 
143                          * ompi_op_reduce only has two buffer pointers, 
144                          * this avoids an extra memory copy.
145                          *
146                          * BUT if the operation is non-commutative or 
147                          * we are root and are USING MPI_IN_PLACE this is wrong!
148                          */
149                         if(  (op==MPI_OP_NULL || op->is_commutative()) &&
150                             !((MPI_IN_PLACE == sendbuf) && (rank == tree->tree_root)) ) {
151                             local_recvbuf = accumbuf + segindex * segment_increment;
152                         }
153                     }
154
155                     reqs[inbi]=Request::irecv(local_recvbuf, recvcount, datatype,
156                                              tree->tree_next[i], 
157                                              COLL_TAG_REDUCE, comm
158                                              );
159                 }
160                 /* wait for previous req to complete, if any.
161                    if there are no requests reqs[inbi ^1] will be 
162                    MPI_REQUEST_NULL. */
163                 /* wait on data from last child for previous segment */
164                 Request::waitall( 1, &reqs[inbi ^ 1], 
165                                              MPI_STATUSES_IGNORE );
166                 local_op_buffer = inbuf[inbi ^ 1];
167                 if( i > 0 ) {
168                     /* our first operation is to combine our own [sendbuf] data 
169                      * with the data we recvd from down stream (but only 
170                      * the operation is commutative and if we are not root and 
171                      * not using MPI_IN_PLACE)
172                      */
173                     if( 1 == i ) {
174                         if( (op==MPI_OP_NULL || op->is_commutative())&& 
175                             !((MPI_IN_PLACE == sendbuf) && (rank == tree->tree_root)) ) {
176                             local_op_buffer = sendtmpbuf + segindex * segment_increment;
177                         }
178                     }
179                     /* apply operation */
180                     if(op!=MPI_OP_NULL) op->apply( local_op_buffer, 
181                                    accumbuf + segindex * segment_increment, 
182                                    &recvcount, datatype );
183                 } else if ( segindex > 0 ) {
184                     void* accumulator = accumbuf + (segindex-1) * segment_increment;
185                     if( tree->tree_nextsize <= 1 ) {
186                         if(  (op==MPI_OP_NULL || op->is_commutative()) &&
187                             !((MPI_IN_PLACE == sendbuf) && (rank == tree->tree_root)) ) {
188                             local_op_buffer = sendtmpbuf + (segindex-1) * segment_increment;
189                         }
190                     }
191                     if(op!=MPI_OP_NULL) op->apply( local_op_buffer, accumulator, &prevcount, 
192                                    datatype );
193
194                     /* all reduced on available data this step (i) complete, 
195                      * pass to the next process unless you are the root.
196                      */
197                     if (rank != tree->tree_root) {
198                         /* send combined/accumulated data to parent */
199                         Request::send( accumulator, prevcount, 
200                                                   datatype, tree->tree_prev, 
201                                                   COLL_TAG_REDUCE,
202                                                   comm);
203                     }
204
205                     /* we stop when segindex = number of segments 
206                        (i.e. we do num_segment+1 steps for pipelining */
207                     if (segindex == num_segments) break;
208                 }
209
210                 /* update input buffer index */
211                 inbi = inbi ^ 1;
212             } /* end of for each child */
213         } /* end of for each segment */
214
215         /* clean up */
216         smpi_free_tmp_buffer(inbuf_free[0]);
217         smpi_free_tmp_buffer(inbuf_free[1]);
218         smpi_free_tmp_buffer(accumbuf_free);
219     }
220
221     /* leaf nodes 
222        Depending on the value of max_outstanding_reqs and 
223        the number of segments we have two options:
224        - send all segments using blocking send to the parent, or
225        - avoid overflooding the parent nodes by limiting the number of 
226        outstanding requests to max_oustanding_reqs.
227        TODO/POSSIBLE IMPROVEMENT: If there is a way to determine the eager size 
228        for the current communication, synchronization should be used only 
229        when the message/segment size is smaller than the eager size.
230     */
231     else {
232
233         /* If the number of segments is less than a maximum number of oustanding
234            requests or there is no limit on the maximum number of outstanding 
235            requests, we send data to the parent using blocking send */
236         if ((0 == max_outstanding_reqs) || 
237             (num_segments <= max_outstanding_reqs)) {
238             
239             segindex = 0;
240             while ( original_count > 0) {
241                 if (original_count < count_by_segment) {
242                     count_by_segment = original_count;
243                 }
244                 Request::send((char*)sendbuf + 
245                                          segindex * segment_increment,
246                                          count_by_segment, datatype,
247                                          tree->tree_prev, 
248                                          COLL_TAG_REDUCE,
249                                          comm) ;
250                 segindex++;
251                 original_count -= count_by_segment;
252             }
253         }
254
255         /* Otherwise, introduce flow control:
256            - post max_outstanding_reqs non-blocking synchronous send,
257            - for remaining segments
258            - wait for a ssend to complete, and post the next one.
259            - wait for all outstanding sends to complete.
260         */
261         else {
262
263             int creq = 0;
264             MPI_Request* sreq = NULL;
265
266             sreq = (MPI_Request*) calloc( max_outstanding_reqs,
267                                               sizeof(MPI_Request ) );
268             if (NULL == sreq) { line = __LINE__; ret = -1; goto error_hndl; }
269
270             /* post first group of requests */
271             for (segindex = 0; segindex < max_outstanding_reqs; segindex++) {
272                 sreq[segindex]=Request::isend((char*)sendbuf +
273                                           segindex * segment_increment,
274                                           count_by_segment, datatype,
275                                           tree->tree_prev, 
276                                           COLL_TAG_REDUCE,
277                                           comm);
278                 original_count -= count_by_segment;
279             }
280
281             creq = 0;
282             while ( original_count > 0 ) {
283                 /* wait on a posted request to complete */
284                 Request::wait(&sreq[creq], MPI_STATUS_IGNORE);
285                 sreq[creq] = MPI_REQUEST_NULL;
286
287                 if( original_count < count_by_segment ) {
288                     count_by_segment = original_count;
289                 }
290                 sreq[creq]=Request::isend((char*)sendbuf + 
291                                           segindex * segment_increment, 
292                                           count_by_segment, datatype, 
293                                           tree->tree_prev, 
294                                           COLL_TAG_REDUCE,
295                                           comm );
296                 creq = (creq + 1) % max_outstanding_reqs;
297                 segindex++;
298                 original_count -= count_by_segment;
299             }
300
301             /* Wait on the remaining request to complete */
302             Request::waitall( max_outstanding_reqs, sreq, 
303                                          MPI_STATUSES_IGNORE );
304
305             /* free requests */
306             free(sreq);
307         }
308     }
309     free(tree);
310     return MPI_SUCCESS;
311
312  error_hndl:  /* error handler */
313     XBT_DEBUG("ERROR_HNDL: node %d file %s line %d error %d\n", 
314                    rank, __FILE__, line, ret );
315     if( inbuf_free[0] != NULL ) free(inbuf_free[0]);
316     if( inbuf_free[1] != NULL ) free(inbuf_free[1]);
317     if( accumbuf_free != NULL ) free(accumbuf);
318     return ret;
319 }
320
321 /* Attention: this version of the reduce operations does not
322    work for:
323    - non-commutative operations
324    - segment sizes which are not multiplies of the extent of the datatype
325      meaning that at least one datatype must fit in the segment !
326 */
327
328
329 int Coll_reduce_ompi_chain::reduce( void *sendbuf, void *recvbuf, int count,
330                                         MPI_Datatype datatype, 
331                                         MPI_Op  op, int root, 
332                                         MPI_Comm  comm
333                                         )
334 {
335     uint32_t segsize=64*1024;
336     int segcount = count;
337     size_t typelng;
338     int fanout = comm->size()/2;
339
340     XBT_DEBUG("coll:tuned:reduce_intra_chain rank %d fo %d ss %5d", comm->rank(), fanout, segsize);
341
342     /**
343      * Determine number of segments and number of elements
344      * sent per operation
345      */
346     typelng = datatype->size();
347     
348     COLL_TUNED_COMPUTED_SEGCOUNT( segsize, typelng, segcount );
349
350     return smpi_coll_tuned_ompi_reduce_generic( sendbuf, recvbuf, count, datatype, 
351                                            op, root, comm,
352                                            ompi_coll_tuned_topo_build_chain(fanout, comm, root), 
353                                            segcount, 0 );
354 }
355
356
357 int Coll_reduce_ompi_pipeline::reduce( void *sendbuf, void *recvbuf,
358                                            int count, MPI_Datatype datatype,
359                                            MPI_Op  op, int root,
360                                            MPI_Comm  comm  )
361 {
362
363     uint32_t segsize;
364     int segcount = count;
365     size_t typelng;
366 //    COLL_TUNED_UPDATE_PIPELINE( comm, tuned_module, root );
367
368     /**
369      * Determine number of segments and number of elements
370      * sent per operation
371      */
372     const double a2 =  0.0410 / 1024.0; /* [1/B] */
373     const double b2 =  9.7128;
374     const double a4 =  0.0033 / 1024.0; /* [1/B] */
375     const double b4 =  1.6761;
376     typelng= datatype->size();
377     int communicator_size = comm->size();
378     size_t message_size = typelng * count; 
379
380     if (communicator_size > (a2 * message_size + b2)) {
381         // Pipeline_1K 
382         segsize = 1024;
383     }else if (communicator_size > (a4 * message_size + b4)) {
384         // Pipeline_32K 
385         segsize = 32*1024;
386     } else {
387         // Pipeline_64K 
388         segsize = 64*1024;
389     }
390
391     XBT_DEBUG("coll:tuned:reduce_intra_pipeline rank %d ss %5d",
392                  comm->rank(), segsize);
393
394     COLL_TUNED_COMPUTED_SEGCOUNT( segsize, typelng, segcount );
395
396     return smpi_coll_tuned_ompi_reduce_generic( sendbuf, recvbuf, count, datatype, 
397                                            op, root, comm,
398                                            ompi_coll_tuned_topo_build_chain( 1, comm, root), 
399                                            segcount, 0);
400 }
401
402 int Coll_reduce_ompi_binary::reduce( void *sendbuf, void *recvbuf,
403                                          int count, MPI_Datatype datatype,
404                                          MPI_Op  op, int root,
405                                          MPI_Comm  comm)
406 {
407     uint32_t segsize;
408     int segcount = count;
409     size_t typelng;
410
411
412
413     /**
414      * Determine number of segments and number of elements
415      * sent per operation
416      */
417     typelng=datatype->size();
418
419         // Binary_32K 
420     segsize = 32*1024;
421
422     XBT_DEBUG("coll:tuned:reduce_intra_binary rank %d ss %5d",
423                  comm->rank(), segsize);
424
425     COLL_TUNED_COMPUTED_SEGCOUNT( segsize, typelng, segcount );
426
427     return smpi_coll_tuned_ompi_reduce_generic( sendbuf, recvbuf, count, datatype, 
428                                            op, root, comm, 
429                                            ompi_coll_tuned_topo_build_tree(2, comm, root), 
430                                            segcount, 0);
431 }
432
433 int Coll_reduce_ompi_binomial::reduce( void *sendbuf, void *recvbuf,
434                                            int count, MPI_Datatype datatype,
435                                            MPI_Op  op, int root,
436                                            MPI_Comm  comm)
437 {
438
439     uint32_t segsize=0;
440     int segcount = count;
441     size_t typelng;
442
443     const double a1 =  0.6016 / 1024.0; /* [1/B] */
444     const double b1 =  1.3496;
445
446 //    COLL_TUNED_UPDATE_IN_ORDER_BMTREE( comm, tuned_module, root );
447
448     /**
449      * Determine number of segments and number of elements
450      * sent per operation
451      */
452     typelng= datatype->size();
453     int communicator_size = comm->size();
454     size_t message_size = typelng * count; 
455     if (((communicator_size < 8) && (message_size < 20480)) ||
456                (message_size < 2048) || (count <= 1)) {
457         /* Binomial_0K */
458         segsize = 0;
459     } else if (communicator_size > (a1 * message_size + b1)) {
460         // Binomial_1K 
461         segsize = 1024;
462     }
463
464     XBT_DEBUG("coll:tuned:reduce_intra_binomial rank %d ss %5d",
465                  comm->rank(), segsize);
466     COLL_TUNED_COMPUTED_SEGCOUNT( segsize, typelng, segcount );
467
468     return smpi_coll_tuned_ompi_reduce_generic( sendbuf, recvbuf, count, datatype, 
469                                            op, root, comm, 
470                                            ompi_coll_tuned_topo_build_in_order_bmtree(comm, root), 
471                                            segcount, 0);
472 }
473
474 /*
475  * reduce_intra_in_order_binary 
476  * 
477  * Function:      Logarithmic reduce operation for non-commutative operations.
478  * Acecpts:       same as MPI_Reduce()
479  * Returns:       MPI_SUCCESS or error code
480  */
481 int Coll_reduce_ompi_in_order_binary::reduce( void *sendbuf, void *recvbuf,
482                                                   int count, 
483                                                   MPI_Datatype datatype,
484                                                   MPI_Op  op, int root,
485                                                   MPI_Comm  comm)
486 {
487     uint32_t segsize=0;
488     int ret;
489     int rank, size, io_root;
490     int segcount = count;
491     void *use_this_sendbuf = NULL, *use_this_recvbuf = NULL;
492     size_t typelng;
493
494     rank = comm->rank();
495     size = comm->size();
496     XBT_DEBUG("coll:tuned:reduce_intra_in_order_binary rank %d ss %5d",
497                  rank, segsize);
498
499     /**
500      * Determine number of segments and number of elements
501      * sent per operation
502      */
503     typelng=datatype->size();
504     COLL_TUNED_COMPUTED_SEGCOUNT( segsize, typelng, segcount );
505
506     /* An in-order binary tree must use root (size-1) to preserve the order of
507        operations.  Thus, if root is not rank (size - 1), then we must handle
508        1. MPI_IN_PLACE option on real root, and 
509        2. we must allocate temporary recvbuf on rank (size - 1).
510        Note that generic function must be careful not to switch order of 
511        operations for non-commutative ops.
512     */
513     io_root = size - 1;
514     use_this_sendbuf = sendbuf;
515     use_this_recvbuf = recvbuf;
516     if (io_root != root) {
517         ptrdiff_t text, ext;
518         char *tmpbuf = NULL;
519     
520         ext=datatype->get_extent();
521         text=datatype->get_extent();
522
523         if ((root == rank) && (MPI_IN_PLACE == sendbuf)) {
524             tmpbuf = (char *) smpi_get_tmp_sendbuffer(text + (count - 1) * ext);
525             if (NULL == tmpbuf) {
526                 return MPI_ERR_INTERN;
527             }
528             Datatype::copy (
529                                                 (char*)recvbuf, count, datatype,
530                                                 (char*)tmpbuf, count, datatype);
531             use_this_sendbuf = tmpbuf;
532         } else if (io_root == rank) {
533             tmpbuf = (char *) smpi_get_tmp_recvbuffer(text + (count - 1) * ext);
534             if (NULL == tmpbuf) {
535                 return MPI_ERR_INTERN;
536             }
537             use_this_recvbuf = tmpbuf;
538         }
539     }
540
541     /* Use generic reduce with in-order binary tree topology and io_root */
542     ret = smpi_coll_tuned_ompi_reduce_generic( use_this_sendbuf, use_this_recvbuf, count, datatype,
543                                           op, io_root, comm, 
544                                           ompi_coll_tuned_topo_build_in_order_bintree(comm), 
545                                           segcount, 0 );
546     if (MPI_SUCCESS != ret) { return ret; }
547
548     /* Clean up */
549     if (io_root != root) {
550         if (root == rank) {
551             /* Receive result from rank io_root to recvbuf */
552             Request::recv(recvbuf, count, datatype, io_root,
553                                     COLL_TAG_REDUCE, comm,
554                                     MPI_STATUS_IGNORE);
555             if (MPI_IN_PLACE == sendbuf) {
556               smpi_free_tmp_buffer(use_this_sendbuf);
557             }
558           
559         } else if (io_root == rank) {
560             /* Send result from use_this_recvbuf to root */
561             Request::send(use_this_recvbuf, count, datatype, root,
562                                     COLL_TAG_REDUCE,
563                                     comm);
564             smpi_free_tmp_buffer(use_this_recvbuf);
565         }
566     }
567
568     return MPI_SUCCESS;
569 }
570
571 /*
572  * Linear functions are copied from the BASIC coll module
573  * they do not segment the message and are simple implementations
574  * but for some small number of nodes and/or small data sizes they 
575  * are just as fast as tuned/tree based segmenting operations 
576  * and as such may be selected by the decision functions
577  * These are copied into this module due to the way we select modules
578  * in V1. i.e. in V2 we will handle this differently and so will not
579  * have to duplicate code.
580  * GEF Oct05 after asking Jeff.
581  */
582
583 /* copied function (with appropriate renaming) starts here */
584
585 /*
586  *  reduce_lin_intra
587  *
588  *  Function:   - reduction using O(N) algorithm
589  *  Accepts:    - same as MPI_Reduce()
590  *  Returns:    - MPI_SUCCESS or error code
591  */
592
593 int
594 Coll_reduce_ompi_basic_linear::reduce(void *sbuf, void *rbuf, int count,
595                                           MPI_Datatype dtype,
596                                           MPI_Op op,
597                                           int root,
598                                           MPI_Comm comm)
599 {
600     int i, rank, size;
601     ptrdiff_t true_extent, lb, extent;
602     char *free_buffer = NULL;
603     char *pml_buffer = NULL;
604     char *inplace_temp = NULL;
605     char *inbuf;
606
607     /* Initialize */
608
609     rank = comm->rank();
610     size = comm->size();
611
612     XBT_DEBUG("coll:tuned:reduce_intra_basic_linear rank %d", rank);
613
614     /* If not root, send data to the root. */
615
616     if (rank != root) {
617         Request::send(sbuf, count, dtype, root,
618                                 COLL_TAG_REDUCE,
619                                 comm);
620         return MPI_SUCCESS;
621     }
622
623     /* see discussion in ompi_coll_basic_reduce_lin_intra about 
624        extent and true extent */
625     /* for reducing buffer allocation lengths.... */
626
627     dtype->extent(&lb, &extent);
628     true_extent = dtype->get_extent();
629
630     if (MPI_IN_PLACE == sbuf) {
631         sbuf = rbuf;
632         inplace_temp = (char*)smpi_get_tmp_recvbuffer(true_extent + (count - 1) * extent);
633         if (NULL == inplace_temp) {
634             return -1;
635         }
636         rbuf = inplace_temp - lb;
637     }
638
639     if (size > 1) {
640         free_buffer = (char*)smpi_get_tmp_recvbuffer(true_extent + (count - 1) * extent);
641         pml_buffer = free_buffer - lb;
642     }
643
644     /* Initialize the receive buffer. */
645
646     if (rank == (size - 1)) {
647         Datatype::copy((char*)sbuf, count, dtype,(char*)rbuf, count, dtype);
648     } else {
649         Request::recv(rbuf, count, dtype, size - 1,
650                                 COLL_TAG_REDUCE, comm,
651                                 MPI_STATUS_IGNORE);
652     }
653
654     /* Loop receiving and calling reduction function (C or Fortran). */
655
656     for (i = size - 2; i >= 0; --i) {
657         if (rank == i) {
658             inbuf = (char*)sbuf;
659         } else {
660             Request::recv(pml_buffer, count, dtype, i,
661                                     COLL_TAG_REDUCE, comm,
662                                     MPI_STATUS_IGNORE);
663             inbuf = pml_buffer;
664         }
665
666         /* Perform the reduction */
667         if(op!=MPI_OP_NULL) op->apply( inbuf, rbuf, &count, dtype);
668     }
669
670     if (NULL != inplace_temp) {
671         Datatype::copy(inplace_temp, count, dtype,(char*)sbuf
672                                                   ,count , dtype);
673         smpi_free_tmp_buffer(inplace_temp);
674     }
675     if (NULL != free_buffer) {
676         smpi_free_tmp_buffer(free_buffer);
677     }
678
679     /* All done */
680     return MPI_SUCCESS;
681 }
682
683 /* copied function (with appropriate renaming) ends here */
684
685
686 }
687 }