1if(ENABLE_GITEE) 2 set(REQ_URL "https://gitee.com/mirrors/nccl/repository/archive/v2.16.5-1.tar.gz") 3 set(SHA256 "be162e5dee73be833cbec0a8e0a8edef6e4c48b71814efc09b7a2233599ab5eb") 4else() 5 set(REQ_URL "https://github.com/NVIDIA/nccl/archive/v2.16.5-1.tar.gz") 6 set(SHA256 "0e3d7b6295beed81dc15002e88abf7a3b45b5c686b13b779ceac056f5612087f") 7endif() 8 9find_package(CUDA REQUIRED) 10set(nccl_CFLAGS "-D_FORTIFY_SOURCE=2 -O2 -fPIC -fstack-protector-all") 11 12# without -I$ENV{CUDA_HOME}/targets/x86_64-linux/include, cuda_runtime.h will not be found 13# "include_directories($ENV{CUDA_HOME}/targets/x86_64-linux/include)" does not help. 14# without -fPIC, ld relocation error will be reported. 15set(nccl_CXXFLAGS "-D_FORTIFY_SOURCE=2 -O2 \ 16 -I$ENV{CUDA_HOME}/targets/x86_64-linux/include -fPIC -fstack-protector-all") 17set(ENV{LDFLAGS} "-Wl,-z,relro,-z,now,-z,noexecstack,-s") 18 19if(NOT BUILD_LITE) 20 enable_language(CUDA) 21 if(NOT CUDA_PATH OR CUDA_PATH STREQUAL "") 22 if(DEFINED ENV{CUDA_HOME} AND NOT $ENV{CUDA_HOME} STREQUAL "") 23 set(CUDA_PATH $ENV{CUDA_HOME}) 24 else() 25 set(CUDA_PATH ${CUDA_TOOLKIT_ROOT_DIR}) 26 endif() 27 endif() 28 ## Function for setting NVCC flag 29 function(set_nccl_arch NCCL_ARCH) 30 # Detect gpu archs by cudaGetDeviceProperties. 31 message("Detect gpu arch on this device.") 32 set(cu_file "${CMAKE_SOURCE_DIR}/build/mindspore/ccsrc/get_device_compute_capabilities.cu") 33 file(WRITE ${cu_file} "" 34 "#include <cuda_runtime.h>\n" 35 "#include <cstdio>\n" 36 "int main () {\n" 37 " int dev_num = 0;\n" 38 " if (cudaGetDeviceCount(&dev_num) != cudaSuccess) return -1;\n" 39 " if (dev_num < 1) return -1;\n" 40 " for (int dev_id = 0; dev_id < dev_num; ++dev_id) {\n" 41 " cudaDeviceProp prop;" 42 " if (cudaGetDeviceProperties(&prop, dev_id) == cudaSuccess) {\n" 43 " printf(\"%d.%d \", prop.major, prop.minor);\n" 44 " }\n" 45 " }\n" 46 " return 0;\n" 47 "}\n") 48 # Build and run cu_file, get the result from properties. 49 if(NOT MSVC) 50 set(CUDA_LIB_PATH ${CUDA_PATH}/lib64/libcudart.so) 51 else() 52 set(CUDA_LIB_PATH ${CUDA_PATH}/lib/x64/cudart.lib) 53 endif() 54 try_run(RUN_RESULT_VAR COMPILE_RESULT_VAR ${CMAKE_SOURCE_DIR}/build/mindspore/ccsrc/ ${cu_file} 55 CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${CUDA_INCLUDE_DIRS}" 56 LINK_LIBRARIES ${CUDA_LIB_PATH} 57 RUN_OUTPUT_VARIABLE compute_cap) 58 set(cuda_archs_bin) 59 if(RUN_RESULT_VAR EQUAL 0) 60 string(REGEX REPLACE "[ \t]+" ";" compute_cap "${compute_cap}") 61 list(REMOVE_DUPLICATES compute_cap) 62 foreach(arch ${compute_cap}) 63 set(arch_bin) 64 if(arch MATCHES "^([0-9]\\.[0-9](\\[0-9]\\.[0-9]\\))?)$") 65 set(arch_bin ${CMAKE_MATCH_1}) 66 else() 67 message(FATAL_ERROR "Unknown CUDA arch Name ${arch} !") 68 endif() 69 if(NOT arch_bin) 70 message(FATAL_ERROR "arch_bin was not set !") 71 endif() 72 list(APPEND cuda_archs_bin ${arch_bin}) 73 endforeach() 74 else() 75 message("Failed to detect gpu arch automatically.") 76 list(APPEND ARCH_FLAGS "-arch=sm_60") 77 endif() 78 # Get build flag from env to choose common/auto build. 79 set(NVCC_ARCH_FLAG_FROM_ENV $ENV{CUDA_ARCH}) 80 if(NVCC_ARCH_FLAG_FROM_ENV STREQUAL "common") 81 message("Build common archs for release.") 82 list(APPEND ARCH_FLAGS "-gencode=arch=compute_60,code=sm_60") 83 list(APPEND ARCH_FLAGS "-gencode=arch=compute_61,code=sm_61") 84 list(APPEND ARCH_FLAGS "-gencode=arch=compute_70,code=sm_70") 85 if(${CUDA_VERSION} VERSION_GREATER "9.5") 86 list(APPEND ARCH_FLAGS "-gencode=arch=compute_75,code=sm_75") 87 if(${CUDA_VERSION} VERSION_LESS "11.0") 88 list(APPEND ARCH_FLAGS "-gencode=arch=compute_75,code=compute_75") 89 endif() 90 endif() 91 if(${CUDA_VERSION} VERSION_GREATER "10.5") 92 list(APPEND ARCH_FLAGS "-gencode=arch=compute_80,code=sm_80") 93 if(${CUDA_VERSION} VERSION_LESS "11.1") 94 list(APPEND ARCH_FLAGS "-gencode=arch=compute_80,code=compute_80") 95 endif() 96 endif() 97 if(NOT ${CUDA_VERSION} VERSION_LESS "11.1") 98 list(APPEND ARCH_FLAGS "-gencode=arch=compute_86,code=compute_86") 99 endif() 100 else() 101 message("Auto build for arch(s) " ${cuda_archs_bin}) 102 string(REGEX REPLACE "\\." "" cuda_archs_bin "${cuda_archs_bin}") 103 string(REGEX MATCHALL "[0-9()]+" cuda_archs_bin "${cuda_archs_bin}") 104 foreach(arch ${cuda_archs_bin}) 105 list(APPEND ARCH_FLAGS -gencode=arch=compute_${arch},code=sm_${arch}) 106 endforeach() 107 list(APPEND ARCH_FLAGS "-arch=sm_60") 108 endif() 109 list(REMOVE_DUPLICATES ARCH_FLAGS) 110 111 # Convert to string. 112 list(LENGTH ARCH_FLAGS arch_flags_length) 113 MATH(EXPR arch_flags_length "${arch_flags_length}-1") 114 foreach(index RANGE ${arch_flags_length}) 115 list(GET ARCH_FLAGS ${index} item) 116 if(${index} EQUAL 0) 117 set(ARCH_FLAGS_STR "${item}") 118 else() 119 list(APPEND ARCH_FLAGS_STR " ${item}") 120 endif() 121 endforeach() 122 message("Final NCCL_ARCH_FLAGS " ${ARCH_FLAGS_STR}) 123 set(${NCCL_ARCH} ${ARCH_FLAGS_STR} PARENT_SCOPE) 124 endfunction() 125 set_nccl_arch(NCCL_ARCH_FLAG) 126else() 127 set(NCCL_ARCH_FLAG "-arch=sm_60") 128endif() 129 130mindspore_add_pkg(nccl 131 VER 2.16.5-1-${CUDA_VERSION} 132 LIBS nccl 133 URL ${REQ_URL} 134 SHA256 ${SHA256} 135 BUILD_OPTION src.build NVCC_GENCODE=""${NCCL_ARCH_FLAG}"" 136 INSTALL_INCS build/include/* 137 INSTALL_LIBS build/lib/*) 138set(ENV{LDFLAGS} "") 139include_directories(${nccl_INC}) 140add_library(mindspore::nccl ALIAS nccl::nccl) 141