HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CE_ArrayView.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: CE_ArrayView.h ( CE Library, C++)
7  *
8  * COMMENTS: Compute Engine Array View.
9  */
10 
11 #ifndef __CE_ArrayView__
12 #define __CE_ArrayView__
13 
14 
15 #include "CE_API.h"
16 #include "CE_Context.h"
17 
18 #include <SYS/SYS_TypeTraits.h>
19 #include <UT/UT_WorkBuffer.h>
20 
21 #include <limits>
22 
23 template <typename T>
25 
26 template <typename T>
27 class CE_Array;
28 
29 /// CE_Array prefix sum operators.
30 enum class CE_ScanOp
31 {
32  ADD,
33  MIN,
34  MAX,
36 };
37 
38 /// Shares an existing OpenCL buffer and views it as an array.
39 /// The lifetime of the shared buffer must exceed the lifetime of this object.
40 template <typename T>
42 {
43 public:
44  typedef T value_type;
45 
46  /// Returns the number of elements in the array.
47  exint size() const { return mySize; }
48  bool isEmpty() const { return mySize == 0; }
49  const cl::Buffer &buffer() const { return myBuffer; }
50 
51  /// Size expects the number of elements in the buffer.
52  /// If size is not provided, it is calculated from the buffer size.
53  CE_ArrayView(const cl::Buffer &b, exint size = -1);
54  explicit CE_ArrayView(const CE_BufferDevice<T> &b);
55 
56  /// Harden the view into a buffer, which now owns a copy of the array data.
57  /// You must include CE_BufferDevice.h to use this.
58  CE_BufferDevice<T> harden() const;
59 
60  /// Copy/convert data from an array of another type and/or tuple size.
61  /// Extra tuple components in the source array are discarded.
62  /// Extra tuple components in the destination array are set to the default.
63  /// It's up to the caller to ensure that the types can convert with
64  /// sufficient precision.
65  /// src_offset, dst_offset, and nelements each expect a number of tuples.
66  /// If nelements >= 0, only the given number of tuples are copied.
67  template <typename V>
68  void convertFrom(const CE_ArrayView<V> &src,
69  int src_tuplesize = 1, int dst_tuplesize = 1,
70  exint src_offset = 0, exint dst_offset = 0,
71  exint nelements = -1, T default_value = 0);
72 
73  /// The same as convertFrom(), except this converts a selection of
74  /// tuples listed in the index array. The indices should be in the range
75  /// [0, src.size()/src_tuplesize - 1].
76  template <typename V, typename I>
77  void convertFromIndices(const CE_ArrayView<V> &src,
78  const CE_ArrayView<I> &indices,
79  int src_tuplesize = 1, int dst_tuplesize = 1,
80  exint dst_offset = 0,
81  T default_value = 0);
82 
83  /// Extract the channel from this into dst.
84  void extractChannel(CE_ArrayView<T> &dst, int tuplesize, int comp) const;
85 
86  /// Insert the channel from src into this.
87  void insertChannel(const CE_ArrayView<T> &src, int tuplesize, int comp);
88 
89  /// Sum entries of this into dst. exclusive true will have dst
90  /// elements only include the values of this strictly prior to itself.
91  /// oneifnonzero will count number of non-zero entries in this.
92  void prefixSum(CE_ArrayView<T> &dst, bool exclusive=true,
93  CE_ScanOp op = CE_ScanOp::ADD) const;
94 
95  void iota();
96 
97  /// Reads a single value from the array, blocking.
98  /// May throw CE Exceptions.
99  T readValue(int idx) const;
100  /// Writes a single value to the array.
101  /// May throw CE Exceptions.
102  void writeValue(int idx, const T &val, bool blocking=true);
103  /// Reads the entire array into a UT_Array, resizing UT_Array
104  /// to be our size.
105  /// May throw CE Exceptions.
106  void readIntoArray(UT_Array<T> &array) const;
107 
108  /// Sort the array.
109  void sort(bool is_descending = false, int maxbits = 0)
110  {
111  cl::Buffer emptybuf;
112  CE_ArrayView<T> emptyvals(emptybuf);
113  sortInternal(emptyvals, is_descending, maxbits);
114  }
115 
116  /// Sort the array and the values.
117  template <typename V>
118  void sortValues(CE_ArrayView<V> &vals, bool is_descending = false, int maxbits = 0)
119  {
120  UT_ASSERT(vals.size() == size());
121  sortInternal(vals, is_descending, maxbits);
122  }
123 
124  /// Reduce to long for integral types and fpreal64 for floats,
125  /// since the caller can always downcast after the fact if desired.
126  using reduce_t = typename std::conditional<std::is_integral<T>::value,
128 
129  reduce_t min(int tuplesize = 1, int comp = 0) const;
130  reduce_t minAbs(int tuplesize = 1, int comp = 0) const;
131  reduce_t minLength(int tuplesize = 1) const;
132  reduce_t max(int tuplesize = 1, int comp = 0) const;
133  reduce_t maxAbs(int tuplesize = 1, int comp = 0) const;
134  reduce_t maxLength(int tuplesize = 1) const;
135  reduce_t sum(int tuplesize = 1, int comp = 0) const;
136  reduce_t sumAbs(int tuplesize = 1, int comp = 0) const;
137  reduce_t sumSqr(int tuplesize = 1, int comp = 0) const;
138 
139  fpreal64 average(int tuplesize = 1, int comp = 0) const
140  {
141  exint tuplecount = size() / tuplesize;
142  fpreal64 fsum = static_cast<fpreal64>(sum(tuplesize, comp));
143  return fsum / tuplecount;
144  }
145 
146  fpreal64 rms(int tuplesize = 1, int comp = 0) const
147  {
148  exint tuplecount = size() / tuplesize;
149  fpreal64 ms = static_cast<fpreal64>(sumSqr(tuplesize, comp));
150  ms /= tuplecount;
151  return SYSsqrt(ms);
152  }
153 
154  reduce_t dot(const CE_ArrayView<T> &b) const;
155 
156  void constant(T cval);
157 
158  // Reorder the array to the ordering in the supplied order array.
159  template <typename I>
160  void reorder(const CE_ArrayView<I> &order);
161 
162  cl::KernelFunctor bind(cl::Kernel &k) const;
163  cl::KernelFunctor bind(const char *kernel_name) const;
164 
165 protected:
166 
167  cl::Kernel loadKernel(const char *kernel_name,
168  const char *opt = NULL) const;
169 
170  reduce_t doReduce(const char *reduce_flags, const CE_ArrayView<T> *a,
171  int tuplesize = 1, int comp = 0) const;
172 
173  template <typename V>
174  V reduceGroup(CE_ArrayView<V> &out, uint groupsize,
175  const char *reduce_flags) const;
176 
177  // Internal sort functions.
178  template <typename V>
179  void sortInternal(CE_ArrayView<V> &vals, bool is_descending, int maxbits);
180 
181  // Appends a type name we use as part of type defines in array.cl
182  template <typename V>
183  static void appendElemType(UT_WorkBuffer &wb);
184 
185  // Promote 16-bit float kernel arguments to 32-bit float.
186  using scalar_arg_t = typename std::conditional_t<
187  std::is_same_v<T, fpreal16>,
189 
190  scalar_arg_t scalarKernelArg(T v) { return static_cast<scalar_arg_t>(v); }
191 
192  template <typename V>
193  class Buffer;
194 
195  template <typename V>
196  friend class CE_Array;
197 
199  const exint mySize;
200 };
201 
202 // Exported instantiantions from libCE:
214 
215 // We need to include this here since it is templated
216 // on the value type as well.
217 #include "CE_ArraySortImpl.h"
218 
219 #endif
#define CE_API
Definition: CE_API.h:13
type
Definition: core.h:556
fpreal64 rms(int tuplesize=1, int comp=0) const
Definition: CE_ArrayView.h:146
A simple OpenCL-based array class.
Definition: CE_Array.h:28
GLsizei GLenum const void * indices
Definition: glcorearb.h:406
CE_EXTERN_TEMPLATE(CE_ArrayView< uint8 >)
void sortValues(CE_ArrayView< V > &vals, bool is_descending=false, int maxbits=0)
Sort the array and the values.
Definition: CE_ArrayView.h:118
typename std::conditional< B, T, F >::type conditional_t
Definition: core.h:266
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLfloat * value
Definition: glcorearb.h:824
const cl::Buffer & buffer() const
Definition: CE_ArrayView.h:49
int64 exint
Definition: SYS_Types.h:125
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
T prefixSum(std::vector< T > &vec, bool threaded=true, OpT op=OpT())
Computes inclusive prefix sum of a vector.
Definition: PrefixSum.h:71
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
typename std::conditional< std::is_integral< T >::value, exint, fpreal64 >::type reduce_t
Definition: CE_ArrayView.h:127
float fpreal32
Definition: SYS_Types.h:200
double fpreal64
Definition: SYS_Types.h:201
const cl::Buffer myBuffer
Definition: CE_ArrayView.h:198
exint size() const
Returns the number of elements in the array.
Definition: CE_ArrayView.h:47
fpreal64 dot(const CE_VectorT< T > &a, const CE_VectorT< T > &b)
Definition: CE_Vector.h:138
typename std::conditional_t< std::is_same_v< T, fpreal16 >, fpreal32, T > scalar_arg_t
Definition: CE_ArrayView.h:188
GLdouble GLdouble GLint GLint order
Definition: glad.h:2676
const exint mySize
Definition: CE_ArrayView.h:199
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
CE_ScanOp
CE_Array prefix sum operators.
Definition: CE_ArrayView.h:30
GLsizeiptr size
Definition: glcorearb.h:664
GLenum GLenum dst
Definition: glcorearb.h:1793
scalar_arg_t scalarKernelArg(T v)
Definition: CE_ArrayView.h:190
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLuint GLfloat * val
Definition: glcorearb.h:1608
Kernel functor interface.
Definition: cl.hpp:3587
Memory buffer interface.
Definition: cl.hpp:1867
Kernel interface that implements cl_kernel.
Definition: cl.hpp:2544
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:165
fpreal64 average(int tuplesize=1, int comp=0) const
Definition: CE_ArrayView.h:139
void sort(bool is_descending=false, int maxbits=0)
Sort the array.
Definition: CE_ArrayView.h:109
bool isEmpty() const
Definition: CE_ArrayView.h:48
unsigned int uint
Definition: SYS_Types.h:45
GLenum src
Definition: glcorearb.h:1793