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  // Check if a particular dependency exists
67  bool has_function(const std::string& fname) const override;
68 
70 
73  Sparsity get_sparsity_in(casadi_int i) override {
74  return repmat(f_.sparsity_in(i), 1, n_);
75  }
76  Sparsity get_sparsity_out(casadi_int i) override {
77  return repmat(f_.sparsity_out(i), 1, n_);
78  }
80 
84  double get_default_in(casadi_int ind) const override { return f_.default_in(ind);}
85 
87 
90  size_t get_n_in() override { return f_.n_in();}
91  size_t get_n_out() override { return f_.n_out();}
93 
95 
98  std::string get_name_in(casadi_int i) override { return f_.name_in(i);}
99  std::string get_name_out(casadi_int i) override { return f_.name_out(i);}
101 
105  template<typename T>
106  int eval_gen(const T** arg, T** res, casadi_int* iw, T* w, int mem=0) const;
107 
109  int eval(const double** arg, double** res, casadi_int* iw, double* w, void* mem) const override;
110 
112  virtual std::string parallelization() const { return "serial"; }
113 
117  int eval_sx(const SXElem** arg, SXElem** res,
118  casadi_int* iw, SXElem* w, void* mem,
119  bool always_inline, bool never_inline) const override;
120 
124  int sp_forward(const bvec_t** arg, bvec_t** res,
125  casadi_int* iw, bvec_t* w, void* mem) const override;
126 
130  int sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w, void* mem) const override;
131 
134  bool has_spfwd() const override { return true;}
135  bool has_sprev() const override { return true;}
137 
141  bool has_codegen() const override { return true;}
142 
146  void codegen_declarations(CodeGenerator& g) const override;
147 
151  void codegen_body(CodeGenerator& g) const override;
152 
156  void init(const Dict& opts) override;
157 
159 
162  bool has_forward(casadi_int nfwd) const override { return true;}
163  Function get_forward(casadi_int nfwd, const std::string& name,
164  const std::vector<std::string>& inames,
165  const std::vector<std::string>& onames,
166  const Dict& opts) const override;
168 
170 
173  bool has_reverse(casadi_int nadj) const override { return true;}
174  Function get_reverse(casadi_int nadj, const std::string& name,
175  const std::vector<std::string>& inames,
176  const std::vector<std::string>& onames,
177  const Dict& opts) const override;
179 
181  Dict info() const override { return {{"f", f_}, {"n", n_}}; }
182 
186  void serialize_body(SerializingStream &s) const override;
190  void serialize_type(SerializingStream &s) const override;
191 
195  std::string serialize_base_function() const override { return "Map"; }
196 
200  static ProtoFunction* deserialize(DeserializingStream& s);
201 
202  protected:
206  explicit Map(DeserializingStream& s);
207 
208  // Constructor (protected, use create function)
209  Map(const std::string& name, const Function& f, casadi_int n);
210 
211  // The function which is to be evaluated in parallel
213 
214  // Number of times to evaluate this function
215  casadi_int n_;
216  };
217 
225  class CASADI_EXPORT OmpMap : public Map {
226  friend class Map;
227  public:
228  // Constructor (protected, use create function in Map)
229  OmpMap(const std::string& name, const Function& f, casadi_int n) : Map(name, f, n) {}
230 
234  ~OmpMap() override;
235 
239  std::string class_name() const override {return "OmpMap";}
240 
244  bool is_a(const std::string& type, bool recursive) const override;
245 
247  int eval(const double** arg, double** res, casadi_int* iw, double* w, void* mem) const override;
248 
252  void init(const Dict& opts) override;
253 
255  std::string parallelization() const override { return "openmp"; }
256 
260  void codegen_body(CodeGenerator& g) const override;
261 
262  protected:
266  explicit OmpMap(DeserializingStream& s) : Map(s) {}
267  };
268 
276  class CASADI_EXPORT ThreadMap : public Map {
277  friend class Map;
278  public:
279  // Constructor (protected, use create function in Map)
280  ThreadMap(const std::string& name, const Function& f, casadi_int n) : Map(name, f, n) {}
281 
285  ~ThreadMap() override;
286 
290  std::string class_name() const override {return "ThreadMap";}
291 
295  bool is_a(const std::string& type, bool recursive) const override;
296 
298  int eval(const double** arg, double** res, casadi_int* iw, double* w, void* mem) const override;
299 
303  void init(const Dict& opts) override;
304 
306  std::string parallelization() const override { return "thread"; }
307 
311  void codegen_body(CodeGenerator& g) const override;
312 
313  protected:
317  explicit ThreadMap(DeserializingStream& s) : Map(s) {}
318  };
319 
320 } // namespace casadi
322 
323 #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:90
double get_default_in(casadi_int ind) const override
Get default input value.
Definition: map.hpp:84
std::string serialize_base_function() const override
String used to identify the immediate FunctionInternal subclass.
Definition: map.hpp:195
Sparsity get_sparsity_out(casadi_int i) override
Sparsities of function inputs and outputs.
Definition: map.hpp:76
bool has_sprev() const override
Definition: map.hpp:135
Function f_
Definition: map.hpp:212
Sparsity get_sparsity_in(casadi_int i) override
Sparsities of function inputs and outputs.
Definition: map.hpp:73
casadi_int n_
Definition: map.hpp:215
bool has_codegen() const override
Is codegen supported?
Definition: map.hpp:141
bool has_reverse(casadi_int nadj) const override
Generate a function that calculates nadj adjoint derivatives.
Definition: map.hpp:173
Dict info() const override
Definition: map.hpp:181
size_t get_n_out() override
Number of function inputs and outputs.
Definition: map.hpp:91
std::string class_name() const override
Get type name.
Definition: map.hpp:53
bool has_spfwd() const override
Definition: map.hpp:134
bool has_forward(casadi_int nfwd) const override
Generate a function that calculates nfwd forward derivatives.
Definition: map.hpp:162
std::string get_name_out(casadi_int i) override
Names of function input and outputs.
Definition: map.hpp:99
std::string get_name_in(casadi_int i) override
Names of function input and outputs.
Definition: map.hpp:98
virtual std::string parallelization() const
Type of parallellization.
Definition: map.hpp:112
OmpMap(const std::string &name, const Function &f, casadi_int n)
Definition: map.hpp:229
std::string class_name() const override
Get type name.
Definition: map.hpp:239
OmpMap(DeserializingStream &s)
Deserializing constructor.
Definition: map.hpp:266
std::string parallelization() const override
Type of parallellization.
Definition: map.hpp:255
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:306
ThreadMap(const std::string &name, const Function &f, casadi_int n)
Definition: map.hpp:280
ThreadMap(DeserializingStream &s)
Deserializing constructor.
Definition: map.hpp:317
std::string class_name() const override
Get type name.
Definition: map.hpp:290
The casadi namespace.
Definition: archiver.cpp:28
unsigned long long bvec_t
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.