diff --git a/cmake/External/nccl.cmake b/cmake/External/nccl.cmake index cb928baf3a595..160d2b648c051 100644 --- a/cmake/External/nccl.cmake +++ b/cmake/External/nccl.cmake @@ -15,23 +15,24 @@ if(NOT __NCCL_INCLUDED) # this second replacement is needed when there are multiple archs string(REPLACE ";-gencode" " -gencode" NVCC_GENCODE "${NVCC_GENCODE}") - if("${CMAKE_GENERATOR}" MATCHES "Make") - # Recursive make with jobserver for parallelism - set(MAKE_COMMAND "$(MAKE)") + if(DEFINED ENV{MAX_JOBS}) + set(MAX_JOBS "$ENV{MAX_JOBS}") else() - if(DEFINED ENV{MAX_JOBS}) - set(MAX_JOBS "$ENV{MAX_JOBS}") - else() - include(ProcessorCount) - ProcessorCount(NUM_HARDWARE_THREADS) - # Assume 2 hardware threads per cpu core - math(EXPR MAX_JOBS "${NUM_HARDWARE_THREADS} / 2") - # ProcessorCount might return 0, set to a positive number - if(MAX_JOBS LESS 2) - set(MAX_JOBS 2) - endif() + include(ProcessorCount) + ProcessorCount(NUM_HARDWARE_THREADS) + # Assume 2 hardware threads per cpu core + math(EXPR MAX_JOBS "${NUM_HARDWARE_THREADS} / 2") + # ProcessorCount might return 0, set to a positive number + if(MAX_JOBS LESS 2) + set(MAX_JOBS 2) endif() + endif() + if("${CMAKE_GENERATOR}" MATCHES "Make") + # Recursive make with jobserver for parallelism, and also put a load limit + # here to avoid flaky OOM, https://www.gnu.org/software/make/manual/html_node/Parallel.html + set(MAKE_COMMAND "$(MAKE)" "-l${MAX_JOBS}") + else() # Parallel build with CPU load limit to avoid oversubscription set(MAKE_COMMAND "make" "-j${MAX_JOBS}" "-l${MAX_JOBS}") endif()