Skip to content

Commit 9defd6b

Browse files
author
pradeep
authored
Use FindcuDNN custom module to locate cudnn (arrayfire#2661)
* Throw error from cmake when cudnn not found * Use FindcuDNN cmake module for cuDNN framework
1 parent a448544 commit 9defd6b

3 files changed

Lines changed: 148 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ arrayfire_set_cmake_default_variables()
3030
set(MKL_THREAD_LAYER "Intel OpenMP" CACHE STRING "The thread layer to choose for MKL")
3131

3232
find_package(CUDA 7.0)
33+
find_package(cuDNN 7.3)
3334
find_package(OpenCL 1.2)
3435
find_package(OpenGL)
3536
find_package(FreeImage)

CMakeModules/FindcuDNN.cmake

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Fetched the original content of this file from
2+
# https://github.com/soumith/cudnn.torch
3+
#
4+
# Original Copyright:
5+
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
6+
# file Copyright.txt or https://cmake.org/licensing for details.
7+
#
8+
# Copyright (c) 2017, ArrayFire
9+
# All rights reserved.
10+
#
11+
# This file is distributed under 3-clause BSD license.
12+
# The complete license agreement can be obtained at:
13+
# http://arrayfire.com/licenses/BSD-3-Clause
14+
#
15+
# FindcuDNN
16+
# -------
17+
#
18+
# Find cuDNN library
19+
#
20+
# This module creates imported target cuDNN::cuDNN upon successfull
21+
# lookup of cuDNN headers and libraries.
22+
#
23+
# Valiables that affect result:
24+
# <VERSION>, <REQUIRED>, <QUIET>: as usual
25+
#
26+
# Usage
27+
# -----
28+
# add_exectuable(helloworld main.cpp)
29+
# target_link_libraries(helloworld PRIVATE cuDNN::cuDNN)
30+
#
31+
# Note: It is recommended to avoid using variables set by the find module.
32+
#
33+
# Result variables
34+
# ----------------
35+
#
36+
# This module will set the following variables in your project:
37+
#
38+
# ``cuDNN_INCLUDE_DIRS``
39+
# where to find cudnn.h.
40+
# ``cuDNN_LINK_LIBRARY``
41+
# the libraries to link against to use cuDNN.
42+
# ``cuDNN_DLL_LIBRARY``
43+
# Windows DLL of cuDNN
44+
# ``cuDNN_FOUND``
45+
# If false, do not try to use cuDNN.
46+
# ``cuDNN_VERSION``
47+
# Version of the cuDNN library we looked for
48+
49+
find_package(PkgConfig)
50+
pkg_check_modules(PC_CUDNN QUIET cuDNN)
51+
52+
find_package(CUDA QUIET)
53+
54+
find_path(cuDNN_INCLUDE_DIRS
55+
NAMES cudnn.h
56+
HINTS
57+
${PC_CUDNN_INCLUDE_DIRS}
58+
${cuDNN_ROOT_DIR}
59+
${CUDA_TOOLKIT_INCLUDE}
60+
PATH_SUFFIXES include
61+
DOC "cuDNN include directory path." )
62+
63+
if(cuDNN_INCLUDE_DIRS)
64+
file(READ ${cuDNN_INCLUDE_DIRS}/cudnn.h CUDNN_VERSION_FILE_CONTENTS)
65+
string(REGEX MATCH "define CUDNN_MAJOR * +([0-9]+)"
66+
CUDNN_MAJOR_VERSION "${CUDNN_VERSION_FILE_CONTENTS}")
67+
string(REGEX REPLACE "define CUDNN_MAJOR * +([0-9]+)" "\\1"
68+
CUDNN_MAJOR_VERSION "${CUDNN_MAJOR_VERSION}")
69+
string(REGEX MATCH "define CUDNN_MINOR * +([0-9]+)"
70+
CUDNN_MINOR_VERSION "${CUDNN_VERSION_FILE_CONTENTS}")
71+
string(REGEX REPLACE "define CUDNN_MINOR * +([0-9]+)" "\\1"
72+
CUDNN_MINOR_VERSION "${CUDNN_MINOR_VERSION}")
73+
string(REGEX MATCH "define CUDNN_PATCHLEVEL * +([0-9]+)"
74+
CUDNN_PATCH_VERSION "${CUDNN_VERSION_FILE_CONTENTS}")
75+
string(REGEX REPLACE "define CUDNN_PATCHLEVEL * +([0-9]+)" "\\1"
76+
CUDNN_PATCH_VERSION "${CUDNN_PATCH_VERSION}")
77+
set(cuDNN_VERSION ${CUDNN_MAJOR_VERSION}.${CUDNN_MINOR_VERSION})
78+
endif()
79+
80+
# Choose lib suffix to be exact major version if requested
81+
# otherwise, just pick the one read from cudnn.h header
82+
if(cuDNN_FIND_VERSION_EXACT)
83+
set(cudnn_ver_suffix "${cuDNN_FIND_VERSION_MAJOR}")
84+
else()
85+
set(cudnn_ver_suffix "${CUDNN_MAJOR_VERSION}")
86+
endif()
87+
88+
if(cuDNN_INCLUDE_DIRS)
89+
get_filename_component(libpath_cudart "${CUDA_CUDART_LIBRARY}" PATH)
90+
91+
find_library(cuDNN_LINK_LIBRARY
92+
NAMES
93+
libcudnn.so.${cudnn_ver_suffix}
94+
libcudnn.${cudnn_ver_suffix}.dylib
95+
cudnn
96+
PATHS
97+
$ENV{LD_LIBRARY_PATH}
98+
${libpath_cudart}
99+
${cuDNN_ROOT_DIR}
100+
${PC_CUDNN_LIBRARY_DIRS}
101+
${CMAKE_INSTALL_PREFIX}
102+
PATH_SUFFIXES lib lib64 bin lib/x64 bin/x64
103+
DOC "cuDNN link library." )
104+
105+
if(WIN32 AND cuDNN_LINK_LIBRARY)
106+
find_file(cuDNN_DLL_LIBRARY
107+
NAMES cudnn64_${cudnn_ver_suffix}${CMAKE_SHARED_LIBRARY_SUFFIX}
108+
PATHS
109+
$ENV{PATH}
110+
${libpath_cudart}
111+
${cuDNN_ROOT_DIR}
112+
${PC_CUDNN_LIBRARY_DIRS}
113+
${CMAKE_INSTALL_PREFIX}
114+
PATH_SUFFIXES lib lib64 bin lib/x64 bin/x64
115+
DOC "cuDNN Windows DLL." )
116+
endif()
117+
endif()
118+
119+
find_package_handle_standard_args(cuDNN
120+
REQUIRED_VARS cuDNN_LINK_LIBRARY cuDNN_INCLUDE_DIRS
121+
VERSION_VAR cuDNN_VERSION)
122+
123+
mark_as_advanced(cuDNN_LINK_LIBRARY cuDNN_INCLUDE_DIRS cuDNN_DLL_LIBRARY)
124+
125+
if(cuDNN_FOUND)
126+
add_library(cuDNN::cuDNN SHARED IMPORTED)
127+
if(WIN32)
128+
set_target_properties(cuDNN::cuDNN
129+
PROPERTIES
130+
IMPORTED_LINK_INTERFACE_LANGUAGE "C"
131+
INTERFACE_INCLUDE_DIRECTORIES "${cuDNN_INCLUDE_DIRS}"
132+
IMPORTED_LOCATION "${cuDNN_DLL_LIBRARY}"
133+
IMPORTED_IMPLIB "${cuDNN_LINK_LIBRARY}"
134+
)
135+
else(WIN32)
136+
set_target_properties(cuDNN::cuDNN
137+
PROPERTIES
138+
IMPORTED_LINK_INTERFACE_LANGUAGE "C"
139+
INTERFACE_INCLUDE_DIRECTORIES "${cuDNN_INCLUDE_DIRS}"
140+
IMPORTED_LOCATION "${cuDNN_LINK_LIBRARY}"
141+
)
142+
endif(WIN32)
143+
endif(cuDNN_FOUND)

src/backend/cuda/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ dependency_check(CUDA_FOUND "CUDA not found.")
1212

1313
find_cuda_helper_libs(nvrtc)
1414
find_cuda_helper_libs(nvrtc-builtins)
15-
find_cuda_helper_libs(cudnn)
15+
if(NOT cuDNN_FOUND)
16+
message(FATAL_ERROR "Atleast cuDNN version 7.3 is required, please install.")
17+
endif()
1618

1719
get_filename_component(CUDA_LIBRARIES_PATH ${CUDA_cudart_static_LIBRARY} DIRECTORY CACHE)
1820

@@ -540,7 +542,7 @@ target_link_libraries(afcuda
540542
${CUDA_CUFFT_LIBRARIES}
541543
${CUDA_cusolver_LIBRARY}
542544
${CUDA_cusparse_LIBRARY}
543-
${CUDA_cudnn_LIBRARY}
545+
cuDNN::cuDNN
544546
${CMAKE_DL_LIBS}
545547
)
546548

0 commit comments

Comments
 (0)