SPH
OpenMp.cpp
Go to the documentation of this file.
1 #include "thread/OpenMp.h"
2 #include "math/MathBasic.h"
4 #include "thread/CheckFunction.h"
5 
6 #ifdef SPH_USE_OPENMP
7 #include <omp.h>
8 #endif
9 
11 
12 #ifdef SPH_USE_OPENMP
13 
14 SharedPtr<OmpScheduler> OmpScheduler::globalInstance;
15 
16 OmpScheduler::OmpScheduler(const Size numThreads) {
17  if (numThreads > 0) {
18  omp_set_num_threads(numThreads);
19  }
20 }
21 
22 class OmpTaskHandle : public ITask {
23 public:
24  virtual void wait() override {}
25 
26  virtual bool completed() const override {
27  return true;
28  }
29 };
30 
31 
32 SharedPtr<ITask> OmpScheduler::submit(const Function<void()>& task) {
33  if (isMainThread()) {
34 #pragma omp parallel
35  {
36 #pragma omp single
37  {
38 #pragma omp taskgroup
39  {
40 #pragma omp task
41  { task(); }
42  }
43  }
44  }
45  } else {
46 #pragma omp task
47  { task(); }
48  }
49  return makeShared<OmpTaskHandle>();
50 }
51 
53  return omp_get_thread_num();
54 }
55 
57  return omp_get_max_threads();
58 }
59 
61  return granularity;
62 }
63 
64 void OmpScheduler::parallelFor(const Size from,
65  const Size to,
66  const Size granularity,
67  const Function<void(Size n1, Size n2)>& functor) {
68 #pragma omp parallel for schedule(dynamic, 1)
69  for (Size n = from; n < to; n += granularity) {
70  const Size n1 = n;
71  const Size n2 = min(n1 + granularity, to);
72  functor(n1, n2);
73  }
74 }
75 
77  if (!globalInstance) {
78  globalInstance = makeShared<OmpScheduler>();
79  }
80  return globalInstance;
81 }
82 
83 #endif
84 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
bool isMainThread()
Checks if the calling thread is the main thred.
Helper functions to check the internal consistency of the code.
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
Basic math routines.
NAMESPACE_SPH_BEGIN constexpr INLINE T min(const T &f1, const T &f2)
Minimum & Maximum value.
Definition: MathBasic.h:15
#define NAMESPACE_SPH_END
Definition: Object.h:12
Wrapper of type value of which may or may not be present.
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.
virtual SharedPtr< ITask > submit(const Function< void()> &task) override
Submits a task to be potentially executed asynchronously.
OmpScheduler(const Size numThreads=0)
virtual void parallelFor(const Size from, const Size to, const Size granularity, const Function< void(Size n1, Size n2)> &functor) override
Processes the given range concurrently.
virtual Size getRecommendedGranularity() const override
Returns a value of granularity that is expected to perform well with the current thread count.
static SharedPtr< OmpScheduler > getGlobalInstance()
virtual Optional< Size > getThreadIdx() const override
Returns the index of the calling thread.
virtual Size getThreadCnt() const override
Returns the number of threads used by this scheduler.