1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 00:03:03 +04:00

ocl: avoid unnecessary loading/initializing OpenCL subsystem

If there are no OpenCL/UMat methods calls from application.

OpenCL subsystem is initialized:
- haveOpenCL() is called from application
- useOpenCL() is called from application
- access to OpenCL allocator: UMat is created (empty UMat is ignored) or UMat <-> Mat conversions are called

Don't call OpenCL functions if OPENCV_OPENCL_RUNTIME=disabled
(independent from OpenCL linkage type)
This commit is contained in:
Alexander Alekhin
2017-11-24 17:34:02 +03:00
parent 92be112388
commit 0ed3209b00
20 changed files with 114 additions and 64 deletions
+43 -6
View File
@@ -423,7 +423,7 @@ struct OpenCLBinaryCacheConfigurator
{
CV_LOG_WARNING(NULL, "- " << remove_entries[i]);
}
CV_LOG_WARNING(NULL,"Note: You can disable this behavior via this option: CV_OPENCL_CACHE_CLEANUP=0");
CV_LOG_WARNING(NULL, "Note: You can disable this behavior via this option: OPENCV_OPENCL_CACHE_CLEANUP=0");
for (size_t i = 0; i < remove_entries.size(); i++)
{
@@ -781,18 +781,34 @@ public:
#endif // OPENCV_HAVE_FILESYSTEM_SUPPORT
// true if we have initialized OpenCL subsystem with available platforms
static bool g_isOpenCVActivated = false;
bool haveOpenCL()
{
CV_TRACE_FUNCTION();
#ifdef HAVE_OPENCL
static bool g_isOpenCLInitialized = false;
static bool g_isOpenCLAvailable = false;
if (!g_isOpenCLInitialized)
{
CV_TRACE_REGION("Init_OpenCL_Runtime");
const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
if (envPath)
{
if (cv::String(envPath) == "disabled")
{
g_isOpenCLAvailable = false;
g_isOpenCLInitialized = true;
}
}
CV_LOG_INFO(NULL, "Initialize OpenCL runtime...");
try
{
cl_uint n = 0;
g_isOpenCLAvailable = ::clGetPlatformIDs(0, NULL, &n) == CL_SUCCESS;
g_isOpenCVActivated = n > 0;
}
catch (...)
{
@@ -813,7 +829,7 @@ bool useOpenCL()
{
try
{
data->useOpenCL = (int)haveOpenCL() && Device::getDefault().ptr() && Device::getDefault().available();
data->useOpenCL = (int)(haveOpenCL() && Device::getDefault().ptr() && Device::getDefault().available()) ? 1 : 0;
}
catch (...)
{
@@ -823,12 +839,27 @@ bool useOpenCL()
return data->useOpenCL > 0;
}
#ifdef HAVE_OPENCL
bool isOpenCLActivated()
{
if (!g_isOpenCVActivated)
return false; // prevent unnecessary OpenCL activation via useOpenCL()->haveOpenCL() calls
return useOpenCL();
}
#endif
void setUseOpenCL(bool flag)
{
if( haveOpenCL() )
CV_TRACE_FUNCTION();
CoreTLSData* data = getCoreTlsData().get();
if (!flag)
{
CoreTLSData* data = getCoreTlsData().get();
data->useOpenCL = (flag && Device::getDefault().ptr() != NULL) ? 1 : 0;
data->useOpenCL = 0;
}
else if( haveOpenCL() )
{
data->useOpenCL = (Device::getDefault().ptr() != NULL) ? 1 : 0;
}
}
@@ -5285,9 +5316,15 @@ public:
}
};
static OpenCLAllocator* getOpenCLAllocator_() // call once guarantee
{
static OpenCLAllocator* g_allocator = new OpenCLAllocator(); // avoid destrutor call (using of this object is too wide)
g_isOpenCVActivated = true;
return g_allocator;
}
MatAllocator* getOpenCLAllocator()
{
CV_SINGLETON_LAZY_INIT(MatAllocator, new OpenCLAllocator())
CV_SINGLETON_LAZY_INIT(MatAllocator, getOpenCLAllocator_())
}
}} // namespace cv::ocl