Skip to content

Commit

Permalink
Merge pull request #129 from alphaville/fix/interface_c_names
Browse files Browse the repository at this point in the history
Solver-specific names in auto-generated C code
  • Loading branch information
alphaville authored Oct 23, 2019
2 parents cef31f8 + b10e806 commit c04604a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 79 deletions.
32 changes: 16 additions & 16 deletions open-codegen/opengen/templates/casadi_memory.h.template
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@
/*
* Cost sizes
*/
#define COST_SZ_ARG {{ cost.sz_arg() }}
#define COST_SZ_IW {{ cost.sz_iw() }}
#define COST_SZ_W {{ cost.sz_w() }}
#define COST_SZ_RES {{ cost.sz_res() }}
#define COST_SZ_ARG_{{ meta.optimizer_name | upper}} {{ cost.sz_arg() }}
#define COST_SZ_IW_{{ meta.optimizer_name | upper}} {{ cost.sz_iw() }}
#define COST_SZ_W_{{ meta.optimizer_name | upper}} {{ cost.sz_w() }}
#define COST_SZ_RES_{{ meta.optimizer_name | upper}} {{ cost.sz_res() }}

/*
* Gradient sizes
*/
#define GRAD_SZ_ARG {{ grad.sz_arg() }}
#define GRAD_SZ_IW {{ grad.sz_iw() }}
#define GRAD_SZ_W {{ grad.sz_w() }}
#define GRAD_SZ_RES {{ grad.sz_res() }}
#define GRAD_SZ_ARG_{{ meta.optimizer_name | upper}} {{ grad.sz_arg() }}
#define GRAD_SZ_IW_{{ meta.optimizer_name | upper}} {{ grad.sz_iw() }}
#define GRAD_SZ_W_{{ meta.optimizer_name | upper}} {{ grad.sz_w() }}
#define GRAD_SZ_RES_{{ meta.optimizer_name | upper}} {{ grad.sz_res() }}

/*
* F1 sizes
*/
#define F1_SZ_ARG {{ f1.sz_arg() }}
#define F1_SZ_IW {{ f1.sz_iw() }}
#define F1_SZ_W {{ f1.sz_w() }}
#define F1_SZ_RES {{ f1.sz_res() }}
#define F1_SZ_ARG_{{ meta.optimizer_name | upper}} {{ f1.sz_arg() }}
#define F1_SZ_IW_{{ meta.optimizer_name | upper}} {{ f1.sz_iw() }}
#define F1_SZ_W_{{ meta.optimizer_name | upper}} {{ f1.sz_w() }}
#define F1_SZ_RES_{{ meta.optimizer_name | upper}} {{ f1.sz_res() }}

/*
* F2 sizes
*/
#define F2_SZ_ARG {{ f2.sz_arg() }}
#define F2_SZ_IW {{ f2.sz_iw() }}
#define F2_SZ_W {{ f2.sz_w() }}
#define F2_SZ_RES {{ f2.sz_res() }}
#define F2_SZ_ARG_{{ meta.optimizer_name | upper}} {{ f2.sz_arg() }}
#define F2_SZ_IW_{{ meta.optimizer_name | upper}} {{ f2.sz_iw() }}
#define F2_SZ_W_{{ meta.optimizer_name | upper}} {{ f2.sz_w() }}
#define F2_SZ_RES_{{ meta.optimizer_name | upper}} {{ f2.sz_res() }}
26 changes: 15 additions & 11 deletions open-codegen/opengen/templates/icasadi_lib.rs.template
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@ const NUM_DECISION_VARIABLES: usize = {{ problem.dim_decision_variables() }};
const NUM_CONSTRAINTS_TYPE_ALM: usize = {{ problem.dim_constraints_aug_lagrangian() or 0 }};

/// Number of penalty constraints (dimension of F2, i.e., n2)
const NUM_CONSTAINTS_TYPE_PENALTY: usize = {{ problem.dim_constraints_penalty() or 0 }};
const NUM_CONSTRAINTS_TYPE_PENALTY: usize = {{ problem.dim_constraints_penalty() or 0 }};

use libc::{c_double, c_int}; // might need to include: c_longlong, c_void

