From b340e88706ce5b5ab0cec9f2341b6338c06a7102 Mon Sep 17 00:00:00 2001 From: ShamSaleem Date: Tue, 23 Jun 2026 02:51:43 +0300 Subject: [PATCH] Use dynamic CPU count for cmake --build -j in docs and test scripts Replace hardcoded -j values (-j9, -j10) in the general build documentation and the test/ build scripts with a portable expression that derives "core count + 1" at runtime: nproc on Linux and sysctl -n hw.ncpu on macOS. This matches the recommendation already stated in docs/source/using-executorch-building-from-source.md and avoids machine-specific job counts that don't fit the user's hardware. Scope is limited to general (non-vendor) docs and contributor-facing test/ scripts. Vendor-backend scripts, CI scripts under .ci/, and non-cmake -j flags are intentionally left for a follow-up. Partially addresses #10887. --- CONTRIBUTING.md | 2 +- backends/xnnpack/README.md | 2 +- docs/source/using-executorch-building-from-source.md | 7 ++++--- examples/xnnpack/README.md | 4 ++-- extension/training/README.md | 2 +- kernels/README.md | 4 ++-- test/build_optimized_size_test.sh | 4 ++-- test/build_size_test.sh | 4 ++-- test/run_oss_cpp_tests.sh | 2 +- 9 files changed, 16 insertions(+), 15 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e60788c4d94..055b400c739 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -325,7 +325,7 @@ To build C++ tests manually with CMake, run the following from the repository ro ```bash cmake . -Bcmake-out -DCMAKE_INSTALL_PREFIX=cmake-out -DEXECUTORCH_BUILD_TESTS=ON -cmake --build cmake-out -j9 --target install +cmake --build cmake-out -j$(( $(nproc 2>/dev/null || sysctl -n hw.ncpu) + 1 )) --target install ``` You can then use `ctest` to list or run individual C++ tests directly: diff --git a/backends/xnnpack/README.md b/backends/xnnpack/README.md index 02664557664..3ce9695b61d 100644 --- a/backends/xnnpack/README.md +++ b/backends/xnnpack/README.md @@ -117,7 +117,7 @@ cmake \ Then you can build the runtime componenets with ```bash -cmake --build cmake-out -j9 --target install --config Release +cmake --build cmake-out -j$(( $(nproc 2>/dev/null || sysctl -n hw.ncpu) + 1 )) --target install --config Release ``` Now you should be able to find the executable built at `./cmake-out/executor_runner` you can run the executable with the model you generated as such diff --git a/docs/source/using-executorch-building-from-source.md b/docs/source/using-executorch-building-from-source.md index 170c3f8758d..3c197c759d6 100644 --- a/docs/source/using-executorch-building-from-source.md +++ b/docs/source/using-executorch-building-from-source.md @@ -143,7 +143,7 @@ When building as a submodule as part of a user CMake build, ExecuTorch CMake opt CMake configuration for standalone runtime build: ```bash cmake -B cmake-out --preset [preset] [options] -cmake --build cmake-out -j10 +cmake --build cmake-out -j$(( $(nproc 2>/dev/null || sysctl -n hw.ncpu) + 1 )) ``` #### Build Presets @@ -265,8 +265,9 @@ cd executorch # # NOTE: The `-j` argument specifies how many jobs/processes to use when # building, and tends to speed up the build significantly. It's typical to use -# "core count + 1" as the `-j` value. -cmake --build cmake-out -j9 +# "core count + 1" as the `-j` value; the command below derives that +# dynamically (`nproc` on Linux, `sysctl -n hw.ncpu` on macOS). +cmake --build cmake-out -j$(( $(nproc 2>/dev/null || sysctl -n hw.ncpu) + 1 )) ``` > **_TIP:_** For faster rebuilds, consider installing ccache (see [Compiler Cache section](#compiler-cache-ccache) below). On first builds, ccache populates its cache. Subsequent builds with the same compiler flags can be significantly faster. diff --git a/examples/xnnpack/README.md b/examples/xnnpack/README.md index fb15d89522d..c678bfb0605 100644 --- a/examples/xnnpack/README.md +++ b/examples/xnnpack/README.md @@ -51,7 +51,7 @@ cmake \ Then you can build the runtime components with ```bash -cmake --build cmake-out -j9 --target install --config Release +cmake --build cmake-out -j$(( $(nproc 2>/dev/null || sysctl -n hw.ncpu) + 1 )) --target install --config Release ``` Now finally you should be able to run this model with the following command @@ -105,7 +105,7 @@ cmake \ Then you can build the runtime componenets with ```bash -cmake --build cmake-out -j9 --target install --config Release +cmake --build cmake-out -j$(( $(nproc 2>/dev/null || sysctl -n hw.ncpu) + 1 )) --target install --config Release ``` Now you should be able to find the executable built at `./cmake-out/executor_runner` you can run the executable with the model you generated as such diff --git a/extension/training/README.md b/extension/training/README.md index ed2d65ef343..a20c18351c0 100644 --- a/extension/training/README.md +++ b/extension/training/README.md @@ -247,7 +247,7 @@ cmake \ Then you can build the runtime componenets with ```bash -cmake --build cmake-out -j9 --target install --config Release +cmake --build cmake-out -j$(( $(nproc 2>/dev/null || sysctl -n hw.ncpu) + 1 )) --target install --config Release ``` Now you should be able to find the executable built at `./cmake-out/extension/training/train_xor` you can run the executable with the model you generated as such diff --git a/kernels/README.md b/kernels/README.md index d8330a4af73..959d10d7f93 100644 --- a/kernels/README.md +++ b/kernels/README.md @@ -257,7 +257,7 @@ cmake -DCMAKE_INSTALL_PREFIX=cmake-out \ -DCMAKE_BUILD_TYPE=Release \ -DPYTHON_EXECUTABLE=python \ -Bcmake-out . -cmake --build cmake-out -j9 --target install --config Release +cmake --build cmake-out -j$(( $(nproc 2>/dev/null || sysctl -n hw.ncpu) + 1 )) --target install --config Release ``` 2. The generated `NativeFunctions.h` file is located in ``` @@ -367,7 +367,7 @@ cmake . \ -DEXECUTORCH_BUILD_TESTS=ON \ -Bcmake-out -cmake --build cmake-out -j9 --target install +cmake --build cmake-out -j$(( $(nproc 2>/dev/null || sysctl -n hw.ncpu) + 1 )) --target install ``` 2. Run tests. You should see your test here. ``` diff --git a/test/build_optimized_size_test.sh b/test/build_optimized_size_test.sh index d73d85fa206..6e949645081 100644 --- a/test/build_optimized_size_test.sh +++ b/test/build_optimized_size_test.sh @@ -30,14 +30,14 @@ cmake_install_executorch_lib() { -DEXECUTORCH_OPTIMIZE_SIZE=ON \ -DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \ -Bcmake-out . - cmake --build cmake-out -j9 --target install --config MinSizeRel + cmake --build cmake-out -j$(( $(nproc 2>/dev/null || sysctl -n hw.ncpu) + 1 )) --target install --config MinSizeRel } test_cmake_size_test() { CXXFLAGS="-g" retry cmake -DCMAKE_BUILD_TYPE=MinSizeRel -DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON -DCMAKE_INSTALL_PREFIX=cmake-out -Bcmake-out/test test echo "Build size test" - cmake --build cmake-out/test -j9 --config MinSizeRel + cmake --build cmake-out/test -j$(( $(nproc 2>/dev/null || sysctl -n hw.ncpu) + 1 )) --config MinSizeRel echo 'ExecuTorch with no ops binary size, unstripped:' ls -al cmake-out/test/size_test diff --git a/test/build_size_test.sh b/test/build_size_test.sh index b22b28a2558..d8c2dfe4dab 100644 --- a/test/build_size_test.sh +++ b/test/build_size_test.sh @@ -33,7 +33,7 @@ cmake_install_executorch_lib() { -DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \ ${EXTRA_BUILD_ARGS} \ -Bcmake-out . - cmake --build cmake-out -j9 --target install --config Release + cmake --build cmake-out -j$(( $(nproc 2>/dev/null || sysctl -n hw.ncpu) + 1 )) --target install --config Release } test_cmake_size_test() { @@ -43,7 +43,7 @@ test_cmake_size_test() { -Bcmake-out/test test echo "Build size test" - cmake --build cmake-out/test -j9 --config Release + cmake --build cmake-out/test -j$(( $(nproc 2>/dev/null || sysctl -n hw.ncpu) + 1 )) --config Release echo 'ExecuTorch with no ops binary size, unstripped:' ls -al cmake-out/test/size_test diff --git a/test/run_oss_cpp_tests.sh b/test/run_oss_cpp_tests.sh index 4c5bc88f03a..4a7e0199a23 100755 --- a/test/run_oss_cpp_tests.sh +++ b/test/run_oss_cpp_tests.sh @@ -59,7 +59,7 @@ build_executorch() { -DEXECUTORCH_BUILD_XNNPACK=ON \ -DEXECUTORCH_BUILD_TESTS=ON \ -Bcmake-out - cmake --build cmake-out -j9 --target install + cmake --build cmake-out -j$(( $(nproc 2>/dev/null || sysctl -n hw.ncpu) + 1 )) --target install } build_and_run_test() {