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  config_t config_;
56 
60  getint_t get_n_in_, get_n_out_;
61 
65  name_t get_name_in_, get_name_out_;
66 
70  default_t get_default_in_;
71 
75  work_t work_;
76 
78 
81  std::vector<casadi_int> int_data_;
82  std::vector<double> real_data_;
83  std::string string_data_;
85 
89  std::vector<std::string> config_args_;
90  std::vector<const char*> args_;
91  public:
92 
96  External(const std::string& name, const Importer& li,
97  const std::vector<std::string> config_args=std::vector<std::string>());
98 
102  ~External() override = 0;
103 
107  virtual bool any_symbol_found() const;
108 
109  // Factory
110  Function factory(const std::string& name,
111  const std::vector<std::string>& s_in,
112  const std::vector<std::string>& s_out,
113  const Function::AuxOut& aux,
114  const Dict& opts) const override;
115 
119  std::string class_name() const override { return "External";}
120 
124  virtual void init_external();
125 
127  void init(const Dict& opts) override;
128 
132  void codegen_declarations(CodeGenerator& g) const override;
133 
137  void codegen_body(CodeGenerator& g) const override;
138 
142  bool codegen_needs_mem() const override { return true; }
143 
149  bool codegen_mem_is_opaque() const override { return true; }
150 
154  void codegen_incref(CodeGenerator& g) const override;
155 
159  void codegen_decref(CodeGenerator& g) const override;
160 
164  void codegen_checkout(CodeGenerator& g) const override;
165 
169  void codegen_release(CodeGenerator& g) const override;
170 
174  void codegen_alloc_mem(CodeGenerator& g) const override;
175 
179  void codegen_init_mem(CodeGenerator& g) const override;
180 
184  void codegen_free_mem(CodeGenerator& g) const override;
185 
187 
190  size_t get_n_in() override;
191  size_t get_n_out() override;
193 
197  double get_default_in(casadi_int i) const override;
198 
200 
203  std::string get_name_in(casadi_int i) override;
204  std::string get_name_out(casadi_int i) override;
206 
208 
211  Function get_forward(casadi_int nfwd, const std::string& name,
212  const std::vector<std::string>& inames,
213  const std::vector<std::string>& onames,
214  const Dict& opts) const override;
215  bool has_forward(casadi_int nfwd) const override;
217 
219 
222  Function get_reverse(casadi_int nadj, const std::string& name,
223  const std::vector<std::string>& inames,
224  const std::vector<std::string>& onames,
225  const Dict& opts) const override;
226  bool has_reverse(casadi_int nadj) const override;
228 
230 
233  bool has_jacobian() const override;
234  Function get_jacobian(const std::string& name,
235  const std::vector<std::string>& inames,
236  const std::vector<std::string>& onames,
237  const Dict& opts) const override;
239 
243  void serialize_body(SerializingStream &s) const override;
244 
248  static ProtoFunction* deserialize(DeserializingStream& s);
249 
253  std::string serialize_base_function() const override { return "External"; }
254 
255  protected:
259  explicit External(DeserializingStream& s);
260 };
261 
262 class CASADI_EXPORT GenericExternal : public External {
263  // Sparsities
264  sparsity_t get_sparsity_in_, get_sparsity_out_, get_jac_sparsity_;
265  // Differentiability
266  diff_t get_diff_in_, get_diff_out_;
267 
268  public:
272  GenericExternal(const std::string& name, const Importer& li,
273  const std::vector<std::string> config_args=std::vector<std::string>());
274 
278  ~GenericExternal() override { this->clear_mem();}
279 
283  bool any_symbol_found() const override;
284 
288  void init_external() override;
289 
291  void init(const Dict& opts) override;
292 
294 
297  Sparsity get_sparsity_in(casadi_int i) override;
298  Sparsity get_sparsity_out(casadi_int i) override;
300 
302 
305  bool has_jac_sparsity(casadi_int oind, casadi_int iind) const override;
306  Sparsity get_jac_sparsity(casadi_int oind, casadi_int iind, bool symmetric) const override;
308 
310 
313  bool get_diff_in(casadi_int i) override;
314  bool get_diff_out(casadi_int i) override;
316 
320  void serialize_type(SerializingStream &s) const override;
321 
325  explicit GenericExternal(DeserializingStream& s);
326 
327 };
328 
329 } // namespace casadi
331 
332 #endif // CASADI_EXTERNAL_IMPL_HPP
std::map< std::string, std::vector< std::string > > AuxOut
Definition: function.hpp:447
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.