iterative-solver 0.0
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
14
15namespace molpro::linalg::array {
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
116 value_type dot(const AL &x, const AR &y) override {
117 if (x.size() > y.size())
118 error("ArrayHandlerIterable::dot() incompatible x and y arrays, x.size() > y.size()");
119 using std::begin;
120 using std::end;
121 return std::inner_product(begin(x), end(x), begin(y), (value_type)0);
122 };
123
124 void gemm_outer(const Matrix<value_type> alphas, const CVecRef<AR> &xx, const VecRef<AL> &yy) override {
125 gemm_outer_default(*this, alphas, xx, yy);
126 }
127
128 Matrix<value_type> gemm_inner(const CVecRef<AL> &xx, const CVecRef<AR> &yy) override {
129 return gemm_inner_default(*this, xx, yy);
130 }
131
132 std::map<size_t, value_type_abs> select_max_dot(size_t n, const AL &x, const AR &y) override {
133 if (n > x.size() || n > y.size())
134 error("ArrayHandlerIterable::select_max_dot() n is too large");
135 return util::select_max_dot<AL, AR, value_type, value_type_abs>(n, x, y);
136 }
137
138 std::map<size_t, value_type> select(size_t n, const AL &x, bool max = false, bool ignore_sign = false) override {
139 if (n > x.size())
140 error("ArrayHandlerIterable::select() n is too large");
141 return util::select<AL, value_type>(n, x, max, ignore_sign);
142 }
143
144 ProxyHandle lazy_handle() override { return this->lazy_handle(*this); };
145
146protected:
147 using ArrayHandler<AL, AR>::error;
148 using ArrayHandler<AL, AR>::lazy_handle;
149 using ArrayHandler<AL, AR>::m_lazy_handles;
150
151 template <typename T, typename S, typename std::enable_if_t<util::is_array_v<T>, std::nullptr_t> = nullptr>
152 T copyAny(const S &source) {
153 auto result = T();
154 copy(result, source);
155 return result;
156 }
157
158 template <typename T, typename S, typename std::enable_if_t<!util::is_array_v<T> && util::is_allocatable_v<T>, int> = 0>
159 T copyAny(const S &source) {
160 auto result = T(source.size());
161 copy(result, source);
162 return result;
163 }
164
165 template <typename T, typename S, typename std::enable_if_t<!util::is_array_v<T> && !util::is_allocatable_v<T>, int> = 0>
166 T copyAny(const S &source) {
167 T result = allocate_array<T>(source.size());
168 copy(result, source);
169 return result;
170 }
171};
172
173} // namespace molpro::linalg::array
174
175#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:152
ProxyHandle lazy_handle() override
Returns a lazy handle. Most implementations simply need to call the overload: return lazy_handle(*thi...
Definition: ArrayHandlerIterable.h:144
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:124
void copy(AL &x, const AR &y) override
Definition: ArrayHandlerIterable.h:88
std::map< size_t, value_type_abs > select_max_dot(size_t n, const AL &x, const AR &y) override
Definition: ArrayHandlerIterable.h:132
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
Definition: ArrayHandlerIterable.h:116
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:138
Matrix< value_type > gemm_inner(const CVecRef< AL > &xx, const CVecRef< AR > &yy) override
Definition: ArrayHandlerIterable.h:128
void axpy(value_type alpha, const AR &x, AL &y) override
Definition: ArrayHandlerIterable.h:105
Enhances various operations between pairs of arrays and allows dynamic code injection with uniform in...
Definition: ArrayHandler.h:162
std::vector< std::weak_ptr< LazyHandle > > m_lazy_handles
keeps track of all created lazy handles
Definition: ArrayHandler.h:416
decltype(value_type_L{} *value_type_R{}) value_type
Definition: ArrayHandler.h:181
virtual void error(const std::string &message)
Throws an error.
Definition: ArrayHandler.h:268
Matrix container that allows simple data access, slicing, copying and resizing without loosing data.
Definition: Matrix.h:28
static std::shared_ptr< Profiler > single()
auto begin(Span< T > &x)
Definition: Span.h:86
auto end(Span< T > &x)
Definition: Span.h:96
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:258
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:268
Definition: ArrayHandler.h:22
T allocate_array(std::size_t size)
Allocates an array of type T to hold the specified amount of items.
Definition: ArrayHandlerIterable.h:60
Definition: ArrayHandlerIterable.h:31
Definition: ArrayHandlerIterable.h:19