mattrussmill/qcvTouchUp

OpenCV first OpenCL call is SLOW

Closed this issue · 2 comments

When using UMat (which implicitly uses OpenCL in OpenCV3+) the first call to the OpenCL functionality is painfully slow. Every subsequent call executes at the correct time. Through searching I have found that OpenCL code will be build during runtime (at the first function call) due to the "Dynamic loading of OpenCL library" (see cv::getBuildInformation() in OpenCV). When using OpenCL enabling code, your OpenCL device needs an initialization step. This step is done the first time an OpenCL command is given and cannot be avoided. Hence a common practice for OpenCL enabling applications is to run OpenCL code at startup and then start the actual application.

For the qcvTouchUp application, this initialization step must take place to alleviate this issue.

Try adding an initialization sequence in the worker thread upon creation that calls the openCL functions with a tmp UMat before setting the necessary buffers to nullptr?

Resolving this step involved an additional initialization step in the worker thread object constructor which calls OpenCL functions within that object during initialization to construct the GPU objects ahead of use with an image. This must be done with all OpenCL calls (OpenCV calls using a UMat) required. This is split up over several worker objects to avoid long loading times.

Note: if the following warning appears during the initialization stage, it does not matter. The object being used can be thrown away and the data is not useful.

! OPENCV warning: getUMat()/getMat() call chain possible problem. ! Base object is dead, while nested/derived object is still alive or processed. ! Please check lifetime of UMat/Mat objects!