Closures

Intel(R) Array Building Blocks (Intel(R) ArBB) provides closures as an advanced mechanism to capture computations in C++ functions performed on Intel(R) ArBB types so that they can be optimized and executed by Intel(R) ArBB.

The arbb::call() function work by capturing a function passed in the first time it is encountered. Intel(R) ArBB exposes this functionality directly through the arbb::capture() function. The arbb::capture() function accepts a user function, and converts it to an arbb::closure. Unlike arbb::call(), which only performs a capture once for a given function, arbb::capture() produces a new closure every time it is called.

The capture process consists of simply executing the C++ function as a regular C++ function. Before doing this, arbb::capture() puts the Intel(R) ArBB runtime into a special mode, where any operations on ArBB types (e.g. element-wise operations on containers) do not take effect immediately. Instead, such operations are stored in the closure returned from arbb::capture(). The returned closure is optimized and compiled through Intel(R) ArBB at runtime, and can then be executed repeatedly.

Because the function being captured is simply being executed in C++ during the call to arbb::capture(), any operations not happening on Intel(R) ArBB types will simply execute immediately during the capture. Simple uses of Intel(R) ArBB can avoid unexpected behavior by ensuring that the function being captured does not contain any such operations and only uses Intel(R) ArBB types. You can take advantage of such operations for more advanced usages of Intel(R) ArBB in order to specialize closures at runtime based on arbitrary C++ data. For example, captured functions can include operations on C++ primitive types such as float, which will only be computed during capture time. The results of these operations, if used within some Intel(R) ArBB type computation, will be stored directly in the closures as constants.

The arbb::capture() function returns an instantiation of the arbb::closure template. The arbb::closure template takes a single template parameter representing the type of the function captured. For example, capturing a function declared as void foo(f32& a, f32 b) yields a closure of type arbb::closure<void (f32&, f32)>.

Because the type of the captured function forms a part of the closure type, instances of arbb::closure will perform type checking at compile time. Attempting to call an arbb::closure with arguments that do not match the closure parameters results in a compile-time failure. Other type errors, such as attempting to assign a closure of one type to a closure of another, also result in compile-time failures.

The arbb::auto_closure class provides the same functionality as arbb::closure, but is a plain C++ class, not a template. Unlike arbb::closure, arbb::auto_closure performs run-time type checking for type errors. Using arbb::auto_closure adds some runtime overhead to perform the type checking on execution, but allows the type of the closure to vary at run-time.

A default-constructed closure is considered empty. Attempting to execute such a closure results in a run-time exception.

See Also

Function Invocation

Classes

class  arbb::auto_closure
 A dynamically-typed closure. More...
class  arbb::closure< FunctionType >
 A statically-typed closure captured from a function of type FunctionType. More...

Functions

template<typename FunctionType >
closure< FunctionType > arbb::capture (FunctionType function)
 Captures all Intel(R) ArBB operations in function by executing it in C++, and returns a closure representing the operations executed.

Function Documentation

template<typename FunctionType >
closure<FunctionType> arbb::capture ( FunctionType  function  )  [inline]

Captures all Intel(R) ArBB operations in function by executing it in C++, and returns a closure representing the operations executed.

See Also

Closures

Submit feedback on this help topic

Copyright © 2010, Intel Corporation. All rights reserved.