1# Find the nccl libraries 2# 3# The following variables are optionally searched for defaults 4# NCCL_ROOT: Base directory where all NCCL components are found 5# NCCL_INCLUDE_DIR: Directory where NCCL header is found 6# NCCL_LIB_DIR: Directory where NCCL library is found 7# 8# The following are set after configuration is done: 9# NCCL_FOUND 10# NCCL_INCLUDE_DIRS 11# NCCL_LIBRARIES 12# 13# The path hints include CUDA_TOOLKIT_ROOT_DIR seeing as some folks 14# install NCCL in the same location as the CUDA toolkit. 15# See https://github.com/caffe2/caffe2/issues/1601 16 17set(NCCL_INCLUDE_DIR $ENV{NCCL_INCLUDE_DIR} CACHE PATH "Folder contains NVIDIA NCCL headers") 18set(NCCL_LIB_DIR $ENV{NCCL_LIB_DIR} CACHE PATH "Folder contains NVIDIA NCCL libraries") 19set(NCCL_VERSION $ENV{NCCL_VERSION} CACHE STRING "Version of NCCL to build with") 20 21if ($ENV{NCCL_ROOT_DIR}) 22 message(WARNING "NCCL_ROOT_DIR is deprecated. Please set NCCL_ROOT instead.") 23endif() 24list(APPEND NCCL_ROOT $ENV{NCCL_ROOT_DIR} ${CUDA_TOOLKIT_ROOT_DIR}) 25# Compatible layer for CMake <3.12. NCCL_ROOT will be accounted in for searching paths and libraries for CMake >=3.12. 26list(APPEND CMAKE_PREFIX_PATH ${NCCL_ROOT}) 27 28find_path(NCCL_INCLUDE_DIRS 29 NAMES nccl.h 30 HINTS ${NCCL_INCLUDE_DIR}) 31 32if (USE_STATIC_NCCL) 33 MESSAGE(STATUS "USE_STATIC_NCCL is set. Linking with static NCCL library.") 34 SET(NCCL_LIBNAME "nccl_static") 35 if (NCCL_VERSION) # Prefer the versioned library if a specific NCCL version is specified 36 set(CMAKE_FIND_LIBRARY_SUFFIXES ".a.${NCCL_VERSION}" ${CMAKE_FIND_LIBRARY_SUFFIXES}) 37 endif() 38else() 39 SET(NCCL_LIBNAME "nccl") 40 if (NCCL_VERSION) # Prefer the versioned library if a specific NCCL version is specified 41 set(CMAKE_FIND_LIBRARY_SUFFIXES ".so.${NCCL_VERSION}" ${CMAKE_FIND_LIBRARY_SUFFIXES}) 42 endif() 43endif() 44 45find_library(NCCL_LIBRARIES 46 NAMES ${NCCL_LIBNAME} 47 HINTS ${NCCL_LIB_DIR}) 48 49include(FindPackageHandleStandardArgs) 50find_package_handle_standard_args(NCCL DEFAULT_MSG NCCL_INCLUDE_DIRS NCCL_LIBRARIES) 51 52if(NCCL_FOUND) # obtaining NCCL version and some sanity checks 53 set (NCCL_HEADER_FILE "${NCCL_INCLUDE_DIRS}/nccl.h") 54 message (STATUS "Determining NCCL version from ${NCCL_HEADER_FILE}...") 55 set (OLD_CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES}) 56 list (APPEND CMAKE_REQUIRED_INCLUDES ${NCCL_INCLUDE_DIRS}) 57 include(CheckCXXSymbolExists) 58 check_cxx_symbol_exists(NCCL_VERSION_CODE nccl.h NCCL_VERSION_DEFINED) 59 60 if (NCCL_VERSION_DEFINED) 61 set(file "${PROJECT_BINARY_DIR}/detect_nccl_version.cc") 62 file(WRITE ${file} " 63 #include <iostream> 64 #include <nccl.h> 65 int main() 66 { 67 std::cout << NCCL_MAJOR << '.' << NCCL_MINOR << '.' << NCCL_PATCH << std::endl; 68 69 int x; 70 ncclGetVersion(&x); 71 return x == NCCL_VERSION_CODE; 72 } 73") 74 try_run(NCCL_VERSION_MATCHED compile_result ${PROJECT_BINARY_DIR} ${file} 75 RUN_OUTPUT_VARIABLE NCCL_VERSION_FROM_HEADER 76 CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${NCCL_INCLUDE_DIRS}" 77 LINK_LIBRARIES ${NCCL_LIBRARIES}) 78 if (NOT NCCL_VERSION_MATCHED) 79 message(FATAL_ERROR "Found NCCL header version and library version do not match! \ 80(include: ${NCCL_INCLUDE_DIRS}, library: ${NCCL_LIBRARIES}) Please set NCCL_INCLUDE_DIR and NCCL_LIB_DIR manually.") 81 endif() 82 message(STATUS "NCCL version: ${NCCL_VERSION_FROM_HEADER}") 83 else() 84 message(STATUS "NCCL version < 2.3.5-5") 85 endif () 86 set (CMAKE_REQUIRED_INCLUDES ${OLD_CMAKE_REQUIRED_INCLUDES}) 87 88 message(STATUS "Found NCCL (include: ${NCCL_INCLUDE_DIRS}, library: ${NCCL_LIBRARIES})") 89 mark_as_advanced(NCCL_ROOT_DIR NCCL_INCLUDE_DIRS NCCL_LIBRARIES) 90endif() 91