This is the first in a series of posts looking at our current ArrayFire <a href="http://www.accelereyes.com/arrayfire/c/examples.htm" target="_blank">examples</a>. The code can be compiled and run from <code>arrayfire/examples/</code> when you <a href="http://www.accelereyes.com/download_arrayfire">download</a> and install the ArrayFire library. Today we will discuss the examples found in the <code>getting_started/</code> directory.

<a title="Hello World" href="http://www.accelereyes.com/arrayfire/c/examples_2helloworld_2helloworld_8cpp-example.htm" target="_blank"><strong>Hello World</strong></a>

Of course we start with the <em>classic</em> "Hello World" example, which walks you through the basics of using the ArrayFire library. Running this example will print out system and device information, as well as perform some basic matrix operations. This is a good place to get familiar with the basic data container for ArrayFire - the <a href="http://www.accelereyes.com/arrayfire/c/classaf_1_1array.htm" target="_blank">array</a>.
<pre class="lang:default highlight:0 decode:true">ArrayFire v1.9 (build XXXXXXX) by AccelerEyes (64-bit Linux)
License: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CUDA toolkit 5.0, driver 304.54
GPU0 Quadro 6000, 6144 MB, Compute 2.0 (single,double)
Display Device: GPU0 Quadro 6000
Memory Usage: 5549 MB free (6144 MB total)...</pre>
<a href="http://www.accelereyes.com/arrayfire/c/examples_2getting_started_2convolve_8cpp-example.htm" target="_blank"><strong>Convolve</strong></a>

In this example we show you how to perform a basic image convolution, as well as how to use our timing functions. The output of running this code is simply the time it took to perform each convolution. Here we do it three different ways:
<pre class="lang:default decode:true">// Wrapper functions for timeit()
static void full() { full_out = convolve(img, kernel); }
static void dsep() { dsep_out = convolve(dx, spread, img); }
static void hsep() { hsep_out = convolve(5, h_dx, 5, h_spread, img); }</pre>
The first method simply convolves signal (img) with the filter (kernel). The second method allows you to just supply the two dimensions of the filter independently, and the third method allows you to use native data pointers (what we call a host) instead of the device data type.
<pre class="lang:default highlight:0 decode:true">Convolving a 640 by 480 matrix
full 2D convolution: 0.000258857 seconds
separable, device pointers: 9.71524e-05 seconds
separable, host pointers: 0.000117301 seconds</pre>
And, trying it on a bigger matrix...
<pre class="lang:default highlight:0 decode:true">Convolving a 6400 by 4800 matrix
full 2D convolution: 0.0242132 seconds
separable, device pointers: 0.008284 seconds
separable, host pointers: 0.0083929 seconds</pre>
<a href="http://www.accelereyes.com/arrayfire/c/examples_2getting_started_2fft_8cpp-example.htm" target="_blank"><strong>FFT</strong></a>

We have a method for performing the Fast Fourier Transform on an ArrayFire array. In this example we show you how to compute and print the result, as well as extract data from device memory. The following code snippet shows you the extraction:
<pre class="lang:default decode:true">cuComplex *B = A.host();
float real = cuCrealf(*B), imag = cuCimagf(*B);
printf("B[0] = %g %c %gin", real, ((imag &lt; 0) ? '-' : '+'), imag);</pre>
Generally, moving data back and forth between the CPU and devices is relatively slow, and eliminating as much of these transfers as possible will give you the best results. This example also shows you the other way to time a function using ArrayFire. Here are some of the results that I get when running the FFT on my machine "<code>Quadro 6000, 6144 MB, Compute 2.0 (single,double)</code>":
<pre class="lang:default highlight:0 decode:true">FFT on a 30x30 matrix: 0.000431 seconds
FFT on a 9000x9000 matrix: 0.001324 seconds</pre>
<a href="http://www.accelereyes.com/arrayfire/c/examples_2getting_started_2gfor_8cpp-example.htm" target="_blank"><strong>GFOR</strong></a>

