18 omp_set_num_threads(numThreads);
22 class OmpTaskHandle :
public ITask {
24 virtual void wait()
override {}
49 return makeShared<OmpTaskHandle>();
53 return omp_get_thread_num();
57 return omp_get_max_threads();
66 const Size granularity,
68 #pragma omp parallel for schedule(dynamic, 1)
69 for (
Size n = from; n < to; n += granularity) {
71 const Size n2 =
min(n1 + granularity, to);
77 if (!globalInstance) {
78 globalInstance = makeShared<OmpScheduler>();
80 return globalInstance;
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).
NAMESPACE_SPH_BEGIN constexpr INLINE T min(const T &f1, const T &f2)
Minimum & Maximum value.
#define NAMESPACE_SPH_END
Wrapper of type value of which may or may not be present.
Handle used to control tasks submitted into the scheduler.
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.