map.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_MAP_HPP
27 #define CASADI_MAP_HPP
28 
29 #include "function_internal.hpp"
30 
32 
33 namespace casadi {
34 
39  class CASADI_EXPORT Map : public FunctionInternal {
40  public:
41  // Create function (use instead of constructor)
42  static Function create(const std::string& parallelization,
43  const Function& f, casadi_int n);
44 
48  ~Map() override;
49 
53  std::string class_name() const override {return "Map";}
54 
58  bool is_a(const std::string& type, bool recursive) const override;
59 
60  // Get list of dependency functions
61  virtual std::vector<std::string> get_function() const override;
62 
63  // Get a dependency function
64  const Function& get_function(const std::string &name) const override;
65 
66  // Get all embedded functions, recursively
67  void find(std::map<FunctionInternal*, std::pair<Function, size_t> >& all_fun,
68  casadi_int max_depth) const override;
69 
70  // Check if a particular dependency exists
71  bool has_function(const std::string& fname) const override;
72 
74 
77  Sparsity get_sparsity_in(casadi_int i) override {
78  return repmat(f_.sparsity_in(i), 1, n_);
79  }
80  Sparsity get_sparsity_out(casadi_int i) override {
81  return repmat(f_.sparsity_out(i), 1, n_);
82  }
84 
88  double get_default_in(casadi_int ind) const override { return f_.default_in(ind);}
89 
91 
94  size_t get_n_in() override { return f_.n_in();}
95  size_t get_n_out() override { return f_.n_out();}
97 
99 
102  std::string get_name_in(casadi_int i) override { return f_.name_in(i);}
103  std::string get_name_out(casadi_int i) override { return f_.name_out(i);}
105 
109  template<typename T>
110  int eval_gen(const T** arg, T** res, casadi_int* iw, T* w, int mem=0) const;
111 
113  int eval(const double** arg, double** res, casadi_int* iw, double* w, void* mem) const override;
114 
116  virtual std::string parallelization() const { return "serial"; }
117 
121  int eval_sx(const SXElem** arg, SXElem** res,
122  casadi_int* iw, SXElem* w, void* mem,
123  bool always_inline, bool never_inline) const override;
124 
128  int sp_forward(const bvec_t** arg, bvec_t** res,
129  casadi_int* iw, bvec_t* w, void* mem) const override;
130 
134  int sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w, void* mem) const override;
135 
138  bool has_spfwd() const override { return true;}
139  bool has_sprev() const override { return true;}
141 
145  bool has_codegen() const override { return true;}
146 
150  void codegen_declarations(CodeGenerator& g) const override;
151 
155  void codegen_body(CodeGenerator& g) const override;
156 
160  void init(const Dict& opts) override;
161 
163 
166  bool has_forward(casadi_int nfwd) const override { return true;}
167  Function get_forward(casadi_int nfwd, const std::string& name,
168  const std::vector<std::string>& inames,
169  const std::vector<std::string>& onames,
170  const Dict& opts) const override;
172 
174 
177  bool has_reverse(casadi_int nadj) const override { return true;}
178  Function get_reverse(casadi_int nadj, const std::string& name,
179  const std::vector<std::string>& inames,
180  const std::vector<std::string>& onames,
181  const Dict& opts) const override;
183 
185  Dict info() const override { return {{"f", f_}, {"n", n_}}; }
186 
190  void serialize_body(SerializingStream &s) const override;
194  void serialize_type(SerializingStream &s) const override;
195 
199  std::string serialize_base_function() const override { return "Map"; }
200 
204  static ProtoFunction* deserialize(DeserializingStream& s);
205 
206  protected:
210  explicit Map(DeserializingStream& s);
211 
212  // Constructor (protected, use create function)
213  Map(const std::string& name, const Function& f, casadi_int n);
214 
215  // The function which is to be evaluated in parallel
217 
218  // Number of times to evaluate this function
219  casadi_int n_;
220  };
221 
229  class CASADI_EXPORT OmpMap : public Map {
230  friend class Map;
231  public:
232  // Constructor (protected, use create function in Map)
233  OmpMap(const std::string& name, const Function& f, casadi_int n) : Map(name, f, n) {}
234 
238  ~OmpMap() override;
239 
243  std::string class_name() const override {return "OmpMap";}
244 
248  bool is_a(const std::string& type, bool recursive) const override;
249 
251  int eval(const double** arg, double** res, casadi_int* iw, double* w, void* mem) const override;
252 
256  void init(const Dict& opts) override;
257 
259  std::string parallelization() const override { return "openmp"; }
260 
264  void codegen_body(CodeGenerator& g) const override;
265 
266  protected:
270  explicit OmpMap(DeserializingStream& s) : Map(s) {}
271  };
272 
280  class CASADI_EXPORT ThreadMap : public Map {
281  friend class Map;
282  public:
283  // Constructor (protected, use create function in Map)
284  ThreadMap(const std::string& name, const Function& f, casadi_int n) : Map(name, f, n) {}
285 
289  ~ThreadMap() override;
290 
294  std::string class_name() const override {return "ThreadMap";}
295 
299  bool is_a(const std::string& type, bool recursive) const override;
300 
302  int eval(const double** arg, double** res, casadi_int* iw, double* w, void* mem) const override;
303 
307  void init(const Dict& opts) override;
308 
310  std::string parallelization() const override { return "thread"; }
311 
315  void codegen_declarations(CodeGenerator& g) const override;
316 
320  void codegen_body(CodeGenerator& g) const override;
321 
322  protected:
326  explicit ThreadMap(DeserializingStream& s) : Map(s) {}
327  };
328 
329 } // namespace casadi
331 
332 #endif // CASADI_MAP_HPP
Helper class for C code generation.
Helper class for Serialization.
Internal class for Function.
Function object.
Definition: function.hpp:60
size_t get_n_in() override
Number of function inputs and outputs.
Definition: map.hpp:94
double get_default_in(casadi_int ind) const override
Get default input value.
Definition: map.hpp:88
std::string serialize_base_function() const override
String used to identify the immediate FunctionInternal subclass.
Definition: map.hpp:199
Sparsity get_sparsity_out(casadi_int i) override
Sparsities of function inputs and outputs.
Definition: map.hpp:80
bool has_sprev() const override
Definition: map.hpp:139
Function f_
Definition: map.hpp:216
Sparsity get_sparsity_in(casadi_int i) override
Sparsities of function inputs and outputs.
Definition: map.hpp:77
casadi_int n_
Definition: map.hpp:219
bool has_codegen() const override
Is codegen supported?
Definition: map.hpp:145
bool has_reverse(casadi_int nadj) const override
Generate a function that calculates nadj adjoint derivatives.
Definition: map.hpp:177
Dict info() const override
Definition: map.hpp:185
size_t get_n_out() override
Number of function inputs and outputs.
Definition: map.hpp:95
std::string class_name() const override
Get type name.
Definition: map.hpp:53
bool has_spfwd() const override
Definition: map.hpp:138
bool has_forward(casadi_int nfwd) const override
Generate a function that calculates nfwd forward derivatives.
Definition: map.hpp:166
std::string get_name_out(casadi_int i) override
Names of function input and outputs.
Definition: map.hpp:103
std::string get_name_in(casadi_int i) override
Names of function input and outputs.
Definition: map.hpp:102
virtual std::string parallelization() const
Type of parallellization.
Definition: map.hpp:116
OmpMap(const std::string &name, const Function &f, casadi_int n)
Definition: map.hpp:233
std::string class_name() const override
Get type name.
Definition: map.hpp:243
OmpMap(DeserializingStream &s)
Deserializing constructor.
Definition: map.hpp:270
std::string parallelization() const override
Type of parallellization.
Definition: map.hpp:259
Base class for FunctionInternal and LinsolInternal.
The basic scalar symbolic class of CasADi.
Definition: sx_elem.hpp:75
Helper class for Serialization.
General sparsity class.
Definition: sparsity.hpp:106
std::string parallelization() const override
Type of parallellization.
Definition: map.hpp:310
ThreadMap(const std::string &name, const Function &f, casadi_int n)
Definition: map.hpp:284
ThreadMap(DeserializingStream &s)
Deserializing constructor.
Definition: map.hpp:326
std::string class_name() const override
Get type name.
Definition: map.hpp:294
The casadi namespace.
Definition: archiver.cpp:28
unsigned long long bvec_t
std::vector< casadi_int > find(const std::vector< T > &v)
find nonzeros
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.