MADNESS  version 0.9
worldtask.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31 
32  $Id$
33 */
34 
35 
36 #ifndef MADNESS_WORLD_WORLDTASK_H__INCLUDED
37 #define MADNESS_WORLD_WORLDTASK_H__INCLUDED
38 
41 
42 // To do:
43 // a) Redo this ontop of serializable tasks which will remote much of the clutter
44 // due to multiple length argument lists.
45 // b) Stealing which pretty much presume a) has been done
46 
47 #include <iostream>
51 #include <madness/world/taskfn.h>
52 
53 namespace madness {
54 
55  // Forward decls
56  class World;
57  class WorldTaskQueue;
58  template <typename> struct TaskFunction;
59  template <typename> struct TaskMemfun;
60 
61  namespace detail {
62 
63  template <typename ptrT, typename memfnT, typename resT>
64  memfnT get_mem_func_ptr(const MemFuncWrapper<ptrT, memfnT, resT>&);
65  template <typename, typename> class ForEachRootTask;
66 
71  template <typename refT, typename functionT>
72  struct TaskHandlerInfo {
73  refT ref;
76 
78 
82  TaskHandlerInfo(const refT& ref, functionT func, const TaskAttributes& attr)
83  : ref(ref), func(func),attr(attr) {}
85 
87 
90  template <typename Archive>
91  void serialize(const Archive& ar) {
92  serialize_internal<functionT>(ar);
93  }
94 
95  private:
96 
98 
100  template <typename fnT>
101  struct is_func_ptr {
102  static const bool value =
105  };
106 
108 
112  template <typename fnT, typename Archive>
113  typename enable_if<is_func_ptr<fnT> >::type
114  serialize_internal(const Archive& ar) {
115  ar & ref & archive::wrap_opaque(func) & attr;
116  }
117 
122  template <typename fnT, typename Archive>
123  typename disable_if<is_func_ptr<fnT> >::type
124  serialize_internal(const Archive& ar) {
125  ar & ref & func & attr;
126  }
127  }; // struct TaskHandlerInfo
128 
129  template <typename fnT>
130  struct function_enabler : public
132  function_traits<fnT>::value || has_result_type<fnT>::value,
133  task_result_type<fnT> >
134  { };
135 
136  template <typename memfnT>
137  struct memfunc_enabler : public
139  std::is_member_function_pointer<memfnT>,
140  task_result_type<memfnT> >
141  { };
142 
143  template <typename T>
144  struct DefaultInitPtr {
145  static T init() { return NULL; }
146  };
147 
148  template <typename T>
149  struct DefaultInitPtr<std::shared_ptr<T> > {
151  };
152 
153  template <typename ptrT, typename memfnT, typename resT>
154  class MemFuncWrapper {
155  private:
156  ptrT ptr_;
157  memfnT memfn_;
158 
159  friend memfnT get_mem_func_ptr<ptrT, memfnT, resT>(const MemFuncWrapper<ptrT, memfnT, resT>&);
160 
161  public:
163  typedef resT result_type;
164  typedef memfnT memfn_type;
165 
166  MemFuncWrapper() : ptr_(DefaultInitPtr<ptrT>::init()), memfn_() { }
167 
168  MemFuncWrapper(const MemFuncWrapper_& other) :
169  ptr_(other.ptr_), memfn_(other.memfn_)
170  { }
171 
172  MemFuncWrapper(ptrT ptr, memfnT memfn) :
173  ptr_(ptr), memfn_(memfn)
174  { }
175 
176  MemFuncWrapper_& operator=(const MemFuncWrapper_& other) {
177  ptr_ = other.ptr_;
178  memfn_ = other.memfn_;
179  return *this;
180  }
181 
182  resT operator()() const {
183  MADNESS_ASSERT(ptr_);
184  return ((*ptr_).*memfn_)();
185  }
186 
187  template <typename a1T>
188  resT operator()(a1T& a1) const {
189  MADNESS_ASSERT(ptr_);
190  return ((*ptr_).*memfn_)(a1);
191  }
192 
193  template <typename a1T, typename a2T>
194  resT operator()(a1T& a1, a2T& a2) const {
195  MADNESS_ASSERT(ptr_);
196  return ((*ptr_).*memfn_)(a1, a2);
197  }
198 
199  template <typename a1T, typename a2T, typename a3T>
200  resT operator()(a1T& a1, a2T& a2, a3T& a3) const {
201  MADNESS_ASSERT(ptr_);
202  return ((*ptr_).*memfn_)(a1, a2, a3);
203  }
204 
205  template <typename a1T, typename a2T, typename a3T, typename a4T>
206  resT operator()(a1T& a1, a2T& a2, a3T& a3, a4T& a4) const {
207  MADNESS_ASSERT(ptr_);
208  return ((*ptr_).*memfn_)(a1, a2, a3, a4);
209  }
210 
211  template <typename a1T, typename a2T, typename a3T, typename a4T,
212  typename a5T>
213  resT operator()(a1T& a1, a2T& a2, a3T& a3, a4T& a4, a5T& a5) const {
214  MADNESS_ASSERT(ptr_);
215  return ((*ptr_).*memfn_)(a1, a2, a3, a4, a5);
216  }
217 
218  template <typename a1T, typename a2T, typename a3T, typename a4T,
219  typename a5T, typename a6T>
220  resT operator()(a1T& a1, a2T& a2, a3T& a3, a4T& a4, a5T& a5, a6T& a6) const {
221  MADNESS_ASSERT(ptr_);
222  return ((*ptr_).*memfn_)(a1, a2, a3, a4, a5, a6);
223  }
224 
225  template <typename a1T, typename a2T, typename a3T, typename a4T,
226  typename a5T, typename a6T, typename a7T>
227  resT operator()(a1T& a1, a2T& a2, a3T& a3, a4T& a4, a5T& a5, a6T& a6, a7T& a7) const {
228  MADNESS_ASSERT(ptr_);
229  return ((*ptr_).*memfn_)(a1, a2, a3, a4, a5, a6, a7);
230  }
231 
232  template <typename a1T, typename a2T, typename a3T, typename a4T,
233  typename a5T, typename a6T, typename a7T, typename a8T>
234  resT operator()(a1T& a1, a2T& a2, a3T& a3, a4T& a4, a5T& a5, a6T& a6, a7T& a7, a8T& a8) const {
235  MADNESS_ASSERT(ptr_);
236  return ((*ptr_).*memfn_)(a1, a2, a3, a4, a5, a6, a7, a8);
237  }
238 
239  template <typename a1T, typename a2T, typename a3T, typename a4T,
240  typename a5T, typename a6T, typename a7T, typename a8T, typename a9T>
241  resT operator()(a1T& a1, a2T& a2, a3T& a3, a4T& a4, a5T& a5, a6T& a6, a7T& a7, a8T& a8, a9T& a9) const {
242  MADNESS_ASSERT(ptr_);
243  return ((*ptr_).*memfn_)(a1, a2, a3, a4, a5, a6, a7, a8, a9);
244  }
245 
246  template <typename Archive>
247  void serialize(const Archive& ar) {
248  ar & ptr_ & memfn_;
249  }
250 
251  friend memfnT get_mem_func_ptr(const MemFuncWrapper_& wrapper) {
252  return wrapper.memfn_;
253  }
254  }; // class MemFuncWrapper
255 
256  template <typename ptrT, typename memfnT>
257  class MemFuncWrapper<ptrT, memfnT, void> {
258  private:
259  ptrT ptr_;
260  memfnT memfn_;
261 
262  friend memfnT get_mem_func_ptr<ptrT, memfnT, void>(const MemFuncWrapper<ptrT, memfnT, void>&);
263 
264  public:
266  typedef void result_type;
267  typedef memfnT memfn_type;
268 
269  MemFuncWrapper() : ptr_(DefaultInitPtr<ptrT>::init()), memfn_() { }
270 
271  MemFuncWrapper(const MemFuncWrapper_& other) :
272  ptr_(other.ptr_), memfn_(other.memfn_)
273  { }
274 
275  MemFuncWrapper(ptrT ptr, memfnT memfn) :
276  ptr_(ptr), memfn_(memfn)
277  { }
278 
279  MemFuncWrapper_& operator=(const MemFuncWrapper_& other) {
280  ptr_ = other.ptr_;
281  memfn_ = other.memfn_;
282  return *this;
283  }
284 
285  void operator()() const {
286  MADNESS_ASSERT(ptr_);
287  ((*ptr_).*memfn_)();
288  }
289 
290  template <typename a1T>
291  void operator()(a1T& a1) const {
292  MADNESS_ASSERT(ptr_);
293  ((*ptr_).*memfn_)(a1);
294  }
295 
296  template <typename a1T, typename a2T>
297  void operator()(a1T& a1, a2T& a2) const {
298  MADNESS_ASSERT(ptr_);
299  ((*ptr_).*memfn_)(a1, a2);
300  }
301 
302  template <typename a1T, typename a2T, typename a3T>
303  void operator()(a1T& a1, a2T& a2, a3T& a3) const {
304  MADNESS_ASSERT(ptr_);
305  ((*ptr_).*memfn_)(a1, a2, a3);
306  }
307 
308  template <typename a1T, typename a2T, typename a3T, typename a4T>
309  void operator()(a1T& a1, a2T& a2, a3T& a3, a4T& a4) const {
310  MADNESS_ASSERT(ptr_);
311  ((*ptr_).*memfn_)(a1, a2, a3, a4);
312  }
313 
314  template <typename a1T, typename a2T, typename a3T, typename a4T,
315  typename a5T>
316  void operator()(a1T& a1, a2T& a2, a3T& a3, a4T& a4, a5T& a5) const {
317  MADNESS_ASSERT(ptr_);
318  ((*ptr_).*memfn_)(a1, a2, a3, a4, a5);
319  }
320 
321  template <typename a1T, typename a2T, typename a3T, typename a4T,
322  typename a5T, typename a6T>
323  void operator()(a1T& a1, a2T& a2, a3T& a3, a4T& a4, a5T& a5, a6T& a6) const {
324  MADNESS_ASSERT(ptr_);
325  ((*ptr_).*memfn_)(a1, a2, a3, a4, a5, a6);
326  }
327 
328  template <typename a1T, typename a2T, typename a3T, typename a4T,
329  typename a5T, typename a6T, typename a7T>
330  void operator()(a1T& a1, a2T& a2, a3T& a3, a4T& a4, a5T& a5, a6T& a6, a7T& a7) const {
331  MADNESS_ASSERT(ptr_);
332  ((*ptr_).*memfn_)(a1, a2, a3, a4, a5, a6, a7);
333  }
334 
335  template <typename a1T, typename a2T, typename a3T, typename a4T,
336  typename a5T, typename a6T, typename a7T, typename a8T>
337  void operator()(a1T& a1, a2T& a2, a3T& a3, a4T& a4, a5T& a5, a6T& a6, a7T& a7, a8T& a8) const {
338  MADNESS_ASSERT(ptr_);
339  ((*ptr_).*memfn_)(a1, a2, a3, a4, a5, a6, a7, a8);
340  }
341 
342  template <typename a1T, typename a2T, typename a3T, typename a4T,
343  typename a5T, typename a6T, typename a7T, typename a8T, typename a9T>
344  void operator()(a1T& a1, a2T& a2, a3T& a3, a4T& a4, a5T& a5, a6T& a6, a7T& a7, a8T& a8, a9T& a9) const {
345  MADNESS_ASSERT(ptr_);
346  ((*ptr_).*memfn_)(a1, a2, a3, a4, a5, a6, a7, a8, a9);
347  }
348 
349  template <typename Archive>
350  void serialize(const Archive& ar) {
351  ar & ptr_ & memfn_;
352  }
353 
354  }; // class MemFuncWrapper<ptrT, memfnT, void>
355 
356  template <typename objT, typename memfnT>
357  MemFuncWrapper<objT*, memfnT, typename result_of<memfnT>::type>
358  wrap_mem_fn(objT& obj, memfnT memfn) {
359  return MemFuncWrapper<objT*, memfnT,
360  typename memfunc_traits<memfnT>::result_type>(& obj, memfn);
361  }
362 
363  template <typename objT, typename memfnT>
364  MemFuncWrapper<objT*, memfnT, typename result_of<memfnT>::type>
365  wrap_mem_fn(objT* obj, memfnT memfn) {
366  return MemFuncWrapper<objT*, memfnT,
367  typename memfunc_traits<memfnT>::result_type>(obj, memfn);
368  }
369 
370  template <typename objT, typename memfnT>
371  MemFuncWrapper<std::shared_ptr<objT>, memfnT, typename result_of<memfnT>::type>
372  wrap_mem_fn(const std::shared_ptr<objT>& obj, memfnT memfn) {
374  typename memfunc_traits<memfnT>::result_type>(obj, memfn);
375  }
376 
377  template <typename objT, typename memfnT>
378  MemFuncWrapper<std::shared_ptr<objT>, memfnT, typename result_of<memfnT>::type>
379  wrap_mem_fn(std::shared_ptr<objT>& obj, memfnT memfn) {
381  typename memfunc_traits<memfnT>::result_type>(obj, memfn);
382  }
383 
384  template <typename ptrT, typename memfnT, typename resT>
386  return wrapper.memfn_;
387  }
388 
389  } // namespace detail
390 
391 
393  class WorldTaskQueue : public CallbackInterface, private NO_DEFAULTS {
394  friend class TaskInterface;
395  private:
396  World& world;
397  const ProcessID me;
398  AtomicInt nregistered;
399 
400  void notify() { nregistered--; }
401 
402  // Used in reduce kernel
403  template <typename resultT, typename opT>
404  static resultT sum(const resultT& left, const resultT& right, const opT& op) {
405  //std::cout << " REDUCE SUM " << left << " " << right << std::endl;
406  return op(left,right);
407  }
408 
409  template <typename taskT>
410  static void spawn_remote_task_handler(const AmArg& arg) {
411  MADNESS_ASSERT(taskT::arity <= 9u);
412 
413  // Get task info and arguments form active message
414 
415  detail::TaskHandlerInfo<typename taskT::futureT::remote_refT,
416  typename taskT::functionT> info;
417 
418  archive::BufferInputArchive input_arch = arg & info;
419 
420  // Construct task
421  taskT* task = new taskT(typename taskT::futureT(info.ref),
422  info.func, info.attr, input_arch);
423 
424  // Add task to queue
425  arg.get_world()->taskq.add(task);
426  }
427 
428  template <typename T>
429  inline const T& am_arg(const Future<T>& f) {
430  MADNESS_ASSERT(f.probe());
431  return f.get();
432  }
433 
434  template <typename T> inline const T& am_arg(const T& t) { return t; }
435 
436  typedef detail::voidT voidT;
437 
438  template <typename taskT, typename fnT, typename a1T, typename a2T, typename a3T,
439  typename a4T, typename a5T, typename a6T, typename a7T,
440  typename a8T, typename a9T>
441  inline typename taskT::futureT
442  send_task(ProcessID where, fnT fn, const a1T& a1,
443  const a2T& a2, const a3T& a3, const a4T& a4, const a5T& a5,
444  const a6T& a6, const a7T& a7, const a8T& a8, const a9T& a9,
445  const TaskAttributes& attr)
446  {
447  typename taskT::futureT result;
448  typedef detail::TaskHandlerInfo<typename taskT::futureT::remote_refT, typename taskT::functionT> infoT;
449  world.am.send(where, & WorldTaskQueue::template spawn_remote_task_handler<taskT>,
450  new_am_arg(infoT(result.remote_ref(world), fn, attr),
451  a1, a2, a3, a4, a5, a6, a7, a8, a9));
452 
453  return result;
454  }
455 
456 
457  public:
458  WorldTaskQueue(World& world);
459 
461  size_t size() const { return nregistered; }
462 
463 
465 
473  void add(TaskInterface* t) {
474  nregistered++;
475 
476  t->set_info(&world, this); // Stuff info
477 
478  if (t->ndep() == 0) {
479  ThreadPool::add(t); // If no dependencies directly submit
480  } else {
481  // With dependencies must use the callback to avoid race condition
482  t->register_submit_callback();
483  //t->dec();
484  }
485  }
486 
487  template <typename fnT, typename a1T, typename a2T, typename a3T,
488  typename a4T, typename a5T, typename a6T, typename a7T, typename a8T,
489  typename a9T>
493  res(t->result());
494  add(static_cast<TaskInterface*>(t));
495  return res;
496  }
497 
499 
515  template <typename resultT, typename rangeT, typename opT>
516  Future<resultT> reduce(const rangeT& range, const opT& op) {
517  if (range.size() <= range.get_chunksize()) {
518  resultT sum = resultT();
519  for (typename rangeT::iterator it=range.begin(); it != range.end(); ++it) sum = op(sum,op(it));
520  return Future<resultT>(sum);
521  } else {
522  rangeT left = range;
523  rangeT right(left,Split());
524 
525  Future<resultT> leftsum = add(*this, &WorldTaskQueue::reduce<resultT,rangeT,opT>, left, op);
526  Future<resultT> rightsum = add(*this, &WorldTaskQueue::reduce<resultT,rangeT,opT>, right, op);
527  return add(&WorldTaskQueue::sum<resultT,opT>, leftsum, rightsum, op);
528  }
529  }
530 
532 
555  template <typename rangeT, typename opT>
556  Future<bool> for_each(const rangeT& range, const opT& op) {
557  detail::ForEachRootTask<rangeT, opT>* for_each_root =
558  new detail::ForEachRootTask<rangeT, opT>(world, range, op);
559  Future<bool> result = for_each_root->result();
560  add(for_each_root);
561  return result;
562  }
563 
564 
566 
575  template <typename fnT>
577  add(fnT fn, const TaskAttributes& attr=TaskAttributes()) {
578  typedef TaskFn<fnT> taskT;
579  return add(new taskT(typename taskT::futureT(),
580  fn, attr));
581  }
582 
583  template <typename fnT, typename a1T>
585  add(fnT fn, const a1T& a1, const TaskAttributes& attr=TaskAttributes()) {
586  typedef TaskFn<fnT, a1T> taskT;
587  return add(new taskT(typename taskT::futureT(),
588  fn, a1, attr));
589  }
590 
591  template <typename fnT, typename a1T, typename a2T>
593  add(fnT fn, const a1T& a1, const a2T& a2, const TaskAttributes& attr=TaskAttributes()) {
594  typedef TaskFn<fnT, a1T, a2T> taskT;
595  return add(new taskT(typename taskT::futureT(),
596  fn, a1, a2, attr));
597  }
598 
599  template <typename fnT, typename a1T, typename a2T, typename a3T>
601  add(fnT fn, const a1T& a1, const a2T& a2, const a3T& a3,
602  const TaskAttributes& attr=TaskAttributes())
603  {
604  typedef TaskFn<fnT, a1T, a2T, a3T> taskT;
605  return add(new taskT(typename taskT::futureT(),
606  fn, a1, a2, a3, attr));
607  }
608 
609  template <typename fnT, typename a1T, typename a2T, typename a3T, typename a4T>
611  add(fnT fn, const a1T& a1, const a2T& a2, const a3T& a3, const a4T& a4,
612  const TaskAttributes& attr=TaskAttributes())
613  {
614  typedef TaskFn<fnT, a1T, a2T, a3T, a4T> taskT;
615  return add(new taskT(typename taskT::futureT(),
616  fn, a1, a2, a3, a4, attr));
617  }
618 
619  template <typename fnT, typename a1T, typename a2T, typename a3T, typename a4T,
620  typename a5T>
622  add(fnT fn, const a1T& a1, const a2T& a2, const a3T& a3, const a4T& a4,
623  const a5T& a5, const TaskAttributes& attr=TaskAttributes())
624  {
626  return add(new taskT(typename taskT::futureT(),
627  fn, a1, a2, a3, a4, a5, attr));
628  }
629 
630  template <typename fnT, typename a1T, typename a2T, typename a3T, typename a4T,
631  typename a5T, typename a6T>
633  add(fnT fn, const a1T& a1, const a2T& a2, const a3T& a3, const a4T& a4,
634  const a5T& a5, const a6T& a6, const TaskAttributes& attr=TaskAttributes())
635  {
637  return add(new taskT(typename taskT::futureT(),
638  fn, a1, a2, a3, a4, a5, a6, attr));
639  }
640 
641  template <typename fnT, typename a1T, typename a2T, typename a3T, typename a4T,
642  typename a5T, typename a6T, typename a7T>
644  add(fnT fn, const a1T& a1, const a2T& a2, const a3T& a3, const a4T& a4,
645  const a5T& a5, const a6T& a6, const a7T& a7,
646  const TaskAttributes& attr=TaskAttributes())
647  {
649  return add(new taskT(typename taskT::futureT(),
650  fn, a1, a2, a3, a4, a5, a6, a7, attr));
651  }
652 
653  template <typename fnT, typename a1T, typename a2T, typename a3T, typename a4T,
654  typename a5T, typename a6T, typename a7T, typename a8T>
656  add(fnT fn, const a1T& a1, const a2T& a2, const a3T& a3, const a4T& a4,
657  const a5T& a5, const a6T& a6, const a7T& a7, const a8T& a8,
658  const TaskAttributes& attr=TaskAttributes())
659  {
661  return add(new taskT(typename taskT::futureT(),
662  fn, a1, a2, a3, a4, a5, a6, a7, a8, attr));
663  }
664 
665 
666  template <typename fnT, typename a1T, typename a2T, typename a3T, typename a4T,
667  typename a5T, typename a6T, typename a7T, typename a8T, typename a9T>
669  add(fnT fn, const a1T& a1, const a2T& a2, const a3T& a3, const a4T& a4,
670  const a5T& a5, const a6T& a6, const a7T& a7, const a8T& a8, const a9T& a9,
671  const TaskAttributes& attr=TaskAttributes())
672  {
674  return add(new taskT(typename taskT::futureT(),
675  fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, attr));
676  }
677 
678 
680 
700  template <typename fnT>
702  add(ProcessID dest, fnT fn, const TaskAttributes& attr=TaskAttributes())
703  {
704  typedef TaskFn<fnT> taskT;
705  if(dest == me)
706  return add(fn, attr);
707  else
708  return send_task<taskT>(dest, fn, voidT::value, voidT::value,
711  }
712 
714 
728 
737  template <typename fnT, typename a1T>
739  add(ProcessID dest, fnT fn, const a1T& a1, const TaskAttributes& attr=TaskAttributes())
740  {
741  typedef TaskFn<fnT, a1T> taskT;
742  if(dest == me)
743  return add(fn, a1, attr);
744  else
745  return send_task<taskT>(dest, fn, am_arg(a1), voidT::value,
748  }
749 
751 
754  template <typename fnT, typename a1T, typename a2T>
756  add(ProcessID dest, fnT fn, const a1T& a1, const a2T& a2,
757  const TaskAttributes& attr=TaskAttributes())
758  {
759  typedef TaskFn<fnT, a1T, a2T> taskT;
760  if(dest == me)
761  return add(fn, a1, a2, attr);
762  else
763  return send_task<taskT>(dest, fn, am_arg(a1), am_arg(a2),
766  }
767 
769 
772  template <typename fnT, typename a1T, typename a2T, typename a3T>
774  add(ProcessID dest, fnT fn, const a1T& a1, const a2T& a2, const a3T& a3,
775  const TaskAttributes& attr=TaskAttributes())
776  {
777  typedef TaskFn<fnT, a1T, a2T, a3T> taskT;
778  if(dest == me)
779  return add(fn, a1, a2, a3, attr);
780  else
781  return send_task<taskT>(dest, fn, am_arg(a1), am_arg(a2),
782  am_arg(a3), voidT::value, voidT::value, voidT::value,
784  }
785 
787 
790  template <typename fnT, typename a1T, typename a2T, typename a3T,
791  typename a4T>
793  add(ProcessID dest, fnT fn, const a1T& a1, const a2T& a2, const a3T& a3,
794  const a4T& a4, const TaskAttributes& attr=TaskAttributes())
795  {
796  typedef TaskFn<fnT, a1T, a2T, a3T, a4T> taskT;
797  if(dest == me)
798  return add(fn, a1, a2, a3, a4, attr);
799  else
800  return send_task<taskT>(dest, fn, am_arg(a1), am_arg(a2),
801  am_arg(a3), am_arg(a4), voidT::value, voidT::value,
803  }
804 
806 
809  template <typename fnT, typename a1T, typename a2T, typename a3T,
810  typename a4T, typename a5T>
812  add(ProcessID dest, fnT fn, const a1T& a1, const a2T& a2, const a3T& a3,
813  const a4T& a4, const a5T& a5, const TaskAttributes& attr=TaskAttributes())
814  {
816  if(dest == me)
817  return add(fn, a1, a2, a3, a4, a5, attr);
818  else
819  return send_task<taskT>(dest, fn, am_arg(a1), am_arg(a2),
820  am_arg(a3), am_arg(a4), am_arg(a5), voidT::value,
822  }
823 
825 
828  template <typename fnT, typename a1T, typename a2T, typename a3T,
829  typename a4T, typename a5T, typename a6T>
831  add(ProcessID dest, fnT fn, const a1T& a1, const a2T& a2, const a3T& a3,
832  const a4T& a4, const a5T& a5, const a6T& a6,
833  const TaskAttributes& attr=TaskAttributes())
834  {
836  if(dest == me)
837  return add(fn, a1, a2, a3, a4, a5, a6, attr);
838  else
839  return send_task<taskT>(dest, fn, am_arg(a1), am_arg(a2),
840  am_arg(a3), am_arg(a4), am_arg(a5), am_arg(a6),
842  }
843 
845 
848  template <typename fnT, typename a1T, typename a2T, typename a3T,
849  typename a4T, typename a5T, typename a6T, typename a7T>
851  add(ProcessID dest, fnT fn, const a1T& a1, const a2T& a2, const a3T& a3,
852  const a4T& a4, const a5T& a5, const a6T& a6, const a7T& a7,
853  const TaskAttributes& attr=TaskAttributes())
854  {
856  if(dest == me)
857  return add(fn, a1, a2, a3, a4, a5, a6, a7, attr);
858  else
859  return send_task<taskT>(dest, fn, am_arg(a1), am_arg(a2),
860  am_arg(a3), am_arg(a4), am_arg(a5), am_arg(a6),
861  am_arg(a7), voidT::value, voidT::value, attr);
862  }
863 
865 
868  template <typename fnT, typename a1T, typename a2T, typename a3T,
869  typename a4T, typename a5T, typename a6T, typename a7T,
870  typename a8T>
872  add(ProcessID dest, fnT fn, const a1T& a1, const a2T& a2, const a3T& a3,
873  const a4T& a4, const a5T& a5, const a6T& a6, const a7T& a7,
874  const a8T& a8, const TaskAttributes& attr=TaskAttributes())
875  {
877  if(dest == me)
878  return add(fn, a1, a2, a3, a4, a5, a6, a7, a8, attr);
879  else
880  return send_task<taskT>(dest, fn, am_arg(a1), am_arg(a2),
881  am_arg(a3), am_arg(a4), am_arg(a5), am_arg(a6),
882  am_arg(a7), am_arg(a8), voidT::value, attr);
883  }
884 
886 
889  template <typename fnT, typename a1T, typename a2T, typename a3T,
890  typename a4T, typename a5T, typename a6T, typename a7T,
891  typename a8T, typename a9T>
893  add(ProcessID dest, fnT fn, const a1T& a1, const a2T& a2, const a3T& a3,
894  const a4T& a4, const a5T& a5, const a6T& a6, const a7T& a7,
895  const a8T& a8, const a9T& a9, const TaskAttributes& attr=TaskAttributes())
896  {
898  if(dest == me)
899  return add(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, attr);
900  else
901  return send_task<taskT>(dest, fn, am_arg(a1), am_arg(a2),
902  am_arg(a3), am_arg(a4), am_arg(a5), am_arg(a6),
903  am_arg(a7), am_arg(a8), am_arg(a9), attr);
904  }
905 
907  template <typename objT, typename memfnT>
909  add(objT& obj, memfnT memfun, const TaskAttributes& attr=TaskAttributes())
910  { return add(detail::wrap_mem_fn(obj,memfun),attr); }
911 
913  template <typename objT, typename memfnT, typename a1T>
915  add(objT& obj, memfnT memfun, const a1T& a1,
916  const TaskAttributes& attr=TaskAttributes())
917  { return add(detail::wrap_mem_fn(obj,memfun),a1,attr); }
918 
920  template <typename objT, typename memfnT, typename a1T, typename a2T>
922  add(objT& obj, memfnT memfun, const a1T& a1, const a2T& a2,
923  const TaskAttributes& attr=TaskAttributes())
924  { return add(detail::wrap_mem_fn(obj,memfun),a1,a2,attr); }
925 
927  template <typename objT, typename memfnT, typename a1T, typename a2T,
928  typename a3T>
930  add(objT& obj, memfnT memfun, const a1T& a1, const a2T& a2, const a3T& a3,
931  const TaskAttributes& attr=TaskAttributes())
932  { return add(detail::wrap_mem_fn(obj,memfun),a1,a2,a3,attr); }
933 
935  template <typename objT, typename memfnT, typename a1T, typename a2T,
936  typename a3T, typename a4T>
938  add(objT& obj, memfnT memfun, const a1T& a1, const a2T& a2, const a3T& a3,
939  const a4T& a4, const TaskAttributes& attr=TaskAttributes())
940  { return add(detail::wrap_mem_fn(obj,memfun),a1,a2,a3,a4,attr); }
941 
943  template <typename objT, typename memfnT, typename a1T, typename a2T,
944  typename a3T, typename a4T, typename a5T>
946  add(objT& obj, memfnT memfun, const a1T& a1, const a2T& a2, const a3T& a3,
947  const a4T& a4, const a5T& a5, const TaskAttributes attr=TaskAttributes())
948  { return add(detail::wrap_mem_fn(obj,memfun),a1,a2,a3,a4,a5,attr); }
949 
951  template <typename objT, typename memfnT, typename a1T, typename a2T,
952  typename a3T, typename a4T, typename a5T, typename a6T>
954  add(objT& obj, memfnT memfun, const a1T& a1, const a2T& a2, const a3T& a3,
955  const a4T& a4, const a5T& a5, const a6T& a6,
956  const TaskAttributes& attr=TaskAttributes())
957  { return add(detail::wrap_mem_fn(obj,memfun),a1,a2,a3,a4,a5,a6,attr); }
958 
960  template <typename objT, typename memfnT, typename a1T, typename a2T,
961  typename a3T, typename a4T, typename a5T, typename a6T, typename a7T>
963  add(objT& obj, memfnT memfun, const a1T& a1, const a2T& a2, const a3T& a3,
964  const a4T& a4, const a5T& a5, const a6T& a6, const a7T& a7,
965  const TaskAttributes& attr=TaskAttributes())
966  { return add(detail::wrap_mem_fn(obj,memfun),a1,a2,a3,a4,a5,a6,a7,attr); }
967 
969  template <typename objT, typename memfnT, typename a1T, typename a2T,
970  typename a3T, typename a4T, typename a5T, typename a6T, typename a7T,
971  typename a8T>
973  add(objT& obj, memfnT memfun, const a1T& a1, const a2T& a2, const a3T& a3,
974  const a4T& a4, const a5T& a5, const a6T& a6, const a7T& a7,
975  const a8T& a8, const TaskAttributes attr=TaskAttributes())
976  { return add(detail::wrap_mem_fn(obj,memfun),a1,a2,a3,a4,a5,a6,a7,a8,attr); }
977 
979  template <typename objT, typename memfnT, typename a1T, typename a2T,
980  typename a3T, typename a4T, typename a5T, typename a6T, typename a7T,
981  typename a8T, typename a9T>
983  add(objT& obj, memfnT memfun, const a1T& a1, const a2T& a2, const a3T& a3,
984  const a4T& a4, const a5T& a5, const a6T& a6, const a7T& a7,
985  const a8T& a8, const a9T& a9, const TaskAttributes attr=TaskAttributes())
986  { return add(detail::wrap_mem_fn(obj,memfun),a1,a2,a3,a4,a5,a6,a7,a8,a9,attr); }
987 
989  template <typename objT, typename memfnT>
991  add(objT* obj, memfnT memfun, const TaskAttributes& attr=TaskAttributes())
992  { return add(detail::wrap_mem_fn(obj,memfun),attr); }
993 
995  template <typename objT, typename memfnT, typename a1T>
997  add(objT* obj, memfnT memfun, const a1T& a1,
998  const TaskAttributes& attr=TaskAttributes())
999  { return add(detail::wrap_mem_fn(obj,memfun),a1,attr); }
1000 
1002  template <typename objT, typename memfnT, typename a1T, typename a2T>
1004  add(objT* obj, memfnT memfun, const a1T& a1, const a2T& a2,
1005  const TaskAttributes& attr=TaskAttributes())
1006  { return add(detail::wrap_mem_fn(obj,memfun),a1,a2,attr); }
1007 
1009  template <typename objT, typename memfnT, typename a1T, typename a2T,
1010  typename a3T>
1012  add(objT* obj, memfnT memfun, const a1T& a1, const a2T& a2, const a3T& a3,
1013  const TaskAttributes& attr=TaskAttributes())
1014  { return add(detail::wrap_mem_fn(obj,memfun),a1,a2,a3,attr); }
1015 
1017  template <typename objT, typename memfnT, typename a1T, typename a2T,
1018  typename a3T, typename a4T>
1020  add(objT* obj, memfnT memfun, const a1T& a1, const a2T& a2, const a3T& a3,
1021  const a4T& a4, const TaskAttributes& attr=TaskAttributes())
1022  { return add(detail::wrap_mem_fn(obj,memfun),a1,a2,a3,a4,attr); }
1023 
1024 
1026  template <typename objT, typename memfnT, typename a1T, typename a2T,
1027  typename a3T, typename a4T, typename a5T>
1029  add(objT* obj, memfnT memfun, const a1T& a1, const a2T& a2, const a3T& a3,
1030  const a4T& a4, const a5T& a5, const TaskAttributes attr=TaskAttributes())
1031  { return add(detail::wrap_mem_fn(obj,memfun),a1,a2,a3,a4,a5,attr); }
1032 
1033 
1035  template <typename objT, typename memfnT, typename a1T, typename a2T,
1036  typename a3T, typename a4T, typename a5T, typename a6T>
1038  add(objT* obj, memfnT memfun, const a1T& a1, const a2T& a2, const a3T& a3,
1039  const a4T& a4, const a5T& a5, const a6T& a6,
1040  const TaskAttributes& attr=TaskAttributes())
1041  { return add(detail::wrap_mem_fn(obj,memfun),a1,a2,a3,a4,a5,a6,attr); }
1042 
1044  template <typename objT, typename memfnT, typename a1T, typename a2T,
1045  typename a3T, typename a4T, typename a5T, typename a6T, typename a7T>
1047  add(objT* obj, memfnT memfun, const a1T& a1, const a2T& a2, const a3T& a3,
1048  const a4T& a4, const a5T& a5, const a6T& a6, const a7T& a7,
1049  const TaskAttributes& attr=TaskAttributes())
1050  { return add(detail::wrap_mem_fn(obj,memfun),a1,a2,a3,a4,a5,a6,a7,attr); }
1051 
1053  template <typename objT, typename memfnT, typename a1T, typename a2T,
1054  typename a3T, typename a4T, typename a5T, typename a6T, typename a7T,
1055  typename a8T>
1057  add(objT* obj, memfnT memfun, const a1T& a1, const a2T& a2, const a3T& a3,
1058  const a4T& a4, const a5T& a5, const a6T& a6, const a7T& a7,
1059  const a8T& a8, const TaskAttributes attr=TaskAttributes())
1060  { return add(detail::wrap_mem_fn(obj,memfun),a1,a2,a3,a4,a5,a6,a7,a8,attr); }
1061 
1063  template <typename objT, typename memfnT, typename a1T, typename a2T,
1064  typename a3T, typename a4T, typename a5T, typename a6T, typename a7T,
1065  typename a8T, typename a9T>
1067  add(objT* obj, memfnT memfun, const a1T& a1, const a2T& a2,
1068  const a3T& a3, const a4T& a4, const a5T& a5, const a6T& a6,
1069  const a7T& a7, const a8T& a8, const a9T& a9,
1070  const TaskAttributes attr=TaskAttributes())
1071  { return add(detail::wrap_mem_fn(obj,memfun),a1,a2,a3,a4,a5,a6,a7,a8,a9,attr); }
1072 
1073  private:
1074 
1075  struct ProbeAllDone {
1076  WorldTaskQueue* tq;
1077  ProbeAllDone(WorldTaskQueue* tq) : tq(tq) {}
1078  bool operator()() const { return (tq->nregistered == 0); }
1079  };
1080 
1081  public:
1082 
1084 
1086  void fence() {
1087  try {
1088  ThreadPool::await(ProbeAllDone(this), true);
1089  } catch(...) {
1090  printf("!!MADNESS ERROR: Exception thrown in WorldTaskQueue::fence() with %i pending task(s)\n", int(nregistered));
1091  throw;
1092  }
1093  }
1094  };
1095 
1096  namespace detail {
1097 
1099 
1105  template <typename rangeT, typename opT>
1106  class ForEachTask : public TaskInterface {
1107  private:
1108  rangeT range_;
1109  opT op_;
1111 
1112  // not allowed
1114  ForEachTask& operator=(const ForEachTask<rangeT, opT>&);
1115 
1116  public:
1117 
1119  ForEachTask(const rangeT range, const opT& op, ForEachRootTask<rangeT, opT>& root) :
1120  TaskInterface(0, TaskAttributes::hipri()), range_(range), op_(op), root_(root)
1121  {
1122  // Increment the master task dependency counter for this task
1123  root_.inc();
1124  }
1125 
1127  virtual ~ForEachTask() { }
1128 
1130  virtual void run(const TaskThreadEnv&) {
1131  // Spawn leaf tasks and split range until it is less than chuncksize
1132  while(range_.size() > range_.get_chunksize()) {
1133  rangeT right(range_,Split());
1134  ForEachTask<rangeT,opT>* leaf = new ForEachTask<rangeT,opT>(right, op_, root_);
1135  root_.world().taskq.add(leaf);
1136  }
1137 
1138  // Iterate over the remaining chunck of range and call op_ for each element
1139  int status = 0;
1140  for(typename rangeT::iterator it = range_.begin(); it != range_.end(); ++it)
1141  if(op_(it))
1142  ++status;
1143 
1144  // Notify the root task that this task is done give the status
1145  root_.complete(status);
1146  }
1147 
1148  private:
1150 
1152  virtual void get_id(std::pair<void*,unsigned short>& id) const {
1153  PoolTaskInterface::make_id(id, *this);
1154  }
1155  }; // class ForEachTask
1156 
1157 
1159 
1165  template <typename rangeT, typename opT>
1166  class ForEachRootTask : public TaskInterface {
1167  private:
1168  World& world_;
1169  AtomicInt status_;
1170  Future<bool> completion_status_;
1171 
1172  public:
1173 
1175 
1179  ForEachRootTask(World& world, const rangeT range, const opT& op) :
1180  TaskInterface(0, TaskAttributes::hipri()), world_(world)
1181  {
1182  status_ = - (range.size());
1183  // Create the first for each task.
1184  world_.taskq.add(new ForEachTask<rangeT,opT>(range, op, *this));
1185  }
1186 
1188  virtual ~ForEachRootTask() { }
1189 
1191 
1193  World& world() const { return world_; }
1194 
1196 
1198  const Future<bool>& result() const { return completion_status_; }
1199 
1201 
1203  void complete(const int status) {
1204  status_ += status;
1206  }
1207 
1209 
1211  virtual void run(const TaskThreadEnv&) { completion_status_.set(status_ == 0); }
1212 
1213  private:
1215 
1217  virtual void get_id(std::pair<void*,unsigned short>& id) const {
1218  PoolTaskInterface::make_id(id, *this);
1219  }
1220  }; // class ForEachRootTask
1221 
1222 
1223  } // namespace detail
1224 
1225 }
1226 
1227 
1228 #endif // MADNESS_WORLD_WORLDTASK_H__INCLUDED
detail::function_enabler< fnT >::type add(ProcessID dest, fnT fn, const TaskAttributes &attr=TaskAttributes())
Spawn a remote task.
Definition: worldtask.h:702
MemFuncWrapper(const MemFuncWrapper_ &other)
Definition: worldtask.h:168
detail::memfunc_enabler< memfnT >::type add(objT &obj, memfnT memfun, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const TaskAttributes attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T,a2T,a3,a4,a5)" as a local task.
Definition: worldtask.h:946
Definition: shared_ptr_bits.h:38
resT operator()(a1T &a1, a2T &a2, a3T &a3, a4T &a4) const
Definition: worldtask.h:206
MemFuncWrapper_ & operator=(const MemFuncWrapper_ &other)
Definition: worldtask.h:176
void operator()(a1T &a1, a2T &a2, a3T &a3, a4T &a4, a5T &a5, a6T &a6, a7T &a7, a8T &a8) const
Definition: worldtask.h:337
detail::memfunc_enabler< memfnT >::type add(objT *obj, memfnT memfun, const a1T &a1, const a2T &a2, const a3T &a3, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T,a2T,a3)" as a local task.
Definition: worldtask.h:1012
void operator()(a1T &a1, a2T &a2, a3T &a3, a4T &a4, a5T &a5, a6T &a6, a7T &a7) const
Definition: worldtask.h:330
static TaskAttributes hipri()
Definition: worldthread.h:277
void inc()
Increment the number of dependencies.
Definition: worlddep.h:108
detail::memfunc_enabler< memfnT >::type add(objT &obj, memfnT memfun, const a1T &a1, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T)" as a local task.
Definition: worldtask.h:915
detail::function_enabler< fnT >::type add(fnT fn, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const a6T &a6, const a7T &a7, const a8T &a8, const TaskAttributes &attr=TaskAttributes())
Definition: worldtask.h:656
Dummy class a la Intel TBB used to distinguish splitting constructor.
Definition: worldrange.h:45
resT operator()(a1T &a1, a2T &a2, a3T &a3, a4T &a4, a5T &a5) const
Definition: worldtask.h:213
Apply an operation to a range of iterators.
Definition: worldtask.h:65
MemFuncWrapper()
Definition: worldtask.h:166
void notify()
Invoked by callbacks to notifiy of dependencies being satisfied.
Definition: worlddep.h:91
detail::function_enabler< fnT >::type add(fnT fn, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const TaskAttributes &attr=TaskAttributes())
Definition: worldtask.h:622
resT operator()(a1T &a1) const
Definition: worldtask.h:188
void operator()(a1T &a1, a2T &a2, a3T &a3, a4T &a4, a5T &a5, a6T &a6) const
Definition: worldtask.h:323
detail::function_enabler< fnT >::type add(fnT fn, const a1T &a1, const a2T &a2, const TaskAttributes &attr=TaskAttributes())
Definition: worldtask.h:593
detail::memfunc_enabler< memfnT >::type add(objT &obj, memfnT memfun, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const a6T &a6, const a7T &a7, const a8T &a8, const TaskAttributes attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T,a2T,a3,a4,a5,a6,a7,a8)" as a local task.
Definition: worldtask.h:973
void serialize(const Archive &ar)
Definition: worldtask.h:247
Definition: worldtask.h:144
resT operator()(a1T &a1, a2T &a2, a3T &a3, a4T &a4, a5T &a5, a6T &a6, a7T &a7, a8T &a8, a9T &a9) const
Definition: worldtask.h:241
detail::function_enabler< fnT >::type add(ProcessID dest, fnT fn, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const a6T &a6, const a7T &a7, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (*fn)(a1T,a2T,a3T,a4T,a5T,a6T,a7T)" as a task, local or remote.
Definition: worldtask.h:851
detail::function_enabler< fnT >::type add(fnT fn, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const a6T &a6, const TaskAttributes &attr=TaskAttributes())
Definition: worldtask.h:633
detail::memfunc_enabler< memfnT >::type add(objT &obj, memfnT memfun, const a1T &a1, const a2T &a2, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T,a2T)" as a local task.
Definition: worldtask.h:922
Future< bool > for_each(const rangeT &range, const opT &op)
Apply op(item) for all items in range.
Definition: worldtask.h:556
detail::memfunc_enabler< memfnT >::type add(objT *obj, memfnT memfun, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (obj.*memfun)()" as a local task.
Definition: worldtask.h:991
AmArg * new_am_arg(const A &a, const B &b, const C &c, const D &d, const E &e, const F &f, const G &g, const H &h, const I &i, const J &j)
Convenience template for serializing arguments into a new AmArg.
Definition: worldam.h:185
resT operator()(a1T &a1, a2T &a2) const
Definition: worldtask.h:194
Used to pass info about thread environment into users task.
Definition: worldthread.h:289
void serialize(const Archive &ar)
Serialization of object.
Definition: worldtask.h:91
resT operator()(a1T &a1, a2T &a2, a3T &a3) const
Definition: worldtask.h:200
This class used for callbacks (e.g., for dependency tracking)
Definition: worlddep.h:51
resT operator()(a1T &a1, a2T &a2, a3T &a3, a4T &a4, a5T &a5, a6T &a6, a7T &a7, a8T &a8) const
Definition: worldtask.h:234
detail::memfunc_enabler< memfnT >::type add(objT *obj, memfnT memfun, const a1T &a1, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T)" as a local task.
Definition: worldtask.h:997
detail::function_enabler< fnT >::type add(fnT fn, const a1T &a1, const TaskAttributes &attr=TaskAttributes())
Definition: worldtask.h:585
MemFuncWrapper< ptrT, memfnT, void > MemFuncWrapper_
Definition: worldtask.h:162
detail::memfunc_enabler< memfnT >::type add(objT &obj, memfnT memfun, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const a6T &a6, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T,a2T,a3,a4,a5,a6)" as a local task.
Definition: worldtask.h:954
Definition: mpreal.h:3066
functionT func
A task function.
Definition: worldtask.h:74
detail::memfunc_enabler< memfnT >::type add(objT &obj, memfnT memfun, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const a6T &a6, const a7T &a7, const a8T &a8, const a9T &a9, const TaskAttributes attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T,a2T,a3,a4,a5,a6,a7,a8,a9)" as a local task.
Definition: worldtask.h:983
Implement Range class for parallel iteration.
NDIM & f
Definition: mra.h:2179
memfnT memfn_type
Definition: worldtask.h:267
POD holding excitation energy and response vector for a single excitation.
Definition: tdhf_CIS.h:134
static void await(const Probe &probe, bool dowork=true)
Gracefully wait for a condition to become true ... executes tasks if any in queue.
Definition: worldthread.h:1056
MemFuncWrapper(ptrT ptr, memfnT memfn)
Definition: worldtask.h:172
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:2429
Multi-threaded queue to manage and run tasks.
Definition: worldtask.h:393
virtual void run(const TaskThreadEnv &)
Task run work.
Definition: worldtask.h:1211
const Future< bool > & result() const
Result accessor.
Definition: worldtask.h:1198
Definition: worldtask.h:59
WorldAmInterface & am
AM interface.
Definition: worldfwd.h:460
detail::function_enabler< fnT >::type add(fnT fn, const a1T &a1, const a2T &a2, const a3T &a3, const TaskAttributes &attr=TaskAttributes())
Definition: worldtask.h:601
virtual void run(const TaskThreadEnv &)
Run the task.
Definition: worldtask.h:1130
detail::function_enabler< fnT >::type add(fnT fn, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const a6T &a6, const a7T &a7, const a8T &a8, const a9T &a9, const TaskAttributes &attr=TaskAttributes())
Definition: worldtask.h:669
size_t size() const
Returns the number of pending tasks.
Definition: worldtask.h:461
detail::function_enabler< fnT >::type add(ProcessID dest, fnT fn, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (*fn)(a1T,a2T,a3T,a4T)" as a task, local or remote.
Definition: worldtask.h:793
TaskFn< fnT, a1T, a2T, a3T, a4T, a5T, a6T, a7T, a8T, a9T >::futureT add(TaskFn< fnT, a1T, a2T, a3T, a4T, a5T, a6T, a7T, a8T, a9T > *t)
Definition: worldtask.h:491
MemFuncWrapper< ptrT, memfnT, void > MemFuncWrapper_
Definition: worldtask.h:265
Definition: worldtask.h:58
const double a2
Definition: vnucso.cc:91
memfnT memfn_type
Definition: worldtask.h:164
const T1 &f1 return GTEST_2_TUPLE_() T(f0, f1)
resT operator()(a1T &a1, a2T &a2, a3T &a3, a4T &a4, a5T &a5, a6T &a6, a7T &a7) const
Definition: worldtask.h:227
detail::memfunc_enabler< memfnT >::type add(objT *obj, memfnT memfun, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T,a2T,a3,a4)" as a local task.
Definition: worldtask.h:1020
void operator()(a1T &a1, a2T &a2, a3T &a3) const
Definition: worldtask.h:303
Definition: worldtask.h:130
Apply an operation to a range of iterators.
Definition: worldtask.h:1106
int ndep() const
Returns the number of unsatisfied dependencies.
Definition: worlddep.h:85
detail::memfunc_enabler< memfnT >::type add(objT &obj, memfnT memfun, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const a6T &a6, const a7T &a7, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T,a2T,a3,a4,a5,a6,a7)" as a local task.
Definition: worldtask.h:963
static void add(PoolTaskInterface *task)
Add a new task to the pool.
Definition: worldthread.h:984
resT result_type
Definition: worldtask.h:163
detail::memfunc_enabler< memfnT >::type add(objT *obj, memfnT memfun, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const TaskAttributes attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T,a2T,a3,a4,a5)" as a local task.
Definition: worldtask.h:1029
detail::memfunc_enabler< memfnT >::type add(objT *obj, memfnT memfun, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const a6T &a6, const a7T &a7, const a8T &a8, const TaskAttributes attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T,a2T,a3,a4,a5,a6,a7,a8)" as a local task.
Definition: worldtask.h:1057
detail::memfunc_enabler< memfnT >::type add(objT *obj, memfnT memfun, const a1T &a1, const a2T &a2, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T,a2T)" as a local task.
Definition: worldtask.h:1004
void operator()(a1T &a1, a2T &a2, a3T &a3, a4T &a4, a5T &a5, a6T &a6, a7T &a7, a8T &a8, a9T &a9) const
Definition: worldtask.h:344
MemFuncWrapper< objT *, memfnT, typename result_of< memfnT >::type > wrap_mem_fn(objT &obj, memfnT memfn)
Definition: worldtask.h:358
Function< double, 3 > functionT
Definition: chem/corepotential.cc:59
detail::function_enabler< fnT >::type add(ProcessID dest, fnT fn, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (*fn)(a1T,a2T,a3T,a4T,a5T)" as a task, local or remote.
Definition: worldtask.h:812
detail::memfunc_enabler< memfnT >::type add(objT &obj, memfnT memfun, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T,a2T,a3,a4)" as a local task.
Definition: worldtask.h:938
Future< void > voidT
Definition: taskfn.h:222
detail::memfunc_enabler< memfnT >::type add(objT &obj, memfnT memfun, const a1T &a1, const a2T &a2, const a3T &a3, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T,a2T,a3)" as a local task.
Definition: worldtask.h:930
refT ref
Remote reference for a task result future.
Definition: worldtask.h:73
detail::function_enabler< fnT >::type add(ProcessID dest, fnT fn, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const a6T &a6, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (*fn)(a1T,a2T,a3T,a4T,a5T,a6T)" as a task, local or remote.
Definition: worldtask.h:831
static T init()
Definition: worldtask.h:145
detail::function_enabler< fnT >::type add(fnT fn, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const TaskAttributes &attr=TaskAttributes())
Definition: worldtask.h:611
An integer with atomic set, get, read+inc, read+dec, dec+test operations.
Definition: atomicint.h:73
RMI::Request send(const ProcessID dest, am_handlerT op, const AmArg *arg, const int attr=RMI::ATTR_ORDERED, const bool managed=true)
Sends an unmanaged non-blocking active message.
Definition: worldam.h:386
A parallel world with full functionality wrapping an MPI communicator.
Definition: worldfwd.h:416
lazy_enable_if from Boost for conditionally instantiating templates based on type ...
Definition: enable_if.h:102
MemFuncWrapper_ & operator=(const MemFuncWrapper_ &other)
Definition: worldtask.h:279
resT operator()() const
Definition: worldtask.h:182
World & world() const
World accessor.
Definition: worldtask.h:1193
detail::function_enabler< fnT >::type add(ProcessID dest, fnT fn, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const a6T &a6, const a7T &a7, const a8T &a8, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (*fn)(a1T,a2T,a3T,a4T,a5T,a6T,a7T,a8T)" as a task, local or remote.
Definition: worldtask.h:872
TaskHandlerInfo()
Definition: worldtask.h:84
void operator()(a1T &a1) const
Definition: worldtask.h:291
void operator()(a1T &a1, a2T &a2, a3T &a3, a4T &a4) const
Definition: worldtask.h:309
Implements NO_DEFAULTS.
int ProcessID
Used to clearly identify process number/rank.
Definition: worldtypes.h:37
ForEachTask(const rangeT range, const opT &op, ForEachRootTask< rangeT, opT > &root)
Constructor.
Definition: worldtask.h:1119
WorldTaskQueue(World &world)
Definition: worldtask.cc:55
TaskAttributes attr
Task attributes.
Definition: worldtask.h:75
detail::function_enabler< fnT >::type add(ProcessID dest, fnT fn, const a1T &a1, const TaskAttributes &attr=TaskAttributes())
Spawn a remote task.
Definition: worldtask.h:739
detail::memfunc_enabler< memfnT >::type add(objT *obj, memfnT memfun, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const a6T &a6, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T,a2T,a3,a4,a5,a6)" as a local task.
Definition: worldtask.h:1038
Wrap a callable object and its arguments into a task function.
Definition: taskfn.h:388
const mpreal sum(const mpreal tab[], unsigned long int n, mp_rnd_t rnd_mode)
Definition: mpreal.cc:241
void operator()() const
Definition: worldtask.h:285
void fence()
Returns after all local tasks have completed.
Definition: worldtask.h:1086
archive_array< unsigned char > wrap_opaque(const T *, unsigned int)
Factory function to wrap pointer to contiguous data as opaque (uchar) archive_array.
Definition: archive.h:827
void operator()(a1T &a1, a2T &a2) const
Definition: worldtask.h:297
detail::memfunc_enabler< memfnT >::type add(objT *obj, memfnT memfun, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const a6T &a6, const a7T &a7, const a8T &a8, const a9T &a9, const TaskAttributes attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T,a2T,a3,a4,a5,a6,a7,a8,a9)" as a local task.
Definition: worldtask.h:1067
void add(TaskInterface *t)
Add a new local task taking ownership of the pointer.
Definition: worldtask.h:473
detail::function_enabler< fnT >::type add(ProcessID dest, fnT fn, const a1T &a1, const a2T &a2, const a3T &a3, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (*fn)(a1T,a2T,a3T)" as a task, local or remote.
Definition: worldtask.h:774
Definition: worldtask.h:72
Contains attributes of a task.
Definition: worldthread.h:208
const double a1
Definition: vnucso.cc:90
static const Future< void > value
Definition: worldfut.h:602
MemFuncWrapper(const MemFuncWrapper_ &other)
Definition: worldtask.h:271
static enable_if_c< detail::function_traits< fnT >::value||detail::memfunc_traits< fnT >::value >::type make_id(std::pair< void *, unsigned short > &id, fnT fn)
Definition: worldthread.h:680
Definition: taskfn.h:50
detail::function_enabler< fnT >::type add(ProcessID dest, fnT fn, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const a6T &a6, const a7T &a7, const a8T &a8, const a9T &a9, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (*fn)(a1T,a2T,a3T,a4T,a5T,a6T,a7T,a8T,a9T)" as a task, local or remote.
Definition: worldtask.h:893
Future< resultT > reduce(const rangeT &range, const opT &op)
Reduce op(item) for all items in range using op(sum,op(item))
Definition: worldtask.h:516
static std::shared_ptr< T > init()
Definition: worldtask.h:150
void serialize(const Archive &ar)
Definition: worldtask.h:350
A future is a possibly yet unevaluated value.
Definition: ref.h:210
detail::function_enabler< fnT >::type add(fnT fn, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const a6T &a6, const a7T &a7, const TaskAttributes &attr=TaskAttributes())
Definition: worldtask.h:644
void operator()(a1T &a1, a2T &a2, a3T &a3, a4T &a4, a5T &a5) const
Definition: worldtask.h:316
Tensor< double > op(const Tensor< double > &x)
Definition: kain.cc:508
Wrappers around platform dependent timers and performance info.
friend memfnT get_mem_func_ptr(const MemFuncWrapper_ &wrapper)
Definition: worldtask.h:251
is_function::value is true if T is a function pointer
Definition: type_traits_bits.h:85
detail::function_enabler< fnT >::type add(fnT fn, const TaskAttributes &attr=TaskAttributes())
Spawn a local task.
Definition: worldtask.h:577
Definition: worldtask.h:137
All world tasks must be derived from this public interface.
Definition: taskfn.h:68
MemFuncWrapper(ptrT ptr, memfnT memfn)
Definition: worldtask.h:275
lazy_enable_if_c from Boost for conditionally instantiating templates based on type ...
Definition: enable_if.h:87
const futureT & result() const
Definition: taskfn.h:614
enable_if from Boost for conditionally instantiating templates based on type
Definition: enable_if.h:60
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
virtual ~ForEachRootTask()
Virtual destructor.
Definition: worldtask.h:1188
is_member_function_pointer::value is true if T is a member function pointer
Definition: type_traits_bits.h:91
resT operator()(a1T &a1, a2T &a2, a3T &a3, a4T &a4, a5T &a5, a6T &a6) const
Definition: worldtask.h:220
detail::memfunc_enabler< memfnT >::type add(objT &obj, memfnT memfun, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (obj.*memfun)()" as a local task.
Definition: worldtask.h:909
void complete(const int status)
Called by child tasks when they are complete.
Definition: worldtask.h:1203
detail::memfunc_enabler< memfnT >::type add(objT *obj, memfnT memfun, const a1T &a1, const a2T &a2, const a3T &a3, const a4T &a4, const a5T &a5, const a6T &a6, const a7T &a7, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (obj.*memfun)(a1T,a2T,a3,a4,a5,a6,a7)" as a local task.
Definition: worldtask.h:1047
detail::function_enabler< fnT >::type add(ProcessID dest, fnT fn, const a1T &a1, const a2T &a2, const TaskAttributes &attr=TaskAttributes())
Invoke "resultT (*fn)(a1T,a2T)" as a task, local or remote.
Definition: worldtask.h:756
memfnT get_mem_func_ptr(const MemFuncWrapper< ptrT, memfnT, resT > &)
Definition: worldtask.h:385
TaskHandlerInfo(const refT &ref, functionT func, const TaskAttributes &attr)
Construct task info object.
Definition: worldtask.h:82
Disables default copy constructor and assignment operators.
Definition: nodefaults.h:49
ForEachRootTask(World &world, const rangeT range, const opT &op)
Constructor.
Definition: worldtask.h:1179
virtual ~ForEachTask()
Virtual destructor.
Definition: worldtask.h:1127