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

Generated on Tue Oct 20 12:45:25 2009 for Teuchos - Trilinos Tools Package by doxygen 1.4.7