mkldnn.cmake 4.6 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
SET(MKLDNN_PREFIX_DIR    ${THIRD_PARTY_PATH}/mkldnn)
19 20
SET(MKLDNN_INSTALL_DIR    ${THIRD_PARTY_PATH}/install/mkldnn)
SET(MKLDNN_INC_DIR        "${MKLDNN_INSTALL_DIR}/include" CACHE PATH "mkldnn include directory." FORCE)
21
SET(MKLDNN_REPOSITORY     https://github.com/intel/mkl-dnn.git)
A
Adam 已提交
22
SET(MKLDNN_TAG            518a316a8cd6deb82dc7866bc04bd0355a25c3a4)
23

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

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

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

P
peizhilin 已提交
38 39 40 41 42
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}")
P
peizhilin 已提交
43 44
ELSE()
    SET(MKLDNN_CXXFLAG "${CMAKE_CXX_FLAGS} /EHsc")
P
peizhilin 已提交
45 46
ENDIF(NOT WIN32)

47 48 49 50
cache_third_party(${MKLDNN_PROJECT}
    REPOSITORY    ${MKLDNN_REPOSITORY}
    TAG           ${MKLDNN_TAG})

51 52 53
ExternalProject_Add(
    ${MKLDNN_PROJECT}
    ${EXTERNAL_PROJECT_LOG_ARGS}
54
    ${SHALLOW_CLONE}
55
    "${MKLDNN_DOWNLOAD_CMD}"
T
tensor-tang 已提交
56
    DEPENDS             ${MKLDNN_DEPENDS}
57 58
    PREFIX              ${MKLDNN_PREFIX_DIR}
    SOURCE_DIR          ${MKLDNN_SOURCE_DIR}
A
Adam 已提交
59 60
    BUILD_ALWAYS        1
    # UPDATE_COMMAND      ""
M
minqiyang 已提交
61
    CMAKE_ARGS          -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
62 63 64 65 66 67 68 69 70 71 72 73
                        -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 已提交
74
                        -DMKLDNN_BUILD_TESTS=OFF -DMKLDNN_BUILD_EXAMPLES=OFF
75
    CMAKE_CACHE_ARGS    -DCMAKE_INSTALL_PREFIX:PATH=${MKLDNN_INSTALL_DIR}
76
)
P
peizhilin 已提交
77
if(WIN32)
78
    SET(MKLDNN_LIB "${MKLDNN_INSTALL_DIR}/${LIBDIR}/mkldnn.lib" CACHE FILEPATH "mkldnn library." FORCE)
P
peizhilin 已提交
79
else(WIN32)
80
    SET(MKLDNN_LIB "${MKLDNN_INSTALL_DIR}/${LIBDIR}/libmkldnn.so" CACHE FILEPATH "mkldnn library." FORCE)
P
peizhilin 已提交
81
endif(WIN32)
82

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

# generate a static dummy target to track mkldnn dependencies
# for cc_library(xxx SRCS xxx.c DEPS mkldnn)
SET(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/mkldnn_dummy.c)
FILE(WRITE ${dummyfile} "const char * dummy = \"${dummyfile}\";")
ADD_LIBRARY(mkldnn STATIC ${dummyfile})
A
Adam 已提交
94
TARGET_LINK_LIBRARIES(mkldnn ${MKLDNN_LIB} ${MKLML_IOMP_LIB})
95
ADD_DEPENDENCIES(mkldnn ${MKLDNN_PROJECT})
96

T
tensor-tang 已提交
97 98
# copy the real so.0 lib to install dir
# it can be directly contained in wheel or capi
P
peizhilin 已提交
99
if(WIN32)
100
    SET(MKLDNN_SHARED_LIB ${MKLDNN_INSTALL_DIR}/bin/mkldnn.dll)
P
peizhilin 已提交
101 102
else(WIN32)
    SET(MKLDNN_SHARED_LIB ${MKLDNN_INSTALL_DIR}/libmkldnn.so.0)
A
Adam 已提交
103
    SET(MKLDNN_SHARED_LIB_1 ${MKLDNN_INSTALL_DIR}/libmkldnn.so.1)
104 105
    ADD_CUSTOM_COMMAND(TARGET ${MKLDNN_PROJECT} POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy ${MKLDNN_LIB} ${MKLDNN_SHARED_LIB})
A
Adam 已提交
106 107
    ADD_CUSTOM_COMMAND(TARGET ${MKLDNN_PROJECT} POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy ${MKLDNN_LIB} ${MKLDNN_SHARED_LIB_1})
P
peizhilin 已提交
108
endif(WIN32)