Intel TBB on Raspberry Pi

the impossible code - TBB on Raspberry Pi

I absolutely love Intel TBB. I also love the raspberry pi, and use it for work and hobby. Using TBB on raspberry pi and apparently using TBB on ARM in general is not as hard as it seems.

I believe Intel TBB to be the most valuable multi-threaded tool in one’s arsenal as a C++ developer. That’s not to say that you don’t need to understand threads, synchronization tools, deadlocks and MT pitfalls, but the workflow concepts, encapsulations and optimizations given to you for free with TBB are outstanding.

Having said that, I was very disappointed to find out that raspbian does not include a package for TBB, especially since the Pi2 & Pi3 have multi core processors. As it turns out TBB won’t build easily enough on ARM, but that’s not to say that it can’t be done, as we’ll soon see.

IMPORTANT UPDATES (17.07.2021):

  1. There is a tbb package for raspberry pi for some time now, simply install it for development with:sudo apt-get install libtbb-dev
  2. The opencv in the official packages is already built with tbb and depends on it (the runtime package is called libtbb2).

So how do you build a TBB package for Raspberry Pi?

I recently needed to build TBB for a Pi3 project I was working on. The project required a proper installation of the package – libraries, headers and pkgconfig setup, and I needed to provide it to a customer for installation – so I also created a package for it (yes – you can just take the prebuilt package – link at the bottom). Follow these instructions carefully, mostly using copy paste into a Raspberry Pi terminal. If you’re having problems then see troubleshooting at the end.

Package preparation

Get and unpack the source
cd ~/Downloads
wget https://www.threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb44_20160526oss_src_0.tgz
tar xvf tbb44_20160526oss_src_0.tgz
Build it
cd tbb44_20160526oss
make tbb CXXFLAGS="-DTBB_USE_GCC_BUILTINS=1 -D__TBB_64BIT_ATOMICS=0"
Prepare the package structure

Prepares package structure:

cd ~/Downloads
mkdir libtbb-dev_4.5-1_armhf
cd libtbb-dev_4.5-1_armhf
mkdir -p usr/local/lib/pkgconfig
mkdir -p usr/local/include
mkdir DEBIAN

Insert the description (the ^D at the end here is a CTRL-D to end the ‘cat’):

cd ~/Downloads/libtbb-dev_4.5-1_armhf/DEBIAN
cat > control
Package: libtbb-dev
Priority: extra
Section: universe/libdevel
Maintainer: Your Name <email address>
Architecture: armhf
Version: 4.5-1
Homepage: http://threadingbuildingblocks.org/
Description: parallelism library for C++ - development files
 TBB is a library that helps you leverage multi-core processor
 performance without having to be a threading expert. It represents a
 higher-level, task-based parallelism that abstracts platform details
 and threading mechanism for performance and scalability.
 .
 (Note: if you are a user of the i386 architecture, i.e., 32-bit Intel
 or compatible hardware, this package only supports Pentium4-compatible
 and higher processors.)
 .
 This package includes the TBB headers, libs and pkg-config
^D

Insert the library:

