lapack_lu.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_LAPACK_LU_HPP
27 #define CASADI_LAPACK_LU_HPP
28 
29 #include "casadi/core/linsol_internal.hpp"
30 #include <casadi/interfaces/lapack/casadi_linsol_lapacklu_export.h>
31 
32 // On WebAssembly the wasm linker enforces strict function signatures, and
33 // the flang-built LAPACK on wasm uses the standard Fortran-77 ABI where
34 // CHARACTER arguments have a hidden trailing length (size_t). Native
35 // builds of LAPACK (OpenBLAS/MKL/Netlib via gfortran) also use this ABI
36 // but are tolerant of the missing length on most architectures because
37 // the extra argument lands in an unused register/stack slot. We add the
38 // hidden length parameter on wasm only to keep the native side unchanged.
39 #ifdef __EMSCRIPTEN__
40 #define CASADI_LAPACK_CHARLEN_1 , 1
41 #define CASADI_LAPACK_CHARLEN_DECL_1 , size_t len1
42 #define CASADI_LAPACK_CHARLEN_DECL_2 , size_t len1, size_t len2
43 #define CASADI_LAPACK_CHARLEN_DECL_4 , size_t len1, size_t len2, size_t len3, size_t len4
44 #define CASADI_LAPACK_CHARLEN_2 , 1, 1
45 #define CASADI_LAPACK_CHARLEN_4 , 1, 1, 1, 1
46 #else
47 #define CASADI_LAPACK_CHARLEN_1
48 #define CASADI_LAPACK_CHARLEN_DECL_1
49 #define CASADI_LAPACK_CHARLEN_DECL_2
50 #define CASADI_LAPACK_CHARLEN_DECL_4
51 #define CASADI_LAPACK_CHARLEN_2
52 #define CASADI_LAPACK_CHARLEN_4
53 #endif
54 
55 extern "C" {
57  void dgetrf_(int *m, int *n, double *a, int *lda, int *ipiv, int *info);
58 
60  void dgetrs_(char* trans, int *n, int *nrhs, double *a,
61  int *lda, int *ipiv, double *b, int *ldb, int *info
62  CASADI_LAPACK_CHARLEN_DECL_1);
63 
65  void dgeequ_(int *m, int *n, double *a, int *lda, double *r, double *c,
66  double *colcnd, double *rowcnd, double *amax, int *info);
67 
69  void dlaqge_(int *m, int *n, double *a, int *lda, double *r, double *c,
70  double *colcnd, double *rowcnd, double *amax, char *equed
71  CASADI_LAPACK_CHARLEN_DECL_1);
72 }
73 
74 namespace casadi {
75 
87  struct LapackLuMemory : public LinsolMemory {
88  // Matrix
89  std::vector<double> mat;
90 
92  std::vector<int> ipiv;
93 
95  std::vector<double> r, c;
96 
98  char equed;
99  };
100 
107  class LapackLu : public LinsolInternal {
108  public:
109  // Create a linear solver given a sparsity pattern and a number of right hand sides
110  LapackLu(const std::string& name, const Sparsity& sp);
111 
113  static LinsolInternal* creator(const std::string& name, const Sparsity& sp) {
114  return new LapackLu(name, sp);
115  }
116 
118  ~LapackLu() override;
119 
121 
122  static const Options options_;
123  const Options& get_options() const override { return options_;}
125 
127  void init(const Dict& opts) override;
128 
130  void* alloc_mem() const override { return new LapackLuMemory();}
131 
133  int init_mem(void* mem) const override;
134 
136  void free_mem(void *mem) const override { delete static_cast<LapackLuMemory*>(mem);}
137 
138  // Factorize the linear system
139  int nfact(void* mem, const double* A) const override;
140 
141  // Solve the linear system
142  int solve(void* mem, const double* A, double* x, casadi_int nrhs, bool tr) const override;
143 
145  static const std::string meta_doc;
146 
148  void serialize_body(SerializingStream &s) const override;
149 
151  static ProtoFunction* deserialize(DeserializingStream& s) { return new LapackLu(s); }
152 
153  protected:
155  explicit LapackLu(DeserializingStream& s);
156 
158  bool equilibriate_;
159 
161  bool allow_equilibration_failure_;
162 
163  // Get name of the plugin
164  const char* plugin_name() const override { return "lapacklu";}
165 
166  // Get name of the class
167  std::string class_name() const override { return "LapackLu";}
168  };
169 
171 
172 } // namespace casadi
173 
174 #endif // CASADI_LAPACK_LU_HPP
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.