cblas.cmake 6.2 KB
Newer Older
L
lzhao4ever 已提交
1
# Find the CBlas and lapack libraries
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# It will search MKL, atlas, OpenBlas, reference-cblas in order.
#
# If any cblas implementation found, the following variable will be set.
#    CBLAS_PROVIDER  # one of MKL, ATLAS, OPENBLAS, REFERENCE
#    CBLAS_INC_DIR   # the include directory for cblas.
#    CBLAS_LIBS      # a list of libraries should be linked by paddle. 
#                    # Each library should be full path to object file.
#
# User should set one of MKL_ROOT, ATLAS_ROOT, OPENBLAS_ROOT, REFERENCE_CBLAS_ROOT
# during cmake. If none of them set, it will try to find cblas implementation in
# system paths.
#

L
liaogang 已提交
16
set(CBLAS_FOUND OFF)
L
Liu Yiqun 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
set(LAPACK_FOUND OFF)

macro(find_lapack)
  ## Find clapack
  set(CLAPACK_ROOT $ENV{CLAPACK_ROOT} CACHE PATH "Folder contain clapack")
  find_path(CLAPACK_INC_DIR NAMES clapack.h PATHS
    ${CLAPACK_ROOT}/include)
  find_path(CLAPACK_F2C_INC_DIR NAMES f2c.h PATHS
    ${CLAPACK_ROOT}/include)
  find_library(CLAPACK_F2C_LIB NAMES f2c PATHS
    ${CLAPACK_ROOT}/lib)
  find_library(CLAPACK_CBLASWR_LIB NAMES cblaswr PATHS
    ${CLAPACK_ROOT}/lib)
  find_library(CLAPACK_CLAPACK_LIB NAMES clapack PATHS
    ${CLAPACK_ROOT}/lib)
  if(CLAPACK_INC_DIR AND CLAPACK_F2C_INC_DIR AND CLAPACK_F2C_LIB AND
     CLAPACK_CBLASWR_LIB AND CLAPACK_CLAPACK_LIB AND NOT LAPACK_FOUND)
    set(LAPACK_PROVIDER CLAPACK)
    set(LAPACK_INC_DIR ${CLAPACK_INC_DIR})
    set(LAPACK_LIBRARIES ${CLAPACK_F2C_LIB} ${CLAPACK_CBLASWR_LIB} ${CLAPACK_CLAPACK_LIB})
    add_definitions(-DPADDLE_USE_CLAPACK)
    message(STATUS "Found CLAPACK (include: ${LAPACK_INC_DIR}, library: ${LAPACK_LIBRARIES})")
    set(LAPACK_FOUND ON)
  endif()
endmacro()
Z
zhangjinchao01 已提交
42 43

## Find MKL First.
L
liaogang 已提交
44 45
set(INTEL_ROOT "/opt/intel" CACHE PATH "Folder contains intel libs")
set(MKL_ROOT ${INTEL_ROOT}/mkl CACHE PATH "Folder contains MKL")
Z
zhangjinchao01 已提交
46

L
Liu Yiqun 已提交
47
find_path(MKL_INC_DIR mkl.h PATHS
B
backyes 已提交
48
  ${MKL_ROOT}/include)
L
Liu Yiqun 已提交
49
find_path(MKL_LAPACK_INC_DIR mkl_lapacke.h PATHS
L
lzhao4ever 已提交
50
  ${MKL_ROOT}/include)
B
backyes 已提交
51 52 53 54 55 56 57 58 59
find_library(MKL_CORE_LIB NAMES mkl_core PATHS
  ${MKL_ROOT}/lib
  ${MKL_ROOT}/lib/intel64)
find_library(MKL_SEQUENTIAL_LIB NAMES mkl_sequential PATHS
  ${MKL_ROOT}/lib
  ${MKL_ROOT}/lib/intel64)
find_library(MKL_INTEL_LP64 NAMES mkl_intel_lp64 PATHS
  ${MKL_ROOT}/lib
  ${MKL_ROOT}/lib/intel64)
Z
zhangjinchao01 已提交
60 61


L
Liu Yiqun 已提交
62
if(MKL_INC_DIR AND MKL_CORE_LIB AND MKL_SEQUENTIAL_LIB AND MKL_INTEL_LP64)
Z
zhangjinchao01 已提交
63
  set(CBLAS_PROVIDER MKL)
L
Liu Yiqun 已提交
64
  set(CBLAS_INC_DIR ${MKL_INC_DIR})
L
liaogang 已提交
65
  set(CBLAS_LIBRARIES ${MKL_INTEL_LP64}
Z
zhangjinchao01 已提交
66 67 68
          ${MKL_SEQUENTIAL_LIB}
          ${MKL_CORE_LIB})
  add_definitions(-DPADDLE_USE_MKL)
L
liaogang 已提交
69 70
  message(STATUS "Found MKL (include: ${CBLAS_INC_DIR}, library: ${CBLAS_LIBRARIES})")
  set(CBLAS_FOUND ON)
L
Liu Yiqun 已提交
71 72 73 74 75 76 77
  if(${MKL_LAPACK_INC_DIR})
    set(LAPACK_INC_DIR ${MKL_LAPACK_INC_DIR})
    message(STATUS "Found lapack in MKL (include: ${MKL_LAPACK_INC_DIR})")
    set(LAPACK_FOUND ON)
  else()
    find_lapack()
  endif()
Z
zhangjinchao01 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91
  return() # return file.
endif()

## Then find atlas.
set(ATLAS_ROOT $ENV{ATLAS_ROOT} CACHE PATH "Folder contains Atlas")
set(ATLAS_INCLUDE_SEARCH_PATHS
        ${ATLAS_ROOT}/include
        /usr/include
        /usr/include/atlas)
set(ATLAS_LIB_SEARCH_PATHS
        ${ATLAS_ROOT}/lib
        /usr/lib
        /usr/lib/blas/atlas
        /usr/lib/atlas
L
liaogang 已提交
92 93
        /usr/lib/atlas-base   # special for ubuntu 14.04.
    )
Z
zhangjinchao01 已提交
94 95
find_path(ATLAS_INC_DIR NAMES cblas.h 
  PATHS ${ATLAS_INCLUDE_SEARCH_PATHS})
L
lzhao4ever 已提交
96 97
find_path(ATLAS_CLAPACK_INC_DIR NAMES clapack.h
  PATHS ${ATLAS_INCLUDE_SEARCH_PATHS})
Z
zhangjinchao01 已提交
98 99
find_library(ATLAS_CBLAS_LIB NAMES cblas libcblas.so.3 
  PATHS ${ATLAS_LIB_SEARCH_PATHS})
L
lzhao4ever 已提交
100
find_library(ATLAS_LIB NAMES lapack_atlas liblapack_atlas.so.3
Z
zhangjinchao01 已提交
101 102
  PATHS ${ATLAS_LIB_SEARCH_PATHS})

L
Liu Yiqun 已提交
103
if(ATLAS_INC_DIR AND ATLAS_CBLAS_LIB AND ATLAS_LIB AND NOT CBLAS_FOUND)
Z
zhangjinchao01 已提交
104
  set(CBLAS_PROVIDER ATLAS)
L
Liu Yiqun 已提交
105
  set(CBLAS_INC_DIR ${ATLAS_INC_DIR})
L
liaogang 已提交
106
  set(CBLAS_LIBRARIES ${ATLAS_LIB} ${ATLAS_CBLAS_LIB})
L
lzhao4ever 已提交
107
  add_definitions(-DPADDLE_USE_ATLAS)  
L
Liu Yiqun 已提交
108
  message(STATUS "Found ATLAS (include: ${CBLAS_INC_DIR}, library: ${CBLAS_LIBRARIES})")
L
liaogang 已提交
109
  set(CBLAS_FOUND ON)
