stack-of-tasks / pinocchio

A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
http://stack-of-tasks.github.io/pinocchio/
BSD 2-Clause "Simplified" License
1.84k stars 386 forks source link

Memory allocation in computeRNEADerivatives #879

Closed cmastalli closed 5 years ago

cmastalli commented 5 years ago

There is memory allocation when we pass fext. To be efficient, I created the following code where you can check this issue:

#define EIGEN_RUNTIME_NO_MALLOC

#include "pinocchio/multibody/model.hpp"
#include "pinocchio/multibody/data.hpp"
#include "pinocchio/algorithm/joint-configuration.hpp"
#include "pinocchio/algorithm/rnea-derivatives.hpp"
#include "pinocchio/parsers/sample-models.hpp"

using namespace Eigen;
using namespace pinocchio;

using std::cout;
using std::endl;

int main() {
  Model model;
  buildModels::humanoidRandom(model);

  Data data(model);

  model.lowerPositionLimit.head<3>().fill(-1.);
  model.upperPositionLimit.head<3>().fill(1.);

  VectorXd q = randomConfiguration(model);
  VectorXd v(VectorXd::Random(model.nv));
  VectorXd a(VectorXd::Random(model.nv));

  typedef container::aligned_vector<Force> ForceVector;
  ForceVector fext((size_t)model.njoints);
  for(ForceVector::iterator it = fext.begin(); it != fext.end(); ++it)
    (*it).setRandom();

  Eigen::internal::set_is_malloc_allowed(false);
  computeRNEADerivatives(model,data,q,v,a,fext); // this allocates memory
//  computeRNEADerivatives(model,data,q,v,a); // this doesn't allocate memory (yeah!)
  Eigen::internal::set_is_malloc_allowed(true);
}

Remember that you need to compile in debug mode. If you name this file as test_rnea_derivatives.cpp, then this is a suggested compilation command:

g++ -I /usr/include/eigen3/ -I /local/users/cmastall/include/ -g test_rnea_derivatives.cpp -o test_rnea_derivatives
jcarpent commented 5 years ago

Thanks for the report. I will check it now.

By the way, please use this line instead which is much more generic:

 g++ -I $(pkg-config --cflags pinocchio) -g test_rnea_derivatives.cpp -o test_rnea_derivatives
jcarpent commented 5 years ago

I found the bug. I will provide a fix in few minutes. Thanks @cmastalli for your concerns.

cmastalli commented 5 years ago

Awesome!

jcarpent commented 5 years ago

Solved by #880.