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);
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 
66  /// Returns the underlying OpenCL buffer.
67  const cl::Buffer &buffer() const { return myBuffer; }
68 
69  /// The following functions all mimic those of the same name in UT_Vector.
70  void multAndSet(const CE_VectorT<T> &a, const CE_VectorT<T> &b);
71 
72  void negPlus(const CE_VectorT<T> &a);
73 
74  void scaleAddVec(T s, const CE_VectorT<T> &a);
75 
76  fpreal64 norm(int type) const;
77 
78  void zero() { setValue(0); }
79 
80  void addScaledVec(T s, const CE_VectorT<T> &a);
81 
82  /// Operators
83  /// NOTE: operator= requires the destination be a matching size.
84  CE_VectorT &operator= (const CE_VectorT<T> &v);
85 
86  /// Reductions of the vector to a single value.
87  fpreal64 sum() const;
88  fpreal64 sumAbs() const;
89  fpreal64 sumSqr() const;
90  fpreal64 min() const;
91  fpreal64 minAbs() const;
92  fpreal64 max() const;
93  fpreal64 maxAbs() const;
94  fpreal64 average() const {return sum() / length();}
95 
96  /// Returns the dot product with provided vector.
97  fpreal64 dot(const CE_VectorT<T> &a) const;
98 
99  cl::KernelFunctor bind(cl::Kernel &k) const;
100  void setValue(T cval);
101  void copyFrom(const CE_VectorT<T> &v);
102 protected:
103 
104  const cl::Buffer &allocBuffer() const;
105  void releaseBuffer();
106  cl::KernelFunctor bind(const char *kernelName) const;
107  cl::Kernel loadKernel(const char *kernelName, const char *opt = NULL) const;
108 
109 
110  // Reduction helpers
111  void getReductionRanges(const cl::Kernel &k,
112  cl::NDRange &globalRange, cl::NDRange &localRange,
113  uint &groupsize, uint &ngroups,
114  size_t &accumsize) const;
115  fpreal64 reduceGroup(cl::Buffer out, uint groupsize, uint ngroups,
116  size_t accumsize, const char *reduceFlags) const;
117 
118  // Main reduction function.
119  fpreal64 doReduce(const char* reduceFlags, const CE_VectorT<T> *a=NULL) const;
120 
122  cl::NDRange myGlobalRange, myLocalRange;
124 
125  bool myRead, myWrite, myUsePool;
126 };
127 
128 template <typename T>
129 inline fpreal64
131 {
132  return a.dot(b);
133 }
134 
135 
138 
139 #else
140 template <typename T>
141 class CE_API CE_VectorT
142 {
143 };
144 
145 #endif
146 #endif
#define CE_API
Definition: CE_API.h:10
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
exint myLen
Definition: CE_Vector.h:123
cl::NDRange myLocalRange
Definition: CE_Vector.h:122
exint length() const
Returns the vector length.
Definition: CE_Vector.h:64
int64 exint
Definition: SYS_Types.h:125
bool myWrite
Definition: CE_Vector.h:125
CE_VectorT< fpreal32 > CE_VectorF
Definition: CE_Vector.h:136
GLenum src
Definition: glcorearb.h:1792
void read(T &in, bool &v)
Definition: ImfXdr.h:611
GLsizeiptr size
Definition: glcorearb.h:663
double fpreal64
Definition: SYS_Types.h:201
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
const cl::Buffer & buffer() const
Returns the underlying OpenCL buffer.
Definition: CE_Vector.h:67
GLenum GLsizei len
Definition: glew.h:7782
const GLdouble * v
Definition: glcorearb.h:836
fpreal64 dot(const CE_VectorT< T > &a, const CE_VectorT< T > &b)
Definition: CE_Vector.h:130
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
void zero()
Definition: CE_Vector.h:78
fpreal64 dot(const CE_VectorT< T > &a) const
Returns the dot product with provided vector.
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:794
cl::Buffer myBuffer
Definition: CE_Vector.h:121
Kernel functor interface.
Definition: cl.hpp:3582
Memory buffer interface.
Definition: cl.hpp:1865
NDRange interface.
Definition: cl.hpp:2463
Kernel interface that implements cl_kernel.
Definition: cl.hpp:2541
fpreal64 average() const
Definition: CE_Vector.h:94
GLenum GLenum dst
Definition: glcorearb.h:1792
T value_type
Definition: CE_Vector.h:29
CE_VectorT< fpreal64 > CE_VectorD
Definition: CE_Vector.h:137
void write(T &out, bool v)
Definition: ImfXdr.h:332
GLdouble s
Definition: glew.h:1395
unsigned int uint
Definition: SYS_Types.h:45