rosa/inc/alsk/impl/callable.h

90 lines
2.6 KiB
C
Raw Normal View History

2021-05-10 16:14:24 +00:00
#ifndef ALSK_ALSK_IMPL_CALLABLE_H
#define ALSK_ALSK_IMPL_CALLABLE_H
#include "../skeleton/utility.h"
#include "../executor/executorstate.h"
namespace alsk {
template<typename Context, typename Executor>
struct CallableState {
Context context;
exec::ExecutorState<Executor> executor;
// out-of-class definition to silence -Winline
~CallableState() noexcept;
};
template<typename Context, typename Executor>
CallableState<Context, Executor>::~CallableState() noexcept {}
template<typename> struct Callable;
template<
template<typename...> class Impl,
template<typename...> class Skel, typename R, typename... Operands, typename... Tasks,
typename Tag, typename Executor, typename State
>
struct Callable<Impl<Skel<R(Operands...), Tasks...>, Tag, Executor, State>>:
Impl<Skel<R(Operands...), Tasks...>, Tag, Executor, State> {
using Skeleton = Skel<R(Operands...), Tasks...>;
using Implementation = Impl<Skeleton, Tag, Executor, State>;
using ThisType = Callable<Implementation>;
private:
std::size_t _parTasksCount;
public:
State state;
template<typename... Args, std::enable_if_t<sizeof...(Args) == 2>* = nullptr>
constexpr Callable(Args&&... args):
Implementation(std::forward<Args>(args)..., state),
_parTasksCount{0}
{}
// out-of-class definition to silence -Winline
~Callable() noexcept;
constexpr decltype(auto) operator()(Operands... operands) {
update();
return Implementation::operator()(operands...);
}
template<typename F, typename T>
constexpr decltype(auto) traverse(F&& function, T&& init) {
return SkeletonTraversal<Skeleton>::execute(Implementation::skeleton, std::forward<F>(function), std::forward<T>(init));
}
static constexpr tmp::Depth parallelizableLevels() {
return skeletonParallelHeight<Skeleton>;
}
std::size_t parallelTasksCount() const { return _parTasksCount; }
constexpr void update() {
auto traverser = [](std::size_t, auto&& skl, auto&&... values) {
using Skl = decltype(skl);
auto subpar = max(values...);
skl.step = subpar;
return SkeletonTraitsT<Skl>::parallelizability(std::forward<Skl>(skl)) * subpar;
};
_parTasksCount = traverse(traverser, 1ul);
ThisType::executor.config(*this);
state.context.setup(ThisType::executor.contextIdCount(static_cast<Implementation&>(*this), _parTasksCount));
}
};
template<
template<typename...> class Impl,
template<typename...> class Skel, typename R, typename... Operands, typename... Tasks,
typename Tag, typename Executor, typename State
>
Callable<Impl<Skel<R(Operands...), Tasks...>, Tag, Executor, State>>::~Callable() noexcept {}
}
#endif