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 (2008) Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
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 __TSQR_TrivialMessenger_hpp
00043 #define __TSQR_TrivialMessenger_hpp
00044 
00045 #include <Tsqr_MessengerBase.hpp>
00046 
00047 #include <algorithm>
00048 #include <sstream>
00049 #include <stdexcept>
00050 #include <vector>
00051 
00052 
00053 namespace TSQR { 
00054 
00065   template<class Datum>
00066   class TrivialMessenger : public MessengerBase<Datum> {
00067   public:
00069     TrivialMessenger () {}
00070 
00072     virtual ~TrivialMessenger() {}
00073 
00080     void 
00081     send (const Datum sendData[], 
00082     const int sendCount, 
00083     const int destProc, 
00084     const int tag) 
00085     {}
00086 
00093     void 
00094     recv (Datum recvData[], 
00095     const int recvCount, 
00096     const int srcProc, 
00097     const int tag) 
00098     {}
00099 
00116     void 
00117     swapData (const Datum sendData[], 
00118         Datum recvData[], 
00119         const int sendRecvCount, 
00120         const int destProc, 
00121         const int tag)
00122     {
00123       if (destProc != rank())
00124   {
00125     std::ostringstream os;
00126     os << "Destination rank " << destProc << " is invalid.  The only "
00127        << "valid rank for TSQR::TrivialMessenger is 0 (zero).";
00128       throw std::invalid_argument (os.str());
00129   }
00130       else if (sendRecvCount < 0)
00131   {
00132     std::ostringstream os;
00133     os << "sendRecvCount = " << sendRecvCount << " is invalid: "
00134        << "only nonnegative values are allowed.";
00135     throw std::invalid_argument (os.str());
00136   }
00137       else if (sendRecvCount == 0)
00138   return; // No data to exchange
00139       else 
00140   safeCopy (sendData, recvData, sendRecvCount);
00141     }
00142 
00144     Datum 
00145     globalSum (const Datum& inDatum) 
00146     {
00147       Datum outDatum (inDatum);
00148       return outDatum;
00149     }
00150 
00154     Datum 
00155     globalMin (const Datum& inDatum)
00156     {
00157       Datum outDatum (inDatum);
00158       return outDatum;
00159     }
00160 
00164     Datum 
00165     globalMax (const Datum& inDatum)
00166     {
00167       Datum outDatum (inDatum);
00168       return outDatum;
00169     }
00170 
00172     void
00173     globalVectorSum (const Datum inData[], 
00174          Datum outData[], 
00175          const int count) 
00176     {
00177       safeCopy (inData, outData, count);
00178     }
00179 
00181     void
00182     broadcast (Datum data[], 
00183          const int count,
00184          const int root)
00185     {}
00186 
00188     int rank () const { return 0; }
00189 
00191     int size () const { return 1; }
00192 
00194     void barrier () const { }
00195 
00196   private:
00197 
00202     void
00203     safeCopy (const Datum inData[],
00204         Datum outData[],
00205         const int count) 
00206     {
00207       // Check for nonaliasing of inData and outData.
00208       if (&inData[count-1] < &outData[0] ||
00209     &outData[count-1] < &inData[0])
00210   // The arrays don't overlap, so we can call std::copy.
00211   // std::copy assumes that the third argument does not
00212   // point to an element in the range of the first two
00213   // arguments.
00214   std::copy (inData, inData+count, outData);
00215       else
00216   {
00217     // If inData and outData do alias one another, use
00218     // the buffer as intermediate scratch space.
00219     buf_.resize (count);
00220     std::copy (inData, inData+count, buf_.begin());
00221     std::copy (buf_.begin(), buf_.end(), outData);
00222   }
00223     }
00224 
00227     std::vector<Datum> buf_;
00228   };
00229 } // namespace TSQR
00230 
00231 #endif // __TSQR_TrivialMessenger_hpp
00232 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends