Skip to content

Commit bb5d5e6

Browse files
committed
Merge branch 'master' of https://github.com/smu-sc-gj/dlib into smu-sc-gj-master
2 parents 56b806d + 54e1dfe commit bb5d5e6

File tree

6 files changed

+130
-208
lines changed

6 files changed

+130
-208
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.10.0)
1+
cmake_minimum_required(VERSION 3.17.0)
22

33
project(dlib_project)
44

dlib/CMakeLists.txt

Lines changed: 36 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if(POLICY CMP0077)
1313
cmake_policy(SET CMP0077 NEW)
1414
endif()
1515

16-
project(dlib)
16+
project(dlib LANGUAGES C CXX)
1717

1818
set(CPACK_PACKAGE_NAME "dlib")
1919
set(CPACK_PACKAGE_VERSION_MAJOR "20")
@@ -26,6 +26,7 @@ if (NOT TARGET dlib)
2626
message(STATUS "Compiling dlib version: ${VERSION}")
2727
endif()
2828

29+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake_utils)
2930

3031
include(cmake_utils/set_compiler_specific_options.cmake)
3132

@@ -104,17 +105,6 @@ elseif(BUILD_SHARED_LIBS)
104105
endif()
105106
endif()
106107

107-
108-
if (CMAKE_VERSION VERSION_LESS "3.9.0")
109-
# Set only because there are old target_link_libraries() statements in the
110-
# FindCUDA.cmake file that comes with CMake that error out if the new behavior
111-
# is used. In newer versions of CMake we can instead set CUDA_LINK_LIBRARIES_KEYWORD which fixes this issue.
112-
cmake_policy(SET CMP0023 OLD)
113-
else()
114-
set(CUDA_LINK_LIBRARIES_KEYWORD PUBLIC)
115-
endif()
116-
117-
118108
macro (enable_preprocessor_switch option_name)
119109
list(APPEND active_preprocessor_switches "-D${option_name}")
120110
endmacro()
@@ -649,178 +639,40 @@ if (NOT TARGET dlib)
649639

650640

