Repository: pytorch
Issue: build: failure when upgrade oneTBB to 2021.7.0
Labels: module: build,triaged,module: tbb

### 🐛 Describe the bug

Currently third_party/tbb uses an old `tbb_2018` branch by https://github.com/pytorch/pytorch/pull/20454.

Upgrading to a later branch `onetbb_2021` and building pytorch from source causes few errors. 

Run the following to reproduce the failure with updated tbb branch:
`MKLDNN_CPU_RUNTIME=TBB MKL_THREADING=TBB ATEN_THREADING=TBB USE_TBB=1 USE_OPENMP=0 python setup.py bdist_wheel`


1.  changes in file locations in  `onetbb_2021` branch
```
CMake Error at aten/src/ATen/cpu/tbb/CMakeLists.txt:268 (add_library):
  Cannot find source file:
<VIRTUAL_ENV>/pytorch/third_party/tbb/src/rml/client/rml_tbb.cpp

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .h
  .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc


CMake Error in aten/src/ATen/cpu/tbb/CMakeLists.txt:
  Cannot find source file:
<VIRTUAL_ENV>/pytorch/third_party/tbb/src/tbb/lin64-tbb-export.def

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .h
  .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc


CMake Error in aten/src/ATen/cpu/tbb/CMakeLists.txt:
  Cannot find source file: 
<VIRTUAL_ENV>/pytorch/third_party/tbb/src/tbbmalloc/lin64-tbbmalloc-export.def

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .h
  .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc
```

2. linkage error to tbb

The error occurs if mismatched versions of `libmkl_tbb_thread.so` and `libtbb.so`.  The cause of error such as `undefined symbol: _ZN3tbb4task13note_affinityEt (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)` is that the symbol `_ZN3tbb4task13note_affinityEt` is required in `libmkl_tbb_thread.so (MKL 2018)` but not required in `libmkl_tbb_thread.so.2 (MKL 2022)`.  The symbol `_ZN3tbb4task13note_affinityEt`  is defined in `libtbb.so (TBB 2018)` [here](https://github.com/oneapi-src/oneTBB/blob/a51a90bc609bb73db8ea13841b5cf7aa4344d4a9/src/tbb/lin64-tbb-export.lst) but not defined in `libtbb.so (TBB 2021)` [here](https://github.com/oneapi-src/oneTBB/blob/onetbb_2021/src/tbb/def/lin64-tbb.def). So the error does not occur with `libmkl_tbb_thread.so.2 (MKL 2022)`. But regardless, it may be better to add `-ltbb` linker flag rather than requiring specific MKL version for TBB. 

```
pytorch/build/lib$ ldd -r libtorch_cpu.so
        linux-vdso.so.1 (0x00007fffd2ff7000)
        libc10.so => <VIRTUAL_ENV>/pytorch/build/lib/libc10.so (0x00007fe226965000)
        libnuma.so.1 => /usr/lib/x86_64-linux-gnu/libnuma.so.1 (0x00007fe22675a000)
        librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fe226552000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fe22633a000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fe226136000)
        libmkl_intel_lp64.so => /opt/intel/mkl/lib/intel64/libmkl_intel_lp64.so (0x00007fe225649000)
        libmkl_tbb_thread.so => /opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so (0x00007fe223eff000)
        libmkl_core.so => /opt/intel/mkl/lib/intel64/libmkl_core.so (0x00007fe21fef6000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fe21fcd7000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fe21f939000)
        libtbb.so => <VIRTUAL_ENV>/pytorch/build/lib/libtbb.so (0x00007fe21f6f4000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fe21f2e7000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe21eef6000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fe232acb000)
undefined symbol: _ZN3tbb4task13note_affinityEt (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb18task_group_context4initEv    (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb19task_scheduler_init9terminateEv      (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal24concurrent_queue_base_v3D2Ev        (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb13queuing_mutex11scoped_lock7releaseEv (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZNK3tbb8internal20allocate_child_proxy8allocateEm    (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal28affinity_partitioner_base_v36resizeEj       (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb10interface58internal9task_base7destroyERNS_4taskE     (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal19allocate_root_proxy8allocateEm      (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal25concurrent_vector_base_v316internal_grow_byEmmPFvPvPKvmES4_ (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal24concurrent_queue_base_v818internal_push_moveEPKv    (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZNK3tbb8internal27allocate_continuation_proxy8allocateEm     (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal24concurrent_queue_base_v312internal_popEPv   (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal25deallocate_via_handler_v3EPv        (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb13queuing_mutex11scoped_lock7acquireERS0_      (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZNK3tbb8internal34allocate_additional_child_of_proxy8allocateEm      (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZNK3tbb8internal32allocate_root_with_context_proxy8allocateEm        (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal18throw_exception_v4ENS0_12exception_idE      (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal8NFS_FreeEPv  (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb19task_scheduler_init10initializeEim   (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb18task_group_contextD1Ev       (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZNK3tbb8internal24concurrent_queue_base_v314internal_emptyEv (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb4task22spawn_and_wait_for_allERNS_9task_listE  (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal23allocate_via_handler_v3Em   (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb19task_scheduler_init19default_num_threadsEv   (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal24concurrent_queue_base_v3C2Em        (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal24concurrent_queue_base_v313internal_pushEPKv (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal25concurrent_vector_base_v314internal_clearEPFvPvmE   (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal36get_initial_auto_partitioner_divisorEv      (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal19critical_section_v418internal_constructEv   (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb10interface78internal15task_arena_base21internal_current_slotEv        (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal24concurrent_queue_base_v323internal_pop_if_presentEPv        (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal25concurrent_vector_base_v3D2Ev       (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal16thread_get_id_v3Ev  (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZNK3tbb18task_group_context28is_group_execution_cancelledEv  (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal24concurrent_queue_base_v321internal_finish_clearEv   (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
undefined symbol: _ZN3tbb8internal12NFS_AllocateEmmPv   (/opt/intel/mkl/lib/intel64/libmkl_tbb_thread.so)
```


### Versions

PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.6 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~18.04) 9.4.0
Clang version: Could not collect
CMake version: version 3.24.1
Libc version: glibc-2.27

Python version: 3.8.13 (default, Mar 28 2022, 11:38:47)  [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-4.15.0-144-generic-x86_64-with-glibc2.17
Is CUDA available: N/A
CUDA runtime version: 9.1.85
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: N/A


cc @malfet @seemethere