Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save emfomenk/34abde34e442e9b42d6eec82e1f93aa1 to your computer and use it in GitHub Desktop.
Save emfomenk/34abde34e442e9b42d6eec82e1f93aa1 to your computer and use it in GitHub Desktop.
Add OpenSource TBB support to DNNL
From 2484a4c3fe1fcab77b9e872737ededd41e8a61de Mon Sep 17 00:00:00 2001
From: "Fomenko, Evarist M" <evarist.m.fomenko@intel.com>
Date: Mon, 11 Nov 2019 14:38:25 -0800
Subject: [PATCH] build: retrieve TBB include dir location from properties
This allows using open source TBB using:
cmake -DTBB_DIR=/path/to/TBBConfig.cmake ..
---
cmake/TBB.cmake | 20 ++++++++++++++++++--
cmake/lnx/TBBConfig.cmake | 13 -------------
cmake/mac/TBBConfig.cmake | 12 ------------
cmake/win/TBBConfig.cmake | 13 -------------
doc/build/build_options.md | 5 +++++
5 files changed, 23 insertions(+), 40 deletions(-)
diff --git a/cmake/TBB.cmake b/cmake/TBB.cmake
index 99c296db3..152c78bf5 100644
--- a/cmake/TBB.cmake
+++ b/cmake/TBB.cmake
@@ -35,7 +35,23 @@ elseif(UNIX)
find_package(TBB REQUIRED tbb HINTS cmake/lnx)
endif()
-include_directories(${TBB_INCLUDE_DIRS})
+# Locate TBB
+get_target_property(_tbb_include_dirs TBB::tbb INTERFACE_INCLUDE_DIRECTORIES)
+
+# Check for TBB version, required >= 2017
+file(READ "${_tbb_include_dirs}/tbb/tbb_stddef.h" _tbb_stddef)
+string(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1" TBB_INTERFACE_VERSION "${_tbb_stddef}")
+if (${TBB_INTERFACE_VERSION} VERSION_LESS 9100)
+ message(FATAL_ERROR "DNNL requires TBB version 2017 or above")
+endif()
+
+include_directories(${_tbb_include_dirs})
list(APPEND EXTRA_SHARED_LIBS ${TBB_IMPORTED_TARGETS})
-message(STATUS "Intel(R) TBB: ${TBBROOT}")
+# Print TBB location
+get_filename_component(_tbb_root "${_tbb_include_dirs}" PATH)
+get_filename_component(_tbb_root "${_tbb_root}" ABSOLUTE)
+message(STATUS "TBB: ${_tbb_root}")
+
+unset(_tbb_include_dirs)
+unset(_tbb_root)
diff --git a/cmake/lnx/TBBConfig.cmake b/cmake/lnx/TBBConfig.cmake
index 7cdeaa90f..3ca4f4a39 100644
--- a/cmake/lnx/TBBConfig.cmake
+++ b/cmake/lnx/TBBConfig.cmake
@@ -88,14 +88,6 @@ unset(_tbb_gcc_version_number)
unset(_tbb_compiler_id)
unset(_tbb_compiler_ver)
-
-# we need to check the version of tbb
-file(READ "${_tbb_root}/include/tbb/tbb_stddef.h" _tbb_stddef)
-string(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1" TBB_INTERFACE_VERSION "${_tbb_stddef}")
-if (${TBB_INTERFACE_VERSION} VERSION_LESS 9100)
- message(FATAL_ERROR "DNNL requires TBB version 2017 or above")
-endif()
-
# Now we check that all the needed component are present
get_filename_component(_tbb_lib_path "${_tbb_root}/lib/${_tbb_arch_subdir}/${_tbb_compiler_subdir}" ABSOLUTE)
@@ -116,11 +108,6 @@ foreach (_tbb_component ${TBB_FIND_COMPONENTS})
IMPORTED_LOCATION_DEBUG "${_tbb_debug_lib}"
INTERFACE_INCLUDE_DIRECTORIES "${_tbb_root}/include")
- # DNNL changes: set TBB_INCLUDE_DIRS to use it for include_directories()
- if (_tbb_component STREQUAL tbb)
- set(TBB_INCLUDE_DIRS "${_tbb_root}/include")
- endif()
-
# Add internal dependencies for imported targets: TBB::tbbmalloc_proxy -> TBB::tbbmalloc
if (_tbb_component STREQUAL tbbmalloc_proxy)
set_target_properties(TBB::tbbmalloc_proxy PROPERTIES INTERFACE_LINK_LIBRARIES TBB::tbbmalloc)
diff --git a/cmake/mac/TBBConfig.cmake b/cmake/mac/TBBConfig.cmake
index 528067d88..581237afa 100644
--- a/cmake/mac/TBBConfig.cmake
+++ b/cmake/mac/TBBConfig.cmake
@@ -58,13 +58,6 @@ endif()
set(_tbb_compiler_subdir .)
-# we need to check the version of tbb
-file(READ "${_tbb_root}/include/tbb/tbb_stddef.h" _tbb_stddef)
-string(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1" TBB_INTERFACE_VERSION "${_tbb_stddef}")
-if (${TBB_INTERFACE_VERSION} VERSION_LESS 9100)
- message(FATAL_ERROR "DNNL requires TBB version 2017 or above")
-endif()
-
get_filename_component(_tbb_lib_path "${_tbb_root}/lib/${_tbb_arch_subdir}/${_tbb_compiler_subdir}" ABSOLUTE)
if (TBB_FOUND)
@@ -84,11 +77,6 @@ foreach (_tbb_component ${TBB_FIND_COMPONENTS})
IMPORTED_LOCATION_DEBUG "${_tbb_debug_lib}"
INTERFACE_INCLUDE_DIRECTORIES "${_tbb_root}/include")
- # DNNL changes: set TBB_INCLUDE_DIRS to use it for include_directories()
- if (_tbb_component STREQUAL tbb)
- set(TBB_INCLUDE_DIRS "${_tbb_root}/include")
- endif()
-
# Add internal dependencies for imported targets: TBB::tbbmalloc_proxy -> TBB::tbbmalloc
if (_tbb_component STREQUAL tbbmalloc_proxy)
set_target_properties(TBB::tbbmalloc_proxy PROPERTIES INTERFACE_LINK_LIBRARIES TBB::tbbmalloc)
diff --git a/cmake/win/TBBConfig.cmake b/cmake/win/TBBConfig.cmake
index 6ccd3d5b7..02c19afc9 100644
--- a/cmake/win/TBBConfig.cmake
+++ b/cmake/win/TBBConfig.cmake
@@ -83,14 +83,6 @@ endif()
get_filename_component(_tbb_lib_path "${_tbb_root}/lib/${_tbb_arch_subdir}/${_tbb_compiler_subdir}" ABSOLUTE)
get_filename_component(_tbb_inc_path "${_tbb_root}/include/" ABSOLUTE)
-
-# we need to check the version of tbb
-file(READ "${_tbb_inc_path}/tbb/tbb_stddef.h" _tbb_stddef)
-string(REGEX REPLACE ".*#define TBB_INTERFACE_VERSION ([0-9]+).*" "\\1" TBB_INTERFACE_VERSION "${_tbb_stddef}")
-if (${TBB_INTERFACE_VERSION} VERSION_LESS 9100)
- message(FATAL_ERROR "DNNL requires TBB version 2017 or above")
-endif()
-
if (TBB_FOUND)
return()
endif()
@@ -111,11 +103,6 @@ foreach (_tbb_component ${TBB_FIND_COMPONENTS})
IMPORTED_IMPLIB_DEBUG "${_tbb_debug_lib}"
INTERFACE_COMPILE_DEFINITIONS "__TBB_NO_IMPLICIT_LINKAGE=1")
- # DNNL changes: set TBB_INCLUDE_DIRS to use it for include_directories()
- if (_tbb_component STREQUAL tbb)
- set(TBB_INCLUDE_DIRS "${_tbb_inc_path}")
- endif()
-
# Add internal dependencies for imported targets: TBB::tbbmalloc_proxy -> TBB::tbbmalloc
if (_tbb_component STREQUAL tbbmalloc_proxy)
set_target_properties(TBB::tbbmalloc_proxy PROPERTIES INTERFACE_LINK_LIBRARIES TBB::tbbmalloc)
diff --git a/doc/build/build_options.md b/doc/build/build_options.md
index 873869601..4658ca3df 100644
--- a/doc/build/build_options.md
+++ b/doc/build/build_options.md
@@ -78,6 +78,11 @@ cmake:
$ cmake -DDNNL_CPU_RUNTIME=TBB -DTBBROOT=/opt/intel/path/tbb ..
~~~
+Alternatively, point the location of `TBBConfig.cmake` config-file package:
+~~~sh
+$ cmake -DDNNL_CPU_RUNTIME=TBB -DTBB_DIR=/usr/local/lib/cmake/tbb ..
+~~~
+
DNNL has limited optimizations for Intel TBB and has some functional
limitations if built with Intel TBB.
--
2.17.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment