Skip to content

Commit 6e00c76

Browse files
author
Woody Chow
committed
Use num_threads clause of #pragma omp parallel instead to avoid calling omp_set_num_threads for the entire application
1 parent 913a2db commit 6e00c76

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

modules/core/src/parallel.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ void cv::parallel_for_(const cv::Range& range, const cv::ParallelLoopBody& body,
349349

350350
#elif defined HAVE_OPENMP
351351

352-
#pragma omp parallel for schedule(dynamic)
352+
#pragma omp parallel for schedule(dynamic) num_threads(numThreads > 0 ? numThreads : numThreadsMax)
353353
for (int i = stripeRange.start; i < stripeRange.end; ++i)
354354
pbody(Range(i, i + 1));
355355

@@ -418,7 +418,10 @@ int cv::getNumThreads(void)
418418

419419
#elif defined HAVE_OPENMP
420420

421-
return omp_get_max_threads();
421+
return numThreads > 0
422+
? numThreads
423+
: numThreadsMax;
424+
422425

423426
#elif defined HAVE_GCD
424427

@@ -463,10 +466,7 @@ void cv::setNumThreads( int threads )
463466

464467
#elif defined HAVE_OPENMP
465468

466-
if(omp_in_parallel())
467-
return; // can't change number of openmp threads inside a parallel region
468-
469-
omp_set_num_threads(threads > 0 ? threads : numThreadsMax);
469+
return; // nothing needed as num_threads clause is used in #pragma omp parallel for
470470

471471
#elif defined HAVE_GCD
472472

0 commit comments

Comments
 (0)