code_generator.cpp
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 
27 #include "code_generator.hpp"
28 #include "function_internal.hpp"
29 #include "convexify.hpp"
30 #include <casadi_runtime_str.h>
31 #include "global_options.hpp"
32 #include "filesystem_impl.hpp"
33 #include <iomanip>
34 
35 namespace casadi {
36 
37  CodeGenerator::CodeGenerator(const std::string& name, const Dict& opts) {
38  // Default options
39  this->verbose = true;
40  this->verbose_runtime = false;
41  this->mex = false;
42  this->with_sfunction = false;
43  this->unroll_args = false;
44  this->cpp = false;
45  this->main = false;
46  this->casadi_real_type = "double";
47  this->casadi_int_type = CASADI_INT_TYPE_STR;
48  this->codegen_scalars = false;
49  this->with_header = false;
50  this->with_mem = false;
51  this->with_export = true;
52  this->with_import = false;
53  this->include_math = true;
54  this->infinity = "INFINITY";
55  this->nan = "NAN";
56  this->real_min = "";
57  bool prefix_set = false;
58  this->prefix = "";
59  this->max_declarations_per_line = 12;
61  this->force_canonical = false;
62 
63  avoid_stack_ = false;
64  indent_ = 2;
65  sz_zeros_ = 0;
66  sz_ones_ = 0;
67 
68  // Read options
69  for (auto&& e : opts) {
70  if (e.first=="verbose") {
71  this->verbose = e.second;
72  } else if (e.first=="verbose_runtime") {
73  this->verbose_runtime = e.second;
74  } else if (e.first=="mex") {
75  this->mex = e.second;
76  } else if (e.first=="with_sfunction") {
77  this->with_sfunction = e.second;
78  } else if (e.first=="unroll_args") {
79  this->unroll_args = e.second;
80  } else if (e.first=="cpp") {
81  this->cpp = e.second;
82  } else if (e.first=="main") {
83  this->main = e.second;
84  } else if (e.first=="casadi_real") {
85  this->casadi_real_type = e.second.to_string();
86  } else if (e.first=="casadi_int") {
87  this->casadi_int_type = e.second.to_string();
88  } else if (e.first=="codegen_scalars") {
89  this->codegen_scalars = e.second;
90  } else if (e.first=="with_header") {
91  this->with_header = e.second;
92  } else if (e.first=="with_mem") {
93  this->with_mem = e.second;
94  } else if (e.first=="with_export") {
95  this->with_export = e.second;
96  } else if (e.first=="with_import") {
97  this->with_import = e.second;
98  } else if (e.first=="include_math") {
99  this->include_math = e.second;
100  } else if (e.first=="infinity") {
101  this->infinity = e.second.to_string();
102  } else if (e.first=="nan") {
103  this->nan = e.second.to_string();
104  } else if (e.first=="real_min") {
105  this->real_min = e.second.to_string();
106  } else if (e.first=="indent") {
107  indent_ = e.second;
108  casadi_assert_dev(indent_>=0);
109  } else if (e.first=="avoid_stack") {
110  avoid_stack_ = e.second;
111  } else if (e.first=="prefix") {
112  this->prefix = e.second.to_string();
113  prefix_set = true;
114  } else if (e.first=="max_declarations_per_line") {
115  this->max_declarations_per_line = e.second;
116  casadi_assert(this->max_declarations_per_line>=0,
117  "Option max_declarations_per_line must be >=0");
118  } else if (e.first=="max_initializer_elements_per_line") {
119  this->max_initializer_elements_per_line = e.second;
120  casadi_assert(this->max_initializer_elements_per_line>=0,
121  "Option max_initializer_elements_per_line must be >=0");
122  } else if (e.first=="force_canonical") {
123  this->force_canonical = e.second;
124  } else {
125  casadi_error("Unrecognized option: " + str(e.first));
126  }
127  }
128 
129  // If real_min is not specified, make an educated guess
130  if (this->real_min.empty()) {
131  std::stringstream ss;
132  ss << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1);
133  if (casadi_real_type=="float") {
134  ss << std::numeric_limits<float>::min();
135  this->real_min = ss.str();
136  } else if (casadi_real_type=="double") {
137  ss << std::numeric_limits<double>::min();
138  this->real_min = ss.str();
139  } else {
140  this->real_min = "<NOT SPECIFIED>";
141  }
142  }
143 
144  // Start at new line with no indentation
145  newline_ = true;
146  current_indent_ = 0;
147 
148  // Start off without the need for thread-local memory
149  needs_mem_ = false;
150 
151  // Divide name into base and suffix (if any)
152  std::string::size_type dotpos = name.rfind('.');
153  if (dotpos==std::string::npos) {
154  this->name = name;
155  this->suffix = this->cpp ? ".cpp" : ".c";
156  } else {
157  this->name = name.substr(0, dotpos);
158  this->suffix = name.substr(dotpos);
159  }
160 
161  // Symbol prefix
162  if (this->with_export) dll_export = "CASADI_SYMBOL_EXPORT ";
163  if (this->with_import) dll_import = "CASADI_SYMBOL_IMPORT ";
164 
165  // Make sure that the base name is sane
166  casadi_assert_dev(Function::check_name(this->name));
167 
168  // Includes needed
169  if (this->include_math) add_include("math.h");
170  if (this->main) add_include("stdio.h");
172 
173  // Mex and main need string.h
174  if (this->mex || this->main) {
175  add_include("string.h");
176  }
177 
178  // Mex
179  if (this->mex) {
180  add_include("mex.h", false, "MATLAB_MEX_FILE");
181  }
182 
183  // s-Function
184  if (this->with_sfunction) {
185  this->casadi_real_type = "real_T";
186  this->casadi_int_type = "int_T";
187  this->with_header = true;
188  add_include("simstruc.h");
189  }
190 
191  // Memory struct entry point
192  if (this->with_mem) {
193  this->header << "#include <casadi/mem.h>\n";
194  }
195 
196  // Use name as default prefix
197  if (!prefix_set) {
198  this->prefix = this->name;
199  }
200 
201  }
202 
204  local_variables_.clear();
205  local_default_.clear();
206  }
207 
209  // Order local variables
210  std::map<std::string, std::set<std::pair<std::string, std::string>>> local_variables_by_type;
211  for (auto&& e : local_variables_) {
212  local_variables_by_type[e.second.first].insert(std::make_pair(e.first, e.second.second));
213  }
214 
215  // Codegen local variables
216  for (auto&& e : local_variables_by_type) {
217  casadi_int cnt = 0;
218  for (auto it=e.second.begin(); it!=e.second.end(); ++it) {
219  bool split_declaration = it==e.second.begin() ||
221  if (split_declaration) {
222  if (it!=e.second.begin()) body << ";\n";
223  body << " " << e.first << " ";
224  } else {
225  body << ", ";
226  }
227  body << it->second << it->first;
228  // Insert definition, if any
229  auto k=local_default_.find(it->first);
230  if (k!=local_default_.end()) body << "=" << k->second;
231  cnt++;
232  }
233  body << ";\n";
234  }
235  }
236 
237  std::string CodeGenerator::wrapper(const Function& base, const std::string& name) {
238  FunctionInternal* f = base.get();
239  std::map<FunctionInternal*, casadi_int>& funs = added_wrappers_[name];
240  auto it = funs.find(f);
241  if (it==funs.end()) {
242  casadi_int n = funs.size();
243  funs[f] = n;
244  return name + str(n);
245  } else {
246  return name + str(it->second);
247  }
248  }
249 
250  std::string CodeGenerator::add_dependency(const Function& f) {
251  // Quick return if it already exists
252  for (auto&& e : added_functions_) if (e.f==f) return e.codegen_name;
253 
254  // Give it a name
255  std::string fname = shorthand("f" + str(added_functions_.size()));
256 
257  // Add to list of functions
258  added_functions_.push_back({f, fname});
259 
260  // Generate declarations
261  f->codegen_declarations(*this);
262 
263  // Print to file
264  f->codegen(*this, fname);
265 
266  // Codegen reference count functions, if needed
267  if (f->has_refcount_) {
268  // Increase reference counter
269  *this << "void " << fname << "_incref(void) {\n";
270  f->codegen_incref(*this);
271  *this << "}\n\n";
272 
273  // Decrease reference counter
274  *this << "void " << fname << "_decref(void) {\n";
275  f->codegen_decref(*this);
276  *this << "}\n\n";
277  }
278 
279  bool fun_needs_mem = !f->codegen_mem_type().empty();
280  needs_mem_ |= fun_needs_mem;
281 
282  if (fun_needs_mem) {
283  // Alloc memory
284  *this << "int " << fname << "_alloc_mem(void) {\n";
285  flush(this->body);
286  scope_enter();
287  f->codegen_alloc_mem(*this);
288  scope_exit();
289  *this << "}\n\n";
290 
291  // Initialize memory
292  *this << "int " << fname << "_init_mem(int mem) {\n";
293  flush(this->body);
294  scope_enter();
295  f->codegen_init_mem(*this);
296  scope_exit();
297  *this << "}\n\n";
298 
299  // Clear memory
300  *this << "void " << fname << "_free_mem(int mem) {\n";
301  flush(this->body);
302  scope_enter();
303  f->codegen_free_mem(*this);
304  scope_exit();
305  *this << "}\n\n";
306 
307  // Checkout
308  *this << "int " << fname << "_checkout(void) {\n";
309  flush(this->body);
310  scope_enter();
311  f->codegen_checkout(*this);
312  scope_exit();
313  *this << "}\n\n";
314 
315  // Clear memory
316  *this << "void " << fname << "_release(int mem) {\n";
317  flush(this->body);
318  scope_enter();
319  f->codegen_release(*this);
320  scope_exit();
321  *this << "}\n\n";
322 
323  }
324 
325  // Flush to body
326  flush(this->body);
327 
328  return fname;
329  }
330 
331  void CodeGenerator::add(const Function& f, bool with_jac_sparsity) {
332  // Add if not already added
333  std::string codegen_name = add_dependency(f);
334 
335  // Define function
336  *this << declare(f->signature(f.name())) << "{\n"
337  << "return " << codegen_name << "(arg, res, iw, w, mem);\n"
338  << "}\n\n";
339 
340  if (this->unroll_args) {
341  // Define function
342  *this << declare(f->signature_unrolled(f.name())) << "{\n";
343  for (casadi_int i=0; i<f.n_in(); ++i) {
344  *this << "arg[" << i << "] = " << f.name_in(i) << ";\n";
345  }
346  for (casadi_int i=0; i<f.n_out(); ++i) {
347  *this << "res[" << i << "] = " << f.name_out(i) << ";\n";
348  }
349  *this << "return " << codegen_name << "(arg, res, iw, w, mem);\n";
350  *this << "}\n\n";
351  // Flush buffers
352  flush(this->body);
353  }
354 
355  // Generate meta information
356  f->codegen_meta(*this);
357 
358  // Generate Jacobian sparsity information
359  if (with_jac_sparsity) {
360  // Generate/get Jacobian sparsity
361  std::vector<Sparsity> jac = f.jac_sparsity();
362  // Code generate the sparsity pattern
363  add_io_sparsities("jac_" + f.name(), f->sparsity_in_, jac);
364 
365  // Flush buffers
366  flush(this->body);
367  }
368 
369  // Generate function specific code for Simulink sfunction
370  if (this->with_sfunction) this->added_sfunctions.push_back( this->codegen_sfunction(f) );
371 
372  // Add to list of exposed symbols
373  this->exposed_fname.push_back(f.name());
374  }
375 
376  std::string CodeGenerator::dump() {
377  std::stringstream s;
378  dump(s);
379  return s.str();
380  }
381 
383  std::stringstream ss;
384  ss << CASADI_MAJOR_VERSION << "." << CASADI_MINOR_VERSION << "." << CASADI_PATCH_VERSION;
385  if (!CASADI_IS_RELEASE) ss << "+";
386  return ss.str();
387  }
388 
389  void CodeGenerator::file_open(std::ofstream& f, const std::string& name, bool cpp) {
390  // Open a file for writing
392 
393  // Print header
394  f << "/* This file was automatically generated by CasADi " << casadi_version() << ".\n"
395  << " * It consists of: \n"
396  << " * 1) content generated by CasADi runtime: not copyrighted\n"
397  << " * 2) template code copied from CasADi source: permissively licensed (MIT-0)\n"
398  << " * 3) user code: owned by the user\n"
399  << " *\n"
400  << " */\n";
401 
402  // C linkage
403  if (!cpp) {
404  f << "#ifdef __cplusplus\n"
405  << "extern \"C\" {\n"
406  << "#endif\n\n";
407  }
408  }
409 
410  void CodeGenerator::file_close(std::ofstream& f, bool cpp) {
411  // C linkage
412  if (!cpp) {
413  f << "#ifdef __cplusplus\n"
414  << "} /* extern \"C\" */\n"
415  << "#endif\n";
416  }
417 
418  // Close file(s)
419  f.close();
420  }
421 
422  void CodeGenerator::generate_casadi_real(std::ostream &s) const {
423  s << "#ifndef casadi_real\n"
424  << "#define casadi_real " << this->casadi_real_type << std::endl
425  << "#endif\n\n";
426  }
427 
428  void CodeGenerator::generate_export_symbol(std::ostream &s) const {
429  s << "/* Symbol visibility in DLLs */\n"
430  << "#ifndef CASADI_SYMBOL_EXPORT\n"
431  << " #if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)\n"
432  << " #if defined(STATIC_LINKED)\n"
433  << " #define CASADI_SYMBOL_EXPORT\n"
434  << " #else\n"
435  << " #define CASADI_SYMBOL_EXPORT __declspec(dllexport)\n"
436  << " #endif\n"
437  << " #elif defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)\n"
438  << " #define CASADI_SYMBOL_EXPORT __attribute__ ((visibility (\"default\")))\n"
439  << " #else" << std::endl
440  << " #define CASADI_SYMBOL_EXPORT\n"
441  << " #endif\n"
442  << "#endif\n\n";
443  }
444 
445  void CodeGenerator::generate_import_symbol(std::ostream &s) const {
446  s << "/* Symbol visibility in DLLs */\n"
447  << "#ifndef CASADI_SYMBOL_IMPORT\n"
448  << " #if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)\n"
449  << " #if defined(STATIC_LINKED)\n"
450  << " #define CASADI_SYMBOL_IMPORT\n"
451  << " #else\n"
452  << " #define CASADI_SYMBOL_IMPORT __declspec(dllimport)\n"
453  << " #endif\n"
454  << " #elif defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)\n"
455  << " #define CASADI_SYMBOL_IMPORT __attribute__ ((visibility (\"default\")))\n"
456  << " #else" << std::endl
457  << " #define CASADI_SYMBOL_IMPORT\n"
458  << " #endif\n"
459  << "#endif\n\n";
460  }
461 
462  void CodeGenerator::generate_casadi_int(std::ostream &s) const {
463  s << "#ifndef casadi_int\n"
464  << "#define casadi_int " << this->casadi_int_type << std::endl
465  << "#endif\n\n";
466  }
467 
468  std::string CodeGenerator::generate(const std::string& prefix) {
469  // Throw an error if the prefix contains the filename, since since syntax
470  // has changed
471  casadi_assert(prefix.find(this->name + this->suffix)==std::string::npos,
472  "The signature of CodeGenerator::generate has changed. "
473  "Instead of providing the filename, only provide the prefix.");
474 
475  // Create c file
476  std::ofstream s;
477  std::string fullname = prefix + this->name + this->suffix;
478  file_open(s, fullname, this->cpp);
479 
480  // Dump code to file
481  dump(s);
482 
483  if (!pool_double_defaults_.empty()) {
484  s << "CASADI_SYMBOL_EXPORT casadi_real* CASADI_PREFIX(get_pool_double)(const char* name) {\n";
485  for (const auto& e : pool_double_) {
486  casadi_int i = e.second;
487  s << " if (strcmp(name, \"" + e.first + "\")==0) "
488  << "return casadi_pd" + str(i) + ";\n";
489  }
490  s << " return 0;\n";
491  s << "}\n";
492  }
493 
494  // Mex entry point
495  if (this->mex) generate_mex(s);
496 
497  // Main entry point
498  if (this->main) generate_main(s);
499 
500  // Finalize file
501  file_close(s, this->cpp);
502 
503  // Generate s-function
504  if (this->with_sfunction) {
505  for (unsigned ii=0; ii<this->added_sfunctions.size(); ii++) {
506  std::string sfunction_code = this->added_sfunctions.at(ii);
507  std::string sfunction_name = this->exposed_fname.at(ii);
508  generate_sfunction(sfunction_name, sfunction_code);
509  }
510  }
511 
512  // Generate header
513  if (this->with_header) {
514  // Create a header file
515  file_open(s, prefix + this->name + ".h", this->cpp);
516 
517  // Define the casadi_real type (typically double)
518  generate_casadi_real(s);
519 
520  // Define the casadi_int type
521  generate_casadi_int(s);
522 
523  // Generate export symbol macros
524  if (this->with_import) generate_import_symbol(s);
525 
526  // Add declarations
527  s << this->header.str();
528 
529  // Finalize file
530  file_close(s, this->cpp);
531  }
532  return fullname;
533  }
534 
535  void CodeGenerator::generate_mex(std::ostream &s) const {
536  // Begin conditional compilation
537  s << "#ifdef MATLAB_MEX_FILE\n";
538 
539  // Function prototype
540  if (this->cpp) s << "extern \"C\"\n"; // C linkage
541  s << "void mexFunction(int resc, mxArray *resv[], int argc, const mxArray *argv[]) {"
542  << std::endl;
543 
544  // Create a buffer
545  size_t buf_len = 0;
546  for (casadi_int i=0; i<exposed_fname.size(); ++i) {
547  buf_len = std::max(buf_len, exposed_fname[i].size());
548  }
549  s << " char buf[" << (buf_len+1) << "];\n";
550 
551  // Read std::string argument
552  s << " int buf_ok = argc > 0 && !mxGetString(*argv, buf, sizeof(buf));\n";
553 
554  // Create switch
555  s << " if (!buf_ok) {\n";
556  // Allow stringless call when unambiguous
557  if (exposed_fname.size()==1) {
558  s << " mex_" << exposed_fname[0] << "(resc, resv, argc, argv);\n"
559  << " return;\n";
560  } else {
561  s << " /* name error */\n";
562  }
563  for (casadi_int i=0; i<exposed_fname.size(); ++i) {
564  s << " } else if (strcmp(buf, \"" << exposed_fname[i] << "\")==0) {\n"
565  << " mex_" << exposed_fname[i] << "(resc, resv, argc-1, argv+1);\n"
566  << " return;\n";
567  }
568  s << " }\n";
569 
570  // Error
571  s << " mexErrMsgTxt(\"First input should be a command string. Possible values:";
572  for (casadi_int i=0; i<exposed_fname.size(); ++i) {
573  s << " '" << exposed_fname[i] << "'";
574  }
575  s << "\");\n";
576 
577  // End conditional compilation and function
578  s << "}\n"
579  << "#endif\n";
580  }
581 
582  void CodeGenerator::generate_sfunction(const std::string& name,
583  const std::string& sfunction) const {
584  // Create c file
585  std::ofstream f;
586  Filesystem::open(f, "sfun_"+ name + ".c");
587 
588  // Print header
589  f << "// Must specify the S_FUNCTION_NAME as the name of the S-function\n"
590  << "#define S_FUNCTION_NAME sfun_" << name << "\n"
591  << "#define S_FUNCTION_LEVEL 2\n\n"
592  << "// Need to include simstruc.h for the definition of the SimStruct and its\n"
593  << "// associated macro definitions\n"
594  << "#ifndef __SIMSTRUC__\n"
595  << "#include \"simstruc.h\"\n"
596  << "#endif\n\n"
597  << "// Specific header file(s) required by the legacy code function\n"
598  << "#include \"" << this->name << ".h\"\n\n\n";
599 
600  // Codegenerate s-function
601  f << sfunction;
602 
603  // Close file(s)
604  f.close();
605  }
606 
607  std::string CodeGenerator::codegen_sfunction(const Function& f) const {
608  std::stringstream g;
609  // TODO(@jaeandersson): These helper functions really should be moved
610  // to the runtime directory
611 
612  // Initialize function
613  g << "/* Function: mdlInitializeSizes ===========================================\n"
614  << "* Abstract:\n"
615  << "* The sizes information is used by Simulink to determine the S-function\n"
616  << "* blocks characteristics (number of inputs, outputs, states, etc.).\n"
617  << "*/\n"
618  << "static void mdlInitializeSizes(SimStruct *S)\n"
619  << "{\n\n"
620  << " /* Declare auxilary variables */\n"
621  << " int_T ii;\n"
622  << " const int_T* sp;\n\n"
623  << " /* Set number of simulink s-function block parameters "
624  "(the ones which appear by double click on simulink block) */\n"
625  << " ssSetNumSFcnParams(S, 0);\n\n"
626  << " /* Report if parameter mismatch occurs */\n"
627  << " if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) return;\n\n"
628  << " /* Specify the number of states for which a block detects "
629  "zero crossings that occur between sample points */\n"
630  << " ssSetNumNonsampledZCs(S, 0);\n\n"
631  << " /* Set number of simulink input ports */\n"
632  << " if (!ssSetNumInputPorts(S, " << f->n_in_ << ")) return;\n\n"
633  << " /* Configure simulink input ports (inputs are assumed to be dense "
634  "vectors or matrices) */\n"
635  << " for (ii=0; ii<" << f->n_in_ << "; ++ii) {\n"
636  << " sp = " << f.name() << "_sparsity_in(ii);\n"
637  << " if (sp[1]==1) {\n"
638  << " ssSetInputPortWidth(S, ii, sp[0]);\n"
639  << " }\n"
640  << " else {\n"
641  << " ssSetInputPortMatrixDimensions(S, ii, sp[0], sp[1]);\n"
642  << " }\n"
643  << " ssSetInputPortDirectFeedThrough(S, ii, 1);\n"
644  << " }\n\n"
645  << " /* Set number of simulink output ports */\n"
646  << " if (!ssSetNumOutputPorts(S, " << f->n_out_ << ")) return;\n\n"
647  << " /* Configure simulink output ports (dense or sparse vectors or matrices allowed) */\n"
648  << " for (ii=0; ii<" << f->n_out_ << "; ++ii) {\n"
649  << " sp = " << f.name() << "_sparsity_out(ii);\n"
650  << " if (sp[1]==1) {\n"
651  << " ssSetOutputPortWidth(S, ii, sp[0]);\n"
652  << " }\n"
653  << " else {\n"
654  << " ssSetOutputPortMatrixDimensions(S, ii, sp[0], sp[1]);\n"
655  << " }\n"
656  << " }\n"
657  << " ssSetOutputPortOutputExprInRTW(S, 0, 0);\n\n"
658  << " /* This S-function can be used in referenced model simulating in normal mode */\n"
659  << " ssSetModelReferenceNormalModeSupport(S, MDL_START_AND_MDL_PROCESS_PARAMS_OK);\n\n"
660  << " /* Set the number of sample time */\n"
661  << " ssSetNumSampleTimes(S, 1);\n\n"
662  << " /* Set the compliance with the SimState feature */\n"
663  << " ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);\n\n"
664  << " /**\n"
665  << " * All options have the form SS_OPTION_<name> and are documented in\n"
666  << " * matlabroot/simulink/include/simstruc.h. The options should be\n"
667  << " * bitwise ord together as in\n"
668  << " * ssSetOptions(S, (SS_OPTION_name1 | SS_OPTION_name2))\n"
669  << " */\n"
670  << "}\n\n\n";
671 
672  // Initialize sample times function
673  g << "/* Function: mdlInitializeSampleTimes =====================================\n"
674  << " * Abstract:\n"
675  << " * This function is used to specify the sample time(s) for your\n"
676  << " * S-function. You must register the same number of sample times as\n"
677  << " * specified in ssSetNumSampleTimes.\n"
678  << " */\n"
679  << "static void mdlInitializeSampleTimes(SimStruct *S)\n"
680  << "{\n"
681  << " ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);\n"
682  << " ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);\n"
683  << " #if defined(ssSetModelReferenceSampleTimeDefaultInheritance)\n"
684  << " ssSetModelReferenceSampleTimeDefaultInheritance(S);\n"
685  << " #endif\n"
686  << "}\n\n\n";
687 
688  // Model output function
689  g << "/* Function: mdlOutputs ===================================================\n"
690  << " * Abstract:\n"
691  << " * In this function, you compute the outputs of your S-function\n"
692  << " * block. Generally outputs are placed in the output vector(s),\n"
693  << " * ssGetOutputPortSignal.\n"
694  << " */\n"
695  << "static void mdlOutputs(SimStruct *S, int_T tid)\n"
696  << "{\n\n"
697  << " /* Declare auxilary variables */\n"
698  << " int_T ii, jj, row, col, nnz_col, ind_start_row_index, offset = 0, jj_total = 0;\n"
699  << " const int_T* sp;\n\n"
700  << " /* Allocate buffers for casadi input and output and simulink output */\n"
701  << " " + array("real_T", "w", f->sz_w()+f->nnz_out())
702  << " " + array("int_T", "iw", f->sz_iw())
703  << " const real_T* arg[" << f->sz_arg() <<"] = {0};\n"
704  << " real_T* res[" << f->sz_res() << "] = {0};\n"
705  << " real_T* y[" << f->n_out_ << "] = {0};\n\n"
706  << " /* Point inputs directly to casadi input buffer */\n"
707  << " for (ii=0; ii<" << f->n_in_ << ";++ii) {\n"
708  << " arg[ii] = *ssGetInputPortRealSignalPtrs( S, ii );\n"
709  << " }\n\n"
710  << " /* Point outputs to buffer */\n"
711  << " for (ii=0; ii<" << f->n_out_ << ";++ii) {\n"
712  << " y[ii] = ssGetOutputPortRealSignal( S, ii );\n"
713  << " }\n\n"
714  << " /* Point allocated working array to casadi output buffer */\n";
715  for (casadi_int ii=0; ii<f->n_out_; ++ii) {
716  g << " res[" << ii << "] = w + offset;\n"
717  << " offset += " << f.nnz_out(ii) << ";\n";
718  }
719  g << " \n"
720  << " /* Call CasADi function */\n"
721  << " " << f.name() << "( arg, res, iw, w+offset, 0 );\n\n"
722  << " /* Assign results to Simulink output array */\n"
723  << " for (ii=0; ii<" << f->n_out_ << "; ++ii){\n\n"
724  << " /* Get sparsity information of casadi function output "
725  "(sp[0] - n_rows, sp[1] - n_cols, sp[2] - dense/sparse) */\n"
726  << " sp = " << f.name() << "_sparsity_out(ii);\n\n"
727  << " /* Check if output is dense (sp[2]=1) or sparse (sp[2]=0) */\n"
728  << " if (sp[2]==0) {\n"
729  << " jj_total = 0;\n"
730  << " ind_start_row_index = 2 + sp[1] + 1;\n\n"
731  << " /* Distribute nonzero elements column by column */\n"
732  << " for (col=0; col<sp[1]; col++) {\n\n"
733  << " /* The cumulative sum of nonzero elements after each column starts at index 2, "
734  "after last entry of CCS array col_ptr; number of nonzero elements in current column is "
735  "obtained by the difference of two consecutive values */\n"
736  << " nnz_col = sp[2+col+1] - sp[2+col];\n\n"
737  << " /* Distribute nonzero elements of current column to correct row position */\n"
738  << " for (jj=0; jj<nnz_col; jj++) {\n"
739  << " row = sp[ind_start_row_index+jj_total];\n"
740  << " y[ii][row + sp[0]*col] = res[ii][jj_total];\n"
741  << " jj_total++;\n"
742  << " }\n"
743  << " }\n"
744  << " }\n"
745  << " else {\n"
746  << " y[ii] = res[ii];\n"
747  << " }\n"
748  << " }\n"
749  << "}\n\n\n";
750 
751  // Model terminate function
752  g << "/* Function: mdlTerminate =================================================\n"
753  << " * Abstract:\n"
754  << " * In this function, you should perform any actions that are necessary\n"
755  << " * at the termination of a simulation.\n"
756  << " */\n"
757  << "static void mdlTerminate(SimStruct *S)\n"
758  << "{\n"
759  << "}\n\n\n"
760  << "/* Required S-function trailer */\n"
761  << "#ifdef MATLAB_MEX_FILE\n"
762  << "# include \"simulink.c\"\n"
763  << "#else\n"
764  << "# include \"cg_sfun.h\"\n"
765  << "#endif";
766 
767  return g.str();
768  }
769 
770  void CodeGenerator::generate_main(std::ostream &s) const {
771  s << this->dll_export << "int main(int argc, char* argv[]) {\n";
772 
773  // Create switch
774  s << " if (argc<2) {\n"
775  << " /* name error */\n";
776  for (casadi_int i=0; i<exposed_fname.size(); ++i) {
777  s << " } else if (strcmp(argv[1], \"" << exposed_fname[i] << "\")==0) {\n"
778  << " return main_" << exposed_fname[i] << "(argc-2, argv+2);\n";
779  }
780  s << " }\n";
781 
782  // Error
783  s << " fprintf(stderr, \"First input should be a command string. Possible values:";
784  for (casadi_int i=0; i<exposed_fname.size(); ++i) {
785  s << " '" << exposed_fname[i] << "'";
786  }
787  s << "\\n";
788  s << "Note: you may use function.generate_input to create a command string.";
789  s << "\\n\");\n";
790 
791  // End main
792  s << " return 1;\n"
793  << "}\n";
794  }
795 
796  void CodeGenerator::define_rom_double(const void* id, casadi_int size) {
797  auto it = file_scope_double_.find(id);
798  casadi_assert(it==file_scope_double_.end(), "Already defined.");
799  shorthand("rd" + str(file_scope_double_.size()));
800  file_scope_double_[id] = size;
801  }
802 
803  std::string CodeGenerator::rom_double(const void* id) const {
804  auto it = file_scope_double_.find(id);
805  casadi_assert(it!=file_scope_double_.end(), "Not defined.");
806  casadi_int size = std::distance(file_scope_double_.begin(), it);
807  return "casadi_rd" + str(size);
808  }
809 
810  void CodeGenerator::define_rom_integer(const void* id, casadi_int size) {
811  auto it = file_scope_double_.find(id);
812  casadi_assert(it==file_scope_double_.end(), "Already defined.");
813  shorthand("ri" + str(file_scope_double_.size()));
814  file_scope_double_[id] = size;
815  }
816 
817  std::string CodeGenerator::rom_integer(const void* id) const {
818  auto it = file_scope_double_.find(id);
819  casadi_assert(it!=file_scope_double_.end(), "Not defined.");
820  casadi_int size = std::distance(file_scope_double_.begin(), it);
821  return "casadi_ri" + str(size);
822  }
823 
824  void CodeGenerator::define_pool_double(const std::string& name, const std::vector<double>& def) {
825  auto it = pool_double_.find(name);
826  if (it==pool_double_.end()) {
827  casadi_int index = pool_double_defaults_.size();
828  pool_double_defaults_.push_back(def);
829  shorthand("pd" + str(index));
830  pool_double_[name] = index;
831  } else {
832  casadi_assert_dev(def==pool_double_defaults_[it->second]);
833  }
834  }
835 
836  std::string CodeGenerator::pool_double(const std::string& name) const {
837  auto it = pool_double_.find(name);
838  casadi_assert(it!=pool_double_.end(), "Not defined.");
839  return "casadi_pd" + str(it->second);
840  }
841 
842  void CodeGenerator::dump(std::ostream& s) {
843  // Consistency check
844  casadi_assert_dev(current_indent_ == 0);
845 
846  // Prefix internal symbols to avoid symbol collisions
847  s << "/* How to prefix internal symbols */\n"
848  << "#ifdef CASADI_CODEGEN_PREFIX\n"
849  << " #define CASADI_NAMESPACE_CONCAT(NS, ID) _CASADI_NAMESPACE_CONCAT(NS, ID)\n"
850  << " #define _CASADI_NAMESPACE_CONCAT(NS, ID) NS ## ID\n"
851  << " #define CASADI_PREFIX(ID) CASADI_NAMESPACE_CONCAT(CODEGEN_PREFIX, ID)\n"
852  << "#else\n"
853  << " #define CASADI_PREFIX(ID) " << this->prefix << "_ ## ID\n"
854  << "#endif\n\n";
855 
856  s << this->includes.str();
857  s << std::endl;
858 
859  // Numeric types after includes: may depend on them. e.g. mex type
860  // Real type (usually double)
861  generate_casadi_real(s);
862 
863  // Integer type (usually long long)
864  generate_casadi_int(s);
865 
866  if (needs_mem_) {
867  s << "#ifndef CASADI_MAX_NUM_THREADS\n";
868  s << "#define CASADI_MAX_NUM_THREADS 1\n";
869  s << "#endif\n\n";
870  }
871 
872  // casadi/mem after numeric types to define derived types
873  // Memory struct entry point
874  if (this->with_mem) {
875  s << "#include <casadi/mem.h>\n" << std::endl;
876  }
877 
878  // Macros
879  if (!added_shorthands_.empty()) {
880  s << "/* Add prefix to internal symbols */\n";
881  for (auto&& i : added_shorthands_) {
882  s << "#define " << "casadi_" << i << " CASADI_PREFIX(" << i << ")\n";
883  }
884  s << std::endl;
885  }
886 
887  if (this->with_export) generate_export_symbol(s);
888 
889  // Check if inf/nan is needed
890  for (const auto& d : double_constants_) {
891  for (double e : d) {
892  if (isinf(e)) add_auxiliary(AUX_INF);
893  if (isnan(e)) add_auxiliary(AUX_NAN);
894  }
895  }
896 
897  // Codegen auxiliary functions
898  s << this->auxiliaries.str();
899 
900  // Print integer constants
901  if (!integer_constants_.empty()) {
902  for (casadi_int i=0; i<integer_constants_.size(); ++i) {
903  print_vector(s, "casadi_s" + str(i), integer_constants_[i]);
904  }
905  s << std::endl;
906  }
907 
908  // Print double constants
909  if (!double_constants_.empty()) {
910  for (casadi_int i=0; i<double_constants_.size(); ++i) {
911  print_vector(s, "casadi_c" + str(i), double_constants_[i]);
912  }
913  s << std::endl;
914  }
915 
916  // Print char constants
917  if (!char_constants_.empty()) {
918  for (casadi_int i=0; i<char_constants_.size(); ++i) {
919  print_vector(s, "casadi_b" + str(i), char_constants_[i]);
920  }
921  s << std::endl;
922  }
923 
924  // Print string constants
925  if (!string_constants_.empty()) {
926  for (casadi_int i=0; i<string_constants_.size(); ++i) {
927  print_vector(s, "casadi_a" + str(i), string_constants_[i]);
928  }
929  s << std::endl;
930  }
931 
932  if (sz_zeros_) {
933  std::vector<double> sz_zeros(sz_zeros_, 0);
934  print_vector(s, "casadi_zeros", std::vector<double>(sz_zeros));
935  s << std::endl;
936  }
937 
938  if (sz_ones_) {
939  std::vector<double> sz_ones(sz_ones_, 0);
940  print_vector(s, "casadi_ones", std::vector<double>(sz_ones));
941  s << std::endl;
942  }
943 
944  // Print file scope double work
945  if (!file_scope_double_.empty()) {
946  casadi_int i=0;
947  for (const auto& it : file_scope_double_) {
948  s << "static casadi_real casadi_rd" + str(i++) + "[" + str(it.second) + "];\n";
949  }
950  s << std::endl;
951  }
952 
953  // Print file scope integer work
954  if (!file_scope_integer_.empty()) {
955  casadi_int i=0;
956  for (const auto& it : file_scope_integer_) {
957  s << "static casadi_real casadi_ri" + str(i++) + "[" + str(it.second) + "];\n";
958  }
959  s << std::endl;
960  }
961 
962  // Print file scope double pool
963  if (!pool_double_.empty()) {
964  casadi_int i=0;
965  for (const auto& v : pool_double_defaults_) {
966  s << "casadi_real casadi_pd" + str(i) +
967  "[" + str(v.size()) + "] = " + initializer(v) + ";\n";
968  i++;
969  }
970  s << std::endl;
971  }
972 
973  // External function declarations
974  if (!added_externals_.empty()) {
975  s << "/* External functions */\n";
976  for (auto&& i : added_externals_) {
977  s << i << std::endl;
978  }
979  s << std::endl << std::endl;
980  }
981 
982  // Codegen body
983  s << this->body.str();
984 
985  // End with new line
986  s << std::endl;
987  }
988 
989  std::string CodeGenerator::work(casadi_int n, casadi_int sz, bool is_ref) const {
990  if (is_ref) {
991  return "wr" + format_padded(n);
992  }
993  if (n<0 || sz==0) {
994  return "0";
995  } else if (sz==1 && !this->codegen_scalars) {
996  return "(&w" + format_padded(n) + ")";
997  } else {
998  return "w" + format_padded(n);
999  }
1000  }
1001 
1002  std::string CodeGenerator::workel(casadi_int n) const {
1003  if (n<0) return "0";
1004  std::stringstream s;
1005  if (this->codegen_scalars) s << "*";
1006  s << "w" << format_padded(n);
1007  return s.str();
1008  }
1009 
1010  void CodeGenerator::reserve_work(casadi_int n) {
1011  if (n == 0) {
1012  padding_length_ = 1;
1013  } else {
1014  // Zero based counting. The 10th entry is to be rendered as '9'
1015  padding_length_ = str(n-1).length();
1016  }
1017  }
1018 
1019  std::string CodeGenerator::format_padded(casadi_int i) const {
1020  std::stringstream ss;
1021  ss.str("");
1022  ss << std::setw(padding_length_) << std::setfill('0') << i;
1023  return ss.str();
1024  }
1025 
1026  std::string CodeGenerator::array(const std::string& type, const std::string& name, casadi_int len,
1027  const std::string& def) {
1028  std::stringstream s;
1029  s << type << " ";
1030  if (len==0) {
1031  s << "*" << name << " = 0";
1032  } else {
1033  s << name << "[" << len << "]";
1034  if (!def.empty()) s << " = " << def;
1035  }
1036  s << ";\n";
1037  return s.str();
1038  }
1039 
1040  void CodeGenerator::print_vector(std::ostream &s, const std::string& name,
1041  const std::vector<casadi_int>& v) {
1042  s << array("static const casadi_int", name, v.size(), initializer(v));
1043  }
1044 
1045  void CodeGenerator::print_vector(std::ostream &s, const std::string& name,
1046  const std::vector<char>& v) {
1047  s << array("static const char", name, v.size(), initializer(v));
1048  }
1049 
1050  void CodeGenerator::print_vector(std::ostream &s, const std::string& name,
1051  const std::vector<double>& v) {
1052  s << array("static const casadi_real", name, v.size(), initializer(v));
1053  }
1054 
1055  void CodeGenerator::print_vector(std::ostream &s, const std::string& name,
1056  const std::vector<std::string>& v) {
1057  s << array("static const char*", name, v.size(), initializer(v));
1058  }
1059 
1060  std::string CodeGenerator::print_op(casadi_int op, const std::string& a0) {
1061  switch (op) {
1062  case OP_FABS:
1064  return "casadi_fabs("+a0+")";
1065  case OP_SQ:
1067  return "casadi_sq("+a0+")";
1068  case OP_SIGN:
1070  return "casadi_sign("+a0+")";
1071  case OP_LOG1P:
1073  return "casadi_log1p("+a0+")";
1074  case OP_EXPM1:
1076  return "casadi_expm1("+a0+")";
1077  default:
1078  return casadi_math<double>::print(op, a0);
1079  }
1080  }
1081  std::string CodeGenerator::print_op(casadi_int op, const std::string& a0, const std::string& a1) {
1082  switch (op) {
1083  case OP_FMIN:
1085  return "casadi_fmin("+a0+","+a1+")";
1086  case OP_FMAX:
1088  return "casadi_fmax("+a0+","+a1+")";
1089  case OP_HYPOT:
1091  return "casadi_hypot("+a0+","+a1+")";
1092  case OP_PRINTME:
1094  return "casadi_printme("+a0+","+a1+")";
1095  default:
1096  return casadi_math<double>::print(op, a0, a1);
1097  }
1098  }
1099 
1100  void CodeGenerator::add_include(const std::string& new_include, bool relative_path,
1101  const std::string& use_ifdef) {
1102  // Register the new element
1103  bool added = added_includes_.insert(new_include).second;
1104 
1105  // Quick return if it already exists
1106  if (!added) return;
1107 
1108  // Ifdef opening
1109  if (!use_ifdef.empty()) this->includes << "#ifdef " << use_ifdef << std::endl;
1110 
1111  // Print to the header section
1112  if (relative_path) {
1113  this->includes << "#include \"" << new_include << "\"\n";
1114  } else {
1115  this->includes << "#include <" << new_include << ">\n";
1116  }
1117 
1118  // Ifdef closing
1119  if (!use_ifdef.empty()) this->includes << "#endif\n";
1120  }
1121 
1122  void CodeGenerator::setup_callback(const std::string& s, const Function& f) {
1123  std::string name = add_dependency(f);
1124  bool needs_mem = !f->codegen_mem_type().empty();
1125  if (needs_mem) {
1126  *this << s << ".checkout = " << name << "_checkout;\n";
1127  } else {
1128  *this << s << ".checkout = 0;\n";
1129  }
1130 
1131  *this << s << ".eval = " << name << ";\n";
1132  if (needs_mem) {
1133  *this << s << ".release = " << name << "_release;\n";
1134  } else {
1135  *this << s << ".release = 0;\n";
1136  }
1137  }
1138 
1139  std::string CodeGenerator::
1140  operator()(const Function& f, const std::string& arg,
1141  const std::string& res, const std::string& iw,
1142  const std::string& w, const std::string& failure_ret) {
1143  std::string name = add_dependency(f);
1144  bool needs_mem = !f->codegen_mem_type().empty();
1145  if (needs_mem) {
1146  std::string mem = "mid";
1147  local("flag", "int");
1148  local(mem, "int");
1149  *this << mem << " = " << name << "_checkout();\n";
1150  *this << "if (" << mem << "<0) return " << failure_ret << ";\n";
1151  *this << "flag = " + name + "(" + arg + ", " + res + ", "
1152  + iw + ", " + w + ", " << mem << ");\n";
1153  *this << name << "_release(" << mem << ");\n";
1154  return "flag";
1155  } else {
1156  return name + "(" + arg + ", " + res + ", "
1157  + iw + ", " + w + ", 0)";
1158  }
1159  }
1160 
1161  void CodeGenerator::add_external(const std::string& new_external) {
1162  added_externals_.insert(new_external);
1163  }
1164 
1165  std::string CodeGenerator::shorthand(const std::string& name) const {
1166  casadi_assert(added_shorthands_.count(name), "No such macro: " + name);
1167  return "casadi_" + name;
1168  }
1169 
1170  std::string CodeGenerator::shorthand(const std::string& name, bool allow_adding) {
1171  bool added = added_shorthands_.insert(name).second;
1172  if (!allow_adding) {
1173  casadi_assert(added, "Duplicate macro: " + name);
1174  }
1175  return "casadi_" + name;
1176  }
1177 
1178  casadi_int CodeGenerator::add_sparsity(const Sparsity& sp, bool canonical) {
1179  return get_constant(sp.compress(canonical), true);
1180  }
1181 
1182  std::string CodeGenerator::sparsity(const Sparsity& sp, bool canonical) {
1183  return shorthand("s" + str(add_sparsity(sp, canonical)));
1184  }
1185 
1186  casadi_int CodeGenerator::get_sparsity(const Sparsity& sp) const {
1187  return const_cast<CodeGenerator&>(*this).get_constant(sp, false);
1188  }
1189 
1190  size_t CodeGenerator::hash(const std::vector<double>& v) {
1191  // Calculate a hash value for the vector
1192  std::size_t seed=0;
1193  if (!v.empty()) {
1194  casadi_assert_dev(sizeof(double) % sizeof(size_t)==0);
1195  const casadi_int int_len = v.size()*(sizeof(double)/sizeof(size_t));
1196  const size_t* int_v = reinterpret_cast<const size_t*>(&v.front());
1197  for (size_t i=0; i<int_len; ++i) {
1198  hash_combine(seed, int_v[i]);
1199  }
1200  }
1201  return seed;
1202  }
1203 
1204  size_t CodeGenerator::hash(const std::vector<casadi_int>& v) {
1205  size_t seed=0;
1206  hash_combine(seed, v);
1207  return seed;
1208  }
1209 
1210  size_t CodeGenerator::hash(const std::vector<char>& v) {
1211  size_t seed=0;
1212  hash_combine(seed, v);
1213  return seed;
1214  }
1215 
1216  size_t CodeGenerator::hash(const std::vector<std::string>& v) {
1217  size_t seed=0;
1218  hash_combine(seed, v);
1219  return seed;
1220  }
1221 
1222  casadi_int CodeGenerator::get_constant(const std::vector<double>& v, bool allow_adding) {
1223  // Hash the vector
1224  size_t h = hash(v);
1225 
1226  // Try to locate it in already added constants
1227  auto eq = added_double_constants_.equal_range(h);
1228  for (auto i=eq.first; i!=eq.second; ++i) {
1229  if (equal(v, double_constants_[i->second])) return i->second;
1230  }
1231 
1232  if (allow_adding) {
1233  // Add to constants
1234  casadi_int ind = double_constants_.size();
1235  double_constants_.push_back(v);
1236  added_double_constants_.insert(std::make_pair(h, ind));
1237  return ind;
1238  } else {
1239  casadi_error("Constant not found");
1240  return -1;
1241  }
1242  }
1243 
1244  casadi_int CodeGenerator::get_constant(const std::vector<casadi_int>& v, bool allow_adding) {
1245  // Hash the vector
1246  size_t h = hash(v);
1247 
1248  // Try to locate it in already added constants
1249  std::pair<std::multimap<size_t, size_t>::iterator, std::multimap<size_t, size_t>::iterator> eq =
1250  added_integer_constants_.equal_range(h);
1251  for (std::multimap<size_t, size_t>::iterator i=eq.first; i!=eq.second; ++i) {
1252  if (equal(v, integer_constants_[i->second])) return i->second;
1253  }
1254 
1255  if (allow_adding) {
1256  // Add to constants
1257  casadi_int ind = integer_constants_.size();
1258  integer_constants_.push_back(v);
1259  added_integer_constants_.insert(std::pair<size_t, size_t>(h, ind));
1260  return ind;
1261  } else {
1262  casadi_error("Constant not found");
1263  return -1;
1264  }
1265  }
1266 
1267  casadi_int CodeGenerator::get_constant(const std::vector<char>& v, bool allow_adding) {
1268  // Hash the vector
1269  size_t h = hash(v);
1270 
1271  // Try to locate it in already added constants
1272  std::pair<std::multimap<size_t, size_t>::iterator, std::multimap<size_t, size_t>::iterator> eq =
1273  added_char_constants_.equal_range(h);
1274  for (std::multimap<size_t, size_t>::iterator i=eq.first; i!=eq.second; ++i) {
1275  if (equal(v, char_constants_[i->second])) return i->second;
1276  }
1277 
1278  if (allow_adding) {
1279  // Add to constants
1280  casadi_int ind = char_constants_.size();
1281  char_constants_.push_back(v);
1282  added_char_constants_.insert(std::pair<size_t, size_t>(h, ind));
1283  return ind;
1284  } else {
1285  casadi_error("Constant not found");
1286  return -1;
1287  }
1288  }
1289 
1290  casadi_int CodeGenerator::get_constant(const std::vector<std::string>& v, bool allow_adding) {
1291  // Hash the vector
1292  size_t h = hash(v);
1293 
1294  // Try to locate it in already added constants
1295  std::pair<std::multimap<size_t, size_t>::iterator, std::multimap<size_t, size_t>::iterator> eq =
1296  added_string_constants_.equal_range(h);
1297  for (std::multimap<size_t, size_t>::iterator i=eq.first; i!=eq.second; ++i) {
1298  if (equal(v, string_constants_[i->second])) return i->second;
1299  }
1300 
1301  if (allow_adding) {
1302  // Add to constants
1303  casadi_int ind = string_constants_.size();
1304  string_constants_.push_back(v);
1305  added_string_constants_.insert(std::pair<size_t, size_t>(h, ind));
1306  return ind;
1307  } else {
1308  casadi_error("Constant not found");
1309  return -1;
1310  }
1311  }
1312 
1313  std::string CodeGenerator::constant(const std::vector<casadi_int>& v) {
1314  return shorthand("s" + str(get_constant(v, true)));
1315  }
1316 
1317  std::string CodeGenerator::constant(const std::vector<char>& v) {
1318  return shorthand("b" + str(get_constant(v, true)));
1319  }
1320 
1321  std::string CodeGenerator::constant(const std::vector<std::string>& v) {
1322  return shorthand("a" + str(get_constant(v, true)));
1323  }
1324 
1325  std::string CodeGenerator::zeros(casadi_int sz) {
1326  sz_zeros_ = std::max(sz_zeros_, sz);
1327  return shorthand("zeros");
1328  }
1329 
1330  std::string CodeGenerator::ones(casadi_int sz) {
1331  sz_ones_ = std::max(sz_ones_, sz);
1332  return shorthand("ones");
1333  }
1334 
1336  const std::string& name, const std::vector<casadi_int>& v, const std::string& type) {
1337  std::string ref = constant(v);
1338  if (!v.empty()) {
1339  local(name+"[" + str(v.size()) + "]", type);
1340  } else {
1341  local(name, type, "*");
1342  }
1343  if (!v.empty()) {
1344  local("i", type);
1345  (*this) << "for (i=0;i<" << v.size() << ";++i) " + name + "[i] = " + ref + "[i];\n";
1346  } else {
1347  init_local(name, "0");
1348  }
1349  }
1350 
1351  std::string CodeGenerator::constant(const std::vector<double>& v) {
1352  return shorthand("c" + str(get_constant(v, true)));
1353  }
1354 
1355  void CodeGenerator::add_auxiliary(Auxiliary f, const std::vector<std::string>& inst) {
1356  // Look for existing instantiations
1357  auto f_match = added_auxiliaries_.equal_range(f);
1358  // Look for duplicates
1359  for (auto it=f_match.first; it!=f_match.second; ++it) {
1360  if (it->second==inst) return;
1361  }
1362  added_auxiliaries_.insert(std::make_pair(f, inst));
1363 
1364  // Add the appropriate function
1365  switch (f) {
1366  case AUX_COPY:
1367  this->auxiliaries << sanitize_source(casadi_copy_str, inst);
1368  break;
1369  case AUX_SCALED_COPY:
1370  this->auxiliaries << sanitize_source(casadi_scaled_copy_str, inst);
1371  break;
1372  case AUX_SWAP:
1373  this->auxiliaries << sanitize_source(casadi_swap_str, inst);
1374  break;
1375  case AUX_SCAL:
1376  this->auxiliaries << sanitize_source(casadi_scal_str, inst);
1377  break;
1378  case AUX_AXPY:
1379  this->auxiliaries << sanitize_source(casadi_axpy_str, inst);
1380  break;
1381  case AUX_DOT:
1382  this->auxiliaries << sanitize_source(casadi_dot_str, inst);
1383  break;
1384  case AUX_BILIN:
1385  this->auxiliaries << sanitize_source(casadi_bilin_str, inst);
1386  break;
1387  case AUX_RANK1:
1388  this->auxiliaries << sanitize_source(casadi_rank1_str, inst);
1389  break;
1390  case AUX_IAMAX:
1391  this->auxiliaries << sanitize_source(casadi_iamax_str, inst);
1392  break;
1393  case AUX_INTERPN:
1396  add_auxiliary(AUX_FLIP, {});
1398  add_auxiliary(AUX_CLEAR, {"casadi_int"});
1399  this->auxiliaries << sanitize_source(casadi_interpn_str, inst);
1400  break;
1401  case AUX_INTERPN_GRAD:
1403  this->auxiliaries << sanitize_source(casadi_interpn_grad_str, inst);
1404  break;
1405  case AUX_DE_BOOR:
1406  this->auxiliaries << sanitize_source(casadi_de_boor_str, inst);
1407  break;
1408  case AUX_ND_BOOR_EVAL:
1411  add_auxiliary(AUX_FILL, {"casadi_int"});
1413  add_auxiliary(AUX_CLEAR, {"casadi_int"});
1415  this->auxiliaries << sanitize_source(casadi_nd_boor_eval_str, inst);
1416  break;
1417  case AUX_FLIP:
1418  this->auxiliaries << sanitize_source(casadi_flip_str, inst);
1419  break;
1420  case AUX_LOW:
1421  this->auxiliaries << sanitize_source(casadi_low_str, inst);
1422  break;
1423  case AUX_INTERPN_WEIGHTS:
1425  this->auxiliaries << sanitize_source(casadi_interpn_weights_str, inst);
1426  break;
1428  this->auxiliaries << sanitize_source(casadi_interpn_interpolate_str, inst);
1429  break;
1430  case AUX_NORM_1:
1431  this->auxiliaries << sanitize_source(casadi_norm_1_str, inst);
1432  break;
1433  case AUX_NORM_2:
1435  this->auxiliaries << sanitize_source(casadi_norm_2_str, inst);
1436  break;
1437  case AUX_NORM_INF:
1439  this->auxiliaries << sanitize_source(casadi_norm_inf_str, inst);
1440  break;
1441  case AUX_VECTOR_FMAX:
1443  this->auxiliaries << sanitize_source(casadi_vector_fmax_str, inst);
1444  break;
1445  case AUX_VECTOR_FMIN:
1447  this->auxiliaries << sanitize_source(casadi_vector_fmin_str, inst);
1448  break;
1449  case AUX_MASKED_NORM_INF:
1451  this->auxiliaries << sanitize_source(casadi_masked_norm_inf_str, inst);
1452  break;
1453  case AUX_CLIP_MIN:
1454  // add_auxiliary(AUX_CLIP_MIN);
1455  this->auxiliaries << sanitize_source(casadi_clip_min_str, inst);
1456  break;
1457  case AUX_CLIP_MAX:
1458  // add_auxiliary(AUX_CLIP_MAX);
1459  this->auxiliaries << sanitize_source(casadi_clip_max_str, inst);
1460  break;
1461  case AUX_CLEAR:
1462  this->auxiliaries << sanitize_source(casadi_clear_str, inst);
1463  break;
1464  case AUX_FILL:
1465  this->auxiliaries << sanitize_source(casadi_fill_str, inst);
1466  break;
1467  case AUX_MV:
1468  this->auxiliaries << sanitize_source(casadi_mv_str, inst);
1469  break;
1470  case AUX_MV_DENSE:
1471  this->auxiliaries << sanitize_source(casadi_mv_dense_str, inst);
1472  break;
1473  case AUX_MTIMES:
1474  this->auxiliaries << sanitize_source(casadi_mtimes_str, inst);
1475  break;
1476  case AUX_TRILSOLVE:
1477  this->auxiliaries << sanitize_source(casadi_trilsolve_str, inst);
1478  break;
1479  case AUX_TRIUSOLVE:
1480  this->auxiliaries << sanitize_source(casadi_triusolve_str, inst);
1481  break;
1482  case AUX_PROJECT:
1483  this->auxiliaries << sanitize_source(casadi_project_str, inst);
1484  break;
1485  case AUX_TRI_PROJECT:
1486  this->auxiliaries << sanitize_source(casadi_tri_project_str, inst);
1487  break;
1488  case AUX_DENSIFY:
1491  {
1492  std::vector<std::string> inst2 = inst;
1493  if (inst.size()==1) inst2.push_back(inst[0]);
1494  this->auxiliaries << sanitize_source(casadi_densify_str, inst2);
1495  }
1496  break;
1497  case AUX_SPARSIFY:
1499  {
1500  std::vector<std::string> inst2 = inst;
1501  if (inst.size()==1) inst2.push_back(inst[0]);
1502  this->auxiliaries << sanitize_source(casadi_sparsify_str, inst2);
1503  }
1504  break;
1505  case AUX_TRANS:
1506  this->auxiliaries << sanitize_source(casadi_trans_str, inst);
1507  break;
1508  case AUX_TO_MEX:
1510  this->auxiliaries << "#ifdef MATLAB_MEX_FILE\n"
1511  << sanitize_source(casadi_to_mex_str, inst)
1512  << "#endif\n\n";
1513  break;
1514  case AUX_FROM_MEX:
1516  this->auxiliaries << "#ifdef MATLAB_MEX_FILE\n"
1517  << sanitize_source(casadi_from_mex_str, inst)
1518  << "#endif\n\n";
1519  break;
1520  case AUX_FINITE_DIFF:
1522  this->auxiliaries << sanitize_source(casadi_finite_diff_str, inst);
1523  break;
1524  case AUX_QR:
1529  this->auxiliaries << sanitize_source(casadi_qr_str, inst);
1530  break;
1531  case AUX_LSQR:
1538  this->auxiliaries << sanitize_source(casadi_lsqr_str, inst);
1539  break;
1540  case AUX_QP:
1541  this->auxiliaries << sanitize_source(casadi_qp_str, inst);
1542  break;
1543  case AUX_QRQP:
1557  add_include("stdarg.h");
1558  add_include("stdio.h");
1559  add_include("math.h");
1560 
1561  this->auxiliaries << sanitize_source(casadi_qrqp_str, inst);
1562  break;
1563  case AUX_NLP:
1565  this->auxiliaries << sanitize_source(casadi_nlp_str, inst);
1566  break;
1567  case AUX_SQPMETHOD:
1572  this->auxiliaries << sanitize_source(casadi_sqpmethod_str, inst);
1573  break;
1574  case AUX_FEASIBLESQPMETHOD:
1577  this->auxiliaries << sanitize_source(casadi_feasiblesqpmethod_str, inst);
1578  break;
1579  case AUX_LDL:
1580  this->auxiliaries << sanitize_source(casadi_ldl_str, inst);
1581  break;
1582  case AUX_NEWTON:
1587  this->auxiliaries << sanitize_source(casadi_newton_str, inst);
1588  break;
1589  case AUX_MAX_VIOL:
1591  this->auxiliaries << sanitize_source(casadi_max_viol_str, inst);
1592  break;
1593  case AUX_SUM_VIOL:
1594  this->auxiliaries << sanitize_source(casadi_sum_viol_str, inst);
1595  break;
1596  case AUX_SUM:
1597  this->auxiliaries << sanitize_source(casadi_sum_str, inst);
1598  break;
1599  case AUX_VFMIN:
1601  this->auxiliaries << sanitize_source(casadi_vfmin_str, inst);
1602  break;
1603  case AUX_VFMAX:
1605  this->auxiliaries << sanitize_source(casadi_vfmax_str, inst);
1606  break;
1607  case AUX_REGULARIZE:
1610  this->auxiliaries << sanitize_source(casadi_regularize_str, inst);
1611  break;
1616  this->auxiliaries << sanitize_source(casadi_bound_consistency_str, inst);
1617  break;
1618  case AUX_FILE_SLURP:
1619  add_include("stdio.h");
1620  this->auxiliaries << sanitize_source(casadi_file_slurp_str, inst);
1621  break;
1622  case AUX_CACHE:
1623  this->auxiliaries << sanitize_source(casadi_cache_str, inst);
1624  break;
1625  case AUX_CVX:
1632  this->auxiliaries << sanitize_source(casadi_cvx_str, inst);
1633  break;
1634  case AUX_CONVEXIFY:
1639  this->auxiliaries << sanitize_source(casadi_convexify_str, inst);
1640  break;
1641  case AUX_LOGSUMEXP:
1644  this->auxiliaries << sanitize_source(casadi_logsumexp_str, inst);
1645  break;
1646  case AUX_SPARSITY:
1647  this->auxiliaries << sanitize_source(casadi_sparsity_str, inst);
1648  break;
1649  case AUX_BFGS:
1658  this->auxiliaries << sanitize_source(casadi_bfgs_str, inst);
1659  break;
1660  case AUX_ORACLE:
1661  this->auxiliaries << sanitize_source(casadi_oracle_str, inst);
1662  break;
1663  case AUX_ORACLE_CALLBACK:
1664  this->auxiliaries << sanitize_source(casadi_oracle_callback_str, inst);
1665  break;
1666  case AUX_OCP_BLOCK:
1667  this->auxiliaries << sanitize_source(casadi_ocp_block_str, inst);
1668  break;
1669  case AUX_TO_DOUBLE:
1670  this->auxiliaries << "#define casadi_to_double(x) "
1671  << "(" << (this->cpp ? "static_cast<double>(x)" : "(double) x") << ")\n\n";
1672  break;
1673  case AUX_TO_INT:
1674  this->auxiliaries << "#define casadi_to_int(x) "
1675  << "(" << (this->cpp ? "static_cast<casadi_int>(x)" : "(casadi_int) x")
1676  << ")\n\n";
1677  break;
1678  case AUX_CAST:
1679  this->auxiliaries << "#define CASADI_CAST(x,y) "
1680  << "(" << (this->cpp ? "static_cast<x>(y)" : "(x) y") << ")\n\n";
1681  break;
1682  case AUX_SQ:
1683  shorthand("sq");
1684  this->auxiliaries << "casadi_real casadi_sq(casadi_real x) { return x*x;}\n\n";
1685  break;
1686  case AUX_SIGN:
1687  shorthand("sign");
1688  this->auxiliaries << "casadi_real casadi_sign(casadi_real x) "
1689  << "{ return x<0 ? -1 : x>0 ? 1 : x;}\n\n";
1690  break;
1691  case AUX_IF_ELSE:
1692  shorthand("if_else");
1693  this->auxiliaries << "casadi_real casadi_if_else"
1694  << "(casadi_real c, casadi_real x, casadi_real y) "
1695  << "{ return c!=0 ? x : y;}\n\n";
1696  break;
1697  case AUX_PRINTF:
1698  this->auxiliaries << "#ifndef CASADI_PRINTF\n";
1699  if (this->mex) {
1700  this->auxiliaries << "#ifdef MATLAB_MEX_FILE\n"
1701  << " #define CASADI_PRINTF mexPrintf\n"
1702  << "#else\n"
1703  << " #define CASADI_PRINTF printf\n"
1704  << "#endif\n";
1705  } else {
1706  add_include("stdio.h");
1707  this->auxiliaries << "#define CASADI_PRINTF printf\n";
1708  this->auxiliaries << "#ifndef CASADI_SNPRINTF\n";
1709  this->auxiliaries << "#define CASADI_SNPRINTF snprintf\n";
1710  this->auxiliaries << "#endif\n\n";
1711  }
1712  this->auxiliaries << "#endif\n\n";
1713  break;
1714  case AUX_FMIN:
1715  shorthand("fmin");
1716  this->auxiliaries << "casadi_real casadi_fmin(casadi_real x, casadi_real y) {\n"
1717  << "/* Pre-c99 compatibility */\n"
1718  << "#if __STDC_VERSION__ < 199901L\n"
1719  << " return x<y ? x : y;\n"
1720  << "#else\n"
1721  << " return fmin(x, y);\n"
1722  << "#endif\n"
1723  << "}\n\n";
1724  break;
1725  case AUX_FMAX:
1726  shorthand("fmax");
1727  this->auxiliaries << "casadi_real casadi_fmax(casadi_real x, casadi_real y) {\n"
1728  << "/* Pre-c99 compatibility */\n"
1729  << "#if __STDC_VERSION__ < 199901L\n"
1730  << " return x>y ? x : y;\n"
1731  << "#else\n"
1732  << " return fmax(x, y);\n"
1733  << "#endif\n"
1734  << "}\n\n";
1735  break;
1736  case AUX_FABS:
1737  shorthand("fabs");
1738  this->auxiliaries << "casadi_real casadi_fabs(casadi_real x) {\n"
1739  << "/* Pre-c99 compatibility */\n"
1740  << "#if __STDC_VERSION__ < 199901L\n"
1741  << " return x>0 ? x : -x;\n"
1742  << "#else\n"
1743  << " return fabs(x);\n"
1744  << "#endif\n"
1745  << "}\n\n";
1746  break;
1747  case AUX_ISINF:
1748  shorthand("isinf");
1749  this->auxiliaries << "casadi_real casadi_isinf(casadi_real x) {\n"
1750  << "/* Pre-c99 compatibility */\n"
1751  << "#if __STDC_VERSION__ < 199901L\n"
1752  << " return x== INFINITY || x==-INFINITY;\n"
1753  << "#else\n"
1754  << " return isinf(x);\n"
1755  << "#endif\n"
1756  << "}\n\n";
1757  break;
1758  case AUX_MIN:
1759  this->auxiliaries << "casadi_int casadi_min(casadi_int x, casadi_int y) {\n"
1760  << " return x>y ? y : x;\n"
1761  << "}\n\n";
1762  break;
1763  case AUX_MAX:
1764  this->auxiliaries << "casadi_int casadi_max(casadi_int x, casadi_int y) {\n"
1765  << " return x>y ? x : y;\n"
1766  << "}\n\n";
1767  break;
1768  case AUX_MMIN:
1771  this->auxiliaries << sanitize_source(casadi_mmin_str, inst);
1772  break;
1773  case AUX_MMAX:
1776  this->auxiliaries << sanitize_source(casadi_mmax_str, inst);
1777  break;
1778  case AUX_INF:
1779  this->auxiliaries << "#ifndef casadi_inf\n"
1780  << " #define casadi_inf " << this->infinity << "\n"
1781  << "#endif\n\n";
1782  break;
1783  case AUX_NAN:
1784  this->auxiliaries << "#ifndef casadi_nan\n"
1785  << " #define casadi_nan " << this->nan << "\n"
1786  << "#endif\n\n";
1787  break;
1788  case AUX_REAL_MIN:
1789  this->auxiliaries << "#ifndef casadi_real_min\n"
1790  << " #define casadi_real_min " << this->real_min << "\n"
1791  << "#endif\n\n";
1792  break;
1793  case AUX_LOG1P:
1794  shorthand("log1p");
1795  this->auxiliaries << "casadi_real casadi_log1p(casadi_real x) {\n"
1796  << "/* Pre-c99 compatibility */\n"
1797  << "#if __STDC_VERSION__ < 199901L\n"
1798  << " return log(1+x);\n"
1799  << "#else\n"
1800  << " return log1p(x);\n"
1801  << "#endif\n"
1802  << "}\n\n";
1803  break;
1804  case AUX_EXPM1:
1805  shorthand("expm1");
1806  this->auxiliaries << "casadi_real casadi_expm1(casadi_real x) {\n"
1807  << "/* Pre-c99 compatibility */\n"
1808  << "#if __STDC_VERSION__ < 199901L\n"
1809  << " return exp(x)-1;\n"
1810  << "#else\n"
1811  << " return expm1(x);\n"
1812  << "#endif\n"
1813  << "}\n\n";
1814  break;
1815  case AUX_HYPOT:
1816  shorthand("hypot");
1817  this->auxiliaries << "casadi_real casadi_hypot(casadi_real x, casadi_real y) {\n"
1818  << "/* Pre-c99 compatibility */\n"
1819  << "#if __STDC_VERSION__ < 199901L\n"
1820  << " return sqrt(x*x+y*y);\n"
1821  << "#else\n"
1822  << " return hypot(x, y);\n"
1823  << "#endif\n"
1824  << "}\n\n";
1825  break;
1826  case AUX_BLAZING_DE_BOOR:
1827  this->auxiliaries << sanitize_source(casadi_blazing_de_boor_str, inst);
1828  break;
1832  this->auxiliaries << sanitize_source(casadi_blazing_1d_boor_eval_str, inst);
1833  break;
1837  this->auxiliaries << sanitize_source(casadi_blazing_2d_boor_eval_str, inst);
1838  break;
1842  this->auxiliaries << sanitize_source(casadi_blazing_3d_boor_eval_str, inst);
1843  break;
1844  case AUX_PRINTME:
1846  this->auxiliaries << sanitize_source(casadi_printme_str, inst);
1847  break;
1848  }
1849  }
1850 
1851  std::string CodeGenerator::to_mex(const Sparsity& sp, const std::string& arg) {
1853  std::stringstream s;
1854  s << "casadi_to_mex(" << sparsity(sp) << ", " << arg << ");";
1855  return s.str();
1856  }
1857 
1858  std::string CodeGenerator::from_mex(std::string& arg,
1859  const std::string& res, std::size_t res_off,
1860  const Sparsity& sp_res, const std::string& w) {
1861  // Handle offset with recursion
1862  if (res_off!=0) return from_mex(arg, res+"+"+str(res_off), 0, sp_res, w);
1863 
1865  std::stringstream s;
1866  s << "casadi_from_mex(" << arg
1867  << ", " << res << ", " << sparsity(sp_res) << ", " << w << ");";
1868  return s.str();
1869  }
1870 
1871  std::string CodeGenerator::fmu_helpers(const std::string& modelname) {
1872  // Process C++ source
1873  std::stringstream ret;
1874  std::string line;
1875  std::istringstream stream(casadi_fmu_str);
1876  while (std::getline(stream, line)) {
1877  // Replacements
1878  if (line.find("MODELNAME") != std::string::npos) {
1879  line = replace(line, "MODELNAME", modelname);
1880  }
1881  // Append to return
1882  ret << line << "\n";
1883  }
1884  return ret.str();
1885  }
1886 
1887  std::string CodeGenerator::constant(const std::string& v) {
1888  std::string ret = v;
1889  ret = replace(ret, "\\", "\\\\");
1890  ret = replace(ret, "\"", "\\\"");
1891  return "\"" + ret + "\"";
1892  }
1893 
1894  std::string CodeGenerator::constant(casadi_int v) {
1895  return str(v);
1896  }
1897 
1898  std::string CodeGenerator::constant(char v) {
1899  return constant(static_cast<casadi_int>(v));
1900  }
1901  std::string CodeGenerator::constant(double v) {
1902  std::stringstream s;
1903  if (isnan(v)) {
1905  s << "casadi_nan";
1906  } else if (isinf(v)) {
1908  if (v<0) s << "-";
1909  s << "casadi_inf";
1910  } else {
1911  casadi_int v_int = static_cast<casadi_int>(v);
1912  if (static_cast<double>(v_int)==v) {
1913  // Print integer
1914  s << v_int << ".";
1915  } else {
1916  // Print real
1917  std::ios_base::fmtflags fmtfl = s.flags(); // get current format flags
1918  s << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1) << v;
1919  s.flags(fmtfl); // reset current format flags
1920  }
1921  }
1922  return s.str();
1923  }
1924 
1925  std::string CodeGenerator::copy(const std::string& arg,
1926  std::size_t n, const std::string& res) {
1927  std::stringstream s;
1928  // Perform operation
1930  s << "casadi_copy(" << arg << ", " << n << ", " << res << ");";
1931  return s.str();
1932  }
1933 
1934  bool CodeGenerator::elide_copy(casadi_int sz) {
1935  if (casadi::GlobalOptions::copy_elision_min_size==-1) return false;
1937  }
1938 
1939  void CodeGenerator::copy_check(const std::string& arg, size_t n, const std::string& res,
1940  bool check_lhs, bool check_rhs) {
1941  std::vector<std::string> checks;
1942  if (check_lhs) checks.push_back(arg);
1943  if (check_rhs) checks.push_back(res);
1944  if (!checks.empty()) *this << "if (" << join(checks, " && ") << ") ";
1945  *this << copy(arg, n, res) << "\n";
1946  }
1947 
1948  void CodeGenerator::copy_default(const std::string& arg, size_t n, const std::string& res,
1949  const std::string& def, bool check_rhs) {
1950  *this << "if (" << arg << ") {\n";
1951  if (check_rhs) *this << "if (" << res << ") ";
1952  *this << copy(arg, n, res) << "\n";
1953  *this << "} else {\n";
1954  if (check_rhs) *this << "if (" << res << ") ";
1955  *this << fill(res, n, def) << "\n";
1956  *this << "}\n";
1957  }
1958 
1959  std::string CodeGenerator::clear(const std::string& res, std::size_t n) {
1960  std::stringstream s;
1961  // Perform operation
1963  s << "casadi_clear(" << res << ", " << n << ");";
1964  return s.str();
1965  }
1966 
1967  std::string CodeGenerator::arg(casadi_int i) const {
1968  return "arg[" + str(i) + "]";
1969  }
1970 
1971  std::string CodeGenerator::res(casadi_int i) const {
1972  return "res[" + str(i) + "]";
1973  }
1974 
1975  std::string CodeGenerator::mem(const Function& f) {
1976  std::string name = f->codegen_name(*this, false);
1977  std::string mem_array = shorthand(name + "_mem");
1978  return mem_array+"[mem]";
1979  }
1980 
1981  std::string CodeGenerator::fill(const std::string& res,
1982  std::size_t n, const std::string& v) {
1983  if (v=="0") return clear(res, n);
1984  std::stringstream s;
1985  // Perform operation
1987  s << "casadi_fill(" << res << ", " << n << ", " << v << ");";
1988  return s.str();
1989  }
1990 
1991  std::string CodeGenerator::dot(casadi_int n, const std::string& x,
1992  const std::string& y) {
1994  std::stringstream s;
1995  s << "casadi_dot(" << n << ", " << x << ", " << y << ")";
1996  return s.str();
1997  }
1998 
1999  std::string CodeGenerator::bilin(const std::string& A, const Sparsity& sp_A,
2000  const std::string& x, const std::string& y) {
2002  std::stringstream s;
2003  s << "casadi_bilin(" << A << ", " << sparsity(sp_A) << ", " << x << ", " << y << ")";
2004  return s.str();
2005  }
2006 
2007  std::string CodeGenerator::rank1(const std::string& A, const Sparsity& sp_A,
2008  const std::string& alpha, const std::string& x,
2009  const std::string& y) {
2011  std::stringstream s;
2012  s << "casadi_rank1(" << A << ", " << sparsity(sp_A) << ", "
2013  << alpha << ", " << x << ", " << y << ");";
2014  return s.str();
2015  }
2016 
2018  const std::string& res, casadi_int ndim, const std::string& grid,
2019  const std::string& offset,
2020  const std::string& values, const std::string& x,
2021  const std::string& lookup_mode, casadi_int m,
2022  const std::string& iw, const std::string& w) {
2024  std::stringstream s;
2025  s << "casadi_interpn(" << res << ", " << ndim << ", " << grid << ", " << offset << ", "
2026  << values << ", " << x << ", " << lookup_mode << ", " << m << ", " << iw << ", " << w << ");";
2027  return s.str();
2028  }
2029 
2030  std::string CodeGenerator::interpn_grad(const std::string& grad,
2031  casadi_int ndim, const std::string& grid, const std::string& offset,
2032  const std::string& values, const std::string& x,
2033  const std::string& lookup_mode, casadi_int m,
2034  const std::string& iw, const std::string& w) {
2036  std::stringstream s;
2037  s << "casadi_interpn_grad(" << grad << ", " << ndim << ", " << grid << ", " << offset << ", "
2038  << values << ", " << x << ", " << lookup_mode << "," << m << ", " << iw << ", " << w << ");";
2039  return s.str();
2040  }
2041 
2042  std::string CodeGenerator::trans(const std::string& x, const Sparsity& sp_x,
2043  const std::string& y, const Sparsity& sp_y,
2044  const std::string& iw) {
2046  return "casadi_trans(" + x + "," + sparsity(sp_x) + ", "
2047  + y + ", " + sparsity(sp_y) + ", " + iw + ")";
2048  }
2049 
2050  std::string CodeGenerator::declare(std::string s) {
2051  // Add c linkage
2052  std::string cpp_prefix = this->cpp ? "extern \"C\" " : "";
2053 
2054  // To header file
2055  if (this->with_header) {
2056  this->header << cpp_prefix << this->dll_import << s << ";\n";
2057  }
2058 
2059  // Return name with declarations
2060  return cpp_prefix + this->dll_export + s;
2061  }
2062 
2063  std::string
2064  CodeGenerator::project(const std::string& arg, const Sparsity& sp_arg,
2065  const std::string& res, const Sparsity& sp_res,
2066  const std::string& w) {
2067  // If sparsity match, simple copy
2068  if (sp_arg==sp_res) return copy(arg, sp_arg.nnz(), res);
2069 
2070  // Create call
2072  std::stringstream s;
2073  s << "casadi_project(" << arg << ", " << sparsity(sp_arg) << ", " << res << ", "
2074  << sparsity(sp_res) << ", " << w << ");";
2075  return s.str();
2076  }
2077 
2078  std::string
2079  CodeGenerator::tri_project(const std::string& arg, const Sparsity& sp_arg,
2080  const std::string& res, bool lower) {
2081  // Create call
2083  std::stringstream s;
2084  s << "casadi_tri_project(" << arg << ", " << sparsity(sp_arg) << ", ";
2085  s << res << ", " << (lower ? 1: 0) << ");";
2086  return s.str();
2087  }
2088 
2089  std::string
2090  CodeGenerator::densify(const std::string& arg, const Sparsity& sp_arg,
2091  const std::string& res, bool tr) {
2092  // Create call
2094  std::stringstream s;
2095  s << "casadi_densify(" << arg << ", " << sparsity(sp_arg) << ", " << res << ", "
2096  << (tr ? 1 : 0) << ");";
2097  return s.str();
2098  }
2099 
2100  std::string
2101  CodeGenerator::sparsify(const std::string& arg, const std::string& res,
2102  const Sparsity& sp_res, bool tr) {
2103  // Create call
2105  std::stringstream s;
2106  s << "casadi_sparsify(" << arg << ", " << res << ", "
2107  << sparsity(sp_res) << ", " << (tr ? 1 : 0) << ");";
2108  return s.str();
2109  }
2110 
2111  std::string CodeGenerator::printf(const std::string& str, const std::vector<std::string>& arg) {
2113  std::stringstream s;
2114  s << "CASADI_PRINTF(\"" << str << "\"";
2115  for (casadi_int i=0; i<arg.size(); ++i) s << ", " << arg[i];
2116  s << ");";
2117  return s.str();
2118  }
2119 
2120  std::string CodeGenerator::printf(const std::string& str, const std::string& arg1) {
2121  std::vector<std::string> arg;
2122  arg.push_back(arg1);
2123  return printf(str, arg);
2124  }
2125 
2126  std::string CodeGenerator::printf(const std::string& str, const std::string& arg1,
2127  const std::string& arg2) {
2128  std::vector<std::string> arg;
2129  arg.push_back(arg1);
2130  arg.push_back(arg2);
2131  return printf(str, arg);
2132  }
2133 
2134  std::string CodeGenerator::printf(const std::string& str, const std::string& arg1,
2135  const std::string& arg2, const std::string& arg3) {
2136  std::vector<std::string> arg;
2137  arg.push_back(arg1);
2138  arg.push_back(arg2);
2139  arg.push_back(arg3);
2140  return printf(str, arg);
2141  }
2142 
2143  std::string CodeGenerator::axpy(casadi_int n, const std::string& a,
2144  const std::string& x, const std::string& y) {
2146  return "casadi_axpy(" + str(n) + ", " + a + ", " + x + ", " + y + ");";
2147  }
2148 
2149  std::string CodeGenerator::clip_min(const std::string& x, casadi_int n,
2150  const std::string& min, const std::string& mask) {
2152  return "casadi_clip_min(" + x + ", " + str(n) + ", " + min + ", " + mask + ");";
2153  }
2154 
2155  std::string CodeGenerator::clip_max(const std::string& x, casadi_int n,
2156  const std::string& min, const std::string& mask) {
2158  return "casadi_clip_max(" + x + ", " + str(n) + ", " + min + ", " + mask + ");";
2159  }
2160 
2161  std::string CodeGenerator::vector_fmax(casadi_int n, const std::string& x,
2162  const std::string& y, const std::string& z) {
2164  return "casadi_vector_fmax(" + str(n) + ", " + x + ", " + y + ", " + z + ");";
2165  }
2166 
2167  std::string CodeGenerator::vector_fmin(casadi_int n, const std::string& x,
2168  const std::string& y, const std::string& z) {
2170  return "casadi_vector_fmin(" + str(n) + ", " + x + ", " + y + ", " + z + ");";
2171  }
2172 
2173  std::string CodeGenerator::masked_norm_inf(casadi_int n, const std::string& x,
2174  const std::string& mask) {
2176  return "casadi_masked_norm_inf(" + str(n) + ", " + x + ", " + mask + ")";
2177  }
2178 
2179  std::string CodeGenerator::scal(casadi_int n, const std::string& alpha, const std::string& x) {
2181  return "casadi_scal(" + str(n) + ", " + alpha + ", " + x + ");";
2182  }
2183 
2184  std::string CodeGenerator::mv(const std::string& x, const Sparsity& sp_x,
2185  const std::string& y, const std::string& z, bool tr) {
2187  return "casadi_mv(" + x + ", " + sparsity(sp_x) + ", " + y + ", "
2188  + z + ", " + (tr ? "1" : "0") + ");";
2189  }
2190 
2191  std::string CodeGenerator::mv(const std::string& x, casadi_int nrow_x, casadi_int ncol_x,
2192  const std::string& y, const std::string& z, bool tr) {
2194  return "casadi_mv_dense(" + x + ", " + str(nrow_x) + ", " + str(ncol_x) + ", "
2195  + y + ", " + z + ", " + (tr ? "1" : "0") + ");";
2196  }
2197 
2198  std::string CodeGenerator::mtimes(const std::string& x, const Sparsity& sp_x,
2199  const std::string& y, const Sparsity& sp_y,
2200  const std::string& z, const Sparsity& sp_z,
2201  const std::string& w, bool tr) {
2203  return "casadi_mtimes(" + x + ", " + sparsity(sp_x) + ", " + y + ", " + sparsity(sp_y) + ", "
2204  + z + ", " + sparsity(sp_z) + ", " + w + ", " + (tr ? "1" : "0") + ");";
2205  }
2206 
2207  std::string CodeGenerator::trilsolve(const Sparsity& sp_x, const std::string& x,
2208  const std::string& y, bool tr, bool unity, casadi_int nrhs) {
2210  return "casadi_trilsolve(" + sparsity(sp_x) + ", " + x + ", " + y + ", " + str(tr) + ", "
2211  + str(unity) + ", " + str(nrhs) + ");";
2212  }
2213 
2214  std::string CodeGenerator::triusolve(const Sparsity& sp_x, const std::string& x,
2215  const std::string& y, bool tr, bool unity, casadi_int nrhs) {
2217  return "casadi_triusolve(" + sparsity(sp_x) + ", " + x + ", " + y + ", " + str(tr) + ", "
2218  + str(unity) + ", " + str(nrhs) + ");";
2219  }
2220 
2221 
2222  std::string CodeGenerator::logsumexp(const std::string& A, casadi_int n) {
2224  std::stringstream s;
2225  s << "casadi_logsumexp(" << A << ", " << n << ");";
2226  return s.str();
2227  }
2228 
2229  void CodeGenerator::print_formatted(const std::string& s) {
2230  // Quick return if empty
2231  if (s.empty()) return;
2232 
2233  // If new line, add indentation
2234  if (newline_) {
2235  casadi_int shift = s.front()=='}' ? -1 : 0;
2236  casadi_assert_dev(current_indent_+shift>=0);
2237  this->buffer << std::string(indent_*(current_indent_+shift), ' ');
2238  newline_ = false;
2239  }
2240 
2241  // Print to body
2242  this->buffer << s;
2243 
2244  // Brackets change indentation for next row
2245  // NOTE(@jaeandersson): Should ignore strings, comments
2246  for (char c : s) {
2247  if (c=='{') {
2248  indent();
2249  } else if (c=='}') {
2250  unindent();
2251  }
2252  }
2253  }
2254 
2255  CodeGenerator& CodeGenerator::operator<<(const std::string& s) {
2256  // Loop over newline characters
2257  size_t off=0;
2258  while (true) {
2259  size_t pos = s.find('\n', off);
2260  if (pos==std::string::npos) {
2261  // No more newline characters
2262  print_formatted(s.substr(off));
2263  break;
2264  } else {
2265  // Ends with newline
2266  print_formatted(s.substr(off, pos-off));
2267  this->buffer << '\n';
2268  newline_ = true;
2269  off = pos+1;
2270  }
2271  }
2272 
2273  return *this;
2274  }
2275 
2276  void CodeGenerator::flush(std::ostream &s) {
2277  s << this->buffer.str();
2278  this->buffer.str(std::string());
2279  }
2280 
2281  void CodeGenerator::local(const std::string& name, const std::string& type,
2282  const std::string& ref) {
2283  // Check if the variable already exists
2284  auto it = local_variables_.find(name);
2285  if (it==local_variables_.end()) {
2286  // Add it
2287  local_variables_[name] = std::make_pair(type, ref);
2288  } else {
2289  // Consistency check
2290  casadi_assert(it->second.first==type, "Type mismatch for " + name);
2291  casadi_assert(it->second.second==ref, "Type mismatch for " + name);
2292  }
2293  }
2294 
2295  std::string CodeGenerator::sx_work(casadi_int i) {
2296  if (avoid_stack_) {
2297  return "w[" + str(i) + "]";
2298  } else {
2299  std::string name = "a"+format_padded(i);
2300 
2301  // Make sure work vector element has been declared
2302  local(name, "casadi_real");
2303 
2304  return name;
2305  }
2306  }
2307 
2308  void CodeGenerator::init_local(const std::string& name, const std::string& def) {
2309  auto it = local_default_.find(name);
2310  if (it!=local_default_.end()) {
2311  casadi_assert(it->second==def, "Initial value mismatch for " + name);
2312  }
2313  local_default_.insert(std::make_pair(name, def));
2314  }
2315 
2316  std::string CodeGenerator::
2317  sanitize_source(const std::string& src,
2318  const std::vector<std::string>& inst, bool add_shorthand) {
2319  // Create suffix if templates type are not all "casadi_real"
2320  std::string suffix;
2321  for (const std::string& s : inst) {
2322  if (s!="casadi_real") {
2323  for (const std::string& s : inst) suffix += "_" + s;
2324  break;
2325  }
2326  }
2327 
2328  // Construct map of name replacements
2329  std::vector<std::pair<std::string, std::string> > rep;
2330  for (casadi_int i=0; i<inst.size(); ++i) {
2331  rep.push_back(std::make_pair("T" + str(i+1), inst[i]));
2332  }
2333 
2334  // Return object
2335  std::stringstream ret;
2336  // Process C++ source
2337  std::string line;
2338  std::istringstream stream(src);
2339  while (std::getline(stream, line)) {
2340  size_t n1, n2;
2341 
2342  // C++ template declarations are ignored
2343  if (line.find("template")==0) continue;
2344 
2345  // Macro definitions are ignored
2346  if (line.find("#define")==0) continue;
2347  if (line.find("#undef")==0) continue;
2348 
2349  // Inline declaration
2350  if (line == "inline") continue;
2351 
2352  // If line starts with "// SYMBOL", add shorthand
2353  if (line.find("// SYMBOL") != std::string::npos) {
2354  n1 = line.find("\"");
2355  n2 = line.find("\"", n1+1);
2356  std::string sym = line.substr(n1+1, n2-n1-1);
2357  if (add_shorthand) shorthand(sym + suffix);
2358  if (!suffix.empty()) {
2359  rep.push_back(std::make_pair(sym, sym + suffix));
2360  }
2361  continue;
2362  }
2363 
2364  // If line starts with "// C-REPLACE", add to list of replacements
2365  if (line.find("// C-REPLACE") != std::string::npos) {
2366  // Get C++ string
2367  n1 = line.find("\"");
2368  n2 = line.find("\"", n1+1);
2369  std::string key = line.substr(n1+1, n2-n1-1);
2370  // Get C string
2371  n1 = line.find("\"", n2+1);
2372  n2 = line.find("\"", n1+1);
2373  std::string sub = line.substr(n1+1, n2-n1-1);
2374  // Add to replacements
2375  rep.push_back(std::make_pair(key, sub));
2376  continue;
2377  }
2378 
2379  // If line starts with "// C-VERBOSE", skip the next line
2380  if (!verbose_runtime && line.find("// C-VERBOSE") != std::string::npos) {
2381  // Ignore next line
2382  std::getline(stream, line);
2383  continue;
2384  }
2385 
2386  // Ignore other C++ style comment
2387  if ((n1 = line.find("//")) != std::string::npos) line.erase(n1);
2388 
2389  // Remove trailing spaces
2390  if ((n1 = line.find_last_not_of(' ')) != std::string::npos) {
2391  line.erase(n1 + 1);
2392  } else {
2393  continue;
2394  }
2395 
2396  // Perform string replacements
2397  for (auto&& it = rep.rbegin(); it!=rep.rend(); ++it) {
2398  line = replace(line, it->first, it->second);
2399  }
2400 
2401  // Append to return
2402  ret << line << "\n";
2403  }
2404 
2405  // Trailing newline
2406  ret << "\n";
2407  return ret.str();
2408  }
2409 
2410  void CodeGenerator::comment(const std::string& s) {
2411  if (verbose) {
2412  *this << "/* " << s << " */\n";
2413  }
2414  }
2415 
2417  add_io_sparsities(const std::string& name,
2418  const std::vector<Sparsity>& sp_in,
2419  const std::vector<Sparsity>& sp_out) {
2420  // Insert element, quick return if it already exists
2421  if (!sparsity_meta.insert(name).second) return;
2422 
2423  // Input sparsities
2424  *this << declare("const casadi_int* " + name + "_sparsity_in(casadi_int i)") << " {\n"
2425  << "switch (i) {\n";
2426  for (casadi_int i=0; i<sp_in.size(); ++i) {
2427  *this << "case " << i << ": return " << sparsity(sp_in[i], force_canonical) << ";\n";
2428  }
2429  *this << "default: return 0;\n}\n"
2430  << "}\n\n";
2431 
2432  // Output sparsities
2433  *this << declare("const casadi_int* " + name + "_sparsity_out(casadi_int i)") << " {\n"
2434  << "switch (i) {\n";
2435  for (casadi_int i=0; i<sp_out.size(); ++i) {
2436  *this << "case " << i << ": return " << sparsity(sp_out[i], force_canonical) << ";\n";
2437  }
2438  *this << "default: return 0;\n}\n"
2439  << "}\n\n";
2440  }
2441 
2442  std::string CodeGenerator::
2443  qr(const std::string& sp, const std::string& A, const std::string& w,
2444  const std::string& sp_v, const std::string& v, const std::string& sp_r,
2445  const std::string& r, const std::string& beta, const std::string& prinv,
2446  const std::string& pc) {
2448  return "casadi_qr(" + sp + ", " + A + ", " + w + ", "
2449  + sp_v + ", " + v + ", " + sp_r + ", " + r + ", "
2450  + beta + ", " + prinv + ", " + pc + ");";
2451  }
2452 
2453  std::string CodeGenerator::
2454  qr_solve(const std::string& x, casadi_int nrhs, bool tr,
2455  const std::string& sp_v, const std::string& v,
2456  const std::string& sp_r, const std::string& r,
2457  const std::string& beta, const std::string& prinv,
2458  const std::string& pc, const std::string& w) {
2460  return "casadi_qr_solve(" + x + ", " + str(nrhs) + ", " + (tr ? "1" : "0") + ", "
2461  + sp_v + ", " + v + ", " + sp_r + ", " + r + ", "
2462  + beta + ", " + prinv + ", " + pc + ", " + w + ");";
2463  }
2464 
2465  std::string CodeGenerator::
2466  lsqr_solve(const std::string& A, const std::string&x,
2467  casadi_int nrhs, bool tr, const std::string& sp, const std::string& w) {
2469  return "casadi_lsqr_solve(" + A + ", " + x + ", " + str(nrhs) + ", "
2470  + (tr ? "1" : "0") + ", " + sp + ", " + w + ");";
2471  }
2472 
2473  std::string CodeGenerator::
2474  ldl(const std::string& sp_a, const std::string& a,
2475  const std::string& sp_lt, const std::string& lt, const std::string& d,
2476  const std::string& p, const std::string& w) {
2478  return "casadi_ldl(" + sp_a + ", " + a + ", " + sp_lt + ", " + lt + ", "
2479  + d + ", " + p + ", " + w + ");";
2480  }
2481 
2482  std::string CodeGenerator::
2483  ldl_solve(const std::string& x, casadi_int nrhs,
2484  const std::string& sp_lt, const std::string& lt, const std::string& d,
2485  const std::string& p, const std::string& w) {
2487  return "casadi_ldl_solve(" + x + ", " + str(nrhs) + ", " + sp_lt + ", "
2488  + lt + ", " + d + ", " + p + ", " + w + ");";
2489  }
2490 
2491  std::string CodeGenerator::
2492  fmax(const std::string& x, const std::string& y) {
2494  return "casadi_fmax(" + x + ", " + y + ");";
2495  }
2496 
2497  std::string CodeGenerator::
2498  fmin(const std::string& x, const std::string& y) {
2500  return "casadi_fmin(" + x + ", " + y + ");";
2501  }
2502 
2503  std::string CodeGenerator::
2504  vfmax(const std::string& x, casadi_int n, const std::string& y) {
2506  return "casadi_vfmax(" + x + ", " + str(n) + ", " + y + ");";
2507  }
2508 
2509  std::string CodeGenerator::
2510  vfmin(const std::string& x, casadi_int n, const std::string& y) {
2512  return "casadi_vfmin(" + x + ", " + str(n) + ", " + y + ");";
2513  }
2514 
2515  std::string CodeGenerator::
2516  vfmax(const std::string& x, const std::string& n, const std::string& y) {
2518  return "casadi_vfmax(" + x + ", " + n + ", " + y + ");";
2519  }
2520 
2521  std::string CodeGenerator::
2522  vfmin(const std::string& x, const std::string& n, const std::string& y) {
2524  return "casadi_vfmin(" + x + ", " + n + ", " + y + ");";
2525  }
2526 
2527  std::string CodeGenerator::
2528  max(const std::string& x, const std::string& y) {
2530  return "casadi_max(" + x + ", " + y + ")";
2531  }
2532 
2533  std::string CodeGenerator::
2534  min(const std::string& x, const std::string& y) {
2536  return "casadi_min(" + x + ", " + y + ")";
2537  }
2538 
2539  std::string CodeGenerator::
2540  mmax(const std::string& x, casadi_int n, bool is_dense) {
2542  return "casadi_mmax(" + x + ", " + str(n) + ", " + str(casadi_int(is_dense)) + ")";
2543  }
2544 
2545  std::string CodeGenerator::
2546  mmin(const std::string& x, casadi_int n, bool is_dense) {
2548  return "casadi_mmin(" + x + ", " + str(n) + ", " + str(casadi_int(is_dense)) + ")";
2549  }
2550 
2551  std::string CodeGenerator::
2552  max_viol(casadi_int n, const std::string& x, const std::string& lb, const std::string& ub) {
2554  return "casadi_max_viol(" + str(n) + ", " + x+ ", " + lb + ", " + ub + ")";
2555  }
2556 
2557  std::string CodeGenerator::
2558  sum_viol(casadi_int n, const std::string& x, const std::string& lb, const std::string& ub) {
2560  return "casadi_sum_viol(" + str(n) + ", " + x+ ", " + lb + ", " + ub + ")";
2561  }
2562 
2563  std::string CodeGenerator::
2564  norm_inf(casadi_int n, const std::string& x) {
2566  return "casadi_norm_inf(" + str(n) + ", " + x + ")";
2567  }
2568 
2569  std::string CodeGenerator::
2570  norm_1(casadi_int n, const std::string& x) {
2572  return "casadi_norm_1(" + str(n) + ", " + x + ")";
2573  }
2574 
2575  std::string CodeGenerator::
2576  norm_2(casadi_int n, const std::string& x) {
2578  return "casadi_norm_2(" + str(n) + ", " + x + ")";
2579  }
2580 
2581  std::string CodeGenerator::
2582  lb_eig(const Sparsity& sp_h, const std::string& h) {
2584  return "casadi_lb_eig(" + sparsity(sp_h) + ", " + h + ")";
2585  }
2586 
2587  std::string CodeGenerator::
2588  regularize(const Sparsity& sp_h, const std::string& h, const std::string& reg) {
2590  return "casadi_regularize(" + sparsity(sp_h) + ", " + h + ", " + reg + ");";
2591  }
2592 
2593  std::string CodeGenerator::
2595  const std::string& Hin, const std::string& Hout, const std::string& iw, const std::string& w) {
2597  return Convexify::generate(*this, d, Hin, Hout, iw, w);
2598  }
2599 
2600  std::string CodeGenerator::
2601  low(const std::string& x, const std::string& grid, casadi_int ng, casadi_int lookup_mode) {
2603  return "casadi_low(" + x + ", " + grid + ", " + str(ng) + ", " + str(lookup_mode) + ");";
2604  }
2605 
2606  std::string CodeGenerator::
2607  bound_consistency(casadi_int n, const std::string& x,
2608  const std::string& lam, const std::string& lbx, const std::string& ubx) {
2610  return "casadi_bound_consistency(" + str(n) + ", " + x + ", " + lam +
2611  ", " + lbx + ", " + ubx + ")";
2612  }
2613 
2614  std::string CodeGenerator::
2615  file_slurp(const std::string& fname, casadi_int n, const std::string& a) {
2617  return "casadi_file_slurp(\"" + fname + "\", " + str(n) + ", " + a + ")";
2618  }
2619 
2620  std::string CodeGenerator::
2621  cache_check(const std::string& key, const std::string& cache, const std::string& loc,
2622  casadi_int stride, casadi_int sz, casadi_int key_sz, const std::string& val) {
2624  return "cache_check(" + key + ", " + cache + ", " + loc + ", " +
2625  str(stride) + ", " + str(sz) + ", " + str(key_sz) + ", " + val + ")";
2626  }
2627 
2628  void CodeGenerator::sz_work(size_t& sz_arg, size_t& sz_res, size_t& sz_iw, size_t& sz_w) const {
2629  sz_arg = sz_res = sz_iw = sz_w = 0;
2630  for (auto&& f : added_functions_) {
2631  sz_arg = std::max(sz_arg, f.f.sz_arg());
2632  sz_res = std::max(sz_res, f.f.sz_res());
2633  sz_iw = std::max(sz_iw, f.f.sz_iw());
2634  sz_w = std::max(sz_w, f.f.sz_w());
2635  }
2636  }
2637 
2638 } // namespace casadi
Helper class for C code generation.
std::string triusolve(const Sparsity &sp_x, const std::string &x, const std::string &y, bool tr, bool unity, casadi_int nrhs)
Codegen upper triangular solve.
void define_pool_double(const std::string &name, const std::vector< double > &def)
Allocate file scope double writeable memory.
bool codegen_scalars
Codegen scalar.
std::string fill(const std::string &res, std::size_t n, const std::string &v)
Create a fill operation.
std::stringstream includes
std::string axpy(casadi_int n, const std::string &a, const std::string &x, const std::string &y)
Codegen axpy: y += a*x.
static std::string fmu_helpers(const std::string &modelname)
FMU helper functions.
std::string lsqr_solve(const std::string &A, const std::string &x, casadi_int nrhs, bool tr, const std::string &sp, const std::string &w)
std::set< std::string > added_externals_
std::string logsumexp(const std::string &A, casadi_int n)
std::string clip_min(const std::string &x, casadi_int n, const std::string &min, const std::string &mask)
Codegen clip_min: Clips the smaller entries in a vector than min to the min.
std::string project(const std::string &arg, const Sparsity &sp_arg, const std::string &res, const Sparsity &sp_res, const std::string &w)
Sparse assignment.
std::string add_dependency(const Function &f)
Add a function dependency.
std::string mmax(const std::string &x, casadi_int n, bool is_dense)
mmax
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string arg(casadi_int i) const
Refer to argument.
std::string dump()
Generate a file, return code as string.
std::string wrapper(const Function &base, const std::string &name)
std::vector< std::string > added_sfunctions
std::string pool_double(const std::string &name) const
Access file scope double writeable memory.
void add_io_sparsities(const std::string &name, const std::vector< Sparsity > &sp_in, const std::vector< Sparsity > &sp_out)
Add io sparsity patterns of a function.
void scope_enter()
Enter a local scope.
std::string rom_integer(const void *id) const
Access file scope integer read-only memory.
std::string norm_2(casadi_int n, const std::string &x)
norm_2
std::string copy(const std::string &arg, std::size_t n, const std::string &res)
Create a copy operation.
std::map< std::string, casadi_int > pool_double_
void comment(const std::string &s)
Write a comment line (ignored if not verbose)
void reserve_work(casadi_int n)
Reserve a maximum size of work elements, used for padding of index.
std::string masked_norm_inf(casadi_int n, const std::string &x, const std::string &mask)
codegen masked_norm_inf: The mask tells what entry is used in the inf-norm.
std::set< std::string > added_includes_
std::string constant(const std::vector< casadi_int > &v)
Represent an array constant; adding it when new.
void add(const Function &f, bool with_jac_sparsity=false)
Add a function (name generated)
void flush(std::ostream &s)
Flush the buffer to a stream of choice.
casadi_int add_sparsity(const Sparsity &sp, bool canonical=true)
std::string scal(casadi_int n, const std::string &alpha, const std::string &x)
What does scal do??
std::string rank1(const std::string &A, const Sparsity &sp_A, const std::string &alpha, const std::string &x, const std::string &y)
Rank-1 update.
std::string low(const std::string &x, const std::string &grid, casadi_int ng, casadi_int lookup_mode)
low
std::multimap< Auxiliary, std::vector< std::string > > added_auxiliaries_
CodeGenerator(const std::string &name, const Dict &opts=Dict())
Constructor.
std::string fmin(const std::string &x, const std::string &y)
fmin
std::string sum_viol(casadi_int n, const std::string &x, const std::string &lb, const std::string &ub)
sum_viol
std::string to_mex(const Sparsity &sp, const std::string &arg)
Create matrix in MATLAB's MEX format.
std::string printf(const std::string &str, const std::vector< std::string > &arg=std::vector< std::string >())
Printf.
std::string print_op(casadi_int op, const std::string &a0)
Print an operation to a c file.
std::map< std::string, std::pair< std::string, std::string > > local_variables_
std::string max(const std::string &x, const std::string &y)
max
void indent()
Increase indentation.
std::string rom_double(const void *id) const
Access file scope double read-only memory.
std::map< std::string, std::string > local_default_
std::vector< std::vector< char > > char_constants_
static void file_close(std::ofstream &f, bool cpp)
Print file header.
static std::string array(const std::string &type, const std::string &name, casadi_int len, const std::string &def=std::string())
std::string bilin(const std::string &A, const Sparsity &sp_A, const std::string &x, const std::string &y)
Codegen bilinear form.
std::string bound_consistency(casadi_int n, const std::string &x, const std::string &lam, const std::string &lbx, const std::string &ubx)
bound_consistency
std::string vector_fmax(casadi_int n, const std::string &x, const std::string &y, const std::string &z)
Codegen vector_fmax: Takes vectorwise max of a vector and writes the result to second vector.
std::string sparsify(const std::string &arg, const std::string &res, const Sparsity &sp_res, bool tr=false)
Sparsify.
std::string mv(const std::string &x, const Sparsity &sp_x, const std::string &y, const std::string &z, bool tr)
Codegen sparse matrix-vector multiplication.
static size_t hash(const std::vector< double > &v)
std::vector< std::vector< double > > pool_double_defaults_
std::string ldl_solve(const std::string &x, casadi_int nrhs, const std::string &sp_lt, const std::string &lt, const std::string &d, const std::string &p, const std::string &w)
LDL solve.
CodeGenerator & operator<<(const std::string &s)
Print a string to buffer.
std::string generate(const std::string &prefix="")
Generate file(s)
std::string ldl(const std::string &sp_a, const std::string &a, const std::string &sp_lt, const std::string &lt, const std::string &d, const std::string &p, const std::string &w)
LDL factorization.
std::string lb_eig(const Sparsity &sp_h, const std::string &h)
lb_eig
static void file_open(std::ofstream &f, const std::string &name, bool cpp)
Print file header.
std::string operator()(const Function &f, const std::string &arg, const std::string &res, const std::string &iw, const std::string &w, const std::string &failure_ret="1")
Generate a call to a function (generic signature)
std::stringstream buffer
std::string mmin(const std::string &x, casadi_int n, bool is_dense)
mmin
std::string densify(const std::string &arg, const Sparsity &sp_arg, const std::string &res, bool tr=false)
Densify.
std::multimap< size_t, size_t > added_double_constants_
void local(const std::string &name, const std::string &type, const std::string &ref="")
Declare a local variable.
void add_external(const std::string &new_external)
Add an external function declaration.
std::string workel(casadi_int n) const
std::string min(const std::string &x, const std::string &y)
min
void setup_callback(const std::string &s, const Function &f)
Setup a callback.
casadi_int max_initializer_elements_per_line
std::string mem(const Function &f)
Access thread-local memory.
void print_vector(std::ostream &s, const std::string &name, const std::vector< casadi_int > &v)
Print casadi_int vector to a c file.
std::stringstream header
void unindent()
Decrease indentation.
std::string from_mex(std::string &arg, const std::string &res, std::size_t res_off, const Sparsity &sp_res, const std::string &w)
Get matrix from MATLAB's MEX format.
std::vector< std::vector< double > > double_constants_
std::string res(casadi_int i) const
Refer to resuly.
std::vector< std::vector< casadi_int > > integer_constants_
std::string interpn(const std::string &res, casadi_int ndim, const std::string &grid, const std::string &offset, const std::string &values, const std::string &x, const std::string &lookup_mode, casadi_int m, const std::string &iw, const std::string &w)
Multilinear interpolation.
void sz_work(size_t &sz_arg, size_t &sz_res, size_t &sz_iw, size_t &sz_w) const
Get number of temporary variables needed for all functions.
std::multimap< size_t, size_t > added_char_constants_
std::vector< std::vector< std::string > > string_constants_
std::string vfmax(const std::string &x, casadi_int n, const std::string &y)
vfmax
std::string norm_inf(casadi_int n, const std::string &x)
norm_inf
std::string ones(casadi_int sz)
std::string vector_fmin(casadi_int n, const std::string &x, const std::string &y, const std::string &z)
Codegen vector_fmin: Takes vectorwise min of a vector and writes the result to second vector.
std::map< const void *, casadi_int > file_scope_integer_
std::string declare(std::string s)
Declare a function.
std::string mtimes(const std::string &x, const Sparsity &sp_x, const std::string &y, const Sparsity &sp_y, const std::string &z, const Sparsity &sp_z, const std::string &w, bool tr)
Codegen sparse matrix-matrix multiplication.
void scope_exit()
Exit a local scope.
std::string vfmin(const std::string &x, casadi_int n, const std::string &y)
vfmin
void init_local(const std::string &name, const std::string &def)
Specify the default value for a local variable.
std::string cache_check(const std::string &key, const std::string &cache, const std::string &loc, casadi_int stride, casadi_int sz, casadi_int key_sz, const std::string &val)
cache check
void define_rom_integer(const void *id, casadi_int size)
Allocate file scope integer read-only memory.
std::string qr_solve(const std::string &x, casadi_int nrhs, bool tr, const std::string &sp_v, const std::string &v, const std::string &sp_r, const std::string &r, const std::string &beta, const std::string &prinv, const std::string &pc, const std::string &w)
QR solve.
casadi_int max_declarations_per_line
std::string sanitize_source(const std::string &src, const std::vector< std::string > &inst, bool add_shorthand=true)
Sanitize source files for codegen.
std::string dot(casadi_int n, const std::string &x, const std::string &y)
Codegen inner product.
std::vector< FunctionMeta > added_functions_
std::string clip_max(const std::string &x, casadi_int n, const std::string &min, const std::string &mask)
Codegen clip_max: Clips the larger entries in a vector than max to the max.
static std::string casadi_version()
Current CasADi version as string.
void define_rom_double(const void *id, casadi_int size)
Allocate file scope double read-only memory.
std::string interpn_grad(const std::string &grad, casadi_int ndim, const std::string &grid, const std::string &offset, const std::string &values, const std::string &x, const std::string &lookup_mode, casadi_int m, const std::string &iw, const std::string &w)
Multilinear interpolation - calculate gradient.
void add_include(const std::string &new_include, bool relative_path=false, const std::string &use_ifdef=std::string())
Add an include file optionally using a relative path "..." instead of an absolute path <....
std::string shorthand(const std::string &name) const
Get a shorthand.
Auxiliary
Auxiliary functions.
void constant_copy(const std::string &var_name, const std::vector< casadi_int > &v, const std::string &type="casadi_int")
Represent an array constant; adding it when new.
std::map< std::string, std::map< FunctionInternal *, casadi_int > > added_wrappers_
void copy_check(const std::string &arg, std::size_t n, const std::string &res, bool check_lhs=true, bool check_rhs=true)
std::stringstream body
std::string tri_project(const std::string &arg, const Sparsity &sp_arg, const std::string &res, bool lower)
Project triangular part.
std::multimap< size_t, size_t > added_integer_constants_
std::multimap< size_t, size_t > added_string_constants_
std::map< const void *, casadi_int > file_scope_double_
std::string file_slurp(const std::string &fname, casadi_int n, const std::string &a)
Slurp a file.
std::string initializer(const std::vector< T > &v)
Print an initializer.
std::string norm_1(casadi_int n, const std::string &x)
norm_1
std::set< std::string > sparsity_meta
std::string max_viol(casadi_int n, const std::string &x, const std::string &lb, const std::string &ub)
max_viol
std::string convexify_eval(const ConvexifyData &d, const std::string &Hin, const std::string &Hout, const std::string &iw, const std::string &w)
convexify
std::vector< std::string > exposed_fname
std::string sparsity(const Sparsity &sp, bool canonical=true)
void copy_default(const std::string &arg, std::size_t n, const std::string &res, const std::string &def, bool check_rhs=true)
std::string trans(const std::string &x, const Sparsity &sp_x, const std::string &y, const Sparsity &sp_y, const std::string &iw)
Transpose.
casadi_int get_constant(const std::vector< double > &v, bool allow_adding=false)
Get or add a constant.
casadi_int get_sparsity(const Sparsity &sp) const
Get the index of an existing sparsity pattern.
void print_formatted(const std::string &s)
Print without newline characters.
bool elide_copy(casadi_int sz)
std::string zeros(casadi_int sz)
std::string trilsolve(const Sparsity &sp_x, const std::string &x, const std::string &y, bool tr, bool unity, casadi_int nrhs)
Codegen lower triangular solve.
std::set< std::string > added_shorthands_
static bool equal(const std::vector< T > &v1, const std::vector< T > &v2)
std::stringstream auxiliaries
std::string qr(const std::string &sp, const std::string &A, const std::string &w, const std::string &sp_v, const std::string &v, const std::string &sp_r, const std::string &r, const std::string &beta, const std::string &prinv, const std::string &pc)
QR factorization.
std::string fmax(const std::string &x, const std::string &y)
fmax
std::string clear(const std::string &res, std::size_t n)
Create a fill operation.
std::string format_padded(casadi_int i) const
std::string regularize(const Sparsity &sp_h, const std::string &h, const std::string &reg)
regularize
void add_auxiliary(Auxiliary f, const std::vector< std::string > &inst={"casadi_real"})
Add a built-in auxiliary function.
std::string sx_work(casadi_int i)
Declare a work vector element.
void generate(CodeGenerator &g, const std::vector< casadi_int > &arg, const std::vector< casadi_int > &res, const std::vector< bool > &arg_is_ref, std::vector< bool > &res_is_ref) const override
Generate code for the operation.
Definition: convexify.cpp:156
static void open(std::ofstream &, const std::string &path, std::ios_base::openmode mode=std::ios_base::out)
Definition: filesystem.cpp:115
Internal class for Function.
bool has_refcount_
Reference counting in codegen?
virtual std::string codegen_mem_type() const
Thread-local memory object type.
std::vector< Sparsity > sparsity_in_
Input and output sparsity.
virtual void codegen_free_mem(CodeGenerator &g) const
Codegen for free_mem.
virtual void codegen_incref(CodeGenerator &g) const
Codegen incref for dependencies.
std::string signature_unrolled(const std::string &fname) const
Code generate the function.
virtual std::string codegen_name(const CodeGenerator &g, bool ns=true) const
Get name in codegen.
void codegen(CodeGenerator &g, const std::string &fname) const
Generate code the function.
virtual void codegen_release(CodeGenerator &g) const
Codegen for release.
virtual void codegen_alloc_mem(CodeGenerator &g) const
Codegen decref for alloc_mem.
virtual void codegen_checkout(CodeGenerator &g) const
Codegen for checkout.
virtual void codegen_declarations(CodeGenerator &g) const
Generate code for the declarations of the C function.
std::string signature(const std::string &fname) const
Code generate the function.
void codegen_meta(CodeGenerator &g) const
Generate meta-information allowing a user to evaluate a generated function.
virtual void codegen_init_mem(CodeGenerator &g) const
Codegen decref for init_mem.
virtual void codegen_decref(CodeGenerator &g) const
Codegen decref for dependencies.
Function object.
Definition: function.hpp:60
FunctionInternal * get() const
Definition: function.cpp:353
const std::vector< std::string > & name_in() const
Get input scheme.
Definition: function.cpp:961
const std::string & name() const
Name of the function.
Definition: function.cpp:1307
static bool check_name(const std::string &name)
Check if a string is a valid function name.
Definition: function.cpp:1316
casadi_int n_out() const
Get the number of function outputs.
Definition: function.cpp:823
casadi_int n_in() const
Get the number of function inputs.
Definition: function.cpp:819
const std::vector< Sparsity > & jac_sparsity(bool compact=false) const
Get, if necessary generate, the sparsity of all Jacobian blocks.
Definition: function.cpp:940
const std::vector< std::string > & name_out() const
Get output scheme.
Definition: function.cpp:965
static casadi_int copy_elision_min_size
General sparsity class.
Definition: sparsity.hpp:106
casadi_int nnz() const
Get the number of (structural) non-zeros.
Definition: sparsity.cpp:148
std::vector< casadi_int > compress(bool canonical=true) const
Compress a sparsity pattern.
Definition: sparsity.cpp:1305
The casadi namespace.
Definition: archiver.cpp:28
std::string join(const std::vector< std::string > &l, const std::string &delim)
CASADI_EXPORT std::string replace(const std::string &s, const std::string &p, const std::string &r)
Replace all occurences of p with r in s.
std::string str(const T &v)
String representation, any type.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
void hash_combine(std::size_t &seed, T v)
Generate a hash value incrementally (function taken from boost)
Definition: sparsity.hpp:1207
@ OP_SIGN
Definition: calculus.hpp:71
@ OP_FMAX
Definition: calculus.hpp:72
@ OP_LOG1P
Definition: calculus.hpp:202
@ OP_PRINTME
Definition: calculus.hpp:190
@ OP_HYPOT
Definition: calculus.hpp:206
@ OP_FMIN
Definition: calculus.hpp:72
@ OP_EXPM1
Definition: calculus.hpp:204
@ OP_FABS
Definition: calculus.hpp:71
@ OP_SQ
Definition: calculus.hpp:67
static std::string print(unsigned char op, const std::string &x, const std::string &y)
Print.
Definition: calculus.hpp:1641