HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CE_Vector.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_VectorT.h ( CE Library, C++)
7  *
8  * COMMENTS: Compute Engine Vector.
9  */
10 
11 #ifndef __CE_Vector__
12 #define __CE_Vector__
13 
14 #include "CE_API.h"
15 #include "CE_Context.h"
16 
17 #include <UT/UT_Vector.h>
18 
19 /// A simple OpenCL-based vector class that at the moment just mimics just
20 /// enough of UT_Vector to be used in CE_SparseMatrixELLT::solveConjugateGradient.
21 template <typename T>
23 {
24 public:
25 
26  typedef T value_type;
27 
28  CE_VectorT();
29 
30  /// Initialize to given length.
31  CE_VectorT(exint len, bool use_pool=false, bool read=true, bool write=true);
32 
33  /// Initialize to zero in in the cases when we want to
34  /// init / not init the vector based on size
35  CE_VectorT(bool use_pool,
36  bool read,
37  bool write);
38 
39  /// Copy constructor.
40  CE_VectorT(const CE_VectorT<T> &v);
41 
42  ~CE_VectorT();
43 
44  /// Initialize to given length.
45  /// NOTE- values are undefined.
46  void init(exint len, bool doalloc=true);
47 
48  /// For "compatibility" with UT_Vector.
49  /// NOTE: Asserts if nl is not zero!
50  void init(exint nl, exint nh);
51 
52  /// Initialize from a UT_Vector.
53  void initFromVector(const UT_VectorT<T> &src, bool block = true);
54 
55  /// Initialize from a cl::Buffer, which this vector does *not* own.
56  void initFromBuffer(const cl::Buffer &src, exint size, int tuplesize = 1);
57 
58  /// Resize the provided UT_Vector to fit and copy this vector's data there.
59  void matchAndCopyToVector(UT_VectorT<T> &dst, bool block = true) const;
60  /// Returns the vector length.
61  exint length() const { return myLen; }
62  /// Returns the size of the tuple contained in the vector.
63  int tupleSize() const { return myTupleSize; }
64  /// Returns the number of tuples contained in the vector.
65  exint tupleCount() const { return myLen / myTupleSize; }
66 
67  /// Returns the underlying OpenCL buffer.
68  const cl::Buffer &buffer() const { return myBuffer; }
69 
70  /// The following functions all mimic those of the same name in UT_Vector.
71  void multAndSet(const CE_VectorT<T> &a, const CE_VectorT<T> &b);
72 
73  void negPlus(const CE_VectorT<T> &a);
74 
75  void scaleAddVec(T s, const CE_VectorT<T> &a);
76 
77  fpreal64 norm(int type) const;
78 
79  void zero() { setValue(0); }
80 
81  void addScaledVec(T s, const CE_VectorT<T> &a);
82 
83  /// Operators
84  /// NOTE: operator= requires the destination be a matching size.
86 
87  /// Reductions of the vector to a single value.
88  fpreal64 sum(int comp = 0) const;
89  fpreal64 sumAbs(int comp = 0) const;
90  fpreal64 sumSqr(int comp = 0) const;
91  fpreal64 min(int comp = 0) const;
92  fpreal64 minAbs(int comp = 0) const;
93  fpreal64 max(int comp = 0) const;
94  fpreal64 maxAbs(int comp = 0) const;
95  fpreal64 average(int comp = 0) const { return sum(comp) / tupleCount(); }
96  fpreal64 rms(int comp = 0) const { return SYSsqrt(sumSqr(comp) / tupleCount()); }
97 
98  /// Returns the dot product with provided vector.
99  fpreal64 dot(const CE_VectorT<T> &a) const;
100 
101  cl::KernelFunctor bind(cl::Kernel &k) const;
102  void setValue(T cval);
103  void copyFrom(const CE_VectorT<T> &v);
104 protected:
105 
106  const cl::Buffer &allocBuffer() const;
107  void releaseBuffer();
108  cl::KernelFunctor bind(const char *kernel_name) const;
109  cl::Kernel loadKernel(const char *kernel_name, const char *opt = NULL) const;
110 
111 
112  // Reduction helpers
113  void getReductionRanges(const cl::Kernel &k,
114  cl::NDRange &global_range, cl::NDRange &local_range,
115  uint &groupsize, uint &ngroups,
116  size_t &accumsize) const;
117  fpreal64 reduceGroup(cl::Buffer out, uint groupsize, uint ngroups,
118  size_t accumsize, const char *reduce_flags) const;
119 
120  // Main reduction function.
121  fpreal64 doReduce(
122  const char *reduce_flags,
123  const CE_VectorT<T> *a = NULL,
124  int tuplesize = 1,
125  int comp = 0) const;
126 
128  cl::NDRange myGlobalRange, myLocalRange;
131 
132  bool myRead, myWrite, myUsePool;
133 };
134 
135 template <typename T>
136 inline fpreal64
138 {
139  return a.dot(b);
140 }
141 
142 
145 
146 #endif
#define CE_API
Definition: CE_API.h:13
exint myLen
Definition: CE_Vector.h:129
cl::NDRange myLocalRange
Definition: CE_Vector.h:128
fpreal64 rms(int comp=0) const
Definition: CE_Vector.h:96
const GLdouble * v
Definition: glcorearb.h:837
exint length() const
Returns the vector length.
Definition: CE_Vector.h:61
int64 exint
Definition: SYS_Types.h:125
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
bool myWrite
Definition: CE_Vector.h:132
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
__hostdev__ void setValue(uint32_t offset, bool v)
Definition: NanoVDB.h:5750
CE_VectorT< fpreal32 > CE_VectorF
Definition: CE_Vector.h:143
double fpreal64
Definition: SYS_Types.h:201
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
const cl::Buffer & buffer() const
Returns the underlying OpenCL buffer.
Definition: CE_Vector.h:68
fpreal64 dot(const CE_VectorT< T > &a, const CE_VectorT< T > &b)
Definition: CE_Vector.h:137
void zero()
Definition: CE_Vector.h:79
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
fpreal64 dot(const CE_VectorT< T > &a) const
Returns the dot product with provided vector.
cl::Buffer myBuffer
Definition: CE_Vector.h:127
GLsizeiptr size
Definition: glcorearb.h:664
GLenum GLenum dst
Definition: glcorearb.h:1793
LeafData & operator=(const LeafData &)=delete
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
Kernel functor interface.
Definition: cl.hpp:3585
Memory buffer interface.
Definition: cl.hpp:1867
NDRange interface.
Definition: cl.hpp:2466
Kernel interface that implements cl_kernel.
Definition: cl.hpp:2544
int myTupleSize
Definition: CE_Vector.h:130
exint tupleCount() const
Returns the number of tuples contained in the vector.
Definition: CE_Vector.h:65
fpreal64 average(int comp=0) const
Definition: CE_Vector.h:95
T value_type
Definition: CE_Vector.h:26
CE_VectorT< fpreal64 > CE_VectorD
Definition: CE_Vector.h:144
unsigned int uint
Definition: SYS_Types.h:45
int tupleSize() const
Returns the size of the tuple contained in the vector.
Definition: CE_Vector.h:63
GLenum src
Definition: glcorearb.h:1793