@ -0,0 +1,102 @@ |
||||
/*
|
||||
* By downloading, copying, installing or using the software you agree to this license. |
||||
* If you do not agree to this license, do not download, install, |
||||
* copy or use the software. |
||||
* |
||||
* |
||||
* License Agreement |
||||
* For Open Source Computer Vision Library |
||||
* (3-clause BSD License) |
||||
* |
||||
* Copyright (C) 2014-2015, NVIDIA Corporation, all rights reserved. |
||||
* Third party copyrights are property of their respective owners. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without modification, |
||||
* are permitted provided that the following conditions are met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright notice, |
||||
* this list of conditions and the following disclaimer. |
||||
* |
||||
* * Redistributions in binary form must reproduce the above copyright notice, |
||||
* this list of conditions and the following disclaimer in the documentation |
||||
* and/or other materials provided with the distribution. |
||||
* |
||||
* * Neither the names of the copyright holders nor the names of the contributors |
||||
* may be used to endorse or promote products derived from this software |
||||
* without specific prior written permission. |
||||
* |
||||
* This software is provided by the copyright holders and contributors "as is" and |
||||
* any express or implied warranties, including, but not limited to, the implied |
||||
* warranties of merchantability and fitness for a particular purpose are disclaimed. |
||||
* In no event shall copyright holders or contributors be liable for any direct, |
||||
* indirect, incidental, special, exemplary, or consequential damages |
||||
* (including, but not limited to, procurement of substitute goods or services; |
||||
* loss of use, data, or profits; or business interruption) however caused |
||||
* and on any theory of liability, whether in contract, strict liability, |
||||
* or tort (including negligence or otherwise) arising in any way out of |
||||
* the use of this software, even if advised of the possibility of such damage. |
||||
*/ |
||||
|
||||
#ifndef CAROTENE_SRC_VROUND_HELPER_HPP |
||||
#define CAROTENE_SRC_VROUND_HELPER_HPP |
||||
|
||||
#include "common.hpp" |
||||
#include "vtransform.hpp" |
||||
|
||||
#ifdef CAROTENE_NEON |
||||
|
||||
/**
|
||||
* This helper header is for rounding from float32xN to uin32xN or int32xN to nearest, ties to even. |
||||
* See https://en.wikipedia.org/wiki/Rounding#Rounding_half_to_even
|
||||
*/ |
||||
|
||||
// See https://github.com/opencv/opencv/pull/24271#issuecomment-1867318007
|
||||
#define CAROTENE_ROUND_DELTA (12582912.0f) |
||||
|
||||
namespace CAROTENE_NS { namespace internal { |
||||
|
||||
inline uint32x4_t vroundq_u32_f32(const float32x4_t val) |
||||
{ |
||||
#if CAROTENE_NEON_ARCH >= 8 /* get ready for ARMv9 */ |
||||
return vcvtnq_u32_f32(val); |
||||
#else |
||||
const float32x4_t delta = vdupq_n_f32(CAROTENE_ROUND_DELTA); |
||||
return vcvtq_u32_f32(vsubq_f32(vaddq_f32(val, delta), delta)); |
||||
#endif |
||||
} |
||||
|
||||
inline uint32x2_t vround_u32_f32(const float32x2_t val) |
||||
{ |
||||
#if CAROTENE_NEON_ARCH >= 8 /* get ready for ARMv9 */ |
||||
return vcvtn_u32_f32(val); |
||||
#else |
||||
const float32x2_t delta = vdup_n_f32(CAROTENE_ROUND_DELTA); |
||||
return vcvt_u32_f32(vsub_f32(vadd_f32(val, delta), delta)); |
||||
#endif |
||||
} |
||||
|
||||
inline int32x4_t vroundq_s32_f32(const float32x4_t val) |
||||
{ |
||||
#if CAROTENE_NEON_ARCH >= 8 /* get ready for ARMv9 */ |
||||
return vcvtnq_s32_f32(val); |
||||
#else |
||||
const float32x4_t delta = vdupq_n_f32(CAROTENE_ROUND_DELTA); |
||||
return vcvtq_s32_f32(vsubq_f32(vaddq_f32(val, delta), delta)); |
||||
#endif |
||||
} |
||||
|
||||
inline int32x2_t vround_s32_f32(const float32x2_t val) |
||||
{ |
||||
#if CAROTENE_NEON_ARCH >= 8 /* get ready for ARMv9 */ |
||||
return vcvtn_s32_f32(val); |
||||
#else |
||||
const float32x2_t delta = vdup_n_f32(CAROTENE_ROUND_DELTA); |
||||
return vcvt_s32_f32(vsub_f32(vadd_f32(val, delta), delta)); |
||||
#endif |
||||
} |
||||
|
||||
} } |
||||
|
||||
#endif // CAROTENE_NEON
|
||||
|
||||
#endif |
@ -0,0 +1,154 @@ |
||||
####################### |
||||
# Previously in FindCUDA and still required for FindCUDNN |
||||
macro(FIND_CUDA_HELPER_LIBS _name) |
||||
if(CMAKE_CROSSCOMPILING AND (ARM OR AARCH64)) |
||||
set(_cuda_cross_arm_lib_dir "lib/stubs") |
||||
endif() |
||||
find_library(CUDA_${_name}_LIBRARY ${_name} |
||||
NAMES ${_name} |
||||
PATHS "${CUDAToolkit_LIBRARY_ROOT}" |
||||
PATH_SUFFIXES "lib/x64" "lib64" ${_cuda_cross_arm_lib_dir} "lib/Win32" "lib" |
||||
DOC "\"${_name}\" library" |
||||
) |
||||
mark_as_advanced(CUDA_${_name}_LIBRARY) |
||||
endmacro() |
||||
####################### |
||||
include(cmake/OpenCVDetectCUDAUtils.cmake) |
||||
|
||||
if((WIN32 AND NOT MSVC) OR OPENCV_CMAKE_FORCE_CUDA) |
||||
message(STATUS "CUDA: Compilation is disabled (due to only Visual Studio compiler supported on your platform).") |
||||
return() |
||||
endif() |
||||
|
||||
if((NOT UNIX AND CV_CLANG) OR OPENCV_CMAKE_FORCE_CUDA) |
||||
message(STATUS "CUDA: Compilation is disabled (due to Clang unsupported on your platform).") |
||||
return() |
||||
endif() |
||||
|
||||
#set(OPENCV_CMAKE_CUDA_DEBUG 1) |
||||
|
||||
find_package(CUDAToolkit) |
||||
if(CMAKE_CUDA_COMPILER AND CUDAToolkit_FOUND) |
||||
set(CUDA_FOUND TRUE) |
||||
set(CUDA_TOOLKIT_INCLUDE ${CUDAToolkit_INCLUDE_DIRS}) |
||||
set(CUDA_VERSION_STRING ${CUDAToolkit_VERSION}) |
||||
set(CUDA_VERSION ${CUDAToolkit_VERSION}) |
||||
if(NOT CUDA_VERSION VERSION_LESS 11.0) |
||||
set(CMAKE_CUDA_STANDARD 14) |
||||
else() |
||||
set(CMAKE_CUDA_STANDARD 11) |
||||
endif() |
||||
if(UNIX AND NOT BUILD_SHARED_LIBS) |
||||
set(CUDA_LIB_EXT "_static") |
||||
endif() |
||||
endif() |
||||
|
||||
if(NOT CUDA_FOUND) |
||||
unset(CUDA_ARCH_BIN CACHE) |
||||
unset(CUDA_ARCH_PTX CACHE) |
||||
return() |
||||
endif() |
||||
|
||||
set(HAVE_CUDA 1) |
||||
|
||||
if(WITH_CUFFT) |
||||
set(HAVE_CUFFT 1) |
||||
endif() |
||||
|
||||
if(WITH_CUBLAS) |
||||
set(HAVE_CUBLAS 1) |
||||
endif() |
||||
|
||||
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) |
||||
|
||||
if(CUDNN_FOUND) |
||||
set(HAVE_CUDNN 1) |
||||
endif() |
||||
endif() |
||||
|
||||
if(WITH_NVCUVID OR WITH_NVCUVENC) |
||||
ocv_check_for_nvidia_video_codec_sdk("${CUDAToolkit_LIBRARY_ROOT}") |
||||
endif() |
||||
|
||||
ocv_check_for_cmake_cuda_architectures() |
||||
ocv_set_cuda_detection_nvcc_flags(CMAKE_CUDA_HOST_COMPILER) |
||||
ocv_set_cuda_arch_bin_and_ptx(${CUDAToolkit_NVCC_EXECUTABLE}) |
||||
|
||||
# NVCC flags to be set |
||||
set(NVCC_FLAGS_EXTRA "") |
||||
|
||||
# These vars will be passed into the templates |
||||
set(OPENCV_CUDA_ARCH_BIN "") |
||||
set(OPENCV_CUDA_ARCH_PTX "") |
||||
set(OPENCV_CUDA_ARCH_FEATURES "") |
||||
|
||||
# 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(CMAKE_CUDA_ARCHITECTURES ${CMAKE_CUDA_ARCHITECTURES} ${CMAKE_MATCH_2}-virtual;${CMAKE_MATCH_1}-real;) |
||||
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(CMAKE_CUDA_ARCHITECTURES ${CMAKE_CUDA_ARCHITECTURES} ${ARCH}-real;) |
||||
set(OPENCV_CUDA_ARCH_BIN "${OPENCV_CUDA_ARCH_BIN} ${ARCH}") |
||||
set(OPENCV_CUDA_ARCH_FEATURES "${OPENCV_CUDA_ARCH_FEATURES} ${ARCH}") |
||||
endif() |
||||
endforeach() |
||||
set(NVCC_FLAGS_EXTRA ${NVCC_FLAGS_EXTRA} -D_FORCE_INLINES) |
||||
|
||||
# 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(CMAKE_CUDA_ARCHITECTURES ${CMAKE_CUDA_ARCHITECTURES} ${ARCH}-virtual;) |
||||
set(OPENCV_CUDA_ARCH_PTX "${OPENCV_CUDA_ARCH_PTX} ${ARCH}") |
||||
set(OPENCV_CUDA_ARCH_FEATURES "${OPENCV_CUDA_ARCH_FEATURES} ${ARCH}") |
||||
endforeach() |
||||
|
||||
ocv_set_nvcc_threads_for_vs() |
||||
|
||||
# These vars will be processed in other scripts |
||||
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} ${NVCC_FLAGS_EXTRA}) |
||||
set(OpenCV_CUDA_CC "${CMAKE_CUDA_ARCHITECTURES}") |
||||
|
||||
if(ANDROID) |
||||
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} "-Xptxas;-dlcm=ca") |
||||
endif() |
||||
|
||||
message(STATUS "CUDA: NVCC target flags ${CUDA_NVCC_FLAGS}") |
||||
|
||||
OCV_OPTION(CUDA_FAST_MATH "Enable --use_fast_math for CUDA compiler " OFF) |
||||
|
||||
if(CUDA_FAST_MATH) |
||||
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} --use_fast_math) |
||||
endif() |
||||
|
||||
OCV_OPTION(CUDA_ENABLE_DELAYLOAD "Enable delayed loading of CUDA DLLs" OFF VISIBLE_IF MSVC) |
||||
|
||||
mark_as_advanced(CUDA_BUILD_CUBIN CUDA_BUILD_EMULATION CUDA_VERBOSE_BUILD CUDA_SDK_ROOT_DIR) |
||||
|
||||
macro(ocv_cuda_unfilter_options) |
||||
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() |
||||
|
||||
macro(ocv_cuda_compile_flags) |
||||
ocv_cuda_filter_options() |
||||
ocv_nvcc_flags() |
||||
set(CMAKE_CXX_FLAGS_CUDA ${CMAKE_CXX_FLAGS}) |
||||
set(CMAKE_CXX_FLAGS_RELEASE_CUDA ${CMAKE_CXX_FLAGS_RELEASE}) |
||||
set(CMAKE_CXX_FLAGS_DEBUG_CUDA ${CMAKE_CXX_FLAGS_DEBUG}) |
||||
ocv_cuda_unfilter_options() |
||||
endmacro() |
||||
|
||||
if(HAVE_CUDA) |
||||
ocv_apply_cuda_stub_workaround("${CUDA_cuda_driver_LIBRARY}") |
||||
ocv_check_cuda_delayed_load("${cuda_toolkit_root_dir}") |
||||
endif() |
@ -0,0 +1,442 @@ |
||||
macro(ocv_check_for_nvidia_video_codec_sdk cuda_toolkit_dirs) |
||||
macro(ocv_cuda_SEARCH_NVCUVID_HEADER _filename _result) |
||||
# place header file under CUDAToolkit_LIBRARY_ROOT |
||||
find_path(_header_result |
||||
${_filename} |
||||
PATHS ${cuda_toolkit_dirs} |
||||
PATH_SUFFIXES include |
||||
NO_DEFAULT_PATH |
||||
) |
||||
if("x${_header_result}" STREQUAL "x_header_result-NOTFOUND") |
||||
set(${_result} 0) |
||||
else() |
||||
set(${_result} 1) |
||||
endif() |
||||
unset(_header_result CACHE) |
||||
endmacro() |
||||
if(WITH_NVCUVID) |
||||
ocv_cuda_SEARCH_NVCUVID_HEADER("nvcuvid.h" HAVE_NVCUVID_HEADER) |
||||
# make sure to have both header and library before enabling |
||||
if(${HAVE_NVCUVID_HEADER}) |
||||
find_cuda_helper_libs(nvcuvid) |
||||
if(CUDA_nvcuvid_LIBRARY) |
||||
set(HAVE_NVCUVID 1) |
||||
message(STATUS "Found NVCUVID: ${CUDA_nvcuvid_LIBRARY}") |
||||
else() |
||||
if(WIN32) |
||||
message(STATUS "NVCUVID: Library not found, WITH_NVCUVID requires Nvidia decoding library nvcuvid.lib to either be inside ${cuda_toolkit_dirs}/lib or its location manually set with CUDA_nvcuvid_LIBRARY, i.e. CUDA_nvcuvid_LIBRARY=${cuda_toolkit_dirs}/lib/nvcuvid.lib") |
||||
else() |
||||
message(STATUS "NVCUVID: Library not found, WITH_NVCUVID requires the Nvidia decoding shared library nvcuvid.so from the driver installation or the location of the stub library to be manually set with CUDA_nvcuvid_LIBRARY i.e. CUDA_nvcuvid_LIBRARY=/home/user/Video_Codec_SDK_X.X.X/Lib/linux/stubs/x86_64/nvcuvid.so") |
||||
endif() |
||||
endif() |
||||
else() |
||||
message(STATUS "NVCUVID: Header not found, WITH_NVCUVID requires Nvidia decoding library header ${cuda_toolkit_dirs}/include/nvcuvid.h") |
||||
endif() |
||||
endif() |
||||
|
||||
if(WITH_NVCUVENC) |
||||
ocv_cuda_SEARCH_NVCUVID_HEADER("nvEncodeAPI.h" HAVE_NVCUVENC_HEADER) |
||||
if(${HAVE_NVCUVENC_HEADER}) |
||||
if(WIN32) |
||||
find_cuda_helper_libs(nvencodeapi) |
||||
else() |
||||
find_cuda_helper_libs(nvidia-encode) |
||||
endif() |
||||
if(CUDA_nvencodeapi_LIBRARY OR CUDA_nvidia-encode_LIBRARY) |
||||
set(HAVE_NVCUVENC 1) |
||||
message(STATUS "Found NVCUVENC: ${CUDA_nvencodeapi_LIBRARY} ${CUDA_nvidia-encode_LIBRARY}") |
||||
else() |
||||
if(WIN32) |
||||
message(STATUS "NVCUVENC: Library not found, WITH_NVCUVENC requires Nvidia encoding library nvencodeapi.lib to either be inside ${cuda_toolkit_dirs}/lib or its location manually set with CUDA_nvencodeapi_LIBRARY, i.e. CUDA_nvencodeapi_LIBRARY=${cuda_toolkit_dirs}/lib/nvencodeapi.lib") |
||||
else() |
||||
message(STATUS "NVCUVENC: Library not found, WITH_NVCUVENC requires the Nvidia encoding shared library libnvidia-encode.so from the driver installation or the location of the stub library to be manually set with CUDA_nvidia-encode_LIBRARY i.e. CUDA_nvidia-encode_LIBRARY=/home/user/Video_Codec_SDK_X.X.X/Lib/linux/stubs/x86_64/libnvidia-encode.so") |
||||
endif() |
||||
endif() |
||||
else() |
||||
message(STATUS "NVCUVENC: Header not found, WITH_NVCUVENC requires Nvidia encoding library header ${cuda_toolkit_dirs}/include/nvEncodeAPI.h") |
||||
endif() |
||||
endif() |
||||
endmacro() |
||||
|
||||
# Use CMAKE_CUDA_ARCHITECTURES if provided: order of preference CMAKE_CUDA_ARCHITECTURES > CUDA_GENERATION > CUDA_ARCH_BIN and/or CUDA_ARCH_PTX |
||||
function(ocv_check_for_cmake_cuda_architectures) |
||||
if(NOT CMAKE_CUDA_ARCHITECTURES) |
||||
return() |
||||
endif() |
||||
if(CMAKE_CUDA_ARCHITECTURES STREQUAL "all" OR CMAKE_CUDA_ARCHITECTURES STREQUAL "all-major" OR CMAKE_CUDA_ARCHITECTURES STREQUAL "native") |
||||
message(WARNING "CUDA: CMAKE_CUDA_ARCHITECTURES=${CMAKE_CUDA_ARCHITECTURES}, special values all, all-major and native are not supported by OpenCV, specify only CUDA real and/or virtual architectures or use combinations of CUDA_ARCH_BIN and CUDA_ARCH_PTX or specify the CUDA_GENERATION where -DCUDA_GENERATION=Auto is equivalent to native!") |
||||
return() |
||||
endif() |
||||
set(internal_ptx "") |
||||
set(internal_bin "") |
||||
foreach(ARCH IN LISTS CMAKE_CUDA_ARCHITECTURES) |
||||
if(ARCH MATCHES "([0-9]+)\-real") |
||||
set(internal_bin ${internal_bin} ${CMAKE_MATCH_1};) |
||||
elseif(ARCH MATCHES "([0-9]+)\-virtual") |
||||
set(internal_ptx ${internal_ptx} ${CMAKE_MATCH_1};) |
||||
elseif(ARCH MATCHES "([0-9]+)") |
||||
set(internal_bin ${internal_bin} ${CMAKE_MATCH_1};) |
||||
set(internal_ptx ${internal_ptx} ${CMAKE_MATCH_1};) |
||||
endif() |
||||
endforeach() |
||||
if(internal_bin OR internal_ptx) |
||||
unset(CUDA_ARCH_BIN CACHE) |
||||
unset(CUDA_ARCH_PTX CACHE) |
||||
endif() |
||||
if(internal_ptx) |
||||
set(CUDA_ARCH_PTX ${internal_ptx} CACHE STRING "Specify 'virtual' PTX architectures to build PTX intermediate code for (see https://docs.opencv.org/5.x/d2/dbc/cuda_intro.html)") |
||||
endif() |
||||
if(internal_bin) |
||||
set(CUDA_ARCH_BIN ${internal_bin} CACHE STRING "Specify 'real' GPU architectures to build binaries for, BIN(PTX) format is supported (see https://docs.opencv.org/5.x/d2/dbc/cuda_intro.html)") |
||||
endif() |
||||
set(CMAKE_CUDA_ARCHITECTURES "" PARENT) |
||||
unset(CUDA_GENERATION CACHE) |
||||
endfunction() |
||||
|
||||
macro(ocv_initialize_nvidia_device_generations) |
||||
OCV_OPTION(CUDA_ENABLE_DEPRECATED_GENERATION "Enable deprecated generations in the list" OFF) |
||||
set(_generations "Maxwell" "Pascal" "Volta" "Turing" "Ampere" "Lovelace" "Hopper") |
||||
if(CUDA_ENABLE_DEPRECATED_GENERATION) |
||||
set(_generations "Fermi" "${_generations}") |
||||
set(_generations "Kepler" "${_generations}") |
||||
endif() |
||||
set(_arch_fermi "2.0") |
||||
set(_arch_kepler "3.0;3.5;3.7") |
||||
set(_arch_maxwell "5.0;5.2") |
||||
set(_arch_pascal "6.0;6.1") |
||||
set(_arch_volta "7.0") |
||||
set(_arch_turing "7.5") |
||||
set(_arch_ampere "8.0;8.6") |
||||
set(_arch_lovelace "8.9") |
||||
set(_arch_hopper "9.0") |
||||
if(NOT CMAKE_CROSSCOMPILING) |
||||
list(APPEND _generations "Auto") |
||||
endif() |
||||
set(CUDA_GENERATION "" CACHE STRING "Build CUDA device code only for specific GPU architecture. Leave empty to build for all architectures (see https://docs.opencv.org/5.x/d2/dbc/cuda_intro.html).") |
||||
if( CMAKE_VERSION VERSION_GREATER "2.8" ) |
||||
set_property( CACHE CUDA_GENERATION PROPERTY STRINGS "" ${_generations} ) |
||||
endif() |
||||
|
||||
if(CUDA_GENERATION) |
||||
if(NOT ";${_generations};" MATCHES ";${CUDA_GENERATION};") |
||||
string(REPLACE ";" ", " _generations "${_generations}") |
||||
message(FATAL_ERROR "ERROR: ${_generations} Generations are supported.") |
||||
endif() |
||||
unset(CUDA_ARCH_BIN CACHE) |
||||
unset(CUDA_ARCH_PTX CACHE) |
||||
endif() |
||||
endmacro() |
||||
|
||||
macro(ocv_set_cuda_detection_nvcc_flags cuda_host_compiler_var) |
||||
if(OPENCV_CUDA_DETECTION_NVCC_FLAGS MATCHES "-ccbin") |
||||
# already specified by user |
||||
elseif(${cuda_host_compiler_var} AND EXISTS "${${cuda_host_compiler_var}}") |
||||
get_filename_component(c_compiler_realpath "${CMAKE_C_COMPILER}" REALPATH) |
||||
# C compiler doesn't work with --run option, forcing C++ compiler instead |
||||
if(${cuda_host_compiler_var} STREQUAL c_compiler_realpath OR ${cuda_host_compiler_var} STREQUAL CMAKE_C_COMPILER) |
||||
if(DEFINED CMAKE_CXX_COMPILER) |
||||
get_filename_component(cxx_compiler_realpath "${CMAKE_CXX_COMPILER}" REALPATH) |
||||
LIST(APPEND OPENCV_CUDA_DETECTION_NVCC_FLAGS -ccbin "${cxx_compiler_realpath}") |
||||
else() |
||||
message(STATUS "CUDA: CMAKE_CXX_COMPILER is not available. You may need to specify ${cuda_host_compiler_var}.") |
||||
endif() |
||||
else() |
||||
LIST(APPEND OPENCV_CUDA_DETECTION_NVCC_FLAGS -ccbin "${${cuda_host_compiler_var}}") |
||||
endif() |
||||
elseif(WIN32 AND CMAKE_LINKER) # Workaround for VS cl.exe not being in the env. path |
||||
get_filename_component(host_compiler_bindir ${CMAKE_LINKER} DIRECTORY) |
||||
LIST(APPEND OPENCV_CUDA_DETECTION_NVCC_FLAGS -ccbin "${host_compiler_bindir}") |
||||
else() |
||||
if(${cuda_host_compiler_var}) |
||||
message(STATUS "CUDA: ${cuda_host_compiler_var}='${cuda_host_compiler}' is not valid, autodetection may not work. Specify OPENCV_CUDA_DETECTION_NVCC_FLAGS with -ccbin option for fix that") |
||||
endif() |
||||
endif() |
||||
endmacro() |
||||
|
||||
macro(ocv_filter_available_architecture nvcc_executable result_list) |
||||
set(__cache_key_check "${ARGN} : ${nvcc_executable} ${OPENCV_CUDA_DETECTION_NVCC_FLAGS}") |
||||
if(DEFINED OPENCV_CACHE_CUDA_SUPPORTED_CC AND OPENCV_CACHE_CUDA_SUPPORTED_CC_check STREQUAL __cache_key_check) |
||||
set(${result_list} "${OPENCV_CACHE_CUDA_SUPPORTED_CC}") |
||||
else() |
||||
set(CC_LIST ${ARGN}) |
||||
foreach(target_arch ${CC_LIST}) |
||||
string(REPLACE "." "" target_arch_short "${target_arch}") |
||||
set(NVCC_OPTION "-gencode;arch=compute_${target_arch_short},code=sm_${target_arch_short}") |
||||
set(_cmd "${nvcc_executable}" ${OPENCV_CUDA_DETECTION_NVCC_FLAGS} ${NVCC_OPTION} "${OpenCV_SOURCE_DIR}/cmake/checks/OpenCVDetectCudaArch.cu" --compile) |
||||
execute_process( |
||||
COMMAND ${_cmd} |
||||
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/" |
||||
RESULT_VARIABLE _nvcc_res |
||||
OUTPUT_VARIABLE _nvcc_out |
||||
ERROR_VARIABLE _nvcc_err |
||||
#ERROR_QUIET |
||||
OUTPUT_STRIP_TRAILING_WHITESPACE |
||||
) |
||||
if(OPENCV_CMAKE_CUDA_DEBUG) |
||||
message(WARNING "COMMAND: ${_cmd}") |
||||
message(STATUS "Result: ${_nvcc_res}") |
||||
message(STATUS "Out: ${_nvcc_out}") |
||||
message(STATUS "Err: ${_nvcc_err}") |
||||
endif() |
||||
if(_nvcc_res EQUAL 0) |
||||
LIST(APPEND ${result_list} "${target_arch}") |
||||
endif() |
||||
endforeach() |
||||
string(STRIP "${${result_list}}" ${result_list}) |
||||
if(" ${${result_list}}" STREQUAL " ") |
||||
message(WARNING "CUDA: Autodetection arch list is empty. Please enable OPENCV_CMAKE_CUDA_DEBUG=1 and check/specify OPENCV_CUDA_DETECTION_NVCC_FLAGS variable") |
||||
endif() |
||||
|
||||
# cache detected values |
||||
set(OPENCV_CACHE_CUDA_SUPPORTED_CC ${${result_list}} CACHE INTERNAL "") |
||||
set(OPENCV_CACHE_CUDA_SUPPORTED_CC_check "${__cache_key_check}" CACHE INTERNAL "") |
||||
endif() |
||||
endmacro() |
||||
|
||||
macro(ocv_detect_native_cuda_arch nvcc_executable status output) |
||||
set(OPENCV_CUDA_DETECT_ARCHS_COMMAND "${nvcc_executable}" ${OPENCV_CUDA_DETECTION_NVCC_FLAGS} "${OpenCV_SOURCE_DIR}/cmake/checks/OpenCVDetectCudaArch.cu" "--run") |
||||
set(__cache_key_check "${OPENCV_CUDA_DETECT_ARCHS_COMMAND}") |
||||
if(DEFINED OPENCV_CACHE_CUDA_ACTIVE_CC AND OPENCV_CACHE_CUDA_ACTIVE_CC_check STREQUAL __cache_key_check) |
||||
set(${output} "${OPENCV_CACHE_CUDA_ACTIVE_CC}") |
||||
set(${status} 0) |
||||
else() |
||||
execute_process( |
||||
COMMAND ${OPENCV_CUDA_DETECT_ARCHS_COMMAND} |
||||
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/" |
||||
RESULT_VARIABLE ${status} |
||||
OUTPUT_VARIABLE _nvcc_out |
||||
ERROR_VARIABLE _nvcc_err |
||||
ERROR_QUIET |
||||
OUTPUT_STRIP_TRAILING_WHITESPACE |
||||
) |
||||
if(OPENCV_CMAKE_CUDA_DEBUG) |
||||
message(WARNING "COMMAND: ${OPENCV_CUDA_DETECT_ARCHS_COMMAND}") |
||||
message(STATUS "Result: ${${status}}") |
||||
message(STATUS "Out: ${_nvcc_out}") |
||||
message(STATUS "Err: ${_nvcc_err}") |
||||
endif() |
||||
string(REGEX REPLACE ".*\n" "" ${output} "${_nvcc_out}") #Strip leading warning messages, if any |
||||
|
||||
if(${status} EQUAL 0) |
||||
# cache detected values |
||||
set(OPENCV_CACHE_CUDA_ACTIVE_CC ${${output}} CACHE INTERNAL "") |
||||
set(OPENCV_CACHE_CUDA_ACTIVE_CC_check "${__cache_key_check}" CACHE INTERNAL "") |
||||
endif() |
||||
endif() |
||||
endmacro() |
||||
|
||||
macro(ocv_set_cuda_arch_bin_and_ptx nvcc_executable) |
||||
ocv_initialize_nvidia_device_generations() |
||||
set(__cuda_arch_ptx ${CUDA_ARCH_PTX}) |
||||
if(CUDA_GENERATION STREQUAL "Fermi") |
||||
set(__cuda_arch_bin ${_arch_fermi}) |
||||
elseif(CUDA_GENERATION STREQUAL "Kepler") |
||||
set(__cuda_arch_bin ${_arch_kepler}) |
||||
elseif(CUDA_GENERATION STREQUAL "Maxwell") |
||||
set(__cuda_arch_bin ${_arch_maxwell}) |
||||
elseif(CUDA_GENERATION STREQUAL "Pascal") |
||||
set(__cuda_arch_bin ${_arch_pascal}) |
||||
elseif(CUDA_GENERATION STREQUAL "Volta") |
||||
set(__cuda_arch_bin ${_arch_volta}) |
||||
elseif(CUDA_GENERATION STREQUAL "Turing") |
||||
set(__cuda_arch_bin ${_arch_turing}) |
||||
elseif(CUDA_GENERATION STREQUAL "Ampere") |
||||
set(__cuda_arch_bin ${_arch_ampere}) |
||||
elseif(CUDA_GENERATION STREQUAL "Lovelace") |
||||
set(__cuda_arch_bin ${_arch_lovelace}) |
||||
elseif(CUDA_GENERATION STREQUAL "Hopper") |
||||
set(__cuda_arch_bin ${_arch_hopper}) |
||||
elseif(CUDA_GENERATION STREQUAL "Auto") |
||||
ocv_detect_native_cuda_arch(${nvcc_executable} _nvcc_res _nvcc_out) |
||||
if(NOT _nvcc_res EQUAL 0) |
||||
message(STATUS "CUDA: Automatic detection of CUDA generation failed. Going to build for all known architectures") |
||||
else() |
||||
string(REGEX MATCHALL "[0-9]+\\.[0-9]" __cuda_arch_bin "${_nvcc_out}") |
||||
endif() |
||||
elseif(CUDA_ARCH_BIN) |
||||
message(STATUS "CUDA: Using CUDA_ARCH_BIN=${CUDA_ARCH_BIN}") |
||||
set(__cuda_arch_bin ${CUDA_ARCH_BIN}) |
||||
endif() |
||||
|
||||
if(NOT DEFINED __cuda_arch_bin AND NOT DEFINED __cuda_arch_ptx) |
||||
if(ARM) |
||||
set(__cuda_arch_bin "3.2") |
||||
set(__cuda_arch_ptx "") |
||||
elseif(AARCH64) |
||||
if(NOT CMAKE_CROSSCOMPILING) |
||||
ocv_detect_native_cuda_arch(${nvcc_executable} _nvcc_res _nvcc_out) |
||||
else() |
||||
set(_nvcc_res -1) # emulate error, see below |
||||
endif() |
||||
if(NOT _nvcc_res EQUAL 0) |
||||
message(STATUS "CUDA: Automatic detection of CUDA generation failed. Going to build for all known architectures") |
||||
# TX1 (5.3) TX2 (6.2) Xavier (7.2) V100 (7.0) Orin (8.7) |
||||
ocv_filter_available_architecture(${nvcc_executable} __cuda_arch_bin |
||||
5.3 |
||||
6.2 |
||||
7.2 |
||||
7.0 |
||||
8.7 |
||||
) |
||||
else() |
||||
set(__cuda_arch_bin "${_nvcc_out}") |
||||
endif() |
||||
set(__cuda_arch_ptx "") |
||||
else() |
||||
ocv_filter_available_architecture(${nvcc_executable} __cuda_arch_bin |
||||
${_arch_fermi} |
||||
${_arch_kepler} |
||||
${_arch_maxwell} |
||||
${_arch_pascal} |
||||
${_arch_volta} |
||||
${_arch_turing} |
||||
${_arch_ampere} |
||||
${_arch_lovelace} |
||||
${_arch_hopper} |
||||
) |
||||
list(GET __cuda_arch_bin -1 __cuda_arch_ptx) |
||||
endif() |
||||
endif() |
||||
|
||||
set(CUDA_ARCH_BIN ${__cuda_arch_bin} CACHE STRING "Specify 'real' GPU architectures to build binaries for, BIN(PTX) format is supported (see https://docs.opencv.org/5.x/d2/dbc/cuda_intro.html)") |
||||
set(CUDA_ARCH_PTX ${__cuda_arch_ptx} CACHE STRING "Specify 'virtual' PTX architectures to build PTX intermediate code for (see https://docs.opencv.org/5.x/d2/dbc/cuda_intro.html)") |
||||
string(REGEX REPLACE "\\." "" ARCH_BIN_NO_POINTS "${CUDA_ARCH_BIN}") |
||||
string(REGEX REPLACE "\\." "" ARCH_PTX_NO_POINTS "${CUDA_ARCH_PTX}") |
||||
|
||||
# Check if user specified 1.0/2.1 compute capability: we don't support it |
||||
macro(ocv_wipeout_deprecated_cc target_cc) |
||||
if(" ${CUDA_ARCH_BIN} ${CUDA_ARCH_PTX}" MATCHES " ${target_cc}") |
||||
message(SEND_ERROR "CUDA: ${target_cc} compute capability is not supported - exclude it from ARCH/PTX list and re-run CMake") |
||||
endif() |
||||
endmacro() |
||||
ocv_wipeout_deprecated_cc("1.0") |
||||
ocv_wipeout_deprecated_cc("2.1") |
||||
endmacro() |
||||
|
||||
macro(ocv_set_nvcc_threads_for_vs) |
||||
# Tell NVCC the maximum number of threads to be used to execute the compilation steps in parallel |
||||
# (option --threads was introduced in version 11.2) |
||||
if(NOT CUDA_VERSION VERSION_LESS "11.2") |
||||
if(CMAKE_GENERATOR MATCHES "Visual Studio" AND NOT $ENV{CMAKE_BUILD_PARALLEL_LEVEL} STREQUAL "") |
||||
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} "--threads=$ENV{CMAKE_BUILD_PARALLEL_LEVEL}") |
||||
endif() |
||||
endif() |
||||
endmacro() |
||||
|
||||
macro(ocv_cuda_filter_options) |
||||
foreach(var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG) |
||||
set(${var}_backup_in_cuda_compile_ "${${var}}") |
||||
|
||||
if (CV_CLANG) |
||||
# we remove -Winconsistent-missing-override and -Qunused-arguments |
||||
# just in case we are compiling CUDA with gcc but OpenCV with clang |
||||
string(REPLACE "-Winconsistent-missing-override" "" ${var} "${${var}}") |
||||
string(REPLACE "-Qunused-arguments" "" ${var} "${${var}}") |
||||
endif() |
||||
|
||||
# we remove /EHa as it generates warnings under windows |
||||
string(REPLACE "/EHa" "" ${var} "${${var}}") |
||||
|
||||
# we remove -ggdb3 flag as it leads to preprocessor errors when compiling CUDA files (CUDA 4.1) |
||||
string(REPLACE "-ggdb3" "" ${var} "${${var}}") |
||||
|
||||
# we remove -Wsign-promo as it generates warnings under linux |
||||
string(REPLACE "-Wsign-promo" "" ${var} "${${var}}") |
||||
|
||||
# we remove -Wno-sign-promo as it generates warnings under linux |
||||
string(REPLACE "-Wno-sign-promo" "" ${var} "${${var}}") |
||||
|
||||
# we remove -Wno-delete-non-virtual-dtor because it's used for C++ compiler |
||||
# but NVCC uses C compiler by default |
||||
string(REPLACE "-Wno-delete-non-virtual-dtor" "" ${var} "${${var}}") |
||||
|
||||
# we remove -frtti because it's used for C++ compiler |
||||
# but NVCC uses C compiler by default |
||||
string(REPLACE "-frtti" "" ${var} "${${var}}") |
||||
|
||||
string(REPLACE "-fvisibility-inlines-hidden" "" ${var} "${${var}}") |
||||
|
||||
# cc1: warning: command line option '-Wsuggest-override' is valid for C++/ObjC++ but not for C |
||||
string(REPLACE "-Wsuggest-override" "" ${var} "${${var}}") |
||||
|
||||
# issue: #11552 (from OpenCVCompilerOptions.cmake) |
||||
string(REGEX REPLACE "-Wimplicit-fallthrough(=[0-9]+)? " "" ${var} "${${var}}") |
||||
|
||||
# removal of custom specified options |
||||
if(OPENCV_CUDA_NVCC_FILTEROUT_OPTIONS) |
||||
foreach(__flag ${OPENCV_CUDA_NVCC_FILTEROUT_OPTIONS}) |
||||
string(REPLACE "${__flag}" "" ${var} "${${var}}") |
||||
endforeach() |
||||
endif() |
||||
endforeach() |
||||
endmacro() |
||||
|
||||
macro(ocv_nvcc_flags) |
||||
if(BUILD_SHARED_LIBS) |
||||
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -Xcompiler=-DCVAPI_EXPORTS) |
||||
endif() |
||||
|
||||
if(UNIX OR APPLE) |
||||
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -Xcompiler=-fPIC) |
||||
endif() |
||||
if(APPLE) |
||||
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -Xcompiler=-fno-finite-math-only) |
||||
endif() |
||||
|
||||
if(WIN32 AND NOT (CUDA_VERSION VERSION_LESS "11.2")) |
||||
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -Xcudafe --display_error_number --diag-suppress 1394,1388) |
||||
endif() |
||||
|
||||
if(CMAKE_CROSSCOMPILING AND (ARM OR AARCH64)) |
||||
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -Xlinker --unresolved-symbols=ignore-in-shared-libs) |
||||
endif() |
||||
|
||||
# disabled because of multiple warnings during building nvcc auto generated files |
||||
if(CV_GCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "4.6.0") |
||||
ocv_warnings_disable(CMAKE_CXX_FLAGS -Wunused-but-set-variable) |
||||
endif() |
||||
endmacro() |
||||
|
||||
macro(ocv_apply_cuda_stub_workaround cuda_driver_library_path) |
||||
# details: https://github.com/NVIDIA/nvidia-docker/issues/775 |
||||
if(" ${cuda_driver_library_path}" MATCHES "/stubs/libcuda.so" AND NOT OPENCV_SKIP_CUDA_STUB_WORKAROUND) |
||||
set(CUDA_STUB_ENABLED_LINK_WORKAROUND 1) |
||||
if(EXISTS "${cuda_driver_library_path}" AND NOT OPENCV_SKIP_CUDA_STUB_WORKAROUND_RPATH_LINK) |
||||
set(CUDA_STUB_TARGET_PATH "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/") |
||||
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${cuda_driver_library_path}" "${CUDA_STUB_TARGET_PATH}/libcuda.so.1" |
||||
RESULT_VARIABLE CUDA_STUB_SYMLINK_RESULT) |
||||
if(NOT CUDA_STUB_SYMLINK_RESULT EQUAL 0) |
||||
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${cuda_driver_library_path}" "${CUDA_STUB_TARGET_PATH}/libcuda.so.1" |
||||
RESULT_VARIABLE CUDA_STUB_COPY_RESULT) |
||||
if(NOT CUDA_STUB_COPY_RESULT EQUAL 0) |
||||
set(CUDA_STUB_ENABLED_LINK_WORKAROUND 0) |
||||
endif() |
||||
endif() |
||||
if(CUDA_STUB_ENABLED_LINK_WORKAROUND) |
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath-link,\"${CUDA_STUB_TARGET_PATH}\"") |
||||
endif() |
||||
else() |
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--allow-shlib-undefined") |
||||
endif() |
||||
if(NOT CUDA_STUB_ENABLED_LINK_WORKAROUND) |
||||
message(WARNING "CUDA: Workaround for stubs/libcuda.so.1 is not applied") |
||||
endif() |
||||
endif() |
||||
endmacro() |
||||
|
||||
macro(ocv_check_cuda_delayed_load cuda_toolkit_root_dir) |
||||
if(MSVC AND CUDA_ENABLE_DELAYLOAD) |
||||
set(DELAYFLAGS "delayimp.lib") |
||||
file(GLOB CUDA_DLLS "${cuda_toolkit_root_dir}/bin/*.dll") |
||||
foreach(d ${CUDA_DLLS}) |
||||
cmake_path(GET "d" FILENAME DLL_NAME) |
||||
if(NOT ${DLL_NAME} MATCHES "cudart") |
||||
set(DELAYFLAGS "${DELAYFLAGS} /DELAYLOAD:${DLL_NAME}") |
||||
endif() |
||||
endforeach() |
||||
set(DELAYFLAGS "${DELAYFLAGS} /DELAYLOAD:nvcuda.dll /DELAYLOAD:nvml.dll /IGNORE:4199") |
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${DELAYFLAGS}") |
||||
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${DELAYFLAGS}") |
||||
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${DELAYFLAGS}") |
||||
endif() |
||||
endmacro() |
@ -0,0 +1,13 @@ |
||||
if(WIN32) |
||||
try_compile(__VALID_DIRECTML |
||||
"${OpenCV_BINARY_DIR}" |
||||
"${OpenCV_SOURCE_DIR}/cmake/checks/directml.cpp" |
||||
LINK_LIBRARIES d3d12 dxcore directml |
||||
OUTPUT_VARIABLE TRY_OUT |
||||
) |
||||
if(NOT __VALID_DIRECTML) |
||||
message(STATUS "No support for DirectML (d3d12, dxcore, directml libs are required)") |
||||
return() |
||||
endif() |
||||
set(HAVE_DIRECTML ON) |
||||
endif() |
@ -0,0 +1,38 @@ |
||||
#include <initguid.h> |
||||
|
||||
#include <d3d11.h> |
||||
#include <dxgi1_2.h> |
||||
#include <dxgi1_4.h> |
||||
#include <dxgi.h> |
||||
#include <dxcore.h> |
||||
#include <dxcore_interface.h> |
||||
#include <d3d12.h> |
||||
#include <directml.h> |
||||
|
||||
int main(int /*argc*/, char** /*argv*/) |
||||
{ |
||||
IDXCoreAdapterFactory* factory; |
||||
DXCoreCreateAdapterFactory(__uuidof(IDXCoreAdapterFactory), (void**)&factory); |
||||
|
||||
IDXCoreAdapterList* adapterList; |
||||
const GUID dxGUIDs[] = { DXCORE_ADAPTER_ATTRIBUTE_D3D12_CORE_COMPUTE }; |
||||
factory->CreateAdapterList(ARRAYSIZE(dxGUIDs), dxGUIDs, __uuidof(IDXCoreAdapterList), (void**)&adapterList); |
||||
|
||||
IDXCoreAdapter* adapter; |
||||
adapterList->GetAdapter(0u, __uuidof(IDXCoreAdapter), (void**)&adapter); |
||||
|
||||
D3D_FEATURE_LEVEL d3dFeatureLevel = D3D_FEATURE_LEVEL_1_0_CORE; |
||||
ID3D12Device* d3d12Device = NULL; |
||||
D3D12CreateDevice((IUnknown*)adapter, d3dFeatureLevel, __uuidof(ID3D11Device), (void**)&d3d12Device); |
||||
|
||||
D3D12_COMMAND_LIST_TYPE commandQueueType = D3D12_COMMAND_LIST_TYPE_COMPUTE; |
||||
ID3D12CommandQueue* cmdQueue; |
||||
D3D12_COMMAND_QUEUE_DESC commandQueueDesc = {}; |
||||
commandQueueDesc.Type = commandQueueType; |
||||
|
||||
d3d12Device->CreateCommandQueue(&commandQueueDesc, __uuidof(ID3D12CommandQueue), (void**)&cmdQueue); |
||||
IDMLDevice* dmlDevice; |
||||
DMLCreateDevice(d3d12Device, DML_CREATE_DEVICE_FLAG_NONE, IID_PPV_ARGS(&dmlDevice)); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,31 @@ |
||||
# Version Compute Capability from which OpenCV has been compiled is remembered |
||||
set(OpenCV_COMPUTE_CAPABILITIES "@OpenCV_CUDA_CC@") |
||||
|
||||
set(OpenCV_CUDA_VERSION "@CUDA_VERSION_STRING@") |
||||
set(OpenCV_USE_CUBLAS "@HAVE_CUBLAS@") |
||||
set(OpenCV_USE_CUFFT "@HAVE_CUFFT@") |
||||
set(OpenCV_USE_NVCUVID "@HAVE_NVCUVID@") |
||||
set(OpenCV_USE_NVCUVENC "@HAVE_NVCUVENC@") |
||||
set(OpenCV_CUDNN_VERSION "@CUDNN_VERSION@") |
||||
set(OpenCV_USE_CUDNN "@HAVE_CUDNN@") |
||||
set(ENABLE_CUDA_FIRST_CLASS_LANGUAGE ON) |
||||
|
||||
if(NOT CUDAToolkit_FOUND) |
||||
if(NOT CMAKE_VERSION VERSION_LESS 3.18) |
||||
if(UNIX AND NOT CMAKE_CUDA_COMPILER AND NOT CUDAToolkit_ROOT) |
||||
message(STATUS "Checking for CUDAToolkit in default location (/usr/local/cuda)") |
||||
set(CUDA_PATH "/usr/local/cuda" CACHE INTERNAL "") |
||||
set(ENV{CUDA_PATH} ${CUDA_PATH}) |
||||
endif() |
||||
find_package(CUDAToolkit ${OpenCV_CUDA_VERSION} EXACT REQUIRED) |
||||
else() |
||||
message(FATAL_ERROR "Using OpenCV compiled with CUDA as first class language requires CMake \>= 3.18.") |
||||
endif() |
||||
else() |
||||
if(CUDAToolkit_FOUND) |
||||
set(CUDA_VERSION_STRING ${CUDAToolkit_VERSION}) |
||||
endif() |
||||
if(NOT CUDA_VERSION_STRING VERSION_EQUAL OpenCV_CUDA_VERSION) |
||||
message(FATAL_ERROR "OpenCV library was compiled with CUDA ${OpenCV_CUDA_VERSION} support. Please, use the same version or rebuild OpenCV with CUDA ${CUDA_VERSION_STRING}") |
||||
endif() |
||||
endif() |
Before Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 55 KiB |
Before Width: | Height: | Size: 34 KiB |
Before Width: | Height: | Size: 37 KiB |
Before Width: | Height: | Size: 5.6 KiB |
Before Width: | Height: | Size: 9.5 KiB |
Before Width: | Height: | Size: 28 KiB |
Before Width: | Height: | Size: 52 KiB |
Before Width: | Height: | Size: 56 KiB |
@ -1,107 +1 @@ |
||||
# How to run deep networks on Android device {#tutorial_dnn_android} |
||||
|
||||
@tableofcontents |
||||
|
||||
@prev_tutorial{tutorial_dnn_openvino} |
||||
@next_tutorial{tutorial_dnn_yolo} |
||||
|
||||
| | | |
||||
| -: | :- | |
||||
| Original author | Dmitry Kurtaev | |
||||
| Compatibility | OpenCV >= 3.3 | |
||||
|
||||
## Introduction |
||||
In this tutorial you'll know how to run deep learning networks on Android device |
||||
using OpenCV deep learning module. |
||||
|
||||
Tutorial was written for the following versions of corresponding software: |
||||
- Android Studio 2.3.3 |
||||
- OpenCV 3.3.0+ |
||||
|
||||
## Requirements |
||||
|
||||
- Download and install Android Studio from https://developer.android.com/studio. |
||||
|
||||
- Get the latest pre-built OpenCV for Android release from https://github.com/opencv/opencv/releases and unpack it (for example, `opencv-5.X.Y-android-sdk.zip`). |
||||
|
||||
- Download MobileNet object detection model from https://github.com/chuanqi305/MobileNet-SSD. We need a configuration file `MobileNetSSD_deploy.prototxt` and weights `MobileNetSSD_deploy.caffemodel`. |
||||
|
||||
## Create an empty Android Studio project |
||||
- Open Android Studio. Start a new project. Let's call it `opencv_mobilenet`. |
||||
 |
||||
|
||||
- Keep default target settings. |
||||
 |
||||
|
||||
- Use "Empty Activity" template. Name activity as `MainActivity` with a |
||||
corresponding layout `activity_main`. |
||||
 |
||||
|
||||
 |
||||
|
||||
- Wait until a project was created. Go to `Run->Edit Configurations`. |
||||
Choose `USB Device` as target device for runs. |
||||
 |
||||
Plug in your device and run the project. It should be installed and launched |
||||
successfully before we'll go next. |
||||
@note Read @ref tutorial_android_dev_intro in case of problems. |
||||
|
||||
 |
||||
|
||||
## Add OpenCV dependency |
||||
|
||||
- Go to `File->New->Import module` and provide a path to `unpacked_OpenCV_package/sdk/java`. The name of module detects automatically. |
||||
Disable all features that Android Studio will suggest you on the next window. |
||||
 |
||||
|
||||
 |
||||
|
||||
- Open two files: |
||||
|
||||
1. `AndroidStudioProjects/opencv_mobilenet/app/build.gradle` |
||||
|
||||
2. `AndroidStudioProjects/opencv_mobilenet/openCVLibrary330/build.gradle` |
||||
|
||||
Copy both `compileSdkVersion` and `buildToolsVersion` from the first file to |
||||
the second one. |
||||
|
||||
`compileSdkVersion 14` -> `compileSdkVersion 26` |
||||
|
||||
`buildToolsVersion "25.0.0"` -> `buildToolsVersion "26.0.1"` |
||||
|
||||
- Make the project. There is no errors should be at this point. |
||||
|
||||
- Go to `File->Project Structure`. Add OpenCV module dependency. |
||||
 |
||||
|
||||
 |
||||
|
||||
- Install once an appropriate OpenCV manager from `unpacked_OpenCV_package/apk` |
||||
to target device. |
||||
@code |
||||
adb install OpenCV_3.3.0_Manager_3.30_armeabi-v7a.apk |
||||
@endcode |
||||
|
||||
- Congratulations! We're ready now to make a sample using OpenCV. |
||||
|
||||
## Make a sample |
||||
Our sample will takes pictures from a camera, forwards it into a deep network and |
||||
receives a set of rectangles, class identifiers and confidence values in `[0, 1]` |
||||
range. |
||||
|
||||
- First of all, we need to add a necessary widget which displays processed |
||||
frames. Modify `app/src/main/res/layout/activity_main.xml`: |
||||
@include android/mobilenet-objdetect/res/layout/activity_main.xml |
||||
|
||||
- Put downloaded `MobileNetSSD_deploy.prototxt` and `MobileNetSSD_deploy.caffemodel` |
||||
into `app/build/intermediates/assets/debug` folder. |
||||
|
||||
- Modify `/app/src/main/AndroidManifest.xml` to enable full-screen mode, set up |
||||
a correct screen orientation and allow to use a camera. |
||||
@include android/mobilenet-objdetect/gradle/AndroidManifest.xml |
||||
|
||||
- Replace content of `app/src/main/java/org/opencv/samples/opencv_mobilenet/MainActivity.java`: |
||||
@include android/mobilenet-objdetect/src/org/opencv/samples/opencv_mobilenet/MainActivity.java |
||||
|
||||
- Launch an application and make a fun! |
||||
 |
||||
The page was moved to @ref tutorial_android_dnn_intro |
||||
|
@ -1,255 +0,0 @@ |
||||
OpenCV4Android SDK {#tutorial_O4A_SDK} |
||||
================== |
||||
|
||||
@prev_tutorial{tutorial_android_dev_intro} |
||||
@next_tutorial{tutorial_dev_with_OCV_on_Android} |
||||
|
||||
| | | |
||||
| -: | :- | |
||||
| Original author | Vsevolod Glumov | |
||||
| Compatibility | OpenCV >= 3.0 | |
||||
|
||||
@warning |
||||
This tutorial is deprecated. |
||||
|
||||
This tutorial was designed to help you with installation and configuration of OpenCV4Android SDK. |
||||
|
||||
This guide was written with MS Windows 7 in mind, though it should work with GNU Linux and Apple Mac |
||||
OS as well. |
||||
|
||||
This tutorial assumes you have the following software installed and configured: |
||||
|
||||
- JDK |
||||
- Android SDK and NDK |
||||
- Eclipse IDE |
||||
- ADT and CDT plugins for Eclipse |
||||
|
||||
If you need help with anything of the above, you may refer to our @ref tutorial_android_dev_intro guide. |
||||
|
||||
If you encounter any error after thoroughly following these steps, feel free to contact us via |
||||
[OpenCV4Android](https://groups.google.com/group/android-opencv/) discussion group or OpenCV [Q&A |
||||
forum](https://forum.opencv.org). We'll do our best to help you out. |
||||
|
||||
General info |
||||
------------ |
||||
|
||||
OpenCV4Android SDK package enables development of Android applications with use of OpenCV library. |
||||
|
||||
The structure of package contents looks as follows: |
||||
|
||||
OpenCV-2.4.9-android-sdk |
||||
|_ apk |
||||
| |_ OpenCV_2.4.9_binary_pack_armv7a.apk |
||||
| |_ OpenCV_2.4.9_Manager_2.18_XXX.apk |
||||
| |
||||
|_ doc |
||||
|_ samples |
||||
|_ sdk |
||||
| |_ etc |
||||
| |_ java |
||||
| |_ native |
||||
| |_ 3rdparty |
||||
| |_ jni |
||||
| |_ libs |
||||
| |_ armeabi |
||||
| |_ armeabi-v7a |
||||
| |_ x86 |
||||
| |
||||
|_ LICENSE |
||||
|_ README.android |
||||
|
||||
- `sdk` folder contains OpenCV API and libraries for Android: |
||||
- `sdk/java` folder contains an Android library Eclipse project providing OpenCV Java API that can |
||||
be imported into developer's workspace; |
||||
- `sdk/native` folder contains OpenCV C++ headers (for JNI code) and native Android libraries |
||||
(\*.so and \*.a) for ARM-v5, ARM-v7a and x86 architectures; |
||||
- `sdk/etc` folder contains Haar and LBP cascades distributed with OpenCV. |
||||
- `apk` folder contains Android packages that should be installed on the target Android device to |
||||
enable OpenCV library access via OpenCV Manager API (see details below). |
||||
|
||||
On production devices that have access to Google Play Market (and Internet) these packages will |
||||
be installed from Market on the first start of an application using OpenCV Manager API. But |
||||
devkits without Market or Internet connection require this packages to be installed manually. |
||||
Install the Manager.apk and optional binary_pack.apk if it needed. See `Manager Selection` |
||||
for details. |
||||
|
||||
@note Installation from Internet is the preferable way since OpenCV team may publish updated |
||||
versions of this packages on the Market. |
||||
|
||||
- `samples` folder contains sample applications projects |
||||
and their prebuilt packages (APK). Import them into Eclipse workspace (like described below) and |
||||
browse the code to learn possible ways of OpenCV use on Android. |
||||
|
||||
- `doc` folder contains various OpenCV documentation in PDF format. It's also available online at |
||||
<http://docs.opencv.org>. |
||||
@note The most recent docs (nightly build) are at <http://docs.opencv.org/5.x>. Generally, it's more |
||||
up-to-date, but can refer to not-yet-released functionality. |
||||
@todo I'm not sure that this is the best place to talk about OpenCV Manager |
||||
|
||||
Starting from version 2.4.3 OpenCV4Android SDK uses OpenCV Manager API for library initialization. |
||||
OpenCV Manager is an Android service based solution providing the following benefits for OpenCV |
||||
applications developers: |
||||
|
||||
- Compact apk-size, since all applications use the same binaries from Manager and do not store |
||||
native libs within themselves; |
||||
- Hardware specific optimizations are automatically enabled on all supported platforms; |
||||
- Automatic updates and bug fixes; |
||||
- Trusted OpenCV library source. All packages with OpenCV are published on Google Play; |
||||
|
||||
|
||||
Manual OpenCV4Android SDK setup |
||||
------------------------------- |
||||
|
||||
### Get the OpenCV4Android SDK |
||||
|
||||
-# Go to the [OpenCV download page on |
||||
SourceForge](http://sourceforge.net/projects/opencvlibrary/files/) and download |
||||
the latest available version. This tutorial is based on this package: [OpenCV-2.4.9-android-sdk.zip](http://sourceforge.net/projects/opencvlibrary/files/opencv-android/2.4.9/OpenCV-2.4.9-android-sdk.zip/download). |
||||
-# Create a new folder for Android with OpenCV development. For this tutorial we have unpacked |
||||
OpenCV SDK to the `C:\Work\OpenCV4Android\` directory. |
||||
|
||||
@note Better to use a path without spaces in it. Otherwise you may have problems with ndk-build. |
||||
|
||||
-# Unpack the SDK archive into the chosen directory. |
||||
|
||||
You can unpack it using any popular archiver (e.g with 7-Zip): |
||||
|
||||
 |
||||
|
||||
On Unix you can use the following command: |
||||
@code{.bash} |
||||
unzip ~/Downloads/OpenCV-2.4.9-android-sdk.zip |
||||
@endcode |
||||
|
||||
### Import OpenCV library and samples to the Eclipse |
||||
|
||||
-# Start Eclipse and choose your workspace location. |
||||
|
||||
We recommend to start working with OpenCV for Android from a new clean workspace. A new Eclipse |
||||
workspace can for example be created in the folder where you have unpacked OpenCV4Android SDK |
||||
package: |
||||
|
||||
 |
||||
|
||||
-# Import OpenCV library and samples into workspace. |
||||
|
||||
OpenCV library is packed as a ready-for-use [Android Library |
||||
Project](http://developer.android.com/guide/developing/projects/index.html#LibraryProjects). You |
||||
can simply reference it in your projects. |
||||
|
||||
Each sample included into the `OpenCV-2.4.9-android-sdk.zip` is a regular Android project that |
||||
already references OpenCV library. Follow the steps below to import OpenCV and samples into the |
||||
workspace: |
||||
|
||||
- Right click on the Package Explorer window and choose Import... option from the context |
||||
menu: |
||||
|
||||
 |
||||
|
||||
- In the main panel select General --\> Existing Projects into Workspace and press Next |
||||
button: |
||||
|
||||
 |
||||
|
||||
- In the Select root directory field locate your OpenCV package folder. Eclipse should |
||||
automatically locate OpenCV library and samples: |
||||
|
||||
 |
||||
|
||||
- Click Finish button to complete the import operation. |
||||
|
||||
@note OpenCV samples are indeed **dependent** on OpenCV library project so don't forget to import it to your workspace as well. |
||||
|
||||
After clicking Finish button Eclipse will load all selected projects into workspace, and you |
||||
have to wait some time while it is building OpenCV samples. Just give a minute to Eclipse to |
||||
complete initialization. |
||||
|
||||
 |
||||
|
||||
Once Eclipse completes build you will have the clean workspace without any build errors: |
||||
|
||||
 |
||||
|
||||
@anchor tutorial_O4A_SDK_samples |
||||
### Running OpenCV Samples |
||||
|
||||
At this point you should be able to build and run the samples. Keep in mind, that face-detection and |
||||
Tutorial 2 - Mixed Processing include some native code and require Android NDK and NDK/CDT plugin |
||||
for Eclipse to build working applications. If you haven't installed these tools, see the |
||||
corresponding section of @ref tutorial_android_dev_intro. |
||||
|
||||
**warning** |
||||
|
||||
Please consider that some samples use Android Java Camera API, which is accessible with an AVD. |
||||
|
||||
@note Recent *Android SDK tools, revision 19+* can run ARM v7a OS images but they available not for |
||||
all Android versions. |
||||
|
||||
Well, running samples from Eclipse is very simple: |
||||
|
||||
- Connect your device with adb tool from Android SDK or create an emulator with camera support. |
||||
- See [Managing Virtual Devices](http://developer.android.com/guide/developing/devices/index.html) document for help |
||||
with Android Emulator. |
||||
- See [Using Hardware Devices](http://developer.android.com/guide/developing/device.html) for |
||||
help with real devices (not emulators). |
||||
|
||||
- Select project you want to start in Package Explorer and just press Ctrl + F11 or select option |
||||
Run --\> Run from the main menu, or click Run button on the toolbar. |
||||
|
||||
@note Android Emulator can take several minutes to start. So, please, be patient. \* On the first |
||||
run Eclipse will ask you about the running mode for your application: |
||||
|
||||
 |
||||
|
||||
- Select the Android Application option and click OK button. Eclipse will install and run the |
||||
sample. |
||||
|
||||
Chances are that on the first launch you will not have the [OpenCV |
||||
Manager](https://docs.google.com/a/itseez.com/presentation/d/1EO_1kijgBg_BsjNp2ymk-aarg-0K279_1VZRcPplSuk/present#slide=id.p) |
||||
package installed. In this case you will see the following message: |
||||
|
||||
 |
||||
|
||||
To get rid of the message you will need to install OpenCV Manager and the appropriate |
||||
OpenCV binary pack. Simply tap Yes if you have *Google Play Market* installed on your |
||||
device/emulator. It will redirect you to the corresponding page on *Google Play Market*. |
||||
|
||||
If you have no access to the *Market*, which is often the case with emulators - you will need to |
||||
install the packages from OpenCV4Android SDK folder manually. See `Manager Selection` for |
||||
details. |
||||
@code{.sh} |
||||
<Android SDK path>/platform-tools/adb install <OpenCV4Android SDK path>/apk/OpenCV_2.4.9_Manager_2.18_armv7a-neon.apk |
||||
@endcode |
||||
|
||||
@note armeabi, armv7a-neon, arm7a-neon-android8, mips and x86 stand for platform targets: |
||||
- armeabi is for ARM v5 and ARM v6 architectures with Android API 8+, |
||||
- armv7a-neon is for NEON-optimized ARM v7 with Android API 9+, |
||||
- arm7a-neon-android8 is for NEON-optimized ARM v7 with Android API 8, |
||||
- mips is for MIPS architecture with Android API 9+, |
||||
- x86 is for Intel x86 CPUs with Android API 9+. |
||||
|
||||
@note |
||||
If using hardware device for testing/debugging, run the following command to learn its CPU |
||||
architecture: |
||||
@code{.sh} |
||||
adb shell getprop ro.product.cpu.abi |
||||
@endcode |
||||
If you're using an AVD emulator, go Window \> AVD Manager to see the list of available devices. |
||||
Click Edit in the context menu of the selected device. In the window, which then pop-ups, find |
||||
the CPU field. |
||||
|
||||
@note |
||||
You may also see section `Manager Selection` for details. |
||||
|
||||
When done, you will be able to run OpenCV samples on your device/emulator seamlessly. |
||||
|
||||
- Here is Sample - image-manipulations sample, running on top of stock camera-preview of the |
||||
emulator. |
||||
|
||||
 |
||||
|
||||
What's next |
||||
----------- |
||||
|
||||
Now, when you have your instance of OpenCV4Adroid SDK set up and configured, you may want to proceed |
||||
to using OpenCV in your own application. You can learn how to do that in a separate @ref tutorial_dev_with_OCV_on_Android tutorial. |