Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_ScalarTraitsCUDA.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef _TEUCHOS_SCALARTRAITS_CUDA_HPP_
43 #define _TEUCHOS_SCALARTRAITS_CUDA_HPP_
44 
50 
51 namespace Teuchos {
52 
53 template<>
54 struct ScalarTraits<int>
55 {
56  typedef int magnitudeType;
57  typedef int halfPrecision;
58  typedef int doublePrecision;
59  static const bool isComplex = false;
60  static const bool isOrdinal = true;
61  static const bool isComparable = true;
62  static const bool hasMachineParameters = false;
63  static inline __device__ __host__ magnitudeType magnitude(int a) { return (int)fabsf((float)a); }
64  static inline __device__ __host__ int zero() { return 0; }
65  static inline __device__ __host__ int one() { return 1; }
66  static inline __device__ __host__ int conjugate(int x) { return x; }
67  static inline __device__ __host__ int real(int x) { return x; }
68  static inline __device__ __host__ int imag(int) { return 0; }
69  static inline __device__ __host__ bool isnaninf(int) { return false; }
70  static inline __device__ __host__ int squareroot(int x) { return (int)sqrtf((float)x); } // perhaps this cast should be replaced by an explicit call like __float2int_rn
71  static inline __device__ __host__ int pow(int x, int y) { return (int)powf((float)x,(float)y); } // perhaps this cast should be replaced by an explicit call like __float2int_rn
72 };
73 
74 template<>
75 struct ScalarTraits<long int>
76 {
77  typedef long int magnitudeType;
78  typedef long int halfPrecision;
79  typedef long int doublePrecision;
80  static const bool isComplex = false;
81  static const bool isOrdinal = true;
82  static const bool isComparable = true;
83  static const bool hasMachineParameters = false;
84  // Not defined: eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax()
85  static inline __device__ __host__ magnitudeType magnitude(long int a) { return (long int)fabsf((float)a); }
86  static inline __device__ __host__ long int zero() { return 0; }
87  static inline __device__ __host__ long int one() { return 1; }
88  static inline __device__ __host__ long int conjugate(long int x) { return x; }
89  static inline __device__ __host__ long int real(long int x) { return x; }
90  static inline __device__ __host__ long int imag(long int) { return 0; }
91  static inline __device__ __host__ bool isnaninf(int) { return false; }
92  static inline __device__ __host__ long int squareroot(long int x) { return (long int)sqrtf((float)x); } // perhaps this cast should be replaced by an explicit call like __float2int_rn
93  static inline __device__ __host__ long int pow(long int x, long int y) { return (long int)powf((float)x,(float)y); } // perhaps this cast should be replaced by an explicit call like __float2int_rn
94 };
95 
96 #ifdef HAVE_KOKKOSCLASSIC_CUDA_FLOAT
97 template<>
98 struct ScalarTraits<float>
99 {
100  typedef float magnitudeType;
101  typedef float halfPrecision; // should become IEEE754-2008 binary16 or fp16 later, perhaps specified at configure according to architectural support
102 #ifdef HAVE_KOKKOSCLASSIC_CUDA_DOUBLE
103  typedef double doublePrecision;
104 #else
105  typedef float doublePrecision;
106 #endif
107  static const bool isComplex = false;
108  static const bool isOrdinal = false;
109  static const bool isComparable = true;
110  static const bool hasMachineParameters = false;
111  static inline __device__ __host__ magnitudeType magnitude(float a) { return fabsf(a); }
112  static inline __device__ __host__ float zero() { return(0.0f); }
113  static inline __device__ __host__ float one() { return(1.0f); }
114  static inline __device__ __host__ float conjugate(float x) { return(x); }
115  static inline __device__ __host__ float real(float x) { return x; }
116  static inline __device__ __host__ float imag(float) { return zero(); }
117  static inline __device__ __host__ bool isnaninf(float x) { return isnan(x) || isinf(x); }
118  static inline __device__ __host__ float squareroot(float x) { return sqrtf(x); }
119  static inline __device__ __host__ float pow(float x, float y) { return powf(x,y); }
120 };
121 #endif // HAVE_KOKKOSCLASSIC_CUDA_FLOAT
122 
123 #ifdef HAVE_KOKKOSCLASSIC_CUDA_DOUBLE
124 template<>
125 struct ScalarTraits<double>
126 {
127  typedef double magnitudeType;
128 #ifdef HAVE_KOKKOSCLASSIC_CUDA_FLOAT
129  typedef float halfPrecision;
130 #else
131  typedef double halfPrecision;
132 #endif
133  typedef double doublePrecision;
134  static const bool isComplex = false;
135  static const bool isOrdinal = false;
136  static const bool isComparable = true;
137  static const bool hasMachineParameters = false;
138  static inline __device__ __host__ magnitudeType magnitude(double a) { return abs(a); }
139  static inline __device__ __host__ double zero() { return(0.0); }
140  static inline __device__ __host__ double one() { return(1.0); }
141  static inline __device__ __host__ double conjugate(double x) { return(x); }
142  static inline __device__ __host__ double real(double x) { return x; }
143  static inline __device__ __host__ double imag(double) { return zero(); }
144  static inline __device__ __host__ bool isnaninf(double x) { return isnan(x) || isinf(x); }
145  static inline __device__ __host__ double squareroot(double x) { return sqrt(x); }
146  static inline __device__ __host__ double pow(double x, double y) { return pow(x,y); }
147 };
148 #endif // HAVE_KOKKOSCLASSIC_CUDA_DOUBLE
149 
150 } // Teuchos namespace
151 
152 #endif // _TEUCHOS_SCALARTRAITS_CUDA_HPP_
T magnitudeType
Mandatory typedef for result of magnitude.
static T squareroot(T x)
Returns a number of magnitudeType that is the square root of this scalar type x.
static const bool isComparable
Determines if scalar type supports relational operators such as <, >, <=, >=.
static magnitudeType real(T a)
Returns the real part of the scalar type a.
Declaration and default implementation for basic traits for the scalar field type.
static T pow(T x, T y)
Returns the result of raising one scalar x to the power y.
static const bool hasMachineParameters
Determines if scalar type have machine-specific parameters (i.e. eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax() are supported).
static T conjugate(T a)
Returns the conjugate of the scalar type a.
static const bool isOrdinal
Determines if scalar type is an ordinal type.
T doublePrecision
Typedef for double precision.
static magnitudeType imag(T a)
Returns the imaginary part of the scalar type a.
static bool isnaninf(const T &x)
Returns true if x is NaN or Inf.
static magnitudeType magnitude(T a)
Returns the magnitudeType of the scalar type a.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
static T zero()
Returns representation of zero for this scalar type.
T halfPrecision
Typedef for half precision.
static const bool isComplex
Determines if scalar type is std::complex.
static T one()
Returns representation of one for this scalar type.