How to execute longest tasks first with TBB

237 Views Asked by At

I have 10000 tasks that I am trying to schedule with tbb across N threads. 9900 tasks take O(1) unit time for execution whereas the remaining 100 tasks take O(100)-O(1000) time for execution. I want tbb to schedule these tasks such that the top 100 longest tasks are scheduled first on the threads, so that we maximize efficiency. If some threads finish faster, they can then run the shorter jobs at the end.

I have one (hypothetical) example: Out of 10000 tasks, I have one super long task which takes 1111 units, remaining 9999 tasks all take just 1 unit, and I have 10 threads. I want thread j to run this super long task in 1111 units of time, and the other 9 threads run the remaining 9999 tasks which take 1 unit each so those 9 threads run 9999/9=1111 tasks in 1111 units of time. Which means I am using my threads at 100% efficiency (ignore any overhead).

What I have is a function which does something like this:

bool run( Worker& worker, size_t numTasks, WorkerData& workerData ) {
    xtbbTaskData<Worker,WorkerData> taskData( worker, workerData, numThreads);
    arena.execute( [&]{ tbb::parallel_for( tbb::blocked_range<size_t>( 0, numTasks ), xtbbTask<Worker,WorkerData>( taskData ) ); } );
}

where I have a xtbb::arena arena created with numThreads. Worker is my worker class with 10000 tasks, workerData is the class with the data needed to run each task, and I have a template class xtbbTaskData which takes the worker, workerdata and eventually has the operator() which calls run on each task in the worker.

What syntax should I use to schedule these tasks such that the longest task gets schedules first? There is task priority, tbb area, enque etc that I have come across but I am not finding good examples of how to code this.

Do I need to create multiple arenas? Or multiple workers? Or put the longest tasks at the end of the vector in worker? Or something else?

If someone can point me to examples or templates that are already doing this, that would be great.

1

There are 1 best solutions below

0
Noorjahan - Intel On

A task_group_context represents a group of tasks that can be canceled, or have their priority level set, together.

Refer to page no: 378 in the Pro TBB C++ Parallel Programming with Threading Building Blocks textbook for examples.

We can also define priority as an attribute to the task arena.

Refer to page no: 494 in the Pro TBB C++ Parallel Programming with Threading Building Blocks textbook for example.