Skip to content

Commit d9ab314

Browse files
committed
ocl: profiling queue
1 parent 6a5298a commit d9ab314

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,12 @@ class CV_EXPORTS Queue
333333
void* ptr() const;
334334
static Queue& getDefault();
335335

336+
/// @brief Returns OpenCL command queue with enable profiling mode support
337+
const Queue& getProfilingQueue() const;
338+
339+
struct Impl; friend struct Impl;
340+
inline Impl* getImpl() const { return p; }
336341
protected:
337-
struct Impl;
338342
Impl* p;
339343
};
340344

modules/core/src/ocl.cpp

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,9 +1840,35 @@ void initializeContextFromHandle(Context& ctx, void* platform, void* _context, v
18401840

18411841
struct Queue::Impl
18421842
{
1843-
Impl(const Context& c, const Device& d)
1843+
inline void __init()
18441844
{
18451845
refcount = 1;
1846+
handle = 0;
1847+
isProfilingQueue_ = false;
1848+
}
1849+
1850+
Impl(cl_command_queue q)
1851+
{
1852+
__init();
1853+
handle = q;
1854+
1855+
cl_command_queue_properties props = 0;
1856+
cl_int result = clGetCommandQueueInfo(handle, CL_QUEUE_PROPERTIES, sizeof(cl_command_queue_properties), &props, NULL);
1857+
CV_Assert(result && "clGetCommandQueueInfo(CL_QUEUE_PROPERTIES)");
1858+
isProfilingQueue_ = !!(props & CL_QUEUE_PROFILING_ENABLE);
1859+
}
1860+
1861+
Impl(cl_command_queue q, bool isProfilingQueue)
1862+
{
1863+
__init();
1864+
handle = q;
1865+
isProfilingQueue_ = isProfilingQueue;
1866+
}
1867+
1868+
Impl(const Context& c, const Device& d, bool withProfiling = false)
1869+
{
1870+
__init();
1871+
18461872
const Context* pc = &c;
18471873
cl_context ch = (cl_context)pc->ptr();
18481874
if( !ch )
@@ -1854,8 +1880,10 @@ struct Queue::Impl
18541880
if( !dh )
18551881
dh = (cl_device_id)pc->device(0).ptr();
18561882
cl_int retval = 0;
1857-
handle = clCreateCommandQueue(ch, dh, 0, &retval);
1883+
cl_command_queue_properties props = withProfiling ? CL_QUEUE_PROFILING_ENABLE : 0;
1884+
handle = clCreateCommandQueue(ch, dh, props, &retval);
18581885
CV_OclDbgAssert(retval == CL_SUCCESS);
1886+
isProfilingQueue_ = withProfiling;
18591887
}
18601888

18611889
~Impl()
@@ -1873,9 +1901,37 @@ struct Queue::Impl
18731901
}
18741902
}
18751903

1904+
const cv::ocl::Queue& getProfilingQueue(const cv::ocl::Queue& self)
1905+
{
1906+
if (isProfilingQueue_)
1907+
return self;
1908+
1909+
if (profiling_queue_.ptr())
1910+
return profiling_queue_;
1911+
1912+
cl_context ctx = 0;
1913+
CV_Assert(CL_SUCCESS == clGetCommandQueueInfo(handle, CL_QUEUE_CONTEXT, sizeof(cl_context), &ctx, NULL));
1914+
1915+
cl_device_id device = 0;
1916+
CV_Assert(CL_SUCCESS == clGetCommandQueueInfo(handle, CL_QUEUE_DEVICE, sizeof(cl_device_id), &device, NULL));
1917+
1918+
cl_int result = CL_SUCCESS;
1919+
cl_command_queue_properties props = CL_QUEUE_PROFILING_ENABLE;
1920+
cl_command_queue q = clCreateCommandQueue(ctx, device, props, &result);
1921+
CV_Assert(result == CL_SUCCESS && "clCreateCommandQueue(with CL_QUEUE_PROFILING_ENABLE)");
1922+
1923+
Queue queue;
1924+
queue.p = new Impl(q, true);
1925+
profiling_queue_ = queue;
1926+
1927+
return profiling_queue_;
1928+
}
1929+
18761930
IMPLEMENT_REFCOUNTABLE();
18771931

18781932
cl_command_queue handle;
1933+
bool isProfilingQueue_;
1934+
cv::ocl::Queue profiling_queue_;
18791935
};
18801936

18811937
Queue::Queue()
@@ -1929,6 +1985,12 @@ void Queue::finish()
19291985
}
19301986
}
19311987

1988+
const Queue& Queue::getProfilingQueue() const
1989+
{
1990+
CV_Assert(p);
1991+
return p->getProfilingQueue(*this);
1992+
}
1993+
19321994
void* Queue::ptr() const
19331995
{
19341996
return p ? p->handle : 0;

0 commit comments

Comments
 (0)