Simplest TBB example
Asked Answered
P

4

22

Can someone give me a TBB example how to:

  1. set the maximum count of active threads.
  2. execute tasks that are independent from each others and presented in the form of class, not static functions.
Phillane answered 15/5, 2012 at 19:6 Comment(0)
D
31

Here's a couple of complete examples, one using parallel_for, the other using parallel_for_each.

Update 2014-04-12: These show what I'd consider to be a pretty old fashioned way of using TBB now; I've added a separate answer using parallel_for with a C++11 lambda.

#include "tbb/blocked_range.h"
#include "tbb/parallel_for.h"
#include "tbb/task_scheduler_init.h"
#include <iostream>
#include <vector>

struct mytask {
  mytask(size_t n)
    :_n(n)
  {}
  void operator()() {
    for (int i=0;i<1000000;++i) {}  // Deliberately run slow
    std::cerr << "[" << _n << "]";
  }
  size_t _n;
};

struct executor
{
  executor(std::vector<mytask>& t)
    :_tasks(t)
  {}
  executor(executor& e,tbb::split)
    :_tasks(e._tasks)
  {}

  void operator()(const tbb::blocked_range<size_t>& r) const {
    for (size_t i=r.begin();i!=r.end();++i)
      _tasks[i]();
  }

  std::vector<mytask>& _tasks;
};

int main(int,char**) {

  tbb::task_scheduler_init init;  // Automatic number of threads
  // tbb::task_scheduler_init init(2);  // Explicit number of threads

  std::vector<mytask> tasks;
  for (int i=0;i<1000;++i)
    tasks.push_back(mytask(i));

  executor exec(tasks);
  tbb::parallel_for(tbb::blocked_range<size_t>(0,tasks.size()),exec);
  std::cerr << std::endl;

  return 0;
}

and

#include "tbb/parallel_for_each.h"
#include "tbb/task_scheduler_init.h"
#include <iostream>
#include <vector>

struct mytask {
  mytask(size_t n)
    :_n(n)
  {}
  void operator()() {
    for (int i=0;i<1000000;++i) {}  // Deliberately run slow
    std::cerr << "[" << _n << "]";
  }
  size_t _n;
};

template <typename T> struct invoker {
  void operator()(T& it) const {it();}
};

int main(int,char**) {

  tbb::task_scheduler_init init;  // Automatic number of threads
  // tbb::task_scheduler_init init(4);  // Explicit number of threads

  std::vector<mytask> tasks;
  for (int i=0;i<1000;++i)
    tasks.push_back(mytask(i));

  tbb::parallel_for_each(tasks.begin(),tasks.end(),invoker<mytask>());
  std::cerr << std::endl;

  return 0;
}

Both compile on a Debian/Wheezy (g++ 4.7) system with g++ tbb_example.cpp -ltbb (then run with ./a.out)

(See this question for replacing that "invoker" thing with a std::mem_fun_ref or boost::bind).

Diffractive answered 15/5, 2012 at 22:39 Comment(1)
Note that tbb::task_scheduler_init was removed by Intel in oneAPI/oneTBB on GitHub. oneapi::tbb::global_control c(oneapi::tbb::global_control::max_allowed_parallelism, 6); seems to be "the new way".Bili
D
22

Here's a more modern use of parallel_for with a lambda; compiles and runs on Debian/Wheezy with g++ -std=c++11 tbb_example.cpp -ltbb && ./a.out:

#include "tbb/parallel_for.h"
#include "tbb/task_scheduler_init.h"
#include <iostream>
#include <vector>

struct mytask {
  mytask(size_t n)
    :_n(n)
  {}
  void operator()() {
    for (int i=0;i<1000000;++i) {}  // Deliberately run slow
    std::cerr << "[" << _n << "]";
  }
  size_t _n;
};

int main(int,char**) {

  //tbb::task_scheduler_init init;  // Automatic number of threads
  tbb::task_scheduler_init init(tbb::task_scheduler_init::default_num_threads());  // Explicit number of threads

  std::vector<mytask> tasks;
  for (int i=0;i<1000;++i)
    tasks.push_back(mytask(i));

  tbb::parallel_for(
    tbb::blocked_range<size_t>(0,tasks.size()),
    [&tasks](const tbb::blocked_range<size_t>& r) {
      for (size_t i=r.begin();i<r.end();++i) tasks[i]();
    }
  );

  std::cerr << std::endl;

  return 0;
}
Diffractive answered 12/4, 2014 at 8:4 Comment(0)
N
7

If you just want to run a couple of tasks concurrently, it might be easier to just use a tbb::task_group. Example taken from tbb:

#include "tbb/task_group.h"  

using namespace tbb;

int Fib(int n) {
    if( n<2 ) {
        return n;
    } else {
        int x, y;
        task_group g;
        g.run([&]{x=Fib(n-1);}); // spawn a task
        g.run([&]{y=Fib(n-2);}); // spawn another task
        g.wait();                // wait for both tasks to complete
        return x+y;
    }
}

Note however that

Creating a large number of tasks for a single task_group is not scalable, because task creation becomes a serial bottleneck.

In those cases, use timday's examples with a parallel_for or alike.

Nazarite answered 22/4, 2016 at 0:45 Comment(0)
M
1

1-

 //!
 //! Get the default number of threads
 //!
 int nDefThreads = tbb::task_scheduler_init::default_num_threads();

 //!
 //! Init the task scheduler with the wanted number of threads
 //!
 tbb::task_scheduler_init init(nDefThreads);

2-

Maybe if your code permits, the best way to run independent task with TBB is the parallel_invoke. In the blog of intel developers zone there is a post explaining some cases of how helpfull parallel_invoke could be. Check out this

Murdoch answered 12/8, 2014 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.