SPH
Scheduler.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "common/ForwardDecl.h"
11 
13 
15 class ITask : public Polymorphic {
16 public:
18  virtual void wait() = 0;
19 
21  virtual bool completed() const = 0;
22 };
23 
27 class IScheduler : public Polymorphic {
28 public:
32  virtual SharedPtr<ITask> submit(const Function<void()>& task) = 0;
33 
38  virtual Optional<Size> getThreadIdx() const = 0;
39 
43  virtual Size getThreadCnt() const = 0;
44 
46  virtual Size getRecommendedGranularity() const = 0;
47 
58  virtual void parallelFor(const Size from,
59  const Size to,
60  const Size granularity,
61  const Function<void(Size n1, Size n2)>& functor);
62 };
63 
68 public:
69  virtual SharedPtr<ITask> submit(const Function<void()>& task) override;
70 
71  virtual Optional<Size> getThreadIdx() const override;
72 
73  virtual Size getThreadCnt() const override;
74 
75  virtual Size getRecommendedGranularity() const override;
76 
78 };
79 
85 
86 
97 template <typename TFunctor>
98 INLINE void parallelFor(IScheduler& scheduler, const Size from, const Size to, TFunctor&& functor) {
99  const Size granularity = scheduler.getRecommendedGranularity();
100  parallelFor(scheduler, from, to, granularity, std::forward<TFunctor>(functor));
101 }
102 
112 template <typename TFunctor>
113 INLINE void parallelFor(IScheduler& scheduler,
114  const Size from,
115  const Size to,
116  const Size granularity,
117  TFunctor&& functor) {
118  scheduler.parallelFor(from, to, granularity, [&functor](Size n1, Size n2) {
119  SPH_ASSERT(n1 < n2);
120  for (Size i = n1; i < n2; ++i) {
121  functor(i);
122  }
123  });
124 }
125 
129 template <typename TFunctor>
130 INLINE void parallelFor(IScheduler& scheduler, const IndexSequence& sequence, TFunctor&& functor) {
131  parallelFor(scheduler, *sequence.begin(), *sequence.end(), std::forward<TFunctor>(functor));
132 }
133 
134 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Generic wrappers of lambdas, functors and other callables.
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
Helper objects allowing to iterate in reverse, iterate over multiple containeres, etc.
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
SequentialScheduler SEQUENTIAL
Global instance of the sequential scheduler.
Definition: Scheduler.cpp:43
INLINE void parallelFor(IScheduler &scheduler, const Size from, const Size to, TFunctor &&functor)
Executes a functor concurrently from all available threads.
Definition: Scheduler.h:98
Interface that allows unified implementation of sequential and parallelized versions of algorithms.
Definition: Scheduler.h:27
virtual Optional< Size > getThreadIdx() const =0
Returns the index of the calling thread.
virtual void parallelFor(const Size from, const Size to, const Size granularity, const Function< void(Size n1, Size n2)> &functor)
Processes the given range concurrently.
Definition: Scheduler.cpp:46
virtual Size getRecommendedGranularity() const =0
Returns a value of granularity that is expected to perform well with the current thread count.
virtual SharedPtr< ITask > submit(const Function< void()> &task)=0
Submits a task to be potentially executed asynchronously.
virtual Size getThreadCnt() const =0
Returns the number of threads used by this scheduler.
Handle used to control tasks submitted into the scheduler.
Definition: Scheduler.h:15
virtual bool completed() const =0
Checks if the task already finished.
virtual void wait()=0
Waits till the task and all the child tasks are completed.
INLINE IndexIterator begin() const
INLINE IndexIterator end() const
Dummy scheduler that simply executes the submitted tasks sequentially on calling thread.
Definition: Scheduler.h:67
virtual Size getRecommendedGranularity() const override
Returns a value of granularity that is expected to perform well with the current thread count.
Definition: Scheduler.cpp:34
virtual SharedPtr< ITask > submit(const Function< void()> &task) override
Submits a task to be potentially executed asynchronously.
Definition: Scheduler.cpp:19
virtual Size getThreadCnt() const override
Returns the number of threads used by this scheduler.
Definition: Scheduler.cpp:29
static SharedPtr< SequentialScheduler > getGlobalInstance()
Definition: Scheduler.cpp:38
virtual Optional< Size > getThreadIdx() const override
Returns the index of the calling thread.
Definition: Scheduler.cpp:24
Base class for all polymorphic objects.
Definition: Object.h:88