L
Liu Yiqun 已提交
110 111 112 113 114 115 116
  if(ATLAS_CLAPACK_INC_DIR)
    set(LAPACK_INC_DIR ${ATLAS_CLAPACK_INC_DIR})
    message(STATUS "Found lapack in ATLAS (include: ${ATLAS_CLAPACK_INC_DIR})")
    set(LAPACK_FOUND ON)
  else()
    find_lapack()
  endif()
Z
zhangjinchao01 已提交
117 118 119 120 121 122 123 124
  return()
endif()

## Then find openblas.
set(OPENBLAS_ROOT $ENV{OPENBLAS_ROOT} CACHE PATH "Folder contains Openblas")
set(OPENBLAS_INCLUDE_SEARCH_PATHS
        ${OPENBLAS_ROOT}/include
        /usr/include
L
liaogang 已提交
125 126
        /usr/include/openblas
        /usr/local/opt/openblas/include)
Z
zhangjinchao01 已提交
127 128 129 130
set(OPENBLAS_LIB_SEARCH_PATHS
        ${OPENBLAS_ROOT}/lib
        /usr/lib
        /usr/lib/blas/openblas
L
liaogang 已提交
131 132
        /usr/lib/openblas
        /usr/local/opt/openblas/lib)
Z
zhangjinchao01 已提交
133 134 135

find_path(OPENBLAS_INC_DIR NAMES cblas.h
  PATHS ${OPENBLAS_INCLUDE_SEARCH_PATHS})
L
lzhao4ever 已提交
136 137
find_path(OPENBLAS_LAPACKE_INC_DIR NAMES lapacke.h
  PATHS ${OPENBLAS_INCLUDE_SEARCH_PATHS})
Z
zhangjinchao01 已提交
138 139 140 141 142 143
find_library(OPENBLAS_LIB NAMES openblas
  PATHS ${OPENBLAS_LIB_SEARCH_PATHS})

if(OPENBLAS_INC_DIR AND OPENBLAS_LIB)
  set(CBLAS_PROVIDER OPENBLAS)
  set(CBLAS_INC_DIR ${OPENBLAS_INC_DIR})
L
liaogang 已提交
144
  set(CBLAS_LIBRARIES ${OPENBLAS_LIB})
L
Liu Yiqun 已提交
145
  message(STATUS "Found OpenBLAS (include: ${CBLAS_INC_DIR}, library: ${CBLAS_LIBRARIES})")
L
liaogang 已提交
146
  set(CBLAS_FOUND ON)
L
Liu Yiqun 已提交
147 148 149 150 151 152 153
  if(OPENBLAS_LAPACKE_INC_DIR)
    set(LAPACK_INC_DIR ${OPENBLAS_LAPACKE_INC_DIR})
    message(STATUS "Found lapack in OpenBLAS (include: ${OPENBLAS_LAPACKE_INC_DIR})")
    set(LAPACK_FOUND ON)
  else()
    find_lapack()
  endif()
Z
zhangjinchao01 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  return()
endif()


## Then find the reference-cblas.  www.netlib.org/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/
)

find_path(REFERENCE_CBLAS_INCLUDE_DIR NAMES cblas.h PATHS
        ${REFERENCE_CBLAS_INCLUDE_SEARCH_PATHS})
find_library(REFERENCE_CBLAS_LIBRARY NAMES cblas PATHS
        ${REFERENCE_CBLAS_LIB_SEARCH_PATHS})

if (REFERENCE_CBLAS_INCLUDE_DIR AND REFERENCE_CBLAS_LIBRARY)
  set(CBLAS_PROVIDER REFERENCE)
  set(CBLAS_INC_DIR ${REFERENCE_CBLAS_INCLUDE_DIR})
L
liaogang 已提交
184 185 186
  set(CBLAS_LIBRARIES ${REFERENCE_CBLAS_LIBRARY})
  message(STATUS "Found reference-cblas (include: ${CBLAS_INC_DIR}, library: ${CBLAS_LIBS})")
  set(CBLAS_FOUND ON)
Z
zhangjinchao01 已提交
187
endif()