Visualizing the af::array with Forge

Stefan ArrayFire Leave a Comment

The ArrayFire library attempts to make high-performance computing as easy as possible for scientists and engineers. Because many tasks need to be visualized, ArrayFire also provides a high-level interface to our Forge visualization library. In today's "Learning ArrayFire from scratch" post we present an overview of ArrayFire's visualization functionality and demonstrate how to use Forge to display data contained in af::array objects.

The Forge library is a general-purpose visualization tool for data stored on the GPU or related accelerator devices. By default, ArrayFire ships with Forge built in. However; if you or your system administrator installed the "no-graphics" version of ArrayFire, Forge won't be included.

ArrayFire provides several wrapper functions that simplify Forge's interface when used with af::array objects. These functions primarily perform interop tasks including visualization range setting, copying data into OpenGL data structures, and generating an OpenGL window for display.

Setup

Before we can use Forge, we must create an window containing an OpenGL context. Creating an instance of theaf::Window class will accomplish both of these tasks. Note, at present Forge must create and manage its own OpenGL context; however, we hope to change this in a future release. Once the window is created, you can use Forge's drawing functions to visualize your data. Here is a basic example of how to create a window, set its title, and enter into a drawing loop:

Rendering Functions

As included in ArrayFire 3.3, Forge provides the following plotting capabilities:

Function Description
af::Window::hist() Creates a histogram plot from input data and bins
af::Window::image() Display an image
af::Window::plot() 2D Line plot
af::Window::plot3() 3D line plot
af::Window::scatter() 2D scatter plot
af::Window::scatter3() 3D scatter plot
af::Window::surface() Visualize a 3D surface

Full documentation on all plotting functions can be found in the rendering functions portion of the documentation.

Image

The af::Window::image() function can be used to plot grayscale or color images. To plot a grayscale image a 2d array should be passed into the function.
Let’s see this on a static noise example:

Forge image plot of noise

Tweaking the previous example by giving our image a depth of 3 for the RGB values allows us to generate colorful noise:

Forge image plot of color noise

Note that Forge automatically handles any af::array type passed from Arrayfire. In the first example we passed in an image of floats in the range [0, 1]. In the last example we cast our array to an unsigned byte array with the range
[0, 255]. The type-handling properties are consistent for all Forge drawing functions.

Plot

The af::Window::plot() function visualizes an array as a 2d-line plot. Let’s see a simple example:

Forge 2d line plot of sin() function
The plot function has the signature:

void plot( const array &X, const array &Y, const char * const title = NULL );

Both the x and y coordinates of the points are required to plot. This allows for non-uniform, or parametric plots:

Scatter

The af::Window::scatter() function visualizes an array as a 2d-scatter plot. Let’s see a example:

Forge 2d scatter plot

The scatter function has the signature:

void scatter( const array &X, const array &Y, af::markerType marker=AF_MARKER_POINT, const char * const title = NULL);

The af::markerType enum determines which marker will be drawn at each point.
It can take on the values of:

Different types of markers in Forge.

Plot3

The af::Window::plot3() function will plot a curve in 3d-space.
Its signature is:

void plot3 (const array &in, const char * title = NULL);

The input array expects xyz-triplets in sequential order. The points can be in a flattened one dimensional (3n x 1) array, or in one of the (3 x n), (n x 3) matrix forms.

Forge 3d line plot

Histogram

The af::Window::hist() function renders an input array as a histogram. In our example, the input array will be created with Arrayfire’s histogram() function, which actually counts and bins each sample. The output from histogram() can directly be fed into the af::Window::hist() rendering function.

In addition to the histogram array with the number of samples in each bin, the af::Window::hist() function takes two additional parameters – the minimum and maximum values of all datapoints in the histogram array. This effectively sets the range of the binned data. The full signature of af::Window::hist() is:

void hist(const array & X, const double minval, const double maxval, const char * const title = NULL);

Forge histogram plot

Scatter3

The af::Window::scatter3() function visualizes an array as a 3d-scatter plot.
Its signature is:

void scatter3 (const array &in, af::markerType marker=AF_MARKER_POINT, const char * title = NULL);

The input array expects xyz-triplets in sequential order. The points can be in a flattened one dimensional (3n x 1) array, or in one of the (3 x n), (n x 3) matrix forms.

Forge 3d scatter plot

The af::markerType enum determines which marker will be drawn at each point. It can take on the values of:

Different types of markers in Forge.

Surface

The af::Window::surface() function will plot af::arrays as a 3d surface.

Forge 3d surface() plot
There are two overloads for the af::Window::surface() function:

void surface (const array & S, const char * const title )
// Accepts a 2d matrix with the z values of the surface

void surface (const array &xVals, const array &yVals, const array &S, const char * const title)
// accepts additional vectors that define the x,y coordinates for the surface points.

The second overload has two options for the x, y coordinate vectors. Assuming a surface grid of size m x n:

  1. Short vectors defining the spacing along each axis. Vectors will have sizes m x 1 and n x 1.
  2. Vectors containing the coordinates of each and every point. Each of the vectors will have length mn x 1. This can be used for completely non-uniform or parametric surfaces.

Conclusion

There is a fairly comprehensive collection of methods to visualize data in Arrayfire. Thanks to the high-performance gpu plotting library Forge, the provided Arrayfire functions not only make visualizations as simple as possible, but keep them as robust as the rest of the Arrayfire library.

Facebooktwitterredditlinkedinmail