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

Merge pull request #10180 from alalek:ocl_avoid_unnecessary_initialization

This commit is contained in:
Vadim Pisarevsky
2017-11-29 11:42:22 +00:00
20 changed files with 114 additions and 64 deletions
@@ -5,7 +5,22 @@
// Copyright (C) 2014, Advanced Micro Devices, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
#ifndef OPENCV_CORE_OPENCL_DEFS_HPP
#define OPENCV_CORE_OPENCL_DEFS_HPP
#include "opencv2/core/utility.hpp"
#include "cvconfig.h"
namespace cv { namespace ocl {
#ifdef HAVE_OPENCL
/// Call is similar to useOpenCL() but doesn't try to load OpenCL runtime or create OpenCL context
CV_EXPORTS bool isOpenCLActivated();
#else
static inline bool isOpenCLActivated() { return false; }
#endif
}} // namespace
//#define CV_OPENCL_RUN_ASSERT
#ifdef HAVE_OPENCL
@@ -13,7 +28,7 @@
#ifdef CV_OPENCL_RUN_VERBOSE
#define CV_OCL_RUN_(condition, func, ...) \
{ \
if (cv::ocl::useOpenCL() && (condition) && func) \
if (cv::ocl::isOpenCLActivated() && (condition) && func) \
{ \
printf("%s: OpenCL implementation is running\n", CV_Func); \
fflush(stdout); \
@@ -29,7 +44,7 @@
#elif defined CV_OPENCL_RUN_ASSERT
#define CV_OCL_RUN_(condition, func, ...) \
{ \
if (cv::ocl::useOpenCL() && (condition)) \
if (cv::ocl::isOpenCLActivated() && (condition)) \
{ \
if(func) \
{ \
@@ -44,7 +59,7 @@
}
#else
#define CV_OCL_RUN_(condition, func, ...) \
if (cv::ocl::useOpenCL() && (condition) && func) \
if (cv::ocl::isOpenCLActivated() && (condition) && func) \
{ \
CV_IMPL_ADD(CV_IMPL_OCL); \
return __VA_ARGS__; \
@@ -56,3 +71,5 @@
#endif
#define CV_OCL_RUN(condition, func) CV_OCL_RUN_(condition, func)
#endif // OPENCV_CORE_OPENCL_DEFS_HPP
+7 -2
View File
@@ -44,6 +44,7 @@
#include "precomp.hpp"
#include "opencl_kernels_core.hpp"
#include "convert.hpp"
#include "opencv2/core/openvx/ovx_defs.hpp"
@@ -897,7 +898,8 @@ void cv::extractChannel(InputArray _src, OutputArray _dst, int coi)
CV_Assert( 0 <= coi && coi < cn );
int ch[] = { coi, 0 };
if (ocl::useOpenCL() && _src.dims() <= 2 && _dst.isUMat())
#ifdef HAVE_OPENCL
if (ocl::isOpenCLActivated() && _src.dims() <= 2 && _dst.isUMat())
{
UMat src = _src.getUMat();
_dst.create(src.dims, &src.size[0], depth);
@@ -905,6 +907,7 @@ void cv::extractChannel(InputArray _src, OutputArray _dst, int coi)
mixChannels(std::vector<UMat>(1, src), std::vector<UMat>(1, dst), ch, 1);
return;
}
#endif
Mat src = _src.getMat();
_dst.create(src.dims, &src.size[0], depth);
@@ -925,12 +928,14 @@ void cv::insertChannel(InputArray _src, InputOutputArray _dst, int coi)
CV_Assert( 0 <= coi && coi < dcn && scn == 1 );
int ch[] = { 0, coi };
if (ocl::useOpenCL() && _src.dims() <= 2 && _dst.isUMat())
#ifdef HAVE_OPENCL
if (ocl::isOpenCLActivated() && _src.dims() <= 2 && _dst.isUMat())
{
UMat src = _src.getUMat(), dst = _dst.getUMat();
mixChannels(std::vector<UMat>(1, src), std::vector<UMat>(1, dst), ch, 1);
return;
}
#endif
Mat src = _src.getMat(), dst = _dst.getMat();
+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;
}
}
@@ -5289,9 +5320,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
+3 -1
View File
@@ -8,6 +8,8 @@
#include <opencv2/core/utils/trace.private.hpp>
#include <opencv2/core/utils/configuration.private.hpp>
#include <opencv2/core/opencl/ocl_defs.hpp>
#include <cstdarg> // va_start
#include <sstream>
@@ -596,7 +598,7 @@ void Region::destroy()
#endif
#ifdef HAVE_OPENCL
case REGION_FLAG_IMPL_OPENCL:
if (param_synchronizeOpenCL && cv::ocl::useOpenCL())
if (param_synchronizeOpenCL && cv::ocl::isOpenCLActivated())
cv::ocl::finish();
myCodePath = Impl::CODE_PATH_OPENCL;
break;
+1 -1
View File
@@ -141,7 +141,7 @@ void UMatData::unlock()
MatAllocator* UMat::getStdAllocator()
{
#ifdef HAVE_OPENCL
if( ocl::haveOpenCL() && ocl::useOpenCL() )
if (ocl::useOpenCL())
return ocl::getOpenCLAllocator();
#endif
return Mat::getDefaultAllocator();