A Very Short Introduction to the Accelerator API
The Accelerator data parallel array library for .Net provides a basic set of data types for parallel array computation and a set of operations on those types.
The operations provided can be categorized as follows.
*Array creation: This is typically done by coercing a system array or bitmap.
*Arithmetic operations: addition, multiplication, etc.
*Boolean operations: and, or, not, great than, less than, etc.
*Type conversion: Coercion of a parallel array to a different type.
*Reductions: sum, product, etc.
*Transformations: expand, pad, shift, gather, scatter, etc.
*Basic linear algebra: inner product, outer product.
Creating Parallel Arrays
In order to create a parallel array, the shape of the array and the values for each entry in the array need to be specified. Constructors are provided to create a parallel array from a .Net system array. For these constructors, the rank, shape, and initial values are taken from the input system array. Another set of constructors provides for the specification of a default value together with a shape input array that determines the properties of the result. Functions are also provided to construct parallel arrays from bitmaps. Note that the creation of a parallel array involving system array types must be done through one of the disposable types mentioned above. There is a limit to the number of simultaneous live parallel array objects. This limit depends on the size of the arrays created and the complexity of the calculations. Methods are provided to query for the rank, shape, and extents of parallel arrays.
DFPA dfpa = new DFPA(new float[] {1.0f, 2.0f, 3.0f, 4.0f, 5.0f}); // Disposable array
FPA fpa = new FPA(3.0f, new int[] {2, 3}); // Creates a 2x3 constant array
Array Expressions
Parallel array expressions are used to specify the computation in an algorithm. Variables of the basic array types must be used to hold the results of an expression. Note that the expressions are built up in data structures in system memory. Execution is delayed. No computation takes place until a result is requested. The following example shows the computation of the norm of an array.
using (DFPA a = new DFPA(1.0f, 2.0f, 3.0f, 4.0f, 5.0f))
{
float[] result;
FPA scale =
FPA.MaxVal(FPA.Abs(a)); scale = FPA.Cmp(scale, scale, 1.0f);
FPA s = a / scale.Replicate(a.Shape);
s = scale * FPA.Sqrt(FPA.Sum(s * s));
s.ToArray(out result);
}
The result is a 1x1 array. Note that no computation takes place on the GPU until the
ToArray method is invoked. Please refer to the help file for a complete list of operations and methods.