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 // 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 
00158 template<typename Ordinal, typename Packet>
00159 void broadcast(
00160   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00161   const int rootRank, const ArrayView<const Ptr<Packet> > &buffer
00162   );
00163 
00169 template<typename Ordinal, typename Packet>
00170 void gatherAll(
00171   const Comm<Ordinal>& comm,
00172   const Ordinal sendCount, const Packet sendBuffer[],
00173   const Ordinal recvCount, Packet recvBuffer[]
00174   );
00175 
00181 template<typename Ordinal, typename Packet>
00182 void gatherAll(
00183   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00184   const Ordinal sendCount, const Packet*const sendBuffer[],
00185   const Ordinal recvCount, Packet*const recvBuffer[]
00186   );
00187 
00193 template<typename Ordinal, typename Packet>
00194 void reduceAll(
00195   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
00196   const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
00197   );
00198 
00204 template<typename Ordinal, typename Packet>
00205 void reduceAll(
00206   const Comm<Ordinal>& comm, const EReductionType reductType,
00207   const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
00208   );
00209 
00215 template<typename Ordinal, typename Packet>
00216 void reduceAll(
00217   const Comm<Ordinal>& comm, const EReductionType reductType,
00218   const Packet &send, const Ptr<Packet> &globalReduct
00219   );
00220 
00222 template<typename Ordinal, typename Packet>
00223 TEUCHOS_DEPRECATED void reduceAll(
00224   const Comm<Ordinal>& comm, const EReductionType reductType,
00225   const Packet &send, Packet *globalReduct
00226   )
00227 {
00228   reduceAll<Ordinal,Packet>(comm, reductType, send, ptr(globalReduct));
00229 }
00230 
00236 template<typename Ordinal, typename Packet>
00237 void reduceAll(
00238   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00239   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
00240   const Ordinal count, const Packet*const sendBuffer[], Packet*const globalReducts[]
00241   );
00242 
00248 template<typename Ordinal, typename Packet>
00249 void reduceAllAndScatter(
00250   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
00251   const Ordinal sendCount, const Packet sendBuffer[] ,
00252   const Ordinal recvCounts[], Packet myGlobalReducts[]
00253   );
00254 
00260 template<typename Ordinal, typename Packet>
00261 void reduceAllAndScatter(
00262   const Comm<Ordinal>& comm, const EReductionType reductType,
00263   const Ordinal sendCount, const Packet sendBuffer[] ,
00264   const Ordinal recvCounts[], Packet myGlobalReducts[]
00265   );
00266 
00272 template<typename Ordinal, typename Packet>
00273 void reduceAllAndScatter(
00274   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00275   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
00276   const Ordinal sendCount, const Packet*const sendBuffer[] ,
00277   const Ordinal recvCounts[], Packet*const myGlobalReducts[]
00278   );
00279 
00285 template<typename Ordinal, typename Packet>
00286 void scan(
00287   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
00288   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
00289   );
00290 
00296 template<typename Ordinal, typename Packet>
00297 void scan(
00298   const Comm<Ordinal>& comm, const EReductionType reductType,
00299   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
00300   );
00301 
00307 template<typename Ordinal, typename Packet>
00308 void scan(
00309   const Comm<Ordinal>& comm, const EReductionType reductType,
00310   const Packet &send, const Ptr<Packet> &scanReduct
00311   );
00312 
00314 template<typename Ordinal, typename Packet>
00315 TEUCHOS_DEPRECATED void scan(
00316   const Comm<Ordinal>& comm, const EReductionType reductType,
00317   const Packet &send, Packet *scanReduct
00318   )
00319 {
00320   scan(comm, reductType, send, ptr(scanReduct));
00321 }
00322 
00328 template<typename Ordinal, typename Packet>
00329 void scan(
00330   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00331   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
00332   const Ordinal count, const Packet*const sendBuffer[], Packet*const scanReducts[]
00333   );
00334 
00339 template<typename Ordinal, typename Packet>
00340 void send(
00341   const Comm<Ordinal>& comm,
00342   const Ordinal count, const Packet sendBuffer[], const int destRank
00343   );
00344 
00349 template<typename Ordinal, typename Packet>
00350 void send(
00351   const Comm<Ordinal>& comm,
00352   const Packet &send, const int destRank
00353   );
00354 
00361 template<typename Ordinal, typename Packet>
00362 void send(
00363   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00364   const Ordinal count, const Packet*const sendBuffer[], const int destRank
00365   );
00366 
00371 template<typename Ordinal, typename Packet>
00372 int receive(
00373   const Comm<Ordinal>& comm,
00374   const int sourceRank, const Ordinal count, Packet recvBuffer[] 
00375   );
00376 
00381 template<typename Ordinal, typename Packet>
00382 int receive(
00383   const Comm<Ordinal>& comm,
00384   const int sourceRank, Packet *recv 
00385   );
00386 
00391 template<typename Ordinal, typename Packet>
00392 int receive(
00393   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00394   const int sourceRank, const Ordinal count, Packet*const recvBuffer[] 
00395   );
00396 
00402 template<typename Ordinal, typename Packet>
00403 void readySend(
00404   const Comm<Ordinal>& comm,
00405   const ArrayView<const Packet> &sendBuffer,
00406   const int destRank
00407   );
00408 
00413 template<typename Ordinal, typename Packet>
00414 void readySend(
00415   const Comm<Ordinal>& comm,
00416   const Packet &send,
00417   const int destRank
00418   );
00419 
00424 template<typename Ordinal, typename Packet>
00425 RCP<CommRequest> isend(
00426   const Comm<Ordinal>& comm,
00427   const ArrayRCP<const Packet> &sendBuffer,
00428   const int destRank
00429   );
00430 
00431 
00436 template<typename Ordinal, typename Packet>
00437 RCP<CommRequest> isend(
00438   const Comm<Ordinal>& comm,
00439   const RCP<const Packet> &send,
00440   const int destRank
00441   );
00442 
00443 
00444 // 2008/07/29: rabartl: ToDo: Add reference semantics version of isend!
00445 
00446 
00451 template<typename Ordinal, typename Packet>
00452 RCP<CommRequest> ireceive(
00453   const Comm<Ordinal>& comm,
00454   const ArrayRCP<Packet> &recvBuffer,
00455   const int sourceRank
00456   );
00457 
00458 
00463 template<typename Ordinal, typename Packet>
00464 RCP<CommRequest> ireceive(
00465   const Comm<Ordinal>& comm,
00466   const RCP<Packet> &recv,
00467   const int sourceRank
00468   );
00469 
00470 
00471 // 2008/07/29: rabartl: ToDo: Add reference semantics version of ireceive!
00472 
00473 
00481 template<typename Ordinal>
00482 void waitAll(
00483   const Comm<Ordinal>& comm,
00484   const ArrayView<RCP<CommRequest> > &requests
00485   );
00486 
00494 template<typename Ordinal>
00495 void wait(
00496   const Comm<Ordinal>& comm,
00497   const Ptr<RCP<CommRequest> > &request
00498   );
00499 
00500 
00501 //
00502 // Standard reduction subclasses for objects that use value semantics
00503 //
00504 
00505 
00510 template<typename Ordinal, typename Packet>
00511 class SumValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00512 {
00513 public:
00515   void reduce(
00516     const Ordinal count,
00517     const Packet inBuffer[],
00518     Packet inoutBuffer[]
00519     ) const;
00520 };
00521 
00522 
00531 template<typename Ordinal, typename Packet>
00532 class MinValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00533 {
00534 public:
00536   void reduce(
00537     const Ordinal count,
00538     const Packet inBuffer[],
00539     Packet inoutBuffer[]
00540     ) const;
00541 };
00542 
00543 
00552 template<typename Ordinal, typename Packet>
00553 class MaxValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00554 {
00555 public:
00557   void reduce(
00558     const Ordinal count,
00559     const Packet inBuffer[],
00560     Packet inoutBuffer[]
00561     ) const;
00562 };
00563 
00564 
00569 template<typename Ordinal, typename Packet>
00570 class ANDValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
00571 {
00572 public:
00574   void reduce(
00575     const Ordinal count,
00576     const Packet inBuffer[],
00577     Packet inoutBuffer[]
00578     ) const;
00579 };
00580 
00581 
00582 // ////////////////////////////////////////////////////////////
00583 // Implementation details (not for geneal users to mess with)
00584 
00585 
00586 //
00587 // ReductionOp Utilities
00588 //
00589 
00590 
00591 namespace MixMaxUtilities {
00592 
00593 
00594 template<bool isComparable, typename Ordinal, typename Packet>
00595 class Min {};
00596 
00597 
00598 template<typename Ordinal, typename Packet>
00599 class Min<true,Ordinal,Packet> {
00600 public:
00601   static void min(
00602     const Ordinal count,
00603     const Packet inBuffer[],
00604     Packet inoutBuffer[]
00605     )
00606     {
00607       for( int i = 0; i < count; ++i )
00608         inoutBuffer[i] = TEUCHOS_MIN(inoutBuffer[i],inBuffer[i]);
00609     }
00610 };
00611 
00612 
00613 template<typename Ordinal, typename Packet>
00614 class Min<false,Ordinal,Packet> {
00615 public:
00616   static void min(
00617     const Ordinal,
00618     const Packet[],
00619     Packet[]
00620     )
00621     {
00622       TEST_FOR_EXCEPTION(
00623         true,std::logic_error,
00624         "Error, the type "<<TypeNameTraits<Packet>::name()
00625         <<" does not support comparison operations!"
00626         );
00627     }
00628 };
00629 
00630 
00631 template<bool isComparable, typename Ordinal, typename Packet>
00632 class Max {};
00633 
00634 
00635 template<typename Ordinal, typename Packet>
00636 class Max<true,Ordinal,Packet> {
00637 public:
00638   static void max(
00639     const Ordinal count,
00640     const Packet inBuffer[],
00641     Packet inoutBuffer[]
00642     )
00643     {
00644       for( int i = 0; i < count; ++i )
00645         inoutBuffer[i] = TEUCHOS_MAX(inoutBuffer[i],inBuffer[i]);
00646     }
00647 };
00648 
00649 
00650 template<typename Ordinal, typename Packet>
00651 class Max<false,Ordinal,Packet> {
00652 public:
00653   static void max(
00654     const Ordinal,
00655     const Packet[],
00656     Packet[]
00657     )
00658     {
00659       TEST_FOR_EXCEPTION(
00660         true,std::logic_error,
00661         "Error, the type "<<TypeNameTraits<Packet>::name()
00662         <<" does not support comparison operations!"
00663         );
00664     }
00665 };
00666 
00667 
00668 template<bool isComparable, typename Ordinal, typename Packet>
00669 class AND {};
00670 
00671 
00672 template<typename Ordinal, typename Packet>
00673 class AND<true,Ordinal,Packet> {
00674 public:
00675   static void andOp(
00676     const Ordinal count,
00677     const Packet inBuffer[],
00678     Packet inoutBuffer[]
00679     )
00680     {
00681       for( int i = 0; i < count; ++i )
00682         inoutBuffer[i] = inoutBuffer[i] && inBuffer[i];
00683     }
00684 };
00685 
00686 
00687 template<typename Ordinal, typename Packet>
00688 class AND<false,Ordinal,Packet> {
00689 public:
00690   static void andOp(
00691     const Ordinal,
00692     const Packet[],
00693     Packet[]
00694     )
00695     {
00696       TEST_FOR_EXCEPTION(
00697         true,std::logic_error,
00698         "Error, the type "<<TypeNameTraits<Packet>::name()
00699         <<" does not support logical AND operations!"
00700         );
00701     }
00702 };
00703 
00704 
00705 } // namespace MixMaxUtilities
00706 
00707 
00708 template<typename Ordinal, typename Packet>
00709 void SumValueReductionOp<Ordinal,Packet>::reduce(
00710   const Ordinal count,
00711   const Packet inBuffer[],
00712   Packet inoutBuffer[]
00713   ) const
00714 {
00715   for( int i = 0; i < count; ++i )
00716     inoutBuffer[i] += inBuffer[i];
00717 }
00718 
00719 
00720 template<typename Ordinal, typename Packet>
00721 void MinValueReductionOp<Ordinal,Packet>::reduce(
00722   const Ordinal count,
00723   const Packet inBuffer[],
00724   Packet inoutBuffer[]
00725   ) const
00726 {
00727   typedef ScalarTraits<Packet> ST;
00728   MixMaxUtilities::Min<ST::isComparable,Ordinal,Packet>::min(
00729     count,inBuffer,inoutBuffer
00730     );
00731 }
00732 
00733 
00734 template<typename Ordinal, typename Packet>
00735 void MaxValueReductionOp<Ordinal,Packet>::reduce(
00736   const Ordinal count,
00737   const Packet inBuffer[],
00738   Packet inoutBuffer[]
00739   ) const
00740 {
00741   typedef ScalarTraits<Packet> ST;
00742   MixMaxUtilities::Max<ST::isComparable,Ordinal,Packet>::max(
00743     count,inBuffer,inoutBuffer
00744     );
00745 }
00746 
00747 
00748 template<typename Ordinal, typename Packet>
00749 void ANDValueReductionOp<Ordinal,Packet>::reduce(
00750   const Ordinal count,
00751   const Packet inBuffer[],
00752   Packet inoutBuffer[]
00753   ) const
00754 {
00755   typedef ScalarTraits<Packet> ST;
00756   MixMaxUtilities::AND<ST::isComparable,Ordinal,Packet>::andOp(
00757     count,inBuffer,inoutBuffer
00758     );
00759 }
00760 
00761 
00762 } // namespace Teuchos
00763 
00764 
00765 // //////////////////////////
00766 // Template implemenations
00767 
00768 
00769 //
00770 // ReductionOp utilities
00771 //
00772 
00773 
00774 namespace Teuchos {
00775 
00776 
00777 // Not for the general user to use! I am returning a raw ReducionOp* pointer
00778 // to avoid the overhead of using RCP. However, given the use case
00779 // this is just fine since I can just use std::auto_ptr to make sure things
00780 // are deleted correctly.
00781 template<typename Ordinal, typename Packet>
00782 ValueTypeReductionOp<Ordinal,Packet>*
00783 createOp( const EReductionType reductType )
00784 {
00785   typedef ScalarTraits<Packet> ST;
00786   switch(reductType) {
00787     case REDUCE_SUM: {
00788       return new SumValueReductionOp<Ordinal,Packet>();
00789       break;
00790     }
00791     case REDUCE_MIN: {
00792       TEST_FOR_EXCEPT(!ST::isComparable);
00793       return new MinValueReductionOp<Ordinal,Packet>();
00794       break;
00795     }
00796     case REDUCE_MAX: {
00797       TEST_FOR_EXCEPT(!ST::isComparable);
00798       return new MaxValueReductionOp<Ordinal,Packet>();
00799       break;
00800     }
00801     case REDUCE_AND: {
00802       return new ANDValueReductionOp<Ordinal, Packet>();
00803       break;
00804     }
00805     default:
00806       TEST_FOR_EXCEPT(true);
00807   }
00808   return 0; // Will never be called!
00809 }
00810 
00811 
00812 } // namespace Teuchos
00813 
00814 
00815 //
00816 // Teuchos::Comm wrapper functions
00817 //
00818 
00819 
00820 template<typename Ordinal>
00821 int Teuchos::rank(const Comm<Ordinal>& comm)
00822 {
00823   return comm.getRank();
00824 }
00825 
00826 
00827 template<typename Ordinal>
00828 int Teuchos::size(const Comm<Ordinal>& comm)
00829 {
00830   return comm.getSize();
00831 }
00832 
00833 
00834 template<typename Ordinal>
00835 void Teuchos::barrier(const Comm<Ordinal>& comm)
00836 {
00837   TEUCHOS_COMM_TIME_MONITOR(
00838     "Teuchos::CommHelpers: barrier<"
00839     <<OrdinalTraits<Ordinal>::name()
00840     <<">()"
00841     );
00842   comm.barrier();
00843 }
00844 
00845 
00846 template<typename Ordinal, typename Packet>
00847 void Teuchos::broadcast(
00848   const Comm<Ordinal>& comm,
00849   const int rootRank, const Ordinal count, Packet buffer[]
00850   )
00851 {
00852   TEUCHOS_COMM_TIME_MONITOR(
00853     "Teuchos::CommHelpers: broadcast<"
00854     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
00855     <<">( value type )"
00856     );
00857   ValueTypeSerializationBuffer<Ordinal,Packet>
00858     charBuffer(count,buffer);
00859   comm.broadcast(
00860     rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
00861     );
00862 }
00863 
00864 
00865 template<typename Ordinal, typename Packet>
00866 void Teuchos::broadcast(
00867   const Comm<Ordinal>& comm,
00868   const int rootRank,
00869   const ArrayView<Packet> &buffer
00870   )
00871 {
00872   broadcast<Ordinal, Packet>(comm, rootRank, buffer.size(), buffer.getRawPtr() );
00873 }
00874 
00875 
00876 template<typename Ordinal, typename Packet>
00877 void Teuchos::broadcast(
00878   const Comm<Ordinal>& comm,
00879   const int rootRank, Packet *object
00880   )
00881 {
00882   broadcast<Ordinal,Packet>(comm,rootRank,1,object);
00883 }
00884 
00885 
00886 template<typename Ordinal, typename Packet>
00887 void Teuchos::broadcast(
00888   const Comm<Ordinal>& comm,
00889   const int rootRank, const Ptr<Packet> &object
00890   )
00891 {
00892   broadcast<Ordinal,Packet>(comm,rootRank,1,object.getRawPtr());
00893 }
00894 
00895 
00896 template<typename Ordinal, typename Packet>
00897 void Teuchos::broadcast(
00898   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00899   const int rootRank, const Ordinal count, Packet*const buffer[]
00900   )
00901 {
00902   TEUCHOS_COMM_TIME_MONITOR(
00903     "Teuchos::CommHelpers: broadcast<"
00904     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
00905     <<">( reference type )"
00906     );
00907   ReferenceTypeSerializationBuffer<Ordinal,Packet>
00908     charBuffer(serializer, count, buffer);
00909   comm.broadcast(
00910     rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
00911     );
00912 }
00913 
00914 
00915 template<typename Ordinal, typename Packet>
00916 void Teuchos::broadcast(
00917   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00918   const int rootRank, const ArrayView<const Ptr<Packet> > &buffer
00919   )
00920 {
00921   Array<Packet*> bufferPtrArray;
00922   for (int i = 0; i < buffer.size(); ++i) {
00923     bufferPtrArray.push_back(buffer[i].getRawPtr());
00924   }
00925   broadcast<Ordinal,Packet>(comm, serializer, rootRank,
00926     buffer.size(), bufferPtrArray.getRawPtr());
00927 }
00928 
00929 
00930 template<typename Ordinal, typename Packet>
00931 void Teuchos::gatherAll(
00932   const Comm<Ordinal>& comm,
00933   const Ordinal sendCount, const Packet sendBuffer[],
00934   const Ordinal recvCount, Packet recvBuffer[]
00935   )
00936 {
00937   TEUCHOS_COMM_TIME_MONITOR(
00938     "Teuchos::CommHelpers: gatherAll<"
00939     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
00940     <<">( value type )"
00941     );
00942   ConstValueTypeSerializationBuffer<Ordinal,Packet>
00943     charSendBuffer(sendCount,sendBuffer);
00944   ValueTypeSerializationBuffer<Ordinal,Packet>
00945     charRecvBuffer(recvCount,recvBuffer);
00946   comm.gatherAll(
00947     charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
00948     ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
00949     );
00950 }
00951 
00952 
00953 template<typename Ordinal, typename Packet>
00954 void Teuchos::gatherAll(
00955   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
00956   const Ordinal sendCount, const Packet*const sendBuffer[],
00957   const Ordinal recvCount, Packet*const recvBuffer[]
00958   )
00959 {
00960   TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
00961 }
00962 
00963 
00964 template<typename Ordinal, typename Packet>
00965 void Teuchos::reduceAll(
00966   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp
00967   ,const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
00968   )
00969 {
00970   TEUCHOS_COMM_TIME_MONITOR(
00971     "Teuchos::CommHelpers: reduceAll<"
00972     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
00973     <<">( value type, user-defined op )"
00974     );
00975   ConstValueTypeSerializationBuffer<Ordinal,Packet>
00976     charSendBuffer(count,sendBuffer);
00977   ValueTypeSerializationBuffer<Ordinal,Packet>
00978     charGlobalReducts(count,globalReducts);
00979   CharToValueTypeReductionOp<Ordinal,Packet>
00980     charReductOp(rcp(&reductOp,false));
00981   comm.reduceAll(
00982     charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
00983     ,charGlobalReducts.getCharBuffer()
00984     );
00985 }
00986 
00987 
00988 template<typename Ordinal, typename Packet>
00989 void Teuchos::reduceAll(
00990   const Comm<Ordinal>& comm, const EReductionType reductType,
00991   const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
00992   )
00993 {
00994   TEUCHOS_COMM_TIME_MONITOR(
00995     "Teuchos::CommHelpers: reduceAll<"
00996     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
00997     <<">( value type, "<<toString(reductType)<<" )"
00998     );
00999   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01000     reductOp(createOp<Ordinal,Packet>(reductType));
01001   reduceAll(comm,*reductOp,count,sendBuffer,globalReducts);
01002 }
01003 
01004 
01005 template<typename Ordinal, typename Packet>
01006 void Teuchos::reduceAll(
01007   const Comm<Ordinal>& comm, const EReductionType reductType
01008   ,const Packet &send, const Ptr<Packet> &globalReduct
01009   )
01010 {
01011   reduceAll<Ordinal,Packet>(comm, reductType, 1, &send, &*globalReduct);
01012 }
01013 
01014 
01015 template<typename Ordinal, typename Packet>
01016 void Teuchos::reduceAll(
01017   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01018   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
01019   const Ordinal count, const Packet*const sendBuffer[], Packet*const globalReducts[]
01020   )
01021 {
01022   TEUCHOS_COMM_TIME_MONITOR(
01023     "Teuchos::CommHelpers: reduceAll<"
01024     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01025     <<">( reference type )"
01026     );
01027   ConstReferenceTypeSerializationBuffer<Ordinal,Packet>
01028     charSendBuffer(serializer,count,sendBuffer);
01029   ReferenceTypeSerializationBuffer<Ordinal,Packet>
01030     charGlobalReducts(serializer,count,globalReducts);
01031   CharToReferenceTypeReductionOp<Ordinal,Packet>
01032     charReductOp(rcp(&serializer,false),rcp(&reductOp,false));
01033   comm.reduceAll(
01034     charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01035     ,charGlobalReducts.getCharBuffer()
01036     );
01037 }
01038 
01039 
01040 template<typename Ordinal, typename Packet>
01041 void Teuchos::reduceAllAndScatter(
01042   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
01043   const Ordinal sendCount, const Packet sendBuffer[] ,
01044   const Ordinal recvCounts[], Packet myGlobalReducts[]
01045   )
01046 {
01047 
01048   TEUCHOS_COMM_TIME_MONITOR(
01049     "Teuchos::CommHelpers: reduceAllAndScatter<"
01050     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01051     <<">( value type, user-defined op )"
01052     );
01053 
01054   const Ordinal size = Teuchos::size(comm);
01055   const Ordinal rank = Teuchos::rank(comm);
01056 
01057 #ifdef TEUCHOS_DEBUG
01058   Ordinal sumRecvCounts = 0;
01059   for( Ordinal i = 0; i < size; ++i )
01060     sumRecvCounts += recvCounts[static_cast<ptrdiff_t>(i)];
01061   TEST_FOR_EXCEPT(!(sumRecvCounts==sendCount));
01062 #endif
01063 
01064   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01065     charSendBuffer(sendCount,sendBuffer);
01066   ValueTypeSerializationBuffer<Ordinal,Packet>
01067     charMyGlobalReducts(recvCounts[static_cast<ptrdiff_t>(rank)], myGlobalReducts);
01068   CharToValueTypeReductionOp<Ordinal,Packet>
01069     charReductOp(rcp(&reductOp,false));
01070 
01071   const Ordinal
01072     packetSize = charSendBuffer.getBytes()/sendCount;
01073 
01074   WorkspaceStore* wss = get_default_workspace_store().get();
01075   Workspace<Ordinal> charRecvCounts(wss, size);
01076   for (Ordinal k = 0; k < size; ++k) {
01077     charRecvCounts[k] = as<Ordinal>(recvCounts[static_cast<ptrdiff_t>(k)] * packetSize);
01078   }
01079  
01080   comm.reduceAllAndScatter(
01081     charReductOp, charSendBuffer.getBytes(), charSendBuffer.getCharBuffer(),
01082     &charRecvCounts[0], charMyGlobalReducts.getCharBuffer()
01083     );
01084 
01085 }
01086 
01087 
01088 template<typename Ordinal, typename Packet>
01089 void Teuchos::reduceAllAndScatter(
01090   const Comm<Ordinal>& comm, const EReductionType reductType,
01091   const Ordinal sendCount, const Packet sendBuffer[] ,
01092   const Ordinal recvCounts[], Packet myGlobalReducts[]
01093   )
01094 {
01095   TEUCHOS_COMM_TIME_MONITOR(
01096     "Teuchos::CommHelpers: reduceAllAndScatter<"
01097     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01098     <<">( value type, "<<toString(reductType)<<" )"
01099     );
01100   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01101     reductOp(createOp<Ordinal,Packet>(reductType));
01102   reduceAllAndScatter(
01103     comm, *reductOp, sendCount, sendBuffer, recvCounts, myGlobalReducts
01104     );
01105 }
01106 
01107 
01108 template<typename Ordinal, typename Packet>
01109 void Teuchos::reduceAllAndScatter(
01110   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01111   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
01112   const Ordinal sendCount, const Packet*const sendBuffer[],
01113   const Ordinal recvCounts[], Packet*const myGlobalReducts[]
01114   )
01115 {
01116   TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01117 }
01118 
01119 
01120 template<typename Ordinal, typename Packet>
01121 void Teuchos::scan(
01122   const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
01123   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
01124   )
01125 {
01126   TEUCHOS_COMM_TIME_MONITOR(
01127     "Teuchos::CommHelpers: scan<"
01128     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01129     <<">( value type, user-defined op )"
01130     );
01131   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01132     charSendBuffer(count,sendBuffer);
01133   ValueTypeSerializationBuffer<Ordinal,Packet>
01134     charScanReducts(count,scanReducts);
01135   CharToValueTypeReductionOp<Ordinal,Packet>
01136     charReductOp(rcp(&reductOp,false));
01137   comm.scan(
01138     charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01139     ,charScanReducts.getCharBuffer()
01140     );
01141 }
01142 
01143 
01144 template<typename Ordinal, typename Packet>
01145 void Teuchos::scan(
01146   const Comm<Ordinal>& comm, const EReductionType reductType,
01147   const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
01148   )
01149 {
01150   TEUCHOS_COMM_TIME_MONITOR(
01151     "Teuchos::CommHelpers: scan<"
01152     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01153     <<">( value type, "<<toString(reductType)<<" )"
01154     );
01155   std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
01156     reductOp(createOp<Ordinal,Packet>(reductType));
01157   scan(comm,*reductOp,count,sendBuffer,scanReducts);
01158 }
01159 
01160 
01161 template<typename Ordinal, typename Packet>
01162 void Teuchos::scan(
01163   const Comm<Ordinal>& comm, const EReductionType reductType,
01164   const Packet &send, const Ptr<Packet> &scanReduct
01165   )
01166 {
01167   scan<Ordinal,Packet>(comm, reductType, 1, &send, &*scanReduct);
01168 }
01169 
01170 
01171 template<typename Ordinal, typename Packet>
01172 void Teuchos::scan(
01173   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01174   const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
01175   const Ordinal count, const Packet*const sendBuffer[], Packet*const scanReducts[]
01176   )
01177 {
01178   TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01179 }
01180 
01181 
01182 template<typename Ordinal, typename Packet>
01183 void Teuchos::send(
01184   const Comm<Ordinal>& comm,
01185   const Ordinal count, const Packet sendBuffer[], const int destRank
01186   )
01187 {
01188   TEUCHOS_COMM_TIME_MONITOR(
01189     "Teuchos::CommHelpers: send<"
01190     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01191     <<">( value type )"
01192     );
01193   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01194     charSendBuffer(count,sendBuffer);
01195   comm.send(
01196     charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
01197     ,destRank
01198     );
01199 }
01200 
01201 
01202 template<typename Ordinal, typename Packet>
01203 void Teuchos::send(
01204   const Comm<Ordinal>& comm,
01205   const Packet &send, const int destRank
01206   )
01207 {
01208   Teuchos::send<Ordinal,Packet>(comm,1,&send,destRank);
01209 }
01210 
01211 
01212 template<typename Ordinal, typename Packet>
01213 void Teuchos::send(
01214   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01215   const Ordinal count, const Packet*const sendBuffer[], const int destRank
01216   )
01217 {
01218   TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01219 }
01220 
01221 template<typename Ordinal, typename Packet>
01222 int Teuchos::receive(
01223   const Comm<Ordinal>& comm,
01224   const int sourceRank, const Ordinal count, Packet recvBuffer[] 
01225   )
01226 {
01227   TEUCHOS_COMM_TIME_MONITOR(
01228     "Teuchos::CommHelpers: receive<"
01229     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01230     <<">( value type )"
01231     );
01232   ValueTypeSerializationBuffer<Ordinal,Packet>
01233     charRecvBuffer(count,recvBuffer);
01234   return comm.receive(
01235     sourceRank
01236     ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
01237     );
01238 }
01239 
01240 
01241 template<typename Ordinal, typename Packet>
01242 int Teuchos::receive(
01243   const Comm<Ordinal>& comm,
01244   const int sourceRank, Packet *recv 
01245   )
01246 {
01247   return Teuchos::receive<Ordinal,Packet>(comm,sourceRank,1,recv);
01248 }
01249 
01250 
01251 template<typename Ordinal, typename Packet>
01252 int Teuchos::receive(
01253   const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
01254   const int sourceRank, const Ordinal count, Packet*const recvBuffer[] 
01255   )
01256 {
01257   TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
01258 }
01259 
01260 
01261 template<typename Ordinal, typename Packet>
01262 void Teuchos::readySend(
01263   const Comm<Ordinal>& comm,
01264   const ArrayView<const Packet> &sendBuffer,
01265   const int destRank
01266   )
01267 {
01268   TEUCHOS_COMM_TIME_MONITOR(
01269     "Teuchos::CommHelpers: readySend<"
01270     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01271     <<">( value type )"
01272     );
01273   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01274     charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr());
01275   comm.readySend( charSendBuffer.getCharBufferView(), destRank );
01276 }
01277 
01278 
01279 template<typename Ordinal, typename Packet>
01280 void Teuchos::readySend(
01281   const Comm<Ordinal>& comm,
01282   const Packet &send,
01283   const int destRank
01284   )
01285 {
01286   readySend<Ordinal, Packet>( comm, arrayView(&send,1), destRank );
01287 }
01288 
01289 
01290 template<typename Ordinal, typename Packet>
01291 Teuchos::RCP<Teuchos::CommRequest>
01292 Teuchos::isend(
01293   const Comm<Ordinal>& comm,
01294   const ArrayRCP<const Packet> &sendBuffer,
01295   const int destRank
01296   )
01297 {
01298   TEUCHOS_COMM_TIME_MONITOR(
01299     "Teuchos::CommHelpers: isend<"
01300     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01301     <<">( value type )"
01302     );
01303   ConstValueTypeSerializationBuffer<Ordinal,Packet>
01304     charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr());
01305   RCP<CommRequest> commRequest = comm.isend(
01306     charSendBuffer.getCharBufferView(), destRank );
01307   set_extra_data( sendBuffer, "buffer", inOutArg(commRequest) );
01308   return commRequest;
01309 }
01310 
01311 
01312 template<typename Ordinal, typename Packet>
01313 Teuchos::RCP<Teuchos::CommRequest>
01314 Teuchos::isend(
01315   const Comm<Ordinal>& comm,
01316   const RCP<const Packet> &send,
01317   const int destRank
01318   )
01319 {
01320   const ArrayRCP<const Packet> sendBuffer =
01321     arcpWithEmbeddedObj( send.get(), 0, 1, send, false );
01322   // 2008/07/29: rabartl: Above: I need to write a helper function to create
01323   // new ArrayRCP object given a single object to copy.
01324   return isend<Ordinal, Packet>( comm, sendBuffer, destRank );
01325 }
01326 
01327 
01328 template<typename Ordinal, typename Packet>
01329 Teuchos::RCP<Teuchos::CommRequest>
01330 Teuchos::ireceive(
01331   const Comm<Ordinal>& comm,
01332   const ArrayRCP<Packet> &recvBuffer,
01333   const int sourceRank
01334   )
01335 {
01336   typedef std::pair<RCP<CommRequest>, ArrayRCP<const Packet> > comm_buffer_pair_t;
01337   TEUCHOS_COMM_TIME_MONITOR(
01338     "Teuchos::CommHelpers: ireceive<"
01339     <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
01340     <<">( value type )"
01341     );
01342   ValueTypeSerializationBuffer<Ordinal,Packet>
01343     charRecvBuffer(recvBuffer.size(), recvBuffer.getRawPtr());
01344   RCP<CommRequest> commRequest = comm.ireceive(
01345     charRecvBuffer.getCharBufferView(), sourceRank );
01346   set_extra_data( recvBuffer, "buffer", inOutArg(commRequest) );
01347   return commRequest;
01348 }
01349 
01350 
01351 template<typename Ordinal, typename Packet>
01352 Teuchos::RCP<Teuchos::CommRequest>
01353 Teuchos::ireceive(
01354   const Comm<Ordinal>& comm,
01355   const RCP<Packet> &recv,
01356   const int sourceRank
01357   )
01358 {
01359   const ArrayRCP<Packet> recvBuffer =
01360     arcpWithEmbeddedObj( recv.get(), 0, 1, recv, false );
01361   // 2008/07/29: rabartl: Above: I need to write a helper function to create
01362   // new ArrayRCP object given a single object to copy.
01363   return ireceive<Ordinal, Packet>( comm, recvBuffer, sourceRank );
01364 }
01365 
01366 
01367 template<typename Ordinal>
01368 void Teuchos::waitAll(
01369   const Comm<Ordinal>& comm,
01370   const ArrayView<RCP<CommRequest> > &requests
01371   )
01372 {
01373   comm.waitAll(requests);
01374 }
01375 
01376 
01377 template<typename Ordinal>
01378 void Teuchos::wait(
01379   const Comm<Ordinal>& comm,
01380   const Ptr<RCP<CommRequest> > &request
01381   )
01382 {
01383   comm.wait(request);
01384   // NOTE: This will release the ArrayRCP to the buffer of data!
01385 }
01386 
01387 
01388 #endif // TEUCHOS_COMM_HELPERS_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines