Skip to content

Commit 3149779

Browse files
xuhdevfacebook-github-bot
authored andcommitted
Add sanity checks for NCCL detection.
Summary: Pull Request resolved: pytorch#22819 Test Plan: Imported from OSS Differential Revision: D16283037 Pulled By: ezyang fbshipit-source-id: fc09c9443a568d9af1c78a847282a7d707c49dd6
1 parent e2046f8 commit 3149779

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

cmake/Modules/FindNCCL.cmake

+33-8
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,41 @@ find_library(NCCL_LIBRARIES
4949
include(FindPackageHandleStandardArgs)
5050
find_package_handle_standard_args(NCCL DEFAULT_MSG NCCL_INCLUDE_DIRS NCCL_LIBRARIES)
5151

52-
if(NCCL_FOUND)
52+
if(NCCL_FOUND) # obtaining NCCL version and some sanity checks
5353
set (NCCL_HEADER_FILE "${NCCL_INCLUDE_DIRS}/nccl.h")
54-
message (STATUS "Determining NCCL version from the header file: ${NCCL_HEADER_FILE}")
55-
file (STRINGS ${NCCL_HEADER_FILE} NCCL_MAJOR_VERSION_DEFINED
56-
REGEX "^[ \t]*#define[ \t]+NCCL_MAJOR[ \t]+[0-9]+.*$" LIMIT_COUNT 1)
57-
if (NCCL_MAJOR_VERSION_DEFINED)
58-
string (REGEX REPLACE "^[ \t]*#define[ \t]+NCCL_MAJOR[ \t]+" ""
59-
NCCL_MAJOR_VERSION ${NCCL_MAJOR_VERSION_DEFINED})
60-
message (STATUS "NCCL_MAJOR_VERSION: ${NCCL_MAJOR_VERSION}")
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+
LINK_LIBRARIES ${NCCL_LIBRARIES})
77+
if (NOT NCCL_VERSION_MATCHED)
78+
message(FATAL_ERROR "Found NCCL header version and library version do not match! \
79+
(include: ${NCCL_INCLUDE_DIRS}, library: ${NCCL_LIBRARIES}) Please set NCCL_INCLUDE_DIR and NCCL_LIB_DIR manually.")
80+
endif()
81+
message(STATUS "NCCL version: ${NCCL_VERSION_FROM_HEADER}")
82+
else()
83+
message(STATUS "NCCL version < 2.3.5-5")
6184
endif ()
85+
set (CMAKE_REQUIRED_INCLUDES ${OLD_CMAKE_REQUIRED_INCLUDES})
86+
6287
message(STATUS "Found NCCL (include: ${NCCL_INCLUDE_DIRS}, library: ${NCCL_LIBRARIES})")
6388
mark_as_advanced(NCCL_ROOT_DIR NCCL_INCLUDE_DIRS NCCL_LIBRARIES)
6489
endif()

0 commit comments

Comments
 (0)