mkldnn.cmake 6.0 KB
Newer Older
1
# Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#
# 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.

INCLUDE(ExternalProject)

T
tensor-tang 已提交
17
SET(MKLDNN_PROJECT        "extern_mkldnn")
18 19
SET(MKLDNN_PREFIX_DIR     ${THIRD_PARTY_PATH}/mkldnn)
SET(MKLDNN_SOURCE_DIR     ${THIRD_PARTY_PATH}/mkldnn/src/extern_mkldnn)
20 21
SET(MKLDNN_INSTALL_DIR    ${THIRD_PARTY_PATH}/install/mkldnn)
SET(MKLDNN_INC_DIR        "${MKLDNN_INSTALL_DIR}/include" CACHE PATH "mkldnn include directory." FORCE)
22
SET(MKLDNN_REPOSITORY     ${GIT_URL}/oneapi-src/oneDNN.git)
23
SET(MKLDNN_TAG            bbaf5d24dde1b6760435d5034d6f48feae7a30b9)
24

25

26 27 28 29 30 31 32 33
# Introduce variables:
# * CMAKE_INSTALL_LIBDIR
INCLUDE(GNUInstallDirs)
SET(LIBDIR "lib")
if(CMAKE_INSTALL_LIBDIR MATCHES ".*lib64$")
  SET(LIBDIR "lib64")
endif()

34
MESSAGE(STATUS "Set ${MKLDNN_INSTALL_DIR}/${LIBDIR} to runtime path")
35
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
36
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}" "${MKLDNN_INSTALL_DIR}/${LIBDIR}")
37

38
INCLUDE_DIRECTORIES(${MKLDNN_INC_DIR}) # For MKLDNN code to include internal headers.
39

40

P
peizhilin 已提交
41 42 43 44 45
IF(NOT WIN32)
    SET(MKLDNN_FLAG "-Wno-error=strict-overflow -Wno-error=unused-result -Wno-error=array-bounds")
    SET(MKLDNN_FLAG "${MKLDNN_FLAG} -Wno-unused-result -Wno-unused-value")
    SET(MKLDNN_CFLAG "${CMAKE_C_FLAGS} ${MKLDNN_FLAG}")
    SET(MKLDNN_CXXFLAG "${CMAKE_CXX_FLAGS} ${MKLDNN_FLAG}")
46
    SET(MKLDNN_LIB "${MKLDNN_INSTALL_DIR}/${LIBDIR}/libdnnl.so" CACHE FILEPATH "mkldnn library." FORCE)
P
peizhilin 已提交
47 48
ELSE()
    SET(MKLDNN_CXXFLAG "${CMAKE_CXX_FLAGS} /EHsc")
49
    SET(MKLDNN_LIB "${MKLDNN_INSTALL_DIR}/bin/mkldnn.lib" CACHE FILEPATH "mkldnn library." FORCE)
P
peizhilin 已提交
50 51
ENDIF(NOT WIN32)

52 53
cache_third_party(${MKLDNN_PROJECT}
    REPOSITORY    ${MKLDNN_REPOSITORY}
54 55
    TAG           ${MKLDNN_TAG}
    DIR           MKLDNN_SOURCE_DIR)
56

57 58 59
ExternalProject_Add(
    ${MKLDNN_PROJECT}
    ${EXTERNAL_PROJECT_LOG_ARGS}
60
    ${SHALLOW_CLONE}
61
    "${MKLDNN_DOWNLOAD_CMD}"
T
tensor-tang 已提交
62
    DEPENDS             ${MKLDNN_DEPENDS}
63 64
    PREFIX              ${MKLDNN_PREFIX_DIR}
    SOURCE_DIR          ${MKLDNN_SOURCE_DIR}
65 66
    UPDATE_COMMAND      ""
    #BUILD_ALWAYS        1
M
minqiyang 已提交
67
    CMAKE_ARGS          -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
68 69 70 71 72 73 74 75 76 77 78 79
                        -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
                        -DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}
                        -DCMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG}
                        -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
                        -DCMAKE_C_FLAGS_DEBUG=${CMAKE_C_FLAGS_DEBUG}
                        -DCMAKE_C_FLAGS_RELEASE=${CMAKE_C_FLAGS_RELEASE}
                        -DCMAKE_INSTALL_PREFIX=${MKLDNN_INSTALL_DIR}
                        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
                        -DCMAKE_POSITION_INDEPENDENT_CODE=ON
                        -DMKLROOT=${MKLML_ROOT}
                        -DCMAKE_C_FLAGS=${MKLDNN_CFLAG}
                        -DCMAKE_CXX_FLAGS=${MKLDNN_CXXFLAG}
A
Adam 已提交
80
                        -DDNNL_BUILD_TESTS=OFF -DDNNL_BUILD_EXAMPLES=OFF
81
    CMAKE_CACHE_ARGS    -DCMAKE_INSTALL_PREFIX:PATH=${MKLDNN_INSTALL_DIR}
