array.h Source File

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>
19 namespace 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
45 class AFAPI array_proxy
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
57 ~array_proxy();
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
80 ASSIGN(=)
81 ASSIGN(+=)
82 ASSIGN(-=)
83 ASSIGN(*=)
84 ASSIGN(/=)
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
92 ASSIGN(=)
93 ASSIGN(+=)
94 ASSIGN(-=)
95 ASSIGN(*=)
96 ASSIGN(/=)
97 #undef ASSIGN
98 #endif
99
100 // af::array member functions. same behavior as those below
101 af_array get();
102 af_array get() const;
103 dim_t elements() 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 #if AF_API_VERSION >= 34
126 bool issparse() const;
127 #endif
128 void eval() const;
129 array as(dtype type) const;
130 array T() const;
131 array H() const;
132 template<typename T> T scalar() const;
133 template<typename T> T* device() const;
134 void unlock() const;
135 #if AF_API_VERSION >= 31
136 void lock() const;
137 #endif
138
139 #if AF_API_VERSION >= 34
140 bool isLocked() const;
141 #endif
142
143 array::array_proxy row(int index);
144 const array::array_proxy row(int index) const;
145
146 array::array_proxy rows(int first, int last);
147 const array::array_proxy rows(int first, int last) const;
148
149 array::array_proxy col(int index);
150 const array::array_proxy col(int index) const;
151 array::array_proxy cols(int first, int last);
152 const array::array_proxy cols(int first, int last) const;
153
154 array::array_proxy slice(int index);
155 const array::array_proxy slice(int index) const;
156
157 array::array_proxy slices(int first, int last);
158 const array::array_proxy slices(int first, int last) const;
159 };
160
161 //array(af_array in, const array *par, af_index_t seqs[4]);
173 array();
174
179 explicit
180 array(const af_array handle);
181
187 array(const array& in);
188
211 array(dim_t dim0, dtype ty = f32);
212
236 array(dim_t dim0, dim_t dim1, dtype ty = f32);
237
262 array(dim_t dim0, dim_t dim1, dim_t dim2, dtype ty = f32);
263
289 array(dim_t dim0, dim_t dim1, dim_t dim2, dim_t dim3, dtype ty = f32);
290
316 explicit
317 array(const dim4& dims, dtype ty = f32);
318
343 template<typename T>
344 array(dim_t dim0,
345 const T *pointer, af::source src=afHost);
346
347
366 template<typename T>
367 array(dim_t dim0, dim_t dim1,
368 const T *pointer, af::source src=afHost);
369
370
392 template<typename T>
393 array(dim_t dim0, dim_t dim1, dim_t dim2,
394 const T *pointer, af::source src=afHost);
395
396
419 template<typename T>
420 array(dim_t dim0, dim_t dim1, dim_t dim2, dim_t dim3,
421 const T *pointer, af::source src=afHost);
422
456 template<typename T>
457 explicit
458 array(const dim4& dims,
459 const T *pointer, af::source src=afHost);
460
488 array(const array& input, const dim4& dims);
489
521 array( const array& input,
522 const dim_t dim0, const dim_t dim1 = 1,
523 const dim_t dim2 = 1, const dim_t dim3 = 1);
524
537 af_array get();
538
542 af_array get() const;
543
547 dim_t elements() const;
548
552 template<typename T> T* host() const;
553
557 void host(void *ptr) const;
558
562 template<typename T> void write(const T *ptr, const size_t bytes, af::source src = afHost);
563
567 dtype type() const;
568
572 dim4 dims() const;
573
577 dim_t dims(unsigned dim) const;
578
582 unsigned numdims() const;
583
587 size_t bytes() const;
588
592 array copy() const;
593
597 bool isempty() const;
598
602 bool isscalar() const;
603
607 bool isvector() const;
608
612 bool isrow() const;
613
617 bool iscolumn() const;
618
622 bool iscomplex() const;
623
627 inline bool isreal() const { return !iscomplex(); }
628
632 bool isdouble() const;
633
637 bool issingle() const;
638
642 bool isrealfloating() const;
643
647 bool isfloating() const;
648
652 bool isinteger() const;
653
657 bool isbool() const;
658
659 #if AF_API_VERSION >= 34
660
663 bool issparse() const;
664 #endif
665
669 void eval() const;
670
676 template<typename T> T scalar() const;
677
694 template<typename T> T* device() const;
699 // INDEXING
700 // Single arguments
701
719 array::array_proxy operator()(const index &s0);
720
726 const array::array_proxy operator()(const index &s0) const;
727
728
746 array::array_proxy operator()(const index &s0,
747 const index &s1,
748 const index &s2 = span,
749 const index &s3 = span);
750
756 const array::array_proxy operator()(const index &s0,
757 const index &s1,
758 const index &s2 = span,
759 const index &s3 = span) const;
760
761
773 array::array_proxy row(int index);
774 const array::array_proxy row(int index) const;
775
785 array::array_proxy rows(int first, int last);
786 const array::array_proxy rows(int first, int last) const;
787
800 array::array_proxy col(int index);
801 const array::array_proxy col(int index) const;
802
812 array::array_proxy cols(int first, int last);
813 const array::array_proxy cols(int first, int last) const;
814
827 array::array_proxy slice(int index);
828 const array::array_proxy slice(int index) const;
829
838 array::array_proxy slices(int first, int last);
839 const array::array_proxy slices(int first, int last) const;
840
847 const array as(dtype type) const;
848
849
850 ~array();
851
856 array T() const;
861 array H() const;
862
863 #define ASSIGN_(OP) \
864 array& OP(const array &val); \
865 array& OP(const double &val); \
866 array& OP(const cdouble &val); \
867 array& OP(const cfloat &val); \
868 array& OP(const float &val); \
869 array& OP(const int &val); \
870 array& OP(const unsigned &val); \
871 array& OP(const bool &val); \
872 array& OP(const char &val); \
873 array& OP(const unsigned char &val); \
874 array& OP(const long &val); \
875 array& OP(const unsigned long &val); \
876 array& OP(const long long &val); \
877 array& OP(const unsigned long long &val); \
878
879 #if AF_API_VERSION >= 32
880 #define ASSIGN(OP) \
881 ASSIGN_(OP) \
882 array& OP(const short &val); \
883 array& OP(const unsigned short &val); \
884
885 #else
886 #define ASSIGN(OP) ASSIGN_(OP)
887 #endif
888
889
899 ASSIGN(operator=)
901
911 ASSIGN(operator+=)
913
923 ASSIGN(operator-=)
925
935 ASSIGN(operator*=)
937
948 ASSIGN(operator/=)
950
951
952 #undef ASSIGN
953 #undef ASSIGN_
954
960 array operator -() const;
961
967 array operator !() const;
968
973 int nonzeros() const;
974
975
981 void lock() const;
982
983
984 #if AF_API_VERSION >= 34
985 bool isLocked() const;
991 #endif
992
993
999 void unlock() const;
1000 };
1001 // end of class array
1002
1003 #define BIN_OP_(OP) \
1004 AFAPI array OP (const array& lhs, const array& rhs); \
1005 AFAPI array OP (const bool& lhs, const array& rhs); \
1006 AFAPI array OP (const int& lhs, const array& rhs); \
1007 AFAPI array OP (const unsigned& lhs, const array& rhs); \
1008 AFAPI array OP (const char& lhs, const array& rhs); \
1009 AFAPI array OP (const unsigned char& lhs, const array& rhs); \
1010 AFAPI array OP (const long& lhs, const array& rhs); \
1011 AFAPI array OP (const unsigned long& lhs, const array& rhs); \
1012 AFAPI array OP (const long long& lhs, const array& rhs); \
1013 AFAPI array OP (const unsigned long long& lhs, const array& rhs); \
1014 AFAPI array OP (const double& lhs, const array& rhs); \
1015 AFAPI array OP (const float& lhs, const array& rhs); \
1016 AFAPI array OP (const cfloat& lhs, const array& rhs); \
1017 AFAPI array OP (const cdouble& lhs, const array& rhs); \
1018 AFAPI array OP (const array& lhs, const bool& rhs); \
1019 AFAPI array OP (const array& lhs, const int& rhs); \
1020 AFAPI array OP (const array& lhs, const unsigned& rhs); \
1021 AFAPI array OP (const array& lhs, const char& rhs); \
1022 AFAPI array OP (const array& lhs, const unsigned char& rhs); \
1023 AFAPI array OP (const array& lhs, const long& rhs); \
1024 AFAPI array OP (const array& lhs, const unsigned long& rhs); \
1025 AFAPI array OP (const array& lhs, const long long& rhs); \
1026 AFAPI array OP (const array& lhs, const unsigned long long& rhs); \
1027 AFAPI array OP (const array& lhs, const double& rhs); \
1028 AFAPI array OP (const array& lhs, const float& rhs); \
1029 AFAPI array OP (const array& lhs, const cfloat& rhs); \
1030 AFAPI array OP (const array& lhs, const cdouble& rhs); \
1031
1032 #if AF_API_VERSION >= 32
1033 #define BIN_OP(OP) \
1034 BIN_OP_(OP) \
1035 AFAPI array OP (const short& lhs, const array& rhs); \
1036 AFAPI array OP (const unsigned short& lhs, const array& rhs); \
1037 AFAPI array OP (const array& lhs, const short& rhs); \
1038 AFAPI array OP (const array& lhs, const unsigned short& rhs); \
1039
1040 #else
1041 #define BIN_OP(OP) BIN_OP_(OP)
1042 #endif
1043
1052 BIN_OP(operator+ )
1054
1063 BIN_OP(operator- )
1065
1074 BIN_OP(operator* )
1076
1085 BIN_OP(operator/ )
1087
1096 BIN_OP(operator==)
1098
1108 BIN_OP(operator!=)
1110
1120 BIN_OP(operator< )
1122
1132 BIN_OP(operator<=)
1134
1144 BIN_OP(operator> )
1146
1156 BIN_OP(operator>=)
1158
1169 BIN_OP(operator&&)
1171
1182 BIN_OP(operator||)
1184
1194 BIN_OP(operator% )
1196
1207 BIN_OP(operator& )
1209
1220 BIN_OP(operator| )
1222
1233 BIN_OP(operator^ )
1235
1246 BIN_OP(operator<<)
1248
1259 BIN_OP(operator>>)
1261
1262 #undef BIN_OP
1263 #undef BIN_OP_
1264
1266
1270 inline array &eval(array &a) { a.eval(); return a; }
1271
1272 #if AF_API_VERSION >= 34
1273 AFAPI void eval(int num, array **arrays);
1277 #endif
1278
1279 inline void eval(array &a, array &b)
1280 {
1281 #if AF_API_VERSION >= 34
1282 array *arrays[] = {&a, &b};
1283 return eval(2, arrays);
1284 #else
1285 eval(a); b.eval();
1286 #endif
1287 }
1288
1289 inline void eval(array &a, array &b, array &c)
1290 {
1291 #if AF_API_VERSION >= 34
1292 array *arrays[] = {&a, &b, &c};
1293 return eval(3, arrays);
1294 #else
1295 eval(a, b); c.eval();
1296 #endif
1297 }
1298
1299 inline void eval(array &a, array &b, array &c, array &d)
1300 {
1301 #if AF_API_VERSION >= 34
1302 array *arrays[] = {&a, &b, &c, &d};
1303 return eval(4, arrays);
1304 #else
1305 eval(a, b, c); d.eval();
1306 #endif
1307
1308 }
1309
1310 inline void eval(array &a, array &b, array &c, array &d, array &e)
1311 {
1312 #if AF_API_VERSION >= 34
1313 array *arrays[] = {&a, &b, &c, &d, &e};
1314 return eval(5, arrays);
1315 #else
1316 eval(a, b, c, d); e.eval();
1317 #endif
1318 }
1319
1320 inline void eval(array &a, array &b, array &c, array &d, array &e, array &f)
1321 {
1322 #if AF_API_VERSION >= 34
1323 array *arrays[] = {&a, &b, &c, &d, &e, &f};
1324 return eval(6, arrays);
1325 #else
1326 eval(a, b, c, d, e); f.eval();
1327 #endif
1328 }
1329
1330 #if AF_API_VERSION >= 34
1331 AFAPI void setManualEvalFlag(bool flag);
1335 #endif
1336
1337 #if AF_API_VERSION >= 34
1338 AFAPI bool getManualEvalFlag();
1340 #endif
1341
1346 }
1347 #endif
1348
1349 #ifdef __cplusplus
1350 extern "C" {
1351 #endif
1352
1371 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);
1372
1383 AFAPI af_err af_create_handle(af_array *arr, const unsigned ndims, const dim_t * const dims, const af_dtype type);
1384
1395 AFAPI af_err af_copy_array(af_array *arr, const af_array in);
1396
1400 AFAPI af_err af_write_array(af_array arr, const void *data, const size_t bytes, af_source src);
1401
1407 AFAPI af_err af_get_data_ptr(void *data, const af_array arr);
1408
1412 AFAPI af_err af_release_array(af_array arr);
1413
1417 AFAPI af_err af_retain_array(af_array *out, const af_array in);
1418
1419 #if AF_API_VERSION >= 31
1420
1426 AFAPI af_err af_get_data_ref_count(int *use_count, const af_array in);
1427 #endif
1428
1429
1433 AFAPI af_err af_eval(af_array in);
1434
1440 #if AF_API_VERSION >= 34
1441
1444 AFAPI af_err af_eval_multiple(const int num, af_array *arrays);
1448 #endif
1449
1450 #if AF_API_VERSION >= 34
1451
1454 AFAPI af_err af_set_manual_eval_flag(bool flag);
1458 #endif
1459
1460 #if AF_API_VERSION >= 34
1461
1464 AFAPI af_err af_get_manual_eval_flag(bool *flag);
1468 #endif
1469
1482 AFAPI af_err af_get_elements(dim_t *elems, const af_array arr);
1483
1492 AFAPI af_err af_get_type(af_dtype *type, const af_array arr);
1493
1505 AFAPI af_err af_get_dims(dim_t *d0, dim_t *d1, dim_t *d2, dim_t *d3,
1506 const af_array arr);
1507
1516 AFAPI af_err af_get_numdims(unsigned *result, const af_array arr);
1517
1526 AFAPI af_err af_is_empty (bool *result, const af_array arr);
1527
1536 AFAPI af_err af_is_scalar (bool *result, const af_array arr);
1537
1546 AFAPI af_err af_is_row (bool *result, const af_array arr);
1547
1556 AFAPI af_err af_is_column (bool *result, const af_array arr);
1557
1568 AFAPI af_err af_is_vector (bool *result, const af_array arr);
1569
1578 AFAPI af_err af_is_complex (bool *result, const af_array arr);
1579
1590 AFAPI af_err af_is_real (bool *result, const af_array arr);
1591
1600 AFAPI af_err af_is_double (bool *result, const af_array arr);
1601
1610 AFAPI af_err af_is_single (bool *result, const af_array arr);
1611
1620 AFAPI af_err af_is_realfloating (bool *result, const af_array arr);
1621
1632 AFAPI af_err af_is_floating (bool *result, const af_array arr);
1633
1642 AFAPI af_err af_is_integer (bool *result, const af_array arr);
1643
1652 AFAPI af_err af_is_bool (bool *result, const af_array arr);
1653
1654 #if AF_API_VERSION >= 34
1655
1663 AFAPI af_err af_is_sparse (bool *result, const af_array arr);
1667 #endif
1668 #ifdef __cplusplus
1669 }
1670 #endif
af_set_manual_eval_flag
AFAPI af_err af_set_manual_eval_flag(bool flag)
Turn the manual eval flag on or off.
af::setManualEvalFlag
AFAPI void setManualEvalFlag(bool flag)
Turn the manual eval flag on or off.
traits.hpp
af_is_row
AFAPI af_err af_is_row(bool *result, const af_array arr)
Check if an array is row vector.
af_is_real
AFAPI af_err af_is_real(bool *result, const af_array arr)
Check if an array is real type.
af::array::array_proxy::isreal
bool isreal() const
Definition: array.h:118
af
Definition: algorithm.h:14
af_is_realfloating
AFAPI af_err af_is_realfloating(bool *result, const af_array arr)
Check if an array is real floating point type.
af_copy_array
AFAPI af_err af_copy_array(af_array *arr, const af_array in)
Deep copy an array to another.
seq.h
af_is_sparse
AFAPI af_err af_is_sparse(bool *result, const af_array arr)
Check if an array is sparse.
af_eval_multiple
AFAPI af_err af_eval_multiple(const int num, af_array *arrays)
Evaluate multiple arrays together.
af_get_data_ptr
AFAPI af_err af_get_data_ptr(void *data, const af_array arr)
Copy data from an af_array to a C pointer.
ASSIGN
#define ASSIGN(OP)
(const array &)
Definition: array.h:896
af_is_complex
AFAPI af_err af_is_complex(bool *result, const af_array arr)
Check if an array is complex type.
af::array::eval
void eval() const
Evaluate any JIT expressions to generate data for the array.
af::array
A multi dimensional data container.
Definition: array.h:27
util.h
af_is_bool
AFAPI af_err af_is_bool(bool *result, const af_array arr)
Check if an array is bool type.
af_is_double
AFAPI af_err af_is_double(bool *result, const af_array arr)
Check if an array is double precision type.
af_is_vector
AFAPI af_err af_is_vector(bool *result, const af_array arr)
Check if an array is a vector.
af_is_integer
AFAPI af_err af_is_integer(bool *result, const af_array arr)
Check if an array is integer type.
index.h
af::eval
array & eval(array &a)
Evaluate an expression (nonblocking).
Definition: array.h:1308
af::array::isreal
bool isreal() const
Returns true if the array type is neither c32 nor c64.
Definition: array.h:627
af_index_t
Struct used while indexing af_array.
Definition: index.h:23
af_err
af_err
Definition: defines.h:63
af::span
AFAPI seq span
af_source
af_source
Definition: defines.h:214
defines.h
af_get_type
AFAPI af_err af_get_type(af_dtype *type, const af_array arr)
Gets the type of an array.
dim_t
long long dim_t
Definition: defines.h:50
BIN_OP
#define BIN_OP(OP)
(const array&, const array&)
Definition: array.h:1064
af_is_single
AFAPI af_err af_is_single(bool *result, const af_array arr)
Check if an array is single precision type.
af_get_dims
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.
af::index
Wrapper for af_index.
Definition: index.h:52
AFAPI
#define AFAPI
Definition: defines.h:31
af_is_scalar
AFAPI af_err af_is_scalar(bool *result, const af_array arr)
Check if an array is scalar, ie.
af_get_elements
AFAPI af_err af_get_elements(dim_t *elems, const af_array arr)
Gets the number of elements in an array.
af::copy
AFAPI void copy(array &dst, const array &src, const index &idx0, const index &idx1=span, const index &idx2=span, const index &idx3=span)
Copy the values of an input array based on index.
af_get_numdims
AFAPI af_err af_get_numdims(unsigned *result, const af_array arr)
Gets the number of dimensions of an array.
af_is_empty
AFAPI af_err af_is_empty(bool *result, const af_array arr)
Check if an array is empty.
af_release_array
AFAPI af_err af_release_array(af_array arr)
Reduce the reference count of the af_array.
af_create_array
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.
afcl::array
static af::array array(af::dim4 idims, cl_mem buf, af::dtype type, bool retain=false)
Create an af::array object from an OpenCL cl_mem buffer.
Definition: opencl.h:327
af_is_column
AFAPI af_err af_is_column(bool *result, const af_array arr)
Check if an array is a column vector.
af_retain_array
AFAPI af_err af_retain_array(af_array *out, const af_array in)
Increments an af_array reference count.
af_array
void * af_array
Definition: defines.h:222
af::operator-
AFAPI array operator-(const array &lhs, const array &rhs)
Subtracts two arrays or an array and a value.
af_write_array
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.
af_create_handle
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.
f32
32-bit floating point values
Definition: defines.h:196
af::dim4
Definition: dim4.hpp:26
af_get_data_ref_count
AFAPI af_err af_get_data_ref_count(int *use_count, const af_array in)
Get the use count of af_array
af::array::array_proxy
Intermediate data class.
Definition: array.h:45
af_get_manual_eval_flag
AFAPI af_err af_get_manual_eval_flag(bool *flag)
Get the manual eval flag.
af::getManualEvalFlag
AFAPI bool getManualEvalFlag()
Get the manual eval flag.
afHost
Host pointer.
Definition: defines.h:216
af_is_floating
AFAPI af_err af_is_floating(bool *result, const af_array arr)
Check if an array is floating precision type.
af_dtype
af_dtype
Definition: defines.h:195
af_eval
AFAPI af_err af_eval(af_array in)
Evaluate any expressions in the Array.