Commit e77abeef authored by Peng Xiao's avatar Peng Xiao

Add a new global function to control ocl binary storage

Previously the feature is controlled by setBinpath implicitly.
We add the function to cope with setBinpath and setBinpath is only
useful when setBinaryDiskCache is set.
Refer to the header to see more info.
parent 4cf7a963
...@@ -118,9 +118,6 @@ namespace cv ...@@ -118,9 +118,6 @@ namespace cv
//the devnum is the index of the selected device in DeviceName vector of INfo //the devnum is the index of the selected device in DeviceName vector of INfo
CV_EXPORTS void setDevice(Info &oclinfo, int devnum = 0); CV_EXPORTS void setDevice(Info &oclinfo, int devnum = 0);
//optional function, if you want save opencl binary kernel to the file, set its path
CV_EXPORTS void setBinpath(const char *path);
//The two functions below enable other opencl program to use ocl module's cl_context and cl_command_queue //The two functions below enable other opencl program to use ocl module's cl_context and cl_command_queue
//returns cl_context * //returns cl_context *
CV_EXPORTS void* getoclContext(); CV_EXPORTS void* getoclContext();
...@@ -181,6 +178,23 @@ namespace cv ...@@ -181,6 +178,23 @@ namespace cv
bool finish = true, bool measureKernelTime = false, bool finish = true, bool measureKernelTime = false,
bool cleanUp = true); bool cleanUp = true);
//! Enable or disable OpenCL program binary caching onto local disk
// After a program (*.cl files in opencl/ folder) is built at runtime, we allow the compiled program to be
// cached onto local disk automatically, which may accelerate subsequent runs.
// Caching mode is controlled by the following enum
// Note, the feature is by default enabled when OpenCV is built in release mode.
// enum BinaryDiskCacheMode
const int CACHE_NONE = 0;
const int CACHE_DEBUG = 0x1 << 0;
const int CACHE_RELEASE = 0x1 << 1;
const int CACHE_ALL = CACHE_DEBUG | CACHE_RELEASE;
const int CACHE_UPDATE = 0x1 << 2; // if the binary cache file with the same name is already on the disk, it will be updated.
CV_EXPORTS void setBinaryDiskCache(int mode = CACHE_RELEASE, cv::String path = "./");
//! set where binary cache to be saved to
CV_EXPORTS void setBinpath(const char *path);
class CV_EXPORTS oclMatExpr; class CV_EXPORTS oclMatExpr;
//////////////////////////////// oclMat //////////////////////////////// //////////////////////////////// oclMat ////////////////////////////////
class CV_EXPORTS oclMat class CV_EXPORTS oclMat
......
...@@ -124,7 +124,8 @@ namespace cv ...@@ -124,7 +124,8 @@ namespace cv
cacheSize = 0; cacheSize = 0;
} }
// not to be exported to dynamic lib
void setBinaryDiskCacheImpl(int mode, String path, Info::Impl * impl);
struct Info::Impl struct Info::Impl
{ {
cl_platform_id oclplatform; cl_platform_id oclplatform;
...@@ -142,22 +143,12 @@ namespace cv ...@@ -142,22 +143,12 @@ namespace cv
char extra_options[512]; char extra_options[512];
int double_support; int double_support;
int unified_memory; //1 means integrated GPU, otherwise this value is 0 int unified_memory; //1 means integrated GPU, otherwise this value is 0
bool enable_disk_cache;
bool update_disk_cache;
string binpath; string binpath;
int refcounter; int refcounter;
Impl() Impl();
{
refcounter = 1;
oclplatform = 0;
oclcontext = 0;
clCmdQueue = 0;
devnum = -1;
maxComputeUnits = 0;
maxWorkGroupSize = 0;
memset(extra_options, 0, 512);
double_support = 0;
unified_memory = 0;
}
void setDevice(void *ctx, void *q, int devnum); void setDevice(void *ctx, void *q, int devnum);
...@@ -182,6 +173,25 @@ namespace cv ...@@ -182,6 +173,25 @@ namespace cv
void releaseResources(); void releaseResources();
}; };
Info::Impl::Impl()
:oclplatform(0),
oclcontext(0),
clCmdQueue(0),
devnum(-1),
maxWorkGroupSize(0),
maxDimensions(0),
maxComputeUnits(0),
double_support(0),
unified_memory(0),
enable_disk_cache(false),
update_disk_cache(false),
binpath("./"),
refcounter(1)
{
memset(extra_options, 0, 512);
setBinaryDiskCacheImpl(CACHE_RELEASE, String("./"), this);
}
void Info::Impl::releaseResources() void Info::Impl::releaseResources()
{ {
devnum = -1; devnum = -1;
...@@ -494,6 +504,24 @@ namespace cv ...@@ -494,6 +504,24 @@ namespace cv
return openCLGetKernelFromSource(clCxt, source, kernelName, NULL); return openCLGetKernelFromSource(clCxt, source, kernelName, NULL);
} }
void setBinaryDiskCacheImpl(int mode, String path, Info::Impl * impl)
{
impl->update_disk_cache = (mode & CACHE_UPDATE) == CACHE_UPDATE;
impl->enable_disk_cache =
#if !defined(NDEBUG) || defined(_DEBUG)
(mode & CACHE_DEBUG) == CACHE_DEBUG;
#else
(mode & CACHE_RELEASE) == CACHE_RELEASE;
#endif
if(impl->enable_disk_cache && !path.empty())
{
impl->binpath = path;
}
}
void setBinaryDiskCache(int mode, cv::String path)
{
setBinaryDiskCacheImpl(mode, path, Context::getContext()->impl);
}
void setBinpath(const char *path) void setBinpath(const char *path)
{ {
...@@ -573,8 +601,8 @@ namespace cv ...@@ -573,8 +601,8 @@ namespace cv
filename = clCxt->impl->binpath + kernelName + "_" + clCxt->impl->devName[clCxt->impl->devnum] + ".clb"; filename = clCxt->impl->binpath + kernelName + "_" + clCxt->impl->devName[clCxt->impl->devnum] + ".clb";
} }
FILE *fp = fopen(filename.c_str(), "rb"); FILE *fp = clCxt->impl->enable_disk_cache ? fopen(filename.c_str(), "rb") : NULL;
if(fp == NULL || clCxt->impl->binpath.size() == 0) //we should generate a binary file for the first time. if(fp == NULL || clCxt->impl->update_disk_cache)
{ {
if(fp != NULL) if(fp != NULL)
fclose(fp); fclose(fp);
...@@ -583,7 +611,7 @@ namespace cv ...@@ -583,7 +611,7 @@ namespace cv
clCxt->impl->oclcontext, 1, source, NULL, &status); clCxt->impl->oclcontext, 1, source, NULL, &status);
openCLVerifyCall(status); openCLVerifyCall(status);
status = clBuildProgram(program, 1, &(clCxt->impl->devices[clCxt->impl->devnum]), all_build_options, NULL, NULL); status = clBuildProgram(program, 1, &(clCxt->impl->devices[clCxt->impl->devnum]), all_build_options, NULL, NULL);
if(status == CL_SUCCESS && clCxt->impl->binpath.size()) if(status == CL_SUCCESS && clCxt->impl->enable_disk_cache)
savetofile(clCxt, program, filename.c_str()); savetofile(clCxt, program, filename.c_str());
} }
else else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment