Kokkos Node API and Local Linear Algebra Kernels Version of the Day
Kokkos_MultiVectorKernelOps.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 KOKKOS_MULTIVECTOR_KERNELOPS_HPP
00043 #define KOKKOS_MULTIVECTOR_KERNELOPS_HPP
00044 
00045 #ifdef __CUDACC__
00046 #include <Teuchos_ScalarTraitsCUDA.hpp>
00047 #else
00048 #include <Teuchos_ScalarTraits.hpp>
00049 #endif
00050 
00051 
00052 #ifndef KERNEL_PREFIX
00053 #define KERNEL_PREFIX
00054 #endif
00055 
00056 namespace Kokkos {
00057 
00058   template <typename Scalar>
00059   struct InitOp {
00060     Scalar *x;
00061     Scalar alpha;
00062     inline KERNEL_PREFIX void execute(int i) const
00063     {
00064       x[i] = alpha;
00065     }
00066   };
00067 
00068   template <typename Scalar>
00069   struct AssignOp {
00070     Scalar *x;
00071     const Scalar *y;
00072     inline KERNEL_PREFIX void execute(int i) const
00073     {
00074       x[i] = y[i];
00075     }
00076   };
00077 
00078   template <typename Scalar>
00079   struct SingleScaleOp {
00080     Scalar alpha;
00081     Scalar *x;
00082     inline KERNEL_PREFIX void execute(int i) const
00083     {
00084       Scalar tmp = x[i];
00085       x[i] = alpha*tmp;
00086     }
00087   };
00088 
00089   template <typename Scalar>
00090   struct MVScaleOp {
00091     Scalar alpha;
00092     const Scalar *y;
00093     Scalar *x;
00094     inline KERNEL_PREFIX void execute(int i) const
00095     {
00096       x[i] = alpha*y[i];
00097     }
00098   };
00099 
00100   template <typename Scalar>
00101   struct MVElemMultOp {
00102     Scalar scalarX;
00103     Scalar scalarYZ;
00104     const Scalar *y;
00105     const Scalar *z;
00106     Scalar *x;
00107     inline KERNEL_PREFIX void execute(int i) const
00108     {
00109       x[i] = scalarX*x[i] + scalarYZ*y[i]*z[i];
00110     }
00111   };
00112 
00113   template <typename Scalar>
00114   struct AbsOp {
00115     const Scalar *y;
00116     Scalar *x;
00117     inline KERNEL_PREFIX void execute(int i) const
00118     {
00119       x[i] = Teuchos::ScalarTraits<Scalar>::magnitude(y[i]);
00120     }
00121   };
00122 
00123   template <typename Scalar>
00124   struct RecipOp {
00125     const Scalar *scale;
00126     Scalar *x;
00127     inline KERNEL_PREFIX void execute(int i) const
00128     {
00129       x[i] = Teuchos::ScalarTraits<Scalar>::one() / scale[i];
00130     }
00131   };
00132 
00133   template <typename Scalar>
00134   struct GESUMOp {
00135     const Scalar *x;
00136     Scalar *y;
00137     Scalar alpha, beta;
00138     inline KERNEL_PREFIX void execute(int i) const
00139     {
00140       Scalar tmp = y[i];
00141       y[i] = alpha * x[i] + beta * tmp;
00142     }
00143   };
00144 
00145   template <typename Scalar>
00146   struct GESUMOp3 {
00147     const Scalar *x, *y;
00148     Scalar *z;
00149     Scalar alpha, beta, gamma;
00150     inline KERNEL_PREFIX void execute(int i) const
00151     {
00152       Scalar tmp = z[i];
00153       z[i] = alpha * x[i] + beta * y[i] + gamma * tmp;
00154     }
00155   };
00156 
00157   template <typename Scalar>
00158   struct SumAbsOp {
00159     typedef  Teuchos::ScalarTraits<Scalar> SCT;
00160     typedef  typename SCT::magnitudeType   Magnitude;
00161     const Scalar *x;
00162     typedef  Magnitude ReductionType;
00163     inline static Magnitude KERNEL_PREFIX identity() {return Teuchos::ScalarTraits<Magnitude>::zero();}
00164     inline static Magnitude KERNEL_PREFIX reduce(Magnitude x, Magnitude y) {return x+y;}
00165     inline        Magnitude KERNEL_PREFIX generate(int i) {
00166       return SCT::magnitude((x[i]));
00167     }
00168   };
00169 
00170   template <typename Scalar>
00171   struct WeightNormOp {
00172     typedef  Teuchos::ScalarTraits<Scalar> SCT;
00173     typedef  typename SCT::magnitudeType   Magnitude;
00174     const Scalar *x, *w;
00175     typedef  Magnitude ReductionType;
00176     inline static Magnitude KERNEL_PREFIX identity() {return Teuchos::ScalarTraits<Magnitude>::zero();}
00177     inline static Magnitude KERNEL_PREFIX reduce(Magnitude x, Magnitude y) {return x+y;}
00178     inline        Magnitude KERNEL_PREFIX generate(int i) {
00179       Scalar tmp = x[i] / w[i];
00180       return SCT::real( SCT::conjugate(tmp)*tmp );
00181     }
00182   };
00183 
00184   template <typename Scalar>
00185   struct SumOp {
00186     const Scalar *x;
00187     typedef  Scalar ReductionType;
00188     inline static Scalar KERNEL_PREFIX identity() {return Teuchos::ScalarTraits<Scalar>::zero();}
00189     inline static Scalar KERNEL_PREFIX reduce(Scalar x, Scalar y) {return x+y;}
00190     inline        Scalar KERNEL_PREFIX generate(int i) { return x[i]; }
00191   };
00192 
00193   template <typename Scalar>
00194   struct MaxAbsOp {
00195     typedef  Teuchos::ScalarTraits<Scalar> SCT;
00196     typedef  typename SCT::magnitudeType   Magnitude;
00197     const Scalar *x;
00198     typedef  Magnitude ReductionType;
00199     inline static Magnitude KERNEL_PREFIX identity() {return Teuchos::ScalarTraits<Magnitude>::zero();}
00200     inline static Magnitude KERNEL_PREFIX reduce(Magnitude x, Magnitude y) {return TEUCHOS_MAX(x,y);}
00201     inline        Magnitude KERNEL_PREFIX generate(int i) {
00202       return SCT::magnitude(x[i]);
00203     }
00204   };
00205 
00206   template <typename Scalar>
00207   struct DotOp1 {
00208     typedef  Teuchos::ScalarTraits<Scalar> SCT;
00209     typedef  typename SCT::magnitudeType   Magnitude;
00210     const Scalar *x;
00211     typedef  Magnitude ReductionType;
00212     inline static Magnitude KERNEL_PREFIX identity() {return Teuchos::ScalarTraits<Magnitude>::zero();}
00213     inline static Magnitude KERNEL_PREFIX reduce(Magnitude x, Magnitude y) {return x+y;}
00214     inline        Magnitude KERNEL_PREFIX generate(int i) {
00215       Scalar xi = x[i]; 
00216       return SCT::real( SCT::conjugate(xi)*xi );
00217     }
00218   };
00219 
00220   template <typename Scalar>
00221   struct DotOp2 {
00222     typedef Teuchos::ScalarTraits<Scalar> SCT;
00223     const Scalar *x, *y;
00224     typedef  Scalar ReductionType;
00225     inline static Scalar KERNEL_PREFIX identity() {return SCT::zero();}
00226     inline static Scalar KERNEL_PREFIX reduce(Scalar x, Scalar y) {return x+y;}
00227     inline        Scalar KERNEL_PREFIX generate(int i) {
00228       Scalar xi = x[i]; Scalar yi = y[i];
00229       return SCT::conjugate(xi)*yi;
00230     }
00231   };
00232 
00233 }
00234 
00235 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends