external.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 "external_impl.hpp"
27 #include "casadi_misc.hpp"
28 #include "serializing_stream.hpp"
29 #include "casadi_meta.hpp"
30 
31 #include <fstream>
32 #include <iostream>
33 #include <sstream>
34 
35 // Set default shared library suffix
36 #ifndef SHARED_LIBRARY_SUFFIX
37 #define SHARED_LIBRARY_SUFFIX CasadiMeta::shared_library_suffix()
38 #endif // SHARED_LIBRARY_SUFFIX
39 
40 namespace casadi {
41 
42 Function external(const std::string& name, const Importer& li,
43  const Dict& opts) {
44  std::vector<std::string> config_args;
45  Dict opts_filtered = extract_from_dict(opts, "config_args", config_args);
46  config_args.insert(config_args.begin(), li.library());
47  return Function::create(new GenericExternal(name, li, config_args), opts_filtered);
48 }
49 
50 Function external(const std::string& name, const Dict& opts) {
51  return external(name, "./" + name + SHARED_LIBRARY_SUFFIX, opts);
52 }
53 
54 Function external(const std::string& name, const std::string& bin_name,
55  const Dict& opts) {
56  return external(name, Importer(bin_name, "dll"), opts);
57 }
58 
59 External::External(const std::string& name, const Importer& li,
60  const std::vector<std::string> config_args)
61  : FunctionInternal(name), config_args_(config_args) {
62  // compiler_ isntance not in use (has_codegen()==false)
63  casadi_assert_dev(!has_codegen());
64  compiler_ = li;
66 }
67 
69  return config_ || incref_ || decref_ || get_default_in_ ||
72 }
73 
75  // Increasing/decreasing reference counter
77 
80 
81  casadi_assert(static_cast<bool>(incref_) == static_cast<bool>(decref_),
82  "External must either define both incref and decref or neither.");
83 
84  // Getting default arguments
86 
87  // Getting number of inputs and outputs
90 
91  // Getting names of inputs and outputs
94 
95  // Work vector sizes
96  work_ = (work_t)compiler_.get_function(name_ + "_work");
97 
98  if (config_) {
99  args_.resize(config_args_.size());
100  for (int i=0;i<config_args_.size();++i) {
101  args_[i] = config_args_.at(i).c_str();
102  }
103  int flag = config_(args_.size(), get_ptr(args_));
104  casadi_assert(flag==0, "config failed.");
105  }
106 
107  // Increase reference counter - external function memory initialized at this point
108  if (incref_) incref_();
109 }
110 
111 GenericExternal::GenericExternal(const std::string& name, const Importer& li,
112  const std::vector<std::string> arguments)
113  : External(name, li, arguments) {
114 
116 }
117 
119  return External::any_symbol_found() ||
120  get_sparsity_in_ || get_sparsity_out_ ||
121  get_diff_in_ || get_diff_out_ ||
122  checkout_ || release_ ||
123  eval_;
124 }
125 
127 
128  // Functions for retrieving sparsities of inputs and outputs
129  get_sparsity_in_ = (sparsity_t)compiler_.get_function(name_ + "_sparsity_in");
130  get_sparsity_out_ = (sparsity_t)compiler_.get_function(name_ + "_sparsity_out");
131 
132  // Differentiability of inputs and outputs
133  get_diff_in_ = (diff_t)compiler_.get_function(name_ + "_diff_in");
134  get_diff_out_ = (diff_t)compiler_.get_function(name_ + "_diff_out");
135 
136  // Memory management functions
139 
140  casadi_assert(static_cast<bool>(checkout_) == static_cast<bool>(release_),
141  "External must either define both checkout and release or neither.");
142 
143  // Function for numerical evaluation
145 
146  // Sparsity patterns of Jacobians
147  get_jac_sparsity_ = (sparsity_t)compiler_.get_function("jac_" + name_ + "_sparsity_out");
148 }
149 
151  clear_mem();
152 }
153 
155  if (get_n_in_) {
156  return get_n_in_();
157  } else if (compiler_.has_meta(name_ + "_N_IN")) {
158  return compiler_.meta_int(name_ + "_N_IN");
159  } else {
160  // Fall back to base class
162  }
163 }
164 
166  if (get_n_out_) {
167  return get_n_out_();
168  } else if (compiler_.has_meta(name_ + "_N_OUT")) {
169  return compiler_.meta_int(name_ + "_N_OUT");
170  } else {
171  // Fall back to base class
173  }
174 }
175 
176 double External::get_default_in(casadi_int i) const {
177  if (get_default_in_) {
178  return get_default_in_(i);
179  } else {
180  // Fall back to base class
182  }
183 }
184 
185 std::string External::get_name_in(casadi_int i) {
186  if (get_name_in_) {
187  // Use function pointer
188  const char* n = get_name_in_(i);
189  casadi_assert(n!=nullptr, "Error querying input name");
190  return n;
191  } else if (compiler_.has_meta(name_ + "_NAME_IN", i)) {
192  // Read meta
193  return compiler_.meta_string(name_ + "_NAME_IN", i);
194  } else {
195  // Default name
197  }
198 }
199 
200 std::string External::get_name_out(casadi_int i) {
201  if (get_name_out_) {
202  // Use function pointer
203  const char* n = get_name_out_(i);
204  casadi_assert(n!=nullptr, "Error querying output name");
205  return n;
206  } else if (compiler_.has_meta(name_ + "_NAME_OUT", i)) {
207  // Read meta
208  return compiler_.meta_string(name_ + "_NAME_OUT", i);
209  } else {
210  // Default name
212  }
213 }
214 
216  // Use sparsity retrieval function, if present
217  if (get_sparsity_in_) {
218  return Sparsity::compressed(get_sparsity_in_(i));
219  } else if (compiler_.has_meta(name_ + "_SPARSITY_IN", i)) {
220  return Sparsity::compressed(compiler_.meta_vector<casadi_int>(name_ + "_SPARSITY_IN", i));
221  } else {
222  // Fall back to base class
224  }
225 }
226 
228  // Use sparsity retrieval function, if present
229  if (get_sparsity_out_) {
230  return Sparsity::compressed(get_sparsity_out_(i));
231  } else if (compiler_.has_meta(name_ + "_SPARSITY_OUT", i)) {
232  return Sparsity::compressed(compiler_.meta_vector<casadi_int>(name_ + "_SPARSITY_OUT", i));
233  } else {
234  // Fall back to base class
236  }
237 }
238 
239 bool GenericExternal::has_jac_sparsity(casadi_int oind, casadi_int iind) const {
240  // Flat index
241  casadi_int ind = iind + oind * n_in_;
242  // Jacobian sparsity pattern known?
243  if (get_jac_sparsity_ || compiler_.has_meta("JAC_" + name_ + "_SPARSITY_OUT", ind)) {
244  return true;
245  } else {
246  // Fall back to base class
247  return FunctionInternal::has_jac_sparsity(oind, iind);
248  }
249 }
250 
251 Sparsity GenericExternal::get_jac_sparsity(casadi_int oind, casadi_int iind,
252  bool symmetric) const {
253  // Flat index
254  casadi_int ind = iind + oind * n_in_;
255  // Use sparsity retrieval function, if present
256  if (get_jac_sparsity_) {
257  return Sparsity::compressed(get_jac_sparsity_(ind));
258  } else if (compiler_.has_meta("JAC_" + name_ + "_SPARSITY_OUT", ind)) {
259  return Sparsity::compressed(
260  compiler_.meta_vector<casadi_int>("jac_" + name_ + "_SPARSITY_OUT", ind));
261  } else {
262  // Fall back to base class
263  return FunctionInternal::get_jac_sparsity(oind, iind, symmetric);
264  }
265 }
266 
267 bool GenericExternal::get_diff_in(casadi_int i) {
268  if (get_diff_in_) {
269  // Query function exists
270  return get_diff_in_(i);
271  } else {
272  // Fall back to base class
274  }
275 }
276 
277 bool GenericExternal::get_diff_out(casadi_int i) {
278  if (get_diff_out_) {
279  // Query function exists
280  return get_diff_out_(i);
281  } else {
282  // Fall back to base class
284  }
285 }
286 
287 void External::init(const Dict& opts) {
288  // Call the initialization method of the base class
290 
291  casadi_assert(any_symbol_found(),
292  "Could not find any function/symbol starting with '" + name_ + "_'. "
293  "Make sure to read documentation of `external()` for proper usage.");
294 
295  // Reference counting?
296  has_refcount_ = compiler_.has_function(name_ + "_incref");
297  casadi_assert(has_refcount_==compiler_.has_function(name_ + "_decref"),
298  "External functions must provide functions for both increasing "
299  "and decreasing the reference count, or neither.");
300 
301  if (compiler_.has_function(name_ + "_config")) {
302  casadi_assert(has_refcount_,
303  "External functions that feature a config functions must also implement incref.");
304  }
305 
306  // Allocate work vectors
307  casadi_int sz_arg=0, sz_res=0, sz_iw=0, sz_w=0;
308  if (work_) {
309  casadi_int flag = work_(&sz_arg, &sz_res, &sz_iw, &sz_w);
310  casadi_assert(flag==0, "External: \"work\" failed");
311  } else if (compiler_.has_meta(name_ + "_WORK")) {
312  std::vector<casadi_int> v = compiler_.meta_vector<casadi_int>(name_ + "_WORK");
313  casadi_assert_dev(v.size()==4);
314  sz_arg = v[0];
315  sz_res = v[1];
316  sz_iw = v[2];
317  sz_w = v[3];
318  }
319 
320  // Work vectors
321  alloc_arg(sz_arg);
322  alloc_res(sz_res);
323  alloc_iw(sz_iw);
324  alloc_w(sz_w);
325 }
326 
327 void GenericExternal::init(const Dict& opts) {
328  // Call recursively
329  External::init(opts);
330 }
331 
333  if (!compiler_.inlined(name_)) {
334  g.add_external(signature(name_) + ";");
335  if (checkout_) g.add_external("int " + name_ + "_checkout(void);");
336  if (release_) g.add_external("void " + name_ + "_release(int mem);");
337  if (incref_) g.add_external("void " + name_ + "_incref(void);");
338  if (decref_) g.add_external("void " + name_ + "_decref(void);");
339  if (config_) g.add_external("int " + name_ + "_config(int argc, const char**);");
340  }
341 }
342 
344  if (compiler_.inlined(name_)) {
345  // Function body is inlined
346  g << compiler_.body(name_) << "\n";
347  } else {
348  g << "if (" << name_ << "(arg, res, iw, w, mem)) return 1;\n";
349  }
350 }
351 
353  if (checkout_) {
354  g << "return " << name_ << "_checkout();\n";
355  } else {
356  g << "return 0;\n";
357  }
358 }
359 
361  if (release_) {
362  g << name_ << "_release(mem);\n";
363  }
364 }
365 
367  if (incref_) {
368  if (config_) {
369  g << name_ << "_config(" << config_args_.size() << ", "
370  << g.constant(config_args_) << ");\n";
371  }
372  g << name_ << "_incref();\n";
373  }
374 }
375 
377  if (decref_) {
378  g << name_ << "_decref();\n";
379  }
380 }
381 
383  g << "return 0;\n";
384 }
385 
387  g << "return 0;\n";
388 }
389 
391 }
392 
394  if (FunctionInternal::has_jacobian()) return true;
395  return compiler_.has_function("jac_" + name_);
396 }
397 
399 ::get_jacobian(const std::string& name,
400  const std::vector<std::string>& inames,
401  const std::vector<std::string>& onames,
402  const Dict& opts) const {
403  if (has_jacobian()) {
404  return external(name, compiler_, opts);
405  } else {
406  return FunctionInternal::get_jacobian(name, inames, onames, opts);
407  }
408 }
409 
411 ::get_forward(casadi_int nfwd, const std::string& name,
412  const std::vector<std::string>& inames,
413  const std::vector<std::string>& onames,
414  const Dict& opts) const {
415  // Consistency check
416  casadi_int n=1;
417  while (n<nfwd) n*=2;
418  if (n!=nfwd || !has_forward(nfwd)) {
419  // Inefficient code to be replaced later
420  Function fwd1 = forward(1);
421  return fwd1.map(name, "serial", nfwd, range(n_in_+n_out_), std::vector<casadi_int>(), opts);
422  //casadi_error("Internal error: Refactoring needed, cf. #1055");
423  }
424  return external(name, compiler_, opts);
425 }
426 
427 bool External::has_forward(casadi_int nfwd) const {
428  return compiler_.has_function("fwd" + str(nfwd) + "_" + name_);
429 }
430 
432 ::get_reverse(casadi_int nadj, const std::string& name,
433  const std::vector<std::string>& inames,
434  const std::vector<std::string>& onames,
435  const Dict& opts) const {
436  // Consistency check
437  casadi_int n=1;
438  while (n<nadj) n*=2;
439  if (n!=nadj || !has_reverse(nadj)) {
440  // Inefficient code to be replaced later
441  Function adj1 = reverse(1);
442  return adj1.map(name, "serial", nadj, range(n_in_+n_out_), std::vector<casadi_int>(), opts);
443  //casadi_error("Internal error: Refactoring needed, cf. #1055");
444  }
445  return external(name, compiler_, opts);
446 }
447 
448 bool External::has_reverse(casadi_int nadj) const {
449  return compiler_.has_function("adj" + str(nadj) + "_" + name_);
450 }
451 
452 Function External::factory(const std::string& name,
453  const std::vector<std::string>& s_in,
454  const std::vector<std::string>& s_out,
455  const Function::AuxOut& aux,
456  const Dict& opts) const {
457  // If not available, call base class function
458  if (!compiler_.has_function(name)) {
459  return FunctionInternal::factory(name, s_in, s_out, aux, opts);
460  }
461 
462  // Retrieve function
463  Function ret = external(name, compiler_, opts);
464 
465  // Replace colons in input names
466  std::vector<std::string> s_io(s_in);
467  for (std::string& s : s_io) replace(s.begin(), s.end(), ':', '_');
468 
469  // Inputs consistency checks
470  casadi_assert(s_in.size() == ret.n_in(),
471  "Inconsistent number of inputs. Expected " + str(s_in.size())+ " "
472  "(" + str(s_io) + "), got " + str(ret.n_in()) + ".");
473  for (size_t i = 0; i < s_in.size(); ++i) {
474  casadi_assert(s_io[i] == ret.name_in(i),
475  "Inconsistent input name. Expected: " + str(s_io) + ", "
476  "got " + ret.name_in(i) + " for input " + str(i));
477  }
478 
479  // Replace colons in output names
480  s_io = s_out;
481  for (std::string& s : s_io) replace(s.begin(), s.end(), ':', '_');
482 
483  // Outputs consistency checks
484  casadi_assert(s_out.size() == ret.n_out(),
485  "Inconsistent number of outputs. Expected " + str(s_out.size()) + " "
486  "(" + str(s_io) + "), got " + str(ret.n_out()) + ".");
487  for (size_t i = 0; i < s_out.size(); ++i) {
488  casadi_assert(s_io[i] == ret.name_out(i),
489  "Inconsistent output name. Expected: " + str(s_io) + ", "
490  "got " + ret.name_out(i) + " for output " + str(i));
491  }
492 
493  return ret;
494 }
495 
498 
499  s.version("External", 2);
500  s.pack("External::int_data", int_data_);
501  s.pack("External::real_data", real_data_);
502  s.pack("External::string_data", string_data_);
503  s.pack("External::li", compiler_);
504  std::vector<std::string> config_args = vector_tail(config_args_);
505  s.pack("External::config_args", config_args);
506 }
507 
509  s.version("GenericExternal", 1);
510  char type;
511  s.unpack("GenericExternal::type", type);
512  switch (type) {
513  case 'g': return new GenericExternal(s);
514  default:
515  casadi_error("External::deserialize error");
516  }
517 }
518 
521  s.version("GenericExternal", 1);
522  s.pack("GenericExternal::type", 'g');
523 }
524 
526  int version = s.version("External", 1, 2);
527  s.unpack("External::int_data", int_data_);
528  s.unpack("External::real_data", real_data_);
529  s.unpack("External::string_data", string_data_);
530  s.unpack("External::li", compiler_);
531  if (version>=2) {
532  s.unpack("External::config_args", config_args_);
533  config_args_.insert(config_args_.begin(), compiler_.library());
534  }
535 
537 }
538 
541 }
542 
543 } // namespace casadi
Helper class for C code generation.
std::string constant(const std::vector< casadi_int > &v)
Represent an array constant; adding it when new.
void add_external(const std::string &new_external)
Add an external function declaration.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
void codegen_body(CodeGenerator &g) const override
Generate code for the body of the C function.
Definition: external.cpp:343
size_t get_n_out() override
Number of function inputs and outputs.
Definition: external.cpp:165
std::vector< double > real_data_
Data vectors.
void codegen_init_mem(CodeGenerator &g) const override
Codegen decref for init_mem.
Definition: external.cpp:382
External(const std::string &name, const Importer &li, const std::vector< std::string > config_args=std::vector< std::string >())
Constructor.
Definition: external.cpp:59
std::vector< const char * > args_
virtual void init_external()
Initialize members that are unique.
Definition: external.cpp:74
bool has_reverse(casadi_int nadj) const override
Reverse mode derivatives.
Definition: external.cpp:448
default_t get_default_in_
Get default inputs.
std::string string_data_
Data vectors.
std::vector< std::string > config_args_
Arguments as passed to init.
size_t get_n_in() override
Number of function inputs and outputs.
Definition: external.cpp:154
void codegen_incref(CodeGenerator &g) const override
Codegen incref for dependencies.
Definition: external.cpp:366
void codegen_declarations(CodeGenerator &g) const override
Generate code for the declarations of the C function.
Definition: external.cpp:332
Function factory(const std::string &name, const std::vector< std::string > &s_in, const std::vector< std::string > &s_out, const Function::AuxOut &aux, const Dict &opts) const override
Definition: external.cpp:452
bool has_jacobian() const override
Full Jacobian.
Definition: external.cpp:393
bool has_forward(casadi_int nfwd) const override
Forward mode derivatives.
Definition: external.cpp:427
void codegen_alloc_mem(CodeGenerator &g) const override
Codegen decref for alloc_mem.
Definition: external.cpp:386
void codegen_release(CodeGenerator &g) const override
Codegen for release.
Definition: external.cpp:360
Function get_reverse(casadi_int nadj, const std::string &name, const std::vector< std::string > &inames, const std::vector< std::string > &onames, const Dict &opts) const override
Reverse mode derivatives.
Definition: external.cpp:432
void codegen_free_mem(CodeGenerator &g) const override
Codegen for free_mem.
Definition: external.cpp:390
std::vector< casadi_int > int_data_
Data vectors.
work_t work_
Work vector sizes.
getint_t get_n_in_
Number of inputs and outputs.
std::string get_name_in(casadi_int i) override
Names of function input and outputs.
Definition: external.cpp:185
Function get_forward(casadi_int nfwd, const std::string &name, const std::vector< std::string > &inames, const std::vector< std::string > &onames, const Dict &opts) const override
Forward mode derivatives.
Definition: external.cpp:411
Function get_jacobian(const std::string &name, const std::vector< std::string > &inames, const std::vector< std::string > &onames, const Dict &opts) const override
Full Jacobian.
Definition: external.cpp:399
~External() override=0
Destructor.
Definition: external.cpp:150
virtual bool any_symbol_found() const
Any symbol found?
Definition: external.cpp:68
void codegen_decref(CodeGenerator &g) const override
Codegen decref for dependencies.
Definition: external.cpp:376
double get_default_in(casadi_int i) const override
Default inputs.
Definition: external.cpp:176
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize into MX.
Definition: external.cpp:508
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
Definition: external.cpp:496
config_t config_
Initialize.
name_t get_name_in_
Names of inputs and outputs.
void init(const Dict &opts) override
Initialize.
Definition: external.cpp:287
void codegen_checkout(CodeGenerator &g) const override
Codegen for checkout.
Definition: external.cpp:352
std::string get_name_out(casadi_int i) override
Names of function input and outputs.
Definition: external.cpp:200
Internal class for Function.
bool has_refcount_
Reference counting in codegen?
void alloc_iw(size_t sz_iw, bool persistent=false)
Ensure required length of iw field.
void init(const Dict &opts) override
Initialize.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
void alloc_res(size_t sz_res, bool persistent=false)
Ensure required length of res field.
virtual Function factory(const std::string &name, const std::vector< std::string > &s_in, const std::vector< std::string > &s_out, const Function::AuxOut &aux, const Dict &opts) const
casadi_release_t release_
Release redirected to a C function.
virtual bool has_jac_sparsity(casadi_int oind, casadi_int iind) const
Get Jacobian sparsity.
void alloc_arg(size_t sz_arg, bool persistent=false)
Ensure required length of arg field.
virtual bool has_jacobian() const
Return Jacobian of all input elements with respect to all output elements.
eval_t eval_
Numerical evaluation redirected to a C function.
size_t n_in_
Number of inputs and outputs.
size_t sz_res() const
Get required length of res field.
virtual size_t get_n_out()
Are all inputs and outputs scalar.
casadi_checkout_t checkout_
Checkout redirected to a C function.
virtual bool get_diff_in(casadi_int i)
Which inputs are differentiable.
signal_t incref_
Incref/decref redirected to C functions.
void serialize_type(SerializingStream &s) const override
Serialize type information.
virtual std::string get_name_out(casadi_int i)
Names of function input and outputs.
virtual Sparsity get_sparsity_out(casadi_int i)
Get sparsity of a given output.
size_t sz_w() const
Get required length of w field.
virtual Sparsity get_jac_sparsity(casadi_int oind, casadi_int iind, bool symmetric) const
Get Jacobian sparsity.
void alloc_w(size_t sz_w, bool persistent=false)
Ensure required length of w field.
std::string signature(const std::string &fname) const
Code generate the function.
virtual Function get_jacobian(const std::string &name, const std::vector< std::string > &inames, const std::vector< std::string > &onames, const Dict &opts) const
Return Jacobian of all input elements with respect to all output elements.
size_t sz_arg() const
Get required length of arg field.
virtual bool has_codegen() const
Is codegen supported?
size_t sz_iw() const
Get required length of iw field.
virtual bool get_diff_out(casadi_int i)
Which outputs are differentiable.
virtual std::string get_name_in(casadi_int i)
Names of function input and outputs.
virtual size_t get_n_in()
Number of function inputs and outputs.
virtual double get_default_in(casadi_int ind) const
Get default input value.
virtual Sparsity get_sparsity_in(casadi_int i)
Get sparsity of a given input.
Function object.
Definition: function.hpp:60
const std::vector< std::string > & name_in() const
Get input scheme.
Definition: function.cpp:1113
static Function create(FunctionInternal *node)
Create from node.
Definition: function.cpp:488
casadi_int n_out() const
Get the number of function outputs.
Definition: function.cpp:975
casadi_int n_in() const
Get the number of function inputs.
Definition: function.cpp:971
Function map(casadi_int n, const std::string &parallelization="serial") const
Create a mapped version of this function.
Definition: function.cpp:861
std::map< std::string, std::vector< std::string > > AuxOut
Definition: function.hpp:447
const std::vector< std::string > & name_out() const
Get output scheme.
Definition: function.cpp:1117
Sparsity get_sparsity_in(casadi_int i) override
Retreive sparsities.
Definition: external.cpp:215
Sparsity get_sparsity_out(casadi_int i) override
Retreive sparsities.
Definition: external.cpp:227
bool get_diff_in(casadi_int i) override
Retreive differentiability.
Definition: external.cpp:267
bool any_symbol_found() const override
Any symbol found?
Definition: external.cpp:118
bool get_diff_out(casadi_int i) override
Retreive differentiability.
Definition: external.cpp:277
void init_external() override
Initialize members that are unique.
Definition: external.cpp:126
GenericExternal(const std::string &name, const Importer &li, const std::vector< std::string > config_args=std::vector< std::string >())
Constructor.
Definition: external.cpp:111
void init(const Dict &opts) override
Initialize.
Definition: external.cpp:327
bool has_jac_sparsity(casadi_int oind, casadi_int iind) const override
Return sparsity of Jacobian of an output respect to an input.
Definition: external.cpp:239
Sparsity get_jac_sparsity(casadi_int oind, casadi_int iind, bool symmetric) const override
Return sparsity of Jacobian of an output respect to an input.
Definition: external.cpp:251
void serialize_type(SerializingStream &s) const override
Serialize type information.
Definition: external.cpp:519
Importer.
Definition: importer.hpp:86
std::string library() const
Get library name.
Definition: importer.cpp:104
std::string meta_string(const std::string &cmd, casadi_int ind=-1) const
Get entry as a string.
Definition: importer.hpp:180
std::string body(const std::string &symname) const
Get the function body, if inlined.
Definition: importer.cpp:100
casadi_int meta_int(const std::string &cmd, casadi_int ind=-1) const
Get entry as an integer.
Definition: importer.hpp:203
std::vector< T > meta_vector(const std::string &cmd, casadi_int ind=-1) const
Get entry as a vector.
Definition: importer.hpp:188
bool has_meta(const std::string &cmd, casadi_int ind=-1) const
Does a meta entry exist?
Definition: importer.cpp:88
bool has_function(const std::string &symname) const
Definition: importer.cpp:80
bool inlined(const std::string &symname) const
Check if a function is inlined.
Definition: importer.cpp:96
signal_t get_function(const std::string &symname)
Get a function pointer for numerical evaluation.
Definition: importer.cpp:84
Base class for FunctionInternal and LinsolInternal.
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.
General sparsity class.
Definition: sparsity.hpp:106
static Sparsity compressed(const std::vector< casadi_int > &v, bool order_rows=false)
Definition: sparsity.cpp:1341
The casadi namespace.
Definition: archiver.cpp:28
std::vector< casadi_int > range(casadi_int start, casadi_int stop, casadi_int step, casadi_int len)
Range function.
const casadi_int *(* sparsity_t)(casadi_int i)
Function pointer types for the C API.
std::vector< T > vector_tail(const std::vector< T > &v)
Return all but the first element of a vector.
int(* casadi_checkout_t)(void)
Function pointer types for the C API.
int(* diff_t)(casadi_int i)
Function pointer types for the C API.
const char *(* name_t)(casadi_int i)
Function pointer types for the C API.
CASADI_EXPORT std::string replace(const std::string &s, const std::string &p, const std::string &r)
Replace all occurences of p with r in s.
int(* eval_t)(const double **arg, double **res, casadi_int *iw, double *w, int)
Function pointer types for the C API.
casadi_int(* getint_t)(void)
Function pointer types for the C API.
int(* config_t)(int, const char **)
Function pointer types for the C API.
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_release_t)(int)
Function pointer types for the C API.
void(* signal_t)(void)
Function pointer types for the C API.
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
double(* default_t)(casadi_int i)
Function pointer types for the C API.
std::vector< T > reverse(const std::vector< T > &v)
Reverse a list.
Dict extract_from_dict(const Dict &d, const std::string &key, T &value)
int(* work_t)(casadi_int *sz_arg, casadi_int *sz_res, casadi_int *sz_iw, casadi_int *sz_w)
Function pointer types for the C API.
Function external(const std::string &name, const Importer &li, const Dict &opts)
Load a just-in-time compiled external function.
Definition: external.cpp:42