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, const std::string &name="")
 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...
 
CodeGenerator & operator<< (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 >
CodeGenerator & operator<< (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 set_codegen_sz_w (const FunctionInternal *f, size_t sz_w)
 Register the work vector size a function needs in generated code. More...
 
bool get_codegen_sz_w (const FunctionInternal *f, size_t &sz_w) const
 Retrieve a work vector size registered by set_codegen_sz_w. 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 > external_names_
 
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::map< const FunctionInternal *, size_t > codegen_sz_w_
 
std::vector< FunctionMeta > added_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  casadi_assert(!external_names_.count(f.name()),
403  "Cannot code generate '" + f.name() + "': name conflicts with an external dependency.");
404 
405  // Define function
406  *this << declare(f->signature(f.name())) << "{\n"
407  << "return " << codegen_name << "(arg, res, iw, w, mem);\n"
408  << "}\n\n";
409 
410  if (this->unroll_args) {
411  // Define function
412  *this << declare(f->signature_unrolled(f.name())) << "{\n";
413  for (casadi_int i=0; i<f.n_in(); ++i) {
414  *this << "arg[" << i << "] = " << f.name_in(i) << ";\n";
415  }
416  for (casadi_int i=0; i<f.n_out(); ++i) {
417  *this << "res[" << i << "] = " << f.name_out(i) << ";\n";
418  }
419  *this << "return " << codegen_name << "(arg, res, iw, w, mem);\n";
420  *this << "}\n\n";
421  // Flush buffers
422  flush(this->body);
423  }
424 
425  // Generate meta information
426  f->codegen_meta(*this);
427 
428  // Generate Jacobian sparsity information
429  if (with_jac_sparsity) {
430  // Generate/get Jacobian sparsity
431  std::vector<Sparsity> jac = f.jac_sparsity();
432  // Code generate the sparsity pattern
433  add_io_sparsities("jac_" + f.name(), f->sparsity_in_, jac);
434 
435  // Flush buffers
436  flush(this->body);
437  }
438 
439  // Generate function specific code for Simulink sfunction
440  if (this->with_sfunction) this->added_sfunctions.push_back( this->codegen_sfunction(f) );
441 
442  // Add to list of exposed symbols
443  this->exposed_fname.push_back(f.name());
444  }
std::string add_dependency(const Function &f)
Add a function dependency.
std::set< std::string > external_names_
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, external_names_, 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 1523 of file code_generator.cpp.

1523  {
1524  // Look for existing instantiations
1525  auto f_match = added_auxiliaries_.equal_range(f);
1526  // Look for duplicates
1527  for (auto it=f_match.first; it!=f_match.second; ++it) {
1528  if (it->second==inst) return;
1529  }
1530  added_auxiliaries_.insert(std::make_pair(f, inst));
1531 
1532  // Add the appropriate function
1533  switch (f) {
1534  case AUX_COPY:
1535  if (this->l1_blas) {
1536  Blas::codegen_copy_aux(*this, inst);
1537  } else {
1538  this->auxiliaries << sanitize_source(casadi_copy_str, inst);
1539  }
1540  break;
1541  case AUX_SCALED_COPY:
1542  this->auxiliaries << sanitize_source(casadi_scaled_copy_str, inst);
1543  break;
1544  case AUX_SWAP:
1545  this->auxiliaries << sanitize_source(casadi_swap_str, inst);
1546  break;
1547  case AUX_SCAL:
1548  if (!(this->l1_blas && Blas::codegen_scal_aux(*this, inst)))
1549  this->auxiliaries << sanitize_source(casadi_scal_str, inst);
1550  break;
1551  case AUX_AXPY:
1552  if (!(this->l1_blas && Blas::codegen_axpy_aux(*this, inst)))
1553  this->auxiliaries << sanitize_source(casadi_axpy_str, inst);
1554  break;
1555  case AUX_DOT:
1556  if (!(this->l1_blas && Blas::codegen_dot_aux(*this, inst)))
1557  this->auxiliaries << sanitize_source(casadi_dot_str, inst);
1558  break;
1559  case AUX_BILIN:
1560  this->auxiliaries << sanitize_source(casadi_bilin_str, inst);
1561  break;
1562  case AUX_KRON:
1563  this->auxiliaries << sanitize_source(casadi_kron_str, inst);
1564  break;
1565  case AUX_KRON_DENSE:
1566  this->auxiliaries << sanitize_source(casadi_kron_dense_str, inst);
1567  break;
1568  case AUX_KRON_DENSE_SPARSE:
1569  this->auxiliaries << sanitize_source(casadi_kron_dense_sparse_str, inst);
1570  break;
1571  case AUX_KRON_SPARSE_DENSE:
1572  this->auxiliaries << sanitize_source(casadi_kron_sparse_dense_str, inst);
1573  break;
1575  this->auxiliaries << sanitize_source(casadi_kron_contract_inner_str, inst);
1576  break;
1578  this->auxiliaries << sanitize_source(casadi_kron_contract_inner_dense_str, inst);
1579  break;
1581  this->auxiliaries << sanitize_source(casadi_kron_contract_inner_dense_sparse_str, inst);
1582  break;
1584  this->auxiliaries << sanitize_source(casadi_kron_contract_inner_sparse_dense_str, inst);
1585  break;
1587  this->auxiliaries << sanitize_source(casadi_kron_contract_outer_str, inst);
1588  break;
1590  this->auxiliaries << sanitize_source(casadi_kron_contract_outer_dense_str, inst);
1591  break;
1593  this->auxiliaries << sanitize_source(casadi_kron_contract_outer_dense_sparse_str, inst);
1594  break;
1596  this->auxiliaries << sanitize_source(casadi_kron_contract_outer_sparse_dense_str, inst);
1597  break;
1598  case AUX_RANK1:
1599  this->auxiliaries << sanitize_source(casadi_rank1_str, inst);
1600  break;
1601  case AUX_IAMAX:
1603  this->auxiliaries << sanitize_source(casadi_iamax_str, inst);
1604  break;
1605  case AUX_INTERPN:
1608  add_auxiliary(AUX_FLIP, {});
1610  add_auxiliary(AUX_CLEAR, {"casadi_int"});
1611  this->auxiliaries << sanitize_source(casadi_interpn_str, inst);
1612  break;
1613  case AUX_INTERPN_GRAD:
1615  this->auxiliaries << sanitize_source(casadi_interpn_grad_str, inst);
1616  break;
1617  case AUX_DE_BOOR:
1618  this->auxiliaries << sanitize_source(casadi_de_boor_str, inst);
1619  break;
1620  case AUX_TENSOR_TTV:
1621  this->auxiliaries << sanitize_source(casadi_tensor_ttv_str, inst);
1622  break;
1623  case AUX_ND_BOOR_EVAL:
1629  this->auxiliaries << sanitize_source(casadi_nd_boor_eval_str, inst);
1630  break;
1631  case AUX_FLIP:
1632  this->auxiliaries << sanitize_source(casadi_flip_str, inst);
1633  break;
1634  case AUX_LOW:
1635  this->auxiliaries << sanitize_source(casadi_low_str, inst);
1636  break;
1637  case AUX_INTERPN_WEIGHTS:
1639  this->auxiliaries << sanitize_source(casadi_interpn_weights_str, inst);
1640  break;
1642  this->auxiliaries << sanitize_source(casadi_interpn_interpolate_str, inst);
1643  break;
1644  case AUX_NORM_1:
1645  if (!(this->l1_blas && Blas::codegen_norm_1_aux(*this, inst))) {
1647  this->auxiliaries << sanitize_source(casadi_norm_1_str, inst);
1648  }
1649  break;
1650  case AUX_NORM_2:
1651  if (!(this->l1_blas && Blas::codegen_norm_2_aux(*this, inst))) {
1653  this->auxiliaries << sanitize_source(casadi_norm_2_str, inst);
1654  }
1655  break;
1656  case AUX_NORM_INF:
1659  this->auxiliaries << sanitize_source(casadi_norm_inf_str, inst);
1660  break;
1661  case AUX_VECTOR_FMAX:
1663  this->auxiliaries << sanitize_source(casadi_vector_fmax_str, inst);
1664  break;
1665  case AUX_VECTOR_FMIN:
1667  this->auxiliaries << sanitize_source(casadi_vector_fmin_str, inst);
1668  break;
1669  case AUX_MASKED_NORM_INF:
1672  this->auxiliaries << sanitize_source(casadi_masked_norm_inf_str, inst);
1673  break;
1674  case AUX_CLIP_MIN:
1675  // add_auxiliary(AUX_CLIP_MIN);
1676  this->auxiliaries << sanitize_source(casadi_clip_min_str, inst);
1677  break;
1678  case AUX_CLIP_MAX:
1679  // add_auxiliary(AUX_CLIP_MAX);
1680  this->auxiliaries << sanitize_source(casadi_clip_max_str, inst);
1681  break;
1682  case AUX_CLEAR:
1683  this->auxiliaries << sanitize_source(casadi_clear_str, inst);
1684  break;
1685  case AUX_FILL:
1686  this->auxiliaries << sanitize_source(casadi_fill_str, inst);
1687  break;
1688  case AUX_MV:
1689  this->auxiliaries << sanitize_source(casadi_mv_str, inst);
1690  break;
1691  case AUX_MV_DENSE:
1692  this->auxiliaries << sanitize_source(casadi_mv_dense_str, inst);
1693  break;
1694  case AUX_MTIMES:
1695  this->auxiliaries << sanitize_source(casadi_mtimes_str, inst);
1696  break;
1697  case AUX_MTIMES_DENSE:
1698  this->auxiliaries << sanitize_source(casadi_mtimes_dense_str, inst);
1699  break;
1701  this->auxiliaries << sanitize_source(casadi_mtimes_dense_sparse_str, inst);
1702  break;
1703  case AUX_TRILSOLVE:
1704  this->auxiliaries << sanitize_source(casadi_trilsolve_str, inst);
1705  break;
1706  case AUX_TRIUSOLVE:
1707  this->auxiliaries << sanitize_source(casadi_triusolve_str, inst);
1708  break;
1709  case AUX_PROJECT:
1710  this->auxiliaries << sanitize_source(casadi_project_str, inst);
1711  break;
1712  case AUX_TRI_PROJECT:
1713  this->auxiliaries << sanitize_source(casadi_tri_project_str, inst);
1714  break;
1715  case AUX_DENSIFY:
1718  {
1719  std::vector<std::string> inst2 = inst;
1720  if (inst.size()==1) inst2.push_back(inst[0]);
1721  this->auxiliaries << sanitize_source(casadi_densify_str, inst2);
1722  }
1723  break;
1724  case AUX_SPARSIFY:
1726  {
1727  std::vector<std::string> inst2 = inst;
1728  if (inst.size()==1) inst2.push_back(inst[0]);
1729  this->auxiliaries << sanitize_source(casadi_sparsify_str, inst2);
1730  }
1731  break;
1732  case AUX_TRANS:
1733  this->auxiliaries << sanitize_source(casadi_trans_str, inst);
1734  break;
1735  case AUX_TO_MEX:
1737  this->auxiliaries << "#ifdef MATLAB_MEX_FILE\n"
1738  << sanitize_source(casadi_to_mex_str, inst)
1739  << "#endif\n\n";
1740  break;
1741  case AUX_FROM_MEX:
1743  this->auxiliaries << "#ifdef MATLAB_MEX_FILE\n"
1744  << sanitize_source(casadi_from_mex_str, inst)
1745  << "#endif\n\n";
1746  break;
1747  case AUX_FINITE_DIFF:
1752  this->auxiliaries << sanitize_source(casadi_finite_diff_str, inst);
1753  break;
1754  case AUX_QR:
1759  this->auxiliaries << sanitize_source(casadi_qr_str, inst);
1760  break;
1761  case AUX_DET:
1762  this->auxiliaries << sanitize_source(casadi_det_str, inst);
1763  break;
1764  case AUX_LSQR:
1771  this->auxiliaries << sanitize_source(casadi_lsqr_str, inst);
1772  break;
1773  case AUX_QP:
1774  this->auxiliaries << sanitize_source(casadi_qp_str, inst);
1775  break;
1776  case AUX_SOCP:
1778  this->auxiliaries << sanitize_source(casadi_socp_str, inst);
1779  break;
1780  case AUX_QRQP:
1795  add_include("stdarg.h");
1796  add_include("stdio.h");
1797  add_include("math.h");
1798 
1799  this->auxiliaries << sanitize_source(casadi_qrqp_str, inst);
1800  break;
1801  case AUX_NLP:
1803  this->auxiliaries << sanitize_source(casadi_nlp_str, inst);
1804  break;
1805  case AUX_SQPMETHOD:
1810  this->auxiliaries << sanitize_source(casadi_sqpmethod_str, inst);
1811  break;
1812  case AUX_FEASIBLESQPMETHOD:
1815  this->auxiliaries << sanitize_source(casadi_feasiblesqpmethod_str, inst);
1816  break;
1817  case AUX_LDL:
1818  this->auxiliaries << sanitize_source(casadi_ldl_str, inst);
1819  break;
1820  case AUX_NEWTON:
1825  this->auxiliaries << sanitize_source(casadi_newton_str, inst);
1826  break;
1827  case AUX_MAX_VIOL:
1829  this->auxiliaries << sanitize_source(casadi_max_viol_str, inst);
1830  break;
1831  case AUX_SUM_VIOL:
1832  this->auxiliaries << sanitize_source(casadi_sum_viol_str, inst);
1833  break;
1834  case AUX_SUM:
1835  this->auxiliaries << sanitize_source(casadi_sum_str, inst);
1836  break;
1837  case AUX_VFMIN:
1839  this->auxiliaries << sanitize_source(casadi_vfmin_str, inst);
1840  break;
1841  case AUX_VFMAX:
1843  this->auxiliaries << sanitize_source(casadi_vfmax_str, inst);
1844  break;
1845  case AUX_REGULARIZE:
1848  this->auxiliaries << sanitize_source(casadi_regularize_str, inst);
1849  break;
1854  this->auxiliaries << sanitize_source(casadi_bound_consistency_str, inst);
1855  break;
1856  case AUX_FILE_SLURP:
1857  add_include("stdio.h");
1858  this->auxiliaries << sanitize_source(casadi_file_slurp_str, inst);
1859  break;
1860  case AUX_CACHE:
1861  this->auxiliaries << sanitize_source(casadi_cache_str, inst);
1862  break;
1863  case AUX_CVX:
1870  this->auxiliaries << sanitize_source(casadi_cvx_str, inst);
1871  break;
1872  case AUX_CONVEXIFY:
1877  this->auxiliaries << sanitize_source(casadi_convexify_str, inst);
1878  break;
1879  case AUX_LOGSUMEXP:
1882  this->auxiliaries << sanitize_source(casadi_logsumexp_str, inst);
1883  break;
1884  case AUX_SPARSITY:
1885  this->auxiliaries << sanitize_source(casadi_sparsity_str, inst);
1886  break;
1887  case AUX_BFGS:
1896  this->auxiliaries << sanitize_source(casadi_bfgs_str, inst);
1897  break;
1898  case AUX_ORACLE:
1899  this->auxiliaries << sanitize_source(casadi_oracle_str, inst);
1900  break;
1901  case AUX_ORACLE_CALLBACK:
1902  this->auxiliaries << sanitize_source(casadi_oracle_callback_str, inst);
1903  break;
1904  case AUX_OCP_BLOCK:
1905  this->auxiliaries << sanitize_source(casadi_ocp_block_str, inst);
1906  break;
1907  case AUX_TO_DOUBLE:
1908  this->auxiliaries << "#define casadi_to_double(x) "
1909  << "(" << (this->cpp ? "static_cast<double>(x)" : "(double) x") << ")\n\n";
1910  break;
1911  case AUX_TO_INT:
1912  this->auxiliaries << "#define casadi_to_int(x) "
1913  << "(" << (this->cpp ? "static_cast<casadi_int>(x)" : "(casadi_int) x")
1914  << ")\n\n";
1915  break;
1916  case AUX_CAST:
1917  this->auxiliaries << "#define CASADI_CAST(x,y) "
1918  << "(" << (this->cpp ? "static_cast<x>(y)" : "(x) y") << ")\n\n";
1919  break;
1920  case AUX_SQ:
1921  this->auxiliaries << sanitize_source(
1922  "// SYMBOL \"sq\"\n"
1923  "casadi_real casadi_sq(casadi_real x) { return x*x;}\n\n",
1924  inst);
1925  break;
1926  case AUX_SIGN:
1927  this->auxiliaries << sanitize_source(
1928  "// SYMBOL \"sign\"\n"
1929  "casadi_real casadi_sign(casadi_real x) "
1930  "{ return x<0 ? -1 : x>0 ? 1 : x;}\n\n",
1931  inst);
1932  break;
1933  case AUX_IF_ELSE:
1934  this->auxiliaries << sanitize_source(
1935  "// SYMBOL \"if_else\"\n"
1936  "casadi_real casadi_if_else"
1937  "(casadi_real c, casadi_real x, casadi_real y) "
1938  "{ return c!=0 ? x : y;}\n\n",
1939  inst);
1940  break;
1941  case AUX_PRINTF:
1942  this->auxiliaries << "#ifndef CASADI_PRINTF\n";
1943  if (this->mex) {
1944  this->auxiliaries << "#ifdef MATLAB_MEX_FILE\n"
1945  << " #define CASADI_PRINTF mexPrintf\n"
1946  << "#else\n"
1947  << " #define CASADI_PRINTF printf\n"
1948  << "#endif\n";
1949  } else {
1950  add_include("stdio.h");
1951  this->auxiliaries << "#define CASADI_PRINTF printf\n";
1952  this->auxiliaries << "#ifndef CASADI_SNPRINTF\n";
1953  this->auxiliaries << "#define CASADI_SNPRINTF snprintf\n";
1954  this->auxiliaries << "#endif\n\n";
1955  }
1956  this->auxiliaries << "#endif\n\n";
1957  break;
1958  case AUX_FMIN:
1959  this->auxiliaries << sanitize_source(
1960  "// SYMBOL \"fmin\"\n"
1961  "casadi_real casadi_fmin(casadi_real x, casadi_real y) {\n"
1962  "/* Pre-c99 compatibility */\n"
1963  "#if __STDC_VERSION__ < 199901L\n"
1964  " return x<y ? x : y;\n"
1965  "#else\n"
1966  " return fmin(x, y);\n"
1967  "#endif\n"
1968  "}\n\n",
1969  inst);
1970  break;
1971  case AUX_FMAX:
1972  this->auxiliaries << sanitize_source(
1973  "// SYMBOL \"fmax\"\n"
1974  "casadi_real casadi_fmax(casadi_real x, casadi_real y) {\n"
1975  "/* Pre-c99 compatibility */\n"
1976  "#if __STDC_VERSION__ < 199901L\n"
1977  " return x>y ? x : y;\n"
1978  "#else\n"
1979  " return fmax(x, y);\n"
1980  "#endif\n"
1981  "}\n\n",
1982  inst);
1983  break;
1984  case AUX_FABS:
1985  this->auxiliaries << sanitize_source(
1986  "// SYMBOL \"fabs\"\n"
1987  "casadi_real casadi_fabs(casadi_real x) {\n"
1988  "/* Pre-c99 compatibility */\n"
1989  "#if __STDC_VERSION__ < 199901L\n"
1990  " return x>0 ? x : -x;\n"
1991  "#else\n"
1992  " return fabs(x);\n"
1993  "#endif\n"
1994  "}\n\n",
1995  inst);
1996  break;
1997  case AUX_ISINF:
1998  this->auxiliaries << sanitize_source(
1999  "// SYMBOL \"isinf\"\n"
2000  "casadi_real casadi_isinf(casadi_real x) {\n"
2001  "/* Pre-c99 compatibility */\n"
2002  "#if __STDC_VERSION__ < 199901L\n"
2003  " return x== INFINITY || x==-INFINITY;\n"
2004  "#else\n"
2005  " return isinf(x);\n"
2006  "#endif\n"
2007  "}\n\n",
2008  inst);
2009  break;
2010  case AUX_ISFINITE:
2011  this->auxiliaries << sanitize_source(
2012  "// SYMBOL \"isfinite\"\n"
2013  "casadi_real casadi_isfinite(casadi_real x) {\n"
2014  "/* Pre-c99 compatibility */\n"
2015  "#if __STDC_VERSION__ < 199901L\n"
2016  " return x==x && x!=INFINITY && x!=-INFINITY;\n"
2017  "#else\n"
2018  " return isfinite(x);\n"
2019  "#endif\n"
2020  "}\n\n",
2021  inst);
2022  break;
2023  case AUX_MIN:
2024  this->auxiliaries << sanitize_source(
2025  "// SYMBOL \"min\"\n"
2026  "casadi_int casadi_min(casadi_int x, casadi_int y) {\n"
2027  " return x>y ? y : x;\n"
2028  "}\n\n",
2029  inst);
2030  break;
2031  case AUX_MAX:
2032  this->auxiliaries << sanitize_source(
2033  "// SYMBOL \"max\"\n"
2034  "casadi_int casadi_max(casadi_int x, casadi_int y) {\n"
2035  " return x>y ? x : y;\n"
2036  "}\n\n",
2037  inst);
2038  break;
2039  case AUX_MMIN:
2042  this->auxiliaries << sanitize_source(casadi_mmin_str, inst);
2043  break;
2044  case AUX_MMAX:
2047  this->auxiliaries << sanitize_source(casadi_mmax_str, inst);
2048  break;
2049  case AUX_INF:
2050  this->auxiliaries << "#ifndef casadi_inf\n"
2051  << " #define casadi_inf " << this->infinity << "\n"
2052  << "#endif\n\n";
2053  break;
2054  case AUX_NAN:
2055  this->auxiliaries << "#ifndef casadi_nan\n"
2056  << " #define casadi_nan " << this->nan << "\n"
2057  << "#endif\n\n";
2058  break;
2059  case AUX_REAL_MIN:
2060  this->auxiliaries << "#ifndef casadi_real_min\n"
2061  << " #define casadi_real_min " << this->real_min << "\n"
2062  << "#endif\n\n";
2063  break;
2064  case AUX_LOG1P:
2065  this->auxiliaries << sanitize_source(
2066  "// SYMBOL \"log1p\"\n"
2067  "casadi_real casadi_log1p(casadi_real x) {\n"
2068  "/* Pre-c99 compatibility */\n"
2069  "#if __STDC_VERSION__ < 199901L\n"
2070  " return log(1+x);\n"
2071  "#else\n"
2072  " return log1p(x);\n"
2073  "#endif\n"
2074  "}\n\n",
2075  inst);
2076  break;
2077  case AUX_EXPM1:
2078  this->auxiliaries << sanitize_source(
2079  "// SYMBOL \"expm1\"\n"
2080  "casadi_real casadi_expm1(casadi_real x) {\n"
2081  "/* Pre-c99 compatibility */\n"
2082  "#if __STDC_VERSION__ < 199901L\n"
2083  " return exp(x)-1;\n"
2084  "#else\n"
2085  " return expm1(x);\n"
2086  "#endif\n"
2087  "}\n\n",
2088  inst);
2089  break;
2090  case AUX_HYPOT:
2091  this->auxiliaries << sanitize_source(
2092  "// SYMBOL \"hypot\"\n"
2093  "casadi_real casadi_hypot(casadi_real x, casadi_real y) {\n"
2094  "/* Pre-c99 compatibility */\n"
2095  "#if __STDC_VERSION__ < 199901L\n"
2096  " return sqrt(x*x+y*y);\n"
2097  "#else\n"
2098  " return hypot(x, y);\n"
2099  "#endif\n"
2100  "}\n\n",
2101  inst);
2102  break;
2103  case AUX_BLAZING_COMMON:
2105  add_include("simde/x86/avx2.h");
2106  add_include("simde/x86/fma.h");
2107  this->auxiliaries << sanitize_source(casadi_blazing_common_str, inst);
2108  break;
2111  this->auxiliaries << sanitize_source(casadi_blazing_1d_boor_eval_str, inst);
2112  break;
2115  this->auxiliaries << sanitize_source(casadi_blazing_2d_boor_eval_str, inst);
2116  break;
2119  this->auxiliaries << sanitize_source(casadi_blazing_3d_boor_eval_str, inst);
2120  break;
2123  this->auxiliaries << sanitize_source(casadi_blazing_4d_boor_eval_str, inst);
2124  break;
2127  this->auxiliaries << sanitize_source(casadi_blazing_5d_boor_eval_str, inst);
2128  break;
2129  case AUX_PRINTME:
2131  this->auxiliaries << sanitize_source(casadi_printme_str, inst);
2132  break;
2133  case AUX_PRINT_SCALAR:
2135  this->auxiliaries << sanitize_source(casadi_print_scalar_str, inst);
2136  break;
2137  case AUX_PRINT_VECTOR:
2139  this->auxiliaries << sanitize_source(casadi_print_vector_str, inst);
2140  break;
2141  case AUX_PRINT_CANONICAL:
2143  this->auxiliaries << sanitize_source(casadi_print_canonical_str, inst);
2144  break;
2145  case AUX_FPRINTF_SCALAR:
2146  add_include("stdio.h");
2148  this->auxiliaries << sanitize_source(casadi_fprintf_scalar_str, inst);
2149  break;
2150  case AUX_FPRINTF_VECTOR:
2152  this->auxiliaries << sanitize_source(casadi_fprintf_vector_str, inst);
2153  break;
2154  case AUX_TO_FILE:
2155  add_include("stdio.h");
2157  this->auxiliaries << sanitize_source(casadi_to_file_str, inst);
2158  break;
2159  case AUX_THREADS:
2160  this->auxiliaries << sanitize_source(casadi_threads_str, inst);
2161  break;
2162  }
2163  }
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,
const std::string &  name = "" 
)

Definition at line 1323 of file code_generator.cpp.

1323  {
1324  if (!name.empty()) {
1325  casadi_assert(std::find(exposed_fname.begin(), exposed_fname.end(), name)
1326  ==exposed_fname.end(),
1327  "Cannot code generate '" + name + "': name conflicts with an external dependency.");
1328  external_names_.insert(name);
1329  }
1330  added_externals_.insert(new_external);
1331  }
std::set< std::string > added_externals_

References added_externals_, exposed_fname, external_names_, and name.

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 1246 of file code_generator.cpp.

1247  {
1248  // Register the new element
1249  bool added = added_includes_.insert(new_include).second;
1250 
1251  // Quick return if it already exists
1252  if (!added) return;
1253 
1254  // Ifdef opening
1255  if (!use_ifdef.empty()) this->includes << "#ifdef " << use_ifdef << std::endl;
1256 
1257  // Print to the header section
1258  if (relative_path) {
1259  this->includes << "#include \"" << new_include << "\"\n";
1260  } else {
1261  this->includes << "#include <" << new_include << ">\n";
1262  }
1263 
1264  // Ifdef closing
1265  if (!use_ifdef.empty()) this->includes << "#endif\n";
1266  }
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 2805 of file code_generator.cpp.

2808  {
2809  // Insert element, quick return if it already exists
2810  if (!sparsity_meta.insert(name).second) return;
2811 
2812  // Input sparsities
2813  *this << declare("const casadi_int* " + name + "_sparsity_in(casadi_int i)") << " {\n"
2814  << "switch (i) {\n";
2815  for (casadi_int i=0; i<sp_in.size(); ++i) {
2816  *this << "case " << i << ": return " << sparsity(sp_in[i], force_canonical) << ";\n";
2817  }
2818  *this << "default: return 0;\n}\n"
2819  << "}\n\n";
2820 
2821  // Output sparsities
2822  *this << declare("const casadi_int* " + name + "_sparsity_out(casadi_int i)") << " {\n"
2823  << "switch (i) {\n";
2824  for (casadi_int i=0; i<sp_out.size(); ++i) {
2825  *this << "case " << i << ": return " << sparsity(sp_out[i], force_canonical) << ";\n";
2826  }
2827  *this << "default: return 0;\n}\n"
2828  << "}\n\n";
2829  }
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 1346 of file code_generator.cpp.

1346  {
1347  return get_constant(sp.compress(canonical), true);
1348  }
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 1136 of file code_generator.cpp.

1137  {
1138  std::stringstream s;
1139  s << type << " ";
1140  if (len==0) {
1141  s << "*" << name << " = 0";
1142  } else {
1143  s << name << "[" << len << "]";
1144  if (!def.empty()) s << " = " << def;
1145  }
1146  s << ";\n";
1147  return s.str();
1148  }

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 2313 of file code_generator.cpp.

2314  {
2316  std::stringstream s;
2317  s << "casadi_bilin(" << A << ", " << sparsity(sp_A) << ", " << x << ", " << y << ")";
2318  return s.str();
2319  }

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 3002 of file code_generator.cpp.

3004  {
3006  return "casadi_bound_consistency(" + str(n) + ", " + x + ", " + lam +
3007  ", " + lbx + ", " + ubx + ")";
3008  }

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 3110 of file code_generator.cpp.

3112  {
3114  return "casadi_cache_check(" + key + ", " + cache + ", " + loc + ", " +
3115  str(stride) + ", " + str(sz) + ", " + str(key_sz) + ", " + val + ")";
3116  }

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 452 of file code_generator.cpp.

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

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 2273 of file code_generator.cpp.

2273  {
2274  std::stringstream s;
2275  // Perform operation
2277  s << "casadi_clear(" << res << ", " << n << ");";
2278  return s.str();
2279  }
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 2478 of file code_generator.cpp.

