code-generator-base.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018-2020 CNRS INRIA
3 //
4 
5 #ifndef __pinocchio_utils_code_generator_base_hpp__
6 #define __pinocchio_utils_code_generator_base_hpp__
7 
9 
12 
13 namespace pinocchio
14 {
15 
16  template<typename _Scalar>
17  struct CodeGenBase
18  {
19  typedef _Scalar Scalar;
20  typedef CppAD::cg::CG<Scalar> CGScalar;
21  typedef CppAD::AD<CGScalar> ADScalar;
22 
23  enum
24  {
25  Options = 0
26  };
27 
34 
35  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Options> MatrixXs;
36  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, Options> VectorXs;
37  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Options | Eigen::RowMajor>
39  typedef Eigen::Matrix<ADScalar, Eigen::Dynamic, 1, Options> ADVectorXs;
40  typedef Eigen::Matrix<ADScalar, Eigen::Dynamic, Eigen::Dynamic, Options> ADMatrixXs;
41 
44 
47 
48  typedef CppAD::ADFun<CGScalar> ADFun;
49 
51  const Model & model,
52  const Eigen::DenseIndex dim_input,
53  const Eigen::DenseIndex dim_output,
54  const std::string & function_name,
55  const std::string & library_name)
56  : ad_model(model.template cast<ADScalar>())
57  , ad_data(ad_model)
60  , build_forward(true)
61  , build_jacobian(true)
62  {
63  ad_X = ADVectorXs(dim_input);
64  ad_Y = ADVectorXs(dim_output);
65 
66  y = VectorXs(ad_Y.size());
67 
68  jac = RowMatrixXs(ad_Y.size(), ad_X.size());
69  }
70 
71  virtual ~CodeGenBase()
72  {
73  }
74 
76  virtual void buildMap() = 0;
77 
78  void initLib()
79  {
80  buildMap();
81 
82  // generates source code
83  cgen_ptr = std::unique_ptr<CppAD::cg::ModelCSourceGen<Scalar>>(
84  new CppAD::cg::ModelCSourceGen<Scalar>(ad_fun, function_name));
85  cgen_ptr->setCreateForwardZero(build_forward);
86  cgen_ptr->setCreateJacobian(build_jacobian);
87  libcgen_ptr = std::unique_ptr<CppAD::cg::ModelLibraryCSourceGen<Scalar>>(
88  new CppAD::cg::ModelLibraryCSourceGen<Scalar>(*cgen_ptr));
89 
90  dynamicLibManager_ptr = std::unique_ptr<CppAD::cg::DynamicModelLibraryProcessor<Scalar>>(
91  new CppAD::cg::DynamicModelLibraryProcessor<Scalar>(*libcgen_ptr, library_name));
92  }
93 
94  CppAD::cg::ModelCSourceGen<Scalar> & codeGenerator()
95  {
96  return *cgen_ptr;
97  }
98 
99  void compileLib(
100  const std::string & gcc_path = "/usr/bin/gcc", const std::string & compile_options = "-Ofast")
101  {
102  CppAD::cg::GccCompiler<Scalar> compiler(gcc_path);
103  std::vector<std::string> compile_flags = compiler.getCompileFlags();
104  compile_flags[0] = compile_options;
105  compiler.setCompileFlags(compile_flags);
106  dynamicLibManager_ptr->createDynamicLibrary(compiler, false);
107  }
108 
109  bool existLib() const
110  {
111  const std::string filename = dynamicLibManager_ptr->getLibraryName()
112  + CppAD::cg::system::SystemInfo<>::DYNAMIC_LIB_EXTENSION;
113  std::ifstream file(filename.c_str());
114  return file.good();
115  }
116 
117  void compileAndLoadLib(const std::string & gcc_path)
118  {
119  compileLib(gcc_path);
120  loadLib(false);
121  }
122 
123  void loadLib(
124  const bool generate_if_not_exist = true,
125  const std::string & gcc_path = "/usr/bin/gcc",
126  const std::string & compile_options = "-Ofast")
127  {
128  if (!existLib() && generate_if_not_exist)
129  compileLib(gcc_path, compile_options);
130 
131  const auto it = dynamicLibManager_ptr->getOptions().find("dlOpenMode");
132  if (it == dynamicLibManager_ptr->getOptions().end())
133  {
134  dynamicLib_ptr.reset(new CppAD::cg::LinuxDynamicLib<Scalar>(
135  dynamicLibManager_ptr->getLibraryName()
136  + CppAD::cg::system::SystemInfo<>::DYNAMIC_LIB_EXTENSION));
137  }
138  else
139  {
140  int dlOpenMode = std::stoi(it->second);
141  dynamicLib_ptr.reset(new CppAD::cg::LinuxDynamicLib<Scalar>(
142  dynamicLibManager_ptr->getLibraryName()
143  + CppAD::cg::system::SystemInfo<>::DYNAMIC_LIB_EXTENSION,
144  dlOpenMode));
145  }
146 
148  }
149 
150  template<typename Vector>
151  void evalFunction(const Eigen::MatrixBase<Vector> & x)
152  {
153  assert(build_forward);
154 
155  generatedFun_ptr->ForwardZero(PINOCCHIO_EIGEN_CONST_CAST(Vector, x), y);
156  }
157 
158  template<typename Vector>
159  void evalJacobian(const Eigen::MatrixBase<Vector> & x)
160  {
161  assert(build_jacobian);
162 
163  CppAD::cg::ArrayView<const Scalar> x_(
164  PINOCCHIO_EIGEN_CONST_CAST(Vector, x).data(), (size_t)x.size());
165  CppAD::cg::ArrayView<Scalar> jac_(jac.data(), (size_t)jac.size());
166  generatedFun_ptr->Jacobian(x_, jac_);
167  }
168 
170  Eigen::DenseIndex getInputDimension() const
171  {
172  return ad_X.size();
173  }
175  Eigen::DenseIndex getOutputDimension() const
176  {
177  return ad_Y.size();
178  }
179 
180  protected:
183 
185  const std::string function_name;
187  const std::string library_name;
188 
191 
194 
197 
200 
203 
204  std::unique_ptr<CppAD::cg::ModelCSourceGen<Scalar>> cgen_ptr;
205  std::unique_ptr<CppAD::cg::ModelLibraryCSourceGen<Scalar>> libcgen_ptr;
206  std::unique_ptr<CppAD::cg::DynamicModelLibraryProcessor<Scalar>> dynamicLibManager_ptr;
207  std::unique_ptr<CppAD::cg::DynamicLib<Scalar>> dynamicLib_ptr;
208  std::unique_ptr<CppAD::cg::GenericModel<Scalar>> generatedFun_ptr;
209 
210  }; // struct CodeGenBase
211 
212 } // namespace pinocchio
213 
214 #endif // ifndef __pinocchio_utils_code_generator_base_hpp__
pinocchio::CodeGenBase::dynamicLibManager_ptr
std::unique_ptr< CppAD::cg::DynamicModelLibraryProcessor< Scalar > > dynamicLibManager_ptr
Definition: code-generator-base.hpp:206
pinocchio::CodeGenBase::initLib
void initLib()
Definition: code-generator-base.hpp:78
pinocchio::CodeGenBase::~CodeGenBase
virtual ~CodeGenBase()
Definition: code-generator-base.hpp:71
pinocchio::CodeGenBase::y
VectorXs y
Definition: code-generator-base.hpp:201
pinocchio::DataTpl
Definition: context/generic.hpp:25
cppadcg.hpp
pinocchio::CodeGenBase::VectorXs
Eigen::Matrix< Scalar, Eigen::Dynamic, 1, Options > VectorXs
Definition: code-generator-base.hpp:36
pinocchio::CodeGenBase::CGModel
pinocchio::ModelTpl< CGScalar, Options > CGModel
Definition: code-generator-base.hpp:30
pinocchio::CodeGenBase::ad_X
ADVectorXs ad_X
Definition: code-generator-base.hpp:195
PINOCCHIO_EIGEN_CONST_CAST
#define PINOCCHIO_EIGEN_CONST_CAST(TYPE, OBJ)
Macro for an automatic const_cast.
Definition: eigen-macros.hpp:51
pinocchio::CodeGenBase::existLib
bool existLib() const
Definition: code-generator-base.hpp:109
pinocchio::name
std::string name(const LieGroupGenericTpl< LieGroupCollection > &lg)
Visit a LieGroupVariant to get the name of it.
model.hpp
pinocchio::CodeGenBase
Definition: code-generator-base.hpp:17
setup.data
data
Definition: cmake/cython/setup.in.py:48
pinocchio::CodeGenBase::Options
@ Options
Definition: code-generator-base.hpp:25
pinocchio::CodeGenBase::Scalar
_Scalar Scalar
Definition: code-generator-base.hpp:19
pinocchio::CodeGenBase::buildMap
virtual void buildMap()=0
build the mapping Y = f(X)
pinocchio::CodeGenBase::build_forward
bool build_forward
Options to generate or not the source code for the evaluation function.
Definition: code-generator-base.hpp:190
pinocchio::CodeGenBase::ADVectorXs
Eigen::Matrix< ADScalar, Eigen::Dynamic, 1, Options > ADVectorXs
Definition: code-generator-base.hpp:39
pinocchio::CodeGenBase::ad_data
ADData ad_data
Definition: code-generator-base.hpp:182
pinocchio::CodeGenBase::compileLib
void compileLib(const std::string &gcc_path="/usr/bin/gcc", const std::string &compile_options="-Ofast")
Definition: code-generator-base.hpp:99
pinocchio::cast
NewScalar cast(const Scalar &value)
Definition: utils/cast.hpp:13
pinocchio::CodeGenBase::CodeGenBase
CodeGenBase(const Model &model, const Eigen::DenseIndex dim_input, const Eigen::DenseIndex dim_output, const std::string &function_name, const std::string &library_name)
Definition: code-generator-base.hpp:50
pinocchio::CodeGenBase::libcgen_ptr
std::unique_ptr< CppAD::cg::ModelLibraryCSourceGen< Scalar > > libcgen_ptr
Definition: code-generator-base.hpp:205
pinocchio::CodeGenBase::ADFun
CppAD::ADFun< CGScalar > ADFun
Definition: code-generator-base.hpp:48
pinocchio::CodeGenBase::ad_fun
ADFun ad_fun
Definition: code-generator-base.hpp:196
pinocchio::CodeGenBase::loadLib
void loadLib(const bool generate_if_not_exist=true, const std::string &gcc_path="/usr/bin/gcc", const std::string &compile_options="-Ofast")
Definition: code-generator-base.hpp:123
pinocchio::CodeGenBase::ConfigVectorType
Model::ConfigVectorType ConfigVectorType
Definition: code-generator-base.hpp:42
pinocchio::CodeGenBase::ad_q_plus
ADConfigVectorType ad_q_plus
Definition: code-generator-base.hpp:198
pinocchio::ModelTpl::TangentVectorType
VectorXs TangentVectorType
Dense vectorized version of a joint tangent vector (e.g. velocity, acceleration, etc)....
Definition: multibody/model.hpp:95
pinocchio::CodeGenBase::generatedFun_ptr
std::unique_ptr< CppAD::cg::GenericModel< Scalar > > generatedFun_ptr
Definition: code-generator-base.hpp:208
pinocchio::CodeGenBase::ADScalar
CppAD::AD< CGScalar > ADScalar
Definition: code-generator-base.hpp:21
filename
filename
data.hpp
pinocchio::CodeGenBase::Data
pinocchio::DataTpl< Scalar, Options > Data
Definition: code-generator-base.hpp:29
x
x
pinocchio::CodeGenBase::ADModel
pinocchio::ModelTpl< ADScalar, Options > ADModel
Definition: code-generator-base.hpp:32
pinocchio::CodeGenBase::ad_a
ADTangentVectorType ad_a
Definition: code-generator-base.hpp:199
pinocchio::CodeGenBase::CGScalar
CppAD::cg::CG< Scalar > CGScalar
Definition: code-generator-base.hpp:20
pinocchio::CodeGenBase::function_name
const std::string function_name
Name of the function.
Definition: code-generator-base.hpp:185
pinocchio::CodeGenBase::ADConfigVectorType
ADModel::ConfigVectorType ADConfigVectorType
Definition: code-generator-base.hpp:45
inverse-kinematics-3d.it
int it
Definition: inverse-kinematics-3d.py:17
pinocchio::CodeGenBase::ad_dq
ADTangentVectorType ad_dq
Definition: code-generator-base.hpp:199
pinocchio::CodeGenBase::RowMatrixXs
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic, Options|Eigen::RowMajor > RowMatrixXs
Definition: code-generator-base.hpp:38
pinocchio::CodeGenBase::codeGenerator
CppAD::cg::ModelCSourceGen< Scalar > & codeGenerator()
Definition: code-generator-base.hpp:94
pinocchio::CodeGenBase::build_jacobian
bool build_jacobian
Options to build or not the Jacobian of he function.
Definition: code-generator-base.hpp:193
pinocchio::CodeGenBase::ad_q
ADConfigVectorType ad_q
Definition: code-generator-base.hpp:198
pinocchio::CodeGenBase::TangentVectorType
Model::TangentVectorType TangentVectorType
Definition: code-generator-base.hpp:43
pinocchio::CodeGenBase::compileAndLoadLib
void compileAndLoadLib(const std::string &gcc_path)
Definition: code-generator-base.hpp:117
pinocchio::CodeGenBase::evalJacobian
void evalJacobian(const Eigen::MatrixBase< Vector > &x)
Definition: code-generator-base.hpp:159
pinocchio::CodeGenBase::ad_model
ADModel ad_model
Definition: code-generator-base.hpp:181
pinocchio::CodeGenBase::cgen_ptr
std::unique_ptr< CppAD::cg::ModelCSourceGen< Scalar > > cgen_ptr
Definition: code-generator-base.hpp:204
pinocchio::CodeGenBase::library_name
const std::string library_name
Name of the library.
Definition: code-generator-base.hpp:187
pinocchio::CodeGenBase::ADMatrixXs
Eigen::Matrix< ADScalar, Eigen::Dynamic, Eigen::Dynamic, Options > ADMatrixXs
Definition: code-generator-base.hpp:40
pinocchio::CodeGenBase::ADTangentVectorType
ADModel::TangentVectorType ADTangentVectorType
Definition: code-generator-base.hpp:46
pinocchio::CodeGenBase::ad_Y
ADVectorXs ad_Y
Definition: code-generator-base.hpp:195
pinocchio::CodeGenBase::getOutputDimension
Eigen::DenseIndex getOutputDimension() const
Dimension of the output vector.
Definition: code-generator-base.hpp:175
pinocchio::CodeGenBase::getInputDimension
Eigen::DenseIndex getInputDimension() const
Dimension of the input vector.
Definition: code-generator-base.hpp:170
pinocchio::CodeGenBase::evalFunction
void evalFunction(const Eigen::MatrixBase< Vector > &x)
Definition: code-generator-base.hpp:151
pinocchio::CodeGenBase::Model
pinocchio::ModelTpl< Scalar, Options > Model
Definition: code-generator-base.hpp:28
pinocchio::ModelTpl::ConfigVectorType
VectorXs ConfigVectorType
Dense vectorized version of a joint configuration vector.
Definition: multibody/model.hpp:87
pinocchio::CodeGenBase::jac
RowMatrixXs jac
Definition: code-generator-base.hpp:202
pinocchio::ModelTpl
Definition: context/generic.hpp:20
pinocchio::CodeGenBase::CGData
pinocchio::DataTpl< CGScalar, Options > CGData
Definition: code-generator-base.hpp:31
pinocchio::CodeGenBase::MatrixXs
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic, Options > MatrixXs
Definition: code-generator-base.hpp:35
pinocchio::CodeGenBase::ad_v
ADTangentVectorType ad_v
Definition: code-generator-base.hpp:199
pinocchio::model
JointCollectionTpl & model
Definition: joint-configuration.hpp:1082
pinocchio::CodeGenBase::dynamicLib_ptr
std::unique_ptr< CppAD::cg::DynamicLib< Scalar > > dynamicLib_ptr
Definition: code-generator-base.hpp:207
pinocchio::CodeGenBase::ADData
pinocchio::DataTpl< ADScalar, Options > ADData
Definition: code-generator-base.hpp:33
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27


pinocchio
Author(s):
autogenerated on Sun Dec 22 2024 03:41:08