List of all members | Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes
casadi::CodeGenerator Class Reference

Helper class for C code generation. More...

#include <code_generator.hpp>

Detailed Description

Author
Joel Andersson
Date
2016

Extra doc: https://github.com/casadi/casadi/wiki/L_ru

Definition at line 44 of file code_generator.hpp.

Classes

struct  FunctionMeta
 

Public Types

enum  Auxiliary {
  AUX_COPY , AUX_CVX , AUX_CONVEXIFY , AUX_SWAP ,
  AUX_SCAL , AUX_AXPY , AUX_DOT , AUX_BILIN ,
  AUX_KRON , AUX_KRON_DENSE , AUX_KRON_DENSE_SPARSE , AUX_KRON_SPARSE_DENSE ,
  AUX_KRON_CONTRACT_INNER , AUX_KRON_CONTRACT_INNER_DENSE , AUX_KRON_CONTRACT_INNER_DENSE_SPARSE , AUX_KRON_CONTRACT_INNER_SPARSE_DENSE ,
  AUX_KRON_CONTRACT_OUTER , AUX_KRON_CONTRACT_OUTER_DENSE , AUX_KRON_CONTRACT_OUTER_DENSE_SPARSE , AUX_KRON_CONTRACT_OUTER_SPARSE_DENSE ,
  AUX_RANK1 , AUX_NORM_1 , AUX_NORM_2 , AUX_CLIP_MAX ,
  AUX_CLIP_MIN , AUX_VECTOR_FMAX , AUX_VECTOR_FMIN , AUX_NORM_INF ,
  AUX_MASKED_NORM_INF , AUX_IAMAX , AUX_CLEAR , AUX_FILL ,
  AUX_MV , AUX_MV_DENSE , AUX_MTIMES , AUX_MTIMES_DENSE ,
  AUX_MTIMES_DENSE_SPARSE , AUX_TRILSOLVE , AUX_TRIUSOLVE , AUX_PROJECT ,
  AUX_TRI_PROJECT , AUX_DENSIFY , AUX_SPARSIFY , AUX_TRANS ,
  AUX_TO_MEX , AUX_FROM_MEX , AUX_INTERPN , AUX_INTERPN_GRAD ,
  AUX_FLIP , AUX_INTERPN_WEIGHTS , AUX_LOW , AUX_INTERPN_INTERPOLATE ,
  AUX_DE_BOOR , AUX_TENSOR_TTV , AUX_ND_BOOR_EVAL , AUX_FINITE_DIFF ,
  AUX_QR , AUX_DET , AUX_QP , AUX_QRQP ,
  AUX_SOCP , AUX_NLP , AUX_SQPMETHOD , AUX_FEASIBLESQPMETHOD ,
  AUX_LDL , AUX_NEWTON , AUX_TO_DOUBLE , AUX_TO_INT ,
  AUX_CAST , AUX_SQ , AUX_SIGN , AUX_IF_ELSE ,
  AUX_PRINTF , AUX_FMIN , AUX_FMAX , AUX_FABS ,
  AUX_MIN , AUX_MAX , AUX_VFMIN , AUX_VFMAX ,
  AUX_MAX_VIOL , AUX_SUM_VIOL , AUX_SUM , AUX_REGULARIZE ,
  AUX_INF , AUX_NAN , AUX_REAL_MIN , AUX_ISINF ,
  AUX_ISFINITE , AUX_BOUNDS_CONSISTENCY , AUX_LSQR , AUX_FILE_SLURP ,
  AUX_CACHE , AUX_LOG1P , AUX_EXPM1 , AUX_HYPOT ,
  AUX_MMIN , AUX_MMAX , AUX_LOGSUMEXP , AUX_SPARSITY ,
  AUX_BFGS , AUX_ORACLE_CALLBACK , AUX_OCP_BLOCK , AUX_ORACLE ,
  AUX_SCALED_COPY , AUX_BLAZING_COMMON , AUX_BLAZING_1D_BOOR_EVAL , AUX_BLAZING_2D_BOOR_EVAL ,
  AUX_BLAZING_3D_BOOR_EVAL , AUX_BLAZING_4D_BOOR_EVAL , AUX_BLAZING_5D_BOOR_EVAL , AUX_PRINTME ,
  AUX_PRINT_SCALAR , AUX_PRINT_VECTOR , AUX_PRINT_CANONICAL , AUX_FPRINTF_SCALAR ,
  AUX_FPRINTF_VECTOR , AUX_TO_FILE , AUX_THREADS
}
 Auxiliary functions. More...
 

Public Member Functions

 CodeGenerator (const std::string &name, const Dict &opts=Dict())
 Constructor. More...
 
void add (const Function &f, bool with_jac_sparsity=false)
 Add a function (name generated) More...
 
void dump (std::ostream &s)
 Generate the code to a stream. More...
 
std::string dump ()
 Generate a file, return code as string. More...
 
std::string generate (const std::string &prefix="")
 Generate file(s) More...
 
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 <...> More...
 
std::string add_dependency (const Function &f)
 Add a function dependency. More...
 
void add_external (const std::string &new_external)
 Add an external function declaration. More...
 
std::string shorthand (const std::string &name) const
 Get a shorthand. More...
 
std::string shorthand (const std::string &name, bool allow_adding=true)
 Add/get a shorthand. More...
 
std::string sparsity (const Sparsity &sp, bool canonical=true)
 
casadi_int add_sparsity (const Sparsity &sp, bool canonical=true)
 
casadi_int get_sparsity (const Sparsity &sp) const
 Get the index of an existing sparsity pattern. More...
 
casadi_int get_constant (const std::vector< double > &v, bool allow_adding=false)
 Get or add a constant. More...
 
casadi_int get_constant (const std::vector< casadi_int > &v, bool allow_adding=false)
 Get or add an integer constant. More...
 
casadi_int get_constant (const std::vector< char > &v, bool allow_adding=false)
 Get or add a char constant. More...
 
casadi_int get_constant (const std::vector< std::string > &v, bool allow_adding=false)
 Get or add a vector<string> constant. More...
 
std::string constant (const std::vector< casadi_int > &v)
 Represent an array constant; adding it when new. More...
 
std::string constant (const std::vector< int > &v)
 Represent an array constant; adding it when new. More...
 
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. More...
 
std::string constant (const std::vector< double > &v)
 Represent an array constant; adding it when new. More...
 
std::string constant (const std::vector< char > &v)
 Represent an array constant; adding it when new. More...
 
std::string constant (const std::vector< std::string > &v)
 Represent an array constant; adding it when new. More...
 
void define_rom_double (const void *id, casadi_int size)
 Allocate file scope double read-only memory. More...
 
std::string rom_double (const void *id) const
 Access file scope double read-only memory. More...
 
void define_rom_integer (const void *id, casadi_int size)
 Allocate file scope integer read-only memory. More...
 
std::string rom_integer (const void *id) const
 Access file scope integer read-only memory. More...
 
void define_pool_double (const std::string &name, const std::vector< double > &def)
 Allocate file scope double writeable memory. More...
 
std::string pool_double (const std::string &name) const
 Access file scope double writeable memory. More...
 
void define_local_mutex (const Function &f, const std::string &name)
 Declare a static mutex associated with a function. More...
 
std::string local_mutex (const Function &f, const std::string &name) const
 Access a static mutex associated with a function. More...
 
const std::set< std::string > & local_mutexes (const Function &f) const
 Get all mutex names associated with a function. More...
 
void setup_callback (const std::string &s, const Function &f)
 Setup a callback. More...
 
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) More...
 
CodeGeneratoroperator<< (const std::string &s)
 Print a string to buffer. More...
 
void print_formatted (const std::string &s)
 Print without newline characters. More...
 
template<typename T >
CodeGeneratoroperator<< (T s)
 Print an arbitrary type to buffer. More...
 
void flush (std::ostream &s)
 Flush the buffer to a stream of choice. More...
 
void local (const std::string &name, const std::string &type, const std::string &ref="")
 Declare a local variable. More...
 
void scope_enter ()
 Enter a local scope. More...
 
void scope_exit ()
 Exit a local scope. More...
 
void scope_return (const std::string &value)
 Return from a scope with a value. More...
 
void scope_return ()
 Return from a scope without a value. More...
 
void scope_add_cleanup (const std::string &code)
 Add cleanup code to be executed upon scope exit. More...
 
std::string sx_work (casadi_int i)
 Declare a work vector element. More...
 
void init_local (const std::string &name, const std::string &def)
 Specify the default value for a local variable. More...
 
void indent ()
 Increase indentation. More...
 
void unindent ()
 Decrease indentation. More...
 
bool avoid_stack () const
 Avoid stack? More...
 
bool thread_safe () const
 Emit thead safe code chekout/release? More...
 
std::string constant (double v)
 Print a constant in a lossless but compact manner. More...
 
std::string constant (casadi_int v)
 
std::string constant (const std::string &v)
 
std::string constant (char v)
 
std::string format_padded (casadi_int i) const
 
std::string zeros (casadi_int sz)
 
std::string ones (casadi_int sz)
 
template<typename T >
std::string initializer (const std::vector< T > &v)
 Print an initializer. More...
 
std::string sanitize_source (const std::string &src, const std::vector< std::string > &inst, bool add_shorthand=true)
 Sanitize source files for codegen. More...
 
std::string dot (casadi_int n, const std::string &x, const std::string &y)
 Codegen inner product. More...
 
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. More...
 
std::string mv (const std::string &x, casadi_int nrow_x, casadi_int ncol_x, const std::string &y, const std::string &z, bool tr)
 Codegen dense matrix-vector multiplication. More...
 
std::string axpy (casadi_int n, const std::string &a, const std::string &x, const std::string &y)
 Codegen axpy: y += a*x. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
std::string scal (casadi_int n, const std::string &alpha, const std::string &x)
 What does scal do?? More...
 
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. More...
 
std::string mtimes (const std::string &x, casadi_int nrow_x, casadi_int ncol_x, const std::string &y, casadi_int ncol_y, const std::string &z, bool tr)
 Codegen dense matrix-matrix multiplication. More...
 
std::string mtimes_dense_sparse (const std::string &x, casadi_int nrow_x, const std::string &y, const Sparsity &sp_y, const std::string &z)
 Codegen dense-sparse matrix-matrix multiplication (z, x dense) More...
 
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. More...
 
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. More...
 
std::string bilin (const std::string &A, const Sparsity &sp_A, const std::string &x, const std::string &y)
 Codegen bilinear form. More...
 
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. More...
 
std::string logsumexp (const std::string &A, casadi_int n)
 
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. More...
 
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. More...
 
std::string trans (const std::string &x, const Sparsity &sp_x, const std::string &y, const Sparsity &sp_y, const std::string &iw)
 Transpose. More...
 
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. More...
 
std::string det (const std::string &sp_v, const std::string &v, const std::string &sp_r, const std::string &r, const std::string &beta)
 Determinant from sparse QR factors. More...
 
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. More...
 
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::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. More...
 
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. More...
 
std::string fmax (const std::string &x, const std::string &y)
 fmax More...
 
std::string fmin (const std::string &x, const std::string &y)
 fmin More...
 
std::string mmax (const std::string &x, casadi_int n, bool is_dense)
 mmax More...
 
std::string mmin (const std::string &x, casadi_int n, bool is_dense)
 mmin More...
 
std::string vfmax (const std::string &x, casadi_int n, const std::string &y)
 vfmax More...
 
std::string vfmin (const std::string &x, casadi_int n, const std::string &y)
 vfmin More...
 
std::string vfmax (const std::string &x, const std::string &n, const std::string &y)
 vfmax More...
 
std::string vfmin (const std::string &x, const std::string &n, const std::string &y)
 vfmin More...
 
std::string max (const std::string &x, const std::string &y)
 max More...
 
std::string min (const std::string &x, const std::string &y)
 min More...
 
std::string norm_inf (casadi_int n, const std::string &x)
 norm_inf More...
 
std::string norm_1 (casadi_int n, const std::string &x)
 norm_1 More...
 
std::string norm_2 (casadi_int n, const std::string &x)
 norm_2 More...
 
std::string max_viol (casadi_int n, const std::string &x, const std::string &lb, const std::string &ub)
 max_viol More...
 
std::string sum_viol (casadi_int n, const std::string &x, const std::string &lb, const std::string &ub)
 sum_viol More...
 
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 More...
 
std::string lb_eig (const Sparsity &sp_h, const std::string &h)
 lb_eig More...
 
std::string regularize (const Sparsity &sp_h, const std::string &h, const std::string &reg)
 regularize More...
 
std::string convexify_eval (const ConvexifyData &d, const std::string &Hin, const std::string &Hout, const std::string &iw, const std::string &w)
 convexify More...
 
std::string low (const std::string &x, const std::string &grid, casadi_int ng, casadi_int lookup_mode)
 low More...
 
std::string declare (std::string s)
 Declare a function. More...
 
void comment (const std::string &s)
 Write a comment line (ignored if not verbose) More...
 
void add_auxiliary (Auxiliary f, const std::vector< std::string > &inst={"casadi_real"})
 Add a built-in auxiliary function. More...
 
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. More...
 
std::string work (casadi_int n, casadi_int sz, bool is_ref) const
 
std::string workel (casadi_int n) const
 
void reserve_work (casadi_int n)
 Reserve a maximum size of work elements, used for padding of index. More...
 
void print_vector (std::ostream &s, const std::string &name, const std::vector< casadi_int > &v)
 Print casadi_int vector to a c file. More...
 
void print_vector (std::ostream &s, const std::string &name, const std::vector< char > &v)
 Print char vector to a c file. More...
 
void print_vector (std::ostream &s, const std::string &name, const std::vector< double > &v)
 Print real vector to a c file. More...
 
void print_vector (std::ostream &s, const std::string &name, const std::vector< std::string > &v)
 Print string vector to a c file. More...
 
std::string print_canonical (const Sparsity &sp, const std::string &arg)
 Print canonical representaion of a matrix. More...
 
std::string print_vector (casadi_int sz, const std::string &arg)
 Print canonical representaion of a vector. More...
 
std::string print_scalar (const std::string &arg)
 Print canonical representaion of a scalar. More...
 
std::string fprintf_scalar (const std::string &f, const std::string &arg)
 fprintf a normalized scalar (canonical nan/inf) to a file More...
 
std::string fprintf_vector (const std::string &f, casadi_int sz, const std::string &arg, const std::string &sep)
 fprintf a normalized vector to a file with a separator More...
 
std::string copy (const std::string &arg, std::size_t n, const std::string &res)
 Create a copy operation. More...
 
void copy_check (const std::string &arg, std::size_t n, const std::string &res, bool check_lhs=true, bool check_rhs=true)
 
void copy_default (const std::string &arg, std::size_t n, const std::string &res, const std::string &def, bool check_rhs=true)
 
bool elide_copy (casadi_int sz)
 
std::string fill (const std::string &res, std::size_t n, const std::string &v)
 Create a fill operation. More...
 
std::string clear (const std::string &res, std::size_t n)
 Create a fill operation. More...
 
std::string arg (casadi_int i) const
 Refer to argument. More...
 
std::string res (casadi_int i) const
 Refer to resuly. More...
 
std::string mem (const Function &f)
 Access thread-local memory. More...
 
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. More...
 
std::string tri_project (const std::string &arg, const Sparsity &sp_arg, const std::string &res, bool lower)
 Project triangular part. More...
 
std::string densify (const std::string &arg, const Sparsity &sp_arg, const std::string &res, bool tr=false)
 Densify. More...
 
std::string sparsify (const std::string &arg, const std::string &res, const Sparsity &sp_res, bool tr=false)
 Sparsify. More...
 
std::string to_mex (const Sparsity &sp, const std::string &arg)
 Create matrix in MATLAB's MEX format. More...
 
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. More...
 
std::string printf (const std::string &str, const std::vector< std::string > &arg=std::vector< std::string >())
 Printf. More...
 
std::string printf (const std::string &str, const std::string &arg1)
 
std::string printf (const std::string &str, const std::string &arg1, const std::string &arg2)
 
std::string printf (const std::string &str, const std::string &arg1, const std::string &arg2, const std::string &arg3)
 
std::string print_op (casadi_int op, const std::string &a0)
 Print an operation to a c file. More...
 
std::string print_op (casadi_int op, const std::string &a0, const std::string &a1)
 
std::string file_slurp (const std::string &fname, casadi_int n, const std::string &a)
 Slurp a file. More...
 
std::string to_file (const std::string &f, const Sparsity &sp, const std::string &x)
 Write matrix to file in MatrixMarket format. More...
 
void generate_dump (const Function &f, const std::string &arr, bool is_input)
 Generate dump_in or dump_out code for a function call. More...
 
void generate_print (const Function &f, const std::string &arr, bool is_input)
 Generate print_in or print_out code for a function call. More...
 
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 More...
 
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. More...
 
std::string wrapper (const Function &base, const std::string &name)
 

Static Public Member Functions

static std::string array (const std::string &type, const std::string &name, casadi_int len, const std::string &def=std::string())
 
static std::string fmu_helpers (const std::string &modelname)
 FMU helper functions. More...
 
static std::string casadi_version ()
 Current CasADi version as string. More...
 
static void stream_open (std::ostream &f, bool cpp)
 Print file header. More...
 
static void stream_close (std::ostream &f, bool cpp)
 Print file header. More...
 
static size_t hash (const std::vector< double > &v)
 
static size_t hash (const std::vector< casadi_int > &v)
 
static size_t hash (const std::vector< char > &v)
 
static size_t hash (const std::vector< std::string > &v)
 
template<typename T >
static bool equal (const std::vector< T > &v1, const std::vector< T > &v2)
 

Public Attributes

std::string name
 
std::string suffix
 
std::string casadi_real_type
 
std::string casadi_int_type
 
bool with_mem
 
bool with_header
 
bool mex
 
bool with_sfunction
 
std::vector< std::string > added_sfunctions
 
bool unroll_args
 
bool static_aux
 
bool inline_aux
 
bool verbose
 
bool verbose_runtime
 
bool cpp
 
bool main
 
bool include_math
 
bool avoid_stack_
 
std::string infinity
 
std::string nan
 
std::string real_min
 
bool codegen_scalars
 Codegen scalar. More...
 
bool with_export
 
bool with_import
 
casadi_int max_declarations_per_line
 
casadi_int max_initializer_elements_per_line
 
bool force_canonical
 
bool l1_blas
 
bool thread_safe_
 
std::string dll_export
 
std::string dll_import
 
std::string prefix
 
std::string dump_dir_prefix
 
std::string dump_dir_suffix
 
std::stringstream includes
 
std::stringstream auxiliaries
 
std::stringstream body
 
std::stringstream header
 
std::stringstream buffer
 
bool newline_
 
casadi_int indent_
 
casadi_int current_indent_
 
casadi_int sz_zeros_
 
casadi_int sz_ones_
 
casadi_int padding_length_
 
std::vector< std::string > exposed_fname
 
std::set< std::string > sparsity_meta
 
std::set< std::string > added_includes_
 
std::set< std::string > added_externals_
 
std::set< std::string > added_shorthands_
 
std::multimap< Auxiliary, std::vector< std::string > > added_auxiliaries_
 
std::multimap< size_t, size_t > added_double_constants_
 
std::multimap< size_t, size_t > added_integer_constants_
 
std::multimap< size_t, size_t > added_char_constants_
 
std::multimap< size_t, size_t > added_string_constants_
 
std::map< std::string, std::pair< std::string, std::string > > local_variables_
 
std::vector< std::string > local_cleanup_
 
bool local_void_
 
std::map< std::string, std::string > local_default_
 
std::map< const void *, casadi_int > file_scope_double_
 
std::map< const void *, casadi_int > file_scope_integer_
 
std::vector< std::vector< double > > pool_double_defaults_
 
std::map< std::string, casadi_int > pool_double_
 
std::map< const FunctionInternal *, std::set< std::string > > local_mutexes_
 
std::vector< FunctionMetaadded_functions_
 
std::map< std::string, std::map< FunctionInternal *, casadi_int > > added_wrappers_
 
std::vector< std::vector< double > > double_constants_
 
std::vector< std::vector< casadi_int > > integer_constants_
 
std::vector< std::vector< char > > char_constants_
 
std::vector< std::vector< std::string > > string_constants_
 
bool needs_mem_
 

Member Enumeration Documentation

◆ Auxiliary

Extra doc: https://github.com/casadi/casadi/wiki/L_to

Enumerator
AUX_COPY 
AUX_CVX 
AUX_CONVEXIFY 
AUX_SWAP 
AUX_SCAL 
AUX_AXPY 
AUX_DOT 
AUX_BILIN 
AUX_KRON 
AUX_KRON_DENSE 
AUX_KRON_DENSE_SPARSE 
AUX_KRON_SPARSE_DENSE 
AUX_KRON_CONTRACT_INNER 
AUX_KRON_CONTRACT_INNER_DENSE 
AUX_KRON_CONTRACT_INNER_DENSE_SPARSE 
AUX_KRON_CONTRACT_INNER_SPARSE_DENSE 
AUX_KRON_CONTRACT_OUTER 
AUX_KRON_CONTRACT_OUTER_DENSE 
AUX_KRON_CONTRACT_OUTER_DENSE_SPARSE 
AUX_KRON_CONTRACT_OUTER_SPARSE_DENSE 
AUX_RANK1 
AUX_NORM_1 
AUX_NORM_2 
AUX_CLIP_MAX 
AUX_CLIP_MIN 
AUX_VECTOR_FMAX 
AUX_VECTOR_FMIN 
AUX_NORM_INF 
AUX_MASKED_NORM_INF 
AUX_IAMAX 
AUX_CLEAR 
AUX_FILL 
AUX_MV 
AUX_MV_DENSE 
AUX_MTIMES 
AUX_MTIMES_DENSE 
AUX_MTIMES_DENSE_SPARSE 
AUX_TRILSOLVE 
AUX_TRIUSOLVE 
AUX_PROJECT 
AUX_TRI_PROJECT 
AUX_DENSIFY 
AUX_SPARSIFY 
AUX_TRANS 
AUX_TO_MEX 
AUX_FROM_MEX 
AUX_INTERPN 
AUX_INTERPN_GRAD 
AUX_FLIP 
AUX_INTERPN_WEIGHTS 
AUX_LOW 
AUX_INTERPN_INTERPOLATE 
AUX_DE_BOOR 
AUX_TENSOR_TTV 
AUX_ND_BOOR_EVAL 
AUX_FINITE_DIFF 
AUX_QR 
AUX_DET 
AUX_QP 
AUX_QRQP 
AUX_SOCP 
AUX_NLP 
AUX_SQPMETHOD 
AUX_FEASIBLESQPMETHOD 
AUX_LDL 
AUX_NEWTON 
AUX_TO_DOUBLE 
AUX_TO_INT 
AUX_CAST 
AUX_SQ 
AUX_SIGN 
AUX_IF_ELSE 
AUX_PRINTF 
AUX_FMIN 
AUX_FMAX 
AUX_FABS 
AUX_MIN 
AUX_MAX 
AUX_VFMIN 
AUX_VFMAX 
AUX_MAX_VIOL 
AUX_SUM_VIOL 
AUX_SUM 
AUX_REGULARIZE 
AUX_INF 
AUX_NAN 
AUX_REAL_MIN 
AUX_ISINF 
AUX_ISFINITE 
AUX_BOUNDS_CONSISTENCY 
AUX_LSQR 
AUX_FILE_SLURP 
AUX_CACHE 
AUX_LOG1P 
AUX_EXPM1 
AUX_HYPOT 
AUX_MMIN 
AUX_MMAX 
AUX_LOGSUMEXP 
AUX_SPARSITY 
AUX_BFGS 
AUX_ORACLE_CALLBACK 
AUX_OCP_BLOCK 
AUX_ORACLE 
AUX_SCALED_COPY 
AUX_BLAZING_COMMON 
AUX_BLAZING_1D_BOOR_EVAL 
AUX_BLAZING_2D_BOOR_EVAL 
AUX_BLAZING_3D_BOOR_EVAL 
AUX_BLAZING_4D_BOOR_EVAL 
AUX_BLAZING_5D_BOOR_EVAL 
AUX_PRINTME 
AUX_PRINT_SCALAR 
AUX_PRINT_VECTOR 
AUX_PRINT_CANONICAL 
AUX_FPRINTF_SCALAR 
AUX_FPRINTF_VECTOR 
AUX_TO_FILE 
AUX_THREADS 

Definition at line 675 of file code_generator.hpp.

