Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_CommHelpers.hpp
Go to the documentation of this file.
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 send(
00470   const Comm<Ordinal>& comm,
00471   const Packet &send, const int destRank
00472   );
00473 
00480 template<typename Ordinal, typename Packet>
00481 void send(
00482   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00483   const Ordinal count, const Packet*const sendBuffer[], const int destRank
00484   );
00485 
00491 template<typename Ordinal, typename Packet, typename Serializer>
00492 void send(
00493   const Comm<Ordinal>& comm,
00494   const Serializer& serializer,
00495   const Ordinal count, const Packet sendBuffer[], const int destRank
00496   );
00497 
00502 template<typename Ordinal, typename Packet>
00503 int receive(
00504   const Comm<Ordinal>& comm,
00505   const int sourceRank, const Ordinal count, Packet recvBuffer[] 
00506   );
00507 
00512 template<typename Ordinal, typename Packet>
00513 int receive(
00514   const Comm<Ordinal>& comm,
00515   const int sourceRank, Packet *recv 
00516   );
00517 
00522 template<typename Ordinal, typename Packet>
00523 int receive(
00524   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00525   const int sourceRank, const Ordinal count, Packet*const recvBuffer[] 
00526   );
00527 
00533 template<typename Ordinal, typename Packet, typename Serializer>
00534 int receive(
00535   const Comm<Ordinal>& comm,
00536   const Serializer& serializer,
00537   const int sourceRank, const Ordinal count, Packet recvBuffer[] 
00538   );
00539 
00545 template<typename Ordinal, typename Packet>
00546 void readySend(
00547   const Comm<Ordinal>& comm,
00548   const ArrayView<const Packet> &sendBuffer,
00549   const int destRank
00550   );
00551 
00556 template<typename Ordinal, typename Packet>
00557 void readySend(
00558   const Comm<Ordinal>& comm,
00559   const Packet &send,
00560   const int destRank
00561   );
00562 
00568 template<typename Ordinal, typename Packet, typename Serializer>
00569 void readySend(
00570   const Comm<Ordinal>& comm,
00571   const Serializer& serializer,
00572   const ArrayView<const Packet> &sendBuffer,
00573   const int destRank
00574   );
00575 
00580 template<typename Ordinal, typename Packet>
00581 RCP<CommRequest> isend(
00582   const Comm<Ordinal>& comm,
00583   const ArrayRCP<const Packet> &sendBuffer,
00584   const int destRank
00585   );
00586 
00587 
00592 template<typename Ordinal, typename Packet>
00593 RCP<CommRequest> isend(
00594   const Comm<Ordinal>& comm,
00595   const RCP<const Packet> &send,
00596   const int destRank
00597   );
00598 
00604 template<typename Ordinal, typename Packet, typename Serializer>
00605 RCP<CommRequest> isend(
00606   const Comm<Ordinal>& comm,
00607   const Serializer& serializer,
00608   const ArrayRCP<const Packet> &sendBuffer,
00609   const int destRank
00610   );
00611 
00612 
00613 // 2008/07/29: rabartl: ToDo: Add reference semantics version of isend!
00614 
00615 
00620 template<typename Ordinal, typename Packet>
00621 RCP<CommRequest> ireceive(
00622   const Comm<Ordinal>& comm,
00623   const ArrayRCP<Packet> &recvBuffer,
00624   const int sourceRank
00625   );
00626 
00627 
00632 template<typename Ordinal, typename Packet>
00633 RCP<CommRequest> ireceive(
00634   const Comm<Ordinal>& comm,
00635   const RCP<Packet> &recv,
00636   const int sourceRank
00637   );
00638 
00644 template<typename Ordinal, typename Packet, typename Serializer>
00645 RCP<CommRequest> ireceive(
00646   const Comm<Ordinal>& comm,
00647   const Serializer& serializer,
00648   const ArrayRCP<Packet> &recvBuffer,
00649   const int sourceRank
00650   );
00651 
00652 
00653 // 2008/07/29: rabartl: ToDo: Add reference semantics version of ireceive!
00654 
00655 
00663 template<typename Ordinal>
00664 void waitAll(
00665   const Comm<Ordinal>& comm,
00666   const ArrayView<RCP<CommRequest> > &requests
00667   );
00668 
00676 template<typename Ordinal>
00677 void wait(
00678   const Comm<Ordinal>& comm,
00679   const Ptr<RCP<CommRequest> > &request
00680   );
00681 
00682 
00683 //
00684 // Standard reduction subclasses for objects that use value semantics
00685 //
00686 
00687 
00692 template<typename Ordinal, typename Packet>
00693 class SumValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00694 {
00695 public:
00697   void reduce(
00698     const Ordinal count,
00699     const Packet inBuffer[],
00700     Packet inoutBuffer[]
00701     ) const;
00702 };
00703 
00704 
00713 template<typename Ordinal, typename Packet>
00714 class MinValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00715 {
00716 public:
00718   void reduce(
00719     const Ordinal count,
00720     const Packet inBuffer[],
00721     Packet inoutBuffer[]
00722     ) const;
00723 };
00724 
00725 
00734 template<typename Ordinal, typename Packet>
00735 class MaxValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00736 {
00737 public:
00739   void reduce(
00740     const Ordinal count,
00741     const Packet inBuffer[],
00742     Packet inoutBuffer[]
00743     ) const;
00744 };
00745 
00746 
00751 template<typename Ordinal, typename Packet>
00752 class ANDValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00753 {
00754 public:
00756   void reduce(
00757     const Ordinal count,
00758     const Packet inBuffer[],
00759     Packet inoutBuffer[]
00760     ) const;
00761 };
00762 
00763 
00764 // ////////////////////////////////////////////////////////////
00765 // Implementation details (not for geneal users to mess with)
00766 
00767 
00768 //
00769 // ReductionOp Utilities
00770 //
00771 
00772 
00773 namespace MixMaxUtilities {
00774 
00775 
00776 template<bool isComparable, typename Ordinal, typename Packet>
00777 class Min {};
00778 
00779 
00780 template<typename Ordinal, typename Packet>
00781 class Min<true,Ordinal,Packet> {
00782 public:
00783   static void min(
00784     const Ordinal count,
00785     const Packet inBuffer[],
00786     Packet inoutBuffer[]
00787     )
00788     {
00789       for( int i = 0; i < count; ++i )
00790         inoutBuffer[i] = TEUCHOS_MIN(inoutBuffer[i],inBuffer[i]);
00791     }
00792 };
00793 
00794 
00795 template<typename Ordinal, typename Packet>
00796 class Min<false,Ordinal,Packet> {
00797 public:
00798   static void min(
00799     const Ordinal,
00800     const Packet[],
00801     Packet[]
00802     )
00803     {
00804       TEUCHOS_TEST_FOR_EXCEPTION(
00805         true,std::logic_error,
00806         "Error, the type "<<TypeNameTraits<Packet>::name()
00807         <<" does not support comparison operations!"
00808         );
00809     }
00810 };
00811 
00812 
00813 template<bool isComparable, typename Ordinal, typename Packet>
00814 class Max {};
00815 
00816 
00817 template<typename Ordinal, typename Packet>
00818 class Max<true,Ordinal,Packet> {
00819 public:
00820   static void max(
00821     const Ordinal count,
00822     const Packet inBuffer[],
00823     Packet inoutBuffer[]
00824     )
00825     {
00826       for( int i = 0; i < count; ++i )
00827         inoutBuffer[i] = TEUCHOS_MAX(inoutBuffer[i],inBuffer[i]);
00828     }
00829 };
00830 
00831 
00832 template<typename Ordinal, typename Packet>
00833 class Max<false,Ordinal,Packet> {
00834 public:
00835   static void max(
00836     const Ordinal,
00837     const Packet[],
00838     Packet[]
00839     )
00840     {
00841       TEUCHOS_TEST_FOR_EXCEPTION(
00842         true,std::logic_error,
00843         "Error, the type "<<TypeNameTraits<Packet>::name()
00844         <<" does not support comparison operations!"
00845         );
00846     }
00847 };
00848 
00849 
00850 template<bool isComparable, typename Ordinal, typename Packet>
00851 class AND {};
00852 
00853 
00854 template<typename Ordinal, typename Packet>
00855 class AND<true,Ordinal,Packet> {
00856 public:
00857   static void andOp(
00858     const Ordinal count,
00859     const Packet inBuffer[],
00860     Packet inoutBuffer[]
00861     )
00862     {
00863       for( int i = 0; i < count; ++i )
00864         inoutBuffer[i] = inoutBuffer[i] && inBuffer[i];
00865     }
00866 };
00867 
00868 
00869 template<typename Ordinal, typename Packet>
00870 class AND<false,Ordinal,Packet> {
00871 public:
00872   static void andOp(
00873     const Ordinal,
00874     const Packet[],
00875     Packet[]
00876     )
00877     {
00878       TEUCHOS_TEST_FOR_EXCEPTION(
00879         true,std::logic_error,
00880         "Error, the type "<<TypeNameTraits<Packet>::name()
00881         <<" does not support logical AND operations!"
00882         );
00883     }
00884 };
00885 
00886 
00887 } // namespace MixMaxUtilities
00888 
00889 
00890 template<typename Ordinal, typename Packet>
00891 void SumValueReductionOp<Ordinal,Packet>::reduce(
00892   const Ordinal count,
00893   const Packet inBuffer[],
00894   Packet inoutBuffer[]
00895   ) const
00896 {
00897   for( int i = 0; i < count; ++i )
00898     inoutBuffer[i] += inBuffer[i];
00899 }
00900 
00901 
00902 template<typename Ordinal, typename Packet>
00903 void MinValueReductionOp<Ordinal,Packet>::reduce(
00904   const Ordinal count,
00905   const Packet inBuffer[],
00906   Packet inoutBuffer[]
00907   ) const
00908 {
00909   typedef ScalarTraits<Packet> ST;
00910   MixMaxUtilities::Min<ST::isComparable,Ordinal,Packet>::min(
00911     count,inBuffer,inoutBuffer
00912     );
00913 }
00914 
00915 
00916 template<typename Ordinal, typename Packet>
00917 void MaxValueReductionOp<Ordinal,Packet>::reduce(
00918   const Ordinal count,
00919   const Packet inBuffer[],
00920   Packet inoutBuffer[]
00921   ) const
00922 {
00923   typedef ScalarTraits<Packet> ST;
00924   MixMaxUtilities::Max<ST::isComparable,Ordinal,Packet>::max(
00925     count,inBuffer,inoutBuffer
00926     );
00927 }
00928 
00929 
00930 template<typename Ordinal, typename Packet>
00931 void ANDValueReductionOp<Ordinal,Packet>::reduce(
00932   const Ordinal count,
00933   const Packet inBuffer[],
00934   Packet inoutBuffer[]
00935   ) const
00936 {
00937   typedef ScalarTraits<Packet> ST;
00938   MixMaxUtilities::AND<ST::isComparable,Ordinal,Packet>::andOp(
00939     count,inBuffer,inoutBuffer
00940     );
00941 }
00942 
00943 
00944 } // namespace Teuchos
00945 
00946 
00947 // //////////////////////////
00948 // Template implemenations
00949 
00950 
00951 //
00952 // ReductionOp utilities
00953 //
00954 
00955 
00956 namespace Teuchos {
00957 
00958 
00959 // Not for the general user to use! I am returning a raw ReducionOp* pointer
00960 // to avoid the overhead of using RCP. However, given the use case
00961 // this is just fine since I can just use std::auto_ptr to make sure things
00962 // are deleted correctly.
00963 template<typename Ordinal, typename Packet>
00964 ValueTypeReductionOp<Ordinal,Packet>*
00965 createOp( const EReductionType reductType )
00966 {
00967   typedef ScalarTraits<Packet> ST;
00968   switch(reductType) {
00969     case REDUCE_SUM: {
00970       return new SumValueReductionOp<Ordinal,Packet>();
00971       break;
00972     }
00973     case REDUCE_MIN: {
00974       TEUCHOS_TEST_FOR_EXCEPT(!ST::isComparable);
00975       return new MinValueReductionOp<Ordinal,Packet>();
00976       break;
00977     }
00978     case REDUCE_MAX: {
00979       TEUCHOS_TEST_FOR_EXCEPT(!ST::isComparable);
00980       return new MaxValueReductionOp<Ordinal,Packet>();
00981       break;
00982     }
00983     case REDUCE_AND: {
00984       return new ANDValueReductionOp<Ordinal, Packet>();
00985       break;
00986     }
00987     default:
00988       TEUCHOS_TEST_FOR_EXCEPT(true);
00989   }
00990   return 0; // Will never be called!
00991 }
00992 
00993 
00994 } // namespace Teuchos
00995 
00996 
00997 //
00998 // Teuchos::Comm wrapper functions
00999 //
01000 
01001 
01002 template<typename Ordinal>
01003 int Teuchos::rank(const Comm<Ordinal>& comm)
01004 {
01005   return comm.getRank();
01006 }
01007 
01008 
01009 template<typename Ordinal>
01010 int Teuchos::size(const Comm<Ordinal>& comm)
01011 {
01012   return comm.getSize();
01013 }
01014 
01015 
01016 template<typename Ordinal>
01017 void Teuchos::barrier(const Comm<Ordinal>& comm)
01018 {
01019   TEUCHOS_COMM_TIME_MONITOR(
01020     "Teuchos::CommHelpers: barrier<"
01021     <<OrdinalTraits<Ordinal>::name()
01022     <<">()"
01023     );
01024   comm.barrier();
01025 }
01026 
01027 
01028 template<typename Ordinal, typename Packet>
01029 void Teuchos::broadcast(
01030   const Comm<Ordinal>& comm,
01031   const int rootRank, const Ordinal count, Packet buffer[]
01032   )
01033 {
01034   TEUCHOS_COMM_TIME_MONITOR(
01035     "Teuchos::CommHelpers: broadcast<"
01036     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01037     <<">( value type )"
01038     );
01039   ValueTypeSerializationBuffer<Ordinal,Packet>
01040     charBuffer(count,buffer);
01041   comm.broadcast(
01042     rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
01043     );
01044 }
01045 
01046 
01047 template<typename Ordinal, typename Packet>
01048 void Teuchos::broadcast(
01049   const Comm<Ordinal>& comm,
01050   const int rootRank,
01051   const ArrayView<Packet> &buffer
01052   )
01053 {
01054   broadcast<Ordinal, Packet>(comm, rootRank, buffer.size(), buffer.getRawPtr() );
01055 }
01056 
01057 
01058 template<typename Ordinal, typename Packet>
01059 void Teuchos::broadcast(
01060   const Comm<Ordinal>& comm,
01061   const int rootRank, Packet *object
01062   )
01063 {
01064   broadcast<Ordinal,Packet>(comm,rootRank,1,object);
01065 }
01066 
01067 
01068 template<typename Ordinal, typename Packet>
01069 void Teuchos::broadcast(
01070   const Comm<Ordinal>& comm,
01071   const int rootRank, const Ptr<Packet> &object
01072   )
01073 {
01074   broadcast<Ordinal,Packet>(comm,rootRank,1,object.getRawPtr());
01075 }
01076 
01077 
01078 template<typename Ordinal, typename Packet>
01079 void Teuchos::broadcast(
01080   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01081   const int rootRank, const Ordinal count, Packet*const buffer[]
01082   )
01083 {
01084   TEUCHOS_COMM_TIME_MONITOR(
01085     "Teuchos::CommHelpers: broadcast<"
01086     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01087     <<">( reference type )"
01088     );
01089   ReferenceTypeSerializationBuffer<Ordinal,Packet>
01090     charBuffer(serializer, count, buffer);
01091   comm.broadcast(
01092     rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
01093     );
01094 }
01095 
01096 
01097 template<typename Ordinal, typename Packet>
01098 void Teuchos::broadcast(
01099   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01100   const int rootRank, const ArrayView<const Ptr<Packet> > &buffer
01101   )
01102 {
01103   Array<Packet*> bufferPtrArray;
01104   for (int i = 0; i < buffer.size(); ++i) {
01105     bufferPtrArray.push_back(buffer[i].getRawPtr());
01106   }
01107   broadcast<Ordinal,Packet>(comm, serializer, rootRank,
01108     buffer.size(), bufferPtrArray.getRawPtr());
01109 }
01110 
01111 template<typename Ordinal, typename Packet, typename Serializer>
01112 void Teuchos::broadcast(
01113   const Comm<Ordinal>& comm,
01114   const Serializer& serializer,
01115   const int rootRank, const Ordinal count, Packet buffer[]
01116   )
01117 {
01118   TEUCHOS_COMM_TIME_MONITOR(
01119     "Teuchos::CommHelpers: broadcast<"
01120     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01121     <<">( value type )"
01122     );
01123   ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01124     charBuffer(count,buffer,rcp(&serializer,false));
01125   comm.broadcast(
01126     rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
01127     );
01128 }
01129 
01130 
01131 template<typename Ordinal, typename Packet>
01132 void Teuchos::gatherAll(
01133   const Comm<Ordinal>& comm,
01134   const Ordinal sendCount, const Packet sendBuffer[],
01135   const Ordinal recvCount, Packet recvBuffer[]
01136   )
01137 {
01138   TEUCHOS_COMM_TIME_MONITOR(
01139     "Teuchos::CommHelpers: gatherAll<"
01140     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01141     <<">( value type )"
01142     );
01143   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01144     charSendBuffer(sendCount,sendBuffer);
01145   ValueTypeSerializationBuffer<Ordinal,Packet>
01146     charRecvBuffer(recvCount,recvBuffer);
01147   comm.gatherAll(
01148     charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01149     ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
01150     );
01151 }
01152 
01153 
01154 template<typename Ordinal, typename Packet>
01155 void Teuchos::gatherAll(
01156   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01157   const Ordinal sendCount, const Packet*const sendBuffer[],
01158   const Ordinal recvCount, Packet*const recvBuffer[]
01159   )
01160 {
01161   TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01162 }
01163 
01164 template<typename Ordinal, typename Packet, typename Serializer>
01165 void Teuchos::gatherAll(
01166   const Comm<Ordinal>& comm,
01167   const Serializer& serializer,
01168   const Ordinal sendCount, const Packet sendBuffer[],
01169   const Ordinal recvCount, Packet recvBuffer[]
01170   )
01171 {
01172   TEUCHOS_COMM_TIME_MONITOR(
01173     "Teuchos::CommHelpers: gatherAll<"
01174     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01175     <<">( value type )"
01176     );
01177   ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01178     charSendBuffer(sendCount,sendBuffer,rcp(&serializer,false));
01179   ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01180     charRecvBuffer(recvCount,recvBuffer,rcp(&serializer,false));
01181   comm.gatherAll(
01182     charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01183     ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
01184     );
01185 }
01186 
01187 
01188 template<typename Ordinal, typename Packet>
01189 void Teuchos::reduceAll(
01190   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp
01191   ,const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
01192   )
01193 {
01194   TEUCHOS_COMM_TIME_MONITOR(
01195     "Teuchos::CommHelpers: reduceAll<"
01196     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01197     <<">( value type, user-defined op )"
01198     );
01199   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01200     charSendBuffer(count,sendBuffer);
01201   ValueTypeSerializationBuffer<Ordinal,Packet>
01202     charGlobalReducts(count,globalReducts);
01203   CharToValueTypeReductionOp<Ordinal,Packet>
01204     charReductOp(rcp(&reductOp,false));
01205   comm.reduceAll(
01206     charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01207     ,charGlobalReducts.getCharBuffer()
01208     );
01209 }
01210 
01211 
01212 template<typename Ordinal, typename Packet>
01213 void Teuchos::reduceAll(
01214   const Comm<Ordinal>& comm, const EReductionType reductType,
01215   const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
01216   )
01217 {
01218   TEUCHOS_COMM_TIME_MONITOR(
01219     "Teuchos::CommHelpers: reduceAll<"
01220     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01221     <<">( value type, "<<toString(reductType)<<" )"
01222     );
01223   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01224     reductOp(createOp<Ordinal,Packet>(reductType));
01225   reduceAll(comm,*reductOp,count,sendBuffer,globalReducts);
01226 }
01227 
01228 
01229 template<typename Ordinal, typename Packet>
01230 void Teuchos::reduceAll(
01231   const Comm<Ordinal>& comm, const EReductionType reductType
01232   ,const Packet &send, const Ptr<Packet> &globalReduct
01233   )
01234 {
01235   reduceAll<Ordinal,Packet>(comm, reductType, 1, &send, &*globalReduct);
01236 }
01237 
01238 
01239 template<typename Ordinal, typename Packet>
01240 void Teuchos::reduceAll(
01241   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01242   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
01243   const Ordinal count, const Packet*const sendBuffer[], Packet*const globalReducts[]
01244   )
01245 {
01246   TEUCHOS_COMM_TIME_MONITOR(
01247     "Teuchos::CommHelpers: reduceAll<"
01248     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01249     <<">( reference type )"
01250     );
01251   ConstReferenceTypeSerializationBuffer<Ordinal,Packet>
01252     charSendBuffer(serializer,count,sendBuffer);
01253   ReferenceTypeSerializationBuffer<Ordinal,Packet>
01254     charGlobalReducts(serializer,count,globalReducts);
01255   CharToReferenceTypeReductionOp<Ordinal,Packet>
01256     charReductOp(rcp(&serializer,false),rcp(&reductOp,false));
01257   comm.reduceAll(
01258     charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01259     ,charGlobalReducts.getCharBuffer()
01260     );
01261 }
01262 
01263 template<typename Ordinal, typename Packet, typename Serializer>
01264 void Teuchos::reduceAll(
01265   const Comm<Ordinal>& comm, 
01266   const Serializer& serializer,
01267   const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
01268   const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
01269   )
01270 {
01271   TEUCHOS_COMM_TIME_MONITOR(
01272     "Teuchos::CommHelpers: reduceAll<"
01273     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01274     <<">( value type, user-defined op )"
01275     );
01276   ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01277     charSendBuffer(count,sendBuffer,rcp(&serializer,false));
01278   ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01279     charGlobalReducts(count,globalReducts,rcp(&serializer,false));
01280   CharToValueTypeReductionOp<Ordinal,Packet,Serializer>
01281     charReductOp(rcp(&reductOp,false),rcp(&serializer,false));
01282   comm.reduceAll(
01283     charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01284     ,charGlobalReducts.getCharBuffer()
01285     );
01286 }
01287 
01288 
01289 template<typename Ordinal, typename Packet, typename Serializer>
01290 void Teuchos::reduceAll(
01291   const Comm<Ordinal>& comm, 
01292   const Serializer& serializer,
01293   const EReductionType reductType,
01294   const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
01295   )
01296 {
01297   TEUCHOS_COMM_TIME_MONITOR(
01298     "Teuchos::CommHelpers: reduceAll<"
01299     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01300     <<">( value type, "<<toString(reductType)<<" )"
01301     );
01302   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01303     reductOp(createOp<Ordinal,Packet>(reductType));
01304   reduceAll(comm,serializer,*reductOp,count,sendBuffer,globalReducts);
01305 }
01306 
01307 
01308 template<typename Ordinal, typename Packet>
01309 void Teuchos::reduceAllAndScatter(
01310   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
01311   const Ordinal sendCount, const Packet sendBuffer[] ,
01312   const Ordinal recvCounts[], Packet myGlobalReducts[]
01313   )
01314 {
01315 
01316   TEUCHOS_COMM_TIME_MONITOR(
01317     "Teuchos::CommHelpers: reduceAllAndScatter<"
01318     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01319     <<">( value type, user-defined op )"
01320     );
01321 
01322   const Ordinal size = Teuchos::size(comm);
01323   const Ordinal rank = Teuchos::rank(comm);
01324 
01325 #ifdef TEUCHOS_DEBUG
01326   Ordinal sumRecvCounts = 0;
01327   for( Ordinal i = 0; i < size; ++i )
01328     sumRecvCounts += recvCounts[static_cast<ptrdiff_t>(i)];
01329   TEUCHOS_TEST_FOR_EXCEPT(!(sumRecvCounts==sendCount));
01330 #endif
01331 
01332   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01333     charSendBuffer(sendCount,sendBuffer);
01334   ValueTypeSerializationBuffer<Ordinal,Packet>
01335     charMyGlobalReducts(recvCounts[static_cast<ptrdiff_t>(rank)], myGlobalReducts);
01336   CharToValueTypeReductionOp<Ordinal,Packet>
01337     charReductOp(rcp(&reductOp,false));
01338 
01339   const Ordinal
01340     packetSize = charSendBuffer.getBytes()/sendCount;
01341 
01342   WorkspaceStore* wss = get_default_workspace_store().get();
01343   Workspace<Ordinal> charRecvCounts(wss, size);
01344   for (Ordinal k = 0; k < size; ++k) {
01345     charRecvCounts[k] = as<Ordinal>(recvCounts[static_cast<ptrdiff_t>(k)] * packetSize);
01346   }
01347  
01348   comm.reduceAllAndScatter(
01349     charReductOp, charSendBuffer.getBytes(), charSendBuffer.getCharBuffer(),
01350     &charRecvCounts[0], charMyGlobalReducts.getCharBuffer()
01351     );
01352 
01353 }
01354 
01355 
01356 template<typename Ordinal, typename Packet>
01357 void Teuchos::reduceAllAndScatter(
01358   const Comm<Ordinal>& comm, const EReductionType reductType,
01359   const Ordinal sendCount, const Packet sendBuffer[] ,
01360   const Ordinal recvCounts[], Packet myGlobalReducts[]
01361   )
01362 {
01363   TEUCHOS_COMM_TIME_MONITOR(
01364     "Teuchos::CommHelpers: reduceAllAndScatter<"
01365     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01366     <<">( value type, "<<toString(reductType)<<" )"
01367     );
01368   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01369     reductOp(createOp<Ordinal,Packet>(reductType));
01370   reduceAllAndScatter(
01371     comm, *reductOp, sendCount, sendBuffer, recvCounts, myGlobalReducts
01372     );
01373 }
01374 
01375 
01376 template<typename Ordinal, typename Packet>
01377 void Teuchos::reduceAllAndScatter(
01378   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01379   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
01380   const Ordinal sendCount, const Packet*const sendBuffer[],
01381   const Ordinal recvCounts[], Packet*const myGlobalReducts[]
01382   )
01383 {
01384   TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01385 }
01386 
01387 template<typename Ordinal, typename Packet, typename Serializer>
01388 void Teuchos::reduceAllAndScatter(
01389   const Comm<Ordinal>& comm, 
01390   const Serializer& serializer,
01391   const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
01392   const Ordinal sendCount, const Packet sendBuffer[] ,
01393   const Ordinal recvCounts[], Packet myGlobalReducts[]
01394   )
01395 {
01396 
01397   TEUCHOS_COMM_TIME_MONITOR(
01398     "Teuchos::CommHelpers: reduceAllAndScatter<"
01399     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01400     <<">( value type, user-defined op )"
01401     );
01402 
01403   const Ordinal size = Teuchos::size(comm);
01404   const Ordinal rank = Teuchos::rank(comm);
01405 
01406 #ifdef TEUCHOS_DEBUG
01407   Ordinal sumRecvCounts = 0;
01408   for( Ordinal i = 0; i < size; ++i )
01409     sumRecvCounts += recvCounts[static_cast<ptrdiff_t>(i)];
01410   TEUCHOS_TEST_FOR_EXCEPT(!(sumRecvCounts==sendCount));
01411 #endif
01412 
01413   ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01414     charSendBuffer(sendCount,sendBuffer,rcp(&serializer,false));
01415   ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01416     charMyGlobalReducts(recvCounts[static_cast<ptrdiff_t>(rank)], myGlobalReducts,rcp(&serializer,false));
01417   CharToValueTypeReductionOp<Ordinal,Packet,Serializer>
01418     charReductOp(rcp(&reductOp,false),rcp(&serializer,false));
01419 
01420   const Ordinal
01421     packetSize = charSendBuffer.getBytes()/sendCount;
01422 
01423   WorkspaceStore* wss = get_default_workspace_store().get();
01424   Workspace<Ordinal> charRecvCounts(wss, size);
01425   for (Ordinal k = 0; k < size; ++k) {
01426     charRecvCounts[k] = as<Ordinal>(recvCounts[static_cast<ptrdiff_t>(k)] * packetSize);
01427   }
01428  
01429   comm.reduceAllAndScatter(
01430     charReductOp, charSendBuffer.getBytes(), charSendBuffer.getCharBuffer(),
01431     &charRecvCounts[0], charMyGlobalReducts.getCharBuffer()
01432     );
01433 
01434 }
01435 
01436 template<typename Ordinal, typename Packet, typename Serializer>
01437 void Teuchos::reduceAllAndScatter(
01438   const Comm<Ordinal>& comm, 
01439   const Serializer& serializer,
01440   const EReductionType reductType,
01441   const Ordinal sendCount, const Packet sendBuffer[] ,
01442   const Ordinal recvCounts[], Packet myGlobalReducts[]
01443   )
01444 {
01445   TEUCHOS_COMM_TIME_MONITOR(
01446     "Teuchos::CommHelpers: reduceAllAndScatter<"
01447     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01448     <<">( value type, "<<toString(reductType)<<" )"
01449     );
01450   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01451     reductOp(createOp<Ordinal,Packet>(reductType));
01452   reduceAllAndScatter(
01453     comm, serializer, *reductOp, sendCount, sendBuffer, recvCounts, 
01454     myGlobalReducts
01455     );
01456 }
01457 
01458 
01459 template<typename Ordinal, typename Packet>
01460 void Teuchos::scan(
01461   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
01462   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
01463   )
01464 {
01465   TEUCHOS_COMM_TIME_MONITOR(
01466     "Teuchos::CommHelpers: scan<"
01467     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01468     <<">( value type, user-defined op )"
01469     );
01470   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01471     charSendBuffer(count,sendBuffer);
01472   ValueTypeSerializationBuffer<Ordinal,Packet>
01473     charScanReducts(count,scanReducts);
01474   CharToValueTypeReductionOp<Ordinal,Packet>
01475     charReductOp(rcp(&reductOp,false));
01476   comm.scan(
01477     charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01478     ,charScanReducts.getCharBuffer()
01479     );
01480 }
01481 
01482 
01483 template<typename Ordinal, typename Packet>
01484 void Teuchos::scan(
01485   const Comm<Ordinal>& comm, const EReductionType reductType,
01486   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
01487   )
01488 {
01489   TEUCHOS_COMM_TIME_MONITOR(
01490     "Teuchos::CommHelpers: scan<"
01491     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01492     <<">( value type, "<<toString(reductType)<<" )"
01493     );
01494   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01495     reductOp(createOp<Ordinal,Packet>(reductType));
01496   scan(comm,*reductOp,count,sendBuffer,scanReducts);
01497 }
01498 
01499 
01500 template<typename Ordinal, typename Packet>
01501 void Teuchos::scan(
01502   const Comm<Ordinal>& comm, const EReductionType reductType,
01503   const Packet &send, const Ptr<Packet> &scanReduct
01504   )
01505 {
01506   scan<Ordinal,Packet>(comm, reductType, 1, &send, &*scanReduct);
01507 }
01508 
01509 
01510 template<typename Ordinal, typename Packet>
01511 void Teuchos::scan(
01512   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01513   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
01514   const Ordinal count, const Packet*const sendBuffer[], Packet*const scanReducts[]
01515   )
01516 {
01517   TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01518 }
01519 
01520 template<typename Ordinal, typename Packet, typename Serializer>
01521 void Teuchos::scan(
01522   const Comm<Ordinal>& comm, 
01523   const Serializer& serializer,
01524   const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
01525   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
01526   )
01527 {
01528   TEUCHOS_COMM_TIME_MONITOR(
01529     "Teuchos::CommHelpers: scan<"
01530     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01531     <<">( value type, user-defined op )"
01532     );
01533   ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01534     charSendBuffer(count,sendBuffer,rcp(&serializer,false));
01535   ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01536     charScanReducts(count,scanReducts,rcp(&serializer,false));
01537   CharToValueTypeReductionOp<Ordinal,Packet,Serializer>
01538     charReductOp(rcp(&reductOp,false),rcp(&serializer,false));
01539   comm.scan(
01540     charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01541     ,charScanReducts.getCharBuffer()
01542     );
01543 }
01544 
01545 
01546 template<typename Ordinal, typename Packet, typename Serializer>
01547 void Teuchos::scan(
01548   const Comm<Ordinal>& comm, 
01549   const Serializer& serializer,
01550   const EReductionType reductType,
01551   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
01552   )
01553 {
01554   TEUCHOS_COMM_TIME_MONITOR(
01555     "Teuchos::CommHelpers: scan<"
01556     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01557     <<">( value type, "<<toString(reductType)<<" )"
01558     );
01559   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01560     reductOp(createOp<Ordinal,Packet>(reductType));
01561   scan(comm,serializer,*reductOp,count,sendBuffer,scanReducts);
01562 }
01563 
01564 template<typename Ordinal, typename Packet>
01565 void Teuchos::send(
01566   const Comm<Ordinal>& comm,
01567   const Ordinal count, const Packet sendBuffer[], const int destRank
01568   )
01569 {
01570   TEUCHOS_COMM_TIME_MONITOR(
01571     "Teuchos::CommHelpers: send<"
01572     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01573     <<">( value type )"
01574     );
01575   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01576     charSendBuffer(count,sendBuffer);
01577   comm.send(
01578     charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01579     ,destRank
01580     );
01581 }
01582 
01583 
01584 template<typename Ordinal, typename Packet>
01585 void Teuchos::send(
01586   const Comm<Ordinal>& comm,
01587   const Packet &send, const int destRank
01588   )
01589 {
01590   Teuchos::send<Ordinal,Packet>(comm,1,&send,destRank);
01591 }
01592 
01593 
01594 template<typename Ordinal, typename Packet>
01595 void Teuchos::send(
01596   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01597   const Ordinal count, const Packet*const sendBuffer[], const int destRank
01598   )
01599 {
01600   TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01601 }
01602 
01603 template<typename Ordinal, typename Packet, typename Serializer>
01604 void Teuchos::send(
01605   const Comm<Ordinal>& comm,
01606   const Serializer& serializer,
01607   const Ordinal count, const Packet sendBuffer[], const int destRank
01608   )
01609 {
01610   TEUCHOS_COMM_TIME_MONITOR(
01611     "Teuchos::CommHelpers: send<"
01612     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01613     <<">( value type )"
01614     );
01615   ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01616     charSendBuffer(count,sendBuffer,rcp(&serializer,false));
01617   comm.send(
01618     charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01619     ,destRank
01620     );
01621 }
01622 
01623 template<typename Ordinal, typename Packet>
01624 int Teuchos::receive(
01625   const Comm<Ordinal>& comm,
01626   const int sourceRank, const Ordinal count, Packet recvBuffer[] 
01627   )
01628 {
01629   TEUCHOS_COMM_TIME_MONITOR(
01630     "Teuchos::CommHelpers: receive<"
01631     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01632     <<">( value type )"
01633     );
01634   ValueTypeSerializationBuffer<Ordinal,Packet>
01635     charRecvBuffer(count,recvBuffer);
01636   return comm.receive(
01637     sourceRank
01638     ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
01639     );
01640 }
01641 
01642 
01643 template<typename Ordinal, typename Packet>
01644 int Teuchos::receive(
01645   const Comm<Ordinal>& comm,
01646   const int sourceRank, Packet *recv 
01647   )
01648 {
01649   return Teuchos::receive<Ordinal,Packet>(comm,sourceRank,1,recv);
01650 }
01651 
01652 
01653 template<typename Ordinal, typename Packet>
01654 int Teuchos::receive(
01655   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01656   const int sourceRank, const Ordinal count, Packet*const recvBuffer[] 
01657   )
01658 {
01659   TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01660 }
01661 
01662 template<typename Ordinal, typename Packet, typename Serializer>
01663 int Teuchos::receive(
01664   const Comm<Ordinal>& comm,
01665   const Serializer& serializer,
01666   const int sourceRank, const Ordinal count, Packet recvBuffer[] 
01667   )
01668 {
01669   TEUCHOS_COMM_TIME_MONITOR(
01670     "Teuchos::CommHelpers: receive<"
01671     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01672     <<">( value type )"
01673     );
01674   ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01675     charRecvBuffer(count,recvBuffer,rcp(&serializer,false));
01676   return comm.receive(
01677     sourceRank
01678     ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
01679     );
01680 }
01681 
01682 template<typename Ordinal, typename Packet>
01683 void Teuchos::readySend(
01684   const Comm<Ordinal>& comm,
01685   const ArrayView<const Packet> &sendBuffer,
01686   const int destRank
01687   )
01688 {
01689   TEUCHOS_COMM_TIME_MONITOR(
01690     "Teuchos::CommHelpers: readySend<"
01691     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01692     <<">( value type )"
01693     );
01694   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01695     charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr());
01696   comm.readySend( charSendBuffer.getCharBufferView(), destRank );
01697 }
01698 
01699 
01700 template<typename Ordinal, typename Packet>
01701 void Teuchos::readySend(
01702   const Comm<Ordinal>& comm,
01703   const Packet &send,
01704   const int destRank
01705   )
01706 {
01707   readySend<Ordinal, Packet>( comm, arrayView(&send,1), destRank );
01708 }
01709 
01710 template<typename Ordinal, typename Packet, typename Serializer>
01711 void Teuchos::readySend(
01712   const Comm<Ordinal>& comm,
01713   const Serializer& serializer,
01714   const ArrayView<const Packet> &sendBuffer,
01715   const int destRank
01716   )
01717 {
01718   TEUCHOS_COMM_TIME_MONITOR(
01719     "Teuchos::CommHelpers: readySend<"
01720     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01721     <<">( value type )"
01722     );
01723   ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01724     charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr(), serializer);
01725   comm.readySend( charSendBuffer.getCharBufferView(), destRank );
01726 }
01727 
01728 template<typename Ordinal, typename Packet>
01729 Teuchos::RCP<Teuchos::CommRequest>
01730 Teuchos::isend(
01731   const Comm<Ordinal>& comm,
01732   const ArrayRCP<const Packet> &sendBuffer,
01733   const int destRank
01734   )
01735 {
01736   TEUCHOS_COMM_TIME_MONITOR(
01737     "Teuchos::CommHelpers: isend<"
01738     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01739     <<">( value type )"
01740     );
01741   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01742     charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr());
01743   RCP<CommRequest> commRequest = comm.isend(
01744     charSendBuffer.getCharBufferView(), destRank );
01745   set_extra_data( sendBuffer, "buffer", inOutArg(commRequest) );
01746   return commRequest;
01747 }
01748 
01749 
01750 template<typename Ordinal, typename Packet>
01751 Teuchos::RCP<Teuchos::CommRequest>
01752 Teuchos::isend(
01753   const Comm<Ordinal>& comm,
01754   const RCP<const Packet> &send,
01755   const int destRank
01756   )
01757 {
01758   const ArrayRCP<const Packet> sendBuffer =
01759     arcpWithEmbeddedObj( send.get(), 0, 1, send, false );
01760   // 2008/07/29: rabartl: Above: I need to write a helper function to create
01761   // new ArrayRCP object given a single object to copy.
01762   return isend<Ordinal, Packet>( comm, sendBuffer, destRank );
01763 }
01764 
01765 template<typename Ordinal, typename Packet, typename Serializer>
01766 Teuchos::RCP<Teuchos::CommRequest>
01767 Teuchos::isend(
01768   const Comm<Ordinal>& comm,
01769   const Serializer& serializer,
01770   const ArrayRCP<const Packet> &sendBuffer,
01771   const int destRank
01772   )
01773 {
01774   TEUCHOS_COMM_TIME_MONITOR(
01775     "Teuchos::CommHelpers: isend<"
01776     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01777     <<">( value type )"
01778     );
01779   ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01780     charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr(), serializer);
01781   RCP<CommRequest> commRequest = comm.isend(
01782     charSendBuffer.getCharBufferView(), destRank );
01783   set_extra_data( sendBuffer, "buffer", inOutArg(commRequest) );
01784   return commRequest;
01785 }
01786 
01787 template<typename Ordinal, typename Packet>
01788 Teuchos::RCP<Teuchos::CommRequest>
01789 Teuchos::ireceive(
01790   const Comm<Ordinal>& comm,
01791   const ArrayRCP<Packet> &recvBuffer,
01792   const int sourceRank
01793   )
01794 {
01795   typedef std::pair<RCP<CommRequest>, ArrayRCP<const Packet> > comm_buffer_pair_t;
01796   TEUCHOS_COMM_TIME_MONITOR(
01797     "Teuchos::CommHelpers: ireceive<"
01798     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01799     <<">( value type )"
01800     );
01801   ValueTypeSerializationBuffer<Ordinal,Packet>
01802     charRecvBuffer(recvBuffer.size(), recvBuffer.getRawPtr());
01803   RCP<CommRequest> commRequest = comm.ireceive(
01804     charRecvBuffer.getCharBufferView(), sourceRank );
01805   set_extra_data( recvBuffer, "buffer", inOutArg(commRequest) );
01806   return commRequest;
01807 }
01808 
01809 
01810 template<typename Ordinal, typename Packet>
01811 Teuchos::RCP<Teuchos::CommRequest>
01812 Teuchos::ireceive(
01813   const Comm<Ordinal>& comm,
01814   const RCP<Packet> &recv,
01815   const int sourceRank
01816   )
01817 {
01818   const ArrayRCP<Packet> recvBuffer =
01819     arcpWithEmbeddedObj( recv.get(), 0, 1, recv, false );
01820   // 2008/07/29: rabartl: Above: I need to write a helper function to create
01821   // new ArrayRCP object given a single object to copy.
01822   return ireceive<Ordinal, Packet>( comm, recvBuffer, sourceRank );
01823 }
01824 
01825 template<typename Ordinal, typename Packet, typename Serializer>
01826 Teuchos::RCP<Teuchos::CommRequest>
01827 Teuchos::ireceive(
01828   const Comm<Ordinal>& comm,
01829   const Serializer& serializer,
01830   const ArrayRCP<Packet> &recvBuffer,
01831   const int sourceRank
01832   )
01833 {
01834   typedef std::pair<RCP<CommRequest>, ArrayRCP<const Packet> > comm_buffer_pair_t;
01835   TEUCHOS_COMM_TIME_MONITOR(
01836     "Teuchos::CommHelpers: ireceive<"
01837     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01838     <<">( value type )"
01839     );
01840   ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
01841     charRecvBuffer(recvBuffer.size(), recvBuffer.getRawPtr(), serializer);
01842   RCP<CommRequest> commRequest = comm.ireceive(
01843     charRecvBuffer.getCharBufferView(), sourceRank );
01844   set_extra_data( recvBuffer, "buffer", inOutArg(commRequest) );
01845   return commRequest;
01846 }
01847 
01848 template<typename Ordinal>
01849 void Teuchos::waitAll(
01850   const Comm<Ordinal>& comm,
01851   const ArrayView<RCP<CommRequest> > &requests
01852   )
01853 {
01854   comm.waitAll(requests);
01855 }
01856 
01857 
01858 template<typename Ordinal>
01859 void Teuchos::wait(
01860   const Comm<Ordinal>& comm,
01861   const Ptr<RCP<CommRequest> > &request
01862   )
01863 {
01864   comm.wait(request);
01865   // NOTE: This will release the ArrayRCP to the buffer of data!
01866 }
01867 
01868 
01869 #endif // TEUCHOS_COMM_HELPERS_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines