TBB Introduction

A summary of tbb documentation

Posted by Zhang Jian on November 22, 2022

1. 简介

OneTBB Benifits

  1. oneTBB enables you to specify logical paralleism instead of threads.
  2. oneTBB targets threading for performance.
  3. oneTBB is compatible with other threading packages.
  4. oneTBB emphasizes scalable, data parallel programming.
  5. oneTBB relies on generic programming.

Testing Approach

几种在并行开发中常见的问题:

  1. Interface correspondence to specification
  2. Memory errors
  3. Data race
  4. Race conditions and deadlocks

其中比较复杂的是:Race conditions 和 deadlocks。可以使用下面的测试方法:

  1. Unit tests that, however, have limited capability to catch such errors
  2. Integration tests. Multiple different functionalities are heavily combined to emulate user use cases that may trigger such errors based on prior knowledge and expertise.
  3. Stress testing with different possible combinations. It ensures that even rarely triggered error conditions are caught by testing.

2. 使用

  1. How to install tbb
  2. oneTBB Samples
  3. oneTBB Developer Guide

安装

1
2
3
4
5
6
7
8
9
10
# Clone oneTBB repository
git clone https://github.com/oneapi-src/oneTBB.git
cd oneTBB
# Create binary directory for out-of-source build
mkdir build && cd build
# Configure: customize CMAKE_INSTALL_PREFIX and disable TBB_TEST to avoid tests build
# if you want to customize, you can config like that:
# cmake -DCMAKE_INSTALL_PREFIX=/tmp/my_installed_onetbb -DTBB_TEST=OFF ..
cmake .. && make -j$(nproc)
sudo make install

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <tbb/tbb.h>
#include <cstdio>

using namespace tbb;

int main()
{

    tick_count t0 = tick_count::now();
    parallel_for(0, 100, 1, [](int i) {
        printf("hello tbb %d \n", i);
    } );
    tick_count t1 = tick_count::now();

    printf("use time %f \n", (t1-t0).seconds());

    return 0;
}

CMakeLists.txt

1
2
3
4
5
6
cmake_minimum_required(VERSION 3.1)

project( TEST )
find_package( TBB REQUIRED)
add_executable( TEST_tbb test_tbb.cpp)
target_link_libraries( TEST_tbb TBB::tbb)

Results

图片名称

3. 参考

https://oneapi-src.github.io/oneTBB/