Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
[MXNET-115] Fixed USE_LAPACK forced on all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
lebeg committed Jun 5, 2018
1 parent 247d579 commit d1caba2
Show file tree
Hide file tree
Showing 4 changed files with 314 additions and 124 deletions.
15 changes: 3 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mxnet_option(USE_OPENMP "Build with Openmp support" ON)
mxnet_option(USE_CUDNN "Build with cudnn support" ON) # one could set CUDNN_ROOT for search path
mxnet_option(USE_SSE "Build with x86 SSE instruction support" ON)
mxnet_option(USE_F16C "Build with x86 F16C instruction support" ON) # autodetects support if ON
mxnet_option(USE_LAPACK "Build with lapack support" ON IF NOT MSVC)
mxnet_option(USE_LAPACK "Build with lapack support" ON)
mxnet_option(USE_MKL_IF_AVAILABLE "Use MKL if found" ON)
mxnet_option(USE_MKLML_MKL "Use MKLDNN variant of MKL (if MKL found)" ON IF USE_MKL_IF_AVAILABLE AND (NOT APPLE))
mxnet_option(USE_MKLDNN "Use MKLDNN variant of MKL (if MKL found)" ON IF USE_MKL_IF_AVAILABLE AND (NOT APPLE))
Expand Down Expand Up @@ -236,7 +236,6 @@ else()
endif()

