// @HEADER // ***************************************************************************** // Sacado Package // // Copyright 2006 NTESS and the Sacado contributors. // SPDX-License-Identifier: LGPL-2.1-or-later // ***************************************************************************** // @HEADER #ifndef SACADO_FAD_EXP_VIEWSTORAGE_HPP #define SACADO_FAD_EXP_VIEWSTORAGE_HPP #include #include #include #include "Sacado_DynamicArrayTraits.hpp" #include "Sacado_mpl_integral_nonzero_constant.hpp" #include "Sacado_mpl_apply.hpp" namespace Sacado { namespace Fad { namespace Exp { #ifndef SACADO_FAD_DERIV_LOOP #if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) && !defined(SACADO_DISABLE_CUDA_IN_KOKKOS) && ( defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) ) #define SACADO_FAD_DERIV_LOOP(I,SZ) for (int I=threadIdx.x; I class ViewFadPtr; /*! * \brief Derivative array storage class that is a view into a contiguous * memory allocation. It does not provide proper value semantics and * thus should not be used in a general-purpose scalar type. */ template class ViewStorage { private: // Enumerated flag so logic is evaluated at compile-time static constexpr bool stride_one = (1 == static_stride); public: typedef typename std::remove_cv::type value_type; static constexpr bool is_statically_sized = (static_length > 0); static constexpr int static_size = static_length; static constexpr bool is_view = true; typedef U base_fad_type; //! Turn ViewStorage into a meta-function class usable with mpl::apply template struct apply { typedef typename std::remove_cv::type non_const_TT; typedef typename BaseExprType::type base_expr_TT; typedef typename mpl::apply::type UU; typedef ViewStorage type; }; //! Replace static derivative length template struct apply_N { typedef ViewStorage type; }; //! Default constructor (needed to satisfy interface) SACADO_INLINE_FUNCTION ViewStorage() : sz_(0), stride_(0), val_(0), dx_(0) {} //! Constructor SACADO_INLINE_FUNCTION ViewStorage(T* v, const int arg_size = 0, const int arg_stride = 0) : sz_(arg_size), stride_(arg_stride), val_(v+sz_.value*static_cast(stride_.value)), dx_(v) {} //! Constructor SACADO_INLINE_FUNCTION ViewStorage(T* arg_dx, T* arg_val, const int arg_size = 0, const int arg_stride = 0) : sz_(arg_size), stride_(arg_stride), val_(arg_val), dx_(arg_dx) {} //! Copy constructor /*! Allow, e.g., ViewStorage(ViewStorage), ViewStorage(ViewStorage), and ViewStorage(ViewStorage) but not ViewStorage(ViewStorage). */ template SACADO_INLINE_FUNCTION ViewStorage( const ViewStorage& x, typename std::enable_if< std::is_same::value || std::is_same::value >::type* = 0) : sz_(x.sz_), stride_(x.stride_), val_(x.val_), dx_(x.dx_) {} // Move does not make sense for this storage since it is always tied to // some preallocated data. Don't define move constructor so compiler will // always fall-back to copy //! Destructor SACADO_INLINE_FUNCTION ~ViewStorage() {} //! Assignment SACADO_INLINE_FUNCTION ViewStorage& operator=(const ViewStorage& x) { // Can't use std::addressof() on the GPU, so this is equivalent // according to cppreference.com if (this != reinterpret_cast( &const_cast( reinterpret_cast(x)))) { *val_ = *x.val_; if (stride_one) //for (int i=0; i::strided_zero(dx_, stride_.value, sz_.value); } //! Resize the derivative array to sz /*! * We don't do anything here as this is used in the context of resizing * the derivative array to zero and then back to some size > 0. Instead * we zero out components when it is resized to zero above. */ SACADO_INLINE_FUNCTION void resizeAndZero(int sz) {} //! Expand derivative array to size sz SACADO_INLINE_FUNCTION void expand(int sz) {} //! Zero out derivative array SACADO_INLINE_FUNCTION void zero() { ds_array::strided_zero(dx_, stride_.value, sz_.value); } //! Returns value SACADO_INLINE_FUNCTION const T& val() const { return *val_; } //! Returns value SACADO_INLINE_FUNCTION T& val() { return *val_; } //! Returns derivative array SACADO_INLINE_FUNCTION const T* dx() const { return dx_;} //! Returns derivative component \c i with bounds checking SACADO_INLINE_FUNCTION T dx(int i) const { return unsigned(sz_.value) ? dx_[ stride_one ? i : i * stride_.value ] : T(0.); } //! Returns derivative component \c i without bounds checking SACADO_INLINE_FUNCTION T& fastAccessDx(int i) { return dx_[ stride_one ? i : i * stride_.value ]; } //! Returns derivative component \c i without bounds checking SACADO_INLINE_FUNCTION const T& fastAccessDx(int i) const { return dx_[ stride_one ? i : i * stride_.value ]; } //! Overload of addressof operator SACADO_INLINE_FUNCTION ViewFadPtr operator&() const { return ViewFadPtr( this->dx_, this->val_, this->sz_.value, this->stride_.value); } protected: template friend class ViewStorage; //! Derivative array size const mpl::integral_nonzero_constant< int, static_length > sz_; //! Derivative array stride const mpl::integral_nonzero_constant< int, static_stride > stride_; //! Value T *val_; //! Derivative array T *dx_; }; // class ViewStorage } // namespace Exp } // namespace Fad } // namespace Sacado #endif // SACADO_FAD_EXP_VIEWSTORAGE_HPP