26 #include "linsol_qr.hpp"
27 #include "casadi/core/global_options.hpp"
32 int CASADI_LINSOL_QR_EXPORT
37 plugin->version = CASADI_VERSION;
60 "Minimum R entry before singularity is declared [1e-12]"}},
63 "Amount of factorisations to remember (thread-local) [0]"}}
74 for (
auto&& op : opts) {
75 if (op.first==
"eps") {
77 }
else if (op.first==
"cache") {
98 m->beta.resize(
ncol());
115 double* cache =
nullptr;
116 bool cache_hit = cache_check(A,
get_ptr(m->cache),
get_ptr(m->cache_loc),
119 if (cache && cache_hit) {
134 casadi_int irmin, nullity;
138 print(
"Singularity detected: Rank %lld<%lld\n",
ncol()-nullity,
ncol());
139 print(
"First singular R entry: %g<%g, corresponding to row %lld\n", rmin,
eps_, irmin);
141 print(
"Linear combination of columns:\n[");
142 for (casadi_int k=0; k<
ncol(); ++k)
print(k==0 ?
"%g" :
", %g", m->w[k]);
157 int LinsolQr::solve(
void* mem,
const double* A,
double* x, casadi_int nrhs,
bool tr)
const {
159 casadi_qr_solve(x, nrhs, tr,
166 casadi_int nrhs,
bool tr)
const {
176 g.
comment(
"FIXME(@jaeandersson): Memory allocation can be avoided");
177 g <<
"casadi_real v[" <<
sp_v_.
nnz() <<
"], "
179 "beta[" <<
ncol() <<
"], "
183 g <<
"casadi_real *c;\n";
185 g <<
"int cache_loc[" <<
n_cache_ <<
"] = {";
186 for (casadi_int i=0;i<
n_cache_;++i) {
190 g <<
"if (" << g.
cache_check(A,
"cache",
"cache_loc",
192 casadi_int offset =
sp_.
nnz();
193 g.
comment(
"Retrieve from cache");
196 g << g.
copy(
"c+" +
str(offset),
ncol(),
"beta") <<
"\n"; offset+=
ncol();
201 g << g.
qr(sp, A,
"w", sp_v,
"v", sp_r,
"r",
"beta", prinv, pc) <<
"\n";
204 casadi_int offset = 0;
214 g << g.
qr_solve(x, nrhs, tr, sp_v,
"v", sp_r,
"r",
"beta", prinv, pc,
"w") <<
"\n";
221 int version = s.
version(
"LinsolQr", 1, 2);
Helper class for C code generation.
std::string copy(const std::string &arg, std::size_t n, const std::string &res)
Create a copy operation.
void comment(const std::string &s)
Write a comment line (ignored if not verbose)
std::string constant(const std::vector< casadi_int > &v)
Represent an array constant; adding it when new.
std::string cache_check(const std::string &key, const std::string &cache, const std::string &loc, casadi_int stride, casadi_int sz, casadi_int key_sz, const std::string &val)
cache check
std::string qr_solve(const std::string &x, casadi_int nrhs, bool tr, const std::string &sp_v, const std::string &v, const std::string &sp_r, const std::string &r, const std::string &beta, const std::string &prinv, const std::string &pc, const std::string &w)
QR solve.
std::string sparsity(const Sparsity &sp, bool canonical=true)
std::string qr(const std::string &sp, const std::string &A, const std::string &w, const std::string &sp_v, const std::string &v, const std::string &sp_r, const std::string &r, const std::string &beta, const std::string &prinv, const std::string &pc)
QR factorization.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
void init(const Dict &opts) override
Initialize.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
casadi_int nrow() const
Get sparsity pattern.
int init_mem(void *mem) const override
Initalize memory block.
static const std::string meta_doc
A documentation string.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
int sfact(void *mem, const double *A) const override
int solve(void *mem, const double *A, double *x, casadi_int nrhs, bool tr) const override
int nfact(void *mem, const double *A) const override
Numeric factorization.
std::vector< casadi_int > pc_
std::vector< casadi_int > prinv_
Symbolic factorization.
static LinsolInternal * creator(const std::string &name, const Sparsity &sp)
Create a new LinsolInternal.
void generate(CodeGenerator &g, const std::string &A, const std::string &x, casadi_int nrhs, bool tr) const override
Generate C code.
int init_mem(void *mem) const override
Initalize memory block.
casadi_int n_cache_
Cache size.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
static const Options options_
Options.
void finalize() override
Finalize the object creation.
LinsolQr(const std::string &name, const Sparsity &sp)
void init(const Dict &opts) override
Initialize.
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
void print(const char *fmt,...) const
C-style formatted printing during evaluation.
bool verbose_
Verbose printout.
virtual void finalize()
Finalize the object creation.
static const Options options_
Options.
void clear_mem()
Clear all memory (called from destructor)
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
casadi_int nnz() const
Get the number of (structural) non-zeros.
void qr_sparse(Sparsity &V, Sparsity &R, std::vector< casadi_int > &prinv, std::vector< casadi_int > &pc, bool amd=true) const
Symbolic QR factorization.
void casadi_copy(const T1 *x, casadi_int n, T1 *y)
COPY: y <-x.
std::string str(const T &v)
String representation, any type.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
void CASADI_LINSOL_QR_EXPORT casadi_load_linsol_qr()
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
int CASADI_LINSOL_QR_EXPORT casadi_register_linsol_qr(LinsolInternal::Plugin *plugin)
Options metadata for a class.