Teuchos - Trilinos Tools Package Version of the Day
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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #ifndef TEUCHOS_COMM_HELPERS_HPP
00043 #define TEUCHOS_COMM_HELPERS_HPP
00044 
00045 #include "Teuchos_Comm.hpp"
00046 #include "Teuchos_CommUtilities.hpp"
00047 #include "Teuchos_SerializationTraitsHelpers.hpp"
00048 #include "Teuchos_ReductionOpHelpers.hpp"
00049 #include "Teuchos_SerializerHelpers.hpp"
00050 #include "Teuchos_ScalarTraits.hpp"
00051 #include "Teuchos_OrdinalTraits.hpp"
00052 #include "Teuchos_Array.hpp"
00053 #include "Teuchos_TypeNameTraits.hpp"
00054 #include "Teuchos_Workspace.hpp"
00055 #include "Teuchos_as.hpp"
00056 
00057 
00058 namespace Teuchos {
00059 
00060 
00061 //
00062 // Teuchos::Comm Helper Functions
00063 //
00064 
00070 enum EReductionType {
00071   REDUCE_SUM, 
00072   REDUCE_MIN, 
00073   REDUCE_MAX, 
00074   REDUCE_AND 
00075 };
00076 
00081 inline
00082 const char* toString( const EReductionType reductType )
00083 {
00084   switch(reductType) {
00085     case REDUCE_SUM: return "REDUCE_SUM";
00086     case REDUCE_MIN: return "REDUCE_MIN";
00087     case REDUCE_MAX: return "REDUCE_MAX";
00088     case REDUCE_AND: return "REDUCE_AND";
00089     default: TEUCHOS_TEST_FOR_EXCEPT(true);
00090   }
00091   return 0; // Will never be called
00092 }
00093 
00098 template<typename Ordinal>
00099 int rank(const Comm<Ordinal>& comm);
00100 
00105 template<typename Ordinal>
00106 int size(const Comm<Ordinal>& comm);
00107 
00112 template<typename Ordinal>
00113 void barrier(const Comm<Ordinal>& comm);
00114 
00119 template<typename Ordinal, typename Packet>
00120 void broadcast(
00121   const Comm<Ordinal>& comm,
00122   const int rootRank,
00123   const Ordinal count, Packet buffer[]
00124   );
00125 
00130 template<typename Ordinal, typename Packet>
00131 void broadcast(
00132   const Comm<Ordinal>& comm,
00133   const int rootRank,
00134   const ArrayView<Packet> &buffer
00135   );
00136 
00141 template<typename Ordinal, typename Packet>
00142 void broadcast(
00143   const Comm<Ordinal>& comm,
00144   const int rootRank, Packet *object
00145   );
00146 
00151 template<typename Ordinal, typename Packet>
00152 void broadcast(
00153   const Comm<Ordinal>& comm,
00154   const int rootRank, const Ptr<Packet> &object
00155   );
00156 
00161 template<typename Ordinal, typename Packet>
00162 void broadcast(
00163   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00164   const int rootRank, const Ordinal count, Packet*const buffer[]
00165   );
00166 
00171 template<typename Ordinal, typename Packet>
00172 void broadcast(
00173   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00174   const int rootRank, const ArrayView<const Ptr<Packet> > &buffer
00175   );
00176 
00182 template<typename Ordinal, typename Packet, typename Serializer>
00183 void broadcast(
00184   const Comm<Ordinal>& comm,
00185   const Serializer& serializer,
00186   const int rootRank,
00187   const Ordinal count, Packet buffer[]
00188   );
00189 
00195 template<typename Ordinal, typename Packet>
00196 void gatherAll(
00197   const Comm<Ordinal>& comm,
00198   const Ordinal sendCount, const Packet sendBuffer[],
00199   const Ordinal recvCount, Packet recvBuffer[]
00200   );
00201 
00207 template<typename Ordinal, typename Packet>
00208 void gatherAll(
00209   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00210   const Ordinal sendCount, const Packet*const sendBuffer[],
00211   const Ordinal recvCount, Packet*const recvBuffer[]
00212   );
00213 
00219 template<typename Ordinal, typename Packet, typename Serializer>
00220 void gatherAll(
00221   const Comm<Ordinal>& comm,
00222   const Serializer& serializer,
00223   const Ordinal sendCount, const Packet sendBuffer[],
00224   const Ordinal recvCount, Packet recvBuffer[]
00225   );
00226 
00232 template<typename Ordinal, typename Packet>
00233 void reduceAll(
00234   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
00235   const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
00236   );
00237 
00243 template<typename Ordinal, typename Packet>
00244 void reduceAll(
00245   const Comm<Ordinal>& comm, const EReductionType reductType,
00246   const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
00247   );
00248 
00254 template<typename Ordinal, typename Packet>
00255 void reduceAll(
00256   const Comm<Ordinal>& comm, const EReductionType reductType,
00257   const Packet &send, const Ptr<Packet> &globalReduct
00258   );
00259 
00261 template<typename Ordinal, typename Packet>
00262 TEUCHOS_DEPRECATED void reduceAll(
00263   const Comm<Ordinal>& comm, const EReductionType reductType,
00264   const Packet &send, Packet *globalReduct
00265   )
00266 {
00267   reduceAll<Ordinal,Packet>(comm, reductType, send, ptr(globalReduct));
00268 }
00269 
00275 template<typename Ordinal, typename Packet>
00276 void reduceAll(
00277   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00278   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
00279   const Ordinal count, const Packet*const sendBuffer[], Packet*const globalReducts[]
00280   );
00281 
00287 template<typename Ordinal, typename Packet, typename Serializer>
00288 void reduceAll(
00289   const Comm<Ordinal>& comm, 
00290   const Serializer& serializer,
00291   const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
00292   const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
00293   );
00294 
00300 template<typename Ordinal, typename Packet, typename Serializer>
00301 void reduceAll(
00302   const Comm<Ordinal>& comm, 
00303   const Serializer& serializer,
00304   const EReductionType reductType,
00305   const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
00306   );
00307 
00313 template<typename Ordinal, typename Packet>
00314 void reduceAllAndScatter(
00315   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
00316   const Ordinal sendCount, const Packet sendBuffer[] ,
00317   const Ordinal recvCounts[], Packet myGlobalReducts[]
00318   );
00319 
00325 template<typename Ordinal, typename Packet>
00326 void reduceAllAndScatter(
00327   const Comm<Ordinal>& comm, const EReductionType reductType,
00328   const Ordinal sendCount, const Packet sendBuffer[] ,
00329   const Ordinal recvCounts[], Packet myGlobalReducts[]
00330   );
00331 
00337 template<typename Ordinal, typename Packet>
00338 void reduceAllAndScatter(
00339   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00340   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
00341   const Ordinal sendCount, const Packet*const sendBuffer[] ,
00342   const Ordinal recvCounts[], Packet*const myGlobalReducts[]
00343   );
00344 
00350 template<typename Ordinal, typename Packet>
00351 void scan(
00352   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
00353   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
00354   );
00355 
00361 template<typename Ordinal, typename Packet, typename Serializer>
00362 void reduceAllAndScatter(
00363   const Comm<Ordinal>& comm, 
00364   const Serializer& serializer,
00365   const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
00366   const Ordinal sendCount, const Packet sendBuffer[],
00367   const Ordinal recvCounts[], Packet myGlobalReducts[]
00368   );
00369 
00375 template<typename Ordinal, typename Packet, typename Serializer>
00376 void reduceAllAndScatter(
00377   const Comm<Ordinal>& comm, 
00378   const Serializer& serializer,
00379   const EReductionType reductType,
00380   const Ordinal sendCount, const Packet sendBuffer[] ,
00381   const Ordinal recvCounts[], Packet myGlobalReducts[]
00382   );
00383 
00389 template<typename Ordinal, typename Packet>
00390 void scan(
00391   const Comm<Ordinal>& comm, const EReductionType reductType,
00392   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
00393   );
00394 
00400 template<typename Ordinal, typename Packet>
00401 void scan(
00402   const Comm<Ordinal>& comm, const EReductionType reductType,
00403   const Packet &send, const Ptr<Packet> &scanReduct
00404   );
00405 
00407 template<typename Ordinal, typename Packet>
00408 TEUCHOS_DEPRECATED void scan(
00409   const Comm<Ordinal>& comm, const EReductionType reductType,
00410   const Packet &send, Packet *scanReduct
00411   )
00412 {
00413   scan(comm, reductType, send, ptr(scanReduct));
00414 }
00415 
00421 template<typename Ordinal, typename Packet>
00422 void scan(
00423   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00424   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
00425   const Ordinal count, const Packet*const sendBuffer[], Packet*const scanReducts[]
00426   );
00427 
00433 template<typename Ordinal, typename Packet, typename Serializer>
00434 void scan(
00435   const Comm<Ordinal>& comm, 
00436   const Serializer& serializer,
00437   const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
00438   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
00439   );
00440 
00446 template<typename Ordinal, typename Packet, typename Serializer>
00447 void scan(
00448   const Comm<Ordinal>& comm, 
00449   const Serializer& serializer,
00450   const EReductionType reductType,
00451   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
00452   );
00453 
00458 template<typename Ordinal, typename Packet>
00459 void send(
00460   const Comm<Ordinal>& comm,
00461   const Ordinal count, const Packet sendBuffer[], const int destRank
00462   );
00463 
00468 template<typename Ordinal, typename Packet>
00469 void ssend(
00470   const Comm<Ordinal>& comm,
00471   const Ordinal count, const Packet sendBuffer[], const int destRank
00472   );
00473 
00478 template<typename Ordinal, typename Packet>
00479 void send(
00480   const Comm<Ordinal>& comm,
00481   const Packet &send, const int destRank
00482   );
00483 
00488 template<typename Ordinal, typename Packet>
00489 void ssend(
00490   const Comm<Ordinal>& comm,
00491   const Packet &send, const int destRank
00492   );
00493 
00500 template<typename Ordinal, typename Packet>
00501 void send(
00502   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00503   const Ordinal count, const Packet*const sendBuffer[], const int destRank
00504   );
00505 
00511 template<typename Ordinal, typename Packet, typename Serializer>
00512 void send(
00513   const Comm<Ordinal>& comm,
00514   const Serializer& serializer,
00515   const Ordinal count, const Packet sendBuffer[], const int destRank
00516   );
00517 
00522 template<typename Ordinal, typename Packet>
00523 int receive(
00524   const Comm<Ordinal>& comm,
00525   const int sourceRank, const Ordinal count, Packet recvBuffer[] 
00526   );
00527 
00532 template<typename Ordinal, typename Packet>
00533 int receive(
00534   const Comm<Ordinal>& comm,
00535   const int sourceRank, Packet *recv 
00536   );
00537 
00542 template<typename Ordinal, typename Packet>
00543 int receive(
00544   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00545   const int sourceRank, const Ordinal count, Packet*const recvBuffer[] 
00546   );
00547 
00553 template<typename Ordinal, typename Packet, typename Serializer>
00554 int receive(
00555   const Comm<Ordinal>& comm,
00556   const Serializer& serializer,
00557   const int sourceRank, const Ordinal count, Packet recvBuffer[] 
00558   );
00559 
00565 template<typename Ordinal, typename Packet>
00566 void readySend(
00567   const Comm<Ordinal>& comm,
00568   const ArrayView<const Packet> &sendBuffer,
00569   const int destRank
00570   );
00571 
00576 template<typename Ordinal, typename Packet>
00577 void readySend(
00578   const Comm<Ordinal>& comm,
00579   const Packet &send,
00580   const int destRank
00581   );
00582 
00588 template<typename Ordinal, typename Packet, typename Serializer>
00589 void readySend(
00590   const Comm<Ordinal>& comm,
00591   const Serializer& serializer,
00592   const ArrayView<const Packet> &sendBuffer,
00593   const int destRank
00594   );
00595 
00600 template<typename Ordinal, typename Packet>
00601 RCP<CommRequest> isend(
00602   const Comm<Ordinal>& comm,
00603   const ArrayRCP<const Packet> &sendBuffer,
00604   const int destRank
00605   );
00606 
00607 
00612 template<typename Ordinal, typename Packet>
00613 RCP<CommRequest> isend(
00614   const Comm<Ordinal>& comm,
00615   const RCP<const Packet> &send,
00616   const int destRank
00617   );
00618 
00624 template<typename Ordinal, typename Packet, typename Serializer>
00625 RCP<CommRequest> isend(
00626   const Comm<Ordinal>& comm,
00627   const Serializer& serializer,
00628   const ArrayRCP<const Packet> &sendBuffer,
00629   const int destRank
00630   );
00631 
00632 
00633 // 2008/07/29: rabartl: ToDo: Add reference semantics version of isend!
00634 
00635 
00640 template<typename Ordinal, typename Packet>
00641 RCP<CommRequest> ireceive(
00642   const Comm<Ordinal>& comm,
00643   const ArrayRCP<Packet> &recvBuffer,
00644   const int sourceRank
00645   );
00646 
00647 
00652 template<typename Ordinal, typename Packet>
00653 RCP<CommRequest> ireceive(
00654   const Comm<Ordinal>& comm,
00655   const RCP<Packet> &recv,
00656   const int sourceRank
00657   );
00658 
00664 template<typename Ordinal, typename Packet, typename Serializer>
00665 RCP<CommRequest> ireceive(
00666   const Comm<Ordinal>& comm,
00667   const Serializer& serializer,
00668   const ArrayRCP<Packet> &recvBuffer,
00669   const int sourceRank
00670   );
00671 
00672 
00673 // 2008/07/29: rabartl: ToDo: Add reference semantics version of ireceive!
00674 
00675 
00683 template<typename Ordinal>
00684 void waitAll(
00685   const Comm<Ordinal>& comm,
00686   const ArrayView<RCP<CommRequest> > &requests
00687   );
00688 
00710 template<typename Ordinal>
00711 void 
00712 waitAll (const Comm<Ordinal>& comm,
00713    const ArrayView<RCP<CommRequest> >& requests,
00714    const ArrayView<RCP<CommStatus<Ordinal> > >& statuses);
00715 
00740 template<typename Ordinal>
00741 RCP<CommStatus<Ordinal> >
00742 wait (const Comm<Ordinal>& comm, const Ptr<RCP<CommRequest> >& request);
00743 
00744 //
00745 // Standard reduction subclasses for objects that use value semantics
00746 //
00747 
00748 
00753 template<typename Ordinal, typename Packet>
00754 class SumValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00755 {
00756 public:
00758   void reduce(
00759     const Ordinal count,
00760     const Packet inBuffer[],
00761     Packet inoutBuffer[]
00762     ) const;
00763 };
00764 
00765 
00774 template<typename Ordinal, typename Packet>
00775 class MinValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00776 {
00777 public:
00779   void reduce(
00780     const Ordinal count,
00781     const Packet inBuffer[],
00782     Packet inoutBuffer[]
00783     ) const;
00784 };
00785 
00786 
00795 template<typename Ordinal, typename Packet>
00796 class MaxValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00797 {
00798 public:
00800   void reduce(
00801     const Ordinal count,
00802     const Packet inBuffer[],
00803     Packet inoutBuffer[]
00804     ) const;
00805 };
00806 
00807 
00812 template<typename Ordinal, typename Packet>
00813 class ANDValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00814 {
00815 public:
00817   void reduce(
00818     const Ordinal count,
00819     const Packet inBuffer[],
00820     Packet inoutBuffer[]
00821     ) const;
00822 };
00823 
00824 
00825 // ////////////////////////////////////////////////////////////
00826 // Implementation details (not for geneal users to mess with)
00827 
00828 
00829 //
00830 // ReductionOp Utilities
00831 //
00832 
00833 
00834 namespace MixMaxUtilities {
00835 
00836 
00837 template<bool isComparable, typename Ordinal, typename Packet>
00838 class Min {};
00839 
00840 
00841 template<typename Ordinal, typename Packet>
00842 class Min<true,Ordinal,Packet> {
00843 public:
00844   static void min(
00845     const Ordinal count,
00846     const Packet inBuffer[],
00847     Packet inoutBuffer[]
00848     )
00849     {
00850       for( int i = 0; i < count; ++i )
00851         inoutBuffer[i] = TEUCHOS_MIN(inoutBuffer[i],inBuffer[i]);
00852     }
00853 };
00854 
00855 
00856 template<typename Ordinal, typename Packet>
00857 class Min<false,Ordinal,Packet> {
00858 public:
00859   static void min(
00860     const Ordinal,
00861     const Packet[],
00862     Packet[]
00863     )
00864     {
00865       TEUCHOS_TEST_FOR_EXCEPTION(
00866         true,std::logic_error,
00867         "Error, the type "<<TypeNameTraits<Packet>::name()
00868         <<" does not support comparison operations!"
00869         );
00870     }
00871 };
00872 
00873 
00874 template<bool isComparable, typename Ordinal, typename Packet>
00875 class Max {};
00876 
00877 
00878 template<typename Ordinal, typename Packet>
00879 class Max<true,Ordinal,Packet> {
00880 public:
00881   static void max(
00882     const Ordinal count,
00883     const Packet inBuffer[],
00884     Packet inoutBuffer[]
00885     )
00886     {
00887       for( int i = 0; i < count; ++i )
00888         inoutBuffer[i] = TEUCHOS_MAX(inoutBuffer[i],inBuffer[i]);
00889     }
00890 };
00891 
00892 
00893 template<typename Ordinal, typename Packet>
00894 class Max<false,Ordinal,Packet> {
00895 public:
00896   static void max(
00897     const Ordinal,
00898     const Packet[],
00899     Packet[]
00900     )
00901     {
00902       TEUCHOS_TEST_FOR_EXCEPTION(
00903         true,std::logic_error,
00904         "Error, the type "<<TypeNameTraits<Packet>::name()
00905         <<" does not support comparison operations!"
00906         );
00907     }
00908 };
00909 
00910 
00911 template<bool isComparable, typename Ordinal, typename Packet>
00912 class AND {};
00913 
00914 
00915 template<typename Ordinal, typename Packet>
00916 class AND<true,Ordinal,Packet> {
00917 public:
00918   static void andOp(
00919     const Ordinal count,
00920     const Packet inBuffer[],
00921     Packet inoutBuffer[]
00922     )
00923     {
00924       for( int i = 0; i < count; ++i )
00925         inoutBuffer[i] = inoutBuffer[i] && inBuffer[i];
00926     }
00927 };
00928 
00929 
00930 template<typename Ordinal, typename Packet>
00931 class AND<false,Ordinal,Packet> {
00932 public:
00933   static void andOp(
00934     const Ordinal,
00935     const Packet[],
00936     Packet[]
00937     )
00938     {
00939       TEUCHOS_TEST_FOR_EXCEPTION(
00940         true,std::logic_error,
00941         "Error, the type "<<TypeNameTraits<Packet>::name()
00942         <<" does not support logical AND operations!"
00943         );
00944     }
00945 };
00946 
00947 
00948 } // namespace MixMaxUtilities
00949 
00950 
00951 template<typename Ordinal, typename Packet>
00952 void SumValueReductionOp<Ordinal,Packet>::reduce(
00953   const Ordinal count,
00954   const Packet inBuffer[],
00955   Packet inoutBuffer[]
00956   ) const
00957 {
00958   for( int i = 0; i < count; ++i )
00959     inoutBuffer[i] += inBuffer[i];
00960 }
00961 
00962 
00963 template<typename Ordinal, typename Packet>
00964 void MinValueReductionOp<Ordinal,Packet>::reduce(
00965   const Ordinal count,
00966   const Packet inBuffer[],
00967   Packet inoutBuffer[]
00968   ) const
00969 {
00970   typedef ScalarTraits<Packet> ST;
00971   MixMaxUtilities::Min<ST::isComparable,Ordinal,Packet>::min(
00972     count,inBuffer,inoutBuffer
00973     );
00974 }
00975 
00976 
00977 template<typename Ordinal, typename Packet>
00978 void MaxValueReductionOp<Ordinal,Packet>::reduce(
00979   const Ordinal count,
00980   const Packet inBuffer[],
00981   Packet inoutBuffer[]
00982   ) const
00983 {
00984   typedef ScalarTraits<Packet> ST;
00985   MixMaxUtilities::Max<ST::isComparable,Ordinal,Packet>::max(
00986     count,inBuffer,inoutBuffer
00987     );
00988 }
00989 
00990 
00991 template<typename Ordinal, typename Packet>
00992 void ANDValueReductionOp<Ordinal,Packet>::reduce(
00993   const Ordinal count,
00994   const Packet inBuffer[],
00995   Packet inoutBuffer[]
00996   ) const
00997 {
00998   typedef ScalarTraits<Packet> ST;
00999   MixMaxUtilities::AND<ST::isComparable,Ordinal,Packet>::andOp(
01000     count,inBuffer,inoutBuffer
01001     );
01002 }
01003 
01004 
01005 } // namespace Teuchos
01006 
01007 
01008 // //////////////////////////
01009 // Template implemenations
01010 
01011 
01012 //
01013 // ReductionOp utilities
01014 //
01015 
01016 
01017 namespace Teuchos {
01018 
01019 
01020 // Not for the general user to use! I am returning a raw ReducionOp* pointer
01021 // to avoid the overhead of using RCP. However, given the use case
01022 // this is just fine since I can just use std::auto_ptr to make sure things
01023 // are deleted correctly.
01024 template<typename Ordinal, typename Packet>
01025 ValueTypeReductionOp<Ordinal,Packet>*
01026 createOp( const EReductionType reductType )
01027 {
01028   typedef ScalarTraits<Packet> ST;
01029   switch(reductType) {
01030     case REDUCE_SUM: {
01031       return new SumValueReductionOp<Ordinal,Packet>();
01032       break;
01033     }
01034     case REDUCE_MIN: {
01035       TEUCHOS_TEST_FOR_EXCEPT(!ST::isComparable);
01036       return new MinValueReductionOp<Ordinal,Packet>();
01037       break;
01038     }
01039     case REDUCE_MAX: {
01040       TEUCHOS_TEST_FOR_EXCEPT(!ST::isComparable);
01041       return new MaxValueReductionOp<Ordinal,Packet>();
01042       break;
01043     }
01044     case REDUCE_AND: {
01045       return new ANDValueReductionOp<Ordinal, Packet>();
01046       break;
01047     }
01048     default:
01049       TEUCHOS_TEST_FOR_EXCEPT(true);
01050   }
01051   return 0; // Will never be called!
01052 }
01053 
01054 
01055 } // namespace Teuchos
01056 
01057 
01058 //
01059 // Teuchos::Comm wrapper functions
01060 //
01061 
01062 
01063 template<typename Ordinal>
01064 int Teuchos::rank(const Comm<Ordinal>& comm)
01065 {
01066   return comm.getRank();
01067 }
01068 
01069 
01070 template<typename Ordinal>
01071 int Teuchos::size(const Comm<Ordinal>& comm)
01072 {
01073   return comm.getSize();
01074 }
01075 
01076 
01077 template<typename Ordinal>
01078 void Teuchos::barrier(const Comm<Ordinal>& comm)
01079 {
01080   TEUCHOS_COMM_TIME_MONITOR(
01081     "Teuchos::CommHelpers: barrier<"
01082     <<OrdinalTraits<Ordinal>::name()
01083     <<">()"
01084     );
01085   comm.barrier();
01086 }
01087 
01088 
01089 template<typename Ordinal, typename Packet>
01090 void Teuchos::broadcast(
01091   const Comm<Ordinal>& comm,
01092   const int rootRank, const Ordinal count, Packet buffer[]
01093   )
01094 {
01095   TEUCHOS_COMM_TIME_MONITOR(
01096     "Teuchos::CommHelpers: broadcast<"
01097     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01098     <<">( value type )"
01099     );
01100   ValueTypeSerializationBuffer<Ordinal,Packet>
01101     charBuffer(count,buffer);
01102   comm.broadcast(
01103     rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
01104     );
01105 }
01106 
01107 
01108 template<typename Ordinal, typename Packet>
01109 void Teuchos::broadcast(
01110   const Comm<Ordinal>& comm,
01111   const int rootRank,
01112   const ArrayView<Packet> &buffer
01113   )
01114 {
01115   broadcast<Ordinal, Packet>(comm, rootRank, buffer.size(), buffer.getRawPtr() );
01116 }
01117 
01118 
01119 template<typename Ordinal, typename Packet>
01120 void Teuchos::broadcast(
01121   const Comm<Ordinal>& comm,
01122   const int rootRank, Packet *object
01123   )
01124 {
01125   broadcast<Ordinal,Packet>(comm,rootRank,1,object);
01126 }
01127 
01128 
01129 template<typename Ordinal, typename Packet>
01130 void Teuchos::broadcast(
01131   const Comm<Ordinal>& comm,
01132   const int rootRank, const Ptr<Packet> &object
01133   )
01134 {
01135   broadcast<Ordinal,Packet>(comm,rootRank,1,object.getRawPtr());
01136 }
01137 
01138 
01139 template<typename Ordinal, typename Packet>
01140 void Teuchos::broadcast(
01141   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01142   const int rootRank, const Ordinal count, Packet*const buffer[]
01143   )
01144 {
01145   TEUCHOS_COMM_TIME_MONITOR(
01146     "Teuchos::CommHelpers: broadcast<"
01147     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01148     <<">( reference type )"
01149     );
01150   ReferenceTypeSerializationBuffer<Ordinal,Packet>
01151     charBuffer(serializer, count, buffer);
01152   comm.broadcast(
01153     rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
01154     );
01155 }
01156 
01157 
01158 template<typename Ordinal, typename Packet>
01159 void Teuchos::broadcast(
01160   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01161   const int rootRank, const ArrayView<const Ptr<Packet> > &buffer
01162   )
01163 {
01164   Array<Packet*> bufferPtrArray;
01165   for (int i = 0; i < buffer.size(); ++i) {
01166     bufferPtrArray.push_back(buffer[i].getRawPtr());
01167   }
01168   broadcast<Ordinal,Packet>(comm, serializer, rootRank,
01169     buffer.size(), bufferPtrArray.getRawPtr());
01170 }
01171 
01172 template<typename Ordinal, typename Packet, typename Serializer>
01173 void Teuchos::broadcast(
01174   const Comm<Ordinal>& comm,
01175   const Serializer& serializer,
01176   const int rootRank, const Ordinal count, Packet buffer[]
01177   )
01178 {
01179   TEUCHOS_COMM_TIME_MONITOR(
01180     "Teuchos::CommHelpers: broadcast<"
01181     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01182     <<">( value type )"
01183     );
01184   ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01185     charBuffer(count,buffer,rcp(&serializer,false));
01186   comm.broadcast(
01187     rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
01188     );
01189 }
01190 
01191 
01192 template<typename Ordinal, typename Packet>
01193 void Teuchos::gatherAll(
01194   const Comm<Ordinal>& comm,
01195   const Ordinal sendCount, const Packet sendBuffer[],
01196   const Ordinal recvCount, Packet recvBuffer[]
01197   )
01198 {
01199   TEUCHOS_COMM_TIME_MONITOR(
01200     "Teuchos::CommHelpers: gatherAll<"
01201     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01202     <<">( value type )"
01203     );
01204   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01205     charSendBuffer(sendCount,sendBuffer);
01206   ValueTypeSerializationBuffer<Ordinal,Packet>
01207     charRecvBuffer(recvCount,recvBuffer);
01208   comm.gatherAll(
01209     charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01210     ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
01211     );
01212 }
01213 
01214 
01215 template<typename Ordinal, typename Packet>
01216 void Teuchos::gatherAll(
01217   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01218   const Ordinal sendCount, const Packet*const sendBuffer[],
01219   const Ordinal recvCount, Packet*const recvBuffer[]
01220   )
01221 {
01222   TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01223 }
01224 
01225 template<typename Ordinal, typename Packet, typename Serializer>
01226 void Teuchos::gatherAll(
01227   const Comm<Ordinal>& comm,
01228   const Serializer& serializer,
01229   const Ordinal sendCount, const Packet sendBuffer[],
01230   const Ordinal recvCount, Packet recvBuffer[]
01231   )
01232 {
01233   TEUCHOS_COMM_TIME_MONITOR(
01234     "Teuchos::CommHelpers: gatherAll<"
01235     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01236     <<">( value type )"
01237     );
01238   ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01239     charSendBuffer(sendCount,sendBuffer,rcp(&serializer,false));
01240   ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01241     charRecvBuffer(recvCount,recvBuffer,rcp(&serializer,false));
01242   comm.gatherAll(
01243     charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01244     ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
01245     );
01246 }
01247 
01248 
01249 template<typename Ordinal, typename Packet>
01250 void Teuchos::reduceAll(
01251   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp
01252   ,const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
01253   )
01254 {
01255   TEUCHOS_COMM_TIME_MONITOR(
01256     "Teuchos::CommHelpers: reduceAll<"
01257     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01258     <<">( value type, user-defined op )"
01259     );
01260   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01261     charSendBuffer(count,sendBuffer);
01262   ValueTypeSerializationBuffer<Ordinal,Packet>
01263     charGlobalReducts(count,globalReducts);
01264   CharToValueTypeReductionOp<Ordinal,Packet>
01265     charReductOp(rcp(&reductOp,false));
01266   comm.reduceAll(
01267     charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01268     ,charGlobalReducts.getCharBuffer()
01269     );
01270 }
01271 
01272 
01273 template<typename Ordinal, typename Packet>
01274 void Teuchos::reduceAll(
01275   const Comm<Ordinal>& comm, const EReductionType reductType,
01276   const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
01277   )
01278 {
01279   TEUCHOS_COMM_TIME_MONITOR(
01280     "Teuchos::CommHelpers: reduceAll<"
01281     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01282     <<">( value type, "<<toString(reductType)<<" )"
01283     );
01284   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01285     reductOp(createOp<Ordinal,Packet>(reductType));
01286   reduceAll(comm,*reductOp,count,sendBuffer,globalReducts);
01287 }
01288 
01289 
01290 template<typename Ordinal, typename Packet>
01291 void Teuchos::reduceAll(
01292   const Comm<Ordinal>& comm, const EReductionType reductType
01293   ,const Packet &send, const Ptr<Packet> &globalReduct
01294   )
01295 {
01296   reduceAll<Ordinal,Packet>(comm, reductType, 1, &send, &*globalReduct);
01297 }
01298 
01299 
01300 template<typename Ordinal, typename Packet>
01301 void Teuchos::reduceAll(
01302   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01303   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
01304   const Ordinal count, const Packet*const sendBuffer[], Packet*const globalReducts[]
01305   )
01306 {
01307   TEUCHOS_COMM_TIME_MONITOR(
01308     "Teuchos::CommHelpers: reduceAll<"
01309     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01310     <<">( reference type )"
01311     );
01312   ConstReferenceTypeSerializationBuffer<Ordinal,Packet>
01313     charSendBuffer(serializer,count,sendBuffer);
01314   ReferenceTypeSerializationBuffer<Ordinal,Packet>
01315     charGlobalReducts(serializer,count,globalReducts);
01316   CharToReferenceTypeReductionOp<Ordinal,Packet>
01317     charReductOp(rcp(&serializer,false),rcp(&reductOp,false));
01318   comm.reduceAll(
01319     charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01320     ,charGlobalReducts.getCharBuffer()
01321     );
01322 }
01323 
01324 template<typename Ordinal, typename Packet, typename Serializer>
01325 void Teuchos::reduceAll(
01326   const Comm<Ordinal>& comm, 
01327   const Serializer& serializer,
01328   const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
01329   const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
01330   )
01331 {
01332   TEUCHOS_COMM_TIME_MONITOR(
01333     "Teuchos::CommHelpers: reduceAll<"
01334     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01335     <<">( value type, user-defined op )"
01336     );
01337   ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01338     charSendBuffer(count,sendBuffer,rcp(&serializer,false));
01339   ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01340     charGlobalReducts(count,globalReducts,rcp(&serializer,false));
01341   CharToValueTypeReductionOp<Ordinal,Packet,Serializer>
01342     charReductOp(rcp(&reductOp,false),rcp(&serializer,false));
01343   comm.reduceAll(
01344     charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01345     ,charGlobalReducts.getCharBuffer()
01346     );
01347 }
01348 
01349 
01350 template<typename Ordinal, typename Packet, typename Serializer>
01351 void Teuchos::reduceAll(
01352   const Comm<Ordinal>& comm, 
01353   const Serializer& serializer,
01354   const EReductionType reductType,
01355   const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
01356   )
01357 {
01358   TEUCHOS_COMM_TIME_MONITOR(
01359     "Teuchos::CommHelpers: reduceAll<"
01360     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01361     <<">( value type, "<<toString(reductType)<<" )"
01362     );
01363   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01364     reductOp(createOp<Ordinal,Packet>(reductType));
01365   reduceAll(comm,serializer,*reductOp,count,sendBuffer,globalReducts);
01366 }
01367 
01368 
01369 template<typename Ordinal, typename Packet>
01370 void Teuchos::reduceAllAndScatter(
01371   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
01372   const Ordinal sendCount, const Packet sendBuffer[] ,
01373   const Ordinal recvCounts[], Packet myGlobalReducts[]
01374   )
01375 {
01376 
01377   TEUCHOS_COMM_TIME_MONITOR(
01378     "Teuchos::CommHelpers: reduceAllAndScatter<"
01379     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01380     <<">( value type, user-defined op )"
01381     );
01382 
01383   const Ordinal size = Teuchos::size(comm);
01384   const Ordinal rank = Teuchos::rank(comm);
01385 
01386 #ifdef TEUCHOS_DEBUG
01387   Ordinal sumRecvCounts = 0;
01388   for( Ordinal i = 0; i < size; ++i )
01389     sumRecvCounts += recvCounts[static_cast<ptrdiff_t>(i)];
01390   TEUCHOS_TEST_FOR_EXCEPT(!(sumRecvCounts==sendCount));
01391 #endif
01392 
01393   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01394     charSendBuffer(sendCount,sendBuffer);
01395   ValueTypeSerializationBuffer<Ordinal,Packet>
01396     charMyGlobalReducts(recvCounts[static_cast<ptrdiff_t>(rank)], myGlobalReducts);
01397   CharToValueTypeReductionOp<Ordinal,Packet>
01398     charReductOp(rcp(&reductOp,false));
01399 
01400   const Ordinal
01401     packetSize = charSendBuffer.getBytes()/sendCount;
01402 
01403   WorkspaceStore* wss = get_default_workspace_store().get();
01404   Workspace<Ordinal> charRecvCounts(wss, size);
01405   for (Ordinal k = 0; k < size; ++k) {
01406     charRecvCounts[k] = as<Ordinal>(recvCounts[static_cast<ptrdiff_t>(k)] * packetSize);
01407   }
01408  
01409   comm.reduceAllAndScatter(
01410     charReductOp, charSendBuffer.getBytes(), charSendBuffer.getCharBuffer(),
01411     &charRecvCounts[0], charMyGlobalReducts.getCharBuffer()
01412     );
01413 
01414 }
01415 
01416 
01417 template<typename Ordinal, typename Packet>
01418 void Teuchos::reduceAllAndScatter(
01419   const Comm<Ordinal>& comm, const EReductionType reductType,
01420   const Ordinal sendCount, const Packet sendBuffer[] ,
01421   const Ordinal recvCounts[], Packet myGlobalReducts[]
01422   )
01423 {
01424   TEUCHOS_COMM_TIME_MONITOR(
01425     "Teuchos::CommHelpers: reduceAllAndScatter<"
01426     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01427     <<">( value type, "<<toString(reductType)<<" )"
01428     );
01429   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01430     reductOp(createOp<Ordinal,Packet>(reductType));
01431   reduceAllAndScatter(
01432     comm, *reductOp, sendCount, sendBuffer, recvCounts, myGlobalReducts
01433     );
01434 }
01435 
01436 
01437 template<typename Ordinal, typename Packet>
01438 void Teuchos::reduceAllAndScatter(
01439   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01440   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
01441   const Ordinal sendCount, const Packet*const sendBuffer[],
01442   const Ordinal recvCounts[], Packet*const myGlobalReducts[]
01443   )
01444 {
01445   TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01446 }
01447 
01448 template<typename Ordinal, typename Packet, typename Serializer>
01449 void Teuchos::reduceAllAndScatter(
01450   const Comm<Ordinal>& comm, 
01451   const Serializer& serializer,
01452   const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
01453   const Ordinal sendCount, const Packet sendBuffer[] ,
01454   const Ordinal recvCounts[], Packet myGlobalReducts[]
01455   )
01456 {
01457 
01458   TEUCHOS_COMM_TIME_MONITOR(
01459     "Teuchos::CommHelpers: reduceAllAndScatter<"
01460     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01461     <<">( value type, user-defined op )"
01462     );
01463 
01464   const Ordinal size = Teuchos::size(comm);
01465   const Ordinal rank = Teuchos::rank(comm);
01466 
01467 #ifdef TEUCHOS_DEBUG
01468   Ordinal sumRecvCounts = 0;
01469   for( Ordinal i = 0; i < size; ++i )
01470     sumRecvCounts += recvCounts[static_cast<ptrdiff_t>(i)];
01471   TEUCHOS_TEST_FOR_EXCEPT(!(sumRecvCounts==sendCount));
01472 #endif
01473 
01474   ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01475     charSendBuffer(sendCount,sendBuffer,rcp(&serializer,false));
01476   ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01477     charMyGlobalReducts(recvCounts[static_cast<ptrdiff_t>(rank)], myGlobalReducts,rcp(&serializer,false));
01478   CharToValueTypeReductionOp<Ordinal,Packet,Serializer>
01479     charReductOp(rcp(&reductOp,false),rcp(&serializer,false));
01480 
01481   const Ordinal
01482     packetSize = charSendBuffer.getBytes()/sendCount;
01483 
01484   WorkspaceStore* wss = get_default_workspace_store().get();
01485   Workspace<Ordinal> charRecvCounts(wss, size);
01486   for (Ordinal k = 0; k < size; ++k) {
01487     charRecvCounts[k] = as<Ordinal>(recvCounts[static_cast<ptrdiff_t>(k)] * packetSize);
01488   }
01489  
01490   comm.reduceAllAndScatter(
01491     charReductOp, charSendBuffer.getBytes(), charSendBuffer.getCharBuffer(),
01492     &charRecvCounts[0], charMyGlobalReducts.getCharBuffer()
01493     );
01494 
01495 }
01496 
01497 template<typename Ordinal, typename Packet, typename Serializer>
01498 void Teuchos::reduceAllAndScatter(
01499   const Comm<Ordinal>& comm, 
01500   const Serializer& serializer,
01501   const EReductionType reductType,
01502   const Ordinal sendCount, const Packet sendBuffer[] ,
01503   const Ordinal recvCounts[], Packet myGlobalReducts[]
01504   )
01505 {
01506   TEUCHOS_COMM_TIME_MONITOR(
01507     "Teuchos::CommHelpers: reduceAllAndScatter<"
01508     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01509     <<">( value type, "<<toString(reductType)<<" )"
01510     );
01511   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01512     reductOp(createOp<Ordinal,Packet>(reductType));
01513   reduceAllAndScatter(
01514     comm, serializer, *reductOp, sendCount, sendBuffer, recvCounts, 
01515     myGlobalReducts
01516     );
01517 }
01518 
01519 
01520 template<typename Ordinal, typename Packet>
01521 void Teuchos::scan(
01522   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
01523   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
01524   )
01525 {
01526   TEUCHOS_COMM_TIME_MONITOR(
01527     "Teuchos::CommHelpers: scan<"
01528     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01529     <<">( value type, user-defined op )"
01530     );
01531   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01532     charSendBuffer(count,sendBuffer);
01533   ValueTypeSerializationBuffer<Ordinal,Packet>
01534     charScanReducts(count,scanReducts);
01535   CharToValueTypeReductionOp<Ordinal,Packet>
01536     charReductOp(rcp(&reductOp,false));
01537   comm.scan(
01538     charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01539     ,charScanReducts.getCharBuffer()
01540     );
01541 }
01542 
01543 
01544 template<typename Ordinal, typename Packet>
01545 void Teuchos::scan(
01546   const Comm<Ordinal>& comm, const EReductionType reductType,
01547   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
01548   )
01549 {
01550   TEUCHOS_COMM_TIME_MONITOR(
01551     "Teuchos::CommHelpers: scan<"
01552     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01553     <<">( value type, "<<toString(reductType)<<" )"
01554     );
01555   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01556     reductOp(createOp<Ordinal,Packet>(reductType));
01557   scan(comm,*reductOp,count,sendBuffer,scanReducts);
01558 }
01559 
01560 
01561 template<typename Ordinal, typename Packet>
01562 void Teuchos::scan(
01563   const Comm<Ordinal>& comm, const EReductionType reductType,
01564   const Packet &send, const Ptr<Packet> &scanReduct
01565   )
01566 {
01567   scan<Ordinal,Packet>(comm, reductType, 1, &send, &*scanReduct);
01568 }
01569 
01570 
01571 template<typename Ordinal, typename Packet>
01572 void Teuchos::scan(
01573   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01574   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
01575   const Ordinal count, const Packet*const sendBuffer[], Packet*const scanReducts[]
01576   )
01577 {
01578   TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01579 }
01580 
01581 template<typename Ordinal, typename Packet, typename Serializer>
01582 void Teuchos::scan(
01583   const Comm<Ordinal>& comm, 
01584   const Serializer& serializer,
01585   const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
01586   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
01587   )
01588 {
01589   TEUCHOS_COMM_TIME_MONITOR(
01590     "Teuchos::CommHelpers: scan<"
01591     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01592     <<">( value type, user-defined op )"
01593     );
01594   ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01595     charSendBuffer(count,sendBuffer,rcp(&serializer,false));
01596   ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01597     charScanReducts(count,scanReducts,rcp(&serializer,false));
01598   CharToValueTypeReductionOp<Ordinal,Packet,Serializer>
01599     charReductOp(rcp(&reductOp,false),rcp(&serializer,false));
01600   comm.scan(
01601     charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01602     ,charScanReducts.getCharBuffer()
01603     );
01604 }
01605 
01606 
01607 template<typename Ordinal, typename Packet, typename Serializer>
01608 void Teuchos::scan(
01609   const Comm<Ordinal>& comm, 
01610   const Serializer& serializer,
01611   const EReductionType reductType,
01612   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
01613   )
01614 {
01615   TEUCHOS_COMM_TIME_MONITOR(
01616     "Teuchos::CommHelpers: scan<"
01617     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01618     <<">( value type, "<<toString(reductType)<<" )"
01619     );
01620   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01621     reductOp(createOp<Ordinal,Packet>(reductType));
01622   scan(comm,serializer,*reductOp,count,sendBuffer,scanReducts);
01623 }
01624 
01625 template<typename Ordinal, typename Packet>
01626 void Teuchos::send(
01627   const Comm<Ordinal>& comm,
01628   const Ordinal count, const Packet sendBuffer[], const int destRank
01629   )
01630 {
01631   TEUCHOS_COMM_TIME_MONITOR(
01632     "Teuchos::CommHelpers: send<"
01633     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01634     <<">( value type )"
01635     );
01636   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01637     charSendBuffer(count,sendBuffer);
01638   comm.send(
01639     charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01640     ,destRank
01641     );
01642 }
01643 
01644 template<typename Ordinal, typename Packet>
01645 void Teuchos::ssend(
01646   const Comm<Ordinal>& comm,
01647   const Ordinal count, const Packet sendBuffer[], const int destRank
01648   )
01649 {
01650   TEUCHOS_COMM_TIME_MONITOR(
01651     "Teuchos::CommHelpers: ssend<"
01652     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01653     <<">( value type )"
01654     );
01655   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01656     charSendBuffer(count,sendBuffer);
01657   comm.ssend(
01658     charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01659     ,destRank
01660     );
01661 }
01662 
01663 template<typename Ordinal, typename Packet>
01664 void Teuchos::send(
01665   const Comm<Ordinal>& comm,
01666   const Packet &send, const int destRank
01667   )
01668 {
01669   Teuchos::send<Ordinal,Packet>(comm,1,&send,destRank);
01670 }
01671 
01672 template<typename Ordinal, typename Packet>
01673 void Teuchos::ssend(
01674   const Comm<Ordinal>& comm,
01675   const Packet &send, const int destRank
01676   )
01677 {
01678   Teuchos::ssend<Ordinal,Packet>(comm,1,&send,destRank);
01679 }
01680 
01681 template<typename Ordinal, typename Packet>
01682 void Teuchos::send(
01683   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01684   const Ordinal count, const Packet*const sendBuffer[], const int destRank
01685   )
01686 {
01687   TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01688 }
01689 
01690 template<typename Ordinal, typename Packet, typename Serializer>
01691 void Teuchos::send(
01692   const Comm<Ordinal>& comm,
01693   const Serializer& serializer,
01694   const Ordinal count, const Packet sendBuffer[], const int destRank
01695   )
01696 {
01697   TEUCHOS_COMM_TIME_MONITOR(
01698     "Teuchos::CommHelpers: send<"
01699     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01700     <<">( value type )"
01701     );
01702   ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01703     charSendBuffer(count,sendBuffer,rcp(&serializer,false));
01704   comm.send(
01705     charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01706     ,destRank
01707     );
01708 }
01709 
01710 template<typename Ordinal, typename Packet>
01711 int Teuchos::receive(
01712   const Comm<Ordinal>& comm,
01713   const int sourceRank, const Ordinal count, Packet recvBuffer[] 
01714   )
01715 {
01716   TEUCHOS_COMM_TIME_MONITOR(
01717     "Teuchos::CommHelpers: receive<"
01718     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01719     <<">( value type )"
01720     );
01721   ValueTypeSerializationBuffer<Ordinal,Packet>
01722     charRecvBuffer(count,recvBuffer);
01723   return comm.receive(
01724     sourceRank
01725     ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
01726     );
01727 }
01728 
01729 
01730 template<typename Ordinal, typename Packet>
01731 int Teuchos::receive(
01732   const Comm<Ordinal>& comm,
01733   const int sourceRank, Packet *recv 
01734   )
01735 {
01736   return Teuchos::receive<Ordinal,Packet>(comm,sourceRank,1,recv);
01737 }
01738 
01739 
01740 template<typename Ordinal, typename Packet>
01741 int Teuchos::receive(
01742   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01743   const int sourceRank, const Ordinal count, Packet*const recvBuffer[] 
01744   )
01745 {
01746   TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01747 }
01748 
01749 template<typename Ordinal, typename Packet, typename Serializer>
01750 int Teuchos::receive(
01751   const Comm<Ordinal>& comm,
01752   const Serializer& serializer,
01753   const int sourceRank, const Ordinal count, Packet recvBuffer[] 
01754   )
01755 {
01756   TEUCHOS_COMM_TIME_MONITOR(
01757     "Teuchos::CommHelpers: receive<"
01758     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01759     <<">( value type )"
01760     );
01761   ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01762     charRecvBuffer(count,recvBuffer,rcp(&serializer,false));
01763   return comm.receive(
01764     sourceRank
01765     ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
01766     );
01767 }
01768 
01769 template<typename Ordinal, typename Packet>
01770 void Teuchos::readySend(
01771   const Comm<Ordinal>& comm,
01772   const ArrayView<const Packet> &sendBuffer,
01773   const int destRank
01774   )
01775 {
01776   TEUCHOS_COMM_TIME_MONITOR(
01777     "Teuchos::CommHelpers: readySend<"
01778     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01779     <<">( value type )"
01780     );
01781   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01782     charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr());
01783   comm.readySend( charSendBuffer.getCharBufferView(), destRank );
01784 }
01785 
01786 
01787 template<typename Ordinal, typename Packet>
01788 void Teuchos::readySend(
01789   const Comm<Ordinal>& comm,
01790   const Packet &send,
01791   const int destRank
01792   )
01793 {
01794   readySend<Ordinal, Packet>( comm, arrayView(&send,1), destRank );
01795 }
01796 
01797 template<typename Ordinal, typename Packet, typename Serializer>
01798 void Teuchos::readySend(
01799   const Comm<Ordinal>& comm,
01800   const Serializer& serializer,
01801   const ArrayView<const Packet> &sendBuffer,
01802   const int destRank
01803   )
01804 {
01805   TEUCHOS_COMM_TIME_MONITOR(
01806     "Teuchos::CommHelpers: readySend<"
01807     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01808     <<">( value type )"
01809     );
01810   ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01811     charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr(), serializer);
01812   comm.readySend( charSendBuffer.getCharBufferView(), destRank );
01813 }
01814 
01815 template<typename Ordinal, typename Packet>
01816 Teuchos::RCP<Teuchos::CommRequest>
01817 Teuchos::isend(
01818   const Comm<Ordinal>& comm,
01819   const ArrayRCP<const Packet> &sendBuffer,
01820   const int destRank
01821   )
01822 {
01823   TEUCHOS_COMM_TIME_MONITOR(
01824     "Teuchos::CommHelpers: isend<"
01825     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01826     <<">( value type )"
01827     );
01828   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01829     charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr());
01830   RCP<CommRequest> commRequest = comm.isend(
01831     charSendBuffer.getCharBufferView(), destRank );
01832   set_extra_data( sendBuffer, "buffer", inOutArg(commRequest) );
01833   return commRequest;
01834 }
01835 
01836 
01837 template<typename Ordinal, typename Packet>
01838 Teuchos::RCP<Teuchos::CommRequest>
01839 Teuchos::isend(
01840   const Comm<Ordinal>& comm,
01841   const RCP<const Packet> &send,
01842   const int destRank
01843   )
01844 {
01845   const ArrayRCP<const Packet> sendBuffer =
01846     arcpWithEmbeddedObj( send.get(), 0, 1, send, false );
01847   // 2008/07/29: rabartl: Above: I need to write a helper function to create
01848   // new ArrayRCP object given a single object to copy.
01849   return isend<Ordinal, Packet>( comm, sendBuffer, destRank );
01850 }
01851 
01852 template<typename Ordinal, typename Packet, typename Serializer>
01853 Teuchos::RCP<Teuchos::CommRequest>
01854 Teuchos::isend(
01855   const Comm<Ordinal>& comm,
01856   const Serializer& serializer,
01857   const ArrayRCP<const Packet> &sendBuffer,
01858   const int destRank
01859   )
01860 {
01861   TEUCHOS_COMM_TIME_MONITOR(
01862     "Teuchos::CommHelpers: isend<"
01863     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01864     <<">( value type )"
01865     );
01866   ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01867     charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr(), serializer);
01868   RCP<CommRequest> commRequest = comm.isend(
01869     charSendBuffer.getCharBufferView(), destRank );
01870   set_extra_data( sendBuffer, "buffer", inOutArg(commRequest) );
01871   return commRequest;
01872 }
01873 
01874 template<typename Ordinal, typename Packet>
01875 Teuchos::RCP<Teuchos::CommRequest>
01876 Teuchos::ireceive(
01877   const Comm<Ordinal>& comm,
01878   const ArrayRCP<Packet> &recvBuffer,
01879   const int sourceRank
01880   )
01881 {
01882   typedef std::pair<RCP<CommRequest>, ArrayRCP<const Packet> > comm_buffer_pair_t;
01883   TEUCHOS_COMM_TIME_MONITOR(
01884     "Teuchos::CommHelpers: ireceive<"
01885     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01886     <<">( value type )"
01887     );
01888   ValueTypeSerializationBuffer<Ordinal,Packet>
01889     charRecvBuffer(recvBuffer.size(), recvBuffer.getRawPtr());
01890   RCP<CommRequest> commRequest = comm.ireceive(
01891     charRecvBuffer.getCharBufferView(), sourceRank );
01892   set_extra_data( recvBuffer, "buffer", inOutArg(commRequest) );
01893   return commRequest;
01894 }
01895 
01896 
01897 template<typename Ordinal, typename Packet>
01898 Teuchos::RCP<Teuchos::CommRequest>
01899 Teuchos::ireceive(
01900   const Comm<Ordinal>& comm,
01901   const RCP<Packet> &recv,
01902   const int sourceRank
01903   )
01904 {
01905   const ArrayRCP<Packet> recvBuffer =
01906     arcpWithEmbeddedObj( recv.get(), 0, 1, recv, false );
01907   // 2008/07/29: rabartl: Above: I need to write a helper function to create
01908   // new ArrayRCP object given a single object to copy.
01909   return ireceive<Ordinal, Packet>( comm, recvBuffer, sourceRank );
01910 }
01911 
01912 template<typename Ordinal, typename Packet, typename Serializer>
01913 Teuchos::RCP<Teuchos::CommRequest>
01914 Teuchos::ireceive(
01915   const Comm<Ordinal>& comm,
01916   const Serializer& serializer,
01917   const ArrayRCP<Packet> &recvBuffer,
01918   const int sourceRank
01919   )
01920 {
01921   typedef std::pair<RCP<CommRequest>, ArrayRCP<const Packet> > comm_buffer_pair_t;
01922   TEUCHOS_COMM_TIME_MONITOR(
01923     "Teuchos::CommHelpers: ireceive<"
01924     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01925     <<">( value type )"
01926     );
01927   ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01928     charRecvBuffer(recvBuffer.size(), recvBuffer.getRawPtr(), serializer);
01929   RCP<CommRequest> commRequest = comm.ireceive(
01930     charRecvBuffer.getCharBufferView(), sourceRank );
01931   set_extra_data( recvBuffer, "buffer", inOutArg(commRequest) );
01932   return commRequest;
01933 }
01934 
01935 template<typename Ordinal>
01936 void Teuchos::waitAll(
01937   const Comm<Ordinal>& comm,
01938   const ArrayView<RCP<CommRequest> > &requests
01939   )
01940 {
01941   comm.waitAll(requests);
01942 }
01943 
01944 
01945 template<typename Ordinal>
01946 void 
01947 Teuchos::waitAll (const Comm<Ordinal>& comm,
01948       const ArrayView<RCP<CommRequest> >& requests,
01949       const ArrayView<RCP<CommStatus<Ordinal> > >& statuses)
01950 {
01951   comm.waitAll (requests, statuses);
01952 }
01953 
01954 
01955 template<typename Ordinal>
01956 Teuchos::RCP<Teuchos::CommStatus<Ordinal> >
01957 Teuchos::wait (const Comm<Ordinal>& comm,
01958          const Ptr<RCP<CommRequest> > &request)
01959 {
01960   return comm.wait (request);
01961 }
01962 
01963 
01964 #endif // TEUCHOS_COMM_HELPERS_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines