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 
44  {
45  struct array_proxy_impl; //forward declaration
46  array_proxy_impl *impl; // implementation
47 
48  public:
49  array_proxy(array& par, af_index_t *ssss, bool linear = false);
50  array_proxy(const array_proxy &other);
51 #if __cplusplus > 199711L
52  array_proxy(array_proxy &&other);
53  array_proxy & operator=(array_proxy &&other);
54 #endif
55  ~array_proxy();
56 
57  // Implicit conversion operators
58  operator array() const;
59  operator array();
60 
61 #define ASSIGN(OP) \
62  array_proxy& operator OP(const array_proxy &a); \
63  array_proxy& operator OP(const array &a); \
64  array_proxy& operator OP(const double &a); \
65  array_proxy& operator OP(const cdouble &a); \
66  array_proxy& operator OP(const cfloat &a); \
67  array_proxy& operator OP(const float &a); \
68  array_proxy& operator OP(const int &a); \
69  array_proxy& operator OP(const unsigned &a); \
70  array_proxy& operator OP(const bool &a); \
71  array_proxy& operator OP(const char &a); \
72  array_proxy& operator OP(const unsigned char &a); \
73  array_proxy& operator OP(const long &a); \
74  array_proxy& operator OP(const unsigned long &a); \
75  array_proxy& operator OP(const long long &a); \
76  array_proxy& operator OP(const unsigned long long &a); \
77 
78  ASSIGN(=)
79  ASSIGN(+=)
80  ASSIGN(-=)
81  ASSIGN(*=)
82  ASSIGN(/=)
83 #undef ASSIGN
84 
85  // af::array member functions. same behavior as those below
86  af_array get();
87  af_array get() const;
88  dim_t elements() const;
89  template<typename T> T* host() const;
90  void host(void *ptr) const;
91  dtype type() const;
92  dim4 dims() const;
93  dim_t dims(unsigned dim) const;
94  unsigned numdims() const;
95  size_t bytes() const;
96  array copy() const;
97  bool isempty() const;
98  bool isscalar() const;
99  bool isvector() const;
100  bool isrow() const;
101  bool iscolumn() const;
102  bool iscomplex() const;
103  inline bool isreal() const { return !iscomplex(); }
104  bool isdouble() const;
105  bool issingle() const;
106  bool isrealfloating() const;
107  bool isfloating() const;
108  bool isinteger() const;
109  bool isbool() const;
110  void eval() const;
111  array as(dtype type) const;
112  array T() const;
113  array H() const;
114  template<typename T> T scalar() const;
115  template<typename T> T* device() const;
116  void unlock() const;
117 
118  array::array_proxy row(int index);
119  const array::array_proxy row(int index) const;
120 
121  array::array_proxy rows(int first, int last);
122  const array::array_proxy rows(int first, int last) const;
123 
124  array::array_proxy col(int index);
125  const array::array_proxy col(int index) const;
126  array::array_proxy cols(int first, int last);
127  const array::array_proxy cols(int first, int last) const;
128 
129  array::array_proxy slice(int index);
130  const array::array_proxy slice(int index) const;
131 
132  array::array_proxy slices(int first, int last);
133  const array::array_proxy slices(int first, int last) const;
134  };
135 
136  //array(af_array in, const array *par, af_index_t seqs[4]);
148  array();
149 
154  explicit
155  array(const af_array handle);
156 
162  array(const array& in);
163 
186  array(dim_t dim0, dtype ty = f32);
187 
211  array(dim_t dim0, dim_t dim1, dtype ty = f32);
212 
237  array(dim_t dim0, dim_t dim1, dim_t dim2, dtype ty = f32);
238 
264  array(dim_t dim0, dim_t dim1, dim_t dim2, dim_t dim3, dtype ty = f32);
265 
291  explicit
292  array(const dim4& dims, dtype ty = f32);
293 
324  template<typename T>
325  array(dim_t dim0,
326  const T *pointer, af::source src=afHost);
327 
328 
353  template<typename T>
354  array(dim_t dim0, dim_t dim1,
355  const T *pointer, af::source src=afHost);
356 
357 
381  template<typename T>
382  array(dim_t dim0, dim_t dim1, dim_t dim2,
383  const T *pointer, af::source src=afHost);
384 
385 
410  template<typename T>
411  array(dim_t dim0, dim_t dim1, dim_t dim2, dim_t dim3,
412  const T *pointer, af::source src=afHost);
413 
445  template<typename T>
446  explicit
447  array(const dim4& dims,
448  const T *pointer, af::source src=afHost);
449 
477  array(const array& input, const dim4& dims);
478 
510  array( const array& input,
511  const dim_t dim0, const dim_t dim1 = 1,
512  const dim_t dim2 = 1, const dim_t dim3 = 1);
513 
526  af_array get();
527 
531  af_array get() const;
532 
536  dim_t elements() const;
537 
541  template<typename T> T* host() const;
542 
546  void host(void *ptr) const;
547 
551  template<typename T> void write(const T *ptr, const size_t bytes, af::source src = afHost);
552 
556  dtype type() const;
557 
561  dim4 dims() const;
562 
566  dim_t dims(unsigned dim) const;
567 
571  unsigned numdims() const;
572 
576  size_t bytes() const;
577 
581  array copy() const;
582 
586  bool isempty() const;
587 
591  bool isscalar() const;
592 
596  bool isvector() const;
597 
601  bool isrow() const;
602 
606  bool iscolumn() const;
607 
611  bool iscomplex() const;
612 
616  inline bool isreal() const { return !iscomplex(); }
617 
621  bool isdouble() const;
622 
626  bool issingle() const;
627 
631  bool isrealfloating() const;
632 
636  bool isfloating() const;
637 
641  bool isinteger() const;
642 
646  bool isbool() const;
647 
651  void eval() const;
652 
658  template<typename T> T scalar() const;
659 
674  template<typename T> T* device() const;
679  void unlock() const;
680 
681 
682  // INDEXING
683  // Single arguments
684 
685 
697  array::array_proxy operator()(const index &s0);
698 
700  const array::array_proxy operator()(const index &s0) const;
701 
702 
715  array::array_proxy operator()(const index &s0,
716  const index &s1,
717  const index &s2 = span,
718  const index &s3 = span);
719 
721  const array::array_proxy operator()(const index &s0,
722  const index &s1,
723  const index &s2 = span,
724  const index &s3 = span) const;
726 
738  array::array_proxy row(int index);
739  const array::array_proxy row(int index) const;
740 
750  array::array_proxy rows(int first, int last);
751  const array::array_proxy rows(int first, int last) const;
752 
765  array::array_proxy col(int index);
766  const array::array_proxy col(int index) const;
767 
777  array::array_proxy cols(int first, int last);
778  const array::array_proxy cols(int first, int last) const;
779 
792  array::array_proxy slice(int index);
793  const array::array_proxy slice(int index) const;
794 
803  array::array_proxy slices(int first, int last);
804  const array::array_proxy slices(int first, int last) const;
805 
812  const array as(dtype type) const;
813 
814 
815  ~array();
816 
817  // Transpose and Conjugate Tranpose
818  array T() const;
819  array H() const;
820 
821 #define ASSIGN(OP) \
822  array& OP(const array &val); \
823  array& OP(const double &val); \
824  array& OP(const cdouble &val); \
825  array& OP(const cfloat &val); \
826  array& OP(const float &val); \
827  array& OP(const int &val); \
828  array& OP(const unsigned &val); \
829  array& OP(const bool &val); \
830  array& OP(const char &val); \
831  array& OP(const unsigned char &val); \
832  array& OP(const long &val); \
833  array& OP(const unsigned long &val); \
834  array& OP(const long long &val); \
835  array& OP(const unsigned long long &val); \
836 
837  ASSIGN(operator=)
848 
858  ASSIGN(operator+=)
860 
870  ASSIGN(operator-=)
872 
882  ASSIGN(operator*=)
884 
895  ASSIGN(operator/=)
897 
898 
899 #undef ASSIGN
900 
906  array operator -() const;
907 
913  array operator !() const;
914 
919  int nonzeros() const;
920  };
921  // end of class array
922 
923 #define BIN_OP(OP) \
924  AFAPI array OP (const array& lhs, const array& rhs); \
925  AFAPI array OP (const bool& lhs, const array& rhs); \
926  AFAPI array OP (const int& lhs, const array& rhs); \
927  AFAPI array OP (const unsigned& lhs, const array& rhs); \
928  AFAPI array OP (const char& lhs, const array& rhs); \
929  AFAPI array OP (const unsigned char& lhs, const array& rhs); \
930  AFAPI array OP (const long& lhs, const array& rhs); \
931  AFAPI array OP (const unsigned long& lhs, const array& rhs); \
932  AFAPI array OP (const long long& lhs, const array& rhs); \
933  AFAPI array OP (const unsigned long long& lhs, const array& rhs); \
934  AFAPI array OP (const double& lhs, const array& rhs); \
935  AFAPI array OP (const float& lhs, const array& rhs); \
936  AFAPI array OP (const cfloat& lhs, const array& rhs); \
937  AFAPI array OP (const cdouble& lhs, const array& rhs); \
938  AFAPI array OP (const array& lhs, const bool& rhs); \
939  AFAPI array OP (const array& lhs, const int& rhs); \
940  AFAPI array OP (const array& lhs, const unsigned& rhs); \
941  AFAPI array OP (const array& lhs, const char& rhs); \
942  AFAPI array OP (const array& lhs, const unsigned char& rhs); \
943  AFAPI array OP (const array& lhs, const long& rhs); \
944  AFAPI array OP (const array& lhs, const unsigned long& rhs); \
945  AFAPI array OP (const array& lhs, const long long& rhs); \
946  AFAPI array OP (const array& lhs, const unsigned long long& rhs); \
947  AFAPI array OP (const array& lhs, const double& rhs); \
948  AFAPI array OP (const array& lhs, const float& rhs); \
949  AFAPI array OP (const array& lhs, const cfloat& rhs); \
950  AFAPI array OP (const array& lhs, const cdouble& rhs); \
951 
952  BIN_OP(operator+ )
962 
971  BIN_OP(operator- )
973 
982  BIN_OP(operator* )
984 
993  BIN_OP(operator/ )
995 
1004  BIN_OP(operator==)
1006 
1016  BIN_OP(operator!=)
1018 
1028  BIN_OP(operator< )
1030 
1040  BIN_OP(operator<=)
1042 
1052  BIN_OP(operator> )
1054 
1064  BIN_OP(operator>=)
1066 
1077  BIN_OP(operator&&)
1079 
1090  BIN_OP(operator||)
1092 
1102  BIN_OP(operator% )
1104 
1115  BIN_OP(operator& )
1117 
1128  BIN_OP(operator| )
1130 
1141  BIN_OP(operator^ )
1143 
1154  BIN_OP(operator<<)
1156 
1167  BIN_OP(operator>>)
1169 
1170 #undef BIN_OP
1171 
1173 
1177  inline array &eval(array &a) { a.eval(); return a; }
1178  inline void eval(array &a, array &b) { eval(a); b.eval(); }
1179  inline void eval(array &a, array &b, array &c) { eval(a, b); c.eval(); }
1180  inline void eval(array &a, array &b, array &c, array &d) { eval(a, b, c); d.eval(); }
1181  inline void eval(array &a, array &b, array &c, array &d, array &e) { eval(a, b, c, d); e.eval(); }
1182  inline void eval(array &a, array &b, array &c, array &d, array &e, array &f) { eval(a, b, c, d, e); f.eval(); }
1187 }
1188 #endif
1189 
1190 #ifdef __cplusplus
1191 extern "C" {
1192 #endif
1193 
1211  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  AFAPI af_err af_create_handle(af_array *arr, const unsigned ndims, const dim_t * const dims, const af_dtype type);
1224 
1235  AFAPI af_err af_copy_array(af_array *arr, const af_array in);
1236 
1240  AFAPI af_err af_write_array(af_array arr, const void *data, const size_t bytes, af_source src);
1241 
1247  AFAPI af_err af_get_data_ptr(void *data, const af_array arr);
1248 
1253 
1257  AFAPI af_err af_retain_array(af_array *out, const af_array in);
1258 
1263 
1268 #ifdef __cplusplus
1269 }
1270 #endif
Definition: algorithm.h:14
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:821
void eval() const
Evaluate any JIT expressions to generate data for the array.
A multi dimensional data container.
Definition: array.h:27
array & eval(array &a)
Evaluate an expression (nonblocking).
Definition: array.h:1207
Struct used while indexing af_array.
Definition: index.h:23
af_err
Definition: defines.h:58
AFAPI seq span
bool isreal() const
Returns true if the array type is neither c32 nor c64.
Definition: array.h:616
af_source
Definition: defines.h:164
long long dim_t
Definition: defines.h:50
#define BIN_OP(OP)
(const array&, const array&)
Definition: array.h:924
bool isreal() const
Definition: array.h:103
Wrapper for af_index.
Definition: index.h:52
#define AFAPI
Definition: defines.h:31
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:105
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:172
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:152
Definition: dim4.hpp:26
Intermediate data class.
Definition: array.h:43
Host pointer.
Definition: defines.h:166
af_dtype
Definition: defines.h:151
AFAPI af_err af_eval(af_array in)
Evaluate any expressions in the Array.