-
-
Notifications
You must be signed in to change notification settings - Fork 56.5k
conflict assignment of g_isOpenCVActivated in OpenCL subsystem #19132
Description
The variable g_isOpenCVActivated is inconsistently used/assigned and conflicts in its intention. The variable only exists in ocl.cpp on 4 lines. The two lines in conflict are both assigning the value true out-of-order and with conflicting intentions. I also think the variable is misnamed -- if I understand the intention, it should be named g_isOpenCLActivated
System information (version)
- OpenCV => 4.5.0
- Operating System / Platform => Windows 10 x64
- Compiler => Visual Studio Community 2019 v16.8.3
Detailed description
The declaration of the variable is clear on its intention, see below
opencv/modules/core/src/ocl.cpp
Lines 1148 to 1149 in d5fd2f0
| // true if we have initialized OpenCL subsystem with available platforms | |
| static bool g_isOpenCVActivated = false; |
The code then correctly assigned the variable matching that intention, see below
opencv/modules/core/src/ocl.cpp
Lines 1175 to 1178 in d5fd2f0
| cl_uint n = 0; | |
| g_isOpenCLAvailable = ::clGetPlatformIDs(0, NULL, &n) == CL_SUCCESS; | |
| g_isOpenCVActivated = n > 0; | |
| CV_LOG_INFO(NULL, "OpenCL: found " << n << " platforms"); |
A wrapper function then uses the variable as an optimization. Note the mis-naming... it is not a code defect. Instead, it is a readability/comprehension defect. The variable should be named g_isOpenCLActivated
opencv/modules/core/src/ocl.cpp
Lines 1211 to 1216 in d5fd2f0
| bool isOpenCLActivated() | |
| { | |
| if (!g_isOpenCVActivated) | |
| return false; // prevent unnecessary OpenCL activation via useOpenCL()->haveOpenCL() calls | |
| return useOpenCL(); | |
| } |
Finally, the defective code is below. It does not match the intention of the variable and also assigns true to the variable far before it should in the sequence of codepaths. I believe the assignment should be completely removed here.
opencv/modules/core/src/ocl.cpp
Lines 6346 to 6351 in d5fd2f0
| static OpenCLAllocator* getOpenCLAllocator_() // call once guarantee | |
| { | |
| static OpenCLAllocator* g_allocator = new OpenCLAllocator(); // avoid destructor call (using of this object is too wide) | |
| g_isOpenCVActivated = true; | |
| return g_allocator; | |
| } |
Steps to reproduce
- Code review as described above
Fix
- Remove the assignment on line 6349
- Rename variable in the 4 places from
g_isOpenCVActivatedtog_isOpenCLActivated
Issue submission checklist
- I report the issue, it's not a question
- I checked the problem with documentation, FAQ, open issues,
answers.opencv.org, Stack Overflow, etc and have not found solution - I updated to latest OpenCV version and the issue is still there
- There is reproducer code and related data files: videos, images, onnx, etc