Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_DefaultSerialComm.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_SERIAL_COMM_HPP
00043 #define TEUCHOS_SERIAL_COMM_HPP
00044 
00045 #include "Teuchos_Comm.hpp"
00046 #include "Teuchos_OrdinalTraits.hpp"
00047 
00048 
00049 namespace Teuchos {
00050 
00051 
00056 template<typename Ordinal>
00057 class SerialComm : public Comm<Ordinal> {
00058 public:
00059 
00061 
00062 
00064   SerialComm();
00065 
00067   SerialComm(const SerialComm<Ordinal>& other);
00068 
00070 
00072 
00073 
00075   virtual int getRank() const;
00077   virtual int getSize() const;
00079   virtual void barrier() const;
00081   virtual void broadcast(
00082     const int rootRank, const Ordinal bytes, char buffer[]
00083     ) const;
00085   virtual void gatherAll(
00086     const Ordinal sendBytes, const char sendBuffer[]
00087     ,const Ordinal recvBytes, char recvBuffer[]
00088     ) const;
00090   virtual void reduceAll(
00091     const ValueTypeReductionOp<Ordinal,char> &reductOp
00092     ,const Ordinal bytes, const char sendBuffer[], char globalReducts[]
00093     ) const;
00095   virtual void reduceAllAndScatter(
00096     const ValueTypeReductionOp<Ordinal,char> &reductOp
00097     ,const Ordinal sendBytes, const char sendBuffer[]
00098     ,const Ordinal recvCounts[], char myGlobalReducts[]
00099     ) const;
00101   virtual void scan(
00102     const ValueTypeReductionOp<Ordinal,char> &reductOp
00103     ,const Ordinal bytes, const char sendBuffer[], char scanReducts[]
00104     ) const;
00106   virtual void send(
00107     const Ordinal bytes, const char sendBuffer[], const int destRank
00108     ) const;
00110   virtual int receive(
00111     const int sourceRank, const Ordinal bytes, char recvBuffer[]
00112     ) const;
00114   virtual void readySend(
00115     const ArrayView<const char> &sendBuffer,
00116     const int destRank
00117     ) const;
00119   virtual RCP<CommRequest> isend(
00120     const ArrayView<const char> &sendBuffer,
00121     const int destRank
00122     ) const;
00124   virtual RCP<CommRequest> ireceive(
00125     const ArrayView<char> &Buffer,
00126     const int sourceRank
00127     ) const;
00129   virtual void waitAll(
00130     const ArrayView<RCP<CommRequest> > &requests
00131     ) const;
00133   virtual void wait(
00134     const Ptr<RCP<CommRequest> > &request
00135     ) const;
00137   virtual RCP< Comm<Ordinal> > duplicate() const;
00139   virtual RCP< Comm<Ordinal> > split(const int color, const int key) const;
00141   virtual RCP< Comm<Ordinal> > createSubcommunicator(
00142     const ArrayView<const int> & ranks) const;
00143 
00145 
00147 
00148 
00150   std::string description() const;
00151 
00153   
00154 };
00155 
00156 
00161 template<typename Ordinal>
00162 RCP<SerialComm<Ordinal> > createSerialComm()
00163 {
00164   return Teuchos::rcp(new SerialComm<Ordinal>);
00165 }
00166 
00167 
00168 // ////////////////////////
00169 // Implementations
00170 
00171 
00172 // Constructors
00173 
00174 
00175 template<typename Ordinal>
00176 SerialComm<Ordinal>::SerialComm()
00177 {}
00178 
00179 template<typename Ordinal>
00180 SerialComm<Ordinal>::SerialComm(const SerialComm<Ordinal>& other)
00181 {}
00182 
00183 
00184 // Overridden from Comm
00185 
00186   
00187 template<typename Ordinal>
00188 int SerialComm<Ordinal>::getRank() const
00189 {
00190   return 0;
00191 }
00192 
00193   
00194 template<typename Ordinal>
00195 int SerialComm<Ordinal>::getSize() const
00196 {
00197   return 1;
00198 }
00199 
00200   
00201 template<typename Ordinal>
00202 void SerialComm<Ordinal>::barrier() const
00203 {
00204   // Nothing to do
00205 }
00206 
00207   
00208 template<typename Ordinal>
00209 void SerialComm<Ordinal>::broadcast(
00210   const int /*rootRank*/, const Ordinal /*bytes*/, char []/*buffer*/
00211   ) const
00212 {
00213   // Nothing to do
00214 }
00215 
00216   
00217 template<typename Ordinal>
00218 void SerialComm<Ordinal>::gatherAll(
00219   const Ordinal sendBytes, const char sendBuffer[]
00220   ,const Ordinal recvBytes, char recvBuffer[]
00221   ) const
00222 {
00223   (void)sendBytes;  // to remove "unused parameter" warning
00224   (void)recvBytes;
00225   (void)sendBuffer;
00226   (void)recvBuffer;
00227 #ifdef TEUCHOS_DEBUG
00228   TEUCHOS_TEST_FOR_EXCEPT(!(sendBytes==recvBytes));
00229 #endif
00230   std::copy(sendBuffer,sendBuffer+sendBytes,recvBuffer);
00231 }
00232 
00233   
00234 template<typename Ordinal>
00235 void SerialComm<Ordinal>::reduceAll(
00236   const ValueTypeReductionOp<Ordinal,char> &reductOp
00237   ,const Ordinal bytes, const char sendBuffer[], char globalReducts[]
00238   ) const
00239 {
00240   (void)reductOp;
00241   std::copy(sendBuffer,sendBuffer+bytes,globalReducts);
00242 }
00243 
00244   
00245 template<typename Ordinal>
00246 void SerialComm<Ordinal>::reduceAllAndScatter(
00247   const ValueTypeReductionOp<Ordinal,char> &reductOp
00248   ,const Ordinal sendBytes, const char sendBuffer[]
00249   ,const Ordinal recvCounts[], char myGlobalReducts[]
00250   ) const
00251 {
00252   // Ignore unused arguments
00253   (void)reductOp;
00254   (void)sendBytes;
00255   (void)sendBuffer;
00256   (void)recvCounts;
00257   (void)myGlobalReducts;
00258 
00259 #ifdef TEUCHOS_DEBUG
00260   TEUCHOS_TEST_FOR_EXCEPT( recvCounts==NULL || recvCounts[0] != sendBytes ); 
00261 #endif
00262   std::copy(sendBuffer,sendBuffer+sendBytes,myGlobalReducts);
00263 }
00264 
00265   
00266 template<typename Ordinal>
00267 void SerialComm<Ordinal>::scan(
00268   const ValueTypeReductionOp<Ordinal,char> &reductOp
00269   ,const Ordinal bytes, const char sendBuffer[], char scanReducts[]
00270   ) const
00271 {
00272   (void)reductOp;
00273   std::copy(sendBuffer,sendBuffer+bytes,scanReducts);
00274 }
00275 
00276   
00277 template<typename Ordinal>
00278 void SerialComm<Ordinal>::send(
00279   const Ordinal /*bytes*/, const char []/*sendBuffer*/, const int /*destRank*/
00280   ) const
00281 {
00282   TEUCHOS_TEST_FOR_EXCEPTION(
00283     true, std::logic_error
00284     ,"SerialComm<Ordinal>::send(...): Error, you can not call send(...) when you"
00285     " only have one process!"
00286     );
00287 }
00288 
00289   
00290 template<typename Ordinal>
00291 int SerialComm<Ordinal>::receive(
00292   const int /*sourceRank*/, const Ordinal /*bytes*/, char []/*recvBuffer*/
00293   ) const
00294 {
00295   TEUCHOS_TEST_FOR_EXCEPTION(
00296     true, std::logic_error
00297     ,"SerialComm<Ordinal>::receive(...): Error, you can not call receive(...) when you"
00298     " only have one process!"
00299     );
00300   // The next line will never be reached, but a return is required on some platforms
00301   return 0; 
00302 }
00303 
00304 
00305 template<typename Ordinal>
00306 void SerialComm<Ordinal>::readySend(
00307   const ArrayView<const char> &/*sendBuffer*/,
00308   const int /*destRank*/
00309   ) const
00310 {
00311   TEUCHOS_TEST_FOR_EXCEPTION(
00312     true, std::logic_error
00313     ,"SerialComm<Ordinal>::readySend(...): Error, you can not call readySend(...) when you"
00314     " only have one process!"
00315     );
00316 }
00317 
00318 
00319 template<typename Ordinal>
00320 RCP<CommRequest> SerialComm<Ordinal>::isend(
00321   const ArrayView<const char> &/*sendBuffer*/,
00322   const int /*destRank*/
00323   ) const
00324 {
00325   TEUCHOS_TEST_FOR_EXCEPT(true);
00326   return null;
00327 }
00328 
00329 
00330 template<typename Ordinal>
00331 RCP<CommRequest> SerialComm<Ordinal>::ireceive(
00332   const ArrayView<char> &/*Buffer*/,
00333   const int /*sourceRank*/
00334   ) const
00335 {
00336   TEUCHOS_TEST_FOR_EXCEPT(true);
00337   return null;
00338 }
00339 
00340 
00341 template<typename Ordinal>
00342 void SerialComm<Ordinal>::waitAll(
00343   const ArrayView<RCP<CommRequest> > &/*requests*/
00344   ) const
00345 {
00346   TEUCHOS_TEST_FOR_EXCEPT(true);
00347 }
00348 
00349 
00350 template<typename Ordinal>
00351 void SerialComm<Ordinal>::wait(
00352   const Ptr<RCP<CommRequest> > &/*request*/
00353   ) const
00354 {
00355   TEUCHOS_TEST_FOR_EXCEPT(true);
00356 }
00357 
00358 template< typename Ordinal>
00359 RCP< Comm<Ordinal> >
00360 SerialComm<Ordinal>::duplicate() const
00361 {
00362   return rcp(new SerialComm<Ordinal>(*this));
00363 }
00364 
00365 template<typename Ordinal>
00366 RCP< Comm<Ordinal> >
00367 SerialComm<Ordinal>::split(const int color, const int /*key*/) const
00368 {
00369   if (color < 0) {
00370     return RCP< Comm<Ordinal> >();
00371   }
00372   // Simply return a copy of this communicator.
00373   return rcp(new SerialComm<Ordinal>(*this));
00374 }
00375 
00376 template<typename Ordinal>
00377 RCP< Comm<Ordinal> >
00378 SerialComm<Ordinal>::createSubcommunicator(const ArrayView<const int> &ranks) const
00379 {
00380   if ((ranks.size()) == 1 && (ranks[0] == 0)) {
00381     return rcp(new SerialComm<Ordinal>(*this));
00382   } else {
00383     return RCP< Comm<Ordinal> >();
00384   }
00385 }
00386 
00387 // Overridden from Describable
00388 
00389 
00390 template<typename Ordinal>
00391 std::string SerialComm<Ordinal>::description() const
00392 {
00393   std::ostringstream oss;
00394   oss << "Teuchos::SerialComm<"<<OrdinalTraits<Ordinal>::name()<<">";
00395   return oss.str();
00396 }
00397 
00398 
00399 } // namespace Teuchos
00400 
00401 
00402 #endif // TEUCHOS_SERIAL_COMM_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines