My Project
array.h
Go to the documentation of this file.
1/*******************************************************
2 * Copyright (c) 2014, ArrayFire
3 * All rights reserved.
4 *
5 * This file is distributed under 3-clause BSD license.
6 * The complete license agreement can be obtained at:
7 * http://arrayfire.com/licenses/BSD-3-Clause
8 ********************************************************/
9
10#pragma once
11#include <af/defines.h>
12#include <af/seq.h>
13#include <af/util.h>
14#include <af/index.h>
15
16#ifdef __cplusplus
17#include <af/traits.hpp>
18#include <vector>
19namespace af
20{
21
22 class dim4;
23
27 class AFAPI array {
28 af_array arr;
29
30
31 public:
38 void set(af_array tmp);
39
46 {
47 struct array_proxy_impl; //forward declaration
48 array_proxy_impl *impl; // implementation
49
50 public:
51 array_proxy(array& par, af_index_t *ssss, bool linear = false);
52 array_proxy(const array_proxy &other);
53#if __cplusplus > 199711L
54 array_proxy(array_proxy &&other);
55 array_proxy & operator=(array_proxy &&other);
56#endif
58
59 // Implicit conversion operators
60 operator array() const;
61 operator array();
62
63#define ASSIGN(OP) \
64 array_proxy& operator OP(const array_proxy &a); \
65 array_proxy& operator OP(const array &a); \
66 array_proxy& operator OP(const double &a); \
67 array_proxy& operator OP(const cdouble &a); \
68 array_proxy& operator OP(const cfloat &a); \
69 array_proxy& operator OP(const float &a); \
70 array_proxy& operator OP(const int &a); \
71 array_proxy& operator OP(const unsigned &a); \
72 array_proxy& operator OP(const bool &a); \
73 array_proxy& operator OP(const char &a); \
74 array_proxy& operator OP(const unsigned char &a); \
75 array_proxy& operator OP(const long &a); \
76 array_proxy& operator OP(const unsigned long &a); \
77 array_proxy& operator OP(const long long &a); \
78 array_proxy& operator OP(const unsigned long long &a); \
79
85#undef ASSIGN
86
87#if AF_API_VERSION >= 32
88#define ASSIGN(OP) \
89 array_proxy& operator OP(const short &a); \
90 array_proxy& operator OP(const unsigned short &a); \
91
97#undef ASSIGN
98#endif
99
100 // af::array member functions. same behavior as those below
102 af_array get() const;
104 template<typename T> T* host() const;
105 void host(void *ptr) const;
106 dtype type() const;
107 dim4 dims() const;
108 dim_t dims(unsigned dim) const;
109 unsigned numdims() const;
110 size_t bytes() const;
111 array copy() const;
112 bool isempty() const;
113 bool isscalar() const;
114 bool isvector() const;
115 bool isrow() const;
116 bool iscolumn() const;
117 bool iscomplex() const;
118 inline bool isreal() const { return !iscomplex(); }
119 bool isdouble() const;
120 bool issingle() const;
121 bool isrealfloating() const;
122 bool isfloating() const;
123 bool isinteger() const;
124 bool isbool() const;
125 void eval() const;
126 array as(dtype type) const;
127 array T() const;
128 array H() const;
129 template<typename T> T scalar() const;
130 template<typename T> T* device() const;
131 void unlock() const;
132#if AF_API_VERSION >= 31
133 void lock() const;
134#endif
135
137 const array::array_proxy row(int index) const;
138
139 array::array_proxy rows(int first, int last);
140 const array::array_proxy rows(int first, int last) const;
141
143 const array::array_proxy col(int index) const;
144 array::array_proxy cols(int first, int last);
145 const array::array_proxy cols(int first, int last) const;
146
149
150 array::array_proxy slices(int first, int last);
151 const array::array_proxy slices(int first, int last) const;
152 };
153
154 //array(af_array in, const array *par, af_index_t seqs[4]);
167
172 explicit
173 array(const af_array handle);
174
180 array(const array& in);
181
204 array(dim_t dim0, dtype ty = f32);
205
229 array(dim_t dim0, dim_t dim1, dtype ty = f32);
230
255 array(dim_t dim0, dim_t dim1, dim_t dim2, dtype ty = f32);
256
282 array(dim_t dim0, dim_t dim1, dim_t dim2, dim_t dim3, dtype ty = f32);
283
309 explicit
310 array(const dim4& dims, dtype ty = f32);
311
336 template<typename T>
338 const T *pointer, af::source src=afHost);
339
340
359 template<typename T>
360 array(dim_t dim0, dim_t dim1,
361 const T *pointer, af::source src=afHost);
362
363
385 template<typename T>
386 array(dim_t dim0, dim_t dim1, dim_t dim2,
387 const T *pointer, af::source src=afHost);
388
389
412 template<typename T>
413 array(dim_t dim0, dim_t dim1, dim_t dim2, dim_t dim3,
414 const T *pointer, af::source src=afHost);
415
449 template<typename T>
450 explicit
451 array(const dim4& dims,
452 const T *pointer, af::source src=afHost);
453
481 array(const array& input, const dim4& dims);
482
514 array( const array& input,
515 const dim_t dim0, const dim_t dim1 = 1,
516 const dim_t dim2 = 1, const dim_t dim3 = 1);
517
531
535 af_array get() const;
536
541
545 template<typename T> T* host() const;
546
550 void host(void *ptr) const;
551
555 template<typename T> void write(const T *ptr, const size_t bytes, af::source src = afHost);
556
560 dtype type() const;
561
565 dim4 dims() const;
566
570 dim_t dims(unsigned dim) const;
571
575 unsigned numdims() const;
576
580 size_t bytes() const;
581
585 array copy() const;
586
590 bool isempty() const;
591
595 bool isscalar() const;
596
600 bool isvector() const;
601
605 bool isrow() const;
606
610 bool iscolumn() const;
611
615 bool iscomplex() const;
616
620 inline bool isreal() const { return !iscomplex(); }
621
625 bool isdouble() const;
626
630 bool issingle() const;
631
635 bool isrealfloating() const;
636
640 bool isfloating() const;
641
645 bool isinteger() const;
646
650 bool isbool() const;
651
655 void eval() const;
656
662 template<typename T> T scalar() const;
663
680 template<typename T> T* device() const;
685 // INDEXING
686 // Single arguments
687
706
712 const array::array_proxy operator()(const index &s0) const;
713
714
733 const index &s1,
734 const index &s2 = span,
735 const index &s3 = span);
736
743 const index &s1,
744 const index &s2 = span,
745 const index &s3 = span) const;
746
747
760 const array::array_proxy row(int index) const;
761
771 array::array_proxy rows(int first, int last);
772 const array::array_proxy rows(int first, int last) const;
774
787 const array::array_proxy col(int index) const;
788
798 array::array_proxy cols(int first, int last);
799 const array::array_proxy cols(int first, int last) const;
801
814 const array::array_proxy slice(int index) const;
815
824 array::array_proxy slices(int first, int last);
825 const array::array_proxy slices(int first, int last) const;
827
833 const array as(dtype type) const;
834
835
837
842 array T() const;
847 array H() const;
848
849#define ASSIGN_(OP) \
850 array& OP(const array &val); \
851 array& OP(const double &val); \
852 array& OP(const cdouble &val); \
853 array& OP(const cfloat &val); \
854 array& OP(const float &val); \
855 array& OP(const int &val); \
856 array& OP(const unsigned &val); \
857 array& OP(const bool &val); \
858 array& OP(const char &val); \
859 array& OP(const unsigned char &val); \
860 array& OP(const long &val); \
861 array& OP(const unsigned long &val); \
862 array& OP(const long long &val); \
863 array& OP(const unsigned long long &val); \
864
865#if AF_API_VERSION >= 32
866#define ASSIGN(OP) \
867 ASSIGN_(OP) \
868 array& OP(const short &val); \
869 array& OP(const unsigned short &val); \
870
871#else
872#define ASSIGN(OP) ASSIGN_(OP)
873#endif
874
875
885 ASSIGN(operator=)
887
897 ASSIGN(operator+=)
899
909 ASSIGN(operator-=)
911
921 ASSIGN(operator*=)
923
934 ASSIGN(operator/=)
936
937
938#undef ASSIGN
939#undef ASSIGN_
940
946 array operator -() const;
947
953 array operator !() const;
954
959 int nonzeros() const;
960
961
967 void lock() const;
968
974 void unlock() const;
975 };
976 // end of class array
977
978#define BIN_OP_(OP) \
979 AFAPI array OP (const array& lhs, const array& rhs); \
980 AFAPI array OP (const bool& lhs, const array& rhs); \
981 AFAPI array OP (const int& lhs, const array& rhs); \
982 AFAPI array OP (const unsigned& lhs, const array& rhs); \
983 AFAPI array OP (const char& lhs, const array& rhs); \
984 AFAPI array OP (const unsigned char& lhs, const array& rhs); \
985 AFAPI array OP (const long& lhs, const array& rhs); \
986 AFAPI array OP (const unsigned long& lhs, const array& rhs); \
987 AFAPI array OP (const long long& lhs, const array& rhs); \
988 AFAPI array OP (const unsigned long long& lhs, const array& rhs); \
989 AFAPI array OP (const double& lhs, const array& rhs); \
990 AFAPI array OP (const float& lhs, const array& rhs); \
991 AFAPI array OP (const cfloat& lhs, const array& rhs); \
992 AFAPI array OP (const cdouble& lhs, const array& rhs); \
993 AFAPI array OP (const array& lhs, const bool& rhs); \
994 AFAPI array OP (const array& lhs, const int& rhs); \
995 AFAPI array OP (const array& lhs, const unsigned& rhs); \
996 AFAPI array OP (const array& lhs, const char& rhs); \
997 AFAPI array OP (const array& lhs, const unsigned char& rhs); \
998 AFAPI array OP (const array& lhs, const long& rhs); \
999 AFAPI array OP (const array& lhs, const unsigned long& rhs); \
1000 AFAPI array OP (const array& lhs, const long long& rhs); \
1001 AFAPI array OP (const array& lhs, const unsigned long long& rhs); \
1002 AFAPI array OP (const array& lhs, const double& rhs); \
1003 AFAPI array OP (const array& lhs, const float& rhs); \
1004 AFAPI array OP (const array& lhs, const cfloat& rhs); \
1005 AFAPI array OP (const array& lhs, const cdouble& rhs); \
1006
1007#if AF_API_VERSION >= 32
1008#define BIN_OP(OP) \
1009 BIN_OP_(OP) \
1010 AFAPI array OP (const short& lhs, const array& rhs); \
1011 AFAPI array OP (const unsigned short& lhs, const array& rhs); \
1012 AFAPI array OP (const array& lhs, const short& rhs); \
1013 AFAPI array OP (const array& lhs, const unsigned short& rhs); \
1014
1015#else
1016#define BIN_OP(OP) BIN_OP_(OP)
1017#endif
1018
1027 BIN_OP(operator+ )
1029
1038 BIN_OP(operator- )
1040
1049 BIN_OP(operator* )
1051
1060 BIN_OP(operator/ )
1062
1071 BIN_OP(operator==)
1073
1083 BIN_OP(operator!=)
1085
1095 BIN_OP(operator< )
1097
1107 BIN_OP(operator<=)
1119 BIN_OP(operator> )
1131 BIN_OP(operator>=)
1144 BIN_OP(operator&&)
1146
1159
1171
1184
1197
1208 BIN_OP(operator^ )
1210
1221 BIN_OP(operator<<)
1223
1234 BIN_OP(operator>>)
1236
1237#undef BIN_OP
1238#undef BIN_OP_
1239
1241
1245 inline array &eval(array &a) { a.eval(); return a; }
1246 inline void eval(array &a, array &b) { eval(a); b.eval(); }
1247 inline void eval(array &a, array &b, array &c) { eval(a, b); c.eval(); }
1248 inline void eval(array &a, array &b, array &c, array &d) { eval(a, b, c); d.eval(); }
1249 inline void eval(array &a, array &b, array &c, array &d, array &e) { eval(a, b, c, d); e.eval(); }
1250 inline void eval(array &a, array &b, array &c, array &d, array &e, array &f) { eval(a, b, c, d, e); f.eval(); }
1255}
1256#endif
1257
1258#ifdef __cplusplus
1259extern "C" {
1260#endif
1261
1280 AFAPI af_err af_create_array(af_array *arr, const void * const data, const unsigned ndims, const dim_t * const dims, const af_dtype type);
1281
1292 AFAPI af_err af_create_handle(af_array *arr, const unsigned ndims, const dim_t * const dims, const af_dtype type);
1293
1304 AFAPI af_err af_copy_array(af_array *arr, const af_array in);
1305
1309 AFAPI af_err af_write_array(af_array arr, const void *data, const size_t bytes, af_source src);
1310
1316 AFAPI af_err af_get_data_ptr(void *data, const af_array arr);
1317
1322
1327
1328#if AF_API_VERSION >= 31
1335 AFAPI af_err af_get_data_ref_count(int *use_count, const af_array in);
1336#endif
1337
1338
1343
1360 AFAPI af_err af_get_elements(dim_t *elems, const af_array arr);
1361
1370 AFAPI af_err af_get_type(af_dtype *type, const af_array arr);
1371
1383 AFAPI af_err af_get_dims(dim_t *d0, dim_t *d1, dim_t *d2, dim_t *d3,
1384 const af_array arr);
1385
1394 AFAPI af_err af_get_numdims(unsigned *result, const af_array arr);
1395
1404 AFAPI af_err af_is_empty (bool *result, const af_array arr);
1405
1414 AFAPI af_err af_is_scalar (bool *result, const af_array arr);
1415
1424 AFAPI af_err af_is_row (bool *result, const af_array arr);
1425
1434 AFAPI af_err af_is_column (bool *result, const af_array arr);
1435
1446 AFAPI af_err af_is_vector (bool *result, const af_array arr);
1447
1456 AFAPI af_err af_is_complex (bool *result, const af_array arr);
1457
1468 AFAPI af_err af_is_real (bool *result, const af_array arr);
1469
1478 AFAPI af_err af_is_double (bool *result, const af_array arr);
1479
1488 AFAPI af_err af_is_single (bool *result, const af_array arr);
1489
1498 AFAPI af_err af_is_realfloating (bool *result, const af_array arr);
1499
1510 AFAPI af_err af_is_floating (bool *result, const af_array arr);
1511
1520 AFAPI af_err af_is_integer (bool *result, const af_array arr);
1521
1530 AFAPI af_err af_is_bool (bool *result, const af_array arr);
1535#ifdef __cplusplus
1536}
1537#endif
#define BIN_OP(OP)
(const array&, const array&)
Definition array.h:1039
#define ASSIGN(OP)
(const array &)
Definition array.h:63
Intermediate data class.
Definition array.h:46
array::array_proxy slices(int first, int last)
bool isrealfloating() const
bool iscomplex() const
array as(dtype type) const
size_t bytes() const
bool isinteger() const
const array::array_proxy slice(int index) const
void host(void *ptr) const
array::array_proxy slice(int index)
array_proxy(const array_proxy &other)
dim_t dims(unsigned dim) const
dim_t elements() const
array_proxy(array &par, af_index_t *ssss, bool linear=false)
const array::array_proxy rows(int first, int last) const
const array::array_proxy row(int index) const
const array::array_proxy cols(int first, int last) const
bool isfloating() const
array::array_proxy row(int index)
array::array_proxy cols(int first, int last)
array::array_proxy rows(int first, int last)
const array::array_proxy slices(int first, int last) const
af_array get() const
array::array_proxy col(int index)
bool isreal() const
Definition array.h:118
const array::array_proxy col(int index) const
unsigned numdims() const
A multi dimensional data container.
Definition array.h:27
void set(af_array tmp)
Updates the internal af_array object.
Definition dim4.hpp:27
Wrapper for af_index.
Definition index.h:52
af_dtype
Definition defines.h:195
@ f32
32-bit floating point values
Definition defines.h:196
long long dim_t
Definition defines.h:50
af_err
Definition defines.h:63
void * af_array
Definition defines.h:222
#define AFAPI
Definition defines.h:31
af_source
Definition defines.h:214
@ afHost
Host pointer.
Definition defines.h:216
const array::array_proxy cols(int first, int last) const
Returns a reference to sequence of columns.
array::array_proxy cols(int first, int last)
Returns a reference to sequence of columns.
array::array_proxy col(int index)
Returns a reference to a col.
const array::array_proxy col(int index) const
Returns a reference to a col.
array::array_proxy operator()(const index &s0)
This operator returns a reference of the original array at a given coordinate.
const array::array_proxy operator()(const index &s0, const index &s1, const index &s2=span, const index &s3=span) const
This operator returns a reference of the original array at a given coordinate.
const array::array_proxy operator()(const index &s0) const
This operator returns a reference of the original array at a given coordinate.
array::array_proxy operator()(const index &s0, const index &s1, const index &s2=span, const index &s3=span)
This operator returns a reference of the original array at a given coordinate.
const array::array_proxy rows(int first, int last) const
Returns a reference to sequence of rows.
const array::array_proxy row(int index) const
Returns a reference to a row.
array::array_proxy row(int index)
Returns a reference to a row.
array::array_proxy rows(int first, int last)
Returns a reference to sequence of rows.
array::array_proxy slices(int first, int last)
Returns a reference to a matrix in a volume.
const array::array_proxy slice(int index) const
Returns a reference to a matrix in a volume.
array::array_proxy slice(int index)
Returns a reference to a matrix in a volume.
const array::array_proxy slices(int first, int last) const
Returns a reference to a matrix in a volume.
array(const dim4 &dims, const T *pointer, af::source src=afHost)
Create an array of specified size on the device using a host/device pointer.
array(dim_t dim0, dim_t dim1, dtype ty=f32)
Allocate a two-dimensional array of a specified size with undefined contents.
AFAPI af_err af_create_handle(af_array *arr, const unsigned ndims, const dim_t *const dims, const af_dtype type)
Create af_array handle.
array(const array &input, const dim_t dim0, const dim_t dim1=1, const dim_t dim2=1, const dim_t dim3=1)
Adjust the dimensions of an N-D array (fast).
array(const dim4 &dims, dtype ty=f32)
Allocate an array of a specified size with undefined contents.
array(dim_t dim0, dim_t dim1, dim_t dim2, dtype ty=f32)
Allocate a three-dimensional (3D) array of a specified size with undefined contents.
array(dim_t dim0, dim_t dim1, dim_t dim2, dim_t dim3, dtype ty=f32)
Allocate a four-dimensional (4D) array of a specified size with undefined contents.
array(dim_t dim0, dtype ty=f32)
Allocate a one-dimensional array of a specified size with undefined contents.
AFAPI af_err af_create_array(af_array *arr, const void *const data, const unsigned ndims, const dim_t *const dims, const af_dtype type)
Create an af_array handle initialized with user defined data.
array(dim_t dim0, const T *pointer, af::source src=afHost)
Create a column vector on the device using a host/device pointer.
array()
Create undimensioned array (no data, undefined size)
array(dim_t dim0, dim_t dim1, const T *pointer, af::source src=afHost)
Create a 2D array on the device using a host/device pointer.
array(dim_t dim0, dim_t dim1, dim_t dim2, dim_t dim3, const T *pointer, af::source src=afHost)
Create a 4D array on the device using a host/device pointer.
array(const array &input, const dim4 &dims)
Adjust the dimensions of an N-D array (fast).
array(dim_t dim0, dim_t dim1, dim_t dim2, const T *pointer, af::source src=afHost)
Create a 3D array on the device using a host/device pointer.
array(const array &in)
Creates a copy to the in array.
array(const af_array handle)
Creates an array from an af_array handle.
T * device() const
AFAPI af_err af_is_vector(bool *result, const af_array arr)
Check if an array is a vector.
bool isempty() const
Returns true of the array is empty.
T scalar() const
Get the first element of the array as a scalar.
AFAPI af_err af_get_type(af_dtype *type, const af_array arr)
Gets the type of an array.
AFAPI af_err af_copy_array(af_array *arr, const af_array in)
Deep copy an array to another.
dim4 dims() const
Get dimensions of the array.
dtype type() const
Get array data type.
AFAPI af_err af_is_column(bool *result, const af_array arr)
Check if an array is a column vector.
AFAPI af_err af_is_empty(bool *result, const af_array arr)
Check if an array is empty.
AFAPI af_err af_is_single(bool *result, const af_array arr)
Check if an array is single precision type.
array H() const
Get the conjugate-transpose of the current array.
bool isvector() const
Returns true if only one of the array dimensions has more than one element.
bool isrealfloating() const
Returns true if the array type is f32 or f64.
bool iscomplex() const
Returns true if the array type is c32 or c64.
AFAPI af_err af_get_data_ref_count(int *use_count, const af_array in)
Get the use count of af_array
size_t bytes() const
Get the size of the array in bytes.
bool isscalar() const
Returns true of the array contains only one value.
T * host() const
Copy array data to host and return host pointer.
bool isinteger() const
Returns true if the array type is u8, b8, s32 u32, s64, u64, s16, u16.
const array as(dtype type) const
Converts the array into another type.
void host(void *ptr) const
Copy array data to existing host pointer.
AFAPI af_err af_is_floating(bool *result, const af_array arr)
Check if an array is floating precision type.
AFAPI af_err af_get_data_ptr(void *data, const af_array arr)
Copy data from an af_array to a C pointer.
AFAPI af_err af_get_elements(dim_t *elems, const af_array arr)
Gets the number of elements in an array.
void eval() const
Evaluate any JIT expressions to generate data for the array.
AFAPI af_err af_is_realfloating(bool *result, const af_array arr)
Check if an array is real floating point type.
bool isbool() const
Returns true if the array type is b8.
AFAPI af_err af_is_integer(bool *result, const af_array arr)
Check if an array is integer type.
AFAPI af_err af_retain_array(af_array *out, const af_array in)
Increments an af_array reference count.
array T() const
Get the transposed the array.
bool issingle() const
Returns true if the array type is neither f64 nor c64.
dim_t dims(unsigned dim) const
Get dimensions of the array.
dim_t elements() const
get the number of elements in array
AFAPI af_err af_get_dims(dim_t *d0, dim_t *d1, dim_t *d2, dim_t *d3, const af_array arr)
Gets the dimseions of an array.
bool isrow() const
Returns true if only the second dimension has more than one element.
AFAPI af_err af_eval(af_array in)
Evaluate any expressions in the Array.
void write(const T *ptr, const size_t bytes, af::source src=afHost)
Perform deep copy from host/device pointer to an existing array.
array copy() const
Perform deep copy of the array.
array & eval(array &a)
Evaluate an expression (nonblocking).
Definition array.h:1283
af_array get()
get the af_array handle
bool isfloating() const
Returns true if the array type is f32, f64, c32 or c64.
AFAPI af_err af_is_row(bool *result, const af_array arr)
Check if an array is row vector.
AFAPI af_err af_is_complex(bool *result, const af_array arr)
Check if an array is complex type.
AFAPI af_err af_release_array(af_array arr)
Reduce the reference count of the af_array.
AFAPI af_err af_is_double(bool *result, const af_array arr)
Check if an array is double precision type.
bool isdouble() const
Returns true if the array type is f64 or c64.
af_array get() const
get the af_array handle
AFAPI af_err af_is_scalar(bool *result, const af_array arr)
Check if an array is scalar, ie.
bool isreal() const
Returns true if the array type is neither c32 nor c64.
Definition array.h:620
AFAPI af_err af_get_numdims(unsigned *result, const af_array arr)
Gets the number of dimensions of an array.
unsigned numdims() const
Get the number of dimensions of the array.
bool iscolumn() const
Returns true if only the first dimension has more than one element.
AFAPI af_err af_is_real(bool *result, const af_array arr)
Check if an array is real type.
AFAPI af_err af_is_bool(bool *result, const af_array arr)
Check if an array is bool type.
AFAPI af_err af_write_array(af_array arr, const void *data, const size_t bytes, af_source src)
Copy data from a C pointer (host/device) to an existing array.
AFAPI array sum(const array &in, const int dim=-1)
C++ Interface for sum of elements in an array.
Definition algorithm.h:15
Struct used while indexing af_array.
Definition index.h:23