array.h Source File

- Main Page
- Tutorials
- Functions
- Release Notes
- Examples

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 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
136 array::array_proxy row(int index);
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
142 array::array_proxy col(int index);
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
147 array::array_proxy slice(int index);
148 const array::array_proxy slice(int index) const;
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]);
166 array();
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>
337 array(dim_t dim0,
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
530 af_array get();
531
535 af_array get() const;
536
540 dim_t elements() const;
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
678 template<typename T> T* device() const;
683 // INDEXING
684 // Single arguments
685
686
698 array::array_proxy operator()(const index &s0);
699
701 const array::array_proxy operator()(const index &s0) const;
702
703
716 array::array_proxy operator()(const index &s0,
717 const index &s1,
718 const index &s2 = span,
719 const index &s3 = span);
720
722 const array::array_proxy operator()(const index &s0,
723 const index &s1,
724 const index &s2 = span,
725 const index &s3 = span) const;
727
739 array::array_proxy row(int index);
740 const array::array_proxy row(int index) const;
741
751 array::array_proxy rows(int first, int last);
752 const array::array_proxy rows(int first, int last) const;
753
766 array::array_proxy col(int index);
767 const array::array_proxy col(int index) const;
768
778 array::array_proxy cols(int first, int last);
779 const array::array_proxy cols(int first, int last) const;
780
793 array::array_proxy slice(int index);
794 const array::array_proxy slice(int index) const;
795
804 array::array_proxy slices(int first, int last);
805 const array::array_proxy slices(int first, int last) const;
806
813 const array as(dtype type) const;
814
815
816 ~array();
817
822 array T() const;
827 array H() const;
828
829 #define ASSIGN_(OP) \
830 array& OP(const array &val); \
831 array& OP(const double &val); \
832 array& OP(const cdouble &val); \
833 array& OP(const cfloat &val); \
834 array& OP(const float &val); \
835 array& OP(const int &val); \
836 array& OP(const unsigned &val); \
837 array& OP(const bool &val); \
838 array& OP(const char &val); \
839 array& OP(const unsigned char &val); \
840 array& OP(const long &val); \
841 array& OP(const unsigned long &val); \
842 array& OP(const long long &val); \
843 array& OP(const unsigned long long &val); \
844
845 #if AF_API_VERSION >= 32
846 #define ASSIGN(OP) \
847 ASSIGN_(OP) \
848 array& OP(const short &val); \
849 array& OP(const unsigned short &val); \
850
851 #else
852 #define ASSIGN(OP) ASSIGN_(OP)
853 #endif
854
855
865 ASSIGN(operator=)
867
877 ASSIGN(operator+=)
879
889 ASSIGN(operator-=)
891
901 ASSIGN(operator*=)
903
914 ASSIGN(operator/=)
916
917
918 #undef ASSIGN
919 #undef ASSIGN_
920
926 array operator -() const;
927
933 array operator !() const;
934
939 int nonzeros() const;
940
941
947 void lock() const;
948
954 void unlock() const;
955 };
956 // end of class array
957
958 #define BIN_OP_(OP) \
959 AFAPI array OP (const array& lhs, const array& rhs); \
960 AFAPI array OP (const bool& lhs, const array& rhs); \
961 AFAPI array OP (const int& lhs, const array& rhs); \
962 AFAPI array OP (const unsigned& lhs, const array& rhs); \
963 AFAPI array OP (const char& lhs, const array& rhs); \
964 AFAPI array OP (const unsigned char& lhs, const array& rhs); \
965 AFAPI array OP (const long& lhs, const array& rhs); \
966 AFAPI array OP (const unsigned long& lhs, const array& rhs); \
967 AFAPI array OP (const long long& lhs, const array& rhs); \
968 AFAPI array OP (const unsigned long long& lhs, const array& rhs); \
969 AFAPI array OP (const double& lhs, const array& rhs); \
970 AFAPI array OP (const float& lhs, const array& rhs); \
971 AFAPI array OP (const cfloat& lhs, const array& rhs); \
972 AFAPI array OP (const cdouble& lhs, const array& rhs); \
973 AFAPI array OP (const array& lhs, const bool& rhs); \
974 AFAPI array OP (const array& lhs, const int& rhs); \
975 AFAPI array OP (const array& lhs, const unsigned& rhs); \
976 AFAPI array OP (const array& lhs, const char& rhs); \
977 AFAPI array OP (const array& lhs, const unsigned char& rhs); \
978 AFAPI array OP (const array& lhs, const long& rhs); \
979 AFAPI array OP (const array& lhs, const unsigned long& rhs); \
980 AFAPI array OP (const array& lhs, const long long& rhs); \
981 AFAPI array OP (const array& lhs, const unsigned long long& rhs); \
982 AFAPI array OP (const array& lhs, const double& rhs); \
983 AFAPI array OP (const array& lhs, const float& rhs); \
984 AFAPI array OP (const array& lhs, const cfloat& rhs); \
985 AFAPI array OP (const array& lhs, const cdouble& rhs); \
986
987 #if AF_API_VERSION >= 32
988 #define BIN_OP(OP) \
989 BIN_OP_(OP) \
990 AFAPI array OP (const short& lhs, const array& rhs); \
991 AFAPI array OP (const unsigned short& lhs, const array& rhs); \
992 AFAPI array OP (const array& lhs, const short& rhs); \
993 AFAPI array OP (const array& lhs, const unsigned short& rhs); \
994
995 #else
996 #define BIN_OP(OP) BIN_OP_(OP)
997 #endif
998
1007 BIN_OP(operator+ )
1009
1018 BIN_OP(operator- )
1020
1029 BIN_OP(operator* )
1031
1040 BIN_OP(operator/ )
1042
1051 BIN_OP(operator==)
1053
1063 BIN_OP(operator!=)
1065
1075 BIN_OP(operator< )
1077
1087 BIN_OP(operator<=)
1089
1099 BIN_OP(operator> )
1101
1111 BIN_OP(operator>=)
1113
1124 BIN_OP(operator&&)
1126
1137 BIN_OP(operator||)
1139
1149 BIN_OP(operator% )
1151
1162 BIN_OP(operator& )
1164
1175 BIN_OP(operator| )
1177
1188 BIN_OP(operator^ )
1190
1201 BIN_OP(operator<<)
1203
1214 BIN_OP(operator>>)
1216
1217 #undef BIN_OP
1218 #undef BIN_OP_
1219
1221
1225 inline array &eval(array &a) { a.eval(); return a; }
1226 inline void eval(array &a, array &b) { eval(a); b.eval(); }
1227 inline void eval(array &a, array &b, array &c) { eval(a, b); c.eval(); }
1228 inline void eval(array &a, array &b, array &c, array &d) { eval(a, b, c); d.eval(); }
1229 inline void eval(array &a, array &b, array &c, array &d, array &e) { eval(a, b, c, d); e.eval(); }
1230 inline void eval(array &a, array &b, array &c, array &d, array &e, array &f) { eval(a, b, c, d, e); f.eval(); }
1235 }
1236 #endif
1237
1238 #ifdef __cplusplus
1239 extern "C" {
1240 #endif
1241
1260 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);
1261
1272 AFAPI af_err af_create_handle(af_array *arr, const unsigned ndims, const dim_t * const dims, const af_dtype type);
1273
1284 AFAPI af_err af_copy_array(af_array *arr, const af_array in);
1285
1289 AFAPI af_err af_write_array(af_array arr, const void *data, const size_t bytes, af_source src);
1290
1296 AFAPI af_err af_get_data_ptr(void *data, const af_array arr);
1297
1301 AFAPI af_err af_release_array(af_array arr);
1302
1306 AFAPI af_err af_retain_array(af_array *out, const af_array in);
1307
1308 #if AF_API_VERSION >= 31
1309
1315 AFAPI af_err af_get_data_ref_count(int *use_count, const af_array in);
1316 #endif
1317
1318
1322 AFAPI af_err af_eval(af_array in);
1323
1340 AFAPI af_err af_get_elements(dim_t *elems, const af_array arr);
1341
1350 AFAPI af_err af_get_type(af_dtype *type, const af_array arr);
1351
1363 AFAPI af_err af_get_dims(dim_t *d0, dim_t *d1, dim_t *d2, dim_t *d3,
1364 const af_array arr);
1365
1374 AFAPI af_err af_get_numdims(unsigned *result, const af_array arr);
1375
1384 AFAPI af_err af_is_empty (bool *result, const af_array arr);
1385
1394 AFAPI af_err af_is_scalar (bool *result, const af_array arr);
1395
1404 AFAPI af_err af_is_row (bool *result, const af_array arr);
1405
1414 AFAPI af_err af_is_column (bool *result, const af_array arr);
1415
1426 AFAPI af_err af_is_vector (bool *result, const af_array arr);
1427
1436 AFAPI af_err af_is_complex (bool *result, const af_array arr);
1437
1448 AFAPI af_err af_is_real (bool *result, const af_array arr);
1449
1458 AFAPI af_err af_is_double (bool *result, const af_array arr);
1459
1468 AFAPI af_err af_is_single (bool *result, const af_array arr);
1469
1478 AFAPI af_err af_is_realfloating (bool *result, const af_array arr);
1479
1490 AFAPI af_err af_is_floating (bool *result, const af_array arr);
1491
1500 AFAPI af_err af_is_integer (bool *result, const af_array arr);
1501
1510 AFAPI af_err af_is_bool (bool *result, const af_array arr);
1515 #ifdef __cplusplus
1516 }
1517 #endif
traits.hpp
ASSIGN
#define ASSIGN(OP)
(const array &)
Definition: array.h:862
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
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.
BIN_OP
#define BIN_OP(OP)
(const array&, const array&)
Definition: array.h:1019
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_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.
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:1263
af_index_t
Struct used while indexing af_array.
Definition: index.h:23
af_err
af_err
Definition: defines.h:67
af::span
AFAPI seq span
af::array::isreal
bool isreal() const
Returns true if the array type is neither c32 nor c64.
Definition: array.h:620
af_source
af_source
Definition: defines.h:211
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
af::array::array_proxy::isreal
bool isreal() const
Definition: array.h:118
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:161
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:219
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:193
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
afHost
Host pointer.
Definition: defines.h:213
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:192
af_eval
AFAPI af_err af_eval(af_array in)
Evaluate any expressions in the Array.