Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

equivalent openmp section in tbb??

mblizz
Beginner
115 Views
Need help.
What is the equivalent implementation of openmp's parallel section in tbb??
Can you cite an example??
I still dont know how to run two different tasks at the same time using tbb.
0 Kudos
1 Reply
ARCH_R_Intel
Employee
115 Views

A general solution is to use an empty task as a dummy parent. See http://software.intel.com/en-us/blogs/2008/07/02/implementing-task_group-interface-in-tbb for code that implements a general task group interface using this approach. It's on our to-do list to add something like this to TBB and add a general template for running a fixed number of functors concurrently.

For exposition, below is a simple example that runs two tasks concurrently.

#include "tbb/task_scheduler_init.h"
#include "tbb/task.h"
#include 

void DummyLoad() {
    for( volatile int x=0; x!=100000000; ++x )
        continue;
}

class X: public tbb::task {
    tbb::task* execute() {
        std::printf("X is runningn");
        DummyLoad();
        std::printf("X finished runningn");
        return NULL;
    }
};

class Y: public tbb::task {
    tbb::task* execute() {
        std::printf("Y is runningn");
        DummyLoad();
        std::printf("Y finished runningn");
        return NULL;
    }
};

int main() {
    tbb::task_scheduler_init init;
    tbb::empty_task* e = new( tbb::task::allocate_root() ) tbb::empty_task;
    // 3 = 1 for each child plus 1 for the wait
    e->set_ref_count(3);
    X* x = new( e->allocate_child() ) X;
    Y* y = new( e->allocate_child() ) Y;
    e->spawn(*x);
    e->spawn(*y);
    // Wait for childen to finish
    e->wait_for_all();
    e->destroy(*e);
    return 0;
}
Reply