-
-
Notifications
You must be signed in to change notification settings - Fork 56.5k
cv::getCPUCount returns number of CPUs instead of available CPUs on Linux #16268
Copy link
Copy link
Closed
Closed
Copy link
Labels
Hackathonhttps://opencv.org/opencv-hackathon-starts-next-week/https://opencv.org/opencv-hackathon-starts-next-week/category: corefeatureplatform: linux
Milestone
Description
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.
opencv/modules/core/src/parallel.cpp
Line 816 in 7b12cbd
| 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));
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Hackathonhttps://opencv.org/opencv-hackathon-starts-next-week/https://opencv.org/opencv-hackathon-starts-next-week/category: corefeatureplatform: linux