Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit template instantiation code is rarely called #2465

Open
jorisv opened this issue Oct 28, 2024 · 3 comments
Open

Explicit template instantiation code is rarely called #2465

jorisv opened this issue Oct 28, 2024 · 3 comments

Comments

@jorisv
Copy link
Contributor

jorisv commented Oct 28, 2024

When using explicit template instantiation we heavily use Eigen::Ref as Matrix reference to avoid copy.

As an example, here the explicit template instantiation of the ABA function:

  extern template const context::VectorXs & aba<
    context::Scalar,
    context::Options,
    JointCollectionDefaultTpl,
    Eigen::Ref<const context::VectorXs>,
    Eigen::Ref<const context::VectorXs>,
    Eigen::Ref<const context::VectorXs>>(
    const context::Model &,
    context::Data &,
    const Eigen::MatrixBase<Eigen::Ref<const context::VectorXs>> &,
    const Eigen::MatrixBase<Eigen::Ref<const context::VectorXs>> &,
    const Eigen::MatrixBase<Eigen::Ref<const context::VectorXs>> &,
    const Convention);

But we rarely call this function with Eigen::Ref.
Instead, we use to write the following:

  Eigen::VectorXd q = randomConfiguration(model);
  Eigen::VectorXd v = Eigen::VectorXd::Zero(model.nv);
  Eigen::VectorXd tau = Eigen::VectorXd::Zero(model.nv);

  Eigen::VectorXd a = pinocchio::aba(model, data, q, v, tau);

This code will then rebuild the ABA function, with the following signature that mismatch the one explicitly template instantiated:

pinocchio::DataTpl<double, 0, pinocchio::JointCollectionDefaultTpl>::TangentVectorType const& pinocchio::aba<double, 0, pinocchio::JointCollectionDefaultTpl, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(pinocchio::ModelTpl<double, 0, pinocchio::JointCollectionDefaultTpl> const&, pinocchio::DataTpl<double, 0, pinocchio::JointCollectionDefaultTpl>&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, pinocchio::Convention)

We can avoid this issue by forcing the use of Eigen::Ref:

  Eigen::VectorXd a = pinocchio::aba(model, data, pinocchio::make_const_ref(q), pinocchio::make_const_ref(v), pinocchio::make_const_ref(tau));

But it's really easy to call a different signature.

We should find a way to safely call an explicit template instantiation.

This will improve:

  • compilation time
  • binary size
    • Release
      • With ETI: 100ko
      • Without ETI: 500ko
    • Debug
      • With ETI: 5mo
      • Without ETI: 80mo
@jorisv
Copy link
Contributor Author

jorisv commented Oct 28, 2024

Also, this do not happen when calling RNEA. The compiler call the template instantiated code.

@jorisv
Copy link
Contributor Author

jorisv commented Oct 28, 2024

RNEA force the use of Eigen::Ref:

  template<
    typename Scalar,
    int Options,
    template<typename, int> class JointCollectionTpl,
    typename ConfigVectorType,
    typename TangentVectorType1,
    typename TangentVectorType2>
  const typename DataTpl<Scalar, Options, JointCollectionTpl>::TangentVectorType & rnea(
    const ModelTpl<Scalar, Options, JointCollectionTpl> & model,
    DataTpl<Scalar, Options, JointCollectionTpl> & data,
    const Eigen::MatrixBase<ConfigVectorType> & q,
    const Eigen::MatrixBase<TangentVectorType1> & v,
    const Eigen::MatrixBase<TangentVectorType2> & a)
  {
    return impl::rnea(model, data, make_const_ref(q), make_const_ref(v), make_const_ref(a));
  }

@jorisv
Copy link
Contributor Author

jorisv commented Oct 28, 2024

If we find a better solution to use explicit template instantiation, we also should solve this issue: #2257

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant