linsol_internal.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_LINSOL_INTERNAL_HPP
27 #define CASADI_LINSOL_INTERNAL_HPP
28 
29 #include "linsol.hpp"
30 #include "function_internal.hpp"
31 #include "plugin_interface.hpp"
32 
34 
35 namespace casadi {
36 
37  struct CASADI_EXPORT LinsolMemory : public ProtoFunctionMemory {
38  // Current state of factorization
39  bool is_sfact, is_nfact;
40 
41  // Constructor
42  LinsolMemory() : is_sfact(false), is_nfact(false) {}
43  };
44 
48  class CASADI_EXPORT LinsolInternal
49  : public ProtoFunction, public PluginInterface<LinsolInternal> {
50  public:
52  LinsolInternal(const std::string& name, const Sparsity& sp);
53 
55  ~LinsolInternal() override;
56 
60  void disp(std::ostream& stream, bool more) const override;
61 
65  virtual void disp_more(std::ostream& stream) const {}
66 
68  void init(const Dict& opts) override;
69 
73  void* alloc_mem() const override { return new LinsolMemory();}
74 
78  int init_mem(void* mem) const override;
79 
83  void free_mem(void *mem) const override { delete static_cast<LinsolMemory*>(mem);}
84 
86  virtual void linsol_eval_sx(const SXElem** arg, SXElem** res,
87  casadi_int* iw, SXElem* w, void* mem,
88  bool tr, casadi_int nrhs) const;
89 
90 #if 0
91  // (Re)factorize the system
92  casadi_int factorize(void* mem, const double* A) const;
93 
94  // Needs symbolic factorization
95  virtual bool needs_sfact(void* mem, const double* A) const;
96 
97  // Needs numeric factorization
98  virtual bool needs_nfact(void* mem, const double* A) const;
99 #endif
100 
101  // Symbolic factorization
102  virtual int sfact(void* mem, const double* A) const { return 0;}
103 
105  virtual int nfact(void* mem, const double* A) const;
106 
107  // Solve numerically
108  virtual int solve(void* mem, const double* A, double* x, casadi_int nrhs, bool tr) const;
109 
111  virtual double det(void* mem, const double* A) const;
112 
114  virtual casadi_int neig(void* mem, const double* A) const;
115 
117  virtual casadi_int rank(void* mem, const double* A) const;
118 
120  virtual void generate(CodeGenerator& g, const std::string& A, const std::string& x,
121  casadi_int nrhs, bool tr) const;
122 
124  virtual void generate_det(CodeGenerator& g, const std::string& A,
125  const std::string& d) const;
126 
129  virtual size_t sz_w_fact() const { return 0; }
130 
131  // Creator function for internal class
132  typedef LinsolInternal* (*Creator)(const std::string& name, const Sparsity& sp);
133 
134  // Symbolic determinant, provided as a plugin static method (e.g. symbolicqr)
135  typedef SX (*Det)(const SX& A, const Dict& opts);
136 
137  // Static functions exposed by a plugin without instantiation
138  struct Exposed{ Det det; };
139 
141  static std::map<std::string, Plugin> solvers_;
142 
143 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
144  static std::mutex mutex_solvers_;
145 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
146 
148  static const std::string infix_;
149 
150  // Get name of the plugin
151  const char* plugin_name() const override = 0;
152 
154  casadi_int nrow() const { return sp_.size1();}
155  casadi_int ncol() const { return sp_.size2();}
156  const casadi_int* colind() const { return sp_.colind();}
157  const casadi_int* row() const { return sp_.row();}
158  casadi_int nnz() const { return sp_.nnz();}
159 
163  void serialize_type(SerializingStream &s) const override;
167  void serialize_body(SerializingStream &s) const override;
168 
172  static ProtoFunction* deserialize(DeserializingStream& s);
173 
174  // Sparsity pattern of the linear system
176 
177  protected:
181  explicit LinsolInternal(DeserializingStream& s);
182  };
183 
184 } // namespace casadi
186 
187 #endif // CASADI_LINSOL_INTERNAL_HPP
Helper class for C code generation.
Helper class for Serialization.
const casadi_int * colind() const
static std::map< std::string, Plugin > solvers_
Collection of solvers.
const casadi_int * row() const
void * alloc_mem() const override
Create memory block.
void free_mem(void *mem) const override
Free memory block.
virtual size_t sz_w_fact() const
virtual int sfact(void *mem, const double *A) const
const char * plugin_name() const override=0
casadi_int nnz() const
casadi_int nrow() const
Get sparsity pattern.
virtual void disp_more(std::ostream &stream) const
Print more.
static const std::string infix_
Infix.
casadi_int ncol() const
Sparse matrix class. SX and DM are specializations.
Interface for accessing input and output data structures.
Base class for FunctionInternal and LinsolInternal.
The basic scalar symbolic class of CasADi.
Definition: sx_elem.hpp:75
Helper class for Serialization.
General sparsity class.
Definition: sparsity.hpp:200
The casadi namespace.
Definition: archiver.cpp:28
Matrix< SXElem > SX
Definition: sx_fwd.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
Function memory with temporary work vectors.