2479  {
2481  return "casadi_clip_max(" + x + ", " + str(n) + ", " + min + ", " + mask + ");";
2482  }
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 2472 of file code_generator.cpp.

2473  {
2475  return "casadi_clip_min(" + x + ", " + str(n) + ", " + min + ", " + mask + ");";
2476  }

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 2208 of file code_generator.cpp.

2208  {
2209  return str(v);
2210  }

References casadi::str().

◆ constant() [2/9]

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

Definition at line 2212 of file code_generator.cpp.

2212  {
2213  return constant(static_cast<casadi_int>(v));
2214  }
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 2201 of file code_generator.cpp.

2201  {
2202  std::string ret = v;
2203  ret = replace(ret, "\\", "\\\\");
2204  ret = replace(ret, "\"", "\\\"");
2205  return "\"" + ret + "\"";
2206  }
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 1481 of file code_generator.cpp.

1481  {
1482  return shorthand("s" + str(get_constant(v, true)));
1483  }

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 1485 of file code_generator.cpp.

1485  {
1486  return shorthand("b" + str(get_constant(v, true)));
1487  }

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 1519 of file code_generator.cpp.

1519  {
1520  return shorthand("c" + str(get_constant(v, true)));
1521  }

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 1489 of file code_generator.cpp.

1489  {
1490  return shorthand("a" + str(get_constant(v, true)));
1491  }

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 2215 of file code_generator.cpp.

2215  {
2216  std::stringstream s;
2217  if (isnan(v)) {
2219  s << "casadi_nan";
2220  } else if (isinf(v)) {
2222  if (v<0) s << "-";
2223  s << "casadi_inf";
2224  } else {
2225  casadi_int v_int = static_cast<casadi_int>(v);
2226  if (static_cast<double>(v_int)==v) {
2227  // Print integer
2228  s << v_int << ".";
2229  } else {
2230  // Print real
2231  std::ios_base::fmtflags fmtfl = s.flags(); // get current format flags
2232  s << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1) << v;
2233  s.flags(fmtfl); // reset current format flags
2234  }
2235  }
2236  return s.str();
2237  }

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 1503 of file code_generator.cpp.

1504  {
1505  std::string ref = constant(v);
1506  if (!v.empty()) {
1507  local(name+"[" + str(v.size()) + "]", type);
1508  } else {
1509  local(name, type, "*");
1510  }
1511  if (!v.empty()) {
1512  local("i", type);
1513  (*this) << "for (i=0;i<" << v.size() << ";++i) " + name + "[i] = " + ref + "[i];\n";
1514  } else {
1515  init_local(name, "0");
1516  }
1517  }
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 2989 of file code_generator.cpp.

2991  {
2993  return Convexify::generate(*this, d, Hin, Hout, iw, w);
2994  }
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 2239 of file code_generator.cpp.

2240  {
2241  std::stringstream s;
2242  // Perform operation
2244  s << "casadi_copy(" << arg << ", " << n << ", " << res << ");";
2245  return s.str();
2246  }
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 2253 of file code_generator.cpp.

2254  {
2255  std::vector<std::string> checks;
2256  if (check_lhs) checks.push_back(arg);
2257  if (check_rhs) checks.push_back(res);
2258  if (!checks.empty()) *this << "if (" << join(checks, " && ") << ") ";
2259  *this << copy(arg, n, res) << "\n";
2260  }
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 2262 of file code_generator.cpp.

2263  {
2264  *this << "if (" << arg << ") {\n";
2265  if (check_rhs) *this << "if (" << res << ") ";
2266  *this << copy(arg, n, res) << "\n";
2267  *this << "} else {\n";
2268  if (check_rhs) *this << "if (" << res << ") ";
2269  *this << fill(res, n, def) << "\n";
2270  *this << "}\n";
2271  }
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 2364 of file code_generator.cpp.

2364  {
2365  // Add c linkage
2366  std::string cpp_prefix = this->cpp ? "extern \"C\" " : "";
2367 
2368  // To header file
2369  if (this->with_header) {
2370  this->header << cpp_prefix << this->dll_import << s << ";\n";
2371  }
2372 
2373  // Return name with declarations
2374  return cpp_prefix + this->dll_export + s;
2375  }

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 911 of file code_generator.cpp.

912  {
913  if (!thread_safe()) return;
914  if (!local_mutexes_[f.get()].insert(name).second) return; // already defined
915  std::string sh = shorthand(name);
916  auxiliaries << "#if CASADI_MUTEX_USE_STATIC_INIT == 0\n";
917  auxiliaries << "static CASADI_MUTEX_TYPE " << sh << ";\n";
918  auxiliaries << "#else\n";
919  auxiliaries << "static CASADI_MUTEX_TYPE " << sh
920  << " = CASADI_MUTEX_STATIC_INIT;\n";
921  auxiliaries << "#endif\n";
922  }
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 893 of file code_generator.cpp.

893  {
894  auto it = pool_double_.find(name);
895  if (it==pool_double_.end()) {
896  casadi_int index = pool_double_defaults_.size();
897  pool_double_defaults_.push_back(def);
898  shorthand("pd" + str(index));
899  pool_double_[name] = index;
900  } else {
901  casadi_assert_dev(def==pool_double_defaults_[it->second]);
902  }
903  }
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 865 of file code_generator.cpp.

865  {
866  auto it = file_scope_double_.find(id);
867  casadi_assert(it==file_scope_double_.end(), "Already defined.");
868  shorthand("rd" + str(file_scope_double_.size()));
869  file_scope_double_[id] = size;
870  }
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 879 of file code_generator.cpp.

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

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 2404 of file code_generator.cpp.

2405  {
2406  // Create call
2408  std::stringstream s;
2409  s << "casadi_densify(" << arg << ", " << sparsity(sp_arg) << ", " << res << ", "
2410  << (tr ? 1 : 0) << ");";
2411  return s.str();
2412  }

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 2842 of file code_generator.cpp.

2844  {
2846  return "casadi_det(" + sp_v + ", " + v + ", " + sp_r + ", " + r + ", " + beta + ")";
2847  }

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 2305 of file code_generator.cpp.

2306  {
2308  std::stringstream s;
2309  s << "casadi_dot(" << n << ", " << x << ", " << y << ")";
2310  return s.str();
2311  }

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 446 of file code_generator.cpp.

446  {
447  std::stringstream s;
448  dump(s);
449  return s.str();
450  }
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 941 of file code_generator.cpp.

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

1223  {
1224  if (v1.size()!=v2.size()) return false;
1225  for (casadi_int j=0; j<v1.size(); ++j) {
1226  if (v1[j]!=v2[j]) return false;
1227  }
1228  return true;
1229  }

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 3010 of file code_generator.cpp.

3011  {
3013  return "casadi_file_slurp(\"" + fname + "\", " + str(n) + ", " + a + ")";
3014  }

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 2295 of file code_generator.cpp.

2296  {
2297  if (v=="0") return clear(res, n);
2298  std::stringstream s;
2299  // Perform operation
2301  s << "casadi_fill(" << res << ", " << n << ", " << v << ");";
2302  return s.str();
2303  }
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 2887 of file code_generator.cpp.

