/* * (C) Copyright 2013 ECMWF. * * This software is licensed under the terms of the Apache Licence Version 2.0 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. * In applying this licence, ECMWF does not waive the privileges and immunities * granted to it by virtue of its status as an intergovernmental organisation * nor does it submit to any jurisdiction. */ #pragma once #include #include "eckit/linalg/Matrix.h" #include "eckit/linalg/Vector.h" #include "eckit/linalg/types.h" #include "atlas/library/config.h" namespace atlas { namespace linalg { namespace introspection { namespace detail { // void_t is a C++17 feature template struct make_void { typedef void type; }; template using void_t = typename make_void::type; } // namespace detail template struct has_contiguous : std::false_type {}; template struct has_contiguous> : std::true_type {}; template struct has_rank : std::false_type {}; template struct has_rank> : std::true_type {}; template struct has_RANK : std::false_type {}; template struct has_RANK> : std::true_type {}; template struct has_shape : std::false_type {}; template struct has_shape().shape(0))>> : std::true_type {}; namespace detail { template struct BaseType { template static constexpr int is_derived_from() { return std::is_base_of::value; } using type = typename std::conditional< is_derived_from(), eckit::linalg::Matrix, typename std::conditional(), eckit::linalg::Vector, typename std::remove_const::type /*default*/ >::type>::type; }; } // namespace detail template using base_type = typename detail::BaseType::type; namespace detail { template struct ValueType { using type = typename T::value_type; }; template struct ValueType { using type = eckit::linalg::Scalar; }; template struct ValueType { using type = eckit::linalg::Scalar; }; template struct Introspect { template ::value, T>::type* = nullptr> static bool contiguous(const T& view) { return view.contiguous(); } template ::value, T>::type* = nullptr> static bool contiguous(const T&) { return true; } template ::value, T>::type* = nullptr> static constexpr int rank() { static_assert(has_RANK::value, "workaround for Cray 8.7: we need to use T::RANK instead of T::rank()"); return T::RANK; } template ::value, T>::type* = nullptr> static constexpr int rank() { return 1; } static constexpr int rank() { return rank(); } template ::value, T>::type* = nullptr> static idx_t outer_dimension(const T& view) { return view.shape(0); } template ::value && (rank() == 1), T>::type* = nullptr> static idx_t outer_dimension(const T& view) { return view.size(); } template ::value, T>::type* = nullptr> static idx_t inner_dimension(const T& view) { return view.shape(rank() - 1); } template ::value && (rank() == 1), T>::type* = nullptr> static idx_t inner_dimension(const T& view) { return view.size(); } template ::value, T>::type* = nullptr> static idx_t shape(const T& view) { //static_assert( Dim == 0, "Dim must be 0 if shape() is not there" ); return view.size(); } template ::value, T>::type* = nullptr> static idx_t shape(const T& view) { return view.shape(Dim); } static constexpr bool layout_left() { return true; } using value_type = typename ValueType>::type; }; template struct Introspect { using value_type = eckit::linalg::Scalar; static constexpr int rank() { return 2; } static bool contiguous(const eckit::linalg::Matrix&) { return true; } static idx_t inner_dimension(const eckit::linalg::Matrix& m) { return static_cast(m.cols()); } static idx_t outer_dimension(const eckit::linalg::Matrix& m) { return static_cast(m.rows()); } template static idx_t shape(const eckit::linalg::Matrix& m) { static_assert(Dim <= 1, "Dim must be 0 or 1"); return (Dim == 0) ? outer_dimension(m) : inner_dimension(m); } static constexpr bool layout_left() { return false; } }; template using Introspection = Introspect>; } // namespace detail template using value_type = typename detail::Introspection::value_type; template using base_type = typename detail::BaseType::type; template static bool contiguous(const T& view) { return detail::Introspection::contiguous(view); } template static constexpr int rank() { return detail::Introspection::rank(); } template static idx_t outer_dimension(const T& view) { return detail::Introspection::outer_dimension(view); } template static idx_t inner_dimension(const T& view) { return detail::Introspection::inner_dimension(view); } template static idx_t shape(const T& view) { return detail::Introspection::template shape(view); } template static constexpr bool layout_left(const T&) { return detail::Introspection::layout_left(); } template static constexpr bool layout_right(const T&) { return not detail::Introspection::layout_left(); } } // namespace introspection } // namespace linalg } // namespace atlas