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 
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  size_t allocated() const;
112  array copy() const;
113  bool isempty() const;
114  bool isscalar() const;
115  bool isvector() const;
116  bool isrow() const;
117  bool iscolumn() const;
118  bool iscomplex() const;
119  inline bool isreal() const { return !iscomplex(); }
120  bool isdouble() const;
121  bool issingle() const;
122  bool isrealfloating() const;
123  bool isfloating() const;
124  bool isinteger() const;
125  bool isbool() const;
126 #if AF_API_VERSION >= 34
127  bool issparse() const;
128 #endif
129  void eval() const;
130  array as(dtype type) const;
131  array T() const;
132  array H() const;
133  template<typename T> T scalar() const;
134  template<typename T> T* device() const;
135  void unlock() const;
136 #if AF_API_VERSION >= 31
137  void lock() const;
138 #endif
139 
140 #if AF_API_VERSION >= 34
141  bool isLocked() const;
142 #endif
143 
144  array::array_proxy row(int index);
145  const array::array_proxy row(int index) const;
146 
147  array::array_proxy rows(int first, int last);
148  const array::array_proxy rows(int first, int last) const;
149 
150  array::array_proxy col(int index);
151  const array::array_proxy col(int index) const;
152  array::array_proxy cols(int first, int last);
153  const array::array_proxy cols(int first, int last) const;
154 
155  array::array_proxy slice(int index);
156  const array::array_proxy slice(int index) const;
157 
158  array::array_proxy slices(int first, int last);
159  const array::array_proxy slices(int first, int last) const;
160  };
161 
162  //array(af_array in, const array *par, af_index_t seqs[4]);
174  array();
175 
180  explicit
181  array(const af_array handle);
182 
188  array(const array& in);
189 
212  array(dim_t dim0, dtype ty = f32);
213 
237  array(dim_t dim0, dim_t dim1, dtype ty = f32);
238 
263  array(dim_t dim0, dim_t dim1, dim_t dim2, dtype ty = f32);
264 
290  array(dim_t dim0, dim_t dim1, dim_t dim2, dim_t dim3, dtype ty = f32);
291 
317  explicit
318  array(const dim4& dims, dtype ty = f32);
319 
344  template<typename T>
345  array(dim_t dim0,
346  const T *pointer, af::source src=afHost);
347 
348 
367  template<typename T>
368  array(dim_t dim0, dim_t dim1,
369  const T *pointer, af::source src=afHost);
370 
371 
393  template<typename T>
394  array(dim_t dim0, dim_t dim1, dim_t dim2,
395  const T *pointer, af::source src=afHost);
396 
397 
420  template<typename T>
421  array(dim_t dim0, dim_t dim1, dim_t dim2, dim_t dim3,
422  const T *pointer, af::source src=afHost);
423 
457  template<typename T>
458  explicit
459  array(const dim4& dims,
460  const T *pointer, af::source src=afHost);
461 
489  array(const array& input, const dim4& dims);
490 
522  array( const array& input,
523  const dim_t dim0, const dim_t dim1 = 1,
524  const dim_t dim2 = 1, const dim_t dim3 = 1);
525 
538  af_array get();
539 
543  af_array get() const;
544 
548  dim_t elements() const;
549 
553  template<typename T> T* host() const;
554 
558  void host(void *ptr) const;
559 
563  template<typename T> void write(const T *ptr, const size_t bytes, af::source src = afHost);
564 
568  dtype type() const;
569 
573  dim4 dims() const;
574 
578  dim_t dims(unsigned dim) const;
579 
583  unsigned numdims() const;
584 
588  size_t bytes() const;
589 
594  size_t allocated() const;
595 
599  array copy() const;
600 
604  bool isempty() const;
605 
609  bool isscalar() const;
610 
614  bool isvector() const;
615 
619  bool isrow() const;
620 
624  bool iscolumn() const;
625 
629  bool iscomplex() const;
630 
634  inline bool isreal() const { return !iscomplex(); }
635 
639  bool isdouble() const;
640 
644  bool issingle() const;
645 
649  bool isrealfloating() const;
650 
654  bool isfloating() const;
655 
659  bool isinteger() const;
660 
664  bool isbool() const;
665 
666 #if AF_API_VERSION >= 34
667 
670  bool issparse() const;
671 #endif
672 
676  void eval() const;
677 
683  template<typename T> T scalar() const;
684 
701  template<typename T> T* device() const;
706  // INDEXING
707  // Single arguments
708 
726  array::array_proxy operator()(const index &s0);
727 
733  const array::array_proxy operator()(const index &s0) const;
734 
735 
753  array::array_proxy operator()(const index &s0,
754  const index &s1,
755  const index &s2 = span,
756  const index &s3 = span);
757 
763  const array::array_proxy operator()(const index &s0,
764  const index &s1,
765  const index &s2 = span,
766  const index &s3 = span) const;
767 
768 
780  array::array_proxy row(int index);
781  const array::array_proxy row(int index) const;
782 
792  array::array_proxy rows(int first, int last);
793  const array::array_proxy rows(int first, int last) const;
794 
807  array::array_proxy col(int index);
808  const array::array_proxy col(int index) const;
809 
819  array::array_proxy cols(int first, int last);
820  const array::array_proxy cols(int first, int last) const;
821 
834  array::array_proxy slice(int index);
835  const array::array_proxy slice(int index) const;
836 
845  array::array_proxy slices(int first, int last);
846  const array::array_proxy slices(int first, int last) const;
847 
854  const array as(dtype type) const;
855 
856 
857  ~array();
858 
863  array T() const;
868  array H() const;
869 
870 #define ASSIGN_(OP) \
871  array& OP(const array &val); \
872  array& OP(const double &val); \
873  array& OP(const cdouble &val); \
874  array& OP(const cfloat &val); \
875  array& OP(const float &val); \
876  array& OP(const int &val); \
877  array& OP(const unsigned &val); \
878  array& OP(const bool &val); \
879  array& OP(const char &val); \
880  array& OP(const unsigned char &val); \
881  array& OP(const long &val); \
882  array& OP(const unsigned long &val); \
883  array& OP(const long long &val); \
884  array& OP(const unsigned long long &val); \
885 
886 #if AF_API_VERSION >= 32
887 #define ASSIGN(OP) \
888  ASSIGN_(OP) \
889  array& OP(const short &val); \
890  array& OP(const unsigned short &val); \
891 
892 #else
893 #define ASSIGN(OP) ASSIGN_(OP)
894 #endif
895 
896 
906  ASSIGN(operator=)
908 
918  ASSIGN(operator+=)
920 
930  ASSIGN(operator-=)
932 
942  ASSIGN(operator*=)
944 
955  ASSIGN(operator/=)
957 
958 
959 #undef ASSIGN
960 #undef ASSIGN_
961 
967  array operator -() const;
968 
974  array operator !() const;
975 
980  int nonzeros() const;
981 
982 
988  void lock() const;
989 
990 
991 #if AF_API_VERSION >= 34
992  bool isLocked() const;
998 #endif
999 
1000 
1006  void unlock() const;
1007  };
1008  // end of class array
1009 
1010 #define BIN_OP_(OP) \
1011  AFAPI array OP (const array& lhs, const array& rhs); \
1012  AFAPI array OP (const bool& lhs, const array& rhs); \
1013  AFAPI array OP (const int& lhs, const array& rhs); \
1014  AFAPI array OP (const unsigned& lhs, const array& rhs); \
1015  AFAPI array OP (const char& lhs, const array& rhs); \
1016  AFAPI array OP (const unsigned char& lhs, const array& rhs); \
1017  AFAPI array OP (const long& lhs, const array& rhs); \
1018  AFAPI array OP (const unsigned long& lhs, const array& rhs); \
1019  AFAPI array OP (const long long& lhs, const array& rhs); \
1020  AFAPI array OP (const unsigned long long& lhs, const array& rhs); \
1021  AFAPI array OP (const double& lhs, const array& rhs); \
1022  AFAPI array OP (const float& lhs, const array& rhs); \
1023  AFAPI array OP (const cfloat& lhs, const array& rhs); \
1024  AFAPI array OP (const cdouble& lhs, const array& rhs); \
1025  AFAPI array OP (const array& lhs, const bool& rhs); \
1026  AFAPI array OP (const array& lhs, const int& rhs); \
1027  AFAPI array OP (const array& lhs, const unsigned& rhs); \
1028  AFAPI array OP (const array& lhs, const char& rhs); \
1029  AFAPI array OP (const array& lhs, const unsigned char& rhs); \
1030  AFAPI array OP (const array& lhs, const long& rhs); \
1031  AFAPI array OP (const array& lhs, const unsigned long& rhs); \
1032  AFAPI array OP (const array& lhs, const long long& rhs); \
1033  AFAPI array OP (const array& lhs, const unsigned long long& rhs); \
1034  AFAPI array OP (const array& lhs, const double& rhs); \
1035  AFAPI array OP (const array& lhs, const float& rhs); \
1036  AFAPI array OP (const array& lhs, const cfloat& rhs); \
1037  AFAPI array OP (const array& lhs, const cdouble& rhs); \
1038 
1039 #if AF_API_VERSION >= 32
1040 #define BIN_OP(OP) \
1041  BIN_OP_(OP) \
1042  AFAPI array OP (const short& lhs, const array& rhs); \
1043  AFAPI array OP (const unsigned short& lhs, const array& rhs); \
1044  AFAPI array OP (const array& lhs, const short& rhs); \
1045  AFAPI array OP (const array& lhs, const unsigned short& rhs); \
1046 
1047 #else
1048 #define BIN_OP(OP) BIN_OP_(OP)
1049 #endif
1050 
1059  BIN_OP(operator+ )
1061 
1070  BIN_OP(operator- )
1072 
1081  BIN_OP(operator* )
1083 
1092  BIN_OP(operator/ )
1094 
1103  BIN_OP(operator==)
1105 
1115  BIN_OP(operator!=)
1117 
1127  BIN_OP(operator< )
1129 
1139  BIN_OP(operator<=)
1141 
1151  BIN_OP(operator> )
1153 
1163  BIN_OP(operator>=)
1165 
1176  BIN_OP(operator&&)
1178 
1189  BIN_OP(operator||)
1191 
1201  BIN_OP(operator% )
1203 
1214  BIN_OP(operator& )
1216 
1227  BIN_OP(operator| )
1229 
1240  BIN_OP(operator^ )
1242 
1253  BIN_OP(operator<<)
1255 
1266  BIN_OP(operator>>)
1268 
1269 #undef BIN_OP
1270 #undef BIN_OP_
1271 
1273 
1277  inline array &eval(array &a) { a.eval(); return a; }
1278 
1279 #if AF_API_VERSION >= 34
1280  AFAPI void eval(int num, array **arrays);
1284 #endif
1285 
1286  inline void eval(array &a, array &b)
1287  {
1288 #if AF_API_VERSION >= 34
1289  array *arrays[] = {&a, &b};
1290  return eval(2, arrays);
1291 #else
1292  eval(a); b.eval();
1293 #endif
1294  }
1295 
1296  inline void eval(array &a, array &b, array &c)
1297  {
1298 #if AF_API_VERSION >= 34
1299  array *arrays[] = {&a, &b, &c};
1300  return eval(3, arrays);
1301 #else
1302  eval(a, b); c.eval();
1303 #endif
1304  }
1305 
1306  inline void eval(array &a, array &b, array &c, array &d)
1307  {
1308 #if AF_API_VERSION >= 34
1309  array *arrays[] = {&a, &b, &c, &d};
1310  return eval(4, arrays);
1311 #else
1312  eval(a, b, c); d.eval();
1313 #endif
1314 
1315  }
1316 
1317  inline void eval(array &a, array &b, array &c, array &d, array &e)
1318  {
1319 #if AF_API_VERSION >= 34
1320  array *arrays[] = {&a, &b, &c, &d, &e};
1321  return eval(5, arrays);
1322 #else
1323  eval(a, b, c, d); e.eval();
1324 #endif
1325  }
1326 
1327  inline void eval(array &a, array &b, array &c, array &d, array &e, array &f)
1328  {
1329 #if AF_API_VERSION >= 34
1330  array *arrays[] = {&a, &b, &c, &d, &e, &f};
1331  return eval(6, arrays);
1332 #else
1333  eval(a, b, c, d, e); f.eval();
1334 #endif
1335  }
1336 
1337 #if AF_API_VERSION >= 34
1338  AFAPI void setManualEvalFlag(bool flag);
1342 #endif
1343 
1344 #if AF_API_VERSION >= 34
1345  AFAPI bool getManualEvalFlag();
1347 #endif
1348 
1353 }
1354 #endif
1356 #ifdef __cplusplus
1357 extern "C" {
1358 #endif
1359 
1378  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);
1379 
1390  AFAPI af_err af_create_handle(af_array *arr, const unsigned ndims, const dim_t * const dims, const af_dtype type);
1391 
1402  AFAPI af_err af_copy_array(af_array *arr, const af_array in);
1403 
1407  AFAPI af_err af_write_array(af_array arr, const void *data, const size_t bytes, af_source src);
1408 
1414  AFAPI af_err af_get_data_ptr(void *data, const af_array arr);
1415 
1420 
1424  AFAPI af_err af_retain_array(af_array *out, const af_array in);
1425 
1426 #if AF_API_VERSION >= 31
1427 
1433  AFAPI af_err af_get_data_ref_count(int *use_count, const af_array in);
1434 #endif
1435 
1436 
1441 
1447 #if AF_API_VERSION >= 34
1448 
1451  AFAPI af_err af_eval_multiple(const int num, af_array *arrays);
1455 #endif
1456 
1457 #if AF_API_VERSION >= 34
1458 
1465 #endif
1466 
1467 #if AF_API_VERSION >= 34
1468 
1471  AFAPI af_err af_get_manual_eval_flag(bool *flag);
1475 #endif
1476 
1489  AFAPI af_err af_get_elements(dim_t *elems, const af_array arr);
1490 
1499  AFAPI af_err af_get_type(af_dtype *type, const af_array arr);
1500 
1512  AFAPI af_err af_get_dims(dim_t *d0, dim_t *d1, dim_t *d2, dim_t *d3,
1513  const af_array arr);
1514 
1523  AFAPI af_err af_get_numdims(unsigned *result, const af_array arr);
1524 
1533  AFAPI af_err af_is_empty (bool *result, const af_array arr);
1534 
1543  AFAPI af_err af_is_scalar (bool *result, const af_array arr);
1544 
1553  AFAPI af_err af_is_row (bool *result, const af_array arr);
1554 
1563  AFAPI af_err af_is_column (bool *result, const af_array arr);
1564 
1575  AFAPI af_err af_is_vector (bool *result, const af_array arr);
1576 
1585  AFAPI af_err af_is_complex (bool *result, const af_array arr);
1586 
1597  AFAPI af_err af_is_real (bool *result, const af_array arr);
1598 
1607  AFAPI af_err af_is_double (bool *result, const af_array arr);
1608 
1617  AFAPI af_err af_is_single (bool *result, const af_array arr);
1618 
1627  AFAPI af_err af_is_realfloating (bool *result, const af_array arr);
1628 
1639  AFAPI af_err af_is_floating (bool *result, const af_array arr);
1640 
1649  AFAPI af_err af_is_integer (bool *result, const af_array arr);
1650 
1659  AFAPI af_err af_is_bool (bool *result, const af_array arr);
1660 
1661 #if AF_API_VERSION >= 34
1662 
1670  AFAPI af_err af_is_sparse (bool *result, const af_array arr);
1671 #endif
1672 
1673 #if AF_API_VERSION >= 35
1674 
1681  AFAPI af_err af_get_scalar(void* output_value, const af_array arr);
1682 #endif
1683 
1688 #ifdef __cplusplus
1689 }
1690 #endif
AFAPI af_err af_set_manual_eval_flag(bool flag)
Turn the manual eval flag on or off.
AFAPI void setManualEvalFlag(bool flag)
Turn the manual eval flag on or off.
AFAPI af_err af_is_row(bool *result, const af_array arr)
Check if an array is row vector.
AFAPI af_err af_is_real(bool *result, const af_array arr)
Check if an array is real type.
bool isreal() const
Definition: array.h:119
Definition: algorithm.h:14
AFAPI af_err af_is_realfloating(bool *result, const af_array arr)
Check if an array is real floating point type.
AFAPI af_err af_copy_array(af_array *arr, const af_array in)
Deep copy an array to another.
AFAPI af_err af_is_sparse(bool *result, const af_array arr)
Check if an array is sparse.
AFAPI af_err af_eval_multiple(const int num, af_array *arrays)
Evaluate multiple arrays together.
AFAPI af_err af_get_data_ptr(void *data, const af_array arr)
Copy data from an af_array to a C pointer.
#define ASSIGN(OP)
(const array &)
Definition: array.h:903
AFAPI af_err af_is_complex(bool *result, const af_array arr)
Check if an array is complex type.
void eval() const
Evaluate any JIT expressions to generate data for the array.
A multi dimensional data container.
Definition: array.h:27
AFAPI af_err af_is_bool(bool *result, const af_array arr)
Check if an array is bool type.
AFAPI af_err af_is_double(bool *result, const af_array arr)
Check if an array is double precision type.
AFAPI af_err af_is_vector(bool *result, const af_array arr)
Check if an array is a vector.
AFAPI af_err af_is_integer(bool *result, const af_array arr)
Check if an array is integer type.
array & eval(array &a)
Evaluate an expression (nonblocking).
Definition: array.h:1315
bool isreal() const
Returns true if the array type is neither c32 nor c64.
Definition: array.h:634
Struct used while indexing af_array.
Definition: index.h:23
af_err
Definition: defines.h:63
AFAPI seq span
af_source
Definition: defines.h:214
AFAPI af_err af_get_type(af_dtype *type, const af_array arr)
Gets the type of an array.
long long dim_t
Definition: defines.h:50
#define BIN_OP(OP)
(const array&, const array&)
Definition: array.h:1071
AFAPI af_err af_is_single(bool *result, const af_array arr)
Check if an array is single precision type.
AFAPI af_err af_get_dims(dim_t *d0, dim_t *d1, dim_t *d2, dim_t *d3, const af_array arr)
Gets the dimensions of an array.
Wrapper for af_index.
Definition: index.h:52
#define AFAPI
Definition: defines.h:31
AFAPI af_err af_is_scalar(bool *result, const af_array arr)
Check if an array is scalar, ie.
AFAPI af_err af_get_elements(dim_t *elems, const af_array arr)
Gets the number of elements in an array.
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.
AFAPI af_err af_get_numdims(unsigned *result, const af_array arr)
Gets the number of dimensions of an array.
AFAPI af_err af_is_empty(bool *result, const af_array arr)
Check if an array is empty.
AFAPI af_err af_release_array(af_array arr)
Reduce the reference count of the af_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.
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
AFAPI af_err af_is_column(bool *result, const af_array arr)
Check if an array is a column vector.
AFAPI af_err af_retain_array(af_array *out, const af_array in)
Increments an af_array reference count.
void * af_array
Definition: defines.h:222
AFAPI array operator-(const array &lhs, const array &rhs)
Subtracts two arrays or an array and a value.
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 af_err af_create_handle(af_array *arr, const unsigned ndims, const dim_t *const dims, const af_dtype type)
Create af_array handle.
32-bit floating point values
Definition: defines.h:196
Definition: dim4.hpp:23
AFAPI af_err af_get_data_ref_count(int *use_count, const af_array in)
Get the use count of af_array
Intermediate data class.
Definition: array.h:45
AFAPI af_err af_get_manual_eval_flag(bool *flag)
Get the manual eval flag.
AFAPI bool getManualEvalFlag()
Get the manual eval flag.
Host pointer.
Definition: defines.h:216
AFAPI af_err af_get_scalar(void *output_value, const af_array arr)
Get first element from an array.
AFAPI af_err af_is_floating(bool *result, const af_array arr)
Check if an array is floating precision type.
af_dtype
Definition: defines.h:195
AFAPI af_err af_eval(af_array in)
Evaluate any expressions in the Array.