|
There is one generic array container object while the underlying data may be one of various basic types:
bool
)float
)cfloat
)int
)unsigned
)double
)cdouble
)intl
)uintl
)short
)unsigned short
)Older devices may not support double precision operations.
ArrayFire arrays always exist on the device. They may be populated with data using an ArrayFire function, or filled with data found on the host. For example:
A complete list of ArrayFire functions that automatically generate data on the device may be found on the functions to create arrays page. The default data type for arrays is f32 (a 32-bit floating point number) unless specified otherwise.
ArrayFire arrays may also be populated from data found on the host. For example:
ArrayFire also supports array initialization from a device pointer. For example ArrayFire can be populated directly by a call to cudaMemcpy
The af_print function can be used to print arrays that have already been generated or an expression involving arrays:
ArrayFire provides several convenient methods for accessing the dimensions. You may use either a dim4 object or access the dimensions directly using the dims() and numdims() functions:
Arrays also provide functions to determine their properties including:
Most of ArrayFire's functions operate on an element-wise basis. This means that function like c[i] = a[i] + b[i]
could simply be written as c = a + b
. ArrayFire has an intelligent runtime JIT compliation engine which converts array expressions into the smallest number of OpenCL/CUDA kernels. 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.
ArrayFire has hundreds of functions for element-wise arithmetic. Here are a few examples:
ArrayFire contains several platform-independent constants, like Pi, NaN, and Inf. If ArrayFire does not have a constant you need, you can create your own using the af::constant array constructor.
Constants can be used in all of ArrayFire's functions. Below we demonstrate their use in element selection and a mathematical expression:
Please note that our constants may, at times, conflict with macro definitions in standard header files. When this occurs, please refer to our constants using the af::
namespace.
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::array
s you may use one or a combination of the following functions:
Please see the indexing page for several examples of how to use these functions.
Memory in af::array
s may be accessed using the host() and device()](af::array::device) functions. The host
function copies the data from the device and makes it available in a C-style array on the host. The device
function returns a pointer to device memory for interoperability with external CUDA/OpenCL kernels. For example, here is how we can interact with both OpenCL and CUDA:
ArrayFire also provides several helper functions for creating af::array
s from OpenCL cl_mem
references and cl::Buffer
objects. See the include/af/opencl.h
file for further information.
Lastly, if you want only the first value from an af::array
you can use get it using the scalar() function:
In addition to supporting standard mathematical functions, af::array
s that contain integer data types also support bitwise operators including and, or, and shift:
The ArrayFire API is wrapped into a unified C/C++ header. To use the library simply include the arrayfire.h
header file and start coding!
Now that you have a general introduction to ArrayFire, where do you go from here? In particular you might find these documents useful