Range Concept

Summary

Requirements for type representing a recursively divisible set of values.

Requirements

The following table lists the requirements for a Range type R.

Range Concept

Pseudo-Signature

Semantics

R::R( const R& )

Copy constructor.

R::~R()

Destructor.

bool R::empty() const

True if range is empty.

bool R::is_divisible() const

True if range can be partitioned into two subranges.

R::R( R& r, split )

Basic splitting constructor. Splits r into two subranges.

R::R( R& r, proportional_split proportion )

Optional. Proportional splitting constructor. Splits r into two subranges in accordance with proportion.

static const bool R::is_splittable_in_proportion

Optional. If true, the proportional splitting constructor is defined for the range and may be used by parallel algorithms.

Description

A Range can be recursively subdivided into two parts. Subdivision is done by calling splitting constructor of Range. There are two types of splitting constructors:

  • Basic splitting constructor. It is recommended that the division be into nearly equal parts in this constructor, but it is not required. Splitting as evenly as possible typically yields the best parallelism.
  • Proportional splitting constructor. This constructor is optional and can be omitted along with is_splittable_in_proportion class variable. When using this type of constructor, for the best results, follow the given proportion with rounding to the nearest integer if necessary.

Ideally, a range is recursively splittable until the parts represent portions of work that are more efficient to execute serially rather than split further. The amount of work represented by a Range typically depends upon higher level context, hence a typical type that models a Range should provide a way to control the degree of splitting. For example, the template class blocked_range has a grainsize parameter that specifies the biggest range considered indivisible.

If the set of values has a sense of direction, then by convention the splitting constructor should construct the second part of the range, and update its argument to be the first part of the range. This enables parallel_for, parallel_reduce and parallel_scan algorithms, when running sequentially, to work across a range in the increasing order, typical of an ordinary sequential loop.

Example

The following code defines a type TrivialIntegerRange that models the Range concept. It represents a half-open interval [lower,upper) that is divisible down to a single integer. Splitting can be done via basic or proportional splitting constructors. The basic splitting constructor divides the range in half. The proportional splitting constructor divides the range in the given proportion p, with r updated to be the left part and a new TrivialIntegerRange constructed for the right part. Note that special care has been taken to ensure non-emptiness of the involved TrivialIntegerRange instances.

 
struct TrivialIntegerRange {
    int lower;
    int upper;
    bool empty() const {return lower==upper;}
    bool is_divisible() const {return upper>lower+1;}
    // basic splitting constructor
    TrivialIntegerRange( TrivialIntegerRange& r, split ) {
        int m = (r.lower+r.upper)/2;
        lower = m;
        upper = r.upper;
        r.upper = m;
    }
    // optional proportional splitting constructor
    TrivialIntegerRange( TrivialIntegerRange& r, proportional_split p ) {
        int m = ((r.lower+r.upper)*p.right())/(p.left()+p.right());
        if (m == 0)
            m = 1;
        else if (m == r.upper)
            m = r.upper - 1;
        lower = m;
        upper = r.upper;
        r.upper = m;
    }
    // optional trait that enables proportional split
    static const bool is_splittable_in_proportion = true;
};

TrivialIntegerRange is for demonstration and not very practical, because it lacks a grainsize parameter. Use the library class blocked_range instead.

Model Types

Type blocked_range models a one-dimensional range.

Type blocked_range2d models a two-dimensional range.

Type blocked_range3d models a three-dimensional range.

The Container Range Concept models a container as a range.

For more complete information about compiler optimizations, see our Optimization Notice.