2888  {
2890  return "casadi_fmax(" + x + ", " + y + ");";
2891  }

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 2893 of file code_generator.cpp.

2894  {
2896  return "casadi_fmin(" + x + ", " + y + ");";
2897  }

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 2185 of file code_generator.cpp.

2185  {
2186  // Process C++ source
2187  std::stringstream ret;
2188  std::string line;
2189  std::istringstream stream(casadi_fmu_str);
2190  while (std::getline(stream, line)) {
2191  // Replacements
2192  if (line.find("MODELNAME") != std::string::npos) {
2193  line = replace(line, "MODELNAME", modelname);
2194  }
2195  // Append to return
2196  ret << line << "\n";
2197  }
2198  return ret.str();
2199  }

References casadi::replace().

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

◆ format_padded()

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

Definition at line 1129 of file code_generator.cpp.

1129  {
1130  std::stringstream ss;
1131  ss.str("");
1132  ss << std::setw(padding_length_) << std::setfill('0') << i;
1133  return ss.str();
1134  }

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 1191 of file code_generator.cpp.

1191  {
1193  std::stringstream s;
1194  s << "casadi_fprintf_scalar(" << f << ", " << arg << ");";
1195  return s.str();
1196  }

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 1198 of file code_generator.cpp.

1199  {
1201  std::stringstream s;
1202  s << "casadi_fprintf_vector(" << f << ", " << sz << ", " << arg << ", \"" << sep << "\");";
1203  return s.str();
1204  }

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 2172 of file code_generator.cpp.

2174  {
2175  // Handle offset with recursion
2176  if (res_off!=0) return from_mex(arg, res+"+"+str(res_off), 0, sp_res, w);
2177 
2179  std::stringstream s;
2180  s << "casadi_from_mex(" << arg
2181  << ", " << res << ", " << sparsity(sp_res) << ", " << w << ");";
2182  return s.str();
2183  }
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 533 of file code_generator.cpp.

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

3023  {
3024  casadi_int n = is_input ? f.n_in() : f.n_out();
3025  std::string dump_format = f->dump_format_.empty() ? "mtx" : f->dump_format_;
3026  std::string effective_dir = dump_dir_prefix + f->dump_dir_ + dump_dir_suffix;
3027  std::string prefix;
3028  if (!effective_dir.empty()) prefix = effective_dir + "/";
3029  std::string inout = is_input ? "in" : "out";
3030 
3031  // Ensure directory exists at codegen time
3032  if (!effective_dir.empty() && Filesystem::is_enabled()) {
3033  std::string dir = Filesystem::ensure_trailing_slash(effective_dir);
3034  casadi_assert(Filesystem::ensure_directory_exists(dir),
3035  "Unable to create the required directory for '" + dir + "'.");
3036  }
3037 
3038  // Assumes dump_id_local is declared in the enclosing scope
3039  // Per-input/output format files (e.g. .mtx)
3040  for (casadi_int i = 0; i < n; ++i) {
3041  std::string io_name = is_input ? f.name_in(i) : f.name_out(i);
3042  Sparsity sp = is_input ? f.sparsity_in(i) : f.sparsity_out(i);
3043  std::string fixed_part = prefix + f.name() + "." + inout + "." + io_name + "." + dump_format;
3044  casadi_int buf_size = fixed_part.size() + 1 + 6 + 1; // +7 for the counter field
3045  *this << "{\n";
3046  *this << "char dump_fname[" << buf_size << "];\n";
3047  *this << "FILE* dump_file;\n";
3048  *this << "snprintf(dump_fname, " << buf_size << ", \""
3049  << prefix << f.name() << ".%06d." << inout << "." << io_name
3050  << "." << dump_format << "\", dump_id_local);\n";
3051  if (f->verbose_) {
3052  *this << printf("dump -> %s\\n", "dump_fname") << "\n";
3053  }
3054  *this << "dump_file = fopen(dump_fname, \"w\");\n";
3055  *this << "if (dump_file) {\n";
3056  *this << to_file("dump_file", sp, arr + "[" + str(i) + "]") << ";\n";
3057  *this << "fclose(dump_file);\n";
3058  *this << "}\n";
3059  *this << "}\n";
3060  }
3061  // Combined .txt file (normalized doubles, one per line)
3062  {
3063  add_include("stdio.h");
3064  std::string fixed_part = prefix + f.name() + "." + inout + ".txt";
3065  casadi_int buf_size = fixed_part.size() + 1 + 6 + 1;
3066  *this << "{\n";
3067  *this << "char dump_fname[" << buf_size << "];\n";
3068  *this << "FILE* dump_file;\n";
3069  *this << "casadi_int dump_k;\n";
3070  *this << "snprintf(dump_fname, " << buf_size << ", \""
3071  << prefix << f.name() << ".%06d." << inout << ".txt\", dump_id_local);\n";
3072  *this << "dump_file = fopen(dump_fname, \"w\");\n";
3073  *this << "if (dump_file) {\n";
3074  for (casadi_int i = 0; i < n; ++i) {
3075  Sparsity sp = is_input ? f.sparsity_in(i) : f.sparsity_out(i);
3076  casadi_int nnz = sp.nnz();
3077  std::string a = arr + "[" + str(i) + "]";
3078  if (nnz > 0) {
3079  *this << "if (" << a << ") {\n";
3080  *this << fprintf_vector("dump_file", nnz, a, "\\n") << "\n";
3081  *this << "fprintf(dump_file, \"\\n\");\n";
3082  *this << "} else {\n";
3083  std::string zero_str = is_input ? "0.0000000000000000e+00" : "nan";
3084  *this << "for (dump_k=0; dump_k<" << nnz << "; ++dump_k) "
3085  << "fprintf(dump_file, \"" << zero_str << "\\n\");\n";
3086  *this << "}\n";
3087  }
3088  }
3089  *this << "fclose(dump_file);\n";
3090  *this << "}\n";
3091  *this << "}\n";
3092  }
3093  }
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 3095 of file code_generator.cpp.

3096  {
3097  casadi_int n = is_input ? f.n_in() : f.n_out();
3098  std::string inout = is_input ? "Input" : "Output";
3099 
3100  *this << printf("Function " + f.name() + "\\n") << "\n";
3101  for (casadi_int i = 0; i < n; ++i) {
3102  std::string io_name = is_input ? f.name_in(i) : f.name_out(i);
3103  Sparsity sp = is_input ? f.sparsity_in(i) : f.sparsity_out(i);
3104  *this << printf(inout + " " + str(i) + " (" + io_name + "): ") << "\n";
3105  *this << print_canonical(sp, arr + "[" + str(i) + "]") << "\n";
3106  *this << printf("\\n") << "\n";
3107  }
3108  }
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_codegen_sz_w()

bool casadi::CodeGenerator::get_codegen_sz_w ( const FunctionInternal *  f,
size_t &  sz_w 
) const

Returns false if the function did not register one.

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

Definition at line 1122 of file code_generator.cpp.

1122  {
1123  auto it = codegen_sz_w_.find(f);
1124  if (it==codegen_sz_w_.end()) return false;
1125  sz_w = it->second;
1126  return true;
1127  }
std::map< const FunctionInternal *, size_t > codegen_sz_w_

References codegen_sz_w_.

Referenced by casadi::MXFunction::codegen_sz_w().

◆ 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 1412 of file code_generator.cpp.

1412  {
1413  // Hash the vector
1414  size_t h = hash(v);
1415 
1416  // Try to locate it in already added constants
1417  std::pair<std::multimap<size_t, size_t>::iterator, std::multimap<size_t, size_t>::iterator> eq =
1418  added_integer_constants_.equal_range(h);
1419  for (std::multimap<size_t, size_t>::iterator i=eq.first; i!=eq.second; ++i) {
1420  if (equal(v, integer_constants_[i->second])) return i->second;
1421  }
1422 
1423  if (allow_adding) {
1424  // Add to constants
1425  casadi_int ind = integer_constants_.size();
1426  integer_constants_.push_back(v);
1427  added_integer_constants_.insert(std::pair<size_t, size_t>(h, ind));
1428  return ind;
1429  } else {
1430  casadi_error("Constant not found");
1431  return -1;
1432  }
1433  }
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 1435 of file code_generator.cpp.

1435  {
1436  // Hash the vector
1437  size_t h = hash(v);
1438 
1439  // Try to locate it in already added constants
1440  std::pair<std::multimap<size_t, size_t>::iterator, std::multimap<size_t, size_t>::iterator> eq =
1441  added_char_constants_.equal_range(h);
1442  for (std::multimap<size_t, size_t>::iterator i=eq.first; i!=eq.second; ++i) {
1443  if (equal(v, char_constants_[i->second])) return i->second;
1444  }
1445 
1446  if (allow_adding) {
1447  // Add to constants
1448  casadi_int ind = char_constants_.size();
1449  char_constants_.push_back(v);
1450  added_char_constants_.insert(std::pair<size_t, size_t>(h, ind));
1451  return ind;
1452  } else {
1453  casadi_error("Constant not found");
1454  return -1;
1455  }
1456  }
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 1390 of file code_generator.cpp.

