Skip to content

core, gapi: supported build with oneTBB 2021#19384

Merged
opencv-pushbot merged 2 commits intoopencv:masterfrom
mshabunin:support-onetbb
Jan 29, 2021
Merged

core, gapi: supported build with oneTBB 2021#19384
opencv-pushbot merged 2 commits intoopencv:masterfrom
mshabunin:support-onetbb

Conversation

@mshabunin
Copy link
Copy Markdown
Contributor

@mshabunin mshabunin commented Jan 24, 2021

resolves #19358

Build Instruction
cmake -DWITH_TBB=ON -DTBB_DIR=<oneTBB>/lib/cmake/tbb ../opencv
# or
TBBROOT=<oneTBB> cmake -DWITH_TBB=ON ../opencv

Sourcing env.sh is not required.

Notes
  • Disabled oneTBB support in gapi module because of deprecated/removed tbb::task interface being used
  • Our code relies on TBB_INTERFACE_VER macro defined in tbb_stddef.h to distinguish between versions, but this file has been removed in favor of new version.hpp file. Thus we had to add our own definitions provided via cmake to be able to include correct header: OPENCV_TBB_VERSION_{MAJOR,MINOR}. These two can be replaced with single TBB_USE_VERSION_H definition for better clarity and compactness.
  • Tested build and smoke test with several official TBB GH releases: 2019-2021 (on Linux)

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake
force_builders=Custom
build_image:Custom=ubuntu:20.04
allow_multiple_commits=1

@h6197627
Copy link
Copy Markdown
Contributor

h6197627 commented Jan 24, 2021

Also, if possible, I would like to request to fix old problem with TBB (that is also related to oneAPI updates).
When configuring MKL LAPACK to use TBB threading -D MKL_WITH_TBB=ON, TBB search path is wrong for TBB distributions installed from previous Intel Performance Libraries and newer oneAPI distributions
https://github.com/opencv/opencv/blob/master/cmake/OpenCVFindMKL.cmake#L121-L125
In these distributions libraries are placed in ${MKL_ROOT_DIR}/../tbb/lib/${MKL_ARCH}/gcc4.* directories, and without this gcc4.* suffix it fails to find libtbb.so (I am no sure about TBB build from source, maybe there is no additional gcc4.* directories) and fails to configure MKL with TBB threading.

Problem that is directly related to oneAPI TBB is that with this TBB version search path needs to be adjusted
${MKL_ROOT_DIR}/../../tbb/latest/lib/${MKL_ARCH}
(gcc4.* problem is also exists for oneAPI)

@mshabunin
Copy link
Copy Markdown
Contributor Author

@h6197627 , is tbb actually required to be linked to use MKL TBB backend? As I can see for dynamic libraries it is not needed. Are configurations with static MKL and TBB really exist?

@h6197627
Copy link
Copy Markdown
Contributor

h6197627 commented Jan 24, 2021

@mshabunin , I have OpenCV built with TBB and MKL with TBB so I can't say for sure that TBB linkage is not necessary for -D WITH_TBB=OFF -D MKL_WITH_TBB=ON. It might be that this check is redundant.
Interesting that:

ldd libmkl_tbb_thread.so 
        linux-vdso.so.1 (0x00007ffc71fa5000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f2147a1f000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f2147800000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f214740f000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f214972f000)

it is not linked to tbb library (although, when installing TBB threading support component of MKL distribution it installs libtbb.so, so I guess it is not statically linked into libmkl_tbb_thread.so), so it probably uses some sort of runtime dispatch.

Considering, static linkage, I am completely not aware of such usage (although libmkl_tbb_thread.a library exists).

#include "tbb/tbb.h"
#include "tbb/task.h"
#if defined(OPENCV_TBB_VERSION_MAJOR) && OPENCV_TBB_VERSION_MAJOR >= 2021
#include "tbb/version.h"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be detected through try_compile() or checking if(EXISTS ...) / TBB_VER_FILE.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Perhaps EXIST check would be the best. I'll update the PR.

Copy link
Copy Markdown
Member

@alalek alalek Jan 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we don't these conditional includes at all.
tbb/version.h or tbb/tbb_stddef.h is included by tbb/tbb.h or by tbb/task.h:


P.S. TBB team is not able to work properly with Git history (too many branches are created from clean repo state with lost history)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removal of these includes works. Tested even on Ubuntu 14.04 with "TBB (ver 4.2 interface 7000)"

@mshabunin mshabunin force-pushed the support-onetbb branch 2 times, most recently from fde32fb to eaf469b Compare January 26, 2021 13:16
@mshabunin
Copy link
Copy Markdown
Contributor Author

