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 
16 #include "CE_Context.h"
17 
18 #ifdef CE_ENABLED
19 
20 #include <UT/UT_Vector.h>
21 
22 /// A simple OpenCL-based vector class that at the moment just mimics just
23 /// enough of UT_Vector to be used in CE_SparseMatrixELLT::solveConjugateGradient.
24 template <typename T>
26 {
27 public:
28 
29  typedef T value_type;
30 
31  CE_VectorT();
32 
33  /// Initialize to given length.
34  CE_VectorT(exint len, bool use_pool=false, bool read=true, bool write=true);
35 
36  /// Initialize to zero in in the cases when we want to
37  /// init / not init the vector based on size
38  CE_VectorT(bool use_pool,
39  bool read,
40  bool write);
41 
42  /// Copy constructor.
43  CE_VectorT(const CE_VectorT<T> &v);
44 
45  ~CE_VectorT();
46 
47  /// Initialize to given length.
48  /// NOTE- values are undefined.
49  void init(exint len, bool doalloc=true);
50 
51  /// For "compatibility" with UT_Vector.
52  /// NOTE: Asserts if nl is not zero!
53  void init(exint nl, exint nh);
54 
55  /// Initialize from a UT_Vector.
56  void initFromVector(const UT_VectorT<T> &src, bool block = true);
57 
58  /// Initialize from a cl::Buffer, which this vector does *not* own.
59  void initFromBuffer(const cl::Buffer &src, exint size, int tuplesize = 1);
60 
61  /// Resize the provided UT_Vector to fit and copy this vector's data there.
62  void matchAndCopyToVector(UT_VectorT<T> &dst, bool block = true) const;
63  /// Returns the vector length.
64  exint length() const { return myLen; }
65  /// Returns the size of the tuple contained in the vector.
66  int tupleSize() const { return myTupleSize; }
67  /// Returns the number of tuples contained in the vector.
68  exint tupleCount() const { return myLen / myTupleSize; }
69 
70  /// Returns the underlying OpenCL buffer.
71  const cl::Buffer &buffer() const { return myBuffer; }
72 
73  /// The following functions all mimic those of the same name in UT_Vector.
74  void multAndSet(const CE_VectorT<T> &a, const CE_VectorT<T> &b);
75 
76  void negPlus(const CE_VectorT<T> &a);
77 
78  void scaleAddVec(T s, const CE_VectorT<T> &a);
79 
80  fpreal64 norm(int type) const;
81 
82  void zero() { setValue(0); }
83 
84  void addScaledVec(T s, const CE_VectorT<T> &a);
85 
86  /// Operators
87  /// NOTE: operator= requires the destination be a matching size.
89 
90  /// Reductions of the vector to a single value.
91  fpreal64 sum(int comp = 0) const;
92  fpreal64 sumAbs(int comp = 0) const;
93  fpreal64 sumSqr(int comp = 0) const;
94  fpreal64 min(int comp = 0) const;
95  fpreal64 minAbs(int comp = 0) const;
96  fpreal64 max(int comp = 0) const;
97  fpreal64 maxAbs(int comp = 0) const;
98  fpreal64 average(int comp = 0) const { return sum(comp) / tupleCount(); }
99  fpreal64 rms(int comp = 0) const { return SYSsqrt(sumSqr(comp) / tupleCount()); }
100 
101  /// Returns the dot product with provided vector.
102  fpreal64 dot(const CE_VectorT<T> &a) const;
103 
104  cl::KernelFunctor bind(cl::Kernel &k) const;
105  void setValue(T cval);
106  void copyFrom(const CE_VectorT<T> &v);
107 protected:
108 
109  const cl::Buffer &allocBuffer() const;
110  void releaseBuffer();
111  cl::KernelFunctor bind(const char *kernelName) const;
112  cl::Kernel loadKernel(const char *kernelName, const char *opt = NULL) const;
113 
114 
115  // Reduction helpers
116  void getReductionRanges(const cl::Kernel &k,
117  cl::NDRange &globalRange, cl::NDRange &localRange,
118  uint &groupsize, uint &ngroups,
119  size_t &accumsize) const;
120  fpreal64 reduceGroup(cl::Buffer out, uint groupsize, uint ngroups,
121  size_t accumsize, const char *reduceFlags) const;
122 
123  // Main reduction function.
124  fpreal64 doReduce(
125  const char *reduceFlags,
126  const CE_VectorT<T> *a = NULL,
127  int tuplesize = 1,
128  int comp = 0) const;
129 
131  cl::NDRange myGlobalRange, myLocalRange;
134 
135  bool myRead, myWrite, myUsePool;
136 };
137 
138 template <typename T>
139 inline fpreal64
141 {
142  return a.dot(b);
143 }
144 
145 
148 
149 #else
150 template <typename T>
151 class CE_API CE_VectorT
152 {
153 };
154 
155 #endif
156 #endif
#define CE_API
Definition: CE_API.h:11
exint myLen
Definition: CE_Vector.h:132
cl::NDRange myLocalRange
Definition: CE_Vector.h:131
fpreal64 rms(int comp=0) const
Definition: CE_Vector.h:99
const GLdouble * v
Definition: glcorearb.h:837
exint length() const
Returns the vector length.
Definition: CE_Vector.h:64
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:135
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:146
double fpreal64
Definition: SYS_Types.h:201
const cl::Buffer & buffer() const
Returns the underlying OpenCL buffer.
Definition: CE_Vector.h:71
fpreal64 dot(const CE_VectorT< T > &a, const CE_VectorT< T > &b)
Definition: CE_Vector.h:140
void zero()
Definition: CE_Vector.h:82
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:130
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:133
exint tupleCount() const
Returns the number of tuples contained in the vector.
Definition: CE_Vector.h:68
fpreal64 average(int comp=0) const
Definition: CE_Vector.h:98
T value_type
Definition: CE_Vector.h:29
CE_VectorT< fpreal64 > CE_VectorD
Definition: CE_Vector.h:147
type
Definition: core.h:1059
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:66
GLenum src
Definition: glcorearb.h:1793