2020-07-04 05:11:11 +08:00
if ( ( WIN32 AND NOT MSVC ) OR OPENCV_CMAKE_FORCE_CUDA )
2022-11-29 14:25:26 +08:00
message ( STATUS "CUDA: Compilation is disabled (due to only Visual Studio compiler supported on your platform)." )
2012-11-24 03:11:44 +08:00
return ( )
endif ( )
2020-07-04 05:11:11 +08:00
if ( ( NOT UNIX AND CV_CLANG ) OR OPENCV_CMAKE_FORCE_CUDA )
2022-11-29 14:25:26 +08:00
message ( STATUS "CUDA: Compilation is disabled (due to Clang unsupported on your platform)." )
2012-09-21 21:42:37 +08:00
return ( )
endif ( )
2020-07-04 05:11:11 +08:00
#set(OPENCV_CMAKE_CUDA_DEBUG 1)
2022-11-29 14:25:26 +08:00
if ( CUDA_TOOLKIT_ROOT_DIR )
set ( CUDA_TOOLKIT_TARGET_DIR ${ CUDA_TOOLKIT_ROOT_DIR } )
endif ( )
2018-07-11 18:47:56 +08:00
if ( ( ( NOT CMAKE_VERSION VERSION_LESS "3.9.0" ) # requires https://gitlab.kitware.com/cmake/cmake/merge_requests/663
O R O P E N C V _ C U D A _ F O R C E _ E X T E R N A L _ C M A K E _ M O D U L E )
A N D N O T O P E N C V _ C U D A _ F O R C E _ B U I L T I N _ C M A K E _ M O D U L E )
2019-12-13 22:52:40 +08:00
ocv_update ( CUDA_LINK_LIBRARIES_KEYWORD "PRIVATE" )
2018-07-11 18:47:56 +08:00
find_host_package ( CUDA "${MIN_VER_CUDA}" QUIET )
else ( )
# Use OpenCV's patched "FindCUDA" module
set ( CMAKE_MODULE_PATH "${OpenCV_SOURCE_DIR}/cmake" ${ CMAKE_MODULE_PATH } )
if ( ANDROID )
set ( CUDA_TARGET_OS_VARIANT "Android" )
endif ( )
find_host_package ( CUDA "${MIN_VER_CUDA}" QUIET )
2013-10-30 20:10:02 +08:00
2018-07-11 18:47:56 +08:00
list ( REMOVE_AT CMAKE_MODULE_PATH 0 )
endif ( )
2013-10-30 20:10:02 +08:00
2022-11-29 14:25:26 +08:00
if ( NOT CUDA_FOUND )
unset ( CUDA_ARCH_BIN CACHE )
unset ( CUDA_ARCH_PTX CACHE )
return ( )
endif ( )
2019-06-02 19:47:15 +08:00
2022-11-29 14:25:26 +08:00
unset ( CUDA_nvcuvenc_LIBRARY CACHE )
set ( HAVE_CUDA 1 )
if ( NOT CUDA_VERSION VERSION_LESS 11.0 )
# CUDA 11.0 removes nppicom
ocv_list_filterout ( CUDA_nppi_LIBRARY "nppicom" )
ocv_list_filterout ( CUDA_npp_LIBRARY "nppicom" )
endif ( )
2013-01-23 19:32:49 +08:00
2022-11-29 14:25:26 +08:00
if ( WITH_CUFFT )
set ( HAVE_CUFFT 1 )
endif ( )
2012-02-03 19:26:49 +08:00
2022-11-29 14:25:26 +08:00
if ( WITH_CUBLAS )
set ( HAVE_CUBLAS 1 )
endif ( )
2013-02-26 17:02:54 +08:00
2022-11-29 14:25:26 +08:00
if ( WITH_CUDNN )
set ( CMAKE_MODULE_PATH "${OpenCV_SOURCE_DIR}/cmake" ${ CMAKE_MODULE_PATH } )
find_host_package ( CUDNN "${MIN_VER_CUDNN}" )
list ( REMOVE_AT CMAKE_MODULE_PATH 0 )
2013-02-26 17:02:54 +08:00
2022-11-29 14:25:26 +08:00
if ( CUDNN_FOUND )
set ( HAVE_CUDNN 1 )
2020-06-11 15:34:31 +08:00
endif ( )
2022-11-29 14:25:26 +08:00
endif ( )
2018-08-16 14:43:52 +08:00
2022-11-29 14:25:26 +08:00
include ( cmake/OpenCVDetectCUDAUtils.cmake )
2020-07-04 05:11:11 +08:00
2022-11-29 14:25:26 +08:00
if ( WITH_NVCUVID OR WITH_NVCUVENC )
set ( cuda_toolkit_dirs "${CUDA_TOOLKIT_TARGET_DIR}" "${CUDA_TOOLKIT_ROOT_DIR}" )
ocv_check_for_nvidia_video_codec_sdk ( "${cuda_toolkit_dirs}" )
endif ( )
2020-07-04 05:11:11 +08:00
2022-11-29 14:25:26 +08:00
message ( STATUS "CUDA detected: " ${ CUDA_VERSION } )
2013-02-26 17:02:54 +08:00
2022-11-29 14:25:26 +08:00
ocv_set_cuda_detection_nvcc_flags ( CUDA_HOST_COMPILER )
ocv_set_cuda_arch_bin_and_ptx ( ${ CUDA_NVCC_EXECUTABLE } )
2013-02-26 17:02:54 +08:00
2022-11-29 14:25:26 +08:00
# NVCC flags to be set
set ( NVCC_FLAGS_EXTRA "" )
2012-06-20 13:41:16 +08:00
2022-11-29 14:25:26 +08:00
# These vars will be passed into the templates
set ( OPENCV_CUDA_ARCH_BIN "" )
set ( OPENCV_CUDA_ARCH_PTX "" )
set ( OPENCV_CUDA_ARCH_FEATURES "" )
2012-02-03 19:26:49 +08:00
2022-11-29 14:25:26 +08:00
# Tell NVCC to add binaries for the specified GPUs
string ( REGEX MATCHALL "[0-9()]+" ARCH_LIST "${ARCH_BIN_NO_POINTS}" )
foreach ( ARCH IN LISTS ARCH_LIST )
if ( ARCH MATCHES "([0-9]+)\\(([0-9]+)\\)" )
# User explicitly specified PTX for the concrete BIN
set ( NVCC_FLAGS_EXTRA ${ NVCC_FLAGS_EXTRA } -gencode arch=compute_ ${ CMAKE_MATCH_2 } ,code=sm_ ${ CMAKE_MATCH_1 } )
set ( OPENCV_CUDA_ARCH_BIN "${OPENCV_CUDA_ARCH_BIN} ${CMAKE_MATCH_1}" )
set ( OPENCV_CUDA_ARCH_FEATURES "${OPENCV_CUDA_ARCH_FEATURES} ${CMAKE_MATCH_2}" )
else ( )
# User didn't explicitly specify PTX for the concrete BIN, we assume PTX=BIN
set ( NVCC_FLAGS_EXTRA ${ NVCC_FLAGS_EXTRA } -gencode arch=compute_ ${ ARCH } ,code=sm_ ${ ARCH } )
set ( OPENCV_CUDA_ARCH_BIN "${OPENCV_CUDA_ARCH_BIN} ${ARCH}" )
2013-01-23 19:32:49 +08:00
set ( OPENCV_CUDA_ARCH_FEATURES "${OPENCV_CUDA_ARCH_FEATURES} ${ARCH}" )
2012-10-11 02:16:42 +08:00
endif ( )
2022-11-29 14:25:26 +08:00
endforeach ( )
set ( NVCC_FLAGS_EXTRA ${ NVCC_FLAGS_EXTRA } -D_FORCE_INLINES )
2012-10-17 07:18:30 +08:00
2022-11-29 14:25:26 +08:00
# Tell NVCC to add PTX intermediate code for the specified architectures
string ( REGEX MATCHALL "[0-9]+" ARCH_LIST "${ARCH_PTX_NO_POINTS}" )
foreach ( ARCH IN LISTS ARCH_LIST )
set ( NVCC_FLAGS_EXTRA ${ NVCC_FLAGS_EXTRA } -gencode arch=compute_ ${ ARCH } ,code=compute_ ${ ARCH } )
set ( OPENCV_CUDA_ARCH_PTX "${OPENCV_CUDA_ARCH_PTX} ${ARCH}" )
set ( OPENCV_CUDA_ARCH_FEATURES "${OPENCV_CUDA_ARCH_FEATURES} ${ARCH}" )
endforeach ( )
2012-10-17 07:18:30 +08:00
2022-11-29 14:25:26 +08:00
# These vars will be processed in other scripts
set ( CUDA_NVCC_FLAGS ${ CUDA_NVCC_FLAGS } ${ NVCC_FLAGS_EXTRA } )
set ( OpenCV_CUDA_CC "${NVCC_FLAGS_EXTRA}" )
2013-04-03 17:29:04 +08:00
2022-11-29 14:25:26 +08:00
if ( ANDROID )
set ( CUDA_NVCC_FLAGS ${ CUDA_NVCC_FLAGS } "-Xptxas;-dlcm=ca" )
endif ( )
2013-06-07 17:34:33 +08:00
2022-11-29 14:25:26 +08:00
ocv_set_nvcc_threads_for_vs ( )
2013-12-03 19:50:44 +08:00
2022-11-29 14:25:26 +08:00
message ( STATUS "CUDA: NVCC target flags ${CUDA_NVCC_FLAGS}" )
2013-10-30 14:21:30 +08:00
2022-11-29 14:25:26 +08:00
OCV_OPTION ( CUDA_FAST_MATH "Enable --use_fast_math for CUDA compiler " OFF )
2014-08-13 22:08:48 +08:00
2022-11-29 14:25:26 +08:00
if ( CUDA_FAST_MATH )
set ( CUDA_NVCC_FLAGS ${ CUDA_NVCC_FLAGS } --use_fast_math )
endif ( )
2018-04-26 20:06:59 +08:00
2022-11-29 14:25:26 +08:00
OCV_OPTION ( CUDA_ENABLE_DELAYLOAD "Enable delayed loading of CUDA DLLs" OFF VISIBLE_IF MSVC )
2018-05-21 22:54:11 +08:00
2022-11-29 14:25:26 +08:00
mark_as_advanced ( CUDA_BUILD_CUBIN CUDA_BUILD_EMULATION CUDA_VERBOSE_BUILD CUDA_SDK_ROOT_DIR )
2018-05-21 22:54:11 +08:00
2022-11-29 14:25:26 +08:00
macro ( ocv_check_windows_crt_linkage )
# The new MSVC runtime abstraction is only useable if CUDA is a first class language
if ( WIN32 AND POLICY CMP0091 )
cmake_policy ( GET CMP0091 MSVC_RUNTIME_SET_BY_ABSTRACTION )
if ( MSVC_RUNTIME_SET_BY_ABSTRACTION STREQUAL "NEW" )
if ( NOT BUILD_SHARED_LIBS AND BUILD_WITH_STATIC_CRT )
2023-12-29 22:57:17 +08:00
set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT" )
set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd" )
2022-11-29 14:25:26 +08:00
else ( )
2023-12-29 22:57:17 +08:00
set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD" )
set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd" )
2020-06-29 06:49:00 +08:00
endif ( )
2012-02-03 19:26:49 +08:00
endif ( )
2022-11-29 14:25:26 +08:00
endif ( )
endmacro ( )
2022-11-16 17:14:17 +08:00
2022-11-29 14:25:26 +08:00
macro ( ocv_cuda_compile VAR )
ocv_cuda_filter_options ( )
ocv_check_windows_crt_linkage ( )
ocv_nvcc_flags ( )
2015-10-31 01:28:19 +08:00
2022-11-29 14:25:26 +08:00
if ( UNIX OR APPLE )
if ( NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS_DEBUG} ${CUDA_NVCC_FLAGS}" MATCHES "-std=" )
if ( CUDA_VERSION VERSION_LESS "11.0" )
list ( APPEND CUDA_NVCC_FLAGS "--std=c++11" )
else ( )
list ( APPEND CUDA_NVCC_FLAGS "--std=c++14" )
endif ( )
2012-08-21 03:35:29 +08:00
endif ( )
2022-11-29 14:25:26 +08:00
endif ( )
2012-02-03 19:26:49 +08:00
2022-11-29 14:25:26 +08:00
CUDA_COMPILE ( ${ VAR } ${ ARGN } )
2012-10-17 07:18:30 +08:00
2022-11-29 14:25:26 +08:00
foreach ( var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG )
set ( ${ var } "${${var}_backup_in_cuda_compile_}" )
unset ( ${ var } _backup_in_cuda_compile_ )
endforeach ( )
endmacro ( )
2014-02-28 22:18:20 +08:00
if ( HAVE_CUDA )
set ( CUDA_LIBS_PATH "" )
foreach ( p ${ CUDA_LIBRARIES } ${ CUDA_npp_LIBRARY } )
get_filename_component ( _tmp ${ p } PATH )
list ( APPEND CUDA_LIBS_PATH ${ _tmp } )
endforeach ( )
if ( HAVE_CUBLAS )
foreach ( p ${ CUDA_cublas_LIBRARY } )
get_filename_component ( _tmp ${ p } PATH )
list ( APPEND CUDA_LIBS_PATH ${ _tmp } )
endforeach ( )
endif ( )
2019-06-02 19:47:15 +08:00
if ( HAVE_CUDNN )
foreach ( p ${ CUDNN_LIBRARIES } )
get_filename_component ( _tmp ${ p } PATH )
list ( APPEND CUDA_LIBS_PATH ${ _tmp } )
endforeach ( )
endif ( )
2014-02-28 22:18:20 +08:00
if ( HAVE_CUFFT )
foreach ( p ${ CUDA_cufft_LIBRARY } )
get_filename_component ( _tmp ${ p } PATH )
list ( APPEND CUDA_LIBS_PATH ${ _tmp } )
endforeach ( )
endif ( )
list ( REMOVE_DUPLICATES CUDA_LIBS_PATH )
link_directories ( ${ CUDA_LIBS_PATH } )
set ( CUDA_LIBRARIES_ABS ${ CUDA_LIBRARIES } )
ocv_convert_to_lib_name ( CUDA_LIBRARIES ${ CUDA_LIBRARIES } )
set ( CUDA_npp_LIBRARY_ABS ${ CUDA_npp_LIBRARY } )
ocv_convert_to_lib_name ( CUDA_npp_LIBRARY ${ CUDA_npp_LIBRARY } )
if ( HAVE_CUBLAS )
set ( CUDA_cublas_LIBRARY_ABS ${ CUDA_cublas_LIBRARY } )
ocv_convert_to_lib_name ( CUDA_cublas_LIBRARY ${ CUDA_cublas_LIBRARY } )
endif ( )
2019-06-02 19:47:15 +08:00
if ( HAVE_CUDNN )
set ( CUDNN_LIBRARIES_ABS ${ CUDNN_LIBRARIES } )
ocv_convert_to_lib_name ( CUDNN_LIBRARIES ${ CUDNN_LIBRARIES } )
endif ( )
2014-02-28 22:18:20 +08:00
if ( HAVE_CUFFT )
set ( CUDA_cufft_LIBRARY_ABS ${ CUDA_cufft_LIBRARY } )
ocv_convert_to_lib_name ( CUDA_cufft_LIBRARY ${ CUDA_cufft_LIBRARY } )
endif ( )
2020-07-07 06:58:17 +08:00
if ( CMAKE_GENERATOR MATCHES "Visual Studio"
A N D N O T O P E N C V _ S K I P _ C U D A _ C M A K E _ S U P P R E S S _ R E G E N E R A T I O N
)
2020-07-04 05:11:11 +08:00
message ( STATUS "CUDA: MSVS generator is detected. Disabling CMake re-run checks (CMAKE_SUPPRESS_REGENERATION=ON). You need to run CMake manually if updates are required." )
2020-07-07 06:58:17 +08:00
set ( CMAKE_SUPPRESS_REGENERATION ON )
endif ( )
2014-03-24 19:47:56 +08:00
endif ( )
2019-07-06 07:10:41 +08:00
if ( HAVE_CUDA )
2022-11-29 14:25:26 +08:00
ocv_apply_cuda_stub_workaround ( "${CUDA_CUDA_LIBRARY}" )
ocv_check_cuda_delayed_load ( "${CUDA_TOOLKIT_ROOT_DIR}" )
2019-07-06 07:10:41 +08:00
2022-11-29 14:25:26 +08:00
# ----------------------------------------------------------------------------
# Add CUDA libraries (needed for apps/tools, samples)
# ----------------------------------------------------------------------------
2019-08-06 00:07:48 +08:00
set ( OPENCV_LINKER_LIBS ${ OPENCV_LINKER_LIBS } ${ CUDA_LIBRARIES } ${ CUDA_npp_LIBRARY } )
2019-07-06 07:10:41 +08:00
if ( HAVE_CUBLAS )
set ( OPENCV_LINKER_LIBS ${ OPENCV_LINKER_LIBS } ${ CUDA_cublas_LIBRARY } )
endif ( )
if ( HAVE_CUDNN )
set ( OPENCV_LINKER_LIBS ${ OPENCV_LINKER_LIBS } ${ CUDNN_LIBRARIES } )
endif ( )
if ( HAVE_CUFFT )
set ( OPENCV_LINKER_LIBS ${ OPENCV_LINKER_LIBS } ${ CUDA_cufft_LIBRARY } )
endif ( )
foreach ( p ${ CUDA_LIBS_PATH } )
if ( MSVC AND CMAKE_GENERATOR MATCHES "Ninja|JOM" )
set ( OPENCV_LINKER_LIBS ${ OPENCV_LINKER_LIBS } ${ CMAKE_LIBRARY_PATH_FLAG } "${p}" )
else ( )
set ( OPENCV_LINKER_LIBS ${ OPENCV_LINKER_LIBS } ${ CMAKE_LIBRARY_PATH_FLAG } ${ p } )
endif ( )
endforeach ( )
endif ( )