iterative-solver 0.0
 
Loading...
Searching...
No Matches
ArrayHandlerIterable.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERITERABLE_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERITERABLE_H
3#include <cstddef>
4#include <molpro/linalg/array/ArrayHandler.h>
5#include <molpro/linalg/array/util/gemm.h>
6#include <molpro/linalg/array/util/select.h>
7#include <molpro/linalg/array/util/select_max_dot.h>
8#include <numeric>
9#include <stdexcept>
10#include <string>
11
12namespace molpro::linalg::array {
15
16namespace util {
17
18template <typename T>
19struct is_std_array : std::false_type {};
20
21template <typename T, std::size_t N>
22struct is_std_array<std::array<T, N>> : std::true_type {};
23
24template <typename T>
26
27template <typename T>
28constexpr bool is_array_v = std::is_array<T>::value || is_std_array_v<T>;
29
30template <typename T, typename = void>
31struct is_allocatable : std::false_type {};
32
33// We use the existence of a constructor taking a size-type as a proxy
34// for whether the given type is "allocatable", that is it can be newly
35// constructed by simply telling it how many elements it shall contain.
36template <typename T>
37struct is_allocatable<T, std::enable_if_t<std::is_constructible_v<T, std::size_t>>> : std::true_type {};
38
39template <typename T>
41
42} // namespace util
43
59template<typename T>
60T allocate_array(std::size_t size) {
61 if constexpr (util::is_allocatable_v<T>) {
62 return T(size);
63 }
64
65 throw std::runtime_error("Required molpro::linalg::array::allocate_array for type '"
66 + std::string(typeid(T).name())
67 + "' - you have to provide an explicit template specialization for this function & type");
68}
69
74template <typename AL, typename AR = AL>
75class ArrayHandlerIterable : public ArrayHandler<AL, AR> {
76public:
82
85
86 AL copy(const AR &source) override { return copyAny<AL, AR>(source); };
87
88 void copy(AL &x, const AR &y) override {
89 using std::begin;
90 using std::end;
91 std::copy(begin(y), end(y), begin(x));
92 };
93
94 void scal(value_type alpha, AL &x) override {
95 for (auto &el : x)
96 el *= alpha;
97 };
98
99 void fill(value_type alpha, AL &x) override {
100 using std::begin;
101 using std::end;
102 std::fill(begin(x), end(x), alpha);
103 };
104
105 void axpy(value_type alpha, const AR &x, AL &y) override {
106 auto prof = molpro::Profiler::single();
107 prof->start("ArrayHandlerIterable::axpy");
108 if (x.size() < y.size())
109 error("ArrayHandlerIterable::axpy() incompatible x and y arrays, x.size() < y.size()");
110 using std::begin;
111 using std::end;
112 std::transform(begin(y), end(y), begin(x), begin(y), [alpha](auto &ely, auto &elx) { return ely + alpha * elx; });
113 prof->stop();
114 };
115
125 value_type dot(const AL &x, const AR &y) override {
126 if (x.size() > y.size())
127 error("ArrayHandlerIterable::dot() incompatible x and y arrays, x.size() > y.size()");
128 using std::begin;
129 using std::end;
131 return std::inner_product(begin(x), end(x), begin(y), value_type{}, std::plus<value_type>{},
132 [](const auto &elx, const auto &ely) {
133 return molpro::linalg::conjugate(static_cast<value_type>(elx)) *
134 static_cast<value_type>(ely);
135 });
136 else
137 return std::inner_product(begin(x), end(x), begin(y), value_type{});
138 };
139
140 void gemm_outer(const Matrix<value_type> alphas, const CVecRef<AR> &xx, const VecRef<AL> &yy) override {
141 gemm_outer_default(*this, alphas, xx, yy);
142 }
143
144 Matrix<value_type> gemm_inner(const CVecRef<AL> &xx, const CVecRef<AR> &yy) override {
145 return gemm_inner_default(*this, xx, yy);
146 }
147
148 std::map<size_t, value_type_abs> select_max_dot(size_t n, const AL &x, const AR &y) override {
149 if (n > x.size() || n > y.size())
150 error("ArrayHandlerIterable::select_max_dot() n is too large");
151 return util::select_max_dot<AL, AR, value_type, value_type_abs>(n, x, y);
152 }
153
154 std::map<size_t, value_type> select(size_t n, const AL &x, bool max = false, bool ignore_sign = false) override {
155 if (n > x.size())
156 error("ArrayHandlerIterable::select() n is too large");
157 return util::select<AL, value_type>(n, x, max, ignore_sign);
158 }
159
160 ProxyHandle lazy_handle() override { return this->lazy_handle(*this); };
161
162protected:
163 using ArrayHandler<AL, AR>::error;
164 using ArrayHandler<AL, AR>::lazy_handle;
165 using ArrayHandler<AL, AR>::m_lazy_handles;
166
167 template <typename T, typename S, typename std::enable_if_t<util::is_array_v<T>, std::nullptr_t> = nullptr>
168 T copyAny(const S &source) {
169 auto result = T();
170 copy(result, source);
171 return result;
172 }
173
174 template <typename T, typename S, typename std::enable_if_t<!util::is_array_v<T> && util::is_allocatable_v<T>, int> = 0>
175 T copyAny(const S &source) {
176 auto result = T(source.size());
177 copy(result, source);
178 return result;
179 }
180
181 template <typename T, typename S, typename std::enable_if_t<!util::is_array_v<T> && !util::is_allocatable_v<T>, int> = 0>
182 T copyAny(const S &source) {
183 T result = allocate_array<T>(source.size());
184 copy(result, source);
185 return result;
186 }
187};
188
189} // namespace molpro::linalg::array
190
191#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERITERABLE_H
Array handler for two containers both of which can be iterated through using begin() and end() member...
Definition ArrayHandlerIterable.h:75
T copyAny(const S &source)
Definition ArrayHandlerIterable.h:168
ProxyHandle lazy_handle() override
Returns a lazy handle. Most implementations simply need to call the overload: return lazy_handle(*thi...
Definition ArrayHandlerIterable.h:160
AL copy(const AR &source) override
Definition ArrayHandlerIterable.h:86
void scal(value_type alpha, AL &x) override
Definition ArrayHandlerIterable.h:94
void gemm_outer(const Matrix< value_type > alphas, const CVecRef< AR > &xx, const VecRef< AL > &yy) override
Definition ArrayHandlerIterable.h:140
void copy(AL &x, const AR &y) override
Copy content of y into x.
Definition ArrayHandlerIterable.h:88
std::map< size_t, value_type_abs > select_max_dot(size_t n, const AL &x, const AR &y) override
Select n indices with largest by absolute value contributions to the dot product.
Definition ArrayHandlerIterable.h:148
ArrayHandlerIterable(const ArrayHandlerIterable< AL, AR > &)=default
void fill(value_type alpha, AL &x) override
Definition ArrayHandlerIterable.h:99
value_type dot(const AL &x, const AR &y) override
The hermitian inner product <x|y>, i.e. conjugate-linear in x and linear in y.
Definition ArrayHandlerIterable.h:125
std::map< size_t, value_type > select(size_t n, const AL &x, bool max=false, bool ignore_sign=false) override
Select n indices with largest (or smallest) actual (or absolute) value.
Definition ArrayHandlerIterable.h:154
Matrix< value_type > gemm_inner(const CVecRef< AL > &xx, const CVecRef< AR > &yy) override
Definition ArrayHandlerIterable.h:144
void axpy(value_type alpha, const AR &x, AL &y) override
Definition ArrayHandlerIterable.h:105
A convenience wrapper around a pointer to the LazyHandle.
Definition ArrayHandler.h:392
Enhances various operations between pairs of arrays and allows dynamic code injection with uniform in...
Definition ArrayHandler.h:163
typename array::mapped_or_value_type_t< AR > value_type_R
Definition ArrayHandler.h:181
std::vector< std::weak_ptr< LazyHandle > > m_lazy_handles
keeps track of all created lazy handles
Definition ArrayHandler.h:424
typename array::mapped_or_value_type_t< AL > value_type_L
Definition ArrayHandler.h:180
decltype(value_type_L{} *value_type_R{}) value_type
Definition ArrayHandler.h:182
decltype(check_abs< value_type >()) value_type_abs
Definition ArrayHandler.h:183
virtual void error(const std::string &message)
Throws an error.
Definition ArrayHandler.h:276
Matrix container that allows simple data access, slicing, copying and resizing without loosing data.
Definition Matrix.h:32
static std::shared_ptr< Profiler > single()
auto begin(Span< T > &x)
Definition Span.h:87
auto end(Span< T > &x)
Definition Span.h:97
constexpr bool is_array_v
Definition ArrayHandlerIterable.h:28
constexpr bool is_std_array_v
Definition ArrayHandlerIterable.h:25
void gemm_outer_default(Handler &handler, const Matrix< typename Handler::value_type > alphas, const CVecRef< AR > &xx, const VecRef< AL > &yy)
Definition gemm.h:288
constexpr bool is_allocatable_v
Definition ArrayHandlerIterable.h:40
Matrix< typename Handler::value_type > gemm_inner_default(Handler &handler, const CVecRef< AL > &xx, const CVecRef< AR > &yy)
Definition gemm.h:298
Definition ArrayHandler.h:19
T allocate_array(std::size_t size)
Allocates an array of type T to hold the specified amount of items.
Definition ArrayHandlerIterable.h:60
T conjugate(const T &x)
Complex conjugate, staying within the scalar type; the identity for a real type.
Definition scalar_traits.h:47
Definition ArrayHandlerIterable.h:31
Definition ArrayHandlerIterable.h:19
Definition scalar_traits.h:19