cblas.cmake 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

L
lzhao4ever 已提交
15
# Find the CBlas and lapack libraries
Z
zhangjinchao01 已提交
16
#
17
# It will search MKLML, OpenBlas, reference-cblas, extern-openblas in order.
Z
zhangjinchao01 已提交
18 19
#
# If any cblas implementation found, the following variable will be set.
L
Luo Tao 已提交
20
#    CBLAS_PROVIDER  # one of MKLML, OPENBLAS, REFERENCE
Z
zhangjinchao01 已提交
21
#    CBLAS_INC_DIR   # the include directory for cblas.
22
#    CBLAS_LIBS      # a list of libraries should be linked by paddle.
Z
zhangjinchao01 已提交
23 24
#                    # Each library should be full path to object file.

25
generate_dummy_static_lib(LIB_NAME "cblas" GENERATOR "cblas.cmake")
26 27 28 29 30

if(WITH_LIBXSMM)
  target_link_libraries(cblas ${LIBXSMM_LIBS})
  add_dependencies(cblas extern_libxsmm)
endif()
Z
zhangjinchao01 已提交
31

T
tensor-tang 已提交
32
## Find MKLML First.
33 34
if(WITH_MKLML)
  include(external/mklml)       # download, install mklml package
T
tensor-tang 已提交
35
  set(CBLAS_PROVIDER MKLML)
36
  set(CBLAS_INC_DIR  ${MKLML_INC_DIR})
T
tensor-tang 已提交
37
  set(CBLAS_LIBRARIES ${MKLML_LIB})
T
tensor-tang 已提交
38

T
tensor-tang 已提交
39
  add_definitions(-DPADDLE_WITH_MKLML)
T
tensor-tang 已提交
40 41
  add_definitions(-DLAPACK_FOUND)

42 43 44
  add_dependencies(cblas mklml)
  target_link_libraries(cblas dynload_mklml)

T
tensor-tang 已提交
45
  message(STATUS "Found cblas and lapack in MKLML "
T
tensor-tang 已提交
46 47 48
    "(include: ${CBLAS_INC_DIR}, library: ${CBLAS_LIBRARIES})")
endif()

Z
zhangjinchao01 已提交
49
## Then find openblas.
50 51 52 53 54 55 56 57 58 59 60 61 62
if(NOT DEFINED CBLAS_PROVIDER)
  set(OPENBLAS_ROOT $ENV{OPENBLAS_ROOT} CACHE PATH "Folder contains Openblas")
  set(OPENBLAS_INCLUDE_SEARCH_PATHS
          ${OPENBLAS_ROOT}/include
          /usr/include
          /usr/include/openblas
          /usr/local/opt/openblas/include)
  set(OPENBLAS_LIB_SEARCH_PATHS
          ${OPENBLAS_ROOT}/lib
          /usr/lib
          /usr/lib/blas/openblas
          /usr/lib/openblas
          /usr/local/opt/openblas/lib)
L
liaogang 已提交
63

64 65 66 67 68 69
  find_path(OPENBLAS_INC_DIR NAMES cblas.h
    PATHS ${OPENBLAS_INCLUDE_SEARCH_PATHS} NO_DEFAULT_PATH)
  find_path(OPENBLAS_LAPACKE_INC_DIR NAMES lapacke.h
    PATHS ${OPENBLAS_INCLUDE_SEARCH_PATHS})
  find_library(OPENBLAS_LIB NAMES openblas
    PATHS ${OPENBLAS_LIB_SEARCH_PATHS})
L
liaogang 已提交
70

71 72 73 74
  if(OPENBLAS_LAPACKE_INC_DIR AND OPENBLAS_INC_DIR AND OPENBLAS_LIB)
    set(CBLAS_PROVIDER OPENBLAS)
    set(CBLAS_INC_DIR ${OPENBLAS_INC_DIR} ${OPENBLAS_LAPACKE_INC_DIR})
    set(CBLAS_LIBRARIES ${OPENBLAS_LIB})
Z
zhangjinchao01 已提交
75

76 77
    add_definitions(-DPADDLE_USE_OPENBLAS)
    add_definitions(-DLAPACK_FOUND)
