CPU INFOrmation library cpuinfo is a library to detect essential for performance optimization information about host CPU. Features Cross-platform availability: Linux, Windows, macOS, Android, iOS and FreeBSD operating systems x86, x86-64, ARM, and ARM64 architectures Modern C/C++ interface Thread-safe No memory allocation after initialization No exceptions thrown Detection of supported instruction sets, up to AVX512…
Pin thread to cores sharing L2 cache with the current core (Linux or Android)
cpuinfo_initialize();
cpu_set_t cpu_set;
CPU_ZERO(&cpu_set);
const struct cpuinfo_cache* current_l2 = cpuinfo_get_current_processor()->cache.l2;
for (uint32_t i = 0; i < current_l2->processor_count; i++) {
CPU_SET(cpuinfo_get_processor(current_l2->processor_start + i)->linux_id, &cpu_set);
}
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_set);
Use via pkg-config
If you would like to provide your project’s build environment with the necessary compiler and linker flags in a portable manner, the library by default when built enables CPUINFO_BUILD_PKG_CONFIG and will generate a pkg-config manifest (libcpuinfo.pc). Here are several examples of how to use it:
Command Line
If you used your distro’s package manager to install the library, you can verify that it is available to your build environment like so:
You can also use this library as a dependency to your Bazel project. Add to your MODULE.bazel file:
# fetch cpuinfo from Bazel Central Registry: https://registry.bazel.build/modules/cpuinfo
bazel_dep(name = "cpuinfo", version = "0.0.0-20250925-877328f")
# Optional: Override it with some specific commit hash
git_override(
module_name = "cpuinfo",
commit = "<replace_with_commit_hash>"
remote = "https://github.com/pytorch/cpuinfo.git",
)
评论