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.h>
6#include <molpro/linalg/array/util/gemm.h>
7#include <molpro/linalg/array/util/select.h>
8#include <molpro/linalg/array/util/select_max_dot.h>
9#include <numeric>
10#include <stdexcept>
11#include <string>
12
13namespace molpro::linalg::array {
16
21template <typename AL, typename AR = AL>
22class ArrayHandlerIterable : public ArrayHandler<AL, AR> {
23public:
29
32
33 AL copy(const AR &source) override {
34 auto result = util::allocate_like<AL>(source.size());
35 copy(result, source);
36 return result;
37 };
38
39 void copy(AL &x, const AR &y) override {
40 using std::begin;
41 using std::end;
42 std::copy(begin(y), end(y), begin(x));
43 };
44
45 void scal(value_type alpha, AL &x) override {
46 for (auto &el : x)
47 el *= alpha;
48 };
49
50 void fill(value_type alpha, AL &x) override {
51 using std::begin;
52 using std::end;
53 std::fill(begin(x), end(x), alpha);
54 };
55
56 void axpy(value_type alpha, const AR &x, AL &y) override {
57 auto prof = molpro::Profiler::single();
58 prof->start("ArrayHandlerIterable::axpy");
59 if (x.size() < y.size())
60 error("ArrayHandlerIterable::axpy() incompatible x and y arrays, x.size() < y.size()");
61 using std::begin;
62 using std::end;
63 std::transform(begin(y), end(y), begin(x), begin(y), [alpha](auto &ely, auto &elx) { return ely + alpha * elx; });
64 prof->stop();
65 };
66
76 value_type dot(const AL &x, const AR &y) override {
77 if (x.size() > y.size())
78 error("ArrayHandlerIterable::dot() incompatible x and y arrays, x.size() > y.size()");
79 using std::begin;
80 using std::end;
82 return std::inner_product(begin(x), end(x), begin(y), value_type{}, std::plus<value_type>{},
83 [](const auto &elx, const auto &ely) {
84 return molpro::linalg::conjugate(static_cast<value_type>(elx)) *
85 static_cast<value_type>(ely);
86 });
87 else
88 return std::inner_product(begin(x), end(x), begin(y), value_type{});
89 };
90
91 void gemm_outer(const Matrix<value_type> alphas, const CVecRef<AR> &xx, const VecRef<AL> &yy) override {
92 gemm_outer_default(*this, alphas, xx, yy);
93 }
94
95 Matrix<value_type> gemm_inner(const CVecRef<AL> &xx, const CVecRef<AR> &yy) override {
96 return gemm_inner_default(*this, xx, yy);
97 }
98
99 std::map<size_t, value_type_abs> select_max_dot(size_t n, const AL &x, const AR &y) override {
100 if (n > x.size() || n > y.size())
101 error("ArrayHandlerIterable::select_max_dot() n is too large");
102 return util::select_max_dot<AL, AR, value_type, value_type_abs>(n, x, y);
103 }
104
105 std::map<size_t, value_type> select(size_t n, const AL &x, bool max = false, bool ignore_sign = false) override {
106 if (n > x.size())
107 error("ArrayHandlerIterable::select() n is too large");
108 return util::select<AL, value_type>(n, x, max, ignore_sign);
109 }
110
111 ProxyHandle lazy_handle() override { return this->lazy_handle(*this); };
112
113protected:
114 using ArrayHandler<AL, AR>::error;
115 using ArrayHandler<AL, AR>::lazy_handle;
116 using ArrayHandler<AL, AR>::m_lazy_handles;
117
119 template <typename T, typename S>
120 [[deprecated("use util::allocate_like() followed by copy() instead")]] T copyAny(const S &source) {
121 auto result = util::allocate_like<T>(source.size());
122 copy(result, source);
123 return result;
124 }
125};
126
127} // namespace molpro::linalg::array
128
129#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:22
ProxyHandle lazy_handle() override
Returns a lazy handle. Most implementations simply need to call the overload: return lazy_handle(*thi...
Definition ArrayHandlerIterable.h:111
AL copy(const AR &source) override
Definition ArrayHandlerIterable.h:33
T copyAny(const S &source)
Creates a copy of source as an array of type T.
Definition ArrayHandlerIterable.h:120
void scal(value_type alpha, AL &x) override
Definition ArrayHandlerIterable.h:45
void gemm_outer(const Matrix< value_type > alphas, const CVecRef< AR > &xx, const VecRef< AL > &yy) override
Definition ArrayHandlerIterable.h:91
void copy(AL &x, const AR &y) override
Copy content of y into x.
Definition ArrayHandlerIterable.h:39
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:99
ArrayHandlerIterable(const ArrayHandlerIterable< AL, AR > &)=default
void fill(value_type alpha, AL &x) override
Definition ArrayHandlerIterable.h:50
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:76
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:105
Matrix< value_type > gemm_inner(const CVecRef< AL > &xx, const CVecRef< AR > &yy) override
Definition ArrayHandlerIterable.h:95
void axpy(value_type alpha, const AR &x, AL &y) override
Definition ArrayHandlerIterable.h:56
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:90
auto end(Span< T > &x)
Definition Span.h:100
void gemm_outer_default(Handler &handler, const Matrix< typename Handler::value_type > alphas, const CVecRef< AR > &xx, const VecRef< AL > &yy)
Definition gemm.h:522
Matrix< typename Handler::value_type > gemm_inner_default(Handler &handler, const CVecRef< AL > &xx, const CVecRef< AR > &yy)
Definition gemm.h:532
Definition ArrayHandler.h:19
T conjugate(const T &x)
Complex conjugate, staying within the scalar type; the identity for a real type.
Definition scalar_traits.h:47
Definition scalar_traits.h:19