Indexing in ArrayFire is a powerful but easy to abuse feature of the af::array class. This feature allows you to reference or copy subsections of a larger array and perform operations on only a subset of elements.
Indexing in ArrayFire can be performed using the parenthesis operator or one of the member functions of the af::array class. These functions allow you to reference one or a range of elements from the original array.
Here we will demonstrate some of the ways you can use indexing in ArrayFire and discuss ways to minimize the memory and performance impact of these operations.
Lets start by creating a new 4x4 matrix of floating point numbers:
ArrayFire is column-major so the resulting A array will look like this:
\[ \begin{bmatrix} 0 & 4 & 8 & 12 \\ 1 & 5 & 9 & 13 \\ 2 & 6 & 10 & 14 \\ 3 & 7 & 11 & 15 \end{bmatrix} \]
This is a two dimensional array so we can access the first element of this matrix by passing 0,0
into the parenthesis operator of the af::array.
\[ A(2, 3) = [ 14 ] \]
We can also access the array using linear indexing by passing in one value. Here we are accessing the fifth element of the array.
\[ A(5) = [ 5 ] \]
Indexing with negative values will access from the end of the array. For example, the value negative one and negative two(-2) will return the last and second to last element of the array, respectively. ArrayFire provides the end
alias for this which also allows you to index the last element of the array.
You can access regions of the array via the af::seq and af::span objects. The span objects allows you to select the entire set of elements across a particular dimension/axis of an array. For example, we can select the third column of the array by passing span as the first argument and 2 as the second argument to the parenthesis operator.
\[ A(span, 2) = \begin{bmatrix} 8 \\ 9 \\ 10 \\ 11 \end{bmatrix} \]
You can read that as saying that you want all values across the first dimension, but only from index 2 of the second dimension.
You can access the second row by passing (1, span) to the array
\[ A(1, span) = [ 1, 5, 9, 13 ] \]
You can use the af::seq (short for sequence) object to define a range when indexing. For example, if you want to get the first two columns, you can access the array by passing af::span for the first argument and af::seq(2) as the second argument.
\[ A(span, seq(2)) = \begin{bmatrix} 0 & 4 \\ 1 & 5 \\ 2 & 6 \\ 3 & 7 \end{bmatrix} \]
There are three constructors for af::seq.
The last constructor that can help create non-continuous ranges. For example, you can select the second and forth(last) rows by passing (seq(1, end, 2), span) to the indexing operator.
\[ A(seq(1, end, 2), span) = \begin{bmatrix} 1 & 5 & 9 & 13 \\ 3 & 7 & 11 & 15 \end{bmatrix} \]
You can also index using other af::array objects. ArrayFire performs a Cartesian product of the input arrays.
\[ A = \begin{bmatrix} 0 & 4 & 8 & 12 \\ 1 & 5 & 9 & 13 \\ 2 & 6 & 10 & 14 \\ 3 & 7 & 11 & 15 \end{bmatrix} \\ A( \begin{bmatrix} 2 \\ 1 \\ 3 \end{bmatrix} , \begin{bmatrix} 3 \\ 1 \\ 2 \end{bmatrix} ) = \begin{bmatrix} (2,3) & (2,1) & (2,2) \\ (1,3) & (1,1) & (1,2) \\ (3,3) & (3,1) & (3,2) \end{bmatrix} = \begin{bmatrix} 14 & 6 & 10 \\ 13 & 5 & 9 \\ 15 & 7 & 11 \end{bmatrix} \]
If you want to index an af::array using coordinate arrays, you can do that using the af::approx1 and af::approx2 functions.
\[ approx2(A, \begin{bmatrix} 2 \\ 1 \\ 3 \end{bmatrix} , \begin{bmatrix} 3 \\ 1 \\ 2 \end{bmatrix} ) = \begin{bmatrix} (2,3) \\ (1,1) \\ (3,2) \end{bmatrix} = \begin{bmatrix} 14 \\ 5 \\ 11 \end{bmatrix} \]
Boolean(b8) arrays can be used to index into another array. In this type of indexing the non-zero values will be selected by the boolean operation. If we want to select all values less than 5, we can pass a boolean expression into the parenthesis operator.
\[ out = \begin{bmatrix} 0 \\ 1 \\ 2 \\ 3 \\ 4 \end{bmatrix} \]
All ArrayFire indexing functions return af::array(technically its an array_proxy class) objects. These objects may be new arrays or they may reference the original array depending on the type of indexing that was performed on them.
The following code snippet shows some examples of indexing that will allocate new memory.
Notice that even though the copy3 array is referencing continuous memory in the original array, a new array is created because we used an array to index into the af::array.
An assignment on an af::array will replace the array with the result of the expression on the right hand side of the equal(=) operator. This means that the type and shape of the result can be different from the array on the left had side of the equal operator. Assignments will not update the array that was previously referenced through an indexing operation. Here is an example:
The ref
array is created by indexing into the data array. The initialized ref
array points to the data array and does not allocate memory when it is created. After the matmul call, the ref
array will not be pointing to the data array. The matmul call will not update the values of the data array.
You can update the contents of an af::array by assigning with the operator parenthesis. For example, if you wanted to change the third column of the A
array you can do that by assigning to A(span, 2)
.
\[ ref = \begin{bmatrix} 8 \\ 9 \\ 10 \\ 11 \end{bmatrix} A = \begin{bmatrix} 0 & 4 & 3.14 & 12 \\ 1 & 5 & 3.14 & 13 \\ 2 & 6 & 3.14 & 14 \\ 3 & 7 & 3.14 & 15 \end{bmatrix} \]
This will update only the array being modified. If there are arrays that are referring to this array because of an indexing operation, those values will remain unchanged.
Allocation will only be performed if there are other arrays referencing the data at the point of assignment. In the previous example, an allocation will be performed when assigning to the A
array because the ref
array is pointing to the original data. Here is another example demonstrating when an allocation will occur:
In this example, no allocation will take place because when the ref
object is created, it is pointing to A
's data. Once it goes out of scope, no data points to A
, therefore when the assignment takes place, the data is modified in place instead of being copied to a new address.
You can also assign to arrays using another af::arrays as an indexing array. This works in a similar way to the other types of assignment but care must be taken to assure that the indexes are unique. Non-unique indexes will result in a race condition which will cause non-deterministic values.
\[ idx = \begin{bmatrix} 4 \\ 3 \\ 4 \\ 0 \end{bmatrix} vals = \begin{bmatrix} 9 \\ 8 \\ 7 \\ 6 \end{bmatrix} \\ A = \begin{bmatrix} 6 & 9\ or\ 7 & 8 & 12 \\ 1 & 5 & 9 & 13 \\ 2 & 6 & 10 & 14 \\ 8 & 7 & 11 & 15 \end{bmatrix} \]
There are several member functions which allow you to index into an af::array. These functions have similar functionality but may be easier to parse for some.
See Assignment & Indexing operation on arrays for the full listing.
You can set values in an array:
Use one array to reference into another.