26 #include "external_impl.hpp"
27 #include "casadi_misc.hpp"
28 #include "serializing_stream.hpp"
29 #include <casadi/config.h>
36 #ifndef SHARED_LIBRARY_SUFFIX
37 #define SHARED_LIBRARY_SUFFIX CASADI_SHARED_LIBRARY_SUFFIX
44 std::vector<std::string> config_args;
46 config_args.insert(config_args.begin(), li.
library());
51 return external(name,
"./" + name + SHARED_LIBRARY_SUFFIX, opts);
60 const std::vector<std::string> config_args)
79 casadi_assert(
static_cast<bool>(
incref_) ==
static_cast<bool>(
decref_),
80 "External must either define both incref and decref or neither.");
102 casadi_assert(flag==0,
"config failed.");
110 const std::vector<std::string> arguments)
118 get_sparsity_in_ || get_sparsity_out_ ||
119 get_diff_in_ || get_diff_out_ ||
139 "External must either define both checkout and release or neither.");
188 casadi_assert(n!=
nullptr,
"Error querying input name");
203 casadi_assert(n!=
nullptr,
"Error querying output name");
216 if (get_sparsity_in_) {
228 if (get_sparsity_out_) {
240 casadi_int ind = iind + oind *
n_in_;
242 if (get_jac_sparsity_ ||
li_.
has_meta(
"JAC_" +
name_ +
"_SPARSITY_OUT", ind)) {
251 bool symmetric)
const {
253 casadi_int ind = iind + oind *
n_in_;
255 if (get_jac_sparsity_) {
269 return get_diff_in_(i);
279 return get_diff_out_(i);
291 "Could not find any function/symbol starting with '" +
name_ +
"_'. "
292 "Make sure to read documentation of `external()` for proper usage.");
297 "External functions must provide functions for both increasing "
298 "and decreasing the reference count, or neither.");
302 "External functions that feature a config functions must also implement incref.");
309 casadi_assert(flag==0,
"External: \"work\" failed");
312 casadi_assert_dev(v.size()==4);
347 g <<
"if (" <<
name_ <<
"(arg, res, iw, w, mem)) return 1;\n";
358 g <<
"return " <<
name_ <<
"_checkout();\n";
366 g <<
name_ <<
"_release(mem);\n";
376 g <<
name_ <<
"_incref();\n";
382 g <<
name_ <<
"_decref();\n";
404 const std::vector<std::string>& inames,
405 const std::vector<std::string>& onames,
406 const Dict& opts)
const {
407 if (has_jacobian()) {
416 const std::vector<std::string>& inames,
417 const std::vector<std::string>& onames,
418 const Dict& opts)
const {
422 if (n!=nfwd || !has_forward(nfwd)) {
425 return fwd1.
map(name,
"serial", nfwd,
range(n_in_+n_out_), std::vector<casadi_int>(), opts);
437 const std::vector<std::string>& inames,
438 const std::vector<std::string>& onames,
439 const Dict& opts)
const {
443 if (n!=nadj || !has_reverse(nadj)) {
446 return adj1.
map(name,
"serial", nadj,
range(n_in_+n_out_), std::vector<casadi_int>(), opts);
457 const std::vector<std::string>& s_in,
458 const std::vector<std::string>& s_out,
460 const Dict& opts)
const {
470 std::vector<std::string> s_io(s_in);
471 for (std::string& s : s_io)
replace(s.begin(), s.end(),
':',
'_');
474 casadi_assert(s_in.size() == ret.
n_in(),
475 "Inconsistent number of inputs. Expected " +
str(s_in.size())+
" "
476 "(" +
str(s_io) +
"), got " +
str(ret.
n_in()) +
".");
477 for (
size_t i = 0; i < s_in.size(); ++i) {
478 casadi_assert(s_io[i] == ret.
name_in(i),
479 "Inconsistent input name. Expected: " +
str(s_io) +
", "
480 "got " + ret.
name_in(i) +
" for input " +
str(i));
485 for (std::string& s : s_io)
replace(s.begin(), s.end(),
':',
'_');
488 casadi_assert(s_out.size() == ret.
n_out(),
489 "Inconsistent number of outputs. Expected " +
str(s_out.size()) +
" "
490 "(" +
str(s_io) +
"), got " +
str(ret.
n_out()) +
".");
491 for (
size_t i = 0; i < s_out.size(); ++i) {
492 casadi_assert(s_io[i] == ret.
name_out(i),
493 "Inconsistent output name. Expected: " +
str(s_io) +
", "
494 "got " + ret.
name_out(i) +
" for output " +
str(i));
509 s.
pack(
"External::config_args", config_args);
513 s.
version(
"GenericExternal", 1);
515 s.
unpack(
"GenericExternal::type", type);
519 casadi_error(
"External::deserialize error");
525 s.
version(
"GenericExternal", 1);
526 s.
pack(
"GenericExternal::type",
'g');
530 int version = s.
version(
"External", 1, 2);
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.
size_t get_n_out() override
Number of function inputs and outputs.
std::vector< double > real_data_
Data vectors.
void codegen_init_mem(CodeGenerator &g) const override
Codegen decref for init_mem.
External(const std::string &name, const Importer &li, const std::vector< std::string > config_args=std::vector< std::string >())
Constructor.
std::vector< const char * > args_
Importer li_
Information about the library.
virtual void init_external()
Initialize members that are unique.
bool has_reverse(casadi_int nadj) const override
Reverse mode derivatives.
std::string codegen_mem_type() const override
Thread-local memory object type.
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.
void codegen_incref(CodeGenerator &g) const override
Codegen incref for dependencies.
void codegen_declarations(CodeGenerator &g) const override
Generate code for the declarations of the C function.
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
bool has_jacobian() const override
Full Jacobian.
bool has_forward(casadi_int nfwd) const override
Forward mode derivatives.
signal_t incref_
Increase/decrease reference counter.
void codegen_alloc_mem(CodeGenerator &g) const override
Codegen decref for alloc_mem.
void codegen_release(CodeGenerator &g) const override
Codegen for release.
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.
void codegen_free_mem(CodeGenerator &g) const override
Codegen for free_mem.
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.
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.
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.
~External() override=0
Destructor.
virtual bool any_symbol_found() const
Any symbol found?
void codegen_decref(CodeGenerator &g) const override
Codegen decref for dependencies.
double get_default_in(casadi_int i) const override
Default inputs.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize into MX.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
config_t config_
Initialize.
name_t get_name_in_
Names of inputs and outputs.
void init(const Dict &opts) override
Initialize.
void codegen_checkout(CodeGenerator &g) const override
Codegen for checkout.
std::string get_name_out(casadi_int i) override
Names of function input and outputs.
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.
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.
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.
const std::vector< std::string > & name_in() const
Get input scheme.
static Function create(FunctionInternal *node)
Create from node.
casadi_int n_out() const
Get the number of function outputs.
casadi_int n_in() const
Get the number of function inputs.
Function map(casadi_int n, const std::string ¶llelization="serial") const
Create a mapped version of this function.
std::map< std::string, std::vector< std::string > > AuxOut
const std::vector< std::string > & name_out() const
Get output scheme.
Sparsity get_sparsity_in(casadi_int i) override
Retreive sparsities.
Sparsity get_sparsity_out(casadi_int i) override
Retreive sparsities.
bool get_diff_in(casadi_int i) override
Retreive differentiability.
bool any_symbol_found() const override
Any symbol found?
bool get_diff_out(casadi_int i) override
Retreive differentiability.
void init_external() override
Initialize members that are unique.
GenericExternal(const std::string &name, const Importer &li, const std::vector< std::string > config_args=std::vector< std::string >())
Constructor.
void init(const Dict &opts) override
Initialize.
bool has_jac_sparsity(casadi_int oind, casadi_int iind) const override
Return sparsity of Jacobian of an output respect to an input.
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.
void serialize_type(SerializingStream &s) const override
Serialize type information.
std::string library() const
Get library name.
std::string meta_string(const std::string &cmd, casadi_int ind=-1) const
Get entry as a string.
std::string body(const std::string &symname) const
Get the function body, if inlined.
casadi_int meta_int(const std::string &cmd, casadi_int ind=-1) const
Get entry as an integer.
std::vector< T > meta_vector(const std::string &cmd, casadi_int ind=-1) const
Get entry as a vector.
bool has_meta(const std::string &cmd, casadi_int ind=-1) const
Does a meta entry exist?
bool has_function(const std::string &symname) const
bool inlined(const std::string &symname) const
Check if a function is inlined.
signal_t get_function(const std::string &symname)
Get a function pointer for numerical evaluation.
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.
static Sparsity compressed(const std::vector< casadi_int > &v, bool order_rows=false)
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.