1390  {
1391  // Hash the vector
1392  size_t h = hash(v);
1393 
1394  // Try to locate it in already added constants
1395  auto eq = added_double_constants_.equal_range(h);
1396  for (auto i=eq.first; i!=eq.second; ++i) {
1397  if (equal(v, double_constants_[i->second])) return i->second;
1398  }
1399 
1400  if (allow_adding) {
1401  // Add to constants
1402  casadi_int ind = double_constants_.size();
1403  double_constants_.push_back(v);
1404  added_double_constants_.insert(std::make_pair(h, ind));
1405  return ind;
1406  } else {
1407  casadi_error("Constant not found");
1408  return -1;
1409  }
1410  }
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 1458 of file code_generator.cpp.

1458  {
1459  // Hash the vector
1460  size_t h = hash(v);
1461 
1462  // Try to locate it in already added constants
1463  std::pair<std::multimap<size_t, size_t>::iterator, std::multimap<size_t, size_t>::iterator> eq =
1464  added_string_constants_.equal_range(h);
1465  for (std::multimap<size_t, size_t>::iterator i=eq.first; i!=eq.second; ++i) {
1466  if (equal(v, string_constants_[i->second])) return i->second;
1467  }
1468 
1469  if (allow_adding) {
1470  // Add to constants
1471  casadi_int ind = string_constants_.size();
1472  string_constants_.push_back(v);
1473  added_string_constants_.insert(std::pair<size_t, size_t>(h, ind));
1474  return ind;
1475  } else {
1476  casadi_error("Constant not found");
1477  return -1;
1478  }
1479  }
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 1354 of file code_generator.cpp.

1354  {
1355  return const_cast<CodeGenerator&>(*this).get_constant(sp, false);
1356  }
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 1372 of file code_generator.cpp.

1372  {
1373  size_t seed=0;
1374  hash_combine(seed, v);
1375  return seed;
1376  }
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 1378 of file code_generator.cpp.

1378  {
1379  size_t seed=0;
1380  hash_combine(seed, v);
1381  return seed;
1382  }

References casadi::hash_combine().

◆ hash() [3/4]

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

Definition at line 1358 of file code_generator.cpp.

1358  {
1359  // Calculate a hash value for the vector
1360  std::size_t seed=0;
1361  if (!v.empty()) {
1362  casadi_assert_dev(sizeof(double) % sizeof(size_t)==0);
1363  const casadi_int int_len = v.size()*(sizeof(double)/sizeof(size_t));
1364  const size_t* int_v = reinterpret_cast<const size_t*>(&v.front());
1365  for (size_t i=0; i<int_len; ++i) {
1366  hash_combine(seed, int_v[i]);
1367  }
1368  }
1369  return seed;
1370  }

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 1384 of file code_generator.cpp.

1384  {
1385  size_t seed=0;
1386  hash_combine(seed, v);
1387  return seed;
1388  }

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 2331 of file code_generator.cpp.

2336  {
2338  std::stringstream s;
2339  s << "casadi_interpn(" << res << ", " << ndim << ", " << grid << ", " << offset << ", "
2340  << values << ", " << x << ", " << lookup_mode << ", " << m << ", " << iw << ", " << w << ");";
2341  return s.str();
2342  }

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 2344 of file code_generator.cpp.

2348  {
2350  std::stringstream s;
2351  s << "casadi_interpn_grad(" << grad << ", " << ndim << ", " << grid << ", " << offset << ", "
2352  << values << ", " << x << ", " << lookup_mode << "," << m << ", " << iw << ", " << w << ");";
2353  return s.str();
2354  }

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 2977 of file code_generator.cpp.

2978  {
2980  return "casadi_lb_eig(" + sparsity(sp_h) + ", " + h + ")";
2981  }

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 2869 of file code_generator.cpp.

2872  {
2874  return "casadi_ldl(" + sp_a + ", " + a + ", " + sp_lt + ", " + lt + ", "
2875  + d + ", " + p + ", " + w + ");";
2876  }

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 2878 of file code_generator.cpp.

2881  {
2883  return "casadi_ldl_solve(" + x + ", " + str(nrhs) + ", " + sp_lt + ", "
2884  + lt + ", " + d + ", " + p + ", " + w + ");";
2885  }

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 2619 of file code_generator.cpp.

2620  {
2621  // Check if the variable already exists
2622  auto it = local_variables_.find(name);
2623  if (it==local_variables_.end()) {
2624  // Add it
2625  local_variables_[name] = std::make_pair(type, ref);
2626  } else {
2627  // Consistency check
2628  casadi_assert(it->second.first==type, "Type mismatch for " + name);
2629  casadi_assert(it->second.second==ref, "Type mismatch for " + name);
2630  }
2631  }
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 934 of file code_generator.cpp.

935  {
936  auto it = local_mutexes_.find(f.get());
937  if (it == local_mutexes_.end()) return empty_mutex_set_;
938  return it->second;
939  }
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 2560 of file code_generator.cpp.

2560  {
2562  std::stringstream s;
2563  s << "casadi_logsumexp(" << A << ", " << n << ");";
2564  return s.str();
2565  }

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 2996 of file code_generator.cpp.

2997  {
2999  return "casadi_low(" + x + ", " + grid + ", " + str(ng) + ", " + str(lookup_mode) + ");";
3000  }

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 2861 of file code_generator.cpp.

2863  {
2865  return "casadi_lsqr_solve(" + A + ", " + x + ", " + str(nrhs) + ", "
2866  + (tr ? "1" : "0") + ", " + sp + ", " + w + ");";
2867  }

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 2496 of file code_generator.cpp.

2497  {
2499  return "casadi_masked_norm_inf(" + str(n) + ", " + x + ", " + mask + ")";
2500  }

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 2923 of file code_generator.cpp.

2924  {
2926  return "casadi_max(" + x + ", " + y + ")";
2927  }

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 2947 of file code_generator.cpp.

2948  {
2950  return "casadi_max_viol(" + str(n) + ", " + x+ ", " + lb + ", " + ub + ")";
2951  }

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 2289 of file code_generator.cpp.

2289  {
2290  std::string name = f->codegen_name(*this, false);
2291  std::string mem_array = shorthand(name + "_mem");
2292  return mem_array+"[mem]";
2293  }

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 2929 of file code_generator.cpp.

2930  {
2932  return "casadi_min(" + x + ", " + y + ")";
2933  }

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 2935 of file code_generator.cpp.

2936  {
2938  return "casadi_mmax(" + x + ", " + str(n) + ", " + str(casadi_int(is_dense)) + ")";
2939  }

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 2941 of file code_generator.cpp.

2942  {
2944  return "casadi_mmin(" + x + ", " + str(n) + ", " + str(casadi_int(is_dense)) + ")";
2945  }

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 2530 of file code_generator.cpp.

2532  {
2534  return "casadi_mtimes_dense(" + x + ", " + str(nrow_x) + ", " + str(ncol_x) + ", "
2535  + y + ", " + str(ncol_y) + ", " + z + ", " + (tr ? "1" : "0") + ");";
2536  }

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 2521 of file code_generator.cpp.

2524  {
2526  return "casadi_mtimes(" + x + ", " + sparsity(sp_x) + ", " + y + ", " + sparsity(sp_y) + ", "
2527  + z + ", " + sparsity(sp_z) + ", " + w + ", " + (tr ? "1" : "0") + ");";
2528  }

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 2538 of file code_generator.cpp.

2539  {
2541  return "casadi_mtimes_dense_sparse(" + x + ", " + str(nrow_x) + ", "
2542  + y + ", " + sparsity(sp_y) + ", " + z + ");";
2543  }

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 2514 of file code_generator.cpp.

2515  {
2517  return "casadi_mv_dense(" + x + ", " + str(nrow_x) + ", " + str(ncol_x) + ", "
2518  + y + ", " + z + ", " + (tr ? "1" : "0") + ");";
2519  }

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 2507 of file code_generator.cpp.

2508  {
2510  return "casadi_mv(" + x + ", " + sparsity(sp_x) + ", " + y + ", "
2511  + z + ", " + (tr ? "1" : "0") + ");";
2512  }

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 2965 of file code_generator.cpp.

2966  {
2968  return "casadi_norm_1(" + str(n) + ", " + x + ")";
2969  }

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 2971 of file code_generator.cpp.

2972  {
2974  return "casadi_norm_2(" + str(n) + ", " + x + ")";
2975  }

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 1498 of file code_generator.cpp.

1498  {
1499  sz_ones_ = std::max(sz_ones_, sz);
1500  return shorthand("ones");
1501  }

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 1285 of file code_generator.cpp.

1288  {
1289  std::string name = add_dependency(f);
1290 
1291  std::string cg_name = f->codegen_name(*this, false);
1292  bool needs_mem = f->codegen_needs_mem();
1293  if (needs_mem) {
1294  std::string mem = "mid";
1295  local("flag", "int");
1296  local(mem, "int");
1297  std::string checkout = shorthand(cg_name + "_checkout");
1298  *this << mem << " = " << checkout << "();\n";
1299  if (failure_ret.empty()) {
1300  *this << "if (" << mem << "<0) {\n";
1301  *this << "flag = 1;\n";
1302  *this << "} else {\n";
1303  } else {
1304  *this << "if (" << mem << "<0) return " << failure_ret << ";\n";
1305  }
1306 
1307  *this << "flag = " + name + "(" + arg + ", " + res + ", "
1308  + iw + ", " + w + ", " << mem << ");\n";
1309 
1310  if (failure_ret.empty()) {
1311  *this << "}\n";
1312  }
1313 
1314  std::string release = shorthand(cg_name + "_release");
1315  *this << release << "(" << mem << ");\n";
1316  return "flag";
1317  } else {
1318  return name + "(" + arg + ", " + res + ", "
1319  + iw + ", " + w + ", 0)";
1320  }
1321  }
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 2593 of file code_generator.cpp.

