|
| 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 | + |
| 23 | +/** |
| 24 | + * @brief Lightweight wrapper around a c-array |
| 25 | + * @tparam T data type |
| 26 | + * @tparam SIZE_TPARAM number of values |
| 27 | + */ |
| 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 | + |
| 35 | + /// Alias for type template parameter |
| 36 | + using value_type = T; |
| 37 | + |
| 38 | + /// Alias for size template parameter |
| 39 | + static constexpr int SIZE = SIZE_TPARAM; |
| 40 | + |
| 41 | + /** |
| 42 | + * @brief Const element access. |
| 43 | + * @param i element index |
| 44 | + * @return const reference to the i-th element |
| 45 | + */ |
| 46 | + GEOSX_HOST_DEVICE |
| 47 | + GEOSX_FORCE_INLINE |
| 48 | + T const & operator[]( std::ptrdiff_t const i ) const |
| 49 | + { |
| 50 | + return data[i]; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @brief Non-const element access. |
| 55 | + * @param i element index |
| 56 | + * @return reference to the i-th element |
| 57 | + */ |
| 58 | + GEOSX_HOST_DEVICE |
| 59 | + GEOSX_FORCE_INLINE |
| 60 | + T & operator[]( std::ptrdiff_t const i ) |
| 61 | + { |
| 62 | + return data[i]; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @brief Equality comparison operator. |
| 67 | + * @tparam U dummy parameter to enable SFINAE, do not provide explicitly |
| 68 | + * @param rhs the right-hand side value to compare to |
| 69 | + * @return true iff @code data[i] == rhs.data[i] @endcode |
| 70 | + * @note Version for floating point types that avoids direct equality comparison. |
| 71 | + */ |
| 72 | + template< typename U = T > |
| 73 | + GEOSX_HOST_DEVICE |
| 74 | + GEOSX_FORCE_INLINE |
| 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 | + |
| 88 | + /** |
| 89 | + * @brief Equality comparison operator. |
| 90 | + * @tparam U dummy parameter to enable SFINAE, do not provide explicitly |
| 91 | + * @param rhs the right-hand side value to compare to |
| 92 | + * @return true iff @code data[i] == rhs.data[i] @endcode |
| 93 | + * @note Version for all types except floating point. |
| 94 | + */ |
| 95 | + template< typename U = T > |
| 96 | + GEOSX_HOST_DEVICE |
| 97 | + GEOSX_FORCE_INLINE |
| 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 | + |
| 111 | + /** |
| 112 | + * @brief Returns the size of the tensor |
| 113 | + * @param junk Unused |
| 114 | + * @return The value of the template parameter SIZE. |
| 115 | + */ |
| 116 | + GEOSX_HOST_DEVICE |
| 117 | + GEOSX_FORCE_INLINE |
| 118 | + constexpr int size( int junk ) const |
| 119 | + { |
| 120 | + GEOSX_UNUSED_VAR( junk ) |
| 121 | + return SIZE; |
| 122 | + } |
| 123 | + |
| 124 | + /// Underlying array |
| 125 | + T data[SIZE] = {}; |
| 126 | + |
| 127 | +private: |
| 128 | + |
| 129 | + /** |
| 130 | + * @brief Stream insertion operator for Tensor. |
| 131 | + * @param os the output stream |
| 132 | + * @param t the tensor value |
| 133 | + * @return reference to @p os |
| 134 | + */ |
| 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_ |
0 commit comments