cudnn.cmake 3.4 KB
Newer Older
1
if(NOT WITH_GPU)
2
  return()
3 4
endif()

P
peizhilin 已提交
5
if(WIN32)
6
  set(CUDNN_ROOT ${CUDA_TOOLKIT_ROOT_DIR})
7
else()
8 9 10
  set(CUDNN_ROOT
      "/usr"
      CACHE PATH "CUDNN ROOT")
11
endif()
P
peizhilin 已提交
12

13 14 15 16 17
find_path(
  CUDNN_INCLUDE_DIR cudnn.h
  PATHS ${CUDNN_ROOT} ${CUDNN_ROOT}/include $ENV{CUDNN_ROOT}
        $ENV{CUDNN_ROOT}/include ${CUDA_TOOLKIT_INCLUDE}
  NO_DEFAULT_PATH)
Z
zhangjinchao01 已提交
18 19 20

get_filename_component(__libpath_hist ${CUDA_CUDART_LIBRARY} PATH)

21 22
set(TARGET_ARCH "x86_64")
if(NOT ${CMAKE_SYSTEM_PROCESSOR})
23
  set(TARGET_ARCH ${CMAKE_SYSTEM_PROCESSOR})
24
endif()
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39
list(
  APPEND
  CUDNN_CHECK_LIBRARY_DIRS
  ${CUDNN_ROOT}
  ${CUDNN_ROOT}/lib64
  ${CUDNN_ROOT}/lib
  ${CUDNN_ROOT}/lib/${TARGET_ARCH}-linux-gnu
  ${CUDNN_ROOT}/local/cuda-${CUDA_VERSION}/targets/${TARGET_ARCH}-linux/lib/
  $ENV{CUDNN_ROOT}
  $ENV{CUDNN_ROOT}/lib64
  $ENV{CUDNN_ROOT}/lib
  /usr/lib
  ${CUDA_TOOLKIT_ROOT_DIR}
  ${CUDA_TOOLKIT_ROOT_DIR}/lib/x64)
D
dzhwinter 已提交
40
set(CUDNN_LIB_NAME "")
41

42 43
if(LINUX)
  set(CUDNN_LIB_NAME "libcudnn.so")
44
endif()
D
dzhwinter 已提交
45 46

if(WIN32)
47 48
  # only support cudnn7
  set(CUDNN_LIB_NAME "cudnn.lib" "cudnn64_7.dll")
49
endif()
D
dzhwinter 已提交
50

51
if(APPLE)
52
  set(CUDNN_LIB_NAME "libcudnn.dylib" "libcudnn.so")
53
endif()
D
dzhwinter 已提交
54

55 56 57 58 59 60
find_library(
  CUDNN_LIBRARY
  NAMES ${CUDNN_LIB_NAME} # libcudnn_static.a
  PATHS ${CUDNN_CHECK_LIBRARY_DIRS} ${CUDNN_INCLUDE_DIR} ${__libpath_hist}
  NO_DEFAULT_PATH
  DOC "Path to cuDNN library.")
61

Z
zhangjinchao01 已提交
62
if(CUDNN_INCLUDE_DIR AND CUDNN_LIBRARY)
63
  set(CUDNN_FOUND ON)
Z
zhangjinchao01 已提交
64
else()
65
  set(CUDNN_FOUND OFF)
Z
zhangjinchao01 已提交
66 67
endif()

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
macro(find_cudnn_version cudnn_header_file)
  file(READ ${cudnn_header_file} CUDNN_VERSION_FILE_CONTENTS)
  get_filename_component(CUDNN_LIB_PATH ${CUDNN_LIBRARY} DIRECTORY)

  string(REGEX MATCH "define CUDNN_VERSION +([0-9]+)" CUDNN_VERSION
               "${CUDNN_VERSION_FILE_CONTENTS}")
  string(REGEX REPLACE "define CUDNN_VERSION +([0-9]+)" "\\1" CUDNN_VERSION
                       "${CUDNN_VERSION}")

  if("${CUDNN_VERSION}" STREQUAL "2000")
    message(STATUS "Current cuDNN version is v2. ")
  else()
    string(REGEX MATCH "define CUDNN_MAJOR +([0-9]+)" CUDNN_MAJOR_VERSION
                 "${CUDNN_VERSION_FILE_CONTENTS}")
    string(REGEX REPLACE "define CUDNN_MAJOR +([0-9]+)" "\\1"
                         CUDNN_MAJOR_VERSION "${CUDNN_MAJOR_VERSION}")
    string(REGEX MATCH "define CUDNN_MINOR +([0-9]+)" CUDNN_MINOR_VERSION
                 "${CUDNN_VERSION_FILE_CONTENTS}")
    string(REGEX REPLACE "define CUDNN_MINOR +([0-9]+)" "\\1"
                         CUDNN_MINOR_VERSION "${CUDNN_MINOR_VERSION}")
    string(REGEX MATCH "define CUDNN_PATCHLEVEL +([0-9]+)"
                 CUDNN_PATCHLEVEL_VERSION "${CUDNN_VERSION_FILE_CONTENTS}")
    string(REGEX REPLACE "define CUDNN_PATCHLEVEL +([0-9]+)" "\\1"
                         CUDNN_PATCHLEVEL_VERSION "${CUDNN_PATCHLEVEL_VERSION}")

    if(NOT CUDNN_MAJOR_VERSION)
      set(CUDNN_VERSION "???")
Z
zhangjinchao01 已提交
95
    else()
96 97
      add_definitions("-DCUDNN_MAJOR_VERSION=\"${CUDNN_MAJOR_VERSION}\"")
      math(EXPR CUDNN_VERSION "${CUDNN_MAJOR_VERSION} * 1000 +
Z
zhangjinchao01 已提交
98
                 ${CUDNN_MINOR_VERSION} * 100 + ${CUDNN_PATCHLEVEL_VERSION}")
99 100 101 102 103
      message(
        STATUS
          "Current cuDNN header is ${cudnn_header_file} "
          "Current cuDNN version is v${CUDNN_MAJOR_VERSION}.${CUDNN_MINOR_VERSION}.${CUDNN_PATCHLEVEL_VERSION}. "
      )
Z
zhangjinchao01 已提交
104
    endif()
105
  endif()
106 107 108
endmacro()

if(CUDNN_FOUND)
109 110 111
  find_cudnn_version(${CUDNN_INCLUDE_DIR}/cudnn.h)
  if(NOT CUDNN_MAJOR_VERSION)
    find_cudnn_version(${CUDNN_INCLUDE_DIR}/cudnn_version.h)
112
  endif()
Z
zhangjinchao01 已提交
113
endif()