This code was adapted from Kitware KWSYS. More sophisticated approaches to Physical CPU count are available in projects including:
- HWLOC
- Google cpu_features
For projects that use MPI on a wide variety of computers running MacOS, Linux, and Windows we have found this single-file approach adequate to determine physical CPU count. For MacOS with Apple Silicon, we extract the count of the "fast" CPU cores.
Examples are given in app/ for C, C++ and Fortran calling the only public-facing function:
unsigned int cpu_count()
Build the examples and test:
cmake --workflow --preset default
Works with every compiler we tried including: GCC, Clang, Intel oneAPI, Visual Studio.