Kokkos Node API and Local Linear Algebra Kernels Version of the Day
Tsqr_TrivialMessenger.hpp
00001 //@HEADER
00002 // ************************************************************************
00003 // 
00004 //          Kokkos: Node API and Parallel Node Kernels
00005 //              Copyright (2009) 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 __TSQR_TrivialMessenger_hpp
00030 #define __TSQR_TrivialMessenger_hpp
00031 
00032 #include <Tsqr_MessengerBase.hpp>
00033 
00034 #include <algorithm>
00035 #include <sstream>
00036 #include <stdexcept>
00037 #include <vector>
00038 
00039 
00040 namespace TSQR { 
00041 
00052   template<class Datum>
00053   class TrivialMessenger : public MessengerBase<Datum> {
00054   public:
00056     TrivialMessenger () {}
00057 
00059     virtual ~TrivialMessenger() {}
00060 
00067     void 
00068     send (const Datum sendData[], 
00069     const int sendCount, 
00070     const int destProc, 
00071     const int tag) 
00072     {}
00073 
00080     void 
00081     recv (Datum recvData[], 
00082     const int recvCount, 
00083     const int srcProc, 
00084     const int tag) 
00085     {}
00086 
00103     void 
00104     swapData (const Datum sendData[], 
00105         Datum recvData[], 
00106         const int sendRecvCount, 
00107         const int destProc, 
00108         const int tag)
00109     {
00110       if (destProc != rank())
00111   {
00112     std::ostringstream os;
00113     os << "Destination rank " << destProc << " is invalid.  The only "
00114        << "valid rank for TSQR::TrivialMessenger is 0 (zero).";
00115       throw std::invalid_argument (os.str());
00116   }
00117       else if (sendRecvCount < 0)
00118   {
00119     std::ostringstream os;
00120     os << "sendRecvCount = " << sendRecvCount << " is invalid: "
00121        << "only nonnegative values are allowed.";
00122     throw std::invalid_argument (os.str());
00123   }
00124       else if (sendRecvCount == 0)
00125   return; // No data to exchange
00126       else 
00127   safeCopy (sendData, recvData, sendRecvCount);
00128     }
00129 
00131     Datum 
00132     globalSum (const Datum& inDatum) 
00133     {
00134       Datum outDatum (inDatum);
00135       return outDatum;
00136     }
00137 
00141     Datum 
00142     globalMin (const Datum& inDatum)
00143     {
00144       Datum outDatum (inDatum);
00145       return outDatum;
00146     }
00147 
00151     Datum 
00152     globalMax (const Datum& inDatum)
00153     {
00154       Datum outDatum (inDatum);
00155       return outDatum;
00156     }
00157 
00159     void
00160     globalVectorSum (const Datum inData[], 
00161          Datum outData[], 
00162          const int count) 
00163     {
00164       safeCopy (inData, outData, count);
00165     }
00166 
00168     void
00169     broadcast (Datum data[], 
00170          const int count,
00171          const int root)
00172     {}
00173 
00175     int rank () const { return 0; }
00176 
00178     int size () const { return 1; }
00179 
00181     void barrier () const { }
00182 
00183   private:
00184 
00189     void
00190     safeCopy (const Datum inData[],
00191         Datum outData[],
00192         const int count) 
00193     {
00194       // Check for nonaliasing of inData and outData.
00195       if (&inData[count-1] < &outData[0] ||
00196     &outData[count-1] < &inData[0])
00197   // The arrays don't overlap, so we can call std::copy.
00198   // std::copy assumes that the third argument does not
00199   // point to an element in the range of the first two
00200   // arguments.
00201   std::copy (inData, inData+count, outData);
00202       else
00203   {
00204     // If inData and outData do alias one another, use
00205     // the buffer as intermediate scratch space.
00206     buf_.resize (count);
00207     std::copy (inData, inData+count, buf_.begin());
00208     std::copy (buf_.begin(), buf_.end(), outData);
00209   }
00210     }
00211 
00214     std::vector<Datum> buf_;
00215   };
00216 } // namespace TSQR
00217 
00218 #endif // __TSQR_TrivialMessenger_hpp
00219 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends