Advanced Topic: Other Kinds of Iteration Spaces

The examples so far have used the class blocked_range<T> to specify ranges. This class is useful in many situations, but it does not fit every one. You can define your own iteration space objects to use with Intel Threading Building Blocks. The object must specify how it can be split into subspaces by providing two methods and a splitting constructor. You can see these simple definitions in the class blocked_range<T>.

If your class is called R, the methods and constructor could be as shown in Example 3-16.

Example 3-16. Define your own iteration space object

class R {
    // True if range is empty
    bool empty() const;
    // True if range can be split into nonempty subranges
    bool is_divisible() const;
    // Split r into subranges r and *this
    R( R& r, split );

The method empty must return true if the range is empty. The method is_divisible needs to return true if the range can be split into two nonempty subspaces, and such a split is worth the overhead. The splitting constructor needs to take two arguments:

  • The first of type R

  • The second of type tbb::split

The second argument is not used; it serves only to distinguish the constructor from an ordinary copy constructor.

The splitting constructor should attempt to split r into two halves of roughly the same size, update r to be the first half, and let the constructed object be the second half. The two halves should be nonempty. The parallel algorithm templates call the splitting constructor on r only ...

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.