675  {
676  AUX_COPY,
677  AUX_CVX,
679  AUX_SWAP,
680  AUX_SCAL,
681  AUX_AXPY,
682  AUX_DOT,
683  AUX_BILIN,
684  AUX_KRON,
696  AUX_RANK1,
697  AUX_NORM_1,
698  AUX_NORM_2,
699  AUX_CLIP_MAX,
700  AUX_CLIP_MIN,
703  AUX_NORM_INF,
705  AUX_IAMAX,
706  AUX_CLEAR,
707  AUX_FILL,
708  AUX_MV,
709  AUX_MV_DENSE,
710  AUX_MTIMES,
715  AUX_PROJECT,
717  AUX_DENSIFY,
718  AUX_SPARSIFY,
719  AUX_TRANS,
720  AUX_TO_MEX,
721  AUX_FROM_MEX,
722  AUX_INTERPN,
724  AUX_FLIP,
726  AUX_LOW,
728  AUX_DE_BOOR,
732  AUX_QR,
733  AUX_DET,
734  AUX_QP,
735  AUX_QRQP,
736  AUX_SOCP,
737  AUX_NLP,
740  AUX_LDL,
741  AUX_NEWTON,
743  AUX_TO_INT,
744  AUX_CAST,
745  AUX_SQ,
746  AUX_SIGN,
747  AUX_IF_ELSE,
748  AUX_PRINTF,
749  AUX_FMIN,
750  AUX_FMAX,
751  AUX_FABS,
752  AUX_MIN,
753  AUX_MAX,
754  AUX_VFMIN,
755  AUX_VFMAX,
756  AUX_MAX_VIOL,
757  AUX_SUM_VIOL,
758  AUX_SUM,
760  AUX_INF,
761  AUX_NAN,
762  AUX_REAL_MIN,
763  AUX_ISINF,
764  AUX_ISFINITE,
766  AUX_LSQR,
768  AUX_CACHE,
769  AUX_LOG1P,
770  AUX_EXPM1,
771  AUX_HYPOT,
772  AUX_MMIN,
773  AUX_MMAX,
775  AUX_SPARSITY,
776  AUX_BFGS,
779  AUX_ORACLE,
787  AUX_PRINTME,
793  AUX_TO_FILE,
795  };

Constructor & Destructor Documentation

◆ CodeGenerator()

casadi::CodeGenerator::CodeGenerator ( const std::string &  name,
const Dict opts = Dict() 
)

Definition at line 38 of file code_generator.cpp.

38  {
39  // Default options
40  this->verbose = true;
41  this->verbose_runtime = false;
42  this->mex = false;
43  this->with_sfunction = false;
44  this->unroll_args = false;
45  this->static_aux = false;
46  this->inline_aux = false;
47  this->cpp = false;
48  this->main = false;
49  this->casadi_real_type = "double";
50  this->casadi_int_type = CASADI_INT_TYPE_STR;
51  this->codegen_scalars = false;
52  this->with_header = false;
53  this->with_mem = false;
54  this->with_export = true;
55  this->with_import = false;
56  this->include_math = true;
57  this->infinity = "INFINITY";
58  this->nan = "NAN";
59  this->real_min = "";
60  bool prefix_set = false;
61  this->prefix = "";
62  this->dump_dir_prefix = "";
63  this->dump_dir_suffix = "";
64  this->max_declarations_per_line = 12;
66  this->force_canonical = false;
67  this->l1_blas = false;
68 
69  avoid_stack_ = false;
70  indent_ = 2;
71  sz_zeros_ = 0;
72  sz_ones_ = 0;
73  thread_safe_ = false;
74 
75  // Read options
76  for (auto&& e : opts) {
77  if (e.first=="verbose") {
78  this->verbose = e.second;
79  } else if (e.first=="verbose_runtime") {
80  this->verbose_runtime = e.second;
81  } else if (e.first=="mex") {
82  this->mex = e.second;
83  } else if (e.first=="with_sfunction") {
84  this->with_sfunction = e.second;
85  } else if (e.first=="unroll_args") {
86  this->unroll_args = e.second;
87  } else if (e.first=="static_aux") {
88  this->static_aux = e.second;
89  } else if (e.first=="inline_aux") {
90  this->inline_aux = e.second;
91  } else if (e.first=="cpp") {
92  this->cpp = e.second;
93  } else if (e.first=="main") {
94  this->main = e.second;
95  } else if (e.first=="casadi_real") {
96  this->casadi_real_type = e.second.to_string();
97  } else if (e.first=="casadi_int") {
98  this->casadi_int_type = e.second.to_string();
99  } else if (e.first=="codegen_scalars") {
100  this->codegen_scalars = e.second;
101  } else if (e.first=="with_header") {
102  this->with_header = e.second;
103  } else if (e.first=="with_mem") {
104  this->with_mem = e.second;
105  } else if (e.first=="with_export") {
106  this->with_export = e.second;
107  } else if (e.first=="with_import") {
108  this->with_import = e.second;
109  } else if (e.first=="include_math") {
110  this->include_math = e.second;
111  } else if (e.first=="infinity") {
112  this->infinity = e.second.to_string();
113  } else if (e.first=="nan") {
114  this->nan = e.second.to_string();
115  } else if (e.first=="real_min") {
116  this->real_min = e.second.to_string();
117  } else if (e.first=="indent") {
118  indent_ = e.second;
119  casadi_assert_dev(indent_>=0);
120  } else if (e.first=="avoid_stack") {
121  avoid_stack_ = e.second;
122  } else if (e.first=="prefix") {
123  this->prefix = e.second.to_string();
124  prefix_set = true;
125  } else if (e.first=="dump_dir_prefix") {
126  this->dump_dir_prefix = e.second.to_string();
127  } else if (e.first=="dump_dir_suffix") {
128  this->dump_dir_suffix = e.second.to_string();
129  } else if (e.first=="max_declarations_per_line") {
130  this->max_declarations_per_line = e.second;
131  casadi_assert(this->max_declarations_per_line>=0,
132  "Option max_declarations_per_line must be >=0");
133  } else if (e.first=="max_initializer_elements_per_line") {
134  this->max_initializer_elements_per_line = e.second;
135  casadi_assert(this->max_initializer_elements_per_line>=0,
136  "Option max_initializer_elements_per_line must be >=0");
137  } else if (e.first=="force_canonical") {
138  this->force_canonical = e.second;
139  } else if (e.first=="l1_blas") {
140  this->l1_blas = e.second;
141  } else if (e.first=="thread_safe") {
142  thread_safe_ = e.second;
143  } else {
144  casadi_error("Unrecognized option: " + str(e.first));
145  }
146  }
147 
148  if (with_mem && !force_canonical) {
149  casadi_error("Codegen options 'with_mem' and 'force_canonical=false' (the default) "
150  "are incompatible. If you rely on with_mem, please explicitly set force_canonical=true.");
151  }
152 
153  // If real_min is not specified, make an educated guess
154  if (this->real_min.empty()) {
155  std::stringstream ss;
156  ss << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1);
157  if (casadi_real_type=="float") {
158  ss << std::numeric_limits<float>::min();
159  this->real_min = ss.str();
160  } else if (casadi_real_type=="double") {
161  ss << std::numeric_limits<double>::min();
162  this->real_min = ss.str();
163  } else {
164  this->real_min = "<NOT SPECIFIED>";
165  }
166  }
167 
169 
170  // Start at new line with no indentation
171  newline_ = true;
172  current_indent_ = 0;
173 
174  // Start off without the need for thread-local memory
175  needs_mem_ = false;
176 
177  // Divide name into base and suffix (if any)
178  std::string::size_type dotpos = name.rfind('.');
179  if (dotpos==std::string::npos) {
180  this->name = name;
181  this->suffix = this->cpp ? ".cpp" : ".c";
182  } else {
183  this->name = name.substr(0, dotpos);
184  this->suffix = name.substr(dotpos);
185  }
186 
187  // Symbol prefix
188  if (this->with_export) dll_export = "CASADI_SYMBOL_EXPORT ";
189  if (this->with_import) dll_import = "CASADI_SYMBOL_IMPORT ";
190 
191  // Make sure that the base name is sane
192  casadi_assert_dev(Function::check_name(this->name));
193 
194  // Includes needed
195  if (this->include_math) add_include("math.h");
196  if (this->main) add_include("stdio.h");
198 
199  // Mex and main need string.h
200  if (this->mex || this->main) {
201  add_include("string.h");
202  }
203 
204  // Mex
205  if (this->mex) {
206  add_include("mex.h", false, "MATLAB_MEX_FILE");
207  }
208 
209  // s-Function
210  if (this->with_sfunction) {
211  this->casadi_real_type = "real_T";
212  this->casadi_int_type = "int_T";
213  this->with_header = true;
214  add_include("simstruc.h");
215  }
216 
217  // Memory struct entry point
218  if (this->with_mem) {
219  this->header << "#include <casadi/mem.h>\n";
220  }
221 
222  // Use name as default prefix
223  if (!prefix_set) {
224  this->prefix = this->name;
225  }
226 
227  }
bool codegen_scalars
Codegen scalar.
casadi_int max_initializer_elements_per_line
std::stringstream header
casadi_int max_declarations_per_line
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 <....
void add_auxiliary(Auxiliary f, const std::vector< std::string > &inst={"casadi_real"})
Add a built-in auxiliary function.
static bool check_name(const std::string &name)
Check if a string is a valid function name.
Definition: function.cpp:1513
std::string str(const T &v)
String representation, any type.

References add_auxiliary(), add_include(), AUX_PRINTF, AUX_THREADS, avoid_stack_, casadi_int_type, casadi_real_type, casadi::Function::check_name(), codegen_scalars, cpp, current_indent_, dll_export, dll_import, dump_dir_prefix, dump_dir_suffix, force_canonical, header, include_math, indent_, infinity, inline_aux, l1_blas, main, max_declarations_per_line, max_initializer_elements_per_line, mex, name, nan, needs_mem_, newline_, prefix, real_min, static_aux, casadi::str(), suffix, sz_ones_, sz_zeros_, thread_safe_, unroll_args, verbose, verbose_runtime, with_export, with_header, with_import, with_mem, and with_sfunction.

Member Function Documentation

◆ add()

void casadi::CodeGenerator::add ( const Function f,
bool  with_jac_sparsity = false 
)

Definition at line 399 of file code_generator.cpp.

399  {
400  // Add if not already added
401  std::string codegen_name = add_dependency(f);
402 
403  // Define function
404  *this << declare(f->signature(f.name())) << "{\n"
405  << "return " << codegen_name << "(arg, res, iw, w, mem);\n"
406  << "}\n\n";
407 
408  if (this->unroll_args) {
409  // Define function
410  *this << declare(f->signature_unrolled(f.name())) << "{\n";
411  for (casadi_int i=0; i<f.n_in(); ++i) {
412  *this << "arg[" << i << "] = " << f.name_in(i) << ";\n";
413  }
414  for (casadi_int i=0; i<f.n_out(); ++i) {
415  *this << "res[" << i << "] = " << f.name_out(i) << ";\n";
416  }
417  *this << "return " << codegen_name << "(arg, res, iw, w, mem);\n";
418  *this << "}\n\n";
419  // Flush buffers
420  flush(this->body);
421  }
422 
423  // Generate meta information
424  f->codegen_meta(*this);
425 
426  // Generate Jacobian sparsity information
427  if (with_jac_sparsity) {
428  // Generate/get Jacobian sparsity
429  std::vector<Sparsity> jac = f.jac_sparsity();
430  // Code generate the sparsity pattern
431  add_io_sparsities("jac_" + f.name(), f->sparsity_in_, jac);
432 
433  // Flush buffers
434  flush(this->body);
435  }
436 
437  // Generate function specific code for Simulink sfunction
438  if (this->with_sfunction) this->added_sfunctions.push_back( this->codegen_sfunction(f) );
439 
440  // Add to list of exposed symbols
441  this->exposed_fname.push_back(f.name());
442  }
std::string add_dependency(const Function &f)
Add a function dependency.
std::vector< std::string > added_sfunctions
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 flush(std::ostream &s)
Flush the buffer to a stream of choice.
std::string declare(std::string s)
Declare a function.
std::stringstream body
std::vector< std::string > exposed_fname

References add_dependency(), add_io_sparsities(), added_sfunctions, body, casadi::FunctionInternal::codegen_meta(), declare(), exposed_fname, flush(), casadi::Function::jac_sparsity(), casadi::Function::n_in(), casadi::Function::n_out(), casadi::Function::name(), casadi::Function::name_in(), casadi::Function::name_out(), casadi::FunctionInternal::signature(), casadi::FunctionInternal::signature_unrolled(), casadi::FunctionInternal::sparsity_in_, unroll_args, and with_sfunction.

Referenced by casadi::DaeBuilderInternal::export_fmu(), casadi::FunctionInternal::finalize(), casadi::Function::generate(), casadi::OracleFunction::generate_dependencies(), and casadi::Scpgen::init().

◆ add_auxiliary()

