Teuchos_CommHelpers.hpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef TEUCHOS_COMM_HELPERS_HPP
00030 #define TEUCHOS_COMM_HELPERS_HPP
00031 
00032 #include "Teuchos_Comm.hpp"
00033 #include "Teuchos_CommUtilities.hpp"
00034 #include "Teuchos_SerializationTraitsHelpers.hpp"
00035 #include "Teuchos_ReductionOpHelpers.hpp"
00036 #include "Teuchos_SerializerHelpers.hpp"
00037 #include "Teuchos_ScalarTraits.hpp"
00038 #include "Teuchos_OrdinalTraits.hpp"
00039 #include "Teuchos_Array.hpp"
00040 
00041 namespace Teuchos {
00042 
00043 //
00044 // Teuchos::Comm Helper Functions
00045 //
00046 
00052 enum EReductionType {
00053   REDUCE_SUM     
00054   ,REDUCE_MIN    
00055   ,REDUCE_MAX    
00056   ,REDUCE_AND    
00057 };
00058 
00063 inline
00064 const char* toString( const EReductionType reductType )
00065 {
00066   switch(reductType) {
00067     case REDUCE_SUM: return "REDUCE_SUM";
00068     case REDUCE_MIN: return "REDUCE_MIN";
00069     case REDUCE_MAX: return "REDUCE_MAX";
00070     case REDUCE_AND: return "REDUCE_AND";
00071    default: TEST_FOR_EXCEPT(true);
00072   }
00073   return 0; // Will never be called
00074 }
00075 
00080 template<typename Ordinal>
00081 int rank(const Comm<Ordinal>& comm);
00082 
00087 template<typename Ordinal>
00088 int size(const Comm<Ordinal>& comm);
00089 
00094 template<typename Ordinal>
00095 void barrier(const Comm<Ordinal>& comm);
00096 
00101 template<typename Ordinal, typename Packet>
00102 void broadcast(
00103   const Comm<Ordinal>& comm
00104   ,const int rootRank, const Ordinal count, Packet buffer[]
00105   );
00106 
00111 template<typename Ordinal, typename Packet>
00112 void broadcast(
00113   const Comm<Ordinal>& comm
00114   ,const int rootRank, Packet *object
00115   );
00116 
00121 template<typename Ordinal, typename Packet>
00122 void broadcast(
00123   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer
00124   ,const int rootRank, const Ordinal count, Packet*const buffer[]
00125   );
00126 
00132 template<typename Ordinal, typename Packet>
00133 void gatherAll(
00134   const Comm<Ordinal>& comm
00135   ,const Ordinal sendCount, const Packet sendBuffer[]
00136   ,const Ordinal recvCount, Packet recvBuffer[]
00137   );
00138 
00144 template<typename Ordinal, typename Packet>
00145 void gatherAll(
00146   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer
00147   ,const Ordinal sendCount, const Packet*const sendBuffer[]
00148   ,const Ordinal recvCount, Packet*const recvBuffer[]
00149   );
00150 
00156 template<typename Ordinal, typename Packet>
00157 void reduceAll(
00158   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp
00159   ,const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
00160   );
00161 
00167 template<typename Ordinal, typename Packet>
00168 void reduceAll(
00169   const Comm<Ordinal>& comm, const EReductionType reductType
00170   ,const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
00171   );
00172 
00178 template<typename Ordinal, typename Packet>
00179 void reduceAll(
00180   const Comm<Ordinal>& comm, const EReductionType reductType
00181   ,const Packet &send, Packet *globalReduct
00182   );
00183 
00189 template<typename Ordinal, typename Packet>
00190 void reduceAll(
00191   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer
00192   ,const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp
00193   ,const Ordinal count, const Packet*const sendBuffer[], Packet*const globalReducts[]
00194   );
00195 
00201 template<typename Ordinal, typename Packet>
00202 void reduceAllAndScatter(
00203   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp
00204   ,const Ordinal sendCount, const Packet sendBuffer[] 
00205   ,const Ordinal recvCounts[], Packet myGlobalReducts[]
00206   );
00207 
00213 template<typename Ordinal, typename Packet>
00214 void reduceAllAndScatter(
00215   const Comm<Ordinal>& comm, const EReductionType reductType
00216   ,const Ordinal sendCount, const Packet sendBuffer[] 
00217   ,const Ordinal recvCounts[], Packet myGlobalReducts[]
00218   );
00219 
00225 template<typename Ordinal, typename Packet>
00226 void reduceAllAndScatter(
00227   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer
00228   ,const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp
00229   ,const Ordinal sendCount, const Packet*const sendBuffer[] 
00230   ,const Ordinal recvCounts[], Packet*const myGlobalReducts[]
00231   );
00232 
00238 template<typename Ordinal, typename Packet>
00239 void scan(
00240   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp
00241   ,const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
00242   );
00243 
00249 template<typename Ordinal, typename Packet>
00250 void scan(
00251   const Comm<Ordinal>& comm, const EReductionType reductType
00252   ,const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
00253   );
00254 
00260 template<typename Ordinal, typename Packet>
00261 void scan(
00262   const Comm<Ordinal>& comm, const EReductionType reductType
00263   ,const Packet &send, Packet *scanReduct
00264   );
00265 
00271 template<typename Ordinal, typename Packet>
00272 void scan(
00273   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer
00274   ,const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp
00275   ,const Ordinal count, const Packet*const sendBuffer[], Packet*const scanReducts[]
00276   );
00277 
00282 template<typename Ordinal, typename Packet>
00283 void send(
00284   const Comm<Ordinal>& comm
00285   ,const Ordinal count, const Packet sendBuffer[], const int destRank
00286   );
00287 
00292 template<typename Ordinal, typename Packet>
00293 void send(
00294   const Comm<Ordinal>& comm
00295   ,const Packet &send, const int destRank
00296   );
00297 
00302 template<typename Ordinal, typename Packet>
00303 void send(
00304   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer
00305   ,const Ordinal count, const Packet*const sendBuffer[], const int destRank
00306   );
00307 
00312 template<typename Ordinal, typename Packet>
00313 int receive(
00314   const Comm<Ordinal>& comm
00315   ,const int sourceRank, const Ordinal count, Packet recvBuffer[] 
00316   );
00317 
00322 template<typename Ordinal, typename Packet>
00323 int receive(
00324   const Comm<Ordinal>& comm
00325   ,const int sourceRank, Packet *recv 
00326   );
00327 
00332 template<typename Ordinal, typename Packet>
00333 int receive(
00334   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer
00335   ,const int sourceRank, const Ordinal count, Packet*const recvBuffer[] 
00336   );
00337 
00338 //
00339 // Standard reduction subclasses for objects that use value semantics
00340 //
00341 
00346 template<typename Ordinal, typename Packet>
00347 class SumValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00348 {
00349 public:
00351   void reduce(
00352     const Ordinal     count
00353     ,const Packet     inBuffer[]
00354     ,Packet           inoutBuffer[]
00355     ) const;
00356 };
00357 
00366 template<typename Ordinal, typename Packet>
00367 class MinValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00368 {
00369 public:
00371   void reduce(
00372     const Ordinal     count
00373     ,const Packet     inBuffer[]
00374     ,Packet           inoutBuffer[]
00375     ) const;
00376 };
00377 
00386 template<typename Ordinal, typename Packet>
00387 class MaxValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00388 {
00389 public:
00391   void reduce(
00392     const Ordinal     count
00393     ,const Packet     inBuffer[]
00394     ,Packet           inoutBuffer[]
00395     ) const;
00396 };
00397 
00398 
00399 
00404 template<typename Ordinal, typename Packet>
00405 class ANDValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00406 {
00407 public:
00409   void reduce(
00410     const Ordinal     count
00411     ,const Packet       inBuffer[]
00412     ,Packet             inoutBuffer[]
00413     ) const;
00414 };
00415 
00416 
00417 
00418 
00419 
00420 // ////////////////////////////////////////////////////////////
00421 // Implementation details (not for geneal users to mess with)
00422 
00423 //
00424 // ReductionOp Utilities
00425 //
00426 
00427 namespace MixMaxUtilities {
00428 
00429 template<bool isComparable, typename Ordinal, typename Packet>
00430 class Min {};
00431 
00432 template<typename Ordinal, typename Packet>
00433 class Min<true,Ordinal,Packet> {
00434 public:
00435   static void min(
00436     const Ordinal     count
00437     ,const Packet     inBuffer[]
00438     ,Packet           inoutBuffer[]
00439     )
00440     {
00441       for( int i = 0; i < count; ++i )
00442         inoutBuffer[i] = TEUCHOS_MIN(inoutBuffer[i],inBuffer[i]);
00443     }
00444 };
00445 
00446 template<typename Ordinal, typename Packet>
00447 class Min<false,Ordinal,Packet> {
00448 public:
00449   static void min(
00450     const Ordinal     count
00451     ,const Packet     inBuffer[]
00452     ,Packet           inoutBuffer[]
00453     )
00454     {
00455       TEST_FOR_EXCEPTION(
00456         true,std::logic_error
00457         ,"Error, the type "<<ScalarTraits<Packet>::name()
00458         <<" does not support comparison operations!"
00459         );
00460     }
00461 };
00462 
00463 template<bool isComparable, typename Ordinal, typename Packet>
00464 class Max {};
00465 
00466 template<typename Ordinal, typename Packet>
00467 class Max<true,Ordinal,Packet> {
00468 public:
00469   static void max(
00470     const Ordinal     count
00471     ,const Packet     inBuffer[]
00472     ,Packet           inoutBuffer[]
00473     )
00474     {
00475       for( int i = 0; i < count; ++i )
00476         inoutBuffer[i] = TEUCHOS_MAX(inoutBuffer[i],inBuffer[i]);
00477     }
00478 };
00479 
00480 template<typename Ordinal, typename Packet>
00481 class Max<false,Ordinal,Packet> {
00482 public:
00483   static void max(
00484     const Ordinal     count
00485     ,const Packet     inBuffer[]
00486     ,Packet           inoutBuffer[]
00487     )
00488     {
00489       TEST_FOR_EXCEPTION(
00490         true,std::logic_error
00491         ,"Error, the type "<<ScalarTraits<Packet>::name()
00492         <<" does not support comparison operations!"
00493         );
00494     }
00495 };
00496 
00497 template<bool isComparable, typename Ordinal, typename Packet>
00498 class AND {};
00499 
00500 template<typename Ordinal, typename Packet>
00501 class AND<true,Ordinal,Packet> {
00502 public:
00503   static void And(
00504                    const Ordinal     count
00505                    ,const Packet     inBuffer[]
00506                    ,Packet           inoutBuffer[]
00507                    )
00508   {
00509     for( int i = 0; i < count; ++i )
00510       inoutBuffer[i] = inoutBuffer[i] && inBuffer[i];
00511   }
00512 };
00513 
00514 template<typename Ordinal, typename Packet>
00515 class AND<false,Ordinal,Packet> {
00516 public:
00517   static void And(
00518     const Ordinal     count
00519     ,const Packet     inBuffer[]
00520     ,Packet           inoutBuffer[]
00521     )
00522     {
00523       TEST_FOR_EXCEPTION(
00524         true,std::logic_error
00525         ,"Error, the type "<<ScalarTraits<Packet>::name()
00526         <<" does not support logical AND operations!"
00527         );
00528     }
00529 };
00530 
00531 } // namespace MixMaxUtilities
00532 
00533 template<typename Ordinal, typename Packet>
00534 void SumValueReductionOp<Ordinal,Packet>::reduce(
00535   const Ordinal     count
00536   ,const Packet     inBuffer[]
00537   ,Packet           inoutBuffer[]
00538   ) const
00539 {
00540   for( int i = 0; i < count; ++i )
00541     inoutBuffer[i] += inBuffer[i];
00542 }
00543 
00544 template<typename Ordinal, typename Packet>
00545 void MinValueReductionOp<Ordinal,Packet>::reduce(
00546   const Ordinal     count
00547   ,const Packet     inBuffer[]
00548   ,Packet           inoutBuffer[]
00549   ) const
00550 {
00551   typedef ScalarTraits<Packet> ST;
00552   MixMaxUtilities::Min<ST::isComparable,Ordinal,Packet>::min(
00553     count,inBuffer,inoutBuffer
00554     );
00555 }
00556 
00557 template<typename Ordinal, typename Packet>
00558 void MaxValueReductionOp<Ordinal,Packet>::reduce(
00559   const Ordinal     count
00560   ,const Packet     inBuffer[]
00561   ,Packet           inoutBuffer[]
00562   ) const
00563 {
00564   typedef ScalarTraits<Packet> ST;
00565   MixMaxUtilities::Max<ST::isComparable,Ordinal,Packet>::max(
00566     count,inBuffer,inoutBuffer
00567     );
00568 }
00569 
00570 template<typename Ordinal, typename Packet>
00571 void ANDValueReductionOp<Ordinal,Packet>::reduce(
00572   const Ordinal     count
00573   ,const Packet     inBuffer[]
00574   ,Packet           inoutBuffer[]
00575   ) const
00576 {
00577   typedef ScalarTraits<Packet> ST;
00578   MixMaxUtilities::AND<ST::isComparable,Ordinal,Packet>::And(
00579     count,inBuffer,inoutBuffer
00580     );
00581 }
00582 
00583 } // namespace Teuchos
00584 
00585 // //////////////////////////
00586 // Template implemenations
00587 
00588 //
00589 // ReductionOp utilities
00590 //
00591 
00592 namespace Teuchos {
00593 
00594 
00595 // Not for the general user to use!  I am returning a raw ReducionOp* pointer
00596 // to avoid the overhead of using RefCountPtr.  However, given the use case
00597 // this is just fine since I can just use std::auto_ptr to make sure things
00598 // are deleted correctly.
00599 template<typename Ordinal, typename Packet>
00600 ValueTypeReductionOp<Ordinal,Packet>* createOp( const EReductionType reductType )
00601 {
00602   typedef ScalarTraits<Packet> ST;
00603   switch(reductType) {
00604     case REDUCE_SUM: {
00605       return new SumValueReductionOp<Ordinal,Packet>();
00606       break;
00607     }
00608     case REDUCE_MIN: {
00609       TEST_FOR_EXCEPT(!ST::isComparable);
00610       return new MinValueReductionOp<Ordinal,Packet>();
00611       break;
00612     }
00613     case REDUCE_MAX: {
00614       TEST_FOR_EXCEPT(!ST::isComparable);
00615       return new MaxValueReductionOp<Ordinal,Packet>();
00616       break;
00617     }
00618     case REDUCE_AND: {
00619       return new ANDValueReductionOp<Ordinal, Packet>();
00620       break;
00621     }
00622     default:
00623       TEST_FOR_EXCEPT(true);
00624   }
00625   return 0; // Will never be called!
00626 }
00627 
00628 } // namespace Teuchos
00629 
00630 //
00631 // Teuchos::Comm wrapper functions
00632 //
00633 
00634 template<typename Ordinal>
00635 int Teuchos::rank(const Comm<Ordinal>& comm)
00636 {
00637   return comm.getRank();
00638 }
00639 
00640 template<typename Ordinal>
00641 int Teuchos::size(const Comm<Ordinal>& comm)
00642 {
00643   return comm.getSize();
00644 }
00645 
00646 template<typename Ordinal>
00647 void Teuchos::barrier(const Comm<Ordinal>& comm)
00648 {
00649   TEUCHOS_COMM_TIME_MONITOR(
00650     "Teuchos::CommHelpers: barrier<"
00651     <<OrdinalTraits<Ordinal>::name()
00652     <<">()"
00653     );
00654   comm.barrier();
00655 }
00656 
00657 template<typename Ordinal, typename Packet>
00658 void Teuchos::broadcast(
00659   const Comm<Ordinal>& comm
00660   ,const int rootRank, const Ordinal count, Packet buffer[]
00661   )
00662 {
00663   TEUCHOS_COMM_TIME_MONITOR(
00664     "Teuchos::CommHelpers: broadcast<"
00665     <<OrdinalTraits<Ordinal>::name()<<","<<ScalarTraits<Packet>::name()
00666     <<">( value type )"
00667     );
00668   ValueTypeSerializationBuffer<Ordinal,Packet>
00669     charBuffer(count,buffer);
00670   comm.broadcast(
00671     rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
00672     );
00673 }
00674 
00675 template<typename Ordinal, typename Packet>
00676 void Teuchos::broadcast(
00677   const Comm<Ordinal>& comm
00678   ,const int rootRank, Packet *object
00679   )
00680 {
00681   broadcast(comm,rootRank,1,object);
00682 }
00683 
00684 template<typename Ordinal, typename Packet>
00685 void Teuchos::broadcast(
00686   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer
00687   ,const int rootRank, const Ordinal count, Packet*const buffer[]
00688   )
00689 {
00690   TEUCHOS_COMM_TIME_MONITOR(
00691     "Teuchos::CommHelpers: broadcast<"
00692     <<OrdinalTraits<Ordinal>::name()<<","<<typeid(Packet).name()
00693     <<">( reference type )"
00694     );
00695   ReferenceTypeSerializationBuffer<Ordinal,Packet>
00696     charBuffer(serializer,count,buffer);
00697   comm.broadcast(
00698     rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
00699     );
00700 }
00701 
00702 template<typename Ordinal, typename Packet>
00703 void Teuchos::gatherAll(
00704   const Comm<Ordinal>& comm
00705   ,const Ordinal sendCount, const Packet sendBuffer[]
00706   ,const Ordinal recvCount, Packet recvBuffer[]
00707   )
00708 {
00709   TEUCHOS_COMM_TIME_MONITOR(
00710     "Teuchos::CommHelpers: gatherAll<"
00711     <<OrdinalTraits<Ordinal>::name()<<","<<ScalarTraits<Packet>::name()
00712     <<">( value type )"
00713     );
00714   ConstValueTypeSerializationBuffer<Ordinal,Packet>
00715     charSendBuffer(sendCount,sendBuffer);
00716   ValueTypeSerializationBuffer<Ordinal,Packet>
00717     charRecvBuffer(recvCount,recvBuffer);
00718   comm.gatherAll(
00719     charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
00720     ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
00721     );
00722 }
00723 
00724 template<typename Ordinal, typename Packet>
00725 void Teuchos::gatherAll(
00726   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer
00727   ,const Ordinal sendCount, const Packet*const sendBuffer[]
00728   ,const Ordinal recvCount, Packet*const recvBuffer[]
00729   )
00730 {
00731   TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
00732 }
00733 
00734 template<typename Ordinal, typename Packet>
00735 void Teuchos::reduceAll(
00736   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp
00737   ,const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
00738   )
00739 {
00740   TEUCHOS_COMM_TIME_MONITOR(
00741     "Teuchos::CommHelpers: reduceAll<"
00742     <<OrdinalTraits<Ordinal>::name()<<","<<ScalarTraits<Packet>::name()
00743     <<">( value type, user-defined op )"
00744     );
00745   ConstValueTypeSerializationBuffer<Ordinal,Packet>
00746     charSendBuffer(count,sendBuffer);
00747   ValueTypeSerializationBuffer<Ordinal,Packet>
00748     charGlobalReducts(count,globalReducts);
00749   CharToValueTypeReductionOp<Ordinal,Packet>
00750     _reductOp(rcp(&reductOp,false));
00751   comm.reduceAll(
00752     _reductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
00753     ,charGlobalReducts.getCharBuffer()
00754     );
00755 }
00756 
00757 template<typename Ordinal, typename Packet>
00758 void Teuchos::reduceAll(
00759   const Comm<Ordinal>& comm, const EReductionType reductType
00760   ,const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
00761   )
00762 {
00763   TEUCHOS_COMM_TIME_MONITOR(
00764     "Teuchos::CommHelpers: reduceAll<"
00765     <<OrdinalTraits<Ordinal>::name()<<","<<ScalarTraits<Packet>::name()
00766     <<">( value type, "<<toString(reductType)<<" )"
00767     );
00768   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
00769     reductOp(createOp<Ordinal,Packet>(reductType));
00770   reduceAll(comm,*reductOp,count,sendBuffer,globalReducts);
00771 }
00772 
00773 template<typename Ordinal, typename Packet>
00774 void Teuchos::reduceAll(
00775   const Comm<Ordinal>& comm, const EReductionType reductType
00776   ,const Packet &send, Packet *globalReduct
00777   )
00778 {
00779   reduceAll(comm,reductType,1,&send,globalReduct);
00780 }
00781 
00782 template<typename Ordinal, typename Packet>
00783 void Teuchos::reduceAll(
00784   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer
00785   ,const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp
00786   ,const Ordinal count, const Packet*const sendBuffer[], Packet*const globalReducts[]
00787   )
00788 {
00789   TEUCHOS_COMM_TIME_MONITOR(
00790     "Teuchos::CommHelpers: reduceAll<"
00791     <<OrdinalTraits<Ordinal>::name()<<","<<typeid(Packet).name()
00792     <<">( reference type )"
00793     );
00794   ConstReferenceTypeSerializationBuffer<Ordinal,Packet>
00795     charSendBuffer(serializer,count,sendBuffer);
00796   ReferenceTypeSerializationBuffer<Ordinal,Packet>
00797     charGlobalReducts(serializer,count,globalReducts);
00798   CharToReferenceTypeReductionOp<Ordinal,Packet>
00799     _reductOp(rcp(&serializer,false),rcp(&reductOp,false));
00800   comm.reduceAll(
00801     _reductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
00802     ,charGlobalReducts.getCharBuffer()
00803     );
00804 }
00805 
00806 template<typename Ordinal, typename Packet>
00807 void Teuchos::reduceAllAndScatter(
00808   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp
00809   ,const Ordinal sendCount, const Packet sendBuffer[] 
00810   ,const Ordinal recvCounts[], Packet myGlobalReducts[]
00811   )
00812 {
00813   TEUCHOS_COMM_TIME_MONITOR(
00814     "Teuchos::CommHelpers: reduceAllAndScatter<"
00815     <<OrdinalTraits<Ordinal>::name()<<","<<ScalarTraits<Packet>::name()
00816     <<">( value type, user-defined op )"
00817     );
00818 #ifdef TEUCHOS_DEBUG
00819   Ordinal sumRecvCounts = 0;
00820   const int size = Teuchos::size(comm);
00821   for( Ordinal i = 0; i < size; ++i )
00822     sumRecvCounts += recvCounts[i];
00823   TEST_FOR_EXCEPT(!(sumRecvCounts==sendCount));
00824 #endif
00825   const int rank = Teuchos::rank(comm);
00826   ConstValueTypeSerializationBuffer<Ordinal,Packet>
00827     charSendBuffer(sendCount,sendBuffer);
00828   ValueTypeSerializationBuffer<Ordinal,Packet>
00829     charMyGlobalReducts(recvCounts[rank],myGlobalReducts);
00830   CharToValueTypeReductionOp<Ordinal,Packet>
00831     _reductOp(rcp(&reductOp,false));
00832   const Ordinal
00833     blockSize = charSendBuffer.getBytes()/sendCount;
00834   comm.reduceAllAndScatter(
00835     _reductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
00836     ,recvCounts,blockSize,charMyGlobalReducts.getCharBuffer()
00837     );
00838 }
00839 
00840 template<typename Ordinal, typename Packet>
00841 void Teuchos::reduceAllAndScatter(
00842   const Comm<Ordinal>& comm, const EReductionType reductType
00843   ,const Ordinal sendCount, const Packet sendBuffer[] 
00844   ,const Ordinal recvCounts[], Packet myGlobalReducts[]
00845   )
00846 {
00847   TEUCHOS_COMM_TIME_MONITOR(
00848     "Teuchos::CommHelpers: reduceAllAndScatter<"
00849     <<OrdinalTraits<Ordinal>::name()<<","<<ScalarTraits<Packet>::name()
00850     <<">( value type, "<<toString(reductType)<<" )"
00851     );
00852   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
00853     reductOp(createOp<Ordinal,Packet>(reductType));
00854   reduceAllAndScatter(
00855     comm,*reductOp,sendCount,sendBuffer,recvCounts,myGlobalReducts
00856     );
00857 }
00858 
00859 template<typename Ordinal, typename Packet>
00860 void Teuchos::reduceAllAndScatter(
00861   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer
00862   ,const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp
00863   ,const Ordinal sendCount, const Packet*const sendBuffer[] 
00864   ,const Ordinal recvCounts[], Packet*const myGlobalReducts[]
00865   )
00866 {
00867   TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
00868 }
00869 
00870 template<typename Ordinal, typename Packet>
00871 void Teuchos::scan(
00872   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp
00873   ,const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
00874   )
00875 {
00876   TEUCHOS_COMM_TIME_MONITOR(
00877     "Teuchos::CommHelpers: scan<"
00878     <<OrdinalTraits<Ordinal>::name()<<","<<ScalarTraits<Packet>::name()
00879     <<">( value type, user-defined op )"
00880     );
00881   ConstValueTypeSerializationBuffer<Ordinal,Packet>
00882     charSendBuffer(count,sendBuffer);
00883   ValueTypeSerializationBuffer<Ordinal,Packet>
00884     charScanReducts(count,scanReducts);
00885   CharToValueTypeReductionOp<Ordinal,Packet>
00886     _reductOp(rcp(&reductOp,false));
00887   comm.scan(
00888     _reductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
00889     ,charScanReducts.getCharBuffer()
00890     );
00891 }
00892 
00893 template<typename Ordinal, typename Packet>
00894 void Teuchos::scan(
00895   const Comm<Ordinal>& comm, const EReductionType reductType
00896   ,const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
00897   )
00898 {
00899   TEUCHOS_COMM_TIME_MONITOR(
00900     "Teuchos::CommHelpers: scan<"
00901     <<OrdinalTraits<Ordinal>::name()<<","<<ScalarTraits<Packet>::name()
00902     <<">( value type, "<<toString(reductType)<<" )"
00903     );
00904   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
00905     reductOp(createOp<Ordinal,Packet>(reductType));
00906   scan(comm,*reductOp,count,sendBuffer,scanReducts);
00907 }
00908 
00909 template<typename Ordinal, typename Packet>
00910 void Teuchos::scan(
00911   const Comm<Ordinal>& comm, const EReductionType reductType
00912   ,const Packet &send, Packet *globalReduct
00913   )
00914 {
00915   scan(comm,reductType,1,&send,globalReduct);
00916 }
00917 
00918 template<typename Ordinal, typename Packet>
00919 void Teuchos::scan(
00920   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer
00921   ,const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp
00922   ,const Ordinal count, const Packet*const sendBuffer[], Packet*const scanReducts[]
00923   )
00924 {
00925   TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
00926 }
00927 
00928 template<typename Ordinal, typename Packet>
00929 void Teuchos::send(
00930   const Comm<Ordinal>& comm
00931   ,const Ordinal count, const Packet sendBuffer[], const int destRank
00932   )
00933 {
00934   TEUCHOS_COMM_TIME_MONITOR(
00935     "Teuchos::CommHelpers: send<"
00936     <<OrdinalTraits<Ordinal>::name()<<","<<ScalarTraits<Packet>::name()
00937     <<">( value type )"
00938     );
00939   ConstValueTypeSerializationBuffer<Ordinal,Packet>
00940     charSendBuffer(count,sendBuffer);
00941   comm.send(
00942     charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
00943     ,destRank
00944     );
00945 }
00946 
00947 template<typename Ordinal, typename Packet>
00948 void Teuchos::send(
00949   const Comm<Ordinal>& comm
00950   ,const Packet &send, const int destRank
00951   )
00952 {
00953   Teuchos::send<Ordinal,Packet>(comm,1,&send,destRank);
00954 }
00955 
00956 template<typename Ordinal, typename Packet>
00957 void Teuchos::send(
00958   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer
00959   ,const Ordinal count, const Packet*const sendBuffer[], const int destRank
00960   )
00961 {
00962   TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
00963 }
00964 
00965 template<typename Ordinal, typename Packet>
00966 int Teuchos::receive(
00967   const Comm<Ordinal>& comm
00968   ,const int sourceRank, const Ordinal count, Packet recvBuffer[] 
00969   )
00970 {
00971   TEUCHOS_COMM_TIME_MONITOR(
00972     "Teuchos::CommHelpers: receive<"
00973     <<OrdinalTraits<Ordinal>::name()<<","<<ScalarTraits<Packet>::name()
00974     <<">( value type )"
00975     );
00976   ValueTypeSerializationBuffer<Ordinal,Packet>
00977     charRecvBuffer(count,recvBuffer);
00978   return comm.receive(
00979     sourceRank
00980     ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
00981     );
00982 }
00983 
00984 template<typename Ordinal, typename Packet>
00985 int Teuchos::receive(
00986   const Comm<Ordinal>& comm
00987   ,const int sourceRank, Packet *recv 
00988   )
00989 {
00990   return Teuchos::receive<Ordinal,Packet>(comm,sourceRank,1,recv);
00991 }
00992 
00993 template<typename Ordinal, typename Packet>
00994 int Teuchos::receive(
00995   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer
00996   ,const int sourceRank, const Ordinal count, Packet*const recvBuffer[] 
00997   )
00998 {
00999   TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01000 }
01001 
01002 #endif // TEUCHOS_COMM_HELPERS_HPP

Generated on Thu Sep 18 12:30:29 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1