lsqr.cpp
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 #include "lsqr.hpp"
27 
28 #ifdef WITH_DL
29 #include <cstdlib>
30 #endif // WITH_DL
31 
32 namespace casadi {
33 
34  extern "C"
35  int CASADI_LINSOL_LSQR_EXPORT
36  casadi_register_linsol_lsqr(LinsolInternal::Plugin* plugin) {
37  plugin->creator = Lsqr::creator;
38  plugin->name = "lsqr";
39  plugin->doc = Lsqr::meta_doc.c_str();
40  plugin->version = CASADI_VERSION;
41  plugin->options = &Lsqr::options_;
42  plugin->deserialize = &Lsqr::deserialize;
43  return 0;
44  }
45 
46  extern "C"
47  void CASADI_LINSOL_LSQR_EXPORT casadi_load_linsol_lsqr() {
49  }
50 
51  Lsqr::Lsqr(const std::string& name, const Sparsity& sp) :
52  LinsolInternal(name, sp) {
53  }
54 
56  clear_mem();
57  }
58 
59  int Lsqr::init_mem(void* mem) const {
60  if (LinsolInternal::init_mem(mem)) return 1;
61  auto m = static_cast<LsqrMemory*>(mem);
62 
63  // Temporary storage
64  m->w.resize(nrow()+4*ncol());
65  m->A.resize(sp_.nnz());
66  return 0;
67  }
68 
69  int Lsqr::nfact(void* mem, const double* A) const {
70  auto m = static_cast<LsqrMemory*>(mem);
71 
72  std::copy(A, A+m->A.size(), get_ptr(m->A));
73  return 0;
74  }
75 
76  void Lsqr::generate(CodeGenerator& g, const std::string& A, const std::string& x,
77  casadi_int nrhs, bool tr) const {
78  // Codegen the integer vectors
79  std::string sp = g.sparsity(sp_);
80 
81  // Place in block to avoid conflicts caused by local variables
82  g << "{\n";
83  g.comment("FIXME(@jaeandersson): Memory allocation can be avoided");
84  g << "casadi_real w[" << nrow() + 4*ncol() << "];\n";
85 
86  // Solve
87  g << g.lsqr_solve(A, x, nrhs, tr, sp, "w") << "\n";
88 
89  // End of block
90  g << "}\n";
91 
92  }
93 
94  int Lsqr::solve(void* mem, const double* A, double* x, casadi_int nrhs, bool tr) const {
95  auto m = static_cast<LsqrMemory*>(mem);
96  return casadi_lsqr_solve(A, x, nrhs, tr, sp_, get_ptr(m->w));
97  }
98 
99 } // namespace casadi
Helper class for C code generation.
std::string lsqr_solve(const std::string &A, const std::string &x, casadi_int nrhs, bool tr, const std::string &sp, const std::string &w)
void comment(const std::string &s)
Write a comment line (ignored if not verbose)
std::string sparsity(const Sparsity &sp, bool canonical=true)
casadi_int nrow() const
Get sparsity pattern.
casadi_int ncol() const
int init_mem(void *mem) const override
Initalize memory block.
void generate(CodeGenerator &g, const std::string &A, const std::string &x, casadi_int nrhs, bool tr) const override
Generate C code.
Definition: lsqr.cpp:76
static const std::string meta_doc
A documentation string.
Definition: lsqr.hpp:101
int init_mem(void *mem) const override
Initalize memory block.
Definition: lsqr.cpp:59
static LinsolInternal * creator(const std::string &name, const Sparsity &sp)
Create a new Linsol.
Definition: lsqr.hpp:77
Lsqr(const std::string &name, const Sparsity &sp)
Definition: lsqr.cpp:51
int nfact(void *mem, const double *A) const override
Numeric factorization.
Definition: lsqr.cpp:69
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
Definition: lsqr.hpp:104
int solve(void *mem, const double *A, double *x, casadi_int nrhs, bool tr) const override
Definition: lsqr.cpp:94
~Lsqr() override
Definition: lsqr.cpp:55
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
static const Options options_
Options.
void clear_mem()
Clear all memory (called from destructor)
General sparsity class.
Definition: sparsity.hpp:106
casadi_int nnz() const
Get the number of (structural) non-zeros.
Definition: sparsity.cpp:148
The casadi namespace.
Definition: archiver.cpp:28
void CASADI_LINSOL_LSQR_EXPORT casadi_load_linsol_lsqr()
Definition: lsqr.cpp:47
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
int CASADI_LINSOL_LSQR_EXPORT casadi_register_linsol_lsqr(LinsolInternal::Plugin *plugin)
Definition: lsqr.cpp:36
Memory for SymbolicQR
Definition: lsqr.hpp:47
std::vector< double > w
Definition: lsqr.hpp:50