This is a great example because you get to see the power of a special ArrayFire feature, the <em>gfor</em>-loop, which optimizes certain cases of for-loop programming. The basic idea of gfor is that you can launch all iterations of a loop in parallel on the GPU. There are a lot of things that can be done with the gfor loop, see the <a href="http://www.accelereyes.com/arrayfire/c/page_gfor.htm">documentation</a> for more details. In this example we see how much faster gfor is than a regular for (even if we are using ArrayFire arrays in the regular for-loop), and we see a few different ways of using a gfor loop. On my machine "<code>Quadro 6000, 6144 MB, Compute 2.0 (single,double)</code>" I got the following speed-up on the matrix multiply portion of the example:
<pre class="lang:default highlight:0 decode:true">Timing matrix multiply...
 for-loop 0.000210663 seconds
 gfor-loop 6.75187e-05 seconds
 speedup 3.1x</pre>
One of the main advantages of using ArrayFire is speed. The library was built to take advantage of your system's resources.

<a href="http://www.accelereyes.com/arrayfire/c/examples_2getting_started_2integer_8cpp-example.htm"><strong>Integer</strong></a>

This example shows off a bunch of the arithmetic that can be done on the elements of a matrix. One of the key things to learn here is that ArrayFire supports a number of basic data types for the elements of the array container. A description of these types can be found <a href="http://www.accelereyes.com/arrayfire/c/page_quickref.htm#quickref_types">here</a>. Many of these operations that shift or pick elements (like <a href="http://www.accelereyes.com/arrayfire/c/group__sort__mat.htm#gabafd9194924cfe3ebe4544fdf4f2863e">sort</a>, or <a href="http://www.accelereyes.com/arrayfire/c/group__arith.htm#gac42e13090c7899711891a4f60a288352">min</a>) can also return an index array containing references to the original positions of the chosen elements. This example shows another great advantage of using ArrayFire - simplicity. Many of the snippets here perform operations in a way that is easy to understand, without a lot of the typical hassle in worrying about the details of data, memory, not to mention GPU interfacing.

Here is a list of functions found in this example: <a href="http://www.accelereyes.com/arrayfire/c/group__indexing.htm#ga2829074e96053cb6dfab7ffa1e542c54">col</a>, <a href="http://www.accelereyes.com/arrayfire/c/group__indexing.htm#ga80c8b0a6536d955a66d0bcc59488e5d8">row</a>, <a href="http://www.accelereyes.com/arrayfire/c/group__indexing.htm#ga97ba48bddbe5a6f01e8e242cf57583d5">span</a>, <a href="http://www.accelereyes.com/arrayfire/c/group__arith.htm#ga0f0e7e912039bef82e3acae70a627a28">array</a> bit-wise ops (&amp;, |, ^) and logical-ops (&amp;&amp;, ||), <a href="http://www.accelereyes.com/arrayfire/c/group__sort__mat.htm#gabafd9194924cfe3ebe4544fdf4f2863e">sort</a>, <a href="http://www.accelereyes.com/arrayfire/c/group__manip.htm#gaeadf9256b56215a58dc12f14d97312eb">transpose</a>, <a href="http://www.accelereyes.com/arrayfire/c/group__manip.htm#ga920af856ce0c993a929850149a289d60">flip</a>, <a href="http://www.accelereyes.com/arrayfire/c/namespaceaf.htm#a808f0e19bbc711642067f9507c392da9">sum</a>, <a href="http://www.accelereyes.com/arrayfire/c/namespaceaf.htm#a15c9b1a86d98410e8bf9d2f0ff074cc5">multiply</a>, <a href="http://www.accelereyes.com/arrayfire/c/group__arith.htm#gac42e13090c7899711891a4f60a288352">min</a>, and <a href="http://www.accelereyes.com/arrayfire/c/group__arith.htm#ga5a2147389744f4840291f1f059ce2369">max</a>

