Array allocation, initialization

Functions

 array ()
 Create non-dimensioned array (no data, undefined size) More...
 
 array (const dim4 &dims, dtype ty=f32)
 Allocate an array of a specified size with undefined contents. More...
 
 array (int d0, int d1, dtype ty=f32)
 Allocate a two-dimensional array of a specified size with undefined contents. More...
 
 array (int d0, int d1, int d2, dtype ty=f32)
 Allocate a three-dimensional (3D) array of a specified size with undefined contents. More...
 
 array (int d0, int d1, int d2, int d3, dtype ty=f32)
 Allocate a four-dimensional (4D) array of a specified size with undefined contents. More...
 
 array (const array &)
 Duplicate an existing array (copy constructor). More...
 
 array (const seq &s)
 Convert a seq object for use with arithmetic. More...
 
template<typename ty >
 array (unsigned dim0, const ty *pointer, af_source_t src=afHost, unsigned ngfor=0)
 Create a column vector on the device using a host/device pointer. More...
 
template<typename ty >
 array (unsigned dim0, unsigned dim1, const ty *pointer, af_source_t src=afHost, unsigned ngfor=0)
 Create a 2D array on the device using a host/device pointer. More...
 
template<typename ty >
 array (unsigned dim0, unsigned dim1, unsigned dim2, const ty *pointer, af_source_t src=afHost, unsigned ngfor=0)
 Create a 3D array on the device using a host/device pointer. More...
 
template<typename ty >
 array (unsigned dim0, unsigned dim1, unsigned dim2, unsigned dim3, const ty *pointer, af_source_t src=afHost, unsigned ngfor=0)
 Create a 4D array on the device using a host/device pointer. More...
 
template<typename ty >
 array (const dim4 &dims, const ty *pointer, af_source_t src=afHost, unsigned ngfor=0)
 Create an array of specified size on the device using a host/device pointer. More...
 
template<typename ty >
static array scalar (const ty value)
 Push scalar value to the device. More...
 

Detailed Description

Function Documentation

array ( )

Create non-dimensioned array (no data, undefined size)

array A, B, C; // creates three arrays called A, B and C
array ( const dim4 dims,
dtype  ty = f32 
)

Allocate an array of a specified size with undefined contents.

This can be useful when the dimensions of the array are calculated somewhere else within the code. The first parameter specifies the size of the array via dim4(). The second parameter is the type of the array. The default type is f32 or 4-byte single-precision floating point numbers.

// create a two-dimensional 10 x 10 array
dim4 dims(10, 10); // converted to (10, 10, 1, 1)
array a1(dims); // create the array (type is f32, the default)
// create a three-dimensional 10 x 10 x 20 array
dim4 dims(10, 10, 20); // converted to (10, 10, 20, 1)
array a2(dims,f64); // f64 = double precision
Parameters
[in]dimssize of the array
[in]tyoptional label describing the data type (default is f32)
array ( int  d0,
int  d1,
dtype  ty = f32 
)

Allocate a two-dimensional array of a specified size with undefined contents.

Declare a two-dimensional array by passing the number of rows and the number of columns as the first two parameters. The (optional) third parameter is the type of the array. The default type is f32 or 4-byte single-precision floating-point numbers.

// allocate space for an array with 10 rows and 8 columns
array A(10, 8); // type is the default f32
// allocate space for a column vector with 100 rows (and 1 column)
array A(100, 1, f64); // f64 = double precision
Parameters
[in]d0number of columns in the array
[in]d1number of rows in the array
[in]tyoptional label describing the data type (default is f32)
array ( int  d0,
int  d1,
int  d2,
dtype  ty = f32 
)

Allocate a three-dimensional (3D) array of a specified size with undefined contents.

This is useful to quickly declare a three-dimensional array by passing the size as the first three parameters. The (optional) fourth parameter is the type of the array. The default type is f32 or 4-byte single-precision floating point numbers.

// allocate space for a 10 x 10 x 10 array
array A(10, 10, 10); // type is the default f32
// allocate space for a 3D, double precision array
array A(10, 10, 10, f64); // f64 = double precision
Parameters
[in]d0first dimension of the array
[in]d1second dimension of the array
[in]d2third dimension of the array
[in]tyoptional label describing the data type (default is f32)
array ( int  d0,
int  d1,
int  d2,
int  d3,
dtype  ty = f32 
)

Allocate a four-dimensional (4D) array of a specified size with undefined contents.

This is useful to quickly declare a four-dimensional array by passing the size as the first four parameters. The (optional) fifth parameter is the type of the array. The default type is f32 or 4-byte floating point numbers.

// allocate space for a 10 x 10 x 10 x 20 array
array A(10, 10, 10, 20); // type is the default f32
// allocate space for a 4D, double precision array
array A(10, 10, 10, 20, f64); // f64 = double precision
Parameters
[in]d0first dimension of the array
[in]d1second dimension of the array
[in]d2third dimension of the array
[in]d3fourth dimension of the array
[in]tyoptional label describing the data type (default is f32)
array ( const array )

Duplicate an existing array (copy constructor).

int h_buffer[] = {1, 2, 3};
array A(3, h_buffer); // A = 1
// 2
// 3
array B(A); // B = 1
// 2
// 3
B -= 1; // B = 0
// 1
// 2
array ( const seq s)

Convert a seq object for use with arithmetic.

