robotology / osqp-eigen

Simple Eigen-C++ wrapper for OSQP library
https://robotology.github.io/osqp-eigen/
BSD 3-Clause "New" or "Revised" License
385 stars 114 forks source link

Dynamic allocations #6

Open ahoarau opened 6 years ago

ahoarau commented 6 years ago

I'd like to integrate this library in a Xenomai setup but I'm afraid of dynamic allocations. I see some push_back in the code. I was wondering If we could take advantage of the reserve methods for std::vector<> and eigen sparse matrices toa void any memory allocation between solver updates.

traversaro commented 6 years ago

@GiulioRomualdi What do you think about this? I think it is a good idea.

S-Dafarra commented 6 years ago

Hi @ahoarau, with https://github.com/robotology/osqp-eigen/pull/5 and https://github.com/robotology/osqp-eigen/commit/38825e5fbe0e1716da8aeea5c1a430f7970a5317 (and for the record https://github.com/robotology/osqp-eigen/commit/7109a226f0cc6dd7d01eb912561e9a0e9eb1d57a, my bad :sweat_smile: ) we have been trying to reduce memory allocation by reusing some buffers. Can you check this out? Comments are welcome :wink:

GiulioRomualdi commented 6 years ago

Hi @ahoarau, Remove/reduce memory allocation is, of course, a best practice, so I'll try to follow this objectivity. However, some memory allocations cannot be removed. Indeed osqp itself uses memory allocation. For example, csc_spalloc() method allocates memory dynamically. https://github.com/robotology/osqp-eigen/blob/38825e5fbe0e1716da8aeea5c1a430f7970a5317/include/OsqpEigen/SparseMatrixHelper.tpp#L34 Furthermore, every time a new optimization problem is initialized the following memory allocations are performed https://github.com/oxfordcontrol/osqp/blob/master/src/osqp.c#L111-L118

ahoarau commented 6 years ago

It also says that it is the only place where they perform memory allocations in the solver. Like qpOASES, you need to setup (allocs) and then solve (no allocs). So I believe that createOsqpSparseMatrix would only be called once before the osqp_setup function, and then all the functions called would be to update the said matrices

NUDTUGVexplorer commented 4 years ago

when I run the example MPCExample.cpp it has an error: Segmentation fault (core dumped). Then I debug the program, it shows the error in the following lines.

solver.settings()->setWarmStart(true);

how to solve the issue? Thanks a lot.

ahoarau commented 11 months ago

Hello @GiulioRomualdi and @traversaro , I'm back ! Was this addressed at some point ? Can I run the solver with Eigen::internal::set_malloc_allowed(false) ?

traversaro commented 10 months ago

Hello @GiulioRomualdi and @traversaro , I'm back ! Was this addressed at some point ? Can I run the solver with Eigen::internal::set_malloc_allowed(false) ?

I am doing some profiling on this, and it seems that solveProblem is malloc-free, but several update*** methods are still using mallocs.