Skip to content

Commit f1c52e4

Browse files
woodychowalalek
authored andcommitted
Merge pull request opencv#10697 from woodychow:tbb_task_arena
* Use Intel TBB's task arena if possible
1 parent 203dc3b commit f1c52e4

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

modules/core/src/parallel.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,12 @@ namespace
298298
{
299299
this->ParallelLoopBodyWrapper::operator()(cv::Range(range.begin(), range.end()));
300300
}
301+
302+
void operator ()() const // run parallel job
303+
{
304+
cv::Range stripeRange = this->stripeRange();
305+
tbb::parallel_for(tbb::blocked_range<int>(stripeRange.start, stripeRange.end), *this);
306+
}
301307
};
302308
#elif defined HAVE_CSTRIPES || defined HAVE_OPENMP
303309
typedef ParallelLoopBodyWrapper ProxyLoopBody;
@@ -328,7 +334,11 @@ namespace
328334
static int numThreads = -1;
329335

330336
#if defined HAVE_TBB
331-
static tbb::task_scheduler_init tbbScheduler(tbb::task_scheduler_init::deferred);
337+
#if TBB_INTERFACE_VERSION >= 8000
338+
static tbb::task_arena tbbArena(tbb::task_arena::automatic);
339+
#else
340+
static tbb::task_scheduler_init tbbScheduler(tbb::task_scheduler_init::deferred);
341+
#endif
332342
#elif defined HAVE_CSTRIPES
333343
// nothing for C=
334344
#elif defined HAVE_OPENMP
@@ -424,7 +434,11 @@ static void parallel_for_impl(const cv::Range& range, const cv::ParallelLoopBody
424434

425435
#if defined HAVE_TBB
426436

427-
tbb::parallel_for(tbb::blocked_range<int>(stripeRange.start, stripeRange.end), pbody);
437+
#if TBB_INTERFACE_VERSION >= 8000
438+
tbbArena.execute(pbody);
439+
#else
440+
pbody();
441+
#endif
428442

429443
#elif defined HAVE_CSTRIPES
430444

@@ -494,9 +508,17 @@ int cv::getNumThreads(void)
494508

495509
#if defined HAVE_TBB
496510

511+
#if TBB_INTERFACE_VERSION >= 9100
512+
return tbbArena.max_concurrency();
513+
#elif TBB_INTERFACE_VERSION >= 8000
514+
return numThreads > 0
515+
? numThreads
516+
: tbb::task_scheduler_init::default_num_threads();
517+
#else
497518
return tbbScheduler.is_active()
498519
? numThreads
499520
: tbb::task_scheduler_init::default_num_threads();
521+
#endif
500522

501523
#elif defined HAVE_CSTRIPES
502524

@@ -545,8 +567,13 @@ void cv::setNumThreads( int threads )
545567

546568
#ifdef HAVE_TBB
547569

570+
#if TBB_INTERFACE_VERSION >= 8000
571+
if(tbbArena.is_active()) tbbArena.terminate();
572+
if(threads > 0) tbbArena.initialize(threads);
573+
#else
548574
if(tbbScheduler.is_active()) tbbScheduler.terminate();
549575
if(threads > 0) tbbScheduler.initialize(threads);
576+
#endif
550577

551578
#elif defined HAVE_CSTRIPES
552579

0 commit comments

Comments
 (0)