From 991f82558366b35e16ffabf376fa2cba298c12d3 Mon Sep 17 00:00:00 2001 From: leejet Date: Mon, 9 Nov 2026 22:33:43 +0800 Subject: [PATCH] fix: limit default MSVC compilation parallelism --- CMakeLists.txt | 19 +++++++++++++++++-- docs/build.md | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e210379..14901f8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,13 +9,28 @@ if (NOT XCODE AND NOT MSVC AND NOT CMAKE_BUILD_TYPE) endif() if (MSVC) + cmake_host_system_information(RESULT SD_LOGICAL_CORES QUERY NUMBER_OF_LOGICAL_CORES) + if (SD_LOGICAL_CORES GREATER 8) + math(EXPR SD_DEFAULT_MSVC_MP_COUNT "${SD_LOGICAL_CORES} - 2") + elseif (SD_LOGICAL_CORES GREATER 2) + math(EXPR SD_DEFAULT_MSVC_MP_COUNT "${SD_LOGICAL_CORES} - 1") + else() + set(SD_DEFAULT_MSVC_MP_COUNT "${SD_LOGICAL_CORES}") + endif() + if (NOT SD_DEFAULT_MSVC_MP_COUNT GREATER 0) + set(SD_DEFAULT_MSVC_MP_COUNT 1) + endif() + set(SD_MSVC_MP_COUNT "${SD_DEFAULT_MSVC_MP_COUNT}" CACHE STRING "Maximum parallel MSVC compilations per project") + if (NOT SD_MSVC_MP_COUNT MATCHES "^[1-9][0-9]*$" OR SD_MSVC_MP_COUNT GREATER 65536) + message(FATAL_ERROR "SD_MSVC_MP_COUNT must be an integer between 1 and 65536") + endif() add_compile_definitions(_CRT_SECURE_NO_WARNINGS) add_compile_definitions(_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING) # /MP is MSVC-only: icx rejects it outright once offloading is enabled. add_compile_options( - $<$,$>:/MP> + $<$,$>:/MP${SD_MSVC_MP_COUNT}> $<$:/utf-8> - $<$,$>:/MP> + $<$,$>:/MP${SD_MSVC_MP_COUNT}> $<$:/utf-8> ) endif() diff --git a/docs/build.md b/docs/build.md index 62966ec6..575436a2 100644 --- a/docs/build.md +++ b/docs/build.md @@ -21,6 +21,25 @@ git pull origin master git submodule update --init --recursive ``` +## MSVC compilation parallelism + +MSVC builds limit parallel C/C++ compilation within each project by default. +For `n` logical processors, the limit is `n` when `n <= 2`, `n - 1` when +`3 <= n <= 8`, and `n - 2` when `n > 8`. The default is calculated when the +build directory is first configured with this setting. Override it with +`-DSD_MSVC_MP_COUNT=4`, or pass `-USD_MSVC_MP_COUNT` to recalculate it. + +For Visual Studio builds, keep project-level parallelism at one to avoid +multiplying the per-project limit: + +```shell +cmake --build build --config Release --parallel 1 +``` + +This setting controls MSVC's `/MP` option, not CUDA compiler parallelism or a +hard CPU usage limit. Other build tools, such as Ninja, schedule compilation +jobs separately; use `cmake --build build --parallel N` to limit those jobs. + ## Selecting a GGML source tree By default, sd.cpp builds the patched GGML submodule in `ggml/`. To build with