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