From e254b70e5027a19b97e1942d230a8bdf4999cac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20M=C3=B6ller?= Date: Fri, 6 Sep 2024 19:37:57 +0200 Subject: [PATCH 1/5] replaces cmake CUDA finder with FindCUDAToolkit --- .github/workflows/build_cpp.yml | 1 + dlib/CMakeLists.txt | 190 +++---------------------------- dlib/cmake_utils/findCUDNN.cmake | 76 +++++++++++++ 3 files changed, 95 insertions(+), 172 deletions(-) create mode 100644 dlib/cmake_utils/findCUDNN.cmake diff --git a/.github/workflows/build_cpp.yml b/.github/workflows/build_cpp.yml index 7a309065b7..e7ebcd097f 100644 --- a/.github/workflows/build_cpp.yml +++ b/.github/workflows/build_cpp.yml @@ -4,6 +4,7 @@ on: push: branches: - master + - cuda-cmake paths: - ".github/workflows/build_cpp.yml" - "**.cpp" diff --git a/dlib/CMakeLists.txt b/dlib/CMakeLists.txt index 44045ef3c2..488a66967e 100644 --- a/dlib/CMakeLists.txt +++ b/dlib/CMakeLists.txt @@ -4,7 +4,7 @@ # -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) set(CMAKE_DISABLE_SOURCE_CHANGES ON) set(CMAKE_DISABLE_IN_SOURCE_BUILD ON) @@ -647,160 +647,16 @@ if (NOT TARGET dlib) endif() endif() + find_package(OpenMP) + set(openmp_libraries ${OpenMP_CXX_FLAGS}) - if (DLIB_USE_CUDA) - find_package(CUDA 7.5) - - if (CUDA_VERSION VERSION_GREATER 9.1 AND CMAKE_VERSION VERSION_LESS 3.12.2) - # This bit of weirdness is to work around a bug in cmake - list(REMOVE_ITEM CUDA_CUBLAS_LIBRARIES "CUDA_cublas_device_LIBRARY-NOTFOUND") - endif() - - - if (CUDA_FOUND AND MSVC AND NOT CUDA_CUBLAS_LIBRARIES AND "${CMAKE_SIZEOF_VOID_P}" EQUAL "4") - message(WARNING "You have CUDA installed, but we can't use it unless you put visual studio in 64bit mode.") - set(CUDA_FOUND 0) - endif() - - if (NOT CUDA_CUBLAS_LIBRARIES) - message(STATUS "Found CUDA, but CMake was unable to find the cuBLAS libraries that should be part of every basic CUDA " - "install. Your CUDA install is somehow broken or incomplete. Since cuBLAS is required for dlib to use CUDA we won't use CUDA.") - set(CUDA_FOUND 0) - endif() - - if (CUDA_FOUND) - - # There is some bug in cmake that causes it to mess up the - # -std=c++11 option if you let it propagate it to nvcc in some - # cases. So instead we disable this and manually include - # things from CMAKE_CXX_FLAGS in the CUDA_NVCC_FLAGS list below. - if (APPLE) - set(CUDA_PROPAGATE_HOST_FLAGS OFF) - # Grab all the -D flags from CMAKE_CXX_FLAGS so we can pass them - # to nvcc. - string(REGEX MATCHALL "-D[^ ]*" FLAGS_FOR_NVCC "${CMAKE_CXX_FLAGS}") - - # Check if we are being built as part of a pybind11 module. - if (COMMAND pybind11_add_module) - # Don't export unnecessary symbols. - list(APPEND FLAGS_FOR_NVCC "-Xcompiler=-fvisibility=hidden") - endif() - endif() - - set(CUDA_HOST_COMPILATION_CPP ON) - string(REPLACE "," ";" DLIB_CUDA_COMPUTE_CAPABILITIES ${DLIB_USE_CUDA_COMPUTE_CAPABILITIES}) - foreach(CAP ${DLIB_CUDA_COMPUTE_CAPABILITIES}) - list(APPEND CUDA_NVCC_FLAGS "-gencode arch=compute_${CAP},code=[sm_${CAP},compute_${CAP}]") - endforeach() - # Note that we add __STRICT_ANSI__ to avoid freaking out nvcc with gcc specific - # magic in the standard C++ header files (since nvcc uses gcc headers on linux). - list(APPEND CUDA_NVCC_FLAGS "-D__STRICT_ANSI__;-D_MWAITXINTRIN_H_INCLUDED;-D_FORCE_INLINES;${FLAGS_FOR_NVCC}") - list(APPEND CUDA_NVCC_FLAGS ${active_preprocessor_switches}) - if (NOT DLIB_IN_PROJECT_BUILD) - LIST(APPEND CUDA_NVCC_FLAGS -DDLIB__CMAKE_GENERATED_A_CONFIG_H_FILE) - endif() - if (NOT MSVC) - list(APPEND CUDA_NVCC_FLAGS "-std=c++14") - endif() - if (CMAKE_POSITION_INDEPENDENT_CODE) - # sometimes this setting isn't propagated to NVCC, which then causes the - # compile to fail. So make sure it's propagated. - if (NOT MSVC) # Visual studio doesn't have -fPIC so don't do it in that case. - list(APPEND CUDA_NVCC_FLAGS "-Xcompiler -fPIC") - endif() - endif() - - include(cmake_utils/test_for_cudnn/find_cudnn.txt) - - if (cudnn AND cudnn_include AND NOT DEFINED cuda_test_compile_worked AND NOT DEFINED cudnn_test_compile_worked) - # make sure cuda is really working by doing a test compile - message(STATUS "Building a CUDA test project to see if your compiler is compatible with CUDA...") - set(CUDA_TEST_CMAKE_FLAGS - "-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" - "-DCMAKE_INCLUDE_PATH=${CMAKE_INCLUDE_PATH}" - "-DCMAKE_LIBRARY_PATH=${CMAKE_LIBRARY_PATH}") - - if (NOT MSVC) # see https://github.com/davisking/dlib/issues/363 - list(APPEND CUDA_TEST_CMAKE_FLAGS "-DCUDA_HOST_COMPILER=${CUDA_HOST_COMPILER}") - endif() - - try_compile(cuda_test_compile_worked - ${PROJECT_BINARY_DIR}/cuda_test_build - ${PROJECT_SOURCE_DIR}/cmake_utils/test_for_cuda cuda_test - CMAKE_FLAGS ${CUDA_TEST_CMAKE_FLAGS} - OUTPUT_VARIABLE try_compile_output_message - ) - if (NOT cuda_test_compile_worked) - string(REPLACE "\n" "\n *** " try_compile_output_message "${try_compile_output_message}") - message(STATUS "*****************************************************************************************************************") - message(STATUS "*** CUDA was found but your compiler failed to compile a simple CUDA program so dlib isn't going to use CUDA. ") - message(STATUS "*** The output of the failed CUDA test compile is shown below: ") - message(STATUS "*** ") - message(STATUS "*** ${try_compile_output_message}") - message(STATUS "*****************************************************************************************************************") - else() - message(STATUS "Building a cuDNN test project to check if you have the right version of cuDNN installed...") - try_compile(cudnn_test_compile_worked - ${PROJECT_BINARY_DIR}/cudnn_test_build - ${PROJECT_SOURCE_DIR}/cmake_utils/test_for_cudnn cudnn_test - CMAKE_FLAGS ${CUDA_TEST_CMAKE_FLAGS} - OUTPUT_VARIABLE try_compile_output_message - ) - if (NOT cudnn_test_compile_worked) - string(REPLACE "\n" "\n *** " try_compile_output_message "${try_compile_output_message}") - message(STATUS "*****************************************************************************************************") - message(STATUS "*** Found cuDNN, but we failed to compile the dlib/cmake_utils/test_for_cudnn project. ") - message(STATUS "*** You either have an unsupported version of cuDNN or something is wrong with your cudDNN install.") - message(STATUS "*** Since a functional cuDNN is not found DLIB WILL NOT USE CUDA. ") - message(STATUS "*** The output of the failed test_for_cudnn build is: ") - message(STATUS "*** ") - message(STATUS "*** ${try_compile_output_message}") - message(STATUS "*****************************************************************************************************") - endif() - endif() - endif() - - # Find where cuSOLVER is since the FindCUDA cmake package doesn't - # bother to look for it in older versions of cmake. - if (NOT CUDA_cusolver_LIBRARY) - get_filename_component(cuda_blas_path "${CUDA_CUBLAS_LIBRARIES}" DIRECTORY) - find_library(CUDA_cusolver_LIBRARY cusolver HINTS ${cuda_blas_path}) - # CUDA 10.1 doesn't install symbolic links to libcusolver.so in - # the usual place. This is probably a bug in the cuda - # installer. In any case, If we haven't found cusolver yet go - # look in the cuda install folder for it. New versions of cmake - # do this correctly, but older versions need help. - if (NOT CUDA_cusolver_LIBRARY) - find_library(CUDA_cusolver_LIBRARY cusolver HINTS - /usr/local/cuda/lib64/ - ) - endif() - mark_as_advanced(CUDA_cusolver_LIBRARY) - endif() - # Also find OpenMP since cuSOLVER needs it. Importantly, we only - # look for one to link to if our use of BLAS, specifically the - # Intel MKL, hasn't already decided what to use. This is because - # it makes the MKL bug out if you link to another openmp lib other - # than Intel's when you use the MKL. I'm also not really sure when - # explicit linking to openmp became unnecessary, but for - # sufficiently older versions of cuda it was needed. Then in - # versions of cmake newer than 3.11 linking to openmp started to - # mess up the switches passed to nvcc, so you can't just leave - # these "try to link to openmp" statements here going forward. Fun - # times. - if (CUDA_VERSION VERSION_LESS "9.1" AND NOT openmp_libraries AND NOT MSVC AND NOT XCODE AND NOT APPLE) - find_package(OpenMP) - if (OPENMP_FOUND) - set(openmp_libraries ${OpenMP_CXX_FLAGS}) - else() - message(STATUS "*** Didn't find OpenMP, which is required to use CUDA. ***") - set(CUDA_FOUND 0) - endif() - endif() - endif() + if (DLIB_USE_CUDA) + find_package(CUDAToolkit 9.1) + set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake_utils) + find_package(CUDNN) - if (CUDA_FOUND AND cudnn AND cuda_test_compile_worked AND cudnn_test_compile_worked AND cudnn_include) + if (CUDAToolkit_FOUND AND CUDNN_FOUND) set(source_files ${source_files} cuda/cuda_dlib.cu cuda/cudnn_dlibapi.cpp @@ -810,19 +666,17 @@ if (NOT TARGET dlib) cuda/cuda_data_ptr.cpp cuda/gpu_data.cpp ) - list (APPEND dlib_needed_private_libraries ${CUDA_CUBLAS_LIBRARIES}) - list (APPEND dlib_needed_private_libraries ${cudnn}) - list (APPEND dlib_needed_private_libraries ${CUDA_curand_LIBRARY}) - list (APPEND dlib_needed_private_libraries ${CUDA_cusolver_LIBRARY}) - list (APPEND dlib_needed_private_libraries ${CUDA_CUDART_LIBRARY}) - if(openmp_libraries) - list (APPEND dlib_needed_private_libraries ${openmp_libraries}) - endif() - - include_directories(${cudnn_include}) + list (APPEND dlib_needed_private_libraries CUDA::cublas) + list (APPEND dlib_needed_private_libraries ${CUDNN_LIBRARY_PATH}) + list (APPEND dlib_needed_private_libraries CUDA::curand) + list (APPEND dlib_needed_private_libraries CUDA::cusolver) + list (APPEND dlib_needed_private_libraries CUDA::cudart) + list (APPEND dlib_needed_private_libraries ${openmp_libraries}) + + include_directories(${CUDAToolkit_INCLUDE_DIRS} ${CUDNN_INCLUDE_PATH}) message(STATUS "Enabling CUDA support for dlib. DLIB WILL USE CUDA, compute capabilities: ${DLIB_CUDA_COMPUTE_CAPABILITIES}") else() - set(DLIB_USE_CUDA OFF CACHE STRING ${DLIB_USE_BLAS_STR} FORCE ) + set(DLIB_USE_CUDA OFF CACHE BOOL ${DLIB_USE_BLAS_STR} FORCE ) toggle_preprocessor_switch(DLIB_USE_CUDA) if (NOT CUDA_FOUND) message(STATUS "DID NOT FIND CUDA") @@ -875,15 +729,7 @@ if (NOT TARGET dlib) endif() endif() - # Tell CMake to build dlib via add_library()/cuda_add_library() - if (DLIB_USE_CUDA) - # The old cuda_add_library() command doesn't support CMake's newer dependency - # stuff, so we have to set the include path manually still, which we do here. - include_directories(${dlib_needed_public_includes}) - cuda_add_library(dlib ${source_files} ) - else() - add_library(dlib ${source_files} ) - endif() + add_library(dlib ${source_files} ) endif () ##### end of if NOT DLIB_ISO_CPP_ONLY ########################################################## diff --git a/dlib/cmake_utils/findCUDNN.cmake b/dlib/cmake_utils/findCUDNN.cmake new file mode 100644 index 0000000000..0c900fcd4e --- /dev/null +++ b/dlib/cmake_utils/findCUDNN.cmake @@ -0,0 +1,76 @@ +# Find the CUDNN libraries +# +# The following variables are optionally searched for defaults +# CUDNN_ROOT: Base directory where CUDNN is found +# CUDNN_INCLUDE_DIR: Directory where CUDNN header is searched for +# CUDNN_LIBRARY: Directory where CUDNN library is searched for +# CUDNN_STATIC: Are we looking for a static library? (default: no) +# +# The following are set after configuration is done: +# CUDNN_FOUND +# CUDNN_INCLUDE_PATH +# CUDNN_LIBRARY_PATH +# + +include(FindPackageHandleStandardArgs) + +set(CUDNN_ROOT $ENV{CUDNN_ROOT_DIR} CACHE PATH "Folder containing NVIDIA cuDNN") +if (DEFINED $ENV{CUDNN_ROOT_DIR}) + message(WARNING "CUDNN_ROOT_DIR is deprecated. Please set CUDNN_ROOT instead.") +endif() +list(APPEND CUDNN_ROOT $ENV{CUDNN_ROOT_DIR} ${CUDA_TOOLKIT_ROOT_DIR}) + +# Compatible layer for CMake <3.12. CUDNN_ROOT will be accounted in for searching paths and libraries for CMake >=3.12. +list(APPEND CMAKE_PREFIX_PATH ${CUDNN_ROOT}) + +set(CUDNN_INCLUDE_DIR $ENV{CUDNN_INCLUDE_DIR} CACHE PATH "Folder containing NVIDIA cuDNN header files") + +find_path(CUDNN_INCLUDE_PATH cudnn.h + HINTS ${CUDNN_INCLUDE_DIR} + PATH_SUFFIXES cuda/include cuda include) + +option(CUDNN_STATIC "Look for static CUDNN" OFF) +if (CUDNN_STATIC) + set(CUDNN_LIBNAME "libcudnn_static.a") +else() + set(CUDNN_LIBNAME "cudnn") +endif() + +set(CUDNN_LIBRARY $ENV{CUDNN_LIBRARY} CACHE PATH "Path to the cudnn library file (e.g., libcudnn.so)") +if (CUDNN_LIBRARY MATCHES ".*cudnn_static.a" AND NOT CUDNN_STATIC) + message(WARNING "CUDNN_LIBRARY points to a static library (${CUDNN_LIBRARY}) but CUDNN_STATIC is OFF.") +endif() + +find_library(CUDNN_LIBRARY_PATH ${CUDNN_LIBNAME} + PATHS ${CUDNN_LIBRARY} + PATH_SUFFIXES lib lib64 cuda/lib cuda/lib64 lib/x64) + +find_package_handle_standard_args(CUDNN DEFAULT_MSG CUDNN_LIBRARY_PATH CUDNN_INCLUDE_PATH) + +if(CUDNN_FOUND) + # Get cuDNN version + if(EXISTS ${CUDNN_INCLUDE_PATH}/cudnn_version.h) + file(READ ${CUDNN_INCLUDE_PATH}/cudnn_version.h CUDNN_HEADER_CONTENTS) + else() + file(READ ${CUDNN_INCLUDE_PATH}/cudnn.h CUDNN_HEADER_CONTENTS) + endif() + string(REGEX MATCH "define CUDNN_MAJOR * +([0-9]+)" + CUDNN_VERSION_MAJOR "${CUDNN_HEADER_CONTENTS}") + string(REGEX REPLACE "define CUDNN_MAJOR * +([0-9]+)" "\\1" + CUDNN_VERSION_MAJOR "${CUDNN_VERSION_MAJOR}") + string(REGEX MATCH "define CUDNN_MINOR * +([0-9]+)" + CUDNN_VERSION_MINOR "${CUDNN_HEADER_CONTENTS}") + string(REGEX REPLACE "define CUDNN_MINOR * +([0-9]+)" "\\1" + CUDNN_VERSION_MINOR "${CUDNN_VERSION_MINOR}") + string(REGEX MATCH "define CUDNN_PATCHLEVEL * +([0-9]+)" + CUDNN_VERSION_PATCH "${CUDNN_HEADER_CONTENTS}") + string(REGEX REPLACE "define CUDNN_PATCHLEVEL * +([0-9]+)" "\\1" + CUDNN_VERSION_PATCH "${CUDNN_VERSION_PATCH}") + # Assemble cuDNN version + if(NOT CUDNN_VERSION_MAJOR) + set(CUDNN_VERSION "?") + else() + set(CUDNN_VERSION + "${CUDNN_VERSION_MAJOR}.${CUDNN_VERSION_MINOR}.${CUDNN_VERSION_PATCH}") + endif() +endif() \ No newline at end of file From 5f94ad5d53a45e8e3e6301f04c1c5f95d9f55c84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20M=C3=B6ller?= Date: Fri, 6 Sep 2024 20:17:21 +0200 Subject: [PATCH 2/5] increase cmake version to 3.17 --- .github/workflows/build_cpp.yml | 15 +++++++-------- CMakeLists.txt | 2 +- dlib/CMakeLists.txt | 7 +++---- dlib/cmake | 2 +- ...k_if_avx_instructions_executable_on_host.cmake | 2 +- dlib/cmake_utils/check_if_neon_available.cmake | 2 +- ..._if_sse4_instructions_executable_on_host.cmake | 2 +- dlib/cmake_utils/find_ffmpeg.cmake | 2 +- dlib/cmake_utils/find_libjpeg.cmake | 2 +- dlib/cmake_utils/find_libpng.cmake | 2 +- .../set_compiler_specific_options.cmake | 2 +- ...tell_visual_studio_to_use_static_runtime.cmake | 2 +- dlib/cmake_utils/test_for_avx/CMakeLists.txt | 2 +- dlib/cmake_utils/test_for_cuda/CMakeLists.txt | 2 +- dlib/cmake_utils/test_for_cudnn/CMakeLists.txt | 2 +- dlib/cmake_utils/test_for_libjpeg/CMakeLists.txt | 2 +- dlib/cmake_utils/test_for_libjxl/CMakeLists.txt | 2 +- dlib/cmake_utils/test_for_libpng/CMakeLists.txt | 2 +- dlib/cmake_utils/test_for_libwebp/CMakeLists.txt | 2 +- dlib/cmake_utils/test_for_neon/CMakeLists.txt | 2 +- dlib/cmake_utils/test_for_sse4/CMakeLists.txt | 2 +- dlib/external/cblas/CMakeLists.txt | 2 +- dlib/external/pybind11/CMakeLists.txt | 6 +++--- dlib/java/CMakeLists.txt | 2 +- dlib/java/cmake_swig_jni | 4 ++-- dlib/matlab/CMakeLists.txt | 2 +- dlib/matlab/cmake_mex_wrapper | 2 +- dlib/test/CMakeLists.txt | 8 +++----- dlib/test/blas_bindings/CMakeLists.txt | 2 +- dlib/test/tools/CMakeLists.txt | 2 +- docs/docs/release_notes.xml | 2 +- examples/CMakeLists.txt | 2 +- tools/archive/CMakeLists.txt | 2 +- tools/convert_dlib_nets_to_caffe/CMakeLists.txt | 2 +- tools/htmlify/CMakeLists.txt | 2 +- tools/imglab/CMakeLists.txt | 2 +- tools/python/CMakeLists.txt | 2 +- 37 files changed, 50 insertions(+), 54 deletions(-) diff --git a/.github/workflows/build_cpp.yml b/.github/workflows/build_cpp.yml index e7ebcd097f..5889d9d68e 100644 --- a/.github/workflows/build_cpp.yml +++ b/.github/workflows/build_cpp.yml @@ -4,7 +4,6 @@ on: push: branches: - master - - cuda-cmake paths: - ".github/workflows/build_cpp.yml" - "**.cpp" @@ -31,7 +30,7 @@ defaults: working-directory: dlib/test jobs: - ubuntu-latest-gcc-default-cmake-3-8-ffmpeg5: + ubuntu-latest-gcc-default-cmake-3-17-ffmpeg5: runs-on: 'ubuntu-latest' steps: - uses: actions/checkout@v2 @@ -41,18 +40,18 @@ jobs: sudo apt update sudo apt install libwebp-dev make yasm - - name: Cache cmake 3.8.0 + - name: Cache cmake 3.17.0 uses: actions/cache@v3 id: cache-cmake-download with: # cache this folder: - path: ~/cmake-3.8.0-Linux-x86_64 - key: cmake-3.8.0_try3 + path: ~/cmake-3.17.0-Linux-x86_64 + key: cmake-3.17.0_try3 - run: | # Get the minimum version of cmake dlib supports - wget https://cmake.org/files/v3.8/cmake-3.8.0-Linux-x86_64.tar.gz - tar -xf cmake-3.8.0-Linux-x86_64.tar.gz -C ~ + wget https://cmake.org/files/v3.17/cmake-3.17.0-Linux-x86_64.tar.gz + tar -xf cmake-3.17.0-Linux-x86_64.tar.gz -C ~ if: steps.cache-cmake-download.outputs.cache-hit != 'true' - name: Cache FFmpeg 5 @@ -77,7 +76,7 @@ jobs: run: | mkdir build cd build - ~/cmake-3.8.0-Linux-x86_64/bin/cmake -DCMAKE_PREFIX_PATH=/home/runner/ffmpeg-n5.1.3_installation .. + ~/cmake-3.17.0-Linux-x86_64/bin/cmake -DCMAKE_PREFIX_PATH=/home/runner/ffmpeg-n5.1.3_installation .. - name: Build just tests run: | diff --git a/CMakeLists.txt b/CMakeLists.txt index da92454aab..ca00843f18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) project(dlib_project) diff --git a/dlib/CMakeLists.txt b/dlib/CMakeLists.txt index 488a66967e..e7bfd29c74 100644 --- a/dlib/CMakeLists.txt +++ b/dlib/CMakeLists.txt @@ -647,16 +647,15 @@ if (NOT TARGET dlib) endif() endif() - find_package(OpenMP) - set(openmp_libraries ${OpenMP_CXX_FLAGS}) - if (DLIB_USE_CUDA) find_package(CUDAToolkit 9.1) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake_utils) find_package(CUDNN) + find_package(OpenMP) + set(openmp_libraries ${OpenMP_CXX_FLAGS}) - if (CUDAToolkit_FOUND AND CUDNN_FOUND) + if (CUDAToolkit_FOUND AND CUDNN_FOUND AND OPENMP_FOUND) set(source_files ${source_files} cuda/cuda_dlib.cu cuda/cudnn_dlibapi.cpp diff --git a/dlib/cmake b/dlib/cmake index 224ba3a491..4a9d7b3985 100644 --- a/dlib/cmake +++ b/dlib/cmake @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) add_subdirectory(${CMAKE_CURRENT_LIST_DIR} dlib_build) diff --git a/dlib/cmake_utils/check_if_avx_instructions_executable_on_host.cmake b/dlib/cmake_utils/check_if_avx_instructions_executable_on_host.cmake index 4f2cfef933..717812678d 100644 --- a/dlib/cmake_utils/check_if_avx_instructions_executable_on_host.cmake +++ b/dlib/cmake_utils/check_if_avx_instructions_executable_on_host.cmake @@ -1,6 +1,6 @@ # This script checks if your compiler and host processor can generate and then run programs with AVX instructions. -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) # Don't rerun this script if its already been executed. if (DEFINED AVX_IS_AVAILABLE_ON_HOST) diff --git a/dlib/cmake_utils/check_if_neon_available.cmake b/dlib/cmake_utils/check_if_neon_available.cmake index 895c810b74..6ed6725cac 100644 --- a/dlib/cmake_utils/check_if_neon_available.cmake +++ b/dlib/cmake_utils/check_if_neon_available.cmake @@ -1,6 +1,6 @@ # This script checks if __ARM_NEON__ is defined for your compiler -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) # Don't rerun this script if its already been executed. if (DEFINED ARM_NEON_IS_AVAILABLE) diff --git a/dlib/cmake_utils/check_if_sse4_instructions_executable_on_host.cmake b/dlib/cmake_utils/check_if_sse4_instructions_executable_on_host.cmake index c475609970..7fbe182aed 100644 --- a/dlib/cmake_utils/check_if_sse4_instructions_executable_on_host.cmake +++ b/dlib/cmake_utils/check_if_sse4_instructions_executable_on_host.cmake @@ -1,6 +1,6 @@ # This script checks if your compiler and host processor can generate and then run programs with SSE4 instructions. -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) # Don't rerun this script if its already been executed. if (DEFINED SSE4_IS_AVAILABLE_ON_HOST) diff --git a/dlib/cmake_utils/find_ffmpeg.cmake b/dlib/cmake_utils/find_ffmpeg.cmake index b05955387b..4179013147 100644 --- a/dlib/cmake_utils/find_ffmpeg.cmake +++ b/dlib/cmake_utils/find_ffmpeg.cmake @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) message(STATUS "Searching for FFMPEG/LIBAV") find_package(PkgConfig) diff --git a/dlib/cmake_utils/find_libjpeg.cmake b/dlib/cmake_utils/find_libjpeg.cmake index 028217b075..1ae3b07ed7 100644 --- a/dlib/cmake_utils/find_libjpeg.cmake +++ b/dlib/cmake_utils/find_libjpeg.cmake @@ -1,7 +1,7 @@ #This script just runs CMake's built in JPEG finding tool. But it also checks that the #copy of libjpeg that cmake finds actually builds and links. -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) if (BUILDING_PYTHON_IN_MSVC) # Never use any system copy of libjpeg when building python in visual studio diff --git a/dlib/cmake_utils/find_libpng.cmake b/dlib/cmake_utils/find_libpng.cmake index 6760739226..87e593c9a6 100644 --- a/dlib/cmake_utils/find_libpng.cmake +++ b/dlib/cmake_utils/find_libpng.cmake @@ -1,7 +1,7 @@ #This script just runs CMake's built in PNG finding tool. But it also checks that the #copy of libpng that cmake finds actually builds and links. -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) if (BUILDING_PYTHON_IN_MSVC) # Never use any system copy of libpng when building python in visual studio diff --git a/dlib/cmake_utils/set_compiler_specific_options.cmake b/dlib/cmake_utils/set_compiler_specific_options.cmake index 8093ca6d30..06220effc4 100644 --- a/dlib/cmake_utils/set_compiler_specific_options.cmake +++ b/dlib/cmake_utils/set_compiler_specific_options.cmake @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) # Check if we are being built as part of a pybind11 module. diff --git a/dlib/cmake_utils/tell_visual_studio_to_use_static_runtime.cmake b/dlib/cmake_utils/tell_visual_studio_to_use_static_runtime.cmake index 80122d8018..0d83f932bf 100644 --- a/dlib/cmake_utils/tell_visual_studio_to_use_static_runtime.cmake +++ b/dlib/cmake_utils/tell_visual_studio_to_use_static_runtime.cmake @@ -2,7 +2,7 @@ # Including this cmake script into your cmake project will cause visual studio # to build your project against the static C runtime. -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) if (MSVC OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") option (DLIB_FORCE_MSVC_STATIC_RUNTIME "use static runtime" ON) diff --git a/dlib/cmake_utils/test_for_avx/CMakeLists.txt b/dlib/cmake_utils/test_for_avx/CMakeLists.txt index 10890153cf..a19ca62bf9 100644 --- a/dlib/cmake_utils/test_for_avx/CMakeLists.txt +++ b/dlib/cmake_utils/test_for_avx/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) project(avx_test) set(USE_AVX_INSTRUCTIONS ON CACHE BOOL "Use AVX instructions") diff --git a/dlib/cmake_utils/test_for_cuda/CMakeLists.txt b/dlib/cmake_utils/test_for_cuda/CMakeLists.txt index 8b6cd7e7b0..7392501f15 100644 --- a/dlib/cmake_utils/test_for_cuda/CMakeLists.txt +++ b/dlib/cmake_utils/test_for_cuda/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) project(cuda_test) include_directories(../../cuda) diff --git a/dlib/cmake_utils/test_for_cudnn/CMakeLists.txt b/dlib/cmake_utils/test_for_cudnn/CMakeLists.txt index 0ca0f6b6a2..4a0315a327 100644 --- a/dlib/cmake_utils/test_for_cudnn/CMakeLists.txt +++ b/dlib/cmake_utils/test_for_cudnn/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) project(cudnn_test) # Override the FindCUDA.cmake setting to avoid duplication of host flags if using a toolchain: diff --git a/dlib/cmake_utils/test_for_libjpeg/CMakeLists.txt b/dlib/cmake_utils/test_for_libjpeg/CMakeLists.txt index a3c6910dbe..db740bca50 100644 --- a/dlib/cmake_utils/test_for_libjpeg/CMakeLists.txt +++ b/dlib/cmake_utils/test_for_libjpeg/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) project(test_if_libjpeg_is_broken) find_package(JPEG) diff --git a/dlib/cmake_utils/test_for_libjxl/CMakeLists.txt b/dlib/cmake_utils/test_for_libjxl/CMakeLists.txt index c91b5d8ff6..b89e46fcbb 100644 --- a/dlib/cmake_utils/test_for_libjxl/CMakeLists.txt +++ b/dlib/cmake_utils/test_for_libjxl/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) project(test_if_libjxl_is_broken) include_directories(${JXL_INCLUDE_DIR}) diff --git a/dlib/cmake_utils/test_for_libpng/CMakeLists.txt b/dlib/cmake_utils/test_for_libpng/CMakeLists.txt index 0be9206132..94ce87826d 100644 --- a/dlib/cmake_utils/test_for_libpng/CMakeLists.txt +++ b/dlib/cmake_utils/test_for_libpng/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) project(test_if_libpng_is_broken) find_package(PNG) diff --git a/dlib/cmake_utils/test_for_libwebp/CMakeLists.txt b/dlib/cmake_utils/test_for_libwebp/CMakeLists.txt index 9ea3ed10ea..83c4502638 100644 --- a/dlib/cmake_utils/test_for_libwebp/CMakeLists.txt +++ b/dlib/cmake_utils/test_for_libwebp/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) project(test_if_libwebp_is_broken) include_directories(${WEBP_INCLUDE_DIR}) diff --git a/dlib/cmake_utils/test_for_neon/CMakeLists.txt b/dlib/cmake_utils/test_for_neon/CMakeLists.txt index b9c6e51e8a..c27d339723 100644 --- a/dlib/cmake_utils/test_for_neon/CMakeLists.txt +++ b/dlib/cmake_utils/test_for_neon/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) project(neon_test) add_library(neon_test STATIC neon_test.cpp ) diff --git a/dlib/cmake_utils/test_for_sse4/CMakeLists.txt b/dlib/cmake_utils/test_for_sse4/CMakeLists.txt index 4cd4e95cef..e01aeafd4d 100644 --- a/dlib/cmake_utils/test_for_sse4/CMakeLists.txt +++ b/dlib/cmake_utils/test_for_sse4/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) project(sse4_test) set(USE_SSE4_INSTRUCTIONS ON CACHE BOOL "Use SSE4 instructions") diff --git a/dlib/external/cblas/CMakeLists.txt b/dlib/external/cblas/CMakeLists.txt index c5379dd9fa..78ade59f3c 100644 --- a/dlib/external/cblas/CMakeLists.txt +++ b/dlib/external/cblas/CMakeLists.txt @@ -4,7 +4,7 @@ # -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) project(cblas) diff --git a/dlib/external/pybind11/CMakeLists.txt b/dlib/external/pybind11/CMakeLists.txt index 7db1bf668f..9156a623df 100644 --- a/dlib/external/pybind11/CMakeLists.txt +++ b/dlib/external/pybind11/CMakeLists.txt @@ -77,9 +77,9 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) if(OSX AND CMAKE_VERSION VERSION_LESS 3.7) # Bug in macOS CMake < 3.7 is unable to download catch message(WARNING "CMAKE 3.7+ needed on macOS to download catch, and newer HIGHLY recommended") - elseif(WINDOWS AND CMAKE_VERSION VERSION_LESS 3.8) - # Only tested with 3.8+ in CI. - message(WARNING "CMAKE 3.8+ tested on Windows, previous versions untested") + elseif(WINDOWS AND CMAKE_VERSION VERSION_LESS 3.17) + # Only tested with 3.8ยด17+ in CI. + message(WARNING "CMAKE 3.17+ tested on Windows, previous versions untested") endif() message(STATUS "CMake ${CMAKE_VERSION}") diff --git a/dlib/java/CMakeLists.txt b/dlib/java/CMakeLists.txt index b31bfad0d4..c87258ccc8 100644 --- a/dlib/java/CMakeLists.txt +++ b/dlib/java/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) project (myproject) set(java_package_name net.dlib) set(source_files diff --git a/dlib/java/cmake_swig_jni b/dlib/java/cmake_swig_jni index 48f960f41f..d4127aa918 100644 --- a/dlib/java/cmake_swig_jni +++ b/dlib/java/cmake_swig_jni @@ -2,7 +2,7 @@ # it by defining some CMake variables and then include(cmake_swig_jni). You # would make a CMakeLists.txt file that looks like the following: # -# cmake_minimum_required (VERSION 3.8.0) +# cmake_minimum_required (VERSION 3.17.0) # project (example) # set(java_package_name "org.mycompany") # set(source_files @@ -40,7 +40,7 @@ ################################################################################ ################################################################################ -cmake_minimum_required (VERSION 3.8.0) +cmake_minimum_required (VERSION 3.17.0) # This block of code tries to figure out what the JAVA_HOME environment # variable should be by looking at the folder that contains the java diff --git a/dlib/matlab/CMakeLists.txt b/dlib/matlab/CMakeLists.txt index 2dee98c60c..b9a5a12289 100644 --- a/dlib/matlab/CMakeLists.txt +++ b/dlib/matlab/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) PROJECT(mex_functions) diff --git a/dlib/matlab/cmake_mex_wrapper b/dlib/matlab/cmake_mex_wrapper index 6cbc27afc6..0621b7dc6a 100644 --- a/dlib/matlab/cmake_mex_wrapper +++ b/dlib/matlab/cmake_mex_wrapper @@ -3,7 +3,7 @@ # that additional library dependencies can be added like this: add_mex_function(name lib1 dlib libetc). # That is, just add more libraries after the name and they will be build into the mex file. -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) set(BUILDING_MATLAB_MEX_FILE true) set(CMAKE_POSITION_INDEPENDENT_CODE True) diff --git a/dlib/test/CMakeLists.txt b/dlib/test/CMakeLists.txt index 4154fa0a3b..bb06619b3d 100644 --- a/dlib/test/CMakeLists.txt +++ b/dlib/test/CMakeLists.txt @@ -3,7 +3,7 @@ # information about it at http://www.cmake.org # -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) # create a variable called target_name and set it to the string "dtest" set (target_name dtest) @@ -213,10 +213,8 @@ else() # basically Clang # This is for the comment in face_detection_ex.cpp that says "faces/*.jpg" add_compile_options(-Wno-comment) - if(${CMAKE_VERSION} VERSION_GREATER "3.8.0") - # strip debug symbols to make the binary smaller - target_link_options(${target_name} PRIVATE $<$:-s>) - endif() + # strip debug symbols to make the binary smaller + target_link_options(${target_name} PRIVATE $<$:-s>) endif() diff --git a/dlib/test/blas_bindings/CMakeLists.txt b/dlib/test/blas_bindings/CMakeLists.txt index f9c0b05a12..883d237bd4 100644 --- a/dlib/test/blas_bindings/CMakeLists.txt +++ b/dlib/test/blas_bindings/CMakeLists.txt @@ -3,7 +3,7 @@ # information about it at http://www.cmake.org # -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) # This variable contains a list of all the tests we are building # into the regression test suite. diff --git a/dlib/test/tools/CMakeLists.txt b/dlib/test/tools/CMakeLists.txt index 80371d6d84..f3db1a0cb2 100644 --- a/dlib/test/tools/CMakeLists.txt +++ b/dlib/test/tools/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) add_subdirectory(../../../tools/imglab imglab_build) add_subdirectory(../../../tools/htmlify htmlify_build) diff --git a/docs/docs/release_notes.xml b/docs/docs/release_notes.xml index 97cb945486..38b3304e35 100644 --- a/docs/docs/release_notes.xml +++ b/docs/docs/release_notes.xml @@ -58,7 +58,7 @@ New Features and Improvements: - FFMPEG wrappers: dlib::ffmpeg::decoder and dlib::ffmpeg::demuxer (PR #2707) Non-Backwards Compatible Changes: - - Dlib now requires C++14 and at least CMake version 3.8.0. + - Dlib now requires C++14 and at least CMake version 3.17.0. Bug fixes: - Various adjustments to avoid errors on unusual platforms of system configurations. diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 61af199179..e3357ab44b 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -32,7 +32,7 @@ # -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) # Every project needs a name. We call this the "examples" project. project(examples) diff --git a/tools/archive/CMakeLists.txt b/tools/archive/CMakeLists.txt index 796e469852..1753969e5e 100644 --- a/tools/archive/CMakeLists.txt +++ b/tools/archive/CMakeLists.txt @@ -4,7 +4,7 @@ # -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) PROJECT(archive) diff --git a/tools/convert_dlib_nets_to_caffe/CMakeLists.txt b/tools/convert_dlib_nets_to_caffe/CMakeLists.txt index 2325366fe0..e043786042 100644 --- a/tools/convert_dlib_nets_to_caffe/CMakeLists.txt +++ b/tools/convert_dlib_nets_to_caffe/CMakeLists.txt @@ -3,7 +3,7 @@ # information about it at http://www.cmake.org # -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) set (target_name dtoc) diff --git a/tools/htmlify/CMakeLists.txt b/tools/htmlify/CMakeLists.txt index 35f327f275..8d8644fc18 100644 --- a/tools/htmlify/CMakeLists.txt +++ b/tools/htmlify/CMakeLists.txt @@ -3,7 +3,7 @@ # information about it at http://www.cmake.org # -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) # create a variable called target_name and set it to the string "htmlify" set (target_name htmlify) diff --git a/tools/imglab/CMakeLists.txt b/tools/imglab/CMakeLists.txt index 5fb1331d15..316a879cd1 100644 --- a/tools/imglab/CMakeLists.txt +++ b/tools/imglab/CMakeLists.txt @@ -3,7 +3,7 @@ # information about it at http://www.cmake.org # -cmake_minimum_required(VERSION 3.8.0) +cmake_minimum_required(VERSION 3.17.0) # create a variable called target_name and set it to the string "imglab" set (target_name imglab) diff --git a/tools/python/CMakeLists.txt b/tools/python/CMakeLists.txt index 8309d5fb93..97759bd26b 100644 --- a/tools/python/CMakeLists.txt +++ b/tools/python/CMakeLists.txt @@ -1,5 +1,5 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 3.8.0) +CMAKE_MINIMUM_REQUIRED(VERSION 3.17.0) if (WIN32 AND NOT "${CMAKE_GENERATOR}" MATCHES "Visual Studio") message(FATAL_ERROR "\n" From 9da5c52a0ec5c366143eaf701c35a5ccd89d7048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20M=C3=B6ller?= Date: Sat, 7 Sep 2024 22:23:14 +0200 Subject: [PATCH 3/5] re-introduce cuda test compile --- dlib/CMakeLists.txt | 157 ++++++++++++++++-- dlib/cmake_utils/findCUDNN.cmake | 76 --------- dlib/cmake_utils/test_for_cuda/CMakeLists.txt | 5 +- .../cmake_utils/test_for_cudnn/CMakeLists.txt | 5 +- 4 files changed, 153 insertions(+), 90 deletions(-) delete mode 100644 dlib/cmake_utils/findCUDNN.cmake diff --git a/dlib/CMakeLists.txt b/dlib/CMakeLists.txt index e7bfd29c74..491b78f57e 100644 --- a/dlib/CMakeLists.txt +++ b/dlib/CMakeLists.txt @@ -649,13 +649,144 @@ if (NOT TARGET dlib) if (DLIB_USE_CUDA) - find_package(CUDAToolkit 9.1) - set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake_utils) - find_package(CUDNN) - find_package(OpenMP) - set(openmp_libraries ${OpenMP_CXX_FLAGS}) + find_package(CUDAToolkit QUIET) - if (CUDAToolkit_FOUND AND CUDNN_FOUND AND OPENMP_FOUND) + if (CUDA_VERSION VERSION_GREATER 9.1 AND CMAKE_VERSION VERSION_LESS 3.12.2) + # This bit of weirdness is to work around a bug in cmake + list(REMOVE_ITEM CUDA_CUBLAS_LIBRARIES "CUDA_cublas_device_LIBRARY-NOTFOUND") + endif() + + + if (CUDAToolkit_FOUND AND MSVC AND NOT TARGET CUDA::cublas AND "${CMAKE_SIZEOF_VOID_P}" EQUAL "4") + message(WARNING "You have CUDA installed, but we can't use it unless you put visual studio in 64bit mode.") + set(CUDA_FOUND 0) + endif() + + if (CUDAToolkit_FOUND) + + # There is some bug in cmake that causes it to mess up the + # -std=c++11 option if you let it propagate it to nvcc in some + # cases. So instead we disable this and manually include + # things from CMAKE_CXX_FLAGS in the CUDA_NVCC_FLAGS list below. + if (APPLE) + set(CUDA_PROPAGATE_HOST_FLAGS OFF) + # Grab all the -D flags from CMAKE_CXX_FLAGS so we can pass them + # to nvcc. + string(REGEX MATCHALL "-D[^ ]*" FLAGS_FOR_NVCC "${CMAKE_CXX_FLAGS}") + + # Check if we are being built as part of a pybind11 module. + if (COMMAND pybind11_add_module) + # Don't export unnecessary symbols. + list(APPEND FLAGS_FOR_NVCC "-Xcompiler=-fvisibility=hidden") + endif() + endif() + + set(CUDA_HOST_COMPILATION_CPP ON) + string(REPLACE "," ";" DLIB_CUDA_COMPUTE_CAPABILITIES ${DLIB_USE_CUDA_COMPUTE_CAPABILITIES}) + foreach(CAP ${DLIB_CUDA_COMPUTE_CAPABILITIES}) + list(APPEND CUDA_NVCC_FLAGS "-gencode arch=compute_${CAP},code=[sm_${CAP},compute_${CAP}]") + endforeach() + # Note that we add __STRICT_ANSI__ to avoid freaking out nvcc with gcc specific + # magic in the standard C++ header files (since nvcc uses gcc headers on linux). + list(APPEND CUDA_NVCC_FLAGS "-D__STRICT_ANSI__;-D_MWAITXINTRIN_H_INCLUDED;-D_FORCE_INLINES;${FLAGS_FOR_NVCC}") + list(APPEND CUDA_NVCC_FLAGS ${active_preprocessor_switches}) + if (NOT DLIB_IN_PROJECT_BUILD) + LIST(APPEND CUDA_NVCC_FLAGS -DDLIB__CMAKE_GENERATED_A_CONFIG_H_FILE) + endif() + if (NOT MSVC) + list(APPEND CUDA_NVCC_FLAGS "-std=c++14") + endif() + if (CMAKE_POSITION_INDEPENDENT_CODE) + # sometimes this setting isn't propagated to NVCC, which then causes the + # compile to fail. So make sure it's propagated. + if (NOT MSVC) # Visual studio doesn't have -fPIC so don't do it in that case. + list(APPEND CUDA_NVCC_FLAGS "-Xcompiler -fPIC") + endif() + endif() + + include(cmake_utils/test_for_cudnn/find_cudnn.txt) + + include(CheckLanguage) + check_language(CUDA) + set(cuda_compiler_found OFF) + if (CMAKE_CUDA_COMPILER) + set(cuda_compiler_found ON) + else() + message(STATUS " *** Cannot find CUDA compiler. If you are on windows using Visual Studio, make sure to install 'Visual Studio Integration' in the cuda installer.") + endif() + + if (cudnn AND cudnn_include AND cuda_compiler_found AND NOT DEFINED cuda_test_compile_worked AND NOT DEFINED cudnn_test_compile_worked) + # make sure cuda is really working by doing a test compile + enable_language(CUDA) + message(STATUS "Building a CUDA test project to see if your compiler is compatible with CUDA...") + + set(CUDA_TEST_CMAKE_FLAGS + "-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" + "-DCMAKE_INCLUDE_PATH=${CMAKE_INCLUDE_PATH}" + "-DCMAKE_LIBRARY_PATH=${CMAKE_LIBRARY_PATH}") + + if (NOT MSVC) # see https://github.com/davisking/dlib/issues/363 + list(APPEND CUDA_TEST_CMAKE_FLAGS "-DCUDA_HOST_COMPILER=${CUDA_HOST_COMPILER}") + endif() + + try_compile(cuda_test_compile_worked + ${PROJECT_BINARY_DIR}/cuda_test_build + ${PROJECT_SOURCE_DIR}/cmake_utils/test_for_cuda cuda_test + CMAKE_FLAGS ${CUDA_TEST_CMAKE_FLAGS} + OUTPUT_VARIABLE try_compile_output_message + ) + if (NOT cuda_test_compile_worked) + string(REPLACE "\n" "\n *** " try_compile_output_message "${try_compile_output_message}") + message(STATUS "*****************************************************************************************************************") + message(STATUS "*** CUDA was found but your compiler failed to compile a simple CUDA program so dlib isn't going to use CUDA. ") + message(STATUS "*** The output of the failed CUDA test compile is shown below: ") + message(STATUS "*** ") + message(STATUS "*** ${try_compile_output_message}") + message(STATUS "*****************************************************************************************************************") + else() + message(STATUS "Building a cuDNN test project to check if you have the right version of cuDNN installed...") + try_compile(cudnn_test_compile_worked + ${PROJECT_BINARY_DIR}/cudnn_test_build + ${PROJECT_SOURCE_DIR}/cmake_utils/test_for_cudnn cudnn_test + CMAKE_FLAGS ${CUDA_TEST_CMAKE_FLAGS} + OUTPUT_VARIABLE try_compile_output_message + ) + if (NOT cudnn_test_compile_worked) + string(REPLACE "\n" "\n *** " try_compile_output_message "${try_compile_output_message}") + message(STATUS "*****************************************************************************************************") + message(STATUS "*** Found cuDNN, but we failed to compile the dlib/cmake_utils/test_for_cudnn project. ") + message(STATUS "*** You either have an unsupported version of cuDNN or something is wrong with your cudDNN install.") + message(STATUS "*** Since a functional cuDNN is not found DLIB WILL NOT USE CUDA. ") + message(STATUS "*** The output of the failed test_for_cudnn build is: ") + message(STATUS "*** ") + message(STATUS "*** ${try_compile_output_message}") + message(STATUS "*****************************************************************************************************") + endif() + endif() + endif() + # Also find OpenMP since cuSOLVER needs it. Importantly, we only + # look for one to link to if our use of BLAS, specifically the + # Intel MKL, hasn't already decided what to use. This is because + # it makes the MKL bug out if you link to another openmp lib other + # than Intel's when you use the MKL. I'm also not really sure when + # explicit linking to openmp became unnecessary, but for + # sufficiently older versions of cuda it was needed. Then in + # versions of cmake newer than 3.11 linking to openmp started to + # mess up the switches passed to nvcc, so you can't just leave + # these "try to link to openmp" statements here going forward. Fun + # times. + if (CUDA_VERSION VERSION_LESS "9.1" AND NOT openmp_libraries AND NOT MSVC AND NOT XCODE AND NOT APPLE) + find_package(OpenMP) + if (OPENMP_FOUND) + set(openmp_libraries ${OpenMP_CXX_FLAGS}) + else() + message(STATUS "*** Didn't find OpenMP, which is required to use CUDA. ***") + set(CUDA_FOUND 0) + endif() + endif() + endif() + + if (CUDAToolkit_FOUND AND cudnn AND cuda_test_compile_worked AND cudnn_test_compile_worked AND cudnn_include) set(source_files ${source_files} cuda/cuda_dlib.cu cuda/cudnn_dlibapi.cpp @@ -665,19 +796,22 @@ if (NOT TARGET dlib) cuda/cuda_data_ptr.cpp cuda/gpu_data.cpp ) + list (APPEND dlib_needed_private_libraries CUDA::toolkit) list (APPEND dlib_needed_private_libraries CUDA::cublas) - list (APPEND dlib_needed_private_libraries ${CUDNN_LIBRARY_PATH}) + list (APPEND dlib_needed_private_libraries ${cudnn}) list (APPEND dlib_needed_private_libraries CUDA::curand) list (APPEND dlib_needed_private_libraries CUDA::cusolver) list (APPEND dlib_needed_private_libraries CUDA::cudart) - list (APPEND dlib_needed_private_libraries ${openmp_libraries}) + if(openmp_libraries) + list (APPEND dlib_needed_private_libraries ${openmp_libraries}) + endif() - include_directories(${CUDAToolkit_INCLUDE_DIRS} ${CUDNN_INCLUDE_PATH}) + include_directories(${cudnn_include}) message(STATUS "Enabling CUDA support for dlib. DLIB WILL USE CUDA, compute capabilities: ${DLIB_CUDA_COMPUTE_CAPABILITIES}") else() set(DLIB_USE_CUDA OFF CACHE BOOL ${DLIB_USE_BLAS_STR} FORCE ) toggle_preprocessor_switch(DLIB_USE_CUDA) - if (NOT CUDA_FOUND) + if (NOT CUDAToolkit_FOUND) message(STATUS "DID NOT FIND CUDA") endif() message(STATUS "Disabling CUDA support for dlib. DLIB WILL NOT USE CUDA") @@ -762,6 +896,9 @@ if (NOT TARGET dlib) target_compile_options(dlib PRIVATE "-DDLIB_CHECK_FOR_VERSION_MISMATCH=${DLIB_CHECK_FOR_VERSION_MISMATCH}") endif() + if (DLIB_USE_CUDA) + set_target_properties(dlib PROPERTIES CUDA_ARCHITECTURES ${DLIB_CUDA_COMPUTE_CAPABILITIES}) + endif() # Allow the unit tests to ask us to compile the all/source.cpp file just to make sure it compiles. if (DLIB_TEST_COMPILE_ALL_SOURCE_CPP) diff --git a/dlib/cmake_utils/findCUDNN.cmake b/dlib/cmake_utils/findCUDNN.cmake deleted file mode 100644 index 0c900fcd4e..0000000000 --- a/dlib/cmake_utils/findCUDNN.cmake +++ /dev/null @@ -1,76 +0,0 @@ -# Find the CUDNN libraries -# -# The following variables are optionally searched for defaults -# CUDNN_ROOT: Base directory where CUDNN is found -# CUDNN_INCLUDE_DIR: Directory where CUDNN header is searched for -# CUDNN_LIBRARY: Directory where CUDNN library is searched for -# CUDNN_STATIC: Are we looking for a static library? (default: no) -# -# The following are set after configuration is done: -# CUDNN_FOUND -# CUDNN_INCLUDE_PATH -# CUDNN_LIBRARY_PATH -# - -include(FindPackageHandleStandardArgs) - -set(CUDNN_ROOT $ENV{CUDNN_ROOT_DIR} CACHE PATH "Folder containing NVIDIA cuDNN") -if (DEFINED $ENV{CUDNN_ROOT_DIR}) - message(WARNING "CUDNN_ROOT_DIR is deprecated. Please set CUDNN_ROOT instead.") -endif() -list(APPEND CUDNN_ROOT $ENV{CUDNN_ROOT_DIR} ${CUDA_TOOLKIT_ROOT_DIR}) - -# Compatible layer for CMake <3.12. CUDNN_ROOT will be accounted in for searching paths and libraries for CMake >=3.12. -list(APPEND CMAKE_PREFIX_PATH ${CUDNN_ROOT}) - -set(CUDNN_INCLUDE_DIR $ENV{CUDNN_INCLUDE_DIR} CACHE PATH "Folder containing NVIDIA cuDNN header files") - -find_path(CUDNN_INCLUDE_PATH cudnn.h - HINTS ${CUDNN_INCLUDE_DIR} - PATH_SUFFIXES cuda/include cuda include) - -option(CUDNN_STATIC "Look for static CUDNN" OFF) -if (CUDNN_STATIC) - set(CUDNN_LIBNAME "libcudnn_static.a") -else() - set(CUDNN_LIBNAME "cudnn") -endif() - -set(CUDNN_LIBRARY $ENV{CUDNN_LIBRARY} CACHE PATH "Path to the cudnn library file (e.g., libcudnn.so)") -if (CUDNN_LIBRARY MATCHES ".*cudnn_static.a" AND NOT CUDNN_STATIC) - message(WARNING "CUDNN_LIBRARY points to a static library (${CUDNN_LIBRARY}) but CUDNN_STATIC is OFF.") -endif() - -find_library(CUDNN_LIBRARY_PATH ${CUDNN_LIBNAME} - PATHS ${CUDNN_LIBRARY} - PATH_SUFFIXES lib lib64 cuda/lib cuda/lib64 lib/x64) - -find_package_handle_standard_args(CUDNN DEFAULT_MSG CUDNN_LIBRARY_PATH CUDNN_INCLUDE_PATH) - -if(CUDNN_FOUND) - # Get cuDNN version - if(EXISTS ${CUDNN_INCLUDE_PATH}/cudnn_version.h) - file(READ ${CUDNN_INCLUDE_PATH}/cudnn_version.h CUDNN_HEADER_CONTENTS) - else() - file(READ ${CUDNN_INCLUDE_PATH}/cudnn.h CUDNN_HEADER_CONTENTS) - endif() - string(REGEX MATCH "define CUDNN_MAJOR * +([0-9]+)" - CUDNN_VERSION_MAJOR "${CUDNN_HEADER_CONTENTS}") - string(REGEX REPLACE "define CUDNN_MAJOR * +([0-9]+)" "\\1" - CUDNN_VERSION_MAJOR "${CUDNN_VERSION_MAJOR}") - string(REGEX MATCH "define CUDNN_MINOR * +([0-9]+)" - CUDNN_VERSION_MINOR "${CUDNN_HEADER_CONTENTS}") - string(REGEX REPLACE "define CUDNN_MINOR * +([0-9]+)" "\\1" - CUDNN_VERSION_MINOR "${CUDNN_VERSION_MINOR}") - string(REGEX MATCH "define CUDNN_PATCHLEVEL * +([0-9]+)" - CUDNN_VERSION_PATCH "${CUDNN_HEADER_CONTENTS}") - string(REGEX REPLACE "define CUDNN_PATCHLEVEL * +([0-9]+)" "\\1" - CUDNN_VERSION_PATCH "${CUDNN_VERSION_PATCH}") - # Assemble cuDNN version - if(NOT CUDNN_VERSION_MAJOR) - set(CUDNN_VERSION "?") - else() - set(CUDNN_VERSION - "${CUDNN_VERSION_MAJOR}.${CUDNN_VERSION_MINOR}.${CUDNN_VERSION_PATCH}") - endif() -endif() \ No newline at end of file diff --git a/dlib/cmake_utils/test_for_cuda/CMakeLists.txt b/dlib/cmake_utils/test_for_cuda/CMakeLists.txt index 7392501f15..f0a0fd0b00 100644 --- a/dlib/cmake_utils/test_for_cuda/CMakeLists.txt +++ b/dlib/cmake_utils/test_for_cuda/CMakeLists.txt @@ -7,8 +7,9 @@ add_definitions(-DDLIB_USE_CUDA) # Override the FindCUDA.cmake setting to avoid duplication of host flags if using a toolchain: option(CUDA_PROPAGATE_HOST_FLAGS "Propage C/CXX_FLAGS and friends to the host compiler via -Xcompile" OFF) -find_package(CUDA 7.5 REQUIRED) +find_package(CUDAToolkit 7.5 REQUIRED) set(CUDA_HOST_COMPILATION_CPP ON) list(APPEND CUDA_NVCC_FLAGS "-arch=sm_50;-std=c++14;-D__STRICT_ANSI__;-D_MWAITXINTRIN_H_INCLUDED;-D_FORCE_INLINES") -cuda_add_library(cuda_test STATIC cuda_test.cu ) +enable_language(CUDA) +add_library(cuda_test STATIC cuda_test.cu ) diff --git a/dlib/cmake_utils/test_for_cudnn/CMakeLists.txt b/dlib/cmake_utils/test_for_cudnn/CMakeLists.txt index 4a0315a327..9bfed91430 100644 --- a/dlib/cmake_utils/test_for_cudnn/CMakeLists.txt +++ b/dlib/cmake_utils/test_for_cudnn/CMakeLists.txt @@ -4,7 +4,7 @@ project(cudnn_test) # Override the FindCUDA.cmake setting to avoid duplication of host flags if using a toolchain: option(CUDA_PROPAGATE_HOST_FLAGS "Propage C/CXX_FLAGS and friends to the host compiler via -Xcompile" OFF) -find_package(CUDA 7.5 REQUIRED) +find_package(CUDAToolkit 7.5 REQUIRED) set(CUDA_HOST_COMPILATION_CPP ON) list(APPEND CUDA_NVCC_FLAGS "-arch=sm_50;-std=c++14;-D__STRICT_ANSI__") add_definitions(-DDLIB_USE_CUDA) @@ -13,6 +13,7 @@ include(find_cudnn.txt) if (cudnn_include AND cudnn) include_directories(${cudnn_include}) - cuda_add_library(cudnn_test STATIC ../../cuda/cudnn_dlibapi.cpp ${cudnn} ) + enable_language(CUDA) + add_library(cudnn_test STATIC ../../cuda/cudnn_dlibapi.cpp ${cudnn} ) target_compile_features(cudnn_test PUBLIC cxx_std_14) endif() From d6780734f2260d320a0fe01b97e7f08f02e7df74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20M=C3=B6ller?= Date: Sun, 8 Sep 2024 20:13:09 +0200 Subject: [PATCH 4/5] links cudnn tests against cuda runtime --- dlib/cmake_utils/test_for_cudnn/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dlib/cmake_utils/test_for_cudnn/CMakeLists.txt b/dlib/cmake_utils/test_for_cudnn/CMakeLists.txt index 9bfed91430..c1a12abafe 100644 --- a/dlib/cmake_utils/test_for_cudnn/CMakeLists.txt +++ b/dlib/cmake_utils/test_for_cudnn/CMakeLists.txt @@ -16,4 +16,6 @@ if (cudnn_include AND cudnn) enable_language(CUDA) add_library(cudnn_test STATIC ../../cuda/cudnn_dlibapi.cpp ${cudnn} ) target_compile_features(cudnn_test PUBLIC cxx_std_14) + target_link_libraries(cudnn_test PRIVATE CUDA::toolkit) + target_link_libraries(cudnn_test PRIVATE CUDA::cudart) endif() From 426c93d36ae112d5c2d27573256d3633c0b21315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20M=C3=B6ller?= Date: Sat, 14 Sep 2024 22:46:06 +0200 Subject: [PATCH 5/5] add cudatoolkit root for test compilation --- dlib/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dlib/CMakeLists.txt b/dlib/CMakeLists.txt index 491b78f57e..e4da4a8bf5 100644 --- a/dlib/CMakeLists.txt +++ b/dlib/CMakeLists.txt @@ -723,7 +723,8 @@ if (NOT TARGET dlib) set(CUDA_TEST_CMAKE_FLAGS "-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" "-DCMAKE_INCLUDE_PATH=${CMAKE_INCLUDE_PATH}" - "-DCMAKE_LIBRARY_PATH=${CMAKE_LIBRARY_PATH}") + "-DCMAKE_LIBRARY_PATH=${CMAKE_LIBRARY_PATH}" + "-DCUDAToolkit_ROOT=${CUDAToolkit_ROOT}") if (NOT MSVC) # see https://github.com/davisking/dlib/issues/363 list(APPEND CUDA_TEST_CMAKE_FLAGS "-DCUDA_HOST_COMPILER=${CUDA_HOST_COMPILER}")