Next Steps
After installing oneTBB, complete the following steps to start working with the library.
Set the Environment Variables
After installing oneTBB, set the environment variables:
For stand-alone oneTBB package, run:
Linux* OS
source <install_dir>/tbb/latest/env/vars.sh
Or:
source <install_dir>/setvars.sh
Windows* OS
source <install_dir>/tbb/latest/env/vars.bat
Or:
source <install_dir>/setvars.bat
TIP:oneTBB can coordinate with Intel(R) OpenMP on CPU resources usage to avoid excessive oversubscription when both runtimes are used within a process. To enable this feature set up TCM_ENABLE environment variable to 1For oneTBB installed as part of Intel® oneAPI Base Toolkit, see:
For Linux* OS: Use the setvars and oneapi-vars Scripts with Linux*
For Windows* OS: Use the setvars and oneapi-vars Scripts with Windows*
Build and Run a Sample
Windows* OS
Create a new C++ project using your IDE. In this example, Microsoft* Visual Studio* Code is used.
Create an example.cpp file in the project.
Copy and paste the code below. It is a typical example of a oneTBB algorithm. The sample calculates a sum of all integer numbers from 1 to 100.
#include <oneapi/tbb/tbb.h> int sum = oneapi::tbb::parallel_reduce( oneapi::tbb::blocked_range<int>(1,101), 0, [](oneapi::tbb::blocked_range<int> const& r, int init) -> int { for (int v = r.begin(); v != r.end(); v++) { init += v; } return init; }, [](int lhs, int rhs) -> int { return lhs + rhs; } ); printf("Sum: %d\n", sum); return 0; }
Open the tasks.json file in the .vscode directory and paste the following lines to the args array:
-Ipath/to/oneTBB/include to add oneTBB include directory.
path/to/oneTBB/ to add oneTBB.
For example:
{ "tasks": [ { "label": "build & run", "type": "cppbuild", "args": [ "/IC:\\Program Files (x86)\\Intel\\oneAPI\\tbb\\latest\\include", "C:\\Program Files (x86)\\Intel\\oneAPI\\tbb\\latest\\lib\\tbb12.lib"
Build the project.
Run the example.
If oneTBB is configured correctly, the output displays Sum: 5050.
Linux* OS
Create an example.cpp file in the project.
Copy and paste the code below. It is a typical example of a oneTBB algorithm. The sample calculates a sum of all integer numbers from 1 to 100.
#include <oneapi/tbb/tbb.h> int sum = oneapi::tbb::parallel_reduce( oneapi::tbb::blocked_range<int>(1,101), 0, [](oneapi::tbb::blocked_range<int> const& r, int init) -> int { for (int v = r.begin(); v != r.end(); v++) { init += v; } return init; }, [](int lhs, int rhs) -> int { return lhs + rhs; } ); printf("Sum: %d\n", sum); return 0; }
Compile the code using oneTBB. For example,
g++ -std=c++11 example.cpp -o example -ltbb
Run the executable:
./example
If oneTBB is configured correctly, the output displays Sum: 5050.
Hybrid CPU and NUMA Support
If you need NUMA/Hybrid CPU support in oneTBB, you need to make sure that HWLOC* is installed on your system.
HWLOC* (Hardware Locality) is a library that provides a portable abstraction of the hierarchical topology of modern architectures (NUMA, hybrid CPU systems, etc). oneTBB relies on HWLOC* to identify the underlying topology of the system to optimize thread scheduling and memory allocation.
Without HWLOC*, oneTBB may not take advantage of NUMA/Hybrid CPU support. Therefore, it’s important to make sure that HWLOC* is installed before using oneTBB on such systems.
Check HWLOC* on the System
To check if HWLOC* is already installed on your system, run hwloc-ls:
For Linux* OS, in the command line.
For Windows* OS, in the command prompt.
If HWLOC* is installed, the command displays information about the hardware topology of your system. If it is not installed, you receive an error message saying that the command hwloc-ls could not be found.
Install HWLOC*
To install HWLOC*, visit the official Portable Hardware Locality website (https://www-lb.open-mpi.org/projects/hwloc/).
For Windows* OS, binaries are available for download.
For Linux* OS, only the source code is provided and binaries should be built.
On Linux* OS, HWLOC* can be also installed with package managers, such as APT*, YUM*, etc. To do so, run: sudo apt install hwloc.
Upgrading oneTBB
The seamless upgrade is supported for oneTBB 2021.1 and later versions. To upgrade oneTBB to the latest version, run the setup once again.
Refer to Migrating from TBB for more information on migrating to oneTBB.