1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_UTIL_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_UTIL_H
15#include <molpro/linalg/array/Span.h>
16#include <molpro/mpi.h>
25template <
typename T, std::
size_t N>
32constexpr bool is_array_v = std::is_array<T>::value || is_std_array_v<T>;
34template <
typename T,
typename =
void>
41struct is_allocatable<T, std::enable_if_t<std::is_constructible_v<T, std::size_t>>> : std::true_type {};
65 if constexpr (util::is_allocatable_v<T>) {
69 throw std::runtime_error(
"Required molpro::linalg::array::allocate_array for type '"
70 + std::string(
typeid(T).name())
71 +
"' - you have to provide an explicit template specialization for this function & type");
83 if constexpr (is_array_v<T>)
85 else if constexpr (is_allocatable_v<T>)
88 return allocate_array<T>(size);
94 return Span<std::remove_pointer_t<
decltype(std::data(r))>>(std::data(r), std::size(r));
142 std::shared_ptr<Proxy>
scope();
152 decltype(std::declval<LockMPI3>().scope())
l;
155template <
typename Result =
void>
158 Task(std::future<Result> &&task) :
m_task{std::move(task)} {};
161 template <
class Func,
typename... Args>
163 return {std::async(std::launch::async, std::forward<Func>(f), std::forward<Args>(args)...)};
175 bool test() {
return m_task.wait_for(std::chrono::microseconds{1}) == std::future_status::ready; }
179 throw std::future_error(std::future_errc::no_state);
Non-owning container taking a pointer to the data buffer and its size and exposing routines for itera...
Definition Span.h:31
Atomic lock allowing only one process to acquire it. Implemented using MPI3 RMA.
Definition util.h:103
void unlock()
Release the lock.
Definition util.cpp:24
MPI_Comm m_comm
MPI communicator.
Definition util.h:105
~LockMPI3()
Release the lock and destroy it. This is collective and must be called by all processes in the commun...
Definition util.cpp:10
void lock()
Acquire exclusive lock.
Definition util.cpp:17
std::shared_ptr< Proxy > scope()
Definition util.cpp:31
bool m_locked
whether lock is active
Definition util.h:107
std::weak_ptr< Proxy > m_proxy
Keep track of proxy object so that if lock is deleted, the proxy does not try to unlock.
Definition util.h:137
LockMPI3(const LockMPI3 &)=delete
LockMPI3 & operator=(const LockMPI3 &)=delete
MPI_Win m_win
empty window handle
Definition util.h:106
Utility object that locks on creation and unlocks on destruction.
Definition util.h:146
LockMPI3 lock
Definition util.h:151
ScopeLock(MPI_Comm comm)
Definition util.h:148
decltype(std::declval< LockMPI3 >().scope()) l
Definition util.h:152
Task(Task &&other)=default
static Task create(Func &&f, Args &&...args)
Definition util.h:162
Task(std::future< Result > &&task)
Definition util.h:158
Result wait()
wait for the task to complete and return its result
Definition util.h:177
std::future< Result > m_task
Definition util.h:184
~Task()
Definition util.h:166
bool test()
Returns true if the task has completed.
Definition util.h:175
Definition ArrayHandler.h:24
T allocate_like(std::size_t size)
Creates an array of type T able to hold size elements.
Definition util.h:82
auto contiguous_span(R &r)
A view of all the elements of a contiguous array.
Definition util.h:93
constexpr bool is_array_v
Definition util.h:32
constexpr bool is_std_array_v
Definition util.h:29
constexpr bool is_allocatable_v
Definition util.h:44
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 util.h:64
Proxy that locks on creation and unlocks on destruction. Useful for locking a scope.
Definition util.h:127
LockMPI3 & m_lock
Definition util.h:128
bool m_deleted
whether the lock was already deleted
Definition util.h:133
~Proxy()
Definition util.cpp:42