This is the eighth 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>visualization/</code> directory.

In these examples, my machine has the following configuration:
<pre class="lang:default decode:true">ArrayFire v1.9.1 (build XXXXXXX) by AccelerEyes (64-bit Mac OSX)
License: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CUDA toolkit 5.0, driver 304.54
GPU0 GeForce GT 650M, 1024 MB, Compute 3.0 
Display Device: GPU0 GeForce GT 650M
Memory Usage: 650 MB free (1024 MB total)</pre>
&nbsp;

So far, we have been looking at the excellent examples that shows the benefits of using ArrayFire in various fields of study. We performed some appropriate solutions to the complex mathematical computations introducing the ArrayFire libraries. Most of the time, the power of ArrayFire was introduced as a tool to gain a fast performance on those computations and ability to visually draw the real-time results. That is a fact that has been proven by now. If we have been focusing too much on the performance of those complex mathematical operations, in this example, we will take a closer look at the performance of heavy visualizations with simple algorithms.

&nbsp;

<strong>1. Julia Sets - julia.cpp</strong>
<p style="text-align: center;"><strong> <a href="http://blog.accelereyes.com/blog/wp-content/uploads/2013/06/Screen-Shot-2013-06-17-at-3.13.04-PM.png"><img class="aligncenter  wp-image-4322" alt="Screen Shot 2013-06-17 at 3.13.04 PM" src="http://blog.accelereyes.com/blog/wp-content/uploads/2013/06/Screen-Shot-2013-06-17-at-3.13.04-PM-1024x546.png" width="614" height="328" /></a></strong>Figure 1</p>
<p style="text-align: left;">This is an example of Julia Sets, one of the most famous fractal transformation defined from iteration of the function. The algorithm is simple enough:</p>
<p style="text-align: left;">1. Set matrix pt = "any complex numbers"
2. Set C = "a single complex number" (Julia Set constant)
3. Set pt = pt<sup>2</sup> + C
4. If pt is in Julia Set, mark it.
5. Repeat 3-4</p>

<pre class="lang:default decode:true"> 18         // Calculate the point in complex plane corresponding to the x and y index
 19         float multiplier = 1.5; // zoom factor
 20         array realPart =  multiplier *  (DIM/2.0 - x_index) / (DIM/2.0); // used to calculate the real part of pt
 21         array complexPart =  multiplier *  (DIM/2.0 - y_index) / (DIM/2.0); // used to calculate the imaginary part of pt
 22         array pt = complex(realPart,complexPart);// DIM*DIM complex points for each pixel of the image
 23         cuComplex C= {-0.8,0.156}; // constant</pre>
The code contains some fixed setting values for the example - not important for our purpose. It first sets the values in matrix "pt" with complex numbers. ArrayFire provides <a href="http://www.accelereyes.com/arrayfire/c/group__arith.htm#gaa307466288cb4e1ef38561c70826204c">COMPLEX</a> function that takes a matrix array with a real numbers as a parameter, and another matrix array with imaginary numbers as a parameter. Then, it produces the matrix array "pt" filled with complex numbers.

<a href="http://www.accelereyes.com/arrayfire/c/page_quickref.htm">cuComplex</a> type (complex single-precision), used for a single value "C" in line 23, is pretty much what <a href="http://www.accelereyes.com/arrayfire/c/group__arith.htm#gaa307466288cb4e1ef38561c70826204c">COMPLEX</a> function uses to produce a single complex number. <a href="http://www.accelereyes.com/arrayfire/c/group__arith.htm#gaa307466288cb4e1ef38561c70826204c">COMPLEX</a> function performs this <a href="http://www.accelereyes.com/arrayfire/c/page_quickref.htm">cuComplex</a> type initializations for many values within a matrix in parallel manner, which makes things much faster.

Once the value "C" (Julia Set Constant) is set and matrix "pt" is filled with values, we can now begin the iteration for "pt = pt<sup>2</sup> + C".

If you want to visualize the actual process, so you can possibly see the flow with your eyes, you can simply add a few lines in the for-loop as the following:
<pre class="lang:default decode:true"> 25         // Calculate pt(n+1) = pt(n)*pt(n) + C iteratively
 26         for(int i=0; i&lt;200; i++)
 27         {
 28             pt = pt*pt+C;                   
 29             //holds 0 if point is in Julia Set or 1 if point is outside the Julia Set
 30             array result = (((real(pt)*real(pt)) + (imag(pt)*imag(pt))) &lt; 1000);
 31             array img = constant(0, DIM, DIM, 3); // DIM*DIM 3 channel (RGB) image initialized to zero
 32             img(span, span, 1) = 255 * result; // set the blue component of the image
 33             image(img);
 34             usleep(10000);
 35         }</pre>
