Skip to content

Commit

Permalink
moved BLAS/LAPACK detection scripts from opencv_contrib/dnn to the ma…
Browse files Browse the repository at this point in the history
…in repository (#7918)

* moved BLAS/LAPACK detection scripts from opencv_contrib/dnn to the main repository.

* trying to fix the bug with undefined symbols sgesdd_ and dgesdd_

* removed extra whitespaces; disabled LAPACK on IOS
  • Loading branch information
vpisarev committed Dec 22, 2016
1 parent ce06fbd commit ad74fdd
Show file tree
Hide file tree
Showing 8 changed files with 444 additions and 44 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Expand Up @@ -227,7 +227,7 @@ OCV_OPTION(WITH_VA "Include VA support" OFF
OCV_OPTION(WITH_VA_INTEL "Include Intel VA-API/OpenCL support" OFF IF (UNIX AND NOT ANDROID) )
OCV_OPTION(WITH_GDAL "Include GDAL Support" OFF IF (NOT ANDROID AND NOT IOS AND NOT WINRT) )
OCV_OPTION(WITH_GPHOTO2 "Include gPhoto2 library support" ON IF (UNIX AND NOT ANDROID) )
OCV_OPTION(WITH_LAPACK "Include Lapack library support" ON IF (NOT ANDROID) )
OCV_OPTION(WITH_LAPACK "Include Lapack library support" ON IF (NOT ANDROID AND NOT IOS) )

# OpenCV build components
# ===================================================
Expand Down Expand Up @@ -556,6 +556,7 @@ include(cmake/OpenCVFindLibsGrfmt.cmake)
include(cmake/OpenCVFindLibsGUI.cmake)
include(cmake/OpenCVFindLibsVideo.cmake)
include(cmake/OpenCVFindLibsPerf.cmake)
include(cmake/OpenCVFindLAPACK.cmake)

# ----------------------------------------------------------------------------
# Detect other 3rd-party libraries/tools
Expand Down Expand Up @@ -1222,7 +1223,7 @@ status(" Use Intel VA-API/OpenCL:" HAVE_VA_INTEL THEN "YES (MSDK: ${VA
endif(DEFINED WITH_VA_INTEL)

if(DEFINED WITH_LAPACK)
status(" Use Lapack:" HAVE_LAPACK THEN "YES" ELSE NO)
status(" Use Lapack:" HAVE_LAPACK THEN "YES (${LAPACK_LIBRARIES})" ELSE NO)
endif(DEFINED WITH_LAPACK)

status(" Use Eigen:" HAVE_EIGEN THEN "YES (ver ${EIGEN_WORLD_VERSION}.${EIGEN_MAJOR_VERSION}.${EIGEN_MINOR_VERSION})" ELSE NO)
Expand Down
97 changes: 97 additions & 0 deletions cmake/OpenCVFindAtlas.cmake
@@ -0,0 +1,97 @@
#COPYRIGHT
#
#All contributions by the University of California:
#Copyright (c) 2014, 2015, The Regents of the University of California (Regents)
#All rights reserved.
#
#All other contributions:
#Copyright (c) 2014, 2015, the respective contributors
#All rights reserved.
#
#Caffe uses a shared copyright model: each contributor holds copyright over
#their contributions to Caffe. The project versioning records all such
#contribution and copyright details. If a contributor wants to further mark
#their specific copyright on a particular contribution, they should indicate
#their copyright solely in the commit message of the change when it is
#committed.
#
#LICENSE
#
#Redistribution and use in source and binary forms, with or without
#modification, are permitted provided that the following conditions are met:
#
#1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#2. 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.
#
#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 THE COPYRIGHT OWNER 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.
#
#CONTRIBUTION AGREEMENT
#
#By contributing to the BVLC/caffe repository through pull-request, comment,
#or otherwise, the contributor releases their content to the
#license and copyright terms herein.


# Find the Atlas (and Lapack) libraries
#
# The following variables are optionally searched for defaults
# Atlas_ROOT_DIR: Base directory where all Atlas components are found
#
# The following are set after configuration is done:
# Atlas_FOUND
# Atlas_INCLUDE_DIRS
# Atlas_LIBRARIES
# Atlas_LIBRARYRARY_DIRS

set(Atlas_INCLUDE_SEARCH_PATHS
/usr/include/atlas
/usr/include/atlas-base
$ENV{Atlas_ROOT_DIR}
$ENV{Atlas_ROOT_DIR}/include
)

set(Atlas_LIB_SEARCH_PATHS
/usr/lib/atlas
/usr/lib/atlas-base
$ENV{Atlas_ROOT_DIR}
$ENV{Atlas_ROOT_DIR}/lib
)

find_path(Atlas_CBLAS_INCLUDE_DIR NAMES cblas.h PATHS ${Atlas_INCLUDE_SEARCH_PATHS})
find_path(Atlas_CLAPACK_INCLUDE_DIR NAMES lapacke.h PATHS ${Atlas_INCLUDE_SEARCH_PATHS})

find_library(Atlas_CBLAS_LIBRARY NAMES ptcblas_r ptcblas cblas_r cblas PATHS ${Atlas_LIB_SEARCH_PATHS})
find_library(Atlas_BLAS_LIBRARY NAMES atlas_r atlas PATHS ${Atlas_LIB_SEARCH_PATHS})
find_library(Atlas_LAPACK_LIBRARY NAMES lapack alapack_r alapack lapack_atlas PATHS ${Atlas_LIB_SEARCH_PATHS})

set(LOOKED_FOR
Atlas_CBLAS_INCLUDE_DIR
Atlas_CLAPACK_INCLUDE_DIR

Atlas_CBLAS_LIBRARY
Atlas_BLAS_LIBRARY
Atlas_LAPACK_LIBRARY
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Atlas DEFAULT_MSG ${LOOKED_FOR})

if(ATLAS_FOUND)
set(Atlas_INCLUDE_DIR ${Atlas_CBLAS_INCLUDE_DIR} ${Atlas_CLAPACK_INCLUDE_DIR})
set(Atlas_LIBRARIES ${Atlas_LAPACK_LIBRARY} ${Atlas_CBLAS_LIBRARY} ${Atlas_BLAS_LIBRARY})
mark_as_advanced(${LOOKED_FOR})

message(STATUS "Found Atlas (include: ${Atlas_CBLAS_INCLUDE_DIR}, library: ${Atlas_BLAS_LIBRARY})")
endif(ATLAS_FOUND)
78 changes: 78 additions & 0 deletions cmake/OpenCVFindLAPACK.cmake
@@ -0,0 +1,78 @@
macro(_find_file_in_dirs VAR NAME DIRS)
find_path(${VAR} ${NAME} ${DIRS} NO_DEFAULT_PATH)
set(${VAR} ${${VAR}}/${NAME})
unset(${VAR} CACHE)
endmacro()

if(WITH_LAPACK)
ocv_update(LAPACK_IMPL "Unknown")
if(NOT LAPACK_LIBRARIES)
include(cmake/OpenCVFindMKL.cmake)
if(HAVE_MKL)
set(LAPACK_INCLUDE_DIR ${MKL_INCLUDE_DIRS})
set(LAPACK_LIBRARIES ${MKL_LIBRARIES} )
set(LAPACK_CBLAS_H "mkl_cblas.h" )
set(LAPACK_LAPACKE_H "mkl_lapacke.h" )
set(LAPACK_IMPL "MKL")
endif()
endif()
#if(NOT LAPACK_LIBRARIES)
# include(cmake/OpenCVFindOpenBLAS.cmake)
# if(OpenBLAS_FOUND)
# set(LAPACK_INCLUDE_DIR ${OpenBLAS_INCLUDE_DIR} )
# set(LAPACK_LIBRARIES ${OpenBLAS_LIB} )
# set(LAPACK_CBLAS_H "cblas.h" )
# set(LAPACK_LAPACKE_H "lapacke.h" )
# set(LAPACK_IMPL "OpenBLAS")
# endif()
#endif()
if(NOT LAPACK_LIBRARIES AND UNIX)
include(cmake/OpenCVFindAtlas.cmake)
if(ATLAS_FOUND)
set(LAPACK_INCLUDE_DIR ${Atlas_INCLUDE_DIR})
set(LAPACK_LIBRARIES ${Atlas_LIBRARIES} )
set(LAPACK_CBLAS_H "cblas.h" )
set(LAPACK_LAPACKE_H "lapacke.h" )
set(LAPACK_IMPL "Atlas")
endif()
endif()

if(NOT LAPACK_LIBRARIES AND APPLE)
set(LAPACK_INCLUDE_DIR "Accelerate")
set(LAPACK_LIBRARIES "-framework Accelerate")
set(LAPACK_CBLAS_H "cblas.h" )
set(LAPACK_LAPACKE_H "lapacke.h" )
set(LAPACK_IMPL "Apple")
endif()

set(LAPACK_INCLUDE_DIR ${LAPACK_INCLUDE_DIR} CACHE PATH "Path to BLAS include dir" FORCE)
set(LAPACK_CBLAS_H ${LAPACK_CBLAS_H} CACHE STRING "Alternative name of cblas.h" FORCE)
set(LAPACK_LAPACKE_H ${LAPACK_LAPACKE_H} CACHE STRING "Alternative name of lapacke.h" FORCE)
set(LAPACK_LIBRARIES ${LAPACK_LIBRARIES} CACHE STRING "Names of BLAS & LAPACK binaries (.so, .dll, .a, .lib)" FORCE)
set(LAPACK_IMPL ${LAPACK_IMPL} CACHE STRING "Lapack implementation id" FORCE)

if(LAPACK_LIBRARIES) #adding proxy cblas.h header
message(STATUS "LAPACK_IMPL: ${LAPACK_IMPL}, LAPACK_LIBRARIES: ${LAPACK_LIBRARIES}")
if("${LAPACK_IMPL}" STREQUAL "Apple")
set(CBLAS_H_PATH "Accelerate/Accelerate.h")
set(LAPACKE_H_PATH "Accelerate/Accelerate.h")
else()
_find_file_in_dirs(CBLAS_H_PATH "${LAPACK_CBLAS_H}" "${LAPACK_INCLUDE_DIR}")
_find_file_in_dirs(LAPACKE_H_PATH "${LAPACK_LAPACKE_H}" "${LAPACK_INCLUDE_DIR}")
endif()
if(NOT CBLAS_H_PATH OR NOT LAPACKE_H_PATH)
message(WARNING "CBLAS/LAPACK headers are not found in '${LAPACK_INCLUDE_DIR}'")
endif()
ocv_include_directories(${LAPACK_INCLUDE_DIR})
list(APPEND OPENCV_LINKER_LIBS ${LAPACK_LIBRARIES})
set(HAVE_LAPACK 1)

set(CBLAS_H_PROXY_PATH ${CMAKE_BINARY_DIR}/opencv_lapack.h)
set(_include_str "\#include \"${CBLAS_H_PATH}\"")
if("${CBLAS_H_PATH}" STREQUAL "${LAPACKE_H_PATH}")
else()
set(_include_str "${_include_str}\n\#include \"${LAPACKE_H_PATH}\"")
endif()
file(WRITE ${CBLAS_H_PROXY_PATH} ${_include_str})
endif()
endif()
50 changes: 23 additions & 27 deletions cmake/OpenCVFindLibsPerf.cmake
Expand Up @@ -3,33 +3,29 @@
# ----------------------------------------------------------------------------

# --- Lapack ---
if(WITH_LAPACK)
if(WIN32)
set(BLA_STATIC 1)
endif()
find_package(LAPACK)
if(LAPACK_FOUND)
find_path(LAPACKE_INCLUDE_DIR "lapacke.h")
find_path(MKL_LAPACKE_INCLUDE_DIR "mkl_lapack.h")
if(LAPACKE_INCLUDE_DIR OR MKL_LAPACKE_INCLUDE_DIR)
find_path(CBLAS_INCLUDE_DIR "cblas.h")
find_path(MKL_CBLAS_INCLUDE_DIR "mkl_cblas.h")

if(CBLAS_INCLUDE_DIR OR MKL_CBLAS_INCLUDE_DIR)
set(HAVE_LAPACK 1)

if(CBLAS_INCLUDE_DIR)
ocv_include_directories(${LAPACKE_INCLUDE_DIR} ${CBLAS_INCLUDE_DIR})
set(HAVE_LAPACK_GENERIC 1)
elseif(MKL_CBLAS_INCLUDE_DIR)
ocv_include_directories(${MKL_LAPACKE_INCLUDE_DIR} ${MKL_CBLAS_INCLUDE_DIR})
set(HAVE_LAPACK_MKL 1)
endif()
list(APPEND OPENCV_LINKER_LIBS ${LAPACK_LIBRARIES})
endif()
endif()
endif()
endif()
# if(WITH_LAPACK)
# if(WIN32)
# set(BLA_STATIC 1)
# endif()
# find_package(LAPACK)
# if(LAPACK_FOUND)
# find_path(LAPACKE_INCLUDE_DIR "lapacke.h")
# find_path(MKL_LAPACKE_INCLUDE_DIR "mkl_lapack.h")
# if(LAPACKE_INCLUDE_DIR)
# ocv_include_directories(${LAPACKE_INCLUDE_DIR})
# set(HAVE_LAPACK 1)
# set(HAVE_LAPACK_GENERIC 1)
# elseif(MKL_LAPACKE_INCLUDE_DIR)
# ocv_include_directories(${MKL_LAPACKE_INCLUDE_DIR})
# set(HAVE_LAPACK 1)
# set(HAVE_LAPACK_MKL 1)
# elseif(APPLE)
# set(HAVE_LAPACK 1)
# set(HAVE_LAPACK_APPLE 1)
# endif()
# list(APPEND OPENCV_LINKER_LIBS ${LAPACK_LIBRARIES})
# endif()
# endif()

# --- TBB ---
if(WITH_TBB)
Expand Down
136 changes: 136 additions & 0 deletions cmake/OpenCVFindMKL.cmake
@@ -0,0 +1,136 @@
#
# The script to detect Intel(R) Math Kernel Library (MKL)
# installation/package
#
# Parameters:
# MKL_WITH_TBB
#
# On return this will define:
#
# HAVE_MKL - True if Intel IPP found
# MKL_ROOT_DIR - root of IPP installation
# MKL_INCLUDE_DIRS - IPP include folder
# MKL_LIBRARIES - IPP libraries that are used by OpenCV
#

macro (mkl_find_lib VAR NAME DIRS)
find_path(${VAR} ${NAME} ${DIRS} NO_DEFAULT_PATH)
set(${VAR} ${${VAR}}/${NAME})
unset(${VAR} CACHE)
endmacro()

macro(mkl_fail)
set(HAVE_MKL OFF CACHE BOOL "True if MKL found")
set(MKL_ROOT_DIR ${MKL_ROOT_DIR} CACHE PATH "Path to MKL directory")
unset(MKL_INCLUDE_DIRS CACHE)
unset(MKL_LIBRARIES CACHE)
return()
endmacro()

macro(get_mkl_version VERSION_FILE)
# read MKL version info from file
file(STRINGS ${VERSION_FILE} STR1 REGEX "__INTEL_MKL__")
file(STRINGS ${VERSION_FILE} STR2 REGEX "__INTEL_MKL_MINOR__")
file(STRINGS ${VERSION_FILE} STR3 REGEX "__INTEL_MKL_UPDATE__")
#file(STRINGS ${VERSION_FILE} STR4 REGEX "INTEL_MKL_VERSION")

# extract info and assign to variables
string(REGEX MATCHALL "[0-9]+" MKL_VERSION_MAJOR ${STR1})
string(REGEX MATCHALL "[0-9]+" MKL_VERSION_MINOR ${STR2})
string(REGEX MATCHALL "[0-9]+" MKL_VERSION_UPDATE ${STR3})
set(MKL_VERSION_STR "${MKL_VERSION_MAJOR}.${MKL_VERSION_MINOR}.${MKL_VERSION_UPDATE}" CACHE STRING "MKL version" FORCE)
endmacro()


if(NOT DEFINED MKL_USE_MULTITHREAD)
OCV_OPTION(MKL_WITH_TBB "Use MKL with TBB multithreading" OFF)#ON IF WITH_TBB)
OCV_OPTION(MKL_WITH_OPENMP "Use MKL with OpenMP multithreading" OFF)#ON IF WITH_OPENMP)
endif()

#check current MKL_ROOT_DIR
if(NOT MKL_ROOT_DIR OR NOT EXISTS ${MKL_ROOT_DIR}/include/mkl.h)
set(mkl_root_paths ${MKL_ROOT_DIR})
if(DEFINED $ENV{MKLROOT})
list(APPEND mkl_root_paths $ENV{MKLROOT})
endif()
if(WIN32)
set(ProgramFilesx86 "ProgramFiles(x86)")
list(APPEND mkl_root_paths $ENV{${ProgramFilesx86}}/IntelSWTools/compilers_and_libraries/windows/mkl)
endif()
if(UNIX)
list(APPEND mkl_root_paths "/opt/intel/mkl")
endif()

find_path(MKL_ROOT_DIR include/mkl.h PATHS ${mkl_root_paths})
endif()

if(NOT MKL_ROOT_DIR)
mkl_fail()
endif()

set(MKL_INCLUDE_DIRS ${MKL_ROOT_DIR}/include)
get_mkl_version(${MKL_INCLUDE_DIRS}/mkl_version.h)

#determine arch
if(CMAKE_CXX_SIZEOF_DATA_PTR EQUAL 8)
set(MKL_X64 1)
set(MKL_ARCH "intel64")

include(CheckTypeSize)
CHECK_TYPE_SIZE(int _sizeof_int)
if (_sizeof_int EQUAL 4)
set(MKL_LP64 "lp64")
else()
set(MKL_LP64 "ilp64")
endif()
else()
set(MKL_ARCH "ia32")
endif()

if(${MKL_VERSION_STR} VERSION_GREATER "11.3.0" OR ${MKL_VERSION_STR} VERSION_EQUAL "11.3.0")
set(mkl_lib_find_paths
${MKL_ROOT_DIR}/lib
${MKL_ROOT_DIR}/lib/${MKL_ARCH} ${MKL_ROOT_DIR}/../tbb/lib/${MKL_ARCH})

set(mkl_lib_list
mkl_core
mkl_intel_${MKL_LP64})

if(MKL_WITH_TBB)
list(APPEND mkl_lib_list mkl_tbb_thread tbb)
elseif(MKL_WITH_OPENMP)
if(MSVC)
list(APPEND mkl_lib_list mkl_intel_thread libiomp5md)
else()
list(APPEND mkl_lib_list libmkl_gnu_thread)
endif()
else()
list(APPEND mkl_lib_list mkl_sequential)
endif()
else()
message(STATUS "MKL version ${MKL_VERSION_STR} is not supported")
mkl_fail()
endif()


set(MKL_LIBRARIES "")
foreach(lib ${mkl_lib_list})
find_library(${lib} ${lib} ${mkl_lib_find_paths})
mark_as_advanced(${lib})
if(NOT ${lib})
mkl_fail()
endif()
list(APPEND MKL_LIBRARIES ${${lib}})
endforeach()

message(STATUS "Found MKL ${MKL_VERSION_STR} at: ${MKL_ROOT_DIR}")
set(HAVE_MKL ON CACHE BOOL "True if MKL found")
set(MKL_ROOT_DIR ${MKL_ROOT_DIR} CACHE PATH "Path to MKL directory")
set(MKL_INCLUDE_DIRS ${MKL_INCLUDE_DIRS} CACHE PATH "Path to MKL include directory")
if(NOT UNIX)
set(MKL_LIBRARIES ${MKL_LIBRARIES} CACHE FILEPATH "MKL libarries")
else()
#it's ugly but helps to avoid cyclic lib problem
set(MKL_LIBRARIES ${MKL_LIBRARIES} ${MKL_LIBRARIES} ${MKL_LIBRARIES} "-lpthread" "-lm" "-ldl")
set(MKL_LIBRARIES ${MKL_LIBRARIES} CACHE STRING "MKL libarries")
endif()

0 comments on commit ad74fdd

Please sign in to comment.