ttg 1.0.0
Template Task Graph (TTG): flowgraph-based programming model for high-performance distributed-memory algorithms
Loading...
Searching...
No Matches
callable.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2//
3// Created by Eduard Valeyev on 3/1/22.
4//
5
6#ifndef TTG_META_CALLABLE_H
7#define TTG_META_CALLABLE_H
8
9#include <type_traits>
10
11#include "ttg/util/meta.h"
12#include "ttg/util/typelist.h"
13
14namespace ttg::meta {
15
17 // callable signature introspection
19 namespace detail {
20 template <typename R, typename... Args>
22 using return_type = R;
23 using args = ttg::typelist<Args...>;
24 };
25
27 template <typename T, typename Enabler = void>
29
30 // functions and function pointers; C-style varargs are not part of the argument list
31 template <typename R, typename... Args>
32 struct callable_traits_impl<R(Args...)> : callable_signature<R, Args...> {};
33 template <typename R, typename... Args>
35 template <typename R, typename... Args>
36 struct callable_traits_impl<R(Args..., ...)> : callable_signature<R, Args...> {};
37 template <typename R, typename... Args>
38 struct callable_traits_impl<R(Args..., ...) noexcept> : callable_signature<R, Args...> {};
39 template <typename F>
40 struct callable_traits_impl<F*, std::enable_if_t<std::is_function_v<F>>> : callable_traits_impl<F> {};
41
42 // member function pointers: the (reference to the) object is the first argument, args_sans_object omits it.
43 // The macros below expand to one partial specialization per combination of the PMF's qualifiers:
44 // 4 cv (none, const, volatile, const volatile) x 3 ref (none, &, &&) x 2 noexcept x 2 C-style varargs = 48.
45 // OBJREF is the reference type of the object argument: & for unqualified and &-qualified PMFs, && for
46 // &&-qualified ones (e.g. `void (C::*)(int) const` -> args = typelist<const C&, int>).
47 // Abominable function types (e.g. `void(int) const`) are not callables and are deliberately not handled.
48#define TTG_CALLABLE_TRAITS_PMF_IMPL(CV, REF, OBJREF, NOEXCEPT, VARARGS) \
49 template <typename R, typename C, typename... Args> \
50 struct callable_traits_impl<R (C::*)(Args... VARARGS) CV REF NOEXCEPT> \
51 : callable_signature<R, CV C OBJREF, Args...> { \
52 using args_sans_object = ttg::typelist<Args...>; \
53 };
54#define TTG_CALLABLE_TRAITS_COMMA_ELLIPSIS , ...
55#define TTG_CALLABLE_TRAITS_PMF_VARARGS(CV, REF, OBJREF, NOEXCEPT) \
56 TTG_CALLABLE_TRAITS_PMF_IMPL(CV, REF, OBJREF, NOEXCEPT, ) \
57 TTG_CALLABLE_TRAITS_PMF_IMPL(CV, REF, OBJREF, NOEXCEPT, TTG_CALLABLE_TRAITS_COMMA_ELLIPSIS)
58#define TTG_CALLABLE_TRAITS_PMF_NOEXCEPT(CV, REF, OBJREF) \
59 TTG_CALLABLE_TRAITS_PMF_VARARGS(CV, REF, OBJREF, ) \
60 TTG_CALLABLE_TRAITS_PMF_VARARGS(CV, REF, OBJREF, noexcept)
61#define TTG_CALLABLE_TRAITS_PMF_REF(CV) \
62 TTG_CALLABLE_TRAITS_PMF_NOEXCEPT(CV, , &) \
63 TTG_CALLABLE_TRAITS_PMF_NOEXCEPT(CV, &, &) \
64 TTG_CALLABLE_TRAITS_PMF_NOEXCEPT(CV, &&, &&)
65
70
71#undef TTG_CALLABLE_TRAITS_PMF_REF
72#undef TTG_CALLABLE_TRAITS_PMF_NOEXCEPT
73#undef TTG_CALLABLE_TRAITS_PMF_VARARGS
74#undef TTG_CALLABLE_TRAITS_COMMA_ELLIPSIS
75#undef TTG_CALLABLE_TRAITS_PMF_IMPL
76
78 template <typename Typelist>
80 template <typename First, typename... Rest>
81 struct typelist_split_first<ttg::typelist<First, Rest...>> {
82 using first = First;
83 using rest = ttg::typelist<Rest...>;
84 };
85
86 template <typename F, typename Typelist>
87 constexpr bool is_invocable_with_v = false;
88 template <typename F, typename... Args>
89 constexpr bool is_invocable_with_v<F, ttg::typelist<Args...>> = std::is_invocable_v<F, Args...>;
90
95 template <typename T, typename CallOpPtr, typename Enabler = void>
96 constexpr bool is_explicit_object_call_op_v = false;
97 template <typename T, typename CallOpPtr>
98 constexpr bool is_explicit_object_call_op_v<
99 T, CallOpPtr,
100 std::enable_if_t<std::is_pointer_v<CallOpPtr> &&
101 std::is_base_of_v<std::remove_cvref_t<typename typelist_split_first<
103 T>>> =
106
109 template <typename T, typename CallOpPtr, typename Enabler = void>
111 // implicit object parameter (nonstatic operator()): drop the object argument
112 template <typename T, typename CallOpPtr>
113 struct function_object_traits<T, CallOpPtr, std::enable_if_t<std::is_member_function_pointer_v<CallOpPtr>>> {
116 };
117 // explicit object parameter: &T::operator() is a function pointer whose first parameter is the object, drop it
118 template <typename T, typename CallOpPtr>
123
124 // function objects with a unique (non-template, non-overloaded) operator()
125 template <typename T>
126 struct callable_traits_impl<T, std::void_t<decltype(&T::operator())>>
127 : function_object_traits<T, decltype(&T::operator())> {};
128 } // namespace detail
129
132 template <typename Callable>
134
137 template <typename Callable>
139
141 template <typename Callable>
143
145 // nongeneric callables
147 // to detect whether a callable is generic or not detect existence of callable_args_t
148 template <typename Callable, typename = void>
149 struct is_generic_callable : std::true_type {};
150
151 template <typename Callable>
152 struct is_generic_callable<Callable, ttg::meta::void_t<callable_args_t<Callable>>> : std::false_type {};
153
154 template <typename Callable>
156
166 template <typename Callable, typename Enabler = void>
167 constexpr std::pair<bool, std::pair<ttg::typelist<>, ttg::typelist<>>> callable_args = {true, {}};
168
169 template <typename Callable>
171 std::pair<bool, std::pair<ttg::typelist<callable_return_t<Callable>>, callable_args_t<Callable>>>{false, {}};
172
174 // generic callables
176
178
182 template <std::size_t N>
183 constexpr auto ordinal2index(std::size_t ordinal, std::array<std::size_t, N> extents) {
184 std::array<std::size_t, N> idx = {};
185 for (size_t d = 0; d < N; ++d) {
186 idx[d] = ordinal % extents[d];
187 ordinal /= extents[d];
188 }
189 return idx;
190 }
191
205 template <std::size_t Ordinal, typename Func, typename... Typelists, std::size_t... ArgIdx>
207 std::index_sequence<ArgIdx...> arg_idx = {}) {
209 constexpr auto Order = sizeof...(Typelists);
210 constexpr std::array<std::size_t, Order> extents = {
211 std::tuple_size_v<std::tuple_element_t<ArgIdx, arg_typelists_t>>...};
212 constexpr auto tensor_size = (extents[ArgIdx] * ...);
213 static_assert(tensor_size >= Ordinal);
214 if constexpr (tensor_size == Ordinal) {
216 } else {
217 constexpr auto idx = ordinal2index(Ordinal, extents);
219 using args_sans_void_t = drop_void_t<decltype(args)>;
222 return ttg::typelist<ttg::typelist<return_type>, decltype(args)>{};
223 } else {
225 }
226 }
227 }
228
243 template <std::size_t Ordinal, typename ReturnType, typename Func, typename... Typelists, std::size_t... ArgIdx>
245 std::index_sequence<ArgIdx...> arg_idx = {}) {
247 constexpr auto Order = sizeof...(Typelists);
248 constexpr std::array<std::size_t, Order> extents = {
249 std::tuple_size_v<std::tuple_element_t<ArgIdx, arg_typelists_t>>...};
250 constexpr auto tensor_size = (extents[ArgIdx] * ...);
251 static_assert(tensor_size >= Ordinal);
252 if constexpr (tensor_size == Ordinal) {
253 return typelist<>{};
254 } else {
255 constexpr auto idx = ordinal2index(Ordinal, extents);
257 if constexpr (is_invocable_typelist_r_v<ReturnType, Func, drop_void_t<decltype(args)>>) {
258 return args;
259 } else {
261 }
262 }
263 }
264
275 template <typename Func, typename... Typelists>
277 constexpr auto is_generic__args = callable_args<Func&>;
278 constexpr bool is_generic = is_generic__args.first;
279 if constexpr (is_generic) {
281 std::make_index_sequence<sizeof...(Typelists)>{});
282 } else {
283 return is_generic__args.second;
284 }
285 }
286
296 template <typename ReturnType, typename Func, typename... Typelists>
298 constexpr auto is_generic__args = callable_args<Func&>;
299 constexpr bool is_generic = is_generic__args.first;
300 if constexpr (is_generic) {
302 std::make_index_sequence<sizeof...(Typelists)>{});
303 } else {
304 return is_generic__args.second.second;
305 }
306 }
307
310 template <typename T, typename = void>
312
313 template <typename T>
314 struct candidate_argument_bindings<T, std::enable_if_t<!std::is_reference_v<T> && !std::is_void_v<T>>> {
315 using type = std::conditional_t<std::is_const_v<T>, typelist<const T&>,
316 typelist<
317 // RATIONALE for this order of binding detection tries:
318 // - to be able to distinguish arguments declared as auto& vs auto&& should try
319 // binding to T&& first since auto& won't bind to it
320 // - HOWEVER argument declared as const T& will bind to either T&& or const T&,
321 // so this order will detect such argument as binding to T&&, which will
322 // indicate to the runtime that the argument is CONSUMABLE and may cause
323 // creation of extra copies. Thus you should not try to use nongeneric
324 // data arguments in generic task functions; for purely nongeneric functions
325 // a different introspection mechanism (callable_traits) is used
326 T&&, const T&
327 // - no need to check T& since auto& and auto&& both bind to it
328 //, T&
329 >>;
330 };
331
332 template <>
336
337 template <>
341
342 template <typename T>
344} // namespace ttg::meta
345
346#endif // TTG_META_CALLABLE_H
#define TTG_CALLABLE_TRAITS_PMF_REF(CV)
Definition callable.h:61
STL namespace.
constexpr bool is_explicit_object_call_op_v
Definition callable.h:96
constexpr bool is_invocable_with_v
Definition callable.h:87
auto compute_arg_binding_types_r(Func &func, typelist< Typelists... > argument_type_lists)
Definition callable.h:297
typename callable_traits< Callable >::args callable_args_t
Definition callable.h:138
auto compute_arg_binding_types_impl(Func &func, typelist< Typelists... > argument_type_lists, std::index_sequence< ArgIdx... > arg_idx={})
Definition callable.h:206
auto compute_arg_binding_types(Func &func, typelist< Typelists... > argument_type_lists)
Definition callable.h:276
constexpr std::pair< bool, std::pair< ttg::typelist<>, ttg::typelist<> > > callable_args
Definition callable.h:167
void void_t
Definition meta.h:21
constexpr bool is_generic_callable_v
Definition callable.h:155
typename callable_traits< Callable >::return_type callable_return_t
return type of a nongeneric Callable; SFINAE-friendly
Definition callable.h:142
constexpr auto ordinal2index(std::size_t ordinal, std::array< std::size_t, N > extents)
Definition callable.h:183
auto compute_arg_binding_types_r_impl(Func &func, typelist< Typelists... > argument_type_lists, std::index_sequence< ArgIdx... > arg_idx={})
Definition callable.h:244
typename candidate_argument_bindings< T >::type candidate_argument_bindings_t
Definition callable.h:343
constexpr auto get(typelist< T, RestOfTs... >)
Definition typelist.h:102
top-level TTG namespace contains runtime-neutral functionality
Definition keymap.h:9
std::conditional_t< std::is_const_v< T >, typelist< const T & >, typelist< T &&, const T & > > type
Definition callable.h:329
primary template: not a callable with a unique signature (e.g. generic or overloaded callables)
Definition callable.h:28
typename typelist_split_first< typename callable_traits_impl< CallOpPtr >::args >::rest args
Definition callable.h:121
splits a nonempty ttg::typelist into its first type and the rest; has no members for an empty typelis...
Definition callable.h:79
A container for types.
Definition typelist.h:25