Skip to content

Commit 7a95e65

Browse files
committed
ocl: update compiled programs
- minimize library initialization time (lazy calculations of program hash) - LRU cache of in-memory compiled programs
1 parent 05d187e commit 7a95e65

File tree

4 files changed

+141
-61
lines changed

4 files changed

+141
-61
lines changed

cmake/cl2cpp.cmake

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,8 @@ endif()
1212
string(REPLACE ".cpp" ".hpp" OUTPUT_HPP "${OUTPUT}")
1313
get_filename_component(OUTPUT_HPP_NAME "${OUTPUT_HPP}" NAME)
1414

15-
if("${MODULE_NAME}" STREQUAL "ocl")
16-
set(nested_namespace_start "")
17-
set(nested_namespace_end "")
18-
else()
19-
set(new_mode ON)
20-
set(nested_namespace_start "namespace ${MODULE_NAME}\n{")
21-
set(nested_namespace_end "}")
22-
endif()
15+
set(nested_namespace_start "namespace ${MODULE_NAME}\n{")
16+
set(nested_namespace_end "}")
2317

2418
set(STR_CPP "// This file is auto-generated. Do not edit!
2519
@@ -35,6 +29,8 @@ namespace ocl
3529
{
3630
${nested_namespace_start}
3731
32+
static const char* const moduleName = \"${MODULE_NAME}\";
33+
3834
")
3935

4036
set(STR_HPP "// This file is auto-generated. Do not edit!
@@ -76,27 +72,23 @@ foreach(cl ${cl_list})
7672

7773
string(MD5 hash "${lines}")
7874

79-
set(STR_CPP_DECL "const struct ProgramEntry ${cl_filename}={\"${cl_filename}\",\n\"${lines}, \"${hash}\"};\n")
80-
set(STR_HPP_DECL "extern const struct ProgramEntry ${cl_filename};\n")
81-
if(new_mode)
82-
set(STR_CPP_DECL "${STR_CPP_DECL}ProgramSource ${cl_filename}_oclsrc(${cl_filename}.programStr);\n")
83-
set(STR_HPP_DECL "${STR_HPP_DECL}extern ProgramSource ${cl_filename}_oclsrc;\n")
84-
endif()
75+
set(STR_CPP_DECL "struct cv::ocl::internal::ProgramEntry ${cl_filename}_oclsrc={moduleName, \"${cl_filename}\",\n\"${lines}, \"${hash}\", NULL};\n")
76+
set(STR_HPP_DECL "extern struct cv::ocl::internal::ProgramEntry ${cl_filename}_oclsrc;\n")
8577

8678
set(STR_CPP "${STR_CPP}${STR_CPP_DECL}")
8779
set(STR_HPP "${STR_HPP}${STR_HPP_DECL}")
8880
endforeach()
8981

90-
set(STR_CPP "${STR_CPP}}\n${nested_namespace_end}}\n#endif\n")
91-
set(STR_HPP "${STR_HPP}}\n${nested_namespace_end}}\n#endif\n")
82+
set(STR_CPP "${STR_CPP}\n${nested_namespace_end}}}\n#endif\n")
83+
set(STR_HPP "${STR_HPP}\n${nested_namespace_end}}}\n#endif\n")
9284

9385
file(WRITE "${OUTPUT}" "${STR_CPP}")
9486

9587
if(EXISTS "${OUTPUT_HPP}")
9688
file(READ "${OUTPUT_HPP}" hpp_lines)
9789
endif()
9890
if("${hpp_lines}" STREQUAL "${STR_HPP}")
99-
message(STATUS "${OUTPUT_HPP} contains same content")
91+
message(STATUS "${OUTPUT_HPP} contains the same content")
10092
else()
10193
file(WRITE "${OUTPUT_HPP}" "${STR_HPP}")
10294
endif()

modules/core/include/opencv2/core/ocl.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,17 +627,18 @@ class CV_EXPORTS Program
627627
class CV_EXPORTS ProgramSource
628628
{
629629
public:
630-
typedef uint64 hash_t;
630+
typedef uint64 hash_t; // deprecated
631631

632632
ProgramSource();
633-
explicit ProgramSource(const String& prog);
634-
explicit ProgramSource(const char* prog);
633+
explicit ProgramSource(const String& module, const String& name, const String& codeStr, const String& codeHash);
634+
explicit ProgramSource(const String& prog); // deprecated
635+
explicit ProgramSource(const char* prog); // deprecated
635636
~ProgramSource();
636637
ProgramSource(const ProgramSource& prog);
637638
ProgramSource& operator = (const ProgramSource& prog);
638639

639640
const String& source() const;
640-
hash_t hash() const;
641+
hash_t hash() const; // deprecated
641642

642643
protected:
643644
struct Impl;

modules/core/include/opencv2/core/ocl_genbase.hpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,28 @@
4242
#ifndef OPENCV_OPENCL_GENBASE_HPP
4343
#define OPENCV_OPENCL_GENBASE_HPP
4444

45-
namespace cv
46-
{
47-
namespace ocl
48-
{
49-
5045
//! @cond IGNORED
5146

52-
struct ProgramEntry
47+
namespace cv {
48+
namespace ocl {
49+
50+
class ProgramSource;
51+
52+
namespace internal {
53+
54+
struct CV_EXPORTS ProgramEntry
5355
{
56+
const char* module;
5457
const char* name;
55-
const char* programStr;
58+
const char* programCode;
5659
const char* programHash;
60+
ProgramSource* pProgramSource;
61+
62+
operator ProgramSource& () const;
5763
};
5864

59-
//! @endcond
65+
} } } // namespace
6066

61-
}
62-
}
67+
//! @endcond
6368

6469
#endif

modules/core/src/ocl.cpp

Lines changed: 112 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
#include <inttypes.h>
5050
#endif
5151

52+
#include "opencv2/core/ocl_genbase.hpp"
53+
5254
#define CV_OPENCL_ALWAYS_SHOW_BUILD_LOG 0
5355
#define CV_OPENCL_SHOW_RUN_ERRORS 0
5456
#define CV_OPENCL_SHOW_SVM_ERROR_LOG 1
@@ -1259,6 +1261,18 @@ static unsigned int getSVMCapabilitiesMask()
12591261
} // namespace
12601262
#endif
12611263

1264+
static size_t getProgramCountLimit()
1265+
{
1266+
static bool initialized = false;
1267+
static size_t count = 0;
1268+
if (!initialized)
1269+
{
1270+
count = getConfigurationParameterForSize("OPENCV_OPENCL_PROGRAM_CACHE", 64);
1271+
initialized = true;
1272+
}
1273+
return count;
1274+
}
1275+
12621276
struct Context::Impl
12631277
{
12641278
static Context::Impl* get(Context& context) { return context.p; }
@@ -1378,35 +1392,57 @@ struct Context::Impl
13781392
Program getProg(const ProgramSource& src,
13791393
const String& buildflags, String& errmsg)
13801394
{
1381-
String prefix = Program::getPrefix(buildflags);
1382-
HashKey k(src.hash(), crc64((const uchar*)prefix.c_str(), prefix.size()));
1383-
phash_t::iterator it = phash.find(k);
1384-
if( it != phash.end() )
1385-
return it->second;
1386-
//String filename = format("%08x%08x_%08x%08x.clb2",
1395+
size_t limit = getProgramCountLimit();
1396+
String key = Program::getPrefix(buildflags);
1397+
{
1398+
cv::AutoLock lock(program_cache_mutex);
1399+
phash_t::iterator it = phash.find(key);
1400+
if (it != phash.end())
1401+
{
1402+
// TODO LRU cache
1403+
CacheList::iterator i = std::find(cacheList.begin(), cacheList.end(), key);
1404+
if (i != cacheList.end() && i != cacheList.begin())
1405+
{
1406+
cacheList.erase(i);
1407+
cacheList.push_front(key);
1408+
}
1409+
return it->second;
1410+
}
1411+
{ // cleanup program cache
1412+
size_t sz = phash.size();
1413+
if (limit > 0 && sz >= limit)
1414+
{
1415+
while (!cacheList.empty())
1416+
{
1417+
size_t c = phash.erase(cacheList.back());
1418+
cacheList.pop_back();
1419+
if (c != 0)
1420+
break;
1421+
}
1422+
}
1423+
}
1424+
}
13871425
Program prog(src, buildflags, errmsg);
13881426
if(prog.ptr())
1389-
phash.insert(std::pair<HashKey,Program>(k, prog));
1427+
{
1428+
cv::AutoLock lock(program_cache_mutex);
1429+
phash.insert(std::pair<std::string, Program>(key, prog));
1430+
cacheList.push_front(key);
1431+
}
13901432
return prog;
13911433
}
13921434

1435+
13931436
IMPLEMENT_REFCOUNTABLE();
13941437

13951438
cl_context handle;
13961439
std::vector<Device> devices;
13971440

1398-
typedef ProgramSource::hash_t hash_t;
1399-
1400-
struct HashKey
1401-
{
1402-
HashKey(hash_t _a, hash_t _b) : a(_a), b(_b) {}
1403-
bool operator < (const HashKey& k) const { return a < k.a || (a == k.a && b < k.b); }
1404-
bool operator == (const HashKey& k) const { return a == k.a && b == k.b; }
1405-
bool operator != (const HashKey& k) const { return a != k.a || b != k.b; }
1406-
hash_t a, b;
1407-
};
1408-
typedef std::map<HashKey, Program> phash_t;
1441+
cv::Mutex program_cache_mutex;
1442+
typedef std::map<std::string, Program> phash_t;
14091443
phash_t phash;
1444+
typedef std::list<cv::String> CacheList;
1445+
CacheList cacheList;
14101446

14111447
#ifdef HAVE_OPENCL_SVM
14121448
bool svmInitialized;
@@ -2580,24 +2616,46 @@ String Program::getPrefix(const String& buildflags)
25802616

25812617
struct ProgramSource::Impl
25822618
{
2583-
Impl(const char* _src)
2619+
Impl(const String& src)
25842620
{
2585-
init(String(_src));
2621+
init(cv::String(), cv::String(), src, cv::String());
25862622
}
2587-
Impl(const String& _src)
2623+
Impl(const String& module, const String& name, const String& codeStr, const String& codeHash)
25882624
{
2589-
init(_src);
2625+
init(module, name, codeStr, codeHash);
25902626
}
2591-
void init(const String& _src)
2627+
void init(const String& module, const String& name, const String& codeStr, const String& codeHash)
25922628
{
25932629
refcount = 1;
2594-
src = _src;
2595-
h = crc64((uchar*)src.c_str(), src.size());
2630+
module_ = module;
2631+
name_ = name;
2632+
codeStr_ = codeStr;
2633+
codeHash_ = codeHash;
2634+
2635+
isHashUpdated = false;
2636+
if (codeHash_.empty())
2637+
{
2638+
updateHash();
2639+
codeHash_ = cv::format("%08llx", hash_);
2640+
}
2641+
}
2642+
2643+
void updateHash()
2644+
{
2645+
hash_ = crc64((uchar*)codeStr_.c_str(), codeStr_.size());
2646+
isHashUpdated = true;
25962647
}
25972648

25982649
IMPLEMENT_REFCOUNTABLE();
2599-
String src;
2600-
ProgramSource::hash_t h;
2650+
2651+
String module_;
2652+
String name_;
2653+
String codeStr_;
2654+
String codeHash_;
2655+
// TODO std::vector<ProgramSource> includes_;
2656+
2657+
bool isHashUpdated;
2658+
ProgramSource::hash_t hash_;
26012659
};
26022660

26032661

@@ -2606,6 +2664,11 @@ ProgramSource::ProgramSource()
26062664
p = 0;
26072665
}
26082666

2667+
ProgramSource::ProgramSource(const String& module, const String& name, const String& codeStr, const String& codeHash)
2668+
{
2669+
p = new Impl(module, name, codeStr, codeHash);
2670+
}
2671+
26092672
ProgramSource::ProgramSource(const char* prog)
26102673
{
26112674
p = new Impl(prog);
@@ -2642,15 +2705,34 @@ ProgramSource& ProgramSource::operator = (const ProgramSource& prog)
26422705

26432706
const String& ProgramSource::source() const
26442707
{
2645-
static String dummy;
2646-
return p ? p->src : dummy;
2708+
CV_Assert(p);
2709+
return p->codeStr_;
26472710
}
26482711

26492712
ProgramSource::hash_t ProgramSource::hash() const
26502713
{
2651-
return p ? p->h : 0;
2714+
CV_Assert(p);
2715+
if (!p->isHashUpdated)
2716+
p->updateHash();
2717+
return p->hash_;
26522718
}
26532719

2720+
2721+
internal::ProgramEntry::operator ProgramSource&() const
2722+
{
2723+
if (this->pProgramSource == NULL)
2724+
{
2725+
cv::AutoLock lock(cv::getInitializationMutex());
2726+
if (this->pProgramSource == NULL)
2727+
{
2728+
ProgramSource* ps = new ProgramSource(this->module, this->name, this->programCode, this->programHash);
2729+
const_cast<ProgramEntry*>(this)->pProgramSource = ps;
2730+
}
2731+
}
2732+
return *this->pProgramSource;
2733+
}
2734+
2735+
26542736
//////////////////////////////////////////// OpenCLAllocator //////////////////////////////////////////////////
26552737

26562738
template<typename T>

0 commit comments

Comments
 (0)