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  queue.enqueueWriteBuffer(myRowValsBuffer, CL_FALSE,
99  0, totalsize * sizeof(T), rowdata);
100  queue.enqueueWriteBuffer(myColumnsBuffer, blocking,
101  0, totalsize * sizeof(inttype), coldata);
102 }
103 
104 
107 
108 #else
109 template <typename T>
111 {
112 };
113 
114 #endif
115 #endif
#define CE_API
Definition: CE_API.h:10
exint getNumRows() const
const GLdouble * v
Definition: glcorearb.h:836
const UT_ValArray< inttype > & getColumns() const
static CE_Context * getContext(bool gl_shared=false)
CE_SparseMatrixELLT< fpreal64 > CE_SparseMatrixELLD
cl_int enqueueWriteBuffer(const Buffer &buffer, cl_bool blocking,::size_t offset,::size_t size, const void *ptr, const VECTOR_CLASS< Event > *events=NULL, Event *event=NULL) const
Definition: cl.hpp:2926
#define CL_FALSE
Definition: cl.h:186
cl::CommandQueue getQueue() const
Definition: CE_Context.h:58
cl::Buffer myRowValsBuffer
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
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
GLenum GLenum dst
Definition: glcorearb.h:1792
CommandQueue interface for cl_command_queue.
Definition: cl.hpp:2840
int getNonZerosPerRow() const
Returns the number of non-zeros per row.
inttype getNumRows() const
Returns the number of rows.
GLint GLenum GLint x
Definition: glcorearb.h:408
Kernel functor interface.
Definition: cl.hpp:3562
Memory buffer interface.
Definition: cl.hpp:1863
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
NDRange interface.
Definition: cl.hpp:2456
Kernel interface that implements cl_kernel.
Definition: cl.hpp:2534
CE_SparseMatrixELLT< fpreal32 > CE_SparseMatrixELLF
int getNonZerosPerRow() const
GLenum src
Definition: glcorearb.h:1792