void casadi::CodeGenerator::add_auxiliary ( Auxiliary  f,
const std::vector< std::string > &  inst = {"casadi_real"} 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tp

Definition at line 1504 of file code_generator.cpp.

1504  {
1505  // Look for existing instantiations
1506  auto f_match = added_auxiliaries_.equal_range(f);
1507  // Look for duplicates
1508  for (auto it=f_match.first; it!=f_match.second; ++it) {
1509  if (it->second==inst) return;
1510  }
1511  added_auxiliaries_.insert(std::make_pair(f, inst));
1512 
1513  // Add the appropriate function
1514  switch (f) {
1515  case AUX_COPY:
1516  if (this->l1_blas) {
1517  Blas::codegen_copy_aux(*this, inst);
1518  } else {
1519  this->auxiliaries << sanitize_source(casadi_copy_str, inst);
1520  }
1521  break;
1522  case AUX_SCALED_COPY:
1523  this->auxiliaries << sanitize_source(casadi_scaled_copy_str, inst);
1524  break;
1525  case AUX_SWAP:
1526  this->auxiliaries << sanitize_source(casadi_swap_str, inst);
1527  break;
1528  case AUX_SCAL:
1529  if (!(this->l1_blas && Blas::codegen_scal_aux(*this, inst)))
1530  this->auxiliaries << sanitize_source(casadi_scal_str, inst);
1531  break;
1532  case AUX_AXPY:
1533  if (!(this->l1_blas && Blas::codegen_axpy_aux(*this, inst)))
1534  this->auxiliaries << sanitize_source(casadi_axpy_str, inst);
1535  break;
1536  case AUX_DOT:
1537  if (!(this->l1_blas && Blas::codegen_dot_aux(*this, inst)))
1538  this->auxiliaries << sanitize_source(casadi_dot_str, inst);
1539  break;
1540  case AUX_BILIN:
1541  this->auxiliaries << sanitize_source(casadi_bilin_str, inst);
1542  break;
1543  case AUX_KRON:
1544  this->auxiliaries << sanitize_source(casadi_kron_str, inst);
1545  break;
1546  case AUX_KRON_DENSE:
1547  this->auxiliaries << sanitize_source(casadi_kron_dense_str, inst);
1548  break;
1549  case AUX_KRON_DENSE_SPARSE:
1550  this->auxiliaries << sanitize_source(casadi_kron_dense_sparse_str, inst);
1551  break;
1552  case AUX_KRON_SPARSE_DENSE:
1553  this->auxiliaries << sanitize_source(casadi_kron_sparse_dense_str, inst);
1554  break;
1556  this->auxiliaries << sanitize_source(casadi_kron_contract_inner_str, inst);
1557  break;
1559  this->auxiliaries << sanitize_source(casadi_kron_contract_inner_dense_str, inst);
1560  break;
1562  this->auxiliaries << sanitize_source(casadi_kron_contract_inner_dense_sparse_str, inst);
1563  break;
1565  this->auxiliaries << sanitize_source(casadi_kron_contract_inner_sparse_dense_str, inst);
1566  break;
1568  this->auxiliaries << sanitize_source(casadi_kron_contract_outer_str, inst);
1569  break;
1571  this->auxiliaries << sanitize_source(casadi_kron_contract_outer_dense_str, inst);
1572  break;
1574  this->auxiliaries << sanitize_source(casadi_kron_contract_outer_dense_sparse_str, inst);
1575  break;
1577  this->auxiliaries << sanitize_source(casadi_kron_contract_outer_sparse_dense_str, inst);
1578  break;
1579  case AUX_RANK1:
1580  this->auxiliaries << sanitize_source(casadi_rank1_str, inst);
1581  break;
1582  case AUX_IAMAX:
1584  this->auxiliaries << sanitize_source(casadi_iamax_str, inst);
1585  break;
1586  case AUX_INTERPN:
1589  add_auxiliary(AUX_FLIP, {});
1591  add_auxiliary(AUX_CLEAR, {"casadi_int"});
1592  this->auxiliaries << sanitize_source(casadi_interpn_str, inst);
1593  break;
1594  case AUX_INTERPN_GRAD:
1596  this->auxiliaries << sanitize_source(casadi_interpn_grad_str, inst);
1597  break;
1598  case AUX_DE_BOOR:
1599  this->auxiliaries << sanitize_source(casadi_de_boor_str, inst);
1600  break;
1601  case AUX_TENSOR_TTV:
1602  this->auxiliaries << sanitize_source(casadi_tensor_ttv_str, inst);
1603  break;
1604  case AUX_ND_BOOR_EVAL:
1610  this->auxiliaries << sanitize_source(casadi_nd_boor_eval_str, inst);
1611  break;
1612  case AUX_FLIP:
1613  this->auxiliaries << sanitize_source(casadi_flip_str, inst);
1614  break;
1615  case AUX_LOW:
1616  this->auxiliaries << sanitize_source(casadi_low_str, inst);
1617  break;
1618  case AUX_INTERPN_WEIGHTS:
1620  this->auxiliaries << sanitize_source(casadi_interpn_weights_str, inst);
1621  break;
1623  this->auxiliaries << sanitize_source(casadi_interpn_interpolate_str, inst);
1624  break;
1625  case AUX_NORM_1:
1626  if (!(this->l1_blas && Blas::codegen_norm_1_aux(*this, inst))) {
1628  this->auxiliaries << sanitize_source(casadi_norm_1_str, inst);
1629  }
1630  break;
1631  case AUX_NORM_2:
1632  if (!(this->l1_blas && Blas::codegen_norm_2_aux(*this, inst))) {
1634  this->auxiliaries << sanitize_source(casadi_norm_2_str, inst);
1635  }
1636  break;
1637  case AUX_NORM_INF:
1640  this->auxiliaries << sanitize_source(casadi_norm_inf_str, inst);
1641  break;
1642  case AUX_VECTOR_FMAX:
1644  this->auxiliaries << sanitize_source(casadi_vector_fmax_str, inst);
1645  break;
1646  case AUX_VECTOR_FMIN:
1648  this->auxiliaries << sanitize_source(casadi_vector_fmin_str, inst);
1649  break;
1650  case AUX_MASKED_NORM_INF:
1653  this->auxiliaries << sanitize_source(casadi_masked_norm_inf_str, inst);
1654  break;
1655  case AUX_CLIP_MIN:
1656  // add_auxiliary(AUX_CLIP_MIN);
1657  this->auxiliaries << sanitize_source(casadi_clip_min_str, inst);
1658  break;
1659  case AUX_CLIP_MAX:
1660  // add_auxiliary(AUX_CLIP_MAX);
1661  this->auxiliaries << sanitize_source(casadi_clip_max_str, inst);
1662  break;
1663  case AUX_CLEAR:
1664  this->auxiliaries << sanitize_source(casadi_clear_str, inst);
1665  break;
1666  case AUX_FILL:
1667  this->auxiliaries << sanitize_source(casadi_fill_str, inst);
1668  break;
1669  case AUX_MV:
1670  this->auxiliaries << sanitize_source(casadi_mv_str, inst);
1671  break;
1672  case AUX_MV_DENSE:
1673  this->auxiliaries << sanitize_source(casadi_mv_dense_str, inst);
1674  break;
1675  case AUX_MTIMES:
1676  this->auxiliaries << sanitize_source(casadi_mtimes_str, inst);
1677  break;
1678  case AUX_MTIMES_DENSE:
1679  this->auxiliaries << sanitize_source(casadi_mtimes_dense_str, inst);
1680  break;
1682  this->auxiliaries << sanitize_source(casadi_mtimes_dense_sparse_str, inst);
1683  break;
1684  case AUX_TRILSOLVE:
1685  this->auxiliaries << sanitize_source(casadi_trilsolve_str, inst);
1686  break;
1687  case AUX_TRIUSOLVE:
1688  this->auxiliaries << sanitize_source(casadi_triusolve_str, inst);
1689  break;
1690  case AUX_PROJECT:
1691  this->auxiliaries << sanitize_source(casadi_project_str, inst);
1692  break;
1693  case AUX_TRI_PROJECT:
1694  this->auxiliaries << sanitize_source(casadi_tri_project_str, inst);
1695  break;
1696  case AUX_DENSIFY:
1699  {
1700  std::vector<std::string> inst2 = inst;
1701  if (inst.size()==1) inst2.push_back(inst[0]);
1702  this->auxiliaries << sanitize_source(casadi_densify_str, inst2);
1703  }
1704  break;
1705  case AUX_SPARSIFY:
1707  {
1708  std::vector<std::string> inst2 = inst;
1709  if (inst.size()==1) inst2.push_back(inst[0]);
1710  this->auxiliaries << sanitize_source(casadi_sparsify_str, inst2);
1711  }
1712  break;
1713  case AUX_TRANS:
1714  this->auxiliaries << sanitize_source(casadi_trans_str, inst);
1715  break;
1716  case AUX_TO_MEX:
1718  this->auxiliaries << "#ifdef MATLAB_MEX_FILE\n"
1719  << sanitize_source(casadi_to_mex_str, inst)
1720  << "#endif\n\n";
1721  break;
1722  case AUX_FROM_MEX:
1724  this->auxiliaries << "#ifdef MATLAB_MEX_FILE\n"
1725  << sanitize_source(casadi_from_mex_str, inst)
1726  << "#endif\n\n";
1727  break;
1728  case AUX_FINITE_DIFF:
1733  this->auxiliaries << sanitize_source(casadi_finite_diff_str, inst);
1734  break;
1735  case AUX_QR:
1740  this->auxiliaries << sanitize_source(casadi_qr_str, inst);
1741  break;
1742  case AUX_DET:
1743  this->auxiliaries << sanitize_source(casadi_det_str, inst);
1744  break;
1745  case AUX_LSQR:
1752  this->auxiliaries << sanitize_source(casadi_lsqr_str, inst);
1753  break;
1754  case AUX_QP:
1755  this->auxiliaries << sanitize_source(casadi_qp_str, inst);
1756  break;
1757  case AUX_SOCP:
1759  this->auxiliaries << sanitize_source(casadi_socp_str, inst);
1760  break;
1761  case AUX_QRQP:
1776  add_include("stdarg.h");
1777  add_include("stdio.h");
1778  add_include("math.h");
1779 
1780  this->auxiliaries << sanitize_source(casadi_qrqp_str, inst);
1781  break;
1782  case AUX_NLP:
1784  this->auxiliaries << sanitize_source(casadi_nlp_str, inst);
1785  break;
1786  case AUX_SQPMETHOD:
1791  this->auxiliaries << sanitize_source(casadi_sqpmethod_str, inst);
1792  break;
1793  case AUX_FEASIBLESQPMETHOD:
1796  this->auxiliaries << sanitize_source(casadi_feasiblesqpmethod_str, inst);
1797  break;
1798  case AUX_LDL:
1799  this->auxiliaries << sanitize_source(casadi_ldl_str, inst);
1800  break;
1801  case AUX_NEWTON:
1806  this->auxiliaries << sanitize_source(casadi_newton_str, inst);
1807  break;
1808  case AUX_MAX_VIOL:
1810  this->auxiliaries << sanitize_source(casadi_max_viol_str, inst);
1811  break;
1812  case AUX_SUM_VIOL:
1813  this->auxiliaries << sanitize_source(casadi_sum_viol_str, inst);
1814  break;
1815  case AUX_SUM:
1816  this->auxiliaries << sanitize_source(casadi_sum_str, inst);
1817  break;
1818  case AUX_VFMIN:
1820  this->auxiliaries << sanitize_source(casadi_vfmin_str, inst);
1821  break;
1822  case AUX_VFMAX:
1824  this->auxiliaries << sanitize_source(casadi_vfmax_str, inst);
1825  break;
1826  case AUX_REGULARIZE:
1829  this->auxiliaries << sanitize_source(casadi_regularize_str, inst);
1830  break;
1835  this->auxiliaries << sanitize_source(casadi_bound_consistency_str, inst);
1836  break;
1837  case AUX_FILE_SLURP:
1838  add_include("stdio.h");
1839  this->auxiliaries << sanitize_source(casadi_file_slurp_str, inst);
1840  break;
1841  case AUX_CACHE:
1842  this->auxiliaries << sanitize_source(casadi_cache_str, inst);
1843  break;
1844  case AUX_CVX:
1851  this->auxiliaries << sanitize_source(casadi_cvx_str, inst);
1852  break;
1853  case AUX_CONVEXIFY:
1858  this->auxiliaries << sanitize_source(casadi_convexify_str, inst);
1859  break;
1860  case AUX_LOGSUMEXP:
1863  this->auxiliaries << sanitize_source(casadi_logsumexp_str, inst);
1864  break;
1865  case AUX_SPARSITY:
1866  this->auxiliaries << sanitize_source(casadi_sparsity_str, inst);
1867  break;
1868  case AUX_BFGS:
1877  this->auxiliaries << sanitize_source(casadi_bfgs_str, inst);
1878  break;
1879  case AUX_ORACLE:
1880  this->auxiliaries << sanitize_source(casadi_oracle_str, inst);
1881  break;
1882  case AUX_ORACLE_CALLBACK:
1883  this->auxiliaries << sanitize_source(casadi_oracle_callback_str, inst);
1884  break;
1885  case AUX_OCP_BLOCK:
1886  this->auxiliaries << sanitize_source(casadi_ocp_block_str, inst);
1887  break;
1888  case AUX_TO_DOUBLE:
1889  this->auxiliaries << "#define casadi_to_double(x) "
1890  << "(" << (this->cpp ? "static_cast<double>(x)" : "(double) x") << ")\n\n";
1891  break;
1892  case AUX_TO_INT:
1893  this->auxiliaries << "#define casadi_to_int(x) "
1894  << "(" << (this->cpp ? "static_cast<casadi_int>(x)" : "(casadi_int) x")
1895  << ")\n\n";
1896  break;
1897  case AUX_CAST:
1898  this->auxiliaries << "#define CASADI_CAST(x,y) "
1899  << "(" << (this->cpp ? "static_cast<x>(y)" : "(x) y") << ")\n\n";
1900  break;
1901  case AUX_SQ:
1902  this->auxiliaries << sanitize_source(
1903  "// SYMBOL \"sq\"\n"
1904  "casadi_real casadi_sq(casadi_real x) { return x*x;}\n\n",
1905  inst);
1906  break;
1907  case AUX_SIGN:
1908  this->auxiliaries << sanitize_source(
1909  "// SYMBOL \"sign\"\n"
1910  "casadi_real casadi_sign(casadi_real x) "
1911  "{ return x<0 ? -1 : x>0 ? 1 : x;}\n\n",
1912  inst);
1913  break;
1914  case AUX_IF_ELSE:
1915  this->auxiliaries << sanitize_source(
1916  "// SYMBOL \"if_else\"\n"
1917  "casadi_real casadi_if_else"
1918  "(casadi_real c, casadi_real x, casadi_real y) "
1919  "{ return c!=0 ? x : y;}\n\n",
1920  inst);
1921  break;
1922  case AUX_PRINTF:
1923  this->auxiliaries << "#ifndef CASADI_PRINTF\n";
1924  if (this->mex) {
1925  this->auxiliaries << "#ifdef MATLAB_MEX_FILE\n"
1926  << " #define CASADI_PRINTF mexPrintf\n"
1927  << "#else\n"
1928  << " #define CASADI_PRINTF printf\n"
1929  << "#endif\n";
1930  } else {
1931  add_include("stdio.h");
1932  this->auxiliaries << "#define CASADI_PRINTF printf\n";
1933  this->auxiliaries << "#ifndef CASADI_SNPRINTF\n";
1934  this->auxiliaries << "#define CASADI_SNPRINTF snprintf\n";
1935  this->auxiliaries << "#endif\n\n";
1936  }
1937  this->auxiliaries << "#endif\n\n";
1938  break;
1939  case AUX_FMIN:
1940  this->auxiliaries << sanitize_source(
1941  "// SYMBOL \"fmin\"\n"
1942  "casadi_real casadi_fmin(casadi_real x, casadi_real y) {\n"
1943  "/* Pre-c99 compatibility */\n"
1944  "#if __STDC_VERSION__ < 199901L\n"
1945  " return x<y ? x : y;\n"
1946  "#else\n"
1947  " return fmin(x, y);\n"
1948  "#endif\n"
1949  "}\n\n",
1950  inst);
1951  break;
1952  case AUX_FMAX:
1953  this->auxiliaries << sanitize_source(
1954  "// SYMBOL \"fmax\"\n"
1955  "casadi_real casadi_fmax(casadi_real x, casadi_real y) {\n"
1956  "/* Pre-c99 compatibility */\n"
1957  "#if __STDC_VERSION__ < 199901L\n"
1958  " return x>y ? x : y;\n"
1959  "#else\n"
1960  " return fmax(x, y);\n"
1961  "#endif\n"
1962  "}\n\n",
1963  inst);
1964  break;
1965  case AUX_FABS:
1966  this->auxiliaries << sanitize_source(
1967  "// SYMBOL \"fabs\"\n"
1968  "casadi_real casadi_fabs(casadi_real x) {\n"
1969  "/* Pre-c99 compatibility */\n"
1970  "#if __STDC_VERSION__ < 199901L\n"
1971  " return x>0 ? x : -x;\n"
1972  "#else\n"
1973  " return fabs(x);\n"
1974  "#endif\n"
1975  "}\n\n",
1976  inst);
1977  break;
1978  case AUX_ISINF:
1979  this->auxiliaries << sanitize_source(
1980  "// SYMBOL \"isinf\"\n"
1981  "casadi_real casadi_isinf(casadi_real x) {\n"
1982  "/* Pre-c99 compatibility */\n"
1983  "#if __STDC_VERSION__ < 199901L\n"
1984  " return x== INFINITY || x==-INFINITY;\n"
1985  "#else\n"
1986  " return isinf(x);\n"
1987  "#endif\n"
1988  "}\n\n",
1989  inst);
1990  break;
1991  case AUX_ISFINITE:
1992  this->auxiliaries << sanitize_source(
1993  "// SYMBOL \"isfinite\"\n"
1994  "casadi_real casadi_isfinite(casadi_real x) {\n"
1995  "/* Pre-c99 compatibility */\n"
1996  "#if __STDC_VERSION__ < 199901L\n"
1997  " return x==x && x!=INFINITY && x!=-INFINITY;\n"
1998  "#else\n"
1999  " return isfinite(x);\n"
2000  "#endif\n"
2001  "}\n\n",
2002  inst);
2003  break;
2004  case AUX_MIN:
2005  this->auxiliaries << sanitize_source(
2006  "// SYMBOL \"min\"\n"
2007  "casadi_int casadi_min(casadi_int x, casadi_int y) {\n"
2008  " return x>y ? y : x;\n"
2009  "}\n\n",
2010  inst);
2011  break;
2012  case AUX_MAX:
2013  this->auxiliaries << sanitize_source(
2014  "// SYMBOL \"max\"\n"
2015  "casadi_int casadi_max(casadi_int x, casadi_int y) {\n"
2016  " return x>y ? x : y;\n"
2017  "}\n\n",
2018  inst);
2019  break;
2020  case AUX_MMIN:
2023  this->auxiliaries << sanitize_source(casadi_mmin_str, inst);
2024  break;
2025  case AUX_MMAX:
2028  this->auxiliaries << sanitize_source(casadi_mmax_str, inst);
2029  break;
2030  case AUX_INF:
2031  this->auxiliaries << "#ifndef casadi_inf\n"
2032  << " #define casadi_inf " << this->infinity << "\n"
2033  << "#endif\n\n";
2034  break;
2035  case AUX_NAN:
2036  this->auxiliaries << "#ifndef casadi_nan\n"
2037  << " #define casadi_nan " << this->nan << "\n"
2038  << "#endif\n\n";
2039  break;
2040  case AUX_REAL_MIN:
2041  this->auxiliaries << "#ifndef casadi_real_min\n"
2042  << " #define casadi_real_min " << this->real_min << "\n"
2043  << "#endif\n\n";
2044  break;
2045  case AUX_LOG1P:
2046  this->auxiliaries << sanitize_source(
2047  "// SYMBOL \"log1p\"\n"
2048  "casadi_real casadi_log1p(casadi_real x) {\n"
2049  "/* Pre-c99 compatibility */\n"
2050  "#if __STDC_VERSION__ < 199901L\n"
2051  " return log(1+x);\n"
2052  "#else\n"
2053  " return log1p(x);\n"
2054  "#endif\n"
2055  "}\n\n",
2056  inst);
2057  break;
2058  case AUX_EXPM1:
2059  this->auxiliaries << sanitize_source(
2060  "// SYMBOL \"expm1\"\n"
2061  "casadi_real casadi_expm1(casadi_real x) {\n"
2062  "/* Pre-c99 compatibility */\n"
2063  "#if __STDC_VERSION__ < 199901L\n"
2064  " return exp(x)-1;\n"
2065  "#else\n"
2066  " return expm1(x);\n"
2067  "#endif\n"
2068  "}\n\n",
2069  inst);
2070  break;
2071  case AUX_HYPOT:
2072  this->auxiliaries << sanitize_source(
2073  "// SYMBOL \"hypot\"\n"
2074  "casadi_real casadi_hypot(casadi_real x, casadi_real y) {\n"
2075  "/* Pre-c99 compatibility */\n"
2076  "#if __STDC_VERSION__ < 199901L\n"
2077  " return sqrt(x*x+y*y);\n"
2078  "#else\n"
2079  " return hypot(x, y);\n"
2080  "#endif\n"
2081  "}\n\n",
2082  inst);
2083  break;
2084  case AUX_BLAZING_COMMON:
2086  add_include("simde/x86/avx2.h");
2087  add_include("simde/x86/fma.h");
2088  this->auxiliaries << sanitize_source(casadi_blazing_common_str, inst);
2089  break;
2092  this->auxiliaries << sanitize_source(casadi_blazing_1d_boor_eval_str, inst);
2093  break;
2096  this->auxiliaries << sanitize_source(casadi_blazing_2d_boor_eval_str, inst);
2097  break;
2100  this->auxiliaries << sanitize_source(casadi_blazing_3d_boor_eval_str, inst);
2101  break;
2104  this->auxiliaries << sanitize_source(casadi_blazing_4d_boor_eval_str, inst);
2105  break;
2108  this->auxiliaries << sanitize_source(casadi_blazing_5d_boor_eval_str, inst);
2109  break;
2110  case AUX_PRINTME:
2112  this->auxiliaries << sanitize_source(casadi_printme_str, inst);
2113  break;
2114  case AUX_PRINT_SCALAR:
2116  this->auxiliaries << sanitize_source(casadi_print_scalar_str, inst);
2117  break;
2118  case AUX_PRINT_VECTOR:
2120  this->auxiliaries << sanitize_source(casadi_print_vector_str, inst);
2121  break;
2122  case AUX_PRINT_CANONICAL:
2124  this->auxiliaries << sanitize_source(casadi_print_canonical_str, inst);
2125  break;
2126  case AUX_FPRINTF_SCALAR:
2127  add_include("stdio.h");
2129  this->auxiliaries << sanitize_source(casadi_fprintf_scalar_str, inst);
2130  break;
2131  case AUX_FPRINTF_VECTOR:
2133  this->auxiliaries << sanitize_source(casadi_fprintf_vector_str, inst);
2134  break;
2135  case AUX_TO_FILE:
2136  add_include("stdio.h");
2138  this->auxiliaries << sanitize_source(casadi_to_file_str, inst);
2139  break;
2140  case AUX_THREADS:
2141  this->auxiliaries << sanitize_source(casadi_threads_str, inst);
2142  break;
2143  }
2144  }
static bool codegen_dot_aux(CodeGenerator &g, const std::vector< std::string > &inst)
Definition: blas.cpp:305
static bool codegen_norm_1_aux(CodeGenerator &g, const std::vector< std::string > &inst)
Definition: blas.cpp:332
static void codegen_copy_aux(CodeGenerator &g, const std::vector< std::string > &inst)
Definition: blas.cpp:283
static bool codegen_scal_aux(CodeGenerator &g, const std::vector< std::string > &inst)
Definition: blas.cpp:314
static bool codegen_axpy_aux(CodeGenerator &g, const std::vector< std::string > &inst)
Definition: blas.cpp:296
static bool codegen_norm_2_aux(CodeGenerator &g, const std::vector< std::string > &inst)
Definition: blas.cpp:323
std::multimap< Auxiliary, std::vector< std::string > > added_auxiliaries_
std::string sanitize_source(const std::string &src, const std::vector< std::string > &inst, bool add_shorthand=true)
Sanitize source files for codegen.
std::stringstream auxiliaries

References add_include(), added_auxiliaries_, AUX_AXPY, AUX_BFGS, AUX_BILIN, AUX_BLAZING_1D_BOOR_EVAL, AUX_BLAZING_2D_BOOR_EVAL, AUX_BLAZING_3D_BOOR_EVAL, AUX_BLAZING_4D_BOOR_EVAL, AUX_BLAZING_5D_BOOR_EVAL, AUX_BLAZING_COMMON, AUX_BOUNDS_CONSISTENCY, AUX_CACHE, AUX_CAST, AUX_CLEAR, AUX_CLIP_MAX, AUX_CLIP_MIN, AUX_CONVEXIFY, AUX_COPY, AUX_CVX, AUX_DE_BOOR, AUX_DENSIFY, AUX_DET, AUX_DOT, AUX_EXPM1, AUX_FABS, AUX_FEASIBLESQPMETHOD, AUX_FILE_SLURP, AUX_FILL, AUX_FINITE_DIFF, AUX_FLIP, AUX_FMAX, AUX_FMIN, AUX_FPRINTF_SCALAR, AUX_FPRINTF_VECTOR, AUX_FROM_MEX, AUX_HYPOT, AUX_IAMAX, AUX_IF_ELSE, AUX_INF, AUX_INTERPN, AUX_INTERPN_GRAD, AUX_INTERPN_INTERPOLATE, AUX_INTERPN_WEIGHTS, AUX_ISFINITE, AUX_ISINF, AUX_KRON, AUX_KRON_CONTRACT_INNER, AUX_KRON_CONTRACT_INNER_DENSE, AUX_KRON_CONTRACT_INNER_DENSE_SPARSE, AUX_KRON_CONTRACT_INNER_SPARSE_DENSE, AUX_KRON_CONTRACT_OUTER, AUX_KRON_CONTRACT_OUTER_DENSE, AUX_KRON_CONTRACT_OUTER_DENSE_SPARSE, AUX_KRON_CONTRACT_OUTER_SPARSE_DENSE, AUX_KRON_DENSE, AUX_KRON_DENSE_SPARSE, AUX_KRON_SPARSE_DENSE, AUX_LDL, AUX_LOG1P, AUX_LOGSUMEXP, AUX_LOW, AUX_LSQR, AUX_MASKED_NORM_INF, AUX_MAX, AUX_MAX_VIOL, AUX_MIN, AUX_MMAX, AUX_MMIN, AUX_MTIMES, AUX_MTIMES_DENSE, AUX_MTIMES_DENSE_SPARSE, AUX_MV, AUX_MV_DENSE, AUX_NAN, AUX_ND_BOOR_EVAL, AUX_NEWTON, AUX_NLP, AUX_NORM_1, AUX_NORM_2, AUX_NORM_INF, AUX_OCP_BLOCK, AUX_ORACLE, AUX_ORACLE_CALLBACK, AUX_PRINT_CANONICAL, AUX_PRINT_SCALAR, AUX_PRINT_VECTOR, AUX_PRINTF, AUX_PRINTME, AUX_PROJECT, AUX_QP, AUX_QR, AUX_QRQP, AUX_RANK1, AUX_REAL_MIN, AUX_REGULARIZE, AUX_SCAL, AUX_SCALED_COPY, AUX_SIGN, AUX_SOCP, AUX_SPARSIFY, AUX_SPARSITY, AUX_SQ, AUX_SQPMETHOD, AUX_SUM, AUX_SUM_VIOL, AUX_SWAP, AUX_TENSOR_TTV, AUX_THREADS, AUX_TO_DOUBLE, AUX_TO_FILE, AUX_TO_INT, AUX_TO_MEX, AUX_TRANS, AUX_TRI_PROJECT, AUX_TRILSOLVE, AUX_TRIUSOLVE, AUX_VECTOR_FMAX, AUX_VECTOR_FMIN, AUX_VFMAX, AUX_VFMIN, auxiliaries, casadi::Blas::codegen_axpy_aux(), casadi::Blas::codegen_copy_aux(), casadi::Blas::codegen_dot_aux(), casadi::Blas::codegen_norm_1_aux(), casadi::Blas::codegen_norm_2_aux(), casadi::Blas::codegen_scal_aux(), cpp, infinity, l1_blas, mex, nan, real_min, and sanitize_source().

Referenced by axpy(), bilin(), bound_consistency(), cache_check(), clear(), clip_max(), clip_min(), casadi::BlazingSplineFunction::codegen_body(), casadi::MapSum::codegen_body(), casadi::Switch::codegen_body(), casadi::ClarabelInterface::codegen_body(), casadi::DaqpInterface::codegen_body(), casadi::HighsInterface::codegen_body(), casadi::HpipmInterface::codegen_body(), casadi::MosekInterface::codegen_body(), casadi::OsqpInterface::codegen_body(), casadi::XpressInterface::codegen_body(), casadi::FastNewton::codegen_body(), casadi::Feasiblesqpmethod::codegen_body(), casadi::Qrqp::codegen_body(), casadi::Sqpmethod::codegen_body(), casadi::FiniteDiff::codegen_declarations(), casadi::ThreadMap::codegen_declarations(), casadi::Nlpsol::codegen_declarations(), casadi::MadmpecInterface::codegen_declarations(), casadi::FatropInterface::codegen_declarations(), casadi::IpoptInterface::codegen_declarations(), casadi::MadnlpInterface::codegen_declarations(), casadi::UnoInterface::codegen_declarations(), casadi::Sqpmethod::codegen_declarations(), CodeGenerator(), constant(), convexify_eval(), copy(), densify(), det(), dot(), dump(), file_slurp(), fill(), fmax(), fmin(), fprintf_scalar(), fprintf_vector(), from_mex(), casadi::BSplineCommon::generate(), casadi::Kron::generate(), casadi::DenseKron::generate(), casadi::DenseSparseKron::generate(), casadi::SparseDenseKron::generate(), casadi::KronContract::generate(), casadi::DenseKronContract::generate(), casadi::DenseSparseKronContract::generate(), casadi::SparseDenseKronContract::generate(), casadi::HorzRepsum::generate(), interpn(), interpn_grad(), lb_eig(), ldl(), ldl_solve(), logsumexp(), low(), lsqr_solve(), masked_norm_inf(), max(), max_viol(), min(), mmax(), mmin(), mtimes(), mtimes_dense_sparse(), mv(), norm_1(), norm_2(), norm_inf(), print_canonical(), print_op(), print_scalar(), print_vector(), printf(), project(), casadi::Conic::qp_codegen_body(), qr(), qr_solve(), rank1(), regularize(), scal(), sparsify(), sum_viol(), to_file(), to_mex(), trans(), tri_project(), trilsolve(), triusolve(), vector_fmax(), vector_fmin(), vfmax(), and vfmin().

◆ add_dependency()

std::string casadi::CodeGenerator::add_dependency ( const Function f)

Definition at line 315 of file code_generator.cpp.

315  {
316  // Quick return if it already exists
317  for (auto&& e : added_functions_) if (e.f==f) return e.codegen_name;
318 
319  // Give it a name
320  std::string fname = shorthand("f" + str(added_functions_.size()));
321 
322  // Add to list of functions
323  added_functions_.push_back({f, fname});
324 
325  // Generate declarations
326  f->codegen_declarations(*this);
327 
328  // Print to file
329  f->codegen(*this, fname);
330 
331  bool fun_needs_mem = f->codegen_needs_mem();
332  needs_mem_ |= fun_needs_mem;
333 
334  if (fun_needs_mem) {
335 
336  if (!f->codegen_mem_is_opaque()) {
337  // Alloc memory
338  *this << "int " << fname << "_alloc_mem(void) {\n";
339  flush(this->body);
340  scope_enter();
341  f->codegen_alloc_mem(*this);
342  scope_exit();
343  *this << "}\n\n";
344 
345  // Initialize memory
346  *this << "int " << fname << "_init_mem(int mem) {\n";
347  flush(this->body);
348  scope_enter();
349  f->codegen_init_mem(*this);
350  scope_exit();
351  *this << "}\n\n";
352 
353  // Clear memory
354  *this << "void " << fname << "_free_mem(int mem) {\n";
355  flush(this->body);
356  scope_enter();
357  f->codegen_free_mem(*this);
358  scope_exit();
359  *this << "}\n\n";
360  }
361 
362  // Checkout
363  *this << "int " << fname << "_checkout(void) {\n";
364  flush(this->body);
365  scope_enter();
366  f->codegen_checkout(*this);
367  scope_exit();
368  *this << "}\n\n";
369 
370  // Clear memory
371  *this << "void " << fname << "_release(int mem) {\n";
372  flush(this->body);
373  scope_enter();
374  f->codegen_release(*this);
375  scope_exit();
376  *this << "}\n\n";
377 
378  }
379 
380  // Codegen reference count functions, if needed
381  if (f->has_refcount_in_deps_) {
382  // Increase reference counter
383  *this << "void " << fname << "_incref(void) {\n";
384  f->codegen_incref(*this);
385  *this << "}\n\n";
386 
387  // Decrease reference counter
388  *this << "void " << fname << "_decref(void) {\n";
389  f->codegen_decref(*this);
390  *this << "}\n\n";
391  }
392 
393  // Flush to body
394  flush(this->body);
395 
396  return fname;
397  }
void scope_enter()
Enter a local scope.
void scope_exit()
Exit a local scope.
std::vector< FunctionMeta > added_functions_
std::string shorthand(const std::string &name) const
Get a shorthand.

References added_functions_, body, casadi::FunctionInternal::codegen(), casadi::FunctionInternal::codegen_alloc_mem(), casadi::FunctionInternal::codegen_checkout(), casadi::FunctionInternal::codegen_declarations(), casadi::FunctionInternal::codegen_decref(), casadi::FunctionInternal::codegen_free_mem(), casadi::FunctionInternal::codegen_incref(), casadi::FunctionInternal::codegen_init_mem(), casadi::FunctionInternal::codegen_mem_is_opaque(), casadi::FunctionInternal::codegen_needs_mem(), casadi::FunctionInternal::codegen_release(), flush(), casadi::FunctionInternal::has_refcount_in_deps_, needs_mem_, scope_enter(), scope_exit(), shorthand(), and casadi::str().

Referenced by add(), casadi::Call::add_dependency(), casadi::FiniteDiff::codegen_declarations(), casadi::Map::codegen_declarations(), casadi::MapSum::codegen_declarations(), casadi::Nlpsol::codegen_declarations(), casadi::Switch::codegen_declarations(), casadi::SXFunction::codegen_declarations(), casadi::MadmpecInterface::codegen_declarations(), casadi::FatropInterface::codegen_declarations(), casadi::IpoptInterface::codegen_declarations(), casadi::MadnlpInterface::codegen_declarations(), casadi::UnoInterface::codegen_declarations(), casadi::FastNewton::codegen_declarations(), casadi::Feasiblesqpmethod::codegen_declarations(), casadi::Sqpmethod::codegen_declarations(), operator()(), and setup_callback().

◆ add_external()

void casadi::CodeGenerator::add_external ( const std::string &  new_external)

Definition at line 1310 of file code_generator.cpp.

1310  {
1311  added_externals_.insert(new_external);
1312  }
std::set< std::string > added_externals_

References added_externals_.

Referenced by casadi::blasfeo_codegen_mtimes(), casadi::classic_codegen_mtimes(), and casadi::External::codegen_declarations().

◆ add_include()

void casadi::CodeGenerator::add_include ( const std::string &  new_include,
bool  relative_path = false,
const std::string &  use_ifdef = std::string() 
)

Definition at line 1233 of file code_generator.cpp.

1234  {
1235  // Register the new element
1236  bool added = added_includes_.insert(new_include).second;
1237 
1238  // Quick return if it already exists
1239  if (!added) return;
1240 
1241  // Ifdef opening
1242  if (!use_ifdef.empty()) this->includes << "#ifdef " << use_ifdef << std::endl;
1243 
1244  // Print to the header section
1245  if (relative_path) {
1246  this->includes << "#include \"" << new_include << "\"\n";
1247  } else {
1248  this->includes << "#include <" << new_include << ">\n";
1249  }
1250 
1251  // Ifdef closing
1252  if (!use_ifdef.empty()) this->includes << "#endif\n";
1253  }
std::stringstream includes
std::set< std::string > added_includes_

References added_includes_, and includes.

Referenced by add_auxiliary(), casadi::ConstantPool::add_dependency(), casadi::BlazingSplineFunction::codegen_body(), casadi::ClarabelInterface::codegen_body(), casadi::DaqpInterface::codegen_body(), casadi::HighsInterface::codegen_body(), casadi::HpipmInterface::codegen_body(), casadi::MosekInterface::codegen_body(), casadi::OsqpInterface::codegen_body(), casadi::XpressInterface::codegen_body(), casadi::Blas::codegen_copy_aux(), casadi::MadmpecInterface::codegen_declarations(), casadi::FatropInterface::codegen_declarations(), casadi::IpoptInterface::codegen_declarations(), casadi::MadnlpInterface::codegen_declarations(), casadi::OnnxRuntimeInterface::codegen_declarations(), casadi::UnoInterface::codegen_declarations(), CodeGenerator(), and generate_dump().

◆ add_io_sparsities()

void casadi::CodeGenerator::add_io_sparsities ( const std::string &  name,
const std::vector< Sparsity > &  sp_in,
const std::vector< Sparsity > &  sp_out 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tq

Definition at line 2786 of file code_generator.cpp.

2789  {
2790  // Insert element, quick return if it already exists
2791  if (!sparsity_meta.insert(name).second) return;
2792 
2793  // Input sparsities
2794  *this << declare("const casadi_int* " + name + "_sparsity_in(casadi_int i)") << " {\n"
2795  << "switch (i) {\n";
2796  for (casadi_int i=0; i<sp_in.size(); ++i) {
2797  *this << "case " << i << ": return " << sparsity(sp_in[i], force_canonical) << ";\n";
2798  }
2799  *this << "default: return 0;\n}\n"
2800  << "}\n\n";
2801 
2802  // Output sparsities
2803  *this << declare("const casadi_int* " + name + "_sparsity_out(casadi_int i)") << " {\n"
2804  << "switch (i) {\n";
2805  for (casadi_int i=0; i<sp_out.size(); ++i) {
2806  *this << "case " << i << ": return " << sparsity(sp_out[i], force_canonical) << ";\n";
2807  }
2808  *this << "default: return 0;\n}\n"
2809  << "}\n\n";
2810  }
std::set< std::string > sparsity_meta
std::string sparsity(const Sparsity &sp, bool canonical=true)

References declare(), force_canonical, name, sparsity(), and sparsity_meta.

Referenced by add(), and casadi::FunctionInternal::codegen_sparsities().

◆ add_sparsity()

casadi_int casadi::CodeGenerator::add_sparsity ( const Sparsity sp,
bool  canonical = true 
)

Definition at line 1327 of file code_generator.cpp.

1327  {
1328  return get_constant(sp.compress(canonical), true);
1329  }
casadi_int get_constant(const std::vector< double > &v, bool allow_adding=false)
Get or add a constant.

References casadi::Sparsity::compress(), and get_constant().

Referenced by sparsity().

◆ arg()

std::string casadi::CodeGenerator::arg ( casadi_int  i) const

◆ array()

std::string casadi::CodeGenerator::array ( const std::string &  type,
const std::string &  name,
casadi_int  len,
const std::string &  def = std::string() 
)
static

Declare an array

Definition at line 1123 of file code_generator.cpp.

1124  {
1125  std::stringstream s;
1126  s << type << " ";
1127  if (len==0) {
1128  s << "*" << name << " = 0";
1129  } else {
1130  s << name << "[" << len << "]";
1131  if (!def.empty()) s << " = " << def;
1132  }
1133  s << ";\n";
1134  return s.str();
1135  }

References name.

Referenced by casadi::codegen_int_array(), casadi::FunctionInternal::codegen_meta(), and print_vector().

◆ avoid_stack()

bool casadi::CodeGenerator::avoid_stack ( ) const
inline

◆ axpy()

std::string casadi::CodeGenerator::axpy ( casadi_int  n,
const std::string &  a,
const std::string &  x,
const std::string &  y 
)

◆ bilin()

std::string casadi::CodeGenerator::bilin ( const std::string &  A,
const Sparsity sp_A,
const std::string &  x,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_su

Definition at line 2294 of file code_generator.cpp.

2295  {
2297  std::stringstream s;
2298  s << "casadi_bilin(" << A << ", " << sparsity(sp_A) << ", " << x << ", " << y << ")";
2299  return s.str();
2300  }

References add_auxiliary(), AUX_BILIN, and sparsity().

Referenced by casadi::Feasiblesqpmethod::codegen_eval_m_k(), and casadi::Bilin::generate().

◆ bound_consistency()

std::string casadi::CodeGenerator::bound_consistency ( casadi_int  n,
const std::string &  x,
const std::string &  lam,
const std::string &  lbx,
const std::string &  ubx 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_th

Definition at line 2983 of file code_generator.cpp.

2985  {
2987  return "casadi_bound_consistency(" + str(n) + ", " + x + ", " + lam +
2988  ", " + lbx + ", " + ubx + ")";
2989  }

References add_auxiliary(), AUX_BOUNDS_CONSISTENCY, and casadi::str().

Referenced by casadi::Feasiblesqpmethod::codegen_body(), and casadi::Nlpsol::codegen_post_solve().

◆ cache_check()

std::string casadi::CodeGenerator::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 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u8

Definition at line 3091 of file code_generator.cpp.

3093  {
3095  return "casadi_cache_check(" + key + ", " + cache + ", " + loc + ", " +
3096  str(stride) + ", " + str(sz) + ", " + str(key_sz) + ", " + val + ")";
3097  }

References add_auxiliary(), AUX_CACHE, and casadi::str().

Referenced by casadi::LinsolQr::generate_factorize().

◆ casadi_version()

std::string casadi::CodeGenerator::casadi_version ( )
static

Definition at line 450 of file code_generator.cpp.

450  {
451  std::stringstream ss;
452  ss << CASADI_MAJOR_VERSION << "." << CASADI_MINOR_VERSION << "." << CASADI_PATCH_VERSION;
453  if (!CASADI_IS_RELEASE) ss << "+";
454  return ss.str();
455  }

Referenced by stream_open().

◆ clear()

std::string casadi::CodeGenerator::clear ( const std::string &  res,
std::size_t  n 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tv

Definition at line 2254 of file code_generator.cpp.

2254  {
2255  std::stringstream s;
2256  // Perform operation
2258  s << "casadi_clear(" << res << ", " << n << ");";
2259  return s.str();
2260  }
std::string res(casadi_int i) const
Refer to resuly.

References add_auxiliary(), AUX_CLEAR, and res().

Referenced by casadi::Feasiblesqpmethod::codegen_body(), casadi::Sqpmethod::codegen_body(), casadi::OsqpInterface::codegen_init_mem(), casadi::Sqpmethod::codegen_solve_elastic_mode(), fill(), casadi::BSplineCommon::generate(), casadi::Constant< Value >::generate(), and casadi::HorzRepsum::generate().

◆ clip_max()

std::string casadi::CodeGenerator::clip_max ( const std::string &  x,
casadi_int  n,
const std::string &  min,
const std::string &  mask 
)

Definition at line 2459 of file code_generator.cpp.

2460  {
2462  return "casadi_clip_max(" + x + ", " + str(n) + ", " + min + ", " + mask + ");";
2463  }
std::string min(const std::string &x, const std::string &y)
min

References add_auxiliary(), AUX_CLIP_MAX, min(), and casadi::str().

Referenced by casadi::OsqpInterface::codegen_body(), casadi::Feasiblesqpmethod::codegen_body(), and casadi::Feasiblesqpmethod::codegen_feasibility_iterations().

◆ clip_min()

std::string casadi::CodeGenerator::clip_min ( const std::string &  x,
casadi_int  n,
const std::string &  min,
const std::string &  mask 
)

Definition at line 2453 of file code_generator.cpp.

2454  {
2456  return "casadi_clip_min(" + x + ", " + str(n) + ", " + min + ", " + mask + ");";
2457  }

References add_auxiliary(), AUX_CLIP_MIN, min(), and casadi::str().

Referenced by casadi::OsqpInterface::codegen_body(), casadi::Feasiblesqpmethod::codegen_body(), and casadi::Feasiblesqpmethod::codegen_feasibility_iterations().

◆ comment()

void casadi::CodeGenerator::comment ( const std::string &  s)

◆ constant() [1/9]

std::string casadi::CodeGenerator::constant ( casadi_int  v)

Definition at line 2189 of file code_generator.cpp.

2189  {
2190  return str(v);
2191  }

References casadi::str().

◆ constant() [2/9]

std::string casadi::CodeGenerator::constant ( char  v)

Definition at line 2193 of file code_generator.cpp.

2193  {
2194  return constant(static_cast<casadi_int>(v));
2195  }
std::string constant(const std::vector< casadi_int > &v)
Represent an array constant; adding it when new.

References constant().

◆ constant() [3/9]

std::string casadi::CodeGenerator::constant ( const std::string &  v)

Definition at line 2182 of file code_generator.cpp.

2182  {
2183  std::string ret = v;
2184  ret = replace(ret, "\\", "\\\\");
2185  ret = replace(ret, "\"", "\\\"");
2186  return "\"" + ret + "\"";
2187  }
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.

References casadi::replace().

◆ constant() [4/9]

std::string casadi::CodeGenerator::constant ( const std::vector< casadi_int > &  v)

Extra doc: https://github.com/casadi/casadi/wiki/L_rz

Definition at line 1462 of file code_generator.cpp.

1462  {
1463  return shorthand("s" + str(get_constant(v, true)));
1464  }

References get_constant(), shorthand(), and casadi::str().

Referenced by casadi::BlazingSplineFunction::codegen_body(), casadi::FiniteDiff::codegen_body(), casadi::SXFunction::codegen_body(), casadi::FatropInterface::codegen_body(), casadi::HighsInterface::codegen_body(), casadi::MosekInterface::codegen_body(), casadi::OnnxRuntimeInterface::codegen_body(), casadi::OsqpInterface::codegen_body(), casadi::XpressInterface::codegen_body(), casadi::FastNewton::codegen_body(), casadi::Feasiblesqpmethod::codegen_body(), casadi::LinearInterpolant::codegen_body(), casadi::LinearInterpolantJac::codegen_body(), casadi::Qrqp::codegen_body(), casadi::External::codegen_incref(), casadi::MadmpecInterface::codegen_init_mem(), casadi::OsqpInterface::codegen_init_mem(), casadi::UnoInterface::codegen_init_mem(), casadi::FunctionInternal::codegen_meta(), casadi::Nlpsol::codegen_setup_constants(), casadi::Sqpmethod::codegen_solve_elastic_mode(), casadi::codegen_unpack_block(), constant(), constant_copy(), casadi::Convexify::generate(), casadi::LinsolLdl::generate(), casadi::LinsolQr::generate(), casadi::BSpline::generate(), casadi::BSplineCommon::generate(), casadi::ConstantDM::generate(), casadi::Constant< Value >::generate(), casadi::GetNonzerosVector::generate(), casadi::GetNonzerosParamVector::generate(), casadi::GetNonzerosSliceParam::generate(), casadi::GetNonzerosParamSlice::generate(), casadi::GetNonzerosParamParam::generate(), casadi::SetNonzerosVector< Add >::generate(), casadi::LinsolQr::generate_factorize(), casadi::DaqpInterface::set_daqp_prob(), casadi::FatropInterface::set_fatrop_prob(), casadi::HpipmInterface::set_hpipm_prob(), casadi::MosekInterface::set_mosek_prob(), and casadi::XpressInterface::set_xpress_prob().

◆ constant() [5/9]

std::string casadi::CodeGenerator::constant ( const std::vector< char > &  v)

Extra doc: https://github.com/casadi/casadi/wiki/L_27m

Definition at line 1466 of file code_generator.cpp.

1466  {
1467  return shorthand("b" + str(get_constant(v, true)));
1468  }

References get_constant(), shorthand(), and casadi::str().

◆ constant() [6/9]

std::string casadi::CodeGenerator::constant ( const std::vector< double > &  v)

Extra doc: https://github.com/casadi/casadi/wiki/L_s1

Definition at line 1500 of file code_generator.cpp.

1500  {
1501  return shorthand("c" + str(get_constant(v, true)));
1502  }

References get_constant(), shorthand(), and casadi::str().

◆ constant() [7/9]

std::string casadi::CodeGenerator::constant ( const std::vector< int > &  v)
inline

Extra doc: https://github.com/casadi/casadi/wiki/L_255

Definition at line 133 of file code_generator.hpp.

133  {
134  return constant(vector_static_cast<casadi_int>(v));
135  }

◆ constant() [8/9]

std::string casadi::CodeGenerator::constant ( const std::vector< std::string > &  v)

Extra doc: https://github.com/casadi/casadi/wiki/L_280

Definition at line 1470 of file code_generator.cpp.

1470  {
1471  return shorthand("a" + str(get_constant(v, true)));
1472  }

References get_constant(), shorthand(), and casadi::str().

◆ constant() [9/9]

std::string casadi::CodeGenerator::constant ( double  v)

Extra doc: https://github.com/casadi/casadi/wiki/L_sj

Definition at line 2196 of file code_generator.cpp.

2196  {
2197  std::stringstream s;
2198  if (isnan(v)) {
2200  s << "casadi_nan";
2201  } else if (isinf(v)) {
2203  if (v<0) s << "-";
2204  s << "casadi_inf";
2205  } else {
2206  casadi_int v_int = static_cast<casadi_int>(v);
2207  if (static_cast<double>(v_int)==v) {
2208  // Print integer
2209  s << v_int << ".";
2210  } else {
2211  // Print real
2212  std::ios_base::fmtflags fmtfl = s.flags(); // get current format flags
2213  s << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1) << v;
2214  s.flags(fmtfl); // reset current format flags
2215  }
2216  }
2217  return s.str();
2218  }

References add_auxiliary(), AUX_INF, and AUX_NAN.

◆ constant_copy()

void casadi::CodeGenerator::constant_copy ( const std::string &  var_name,
const std::vector< casadi_int > &  v,
const std::string &  type = "casadi_int" 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_s0

Definition at line 1484 of file code_generator.cpp.

1485  {
1486  std::string ref = constant(v);
1487  if (!v.empty()) {
1488  local(name+"[" + str(v.size()) + "]", type);
1489  } else {
1490  local(name, type, "*");
1491  }
1492  if (!v.empty()) {
1493  local("i", type);
1494  (*this) << "for (i=0;i<" << v.size() << ";++i) " + name + "[i] = " + ref + "[i];\n";
1495  } else {
1496  init_local(name, "0");
1497  }
1498  }
void local(const std::string &name, const std::string &type, const std::string &ref="")
Declare a local variable.
void init_local(const std::string &name, const std::string &def)
Specify the default value for a local variable.

References constant(), init_local(), local(), name, and casadi::str().

Referenced by casadi::OsqpInterface::codegen_init_mem(), and casadi::codegen_int_field().

◆ convexify_eval()

std::string casadi::CodeGenerator::convexify_eval ( const ConvexifyData d,
const std::string &  Hin,
const std::string &  Hout,
const std::string &  iw,
const std::string &  w 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tk

Definition at line 2970 of file code_generator.cpp.

2972  {
2974  return Convexify::generate(*this, d, Hin, Hout, iw, w);
2975  }
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:157

References add_auxiliary(), AUX_CONVEXIFY, and casadi::Convexify::generate().

Referenced by casadi::Sqpmethod::codegen_body(), and casadi::Convexify::generate().

◆ copy()

std::string casadi::CodeGenerator::copy ( const std::string &  arg,
std::size_t  n,
const std::string &  res 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tt

Definition at line 2220 of file code_generator.cpp.

2221  {
2222  std::stringstream s;
2223  // Perform operation
2225  s << "casadi_copy(" << arg << ", " << n << ", " << res << ");";
2226  return s.str();
2227  }
std::string arg(casadi_int i) const
Refer to argument.

References add_auxiliary(), arg(), AUX_COPY, and res().

Referenced by casadi::FiniteDiff::codegen_body(), casadi::JitFunction::codegen_body(), casadi::FastNewton::codegen_body(), casadi::Feasiblesqpmethod::codegen_body(), casadi::Sqpmethod::codegen_body(), casadi::codegen_copy_z(), casadi::Feasiblesqpmethod::codegen_feasibility_iterations(), casadi::Nlpsol::codegen_post_solve(), casadi::Sqpmethod::codegen_solve_elastic_mode(), casadi::Feasiblesqpmethod::codegen_step_update(), copy_check(), copy_default(), casadi::ConstantDM::generate(), casadi::ConstantFile::generate(), casadi::ConstantPool::generate(), casadi::Einstein::generate(), casadi::Input::generate(), casadi::Output::generate(), casadi::Rank1::generate(), casadi::HorzRepmat::generate(), casadi::SetNonzerosVector< Add >::generate(), casadi::SetNonzerosSlice< Add >::generate(), casadi::SetNonzerosSlice2< Add >::generate(), casadi::SetNonzerosParam< Add >::generate(), casadi::LinsolCall< Tr >::generate(), casadi::TriuSolve< Tr >::generate(), casadi::TrilSolve< Tr >::generate(), casadi::TriuSolveUnity< Tr >::generate(), casadi::TrilSolveUnity< Tr >::generate(), casadi::Split::generate(), casadi::MXNode::generate_copy(), casadi::LinsolQr::generate_factorize(), and project().

◆ copy_check()

void casadi::CodeGenerator::copy_check ( const std::string &  arg,
std::size_t  n,
const std::string &  res,
bool  check_lhs = true,
bool  check_rhs = true 
)

Definition at line 2234 of file code_generator.cpp.

2235  {
2236  std::vector<std::string> checks;
2237  if (check_lhs) checks.push_back(arg);
2238  if (check_rhs) checks.push_back(res);
2239  if (!checks.empty()) *this << "if (" << join(checks, " && ") << ") ";
2240  *this << copy(arg, n, res) << "\n";
2241  }
std::string copy(const std::string &arg, std::size_t n, const std::string &res)
Create a copy operation.
std::string join(const std::vector< std::string > &l, const std::string &delim)

References arg(), copy(), casadi::join(), and res().

Referenced by casadi::OsqpInterface::codegen_body(), casadi::Feasiblesqpmethod::codegen_body(), casadi::Qrqp::codegen_body(), and casadi::Nlpsol::codegen_post_solve().

◆ copy_default()

void casadi::CodeGenerator::copy_default ( const std::string &  arg,
std::size_t  n,
const std::string &  res,
const std::string &  def,
bool  check_rhs = true 
)

Definition at line 2243 of file code_generator.cpp.

2244  {
2245  *this << "if (" << arg << ") {\n";
2246  if (check_rhs) *this << "if (" << res << ") ";
2247  *this << copy(arg, n, res) << "\n";
2248  *this << "} else {\n";
2249  if (check_rhs) *this << "if (" << res << ") ";
2250  *this << fill(res, n, def) << "\n";
2251  *this << "}\n";
2252  }
std::string fill(const std::string &res, std::size_t n, const std::string &v)
Create a fill operation.

References arg(), copy(), fill(), and res().

Referenced by casadi::OsqpInterface::codegen_body(), casadi::Feasiblesqpmethod::codegen_body(), casadi::Qrqp::codegen_body(), and casadi::Nlpsol::codegen_setup_per_call().

◆ declare()

std::string casadi::CodeGenerator::declare ( std::string  s)

Extra doc: https://github.com/casadi/casadi/wiki/L_tm

Definition at line 2345 of file code_generator.cpp.

2345  {
2346  // Add c linkage
2347  std::string cpp_prefix = this->cpp ? "extern \"C\" " : "";
2348 
2349  // To header file
2350  if (this->with_header) {
2351  this->header << cpp_prefix << this->dll_import << s << ";\n";
2352  }
2353 
2354  // Return name with declarations
2355  return cpp_prefix + this->dll_export + s;
2356  }

References cpp, dll_import, header, and with_header.

Referenced by add(), add_io_sparsities(), and casadi::FunctionInternal::codegen_meta().

◆ define_local_mutex()

void casadi::CodeGenerator::define_local_mutex ( const Function f,
const std::string &  name 
)

(thread-safe mode only, idempotent)

Extra doc: https://github.com/casadi/casadi/wiki/L_2fm

Definition at line 909 of file code_generator.cpp.

910  {
911  if (!thread_safe()) return;
912  if (!local_mutexes_[f.get()].insert(name).second) return; // already defined
913  std::string sh = shorthand(name);
914  auxiliaries << "#if CASADI_MUTEX_USE_STATIC_INIT == 0\n";
915  auxiliaries << "static CASADI_MUTEX_TYPE " << sh << ";\n";
916  auxiliaries << "#else\n";
917  auxiliaries << "static CASADI_MUTEX_TYPE " << sh
918  << " = CASADI_MUTEX_STATIC_INIT;\n";
919  auxiliaries << "#endif\n";
920  }
bool thread_safe() const
Emit thead safe code chekout/release?
std::map< const FunctionInternal *, std::set< std::string > > local_mutexes_

References auxiliaries, casadi::Function::get(), local_mutexes_, name, shorthand(), and thread_safe().

Referenced by casadi::FunctionInternal::codegen(), casadi::FatropInterface::codegen_body(), and casadi::FunctionInternal::codegen_checkout().

◆ define_pool_double()

void casadi::CodeGenerator::define_pool_double ( const std::string &  name,
const std::vector< double > &  def 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_2aw

Definition at line 891 of file code_generator.cpp.

891  {
892  auto it = pool_double_.find(name);
893  if (it==pool_double_.end()) {
894  casadi_int index = pool_double_defaults_.size();
895  pool_double_defaults_.push_back(def);
896  shorthand("pd" + str(index));
897  pool_double_[name] = index;
898  } else {
899  casadi_assert_dev(def==pool_double_defaults_[it->second]);
900  }
901  }
std::map< std::string, casadi_int > pool_double_
std::vector< std::vector< double > > pool_double_defaults_

References name, pool_double_, pool_double_defaults_, shorthand(), and casadi::str().

Referenced by casadi::ConstantPool::add_dependency().

◆ define_rom_double()

void casadi::CodeGenerator::define_rom_double ( const void *  id,
casadi_int  size 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_s2

Definition at line 863 of file code_generator.cpp.

863  {
864  auto it = file_scope_double_.find(id);
865  casadi_assert(it==file_scope_double_.end(), "Already defined.");
866  shorthand("rd" + str(file_scope_double_.size()));
867  file_scope_double_[id] = size;
868  }
std::map< const void *, casadi_int > file_scope_double_

References file_scope_double_, shorthand(), and casadi::str().

Referenced by casadi::ConstantFile::add_dependency().

◆ define_rom_integer()

void casadi::CodeGenerator::define_rom_integer ( const void *  id,
casadi_int  size 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_s4

Definition at line 877 of file code_generator.cpp.

877  {
878  auto it = file_scope_double_.find(id);
879  casadi_assert(it==file_scope_double_.end(), "Already defined.");
880  shorthand("ri" + str(file_scope_double_.size()));
881  file_scope_double_[id] = size;
882  }

References file_scope_double_, shorthand(), and casadi::str().

◆ densify()

std::string casadi::CodeGenerator::densify ( const std::string &  arg,
const Sparsity sp_arg,
const std::string &  res,
bool  tr = false 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u1

Definition at line 2385 of file code_generator.cpp.

2386  {
2387  // Create call
2389  std::stringstream s;
2390  s << "casadi_densify(" << arg << ", " << sparsity(sp_arg) << ", " << res << ", "
2391  << (tr ? 1 : 0) << ");";
2392  return s.str();
2393  }

References add_auxiliary(), arg(), AUX_DENSIFY, res(), and sparsity().

Referenced by casadi::Densify::generate().

◆ det()

std::string casadi::CodeGenerator::det ( const std::string &  sp_v,
const std::string &  v,
const std::string &  sp_r,
const std::string &  r,
const std::string &  beta 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_2hy

Definition at line 2823 of file code_generator.cpp.

2825  {
2827  return "casadi_det(" + sp_v + ", " + v + ", " + sp_r + ", " + r + ", " + beta + ")";
2828  }

References add_auxiliary(), and AUX_DET.

Referenced by casadi::LinsolQr::generate_det().

◆ dot()

std::string casadi::CodeGenerator::dot ( casadi_int  n,
const std::string &  x,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sm

Definition at line 2286 of file code_generator.cpp.

2287  {
2289  std::stringstream s;
2290  s << "casadi_dot(" << n << ", " << x << ", " << y << ")";
2291  return s.str();
2292  }

References add_auxiliary(), and AUX_DOT.

Referenced by casadi::Sqpmethod::codegen_body(), casadi::Feasiblesqpmethod::codegen_eval_m_k(), and casadi::Dot::generate().

◆ dump() [1/2]

std::string casadi::CodeGenerator::dump ( )

Definition at line 444 of file code_generator.cpp.

444  {
445  std::stringstream s;
446  dump(s);
447  return s.str();
448  }
std::string dump()
Generate a file, return code as string.

Referenced by generate().

◆ dump() [2/2]

void casadi::CodeGenerator::dump ( std::ostream &  s)

Definition at line 939 of file code_generator.cpp.

939  {
940  // Consistency check
941  casadi_assert_dev(current_indent_ == 0);
942 
943  // Prefix internal symbols to avoid symbol collisions
944  s << "/* How to prefix internal symbols */\n"
945  << "#ifdef CASADI_CODEGEN_PREFIX\n"
946  << " #define CASADI_NAMESPACE_CONCAT(NS, ID) _CASADI_NAMESPACE_CONCAT(NS, ID)\n"
947  << " #define _CASADI_NAMESPACE_CONCAT(NS, ID) NS ## ID\n"
948  << " #define CASADI_PREFIX(ID) CASADI_NAMESPACE_CONCAT(CODEGEN_PREFIX, ID)\n"
949  << "#else\n"
950  << " #define CASADI_PREFIX(ID) " << this->prefix << "_ ## ID\n"
951  << "#endif\n\n";
952 
953  s << this->includes.str();
954  s << std::endl;
955 
956  // Numeric types after includes: may depend on them. e.g. mex type
957  // Real type (usually double)
958  generate_casadi_real(s);
959 
960  // Integer type (usually long long)
961  generate_casadi_int(s);
962 
963  if (needs_mem_) {
964  s << "#ifndef CASADI_MAX_NUM_THREADS\n";
965  s << "#define CASADI_MAX_NUM_THREADS 1\n";
966  s << "#endif\n\n";
967  }
968 
969  // casadi/mem after numeric types to define derived types
970  // Memory struct entry point
971  if (this->with_mem) {
972  s << "#include <casadi/mem.h>\n" << std::endl;
973  }
974 
975  // Macros
976  if (!added_shorthands_.empty()) {
977  s << "/* Add prefix to internal symbols */\n";
978  for (auto&& i : added_shorthands_) {
979  s << "#define " << "casadi_" << i << " CASADI_PREFIX(" << i << ")\n";
980  }
981  s << std::endl;
982  }
983 
984  if (this->with_export) generate_export_symbol(s);
985 
986  // Check if inf/nan is needed
987  for (const auto& d : double_constants_) {
988  for (double e : d) {
989  if (isinf(e)) add_auxiliary(AUX_INF);
990  if (isnan(e)) add_auxiliary(AUX_NAN);
991  }
992  }
993 
994  // Codegen auxiliary functions
995  s << this->auxiliaries.str();
996 
997  // Print integer constants
998  if (!integer_constants_.empty()) {
999  for (casadi_int i=0; i<integer_constants_.size(); ++i) {
1000  print_vector(s, "casadi_s" + str(i), integer_constants_[i]);
1001  }
1002  s << std::endl;
1003  }
1004 
1005  // Print double constants
1006  if (!double_constants_.empty()) {
1007  for (casadi_int i=0; i<double_constants_.size(); ++i) {
1008  print_vector(s, "casadi_c" + str(i), double_constants_[i]);
1009  }
1010  s << std::endl;
1011  }
1012 
1013  // Print char constants
1014  if (!char_constants_.empty()) {
1015  for (casadi_int i=0; i<char_constants_.size(); ++i) {
1016  print_vector(s, "casadi_b" + str(i), char_constants_[i]);
1017  }
1018  s << std::endl;
1019  }
1020 
1021  // Print string constants
1022  if (!string_constants_.empty()) {
1023  for (casadi_int i=0; i<string_constants_.size(); ++i) {
1024  print_vector(s, "casadi_a" + str(i), string_constants_[i]);
1025  }
1026  s << std::endl;
1027  }
1028 
1029  if (sz_zeros_) {
1030  std::vector<double> sz_zeros(sz_zeros_, 0);
1031  print_vector(s, "casadi_zeros", std::vector<double>(sz_zeros));
1032  s << std::endl;
1033  }
1034 
1035  if (sz_ones_) {
1036  std::vector<double> sz_ones(sz_ones_, 0);
1037  print_vector(s, "casadi_ones", std::vector<double>(sz_ones));
1038  s << std::endl;
1039  }
1040 
1041  // Print file scope double work
1042  if (!file_scope_double_.empty()) {
1043  casadi_int i=0;
1044  for (const auto& it : file_scope_double_) {
1045  s << "static casadi_real casadi_rd" + str(i++) + "[" + str(it.second) + "];\n";
1046  }
1047  s << std::endl;
1048  }
1049 
1050  // Print file scope integer work
1051  if (!file_scope_integer_.empty()) {
1052  casadi_int i=0;
1053  for (const auto& it : file_scope_integer_) {
1054  s << "static casadi_real casadi_ri" + str(i++) + "[" + str(it.second) + "];\n";
1055  }
1056  s << std::endl;
1057  }
1058 
1059  // Print file scope double pool
1060  if (!pool_double_.empty()) {
1061  casadi_int i=0;
1062  for (const auto& v : pool_double_defaults_) {
1063  s << "casadi_real casadi_pd" + str(i) +
1064  "[" + str(v.size()) + "] = " + initializer(v) + ";\n";
1065  i++;
1066  }
1067  s << std::endl;
1068  }
1069 
1070  // External function declarations
1071  if (!added_externals_.empty()) {
1072  s << "/* External functions */\n";
1073  for (auto&& i : added_externals_) {
1074  s << i << std::endl;
1075  }
1076  s << std::endl << std::endl;
1077  }
1078 
1079  // Codegen body
1080  s << this->body.str();
1081 
1082  // End with new line
1083  s << std::endl;
1084  }
std::vector< std::vector< char > > char_constants_
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::vector< std::vector< double > > double_constants_
std::vector< std::vector< casadi_int > > integer_constants_
std::vector< std::vector< std::string > > string_constants_
std::map< const void *, casadi_int > file_scope_integer_
std::string initializer(const std::vector< T > &v)
Print an initializer.
std::set< std::string > added_shorthands_

References add_auxiliary(), added_externals_, added_shorthands_, AUX_INF, AUX_NAN, auxiliaries, body, char_constants_, current_indent_, double_constants_, file_scope_double_, file_scope_integer_, includes, initializer(), integer_constants_, needs_mem_, pool_double_, pool_double_defaults_, prefix, print_vector(), casadi::str(), string_constants_, sz_ones_, sz_zeros_, with_export, and with_mem.

◆ elide_copy()

bool casadi::CodeGenerator::elide_copy ( casadi_int  sz)

◆ equal()

template<typename T >
static bool casadi::CodeGenerator::equal ( const std::vector< T > &  v1,
const std::vector< T > &  v2 
)
inlinestatic

Definition at line 1204 of file code_generator.hpp.

1204  {
1205  if (v1.size()!=v2.size()) return false;
1206  for (casadi_int j=0; j<v1.size(); ++j) {
1207  if (v1[j]!=v2[j]) return false;
1208  }
1209  return true;
1210  }

Referenced by get_constant().

◆ file_slurp()

std::string casadi::CodeGenerator::file_slurp ( const std::string &  fname,
casadi_int  n,
const std::string &  a 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u7

Definition at line 2991 of file code_generator.cpp.

2992  {
2994  return "casadi_file_slurp(\"" + fname + "\", " + str(n) + ", " + a + ")";
2995  }

References add_auxiliary(), AUX_FILE_SLURP, and casadi::str().

Referenced by casadi::ConstantFile::codegen_incref().

◆ fill()

std::string casadi::CodeGenerator::fill ( const std::string &  res,
std::size_t  n,
const std::string &  v 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tu

Definition at line 2276 of file code_generator.cpp.

2277  {
2278  if (v=="0") return clear(res, n);
2279  std::stringstream s;
2280  // Perform operation
2282  s << "casadi_fill(" << res << ", " << n << ", " << v << ");";
2283  return s.str();
2284  }
std::string clear(const std::string &res, std::size_t n)
Create a fill operation.

References add_auxiliary(), AUX_FILL, clear(), and res().

Referenced by casadi::Qrqp::codegen_body(), casadi::Sqpmethod::codegen_body(), casadi::Sqpmethod::codegen_solve_elastic_mode(), copy_default(), and casadi::Constant< Value >::generate().

◆ flush()

void casadi::CodeGenerator::flush ( std::ostream &  s)

◆ fmax()

std::string casadi::CodeGenerator::fmax ( const std::string &  x,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t4

Definition at line 2868 of file code_generator.cpp.

2869  {
2871  return "casadi_fmax(" + x + ", " + y + ");";
2872  }

References add_auxiliary(), and AUX_FMAX.

Referenced by casadi::Sqpmethod::codegen_body(), and casadi::Sqpmethod::codegen_calc_gamma_1().

◆ fmin()

std::string casadi::CodeGenerator::fmin ( const std::string &  x,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t5

Definition at line 2874 of file code_generator.cpp.

2875  {
2877  return "casadi_fmin(" + x + ", " + y + ");";
2878  }

References add_auxiliary(), and AUX_FMIN.

Referenced by casadi::Feasiblesqpmethod::codegen_tr_update().

◆ fmu_helpers()

std::string casadi::CodeGenerator::fmu_helpers ( const std::string &  modelname)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_257

Definition at line 2166 of file code_generator.cpp.

2166  {
2167  // Process C++ source
2168  std::stringstream ret;
2169  std::string line;
2170  std::istringstream stream(casadi_fmu_str);
2171  while (std::getline(stream, line)) {
2172  // Replacements
2173  if (line.find("MODELNAME") != std::string::npos) {
2174  line = replace(line, "MODELNAME", modelname);
2175  }
2176  // Append to return
2177  ret << line << "\n";
2178  }
2179  return ret.str();
2180  }

References casadi::replace().

Referenced by casadi::DaeBuilderInternal::generate_wrapper().

◆ format_padded()

std::string casadi::CodeGenerator::format_padded ( casadi_int  i) const

Definition at line 1116 of file code_generator.cpp.

1116  {
1117  std::stringstream ss;
1118  ss.str("");
1119  ss << std::setw(padding_length_) << std::setfill('0') << i;
1120  return ss.str();
1121  }

References padding_length_.

Referenced by casadi::MXFunction::codegen_body(), sx_work(), work(), and workel().

◆ fprintf_scalar()

std::string casadi::CodeGenerator::fprintf_scalar ( const std::string &  f,
const std::string &  arg 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_2fp

Definition at line 1178 of file code_generator.cpp.

1178  {
1180  std::stringstream s;
1181  s << "casadi_fprintf_scalar(" << f << ", " << arg << ");";
1182  return s.str();
1183  }

References add_auxiliary(), arg(), and AUX_FPRINTF_SCALAR.

◆ fprintf_vector()

std::string casadi::CodeGenerator::fprintf_vector ( const std::string &  f,
casadi_int  sz,
const std::string &  arg,
const std::string &  sep 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_2fq

Definition at line 1185 of file code_generator.cpp.

1186  {
1188  std::stringstream s;
1189  s << "casadi_fprintf_vector(" << f << ", " << sz << ", " << arg << ", \"" << sep << "\");";
1190  return s.str();
1191  }

References add_auxiliary(), arg(), and AUX_FPRINTF_VECTOR.

Referenced by generate_dump().

◆ from_mex()

std::string casadi::CodeGenerator::from_mex ( std::string &  arg,
const std::string &  res,
std::size_t  res_off,
const Sparsity sp_res,
const std::string &  w 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u4

Definition at line 2153 of file code_generator.cpp.

2155  {
2156  // Handle offset with recursion
2157  if (res_off!=0) return from_mex(arg, res+"+"+str(res_off), 0, sp_res, w);
2158 
2160  std::stringstream s;
2161  s << "casadi_from_mex(" << arg
2162  << ", " << res << ", " << sparsity(sp_res) << ", " << w << ");";
2163  return s.str();
2164  }
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.

References add_auxiliary(), arg(), AUX_FROM_MEX, res(), sparsity(), and casadi::str().

Referenced by casadi::FunctionInternal::codegen_meta().

◆ generate()

std::string casadi::CodeGenerator::generate ( const std::string &  prefix = "")

The "prefix" argument will be prepended to the generated files and may be a directory or a file prefix. returns the filename

Extra doc: https://github.com/casadi/casadi/wiki/L_rv

Definition at line 531 of file code_generator.cpp.

531  {
532  // Throw an error if the prefix contains the filename, since since syntax
533  // has changed
534  casadi_assert(prefix.find(this->name + this->suffix)==std::string::npos,
535  "The signature of CodeGenerator::generate has changed. "
536  "Instead of providing the filename, only provide the prefix.");
537 
538  // Create c file
539  std::string fullname = prefix + this->name + this->suffix;
540 
541  auto s_ptr = Filesystem::ofstream_ptr(fullname);
542  std::ostream& s = *s_ptr;
543  stream_open(s, this->cpp);
544 
545  // Dump code to file
546  dump(s);
547 
548  if (!pool_double_defaults_.empty()) {
549  s << "CASADI_SYMBOL_EXPORT casadi_real* CASADI_PREFIX(get_pool_double)(const char* name) {\n";
550  for (const auto& e : pool_double_) {
551  casadi_int i = e.second;
552  s << " if (strcmp(name, \"" + e.first + "\")==0) "
553  << "return casadi_pd" + str(i) + ";\n";
554  }
555  s << " return 0;\n";
556  s << "}\n";
557  }
558 
559  // Mex entry point
560  if (this->mex) generate_mex(s);
561 
562  // Main entry point
563  if (this->main) generate_main(s);
564 
565  // Finalize file
566  stream_close(s, this->cpp);
567  s_ptr.reset();
568 
569  // Generate s-function
570  if (this->with_sfunction) {
571  for (unsigned ii=0; ii<this->added_sfunctions.size(); ii++) {
572  std::string sfunction_code = this->added_sfunctions.at(ii);
573  std::string sfunction_name = this->exposed_fname.at(ii);
574  generate_sfunction(sfunction_name, sfunction_code);
575  }
576  }
577 
578  // Generate header
579  if (this->with_header) {
580  auto s_ptr = Filesystem::ofstream_ptr(prefix + this->name + ".h");
581  std::ostream& s = *s_ptr;
582  // Create a header file
583  stream_open(s, this->cpp);
584 
585  // Define the casadi_real type (typically double)
586  generate_casadi_real(s);
587 
588  // Define the casadi_int type
589  generate_casadi_int(s);
590 
591  // Generate export symbol macros
592  if (this->with_import) generate_import_symbol(s);
593 
594  // Add declarations
595  s << this->header.str();
596 
597  // Finalize file
598  stream_close(s, this->cpp);
599  s_ptr.reset();
600  }
601  return fullname;
602  }
static void stream_open(std::ostream &f, bool cpp)
Print file header.
static void stream_close(std::ostream &f, bool cpp)
Print file header.
static std::unique_ptr< std::ostream > ofstream_ptr(const std::string &path, std::ios_base::openmode mode=std::ios_base::out)
Definition: filesystem.cpp:115

References added_sfunctions, cpp, dump(), exposed_fname, header, main, mex, name, casadi::Filesystem::ofstream_ptr(), pool_double_, pool_double_defaults_, prefix, casadi::str(), stream_close(), stream_open(), suffix, with_header, with_import, and with_sfunction.

Referenced by casadi::DaeBuilderInternal::export_fmu(), casadi::FunctionInternal::finalize(), casadi::Function::generate(), casadi::OracleFunction::generate_dependencies(), and casadi::Scpgen::init().

◆ generate_dump()

void casadi::CodeGenerator::generate_dump ( const Function f,
const std::string &  arr,
bool  is_input 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_2fr

Definition at line 3003 of file code_generator.cpp.

3004  {
3005  casadi_int n = is_input ? f.n_in() : f.n_out();
3006  std::string dump_format = f->dump_format_.empty() ? "mtx" : f->dump_format_;
3007  std::string effective_dir = dump_dir_prefix + f->dump_dir_ + dump_dir_suffix;
3008  std::string prefix;
3009  if (!effective_dir.empty()) prefix = effective_dir + "/";
3010  std::string inout = is_input ? "in" : "out";
3011 
3012  // Ensure directory exists at codegen time
3013  if (!effective_dir.empty() && Filesystem::is_enabled()) {
3014  std::string dir = Filesystem::ensure_trailing_slash(effective_dir);
3015  casadi_assert(Filesystem::ensure_directory_exists(dir),
3016  "Unable to create the required directory for '" + dir + "'.");
3017  }
3018 
3019  // Assumes dump_id_local is declared in the enclosing scope
3020  // Per-input/output format files (e.g. .mtx)
3021  for (casadi_int i = 0; i < n; ++i) {
3022  std::string io_name = is_input ? f.name_in(i) : f.name_out(i);
3023  Sparsity sp = is_input ? f.sparsity_in(i) : f.sparsity_out(i);
3024  std::string fixed_part = prefix + f.name() + "." + inout + "." + io_name + "." + dump_format;
3025  casadi_int buf_size = fixed_part.size() + 1 + 6 + 1; // +7 for the counter field
3026  *this << "{\n";
3027  *this << "char dump_fname[" << buf_size << "];\n";
3028  *this << "FILE* dump_file;\n";
3029  *this << "snprintf(dump_fname, " << buf_size << ", \""
3030  << prefix << f.name() << ".%06d." << inout << "." << io_name
3031  << "." << dump_format << "\", dump_id_local);\n";
3032  if (f->verbose_) {
3033  *this << printf("dump -> %s\\n", "dump_fname") << "\n";
3034  }
3035  *this << "dump_file = fopen(dump_fname, \"w\");\n";
3036  *this << "if (dump_file) {\n";
3037  *this << to_file("dump_file", sp, arr + "[" + str(i) + "]") << ";\n";
3038  *this << "fclose(dump_file);\n";
3039  *this << "}\n";
3040  *this << "}\n";
3041  }
3042  // Combined .txt file (normalized doubles, one per line)
3043  {
3044  add_include("stdio.h");
3045  std::string fixed_part = prefix + f.name() + "." + inout + ".txt";
3046  casadi_int buf_size = fixed_part.size() + 1 + 6 + 1;
3047  *this << "{\n";
3048  *this << "char dump_fname[" << buf_size << "];\n";
3049  *this << "FILE* dump_file;\n";
3050  *this << "casadi_int dump_k;\n";
3051  *this << "snprintf(dump_fname, " << buf_size << ", \""
3052  << prefix << f.name() << ".%06d." << inout << ".txt\", dump_id_local);\n";
3053  *this << "dump_file = fopen(dump_fname, \"w\");\n";
3054  *this << "if (dump_file) {\n";
3055  for (casadi_int i = 0; i < n; ++i) {
3056  Sparsity sp = is_input ? f.sparsity_in(i) : f.sparsity_out(i);
3057  casadi_int nnz = sp.nnz();
3058  std::string a = arr + "[" + str(i) + "]";
3059  if (nnz > 0) {
3060  *this << "if (" << a << ") {\n";
3061  *this << fprintf_vector("dump_file", nnz, a, "\\n") << "\n";
3062  *this << "fprintf(dump_file, \"\\n\");\n";
3063  *this << "} else {\n";
3064  std::string zero_str = is_input ? "0.0000000000000000e+00" : "nan";
3065  *this << "for (dump_k=0; dump_k<" << nnz << "; ++dump_k) "
3066  << "fprintf(dump_file, \"" << zero_str << "\\n\");\n";
3067  *this << "}\n";
3068  }
3069  }
3070  *this << "fclose(dump_file);\n";
3071  *this << "}\n";
3072  *this << "}\n";
3073  }
3074  }
std::string printf(const std::string &str, const std::vector< std::string > &arg=std::vector< std::string >())
Printf.
std::string to_file(const std::string &f, const Sparsity &sp, const std::string &x)
Write matrix to file in MatrixMarket format.
std::string fprintf_vector(const std::string &f, casadi_int sz, const std::string &arg, const std::string &sep)
fprintf a normalized vector to a file with a separator
static std::string ensure_trailing_slash(const std::string &path)
Definition: filesystem.cpp:155
static bool ensure_directory_exists(const std::string &path)
Definition: filesystem.cpp:105
static bool is_enabled()
Definition: filesystem.cpp:83

References add_include(), casadi::FunctionInternal::dump_dir_, dump_dir_prefix, dump_dir_suffix, casadi::FunctionInternal::dump_format_, casadi::Filesystem::ensure_directory_exists(), casadi::Filesystem::ensure_trailing_slash(), fprintf_vector(), casadi::Filesystem::is_enabled(), casadi::Function::n_in(), casadi::Function::n_out(), casadi::Function::name(), casadi::Function::name_in(), casadi::Function::name_out(), casadi::Sparsity::nnz(), prefix, printf(), casadi::Function::sparsity_in(), casadi::Function::sparsity_out(), casadi::str(), to_file(), and casadi::ProtoFunction::verbose_.

Referenced by casadi::FunctionInternal::codegen().

◆ generate_print()

void casadi::CodeGenerator::generate_print ( const Function f,
const std::string &  arr,
bool  is_input 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_2fs

Definition at line 3076 of file code_generator.cpp.

3077  {
3078  casadi_int n = is_input ? f.n_in() : f.n_out();
3079  std::string inout = is_input ? "Input" : "Output";
3080 
3081  *this << printf("Function " + f.name() + "\\n") << "\n";
3082  for (casadi_int i = 0; i < n; ++i) {
3083  std::string io_name = is_input ? f.name_in(i) : f.name_out(i);
3084  Sparsity sp = is_input ? f.sparsity_in(i) : f.sparsity_out(i);
3085  *this << printf(inout + " " + str(i) + " (" + io_name + "): ") << "\n";
3086  *this << print_canonical(sp, arr + "[" + str(i) + "]") << "\n";
3087  *this << printf("\\n") << "\n";
3088  }
3089  }
std::string print_canonical(const Sparsity &sp, const std::string &arg)
Print canonical representaion of a matrix.

References casadi::Function::n_in(), casadi::Function::n_out(), casadi::Function::name(), casadi::Function::name_in(), casadi::Function::name_out(), print_canonical(), printf(), casadi::Function::sparsity_in(), casadi::Function::sparsity_out(), and casadi::str().

Referenced by casadi::FunctionInternal::codegen().

◆ get_constant() [1/4]

casadi_int casadi::CodeGenerator::get_constant ( const std::vector< casadi_int > &  v,
bool  allow_adding = false 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_ry

Definition at line 1393 of file code_generator.cpp.

1393  {
1394  // Hash the vector
1395  size_t h = hash(v);
1396 
1397  // Try to locate it in already added constants
1398  std::pair<std::multimap<size_t, size_t>::iterator, std::multimap<size_t, size_t>::iterator> eq =
1399  added_integer_constants_.equal_range(h);
1400  for (std::multimap<size_t, size_t>::iterator i=eq.first; i!=eq.second; ++i) {
1401  if (equal(v, integer_constants_[i->second])) return i->second;
1402  }
1403 
1404  if (allow_adding) {
1405  // Add to constants
1406  casadi_int ind = integer_constants_.size();
1407  integer_constants_.push_back(v);
1408  added_integer_constants_.insert(std::pair<size_t, size_t>(h, ind));
1409  return ind;
1410  } else {
1411  casadi_error("Constant not found");
1412  return -1;
1413  }
1414  }
static size_t hash(const std::vector< double > &v)
std::multimap< size_t, size_t > added_integer_constants_
static bool equal(const std::vector< T > &v1, const std::vector< T > &v2)

References added_integer_constants_, equal(), hash(), and integer_constants_.

◆ get_constant() [2/4]

casadi_int casadi::CodeGenerator::get_constant ( const std::vector< char > &  v,
bool  allow_adding = false 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_27l

Definition at line 1416 of file code_generator.cpp.

1416  {
1417  // Hash the vector
1418  size_t h = hash(v);
1419 
1420  // Try to locate it in already added constants
1421  std::pair<std::multimap<size_t, size_t>::iterator, std::multimap<size_t, size_t>::iterator> eq =
1422  added_char_constants_.equal_range(h);
1423  for (std::multimap<size_t, size_t>::iterator i=eq.first; i!=eq.second; ++i) {
1424  if (equal(v, char_constants_[i->second])) return i->second;
1425  }
1426 
1427  if (allow_adding) {
1428  // Add to constants
1429  casadi_int ind = char_constants_.size();
1430  char_constants_.push_back(v);
1431  added_char_constants_.insert(std::pair<size_t, size_t>(h, ind));
1432  return ind;
1433  } else {
1434  casadi_error("Constant not found");
1435  return -1;
1436  }
1437  }
std::multimap< size_t, size_t > added_char_constants_

References added_char_constants_, char_constants_, equal(), and hash().

◆ get_constant() [3/4]

casadi_int casadi::CodeGenerator::get_constant ( const std::vector< double > &  v,
bool  allow_adding = false 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_rx

Definition at line 1371 of file code_generator.cpp.

1371  {
1372  // Hash the vector
1373  size_t h = hash(v);
1374 
1375  // Try to locate it in already added constants
1376  auto eq = added_double_constants_.equal_range(h);
1377  for (auto i=eq.first; i!=eq.second; ++i) {
1378  if (equal(v, double_constants_[i->second])) return i->second;
1379  }
1380 
1381  if (allow_adding) {
1382  // Add to constants
1383  casadi_int ind = double_constants_.size();
1384  double_constants_.push_back(v);
1385  added_double_constants_.insert(std::make_pair(h, ind));
1386  return ind;
1387  } else {
1388  casadi_error("Constant not found");
1389  return -1;
1390  }
1391  }
std::multimap< size_t, size_t > added_double_constants_

References added_double_constants_, double_constants_, equal(), and hash().

Referenced by add_sparsity(), constant(), and get_sparsity().

◆ get_constant() [4/4]

casadi_int casadi::CodeGenerator::get_constant ( const std::vector< std::string > &  v,
bool  allow_adding = false 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_27z

Definition at line 1439 of file code_generator.cpp.

1439  {
1440  // Hash the vector
1441  size_t h = hash(v);
1442 
1443  // Try to locate it in already added constants
1444  std::pair<std::multimap<size_t, size_t>::iterator, std::multimap<size_t, size_t>::iterator> eq =
1445  added_string_constants_.equal_range(h);
1446  for (std::multimap<size_t, size_t>::iterator i=eq.first; i!=eq.second; ++i) {
1447  if (equal(v, string_constants_[i->second])) return i->second;
1448  }
1449 
1450  if (allow_adding) {
1451  // Add to constants
1452  casadi_int ind = string_constants_.size();
1453  string_constants_.push_back(v);
1454  added_string_constants_.insert(std::pair<size_t, size_t>(h, ind));
1455  return ind;
1456  } else {
1457  casadi_error("Constant not found");
1458  return -1;
1459  }
1460  }
std::multimap< size_t, size_t > added_string_constants_

References added_string_constants_, equal(), hash(), and string_constants_.

◆ get_sparsity()

casadi_int casadi::CodeGenerator::get_sparsity ( const Sparsity sp) const

Extra doc: https://github.com/casadi/casadi/wiki/L_rw

Definition at line 1335 of file code_generator.cpp.

1335  {
1336  return const_cast<CodeGenerator&>(*this).get_constant(sp, false);
1337  }
CodeGenerator(const std::string &name, const Dict &opts=Dict())
Constructor.

References get_constant().

◆ hash() [1/4]

size_t casadi::CodeGenerator::hash ( const std::vector< casadi_int > &  v)
static

Definition at line 1353 of file code_generator.cpp.

1353  {
1354  size_t seed=0;
1355  hash_combine(seed, v);
1356  return seed;
1357  }
void hash_combine(std::size_t &seed, T v)
Generate a hash value incrementally (function taken from boost)

References casadi::hash_combine().

◆ hash() [2/4]

size_t casadi::CodeGenerator::hash ( const std::vector< char > &  v)
static

Definition at line 1359 of file code_generator.cpp.

1359  {
1360  size_t seed=0;
1361  hash_combine(seed, v);
1362  return seed;
1363  }

References casadi::hash_combine().

◆ hash() [3/4]

size_t casadi::CodeGenerator::hash ( const std::vector< double > &  v)
static

Definition at line 1339 of file code_generator.cpp.

1339  {
1340  // Calculate a hash value for the vector
1341  std::size_t seed=0;
1342  if (!v.empty()) {
1343  casadi_assert_dev(sizeof(double) % sizeof(size_t)==0);
1344  const casadi_int int_len = v.size()*(sizeof(double)/sizeof(size_t));
1345  const size_t* int_v = reinterpret_cast<const size_t*>(&v.front());
1346  for (size_t i=0; i<int_len; ++i) {
1347  hash_combine(seed, int_v[i]);
1348  }
1349  }
1350  return seed;
1351  }

References casadi::hash_combine().

Referenced by get_constant().

◆ hash() [4/4]

size_t casadi::CodeGenerator::hash ( const std::vector< std::string > &  v)
static

Definition at line 1365 of file code_generator.cpp.

1365  {
1366  size_t seed=0;
1367  hash_combine(seed, v);
1368  return seed;
1369  }

References casadi::hash_combine().

◆ indent()

void casadi::CodeGenerator::indent ( )
inline

Extra doc: https://github.com/casadi/casadi/wiki/L_sg

Definition at line 291 of file code_generator.hpp.

291 {current_indent_++;}

Referenced by print_formatted().

◆ init_local()

void casadi::CodeGenerator::init_local ( const std::string &  name,
const std::string &  def 
)

◆ initializer()

template<typename T >
std::string casadi::CodeGenerator::initializer ( const std::vector< T > &  v)
inline

Extra doc: https://github.com/casadi/casadi/wiki/L_sk

Definition at line 325 of file code_generator.hpp.

325  {
326  std::stringstream s;
327  if (v.size() > max_initializer_elements_per_line) {
328  s << "\n ";
329  }
330 
331  s << "{";
332  // Shortcut only for arithmetic T; for std::string, `T(0)` would
333  // invoke std::string(const char*) with a null pointer.
334  bool all_zeros = std::is_arithmetic<T>::value && v.size() > 0;
335  if (all_zeros) {
336  for (const auto& el : v) {
337  if (el != T()) {
338  all_zeros = false;
339  break;
340  }
341  }
342  }
343  if (all_zeros) {
344  s << constant(T()); // empty_initialization shorthand
345  } else {
346  for (casadi_int i = 0; i < v.size(); ++i) {
347  if (i != 0) {
350  s << ",\n ";
351  } else {
352  s << ", ";
353  }
354  }
355  s << constant(v[i]);
356  }
357  }
358  s << "}";
359  return s.str();
360  }

References casadi::T.

Referenced by casadi::codegen_int_array(), dump(), and print_vector().

◆ interpn()

std::string casadi::CodeGenerator::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 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sw

Definition at line 2312 of file code_generator.cpp.

2317  {
2319  std::stringstream s;
2320  s << "casadi_interpn(" << res << ", " << ndim << ", " << grid << ", " << offset << ", "
2321  << values << ", " << x << ", " << lookup_mode << ", " << m << ", " << iw << ", " << w << ");";
2322  return s.str();
2323  }

References add_auxiliary(), AUX_INTERPN, and res().

Referenced by casadi::LinearInterpolant::codegen_body().

◆ interpn_grad()

std::string casadi::CodeGenerator::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 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sx

Definition at line 2325 of file code_generator.cpp.

2329  {
2331  std::stringstream s;
2332  s << "casadi_interpn_grad(" << grad << ", " << ndim << ", " << grid << ", " << offset << ", "
2333  << values << ", " << x << ", " << lookup_mode << "," << m << ", " << iw << ", " << w << ");";
2334  return s.str();
2335  }

References add_auxiliary(), and AUX_INTERPN_GRAD.

Referenced by casadi::LinearInterpolantJac::codegen_body().

◆ lb_eig()

std::string casadi::CodeGenerator::lb_eig ( const Sparsity sp_h,
const std::string &  h 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_ti

Definition at line 2958 of file code_generator.cpp.

2959  {
2961  return "casadi_lb_eig(" + sparsity(sp_h) + ", " + h + ")";
2962  }

References add_auxiliary(), AUX_REGULARIZE, and sparsity().

◆ ldl()

std::string casadi::CodeGenerator::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 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t2

Definition at line 2850 of file code_generator.cpp.

2853  {
2855  return "casadi_ldl(" + sp_a + ", " + a + ", " + sp_lt + ", " + lt + ", "
2856  + d + ", " + p + ", " + w + ");";
2857  }

References add_auxiliary(), and AUX_LDL.

Referenced by casadi::LinsolLdl::generate().

◆ ldl_solve()

std::string casadi::CodeGenerator::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 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t3

Definition at line 2859 of file code_generator.cpp.

2862  {
2864  return "casadi_ldl_solve(" + x + ", " + str(nrhs) + ", " + sp_lt + ", "
2865  + lt + ", " + d + ", " + p + ", " + w + ");";
2866  }

References add_auxiliary(), AUX_LDL, and casadi::str().

Referenced by casadi::LinsolLdl::generate().

◆ local()

void casadi::CodeGenerator::local ( const std::string &  name,
const std::string &  type,
const std::string &  ref = "" 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sb

Definition at line 2600 of file code_generator.cpp.

2601  {
2602  // Check if the variable already exists
2603  auto it = local_variables_.find(name);
2604  if (it==local_variables_.end()) {
2605  // Add it
2606  local_variables_[name] = std::make_pair(type, ref);
2607  } else {
2608  // Consistency check
2609  casadi_assert(it->second.first==type, "Type mismatch for " + name);
2610  casadi_assert(it->second.second==ref, "Type mismatch for " + name);
2611  }
2612  }
std::map< std::string, std::pair< std::string, std::string > > local_variables_

References local_variables_, and name.

Referenced by casadi::blasfeo_codegen_mtimes(), casadi::classic_codegen_mtimes(), casadi::FiniteDiff::codegen_body(), casadi::JitFunction::codegen_body(), casadi::Map::codegen_body(), casadi::OmpMap::codegen_body(), casadi::ThreadMap::codegen_body(), casadi::MapSum::codegen_body(), casadi::MXFunction::codegen_body(), casadi::Switch::codegen_body(), casadi::MadmpecInterface::codegen_body(), casadi::ClarabelInterface::codegen_body(), casadi::DaqpInterface::codegen_body(), casadi::FatropInterface::codegen_body(), casadi::HighsInterface::codegen_body(), casadi::HpipmInterface::codegen_body(), casadi::IpoptInterface::codegen_body(), casadi::MadnlpInterface::codegen_body(), casadi::MosekInterface::codegen_body(), casadi::OsqpInterface::codegen_body(), casadi::UnoInterface::codegen_body(), casadi::XpressInterface::codegen_body(), casadi::FastNewton::codegen_body(), casadi::Feasiblesqpmethod::codegen_body(), casadi::Qrqp::codegen_body(), casadi::Sqpmethod::codegen_body(), casadi::OracleFunction::codegen_body_enter(), casadi::Nlpsol::codegen_body_enter(), casadi::FunctionInternal::codegen_checkout(), casadi::Feasiblesqpmethod::codegen_feasibility_iterations(), casadi::OsqpInterface::codegen_init_mem(), casadi::UnoInterface::codegen_init_mem(), casadi::codegen_local(), casadi::Nlpsol::codegen_post_solve(), casadi::codegen_unpack_block(), constant_copy(), casadi::Convexify::generate(), casadi::BinaryMX< ScX, ScY >::generate(), casadi::Call::generate(), casadi::Find::generate(), casadi::Low::generate(), casadi::Concat::generate(), casadi::Einstein::generate(), casadi::GetNonzerosVector::generate(), casadi::GetNonzerosSlice::generate(), casadi::GetNonzerosSlice2::generate(), casadi::GetNonzerosParamVector::generate(), casadi::GetNonzerosSliceParam::generate(), casadi::GetNonzerosParamSlice::generate(), casadi::GetNonzerosParamParam::generate(), casadi::HorzRepmat::generate(), casadi::HorzRepsum::generate(), casadi::SetNonzerosVector< Add >::generate(), casadi::SetNonzerosSlice< Add >::generate(), casadi::SetNonzerosSlice2< Add >::generate(), casadi::SetNonzerosParamVector< Add >::generate(), casadi::SetNonzerosParamSlice< Add >::generate(), casadi::SetNonzerosSliceParam< Add >::generate(), casadi::SetNonzerosParamParam< Add >::generate(), casadi::LinsolCall< Tr >::generate(), casadi::DenseTranspose::generate(), casadi::UnaryMX::generate(), casadi::LinsolQr::generate_factorize(), operator()(), casadi::Conic::qp_codegen_body(), scope_return(), casadi::MosekInterface::set_mosek_prob(), casadi::XpressInterface::set_xpress_prob(), and sx_work().

◆ local_mutex()

std::string casadi::CodeGenerator::local_mutex ( const Function f,
const std::string &  name 
) const

◆ local_mutexes()

const std::set< std::string > & casadi::CodeGenerator::local_mutexes ( const Function f) const

Extra doc: https://github.com/casadi/casadi/wiki/L_2fo

Definition at line 932 of file code_generator.cpp.

933  {
934  auto it = local_mutexes_.find(f.get());
935  if (it == local_mutexes_.end()) return empty_mutex_set_;
936  return it->second;
937  }
static const std::set< std::string > empty_mutex_set_

References casadi::empty_mutex_set_, casadi::Function::get(), and local_mutexes_.

Referenced by casadi::FunctionInternal::codegen_decref(), and casadi::FunctionInternal::codegen_incref().

◆ logsumexp()

std::string casadi::CodeGenerator::logsumexp ( const std::string &  A,
casadi_int  n 
)

\brie LogSumExp

Definition at line 2541 of file code_generator.cpp.

2541  {
2543  std::stringstream s;
2544  s << "casadi_logsumexp(" << A << ", " << n << ");";
2545  return s.str();
2546  }

References add_auxiliary(), and AUX_LOGSUMEXP.

Referenced by casadi::LogSumExp::generate().

◆ low()

std::string casadi::CodeGenerator::low ( const std::string &  x,
const std::string &  grid,
casadi_int  ng,
casadi_int  lookup_mode 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tl

Definition at line 2977 of file code_generator.cpp.

2978  {
2980  return "casadi_low(" + x + ", " + grid + ", " + str(ng) + ", " + str(lookup_mode) + ");";
2981  }

References add_auxiliary(), AUX_LOW, and casadi::str().

Referenced by casadi::Low::generate().

◆ lsqr_solve()

std::string casadi::CodeGenerator::lsqr_solve ( const std::string &  A,
const std::string &  x,
casadi_int  nrhs,
bool  tr,
const std::string &  sp,
const std::string &  w 
)

\brief LSQR solve

Extra doc: https://github.com/casadi/casadi/wiki/L_t1

Definition at line 2842 of file code_generator.cpp.

2844  {
2846  return "casadi_lsqr_solve(" + A + ", " + x + ", " + str(nrhs) + ", "
2847  + (tr ? "1" : "0") + ", " + sp + ", " + w + ");";
2848  }

References add_auxiliary(), AUX_LSQR, and casadi::str().

Referenced by casadi::Lsqr::generate().

◆ masked_norm_inf()

std::string casadi::CodeGenerator::masked_norm_inf ( casadi_int  n,
const std::string &  x,
const std::string &  mask 
)

Definition at line 2477 of file code_generator.cpp.

2478  {
2480  return "casadi_masked_norm_inf(" + str(n) + ", " + x + ", " + mask + ")";
2481  }

References add_auxiliary(), AUX_MASKED_NORM_INF, and casadi::str().

Referenced by casadi::Feasiblesqpmethod::codegen_body(), casadi::Feasiblesqpmethod::codegen_feasibility_iterations(), and casadi::Feasiblesqpmethod::codegen_tr_update().

◆ max()

std::string casadi::CodeGenerator::max ( const std::string &  x,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tc

Definition at line 2904 of file code_generator.cpp.

2905  {
2907  return "casadi_max(" + x + ", " + y + ")";
2908  }

References add_auxiliary(), and AUX_MAX.

◆ max_viol()

std::string casadi::CodeGenerator::max_viol ( casadi_int  n,
const std::string &  x,
const std::string &  lb,
const std::string &  ub 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tf

Definition at line 2928 of file code_generator.cpp.

2929  {
2931  return "casadi_max_viol(" + str(n) + ", " + x+ ", " + lb + ", " + ub + ")";
2932  }

References add_auxiliary(), AUX_MAX_VIOL, and casadi::str().

Referenced by casadi::Feasiblesqpmethod::codegen_body(), casadi::Sqpmethod::codegen_body(), and casadi::Feasiblesqpmethod::codegen_feasibility_iterations().

◆ mem()

std::string casadi::CodeGenerator::mem ( const Function f)

Extra doc: https://github.com/casadi/casadi/wiki/L_ty

Definition at line 2270 of file code_generator.cpp.

2270  {
2271  std::string name = f->codegen_name(*this, false);
2272  std::string mem_array = shorthand(name + "_mem");
2273  return mem_array+"[mem]";
2274  }

References casadi::FunctionInternal::codegen_name(), name, and shorthand().

Referenced by operator()().

◆ min()

std::string casadi::CodeGenerator::min ( const std::string &  x,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_td

Definition at line 2910 of file code_generator.cpp.

2911  {
2913  return "casadi_min(" + x + ", " + y + ")";
2914  }

References add_auxiliary(), and AUX_MIN.

Referenced by clip_max(), and clip_min().

◆ mmax()

std::string casadi::CodeGenerator::mmax ( const std::string &  x,
casadi_int  n,
bool  is_dense 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t6

Definition at line 2916 of file code_generator.cpp.

2917  {
2919  return "casadi_mmax(" + x + ", " + str(n) + ", " + str(casadi_int(is_dense)) + ")";
2920  }

References add_auxiliary(), AUX_MMAX, and casadi::str().

Referenced by casadi::MMax::generate().

◆ mmin()

std::string casadi::CodeGenerator::mmin ( const std::string &  x,
casadi_int  n,
bool  is_dense 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t7

Definition at line 2922 of file code_generator.cpp.

2923  {
2925  return "casadi_mmin(" + x + ", " + str(n) + ", " + str(casadi_int(is_dense)) + ")";
2926  }

References add_auxiliary(), AUX_MMIN, and casadi::str().

Referenced by casadi::MMin::generate().

◆ mtimes() [1/2]

std::string casadi::CodeGenerator::mtimes ( const std::string &  x,
casadi_int  nrow_x,
casadi_int  ncol_x,
const std::string &  y,
casadi_int  ncol_y,
const std::string &  z,
bool  tr 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_2gc

Definition at line 2511 of file code_generator.cpp.

2513  {
2515  return "casadi_mtimes_dense(" + x + ", " + str(nrow_x) + ", " + str(ncol_x) + ", "
2516  + y + ", " + str(ncol_y) + ", " + z + ", " + (tr ? "1" : "0") + ");";
2517  }

References add_auxiliary(), AUX_MTIMES_DENSE, and casadi::str().

◆ mtimes() [2/2]

std::string casadi::CodeGenerator::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 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sr

Definition at line 2502 of file code_generator.cpp.

2505  {
2507  return "casadi_mtimes(" + x + ", " + sparsity(sp_x) + ", " + y + ", " + sparsity(sp_y) + ", "
2508  + z + ", " + sparsity(sp_z) + ", " + w + ", " + (tr ? "1" : "0") + ");";
2509  }

References add_auxiliary(), AUX_MTIMES, and sparsity().

Referenced by casadi::Blas::codegen_mtimes(), and casadi::Multiplication::generate().

◆ mtimes_dense_sparse()

std::string casadi::CodeGenerator::mtimes_dense_sparse ( const std::string &  x,
casadi_int  nrow_x,
const std::string &  y,
const Sparsity sp_y,
const std::string &  z 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_2gd

Definition at line 2519 of file code_generator.cpp.

2520  {
2522  return "casadi_mtimes_dense_sparse(" + x + ", " + str(nrow_x) + ", "
2523  + y + ", " + sparsity(sp_y) + ", " + z + ");";
2524  }

References add_auxiliary(), AUX_MTIMES_DENSE_SPARSE, sparsity(), and casadi::str().

Referenced by casadi::DenseSparseMultiplication::generate().

◆ mv() [1/2]

std::string casadi::CodeGenerator::mv ( const std::string &  x,
casadi_int  nrow_x,
casadi_int  ncol_x,
const std::string &  y,
const std::string &  z,
bool  tr 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_so

Definition at line 2495 of file code_generator.cpp.

2496  {
2498  return "casadi_mv_dense(" + x + ", " + str(nrow_x) + ", " + str(ncol_x) + ", "
2499  + y + ", " + z + ", " + (tr ? "1" : "0") + ");";
2500  }

References add_auxiliary(), AUX_MV_DENSE, and casadi::str().

◆ mv() [2/2]

std::string casadi::CodeGenerator::mv ( const std::string &  x,
const Sparsity sp_x,
const std::string &  y,
const std::string &  z,
bool  tr 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sn

Definition at line 2488 of file code_generator.cpp.

2489  {
2491  return "casadi_mv(" + x + ", " + sparsity(sp_x) + ", " + y + ", "
2492  + z + ", " + (tr ? "1" : "0") + ");";
2493  }

References add_auxiliary(), AUX_MV, and sparsity().

Referenced by casadi::Feasiblesqpmethod::codegen_body(), casadi::Sqpmethod::codegen_body(), casadi::Feasiblesqpmethod::codegen_feasibility_iterations(), and casadi::Sqpmethod::codegen_solve_elastic_mode().

◆ norm_1()

std::string casadi::CodeGenerator::norm_1 ( casadi_int  n,
const std::string &  x 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_2br

Definition at line 2946 of file code_generator.cpp.

2947  {
2949  return "casadi_norm_1(" + str(n) + ", " + x + ")";
2950  }

References add_auxiliary(), AUX_NORM_1, and casadi::str().

Referenced by casadi::Norm1::generate().

◆ norm_2()

std::string casadi::CodeGenerator::norm_2 ( casadi_int  n,
const std::string &  x 
)
     Extra doc: https://github.com/casadi/casadi/wiki/L_256 

Definition at line 2952 of file code_generator.cpp.

2953  {
2955  return "casadi_norm_2(" + str(n) + ", " + x + ")";
2956  }

References add_auxiliary(), AUX_NORM_2, and casadi::str().

Referenced by casadi::Feasiblesqpmethod::codegen_feasibility_iterations(), and casadi::NormF::generate().

◆ norm_inf()

std::string casadi::CodeGenerator::norm_inf ( casadi_int  n,
const std::string &  x 
)

◆ ones()

std::string casadi::CodeGenerator::ones ( casadi_int  sz)

Definition at line 1479 of file code_generator.cpp.

1479  {
1480  sz_ones_ = std::max(sz_ones_, sz);
1481  return shorthand("ones");
1482  }

References shorthand(), and sz_ones_.

◆ operator()()

std::string casadi::CodeGenerator::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" 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_s6

Definition at line 1272 of file code_generator.cpp.

1275  {
1276  std::string name = add_dependency(f);
1277 
1278  std::string cg_name = f->codegen_name(*this, false);
1279  bool needs_mem = f->codegen_needs_mem();
1280  if (needs_mem) {
1281  std::string mem = "mid";
1282  local("flag", "int");
1283  local(mem, "int");
1284  std::string checkout = shorthand(cg_name + "_checkout");
1285  *this << mem << " = " << checkout << "();\n";
1286  if (failure_ret.empty()) {
1287  *this << "if (" << mem << "<0) {\n";
1288  *this << "flag = 1;\n";
1289  *this << "} else {\n";
1290  } else {
1291  *this << "if (" << mem << "<0) return " << failure_ret << ";\n";
1292  }
1293 
1294  *this << "flag = " + name + "(" + arg + ", " + res + ", "
1295  + iw + ", " + w + ", " << mem << ");\n";
1296 
1297  if (failure_ret.empty()) {
1298  *this << "}\n";
1299  }
1300 
1301  std::string release = shorthand(cg_name + "_release");
1302  *this << release << "(" << mem << ");\n";
1303  return "flag";
1304  } else {
1305  return name + "(" + arg + ", " + res + ", "
1306  + iw + ", " + w + ", 0)";
1307  }
1308  }
std::string mem(const Function &f)
Access thread-local memory.

References add_dependency(), arg(), casadi::FunctionInternal::codegen_name(), casadi::FunctionInternal::codegen_needs_mem(), local(), mem(), name, res(), and shorthand().

◆ operator<<() [1/2]

CodeGenerator & casadi::CodeGenerator::operator<< ( const std::string &  s)

Extra doc: https://github.com/casadi/casadi/wiki/L_s7

Definition at line 2574 of file code_generator.cpp.

2574  {
2575  // Loop over newline characters
2576  size_t off=0;
2577  while (true) {
2578  size_t pos = s.find('\n', off);
2579  if (pos==std::string::npos) {
2580  // No more newline characters
2581  print_formatted(s.substr(off));
2582  break;
2583  } else {
2584  // Ends with newline
2585  print_formatted(s.substr(off, pos-off));
2586  this->buffer << '\n';
2587  newline_ = true;
2588  off = pos+1;
2589  }
2590  }
2591 
2592  return *this;
2593  }
void print_formatted(const std::string &s)
Print without newline characters.

References buffer, newline_, and print_formatted().

◆ operator<<() [2/2]

template<typename T >
CodeGenerator& casadi::CodeGenerator::operator<< ( s)
inline

Extra doc: https://github.com/casadi/casadi/wiki/L_s9

Definition at line 233 of file code_generator.hpp.

233  {
234  std::stringstream ss;
235  ss << s;
236  return (*this) << ss.str();
237  }

◆ pool_double()

std::string casadi::CodeGenerator::pool_double ( const std::string &  name) const

Extra doc: https://github.com/casadi/casadi/wiki/L_2ax

Definition at line 903 of file code_generator.cpp.

903  {
904  auto it = pool_double_.find(name);
905  casadi_assert(it!=pool_double_.end(), "Not defined.");
906  return "casadi_pd" + str(it->second);
907  }

References name, pool_double_, and casadi::str().

Referenced by casadi::ConstantPool::generate().

◆ print_canonical()

std::string casadi::CodeGenerator::print_canonical ( const Sparsity sp,
const std::string &  arg 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_2dk

Definition at line 1157 of file code_generator.cpp.

1157  {
1159  std::stringstream s;
1160  s << "casadi_print_canonical(" << sparsity(sp) << ", " << arg << ");";
1161  return s.str();
1162  }

References add_auxiliary(), arg(), AUX_PRINT_CANONICAL, and sparsity().

Referenced by casadi::Monitor::generate(), generate_print(), casadi::MXFunction::print_arg(), and casadi::MXFunction::print_res().

◆ print_formatted()

void casadi::CodeGenerator::print_formatted ( const std::string &  s)

Extra doc: https://github.com/casadi/casadi/wiki/L_s8

Definition at line 2548 of file code_generator.cpp.

2548  {
2549  // Quick return if empty
2550  if (s.empty()) return;
2551 
2552  // If new line, add indentation
2553  if (newline_) {
2554  casadi_int shift = s.front()=='}' ? -1 : 0;
2555  casadi_assert_dev(current_indent_+shift>=0);
2556  this->buffer << std::string(indent_*(current_indent_+shift), ' ');
2557  newline_ = false;
2558  }
2559 
2560  // Print to body
2561  this->buffer << s;
2562 
2563  // Brackets change indentation for next row
2564  // NOTE(@jaeandersson): Should ignore strings, comments
2565  for (char c : s) {
2566  if (c=='{') {
2567  indent();
2568  } else if (c=='}') {
2569  unindent();
2570  }
2571  }
2572  }
void indent()
Increase indentation.
void unindent()
Decrease indentation.

References buffer, current_indent_, indent(), indent_, newline_, and unindent().

Referenced by operator<<().

◆ print_op() [1/2]

std::string casadi::CodeGenerator::print_op ( casadi_int  op,
const std::string &  a0 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u6

Definition at line 1193 of file code_generator.cpp.

1193  {
1194  switch (op) {
1195  case OP_FABS:
1197  return "casadi_fabs("+a0+")";
1198  case OP_SQ:
1200  return "casadi_sq("+a0+")";
1201  case OP_SIGN:
1203  return "casadi_sign("+a0+")";
1204  case OP_LOG1P:
1206  return "casadi_log1p("+a0+")";
1207  case OP_EXPM1:
1209  return "casadi_expm1("+a0+")";
1210  default:
1211  return casadi_math<double>::print(op, a0);
1212  }
1213  }
@ OP_SIGN
Definition: calculus.hpp:71
@ OP_LOG1P
Definition: calculus.hpp:202
@ 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:1651

References add_auxiliary(), AUX_EXPM1, AUX_FABS, AUX_LOG1P, AUX_SIGN, AUX_SQ, casadi::OP_EXPM1, casadi::OP_FABS, casadi::OP_LOG1P, casadi::OP_SIGN, casadi::OP_SQ, and casadi::casadi_math< T >::print().

Referenced by casadi::SXFunction::codegen_body(), casadi::BinaryMX< ScX, ScY >::generate(), and casadi::UnaryMX::generate().

◆ print_op() [2/2]

std::string casadi::CodeGenerator::print_op ( casadi_int  op,
const std::string &  a0,
const std::string &  a1 
)

Definition at line 1214 of file code_generator.cpp.

1214  {
1215  switch (op) {
1216  case OP_FMIN:
1218  return "casadi_fmin("+a0+","+a1+")";
1219  case OP_FMAX:
1221  return "casadi_fmax("+a0+","+a1+")";
1222  case OP_HYPOT:
1224  return "casadi_hypot("+a0+","+a1+")";
1225  case OP_PRINTME:
1227  return "casadi_printme("+a0+","+a1+")";
1228  default:
1229  return casadi_math<double>::print(op, a0, a1);
1230  }
1231  }
@ OP_FMAX
Definition: calculus.hpp:72
@ OP_PRINTME
Definition: calculus.hpp:190
@ OP_HYPOT
Definition: calculus.hpp:206
@ OP_FMIN
Definition: calculus.hpp:72

References add_auxiliary(), AUX_FMAX, AUX_FMIN, AUX_HYPOT, AUX_PRINTME, casadi::OP_FMAX, casadi::OP_FMIN, casadi::OP_HYPOT, casadi::OP_PRINTME, and casadi::casadi_math< T >::print().

◆ print_scalar()

std::string casadi::CodeGenerator::print_scalar ( const std::string &  arg)

Extra doc: https://github.com/casadi/casadi/wiki/L_2dm

Definition at line 1171 of file code_generator.cpp.

1171  {
1173  std::stringstream s;
1174  s << "casadi_print_scalar(" << arg << ");";
1175  return s.str();
1176  }

References add_auxiliary(), arg(), and AUX_PRINT_SCALAR.

◆ print_vector() [1/5]

std::string casadi::CodeGenerator::print_vector ( casadi_int  sz,
const std::string &  arg 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_2dl

Definition at line 1164 of file code_generator.cpp.

1164  {
1166  std::stringstream s;
1167  s << "casadi_print_vector(" << sz << ", " << arg << ");";
1168  return s.str();
1169  }

References add_auxiliary(), arg(), and AUX_PRINT_VECTOR.

◆ print_vector() [2/5]

void casadi::CodeGenerator::print_vector ( std::ostream &  s,
const std::string &  name,
const std::vector< casadi_int > &  v 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tr

Definition at line 1137 of file code_generator.cpp.

1138  {
1139  s << array("static const casadi_int", name, v.size(), initializer(v));
1140  }
static std::string array(const std::string &type, const std::string &name, casadi_int len, const std::string &def=std::string())

References array(), initializer(), and name.

Referenced by dump(), casadi::SXFunction::print_arg(), and casadi::SXFunction::print_res().

◆ print_vector() [3/5]

void casadi::CodeGenerator::print_vector ( std::ostream &  s,
const std::string &  name,
const std::vector< char > &  v 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_27n

Definition at line 1142 of file code_generator.cpp.

1143  {
1144  s << array("static const char", name, v.size(), initializer(v));
1145  }

References array(), initializer(), and name.

◆ print_vector() [4/5]

void casadi::CodeGenerator::print_vector ( std::ostream &  s,
const std::string &  name,
const std::vector< double > &  v 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_ts

Definition at line 1147 of file code_generator.cpp.

1148  {
1149  s << array("static const casadi_real", name, v.size(), initializer(v));
1150  }

References array(), initializer(), and name.

◆ print_vector() [5/5]

void casadi::CodeGenerator::print_vector ( std::ostream &  s,
const std::string &  name,
const std::vector< std::string > &  v 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_281

Definition at line 1152 of file code_generator.cpp.

1153  {
1154  s << array("static const char*", name, v.size(), initializer(v));
1155  }

References array(), initializer(), and name.

◆ printf() [1/4]

std::string casadi::CodeGenerator::printf ( const std::string &  str,
const std::string &  arg1 
)

Definition at line 2424 of file code_generator.cpp.

2424  {
2425  std::vector<std::string> arg;
2426  arg.push_back(arg1);
2427  return printf(str, arg);
2428  }

References arg(), printf(), and casadi::str().

◆ printf() [2/4]

std::string casadi::CodeGenerator::printf ( const std::string &  str,
const std::string &  arg1,
const std::string &  arg2 
)

Definition at line 2430 of file code_generator.cpp.

2431  {
2432  std::vector<std::string> arg;
2433  arg.push_back(arg1);
2434  arg.push_back(arg2);
2435  return printf(str, arg);
2436  }

References arg(), printf(), and casadi::str().

◆ printf() [3/4]

std::string casadi::CodeGenerator::printf ( const std::string &  str,
const std::string &  arg1,
const std::string &  arg2,
const std::string &  arg3 
)

Definition at line 2438 of file code_generator.cpp.

2439  {
2440  std::vector<std::string> arg;
2441  arg.push_back(arg1);
2442  arg.push_back(arg2);
2443  arg.push_back(arg3);
2444  return printf(str, arg);
2445  }

References arg(), printf(), and casadi::str().

◆ printf() [4/4]

std::string casadi::CodeGenerator::printf ( const std::string &  str,
const std::vector< std::string > &  arg = std::vector<std::string>() 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u5

Definition at line 2406 of file code_generator.cpp.

2406  {
2408  std::stringstream s;
2409  s << "CASADI_PRINTF(";
2410  // Loop over lines in str
2411  std::string::size_type pos = 0, prev = 0;
2412  while ((pos = str.find('\n', prev)) != std::string::npos) {
2413  // Any line containing a trailing new line
2414  s << "\"" << str.substr(prev, pos-prev) << "\\n\"\n";
2415  prev = pos + 1;
2416  }
2417  // Remainder without trailing new line
2418  s << "\"" << str.substr(prev) << "\"";
2419  for (casadi_int i=0; i<arg.size(); ++i) s << ", " << arg[i];
2420  s << ");";
2421  return s.str();
2422  }

References add_auxiliary(), arg(), AUX_PRINTF, and casadi::str().

Referenced by casadi::Feasiblesqpmethod::codegen_body(), casadi::Qrqp::codegen_body(), casadi::FunctionInternal::codegen_meta(), casadi::Dump::generate(), casadi::Monitor::generate(), generate_dump(), generate_print(), casadi::MXFunction::print_arg(), casadi::SXFunction::print_arg(), casadi::MXFunction::print_res(), casadi::SXFunction::print_res(), and printf().

◆ project()

std::string casadi::CodeGenerator::project ( const std::string &  arg,
const Sparsity sp_arg,
const std::string &  res,
const Sparsity sp_res,
const std::string &  w 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tz

Definition at line 2359 of file code_generator.cpp.

2361  {
2362  // If sparsity match, simple copy
2363  if (sp_arg==sp_res) return copy(arg, sp_arg.nnz(), res);
2364 
2365  // Create call
2367  std::stringstream s;
2368  s << "casadi_project(" << arg << ", " << sparsity(sp_arg) << ", " << res << ", "
2369  << sparsity(sp_res) << ", " << w << ");";
2370  return s.str();
2371  }

References add_auxiliary(), arg(), AUX_PROJECT, copy(), casadi::Sparsity::nnz(), res(), and sparsity().

Referenced by casadi::Switch::codegen_body(), and casadi::Project::generate().

◆ qr()

std::string casadi::CodeGenerator::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 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sz

Definition at line 2812 of file code_generator.cpp.

2816  {
2818  return "casadi_qr(" + sp + ", " + A + ", " + w + ", "
2819  + sp_v + ", " + v + ", " + sp_r + ", " + r + ", "
2820  + beta + ", " + prinv + ", " + pc + ");";
2821  }

References add_auxiliary(), and AUX_QR.

Referenced by casadi::LinsolQr::generate_factorize().

◆ qr_solve()

std::string casadi::CodeGenerator::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 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t0

Definition at line 2830 of file code_generator.cpp.

2835  {
2837  return "casadi_qr_solve(" + x + ", " + str(nrhs) + ", " + (tr ? "1" : "0") + ", "
2838  + sp_v + ", " + v + ", " + sp_r + ", " + r + ", "
2839  + beta + ", " + prinv + ", " + pc + ", " + w + ");";
2840  }

References add_auxiliary(), AUX_QR, and casadi::str().

Referenced by casadi::LinsolQr::generate().

◆ rank1()

std::string casadi::CodeGenerator::rank1 ( const std::string &  A,
const Sparsity sp_A,
const std::string &  alpha,
const std::string &  x,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sv

Definition at line 2302 of file code_generator.cpp.

2304  {
2306  std::stringstream s;
2307  s << "casadi_rank1(" << A << ", " << sparsity(sp_A) << ", "
2308  << alpha << ", " << x << ", " << y << ");";
2309  return s.str();
2310  }

References add_auxiliary(), AUX_RANK1, and sparsity().

Referenced by casadi::Rank1::generate().

◆ regularize()

std::string casadi::CodeGenerator::regularize ( const Sparsity sp_h,
const std::string &  h,
const std::string &  reg 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tj

Definition at line 2964 of file code_generator.cpp.

2965  {
2967  return "casadi_regularize(" + sparsity(sp_h) + ", " + h + ", " + reg + ");";
2968  }

References add_auxiliary(), AUX_REGULARIZE, and sparsity().

◆ res()

std::string casadi::CodeGenerator::res ( casadi_int  i) const

◆ reserve_work()

void casadi::CodeGenerator::reserve_work ( casadi_int  n)

Extra doc: https://github.com/casadi/casadi/wiki/L_2ay

Definition at line 1107 of file code_generator.cpp.

1107  {
1108  if (n == 0) {
1109  padding_length_ = 1;
1110  } else {
1111  // Zero based counting. The 10th entry is to be rendered as '9'
1112  padding_length_ = str(n-1).length();
1113  }
1114  }

References padding_length_, and casadi::str().

Referenced by casadi::MXFunction::codegen_body(), and casadi::SXFunction::codegen_body().

◆ rom_double()

std::string casadi::CodeGenerator::rom_double ( const void *  id) const

Extra doc: https://github.com/casadi/casadi/wiki/L_s3

Definition at line 870 of file code_generator.cpp.

870  {
871  auto it = file_scope_double_.find(id);
872  casadi_assert(it!=file_scope_double_.end(), "Not defined.");
873  casadi_int size = std::distance(file_scope_double_.begin(), it);
874  return "casadi_rd" + str(size);
875  }

References file_scope_double_, and casadi::str().

Referenced by casadi::ConstantFile::codegen_incref(), and casadi::ConstantFile::generate().

◆ rom_integer()

std::string casadi::CodeGenerator::rom_integer ( const void *  id) const

Extra doc: https://github.com/casadi/casadi/wiki/L_s5

Definition at line 884 of file code_generator.cpp.

884  {
885  auto it = file_scope_double_.find(id);
886  casadi_assert(it!=file_scope_double_.end(), "Not defined.");
887  casadi_int size = std::distance(file_scope_double_.begin(), it);
888  return "casadi_ri" + str(size);
889  }

References file_scope_double_, and casadi::str().

◆ sanitize_source()

std::string casadi::CodeGenerator::sanitize_source ( const std::string &  src,
const std::vector< std::string > &  inst,
bool  add_shorthand = true 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sl

Definition at line 2657 of file code_generator.cpp.

2659  {
2660  // Create suffix if templates type are not all "casadi_real"
2661  std::string suffix;
2662  for (const std::string& s : inst) {
2663  if (s!="casadi_real") {
2664  for (const std::string& s : inst) suffix += "_" + s;
2665  break;
2666  }
2667  }
2668 
2669  // Construct map of name replacements
2670  std::vector<std::pair<std::string, std::string> > rep;
2671  for (casadi_int i=0; i<inst.size(); ++i) {
2672  rep.push_back(std::make_pair("T" + str(i+1), inst[i]));
2673  }
2674 
2675  // Return object
2676  std::stringstream ret;
2677  // Process C++ source
2678  std::string line;
2679  std::istringstream stream(src);
2680 
2681  bool filter_macros = true; // Macro definitions are ignored
2682 
2683  // Set by `// SYMBOL "X"` to "casadi_X"; cleared when the matching
2684  // function signature is found and the prefix is prepended.
2685  std::string active_symbol;
2686 
2687  while (std::getline(stream, line)) {
2688  size_t n1, n2;
2689 
2690  // C++ template declarations are ignored
2691  if (line.find("template")==0) continue;
2692 
2693  // Macro definitions are ignored
2694  if (filter_macros && line.find("#define")==0) continue;
2695  if (filter_macros && line.find("#undef")==0) continue;
2696 
2697  // casadi_assert and error are ignored
2698  if (line.find("casadi_assert") != std::string::npos) continue;
2699  if (line.find("casadi_error") != std::string::npos) continue;
2700  if (line.find("casadi_message") != std::string::npos) continue;
2701 
2702  // Inline declaration
2703  if (line == "inline") continue;
2704 
2705  // If line starts with "// SYMBOL", add shorthand and track the symbol
2706  if (line.find("// SYMBOL") != std::string::npos) {
2707  n1 = line.find("\"");
2708  n2 = line.find("\"", n1+1);
2709  std::string sym = line.substr(n1+1, n2-n1-1);
2710  if (add_shorthand) shorthand(sym + suffix);
2711  if (!suffix.empty()) {
2712  rep.push_back(std::make_pair(sym, sym + suffix));
2713  }
2714  active_symbol = "casadi_" + sym;
2715  continue;
2716  }
2717 
2718  // If line starts with "// C-REPLACE", add to list of replacements
2719  if (line.find("// C-REPLACE") != std::string::npos) {
2720  // Get C++ string, then C string; \" and \\ are unescaped
2721  size_t pos = line.find("// C-REPLACE");
2722  std::string key = next_quoted_token(line, pos);
2723  std::string sub = next_quoted_token(line, pos);
2724  // Add to replacements
2725  rep.push_back(std::make_pair(key, sub));
2726  continue;
2727  }
2728 
2729  // If line starts with "// C-VERBOSE", skip the next line
2730  if (!verbose_runtime && line.find("// C-VERBOSE") != std::string::npos) {
2731  // Ignore next line
2732  std::getline(stream, line);
2733  continue;
2734  }
2735 
2736  if (line.find("// FILTER-MACROS ON") != std::string::npos) {
2737  filter_macros = true;
2738  }
2739 
2740  if (line.find("// FILTER-MACROS OFF") != std::string::npos) {
2741  filter_macros = false;
2742  }
2743 
2744  // Ignore other C++ style comment
2745  n1 = line.find("//");
2746  if (n1 != std::string::npos) line.erase(n1);
2747 
2748  // Remove trailing spaces
2749  n1 = line.find_last_not_of(' ');
2750  if (n1 != std::string::npos) {
2751  line.erase(n1 + 1);
2752  } else {
2753  continue;
2754  }
2755 
2756  // Prepend storage-class/inline-hint to the symbol's signature line
2757  if (!active_symbol.empty() &&
2758  line.find(active_symbol + "(") != std::string::npos) {
2759  std::string sig_prefix;
2760  if (this->static_aux) sig_prefix += "static ";
2761  if (this->inline_aux) sig_prefix += "inline ";
2762  if (!sig_prefix.empty()) line = sig_prefix + line;
2763  active_symbol.clear();
2764  }
2765 
2766  // Perform string replacements
2767  for (auto&& it = rep.rbegin(); it!=rep.rend(); ++it) {
2768  line = replace(line, it->first, it->second);
2769  }
2770 
2771  // Append to return
2772  ret << line << "\n";
2773  }
2774 
2775  // Trailing newline
2776  ret << "\n";
2777  return ret.str();
2778  }
static std::string next_quoted_token(const std::string &line, size_t &pos)

References inline_aux, casadi::next_quoted_token(), casadi::replace(), shorthand(), static_aux, casadi::str(), suffix, and verbose_runtime.

Referenced by add_auxiliary(), casadi::blasfeo_codegen_axpy_aux(), casadi::blasfeo_codegen_dot_aux(), casadi::classic_codegen_asum_aux(), casadi::classic_codegen_axpy_aux(), casadi::classic_codegen_dot_aux(), casadi::classic_codegen_nrm2_aux(), casadi::classic_codegen_scal_aux(), casadi::MadmpecInterface::codegen_body(), casadi::ClarabelInterface::codegen_body(), casadi::DaqpInterface::codegen_body(), casadi::FatropInterface::codegen_body(), casadi::HighsInterface::codegen_body(), casadi::HpipmInterface::codegen_body(), casadi::IpoptInterface::codegen_body(), casadi::MadnlpInterface::codegen_body(), casadi::MosekInterface::codegen_body(), casadi::XpressInterface::codegen_body(), casadi::Blas::codegen_copy_aux(), and casadi::UnoInterface::codegen_declarations().

◆ scal()

std::string casadi::CodeGenerator::scal ( casadi_int  n,
const std::string &  alpha,
const std::string &  x 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sq

Definition at line 2483 of file code_generator.cpp.

2483  {
2485  return "casadi_scal(" + str(n) + ", " + alpha + ", " + x + ");";
2486  }

References add_auxiliary(), AUX_SCAL, and casadi::str().

Referenced by casadi::Sqpmethod::codegen_body(), and casadi::Nlpsol::codegen_post_solve().

◆ scope_add_cleanup()

void casadi::CodeGenerator::scope_add_cleanup ( const std::string &  code)

Extra doc: https://github.com/casadi/casadi/wiki/L_2f6

Definition at line 256 of file code_generator.cpp.

256  {
257  local_cleanup_.push_back(code);
258  }
std::vector< std::string > local_cleanup_

References local_cleanup_.

Referenced by casadi::FunctionInternal::codegen_checkout(), and casadi::FunctionInternal::codegen_release().

◆ scope_enter()

void casadi::CodeGenerator::scope_enter ( )

◆ scope_exit()

void casadi::CodeGenerator::scope_exit ( )

Extra doc: https://github.com/casadi/casadi/wiki/L_sd

Definition at line 260 of file code_generator.cpp.

260  {
261  // Order local variables
262  std::map<std::string, std::set<std::pair<std::string, std::string>>> local_variables_by_type;
263  for (auto&& e : local_variables_) {
264  local_variables_by_type[e.second.first].insert(std::make_pair(e.first, e.second.second));
265  }
266 
267  // Codegen local variables
268  for (auto&& e : local_variables_by_type) {
269  casadi_int cnt = 0;
270  for (auto it=e.second.begin(); it!=e.second.end(); ++it) {
271  bool split_declaration = it==e.second.begin() ||
273  if (split_declaration) {
274  if (it!=e.second.begin()) body << ";\n";
275  body << " " << e.first << " ";
276  } else {
277  body << ", ";
278  }
279  body << it->second << it->first;
280  // Insert definition, if any
281  auto k=local_default_.find(it->first);
282  if (k!=local_default_.end()) body << "=" << k->second;
283  cnt++;
284  }
285  body << ";\n";
286  }
287 
288  // Loop over local_cleanup_ in reverse order
289  if (!local_cleanup_.empty()) {
290  *this << "done" << local_cleanup_.size() << ":\n";
291  for (casadi_int i=local_cleanup_.size()-1; i>=0; --i) {
292  *this << local_cleanup_[i];
293  }
294  if (local_void_) {
295  *this << "return;\n";
296  } else {
297  *this << "return ret;\n";
298  }
299  }
300  }

References body, local_cleanup_, local_default_, local_variables_, local_void_, and max_declarations_per_line.

Referenced by add_dependency(), casadi::FunctionInternal::codegen(), casadi::ThreadMap::codegen_declarations(), casadi::FatropInterface::codegen_declarations(), and casadi::IpoptInterface::codegen_declarations().

◆ scope_return() [1/2]

void casadi::CodeGenerator::scope_return ( )

Takes care of cleanup code

Extra doc: https://github.com/casadi/casadi/wiki/L_2f5

Definition at line 247 of file code_generator.cpp.

247  {
248  if (local_cleanup_.empty()) {
249  *this << "return;\n";
250  return;
251  }
252  *this << "goto done" << local_cleanup_.size() << ";\n";
253  }

References local_cleanup_.

◆ scope_return() [2/2]

void casadi::CodeGenerator::scope_return ( const std::string &  value)

Takes care of cleanup code

Extra doc: https://github.com/casadi/casadi/wiki/L_2f4

Definition at line 236 of file code_generator.cpp.

236  {
237  local_void_ = false;
238  if (local_cleanup_.empty()) {
239  *this << "return " << value << ";\n";
240  return;
241  }
242  local("ret", "int");
243  *this << "ret = " << value << ";\n";
244  *this << "goto done" << local_cleanup_.size() << ";\n";
245  }

References local(), local_cleanup_, and local_void_.

Referenced by casadi::FunctionInternal::codegen_checkout(), and casadi::FunctionInternal::codegen_release().

◆ setup_callback()

void casadi::CodeGenerator::setup_callback ( const std::string &  s,
const Function f 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_27s

Definition at line 1255 of file code_generator.cpp.

1255  {
1256  std::string name = add_dependency(f);
1257  bool needs_mem = f->codegen_needs_mem();
1258  if (needs_mem) {
1259  *this << s << ".checkout = " << name << "_checkout;\n";
1260  } else {
1261  *this << s << ".checkout = 0;\n";
1262  }
1263 
1264  *this << s << ".eval = " << name << ";\n";
1265  if (needs_mem) {
1266  *this << s << ".release = " << name << "_release;\n";
1267  } else {
1268  *this << s << ".release = 0;\n";
1269  }
1270  }

References add_dependency(), casadi::FunctionInternal::codegen_needs_mem(), and name.

Referenced by casadi::MadmpecInterface::set_ccopt_prob(), casadi::FatropInterface::set_fatrop_prob(), and casadi::MadnlpInterface::set_madnlp_prob().

◆ shorthand() [1/2]

std::string casadi::CodeGenerator::shorthand ( const std::string &  name) const

◆ shorthand() [2/2]

std::string casadi::CodeGenerator::shorthand ( const std::string &  name,
bool  allow_adding = true 
)

Definition at line 1319 of file code_generator.cpp.

1319  {
1320  bool added = added_shorthands_.insert(name).second;
1321  if (!allow_adding) {
1322  casadi_assert(added, "Duplicate macro: " + name);
1323  }
1324  return "casadi_" + name;
1325  }

References added_shorthands_, and name.

◆ sparsify()

std::string casadi::CodeGenerator::sparsify ( const std::string &  arg,
const std::string &  res,
const Sparsity sp_res,
bool  tr = false 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u2

Definition at line 2396 of file code_generator.cpp.

2397  {
2398  // Create call
2400  std::stringstream s;
2401  s << "casadi_sparsify(" << arg << ", " << res << ", "
2402  << sparsity(sp_res) << ", " << (tr ? 1 : 0) << ");";
2403  return s.str();
2404  }

References add_auxiliary(), arg(), AUX_SPARSIFY, res(), and sparsity().

Referenced by casadi::Sparsify::generate().

◆ sparsity()

std::string casadi::CodeGenerator::sparsity ( const Sparsity sp,
bool  canonical = true 
)

◆ stream_close()

void casadi::CodeGenerator::stream_close ( std::ostream &  f,
bool  cpp 
)
static

Definition at line 476 of file code_generator.cpp.

476  {
477  // C linkage
478  if (!cpp) {
479  f << "#ifdef __cplusplus\n"
480  << "} /* extern \"C\" */\n"
481  << "#endif\n";
482  }
483  }

References cpp.

Referenced by generate(), and casadi::DaeBuilderInternal::generate_wrapper().

◆ stream_open()

void casadi::CodeGenerator::stream_open ( std::ostream &  f,
bool  cpp 
)
static

Definition at line 457 of file code_generator.cpp.

457  {
458 
459  // Print header
460  f << "/* This file was automatically generated by CasADi " << casadi_version() << ".\n"
461  << " * It consists of: \n"
462  << " * 1) content generated by CasADi runtime: not copyrighted\n"
463  << " * 2) template code copied from CasADi source: permissively licensed (MIT-0)\n"
464  << " * 3) user code: owned by the user\n"
465  << " *\n"
466  << " */\n";
467 
468  // C linkage
469  if (!cpp) {
470  f << "#ifdef __cplusplus\n"
471  << "extern \"C\" {\n"
472  << "#endif\n\n";
473  }
474  }
static std::string casadi_version()
Current CasADi version as string.

References casadi_version(), and cpp.

Referenced by generate(), and casadi::DaeBuilderInternal::generate_wrapper().

◆ sum_viol()

std::string casadi::CodeGenerator::sum_viol ( casadi_int  n,
const std::string &  x,
const std::string &  lb,
const std::string &  ub 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tg

Definition at line 2934 of file code_generator.cpp.

2935  {
2937  return "casadi_sum_viol(" + str(n) + ", " + x+ ", " + lb + ", " + ub + ")";
2938  }

References add_auxiliary(), AUX_SUM_VIOL, and casadi::str().

Referenced by casadi::Sqpmethod::codegen_body().

◆ sx_work()

std::string casadi::CodeGenerator::sx_work ( casadi_int  i)

Extra doc: https://github.com/casadi/casadi/wiki/L_se

Definition at line 2614 of file code_generator.cpp.

2614  {
2615  if (avoid_stack_) {
2616  return "w[" + str(i) + "]";
2617  } else {
2618  std::string name = "a"+format_padded(i);
2619 
2620  // Make sure work vector element has been declared
2621  local(name, "casadi_real");
2622 
2623  return name;
2624  }
2625  }
std::string format_padded(casadi_int i) const

References avoid_stack_, format_padded(), local(), name, and casadi::str().

Referenced by casadi::SXFunction::codegen_body(), casadi::SXFunction::print_arg(), and casadi::SXFunction::print_res().

◆ sz_work()

void casadi::CodeGenerator::sz_work ( size_t &  sz_arg,
size_t &  sz_res,
size_t &  sz_iw,
size_t &  sz_w 
) const

Extra doc: https://github.com/casadi/casadi/wiki/L_258

Definition at line 3099 of file code_generator.cpp.

3099  {
3100  sz_arg = sz_res = sz_iw = sz_w = 0;
3101  for (auto&& f : added_functions_) {
3102  sz_arg = std::max(sz_arg, f.f.sz_arg());
3103  sz_res = std::max(sz_res, f.f.sz_res());
3104  sz_iw = std::max(sz_iw, f.f.sz_iw());
3105  sz_w = std::max(sz_w, f.f.sz_w());
3106  }
3107  }

References added_functions_.

Referenced by casadi::DaeBuilderInternal::generate_wrapper().

◆ thread_safe()

bool casadi::CodeGenerator::thread_safe ( ) const
inline

◆ to_file()

std::string casadi::CodeGenerator::to_file ( const std::string &  f,
const Sparsity sp,
const std::string &  x 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_2f8

Definition at line 2997 of file code_generator.cpp.

2998  {
3000  return "casadi_to_file(" + f + ", " + sparsity(sp) + ", " + x + ")";
3001  }

References add_auxiliary(), AUX_TO_FILE, and sparsity().

Referenced by casadi::Dump::generate(), and generate_dump().

◆ to_mex()

std::string casadi::CodeGenerator::to_mex ( const Sparsity sp,
const std::string &  arg 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u3

Definition at line 2146 of file code_generator.cpp.

2146  {
2148  std::stringstream s;
2149  s << "casadi_to_mex(" << sparsity(sp) << ", " << arg << ");";
2150  return s.str();
2151  }

References add_auxiliary(), arg(), AUX_TO_MEX, and sparsity().

Referenced by casadi::FunctionInternal::codegen_meta().

◆ trans()

std::string casadi::CodeGenerator::trans ( const std::string &  x,
const Sparsity sp_x,
const std::string &  y,
const Sparsity sp_y,
const std::string &  iw 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sy

Definition at line 2337 of file code_generator.cpp.

2339  {
2341  return "casadi_trans(" + x + "," + sparsity(sp_x) + ", "
2342  + y + ", " + sparsity(sp_y) + ", " + iw + ")";
2343  }

References add_auxiliary(), AUX_TRANS, and sparsity().

Referenced by casadi::Transpose::generate().

◆ tri_project()

std::string casadi::CodeGenerator::tri_project ( const std::string &  arg,
const Sparsity sp_arg,
const std::string &  res,
bool  lower 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u0

Definition at line 2374 of file code_generator.cpp.

2375  {
2376  // Create call
2378  std::stringstream s;
2379  s << "casadi_tri_project(" << arg << ", " << sparsity(sp_arg) << ", ";
2380  s << res << ", " << (lower ? 1: 0) << ");";
2381  return s.str();
2382  }

References add_auxiliary(), arg(), AUX_TRI_PROJECT, res(), and sparsity().

Referenced by casadi::OsqpInterface::codegen_body().

◆ trilsolve()

std::string casadi::CodeGenerator::trilsolve ( const Sparsity sp_x,
const std::string &  x,
const std::string &  y,
bool  tr,
bool  unity,
casadi_int  nrhs 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_ss

Definition at line 2526 of file code_generator.cpp.

2527  {
2529  return "casadi_trilsolve(" + sparsity(sp_x) + ", " + x + ", " + y + ", " + str(tr) + ", "
2530  + str(unity) + ", " + str(nrhs) + ");";
2531  }

References add_auxiliary(), AUX_TRILSOLVE, sparsity(), and casadi::str().

Referenced by casadi::TrilSolve< Tr >::generate(), and casadi::TrilSolveUnity< Tr >::generate().

◆ triusolve()

std::string casadi::CodeGenerator::triusolve ( const Sparsity sp_x,
const std::string &  x,
const std::string &  y,
bool  tr,
bool  unity,
casadi_int  nrhs 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_st

Definition at line 2533 of file code_generator.cpp.

2534  {
2536  return "casadi_triusolve(" + sparsity(sp_x) + ", " + x + ", " + y + ", " + str(tr) + ", "
2537  + str(unity) + ", " + str(nrhs) + ");";
2538  }

References add_auxiliary(), AUX_TRIUSOLVE, sparsity(), and casadi::str().

Referenced by casadi::TriuSolve< Tr >::generate(), and casadi::TriuSolveUnity< Tr >::generate().

◆ unindent()

void casadi::CodeGenerator::unindent ( )
inline

Extra doc: https://github.com/casadi/casadi/wiki/L_sh

Definition at line 296 of file code_generator.hpp.

296 {current_indent_--;}

Referenced by print_formatted().

◆ vector_fmax()

std::string casadi::CodeGenerator::vector_fmax ( casadi_int  n,
const std::string &  x,
const std::string &  y,
const std::string &  z 
)

Definition at line 2465 of file code_generator.cpp.

2466  {
2468  return "casadi_vector_fmax(" + str(n) + ", " + x + ", " + y + ", " + z + ");";
2469  }

References add_auxiliary(), AUX_VECTOR_FMAX, and casadi::str().

Referenced by casadi::Feasiblesqpmethod::codegen_feasibility_iterations().

◆ vector_fmin()

std::string casadi::CodeGenerator::vector_fmin ( casadi_int  n,
const std::string &  x,
const std::string &  y,
const std::string &  z 
)

Definition at line 2471 of file code_generator.cpp.

2472  {
2474  return "casadi_vector_fmin(" + str(n) + ", " + x + ", " + y + ", " + z + ");";
2475  }

References add_auxiliary(), AUX_VECTOR_FMIN, and casadi::str().

Referenced by casadi::Feasiblesqpmethod::codegen_feasibility_iterations().

◆ vfmax() [1/2]

std::string casadi::CodeGenerator::vfmax ( const std::string &  x,
casadi_int  n,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t8

Definition at line 2880 of file code_generator.cpp.

2881  {
2883  return "casadi_vfmax(" + x + ", " + str(n) + ", " + y + ");";
2884  }

References add_auxiliary(), AUX_VFMAX, and casadi::str().

◆ vfmax() [2/2]

std::string casadi::CodeGenerator::vfmax ( const std::string &  x,
const std::string &  n,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_ta

Definition at line 2892 of file code_generator.cpp.

2893  {
2895  return "casadi_vfmax(" + x + ", " + n + ", " + y + ");";
2896  }

References add_auxiliary(), and AUX_VFMAX.

◆ vfmin() [1/2]

std::string casadi::CodeGenerator::vfmin ( const std::string &  x,
casadi_int  n,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t9

Definition at line 2886 of file code_generator.cpp.

2887  {
2889  return "casadi_vfmin(" + x + ", " + str(n) + ", " + y + ");";
2890  }

References add_auxiliary(), AUX_VFMIN, and casadi::str().

◆ vfmin() [2/2]

std::string casadi::CodeGenerator::vfmin ( const std::string &  x,
const std::string &  n,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tb

Definition at line 2898 of file code_generator.cpp.

2899  {
2901  return "casadi_vfmin(" + x + ", " + n + ", " + y + ");";
2902  }

References add_auxiliary(), and AUX_VFMIN.

◆ work()

std::string casadi::CodeGenerator::work ( casadi_int  n,
casadi_int  sz,
bool  is_ref 
) const

Get work vector name from index

Definition at line 1086 of file code_generator.cpp.

1086  {
1087  if (is_ref) {
1088  return "wr" + format_padded(n);
1089  }
1090  if (n<0 || sz==0) {
1091  return "0";
1092  } else if (sz==1 && !this->codegen_scalars) {
1093  return "(&w" + format_padded(n) + ")";
1094  } else {
1095  return "w" + format_padded(n);
1096  }
1097  }

References codegen_scalars, and format_padded().

Referenced by casadi::codegen_copy_z(), casadi::BSplineParametric::generate(), casadi::Bilin::generate(), casadi::BinaryMX< ScX, ScY >::generate(), casadi::BSplineCommon::generate(), casadi::Call::generate(), casadi::Find::generate(), casadi::Low::generate(), casadi::Concat::generate(), casadi::ConstantDM::generate(), casadi::ConstantFile::generate(), casadi::ConstantPool::generate(), casadi::Constant< Value >::generate(), casadi::Convexify::generate(), casadi::Determinant::generate(), casadi::Dot::generate(), casadi::Dump::generate(), casadi::Einstein::generate(), casadi::GetNonzerosVector::generate(), casadi::GetNonzerosSlice::generate(), casadi::GetNonzerosSlice2::generate(), casadi::GetNonzerosParamVector::generate(), casadi::GetNonzerosSliceParam::generate(), casadi::GetNonzerosParamSlice::generate(), casadi::GetNonzerosParamParam::generate(), casadi::Input::generate(), casadi::Output::generate(), casadi::Kron::generate(), casadi::DenseKron::generate(), casadi::DenseSparseKron::generate(), casadi::SparseDenseKron::generate(), casadi::KronContract::generate(), casadi::DenseKronContract::generate(), casadi::DenseSparseKronContract::generate(), casadi::SparseDenseKronContract::generate(), casadi::LogSumExp::generate(), casadi::MMin::generate(), casadi::MMax::generate(), casadi::Monitor::generate(), casadi::Multiplication::generate(), casadi::DenseMultiplication::generate(), casadi::DenseSparseMultiplication::generate(), casadi::PseudoDenseMultiplication::generate(), casadi::NormF::generate(), casadi::Norm1::generate(), casadi::NormInf::generate(), casadi::Project::generate(), casadi::Densify::generate(), casadi::Sparsify::generate(), casadi::Rank1::generate(), casadi::HorzRepmat::generate(), casadi::HorzRepsum::generate(), casadi::SetNonzerosVector< Add >::generate(), casadi::SetNonzerosSlice< Add >::generate(), casadi::SetNonzerosSlice2< Add >::generate(), casadi::SetNonzerosParam< Add >::generate(), casadi::SetNonzerosParamVector< Add >::generate(), casadi::SetNonzerosParamSlice< Add >::generate(), casadi::SetNonzerosSliceParam< Add >::generate(), casadi::SetNonzerosParamParam< Add >::generate(), casadi::LinsolCall< Tr >::generate(), casadi::TriuSolve< Tr >::generate(), casadi::TrilSolve< Tr >::generate(), casadi::TriuSolveUnity< Tr >::generate(), casadi::TrilSolveUnity< Tr >::generate(), casadi::Split::generate(), casadi::Transpose::generate(), casadi::DenseTranspose::generate(), casadi::UnaryMX::generate(), casadi::MXNode::generate_copy(), casadi::MXFunction::print_arg(), and casadi::MXFunction::print_res().

◆ workel()

std::string casadi::CodeGenerator::workel ( casadi_int  n) const

◆ wrapper()

std::string casadi::CodeGenerator::wrapper ( const Function base,
const std::string &  name 
)

Definition at line 302 of file code_generator.cpp.

302  {
303  FunctionInternal* f = base.get();
304  std::map<FunctionInternal*, casadi_int>& funs = added_wrappers_[name];
305  auto it = funs.find(f);
306  if (it==funs.end()) {
307  casadi_int n = funs.size();
308  funs[f] = n;
309  return name + str(n);
310  } else {
311  return name + str(it->second);
312  }
313  }
std::map< std::string, std::map< FunctionInternal *, casadi_int > > added_wrappers_

References added_wrappers_, casadi::Function::get(), name, and casadi::str().

Referenced by casadi::ThreadMap::codegen_body(), casadi::ThreadMap::codegen_declarations(), casadi::Nlpsol::codegen_declarations(), casadi::IpoptInterface::codegen_declarations(), casadi::Nlpsol::codegen_setup_constants(), and casadi::IpoptInterface::set_ipopt_prob().

◆ zeros()

std::string casadi::CodeGenerator::zeros ( casadi_int  sz)

Definition at line 1474 of file code_generator.cpp.

1474  {
1475  sz_zeros_ = std::max(sz_zeros_, sz);
1476  return shorthand("zeros");
1477  }

References shorthand(), and sz_zeros_.

Referenced by casadi::Input::generate().

Member Data Documentation

◆ added_auxiliaries_

std::multimap<Auxiliary, std::vector<std::string> > casadi::CodeGenerator::added_auxiliaries_

Definition at line 1158 of file code_generator.hpp.

Referenced by add_auxiliary().

◆ added_char_constants_

std::multimap<size_t, size_t> casadi::CodeGenerator::added_char_constants_

Definition at line 1161 of file code_generator.hpp.

Referenced by get_constant().

◆ added_double_constants_

std::multimap<size_t, size_t> casadi::CodeGenerator::added_double_constants_

Definition at line 1159 of file code_generator.hpp.

Referenced by get_constant().

◆ added_externals_

std::set<std::string> casadi::CodeGenerator::added_externals_

Definition at line 1156 of file code_generator.hpp.

Referenced by add_external(), and dump().

◆ added_functions_

std::vector<FunctionMeta> casadi::CodeGenerator::added_functions_

◆ added_includes_

std::set<std::string> casadi::CodeGenerator::added_includes_

Definition at line 1155 of file code_generator.hpp.

Referenced by add_include().

◆ added_integer_constants_

std::multimap<size_t, size_t> casadi::CodeGenerator::added_integer_constants_

Definition at line 1160 of file code_generator.hpp.

Referenced by get_constant().

◆ added_sfunctions

std::vector<std::string> casadi::CodeGenerator::added_sfunctions

Definition at line 1060 of file code_generator.hpp.

Referenced by add(), and generate().

◆ added_shorthands_

std::set<std::string> casadi::CodeGenerator::added_shorthands_

Definition at line 1157 of file code_generator.hpp.

Referenced by dump(), and shorthand().

◆ added_string_constants_

std::multimap<size_t, size_t> casadi::CodeGenerator::added_string_constants_

Definition at line 1162 of file code_generator.hpp.

Referenced by get_constant().

◆ added_wrappers_

std::map<std::string, std::map<FunctionInternal*, casadi_int> > casadi::CodeGenerator::added_wrappers_

Definition at line 1183 of file code_generator.hpp.

Referenced by wrapper().

◆ auxiliaries

std::stringstream casadi::CodeGenerator::auxiliaries

◆ avoid_stack_

bool casadi::CodeGenerator::avoid_stack_

Definition at line 1087 of file code_generator.hpp.

Referenced by CodeGenerator(), and sx_work().

◆ body

std::stringstream casadi::CodeGenerator::body

◆ buffer

std::stringstream casadi::CodeGenerator::buffer

Definition at line 1133 of file code_generator.hpp.

Referenced by flush(), operator<<(), and print_formatted().

◆ casadi_int_type

std::string casadi::CodeGenerator::casadi_int_type

Definition at line 1047 of file code_generator.hpp.

Referenced by CodeGenerator().

◆ casadi_real_type

std::string casadi::CodeGenerator::casadi_real_type

Definition at line 1044 of file code_generator.hpp.

Referenced by CodeGenerator().

◆ char_constants_

std::vector<std::vector<char> > casadi::CodeGenerator::char_constants_

Definition at line 1188 of file code_generator.hpp.

Referenced by dump(), and get_constant().

◆ codegen_scalars

bool casadi::CodeGenerator::codegen_scalars

Use the work vector for storing work vector elements of length 1 (typically scalar) instead of using local variables

Extra doc: https://github.com/casadi/casadi/wiki/L_u9

Definition at line 1097 of file code_generator.hpp.

Referenced by casadi::MXFunction::codegen_body(), CodeGenerator(), casadi::BinaryMX< ScX, ScY >::generate(), work(), and workel().

◆ cpp

bool casadi::CodeGenerator::cpp

◆ current_indent_

casadi_int casadi::CodeGenerator::current_indent_

Definition at line 1140 of file code_generator.hpp.

Referenced by CodeGenerator(), dump(), and print_formatted().

◆ dll_export

std::string casadi::CodeGenerator::dll_export

Definition at line 1119 of file code_generator.hpp.

Referenced by CodeGenerator().

◆ dll_import

std::string casadi::CodeGenerator::dll_import

Definition at line 1119 of file code_generator.hpp.

Referenced by CodeGenerator(), and declare().

◆ double_constants_

std::vector<std::vector<double> > casadi::CodeGenerator::double_constants_

Definition at line 1186 of file code_generator.hpp.

Referenced by dump(), and get_constant().

◆ dump_dir_prefix

std::string casadi::CodeGenerator::dump_dir_prefix

◆ dump_dir_suffix

std::string casadi::CodeGenerator::dump_dir_suffix

◆ exposed_fname

std::vector<std::string> casadi::CodeGenerator::exposed_fname

Definition at line 1149 of file code_generator.hpp.

Referenced by add(), and generate().

◆ file_scope_double_

std::map<const void *, casadi_int> casadi::CodeGenerator::file_scope_double_

◆ file_scope_integer_

std::map<const void *, casadi_int> casadi::CodeGenerator::file_scope_integer_

Definition at line 1168 of file code_generator.hpp.

Referenced by dump().

◆ force_canonical

bool casadi::CodeGenerator::force_canonical

Definition at line 1109 of file code_generator.hpp.

Referenced by add_io_sparsities(), and CodeGenerator().

◆ header

std::stringstream casadi::CodeGenerator::header

◆ include_math

bool casadi::CodeGenerator::include_math

Definition at line 1084 of file code_generator.hpp.

Referenced by CodeGenerator().

◆ includes

std::stringstream casadi::CodeGenerator::includes

Definition at line 1129 of file code_generator.hpp.

Referenced by add_include(), and dump().

◆ indent_

casadi_int casadi::CodeGenerator::indent_

Definition at line 1139 of file code_generator.hpp.

Referenced by CodeGenerator(), and print_formatted().

◆ infinity

std::string casadi::CodeGenerator::infinity

Definition at line 1089 of file code_generator.hpp.

Referenced by add_auxiliary(), and CodeGenerator().

◆ inline_aux

bool casadi::CodeGenerator::inline_aux

Definition at line 1069 of file code_generator.hpp.

Referenced by CodeGenerator(), and sanitize_source().

◆ integer_constants_

std::vector<std::vector<casadi_int> > casadi::CodeGenerator::integer_constants_

Definition at line 1187 of file code_generator.hpp.

Referenced by dump(), and get_constant().

◆ l1_blas

bool casadi::CodeGenerator::l1_blas

Definition at line 1113 of file code_generator.hpp.

Referenced by add_auxiliary(), and CodeGenerator().

◆ local_cleanup_

std::vector<std::string> casadi::CodeGenerator::local_cleanup_

Definition at line 1164 of file code_generator.hpp.

Referenced by scope_add_cleanup(), scope_enter(), scope_exit(), and scope_return().

◆ local_default_

std::map<std::string, std::string> casadi::CodeGenerator::local_default_

Definition at line 1166 of file code_generator.hpp.

Referenced by init_local(), scope_enter(), and scope_exit().

◆ local_mutexes_

std::map<const FunctionInternal*, std::set<std::string> > casadi::CodeGenerator::local_mutexes_

Definition at line 1171 of file code_generator.hpp.

Referenced by define_local_mutex(), local_mutex(), and local_mutexes().

◆ local_variables_

std::map<std::string, std::pair<std::string, std::string> > casadi::CodeGenerator::local_variables_

Definition at line 1163 of file code_generator.hpp.

Referenced by local(), scope_enter(), and scope_exit().

◆ local_void_

bool casadi::CodeGenerator::local_void_

Definition at line 1165 of file code_generator.hpp.

Referenced by scope_enter(), scope_exit(), and scope_return().

◆ main

bool casadi::CodeGenerator::main

◆ max_declarations_per_line

casadi_int casadi::CodeGenerator::max_declarations_per_line

Definition at line 1103 of file code_generator.hpp.

Referenced by CodeGenerator(), and scope_exit().

◆ max_initializer_elements_per_line

casadi_int casadi::CodeGenerator::max_initializer_elements_per_line

Definition at line 1106 of file code_generator.hpp.

Referenced by CodeGenerator().

◆ mex

bool casadi::CodeGenerator::mex

◆ name

std::string casadi::CodeGenerator::name

◆ nan

std::string casadi::CodeGenerator::nan

Definition at line 1089 of file code_generator.hpp.

Referenced by add_auxiliary(), and CodeGenerator().

◆ needs_mem_

bool casadi::CodeGenerator::needs_mem_

Definition at line 1192 of file code_generator.hpp.

Referenced by add_dependency(), CodeGenerator(), and dump().

◆ newline_

bool casadi::CodeGenerator::newline_

Definition at line 1136 of file code_generator.hpp.

Referenced by CodeGenerator(), operator<<(), and print_formatted().

◆ padding_length_

casadi_int casadi::CodeGenerator::padding_length_

Definition at line 1146 of file code_generator.hpp.

Referenced by format_padded(), and reserve_work().

◆ pool_double_

std::map<std::string, casadi_int> casadi::CodeGenerator::pool_double_

Definition at line 1170 of file code_generator.hpp.

Referenced by define_pool_double(), dump(), generate(), and pool_double().

◆ pool_double_defaults_

std::vector< std::vector<double> > casadi::CodeGenerator::pool_double_defaults_

Definition at line 1169 of file code_generator.hpp.

Referenced by define_pool_double(), dump(), and generate().

◆ prefix

std::string casadi::CodeGenerator::prefix

Definition at line 1122 of file code_generator.hpp.

Referenced by CodeGenerator(), dump(), generate(), and generate_dump().

◆ real_min

std::string casadi::CodeGenerator::real_min

Definition at line 1089 of file code_generator.hpp.

Referenced by add_auxiliary(), and CodeGenerator().

◆ sparsity_meta

std::set<std::string> casadi::CodeGenerator::sparsity_meta

Definition at line 1152 of file code_generator.hpp.

Referenced by add_io_sparsities().

◆ static_aux

bool casadi::CodeGenerator::static_aux

Definition at line 1066 of file code_generator.hpp.

Referenced by CodeGenerator(), and sanitize_source().

◆ string_constants_

std::vector<std::vector<std::string> > casadi::CodeGenerator::string_constants_

Definition at line 1189 of file code_generator.hpp.

Referenced by dump(), and get_constant().

◆ suffix

std::string casadi::CodeGenerator::suffix

Definition at line 1041 of file code_generator.hpp.

Referenced by CodeGenerator(), generate(), and sanitize_source().

◆ sz_ones_

casadi_int casadi::CodeGenerator::sz_ones_

Definition at line 1144 of file code_generator.hpp.

Referenced by CodeGenerator(), dump(), and ones().

◆ sz_zeros_

casadi_int casadi::CodeGenerator::sz_zeros_

Definition at line 1143 of file code_generator.hpp.

Referenced by CodeGenerator(), dump(), and zeros().

◆ thread_safe_

bool casadi::CodeGenerator::thread_safe_

Definition at line 1116 of file code_generator.hpp.

Referenced by CodeGenerator().

◆ unroll_args

bool casadi::CodeGenerator::unroll_args

Definition at line 1063 of file code_generator.hpp.

Referenced by add(), and CodeGenerator().

◆ verbose

bool casadi::CodeGenerator::verbose

Definition at line 1072 of file code_generator.hpp.

Referenced by casadi::MXFunction::codegen_body(), CodeGenerator(), and comment().

◆ verbose_runtime

bool casadi::CodeGenerator::verbose_runtime

Definition at line 1075 of file code_generator.hpp.

Referenced by CodeGenerator(), and sanitize_source().

◆ with_export

bool casadi::CodeGenerator::with_export

Definition at line 1100 of file code_generator.hpp.

Referenced by CodeGenerator(), and dump().

◆ with_header

bool casadi::CodeGenerator::with_header

◆ with_import

bool casadi::CodeGenerator::with_import

Definition at line 1100 of file code_generator.hpp.

Referenced by CodeGenerator(), and generate().

◆ with_mem

bool casadi::CodeGenerator::with_mem

◆ with_sfunction

bool casadi::CodeGenerator::with_sfunction

Definition at line 1059 of file code_generator.hpp.

Referenced by add(), CodeGenerator(), and generate().


The documentation for this class was generated from the following files: