Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkldnn.cmake
1 #===============================================================================
2 # Copyright (C) 2018-2019 Intel Corporation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #===============================================================================
16 #
17 #  Brief description: This cmake file replase original mkl-dnn build scripts
18 #  for more convenient integration to IE build process
19 #
20 #===============================================================================
21
22 set (CMAKE_CXX_STANDARD 11)
23 set (CMAKE_CXX_STANDARD_REQUIRED ON)
24
25 set(version_cmake_included true)
26
27 set(TARGET mkldnn)
28 set(MKLDNN_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/mkl-dnn)
29
30 string(REPLACE "." ";" VERSION_LIST "0.18.0")
31 list(GET VERSION_LIST 0 MKLDNN_VERSION_MAJOR)
32 list(GET VERSION_LIST 1 MKLDNN_VERSION_MINOR)
33 list(GET VERSION_LIST 2 MKLDNN_VERSION_PATCH)
34
35 find_package(Git)
36 if (GIT_FOUND)
37     execute_process(COMMAND ${GIT_EXECUTABLE} log -1 --format=%H
38             WORKING_DIRECTORY ${MKLDNN_ROOT}
39             RESULT_VARIABLE RESULT
40             OUTPUT_VARIABLE MKLDNN_VERSION_HASH
41             OUTPUT_STRIP_TRAILING_WHITESPACE)
42 endif()
43
44 if(NOT GIT_FOUND OR RESULT)
45     set(MKLDNN_VERSION_HASH "N/A")
46 endif()
47
48 configure_file(
49     "${MKLDNN_ROOT}/include/mkldnn_version.h.in"
50     "${CMAKE_BINARY_DIR}/include/mkldnn_version.h"
51 )
52
53 function(detect_mkl LIBNAME)
54     message(STATUS "Detecting Intel(R) MKL: trying ${LIBNAME}")
55     find_path(MKLINC mkl_cblas.h ${MKL}/include)
56     find_library(MKLLIB ${LIBNAME} "${MKL}/lib")
57
58     if(NOT MKLLIB OR NOT MKLINC)
59         message(FATAL_ERROR "${MKLINC} or ${MKLLIB} are not found")
60         return()
61     endif()
62
63     if(WIN32)
64         find_file(MKLDLL ${LIBNAME}.dll PATHS "${MKL}/lib")
65         if(NOT MKLDLL)
66             message(FATAL_ERROR "${LIBNAME} not found")
67             return()
68         endif()
69     endif()
70
71     set(MKLINC ${MKLINC} PARENT_SCOPE)
72     set(MKLLIB "${MKLLIB}" PARENT_SCOPE)
73     message(STATUS "Intel(R) MKL: include ${MKLINC}")
74     message(STATUS "Intel(R) MKL: lib ${MKLLIB}")
75
76     if(WIN32)
77         set(MKLDLL "${MKLDLL}" PARENT_SCOPE)
78         message(STATUS "Intel(R) MKL: dll ${MKLDLL}")
79     endif()
80 endfunction()
81
82 if (THREADING STREQUAL "TBB")
83     add_definitions(-DMKLDNN_THR=MKLDNN_THR_TBB)
84 elseif (THREADING STREQUAL "OMP")
85     add_definitions(-DMKLDNN_THR=MKLDNN_THR_OMP)
86 else()
87     add_definitions(-DMKLDNN_THR=MKLDNN_THR_SEQ)
88 endif ()
89
90 file(GLOB_RECURSE HDR
91         ${MKLDNN_ROOT}/include/*.h
92         ${MKLDNN_ROOT}/include/*.hpp
93 )
94 file(GLOB_RECURSE SRC
95         ${MKLDNN_ROOT}/src/*.c
96         ${MKLDNN_ROOT}/src/*.cpp
97         ${MKLDNN_ROOT}/src/*.h
98         ${MKLDNN_ROOT}/src/*.hpp
99 )
100 include_directories(
101         ${MKLDNN_ROOT}/include
102         ${MKLDNN_ROOT}/src
103         ${MKLDNN_ROOT}/src/common
104         ${MKLDNN_ROOT}/src/cpu/
105         ${MKLDNN_ROOT}/src/cpu/xbyak
106         ${CMAKE_BINARY_DIR}/include/
107 )
108
109 if(WIN32)
110     add_definitions(-D_WIN)
111     add_definitions(-DNOMINMAX)
112     # Correct 'jnl' macro/jit issue
113     if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Intel")
114         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Qlong-double /bigobj")
115     endif()
116 endif()
117
118 # to make build time reasonable, don't use optimizations for s8u8s32 Xbyak
119 # kernels
120 file(GLOB FILES_WITHNO_OPT
121     ${MKLDNN_ROOT}/src/cpu/gemm/s8x8s32/jit_avx512_core_kernel_b0_gemm_s8u8s32_kern.cpp
122     ${MKLDNN_ROOT}/src/cpu/gemm/s8x8s32/jit_avx512_core_kernel_gemm_s8u8s32_kern.cpp
123     ${MKLDNN_ROOT}/src/cpu/gemm/s8x8s32/jit_avx512_core_u8_copy_an_kern.cpp
124     ${MKLDNN_ROOT}/src/cpu/gemm/s8x8s32/jit_avx512_core_u8_copy_at_kern.cpp
125     ${MKLDNN_ROOT}/src/cpu/gemm/s8x8s32/jit_avx512_core_u8_copy_bn_kern.cpp
126     ${MKLDNN_ROOT}/src/cpu/gemm/s8x8s32/jit_avx512_core_u8_copy_bt_kern.cpp)
127 if(WIN32 AND NOT MINGW)
128     set_source_files_properties(${FILES_WITHNO_OPT}
129         PROPERTIES COMPILE_FLAGS "/Od")
130 else()
131     set_source_files_properties(${FILES_WITHNO_OPT}
132         PROPERTIES COMPILE_FLAGS "-O0 -U_FORTIFY_SOURCE")
133 endif()
134
135 add_library(${TARGET} STATIC ${HDR} ${SRC})
136 set_ie_threading_interface_for(${TARGET})
137
138 if(GEMM STREQUAL "OPENBLAS")
139     ## enable cblas_gemm from OpenBLAS package
140     add_definitions(-DUSE_CBLAS)
141     include_directories(${BLAS_INCLUDE_DIRS})
142     list(APPEND ${TARGET}_LINKER_LIBS ${BLAS_LIBRARIES})
143 elseif (GEMM STREQUAL "MKL")
144     ## enable cblas_gemm from mlkml package
145 if(WIN32 OR APPLE)
146     detect_mkl("mklml")
147 else()
148     if(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
149         detect_mkl("mklml_intel")
150     else()
151         detect_mkl("mklml_gnu")
152     endif()
153 endif()
154     add_definitions(-DUSE_MKL -DUSE_CBLAS)
155     include_directories(AFTER ${MKLINC})
156     list(APPEND ${TARGET}_LINKER_LIBS ${MKLLIB})
157 endif()
158 ## enable jit_gemm from mlk-dnn
159
160 target_link_libraries(${TARGET} PRIVATE ${${TARGET}_LINKER_LIBS})