/// C interface (Function API exactly as provided by CasADi)
extern "C" {
fn cost_function(arg: *const *const c_double, casadi_results: *mut *mut c_double) -> c_int;
fn grad_cost_function(arg: *const *const c_double, casadi_results: *mut *mut c_double)
fn cost_function_{{ meta.optimizer_name }}(
arg: *const *const c_double,
casadi_results: *mut *mut c_double) -> c_int;
fn grad_cost_function_{{ meta.optimizer_name }}(
arg: *const *const c_double,
casadi_results: *mut *mut c_double)
-> c_int;
fn mapping_f1_function(
fn mapping_f1_function_{{ meta.optimizer_name }}(
arg: *const *const c_double,
casadi_results: *mut *mut c_double,
) -> c_int;
fn mapping_f2_function(
fn mapping_f2_function_{{ meta.optimizer_name }}(
arg: *const *const c_double,
casadi_results: *mut *mut c_double,
) -> c_int;
Expand Down Expand Up @@ -83,7 +87,7 @@ pub fn cost(u: &[f64], xi: &[f64], static_params: &[f64], cost_value: &mut f64)
let cost = &mut [cost_value as *mut c_double];

unsafe {
cost_function(
cost_function_{{ meta.optimizer_name }}(
arguments.as_ptr(),
cost.as_mut_ptr(),
) as i32
Expand Down Expand Up @@ -128,7 +132,7 @@ pub fn grad(u: &[f64], xi: &[f64], static_params: &[f64], cost_jacobian: &mut [f
let grad = &mut [cost_jacobian.as_mut_ptr()];

unsafe {
grad_cost_function(
grad_cost_function_{{ meta.optimizer_name }}(
arguments.as_ptr(),
grad.as_mut_ptr()
) as i32
Expand Down Expand Up @@ -174,7 +178,7 @@ pub fn mapping_f1(
let constraints = &mut [f1.as_mut_ptr()];

unsafe {
mapping_f1_function(
mapping_f1_function_{{ meta.optimizer_name }}(
arguments.as_ptr(),
constraints.as_mut_ptr()
) as i32
Expand Down Expand Up @@ -210,15 +214,15 @@ pub fn mapping_f2(
"Incompatible dimension of `p`"
);
assert!(
f2.len() == NUM_CONSTAINTS_TYPE_PENALTY || NUM_CONSTAINTS_TYPE_PENALTY == 0,
f2.len() == NUM_CONSTRAINTS_TYPE_PENALTY || NUM_CONSTRAINTS_TYPE_PENALTY == 0,
"Incompatible dimension of `f2` (result)"
);

let arguments = &[u.as_ptr(), static_params.as_ptr()];
let constraints = &mut [f2.as_mut_ptr()];

unsafe {
mapping_f2_function(
mapping_f2_function_{{ meta.optimizer_name }}(
arguments.as_ptr(),
constraints.as_mut_ptr()
) as i32
Expand Down Expand Up @@ -262,7 +266,7 @@ mod tests {
fn tst_f2() {
let u = [0.1; NUM_DECISION_VARIABLES];
let p = [0.1; NUM_STATIC_PARAMETERS];
let mut f2up = [0.0; NUM_CONSTAINTS_TYPE_PENALTY];
let mut f2up = [0.0; NUM_CONSTRAINTS_TYPE_PENALTY];
assert_eq!(0, super::mapping_f2(&u, &p, &mut f2up));
println!("F2(u, p) = {:#?}", f2up);
}
Expand Down
107 changes: 55 additions & 52 deletions open-codegen/opengen/templates/interface.c.template
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,20 @@
#include <stdlib.h>
#include "casadi_memory.h"

#define TRUE 1
#define FALSE 0

/* Number of input variables */
#define NU {{ problem.dim_decision_variables() }}
#define NU_{{ meta.optimizer_name | upper}} {{ problem.dim_decision_variables() }}

/* Number of static parameters */
#define NP {{ problem.dim_parameters() + problem.dim_constraints_penalty() }}
#define NP_{{ meta.optimizer_name | upper}} {{ problem.dim_parameters() + problem.dim_constraints_penalty() }}

/* Dimension of F1 (number of ALM constraints) */
#define N1 {{ problem.dim_constraints_aug_lagrangian() }}
#define N1_{{ meta.optimizer_name | upper}} {{ problem.dim_constraints_aug_lagrangian() }}

/* Dimension of F2 (number of PM constraints) */
#define N2 {{ problem.dim_constraints_penalty() }}
#define N2_{{ meta.optimizer_name | upper}} {{ problem.dim_constraints_penalty() }}

/* Dimension of xi = (c, y) */
#define NXI {% if problem.dim_constraints_aug_lagrangian() + problem.dim_constraints_penalty() > 0 %}{{ 1 + problem.dim_constraints_aug_lagrangian() }}{% else %}0{% endif %}
#define NXI_{{ meta.optimizer_name | upper}} {% if problem.dim_constraints_aug_lagrangian() + problem.dim_constraints_penalty() > 0 %}{{ 1 + problem.dim_constraints_aug_lagrangian() }}{% else %}0{% endif %}

#ifndef casadi_real
#define casadi_real double
Expand Down Expand Up @@ -103,26 +100,26 @@ extern int {{build_config.constraint_penalty_function_name}}(
/*
* Integer workspaces
*/
#if COST_SZ_IW > 0
static casadi_int allocated_i_workspace_cost[COST_SZ_IW]; /* cost (int ) */
#if COST_SZ_IW_{{ meta.optimizer_name | upper}} > 0
static casadi_int allocated_i_workspace_cost[COST_SZ_IW_{{ meta.optimizer_name | upper}}]; /* cost (int ) */
#else
static casadi_int *allocated_i_workspace_cost = NULL;
#endif

#if GRAD_SZ_IW > 0
static casadi_int allocated_i_workspace_grad[GRAD_SZ_IW]; /* grad (int ) */
#if GRAD_SZ_IW_{{ meta.optimizer_name | upper}} > 0
static casadi_int allocated_i_workspace_grad[GRAD_SZ_IW_{{ meta.optimizer_name | upper}}]; /* grad (int ) */
#else
static casadi_int *allocated_i_workspace_grad = NULL;
#endif

#if F1_SZ_IW > 0
static casadi_int allocated_i_workspace_f1[F1_SZ_IW]; /* f1 (int ) */
#if F1_SZ_IW_{{ meta.optimizer_name | upper}} > 0
static casadi_int allocated_i_workspace_f1[F1_SZ_IW_{{ meta.optimizer_name | upper}}]; /* f1 (int ) */
#else
static casadi_int *allocated_i_workspace_f1 = NULL;
#endif

#if F2_SZ_IW > 0
static casadi_int allocated_i_workspace_f2[F2_SZ_IW]; /* f2 (int ) */
#if F2_SZ_IW_{{ meta.optimizer_name | upper}} > 0
static casadi_int allocated_i_workspace_f2[F2_SZ_IW_{{ meta.optimizer_name | upper}}]; /* f2 (int ) */
#else
static casadi_int *allocated_i_workspace_f2 = NULL;
#endif
Expand All @@ -131,56 +128,56 @@ static casadi_int *allocated_i_workspace_f2 = NULL;
/*
* Real workspaces
*/
#if COST_SZ_W > 0
static casadi_real allocated_r_workspace_cost[COST_SZ_W]; /* cost (real) */
#if COST_SZ_W_{{ meta.optimizer_name | upper}} > 0
static casadi_real allocated_r_workspace_cost[COST_SZ_W_{{ meta.optimizer_name | upper}}]; /* cost (real) */
#else
static casadi_real *allocated_r_workspace_cost = NULL;
#endif


#if GRAD_SZ_W > 0
static casadi_real allocated_r_workspace_grad[GRAD_SZ_W]; /* grad (real ) */
#if GRAD_SZ_W_{{ meta.optimizer_name | upper}} > 0
static casadi_real allocated_r_workspace_grad[GRAD_SZ_W_{{ meta.optimizer_name | upper}}]; /* grad (real ) */
#else
static casadi_real *allocated_r_workspace_grad = NULL;
#endif

#if F1_SZ_W > 0
static casadi_real allocated_r_workspace_f1[F1_SZ_W]; /* f1 (real ) */
#if F1_SZ_W_{{ meta.optimizer_name | upper}} > 0
static casadi_real allocated_r_workspace_f1[F1_SZ_W_{{ meta.optimizer_name | upper}}]; /* f1 (real ) */
#else
static casadi_real *allocated_r_workspace_f1 = NULL;
#endif

#if F2_SZ_W > 0
static casadi_real allocated_r_workspace_f2[F2_SZ_W]; /* f2 (real ) */
#if F2_SZ_W_{{ meta.optimizer_name | upper}} > 0
static casadi_real allocated_r_workspace_f2[F2_SZ_W_{{ meta.optimizer_name | upper}}]; /* f2 (real ) */
#else
static casadi_real *allocated_r_workspace_f2 = NULL;
#endif

/*
* Result workspaces
*/
#if COST_SZ_RES > 0
static casadi_real *result_space_cost[COST_SZ_RES ]; /* cost (res ) */
#if COST_SZ_RES_{{ meta.optimizer_name | upper}} > 0
static casadi_real *result_space_cost[COST_SZ_RES_{{ meta.optimizer_name | upper}}]; /* cost (res ) */
#else
static casadi_real **result_space_cost = NULL;
#endif

#if GRAD_SZ_RES > 0
static casadi_real *result_space_grad[GRAD_SZ_RES]; /* grad (res ) */
#if GRAD_SZ_RES_{{ meta.optimizer_name | upper}} > 0
static casadi_real *result_space_grad[GRAD_SZ_RES_{{ meta.optimizer_name | upper}}]; /* grad (res ) */
#else
static casadi_real **result_space_grad = NULL;
#endif


#if F1_SZ_RES > 0
static casadi_real *result_space_f1[F1_SZ_RES]; /* f1 (res ) */
#if F1_SZ_RES_{{ meta.optimizer_name | upper}} > 0
static casadi_real *result_space_f1[F1_SZ_RES_{{ meta.optimizer_name | upper}}]; /* f1 (res ) */
#else
static casadi_real **result_space_f1 = NULL;
#endif


#if F2_SZ_RES > 0
static casadi_real *result_space_f2[F2_SZ_RES]; /* f2 (res ) */
#if F2_SZ_RES_{{ meta.optimizer_name | upper}} > 0
static casadi_real *result_space_f2[F2_SZ_RES_{{ meta.optimizer_name | upper}}]; /* f2 (res ) */
#else
static casadi_real **result_space_f2 = NULL;
#endif
Expand All @@ -198,7 +195,9 @@ static casadi_real **result_space_f2 = NULL;
* | |
* |------------------- uxip_space ------------------------------|
*/
static casadi_real uxip_space[NU+NXI+NP];
static casadi_real uxip_space[NU_{{ meta.optimizer_name | upper}}
+NXI_{{ meta.optimizer_name | upper}}
+NP_{{ meta.optimizer_name | upper}}];

/**
* Copy (u, xi, p) into uxip_space
Expand All @@ -208,26 +207,27 @@ static casadi_real uxip_space[NU+NXI+NP];
*/
static void copy_args_into_uxip_space(const casadi_real** arg) {
int i;
for (i=0; i<NU; i++) uxip_space[i] = arg[0][i]; /* copy u */
for (i=0; i<NXI; i++) uxip_space[NU+i] = arg[1][i]; /* copy xi */
for (i=0; i<NP; i++) uxip_space[NU+NXI+i] = arg[2][i]; /* copy p */
for (i=0; i<NU_{{ meta.optimizer_name | upper}}; i++) uxip_space[i] = arg[0][i]; /* copy u */
for (i=0; i<NXI_{{ meta.optimizer_name | upper}}; i++) uxip_space[NU_{{ meta.optimizer_name | upper}}+i] = arg[1][i]; /* copy xi */
for (i=0; i<NP_{{ meta.optimizer_name | upper}}; i++) uxip_space[NU_{{ meta.optimizer_name | upper}}+NXI_{{ meta.optimizer_name | upper}}+i] = arg[2][i]; /* copy p */
}

/**
* Copy (u, p) into uxip_space
*/
static void copy_args_into_up_space(const casadi_real** arg) {
int i;
for (i=0; i<NU; i++) uxip_space[i] = arg[0][i]; /* copy u */
for (i=0; i<NP; i++) uxip_space[NU+NXI+i] = arg[1][i]; /* copy p */
for (i=0; i<NU_{{ meta.optimizer_name | upper}}; i++) uxip_space[i] = arg[0][i]; /* copy u */
for (i=0; i<NP_{{ meta.optimizer_name | upper}}; i++) uxip_space[NU_{{ meta.optimizer_name | upper}}+NXI_{{ meta.optimizer_name | upper}}+i] = arg[1][i]; /* copy p */
}

/* ------COST------------------------------------------------------------------- */

int cost_function(const casadi_real** arg, casadi_real** res) {
const casadi_real* args__[COST_SZ_ARG] = {uxip_space, /* :u */
uxip_space + NU, /* :xi */
uxip_space + NU + NXI}; /* :p */
int cost_function_{{ meta.optimizer_name }}(const casadi_real** arg, casadi_real** res) {
const casadi_real* args__[COST_SZ_ARG_{{ meta.optimizer_name | upper}}] =
{uxip_space, /* :u */
uxip_space + NU_{{ meta.optimizer_name | upper}}, /* :xi */
uxip_space + NU_{{ meta.optimizer_name | upper}} + NXI_{{ meta.optimizer_name | upper}}}; /* :p */
copy_args_into_uxip_space(arg);

result_space_cost[0] = res[0];
Expand All @@ -242,10 +242,11 @@ int cost_function(const casadi_real** arg, casadi_real** res) {

/* ------GRADIENT--------------------------------------------------------------- */

int grad_cost_function(const casadi_real** arg, casadi_real** res) {
const casadi_real* args__[GRAD_SZ_ARG] = {uxip_space, /* :u */
uxip_space + NU, /* :xi */
uxip_space + NU + NXI}; /* :p */
int grad_cost_function_{{ meta.optimizer_name }}(const casadi_real** arg, casadi_real** res) {
const casadi_real* args__[GRAD_SZ_ARG_{{ meta.optimizer_name | upper}}] =
{ uxip_space, /* :u */
uxip_space + NU_{{ meta.optimizer_name | upper}}, /* :xi */
uxip_space + NU_{{ meta.optimizer_name | upper}} + NXI_{{ meta.optimizer_name | upper}}}; /* :p */
copy_args_into_uxip_space(arg);
result_space_grad[0] = res[0];
return {{build_config.grad_function_name or 'grad_phi'}}(
Expand All @@ -259,10 +260,11 @@ int grad_cost_function(const casadi_real** arg, casadi_real** res) {

/* ------MAPPING F1------------------------------------------------------------- */

int mapping_f1_function(const casadi_real** arg, casadi_real** res) {
int mapping_f1_function_{{ meta.optimizer_name }}(const casadi_real** arg, casadi_real** res) {
/* Array of pointers to where (u, p) are stored */
const casadi_real* args__[F1_SZ_ARG] = {uxip_space, /* :u */
uxip_space + NU + NXI}; /* :p */
const casadi_real* args__[F1_SZ_ARG_{{ meta.optimizer_name | upper}}] =
{uxip_space, /* :u */
uxip_space + NU_{{ meta.optimizer_name | upper}} + NXI_{{ meta.optimizer_name | upper}}}; /* :p */
/* Copy given data to variable `uxip_space` */
copy_args_into_up_space(arg);
/*
Expand All @@ -285,10 +287,11 @@ int mapping_f1_function(const casadi_real** arg, casadi_real** res) {

/* ------MAPPING F2------------------------------------------------------------- */

int mapping_f2_function(const casadi_real** arg, casadi_real** res) {
int mapping_f2_function_{{ meta.optimizer_name }}(const casadi_real** arg, casadi_real** res) {
/* Array of pointers to where (u, p) are stored */
const casadi_real* args__[F2_SZ_ARG] = {uxip_space, /* :u */
uxip_space + NU + NXI}; /* :p */
const casadi_real* args__[F2_SZ_ARG_{{ meta.optimizer_name | upper}}] =
{uxip_space, /* :u */
uxip_space + NU_{{ meta.optimizer_name | upper}} + NXI_{{ meta.optimizer_name | upper}}}; /* :p */
/* Copy given data to variable `uxip_space` */
copy_args_into_up_space(arg);
/*
Expand Down

0 comments on commit c04604a

Please sign in to comment.