Skip to content

conflict assignment of g_isOpenCVActivated in OpenCL subsystem #19132

@diablodale

Description

@diablodale

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

// 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

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

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.

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
  1. Code review as described above
Fix
  • Remove the assignment on line 6349
  • Rename variable in the 4 places from g_isOpenCVActivated to g_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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions