Skip to content
This repository has been archived by the owner on Oct 3, 2022. It is now read-only.

Commit

Permalink
add OpenBLAS'
Browse files Browse the repository at this point in the history
openblas example

distinguish paths

better opts

consolidate

do
  • Loading branch information
scivision committed Apr 18, 2019
1 parent a0bc8f5 commit c6124ea
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 15 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ enable_testing()
option(BUILD_SHARED_LIBS "static or shared libs")
option(atlas "Atlas Lapack implementation")
option(lapack95 "use Lapack95")
option(USEOpenBLAS "OpenBLAS")

if(NOT CMAKE_BUILD_TYPE)
# must have FORCE or it won't take effect
Expand Down Expand Up @@ -38,9 +39,13 @@ elseif(USEMKL OR DEFINED ENV{MKLROOT})
else()
find_package(LAPACK COMPONENTS MKL REQUIRED)
endif()
elseif(USEOpenBLAS)
set(USEMKL 0)
set(USEOpenBLAS 1)
find_package(LAPACK COMPONENTS OpenBLAS REQUIRED)
else()
set(USEMKL 0)
find_package(LAPACK COMPONENTS LAPACKE REQUIRED) # netlib -- apt install liblapacke-dev
find_package(LAPACK COMPONENTS Netlib LAPACKE REQUIRED) # netlib -- apt install liblapacke-dev
endif()

# here are some exmaple program using Lapack
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ Compiler | Lapack
gcc + gfortran | Netlib Lapack
gcc + gfortran | Intel MKL
gcc + gfortran | Atlas
gcc + gfortran | OpenBLAS
Clang + gfortran | Netlib Lapack
Clang + gfortran | Intel MKL
Clang + gfortran | Atlas
Clang + gfortran | OpenBLAS
Clang + Flang | Netlib Lapack
Clang + Flang | Intel MKL
Clang + Flang | Atlas
Clang + Flang | OpenBLAS


## prereq
Expand Down
6 changes: 6 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ def clang_params(impl: str) -> Tuple[Dict[str, str], List[str]]:
args = ['-Datlas=1']
elif impl == 'mkl':
args = ['-DUSEMKL=1']
elif impl == 'openblas':
args = ['-DUSEOpenBLAS=1']
else:
args = []

Expand All @@ -203,6 +205,8 @@ def gnu_params(impl: str) -> Tuple[Dict[str, str], List[str]]:
args = ['-Datlas=1']
elif impl == 'mkl':
args = ['-DUSEMKL=1']
elif impl == 'openblas':
args = ['-DUSEOpenBLAS=1']
else:
args = []

Expand Down Expand Up @@ -251,6 +255,8 @@ def pgi_params(impl: str) -> Tuple[Dict[str, str], List[str]]:
args = ['-Datlas=1']
elif impl == 'mkl':
args = ['-DUSEMKL=1']
elif impl == 'openblas':
args = ['-DUSEOpenBLAS=1']
else:
args = []

