FindBLASEXT.cmake 12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
# - Find BLAS EXTENDED for MORSE projects: find include dirs and libraries
#
# This module allows to find BLAS libraries by calling the official FindBLAS module
# and handles the creation of different library lists whether the user wishes to link
# with a sequential BLAS or a multihreaded (BLAS_SEQ_LIBRARIES and BLAS_PAR_LIBRARIES).
# BLAS is detected with a FindBLAS call then if the BLAS vendor is Intel10_64lp or ACML
# then the module tries to find the corresponding multithreaded libraries.
#
# The following variables have been added to manage links with sequential or multithreaded
# versions:
#  BLAS_INCLUDE_DIRS  - BLAS include directories
#  BLAS_LIBRARY_DIRS  - Link directories for BLAS libraries
#  BLAS_SEQ_LIBRARIES - BLAS component libraries to be linked (sequential)
#  BLAS_PAR_LIBRARIES - BLAS component libraries to be linked (multithreaded)

#=============================================================================
# Copyright 2012-2013 Inria
# Copyright 2012-2013 Emmanuel Agullo
# Copyright 2012-2013 Mathieu Faverge
# Copyright 2012      Cedric Castagnede
# Copyright 2013      Florent Pruvost
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file MORSE-Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of Morse, substitute the full
#  License text for the above reference.)


# Some macros to print status when search for headers and libs
35
# PrintFindStatus.cmake is in cmake_modules/morse/find directory
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
include(PrintFindStatus)

