HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CE_SparseMatrix.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_SparseMatrix.h ( CE Library, C++)
7  *
8  * COMMENTS: Compute Engine Sparse Matrix.
9  */
10 
11 #ifndef __CE_SparseMatrix__
12 #define __CE_SparseMatrix__
13 
14 #include "CE_API.h"
15 #include "CE_Vector.h"
16 #include "CE_Context.h"
17 
18 #include <UT/UT_NonCopyable.h>
19 #include <UT/UT_SparseMatrix.h>
20 
21 #include <type_traits>
22 
23 #ifdef CE_ENABLED
24 
25 /// An ELL-format sparse matrix for OpenCL. This class is fairly limited;
26 /// its primary purpose is to initialize with an alread-populated
27 /// UT_SparseMatrixELL, then call its solveConjugateGradient function to
28 /// solve a linear system using OpenCL.
29 template <typename T, bool useexint = false>
31 {
32  typedef typename std::conditional<useexint, exint, int>::type inttype;
33 
34 public:
35 
36  typedef T value_type;
37 
39 
40  virtual ~CE_SparseMatrixELLT();
41 
43 
44  /// Initialize to the given number of rows and non-zeros per row.
45  void init(inttype rows, int nzperrow, bool colmajor = true);
46 
47  /// Initialize from the provided UT_SparseMatrixELL.
48  template <bool colmajor>
49  void initFromMatrix(const UT_SparseMatrixELLT<T, colmajor, useexint> &src,
50  bool block = true);
51 
52  /// Copy back to the provided UT_SparseMatrixELL. Only callable with
53  /// column-major destinations.
54  void matchAndCopyToMatrix(UT_SparseMatrixELLT<T, true, useexint> &dst,
55  bool block = true) const;
56 
57  /// Returns the number of rows.
58  inttype getNumRows() const { return myRows; }
59 
60  /// Returns the number of non-zeros per row.
61  int getNonZerosPerRow() const { return myNonZeros; }
62 
63  /// Sparse-matrix vector multiply.
64  void multVec(const CE_VectorT<T> &v, CE_VectorT<T> &result) const;
65 
66  /// Get the inverse of the diagonal.
67  void getDiagonalInv(CE_VectorT<T> &result) const;
68 
69  /// Solve Ax = b using conjugate gradient with Jacobi pre-conditioning,
70  float solveConjugateGradient(CE_VectorT<T> &x, const CE_VectorT<T> &b,
71  T tol2=1e-5, int max_iters = -1,
72  int *iterout = NULL) const;
73 
74 protected:
75  void allocBuffers() const;
76  void releaseBuffers();
77  cl::Kernel loadKernel(const char *kernelName, const char *opt = NULL) const;
78  cl::KernelFunctor bind(const char *kernelName) const;
79  mutable cl::Buffer myRowValsBuffer, myColumnsBuffer;
80  inttype myRows;
82  bool myColMajor;
83  cl::NDRange myGlobalRange, myLocalRange;
84 };
85 
86 template <typename T, bool useexint>
87 template <bool colmajor>
88 void
90  bool blocking)
91 {
92  init(src.getNumRows(), src.getNonZerosPerRow(), colmajor);
93  size_t totalsize = myRows * (inttype)myNonZeros;
94  if (!totalsize)
95  return;
96  const UT_ValArray<T> &srcValues = src.getRowValues();
97  const T *rowdata = &srcValues(0);
98  const UT_ValArray<inttype> &cols = src.getColumns();
99  const inttype *coldata = &cols(0);
100  CE_Context *context = CE_Context::getContext();
101  context->writeBuffer(myRowValsBuffer, totalsize * sizeof(T), rowdata, false);
102  context->writeBuffer(myColumnsBuffer, totalsize * sizeof(inttype), coldata, blocking);
103 }
104 
105 
108 
109 #else
110 template <typename T>
112 {
113 };
114 
115 #endif
116 #endif
#define CE_API
Definition: CE_API.h:10
void
Definition: png.h:1083
exint getNumRows() const
const GLdouble * v
Definition: glcorearb.h:837
const UT_ValArray< inttype > & getColumns() const
CE_SparseMatrixELLT< fpreal64 > CE_SparseMatrixELLD
**But if you need a result
Definition: thread.h:613
void writeBuffer(const cl::Buffer &buf, size_t size, const void *p, bool blocking=true, size_t offset=0)
Write the specified number of bytes to the buffer.
cl::Buffer myRowValsBuffer
static CE_Context * getContext(bool gl_shared=true, bool shared_fallback=true)
void initFromMatrix(const UT_SparseMatrixELLT< T, colmajor, useexint > &src, bool block=true)
Initialize from the provided UT_SparseMatrixELL.
const UT_ValArray< T > & getRowValues() const
cl::NDRange myLocalRange
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLint GLenum GLint x
Definition: glcorearb.h:409
GLenum GLenum dst
Definition: glcorearb.h:1793
int getNonZerosPerRow() const
Returns the number of non-zeros per row.
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
CE_SparseMatrixELLT< fpreal32 > CE_SparseMatrixELLF
#define const
Definition: zconf.h:214
int getNonZerosPerRow() const
type
Definition: core.h:1059
GLenum src
Definition: glcorearb.h:1793