Expand Down
2 changes: 1 addition & 1 deletion c_src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ endif()
add_executable(svd_c example_gesvd.c)
target_include_directories(svd_c PRIVATE ${LAPACK_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(svd_c PRIVATE ${LAPACK_LIBRARIES} ${LDFLAGS})
target_compile_definitions(svd_c PRIVATE USEMKL=${USEMKL} USEATLAS=${atlas})
target_compile_definitions(svd_c PRIVATE USEMKL=${USEMKL} USEATLAS=${atlas} USEOpenBLAS=${USEOpenBLAS})

add_test(NAME C-SVD COMMAND svd_c)
set_tests_properties(C-SVD PROPERTIES
Expand Down
6 changes: 5 additions & 1 deletion c_src/example_gesvd.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ from http://www.netlib.org/lapack/explore-html/d5/df8/example___d_g_e_s_v__colma
#elif USEATLAS == 1
#include "cblas.h"
#include "clapack.h"
#elif USEOpenBLAS == 1
#include "cblas.h"
#else
#include "lapacke.h"
#endif
Expand Down Expand Up @@ -95,10 +97,12 @@ int main(int argc, char **argv) {
//printf( "\n" );

/* Executable statements */
printf( "dgesv (row-major, high-level) Example Program Results\n" );
/* printf( "dgesv (row-major, high-level) Example Program Results\n" ); */
/* Solve the equations A*X = B */
#if USEATLAS==1
info = clapack_dgesv( 102, n, nrhs, A, lda, ipiv, b, ldb );
#elif USEOpenBLAS == 1
cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans,n,n,n,1, A, lda, b, ldb, 0, A, lda);
#else
info = LAPACKE_dgesv( LAPACK_COL_MAJOR, n, nrhs, A, lda, ipiv, b, ldb );
#endif
Expand Down
83 changes: 71 additions & 12 deletions cmake/Modules/FindLAPACK.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ COMPONENTS default to Netlib LAPACK / LapackE, otherwise:
Netlib LapackE for C / C++
``Netlib``
Netlib Lapack for Fortran
``OpenBLAS``
OpenBLAS Lapack for Fortran
``LAPACK95``
get Lapack95 interfaces for MKL or Netlib (must also specify one of IntelPar, IntelSeq, Netlib)
Expand Down Expand Up @@ -77,38 +79,39 @@ References

function(atlas_libs)

if(NOT WIN32)
find_package(Threads) # not required--for example Flang
endif()

find_library(ATLAS_LIB
NAMES atlas)
NAMES atlas
PATH_SUFFIXES atlas)

pkg_check_modules(LAPACK_ATLAS lapack-atlas)

find_library(LAPACK_ATLAS
NAMES ptlapack lapack_atlas lapack
PATH_SUFFIXES atlas
HINTS ${LAPACK_ATLAS_LIBRARY_DIRS})

pkg_check_modules(LAPACK_BLAS blas-atlas)

find_library(BLAS_LIBRARY
NAMES ptf77blas f77blas blas
PATH_SUFFIXES atlas
HINTS ${LAPACK_BLAS_LIBRARY_DIRS})
# === C ===
find_library(BLAS_C_ATLAS
NAMES ptcblas cblas
PATH_SUFFIXES atlas
HINTS ${LAPACK_BLAS_LIBRARY_DIRS})

find_path(LAPACK_INCLUDE_DIR
NAMES cblas.h clapack.h
NAMES cblas-atlas.h cblas.h clapack.h
HINTS ${LAPACK_BLAS_INCLUDE_DIRS})

#===========
if(LAPACK_ATLAS AND BLAS_C_ATLAS AND BLAS_LIBRARY AND ATLAS_LIB)
set(LAPACK_Atlas_FOUND true PARENT_SCOPE)
set(LAPACK_LIBRARY ${LAPACK_ATLAS} ${BLAS_C_ATLAS} ${BLAS_LIBRARY} ${ATLAS_LIB})
if(NOT WIN32)
find_package(Threads) # not required--for example Flang
list(APPEND LAPACK_LIBRARY ${CMAKE_THREAD_LIBS_INIT})
endif()
endif()
Expand Down Expand Up @@ -143,7 +146,8 @@ endif()
pkg_check_modules(LAPACK lapack-netlib)
find_library(LAPACK_LIB
NAMES lapack
HINTS ${LAPACK_LIBRARY_DIRS})
HINTS ${LAPACK_LIBRARY_DIRS}
PATH_SUFFIXES lapack)
if(LAPACK_LIB)
list(APPEND LAPACK_LIBRARY ${LAPACK_LIB})
else()
Expand Down Expand Up @@ -177,7 +181,8 @@ endif()
pkg_check_modules(BLAS blas-netlib)
find_library(BLAS_LIBRARY
NAMES refblas blas
HINTS ${BLAS_LIBRARY_DIRS})
HINTS ${BLAS_LIBRARY_DIRS}
PATH_SUFFIXES blas)

if(BLAS_LIBRARY)
list(APPEND LAPACK_LIBRARY ${LAPACK_LIB} ${BLAS_LIBRARY})
Expand All @@ -195,6 +200,46 @@ set(LAPACK_INCLUDE_DIR ${LAPACK_INCLUDE_DIR} PARENT_SCOPE)

endfunction(netlib_libs)

#===============================
function(openblas_libs)