2593  {
2594  // Loop over newline characters
2595  size_t off=0;
2596  while (true) {
2597  size_t pos = s.find('\n', off);
2598  if (pos==std::string::npos) {
2599  // No more newline characters
2600  print_formatted(s.substr(off));
2601  break;
2602  } else {
2603  // Ends with newline
2604  print_formatted(s.substr(off, pos-off));
2605  this->buffer << '\n';
2606  newline_ = true;
2607  off = pos+1;
2608  }
2609  }
2610 
2611  return *this;
2612  }
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<< ( T  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 905 of file code_generator.cpp.

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

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 1170 of file code_generator.cpp.

1170  {
1172  std::stringstream s;
1173  s << "casadi_print_canonical(" << sparsity(sp) << ", " << arg << ");";
1174  return s.str();
1175  }

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 2567 of file code_generator.cpp.

2567  {
2568  // Quick return if empty
2569  if (s.empty()) return;
2570 
2571  // If new line, add indentation
2572  if (newline_) {
2573  casadi_int shift = s.front()=='}' ? -1 : 0;
2574  casadi_assert_dev(current_indent_+shift>=0);
2575  this->buffer << std::string(indent_*(current_indent_+shift), ' ');
2576  newline_ = false;
2577  }
2578 
2579  // Print to body
2580  this->buffer << s;
2581 
2582  // Brackets change indentation for next row
2583  // NOTE(@jaeandersson): Should ignore strings, comments
2584  for (char c : s) {
2585  if (c=='{') {
2586  indent();
2587  } else if (c=='}') {
2588  unindent();
2589  }
2590  }
2591  }
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 1206 of file code_generator.cpp.

1206  {
1207  switch (op) {
1208  case OP_FABS:
1210  return "casadi_fabs("+a0+")";
1211  case OP_SQ:
1213  return "casadi_sq("+a0+")";
1214  case OP_SIGN:
1216  return "casadi_sign("+a0+")";
1217  case OP_LOG1P:
1219  return "casadi_log1p("+a0+")";
1220  case OP_EXPM1:
1222  return "casadi_expm1("+a0+")";
1223  default:
1224  return casadi_math<double>::print(op, a0);
1225  }
1226  }
@ 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 1227 of file code_generator.cpp.

1227  {
1228  switch (op) {
1229  case OP_FMIN:
1231  return "casadi_fmin("+a0+","+a1+")";
1232  case OP_FMAX:
1234  return "casadi_fmax("+a0+","+a1+")";
1235  case OP_HYPOT:
1237  return "casadi_hypot("+a0+","+a1+")";
1238  case OP_PRINTME:
1240  return "casadi_printme("+a0+","+a1+")";
1241  default:
1242  return casadi_math<double>::print(op, a0, a1);
1243  }
1244  }
@ 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 1184 of file code_generator.cpp.

1184  {
1186  std::stringstream s;
1187  s << "casadi_print_scalar(" << arg << ");";
1188  return s.str();
1189  }

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 1177 of file code_generator.cpp.

1177  {
1179  std::stringstream s;
1180  s << "casadi_print_vector(" << sz << ", " << arg << ");";
1181  return s.str();
1182  }

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 1150 of file code_generator.cpp.

1151  {
1152  s << array("static const casadi_int", name, v.size(), initializer(v));
1153  }
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 1155 of file code_generator.cpp.

1156  {
1157  s << array("static const char", name, v.size(), initializer(v));
1158  }

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 1160 of file code_generator.cpp.

1161  {
1162  s << array("static const casadi_real", name, v.size(), initializer(v));
1163  }

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 1165 of file code_generator.cpp.

1166  {
1167  s << array("static const char*", name, v.size(), initializer(v));
1168  }

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

◆ printf() [1/4]

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

Definition at line 2443 of file code_generator.cpp.

2443  {
2444  std::vector<std::string> arg;
2445  arg.push_back(arg1);
2446  return printf(str, arg);
2447  }

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 2449 of file code_generator.cpp.

2450  {
2451  std::vector<std::string> arg;
2452  arg.push_back(arg1);
2453  arg.push_back(arg2);
2454  return printf(str, arg);
2455  }

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 2457 of file code_generator.cpp.

2458  {
2459  std::vector<std::string> arg;
2460  arg.push_back(arg1);
2461  arg.push_back(arg2);
2462  arg.push_back(arg3);
2463  return printf(str, arg);
2464  }

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 2425 of file code_generator.cpp.

2425  {
2427  std::stringstream s;
2428  s << "CASADI_PRINTF(";
2429  // Loop over lines in str
2430  std::string::size_type pos = 0, prev = 0;
2431  while ((pos = str.find('\n', prev)) != std::string::npos) {
2432  // Any line containing a trailing new line
2433  s << "\"" << str.substr(prev, pos-prev) << "\\n\"\n";
2434  prev = pos + 1;
2435  }
2436  // Remainder without trailing new line
2437  s << "\"" << str.substr(prev) << "\"";
2438  for (casadi_int i=0; i<arg.size(); ++i) s << ", " << arg[i];
2439  s << ");";
2440  return s.str();
2441  }

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 2378 of file code_generator.cpp.

2380  {
2381  // If sparsity match, simple copy
2382  if (sp_arg==sp_res) return copy(arg, sp_arg.nnz(), res);
2383 
2384  // Create call
2386  std::stringstream s;
2387  s << "casadi_project(" << arg << ", " << sparsity(sp_arg) << ", " << res << ", "
2388  << sparsity(sp_res) << ", " << w << ");";
2389  return s.str();
2390  }

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 2831 of file code_generator.cpp.

2835  {
2837  return "casadi_qr(" + sp + ", " + A + ", " + w + ", "
2838  + sp_v + ", " + v + ", " + sp_r + ", " + r + ", "
2839  + beta + ", " + prinv + ", " + pc + ");";
2840  }

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 2849 of file code_generator.cpp.

2854  {
2856  return "casadi_qr_solve(" + x + ", " + str(nrhs) + ", " + (tr ? "1" : "0") + ", "
2857  + sp_v + ", " + v + ", " + sp_r + ", " + r + ", "
2858  + beta + ", " + prinv + ", " + pc + ", " + w + ");";
2859  }

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 2321 of file code_generator.cpp.

2323  {
2325  std::stringstream s;
2326  s << "casadi_rank1(" << A << ", " << sparsity(sp_A) << ", "
2327  << alpha << ", " << x << ", " << y << ");";
2328  return s.str();
2329  }

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 2983 of file code_generator.cpp.

2984  {
2986  return "casadi_regularize(" + sparsity(sp_h) + ", " + h + ", " + reg + ");";
2987  }

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 1109 of file code_generator.cpp.

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

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 872 of file code_generator.cpp.

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

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 886 of file code_generator.cpp.

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

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 2676 of file code_generator.cpp.

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

2502  {
2504  return "casadi_scal(" + str(n) + ", " + alpha + ", " + x + ");";
2505  }

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().

◆ set_codegen_sz_w()

void casadi::CodeGenerator::set_codegen_sz_w ( const FunctionInternal *  f,
size_t  sz_w 
)

Called from codegen_body when the generated layout of the work vector differs from the one used by numeric evaluation.

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

Definition at line 1118 of file code_generator.cpp.

1118  {
1119  codegen_sz_w_[f] = sz_w;
1120  }

References codegen_sz_w_.

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

◆ 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 1268 of file code_generator.cpp.

1268  {
1269  std::string name = add_dependency(f);
1270  bool needs_mem = f->codegen_needs_mem();
1271  if (needs_mem) {
1272  *this << s << ".checkout = " << name << "_checkout;\n";
1273  } else {
1274  *this << s << ".checkout = 0;\n";
1275  }
1276 
1277  *this << s << ".eval = " << name << ";\n";
1278  if (needs_mem) {
1279  *this << s << ".release = " << name << "_release;\n";
1280  } else {
1281  *this << s << ".release = 0;\n";
1282  }
1283  }

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 1338 of file code_generator.cpp.

1338  {
1339  bool added = added_shorthands_.insert(name).second;
1340  if (!allow_adding) {
1341  casadi_assert(added, "Duplicate macro: " + name);
1342  }
1343  return "casadi_" + name;
1344  }

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 2415 of file code_generator.cpp.

2416  {
2417  // Create call
2419  std::stringstream s;
2420  s << "casadi_sparsify(" << arg << ", " << res << ", "
2421  << sparsity(sp_res) << ", " << (tr ? 1 : 0) << ");";
2422  return s.str();
2423  }

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 478 of file code_generator.cpp.

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

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 459 of file code_generator.cpp.

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

2954  {
2956  return "casadi_sum_viol(" + str(n) + ", " + x+ ", " + lb + ", " + ub + ")";
2957  }

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 2633 of file code_generator.cpp.

2633  {
2634  if (avoid_stack_) {
2635  return "w[" + str(i) + "]";
2636  } else {
2637  std::string name = "a"+format_padded(i);
2638 
2639  // Make sure work vector element has been declared
2640  local(name, "casadi_real");
2641 
2642  return name;
2643  }
2644  }
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 3118 of file code_generator.cpp.

3118  {
3119  sz_arg = sz_res = sz_iw = sz_w = 0;
3120  for (auto&& f : added_functions_) {
3121  sz_arg = std::max(sz_arg, f.f.sz_arg());
3122  sz_res = std::max(sz_res, f.f.sz_res());
3123  sz_iw = std::max(sz_iw, f.f.sz_iw());
3124  sz_w = std::max(sz_w, f.f.sz_w());
3125  }
3126  }

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 3016 of file code_generator.cpp.

3017  {
3019  return "casadi_to_file(" + f + ", " + sparsity(sp) + ", " + x + ")";
3020  }

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 2165 of file code_generator.cpp.

2165  {
2167  std::stringstream s;
2168  s << "casadi_to_mex(" << sparsity(sp) << ", " << arg << ");";
2169  return s.str();
2170  }

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 2356 of file code_generator.cpp.

2358  {
2360  return "casadi_trans(" + x + "," + sparsity(sp_x) + ", "
2361  + y + ", " + sparsity(sp_y) + ", " + iw + ")";
2362  }

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 2393 of file code_generator.cpp.

2394  {
2395  // Create call
2397  std::stringstream s;
2398  s << "casadi_tri_project(" << arg << ", " << sparsity(sp_arg) << ", ";
2399  s << res << ", " << (lower ? 1: 0) << ");";
2400  return s.str();
2401  }

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 2545 of file code_generator.cpp.

2546  {
2548  return "casadi_trilsolve(" + sparsity(sp_x) + ", " + x + ", " + y + ", " + str(tr) + ", "
2549  + str(unity) + ", " + str(nrhs) + ");";
2550  }

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 2552 of file code_generator.cpp.

2553  {
2555  return "casadi_triusolve(" + sparsity(sp_x) + ", " + x + ", " + y + ", " + str(tr) + ", "
2556  + str(unity) + ", " + str(nrhs) + ");";
2557  }

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 2484 of file code_generator.cpp.

2485  {
2487  return "casadi_vector_fmax(" + str(n) + ", " + x + ", " + y + ", " + z + ");";
2488  }

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 2490 of file code_generator.cpp.

2491  {
2493  return "casadi_vector_fmin(" + str(n) + ", " + x + ", " + y + ", " + z + ");";
2494  }

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 2899 of file code_generator.cpp.

2900  {
2902  return "casadi_vfmax(" + x + ", " + str(n) + ", " + y + ");";
2903  }

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 2911 of file code_generator.cpp.

2912  {
2914  return "casadi_vfmax(" + x + ", " + n + ", " + y + ");";
2915  }

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 2905 of file code_generator.cpp.

2906  {
2908  return "casadi_vfmin(" + x + ", " + str(n) + ", " + y + ");";
2909  }

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 2917 of file code_generator.cpp.

2918  {
2920  return "casadi_vfmin(" + x + ", " + n + ", " + y + ");";
2921  }

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 1088 of file code_generator.cpp.

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

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 1493 of file code_generator.cpp.

1493  {
1494  sz_zeros_ = std::max(sz_zeros_, sz);
1495  return shorthand("zeros");
1496  }

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 1174 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 1177 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 1175 of file code_generator.hpp.

Referenced by get_constant().

◆ added_externals_

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

Definition at line 1171 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 1170 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 1176 of file code_generator.hpp.

Referenced by get_constant().

◆ added_sfunctions

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

Definition at line 1075 of file code_generator.hpp.

Referenced by add(), and generate().

◆ added_shorthands_

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

Definition at line 1173 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 1178 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 1202 of file code_generator.hpp.

Referenced by wrapper().

◆ auxiliaries

std::stringstream casadi::CodeGenerator::auxiliaries

◆ avoid_stack_

bool casadi::CodeGenerator::avoid_stack_

Definition at line 1102 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 1148 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 1062 of file code_generator.hpp.

Referenced by CodeGenerator().

◆ casadi_real_type

std::string casadi::CodeGenerator::casadi_real_type

Definition at line 1059 of file code_generator.hpp.

Referenced by CodeGenerator().

◆ char_constants_

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

Definition at line 1207 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 1112 of file code_generator.hpp.

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

◆ codegen_sz_w_

std::map<const FunctionInternal*, size_t> casadi::CodeGenerator::codegen_sz_w_

Definition at line 1190 of file code_generator.hpp.

Referenced by get_codegen_sz_w(), and set_codegen_sz_w().

◆ cpp

bool casadi::CodeGenerator::cpp

◆ current_indent_

casadi_int casadi::CodeGenerator::current_indent_

Definition at line 1155 of file code_generator.hpp.

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

◆ dll_export

std::string casadi::CodeGenerator::dll_export

Definition at line 1134 of file code_generator.hpp.

Referenced by CodeGenerator().

◆ dll_import

std::string casadi::CodeGenerator::dll_import

Definition at line 1134 of file code_generator.hpp.

Referenced by CodeGenerator(), and declare().

◆ double_constants_

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

Definition at line 1205 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 1164 of file code_generator.hpp.

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

◆ external_names_

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

Definition at line 1172 of file code_generator.hpp.

Referenced by add(), and add_external().

◆ 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 1184 of file code_generator.hpp.

Referenced by dump().

◆ force_canonical

bool casadi::CodeGenerator::force_canonical

Definition at line 1124 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 1099 of file code_generator.hpp.

Referenced by CodeGenerator().

◆ includes

std::stringstream casadi::CodeGenerator::includes

Definition at line 1144 of file code_generator.hpp.

Referenced by add_include(), and dump().

◆ indent_

casadi_int casadi::CodeGenerator::indent_

Definition at line 1154 of file code_generator.hpp.

Referenced by CodeGenerator(), and print_formatted().

◆ infinity

std::string casadi::CodeGenerator::infinity

Definition at line 1104 of file code_generator.hpp.

Referenced by add_auxiliary(), and CodeGenerator().

◆ inline_aux

bool casadi::CodeGenerator::inline_aux

Definition at line 1084 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 1206 of file code_generator.hpp.

Referenced by dump(), and get_constant().

◆ l1_blas

bool casadi::CodeGenerator::l1_blas

Definition at line 1128 of file code_generator.hpp.

Referenced by add_auxiliary(), and CodeGenerator().

◆ local_cleanup_

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

Definition at line 1180 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 1182 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 1187 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 1179 of file code_generator.hpp.

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

◆ local_void_

bool casadi::CodeGenerator::local_void_

Definition at line 1181 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 1118 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 1121 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 1104 of file code_generator.hpp.

Referenced by add_auxiliary(), and CodeGenerator().

◆ needs_mem_

bool casadi::CodeGenerator::needs_mem_

Definition at line 1211 of file code_generator.hpp.

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

◆ newline_

bool casadi::CodeGenerator::newline_

Definition at line 1151 of file code_generator.hpp.

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

◆ padding_length_

casadi_int casadi::CodeGenerator::padding_length_

Definition at line 1161 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 1186 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 1185 of file code_generator.hpp.

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

◆ prefix

std::string casadi::CodeGenerator::prefix

Definition at line 1137 of file code_generator.hpp.

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

◆ real_min

std::string casadi::CodeGenerator::real_min

Definition at line 1104 of file code_generator.hpp.

Referenced by add_auxiliary(), and CodeGenerator().

◆ sparsity_meta

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

Definition at line 1167 of file code_generator.hpp.

Referenced by add_io_sparsities().

◆ static_aux

bool casadi::CodeGenerator::static_aux

Definition at line 1081 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 1208 of file code_generator.hpp.

Referenced by dump(), and get_constant().

◆ suffix

std::string casadi::CodeGenerator::suffix

Definition at line 1056 of file code_generator.hpp.

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

◆ sz_ones_

casadi_int casadi::CodeGenerator::sz_ones_

Definition at line 1159 of file code_generator.hpp.

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

◆ sz_zeros_

casadi_int casadi::CodeGenerator::sz_zeros_

Definition at line 1158 of file code_generator.hpp.

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

◆ thread_safe_

bool casadi::CodeGenerator::thread_safe_

Definition at line 1131 of file code_generator.hpp.

Referenced by CodeGenerator().

◆ unroll_args

bool casadi::CodeGenerator::unroll_args

Definition at line 1078 of file code_generator.hpp.

Referenced by add(), and CodeGenerator().

◆ verbose

bool casadi::CodeGenerator::verbose

Definition at line 1087 of file code_generator.hpp.

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

◆ verbose_runtime

bool casadi::CodeGenerator::verbose_runtime

Definition at line 1090 of file code_generator.hpp.

Referenced by CodeGenerator(), and sanitize_source().

◆ with_export

bool casadi::CodeGenerator::with_export

Definition at line 1115 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 1115 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 1074 of file code_generator.hpp.

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


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