if(USE_CUDA AND FIRST_CUDA)
include(cmake/ChooseBlas.cmake)
include(3rdparty/mshadow/cmake/Utils.cmake)
include(cmake/FirstClassLangCuda.cmake)
include_directories(${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
Expand Down Expand Up @@ -363,16 +362,8 @@ elseif(UNIX)
list(APPEND mxnet_LINKER_LIBS pthread)
endif()

# ---[ LAPack
if(USE_LAPACK AND NOT MSVC)
add_definitions(-DMXNET_USE_LAPACK=1)
list(APPEND mxnet_LINKER_LIBS lapack)
else(USE_LAPACK)
# Workaround for Windows until using new Jenkinsfile.
if(BLAS STREQUAL "Open" OR BLAS STREQUAL "open" OR USE_BLAS STREQUAL "Open" OR USE_BLAS STREQUAL "open")
add_definitions(-DMXNET_USE_LAPACK=1)
endif()
endif()
# ---[ BLAS
include(cmake/ChooseBlas.cmake)

# ---[ jemalloc
if(USE_JEMALLOC)
Expand Down
102 changes: 91 additions & 11 deletions cmake/ChooseBlas.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,120 @@ set(BLAS "Open" CACHE STRING "Selected BLAS library")
set_property(CACHE BLAS PROPERTY STRINGS "Atlas;Open;MKL")

if(USE_MKL_IF_AVAILABLE)
message(STATUS "Trying to find MKL library due to USE_MKL_IF_AVAILABLE=True...")

if(NOT MKL_FOUND)
find_package(MKL)
endif()

if(MKL_FOUND)
if(USE_MKLDNN)
message(STATUS "MKL library found, checking if USE_MKLDNN...")

if(USE_MKLDNN)
message(STATUS "USE_MKLDNN=True, setting to use OpenBLAS")
set(BLAS "open")
else()
message(STATUS "USE_MKLDNN=False, setting to use MKL")
set(BLAS "MKL")
endif()
else()
message(STATUS "MKL library not found, BLAS=${BLAS}")
endif()
endif()

if(BLAS STREQUAL "Atlas" OR BLAS STREQUAL "atlas")
find_package(Atlas REQUIRED)
include_directories(SYSTEM ${Atlas_INCLUDE_DIR})
# cmake regexp does not support case insensitive match (?i) or //i
if(BLAS MATCHES "[Aa][Tt][Ll][Aa][Ss]")
message(STATUS "Using Atlas for BLAS")

set(Atlas_NEED_LAPACK ${USE_LAPACK})

include(${CMAKE_CURRENT_LIST_DIR}/Modules/FindAtlas.cmake)

include_directories(SYSTEM ${Atlas_INCLUDE_DIRS})
list(APPEND mshadow_LINKER_LIBS ${Atlas_LIBRARIES})

add_definitions(-DMSHADOW_USE_CBLAS=1)
add_definitions(-DMSHADOW_USE_MKL=0)
elseif(BLAS STREQUAL "Open" OR BLAS STREQUAL "open")
find_package(OpenBLAS REQUIRED)
include_directories(SYSTEM ${OpenBLAS_INCLUDE_DIR})
list(APPEND mshadow_LINKER_LIBS ${OpenBLAS_LIB})

if(USE_LAPACK AND Atlas_LAPACK_FOUND)
add_definitions(-DMXNET_USE_LAPACK=1)
endif()

return()
endif()

if(BLAS MATCHES "[Oo][Pp][Ee][Nn]")
message(STATUS "Using OpenBLAS for BLAS")

set(OpenBLAS_NEED_LAPACK ${USE_LAPACK})

include(${CMAKE_CURRENT_LIST_DIR}/Modules/FindOpenBLAS.cmake)

include_directories(SYSTEM ${OpenBLAS_INCLUDE_DIRS})
list(APPEND mshadow_LINKER_LIBS ${OpenBLAS_LIBRARIES})

add_definitions(-DMSHADOW_USE_CBLAS=1)
add_definitions(-DMSHADOW_USE_MKL=0)
elseif(BLAS STREQUAL "MKL" OR BLAS STREQUAL "mkl")

if(USE_LAPACK AND OpenBLAS_LAPACK_FOUND)
add_definitions(-DMXNET_USE_LAPACK=1)
endif()

return()
endif()

if(BLAS MATCHES "[Mm][Kk][Ll]")
message(STATUS "Using MKL for BLAS")

# todo(lebeg): include(${CMAKE_CURRENT_LIST_DIR}/Modules/FindMKL.cmake)

find_package(MKL REQUIRED)

include_directories(SYSTEM ${MKL_INCLUDE_DIR})
list(APPEND mshadow_LINKER_LIBS ${MKL_LIBRARIES})

add_definitions(-DMSHADOW_USE_CBLAS=0)
add_definitions(-DMSHADOW_USE_MKL=1)
elseif(BLAS STREQUAL "apple")

if(USE_LAPACK)
include(CheckFunctionExists)
check_function_exists("cheev_" LAPACK_FOUND)

if(LAPACK_FOUND)
add_definitions(-DMXNET_USE_LAPACK=1)
endif()
endif()

return()
endif()

if(BLAS MATCHES "[Aa][Pp][Pp][Ll][Ee]")
if(NOT APPLE)
message(FATAL_ERROR "Apple BLAS framework is available only on MAC")
return()
endif()

message(STATUS "Using Apple Accelerate for BLAS")

# Accelerate framework documentation
# https://developer.apple.com/documentation/accelerate?changes=_2
find_package(Accelerate REQUIRED)
include_directories(SYSTEM ${Accelerate_INCLUDE_DIR})
list(APPEND mshadow_LINKER_LIBS ${Accelerate_LIBRARIES})
add_definitions(-DMSHADOW_USE_MKL=0)

add_definitions(-DMSHADOW_USE_CBLAS=1)
add_definitions(-DMSHADOW_USE_MKL=0)

if(USE_LAPACK)
# Apples vecLib should contain lapack functionalities included in the Accelerate framework, but we will double check
# https://developer.apple.com/documentation/accelerate/veclib?changes=_2
include(CheckFunctionExists)
check_function_exists("cheev_" LAPACK_FOUND)

if(LAPACK_FOUND)
add_definitions(-DMXNET_USE_LAPACK=1)
endif()
endif()

return()
endif()
123 changes: 91 additions & 32 deletions cmake/Modules/FindAtlas.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,113 @@
# 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
# Atlas_NEED_LAPACK: Whether need lapack libraries
#
# Atlas_ROOT_DIR: Base directory where all Atlas components are found
# Atlas_NEED_LAPACK: Whether need lapack libraries
#
# The following are set after configuration is done:
# Atlas_FOUND
# Atlas_INCLUDE_DIRS
# Atlas_LIBRARIES
# Atlas_LIBRARYRARY_DIRS
#
# Atlas_FOUND
# Atlas_LAPACK_FOUND
# Atlas_INCLUDE_DIRS
# Atlas_LIBRARIES

file(TO_CMAKE_PATH "$ENV{CROSS_ROOT}" CROSS_ROOT)

if(CMAKE_CROSSCOMPILING)
set(Atlas_INCLUDE_SEARCH_PATHS
${CROSS_ROOT}
${CROSS_ROOT}/include
)
endif()

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

$ENV{Atlas_ROOT_DIR}
$ENV{Atlas_ROOT_DIR}/include
$ENV{Atlas_ROOT_DIR}/include/atlas

/usr/include/atlas
/usr/include/atlas-base
)

if(CMAKE_CROSSCOMPILING)
set(Atlas_LIB_SEARCH_PATHS
${CROSS_ROOT}
${CROSS_ROOT}/lib
${CROSS_ROOT}/lib64
)
endif()

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

$ENV{Atlas_ROOT_DIR}
$ENV{Atlas_ROOT_DIR}/lib

/usr/lib/atlas
/usr/lib/atlas-base
)

