1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERITERABLE_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERITERABLE_H
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>
21template <
typename T, std::
size_t N>
28constexpr bool is_array_v = std::is_array<T>::value || is_std_array_v<T>;
30template <
typename T,
typename =
void>
37struct is_allocatable<T, std::enable_if_t<std::is_constructible_v<T, std::size_t>>> : std::true_type {};
61 if constexpr (util::is_allocatable_v<T>) {
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");
74template <
typename AL,
typename AR = AL>
86 AL
copy(
const AR &source)
override {
return copyAny<AL, AR>(source); };
88 void copy(AL &x,
const AR &y)
override {
107 prof->start(
"ArrayHandlerIterable::axpy");
108 if (x.size() < y.size())
109 error(
"ArrayHandlerIterable::axpy() incompatible x and y arrays, x.size() < y.size()");
112 std::transform(
begin(y),
end(y),
begin(x),
begin(y), [alpha](
auto &ely,
auto &elx) {
return ely + alpha * elx; });
117 if (x.size() > y.size())
118 error(
"ArrayHandlerIterable::dot() incompatible x and y arrays, x.size() > y.size()");
125 gemm_outer_default(*
this, alphas, xx, yy);
129 return gemm_inner_default(*
this, xx, yy);
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);
138 std::map<size_t, value_type>
select(
size_t n,
const AL &x,
bool max =
false,
bool ignore_sign =
false)
override {
140 error(
"ArrayHandlerIterable::select() n is too large");
141 return util::select<AL, value_type>(n, x, max, ignore_sign);
151 template <
typename T,
typename S,
typename std::enable_if_t<util::is_array_v<T>, std::
nullptr_t> =
nullptr>
154 copy(result, source);
158 template <
typename T,
typename S,
typename std::enable_if_t<!util::is_array_v<T> && util::is_allocatable_v<T>,
int> = 0>
160 auto result = T(source.size());
161 copy(result, source);
165 template <
typename T,
typename S,
typename std::enable_if_t<!util::is_array_v<T> && !util::is_allocatable_v<T>,
int> = 0>
167 T result = allocate_array<T>(source.size());
168 copy(result, source);
Array handler for two containers both of which can be iterated through using begin() and end() member...
Definition: ArrayHandlerIterable.h:75
ArrayHandlerIterable()=default
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