SPH
Scheduler.cpp
Go to the documentation of this file.
1 #include "thread/Scheduler.h"
2 #include "math/MathUtils.h"
4 
6 
7 class SequentialTaskHandle : public ITask {
8 public:
9  virtual void wait() override {
10  // when we return this handle, the task has been already finished, so we do not have to wait
11  }
12 
13  virtual bool completed() const override {
14  // when we return this handle, the task has been already finished
15  return true;
16  }
17 };
18 
20  task();
21  return makeShared<SequentialTaskHandle>();
22 }
23 
25  // imitating ThreadPool with 1 thread, so that we can use ThreadLocal with this scheduler
26  return 0;
27 }
28 
30  // imitating ThreadPool with 1 thread, so that we can use ThreadLocal with this scheduler
31  return 1;
32 }
33 
35  return 1;
36 }
37 
39  static SharedPtr<SequentialScheduler> scheduler = makeShared<SequentialScheduler>();
40  return scheduler;
41 }
42 
44 
45 
46 void IScheduler::parallelFor(const Size from,
47  const Size to,
48  const Size granularity,
49  const Function<void(Size n1, Size n2)>& functor) {
50  SPH_ASSERT(to > from);
51  SPH_ASSERT(granularity > 0);
52 
53  SharedPtr<ITask> handle = this->submit([this, from, to, granularity, &functor] {
54  for (Size n = from; n < to; n += granularity) {
55  const Size n1 = n;
56  const Size n2 = min(n1 + granularity, to);
57  this->submit([n1, n2, &functor] { functor(n1, n2); });
58  }
59  });
60  handle->wait();
61  SPH_ASSERT(handle->completed());
62 }
63 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
NAMESPACE_SPH_BEGIN constexpr INLINE T min(const T &f1, const T &f2)
Minimum & Maximum value.
Definition: MathBasic.h:15
Additional math routines (with more includes).
#define NAMESPACE_SPH_END
Definition: Object.h:12
Wrapper of type value of which may or may not be present.
SequentialScheduler SEQUENTIAL
Global instance of the sequential scheduler.
Definition: Scheduler.cpp:43
Interface for executing tasks (potentially) asynchronously.
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 SharedPtr< ITask > submit(const Function< void()> &task)=0
Submits a task to be potentially executed asynchronously.
Handle used to control tasks submitted into the scheduler.
Definition: Scheduler.h:15
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
virtual void wait() override
Waits till the task and all the child tasks are completed.
Definition: Scheduler.cpp:9
virtual bool completed() const override
Checks if the task already finished.
Definition: Scheduler.cpp:13