pkg_check_modules(LAPACK lapack-openblas)
find_library(LAPACK_LIBRARY
NAMES lapack
HINTS ${LAPACK_LIBRARY_DIRS}
PATH_SUFFIXES openblas)


pkg_check_modules(BLAS blas-openblas)
find_library(BLAS_LIBRARY
NAMES openblas blas
HINTS ${BLAS_LIBRARY_DIRS}
PATH_SUFFIXES openblas)

find_path(LAPACK_INCLUDE_DIR
NAMES cblas-openblas.h cblas.h f77blas.h openblas_config.h
HINTS ${LAPACK_INCLUDE_DIRS})

if(LAPACK_LIBRARY AND BLAS_LIBRARY)
list(APPEND LAPACK_LIBRARY ${BLAS_LIBRARY})
set(LAPACK_OpenBLAS_FOUND true PARENT_SCOPE)
else()
message(WARNING "Trouble finding OpenBLAS:
include: ${LAPACK_INCLUDE_DIR}
libs: ${LAPACK_LIBRARY} ${BLAS_LIBRARY}")
return()
endif()

if(NOT WIN32)
find_package(Threads) # not required--for example Flang
list(APPEND LAPACK_LIBRARY ${CMAKE_THREAD_LIBS_INIT})
endif()

set(LAPACK_LIBRARY ${LAPACK_LIBRARY} PARENT_SCOPE)
set(LAPACK_INCLUDE_DIR ${LAPACK_INCLUDE_DIR} PARENT_SCOPE)

endfunction(openblas_libs)

#===============================

function(find_mkl_libs)
Expand Down Expand Up @@ -246,7 +291,10 @@ cmake_policy(VERSION 3.3)

unset(LAPACK_LIBRARY)

if(NOT (Netlib IN_LIST LAPACK_FIND_COMPONENTS OR Atlas IN_LIST LAPACK_FIND_COMPONENTS OR MKL IN_LIST LAPACK_FIND_COMPONENTS))
if(NOT (OpenBLAS IN_LIST LAPACK_FIND_COMPONENTS
OR Netlib IN_LIST LAPACK_FIND_COMPONENTS
OR Atlas IN_LIST LAPACK_FIND_COMPONENTS
OR MKL IN_LIST LAPACK_FIND_COMPONENTS))
if(NOT DEFINED USEMKL AND DEFINED ENV{MKLROOT})
list(APPEND LAPACK_FIND_COMPONENTS MKL)
else()
Expand Down Expand Up @@ -339,6 +387,10 @@ elseif(Netlib IN_LIST LAPACK_FIND_COMPONENTS)

netlib_libs()

elseif(OpenBLAS IN_LIST LAPACK_FIND_COMPONENTS)

openblas_libs()

endif()

# verify LAPACK
Expand All @@ -348,15 +400,22 @@ set(CMAKE_REQUIRED_LIBRARIES ${LAPACK_LIBRARY})
set(_lapack_ok true)
if(CMAKE_Fortran_COMPILER AND LAPACK_LIBRARY)
include(CheckFortranFunctionExists)
check_fortran_function_exists(sgemm BLAS_OK)
check_fortran_function_exists(sgesv LAPACK_OK)
if(OpenBLAS IN_LIST LAPACK_FIND_COMPONENTS)
set(_blas_func sgemm)
set(_lapack_func sgemv)
else()
set(_blas_func sgemm)
set(_lapack_func sgesv)
endif()
check_fortran_function_exists(${_blas_func} BLAS_OK)
check_fortran_function_exists(${_lapack_func} LAPACK_OK)
if(NOT (BLAS_OK AND LAPACK_OK))
set(_lapack_ok false)
endif()
endif()

if(_lapack_ok)
if(MSVC OR (CMAKE_C_COMPILER AND USEMKL) OR LAPACKE IN_LIST LAPACK_FIND_COMPONENTS)
if(MSVC OR CMAKE_C_COMPILER OR LAPACKE IN_LIST LAPACK_FIND_COMPONENTS)
include(CheckSymbolExists)
if(USEMKL)
check_symbol_exists(LAPACKE_cheev mkl_lapacke.h _lapack_ok)
Expand Down

0 comments on commit c6124ea

Please sign in to comment.