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_SparseMatrix.h>
19 
20 #include <type_traits>
21 
22 #ifdef CE_ENABLED
23 
24 /// An ELL-format sparse matrix for OpenCL. This class is fairly limited;
25 /// its primary purpose is to initialize with an alread-populated
26 /// UT_SparseMatrixELL, then call its solveConjugateGradient function to
27 /// solve a linear system using OpenCL.
28 template <typename T, bool useexint = false>
30 {
31  typedef typename std::conditional<useexint, exint, int>::type inttype;
32 
33 public:
34 
35  typedef T value_type;
36 
38 
39  virtual ~CE_SparseMatrixELLT();
40 
41  /// Initialize to the given number of rows and non-zeros per row.
42  void init(inttype rows, int nzperrow, bool colmajor = true);
43 
44  /// Initialize from the provided UT_SparseMatrixELL.
45  template <bool colmajor>
46  void initFromMatrix(const UT_SparseMatrixELLT<T, colmajor, useexint> &src,
47  bool block = true);
48 
49  /// Copy back to the provided UT_SparseMatrixELL. Only callable with
50  /// column-major destinations.
51  void matchAndCopyToMatrix(UT_SparseMatrixELLT<T, true, useexint> &dst,
52  bool block = true) const;
53 
54  /// Returns the number of rows.
55  inttype getNumRows() const { return myRows; }
56 
57  /// Returns the number of non-zeros per row.
58  int getNonZerosPerRow() const { return myNonZeros; }
59 
60  /// Sparse-matrix vector multiply.
61  void multVec(const CE_VectorT<T> &v, CE_VectorT<T> &result) const;
62 
63  /// Get the inverse of the diagonal.
64  void getDiagonalInv(CE_VectorT<T> &result) const;
65 
66  /// Solve Ax = b using conjugate gradient with Jacobi pre-conditioning,
67  float solveConjugateGradient(CE_VectorT<T> &x, const CE_VectorT<T> &b,
68  T tol2=1e-5, int max_iters = -1,
69  int *iterout = NULL) const;
70 
71 protected:
72  void allocBuffers() const;
73  void releaseBuffers();
74  cl::Kernel loadKernel(const char *kernelName, const char *opt = NULL) const;
75  cl::KernelFunctor bind(const char *kernelName) const;
76  mutable cl::Buffer myRowValsBuffer, myColumnsBuffer;
77  inttype myRows;
79  bool myColMajor;
80  cl::NDRange myGlobalRange, myLocalRange;
81 };
82 
83 template <typename T, bool useexint>
84 template <bool colmajor>
85 void
87  bool blocking)
88 {
89  init(src.getNumRows(), src.getNonZerosPerRow(), colmajor);
90  size_t totalsize = myRows * (inttype)myNonZeros;
91  if (!totalsize)
92  return;
93  const UT_ValArray<T> &srcValues = src.getRowValues();
94  const T *rowdata = &srcValues(0);
95  const UT_ValArray<inttype> &cols = src.getColumns();
96  const inttype *coldata = &cols(0);
98  context->writeBuffer(myRowValsBuffer, totalsize * sizeof(T), rowdata, false);
99  context->writeBuffer(myColumnsBuffer, totalsize * sizeof(inttype), coldata, blocking);
100 }
101 
102 
105 
106 #else
107 template <typename T>
109 {
110 };
111 
112 #endif
113 #endif
#define CE_API
Definition: CE_API.h:10
type
Definition: core.h:977
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
exint getNumRows() const
const UT_ValArray< inttype > & getColumns() const
CE_SparseMatrixELLT< fpreal64 > CE_SparseMatrixELLD
GLenum src
Definition: glcorearb.h:1792
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.
GLint GLenum GLint x
Definition: glcorearb.h:408
cl::Buffer myRowValsBuffer
GLuint64EXT * result
Definition: glew.h:14311
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
const GLdouble * v
Definition: glcorearb.h:836
cl::NDRange myLocalRange
int getNonZerosPerRow() const
Returns the number of non-zeros per row.
inttype getNumRows() const
Returns the number of rows.
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
CE_SparseMatrixELLT< fpreal32 > CE_SparseMatrixELLF
int getNonZerosPerRow() const
GLenum GLenum dst
Definition: glcorearb.h:1792