external_impl.hpp
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 #ifndef CASADI_EXTERNAL_IMPL_HPP
27 #define CASADI_EXTERNAL_IMPL_HPP
28 
29 #include "external.hpp"
30 #include "function_internal.hpp"
31 
32 #ifdef WITH_DL
33 #ifdef _WIN32 // also for 64-bit
34 #ifndef NOMINMAX
35 #define NOMINMAX
36 #endif
37 #ifndef _WIN32_WINNT
38 #define _WIN32_WINNT 0x0502
39 #endif
40 #include <windows.h>
41 #else // _WIN32
42 #include <dlfcn.h>
43 #endif // _WIN32
44 #endif // WITH_DL
45 
47 
48 namespace casadi {
49 
50 class CASADI_EXPORT External : public FunctionInternal {
51  protected:
55  Importer li_;
56 
60  config_t config_;
61 
65  signal_t incref_, decref_;
66 
70  getint_t get_n_in_, get_n_out_;
71 
75  name_t get_name_in_, get_name_out_;
76 
80  default_t get_default_in_;
81 
85  work_t work_;
86 
88 
91  std::vector<casadi_int> int_data_;
92  std::vector<double> real_data_;
93  std::string string_data_;
95 
99  std::vector<std::string> config_args_;
100  std::vector<const char*> args_;
101  public:
102 
106  External(const std::string& name, const Importer& li,
107  const std::vector<std::string> config_args=std::vector<std::string>());
108 
112  ~External() override = 0;
113 
117  virtual bool any_symbol_found() const;
118 
119  // Factory
120  Function factory(const std::string& name,
121  const std::vector<std::string>& s_in,
122  const std::vector<std::string>& s_out,
123  const Function::AuxOut& aux,
124  const Dict& opts) const override;
125 
129  std::string class_name() const override { return "External";}
130 
134  virtual void init_external();
135 
137  void init(const Dict& opts) override;
138 
142  void codegen_declarations(CodeGenerator& g) const override;
143 
147  void codegen_body(CodeGenerator& g) const override;
148 
152  std::string codegen_mem_type() const override;
153 
157  void codegen_incref(CodeGenerator& g) const override;
158 
162  void codegen_decref(CodeGenerator& g) const override;
163 
167  void codegen_checkout(CodeGenerator& g) const override;
168 
172  void codegen_release(CodeGenerator& g) const override;
173 
177  void codegen_alloc_mem(CodeGenerator& g) const override;
178 
182  void codegen_init_mem(CodeGenerator& g) const override;
183 
187  void codegen_free_mem(CodeGenerator& g) const override;
188 
190 
193  size_t get_n_in() override;
194  size_t get_n_out() override;
196 
200  double get_default_in(casadi_int i) const override;
201 
203 
206  std::string get_name_in(casadi_int i) override;
207  std::string get_name_out(casadi_int i) override;
209 
211 
214  Function get_forward(casadi_int nfwd, const std::string& name,
215  const std::vector<std::string>& inames,
216  const std::vector<std::string>& onames,
217  const Dict& opts) const override;
218  bool has_forward(casadi_int nfwd) const override;
220 
222 
225  Function get_reverse(casadi_int nadj, const std::string& name,
226  const std::vector<std::string>& inames,
227  const std::vector<std::string>& onames,
228  const Dict& opts) const override;
229  bool has_reverse(casadi_int nadj) const override;
231 
233 
236  bool has_jacobian() const override;
237  Function get_jacobian(const std::string& name,
238  const std::vector<std::string>& inames,
239  const std::vector<std::string>& onames,
240  const Dict& opts) const override;
242 
246  void serialize_body(SerializingStream &s) const override;
247 
251  static ProtoFunction* deserialize(DeserializingStream& s);
252 
256  std::string serialize_base_function() const override { return "External"; }
257 
258  protected:
262  explicit External(DeserializingStream& s);
263 };
264 
265 class CASADI_EXPORT GenericExternal : public External {
266  // Sparsities
267  sparsity_t get_sparsity_in_, get_sparsity_out_, get_jac_sparsity_;
268  // Differentiability
269  diff_t get_diff_in_, get_diff_out_;
270 
271  public:
275  GenericExternal(const std::string& name, const Importer& li,
276  const std::vector<std::string> config_args=std::vector<std::string>());
277 
281  ~GenericExternal() override { this->clear_mem();}
282 
286  bool any_symbol_found() const override;
287 
291  void init_external() override;
292 
294  void init(const Dict& opts) override;
295 
297 
300  Sparsity get_sparsity_in(casadi_int i) override;
301  Sparsity get_sparsity_out(casadi_int i) override;
303 
305 
308  bool has_jac_sparsity(casadi_int oind, casadi_int iind) const override;
309  Sparsity get_jac_sparsity(casadi_int oind, casadi_int iind, bool symmetric) const override;
311 
313 
316  bool get_diff_in(casadi_int i) override;
317  bool get_diff_out(casadi_int i) override;
319 
323  void serialize_type(SerializingStream &s) const override;
324 
328  explicit GenericExternal(DeserializingStream& s);
329 
330 };
331 
332 } // namespace casadi
334 
335 #endif // CASADI_EXTERNAL_IMPL_HPP
std::map< std::string, std::vector< std::string > > AuxOut
Definition: function.hpp:395
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.