82
    BUILD_BYPRODUCTS    ${MKLDNN_LIB}
83 84
)

85 86 87
ADD_LIBRARY(shared_mkldnn SHARED IMPORTED GLOBAL)
SET_PROPERTY(TARGET shared_mkldnn PROPERTY IMPORTED_LOCATION ${MKLDNN_LIB})
ADD_DEPENDENCIES(shared_mkldnn ${MKLDNN_PROJECT})
88
MESSAGE(STATUS "MKLDNN library: ${MKLDNN_LIB}")
T
tensor-tang 已提交
89
add_definitions(-DPADDLE_WITH_MKLDNN)
90 91 92

# generate a static dummy target to track mkldnn dependencies
# for cc_library(xxx SRCS xxx.c DEPS mkldnn)
93 94
generate_dummy_static_lib(LIB_NAME "mkldnn" GENERATOR "mkldnn.cmake")

A
Adam 已提交
95
TARGET_LINK_LIBRARIES(mkldnn ${MKLDNN_LIB} ${MKLML_IOMP_LIB})
96
ADD_DEPENDENCIES(mkldnn ${MKLDNN_PROJECT})
97

T
tensor-tang 已提交
98 99
# copy the real so.0 lib to install dir
# it can be directly contained in wheel or capi
P
peizhilin 已提交
100
if(WIN32)
101
    SET(MKLDNN_SHARED_LIB ${MKLDNN_INSTALL_DIR}/bin/mkldnn.dll)
Z
Zhou Wei 已提交
102 103 104

    file(TO_NATIVE_PATH ${MKLDNN_INSTALL_DIR} NATIVE_MKLDNN_INSTALL_DIR)
    file(TO_NATIVE_PATH ${MKLDNN_SHARED_LIB} NATIVE_MKLDNN_SHARED_LIB)
A
Adam 已提交
105
    ADD_CUSTOM_COMMAND(TARGET ${MKLDNN_PROJECT} POST_BUILD
Z
Zhou Wei 已提交
106
        COMMAND (copy ${NATIVE_MKLDNN_INSTALL_DIR}\\bin\\dnnl.dll ${NATIVE_MKLDNN_SHARED_LIB} /Y))
A
Adam 已提交
107 108 109 110 111 112 113
    add_custom_command(TARGET ${MKLDNN_PROJECT} POST_BUILD VERBATIM
        COMMAND dumpbin /exports ${MKLDNN_INSTALL_DIR}/bin/mkldnn.dll > ${MKLDNN_INSTALL_DIR}/bin/exports.txt)
    add_custom_command(TARGET ${MKLDNN_PROJECT} POST_BUILD VERBATIM
        COMMAND echo LIBRARY mkldnn > ${MKLDNN_INSTALL_DIR}/bin/mkldnn.def)
    add_custom_command(TARGET ${MKLDNN_PROJECT} POST_BUILD VERBATIM
        COMMAND echo EXPORTS >> ${MKLDNN_INSTALL_DIR}/bin/mkldnn.def)
    add_custom_command(TARGET ${MKLDNN_PROJECT} POST_BUILD VERBATIM
114
        COMMAND echo off && (for /f "skip=19 tokens=4" %A in (${MKLDNN_INSTALL_DIR}/bin/exports.txt) do echo %A >> ${MKLDNN_INSTALL_DIR}/bin/mkldnn.def) && echo on)
A
Adam 已提交
115 116
    add_custom_command(TARGET ${MKLDNN_PROJECT} POST_BUILD VERBATIM
        COMMAND lib /def:${MKLDNN_INSTALL_DIR}/bin/mkldnn.def /out:${MKLDNN_INSTALL_DIR}/bin/mkldnn.lib /machine:x64)
P
peizhilin 已提交
117 118
else(WIN32)
    SET(MKLDNN_SHARED_LIB ${MKLDNN_INSTALL_DIR}/libmkldnn.so.0)
A
Adam 已提交
119
    SET(MKLDNN_SHARED_LIB_1 ${MKLDNN_INSTALL_DIR}/libdnnl.so.1)
120
    SET(MKLDNN_SHARED_LIB_2 ${MKLDNN_INSTALL_DIR}/libdnnl.so.2)
121 122
    ADD_CUSTOM_COMMAND(TARGET ${MKLDNN_PROJECT} POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy ${MKLDNN_LIB} ${MKLDNN_SHARED_LIB})
A
Adam 已提交
123 124
    ADD_CUSTOM_COMMAND(TARGET ${MKLDNN_PROJECT} POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy ${MKLDNN_LIB} ${MKLDNN_SHARED_LIB_1})
125 126
    ADD_CUSTOM_COMMAND(TARGET ${MKLDNN_PROJECT} POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy ${MKLDNN_LIB} ${MKLDNN_SHARED_LIB_2})
P
peizhilin 已提交
127
endif(WIN32)