array A(seq(3)); // A = 0
// 1
// 2
array B(seq(0,3,10); // B = 0
// 3
// 6
// 9
array ( unsigned  dim0,
const ty *  pointer,
af_source_t  src = afHost,
unsigned  ngfor = 0 
)

Create a column vector on the device using a host/device pointer.

This function can be used to transfer data from a host or device pointer to an array object on the device with one column. The type of the array is automatically matched to the type of the data.

Depending on the specified size of the column vector, the data will be copied partially or completely. However, the user needs to be careful to ensure that the array size is not larger than the number of elements in the input buffer.

Parameters
[in]dim0number of elements in the column vector
[in]pointerpointer (points to a buffer on the host/device)
[in]srcsource of the data (default is afHost, can also be afDevice)
[in]ngfornumber of gfor tiles (default is ZERO)
// allocate data on the host
int h_buffer[] = {23, 34, 18, 99, 34};
array A(4, h_buffer); // copy host data to device
//
// A = 23
// = 34
// = 18
// = 99
array ( unsigned  dim0,
unsigned  dim1,
const ty *  pointer,
af_source_t  src = afHost,
unsigned  ngfor = 0 
)

Create a 2D array on the device using a host/device pointer.

This function copies data from the location specified by the pointer to a 2D array on the device. The data is arranged in "column-major" format (similar to that used by FORTRAN).

Note that this is an synchronous copy. The elements are not actually filled until this array is evaluated or used in the evaluation of some other expression that uses this array object.

Parameters
[in]dim0number of rows
[in]dim1number of columns
[in]pointerpointer (points to a buffer on the host/device)
[in]srcsource of the data (default is afHost, can also be afDevice)
[in]ngfornumber of gfor tiles (default is ZERO)
int h_buffer[] = {0, 1, 2, 3, 4, 5}; // host array
array A(2, 3, h_buffer); // copy host data to device
2dArray.png
array ( unsigned  dim0,
unsigned  dim1,
unsigned  dim2,
const ty *  pointer,
af_source_t  src = afHost,
unsigned  ngfor = 0 
)

Create a 3D array on the device using a host/device pointer.

This function copies data from the location specified by the pointer to a 3D array on the device. The data is arranged in "column-major" format (similar to that used by FORTRAN).

Parameters
[in]dim0first dimension
[in]dim1second dimension
[in]dim2third dimension
[in]pointerpointer (points to a buffer on the host/device)
[in]srcsource of the data (default is afHost, can also be afDevice)
[in]ngfornumber of gfor tiles (default is ZERO)
int h_buffer[] = {0, 1, 2, 3, 4, 5, 6, 7, 8
9, 0, 1, 2, 3, 4, 5, 6, 7}; // host array
array A(3, 3, 2, h_buffer); // copy host data to 3D device array
3dArray.png
array ( unsigned  dim0,
unsigned  dim1,
unsigned  dim2,
unsigned  dim3,
const ty *  pointer,
af_source_t  src = afHost,
unsigned  ngfor = 0 
)

Create a 4D array on the device using a host/device pointer.

This function copies data from the location specified by the pointer to a 4D array on the device. The data is arranged in "column-major" format (similar to that used by FORTRAN).

Parameters
[in]dim0first dimension
[in]dim1second dimension
[in]dim2third dimension
[in]dim3fourth dimension
[in]pointerpointer (points to a buffer on the host/device)
[in]srcsource of the data (default is afHost, can also be afDevice)
[in]ngfornumber of gfor tiles (default is ZERO)
int h_buffer[] = {0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 0, 1,
2, 3, 4, 5}; // host array with 16 elements
array A(2, 2, 2, 2, h_buffer); // copy host data to 4D device array
array ( const dim4 dims,
const ty *  pointer,
af_source_t  src = afHost,
unsigned  ngfor = 0 
)

Create an array of specified size on the device using a host/device pointer.

This function copies data from the location specified by the pointer to a 1D/2D/3D/4D array on the device. The data is arranged in "column-major" format (similar to that used by FORTRAN).

Parameters
[in]dimsvector data type containing the dimension of the array
[in]pointerpointer (points to a buffer on the host/device)
[in]srcsource of the data (default is afHost, can also be afDevice)
[in]ngfornumber of gfor tiles (default is ZERO)
int h_buffer[] = {0, 1, 2, 3, // host array with 16 elements
4, 5, 6, 7, // written in "row-major" format
8, 9, 0, 1,
2, 3, 4, 5};
dim4 dims(4, 4);
array A(dims, h_buffer); // A = 0 4 8 2
// 1 5 9 3
// 2 6 0 4
// 3 7 1 5
// Note the "column-major" ordering
// used in ArrayFire
static array scalar ( const ty  value)
static

Push scalar value to the device.

This operation is useful for extracting elements inside an array for use as regular variables in device memory. The template parameter can be used to specify what type is extracted from this function.

Although useful in some cases, it is recommended not to use this too often. It is recommended to copy large amounts of data to the device when possible.

array A(seq(5)); // A = 0 A(0) = 0
// 1 A(1) = 1
// 2 A(2) = 2
// 3 A(3) = 3
// 4 A(4) = 4
A += 0.33; // A = 0.33 A(0) = 0.33
// 1.33 A(1) = 1.33
// 2.33 A(2) = 2.33
// 3.33 A(3) = 3.33
// 4.33 A(4) = 4.33
int a = A(2).scalar<int>(); // A(2) = 2.33, a = 2
float b = A(2).scalar<float>(); // A(2) = 2.33, b = 2.33
Parameters
[in]valueThe initial value of the array.