Scheduler bypass

Scheduler bypass is an optimization in which you directly specify the next task to run instead of letting the scheduler pick. Continuation-passing style often opens up an opportunity for scheduler bypass. For instance, in the continuation-passing example, it turns out that once FibTask::execute() returns, by the getting rules, task a is always the next task taken from the ready pool. Putting the task into the ready pool and then getting it back out incurs some overhead that can be avoided. To avoid the overhead, make sure that execute does not spawn the task but instead returns a pointer to it as the result. Example 9-8 shows the necessary changes.

Example 9-8. Scheduler bypass

structFibTask: public task {
    ...
    task* execute() {
        if( n<CutOff ) {
            *sum = SerialFib(n);
            return NULL;
        } else {
            FibContinuation& c =
                *new( allocate_continuation() ) FibContinuation(sum);
            FibTask& a = *new( c.allocate_child() ) FibTask(n-2,&c.x);
            FibTask& b = *new( c.allocate_child() ) FibTask(n-1,&c.y);
            // Set ref_count to "two children".
            set_ref_count(2);
            c.spawn( b );
// was:     c.spawn( a );   // the return of &a will spawn a.
// was:     return NULL;
            return &a;
        }
    }
};

Get Intel Threading Building Blocks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.