alpaqa no-casadi-dep
Nonconvex constrained optimization
Loading...
Searching...
No Matches
casadi-external-function.cpp
Go to the documentation of this file.
3#include <alpaqa/util/dl.hpp>
4
6
7void Function::load(void *so_handle, const std::string &func_name) {
8 functions = {
9 .incref = fname_incref::load(so_handle, func_name),
10 .decref = fname_decref::load(so_handle, func_name),
11 .n_in = fname_n_in::load(so_handle, func_name),
12 .n_out = fname_n_out::load(so_handle, func_name),
13 .name_in = fname_name_in::load(so_handle, func_name),
14 .name_out = fname_name_out::load(so_handle, func_name),
15 .sparsity_in = fname_sparsity_in::load(so_handle, func_name),
16 .sparsity_out = fname_sparsity_out::load(so_handle, func_name),
17 .alloc_mem = fname_alloc_mem::load(so_handle, func_name),
18 .init_mem = fname_init_mem::load(so_handle, func_name),
19 .free_mem = fname_free_mem::load(so_handle, func_name),
20 .work = fname_work::load(so_handle, func_name),
21 .call = fname::load(so_handle, func_name),
22 };
23 functions.incref();
24}
25
26void Function::init_work() {
27 if (work)
28 return;
29 mem = functions.alloc_mem();
30 functions.init_mem(mem); // TODO: what to do upon failure?
32 functions.work(&sz_arg, &sz_res, &sz_iw, &sz_w);
33 auto &w = work.emplace();
34 w.arg.resize(static_cast<size_t>(sz_arg));
35 w.res.resize(static_cast<size_t>(sz_res));
36 w.iw.resize(static_cast<size_t>(sz_iw));
37 w.w.resize(static_cast<size_t>(sz_w));
38}
39
40Function::Function(std::shared_ptr<void> so_handle,
41 const std::string &func_name)
42 : so_handle{std::move(so_handle)} {
43 load(this->so_handle.get(), func_name);
44}
46 : so_handle{o.so_handle}, functions{o.functions} {
48}
50 : so_handle{std::move(o.so_handle)}, functions{o.functions},
51 work{std::move(o.work)}, mem{std::exchange(o.mem, nullptr)} {}
53 if (so_handle) {
54 if (mem)
57 }
58}
59
62std::pair<casadi_int, casadi_int> Function::size_in(casadi_int n) const {
63 return sparsity_in(n).size();
64}
65std::pair<casadi_int, casadi_int> Function::size_out(casadi_int n) const {
66 return sparsity_out(n).size();
67}
86
87Function external(const std::string &name, const std::string &bin_name) {
88 return Function{util::load_lib(std::filesystem::path{bin_name}), name};
89}
90
91} // namespace alpaqa::inline ALPAQA_CASADI_LOADER_NAMESPACE::casadi
#define ALPAQA_CASADI_LOADER_NAMESPACE
std::pair< casadi_int, casadi_int > size() const
Class that loads and calls pre-compiled CasADi functions in a DLL/SO file.
casadi_int size1_out(casadi_int) const
fname_decref::signature_t * decref
std::pair< casadi_int, casadi_int > size_in(casadi_int) const
fname_sparsity_in::signature_t * sparsity_in
void load(void *so_handle, const std::string &func_name)
std::pair< casadi_int, casadi_int > size_out(casadi_int) const
fname_sparsity_out::signature_t * sparsity_out
Sparsity sparsity_in(casadi_int) const
struct builtin::Function::Functions functions
std::shared_ptr< void > so_handle
casadi_int size2_out(casadi_int) const
Function(std::shared_ptr< void > so_handle, const std::string &func_name)
Sparsity sparsity_out(casadi_int) const
casadi_int size1_in(casadi_int) const
fname_free_mem::signature_t * free_mem
fname_incref::signature_t * incref
casadi_int size2_in(casadi_int) const
constexpr const auto inf
Definition config.hpp:112
Function external(const std::string &name, const std::string &bin_name)
Load the given CasADi function from the given DLL/SO file.
long long int casadi_int