mapsum.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_MAPSUM_HPP
27 #define CASADI_MAPSUM_HPP
28 
29 #include "function_internal.hpp"
30 
32 
33 namespace casadi {
34 
40  class CASADI_EXPORT MapSum : public FunctionInternal {
41  public:
42  // Create function (use instead of constructor)
43  static Function create(const std::string& name,
44  const std::string& parallelization,
45  const Function& f, casadi_int n,
46  const std::vector<bool>& reduce_in,
47  const std::vector<bool>& reduce_out,
48  const Dict& opts=Dict());
49 
50  // Create function (use instead of constructor)
51  static Function create(const std::string& name,
52  const std::string& parallelization,
53  const Function& f, casadi_int n,
54  const std::vector<casadi_int>& reduce_in,
55  const std::vector<casadi_int>& reduce_out,
56  const Dict& opts=Dict());
57 
61  ~MapSum() override;
62 
66  std::string class_name() const override {return "MapSum";}
67 
69 
72  Sparsity get_sparsity_in(casadi_int i) override {
73  return repmat(f_.sparsity_in(i), 1, reduce_in_[i] ? 1 : n_);
74  }
75  Sparsity get_sparsity_out(casadi_int i) override {
76  return repmat(f_.sparsity_out(i), 1, reduce_out_[i] ? 1 : n_);
77  }
79 
83  double get_default_in(casadi_int ind) const override { return f_.default_in(ind);}
84 
86 
89  size_t get_n_in() override { return f_.n_in();}
90  size_t get_n_out() override { return f_.n_out();}
92 
94 
97  std::string get_name_in(casadi_int i) override { return f_.name_in(i);}
98  std::string get_name_out(casadi_int i) override { return f_.name_out(i);}
100 
104  template<typename T>
105  int eval_gen(const T** arg, T** res, casadi_int* iw, T* w, int mem=0) const;
106 
108  int eval(const double** arg, double** res, casadi_int* iw, double* w, void* mem) const override;
109 
111  virtual std::string parallelization() const { return "serial"; }
112 
116  int eval_sx(const SXElem** arg, SXElem** res,
117  casadi_int* iw, SXElem* w, void* mem) const override;
118 
122  int sp_forward(const bvec_t** arg, bvec_t** res,
123  casadi_int* iw, bvec_t* w, void* mem) const override;
124 
128  int sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w, void* mem) const override;
129 
132  bool has_spfwd() const override { return true;}
133  bool has_sprev() const override { return true;}
135 
139  bool has_codegen() const override { return true;}
140 
144  void codegen_declarations(CodeGenerator& g) const override;
145 
149  void codegen_body(CodeGenerator& g) const override;
150 
154  void init(const Dict& opts) override;
155 
156  // Get list of dependency functions
157  virtual std::vector<std::string> get_function() const override;
158 
159  // Get a dependency function
160  const Function& get_function(const std::string &name) const override;
161 
162  // Check if a particular dependency exists
163  bool has_function(const std::string& fname) const override;
164 
166 
169  bool has_forward(casadi_int nfwd) const override { return true;}
170  Function get_forward(casadi_int nfwd, const std::string& name,
171  const std::vector<std::string>& inames,
172  const std::vector<std::string>& onames,
173  const Dict& opts) const override;
175 
177 
180  bool has_reverse(casadi_int nadj) const override { return true;}
181  Function get_reverse(casadi_int nadj, const std::string& name,
182  const std::vector<std::string>& inames,
183  const std::vector<std::string>& onames,
184  const Dict& opts) const override;
186 
187 
191  void serialize_body(SerializingStream &s) const override;
192 
196  void serialize_type(SerializingStream &s) const override;
197 
201  std::string serialize_base_function() const override { return "MapSum"; }
202 
206  static ProtoFunction* deserialize(DeserializingStream& s);
207 
208  protected:
212  explicit MapSum(DeserializingStream& s);
213 
214  // Constructor (protected, use create function)
215  MapSum(const std::string& name, const Function& f, casadi_int n,
216  const std::vector<bool>& reduce_in,
217  const std::vector<bool>& reduce_out);
218 
219  // The function which is to be evaluated in parallel
220  Function f_;
221 
222  // Number of times to evaluate this function
223  casadi_int n_;
224 
225  // Reduce an input?
226  std::vector<bool> reduce_in_;
227 
228  // Reduce an output?
229  std::vector<bool> reduce_out_;
230  };
231 
232 
233 } // namespace casadi
235 
236 #endif // CASADI_MAPSUM_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.