@@ -13,7 +13,7 @@ if(POLICY CMP0077)
1313 cmake_policy (SET CMP0077 NEW)
1414endif ()
1515
16- project (dlib)
16+ project (dlib LANGUAGES C CXX )
1717
1818set (CPACK_PACKAGE_NAME "dlib" )
1919set (CPACK_PACKAGE_VERSION_MAJOR "20" )
@@ -26,6 +26,7 @@ if (NOT TARGET dlib)
2626 message (STATUS "Compiling dlib version: ${VERSION} " )
2727endif ()
2828
29+ set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR} /cmake_utils)
2930
3031include (cmake_utils/set_compiler_specific_options.cmake)
3132
@@ -104,17 +105,6 @@ elseif(BUILD_SHARED_LIBS)
104105 endif ()
105106endif ()
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-
118108macro (enable_preprocessor_switch option_name)
119109 list (APPEND active_preprocessor_switches "-D${option_name} " )
120110endmacro ()
@@ -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 ##########################################################
0 commit comments