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  bool is_a(const std::string& type, bool recursive) const override {
125  return type=="External" || (recursive && FunctionInternal::is_a(type, recursive));
126  }
127 
131  virtual void init_external();
132 
134  void init(const Dict& opts) override;
135 
139  void codegen_declarations(CodeGenerator& g) const override;
140 
144  void codegen_body(CodeGenerator& g) const override;
145 
149  bool codegen_needs_mem() const override { return true; }
150 
156  bool codegen_mem_is_opaque() const override { return true; }
157 
161  void codegen_incref(CodeGenerator& g) const override;
162 
166  void codegen_decref(CodeGenerator& g) const override;
167 
171  void codegen_checkout(CodeGenerator& g) const override;
172 
176  void codegen_release(CodeGenerator& g) const override;
177 
181  void codegen_alloc_mem(CodeGenerator& g) const override;
182 
186  void codegen_init_mem(CodeGenerator& g) const override;
187 
191  void codegen_free_mem(CodeGenerator& g) const override;
192 
194 
197  size_t get_n_in() override;
198  size_t get_n_out() override;
200 
204  double get_default_in(casadi_int i) const override;
205 
207 
210  std::string get_name_in(casadi_int i) override;
211  std::string get_name_out(casadi_int i) override;
213 
215 
218  Function get_forward(casadi_int nfwd, const std::string& name,
219  const std::vector<std::string>& inames,
220  const std::vector<std::string>& onames,
221  const Dict& opts) const override;
222  bool has_forward(casadi_int nfwd) const override;
224 
226 
229  Function get_reverse(casadi_int nadj, const std::string& name,
230  const std::vector<std::string>& inames,
231  const std::vector<std::string>& onames,
232  const Dict& opts) const override;
233  bool has_reverse(casadi_int nadj) const override;
235 
237 
240  bool has_jacobian() const override;
241  Function get_jacobian(const std::string& name,
242  const std::vector<std::string>& inames,
243  const std::vector<std::string>& onames,
244  const Dict& opts) const override;
246 
250  void serialize_body(SerializingStream &s) const override;
251 
255  static ProtoFunction* deserialize(DeserializingStream& s);
256 
260  std::string serialize_base_function() const override { return "External"; }
261 
262  protected:
266  explicit External(DeserializingStream& s);
267 };
268 
269 class CASADI_EXPORT GenericExternal : public External {
270  // Sparsities
271  sparsity_t get_sparsity_in_, get_sparsity_out_, get_jac_sparsity_;
272  // Differentiability
273  diff_t get_diff_in_, get_diff_out_;
274 
275  public:
279  GenericExternal(const std::string& name, const Importer& li,
280  const std::vector<std::string> config_args=std::vector<std::string>());
281 
285  ~GenericExternal() override { this->clear_mem();}
286 
290  bool any_symbol_found() const override;
291 
295  void init_external() override;
296 
298  void init(const Dict& opts) override;
299 
301 
304  Sparsity get_sparsity_in(casadi_int i) override;
305  Sparsity get_sparsity_out(casadi_int i) override;
307 
309 
312  bool has_jac_sparsity(casadi_int oind, casadi_int iind) const override;
313  Sparsity get_jac_sparsity(casadi_int oind, casadi_int iind, bool symmetric) const override;
315 
317 
320  bool get_diff_in(casadi_int i) override;
321  bool get_diff_out(casadi_int i) override;
323 
327  void serialize_type(SerializingStream &s) const override;
328 
332  explicit GenericExternal(DeserializingStream& s);
333 
334 };
335 
336 } // namespace casadi
338 
339 #endif // CASADI_EXTERNAL_IMPL_HPP
std::map< std::string, std::vector< std::string > > AuxOut
Definition: function.hpp:1546
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.