The fundamental primitive of the ArrayFire Library is our container object, the array. As the next post in our blog series, "<a href="http://sandbox.arrayfire.com/tag/learning-arrayfire-from-scratch/">Learning ArrayFire from scratch</a>", we will describe how to construct arrays, what datatypes arrays support, indexing, how to query an array for various properties (content, dimensions), and how to write mathematical expressions that involve arrays.

<!--more-->

ArrayFire abstracts away much of the details of programming parallel architectures by providing a high-level container object, the <a class="el" href="http://arrayfire.org/docs/group__array__mat.htm">array</a>, that represents data stored on a CPU, GPU, FPGA, or other type of accelerator. This abstraction permits developers to write massively parallel applications in a high-level language where they need not be concerned about low-level optimizations that are frequently required to achieve high throughput on most parallel architectures.
<h4>Creating ArrayFire Arrays</h4>
Arrays represent memory on a supported device. You can create arrays of uninitialized memory using the array constructor:
<pre class="lang:c++ decode:true">// Arrays may be created using the af::array constructor and dimensioned
// as 1D, 2D, 3D; however, the values in these arrays will be undefined
array undefined_1D = array(100);        // 1D array with 100 elements
array undefined_2D = array(10, 100);    // 2D array of size 10 x 100
array undefined_3D = array(10, 10, 10); // 3D array of size 10 x 10 x 10</pre>
Or using one of many constructors that initialize memory:
<pre class="lang:default decode:true">// Create an array of zeros of length three:
array zeros      = constant(0, 3);

// Generate a 1x4 array of uniformly distributed [0,1] random numbe// using af::randu():
array rand1      = randu(1, 4);
    
// Make a 2x2 array (or matrix, if you prefer) of random numbers
// sampled from a normal distribution using af::randn():
array rand2      = randn(2, 2);

// Construct a 3x3 identity matrix. 
array iden       = af::identity(3, 3);</pre>
Lastly, arrays may be populated with data from the host using our copy constructor:
<pre class="lang:default decode:true">// Create a six-element array on the host
float hA[] = {0, 1, 2, 3, 4, 5};
    
// Using the array copy constructor, we can create a 2x3
// matrix from this data:
array A(2, 3, hA);</pre>
Arrays can also be created from CUDA device pointers or OpenCL memory buffers. Please consult the <a href="http://arrayfire.org/docs/gettingstarted.htm">documentation</a> on these aspects.
<h4>Supported data types</h4>
ArrayFire arrays support standard integral and complex data types found in C/C++. The data type for an array is specified by providing an (optional) final parameter to any of the array constructors. The type field and supported types are as follows:
<ul>
 	<li>b8 8-bit boolean values (bool)</li>
 	<li>f32 real single-precision (float)</li>
 	<li>c32 complex single-precision (cfloat)</li>
 	<li>s32 32-bit signed integer (int)</li>
 	<li>u32 32-bit unsigned integer (unsigned)</li>
 	<li>f64 real double-precision (double)</li>
 	<li>c64 complex double-precision (cdouble)</li>
 	<li>s64 64-bit signed integer (intl)</li>
 	<li>u64 64-bit unsigned integer (uintl)</li>
 	<li>s16 16-bit signed integer (short)</li>
 	<li>u16 16-bit unsigned integer (unsigned short)</li>
</ul>
For example, if you wanted to construct a 2x1 (column vector) of uniformly distributed 32-bit complex numbers (c32 data type), you would use the following code:
<pre class="lang:default decode:true">// Create a 2x1 column vector of uniformly distributed
// 32-bit complex values:
array randcplx   = randu(2, 1, c32);</pre>
<h4>Indexing</h4>
Like all functions in ArrayFire, indexing is also executed in parallel on the OpenCL/CUDA device. Because of this, indexing becomes part of a JIT operation and is accomplished using parentheses instead of square brackets (i.e. as A(0) instead of A[0]). To index af::arrays you may use one or a combination of the following functions:
<ul>
 	<li>integer scalars</li>
 	<li>seq() representing a linear sequence</li>
 	<li>end representing the last element of a dimension</li>
 	<li>span representing the entire dimension</li>
 	<li>row(i) or col(i) specifying a single row/column</li>
 	<li>rows(first,last) or cols(first,last) specifying a span of rows or columns</li>
</ul>
Here are a few examples of these indexing functions in operation:
<pre class="lang:default decode:true">// Make an 3x3 array containing numbers 1-9:
array A = array(seq(1,9), 3, 3);

A(0);            // first element
A(0,1);          // first row, second column
A(end);          // last element
A(-1);           // also last element
A(end-1);        // second-to-last element
A(1,span);       // second row
A.row(end);      // last row
A.cols(1,end);   // all but first column</pre>
For further examples, see the <a href="http://arrayfire.org/docs/indexing.htm">documentation on indexing</a>.
<h4>Querying existing arrays for their properties</h4>
ArrayFire array objects carry some ancillary information which can be quite useful when developing applications. For example, once you have an array <span class="lang:default decode:true crayon-inline ">A</span> , you can query for:
<ul>
 	<li>Number and size of dimensions via. <span class="lang:default decode:true crayon-inline ">A.numdims()</span>  and <span class="lang:default decode:true crayon-inline ">A.dims(int)</span>  functions.</li>
 	<li>The size of the array in bytes with <span class="lang:default decode:true crayon-inline ">A.bytes()</span></li>
 	<li>The underlying data type using <span class="lang:default decode:true crayon-inline ">A.type()</span> or one of many type-specific queries.</li>
 	<li>Whether or not the array is complex or real using <span class="lang:default decode:true crayon-inline ">A.iscomplex()</span>  or <span class="lang:default decode:true crayon-inline ">A.isreal()</span></li>
 	<li>If the data is scalar, <span class="lang:default decode:true crayon-inline ">A.isscalar()</span> , a vector, <span class="lang:default decode:true crayon-inline ">A.isvector()</span> , a row, <span class="lang:default decode:true crayon-inline ">A.isrow()</span> , or a column, <span class="lang:default decode:true crayon-inline ">A.iscolumn()</span> .</li>
</ul>
<h4>Writing mathematical expressions</h4>
ArrayFire features an intelligent Just-In-Time (JIT) compilation engine that converts expressions using arrays into the smallest number of CUDA/OpenCL kernels. For most operations on arrays, ArrayFire functions like a vector library. That means that an element-wise operation, like <span class="lang:default decode:true crayon-inline ">c[i] = a[i] + b[i]</span>  in C, would be written more concisely without indexing, like <span class="lang:default decode:true crayon-inline ">c = a + b</span> . When there are multiple expressions involving arrays, ArrayFire's JIT engine will merge them together. This "kernel fusion" technology not only decreases the number of kernel calls, but, more importantly, avoids extraneous global memory operations. Our JIT functionality extends across C/C++ function boundaries and only ends when a non-JIT function is encountered or a synchronization operation is explicitly called by the code. Thus writing mathematical functions in ArrayFire is as simple as writing the equations themselves!

ArrayFire provides <a class="el" href="http://arrayfire.org/docs/group__arith__mat.htm">hundreds of functions</a> for element-wise operations. All of the standard operators (e.g. +,-,*,/) are supported as are most transcendental functions (sin, cos, log, sqrt, etc.).

In addition to mathematical functions, ArrayFire also includes frequently used computer vision functions (FAST, ORB, SIFT), and image processing functions (colorspace conversion, filters, histograms, transformations, and morphological operations).

&nbsp;