Skip to content

cv::getCPUCount returns number of CPUs instead of available CPUs on Linux #16268

Closed

Description

@gnif

On a system using the kernel argument isolcpu, or when using taskset to limit the CPUs available to the process OpenCV continues to allocate threads for all cores on the system even if they are not available to it. This causes OpenCV to create far more threads then is optimal for the target system.

return (int)sysconf( _SC_NPROCESSORS_ONLN );

To obtain the actual number of cores available to the process one should use sched_getaffinity as such:

#include <sched.h>

int getCPUs()
{
  cpu_set_t cpu_set;
  sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
  return CPU_COUNT(&cpu_set);
}

To workaround this bad behavior one can do the following:

cpu_set_t cpu_set;
sched_getaffinity(0, sizeof(cpu_set_t), &cpu_set);
cv::setNumThreads(std::max(1, CPU_COUNT(&cpu_set)-1));

Activity

  1. nappaillav commented on Jan 1, 2020

    @nappaillav

    Hello Everyone,
    I understood the issue and would like to contribute. For which i would like to get your help @alalek .
    As explained by @gnif, the solution to this issue as follows

    `

        #elif defined linux || defined GLIBC || defined HAIKU || defined EMSCRIPTEN
    
            // return (int)sysconf( _SC_NPROCESSORS_ONLN );
    
    	cpu_set_t cpu_set;
    
    	sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
    
    	return CPU_COUNT(&cpu_set);
    

    `

  2. added
    Hackathon
    on Jan 14, 2020
  3. ganesh-k13 commented on Jan 24, 2020

    @ganesh-k13
    Contributor

    Hi @nappaillav, are you looking into this issue? Else I was hoping to take it up.

  4. nappaillav commented on Jan 24, 2020

    @nappaillav

    @ganesh-k13 You can take up the issue

  5. ganesh-k13 commented on Jan 24, 2020

    @ganesh-k13
    Contributor

    Thanks @nappaillav, @alalek I would like to take up this issue please.

  6. ganesh-k13 commented on Jan 25, 2020

    @ganesh-k13
    Contributor

    Hi @alalek and @gnif, this function seems to be an implementation of this.
    I think the number of cores should not be something that should be process wise, but I'm not sure how it will affect the use case. @alalek I would like your opinion on whether this change is needed because it seems both Apple and Windows also is getting machine CPU count as per that answer.

  7. alalek commented on Jan 25, 2020

    @alalek
    Member

    OpenCV has parallel_for_() jobs runner, which may allocate threads.
    There is no reason to allocate threads for each machine CPU/core If application is limited on CPU resource externally. We should avoid "oversubscribing" through creation of huge number of threads.

    Purpose of this issue is to handle limitations applied to current process.

    Need to apply fix which would respect of these CPU resource limitations approaches:

    • isolcpu kernel parameter
    • taskset process launcher
    • sched_setaffinity() call (can be used by user application before calling OpenCV)
    • docker: --cpus= option
    • kubernetes (k8s)
    • other approaches on Linux / FreeBSD
    • something on MacOS X
    • something on Windows

    If proposed std::thread::hardware_concurrency() (C++11) works well then it can be added with C++11 check for OpenCV 3.4.x and without C++11 check for OpenCV 4.x.

    Fallback on "machine's CPUs" is also acceptable.


    BTW, OpenCV has configuration parameter to limit own Thread Pool - see OPENCV_FOR_THREADS_NUM.

  8. ganesh-k13 commented on Jan 26, 2020

    @ganesh-k13
    Contributor

    Hi @alalek, this issue is interesting to make robust.
    My system config before we proceed: 64 cores Xeon running a Linux VM with 6 cores.

    I modified the function slightly like this (see the both the cout) and ran on my machine and got 6.

    Then I ran on docker with this command: docker run -v $PWD:/var/tmp --cpus=2 -it gcc:7.5 bash

    I still got 6 on both cout statements and this "issue" is tracked here. Main reason is that they all use nprocs, etc outputs which will be system level.

    I think a proper solution till docker community solves it wil be to use a cgroups API. I'll investigate more and update you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Hackathoncategory: corefeatureplatform: linux

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions