2012-08-02 20:25:30 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
|
|
// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
|
2012-11-01 16:44:10 +08:00
|
|
|
#if defined WIN32 || defined WINCE
|
|
|
|
#include <windows.h>
|
|
|
|
#undef small
|
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
#undef abs
|
|
|
|
#endif
|
|
|
|
|
2012-10-17 21:00:53 +08:00
|
|
|
#if defined __linux__ || defined __APPLE__
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#if defined ANDROID
|
|
|
|
#include <sys/sysconf.h>
|
2015-01-13 08:52:56 +08:00
|
|
|
#elif defined __APPLE__
|
2012-10-17 21:00:53 +08:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2013-06-21 16:43:16 +08:00
|
|
|
#ifdef _OPENMP
|
|
|
|
#define HAVE_OPENMP
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#define HAVE_GCD
|
|
|
|
#endif
|
|
|
|
|
2015-03-18 20:13:08 +08:00
|
|
|
#if defined _MSC_VER && _MSC_VER >= 1600
|
2013-06-21 16:43:16 +08:00
|
|
|
#define HAVE_CONCURRENCY
|
|
|
|
#endif
|
2012-10-13 00:06:40 +08:00
|
|
|
|
|
|
|
/* IMPORTANT: always use the same order of defines
|
|
|
|
1. HAVE_TBB - 3rdparty library, should be explicitly enabled
|
|
|
|
2. HAVE_CSTRIPES - 3rdparty library, should be explicitly enabled
|
|
|
|
3. HAVE_OPENMP - integrated to compiler, should be explicitly enabled
|
|
|
|
4. HAVE_GCD - system wide, used automatically (APPLE only)
|
2015-03-18 20:13:08 +08:00
|
|
|
5. WINRT - system wide, used automatically (Windows RT only)
|
|
|
|
6. HAVE_CONCURRENCY - part of runtime, used automatically (Windows only - MSVS 10, MSVS 11)
|
2015-06-11 21:53:07 +08:00
|
|
|
7. HAVE_PTHREADS_PF - pthreads if available
|
2012-10-13 00:06:40 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined HAVE_TBB
|
2012-09-24 16:40:20 +08:00
|
|
|
#include "tbb/tbb_stddef.h"
|
|
|
|
#if TBB_VERSION_MAJOR*100 + TBB_VERSION_MINOR >= 202
|
|
|
|
#include "tbb/tbb.h"
|
|
|
|
#include "tbb/task.h"
|
2012-10-13 00:06:40 +08:00
|
|
|
#if TBB_INTERFACE_VERSION >= 6100
|
|
|
|
#include "tbb/task_arena.h"
|
|
|
|
#endif
|
2012-09-24 16:40:20 +08:00
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
#else
|
|
|
|
#undef HAVE_TBB
|
|
|
|
#endif // end TBB version
|
2012-10-10 19:36:32 +08:00
|
|
|
#endif
|
2012-08-02 20:25:30 +08:00
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
#ifndef HAVE_TBB
|
|
|
|
#if defined HAVE_CSTRIPES
|
|
|
|
#include "C=.h"
|
|
|
|
#undef shared
|
|
|
|
#elif defined HAVE_OPENMP
|
|
|
|
#include <omp.h>
|
|
|
|
#elif defined HAVE_GCD
|
|
|
|
#include <dispatch/dispatch.h>
|
|
|
|
#include <pthread.h>
|
2015-12-18 04:15:59 +08:00
|
|
|
#elif defined WINRT && _MSC_VER < 1900
|
2015-03-18 20:13:08 +08:00
|
|
|
#include <ppltasks.h>
|
2012-10-13 00:06:40 +08:00
|
|
|
#elif defined HAVE_CONCURRENCY
|
|
|
|
#include <ppl.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2013-06-21 16:43:16 +08:00
|
|
|
#if defined HAVE_TBB && TBB_VERSION_MAJOR*100 + TBB_VERSION_MINOR >= 202
|
|
|
|
# define CV_PARALLEL_FRAMEWORK "tbb"
|
|
|
|
#elif defined HAVE_CSTRIPES
|
|
|
|
# define CV_PARALLEL_FRAMEWORK "cstripes"
|
|
|
|
#elif defined HAVE_OPENMP
|
|
|
|
# define CV_PARALLEL_FRAMEWORK "openmp"
|
|
|
|
#elif defined HAVE_GCD
|
|
|
|
# define CV_PARALLEL_FRAMEWORK "gcd"
|
2015-03-18 20:13:08 +08:00
|
|
|
#elif defined WINRT
|
|
|
|
# define CV_PARALLEL_FRAMEWORK "winrt-concurrency"
|
2013-06-21 16:43:16 +08:00
|
|
|
#elif defined HAVE_CONCURRENCY
|
|
|
|
# define CV_PARALLEL_FRAMEWORK "ms-concurrency"
|
2015-06-11 21:53:07 +08:00
|
|
|
#elif defined HAVE_PTHREADS_PF
|
2015-05-30 02:58:45 +08:00
|
|
|
# define CV_PARALLEL_FRAMEWORK "pthreads"
|
2013-06-21 16:43:16 +08:00
|
|
|
#endif
|
|
|
|
|
2012-08-02 20:25:30 +08:00
|
|
|
namespace cv
|
|
|
|
{
|
2012-10-13 00:06:40 +08:00
|
|
|
ParallelLoopBody::~ParallelLoopBody() {}
|
2015-06-11 21:53:07 +08:00
|
|
|
#ifdef HAVE_PTHREADS_PF
|
2015-06-09 18:59:48 +08:00
|
|
|
void parallel_for_pthreads(const cv::Range& range, const cv::ParallelLoopBody& body, double nstripes);
|
|
|
|
size_t parallel_pthreads_get_threads_num();
|
|
|
|
void parallel_pthreads_set_threads_num(int num);
|
|
|
|
#endif
|
2012-10-13 00:06:40 +08:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:59:48 +08:00
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
namespace
|
|
|
|
{
|
2013-06-18 01:06:15 +08:00
|
|
|
#ifdef CV_PARALLEL_FRAMEWORK
|
2012-10-12 02:37:14 +08:00
|
|
|
class ParallelLoopBodyWrapper
|
|
|
|
{
|
|
|
|
public:
|
2012-10-13 00:06:40 +08:00
|
|
|
ParallelLoopBodyWrapper(const cv::ParallelLoopBody& _body, const cv::Range& _r, double _nstripes)
|
2012-10-12 02:37:14 +08:00
|
|
|
{
|
2016-08-15 22:12:45 +08:00
|
|
|
|
2012-10-12 02:37:14 +08:00
|
|
|
body = &_body;
|
|
|
|
wholeRange = _r;
|
|
|
|
double len = wholeRange.end - wholeRange.start;
|
2012-10-13 00:06:40 +08:00
|
|
|
nstripes = cvRound(_nstripes <= 0 ? len : MIN(MAX(_nstripes, 1.), len));
|
2016-08-15 22:12:45 +08:00
|
|
|
|
|
|
|
#ifdef ENABLE_INSTRUMENTATION
|
|
|
|
pThreadRoot = cv::instr::getInstrumentTLSStruct().pCurrentNode;
|
|
|
|
#endif
|
2012-10-12 02:37:14 +08:00
|
|
|
}
|
2012-10-13 00:06:40 +08:00
|
|
|
void operator()(const cv::Range& sr) const
|
2012-10-12 02:37:14 +08:00
|
|
|
{
|
2016-08-15 22:12:45 +08:00
|
|
|
#ifdef ENABLE_INSTRUMENTATION
|
|
|
|
{
|
|
|
|
cv::instr::InstrTLSStruct *pInstrTLS = &cv::instr::getInstrumentTLSStruct();
|
|
|
|
pInstrTLS->pCurrentNode = pThreadRoot; // Initialize TLS node for thread
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
cv::Range r;
|
2012-10-12 02:37:14 +08:00
|
|
|
r.start = (int)(wholeRange.start +
|
2013-08-23 15:35:55 +08:00
|
|
|
((uint64)sr.start*(wholeRange.end - wholeRange.start) + nstripes/2)/nstripes);
|
2012-10-12 02:37:14 +08:00
|
|
|
r.end = sr.end >= nstripes ? wholeRange.end : (int)(wholeRange.start +
|
2013-08-23 15:35:55 +08:00
|
|
|
((uint64)sr.end*(wholeRange.end - wholeRange.start) + nstripes/2)/nstripes);
|
2012-10-12 02:37:14 +08:00
|
|
|
(*body)(r);
|
|
|
|
}
|
2012-10-13 00:06:40 +08:00
|
|
|
cv::Range stripeRange() const { return cv::Range(0, nstripes); }
|
2012-08-02 20:25:30 +08:00
|
|
|
|
2012-10-12 02:37:14 +08:00
|
|
|
protected:
|
2012-10-13 00:06:40 +08:00
|
|
|
const cv::ParallelLoopBody* body;
|
|
|
|
cv::Range wholeRange;
|
2012-10-12 02:37:14 +08:00
|
|
|
int nstripes;
|
2016-08-15 22:12:45 +08:00
|
|
|
#ifdef ENABLE_INSTRUMENTATION
|
|
|
|
cv::instr::InstrNode *pThreadRoot;
|
|
|
|
#endif
|
2012-10-12 02:37:14 +08:00
|
|
|
};
|
2012-10-12 18:49:22 +08:00
|
|
|
|
2012-10-12 02:37:14 +08:00
|
|
|
#if defined HAVE_TBB
|
|
|
|
class ProxyLoopBody : public ParallelLoopBodyWrapper
|
2012-08-02 20:25:30 +08:00
|
|
|
{
|
|
|
|
public:
|
2012-10-13 00:06:40 +08:00
|
|
|
ProxyLoopBody(const cv::ParallelLoopBody& _body, const cv::Range& _r, double _nstripes)
|
2012-10-12 02:37:14 +08:00
|
|
|
: ParallelLoopBodyWrapper(_body, _r, _nstripes)
|
|
|
|
{}
|
2012-08-02 20:25:30 +08:00
|
|
|
|
|
|
|
void operator ()(const tbb::blocked_range<int>& range) const
|
|
|
|
{
|
2012-10-13 00:06:40 +08:00
|
|
|
this->ParallelLoopBodyWrapper::operator()(cv::Range(range.begin(), range.end()));
|
2012-08-02 20:25:30 +08:00
|
|
|
}
|
|
|
|
};
|
2012-10-13 00:06:40 +08:00
|
|
|
#elif defined HAVE_CSTRIPES || defined HAVE_OPENMP
|
|
|
|
typedef ParallelLoopBodyWrapper ProxyLoopBody;
|
2012-10-12 02:37:14 +08:00
|
|
|
#elif defined HAVE_GCD
|
|
|
|
typedef ParallelLoopBodyWrapper ProxyLoopBody;
|
2012-10-13 00:06:40 +08:00
|
|
|
static void block_function(void* context, size_t index)
|
2012-08-02 20:25:30 +08:00
|
|
|
{
|
2012-10-12 02:37:14 +08:00
|
|
|
ProxyLoopBody* ptr_body = static_cast<ProxyLoopBody*>(context);
|
2014-10-17 20:32:53 +08:00
|
|
|
(*ptr_body)(cv::Range((int)index, (int)index + 1));
|
2012-08-02 20:25:30 +08:00
|
|
|
}
|
2015-03-18 20:13:08 +08:00
|
|
|
#elif defined WINRT || defined HAVE_CONCURRENCY
|
2012-10-12 02:37:14 +08:00
|
|
|
class ProxyLoopBody : public ParallelLoopBodyWrapper
|
|
|
|
{
|
|
|
|
public:
|
2012-10-13 00:06:40 +08:00
|
|
|
ProxyLoopBody(const cv::ParallelLoopBody& _body, const cv::Range& _r, double _nstripes)
|
2012-10-12 02:37:14 +08:00
|
|
|
: ParallelLoopBodyWrapper(_body, _r, _nstripes)
|
|
|
|
{}
|
2012-10-12 18:49:22 +08:00
|
|
|
|
2012-10-12 02:37:14 +08:00
|
|
|
void operator ()(int i) const
|
|
|
|
{
|
2012-10-13 00:06:40 +08:00
|
|
|
this->ParallelLoopBodyWrapper::operator()(cv::Range(i, i + 1));
|
2012-10-12 02:37:14 +08:00
|
|
|
}
|
2012-10-12 03:22:32 +08:00
|
|
|
};
|
2012-10-12 02:37:14 +08:00
|
|
|
#else
|
|
|
|
typedef ParallelLoopBodyWrapper ProxyLoopBody;
|
|
|
|
#endif
|
2012-08-02 20:25:30 +08:00
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
static int numThreads = -1;
|
|
|
|
|
|
|
|
#if defined HAVE_TBB
|
|
|
|
static tbb::task_scheduler_init tbbScheduler(tbb::task_scheduler_init::deferred);
|
|
|
|
#elif defined HAVE_CSTRIPES
|
|
|
|
// nothing for C=
|
|
|
|
#elif defined HAVE_OPENMP
|
|
|
|
static int numThreadsMax = omp_get_max_threads();
|
|
|
|
#elif defined HAVE_GCD
|
|
|
|
// nothing for GCD
|
2015-03-18 20:13:08 +08:00
|
|
|
#elif defined WINRT
|
|
|
|
// nothing for WINRT
|
2012-10-13 00:06:40 +08:00
|
|
|
#elif defined HAVE_CONCURRENCY
|
2015-03-18 20:13:08 +08:00
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
class SchedPtr
|
|
|
|
{
|
|
|
|
Concurrency::Scheduler* sched_;
|
|
|
|
public:
|
|
|
|
Concurrency::Scheduler* operator->() { return sched_; }
|
|
|
|
operator Concurrency::Scheduler*() { return sched_; }
|
|
|
|
|
|
|
|
void operator=(Concurrency::Scheduler* sched)
|
|
|
|
{
|
|
|
|
if (sched_) sched_->Release();
|
|
|
|
sched_ = sched;
|
|
|
|
}
|
|
|
|
|
|
|
|
SchedPtr() : sched_(0) {}
|
2015-09-07 23:59:07 +08:00
|
|
|
~SchedPtr() {}
|
2012-10-13 00:06:40 +08:00
|
|
|
};
|
|
|
|
static SchedPtr pplScheduler;
|
2015-03-18 20:13:08 +08:00
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
#endif
|
|
|
|
|
2013-06-18 01:06:15 +08:00
|
|
|
#endif // CV_PARALLEL_FRAMEWORK
|
2012-10-13 00:06:40 +08:00
|
|
|
|
|
|
|
} //namespace
|
|
|
|
|
|
|
|
/* ================================ parallel_for_ ================================ */
|
|
|
|
|
|
|
|
void cv::parallel_for_(const cv::Range& range, const cv::ParallelLoopBody& body, double nstripes)
|
|
|
|
{
|
2016-08-15 22:12:45 +08:00
|
|
|
CV_INSTRUMENT_REGION()
|
|
|
|
|
2013-06-18 01:06:15 +08:00
|
|
|
#ifdef CV_PARALLEL_FRAMEWORK
|
2012-10-13 00:06:40 +08:00
|
|
|
|
|
|
|
if(numThreads != 0)
|
2012-08-02 20:25:30 +08:00
|
|
|
{
|
2012-10-12 02:37:14 +08:00
|
|
|
ProxyLoopBody pbody(body, range, nstripes);
|
2012-10-13 00:06:40 +08:00
|
|
|
cv::Range stripeRange = pbody.stripeRange();
|
2014-08-16 04:29:10 +08:00
|
|
|
if( stripeRange.end - stripeRange.start == 1 )
|
|
|
|
{
|
|
|
|
body(range);
|
|
|
|
return;
|
|
|
|
}
|
2012-10-12 18:49:22 +08:00
|
|
|
|
2012-10-12 02:37:14 +08:00
|
|
|
#if defined HAVE_TBB
|
2012-08-02 20:25:30 +08:00
|
|
|
|
2012-10-12 02:37:14 +08:00
|
|
|
tbb::parallel_for(tbb::blocked_range<int>(stripeRange.start, stripeRange.end), pbody);
|
2012-08-02 20:25:30 +08:00
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
#elif defined HAVE_CSTRIPES
|
2012-08-02 20:25:30 +08:00
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
parallel(MAX(0, numThreads))
|
|
|
|
{
|
|
|
|
int offset = stripeRange.start;
|
|
|
|
int len = stripeRange.end - offset;
|
|
|
|
Range r(offset + CPX_RANGE_START(len), offset + CPX_RANGE_END(len));
|
|
|
|
pbody(r);
|
|
|
|
barrier();
|
|
|
|
}
|
2012-08-02 20:25:30 +08:00
|
|
|
|
|
|
|
#elif defined HAVE_OPENMP
|
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
#pragma omp parallel for schedule(dynamic)
|
2012-10-12 02:37:14 +08:00
|
|
|
for (int i = stripeRange.start; i < stripeRange.end; ++i)
|
|
|
|
pbody(Range(i, i + 1));
|
2012-08-02 20:25:30 +08:00
|
|
|
|
2012-10-10 19:36:32 +08:00
|
|
|
#elif defined HAVE_GCD
|
2012-08-02 20:25:30 +08:00
|
|
|
|
|
|
|
dispatch_queue_t concurrent_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
|
2012-10-12 02:37:14 +08:00
|
|
|
dispatch_apply_f(stripeRange.end - stripeRange.start, concurrent_queue, &pbody, block_function);
|
2012-08-02 20:25:30 +08:00
|
|
|
|
2015-03-18 20:13:08 +08:00
|
|
|
#elif defined WINRT
|
|
|
|
|
|
|
|
Concurrency::parallel_for(stripeRange.start, stripeRange.end, pbody);
|
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
#elif defined HAVE_CONCURRENCY
|
2012-10-10 19:36:32 +08:00
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
if(!pplScheduler || pplScheduler->Id() == Concurrency::CurrentScheduler::Id())
|
2012-10-10 19:36:32 +08:00
|
|
|
{
|
2012-10-13 00:06:40 +08:00
|
|
|
Concurrency::parallel_for(stripeRange.start, stripeRange.end, pbody);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pplScheduler->Attach();
|
|
|
|
Concurrency::parallel_for(stripeRange.start, stripeRange.end, pbody);
|
|
|
|
Concurrency::CurrentScheduler::Detach();
|
2012-10-10 19:36:32 +08:00
|
|
|
}
|
|
|
|
|
2015-06-11 21:53:07 +08:00
|
|
|
#elif defined HAVE_PTHREADS_PF
|
2015-06-09 18:59:48 +08:00
|
|
|
|
2015-05-30 02:58:45 +08:00
|
|
|
parallel_for_pthreads(range, body, nstripes);
|
|
|
|
|
2012-08-02 20:25:30 +08:00
|
|
|
#else
|
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
#error You have hacked and compiling with unsupported parallel framework
|
2012-08-02 20:25:30 +08:00
|
|
|
|
2012-10-12 02:37:14 +08:00
|
|
|
#endif
|
2012-10-12 18:49:22 +08:00
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
}
|
|
|
|
else
|
2012-10-12 18:49:22 +08:00
|
|
|
|
2013-06-18 01:06:15 +08:00
|
|
|
#endif // CV_PARALLEL_FRAMEWORK
|
2012-10-13 00:06:40 +08:00
|
|
|
{
|
|
|
|
(void)nstripes;
|
|
|
|
body(range);
|
|
|
|
}
|
|
|
|
}
|
2012-10-12 18:49:22 +08:00
|
|
|
|
|
|
|
int cv::getNumThreads(void)
|
|
|
|
{
|
2013-06-18 01:06:15 +08:00
|
|
|
#ifdef CV_PARALLEL_FRAMEWORK
|
2012-10-13 00:06:40 +08:00
|
|
|
|
|
|
|
if(numThreads == 0)
|
|
|
|
return 1;
|
2012-10-12 18:49:22 +08:00
|
|
|
|
|
|
|
#endif
|
2012-10-13 00:06:40 +08:00
|
|
|
|
|
|
|
#if defined HAVE_TBB
|
|
|
|
|
|
|
|
return tbbScheduler.is_active()
|
|
|
|
? numThreads
|
|
|
|
: tbb::task_scheduler_init::default_num_threads();
|
|
|
|
|
|
|
|
#elif defined HAVE_CSTRIPES
|
|
|
|
|
2013-02-04 21:34:18 +08:00
|
|
|
return numThreads > 0
|
|
|
|
? numThreads
|
|
|
|
: cv::getNumberOfCPUs();
|
2012-10-13 00:06:40 +08:00
|
|
|
|
|
|
|
#elif defined HAVE_OPENMP
|
|
|
|
|
|
|
|
return omp_get_max_threads();
|
|
|
|
|
|
|
|
#elif defined HAVE_GCD
|
|
|
|
|
|
|
|
return 512; // the GCD thread pool limit
|
|
|
|
|
2015-03-18 20:13:08 +08:00
|
|
|
#elif defined WINRT
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
#elif defined HAVE_CONCURRENCY
|
|
|
|
|
|
|
|
return 1 + (pplScheduler == 0
|
2015-03-18 20:13:08 +08:00
|
|
|
? Concurrency::CurrentScheduler::Get()->GetNumberOfVirtualProcessors()
|
|
|
|
: pplScheduler->GetNumberOfVirtualProcessors());
|
2012-10-13 00:06:40 +08:00
|
|
|
|
2015-06-11 21:53:07 +08:00
|
|
|
#elif defined HAVE_PTHREADS_PF
|
2015-05-30 02:58:45 +08:00
|
|
|
|
|
|
|
return parallel_pthreads_get_threads_num();
|
|
|
|
|
2012-10-12 18:49:22 +08:00
|
|
|
#else
|
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
return 1;
|
2012-10-12 18:49:22 +08:00
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void cv::setNumThreads( int threads )
|
|
|
|
{
|
2012-10-15 23:30:18 +08:00
|
|
|
(void)threads;
|
2013-06-18 01:06:15 +08:00
|
|
|
#ifdef CV_PARALLEL_FRAMEWORK
|
2012-10-12 18:49:22 +08:00
|
|
|
numThreads = threads;
|
2012-10-13 00:06:40 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_TBB
|
|
|
|
|
|
|
|
if(tbbScheduler.is_active()) tbbScheduler.terminate();
|
|
|
|
if(threads > 0) tbbScheduler.initialize(threads);
|
|
|
|
|
|
|
|
#elif defined HAVE_CSTRIPES
|
|
|
|
|
|
|
|
return; // nothing needed
|
|
|
|
|
|
|
|
#elif defined HAVE_OPENMP
|
|
|
|
|
|
|
|
if(omp_in_parallel())
|
|
|
|
return; // can't change number of openmp threads inside a parallel region
|
|
|
|
|
|
|
|
omp_set_num_threads(threads > 0 ? threads : numThreadsMax);
|
|
|
|
|
|
|
|
#elif defined HAVE_GCD
|
|
|
|
|
|
|
|
// unsupported
|
|
|
|
// there is only private dispatch_queue_set_width() and only for desktop
|
|
|
|
|
2015-03-18 20:13:08 +08:00
|
|
|
#elif defined WINRT
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
2012-10-13 00:06:40 +08:00
|
|
|
#elif defined HAVE_CONCURRENCY
|
|
|
|
|
|
|
|
if (threads <= 0)
|
|
|
|
{
|
|
|
|
pplScheduler = 0;
|
|
|
|
}
|
|
|
|
else if (threads == 1)
|
|
|
|
{
|
|
|
|
// Concurrency always uses >=2 threads, so we just disable it if 1 thread is requested
|
|
|
|
numThreads = 0;
|
|
|
|
}
|
|
|
|
else if (pplScheduler == 0 || 1 + pplScheduler->GetNumberOfVirtualProcessors() != (unsigned int)threads)
|
|
|
|
{
|
|
|
|
pplScheduler = Concurrency::Scheduler::Create(Concurrency::SchedulerPolicy(2,
|
2012-10-22 22:46:26 +08:00
|
|
|
Concurrency::MinConcurrency, threads-1,
|
|
|
|
Concurrency::MaxConcurrency, threads-1));
|
2012-10-13 00:06:40 +08:00
|
|
|
}
|
|
|
|
|
2015-06-11 21:53:07 +08:00
|
|
|
#elif defined HAVE_PTHREADS_PF
|
2015-05-30 02:58:45 +08:00
|
|
|
|
|
|
|
parallel_pthreads_set_threads_num(threads);
|
|
|
|
|
2012-10-12 18:49:22 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int cv::getThreadNum(void)
|
|
|
|
{
|
2012-10-13 00:06:40 +08:00
|
|
|
#if defined HAVE_TBB
|
|
|
|
#if TBB_INTERFACE_VERSION >= 6100 && defined TBB_PREVIEW_TASK_ARENA && TBB_PREVIEW_TASK_ARENA
|
|
|
|
return tbb::task_arena::current_slot();
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
#elif defined HAVE_CSTRIPES
|
|
|
|
return pix();
|
|
|
|
#elif defined HAVE_OPENMP
|
2012-10-12 18:49:22 +08:00
|
|
|
return omp_get_thread_num();
|
2012-10-13 00:06:40 +08:00
|
|
|
#elif defined HAVE_GCD
|
2012-10-15 17:55:32 +08:00
|
|
|
return (int)(size_t)(void*)pthread_self(); // no zero-based indexing
|
2015-03-18 20:13:08 +08:00
|
|
|
#elif defined WINRT
|
|
|
|
return 0;
|
2012-10-13 00:06:40 +08:00
|
|
|
#elif defined HAVE_CONCURRENCY
|
|
|
|
return std::max(0, (int)Concurrency::Context::VirtualProcessorId()); // zero for master thread, unique number for others but not necessary 1,2,3,...
|
2015-06-11 21:53:07 +08:00
|
|
|
#elif defined HAVE_PTHREADS_PF
|
|
|
|
return (int)(size_t)(void*)pthread_self(); // no zero-based indexing
|
2012-10-12 18:49:22 +08:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
static inline int getNumberOfCPUsImpl()
|
|
|
|
{
|
|
|
|
FILE* cpuPossible = fopen("/sys/devices/system/cpu/possible", "r");
|
|
|
|
if(!cpuPossible)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
char buf[2000]; //big enough for 1000 CPUs in worst possible configuration
|
|
|
|
char* pbuf = fgets(buf, sizeof(buf), cpuPossible);
|
|
|
|
fclose(cpuPossible);
|
|
|
|
if(!pbuf)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
//parse string of form "0-1,3,5-7,10,13-15"
|
|
|
|
int cpusAvailable = 0;
|
|
|
|
|
|
|
|
while(*pbuf)
|
|
|
|
{
|
|
|
|
const char* pos = pbuf;
|
|
|
|
bool range = false;
|
|
|
|
while(*pbuf && *pbuf != ',')
|
|
|
|
{
|
|
|
|
if(*pbuf == '-') range = true;
|
|
|
|
++pbuf;
|
|
|
|
}
|
|
|
|
if(*pbuf) *pbuf++ = 0;
|
|
|
|
if(!range)
|
|
|
|
++cpusAvailable;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int rstart = 0, rend = 0;
|
|
|
|
sscanf(pos, "%d-%d", &rstart, &rend);
|
|
|
|
cpusAvailable += rend - rstart + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return cpusAvailable ? cpusAvailable : 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int cv::getNumberOfCPUs(void)
|
|
|
|
{
|
|
|
|
#if defined WIN32 || defined _WIN32
|
|
|
|
SYSTEM_INFO sysinfo;
|
2015-02-21 00:47:45 +08:00
|
|
|
#if defined(_M_ARM) || defined(_M_X64) || defined(WINRT)
|
2013-07-19 17:43:05 +08:00
|
|
|
GetNativeSystemInfo( &sysinfo );
|
|
|
|
#else
|
2012-10-12 18:49:22 +08:00
|
|
|
GetSystemInfo( &sysinfo );
|
2013-07-19 17:43:05 +08:00
|
|
|
#endif
|
2012-10-12 18:49:22 +08:00
|
|
|
|
|
|
|
return (int)sysinfo.dwNumberOfProcessors;
|
|
|
|
#elif defined ANDROID
|
|
|
|
static int ncpus = getNumberOfCPUsImpl();
|
|
|
|
return ncpus;
|
|
|
|
#elif defined __linux__
|
|
|
|
return (int)sysconf( _SC_NPROCESSORS_ONLN );
|
|
|
|
#elif defined __APPLE__
|
|
|
|
int numCPU=0;
|
|
|
|
int mib[4];
|
|
|
|
size_t len = sizeof(numCPU);
|
|
|
|
|
|
|
|
/* set the mib for hw.ncpu */
|
|
|
|
mib[0] = CTL_HW;
|
|
|
|
mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
|
|
|
|
|
|
|
|
/* get the number of CPUs from the system */
|
|
|
|
sysctl(mib, 2, &numCPU, &len, NULL, 0);
|
|
|
|
|
|
|
|
if( numCPU < 1 )
|
|
|
|
{
|
|
|
|
mib[1] = HW_NCPU;
|
|
|
|
sysctl( mib, 2, &numCPU, &len, NULL, 0 );
|
|
|
|
|
|
|
|
if( numCPU < 1 )
|
|
|
|
numCPU = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int)numCPU;
|
|
|
|
#else
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:43:16 +08:00
|
|
|
const char* cv::currentParallelFramework() {
|
|
|
|
#ifdef CV_PARALLEL_FRAMEWORK
|
|
|
|
return CV_PARALLEL_FRAMEWORK;
|
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-10-12 18:49:22 +08:00
|
|
|
CV_IMPL void cvSetNumThreads(int nt)
|
|
|
|
{
|
|
|
|
cv::setNumThreads(nt);
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL int cvGetNumThreads()
|
|
|
|
{
|
|
|
|
return cv::getNumThreads();
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL int cvGetThreadNum()
|
|
|
|
{
|
|
|
|
return cv::getThreadNum();
|
2013-02-04 21:34:18 +08:00
|
|
|
}
|