32 #ifndef MADNESS_WORLD_TASKFN_H__INCLUDED
33 #define MADNESS_WORLD_TASKFN_H__INCLUDED
35 #include <type_traits>
41 #define MADNESS_TASKQ_VARIADICS 1
51 template <
typename,
typename,
typename>
class MemFuncWrapper;
52 template <
typename ptrT,
typename memfnT,
typename resT>
141 MADNESS_EXCEPTION(
"World TaskInterface: user did not implement one of run(world) or run(world, taskthreadenv)", 0);
148 MADNESS_EXCEPTION(
"World TaskInterface: user did not implement run(world, taskthreadenv) for multithreaded task", 0);
160 template <
typename T>
162 static const unsigned int value = 1u;
171 template <
typename a1T,
typename a2T,
typename a3T,
typename a4T,
typename a5T,
172 typename a6T,
typename a7T,
typename a8T,
typename a9T>
173 struct ArgCount :
public std::integral_constant<unsigned int, ArgCountHelper<a1T>::value
174 + ArgCountHelper<a2T>::value + ArgCountHelper<a3T>::value
175 + ArgCountHelper<a4T>::value + ArgCountHelper<a5T>::value
176 + ArgCountHelper<a6T>::value + ArgCountHelper<a7T>::value
177 + ArgCountHelper<a8T>::value + ArgCountHelper<a9T>::value> {
182 template <
typename Arg>
188 template <
typename Arg_ = Arg,
189 typename = std::enable_if_t<
190 !std::is_same<std::decay_t<Arg_>,
201 operator Arg&() & {
return arg_; }
202 operator const Arg&()
const& {
return arg_; }
203 explicit operator Arg&&() && {
return std::move(
arg_); }
206 template <
typename T>
208 template <
typename T>
211 template <
typename T>
217 template <
typename T>
223 template <
typename T>
241 template <
typename fnT>
253 namespace arg_holder {
256 template <
typename T>
262 template <
typename T>
268 template <
typename T>
270 return static_cast<T&&
>(arg_holder);
275 template <
typename T>
281 template <
typename T>
287 template <
typename T>
289 return static_cast<T&&
>(arg_holder);
293 template <
typename T>
295 return static_cast<T&
>(*arg_holder);
299 template <
typename T>
300 auto decay(
T&& arg_holder) ->
typename std::enable_if<
302 return std::forward<T>(arg_holder);
315 template <
typename fnT>
320 { fn(); result.
set(); }
322 template <
typename fnT,
typename a1T>
329 template <
typename fnT,
typename a1T,
typename a2T>
336 template <
typename fnT,
typename a1T,
typename a2T,
typename a3T>
343 template <
typename fnT,
typename a1T,
typename a2T,
typename a3T,
typename a4T>
350 template <
typename fnT,
typename a1T,
typename a2T,
typename a3T,
typename a4T,
354 a3T& a3, a4T& a4, a5T& a5,
const voidT&,
const voidT&,
358 template <
typename fnT,
typename a1T,
typename a2T,
typename a3T,
typename a4T,
359 typename a5T,
typename a6T>
362 a3T& a3, a4T& a4, a5T& a5, a6T& a6,
const voidT&,
const voidT&,
366 template <
typename fnT,
typename a1T,
typename a2T,
typename a3T,
typename a4T,
367 typename a5T,
typename a6T,
typename a7T>
370 a3T& a3, a4T& a4, a5T& a5, a6T& a6, a7T& a7,
const voidT&,
374 template <
typename fnT,
typename a1T,
typename a2T,
typename a3T,
typename a4T,
375 typename a5T,
typename a6T,
typename a7T,
typename a8T>
378 a3T& a3, a4T& a4, a5T& a5, a6T& a6, a7T& a7, a8T& a8,
const voidT&)
381 template <
typename fnT,
typename a1T,
typename a2T,
typename a3T,
typename a4T,
382 typename a5T,
typename a6T,
typename a7T,
typename a8T,
typename a9T>
385 a3T& a3, a4T& a4, a5T& a5, a6T& a6, a7T& a7, a8T& a8, a9T& a9)
388 template <
typename fnT>
393 { result.
set(fn()); }
395 template <
typename fnT,
typename a1T>
402 template <
typename fnT,
typename a1T,
typename a2T>
409 template <
typename fnT,
typename a1T,
typename a2T,
typename a3T>
416 template <
typename fnT,
typename a1T,
typename a2T,
typename a3T,
typename a4T>
419 fnT fn, a1T&
a1, a2T&
a2, a3T& a3, a4T& a4,
const voidT&,
423 template <
typename fnT,
typename a1T,
typename a2T,
typename a3T,
typename a4T,
427 fnT fn, a1T&
a1, a2T&
a2, a3T& a3, a4T& a4, a5T& a5,
const voidT&,
431 template <
typename fnT,
typename a1T,
typename a2T,
typename a3T,
typename a4T,
432 typename a5T,
typename a6T>
435 fnT fn, a1T&
a1, a2T&
a2, a3T& a3, a4T& a4, a5T& a5, a6T& a6,
439 template <
typename fnT,
typename a1T,
typename a2T,
typename a3T,
typename a4T,
440 typename a5T,
typename a6T,
typename a7T>
443 fnT fn, a1T&
a1, a2T&
a2, a3T& a3, a4T& a4, a5T& a5, a6T& a6,
447 template <
typename fnT,
typename a1T,
typename a2T,
typename a3T,
typename a4T,
448 typename a5T,
typename a6T,
typename a7T,
typename a8T>
451 fnT fn, a1T&
a1, a2T&
a2, a3T& a3, a4T& a4, a5T& a5, a6T& a6,
452 a7T& a7, a8T& a8,
const voidT&)
455 template <
typename fnT,
typename a1T,
typename a2T,
typename a3T,
typename a4T,
456 typename a5T,
typename a6T,
typename a7T,
typename a8T,
typename a9T>
459 fnT fn, a1T&
a1, a2T&
a2, a3T& a3, a4T& a4, a5T& a5, a6T& a6,
460 a7T& a7, a8T& a8, a9T& a9)
469 template <
typename fnT,
typename arg1T = void,
typename arg2T = void,
470 typename arg3T = void,
typename arg4T = void,
typename arg5T = void,
471 typename arg6T = void,
typename arg7T = void,
typename arg8T = void,
472 typename arg9T =
void>
475 static_assert(not (std::is_const<arg1T>::value || std::is_reference<arg1T>::value),
476 "improper instantiation of TaskFn, arg1T cannot be a const or reference type");
477 static_assert(not (std::is_const<arg2T>::value || std::is_reference<arg2T>::value),
478 "improper instantiation of TaskFn, arg2T cannot be a const or reference type");
479 static_assert(not (std::is_const<arg3T>::value || std::is_reference<arg3T>::value),
480 "improper instantiation of TaskFn, arg3T cannot be a const or reference type");
481 static_assert(not (std::is_const<arg4T>::value || std::is_reference<arg4T>::value),
482 "improper instantiation of TaskFn, arg4T cannot be a const or reference type");
483 static_assert(not (std::is_const<arg5T>::value || std::is_reference<arg5T>::value),
484 "improper instantiation of TaskFn, arg5T cannot be a const or reference type");
485 static_assert(not (std::is_const<arg6T>::value || std::is_reference<arg6T>::value),
486 "improper instantiation of TaskFn, arg6T cannot be a const or reference type");
487 static_assert(not (std::is_const<arg7T>::value || std::is_reference<arg7T>::value),
488 "improper instantiation of TaskFn, arg7T cannot be a const or reference type");
489 static_assert(not (std::is_const<arg8T>::value || std::is_reference<arg8T>::value),
490 "improper instantiation of TaskFn, arg8T cannot be a const or reference type");
491 static_assert(not (std::is_const<arg9T>::value || std::is_reference<arg9T>::value),
492 "improper instantiation of TaskFn, arg9T cannot be a const or reference type");
496 typedef TaskFn<fnT, arg1T, arg2T, arg3T, arg4T, arg5T, arg6T, arg7T,
509 arg4T, arg5T, arg6T, arg7T, arg8T, arg9T>::value;
531 template <
typename fT>
534 template <
typename ptrT,
typename memfnT,
typename resT>
539 virtual void get_id(std::pair<void*,unsigned short>&
id)
const {
548 template <
typename T>
560 template <
typename T>
569 template <
typename T>
575 template <
typename T>
577 for(
typename std::vector<
Future<T> >::iterator it =
vec.begin(); it !=
vec.end(); ++it)
585 template <
typename T>
610 #if MADNESS_TASKQ_VARIADICS
629 template <
typename a1T>
639 template <
typename a1T,
typename a2T>
649 template <
typename a1T,
typename a2T,
typename a3T>
659 template <
typename a1T,
typename a2T,
typename a3T,
typename a4T>
669 template <
typename a1T,
typename a2T,
typename a3T,
typename a4T,
typename a5T>
679 template <
typename a1T,
typename a2T,
typename a3T,
typename a4T,
typename a5T,
682 a3T&& a3, a4T&& a4, a5T&& a5, a6T&& a6,
691 template <
typename a1T,
typename a2T,
typename a3T,
typename a4T,
typename a5T,
692 typename a6T,
typename a7T>
694 a3T&& a3, a4T&& a4, a5T&& a5, a6T&& a6,
703 template <
typename a1T,
typename a2T,
typename a3T,
typename a4T,
typename a5T,
704 typename a6T,
typename a7T,
typename a8T>
706 a3T&& a3, a4T&& a4, a5T&& a5, a6T&& a6, a7T&& a7,
715 template <
typename a1T,
typename a2T,
typename a3T,
typename a4T,
typename a5T,
716 typename a6T,
typename a7T,
typename a8T,
typename a9T>
718 a3T&& a3, a4T&& a4, a5T&& a5, a6T&& a6,
744 template <
typename a1T>
754 template <
typename a1T,
typename a2T>
764 template <
typename a1T,
typename a2T,
typename a3T>
774 template <
typename a1T,
typename a2T,
typename a3T,
typename a4T>
784 template <
typename a1T,
typename a2T,
typename a3T,
typename a4T,
typename a5T>
786 const a3T& a3,
const a4T& a4,
const a5T& a5,
const TaskAttributes& attr) :
794 template <
typename a1T,
typename a2T,
typename a3T,
typename a4T,
typename a5T,
797 const a3T& a3,
const a4T& a4,
const a5T& a5,
const a6T& a6,
806 template <
typename a1T,
typename a2T,
typename a3T,
typename a4T,
typename a5T,
807 typename a6T,
typename a7T>
809 const a3T& a3,
const a4T& a4,
const a5T& a5,
const a6T& a6,
818 template <
typename a1T,
typename a2T,
typename a3T,
typename a4T,
typename a5T,
819 typename a6T,
typename a7T,
typename a8T>
821 const a3T& a3,
const a4T& a4,
const a5T& a5,
const a6T& a6,
const a7T& a7,
830 template <
typename a1T,
typename a2T,
typename a3T,
typename a4T,
typename a5T,
831 typename a6T,
typename a7T,
typename a8T,
typename a9T>
833 const a3T& a3,
const a4T& a4,
const a5T& a5,
const a6T& a6,
834 const a7T& a7,
const a8T& a8,
const a9T& a9,
const TaskAttributes& attr) :
843 archive::BufferInputArchive& input_arch) :
double w(double t, double eps)
Definition: DKops.h:22
Disables default copy constructor and assignment operators.
Definition: nodefaults.h:49
The class used for callbacks (e.g., dependency tracking).
Definition: dependency_interface.h:61
virtual void notify()=0
Invoked by the callback to notify when a dependency is satisfied.
Provides an interface for tracking dependencies.
Definition: dependency_interface.h:100
void inc()
Increment the number of dependencies.
Definition: dependency_interface.h:251
void register_final_callback(CallbackInterface *callback)
Registers the final callback to be executed when ndepend==0; immediately invoked if ndepend==0.
Definition: dependency_interface.h:229
std::atomic< int > ndepend
Counts dependencies. For a valid object ndepend >= 0, after the final callback is executed ndepend is...
Definition: dependency_interface.h:105
Specialization of Future<void> for internal convenience. This does nothing useful!
Definition: future.h:736
static void set(const Future< void > &f)
Set the future from another void future.
Definition: future.h:784
A future is a possibly yet unevaluated value.
Definition: future.h:373
void set(const Future< T > &other)
A.set(B), where A and B are futures ensures A has/will have the same value as B.
Definition: future.h:508
void register_callback(CallbackInterface *callback)
Registers an object to be called when future is assigned.
Definition: future.h:708
bool probe() const
Check whether this future has been assigned.
Definition: future.h:631
Lowest level task interface.
Definition: thread.h:867
static std::enable_if< detail::function_traits< fnT >::value||detail::memfunc_traits< fnT >::value >::type make_id(std::pair< void *, unsigned short > &id, fnT fn)
Definition: thread.h:916
Contains attributes of a task.
Definition: thread.h:323
TaskAttributes(unsigned long flags=0)
Sets the attributes to the desired values.
Definition: thread.h:337
All world tasks must be derived from this public interface.
Definition: taskfn.h:69
virtual void run(const TaskThreadEnv &env)
Override this method to implement a multi-threaded task.
Definition: world_task_queue.cc:45
void set_info(World *w, CallbackInterface *c)
Set task info.
Definition: taskfn.h:89
virtual void run(World &world, const TaskThreadEnv &env)
Runs a multi-threaded task.
Definition: taskfn.h:145
World * get_world() const
Definition: taskfn.h:152
void register_submit_callback()
Adds call back to schedule task when outstanding dependencies are satisfied.
Definition: taskfn.h:95
TaskInterface(int ndepend, const char *caller, const TaskAttributes attr=TaskAttributes())
Definition: taskfn.h:114
static bool debug
Definition: taskfn.h:101
virtual ~TaskInterface()
Definition: taskfn.h:154
TaskInterface(const TaskAttributes &attr)
Create a new task with zero dependencies and given attributes.
Definition: taskfn.h:123
madness::TaskInterface::Submit submit
CallbackInterface * completion
Definition: taskfn.h:73
TaskInterface(int ndepend=0, const TaskAttributes attr=TaskAttributes())
Create a new task with ndepend dependencies (default 0) and given attributes.
Definition: taskfn.h:104
virtual void run(World &)
Runs a single-threaded task ... derived classes must implement this.
Definition: taskfn.h:139
volatile World * world
Definition: taskfn.h:72
Used to pass information about the thread environment to a user's task.
Definition: thread.h:466
int nthread() const
Get the number of threads collaborating on this task.
Definition: thread.h:499
static void add(PoolTaskInterface *task)
Add a new task to the pool.
Definition: thread.h:1333
Multi-threaded queue to manage and run tasks.
Definition: world_task_queue.h:319
A parallel world class.
Definition: world.h:132
A wrapper object for holding task function argument objects.
Definition: taskfn.h:183
ArgHolder(const archive::BufferInputArchive &input_arch)
Definition: taskfn.h:195
Arg arg_
Definition: taskfn.h:185
ArgHolder(Arg_ &&arg)
Definition: taskfn.h:193
Functor wrapper for object and member function pointers.
Definition: mem_func_wrapper.h:89
Defines DependencyInterface and CallbackInterface.
Implements Future and related items.
auto T(World &world, response_space &f) -> response_space
Definition: global_functions.cc:34
Tensor< typename Tensor< T >::scalar_type > arg(const Tensor< T > &t)
Return a new tensor holding the argument of each element of t (complex types only)
Definition: tensor.h:2502
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition: madness_exception.h:119
#define MADNESS_ASSERT(condition)
Assert a condition that should be free of side-effects since in release builds this might be a no-op.
Definition: madness_exception.h:134
auto decay(T &&arg_holder) -> typename std::enable_if< !is_future< T >::value &&!is_arg_holder< T >::value, decltype(arg_holder)>::type
Definition: taskfn.h:300
T & decay(ArgHolder< T > &arg_holder)
Definition: taskfn.h:257
std::enable_if< std::is_void< typename detail::result_of< fnT >::type >::value >::type run_function(Future< void > &result, fnT fn, const voidT &, const voidT &, const voidT &, const voidT &, const voidT &, const voidT &, const voidT &, const voidT &, const voidT &)
Definition: taskfn.h:317
Future< void > voidT
Definition: taskfn.h:250
memfnT get_mem_func_ptr(const MemFuncWrapper< ptrT, memfnT, resT > &wrapper)
Returns the member function pointer from a wrapper.
Definition: mem_func_wrapper.h:339
File holds all helper structures necessary for the CC_Operator and CC2 class.
Definition: DFParameters.h:10
std::shared_ptr< FunctionFunctorInterface< double, 3 > > func(new opT(g))
NDIM & f
Definition: mra.h:2416
std::string type(const PairType &n)
Definition: PNOParameters.h:18
Vector< T, sizeof...(Ts)+1 > vec(T t, Ts... ts)
Factory function for creating a madness::Vector.
Definition: vector.h:711
static const double c
Definition: relops.cc:10
Wrap a callable object and its arguments into a task function.
Definition: taskfn.h:473
void check_dependencies()
Check dependencies and register callbacks where necessary.
Definition: taskfn.h:592
futureT result_
The task Future result.
Definition: taskfn.h:515
detail::task_arg< arg4T >::holderT arg4_
Argument 4 that will be given to the function.
Definition: taskfn.h:524
detail::task_result_type< fnT >::resultT resultT
The result type of the function.
Definition: taskfn.h:502
TaskFn(const futureT &result, functionT func, a1T &&a1, a2T &&a2, a3T &&a3, a4T &&a4, a5T &&a5, a6T &&a6, a7T &&a7, const TaskAttributes &attr)
Definition: taskfn.h:693
detail::task_arg< arg7T >::holderT arg7_
Argument 7 that will be given to the function.
Definition: taskfn.h:527
void check_dependency(Future< T > *fut)
Register (pointer to) a non-ready future as a dependency.
Definition: taskfn.h:561
TaskFn(const futureT &result, functionT func, a1T &&a1, a2T &&a2, a3T &&a3, a4T &&a4, a5T &&a5, const TaskAttributes &attr)
Definition: taskfn.h:670
detail::task_arg< arg8T >::holderT arg8_
Argument 8 that will be given to the function.
Definition: taskfn.h:528
const futureT & result() const
Definition: taskfn.h:854
void check_dependency(const std::vector< Future< void > > &)
Future<void> is always ready => no op.
Definition: taskfn.h:582
TaskFn(const futureT &result, functionT func, a1T &&a1, a2T &&a2, a3T &&a3, a4T &&a4, const TaskAttributes &attr)
Definition: taskfn.h:660
TaskFn(const futureT &result, functionT func, a1T &&a1, a2T &&a2, const TaskAttributes &attr)
Definition: taskfn.h:640
virtual void get_id(std::pair< void *, unsigned short > &id) const
Definition: taskfn.h:539
void check_dependency(const Future< void > &)
Future<void> is always ready => no op.
Definition: taskfn.h:589
virtual void run(World &, const TaskThreadEnv &)
Runs a multi-threaded task.
Definition: taskfn.h:857
detail::task_arg< arg3T >::holderT arg3_
Argument 3 that will be given to the function.
Definition: taskfn.h:523
detail::task_result_type< fnT >::futureT futureT
Definition: taskfn.h:504
detail::task_arg< arg6T >::holderT arg6_
Argument 6 that will be given to the function.
Definition: taskfn.h:526
fnT functionT
The task function type.
Definition: taskfn.h:501
detail::task_arg< arg1T >::holderT arg1_
Argument 1 that will be given to the function.
Definition: taskfn.h:521
TaskFn(const futureT &result, functionT func, a1T &&a1, a2T &&a2, a3T &&a3, const TaskAttributes &attr)
Definition: taskfn.h:650
TaskFn(const futureT &result, functionT func, a1T &&a1, a2T &&a2, a3T &&a3, a4T &&a4, a5T &&a5, a6T &&a6, a7T &&a7, a8T &&a8, a9T &&a9, const TaskAttributes &attr)
Definition: taskfn.h:717
TaskFn(const futureT &result, functionT func, const TaskAttributes &attr, archive::BufferInputArchive &input_arch)
Definition: taskfn.h:727
void check_dependency(Future< T > &fut)
Register a non-ready future as a dependency.
Definition: taskfn.h:549
TaskFn(const futureT &result, functionT func, a1T &&a1, const TaskAttributes &attr)
Definition: taskfn.h:630
detail::task_arg< arg2T >::holderT arg2_
Argument 2 that will be given to the function.
Definition: taskfn.h:522
detail::task_arg< arg5T >::holderT arg5_
Argument 5 that will be given to the function.
Definition: taskfn.h:525
TaskFn(const futureT &result, functionT func, a1T &&a1, a2T &&a2, a3T &&a3, a4T &&a4, a5T &&a5, a6T &&a6, a7T &&a7, a8T &&a8, const TaskAttributes &attr)
Definition: taskfn.h:705
detail::task_arg< arg9T >::holderT arg9_
Argument 9 that will be given to the function.
Definition: taskfn.h:529
static const unsigned int arity
Definition: taskfn.h:508
void check_dependency(std::vector< Future< T > > &vec)
None future arguments are always ready => no op.
Definition: taskfn.h:576
TaskFn_ operator=(TaskFn_ &)
TaskFn(const futureT &result, functionT func, const TaskAttributes &attr)
Definition: taskfn.h:621
void check_dependency(const detail::ArgHolder< T > &)
None future arguments are always ready => no op.
Definition: taskfn.h:586
static memfnT get_func(const detail::MemFuncWrapper< ptrT, memfnT, resT > &wrapper)
Definition: taskfn.h:535
void check_dependency(detail::ArgHolder< std::vector< Future< T > > > &arg)
None future arguments are always ready => no op.
Definition: taskfn.h:570
TaskFn(const futureT &result, functionT func, a1T &&a1, a2T &&a2, a3T &&a3, a4T &&a4, a5T &&a5, a6T &&a6, const TaskAttributes &attr)
Definition: taskfn.h:681
virtual ~TaskFn()
Definition: taskfn.h:852
const functionT func_
The task function.
Definition: taskfn.h:516
TaskFn< fnT, arg1T, arg2T, arg3T, arg4T, arg5T, arg6T, arg7T, arg8T, arg9T > TaskFn_
This class type.
Definition: taskfn.h:476
static fT & get_func(fT &f)
Definition: taskfn.h:532
PoolTaskInterface * p
Definition: taskfn.h:77
Submit(PoolTaskInterface *p)
Definition: taskfn.h:78
void notify()
Invoked by the callback to notify when a dependency is satisfied.
Definition: taskfn.h:79
static const unsigned int value
Definition: taskfn.h:162
T type
Definition: taskfn.h:219
Future< T > holderT
Definition: taskfn.h:220
T type
Definition: taskfn.h:225
Future< T > * holderT
Definition: taskfn.h:226
const Future< void > holderT
Definition: taskfn.h:232
const Future< void > type
Definition: taskfn.h:231
const Future< void > holderT
Definition: taskfn.h:238
const Future< void > type
Definition: taskfn.h:237
ArgHolder< T > holderT
Definition: taskfn.h:214
T type
Definition: taskfn.h:213
futureT type
Definition: taskfn.h:245
madness::remove_fcvr< typename madness::detail::result_of< fnT >::type >::type resultT
Definition: taskfn.h:243
Future< resultT > futureT
Definition: taskfn.h:244
test if a type is a future.
Definition: type_traits.h:79
Definition: type_traits.h:197
double u(const double x, const double expnt)
Definition: testperiodic.cc:56
Implements Dqueue, Thread, ThreadBase and ThreadPool.
const double a2
Definition: vnucso.cc:86
const double a1
Definition: vnucso.cc:85