8 #ifndef TREELITE_DETAIL_THREADING_UTILS_H_
9 #define TREELITE_DETAIL_THREADING_UTILS_H_
20 #include <type_traits>
22 #if TREELITE_OPENMP_SUPPORT
41 return std::numeric_limits<int>::max();
51 int limit = std::numeric_limits<int>::max();
80 <<
"nthread cannot exceed " <<
MaxNumThread() <<
" (configured by OpenMP).";
82 this->nthread =
static_cast<std::uint32_t
>(
nthread);
110 template <
typename IndexType,
typename FuncType>
117 #if defined(_MSC_VER)
119 using OmpInd = std::conditional_t<std::is_signed<IndexType>::value, IndexType, std::int64_t>;
121 using OmpInd = IndexType;
125 switch (sched.sched) {
127 #pragma omp parallel for num_threads(thread_config.nthread)
128 for (OmpInd i = begin; i < end; ++i) {
134 if (sched.
chunk == 0) {
135 #pragma omp parallel for num_threads(thread_config.nthread) schedule(dynamic)
136 for (OmpInd i = begin; i < end; ++i) {
140 #pragma omp parallel for num_threads(thread_config.nthread) schedule(dynamic, sched.chunk)
141 for (OmpInd i = begin; i < end; ++i) {
148 if (sched.
chunk == 0) {
149 #pragma omp parallel for num_threads(thread_config.nthread) schedule(static)
150 for (OmpInd i = begin; i < end; ++i) {
154 #pragma omp parallel for num_threads(thread_config.nthread) schedule(static, sched.chunk)
155 for (OmpInd i = begin; i < end; ++i) {
162 #pragma omp parallel for num_threads(thread_config.nthread) schedule(guided)
163 for (OmpInd i = begin; i < end; ++i) {
OMP Exception class catches, saves and rethrows exception from OMP blocks.
Definition: omp_exception.h:20
void Run(Function f, Parameters... params)
Parallel OMP blocks should be placed within Run to save exception.
Definition: omp_exception.h:32
void Rethrow()
should be called from the main thread to rethrow the exception
Definition: omp_exception.h:51
logging facility for Treelite
#define TREELITE_CHECK_LE(x, y)
Definition: logging.h:75
#define TREELITE_CHECK_GE(x, y)
Definition: logging.h:76
Definition: threading_utils.h:46
int OmpGetThreadLimit()
Definition: threading_utils.h:48
void ParallelFor(IndexType begin, IndexType end, ThreadConfig const &thread_config, ParallelSchedule sched, FuncType func)
Definition: threading_utils.h:111
int MaxNumThread()
Definition: threading_utils.h:59
Utility to propagate exceptions throws inside an OpenMP block.
Definition: threading_utils.h:87
@ kGuided
Definition: threading_utils.h:92
@ kAuto
Definition: threading_utils.h:89
@ kStatic
Definition: threading_utils.h:91
@ kDynamic
Definition: threading_utils.h:90
static ParallelSchedule Dynamic(std::size_t n=0)
Definition: threading_utils.h:99
static ParallelSchedule Static(std::size_t n=0)
Definition: threading_utils.h:102
enum treelite::detail::threading_utils::ParallelSchedule::@0 kStatic
static ParallelSchedule Auto()
Definition: threading_utils.h:96
static ParallelSchedule Guided()
Definition: threading_utils.h:105
std::size_t chunk
Definition: threading_utils.h:94
Represent thread configuration, to be used with parallel loops.
Definition: threading_utils.h:66
std::uint32_t nthread
Definition: threading_utils.h:67
ThreadConfig(int nthread)
Create thread configuration.
Definition: threading_utils.h:74
int omp_get_num_procs()
Definition: threading_utils.h:32
int omp_get_max_threads()
Definition: threading_utils.h:28
int omp_get_thread_limit()
Definition: threading_utils.h:40
int omp_get_thread_num()
Definition: threading_utils.h:36