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