find_path(Atlas_CBLAS_INCLUDE_DIR NAMES cblas.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_path(Atlas_CBLAS_INCLUDE_DIR
NAMES cblas.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})

set(LOOKED_FOR
Atlas_CBLAS_INCLUDE_DIR
Atlas_CBLAS_INCLUDE_DIR

Atlas_CBLAS_LIBRARY
Atlas_BLAS_LIBRARY
)
Atlas_CBLAS_LIBRARY
Atlas_BLAS_LIBRARY
)

if(Atlas_NEED_LAPACK)
find_path(Atlas_CLAPACK_INCLUDE_DIR NAMES clapack.h PATHS ${Atlas_INCLUDE_SEARCH_PATHS})
find_library(Atlas_LAPACK_LIBRARY NAMES alapack_r alapack lapack_atlas PATHS ${Atlas_LIB_SEARCH_PATHS})
set(LOOKED_FOR ${LOOKED_FOR} Atlas_CLAPACK_INCLUDE_DIR Atlas_LAPACK_LIBRARY)
endif(Atlas_NEED_LAPACK)
message(STATUS "Looking for lapack support...")

find_path(Atlas_CLAPACK_INCLUDE_DIR
NAMES clapack.h
PATHS ${Atlas_INCLUDE_SEARCH_PATHS})
find_library(Atlas_LAPACK_LIBRARY
NAMES lapack_r lapack lapack_atlas
PATHS ${Atlas_LIB_SEARCH_PATHS})

set(CMAKE_REQUIRED_LIBRARIES ${Atlas_LAPACK_LIBRARY})
include(CheckFunctionExists)
check_function_exists("cheev_" LAPACK_FOUND)

if(LAPACK_FOUND)
set(Atlas_LAPACK_FOUND True)

set(LOOKED_FOR
${LOOKED_FOR}
Atlas_CLAPACK_INCLUDE_DIR
Atlas_LAPACK_LIBRARY)

message(STATUS "Lapack found")
else()
set(Atlas_LAPACK_FOUND False)
message(WARNING "Lapack with Atlas could not be found, lapack functionality will not be available")
endif()

endif()

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})
if(Atlas_FOUND)
set(Atlas_INCLUDE_DIRS ${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)
message(STATUS "Found Atlas (include: ${Atlas_INCLUDE_DIRS}, libraries: ${Atlas_LIBRARIES})")
else()
message(FATAL "Atlas not found, looked for ${LOOKED_FOR}")
endif()
Loading

0 comments on commit d1caba2

Please sign in to comment.