O'Reilly logo

Intel Threading Building Blocks by James Reinders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Example Program for Fibonacci Numbers

This section uses the computation of the Fibonacci numbers as an example to illustrate the direct use of the Intel Threading Building Blocks task scheduler. A Fibonacci number in the Fibonacci series F is defined as the sum of the previous two terms:

Fn = Fn − 1 + Fn − 2

Therefore, the seventh Fibonacci number of the series beginning with F0 = 0, F1 = 1 is 8 (F6 = 8). This example uses an inefficient method to compute Fibonacci numbers, but it demonstrates the basics of a task library using a simple recursive pattern.

To get scalable speedup out of task-based programming, you need to specify a lot of tasks. This is typically done in Threading Building Blocks with a recursive task pattern.

Example 9-1 shows a traditional, serial solution using recursion.

Example 9-1. Fibonacci serial version

long SerialFib( long n ) {
    if( n<2 )
        return n;
    else
        return SerialFib(n-1)+SerialFib(n-2);
}

The top-level code ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required