<a href="http://www.accelereyes.com/arrayfire/c/examples_2getting_started_2lin_algebra_8cpp-example.htm"><strong>Linear Algebra</strong></a>

The example walks you through three of the functions in the linear algebra package - <a href="http://www.accelereyes.com/arrayfire/c/group__factor__mat.htm#ga5a97859d6dcdae94c39160d6d6ea9fa0">LU decomposition</a>, <a href="http://www.accelereyes.com/arrayfire/c/group__linsolve__mat.htm#gab942dfee90538a7f168f514e5dfc443e">solving</a> the equation AX = B, and <a href="http://www.accelereyes.com/arrayfire/c/group__factor__mat.htm#gaf97c09bd475b8e0b415de3e73e8a5589">Eigen value decomposition</a>. The setup and function calls are simple:
<pre class="lang:default decode:true">out = lu(in);
array X = solve(A, B);
print(eigen(in));</pre>
Just as a note, the last function "eigen" is part of the <a href="http://www.accelereyes.com/products/arrayfire_licensing">Pro License package</a>, so you won't be able to run the example without the license. Here is what the output looks like on my machine "<code>Quadro 6000, 6144 MB, Compute 2.0 (single,double)</code>":
<pre class="lang:default highlight:0 decode:true">-- ArrayFire Eigen value decomposition

eigen(in) =
   1.8201
   -0.5023
   0.1251

in =
   0.3022  0.7513  0.9504
   0.8077  0.1578  0.4499
   0.4071  0.5678  0.9829

val =
   1.8201  0.0000  0.0000
   0.0000  -0.5023  0.0000
   0.0000  0.0000  0.1251

vec =
   -0.6237  -0.5741  -0.3185
   -0.4717  0.8049  -0.7139
   -0.6232  -0.1503  0.6237</pre>
<span style="text-decoration: underline;"><strong>Rainfall</strong></span>

To wrap it all up, there is an sample problem of measuring data across many sites. The ArrayFire library makes it simple to aggregate, and analyze this data. Of course, here we just use a small number of data points, but you can see how easy it would be to run this kind of powerful analysis on a large data set.

<a href="http://www.accelereyes.com/download_arrayfire">Download</a> the ArrayFire library and give it a try!

---

<strong>Posts in this series:</strong>
<ol>
	<li><a href="http://blog.accelereyes.com/blog/2013/03/04/arrayfire-examples-part-1-of-8-getting-started/">ArrayFire Examples (Part 1 of 8) – Getting Started</a></li>
	<li><a href="http://blog.accelereyes.com/blog/2013/03/06/arrayfire-examples-part-2-of-8-benchmarks/">ArrayFire Examples (Part 2 of 8) – Benchmarks</a></li>
	<li><a href="http://blog.accelereyes.com/blog/2013/03/25/arrayfire-examples-part-3-of-8-financial/">ArrayFire Examples (Part 3 of 8) – Financial</a></li>
	<li><a href="http://blog.accelereyes.com/blog/2013/04/16/arrayfire-examples-part-4-of-8-image-processing">ArrayFire Examples (Part 4 of 8) - Image Processing</a></li>
	<li><a href="http://blog.accelereyes.com/blog/2013/06/05/arrayfire-examples-part-5-of-8-machine-learning/">ArrayFire Examples (Part 5 of 8) - Machine Learning</a></li>
	<li><a href="http://blog.accelereyes.com/blog/2013/06/12/arrayfire-examples-part-6-of-8-multiple-gpus/">ArrayFire Examples (Part 6 of 8) - Multiple GPU</a></li>
	<li><a href="http://blog.accelereyes.com/blog/2013/07/03/arrayfire-examples-part-7-of-8-pde/">ArrayFire Examples (Part 7 of 8) - PDE</a></li>
</ol>
&nbsp;