Z
zhangjinchao01 已提交
78

79 80 81 82
    message(STATUS "Found OpenBLAS (include: ${OPENBLAS_INC_DIR}, library: ${CBLAS_LIBRARIES})")
    message(STATUS "Found lapack in OpenBLAS (include: ${OPENBLAS_LAPACKE_INC_DIR})")
  endif()
endif()
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
## Then find the reference-cblas if WITH_SYSTEM_BLAS.  www.netlib.org/blas/
if(NOT DEFINED CBLAS_PROVIDER AND WITH_SYSTEM_BLAS)
  set(REFERENCE_CBLAS_ROOT $ENV{REFERENCE_CBLAS_ROOT} CACHE PATH
    "Folder contains reference-cblas")
  set(REFERENCE_CBLAS_INCLUDE_SEARCH_PATHS
    ${REFERENCE_CBLAS_ROOT}/include
    /usr/include
    /usr/include/cblas
  )
  set(REFERENCE_CBLAS_LIB_SEARCH_PATHS
    ${REFERENCE_CBLAS_ROOT}/lib
    /usr/lib
    /usr/lib/blas/reference/
    /usr/lib/reference/
  )
Z
zhangjinchao01 已提交
99

W
Wu Yi 已提交
100
  find_path(REFERENCE_CBLAS_INCLUDE_DIR NAMES cblas.h PATHS
Z
zhangjinchao01 已提交
101
        ${REFERENCE_CBLAS_INCLUDE_SEARCH_PATHS})
W
Wu Yi 已提交
102
  find_library(REFERENCE_CBLAS_LIBRARY NAMES cblas PATHS
Z
zhangjinchao01 已提交
103
        ${REFERENCE_CBLAS_LIB_SEARCH_PATHS})
W
Wilber 已提交
104
  find_library(REFERENCE_BLAS_LIBRARY NAMES blas PATHS
W
Wilber 已提交
105
        ${REFERENCE_CBLAS_LIB_SEARCH_PATHS})
Z
zhangjinchao01 已提交
106

W
Wu Yi 已提交
107
  if(REFERENCE_CBLAS_INCLUDE_DIR AND REFERENCE_CBLAS_LIBRARY)
108
    set(CBLAS_PROVIDER REFERENCE_CBLAS)
W
Wu Yi 已提交
109 110 111 112 113
    set(CBLAS_INC_DIR ${REFERENCE_CBLAS_INCLUDE_DIR})
    set(CBLAS_LIBRARIES ${REFERENCE_CBLAS_LIBRARY})
    add_definitions(-DPADDLE_USE_REFERENCE_CBLAS)
    message(STATUS "Found reference-cblas (include: ${CBLAS_INC_DIR}, library: ${CBLAS_LIBRARIES})")
  endif()
Z
zhangjinchao01 已提交
114
endif()
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

## Then build openblas by external_project
if(NOT DEFINED CBLAS_PROVIDER)
  include(external/openblas)          # download, build, install openblas
  set(CBLAS_PROVIDER EXTERN_OPENBLAS)
  add_dependencies(cblas extern_openblas)
  add_definitions(-DPADDLE_USE_OPENBLAS)
  message(STATUS "Build OpenBLAS by External Project "
    "(include: ${CBLAS_INC_DIR}, library: ${CBLAS_LIBRARIES})")
endif()

# FIXME(gangliao): generate cblas target to track all high performance
# linear algebra libraries for cc_library(xxx SRCS xxx.c DEPS cblas)

include_directories(${CBLAS_INC_DIR})
W
Wilber 已提交
130
if(${CBLAS_PROVIDER} STREQUAL REFERENCE_CBLAS)
W
Wilber 已提交
131
  target_link_libraries(cblas gfortran ${CBLAS_LIBRARIES} ${REFERENCE_BLAS_LIBRARY})
W
Wilber 已提交
132 133
elseif(NOT ${CBLAS_PROVIDER} STREQUAL MKLML)
  target_link_libraries(cblas ${CBLAS_LIBRARIES})
134 135
endif()