# add a cache variable to let the user specify the BLAS vendor
set(BLA_VENDOR "" CACHE STRING "list of possible BLAS vendor:
    Goto, ATLAS PhiPACK, CXML, DXML, SunPerf, SCSL, SGIMATH, IBMESSL,
    Intel10_32 (intel mkl v10 32 bit),
    Intel10_64lp (intel mkl v10 64 bit, lp thread model, lp64 model),
    Intel10_64lp_seq (intel mkl v10 64 bit, sequential code, lp64 model),
    Intel( older versions of mkl 32 and 64 bit),
    ACML, ACML_MP, ACML_GPU, Apple, NAS, Generic")


if (NOT BLAS_FOUND)
    # First try to detect two cases:
    # 1: only SEQ libs are handled
    # 2: both SEQ and PAR libs are handled
    if(BLASEXT_FIND_REQUIRED)
        find_package(BLAS REQUIRED)
    else()
        find_package(BLAS)
    endif()
endif ()

# detect the cases where SEQ and PAR libs are handled
if(BLA_VENDOR STREQUAL "All" AND
   (BLAS_mkl_core_LIBRARY OR BLAS_mkl_core_dll_LIBRARY)
   )
    set(BLA_VENDOR "Intel")
    if(BLAS_mkl_intel_LIBRARY)
        set(BLA_VENDOR "Intel10_32")
    endif()
    if(BLAS_mkl_intel_lp64_LIBRARY)
        set(BLA_VENDOR "Intel10_64lp")
    endif()
elseif(BLA_VENDOR STREQUAL "All" AND BLAS_acml_LIBRARY)
    set(BLA_VENDOR "ACML")
endif()

# Intel case
if(BLA_VENDOR MATCHES "Intel*")

    if(NOT BLASEXT_FIND_QUIETLY)
        message(STATUS "A BLAS library has been found (${BLAS_LIBRARIES}) but we"
            "have also potentially detected some BLAS libraries from the MKL."
            "We try to use this one.")
        message(STATUS "If you want to force the use of one specific library, "
            "please specify the BLAS vendor by setting -DBLA_VENDOR=blas_vendor_name"
            "at cmake configure.")
        message(STATUS "List of possible BLAS vendor: Goto, ATLAS PhiPACK, CXML, "
            "DXML, SunPerf, SCSL, SGIMATH, IBMESSL, Intel10_32 (intel mkl v10 32 bit),"
            "Intel10_64lp (intel mkl v10 64 bit, lp thread model, lp64 model),"
            "Intel10_64lp_seq (intel mkl v10 64 bit, sequential code, lp64 model),"
            "Intel( older versions of mkl 32 and 64 bit),"
            "ACML, ACML_MP, ACML_GPU, Apple, NAS, Generic")
    endif()
    ###
    # look for include path if the BLAS vendor is Intel
    ###

    # gather system include paths
    unset(_inc_env)
    if(WIN32)
        string(REPLACE ":" ";" _inc_env "$ENV{INCLUDE}")
    else()
        string(REPLACE ":" ";" _path_env "$ENV{INCLUDE}")
        list(APPEND _inc_env "${_path_env}")
        string(REPLACE ":" ";" _path_env "$ENV{C_INCLUDE_PATH}")
        list(APPEND _inc_env "${_path_env}")
        string(REPLACE ":" ";" _path_env "$ENV{CPATH}")
        list(APPEND _inc_env "${_path_env}")
        string(REPLACE ":" ";" _path_env "$ENV{INCLUDE_PATH}")
        list(APPEND _inc_env "${_path_env}")
    endif()
    list(APPEND _inc_env "${CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES}")
    list(APPEND _inc_env "${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}")
    list(REMOVE_DUPLICATES _inc_env)

    # find mkl.h inside known include paths
    set(BLAS_mkl.h_INCLUDE_DIRS "BLAS_mkl.h_INCLUDE_DIRS-NOTFOUND")
    if(BLAS_INCDIR)
        find_path(BLAS_mkl.h_INCLUDE_DIRS
                NAMES mkl.h
                HINTS ${BLAS_INCDIR})
    else()
        if(BLAS_DIR)
            find_path(BLAS_mkl.h_INCLUDE_DIRS
                    NAMES mkl.h
                    HINTS ${BLAS_DIR}
                    PATH_SUFFIXES include)
        else()
            find_path(BLAS_mkl.h_INCLUDE_DIRS
                    NAMES mkl.h
                    HINTS ${_inc_env})
        endif()
    endif()
    mark_as_advanced(BLAS_mkl.h_INCLUDE_DIRS)
    # Print status if not found
    # -------------------------
    if (NOT BLAS_mkl.h_INCLUDE_DIRS)
        Print_Find_Header_Status(blas mkl.h)
    endif ()
    set(BLAS_INCLUDE_DIRS "")
    if(BLAS_mkl.h_INCLUDE_DIRS)
        list(APPEND BLAS_INCLUDE_DIRS "${BLAS_mkl.h_INCLUDE_DIRS}" )
    endif()

    ###
    # look for libs
    ###
    # if Intel 10 64 bit -> look for sequential and multithreaded versions
    if(BLA_VENDOR MATCHES "Intel10_64lp*")

        ## look for the sequential version
        set(BLA_VENDOR "Intel10_64lp_seq")
#         if(NOT BLAS_FOUND AND BLASEXT_FIND_REQUIRED)
        if(BLASEXT_FIND_REQUIRED)
            find_package(BLAS REQUIRED)
        else()
            find_package(BLAS)
        endif()
        if(BLAS_FOUND)
            set(BLAS_SEQ_LIBRARIES "${BLAS_LIBRARIES}")
        else()
            set(BLAS_SEQ_LIBRARIES "${BLAS_SEQ_LIBRARIES-NOTFOUND}")
        endif()

        ## look for the multithreaded version
        set(BLA_VENDOR "Intel10_64lp")
        find_package(BLAS)
        if(BLAS_FOUND)
            set(BLAS_PAR_LIBRARIES "${BLAS_LIBRARIES}")
        else()
            set(BLAS_PAR_LIBRARIES "${BLAS_PAR_LIBRARIES-NOTFOUND}")
        endif()

    else()

        if(BLAS_FOUND)
            set(BLAS_SEQ_LIBRARIES "${BLAS_LIBRARIES}")
        else()
            set(BLAS_SEQ_LIBRARIES "${BLAS_SEQ_LIBRARIES-NOTFOUND}")
        endif()

    endif()

# ACML case
elseif(BLA_VENDOR MATCHES "ACML*")

    if(NOT BLASEXT_FIND_QUIETLY)
        message(STATUS "A BLAS library has been found (${BLAS_LIBRARIES}) but we"
            "have also potentially detected some BLAS libraries from the ACML."
            "We try to use this one.")
        message(STATUS "If you want to force the use of one specific library, "
            "please specify the BLAS vendor by setting -DBLA_VENDOR=blas_vendor_name"
            "at cmake configure.")
        message(STATUS "List of possible BLAS vendor: Goto, ATLAS PhiPACK, CXML, "
            "DXML, SunPerf, SCSL, SGIMATH, IBMESSL, Intel10_32 (intel mkl v10 32 bit),"
            "Intel10_64lp (intel mkl v10 64 bit, lp thread model, lp64 model),"
            "Intel10_64lp_seq (intel mkl v10 64 bit, sequential code, lp64 model),"
            "Intel( older versions of mkl 32 and 64 bit),"
            "ACML, ACML_MP, ACML_GPU, Apple, NAS, Generic")
    endif()

    ## look for the sequential version
    set(BLA_VENDOR "ACML")
    if(BLASEXT_FIND_REQUIRED)
        find_package(BLAS REQUIRED)
    else()
        find_package(BLAS)
    endif()
    if(BLAS_FOUND)
        set(BLAS_SEQ_LIBRARIES "${BLAS_LIBRARIES}")
    else()
        set(BLAS_SEQ_LIBRARIES "${BLAS_SEQ_LIBRARIES-NOTFOUND}")
    endif()

    ## look for the multithreaded version
    set(BLA_VENDOR "ACML_MP")
    if(BLASEXT_FIND_REQUIRED)
        find_package(BLAS REQUIRED)
    else(BLAS_FOUND)
        find_package(BLAS)
    endif()
    if(BLAS_FOUND)
        set(BLAS_PAR_LIBRARIES "${BLAS_LIBRARIES}")
    else()
        set(BLAS_PAR_LIBRARIES "${BLAS_PAR_LIBRARIES-NOTFOUND}")
    endif()

else()

    if(BLAS_FOUND)
        # define the SEQ libs as the BLAS_LIBRARIES
        set(BLAS_SEQ_LIBRARIES "${BLAS_LIBRARIES}")
    else()
        set(BLAS_SEQ_LIBRARIES "${BLAS_SEQ_LIBRARIES-NOTFOUND}")
    endif()
    set(BLAS_PAR_LIBRARIES "${BLAS_PAR_LIBRARIES-NOTFOUND}")

endif()


if(BLAS_SEQ_LIBRARIES)
    set(BLAS_LIBRARIES "${BLAS_SEQ_LIBRARIES}")
endif()
# message("DEBUG BLAS: ${BLAS_SEQ_LIBRARIES} ${BLAS_LIBRARIES}")

# extract libs paths
# remark: because it is not given by find_package(BLAS)
set(BLAS_LIBRARY_DIRS "")
foreach(blas_lib ${BLAS_LIBRARIES})
    get_filename_component(a_blas_lib_dir "${blas_lib}" PATH)
    list(APPEND BLAS_LIBRARY_DIRS "${a_blas_lib_dir}" )
endforeach()
if (BLAS_LIBRARY_DIRS)
    list(REMOVE_DUPLICATES BLAS_LIBRARY_DIRS)
endif ()


# message(STATUS "BLAS_FOUND: ${BLAS_FOUND}")
# message(STATUS "BLA_VENDOR: ${BLA_VENDOR}")
# message(STATUS "BLAS_LIBRARIES: ${BLAS_LIBRARIES}")
# message(STATUS "BLAS_SEQ_LIBRARIES: ${BLAS_SEQ_LIBRARIES}")
# message(STATUS "BLAS_PAR_LIBRARIES: ${BLAS_PAR_LIBRARIES}")
# message(STATUS "BLAS_INCLUDE_DIRS: ${BLAS_INCLUDE_DIRS}")
# message(STATUS "BLAS_LIBRARY_DIRS: ${BLAS_LIBRARY_DIRS}")

# check that BLAS has been found
# ---------------------------------
include(FindPackageHandleStandardArgs)
if(BLA_VENDOR MATCHES "Intel*")
    if(BLA_VENDOR MATCHES "Intel10_64lp*")
        if(NOT BLASEXT_FIND_QUIETLY)
            message(STATUS "BLAS found is Intel MKL:"
                           "we manage two lists of libs,"
                           " one sequential and one parallel if found (see BLAS_SEQ_LIBRARIES and BLAS_PAR_LIBRARIES)")
            message(STATUS "BLAS sequential libraries stored in BLAS_SEQ_LIBRARIES")
        endif()
        find_package_handle_standard_args(BLAS DEFAULT_MSG
                                          BLAS_SEQ_LIBRARIES
                                          BLAS_LIBRARY_DIRS
                                          BLAS_INCLUDE_DIRS)
        if(BLAS_PAR_LIBRARIES)
            if(NOT BLASEXT_FIND_QUIETLY)
                message(STATUS "BLAS parallel libraries stored in BLAS_PAR_LIBRARIES")
            endif()
            find_package_handle_standard_args(BLAS DEFAULT_MSG
                                              BLAS_PAR_LIBRARIES)
        endif()
    else()
        if(NOT BLASEXT_FIND_QUIETLY)
            message(STATUS "BLAS sequential libraries stored in BLAS_SEQ_LIBRARIES")
        endif()
        find_package_handle_standard_args(BLAS DEFAULT_MSG
                                          BLAS_SEQ_LIBRARIES
                                          BLAS_LIBRARY_DIRS
                                          BLAS_INCLUDE_DIRS)
    endif()
elseif(BLA_VENDOR MATCHES "ACML*")
    if(NOT BLASEXT_FIND_QUIETLY)
        message(STATUS "BLAS found is ACML:"
                        "we manage two lists of libs,"
                        " one sequential and one parallel if found (see BLAS_SEQ_LIBRARIES and BLAS_PAR_LIBRARIES)")
        message(STATUS "BLAS sequential libraries stored in BLAS_SEQ_LIBRARIES")
    endif()
    find_package_handle_standard_args(BLAS DEFAULT_MSG
                                      BLAS_SEQ_LIBRARIES
                                      BLAS_LIBRARY_DIRS
                                      BLAS_INCLUDE_DIRS)
    if(BLAS_PAR_LIBRARIES)
        if(NOT BLASEXT_FIND_QUIETLY)
            message(STATUS "BLAS parallel libraries stored in BLAS_PAR_LIBRARIES")
        endif()
        find_package_handle_standard_args(BLAS DEFAULT_MSG
                                          BLAS_PAR_LIBRARIES)
    endif()
else()
    if(NOT BLASEXT_FIND_QUIETLY)
        message(STATUS "BLAS sequential libraries stored in BLAS_SEQ_LIBRARIES")
    endif()
    find_package_handle_standard_args(BLAS DEFAULT_MSG
                                      BLAS_SEQ_LIBRARIES
                                      BLAS_LIBRARY_DIRS)
endif()