You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cv::getCPUCount returns number of CPUs instead of available CPUs on Linux #16268
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.
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);
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.
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.
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
Activity
nappaillav commentedon Jan 1, 2020
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
`
`
ganesh-k13 commentedon Jan 24, 2020
Hi @nappaillav, are you looking into this issue? Else I was hoping to take it up.
nappaillav commentedon Jan 24, 2020
@ganesh-k13 You can take up the issue
ganesh-k13 commentedon Jan 24, 2020
Thanks @nappaillav, @alalek I would like to take up this issue please.
ganesh-k13 commentedon Jan 25, 2020
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.
alalek commentedon Jan 25, 2020
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:
isolcpukernel parametertasksetprocess launchersched_setaffinity()call (can be used by user application before calling OpenCV)docker:--cpus=optionIf 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.ganesh-k13 commentedon Jan 26, 2020
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 bashI 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