651641
if (DLIB_USE_CUDA)
652-
find_package(CUDA 7.5)
653-
654-
if (CUDA_VERSION VERSION_GREATER 9.1 AND CMAKE_VERSION VERSION_LESS 3.12.2)
655-
# This bit of weirdness is to work around a bug in cmake
656-
list(REMOVE_ITEM CUDA_CUBLAS_LIBRARIES "CUDA_cublas_device_LIBRARY-NOTFOUND")
657-
endif()
658-
659-
660-
if (CUDA_FOUND AND MSVC AND NOT CUDA_CUBLAS_LIBRARIES AND "${CMAKE_SIZEOF_VOID_P}" EQUAL "4")
661-
message(WARNING "You have CUDA installed, but we can't use it unless you put visual studio in 64bit mode.")
662-
set(CUDA_FOUND 0)
663-
endif()
664-
665-
if (NOT CUDA_CUBLAS_LIBRARIES)
666-
message(STATUS "Found CUDA, but CMake was unable to find the cuBLAS libraries that should be part of every basic CUDA "
667-
"install. Your CUDA install is somehow broken or incomplete. Since cuBLAS is required for dlib to use CUDA we won't use CUDA.")
668-
set(CUDA_FOUND 0)
669-
endif()
670-
671-
if (CUDA_FOUND)
672-
673-
# There is some bug in cmake that causes it to mess up the
674-
# -std=c++11 option if you let it propagate it to nvcc in some
675-
# cases. So instead we disable this and manually include
676-
# things from CMAKE_CXX_FLAGS in the CUDA_NVCC_FLAGS list below.
677-
if (APPLE)
678-
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
679-
# Grab all the -D flags from CMAKE_CXX_FLAGS so we can pass them
680-
# to nvcc.
681-
string(REGEX MATCHALL "-D[^ ]*" FLAGS_FOR_NVCC "${CMAKE_CXX_FLAGS}")
682-
683-
# Check if we are being built as part of a pybind11 module.
684-
if (COMMAND pybind11_add_module)
685-
# Don't export unnecessary symbols.
686-
list(APPEND FLAGS_FOR_NVCC "-Xcompiler=-fvisibility=hidden")
687-
endif()
688-
endif()
689-
690-
set(CUDA_HOST_COMPILATION_CPP ON)
691-
string(REPLACE "," ";" DLIB_CUDA_COMPUTE_CAPABILITIES ${DLIB_USE_CUDA_COMPUTE_CAPABILITIES})
692-
foreach(CAP ${DLIB_CUDA_COMPUTE_CAPABILITIES})
693-
list(APPEND CUDA_NVCC_FLAGS "-gencode arch=compute_${CAP},code=[sm_${CAP},compute_${CAP}]")
694-
endforeach()
695-
# Note that we add __STRICT_ANSI__ to avoid freaking out nvcc with gcc specific
696-
# magic in the standard C++ header files (since nvcc uses gcc headers on linux).
697-
list(APPEND CUDA_NVCC_FLAGS "-D__STRICT_ANSI__;-D_MWAITXINTRIN_H_INCLUDED;-D_FORCE_INLINES;${FLAGS_FOR_NVCC}")
698-
list(APPEND CUDA_NVCC_FLAGS ${active_preprocessor_switches})
699-
if (NOT DLIB_IN_PROJECT_BUILD)
700-
LIST(APPEND CUDA_NVCC_FLAGS -DDLIB__CMAKE_GENERATED_A_CONFIG_H_FILE)
701-
endif()
702-
if (NOT MSVC)
703-
list(APPEND CUDA_NVCC_FLAGS "-std=c++14")
704-
endif()
705-
if (CMAKE_POSITION_INDEPENDENT_CODE)
706-
# sometimes this setting isn't propagated to NVCC, which then causes the
707-
# compile to fail. So make sure it's propagated.
708-
if (NOT MSVC) # Visual studio doesn't have -fPIC so don't do it in that case.
709-
list(APPEND CUDA_NVCC_FLAGS "-Xcompiler -fPIC")
710-
endif()
711-
endif()
712-
713-
include(cmake_utils/test_for_cudnn/find_cudnn.txt)
714-
715-
if (cudnn AND cudnn_include AND NOT DEFINED cuda_test_compile_worked AND NOT DEFINED cudnn_test_compile_worked)
716-
# make sure cuda is really working by doing a test compile
717-
message(STATUS "Building a CUDA test project to see if your compiler is compatible with CUDA...")
718-
719-
set(CUDA_TEST_CMAKE_FLAGS
720-
"-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}"
721-
"-DCMAKE_INCLUDE_PATH=${CMAKE_INCLUDE_PATH}"
722-
"-DCMAKE_LIBRARY_PATH=${CMAKE_LIBRARY_PATH}")
723-
724-
if (NOT MSVC) # see https://github.com/davisking/dlib/issues/363
725-
list(APPEND CUDA_TEST_CMAKE_FLAGS "-DCUDA_HOST_COMPILER=${CUDA_HOST_COMPILER}")
726-
endif()
727-
728-
try_compile(cuda_test_compile_worked
729-
${PROJECT_BINARY_DIR}/cuda_test_build
730-
${PROJECT_SOURCE_DIR}/cmake_utils/test_for_cuda cuda_test
731-
CMAKE_FLAGS ${CUDA_TEST_CMAKE_FLAGS}
732-
OUTPUT_VARIABLE try_compile_output_message
642+
find_package(CUDAToolkit)
643+
644+
if (CUDAToolkit_FOUND)
645+
enable_language(CUDA)
646+
647+
find_package(CUDNN)
648+
649+
if(CUDNN_FOUND)
650+
set(source_files ${source_files}
651+
cuda/cuda_dlib.cu
652+
cuda/cudnn_dlibapi.cpp
653+
cuda/cublas_dlibapi.cpp
654+
cuda/cusolver_dlibapi.cu
655+
cuda/curand_dlibapi.cpp
656+
cuda/cuda_data_ptr.cpp
657+
cuda/gpu_data.cpp
733658
)
734-
if (NOT cuda_test_compile_worked)
735-
string(REPLACE "\n" "\n *** " try_compile_output_message "${try_compile_output_message}")
736-
message(STATUS "*****************************************************************************************************************")
737-
message(STATUS "*** CUDA was found but your compiler failed to compile a simple CUDA program so dlib isn't going to use CUDA. ")
738-
message(STATUS "*** The output of the failed CUDA test compile is shown below: ")
739-
message(STATUS "*** ")
740-
message(STATUS "*** ${try_compile_output_message}")
741-
message(STATUS "*****************************************************************************************************************")
742-
else()
743-
message(STATUS "Building a cuDNN test project to check if you have the right version of cuDNN installed...")
744-
try_compile(cudnn_test_compile_worked
745-
${PROJECT_BINARY_DIR}/cudnn_test_build
746-
${PROJECT_SOURCE_DIR}/cmake_utils/test_for_cudnn cudnn_test
747-
CMAKE_FLAGS ${CUDA_TEST_CMAKE_FLAGS}
748-
OUTPUT_VARIABLE try_compile_output_message
749-
)
750-
if (NOT cudnn_test_compile_worked)
751-
string(REPLACE "\n" "\n *** " try_compile_output_message "${try_compile_output_message}")
752-
message(STATUS "*****************************************************************************************************")
753-
message(STATUS "*** Found cuDNN, but we failed to compile the dlib/cmake_utils/test_for_cudnn project. ")
754-
message(STATUS "*** You either have an unsupported version of cuDNN or something is wrong with your cudDNN install.")
755-
message(STATUS "*** Since a functional cuDNN is not found DLIB WILL NOT USE CUDA. ")
756-
message(STATUS "*** The output of the failed test_for_cudnn build is: ")
757-
message(STATUS "*** ")
758-
message(STATUS "*** ${try_compile_output_message}")
759-
message(STATUS "*****************************************************************************************************")
760-
endif()
659+
list (APPEND dlib_needed_private_libraries CUDA::cublas)
660+
list (APPEND dlib_needed_private_libraries ${CUDNN_LIBRARY_PATH})
661+
list (APPEND dlib_needed_private_libraries CUDA::curand)
662+
list (APPEND dlib_needed_private_libraries CUDA::cusolver)
663+
list (APPEND dlib_needed_private_libraries CUDA::cudart)
664+
if(openmp_libraries)
665+
list (APPEND dlib_needed_private_libraries ${openmp_libraries})
761666
endif()
762-
endif()
763667

764-
# Find where cuSOLVER is since the FindCUDA cmake package doesn't
765-
# bother to look for it in older versions of cmake.
766-
if (NOT CUDA_cusolver_LIBRARY)
767-
get_filename_component(cuda_blas_path "${CUDA_CUBLAS_LIBRARIES}" DIRECTORY)
768-
find_library(CUDA_cusolver_LIBRARY cusolver HINTS ${cuda_blas_path})
769-
# CUDA 10.1 doesn't install symbolic links to libcusolver.so in
770-
# the usual place. This is probably a bug in the cuda
771-
# installer. In any case, If we haven't found cusolver yet go
772-
# look in the cuda install folder for it. New versions of cmake
773-
# do this correctly, but older versions need help.
774-
if (NOT CUDA_cusolver_LIBRARY)
775-
find_library(CUDA_cusolver_LIBRARY cusolver HINTS
776-
/usr/local/cuda/lib64/
777-
)
778-
endif()
779-
mark_as_advanced(CUDA_cusolver_LIBRARY)
780-
endif()
781-
# Also find OpenMP since cuSOLVER needs it. Importantly, we only
782-
# look for one to link to if our use of BLAS, specifically the
783-
# Intel MKL, hasn't already decided what to use. This is because
784-
# it makes the MKL bug out if you link to another openmp lib other
785-
# than Intel's when you use the MKL. I'm also not really sure when
786-
# explicit linking to openmp became unnecessary, but for
787-
# sufficiently older versions of cuda it was needed. Then in
788-
# versions of cmake newer than 3.11 linking to openmp started to
789-
# mess up the switches passed to nvcc, so you can't just leave
790-
# these "try to link to openmp" statements here going forward. Fun
791-
# times.
792-
if (CUDA_VERSION VERSION_LESS "9.1" AND NOT openmp_libraries AND NOT MSVC AND NOT XCODE AND NOT APPLE)
793-
find_package(OpenMP)
794-
if (OPENMP_FOUND)
795-
set(openmp_libraries ${OpenMP_CXX_FLAGS})
796-
else()
797-
message(STATUS "*** Didn't find OpenMP, which is required to use CUDA. ***")
798-
set(CUDA_FOUND 0)
668+
include_directories(${CUDAToolkit_INCLUDE_DIRS} ${CUDNN_INCLUDE_PATH})
669+
message(STATUS "Enabling CUDA support for dlib. DLIB WILL USE CUDA, compute capabilities: ${DLIB_CUDA_COMPUTE_CAPABILITIES}")
670+
else()
671+
set(DLIB_USE_CUDA OFF CACHE STRING ${DLIB_USE_BLAS_STR} FORCE )
672+
toggle_preprocessor_switch(DLIB_USE_CUDA)
673+
message(STATUS "DID NOT FIND CUDNN")
674+
message(STATUS "Disabling CUDA support for dlib. DLIB WILL NOT USE CUDA")
799675
endif()
800-
endif()
801-
endif()
802-
803-
if (CUDA_FOUND AND cudnn AND cuda_test_compile_worked AND cudnn_test_compile_worked AND cudnn_include)
804-
set(source_files ${source_files}
805-
cuda/cuda_dlib.cu
806-
cuda/cudnn_dlibapi.cpp
807-
cuda/cublas_dlibapi.cpp
808-
cuda/cusolver_dlibapi.cu
809-
cuda/curand_dlibapi.cpp
810-
cuda/cuda_data_ptr.cpp
811-
cuda/gpu_data.cpp
812-
)
813-
list (APPEND dlib_needed_private_libraries ${CUDA_CUBLAS_LIBRARIES})
814-
list (APPEND dlib_needed_private_libraries ${cudnn})
815-
list (APPEND dlib_needed_private_libraries ${CUDA_curand_LIBRARY})
816-
list (APPEND dlib_needed_private_libraries ${CUDA_cusolver_LIBRARY})
817-
list (APPEND dlib_needed_private_libraries ${CUDA_CUDART_LIBRARY})
818-
if(openmp_libraries)
819-
list (APPEND dlib_needed_private_libraries ${openmp_libraries})
820-
endif()
821-
822-
include_directories(${cudnn_include})
823-
message(STATUS "Enabling CUDA support for dlib. DLIB WILL USE CUDA, compute capabilities: ${DLIB_CUDA_COMPUTE_CAPABILITIES}")
824676
else()
825677
set(DLIB_USE_CUDA OFF CACHE STRING ${DLIB_USE_BLAS_STR} FORCE )
826678
toggle_preprocessor_switch(DLIB_USE_CUDA)
@@ -875,14 +727,10 @@ if (NOT TARGET dlib)
875727
endif()
876728
endif()
877729

878-
# Tell CMake to build dlib via add_library()/cuda_add_library()
879-
if (DLIB_USE_CUDA)
880-
# The old cuda_add_library() command doesn't support CMake's newer dependency
881-
# stuff, so we have to set the include path manually still, which we do here.
882-
include_directories(${dlib_needed_public_includes})
883-
cuda_add_library(dlib ${source_files} )
884-
else()
885-
add_library(dlib ${source_files} )
730+
add_library(dlib ${source_files})
731+
732+
if(DLIB_USE_CUDA)
733+
set_property(TARGET dlib PROPERTY CUDA_ARCHITECTURES all)
886734
endif()
887735

888736
endif () ##### end of if NOT DLIB_ISO_CPP_ONLY ##########################################################

dlib/cmake_utils/FindCUDNN.cmake

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Find the CUDNN libraries
2+
#
3+
# The following variables are optionally searched for defaults
4+
# CUDNN_ROOT: Base directory where CUDNN is found
5+
# CUDNN_INCLUDE_DIR: Directory where CUDNN header is searched for
6+
# CUDNN_LIBRARY: Directory where CUDNN library is searched for
7+
# CUDNN_STATIC: Are we looking for a static library? (default: no)
8+
#
9+
# The following are set after configuration is done:
10+
# CUDNN_FOUND
11+
# CUDNN_INCLUDE_PATH
12+
# CUDNN_LIBRARY_PATH
13+
#
14+
15+
include(FindPackageHandleStandardArgs)
16+
17+
set(CUDNN_ROOT $ENV{CUDNN_ROOT_DIR} CACHE PATH "Folder containing NVIDIA cuDNN")
18+
if (DEFINED $ENV{CUDNN_ROOT_DIR})
19+
message(WARNING "CUDNN_ROOT_DIR is deprecated. Please set CUDNN_ROOT instead.")
20+
endif()
21+
list(APPEND CUDNN_ROOT $ENV{CUDNN_ROOT_DIR} ${CUDA_TOOLKIT_ROOT_DIR})
22+
23+
# Compatible layer for CMake <3.12. CUDNN_ROOT will be accounted in for searching paths and libraries for CMake >=3.12.
24+
list(APPEND CMAKE_PREFIX_PATH ${CUDNN_ROOT})
25+
26+
set(CUDNN_INCLUDE_DIR $ENV{CUDNN_INCLUDE_DIR} CACHE PATH "Folder containing NVIDIA cuDNN header files")
27+
28+
find_path(CUDNN_INCLUDE_PATH cudnn.h
29+
HINTS ${CUDNN_INCLUDE_DIR} ENV CUDNN_INCLUDE_DIR ENV CUDNN_HOME
30+
PATHS /usr/local /usr/local/cuda "C:/Program Files/NVIDIA/CUDNN/*/include/${CUDA_VERSION}" "C:/Program Files/NVIDIA/CUDNN/*/include/*" ENV CPATH
31+
PATH_SUFFIXES cuda/include cuda include)
32+
33+
option(CUDNN_STATIC "Look for static CUDNN" OFF)
34+
if (CUDNN_STATIC)
35+
set(CUDNN_LIBNAME "libcudnn_static.a")
36+
else()
37+
set(CUDNN_LIBNAME "cudnn")
38+
endif()
39+
40+
set(CUDNN_LIBRARY $ENV{CUDNN_LIBRARY} CACHE PATH "Path to the cudnn library file (e.g., libcudnn.so)")
41+
if (CUDNN_LIBRARY MATCHES ".*cudnn_static.a" AND NOT CUDNN_STATIC)
42+
message(WARNING "CUDNN_LIBRARY points to a static library (${CUDNN_LIBRARY}) but CUDNN_STATIC is OFF.")
43+
endif()
44+
45+
find_library(CUDNN_LIBRARY_PATH ${CUDNN_LIBNAME}
46+
PATHS ${CUDNN_LIBRARY} /usr/local /usr/local/cuda "C:/Program Files/NVIDIA/CUDNN/*/lib/${CUDA_VERSION}" "C:/Program Files/NVIDIA/CUDNN/*/lib/*" ENV LD_LIBRARY_PATH
47+
PATH_SUFFIXES lib lib64 cuda/lib cuda/lib64 lib/x64)
48+
49+
find_package_handle_standard_args(CUDNN DEFAULT_MSG CUDNN_LIBRARY_PATH CUDNN_INCLUDE_PATH)
50+
51+
if(CUDNN_FOUND)
52+
# Get cuDNN version
53+
if(EXISTS ${CUDNN_INCLUDE_PATH}/cudnn_version.h)
54+
file(READ ${CUDNN_INCLUDE_PATH}/cudnn_version.h CUDNN_HEADER_CONTENTS)
55+
else()
56+
file(READ ${CUDNN_INCLUDE_PATH}/cudnn.h CUDNN_HEADER_CONTENTS)
57+
endif()
58+
string(REGEX MATCH "define CUDNN_MAJOR * +([0-9]+)"
59+
CUDNN_VERSION_MAJOR "${CUDNN_HEADER_CONTENTS}")
60+
string(REGEX REPLACE "define CUDNN_MAJOR * +([0-9]+)" "\\1"
61+
CUDNN_VERSION_MAJOR "${CUDNN_VERSION_MAJOR}")
62+
string(REGEX MATCH "define CUDNN_MINOR * +([0-9]+)"
63+
CUDNN_VERSION_MINOR "${CUDNN_HEADER_CONTENTS}")
64+
string(REGEX REPLACE "define CUDNN_MINOR * +([0-9]+)" "\\1"
65+
CUDNN_VERSION_MINOR "${CUDNN_VERSION_MINOR}")
66+
string(REGEX MATCH "define CUDNN_PATCHLEVEL * +([0-9]+)"
67+
CUDNN_VERSION_PATCH "${CUDNN_HEADER_CONTENTS}")
68+
string(REGEX REPLACE "define CUDNN_PATCHLEVEL * +([0-9]+)" "\\1"
69+
CUDNN_VERSION_PATCH "${CUDNN_VERSION_PATCH}")
70+
# Assemble cuDNN version
71+
if(NOT CUDNN_VERSION_MAJOR)
72+
set(CUDNN_VERSION "?")
73+
else()
74+
set(CUDNN_VERSION
75+
"${CUDNN_VERSION_MAJOR}.${CUDNN_VERSION_MINOR}.${CUDNN_VERSION_PATCH}")
76+
endif()
77+
endif()
78+
79+
mark_as_advanced(CUDNN_ROOT CUDNN_INCLUDE_DIR CUDNN_LIBRARY CUDNN_VERSION)

dlib/cmake_utils/test_for_cuda/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ include_directories(../../cuda)
66
add_definitions(-DDLIB_USE_CUDA)
77

88
# Override the FindCUDA.cmake setting to avoid duplication of host flags if using a toolchain:
9+
# Do we still need this?
910
option(CUDA_PROPAGATE_HOST_FLAGS "Propage C/CXX_FLAGS and friends to the host compiler via -Xcompile" OFF)
10-
find_package(CUDA 7.5 REQUIRED)
11+
12+
enable_language(CUDA)
13+
find_package(CUDAToolkit)
14+
1115
set(CUDA_HOST_COMPILATION_CPP ON)
1216
list(APPEND CUDA_NVCC_FLAGS "-arch=sm_50;-std=c++14;-D__STRICT_ANSI__;-D_MWAITXINTRIN_H_INCLUDED;-D_FORCE_INLINES")
1317

14-
cuda_add_library(cuda_test STATIC cuda_test.cu )
18+
add_library(cuda_test STATIC cuda_test.cu )
19+
set_property(TARGET cuda_test PROPERTY CUDA_ARCHITECTURES all)

dlib/cmake_utils/test_for_cudnn/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ cmake_minimum_required(VERSION 3.10.0)
33
project(cudnn_test)
44

55
# Override the FindCUDA.cmake setting to avoid duplication of host flags if using a toolchain:
6+
# Do we still need this?
67
option(CUDA_PROPAGATE_HOST_FLAGS "Propage C/CXX_FLAGS and friends to the host compiler via -Xcompile" OFF)
7-
find_package(CUDA 7.5 REQUIRED)
8+
9+
enable_language(CUDA)
10+
find_package(CUDAToolkit)
11+
812
set(CUDA_HOST_COMPILATION_CPP ON)
913
list(APPEND CUDA_NVCC_FLAGS "-arch=sm_50;-std=c++14;-D__STRICT_ANSI__")
1014
add_definitions(-DDLIB_USE_CUDA)
@@ -13,6 +17,7 @@ include(find_cudnn.txt)
1317

1418
if (cudnn_include AND cudnn)
1519
include_directories(${cudnn_include})
16-
cuda_add_library(cudnn_test STATIC ../../cuda/cudnn_dlibapi.cpp ${cudnn} )
20+
add_library(cudnn_test STATIC ../../cuda/cudnn_dlibapi.cpp ${cudnn} )
21+
set_property(TARGET cudnn_test PROPERTY CUDA_ARCHITECTURES all)
1722
target_compile_features(cudnn_test PUBLIC cxx_std_14)
1823
endif()

0 commit comments

Comments
 (0)