cd ~/Downloads/libtbb-dev_4.5-1_armhf/usr/local/lib
cp ~/Downloads/tbb44_20160526oss/build/*_release/libtbb.so.2 .
ln -s libtbb.so.2 libtbb.so

Insert the headers:

cd ~/Downloads/tbb44_20160526oss/include
cp -r serial tbb ~/Downloads/libtbb-dev_4.5-1_armhf/usr/local/include

Insert pkgconfig (the ^D at the end here is a CTRL-D to end the ‘cat’):

cd ~/Downloads/libtbb-dev_4.5-1_armhf/usr/local/lib/pkgconfig
cat > tbb.pc
# Manually added pkg-config file for tbb - START
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Name: tbb
Description: thread building block
Version: 4.4.5
Cflags: -I${includedir} -DTBB_USE_GCC_BUILTINS=1 -D__TBB_64BIT_ATOMICS=0
Libs: -L${libdir} -ltbb
# Manually added pkg-config file for tbb - END
^D

Build the package, with correct ownership:

cd ~/Downloads
sudo chown -R root:staff libtbb-dev_4.5-1_armhf
sudo dpkg-deb --build libtbb-dev_4.5-1_armhf

Install the package

sudo dpkg -i ~/Downloads/libtbb-dev_4.5-1_armhf.deb
sudo ldconfig

Note that instead of running ldconfig manually you can add DEBIAN/postinst (and DEBIAN/postrm), but it is somewhat of an overkill for this specific post.

Test the package

cd /tmp
cat > hello_world.cpp
#include "tbb/tbb.h"
#include <iostream>
using namespace tbb;
using namespace std;

class first_task : public task { 
 public: 
 task* execute( ) { 
 cout << "Hello World!\n";
 return NULL;
 }
};

int main( )
{ 
 task_scheduler_init init(task_scheduler_init::automatic);
 first_task& f1 = *new(tbb::task::allocate_root()) first_task( );
 tbb::task::spawn_root_and_wait(f1);
}
^D
g++ hello_world.cpp `pkg-config tbb --cflags --libs`
./a.out

Troubleshooting

See the successful package creation and installation:

Final notes

That’s all for today.

See you soon in an upcoming post.

 

Sagi Zeevi

20 years writing code and still loving it!

You may also like...

29 Responses

  1. Noga says:

    Thanks for the very useful post!

  2. sagi says:

    Thank you for your kind words 🙂

  3. hsiehtl says:

    it have a step error happened.
    “Insert the headers: ”
    cd ~/Downloads/tbb44_20160526oss/include
    cp -r serial tbb ~/Downloads/libtbb-dev_4.5-1_armhf/usr/local/include

    The folder “~/Downloads/tbb44_20160526oss/include” does not have any file can copy.

    • hsiehtl says:

      I found the problem.
      I type a wrong direction.
      Right now already fixed this problem.

      But after I finished the step of c++ file compile, run the ./a.out, it show a message “error while loading shared libraries: libtbb.so.2: cannot open shared object file: No such file or directory”

      Do you have any suggestion??

      • sagi says:

        Yes.
        Try ‘sudo ldconfig’

      • hsiehtl says:

        I found the problem.
        After install deb file, then need run the “ldconfig” to link the file.
        After “ldconfig” command, then run the “a.out”, it will show the “Hello world”..

        Right now I am going to install the OpenCV…

        Thanks~~

  4. hsiehtl says:

    Hi, Sage,

    I had try to install OpenCV3.0.0 with this version TBB, but got a problem.
    When I used “make” commend to compile the OpenCV, it gave me 2 errors , all are “#error compilation requires an ARMv7-a architecture.”

    1st error : /usr/local/include/tbb/machine/gcc_armv7.h:31:2: error compilation requires an ARMv7-a architecture.
    2nd error: [modules/core/CMakeFiles/opencv_core_pch_dephelp.dir/all] Error 2

    I had add (make tbb CXXFLAGS=”-DTBB_USE_GCC_BUILTINS=1 -D__TBB_64BIT_ATOMICS=0″) when compile tbb.
    And had “source tbbvars.sh” to sync the bash.
    But still fail when compile Opencv 3.0.0

    After I add -D BUILD_TBB=ON in Opencv cmake commend, then the compiler will download the TBB41_20130116oss and compile continuously without above errors.

    Do you have any idea with this error???

    • Sagi Zeevi says:

      BTW – I am using OpenCV3.1.0, but not sure this matters.
      Don’t download TBB and don’t build it from OpenCV and don’t source tbbvars.sh.
      Just add to the opencv cmake -DCMAKE_CXX_FLAGS=”-DTBB_USE_GCC_BUILTINS=1 -D__TBB_64BIT_ATOMICS=0″

  5. hsiehtl says:

    So, you mean the cmake commend of OpenCV should change to :

    $ cd opencv-3.0.0\build
    $ cmake -DCMAKE_CXX_FLAGS=”-DTBB_USE_GCC_BUILTINS=1 -D__TBB_64BIT_ATOMICS=0″ \
    -D WITH_TBB:BOOL=TRUE \
    -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
    -D BUILD_EXAMPLES=ON ..

    • Sagi Zeevi says:

      I can verify the correctness of the part related to TBB, which is
      -DWITH_TBB=ON -DCMAKE_CXX_FLAGS=”-DTBB_USE_GCC_BUILTINS=1 -D__TBB_64BIT_ATOMICS=0″

      The rest depends on your needs and local setup.

  6. hsiehtl says:

    Hi, Sagi,

    Follow your commend, I install TBB and OpenCV 3.1.0 success.

    Thanks for your suggestion.

  7. Sagi Zeevi says:

    See how to build a 30% faster OpenCV for Raspberry Pi3 here – https://www.theimpossiblecode.com/blog/build-faster-opencv-raspberry-pi3

  8. Do you want to contribute it back to TBB? ARM is supported by community there. And the pkgconfig and stuff would be appreciated by many as well.

    • Sagi Zeevi says:

      Actually this seems specific to raspbian packaging on Raspberry Pi. Regardless – feel free to contribute this wherever you think is relevant 🙂

  9. bdk says:

    I’m having this error:
    pi@rpi1:~/Downloads $ sudo dpkg-deb –build libtbb-dev_4.5-1_armhf
    dpkg-deb: error: parsing file ‘libtbb-dev_4.5-1_armhf/DEBIAN/control’ near line 0:
    EOF during value of field `Package’ (missing final newline)

    I tried adding a line at the end of the control file and it didn’t work. I thought perhaps I screwed up somewhere so I re-did everything but I still got the same error.
    Any ideas what is might be? Thanks 🙂

    • Sagi Zeevi says:

      Please copy-paste in a reply the output of:
      cat ~/Downloads/libtbb-dev_4.5-1_armhf/DEBIAN/control

      • bdk says:

        I’m getting this output:

        pi@rpi2:~ $ cat ~/Downloads/libtbb-dev_4.5-1_armhf/DEBIAN/control
        This package includes the TBB headers, libs and pkg-configpi@rpi2:~ $

        • Sagi Zeevi says:

          Not sure how you came to have this content for the control file. This should be the content of the file:
          Package: libtbb-dev
          Priority: extra
          Section: universe/libdevel
          Maintainer: Your Name
          Architecture: armhf
          Version: 4.5-1
          Homepage: http://threadingbuildingblocks.org/
          Description: parallelism library for C++ – development files
          TBB is a library that helps you leverage multi-core processor
          performance without having to be a threading expert. It represents a
          higher-level, task-based parallelism that abstracts platform details
          and threading mechanism for performance and scalability.
          .
          (Note: if you are a user of the i386 architecture, i.e., 32-bit Intel
          or compatible hardware, this package only supports Pentium4-compatible
          and higher processors.)
          .
          This package includes the TBB headers, libs and pkg-config

  10. bdk says:

    I managed to solve the issue. Instead of using the cat to create and write the control file, I used nano and pasted the contents manually. Then I used cat to verify that everything was as it was supposed to.
    Now I manged to build the package without problems 😀

    Another newbie question, would this guide also work for creating a package for ubuntu since it’s debian-based?

    Thanks you for the help 😀

  11. Sagi Zeevi says:

    Glad it works now 🙂

    Ubuntu should have this specific package already (at least for intel CPUs).
    In general this guide will work for building a package for debian based systems give or take some specifics you might want to do differently for the source you are building. However, if the source you are building has a decent ‘make install’, then I prefer to use ‘checkinstall’ (sudo apt-get install checkinstall) to create the package, since it automates all of the work.

  12. Nadeesh Sampath says:

    This was absolutely amazing. I wasted a week trying to install opencv with tbb. Thank you very much for this tutorial and make even more 😀

  13. tw39124 says:

    Hi Sagi,

    Thank you for this great package. You’ve put a lot of work in and saved other people time and hassle!

    Will the pre-built .deb file also install and work on the RPi 2?

  14. Sagi Zeevi says:

    Hi.
    You’re welcome.
    You should build it yourself for RPi 2, but It is a simple copy-paste from this page as you can see in the YouTube video above.
    This copy-paste procedure was also tested on RPi 2, so at the end of the copy paste you should have a working package.

  15. Kev says:

    Any luck building TBB for ARMv6 (for RasPi Zero, etc)?

  1. December 1, 2016

    […] This simplest way to accomplish this optimization, of course, is to simply reduce what’s running down to the essentials. For example, there’s no sense in running a GUI if your project doesn’t even use a display. Another strategy, however, is to ensure that you’re actually using all of the available processing power that the Raspberry Pi offers. In [sagiz’s] case, that meant using Intel’s open source Threading Building Blocks to achieve better parallelism in his OpenCV project. […]

  2. December 2, 2016

    […] This simplest way to accomplish this optimization, of course, is to simply reduce what’s running down to the essentials. For example, there’s no sense in running a GUI if your project doesn’t even use a display. Another strategy, however, is to ensure that you’re actually using all of the available processing power that the Raspberry Pi offers. In [sagiz’s] case, that meant using Intel’s open source Threading Building Blocks to achieve better parallelism in his OpenCV project. […]

  3. December 8, 2016

    […] already followed this post – Intel TBB on Raspberry Pi. (Optional for reasons I’ll detail later on, but I’m showing below how to build with […]

Scroll Up