cblas.cmake 5.3 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
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
55
          /usr/include/lapacke
56 57 58 59 60 61 62 63
          /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 已提交
64

65 66 67 68
  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})
69 70
  find_path(OPENBLAS_CONFIG_INC_DIR NAMES openblas_config.h
    PATHS ${OPENBLAS_INCLUDE_SEARCH_PATHS})
71 72
  find_library(OPENBLAS_LIB NAMES openblas
    PATHS ${OPENBLAS_LIB_SEARCH_PATHS})
L
liaogang 已提交
73

74 75
  if(OPENBLAS_LAPACKE_INC_DIR AND OPENBLAS_INC_DIR AND OPENBLAS_CONFIG_INC_DIR AND OPENBLAS_LIB)
    file(READ "${OPENBLAS_CONFIG_INC_DIR}/openblas_config.h" config_file)
76 77 78
    string(REGEX MATCH "OpenBLAS ([0-9]+\.[0-9]+\.[0-9]+)" tmp ${config_file})
    string(REGEX MATCH "([0-9]+\.[0-9]+\.[0-9]+)" ver ${tmp})
    
79
    if (${ver} VERSION_GREATER_EQUAL "0.3.5")
80 81 82
      set(CBLAS_PROVIDER OPENBLAS)
      set(CBLAS_INC_DIR ${OPENBLAS_INC_DIR} ${OPENBLAS_LAPACKE_INC_DIR})
      set(CBLAS_LIBRARIES ${OPENBLAS_LIB})
Z
zhangjinchao01 已提交
83

84 85
      add_definitions(-DPADDLE_USE_OPENBLAS)
      add_definitions(-DLAPACK_FOUND)
Z
zhangjinchao01 已提交
86

87 88 89
      message(STATUS "Found OpenBLAS (include: ${OPENBLAS_INC_DIR}, library: ${CBLAS_LIBRARIES})")
      message(STATUS "Found lapack in OpenBLAS (include: ${OPENBLAS_LAPACKE_INC_DIR})")
    endif()
90 91
  endif()
endif()
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
## 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 已提交
108

W
Wu Yi 已提交
109
  find_path(REFERENCE_CBLAS_INCLUDE_DIR NAMES cblas.h PATHS
Z
zhangjinchao01 已提交
110
        ${REFERENCE_CBLAS_INCLUDE_SEARCH_PATHS})
W
Wu Yi 已提交
111
  find_library(REFERENCE_CBLAS_LIBRARY NAMES cblas PATHS
Z
zhangjinchao01 已提交
112
        ${REFERENCE_CBLAS_LIB_SEARCH_PATHS})
W
Wilber 已提交
113
  find_library(REFERENCE_BLAS_LIBRARY NAMES blas PATHS
W
Wilber 已提交
114
        ${REFERENCE_CBLAS_LIB_SEARCH_PATHS})
Z
zhangjinchao01 已提交
115

W
Wu Yi 已提交
116
  if(REFERENCE_CBLAS_INCLUDE_DIR AND REFERENCE_CBLAS_LIBRARY)
117
    set(CBLAS_PROVIDER REFERENCE_CBLAS)
W
Wu Yi 已提交
118 119 120 121 122
    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 已提交
123
endif()
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

## 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 已提交
139
if(${CBLAS_PROVIDER} STREQUAL REFERENCE_CBLAS)
W
Wilber 已提交
140
  target_link_libraries(cblas gfortran ${CBLAS_LIBRARIES} ${REFERENCE_BLAS_LIBRARY})
W
Wilber 已提交
141 142
elseif(NOT ${CBLAS_PROVIDER} STREQUAL MKLML)
  target_link_libraries(cblas ${CBLAS_LIBRARIES})
143
endif()