1#===============================================================================
2# Copyright 2020-2021 Intel Corporation
3# Copyright 2020 Codeplay Software Limited
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#===============================================================================
17
18find_package(CUDA 10.0 REQUIRED)
19
20set(cudnn_inc_dir_hints "${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES};/usr/local/cuda/include")
21find_path(CUDNN_INCLUDE_DIR "cudnn.h" HINTS ${cudnn_inc_dir_hints})
22
23find_library(CUDNN_LIBRARY cudnn)
24find_library(CUDA_DRIVER_LIBRARY cuda)
25# this is work around to avoid duplication half creation in both cuda and SYCL
26
27find_package(Threads REQUIRED)
28
29include(FindPackageHandleStandardArgs)
30
31find_library(
32    CUDNN_LIBRARY cudnn
33    HINTS ${CUDA_TOOLKIT_ROOT_DIR}
34    PATH_SUFFIXES lib lib64 bin)
35
36find_package_handle_standard_args(cuDNN
37    REQUIRED_VARS
38        CUDNN_INCLUDE_DIR
39        CUDA_INCLUDE_DIRS
40        CUDNN_LIBRARY
41        CUDA_LIBRARIES
42        CUDA_DRIVER_LIBRARY
43)
44
45# extract version from the include
46if(CUDNN_INCLUDE_DIR)
47  # cuDNN v8 has a separate header file with version defined
48  if (EXISTS "${CUDNN_INCLUDE_DIR}/cudnn_version.h")
49    file(READ "${CUDNN_INCLUDE_DIR}/cudnn_version.h" CUDNN_H_CONTENTS)
50  else()
51    file(READ "${CUDNN_INCLUDE_DIR}/cudnn.h" CUDNN_H_CONTENTS)
52  endif()
53
54  string(REGEX MATCH "define CUDNN_MAJOR ([0-9]+)" _ "${CUDNN_H_CONTENTS}")
55  set(CUDNN_MAJOR_VERSION ${CMAKE_MATCH_1} CACHE INTERNAL "")
56  string(REGEX MATCH "define CUDNN_MINOR ([0-9]+)" _ "${CUDNN_H_CONTENTS}")
57  set(CUDNN_MINOR_VERSION ${CMAKE_MATCH_1} CACHE INTERNAL "")
58  string(REGEX MATCH "define CUDNN_PATCHLEVEL ([0-9]+)" _ "${CUDNN_H_CONTENTS}")
59  set(CUDNN_PATCH_VERSION ${CMAKE_MATCH_1} CACHE INTERNAL "")
60
61  set(CUDNN_VERSION
62    "${CUDNN_MAJOR_VERSION}.${CUDNN_MINOR_VERSION}.${CUDNN_PATCH_VERSION}"
63  )
64  message(STATUS "cuDNN version: ${CUDNN_VERSION}")
65
66  unset(CUDNN_H_CONTENTS)
67endif()
68
69if(NOT TARGET cuDNN::cuDNN)
70  add_library(cuDNN::cuDNN SHARED IMPORTED)
71  set_target_properties(cuDNN::cuDNN PROPERTIES
72      IMPORTED_LOCATION
73      ${CUDNN_LIBRARY}
74      INTERFACE_INCLUDE_DIRECTORIES
75      "${CUDA_INCLUDE_DIRS};${CUDNN_INCLUDE_DIR}"
76      INTERFACE_LINK_LIBRARIES
77      "Threads::Threads;${CUDA_DRIVER_LIBRARY};${CUDA_LIBRARIES}"
78      INTERFACE_COMPILE_DEFINITIONS
79      CUDA_NO_HALF)
80endif()
81