Permalink
Browse files

checked ocp_nlp_interface - improved memory allocation & loop

  • Loading branch information...
FreyJo committed Nov 15, 2018
1 parent ca6b27c commit 8bda8bb103c36de614b3a179ce276831b8e77ab2
Showing with 33 additions and 23 deletions.
  1. +6 −0 acados/utils/mem.c
  2. +3 −0 acados/utils/mem.h
  3. +24 −23 interfaces/acados_c/ocp_nlp_interface.c
@@ -59,6 +59,12 @@ void *acados_malloc(size_t nitems, size_t size)
return ptr;
}
void *acados_calloc(size_t nitems, size_t size)
{
void *ptr = calloc(nitems, size);
return ptr;
}
void assign_and_advance_double_ptrs(int n, double ***v, char **ptr)
{
#ifndef WINDOWS_SKIP_PTR_ALIGNMENT_CHECK
@@ -52,6 +52,9 @@ int align_char_to(int num, char **c_ptr);
// switch between malloc and calloc (for valgrinding)
void *acados_malloc(size_t nitems, size_t size);
// uses always calloc
void *acados_calloc(size_t nitems, size_t size);
// allocate vector of pointers to vectors of doubles and advance pointer
void assign_and_advance_double_ptrs(int n, double ***v, char **ptr);
@@ -26,6 +26,7 @@
#include <string.h>
#include <ctype.h>
#include "acados/ocp_nlp/ocp_nlp_common.h"
#include "acados/ocp_nlp/ocp_nlp_cost_external.h"
#include "acados/ocp_nlp/ocp_nlp_cost_ls.h"
#include "acados/ocp_nlp/ocp_nlp_cost_nls.h"
@@ -40,9 +41,10 @@
#include "acados/utils/mem.h"
int ocp_nlp_plan_calculate_size(int N)
/* ocp_nlp_plan */
static int ocp_nlp_plan_calculate_size(int N)
{
// N - number of shooting nodes
int bytes = sizeof(ocp_nlp_solver_plan);
bytes += N * sizeof(sim_solver_plan);
bytes += (N + 1) * sizeof(ocp_nlp_cost_t);
@@ -53,7 +55,7 @@ int ocp_nlp_plan_calculate_size(int N)
ocp_nlp_solver_plan *ocp_nlp_plan_assign(int N, void *raw_memory)
static ocp_nlp_solver_plan *ocp_nlp_plan_assign(int N, void *raw_memory)
{
int ii;
@@ -82,25 +84,24 @@ ocp_nlp_solver_plan *ocp_nlp_plan_assign(int N, void *raw_memory)
for (ii=0; ii <= N; ii++)
plan->nlp_constraints[ii] = 100;
// TODO(all): fix assert
// assert( 0 == 0);
return plan;
}
void ocp_nlp_plan_initialize_default(int N, ocp_nlp_solver_plan *plan)
static void ocp_nlp_plan_initialize_default(int N, ocp_nlp_solver_plan *plan)
{
plan->nlp_solver = SQP;
plan->regularization = NO_REGULARIZATION;
for (int ii = 0; ii <= N; ii++)
{
plan->nlp_cost[ii] = NONLINEAR_LS;
if (ii < N)
{
plan->sim_solver_plan[ii].sim_solver = ERK;
}
}
for (int ii = 0; ii < N; ii++)
{
plan->sim_solver_plan[ii].sim_solver = ERK;
}
}
@@ -119,11 +120,11 @@ ocp_nlp_solver_plan *ocp_nlp_plan_create(int N)
}
/* ocp_nlp_reg - regularization */
static ocp_nlp_reg_config *ocp_nlp_reg_config_create(ocp_nlp_reg_t plan)
{
int size = ocp_nlp_reg_config_calculate_size();
ocp_nlp_reg_config *config = malloc(size);
ocp_nlp_reg_config *config = acados_malloc(1, size);
switch (plan)
{
@@ -138,20 +139,20 @@ static ocp_nlp_reg_config *ocp_nlp_reg_config_create(ocp_nlp_reg_t plan)
ocp_nlp_reg_conv_config_initialize_default(config);
break;
default:
printf("Regularization not available!\n");
printf("Regularization option not available!\n");
exit(1);
}
return config;
}
/* ocp_nlp_solver_config */
// TODO(dimitris): this leaks memory! Either provide free config or calculate size should be nested
ocp_nlp_solver_config *ocp_nlp_config_create(ocp_nlp_solver_plan plan, int N)
{
int bytes = ocp_nlp_solver_config_calculate_size(N);
void *config_mem = calloc(1, bytes);
void *config_mem = acados_calloc(1, bytes);
ocp_nlp_solver_config *config = ocp_nlp_solver_config_assign(N, config_mem);
if (plan.nlp_solver == SQP)
@@ -241,27 +242,27 @@ ocp_nlp_solver_config *ocp_nlp_config_create(ocp_nlp_solver_plan plan, int N)
}
/* ocp_nlp_dims */
ocp_nlp_dims *ocp_nlp_dims_create(void *config_)
{
ocp_nlp_solver_config *config = config_;
int bytes = ocp_nlp_dims_calculate_size(config);
void *ptr = calloc(1, bytes);
void *ptr = acados_calloc(1, bytes);
ocp_nlp_dims *dims = ocp_nlp_dims_assign(config, ptr);
return dims;
}
/* ocp_nlp_in */
ocp_nlp_in *ocp_nlp_in_create(ocp_nlp_solver_config *config, ocp_nlp_dims *dims)
{
int bytes = ocp_nlp_in_calculate_size(config, dims);
void *ptr = calloc(1, bytes);
void *ptr = acados_calloc(1, bytes);
ocp_nlp_in *nlp_in = ocp_nlp_in_assign(config, dims, ptr);
@@ -420,7 +421,7 @@ ocp_nlp_out *ocp_nlp_out_create(ocp_nlp_solver_config *config, ocp_nlp_dims *dim
{
int bytes = ocp_nlp_out_calculate_size(config, dims);
void *ptr = calloc(1, bytes);
void *ptr = acados_calloc(1, bytes);
ocp_nlp_out *nlp_out = ocp_nlp_out_assign(config, dims, ptr);
@@ -437,7 +438,7 @@ void *ocp_nlp_opts_create(ocp_nlp_solver_config *config, ocp_nlp_dims *dims)
{
int bytes = config->opts_calculate_size(config, dims);
void *ptr = calloc(1, bytes);
void *ptr = acados_calloc(1, bytes);
void *opts = config->opts_assign(config, dims, ptr);
@@ -491,7 +492,7 @@ ocp_nlp_solver *ocp_nlp_create(ocp_nlp_solver_config *config, ocp_nlp_dims *dims
int bytes = ocp_nlp_calculate_size(config, dims, opts_);
void *ptr = calloc(1, bytes);
void *ptr = acados_calloc(1, bytes);
ocp_nlp_solver *solver = ocp_nlp_assign(config, dims, opts_, ptr);

0 comments on commit 8bda8bb

Please sign in to comment.