| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include <type_traits> |
| 14 |
|
| 15 |
#include "eckit/linalg/Matrix.h" |
| 16 |
#include "eckit/linalg/Vector.h" |
| 17 |
|
| 18 |
#include "atlas/array/LocalView.h" |
| 19 |
#include "atlas/linalg/Introspection.h" |
| 20 |
|
| 21 |
namespace atlas { |
| 22 |
namespace linalg { |
| 23 |
|
| 24 |
template <typename Value, int Rank> |
| 25 |
using View = array::LocalView<Value, Rank>; |
| 26 |
|
| 27 |
namespace view { |
| 28 |
|
| 29 |
template <typename View, typename> |
| 30 |
struct Convert { |
| 31 |
using value_type = typename std::conditional<std::is_const<View>::value, const introspection::value_type<View>, |
| 32 |
introspection::value_type<View>>::type; |
| 33 |
using type = array::LocalView<value_type, introspection::rank<View>()>; |
| 34 |
static type apply(View& view) { return type(view.data(), view.shape()); } |
| 35 |
}; |
| 36 |
|
| 37 |
template <typename View> |
| 38 |
struct Convert<View, eckit::linalg::Vector> { |
| 39 |
using value_type = |
| 40 |
typename std::conditional<std::is_const<View>::value, const eckit::linalg::Scalar, eckit::linalg::Scalar>::type; |
| 41 |
using type = array::LocalView<value_type, 1>; |
| 42 |
static type apply(View& v) { return type(v.data(), array::make_shape(v.size())); } |
| 43 |
}; |
| 44 |
|
| 45 |
template <typename View> |
| 46 |
struct Convert<View, eckit::linalg::Matrix> { |
| 47 |
using value_type = |
| 48 |
typename std::conditional<std::is_const<View>::value, const eckit::linalg::Scalar, eckit::linalg::Scalar>::type; |
| 49 |
using type = array::LocalView<value_type, 2>; |
| 50 |
static type apply(View& m) { return type(m.data(), array::make_shape(m.cols(), m.rows())); } |
| 51 |
}; |
| 52 |
|
| 53 |
template <typename View> |
| 54 |
struct ConvertView { |
| 55 |
using type = typename Convert<View, introspection::base_type<View>>::type; |
| 56 |
static type apply(View& v) { return Convert<View, introspection::base_type<View>>::apply(v); } |
| 57 |
}; |
| 58 |
|
| 59 |
} // namespace view |
| 60 |
|
| 61 |
template <typename View> |
| 62 |
using view_type = typename view::ConvertView<View>::type; |
| 63 |
|
| 64 |
template <typename T> |
| 65 |
view_type<T> make_view(T& view) { |
| 66 |
return view::ConvertView<T>::apply(view); |
| 67 |
} |
| 68 |
|
| 69 |
} // namespace linalg |
| 70 |
} // namespace atlas |