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 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef TEUCHOS_COMM_HELPERS_HPP
00030 #define TEUCHOS_COMM_HELPERS_HPP
00031 
00032 #include "Teuchos_Comm.hpp"
00033 #include "Teuchos_CommUtilities.hpp"
00034 #include "Teuchos_SerializationTraitsHelpers.hpp"
00035 #include "Teuchos_ReductionOpHelpers.hpp"
00036 #include "Teuchos_SerializerHelpers.hpp"
00037 #include "Teuchos_ScalarTraits.hpp"
00038 #include "Teuchos_OrdinalTraits.hpp"
00039 #include "Teuchos_Array.hpp"
00040 #include "Teuchos_TypeNameTraits.hpp"
00041 #include "Teuchos_Workspace.hpp"
00042 #include "Teuchos_as.hpp"
00043 
00044 
00045 namespace Teuchos {
00046 
00047 
00048 //
00049 // Teuchos::Comm Helper Functions
00050 //
00051 
00057 enum EReductionType {
00058   REDUCE_SUM, 
00059   REDUCE_MIN, 
00060   REDUCE_MAX, 
00061   REDUCE_AND 
00062 };
00063 
00068 inline
00069 const char* toString( const EReductionType reductType )
00070 {
00071   switch(reductType) {
00072     case REDUCE_SUM: return "REDUCE_SUM";
00073     case REDUCE_MIN: return "REDUCE_MIN";
00074     case REDUCE_MAX: return "REDUCE_MAX";
00075     case REDUCE_AND: return "REDUCE_AND";
00076     default: TEST_FOR_EXCEPT(true);
00077   }
00078   return 0; // Will never be called
00079 }
00080 
00085 template<typename Ordinal>
00086 int rank(const Comm<Ordinal>& comm);
00087 
00092 template<typename Ordinal>
00093 int size(const Comm<Ordinal>& comm);
00094 
00099 template<typename Ordinal>
00100 void barrier(const Comm<Ordinal>& comm);
00101 
00106 template<typename Ordinal, typename Packet>
00107 void broadcast(
00108   const Comm<Ordinal>& comm,
00109   const int rootRank,
00110   const Ordinal count, Packet buffer[]
00111   );
00112 
00117 template<typename Ordinal, typename Packet>
00118 void broadcast(
00119   const Comm<Ordinal>& comm,
00120   const int rootRank,
00121   const ArrayView<Packet> &buffer
00122   );
00123 
00128 template<typename Ordinal, typename Packet>
00129 void broadcast(
00130   const Comm<Ordinal>& comm,
00131   const int rootRank, Packet *object
00132   );
00133 
00138 template<typename Ordinal, typename Packet>
00139 void broadcast(
00140   const Comm<Ordinal>& comm,
00141   const int rootRank, const Ptr<Packet> &object
00142   );
00143 
00148 template<typename Ordinal, typename Packet>
00149 void broadcast(
00150   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00151   const int rootRank, const Ordinal count, Packet*const buffer[]
00152   );
00153 
00159 template<typename Ordinal, typename Packet>
00160 void gatherAll(
00161   const Comm<Ordinal>& comm,
00162   const Ordinal sendCount, const Packet sendBuffer[],
00163   const Ordinal recvCount, Packet recvBuffer[]
00164   );
00165 
00171 template<typename Ordinal, typename Packet>
00172 void gatherAll(
00173   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00174   const Ordinal sendCount, const Packet*const sendBuffer[],
00175   const Ordinal recvCount, Packet*const recvBuffer[]
00176   );
00177 
00183 template<typename Ordinal, typename Packet>
00184 void reduceAll(
00185   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
00186   const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
00187   );
00188 
00194 template<typename Ordinal, typename Packet>
00195 void reduceAll(
00196   const Comm<Ordinal>& comm, const EReductionType reductType,
00197   const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
00198   );
00199 
00205 template<typename Ordinal, typename Packet>
00206 void reduceAll(
00207   const Comm<Ordinal>& comm, const EReductionType reductType,
00208   const Packet &send, const Ptr<Packet> &globalReduct
00209   );
00210 
00212 template<typename Ordinal, typename Packet>
00213 void reduceAll(
00214   const Comm<Ordinal>& comm, const EReductionType reductType,
00215   const Packet &send, Packet *globalReduct
00216   )
00217 {
00218   reduceAll<Ordinal,Packet>(comm, reductType, send, ptr(globalReduct));
00219 }
00220 
00226 template<typename Ordinal, typename Packet>
00227 void reduceAll(
00228   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00229   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
00230   const Ordinal count, const Packet*const sendBuffer[], Packet*const globalReducts[]
00231   );
00232 
00238 template<typename Ordinal, typename Packet>
00239 void reduceAllAndScatter(
00240   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
00241   const Ordinal sendCount, const Packet sendBuffer[] ,
00242   const Ordinal recvCounts[], Packet myGlobalReducts[]
00243   );
00244 
00250 template<typename Ordinal, typename Packet>
00251 void reduceAllAndScatter(
00252   const Comm<Ordinal>& comm, const EReductionType reductType,
00253   const Ordinal sendCount, const Packet sendBuffer[] ,
00254   const Ordinal recvCounts[], Packet myGlobalReducts[]
00255   );
00256 
00262 template<typename Ordinal, typename Packet>
00263 void reduceAllAndScatter(
00264   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00265   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
00266   const Ordinal sendCount, const Packet*const sendBuffer[] ,
00267   const Ordinal recvCounts[], Packet*const myGlobalReducts[]
00268   );
00269 
00275 template<typename Ordinal, typename Packet>
00276 void scan(
00277   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
00278   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
00279   );
00280 
00286 template<typename Ordinal, typename Packet>
00287 void scan(
00288   const Comm<Ordinal>& comm, const EReductionType reductType,
00289   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
00290   );
00291 
00297 template<typename Ordinal, typename Packet>
00298 void scan(
00299   const Comm<Ordinal>& comm, const EReductionType reductType,
00300   const Packet &send, const Ptr<Packet> &scanReduct
00301   );
00302 
00304 template<typename Ordinal, typename Packet>
00305 void scan(
00306   const Comm<Ordinal>& comm, const EReductionType reductType,
00307   const Packet &send, Packet *scanReduct
00308   )
00309 {
00310   scan(comm, reductType, send, ptr(scanReduct));
00311 }
00312 
00318 template<typename Ordinal, typename Packet>
00319 void scan(
00320   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00321   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
00322   const Ordinal count, const Packet*const sendBuffer[], Packet*const scanReducts[]
00323   );
00324 
00329 template<typename Ordinal, typename Packet>
00330 void send(
00331   const Comm<Ordinal>& comm,
00332   const Ordinal count, const Packet sendBuffer[], const int destRank
00333   );
00334 
00339 template<typename Ordinal, typename Packet>
00340 void send(
00341   const Comm<Ordinal>& comm,
00342   const Packet &send, const int destRank
00343   );
00344 
00351 template<typename Ordinal, typename Packet>
00352 void send(
00353   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00354   const Ordinal count, const Packet*const sendBuffer[], const int destRank
00355   );
00356 
00361 template<typename Ordinal, typename Packet>
00362 int receive(
00363   const Comm<Ordinal>& comm,
00364   const int sourceRank, const Ordinal count, Packet recvBuffer[] 
00365   );
00366 
00371 template<typename Ordinal, typename Packet>
00372 int receive(
00373   const Comm<Ordinal>& comm,
00374   const int sourceRank, Packet *recv 
00375   );
00376 
00381 template<typename Ordinal, typename Packet>
00382 int receive(
00383   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00384   const int sourceRank, const Ordinal count, Packet*const recvBuffer[] 
00385   );
00386 
00392 template<typename Ordinal, typename Packet>
00393 void readySend(
00394   const Comm<Ordinal>& comm,
00395   const ArrayView<const Packet> &sendBuffer,
00396   const int destRank
00397   );
00398 
00403 template<typename Ordinal, typename Packet>
00404 void readySend(
00405   const Comm<Ordinal>& comm,
00406   const Packet &send,
00407   const int destRank
00408   );
00409 
00414 template<typename Ordinal, typename Packet>
00415 RCP<CommRequest> isend(
00416   const Comm<Ordinal>& comm,
00417   const ArrayRCP<const Packet> &sendBuffer,
00418   const int destRank
00419   );
00420 
00421 
00426 template<typename Ordinal, typename Packet>
00427 RCP<CommRequest> isend(
00428   const Comm<Ordinal>& comm,
00429   const RCP<const Packet> &send,
00430   const int destRank
00431   );
00432 
00433 
00434 // 2008/07/29: rabartl: ToDo: Add reference semantics version of isend!
00435 
00436 
00441 template<typename Ordinal, typename Packet>
00442 RCP<CommRequest> ireceive(
00443   const Comm<Ordinal>& comm,
00444   const ArrayRCP<Packet> &recvBuffer,
00445   const int sourceRank
00446   );
00447 
00448 
00453 template<typename Ordinal, typename Packet>
00454 RCP<CommRequest> ireceive(
00455   const Comm<Ordinal>& comm,
00456   const RCP<Packet> &recv,
00457   const int sourceRank
00458   );
00459 
00460 
00461 // 2008/07/29: rabartl: ToDo: Add reference semantics version of ireceive!
00462 
00463 
00471 template<typename Ordinal>
00472 void waitAll(
00473   const Comm<Ordinal>& comm,
00474   const ArrayView<RCP<CommRequest> > &requests
00475   );
00476 
00484 template<typename Ordinal>
00485 void wait(
00486   const Comm<Ordinal>& comm,
00487   const Ptr<RCP<CommRequest> > &request
00488   );
00489 
00490 
00491 //
00492 // Standard reduction subclasses for objects that use value semantics
00493 //
00494 
00495 
00500 template<typename Ordinal, typename Packet>
00501 class SumValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00502 {
00503 public:
00505   void reduce(
00506     const Ordinal count,
00507     const Packet inBuffer[],
00508     Packet inoutBuffer[]
00509     ) const;
00510 };
00511 
00512 
00521 template<typename Ordinal, typename Packet>
00522 class MinValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00523 {
00524 public:
00526   void reduce(
00527     const Ordinal count,
00528     const Packet inBuffer[],
00529     Packet inoutBuffer[]
00530     ) const;
00531 };
00532 
00533 
00542 template<typename Ordinal, typename Packet>
00543 class MaxValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00544 {
00545 public:
00547   void reduce(
00548     const Ordinal count,
00549     const Packet inBuffer[],
00550     Packet inoutBuffer[]
00551     ) const;
00552 };
00553 
00554 
00559 template<typename Ordinal, typename Packet>
00560 class ANDValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00561 {
00562 public:
00564   void reduce(
00565     const Ordinal count,
00566     const Packet inBuffer[],
00567     Packet inoutBuffer[]
00568     ) const;
00569 };
00570 
00571 
00572 // ////////////////////////////////////////////////////////////
00573 // Implementation details (not for geneal users to mess with)
00574 
00575 
00576 //
00577 // ReductionOp Utilities
00578 //
00579 
00580 
00581 namespace MixMaxUtilities {
00582 
00583 
00584 template<bool isComparable, typename Ordinal, typename Packet>
00585 class Min {};
00586 
00587 
00588 template<typename Ordinal, typename Packet>
00589 class Min<true,Ordinal,Packet> {
00590 public:
00591   static void min(
00592     const Ordinal count,
00593     const Packet inBuffer[],
00594     Packet inoutBuffer[]
00595     )
00596     {
00597       for( int i = 0; i < count; ++i )
00598         inoutBuffer[i] = TEUCHOS_MIN(inoutBuffer[i],inBuffer[i]);
00599     }
00600 };
00601 
00602 
00603 template<typename Ordinal, typename Packet>
00604 class Min<false,Ordinal,Packet> {
00605 public:
00606   static void min(
00607     const Ordinal,
00608     const Packet[],
00609     Packet[]
00610     )
00611     {
00612       TEST_FOR_EXCEPTION(
00613         true,std::logic_error,
00614         "Error, the type "<<TypeNameTraits<Packet>::name()
00615         <<" does not support comparison operations!"
00616         );
00617     }
00618 };
00619 
00620 
00621 template<bool isComparable, typename Ordinal, typename Packet>
00622 class Max {};
00623 
00624 
00625 template<typename Ordinal, typename Packet>
00626 class Max<true,Ordinal,Packet> {
00627 public:
00628   static void max(
00629     const Ordinal count,
00630     const Packet inBuffer[],
00631     Packet inoutBuffer[]
00632     )
00633     {
00634       for( int i = 0; i < count; ++i )
00635         inoutBuffer[i] = TEUCHOS_MAX(inoutBuffer[i],inBuffer[i]);
00636     }
00637 };
00638 
00639 
00640 template<typename Ordinal, typename Packet>
00641 class Max<false,Ordinal,Packet> {
00642 public:
00643   static void max(
00644     const Ordinal,
00645     const Packet[],
00646     Packet[]
00647     )
00648     {
00649       TEST_FOR_EXCEPTION(
00650         true,std::logic_error,
00651         "Error, the type "<<TypeNameTraits<Packet>::name()
00652         <<" does not support comparison operations!"
00653         );
00654     }
00655 };
00656 
00657 
00658 template<bool isComparable, typename Ordinal, typename Packet>
00659 class AND {};
00660 
00661 
00662 template<typename Ordinal, typename Packet>
00663 class AND<true,Ordinal,Packet> {
00664 public:
00665   static void andOp(
00666     const Ordinal count,
00667     const Packet inBuffer[],
00668     Packet inoutBuffer[]
00669     )
00670     {
00671       for( int i = 0; i < count; ++i )
00672         inoutBuffer[i] = inoutBuffer[i] && inBuffer[i];
00673     }
00674 };
00675 
00676 
00677 template<typename Ordinal, typename Packet>
00678 class AND<false,Ordinal,Packet> {
00679 public:
00680   static void andOp(
00681     const Ordinal,
00682     const Packet[],
00683     Packet[]
00684     )
00685     {
00686       TEST_FOR_EXCEPTION(
00687         true,std::logic_error,
00688         "Error, the type "<<TypeNameTraits<Packet>::name()
00689         <<" does not support logical AND operations!"
00690         );
00691     }
00692 };
00693 
00694 
00695 } // namespace MixMaxUtilities
00696 
00697 
00698 template<typename Ordinal, typename Packet>
00699 void SumValueReductionOp<Ordinal,Packet>::reduce(
00700   const Ordinal count,
00701   const Packet inBuffer[],
00702   Packet inoutBuffer[]
00703   ) const
00704 {
00705   for( int i = 0; i < count; ++i )
00706     inoutBuffer[i] += inBuffer[i];
00707 }
00708 
00709 
00710 template<typename Ordinal, typename Packet>
00711 void MinValueReductionOp<Ordinal,Packet>::reduce(
00712   const Ordinal count,
00713   const Packet inBuffer[],
00714   Packet inoutBuffer[]
00715   ) const
00716 {
00717   typedef ScalarTraits<Packet> ST;
00718   MixMaxUtilities::Min<ST::isComparable,Ordinal,Packet>::min(
00719     count,inBuffer,inoutBuffer
00720     );
00721 }
00722 
00723 
00724 template<typename Ordinal, typename Packet>
00725 void MaxValueReductionOp<Ordinal,Packet>::reduce(
00726   const Ordinal count,
00727   const Packet inBuffer[],
00728   Packet inoutBuffer[]
00729   ) const
00730 {
00731   typedef ScalarTraits<Packet> ST;
00732   MixMaxUtilities::Max<ST::isComparable,Ordinal,Packet>::max(
00733     count,inBuffer,inoutBuffer
00734     );
00735 }
00736 
00737 
00738 template<typename Ordinal, typename Packet>
00739 void ANDValueReductionOp<Ordinal,Packet>::reduce(
00740   const Ordinal count,
00741   const Packet inBuffer[],
00742   Packet inoutBuffer[]
00743   ) const
00744 {
00745   typedef ScalarTraits<Packet> ST;
00746   MixMaxUtilities::AND<ST::isComparable,Ordinal,Packet>::andOp(
00747     count,inBuffer,inoutBuffer
00748     );
00749 }
00750 
00751 
00752 } // namespace Teuchos
00753 
00754 
00755 // //////////////////////////
00756 // Template implemenations
00757 
00758 
00759 //
00760 // ReductionOp utilities
00761 //
00762 
00763 
00764 namespace Teuchos {
00765 
00766 
00767 // Not for the general user to use! I am returning a raw ReducionOp* pointer
00768 // to avoid the overhead of using RCP. However, given the use case
00769 // this is just fine since I can just use std::auto_ptr to make sure things
00770 // are deleted correctly.
00771 template<typename Ordinal, typename Packet>
00772 ValueTypeReductionOp<Ordinal,Packet>*
00773 createOp( const EReductionType reductType )
00774 {
00775   typedef ScalarTraits<Packet> ST;
00776   switch(reductType) {
00777     case REDUCE_SUM: {
00778       return new SumValueReductionOp<Ordinal,Packet>();
00779       break;
00780     }
00781     case REDUCE_MIN: {
00782       TEST_FOR_EXCEPT(!ST::isComparable);
00783       return new MinValueReductionOp<Ordinal,Packet>();
00784       break;
00785     }
00786     case REDUCE_MAX: {
00787       TEST_FOR_EXCEPT(!ST::isComparable);
00788       return new MaxValueReductionOp<Ordinal,Packet>();
00789       break;
00790     }
00791     case REDUCE_AND: {
00792       return new ANDValueReductionOp<Ordinal, Packet>();
00793       break;
00794     }
00795     default:
00796       TEST_FOR_EXCEPT(true);
00797   }
00798   return 0; // Will never be called!
00799 }
00800 
00801 
00802 } // namespace Teuchos
00803 
00804 
00805 //
00806 // Teuchos::Comm wrapper functions
00807 //
00808 
00809 
00810 template<typename Ordinal>
00811 int Teuchos::rank(const Comm<Ordinal>& comm)
00812 {
00813   return comm.getRank();
00814 }
00815 
00816 
00817 template<typename Ordinal>
00818 int Teuchos::size(const Comm<Ordinal>& comm)
00819 {
00820   return comm.getSize();
00821 }
00822 
00823 
00824 template<typename Ordinal>
00825 void Teuchos::barrier(const Comm<Ordinal>& comm)
00826 {
00827   TEUCHOS_COMM_TIME_MONITOR(
00828     "Teuchos::CommHelpers: barrier<"
00829     <<OrdinalTraits<Ordinal>::name()
00830     <<">()"
00831     );
00832   comm.barrier();
00833 }
00834 
00835 
00836 template<typename Ordinal, typename Packet>
00837 void Teuchos::broadcast(
00838   const Comm<Ordinal>& comm,
00839   const int rootRank, const Ordinal count, Packet buffer[]
00840   )
00841 {
00842   TEUCHOS_COMM_TIME_MONITOR(
00843     "Teuchos::CommHelpers: broadcast<"
00844     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
00845     <<">( value type )"
00846     );
00847   ValueTypeSerializationBuffer<Ordinal,Packet>
00848     charBuffer(count,buffer);
00849   comm.broadcast(
00850     rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
00851     );
00852 }
00853 
00854 
00855 template<typename Ordinal, typename Packet>
00856 void Teuchos::broadcast(
00857   const Comm<Ordinal>& comm,
00858   const int rootRank,
00859   const ArrayView<Packet> &buffer
00860   )
00861 {
00862   broadcast<Ordinal, Packet>(comm, rootRank, buffer.size(), buffer.getRawPtr() );
00863 }
00864 
00865 
00866 template<typename Ordinal, typename Packet>
00867 void Teuchos::broadcast(
00868   const Comm<Ordinal>& comm,
00869   const int rootRank, Packet *object
00870   )
00871 {
00872   broadcast<Ordinal,Packet>(comm,rootRank,1,object);
00873 }
00874 
00875 
00876 template<typename Ordinal, typename Packet>
00877 void Teuchos::broadcast(
00878   const Comm<Ordinal>& comm,
00879   const int rootRank, const Ptr<Packet> &object
00880   )
00881 {
00882   broadcast<Ordinal,Packet>(comm,rootRank,1,object.getRawPtr());
00883 }
00884 
00885 
00886 template<typename Ordinal, typename Packet>
00887 void Teuchos::broadcast(
00888   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00889   const int rootRank, const Ordinal count, Packet*const buffer[]
00890   )
00891 {
00892   TEUCHOS_COMM_TIME_MONITOR(
00893     "Teuchos::CommHelpers: broadcast<"
00894     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
00895     <<">( reference type )"
00896     );
00897   ReferenceTypeSerializationBuffer<Ordinal,Packet>
00898     charBuffer(serializer,count,buffer);
00899   comm.broadcast(
00900     rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
00901     );
00902 }
00903 
00904 
00905 template<typename Ordinal, typename Packet>
00906 void Teuchos::gatherAll(
00907   const Comm<Ordinal>& comm,
00908   const Ordinal sendCount, const Packet sendBuffer[],
00909   const Ordinal recvCount, Packet recvBuffer[]
00910   )
00911 {
00912   TEUCHOS_COMM_TIME_MONITOR(
00913     "Teuchos::CommHelpers: gatherAll<"
00914     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
00915     <<">( value type )"
00916     );
00917   ConstValueTypeSerializationBuffer<Ordinal,Packet>
00918     charSendBuffer(sendCount,sendBuffer);
00919   ValueTypeSerializationBuffer<Ordinal,Packet>
00920     charRecvBuffer(recvCount,recvBuffer);
00921   comm.gatherAll(
00922     charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
00923     ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
00924     );
00925 }
00926 
00927 
00928 template<typename Ordinal, typename Packet>
00929 void Teuchos::gatherAll(
00930   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00931   const Ordinal sendCount, const Packet*const sendBuffer[],
00932   const Ordinal recvCount, Packet*const recvBuffer[]
00933   )
00934 {
00935   TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
00936 }
00937 
00938 
00939 template<typename Ordinal, typename Packet>
00940 void Teuchos::reduceAll(
00941   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp
00942   ,const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
00943   )
00944 {
00945   TEUCHOS_COMM_TIME_MONITOR(
00946     "Teuchos::CommHelpers: reduceAll<"
00947     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
00948     <<">( value type, user-defined op )"
00949     );
00950   ConstValueTypeSerializationBuffer<Ordinal,Packet>
00951     charSendBuffer(count,sendBuffer);
00952   ValueTypeSerializationBuffer<Ordinal,Packet>
00953     charGlobalReducts(count,globalReducts);
00954   CharToValueTypeReductionOp<Ordinal,Packet>
00955     charReductOp(rcp(&reductOp,false));
00956   comm.reduceAll(
00957     charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
00958     ,charGlobalReducts.getCharBuffer()
00959     );
00960 }
00961 
00962 
00963 template<typename Ordinal, typename Packet>
00964 void Teuchos::reduceAll(
00965   const Comm<Ordinal>& comm, const EReductionType reductType,
00966   const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
00967   )
00968 {
00969   TEUCHOS_COMM_TIME_MONITOR(
00970     "Teuchos::CommHelpers: reduceAll<"
00971     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
00972     <<">( value type, "<<toString(reductType)<<" )"
00973     );
00974   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
00975     reductOp(createOp<Ordinal,Packet>(reductType));
00976   reduceAll(comm,*reductOp,count,sendBuffer,globalReducts);
00977 }
00978 
00979 
00980 template<typename Ordinal, typename Packet>
00981 void Teuchos::reduceAll(
00982   const Comm<Ordinal>& comm, const EReductionType reductType
00983   ,const Packet &send, const Ptr<Packet> &globalReduct
00984   )
00985 {
00986   reduceAll<Ordinal,Packet>(comm, reductType, 1, &send, &*globalReduct);
00987 }
00988 
00989 
00990 template<typename Ordinal, typename Packet>
00991 void Teuchos::reduceAll(
00992   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00993   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
00994   const Ordinal count, const Packet*const sendBuffer[], Packet*const globalReducts[]
00995   )
00996 {
00997   TEUCHOS_COMM_TIME_MONITOR(
00998     "Teuchos::CommHelpers: reduceAll<"
00999     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01000     <<">( reference type )"
01001     );
01002   ConstReferenceTypeSerializationBuffer<Ordinal,Packet>
01003     charSendBuffer(serializer,count,sendBuffer);
01004   ReferenceTypeSerializationBuffer<Ordinal,Packet>
01005     charGlobalReducts(serializer,count,globalReducts);
01006   CharToReferenceTypeReductionOp<Ordinal,Packet>
01007     charReductOp(rcp(&serializer,false),rcp(&reductOp,false));
01008   comm.reduceAll(
01009     charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01010     ,charGlobalReducts.getCharBuffer()
01011     );
01012 }
01013 
01014 
01015 template<typename Ordinal, typename Packet>
01016 void Teuchos::reduceAllAndScatter(
01017   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
01018   const Ordinal sendCount, const Packet sendBuffer[] ,
01019   const Ordinal recvCounts[], Packet myGlobalReducts[]
01020   )
01021 {
01022 
01023   TEUCHOS_COMM_TIME_MONITOR(
01024     "Teuchos::CommHelpers: reduceAllAndScatter<"
01025     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01026     <<">( value type, user-defined op )"
01027     );
01028 
01029   const Ordinal size = Teuchos::size(comm);
01030   const Ordinal rank = Teuchos::rank(comm);
01031 
01032 #ifdef TEUCHOS_DEBUG
01033   Ordinal sumRecvCounts = 0;
01034   for( Ordinal i = 0; i < size; ++i )
01035     sumRecvCounts += recvCounts[static_cast<ptrdiff_t>(i)];
01036   TEST_FOR_EXCEPT(!(sumRecvCounts==sendCount));
01037 #endif
01038 
01039   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01040     charSendBuffer(sendCount,sendBuffer);
01041   ValueTypeSerializationBuffer<Ordinal,Packet>
01042     charMyGlobalReducts(recvCounts[static_cast<ptrdiff_t>(rank)], myGlobalReducts);
01043   CharToValueTypeReductionOp<Ordinal,Packet>
01044     charReductOp(rcp(&reductOp,false));
01045 
01046   const Ordinal
01047     packetSize = charSendBuffer.getBytes()/sendCount;
01048 
01049   WorkspaceStore* wss = get_default_workspace_store().get();
01050   Workspace<Ordinal> charRecvCounts(wss, size);
01051   for (Ordinal k = 0; k < size; ++k) {
01052     charRecvCounts[k] = as<Ordinal>(recvCounts[static_cast<ptrdiff_t>(k)] * packetSize);
01053   }
01054  
01055   comm.reduceAllAndScatter(
01056     charReductOp, charSendBuffer.getBytes(), charSendBuffer.getCharBuffer(),
01057     &charRecvCounts[0], charMyGlobalReducts.getCharBuffer()
01058     );
01059 
01060 }
01061 
01062 
01063 template<typename Ordinal, typename Packet>
01064 void Teuchos::reduceAllAndScatter(
01065   const Comm<Ordinal>& comm, const EReductionType reductType,
01066   const Ordinal sendCount, const Packet sendBuffer[] ,
01067   const Ordinal recvCounts[], Packet myGlobalReducts[]
01068   )
01069 {
01070   TEUCHOS_COMM_TIME_MONITOR(
01071     "Teuchos::CommHelpers: reduceAllAndScatter<"
01072     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01073     <<">( value type, "<<toString(reductType)<<" )"
01074     );
01075   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01076     reductOp(createOp<Ordinal,Packet>(reductType));
01077   reduceAllAndScatter(
01078     comm, *reductOp, sendCount, sendBuffer, recvCounts, myGlobalReducts
01079     );
01080 }
01081 
01082 
01083 template<typename Ordinal, typename Packet>
01084 void Teuchos::reduceAllAndScatter(
01085   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01086   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
01087   const Ordinal sendCount, const Packet*const sendBuffer[],
01088   const Ordinal recvCounts[], Packet*const myGlobalReducts[]
01089   )
01090 {
01091   TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01092 }
01093 
01094 
01095 template<typename Ordinal, typename Packet>
01096 void Teuchos::scan(
01097   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
01098   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
01099   )
01100 {
01101   TEUCHOS_COMM_TIME_MONITOR(
01102     "Teuchos::CommHelpers: scan<"
01103     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01104     <<">( value type, user-defined op )"
01105     );
01106   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01107     charSendBuffer(count,sendBuffer);
01108   ValueTypeSerializationBuffer<Ordinal,Packet>
01109     charScanReducts(count,scanReducts);
01110   CharToValueTypeReductionOp<Ordinal,Packet>
01111     charReductOp(rcp(&reductOp,false));
01112   comm.scan(
01113     charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01114     ,charScanReducts.getCharBuffer()
01115     );
01116 }
01117 
01118 
01119 template<typename Ordinal, typename Packet>
01120 void Teuchos::scan(
01121   const Comm<Ordinal>& comm, const EReductionType reductType,
01122   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
01123   )
01124 {
01125   TEUCHOS_COMM_TIME_MONITOR(
01126     "Teuchos::CommHelpers: scan<"
01127     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01128     <<">( value type, "<<toString(reductType)<<" )"
01129     );
01130   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01131     reductOp(createOp<Ordinal,Packet>(reductType));
01132   scan(comm,*reductOp,count,sendBuffer,scanReducts);
01133 }
01134 
01135 
01136 template<typename Ordinal, typename Packet>
01137 void Teuchos::scan(
01138   const Comm<Ordinal>& comm, const EReductionType reductType,
01139   const Packet &send, const Ptr<Packet> &scanReduct
01140   )
01141 {
01142   scan<Ordinal,Packet>(comm, reductType, 1, &send, &*scanReduct);
01143 }
01144 
01145 
01146 template<typename Ordinal, typename Packet>
01147 void Teuchos::scan(
01148   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01149   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
01150   const Ordinal count, const Packet*const sendBuffer[], Packet*const scanReducts[]
01151   )
01152 {
01153   TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01154 }
01155 
01156 
01157 template<typename Ordinal, typename Packet>
01158 void Teuchos::send(
01159   const Comm<Ordinal>& comm,
01160   const Ordinal count, const Packet sendBuffer[], const int destRank
01161   )
01162 {
01163   TEUCHOS_COMM_TIME_MONITOR(
01164     "Teuchos::CommHelpers: send<"
01165     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01166     <<">( value type )"
01167     );
01168   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01169     charSendBuffer(count,sendBuffer);
01170   comm.send(
01171     charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01172     ,destRank
01173     );
01174 }
01175 
01176 
01177 template<typename Ordinal, typename Packet>
01178 void Teuchos::send(
01179   const Comm<Ordinal>& comm,
01180   const Packet &send, const int destRank
01181   )
01182 {
01183   Teuchos::send<Ordinal,Packet>(comm,1,&send,destRank);
01184 }
01185 
01186 
01187 template<typename Ordinal, typename Packet>
01188 void Teuchos::send(
01189   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01190   const Ordinal count, const Packet*const sendBuffer[], const int destRank
01191   )
01192 {
01193   TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01194 }
01195 
01196 template<typename Ordinal, typename Packet>
01197 int Teuchos::receive(
01198   const Comm<Ordinal>& comm,
01199   const int sourceRank, const Ordinal count, Packet recvBuffer[] 
01200   )
01201 {
01202   TEUCHOS_COMM_TIME_MONITOR(
01203     "Teuchos::CommHelpers: receive<"
01204     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01205     <<">( value type )"
01206     );
01207   ValueTypeSerializationBuffer<Ordinal,Packet>
01208     charRecvBuffer(count,recvBuffer);
01209   return comm.receive(
01210     sourceRank
01211     ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
01212     );
01213 }
01214 
01215 
01216 template<typename Ordinal, typename Packet>
01217 int Teuchos::receive(
01218   const Comm<Ordinal>& comm,
01219   const int sourceRank, Packet *recv 
01220   )
01221 {
01222   return Teuchos::receive<Ordinal,Packet>(comm,sourceRank,1,recv);
01223 }
01224 
01225 
01226 template<typename Ordinal, typename Packet>
01227 int Teuchos::receive(
01228   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01229   const int sourceRank, const Ordinal count, Packet*const recvBuffer[] 
01230   )
01231 {
01232   TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01233 }
01234 
01235 
01236 template<typename Ordinal, typename Packet>
01237 void Teuchos::readySend(
01238   const Comm<Ordinal>& comm,
01239   const ArrayView<const Packet> &sendBuffer,
01240   const int destRank
01241   )
01242 {
01243   TEUCHOS_COMM_TIME_MONITOR(
01244     "Teuchos::CommHelpers: readySend<"
01245     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01246     <<">( value type )"
01247     );
01248   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01249     charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr());
01250   comm.readySend( charSendBuffer.getCharBufferView(), destRank );
01251 }
01252 
01253 
01254 template<typename Ordinal, typename Packet>
01255 void Teuchos::readySend(
01256   const Comm<Ordinal>& comm,
01257   const Packet &send,
01258   const int destRank
01259   )
01260 {
01261   readySend<Ordinal, Packet>( comm, arrayView(&send,1), destRank );
01262 }
01263 
01264 
01265 template<typename Ordinal, typename Packet>
01266 Teuchos::RCP<Teuchos::CommRequest>
01267 Teuchos::isend(
01268   const Comm<Ordinal>& comm,
01269   const ArrayRCP<const Packet> &sendBuffer,
01270   const int destRank
01271   )
01272 {
01273   TEUCHOS_COMM_TIME_MONITOR(
01274     "Teuchos::CommHelpers: isend<"
01275     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01276     <<">( value type )"
01277     );
01278   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01279     charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr());
01280   RCP<CommRequest> commRequest = comm.isend(
01281     charSendBuffer.getCharBufferView(), destRank );
01282   set_extra_data( sendBuffer, "buffer", inOutArg(commRequest) );
01283   return commRequest;
01284 }
01285 
01286 
01287 template<typename Ordinal, typename Packet>
01288 Teuchos::RCP<Teuchos::CommRequest>
01289 Teuchos::isend(
01290   const Comm<Ordinal>& comm,
01291   const RCP<const Packet> &send,
01292   const int destRank
01293   )
01294 {
01295   const ArrayRCP<const Packet> sendBuffer =
01296     arcpWithEmbeddedObj( send.get(), 0, 1, send, false );
01297   // 2008/07/29: rabartl: Above: I need to write a helper function to create
01298   // new ArrayRCP object given a single object to copy.
01299   return isend<Ordinal, Packet>( comm, sendBuffer, destRank );
01300 }
01301 
01302 
01303 template<typename Ordinal, typename Packet>
01304 Teuchos::RCP<Teuchos::CommRequest>
01305 Teuchos::ireceive(
01306   const Comm<Ordinal>& comm,
01307   const ArrayRCP<Packet> &recvBuffer,
01308   const int sourceRank
01309   )
01310 {
01311   typedef std::pair<RCP<CommRequest>, ArrayRCP<const Packet> > comm_buffer_pair_t;
01312   TEUCHOS_COMM_TIME_MONITOR(
01313     "Teuchos::CommHelpers: ireceive<"
01314     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01315     <<">( value type )"
01316     );
01317   ValueTypeSerializationBuffer<Ordinal,Packet>
01318     charRecvBuffer(recvBuffer.size(), recvBuffer.getRawPtr());
01319   RCP<CommRequest> commRequest = comm.ireceive(
01320     charRecvBuffer.getCharBufferView(), sourceRank );
01321   set_extra_data( recvBuffer, "buffer", inOutArg(commRequest) );
01322   return commRequest;
01323 }
01324 
01325 
01326 template<typename Ordinal, typename Packet>
01327 Teuchos::RCP<Teuchos::CommRequest>
01328 Teuchos::ireceive(
01329   const Comm<Ordinal>& comm,
01330   const RCP<Packet> &recv,
01331   const int sourceRank
01332   )
01333 {
01334   const ArrayRCP<Packet> recvBuffer =
01335     arcpWithEmbeddedObj( recv.get(), 0, 1, recv, false );
01336   // 2008/07/29: rabartl: Above: I need to write a helper function to create
01337   // new ArrayRCP object given a single object to copy.
01338   return ireceive<Ordinal, Packet>( comm, recvBuffer, sourceRank );
01339 }
01340 
01341 
01342 template<typename Ordinal>
01343 void Teuchos::waitAll(
01344   const Comm<Ordinal>& comm,
01345   const ArrayView<RCP<CommRequest> > &requests
01346   )
01347 {
01348   comm.waitAll(requests);
01349 }
01350 
01351 
01352 template<typename Ordinal>
01353 void Teuchos::wait(
01354   const Comm<Ordinal>& comm,
01355   const Ptr<RCP<CommRequest> > &request
01356   )
01357 {
01358   comm.wait(request);
01359   // NOTE: This will release the ArrayRCP to the buffer of data!
01360 }
01361 
01362 
01363 #endif // TEUCHOS_COMM_HELPERS_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Tue Oct 20 10:13:59 2009 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.1