xerus
a general purpose tensor library
cholmod_wrapper.h
Go to the documentation of this file.
1 // Xerus - A General Purpose Tensor Library
2 // Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf.
3 //
4 // Xerus is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published
6 // by the Free Software Foundation, either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // Xerus is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with Xerus. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // For further information on Xerus visit https://libXerus.org
18 // or contact us at contact@libXerus.org.
19 
25 #pragma once
26 
27 #include <memory>
28 #include <map>
29 #include <mutex>
30 #include <functional>
31 
32 #include <suitesparse/cholmod.h>
33 #include <suitesparse/SuiteSparseQR.hpp>
34 
35 
36 namespace xerus {
37  class Tensor;
38 
39  namespace internal {
40 
41 
43  struct CholmodCommon final {
44  struct RestrictedAccess final {
45  cholmod_common* const c;
46  std::mutex &lock;
47  RestrictedAccess(cholmod_common* const _c, std::mutex &_lock);
48  operator cholmod_common*() const;
50  };
51 
52  std::unique_ptr<cholmod_common> c;
53  std::mutex lock;
54  CholmodCommon();
56  RestrictedAccess get();
57  std::function<void(cholmod_sparse*)> get_deleter();
58  };
59 
61  class CholmodSparse final {
62  public:
63  using ptr_type = std::unique_ptr<cholmod_sparse, std::function<void(cholmod_sparse*)>>;
65 
67  CholmodSparse(cholmod_sparse *_matrix);
68 
70  CholmodSparse(const size_t _m, const size_t _n, const size_t _N);
71 
74  CholmodSparse(const std::map<size_t, double>& _input, const size_t _m, const size_t _n, const bool _transpose);
75 
77  std::map<size_t, double> to_map(double _alpha=1.0) const;
78 
80  CholmodSparse operator*(const CholmodSparse &_rhs) const;
81 
83  void transpose();
84 
86  static void matrix_matrix_product(std::map<size_t, double>& _C,
87  const size_t _leftDim,
88  const size_t _rightDim,
89  const double _alpha,
90  const std::map<size_t, double>& _A,
91  const bool _transposeA,
92  const size_t _midDim,
93  const std::map<size_t, double>& _B,
94  const bool _transposeB);
95 
97  static void solve_sparse_rhs(std::map<size_t, double>& _x,
98  size_t _xDim,
99  const std::map<size_t, double>& _A,
100  const bool _transposeA,
101  const std::map<size_t, double>& _b,
102  size_t _bDim
103  );
104 
106  static void solve_dense_rhs(double * _x,
107  size_t _xDim,
108  const std::map<size_t, double>& _A,
109  const bool _transposeA,
110  const double* _b,
111  size_t _bDim
112  );
113 
119  static std::tuple<std::map<size_t, double>, std::map<size_t, double>, size_t> qc(
120  const std::map<size_t, double> &_A,
121  const bool _transposeA,
122  size_t _m,
123  size_t _n,
124  bool _fullrank
125  );
126 
132  static std::tuple<std::map<size_t, double>, std::map<size_t, double>, size_t> cq(
133  const std::map<size_t, double> &_A,
134  const bool _transposeA,
135  size_t _m,
136  size_t _n,
137  bool _fullrank
138  );
139  };
140 
141 
142  extern thread_local CholmodCommon cholmodObject;
143  }
144 }
std::tuple< std::unique_ptr< double[]>, std::unique_ptr< double[]>, size_t > cq(const double *const _A, const size_t _m, const size_t _n)
: splits A = C*Q, with _C an rxm matrix (where r is the rank of _A) and _Q orthogonal.
thread_local CholmodCommon cholmodObject
std::function< void(cholmod_sparse *)> get_deleter()
std::tuple< std::unique_ptr< double[]>, std::unique_ptr< double[]>, size_t > qc(const double *const _A, const size_t _m, const size_t _n)
: splits A = Q*C, with _C an rxn matrix (where r is the rank of _A) and _Q orthogonal.
RestrictedAccess(cholmod_common *const _c, std::mutex &_lock)
The main namespace of xerus.
Definition: basic.h:37
wrapper class for the cholmod sparse matrix objects
wrapper object for the cholmod_common struct to automatically call the constructor and destructor ...
std::unique_ptr< cholmod_common > c
std::unique_ptr< cholmod_sparse, std::function< void(cholmod_sparse *)> > ptr_type
XERUS_force_inline void transpose(double *const __restrict _out, const double *const __restrict _in, const size_t _leftDim, const size_t _rightDim)
void matrix_matrix_product(double *const _C, const size_t _leftDim, const size_t _rightDim, const double _alpha, const std::map< size_t, double > &_A, const bool _transposeA, const size_t _midDim, const double *const _B, const bool _transposeB)
IndexedTensorMoveable< tensor_type > operator*(const value_t _factor, IndexedTensorMoveable< tensor_type > &&_tensor)