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  // af::array member functions. same behavior as those below
88  af_array get();
89  af_array get() const;
90  dim_t elements() const;
91  template<typename T> T* host() const;
92  void host(void *ptr) const;
93  dtype type() const;
94  dim4 dims() const;
95  dim_t dims(unsigned dim) const;
96  unsigned numdims() const;
97  size_t bytes() const;
98  array copy() const;
99  bool isempty() const;
100  bool isscalar() const;
101  bool isvector() const;
102  bool isrow() const;
103  bool iscolumn() const;
104  bool iscomplex() const;
105  inline bool isreal() const { return !iscomplex(); }
106  bool isdouble() const;
107  bool issingle() const;
108  bool isrealfloating() const;
109  bool isfloating() const;
110  bool isinteger() const;
111  bool isbool() const;
112  void eval() const;
113  array as(dtype type) const;
114  array T() const;
115  array H() const;
116  template<typename T> T scalar() const;
117  template<typename T> T* device() const;
118  void unlock() const;
119 #if AF_API_VERSION >= 31
120  void lock() const;
121 #endif
122 
123  array::array_proxy row(int index);
124  const array::array_proxy row(int index) const;
125 
126  array::array_proxy rows(int first, int last);
127  const array::array_proxy rows(int first, int last) const;
128 
129  array::array_proxy col(int index);
130  const array::array_proxy col(int index) const;
131  array::array_proxy cols(int first, int last);
132  const array::array_proxy cols(int first, int last) const;
133 
134  array::array_proxy slice(int index);
135  const array::array_proxy slice(int index) const;
136 
137  array::array_proxy slices(int first, int last);
138  const array::array_proxy slices(int first, int last) const;
139  };
140 
141  //array(af_array in, const array *par, af_index_t seqs[4]);
153  array();
154 
159  explicit
160  array(const af_array handle);
161 
167  array(const array& in);
168 
191  array(dim_t dim0, dtype ty = f32);
192 
216  array(dim_t dim0, dim_t dim1, dtype ty = f32);
217 
242  array(dim_t dim0, dim_t dim1, dim_t dim2, dtype ty = f32);
243 
269  array(dim_t dim0, dim_t dim1, dim_t dim2, dim_t dim3, dtype ty = f32);
270 
296  explicit
297  array(const dim4& dims, dtype ty = f32);
298 
323  template<typename T>
324  array(dim_t dim0,
325  const T *pointer, af::source src=afHost);
326 
327 
346  template<typename T>
347  array(dim_t dim0, dim_t dim1,
348  const T *pointer, af::source src=afHost);
349 
350 
372  template<typename T>
373  array(dim_t dim0, dim_t dim1, dim_t dim2,
374  const T *pointer, af::source src=afHost);
375 
376 
399  template<typename T>
400  array(dim_t dim0, dim_t dim1, dim_t dim2, dim_t dim3,
401  const T *pointer, af::source src=afHost);
402 
436  template<typename T>
437  explicit
438  array(const dim4& dims,
439  const T *pointer, af::source src=afHost);
440 
468  array(const array& input, const dim4& dims);
469 
501  array( const array& input,
502  const dim_t dim0, const dim_t dim1 = 1,
503  const dim_t dim2 = 1, const dim_t dim3 = 1);
504 
517  af_array get();
518 
522  af_array get() const;
523 
527  dim_t elements() const;
528 
532  template<typename T> T* host() const;
533 
537  void host(void *ptr) const;
538 
542  template<typename T> void write(const T *ptr, const size_t bytes, af::source src = afHost);
543 
547  dtype type() const;
548 
552  dim4 dims() const;
553 
557  dim_t dims(unsigned dim) const;
558 
562  unsigned numdims() const;
563 
567  size_t bytes() const;
568 
572  array copy() const;
573 
577  bool isempty() const;
578 
582  bool isscalar() const;
583 
587  bool isvector() const;
588 
592  bool isrow() const;
593 
597  bool iscolumn() const;
598 
602  bool iscomplex() const;
603 
607  inline bool isreal() const { return !iscomplex(); }
608 
612  bool isdouble() const;
613 
617  bool issingle() const;
618 
622  bool isrealfloating() const;
623 
627  bool isfloating() const;
628 
632  bool isinteger() const;
633 
637  bool isbool() const;
638 
642  void eval() const;
643 
649  template<typename T> T scalar() const;
650 
665  template<typename T> T* device() const;
670  // INDEXING
671  // Single arguments
672 
673 
685  array::array_proxy operator()(const index &s0);
686 
688  const array::array_proxy operator()(const index &s0) const;
689 
690 
703  array::array_proxy operator()(const index &s0,
704  const index &s1,
705  const index &s2 = span,
706  const index &s3 = span);
707 
709  const array::array_proxy operator()(const index &s0,
710  const index &s1,
711  const index &s2 = span,
712  const index &s3 = span) const;
714 
726  array::array_proxy row(int index);
727  const array::array_proxy row(int index) const;
728 
738  array::array_proxy rows(int first, int last);
739  const array::array_proxy rows(int first, int last) const;
740 
753  array::array_proxy col(int index);
754  const array::array_proxy col(int index) const;
755 
765  array::array_proxy cols(int first, int last);
766  const array::array_proxy cols(int first, int last) const;
767 
780  array::array_proxy slice(int index);
781  const array::array_proxy slice(int index) const;
782 
791  array::array_proxy slices(int first, int last);
792  const array::array_proxy slices(int first, int last) const;
793 
800  const array as(dtype type) const;
801 
802 
803  ~array();
804 
809  array T() const;
814  array H() const;
815 
816 #define ASSIGN(OP) \
817  array& OP(const array &val); \
818  array& OP(const double &val); \
819  array& OP(const cdouble &val); \
820  array& OP(const cfloat &val); \
821  array& OP(const float &val); \
822  array& OP(const int &val); \
823  array& OP(const unsigned &val); \
824  array& OP(const bool &val); \
825  array& OP(const char &val); \
826  array& OP(const unsigned char &val); \
827  array& OP(const long &val); \
828  array& OP(const unsigned long &val); \
829  array& OP(const long long &val); \
830  array& OP(const unsigned long long &val); \
831 
832  ASSIGN(operator=)
843 
853  ASSIGN(operator+=)
855 
865  ASSIGN(operator-=)
867 
877  ASSIGN(operator*=)
879 
890  ASSIGN(operator/=)
892 
893 
894 #undef ASSIGN
895 
901  array operator -() const;
902 
908  array operator !() const;
909 
914  int nonzeros() const;
915 
916 
922  void lock() const;
923 
929  void unlock() const;
930  };
931  // end of class array
932 
933 #define BIN_OP(OP) \
934  AFAPI array OP (const array& lhs, const array& rhs); \
935  AFAPI array OP (const bool& lhs, const array& rhs); \
936  AFAPI array OP (const int& lhs, const array& rhs); \
937  AFAPI array OP (const unsigned& lhs, const array& rhs); \
938  AFAPI array OP (const char& lhs, const array& rhs); \
939  AFAPI array OP (const unsigned char& lhs, const array& rhs); \
940  AFAPI array OP (const long& lhs, const array& rhs); \
941  AFAPI array OP (const unsigned long& lhs, const array& rhs); \
942  AFAPI array OP (const long long& lhs, const array& rhs); \
943  AFAPI array OP (const unsigned long long& lhs, const array& rhs); \
944  AFAPI array OP (const double& lhs, const array& rhs); \
945  AFAPI array OP (const float& lhs, const array& rhs); \
946  AFAPI array OP (const cfloat& lhs, const array& rhs); \
947  AFAPI array OP (const cdouble& lhs, const array& rhs); \
948  AFAPI array OP (const array& lhs, const bool& rhs); \
949  AFAPI array OP (const array& lhs, const int& rhs); \
950  AFAPI array OP (const array& lhs, const unsigned& rhs); \
951  AFAPI array OP (const array& lhs, const char& rhs); \
952  AFAPI array OP (const array& lhs, const unsigned char& rhs); \
953  AFAPI array OP (const array& lhs, const long& rhs); \
954  AFAPI array OP (const array& lhs, const unsigned long& rhs); \
955  AFAPI array OP (const array& lhs, const long long& rhs); \
956  AFAPI array OP (const array& lhs, const unsigned long long& rhs); \
957  AFAPI array OP (const array& lhs, const double& rhs); \
958  AFAPI array OP (const array& lhs, const float& rhs); \
959  AFAPI array OP (const array& lhs, const cfloat& rhs); \
960  AFAPI array OP (const array& lhs, const cdouble& rhs); \
961 
962  BIN_OP(operator+ )
972 
981  BIN_OP(operator- )
983 
992  BIN_OP(operator* )
994 
1003  BIN_OP(operator/ )
1005 
1014  BIN_OP(operator==)
1016 
1026  BIN_OP(operator!=)
1028 
1038  BIN_OP(operator< )
1040 
1050  BIN_OP(operator<=)
1052 
1062  BIN_OP(operator> )
1064 
1074  BIN_OP(operator>=)
1076 
1087  BIN_OP(operator&&)
1089 
1100  BIN_OP(operator||)
1102 
1112  BIN_OP(operator% )
1114 
1125  BIN_OP(operator& )
1127 
1138  BIN_OP(operator| )
1140 
1151  BIN_OP(operator^ )
1153 
1164  BIN_OP(operator<<)
1166 
1177  BIN_OP(operator>>)
1179 
1180 #undef BIN_OP
1181 
1183 
1187  inline array &eval(array &a) { a.eval(); return a; }
1188  inline void eval(array &a, array &b) { eval(a); b.eval(); }
1189  inline void eval(array &a, array &b, array &c) { eval(a, b); c.eval(); }
1190  inline void eval(array &a, array &b, array &c, array &d) { eval(a, b, c); d.eval(); }
1191  inline void eval(array &a, array &b, array &c, array &d, array &e) { eval(a, b, c, d); e.eval(); }
1192  inline void eval(array &a, array &b, array &c, array &d, array &e, array &f) { eval(a, b, c, d, e); f.eval(); }
1197 }
1198 #endif
1199 
1200 #ifdef __cplusplus
1201 extern "C" {
1202 #endif
1203 
1222  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);
1223 
1234  AFAPI af_err af_create_handle(af_array *arr, const unsigned ndims, const dim_t * const dims, const af_dtype type);
1235 
1246  AFAPI af_err af_copy_array(af_array *arr, const af_array in);
1247 
1251  AFAPI af_err af_write_array(af_array arr, const void *data, const size_t bytes, af_source src);
1252 
1258  AFAPI af_err af_get_data_ptr(void *data, const af_array arr);
1259 
1264 
1268  AFAPI af_err af_retain_array(af_array *out, const af_array in);
1269 
1270 #if AF_API_VERSION >= 31
1271 
1277  AFAPI af_err af_get_data_ref_count(int *use_count, const af_array in);
1278 #endif
1279 
1280 
1285 
1302  AFAPI af_err af_get_elements(dim_t *elems, const af_array arr);
1303 
1312  AFAPI af_err af_get_type(af_dtype *type, const af_array arr);
1313 
1325  AFAPI af_err af_get_dims(dim_t *d0, dim_t *d1, dim_t *d2, dim_t *d3,
1326  const af_array arr);
1327 
1336  AFAPI af_err af_get_numdims(unsigned *result, const af_array arr);
1337 
1346  AFAPI af_err af_is_empty (bool *result, const af_array arr);
1347 
1356  AFAPI af_err af_is_scalar (bool *result, const af_array arr);
1357 
1366  AFAPI af_err af_is_row (bool *result, const af_array arr);
1367 
1376  AFAPI af_err af_is_column (bool *result, const af_array arr);
1377 
1388  AFAPI af_err af_is_vector (bool *result, const af_array arr);
1389 
1398  AFAPI af_err af_is_complex (bool *result, const af_array arr);
1399 
1410  AFAPI af_err af_is_real (bool *result, const af_array arr);
1411 
1420  AFAPI af_err af_is_double (bool *result, const af_array arr);
1421 
1430  AFAPI af_err af_is_single (bool *result, const af_array arr);
1431 
1440  AFAPI af_err af_is_realfloating (bool *result, const af_array arr);
1441 
1452  AFAPI af_err af_is_floating (bool *result, const af_array arr);
1453 
1462  AFAPI af_err af_is_integer (bool *result, const af_array arr);
1463 
1472  AFAPI af_err af_is_bool (bool *result, const af_array arr);
1477 #ifdef __cplusplus
1478 }
1479 #endif
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.
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_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:816
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:1217
Struct used while indexing af_array.
Definition: index.h:23
af_err
Definition: defines.h:67
AFAPI seq span
bool isreal() const
Returns true if the array type is neither c32 nor c64.
Definition: array.h:607
af_source
Definition: defines.h:179
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:934
bool isreal() const
Definition: array.h:105
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 dimseions 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:136
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:187
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:167
Definition: dim4.hpp:26
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
Host pointer.
Definition: defines.h:181
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:166
AFAPI af_err af_eval(af_array in)
Evaluate any expressions in the Array.