@alalek , I've updated macros mechanism as you suggested.

@h6197627 , I've removed tbb from MKL-TBB dependencies list. Still not sure if it is required, builds with oneAPI without issues (-DWITH_TBB=OFF -DMKL_WITH_TBB=ON). My version of MKL-TBB shows different dependencies list:

$ objdump -p /opt/intel/oneapi/mkl/2021.1.1/lib/intel64/libmkl_tbb_thread.so | grep NEEDED
  NEEDED               libdl.so.2
  NEEDED               libtbb.so.12
  NEEDED               libstdc++.so.6
  NEEDED               libpthread.so.0

Please check on your side if possible.

@h6197627
Copy link
Copy Markdown
Contributor

@mshabunin ,
interestingly 2021.1.1 has tbb linkage

readelf -d libmkl_tbb_thread.so | grep NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libtbb.so.12]
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]

and 2020.1.217 (which I posted before) does not:

readelf -d libmkl_tbb_thread.so | grep NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]

#include "gtbbexecutor.hpp"

#if defined(HAVE_TBB)
#if defined(HAVE_TBB) && !defined(OPENCV_TBB_USE_VERSION_H)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to include tbb.h / tbb/task.h and then check TBB_INTERFACE_VERSION < XYZ instead.

Perhaps XYZ should be < 12000, because -DWITH_TBB=ON -DBUILD_TBB=ON uses 2020.2 with interface 11102 and gapi builds succesfully.


BTW, Please add a comment which API is not available.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

set(mkl_lib_list "mkl_intel_${MKL_ARCH_SUFFIX}")

if(MKL_WITH_TBB)
list(APPEND mkl_lib_list mkl_tbb_thread tbb)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets allow to add custom libraries below:

list(APPEND MKL_LIBRARIES ${OPENCV_EXTRA_MKL_LIBRARIES})

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@alalek
Copy link
Copy Markdown
Member

alalek commented Jan 29, 2021

Validated with:

  • Intel oneAPI Base Toolkit 2021.1 (on Linux):
. /opt/intel/oneapi/setvars.sh
cmake ../opencv -DWITH_TBB=ON -DCMAKE_DISABLE_FIND_PACKAGE_TBB=1

(CMAKE_DISABLE_FIND_PACKAGE_TBB=1 is necessary to bypass system's CMake scripts (which detects system's tbb) until we get TBBConfig.cmake in oneAPI distribution, probably this will be fixed in the future)

-- Found TBB (env): /opt/intel/oneapi/tbb/2021.1.1/lib/intel64/gcc4.8/libtbb.so
...
-- Found MKL 2021.0.1 at: /opt/intel/oneapi/mkl/latest
-- LAPACK(MKL): LAPACK_LIBRARIES: /opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_intel_lp64.so;/opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_sequential.so;/opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_core.so;/opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_intel_lp64.so;/opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_sequential.so;/opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_core.so;/opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_intel_lp64.so;/opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_sequential.so;/opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_core.so;-lpthread;-lm;-ldl
  • MKL with TBB case:
cmake ../opencv -DWITH_TBB=ON -DCMAKE_DISABLE_FIND_PACKAGE_TBB=1 -DMKL_WITH_TBB=ON
...
-- Found MKL 2021.0.1 at: /opt/intel/oneapi/mkl/latest
-- LAPACK(MKL): LAPACK_LIBRARIES: /opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_intel_lp64.so;/opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_tbb_thread.so;/opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_core.so;/opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_intel_lp64.so;/opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_tbb_thread.so;/opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_core.so;/opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_intel_lp64.so;/opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_tbb_thread.so;/opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_core.so;-lpthread;-lm;-ldl
-- LAPACK(MKL): Support is enabled.

@dmatveev
Copy link
Copy Markdown
Contributor

dmatveev commented Feb 8, 2021

Wow, thanks folks!

@dizcza
Copy link
Copy Markdown
Contributor

dizcza commented Mar 30, 2023

Please back-port the fix for OpenCV v3.x
I'm cherry-picking your changes...
Thanks

@mshabunin
Copy link
Copy Markdown
Contributor Author

@dizcza , if you already have cherry-picked commit for 3.4 which you've tested with recent oneTBB, then you can propose it in a PR. It might be faster this way.

@asmorkalov asmorkalov removed the backport is needed Label for maintainers. Authors of PR can ignore this label Jun 16, 2023
@asmorkalov asmorkalov added the port/backport done Label for maintainers. Authors of PR can ignore this label Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Intel OneApi TBB does not have tbb_stddef.h anymore

7 participants