Skip to content

Commit 6be2572

Browse files
committed
ocl: refactor program compilation
1 parent 04b4495 commit 6be2572

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,10 @@ class CV_EXPORTS Program
613613
String getPrefix() const;
614614
static String getPrefix(const String& buildflags);
615615

616-
protected:
616+
617617
struct Impl;
618+
inline Impl* getImpl() const { return (Impl*)p; }
619+
protected:
618620
Impl* p;
619621
};
620622

@@ -635,8 +637,9 @@ class CV_EXPORTS ProgramSource
635637
const String& source() const;
636638
hash_t hash() const; // deprecated
637639

638-
protected:
639640
struct Impl;
641+
inline Impl* getImpl() const { return (Impl*)p; }
642+
protected:
640643
Impl* p;
641644
};
642645

modules/core/src/ocl.cpp

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,20 +2618,27 @@ internal::ProgramEntry::operator ProgramSource&() const
26182618
struct Program::Impl
26192619
{
26202620
Impl(const ProgramSource& _src,
2621-
const String& _buildflags, String& errmsg)
2621+
const String& _buildflags, String& errmsg) :
2622+
src(_src),
2623+
buildflags(_buildflags),
2624+
handle(NULL)
26222625
{
2623-
CV_INSTRUMENT_REGION_OPENCL_COMPILE(cv::format("Compile: %" PRIx64 " options: %s", _src.hash(), _buildflags.c_str()).c_str());
26242626
refcount = 1;
2625-
const Context& ctx = Context::getDefault();
2626-
src = _src;
2627-
buildflags = _buildflags;
2627+
compile(Context::getDefault(), errmsg);
2628+
}
2629+
2630+
bool compile(const Context& ctx, String& errmsg)
2631+
{
2632+
CV_Assert(handle == NULL);
2633+
CV_INSTRUMENT_REGION_OPENCL_COMPILE(cv::format("Compile: %" PRIx64 " options: %s", src.hash(), buildflags.c_str()).c_str());
26282634
const String& srcstr = src.source();
26292635
const char* srcptr = srcstr.c_str();
26302636
size_t srclen = srcstr.size();
26312637
cl_int retval = 0;
26322638

26332639
handle = clCreateProgramWithSource((cl_context)ctx.ptr(), 1, &srcptr, &srclen, &retval);
2634-
if( handle && retval == CL_SUCCESS )
2640+
CV_OclDbgAssert(handle && retval == CL_SUCCESS);
2641+
if (handle && retval == CL_SUCCESS)
26352642
{
26362643
int i, n = (int)ctx.ndevices();
26372644
AutoBuffer<void*> deviceListBuf(n+1);
@@ -2649,33 +2656,49 @@ struct Program::Impl
26492656
(const cl_device_id*)deviceList,
26502657
buildflags.c_str(), 0, 0);
26512658
#if !CV_OPENCL_ALWAYS_SHOW_BUILD_LOG
2652-
if( retval != CL_SUCCESS )
2659+
if (retval != CL_SUCCESS)
26532660
#endif
26542661
{
2662+
AutoBuffer<char, 4096> buffer; buffer[0] = 0;
2663+
26552664
size_t retsz = 0;
2656-
cl_int buildInfo_retval = clGetProgramBuildInfo(handle, (cl_device_id)deviceList[0],
2657-
CL_PROGRAM_BUILD_LOG, 0, 0, &retsz);
2658-
if (buildInfo_retval == CL_SUCCESS && retsz > 1)
2665+
cl_int log_retval = clGetProgramBuildInfo(handle, (cl_device_id)deviceList[0],
2666+
CL_PROGRAM_BUILD_LOG, 0, 0, &retsz);
2667+
if (log_retval == CL_SUCCESS && retsz > 1)
26592668
{
2660-
AutoBuffer<char> bufbuf(retsz + 16);
2661-
char* buf = bufbuf;
2662-
buildInfo_retval = clGetProgramBuildInfo(handle, (cl_device_id)deviceList[0],
2663-
CL_PROGRAM_BUILD_LOG, retsz+1, buf, &retsz);
2664-
if (buildInfo_retval == CL_SUCCESS)
2669+
buffer.resize(retsz + 16);
2670+
log_retval = clGetProgramBuildInfo(handle, (cl_device_id)deviceList[0],
2671+
CL_PROGRAM_BUILD_LOG, retsz+1, (char*)buffer, &retsz);
2672+
if (log_retval == CL_SUCCESS)
2673+
{
2674+
if (retsz < buffer.size())
2675+
buffer[retsz] = 0;
2676+
else
2677+
buffer[buffer.size() - 1] = 0;
2678+
}
2679+
else
26652680
{
2666-
// TODO It is useful to see kernel name & program file name also
2667-
errmsg = String(buf);
2668-
printf("OpenCL program build log: %s\n%s\n", buildflags.c_str(), errmsg.c_str());
2669-
fflush(stdout);
2681+
buffer[0] = 0;
26702682
}
26712683
}
2684+
2685+
errmsg = String(buffer);
2686+
printf("OpenCL program build log: %s (%s)\nStatus %d: %s\n%s\n%s\n",
2687+
src.getImpl()->name_.c_str(), src.getImpl()->module_.c_str(),
2688+
retval, getOpenCLErrorString(retval),
2689+
buildflags.c_str(), errmsg.c_str());
2690+
fflush(stdout);
2691+
2692+
// don't remove "retval != CL_SUCCESS" condition here:
2693+
// it would break CV_OPENCL_ALWAYS_SHOW_BUILD_LOG mode
26722694
if (retval != CL_SUCCESS && handle)
26732695
{
26742696
clReleaseProgram(handle);
26752697
handle = NULL;
26762698
}
26772699
}
26782700
}
2701+
return handle != NULL;
26792702
}
26802703

26812704
Impl(const String& _buf, const String& _buildflags)

0 commit comments

Comments
 (0)