HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 #ifdef CE_ENABLED
21 
22 /// An ELL-format sparse matrix for OpenCL. This class is fairly limited;
23 /// its primary purpose is to initialize with an alread-populated
24 /// UT_SparseMatrixELL, then call its solveConjugateGradient function to
25 /// solve a linear system using OpenCL.
26 template <typename T>
28 {
29 public:
30 
31  typedef T value_type;
32 
34 
35  virtual ~CE_SparseMatrixELLT();
36 
37  /// Initialize to the given number of rows and non-zeros per row.
38  void init(int rows, int nzperrow, bool colmajor = true);
39 
40  /// Initialize from the provided UT_SparseMatrixELL.
41  template <bool colmajor>
42  void initFromMatrix(const UT_SparseMatrixELLT<T, colmajor> &src,
43  bool block = true);
44 
45  /// Copy back to the provided UT_SparseMatrixELL. Only callable with
46  /// column-major destinations.
47  void matchAndCopyToMatrix(UT_SparseMatrixELLT<T, true> &dst,
48  bool block = true) const;
49 
50  /// Returns the number of rows.
51  int getNumRows() const { return myRows; }
52 
53  /// Returns the number of non-zeros per row.
54  int getNonZerosPerRow() const { return myNonZeros; }
55 
56  /// Sparse-matrix vector multiply.
57  void multVec(const CE_VectorT<T> &v, CE_VectorT<T> &result) const;
58 
59  /// Get the inverse of the diagonal.
60  void getDiagonalInv(CE_VectorT<T> &result) const;
61 
62  /// Solve Ax = b using conjugate gradient with Jacobi pre-conditioning,
63  float solveConjugateGradient(CE_VectorT<T> &x, const CE_VectorT<T> &b,
64  T tol2=1e-5, int max_iters = -1,
65  int *iterout = NULL) const;
66 
67 protected:
68  void allocBuffers() const;
69  void releaseBuffers();
70  cl::Kernel loadKernel(const char *kernelName, const char *opt = NULL) const;
71  cl::KernelFunctor bind(const char *kernelName) const;
72  mutable cl::Buffer myRowValsBuffer, myColumnsBuffer;
73  int myRows;
75  bool myColMajor;
76  cl::NDRange myGlobalRange, myLocalRange;
77 };
78 
79 template <typename T>
80 template <bool colmajor>
81 void
83  bool blocking)
84 {
85  init(src.getNumRows(), src.getNonZerosPerRow(), colmajor);
86  size_t totalsize = myRows * myNonZeros;
87  if (!totalsize)
88  return;
89  const UT_ValArray<T> &srcValues = src.getRowValues();
90  const T *rowdata = &srcValues(0);
91  const UT_IntArray &cols = src.getColumns();
92  const int *coldata = &cols(0);
94  queue.enqueueWriteBuffer(myRowValsBuffer, CL_FALSE,
95  0, totalsize * sizeof(T), rowdata);
96  queue.enqueueWriteBuffer(myColumnsBuffer, blocking,
97  0, totalsize * sizeof(int), coldata);
98 }
99 
100 
103 
104 #else
105 template <typename T>
107 {
108 };
109 
110 #endif
111 #endif
#define CE_API
Definition: CE_API.h:10
int getNonZerosPerRow() const
Returns the number of non-zeros per row.
int getNumRows() const
const GLdouble * v
Definition: glcorearb.h:836
const UT_ValArray< T > & getRowValues() 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::NDRange myLocalRange
int getNonZerosPerRow() const
void initFromMatrix(const UT_SparseMatrixELLT< T, colmajor > &src, bool block=true)
Initialize from the provided UT_SparseMatrixELL.
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
int getNumRows() const
Returns the number of rows.
const UT_IntArray & getColumns() const
cl::Buffer myRowValsBuffer
GLenum GLenum dst
Definition: glcorearb.h:1792
CommandQueue interface for cl_command_queue.
Definition: cl.hpp:2840
GLint GLenum GLint x
Definition: glcorearb.h:408
Kernel functor interface.
Definition: cl.hpp:3562
Memory buffer interface.
Definition: cl.hpp:1863
NDRange interface.
Definition: cl.hpp:2456
Kernel interface that implements cl_kernel.
Definition: cl.hpp:2534
CE_SparseMatrixELLT< fpreal32 > CE_SparseMatrixELLF
GLenum src
Definition: glcorearb.h:1792