GEOSX
Tensor.hpp
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2020 Total, S.A
8  * Copyright (c) 2019- GEOSX Contributors
9  * All rights reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
15 #ifndef GEOSX_COMMON_TENSOR_HPP_
16 #define GEOSX_COMMON_TENSOR_HPP_
17 
18 #include "common/GeosxMacros.hpp"
19 
20 namespace geosx
21 {
22 
28 template< typename T, int SIZE_TPARAM >
29 class Tensor
30 {
31 public:
32 
33  static_assert( SIZE_TPARAM > 0, "Tensor size must be a positive value" );
34 
36  using value_type = T;
37 
39  static constexpr int SIZE = SIZE_TPARAM;
40 
48  T const & operator[]( std::ptrdiff_t const i ) const
49  {
50  return data[i];
51  }
52 
60  T & operator[]( std::ptrdiff_t const i )
61  {
62  return data[i];
63  }
64 
72  template< typename U = T >
75  std::enable_if_t< std::is_floating_point< U >::value, bool >
76  operator==( Tensor< U, SIZE > const & rhs ) const
77  {
78  for( int i = 0; i < SIZE; ++i )
79  {
80  if( (data[i] > rhs.data[i]) || (data[i] < rhs.data[i]) )
81  {
82  return false;
83  }
84  }
85  return true;
86  }
87 
95  template< typename U = T >
98  std::enable_if_t< !std::is_floating_point< U >::value, bool >
99  operator==( Tensor< U, SIZE > const & rhs ) const
100  {
101  for( int i = 0; i < SIZE; ++i )
102  {
103  if( data[i] != rhs.data[i] )
104  {
105  return false;
106  }
107  }
108  return true;
109  }
110 
118  constexpr int size( int junk ) const
119  {
120  GEOSX_UNUSED_VAR( junk )
121  return SIZE;
122  }
123 
125  T data[SIZE] = {};
126 
127 private:
128 
135  friend inline std::ostream & operator<<( std::ostream & os, Tensor< T, SIZE > const & t )
136  {
137  os << t.data[0];
138  for( int i = 1; i < SIZE; ++i )
139  {
140  os << ',' << t.data[i];
141  }
142  return os;
143  }
144 };
145 
146 }
147 
148 #endif //GEOSX_COMMON_TENSOR_HPP_
#define GEOSX_HOST_DEVICE
Marks a host-device function.
Definition: GeosxMacros.hpp:48
GEOSX_HOST_DEVICE GEOSX_FORCE_INLINE std::enable_if_t< std::is_floating_point< U >::value, bool > operator==(Tensor< U, SIZE > const &rhs) const
Equality comparison operator.
Definition: Tensor.hpp:76
T data[SIZE]
Underlying array.
Definition: Tensor.hpp:125
GEOSX_HOST_DEVICE GEOSX_FORCE_INLINE T & operator[](std::ptrdiff_t const i)
Non-const element access.
Definition: Tensor.hpp:60
GEOSX_HOST_DEVICE GEOSX_FORCE_INLINE T const & operator[](std::ptrdiff_t const i) const
Const element access.
Definition: Tensor.hpp:48
Lightweight wrapper around a c-array.
Definition: Tensor.hpp:29
GEOSX_HOST_DEVICE GEOSX_FORCE_INLINE constexpr int size(int junk) const
Returns the size of the tensor.
Definition: Tensor.hpp:118
GEOSX_HOST_DEVICE GEOSX_FORCE_INLINE std::enable_if_t< !std::is_floating_point< U >::value, bool > operator==(Tensor< U, SIZE > const &rhs) const
Equality comparison operator.
Definition: Tensor.hpp:99
#define GEOSX_FORCE_INLINE
Marks a function or lambda for inlining.
Definition: GeosxMacros.hpp:50
#define GEOSX_UNUSED_VAR(...)
Mark an unused variable and silence compiler warnings.
Definition: GeosxMacros.hpp:78
static constexpr int SIZE
Alias for size template parameter.
Definition: Tensor.hpp:39
real64 value_type
Alias for type template parameter.
Definition: Tensor.hpp:36