Processing with more iterations will mark off more points for detail. Also, notice that you can replace the complex number C with an arbitrary number and set an appropriate number for iteration to see different image results. It is also noticeable that the whole process for this cool visualization only took seven lines for initial set-up, one line for the a for-loop to do the actual process, and four lines to display the results in plot. There was no need of an additional for-loop for matrix initialization with complex numbers and another additional for-loop for traversing each value within the matrix. With these few lines of code, you can draw "Juila Sets" on screen even though it consists of a tremendous number of points for a better accuracy (As you see figure 1, its domain and range is 0 to 1000 for each). ArrayFire library actually made this as simple as the algorithm of Julia Sets.

&nbsp;

<strong>2. Vortex - <a href="http://www.accelereyes.com/arrayfire/c/examples_2visualization_2vortex_8cpp-example.htm">vortex.cpp</a></strong>
<p style="text-align: center;"><a href="http://blog.accelereyes.com/blog/wp-content/uploads/2013/06/Screen-Shot-2013-06-17-at-4.40.29-PM.png"><img class="aligncenter  wp-image-4335" alt="Screen Shot 2013-06-17 at 4.40.29 PM" src="http://blog.accelereyes.com/blog/wp-content/uploads/2013/06/Screen-Shot-2013-06-17-at-4.40.29-PM-1024x574.png" width="614" height="344" /></a></p>
<p style="text-align: center;">Figure 2</p>
<p style="text-align: left;">This is an example of drawing a Vortex in three dimensional space. You might only discover that the green points are blinking within a boundary of a vortex; however, the algorithm contains more than what it looks. It only seems like the points are just blinking; that is because there are too many iterations occurring per second with the speed comes from using the device and ArrayFire libraries. If we add "usleep(10000);" in the end of the while loop, you will be able to visualize the movement of points with your eyes.</p>
<p style="text-align: left;">First, the following line simply adds new height to every point passed very bottom of the vortex (Z&lt;0). This visualizes as if it creates many new green points on the top of this vortex, but also remove them when they get to the bottom.</p>

<pre class="lang:default decode:true"> 35         // Remove old points and add new points
 36         Z = array(Z &lt; zero) + Z * array(Z &gt;= zero);</pre>
<p style="text-align: left;">After the new points are created, the following lines make them flow through the hollow vortex in tornadoes form with some math operations to X and Y.</p>

<pre class="lang:default decode:true"> 38         // Update the X and Y points
 39         array TEMP = T + 2 * af::Pi * revolutions * .00025 * (iter++);
 40         X = xAx * Z * cos(TEMP) + 0.5;
 41         Y = yAx * Z * sin(TEMP) + 0.5;</pre>
To make them flow down to the bottom, each Z value also gets subtracted by 0.0025 before X and Y values are updated.
<pre class="lang:default decode:true"> 17     float zD = -0.0025f;
 ..
 33     Z = Z + zD;</pre>
The number of points in figure 2 are actually the number of elements formed by the matrix X, Y, and Z. Each matrix contains such many values and each value in those matrices get updated in a parallel manner using ArrayFire libraries - which lead to approximately 1500 iterations per second. This is one of the best example that visually proves that ArrayFire libraries help users to play with a tremendous number of elements and still maintains the decent performance of drawing them.

&nbsp;

<strong> 3. blip - <a href="http://www.accelereyes.com/arrayfire/c/examples_2visualization_2blip_8cpp-example.htm">blip.cpp</a></strong>
<p style="text-align: left;">This is the last example, blip, a sinusoidal function that oscillates the surface, showing in four different perspective views. This example is also to show off the graphics performance with ArrayFire. Oscillating three dimensional form is a complex movement for graphics since every point has to be relocated for each update, meaning the process must be more precise and complex to make the smooth motion. This also contains only few lines of mathematical operations, but within very wide domain and range. I will probably repeat the same things I mentioned in the example above. Perhaps, it might be better to just visualize this example with a clip in case of those missing to see it. Also, notice that the performance will be better than the quality shown in this clip, depending on your device capacity.</p>
<object width="650" height="400" classid="clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b" codebase="http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0"><param name="src" value="http://blog.accelereyes.com/blog/wp-content/uploads/2013/06/blip.m4v" /><embed width="650" height="400" type="video/quicktime" src="http://blog.accelereyes.com/blog/wp-content/uploads/2013/06/blip.m4v" /></object>

&nbsp;

Many of you probably need the GPU programming experience to perform graphics, maybe a simple picture, or maybe heavy three-dimensional motions with your experiment data. However, you may have no experience with the existing GPU programming platforms. ArrayFire can make you to have the GPU programming skill with its easy libraries. If you know C, C++, or Fortran, you are ready to go. <a href="http://www.accelereyes.com/download_arrayfire">Download</a> the ArrayFire library and visualize what you need today!

&nbsp;

---

<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;