timings-parallel.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2021 INRIA
3 //
4 
5 #include "pinocchio/algorithm/parallel/rnea.hpp"
6 #include "pinocchio/algorithm/parallel/aba.hpp"
7 #include "pinocchio/algorithm/joint-configuration.hpp"
8 #include "pinocchio/parsers/sample-models.hpp"
9 
10 #ifdef PINOCCHIO_WITH_HPP_FCL
11  #include "pinocchio/algorithm/parallel/geometry.hpp"
12 #endif
13 
14 #include "pinocchio/parsers/urdf.hpp"
15 #include "pinocchio/parsers/srdf.hpp"
16 #include "pinocchio/parsers/sample-models.hpp"
17 
18 #include <iostream>
19 
20 #include "pinocchio/utils/timer.hpp"
21 
22 #include <Eigen/StdVector>
23 EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Eigen::VectorXd)
24 
25 int main(int /*argc*/, const char ** /*argv*/)
26 {
27  using namespace Eigen;
28  using namespace pinocchio;
29 
30  typedef Eigen::Matrix<bool,Eigen::Dynamic,1> VectorXb;
31 // typedef Eigen::Matrix<bool,Eigen::Dynamic,Eigen::Dynamic> MatrixXb;
32 
34  #ifdef NDEBUG
35  const int NBT = 4000;
36 
37  #else
38  const int NBT = 1;
39  std::cout << "(the time score in debug mode is not relevant) " << std::endl;
40  #endif
41 
42  const int BATCH_SIZE = 256;
43  const int NUM_THREADS = omp_get_max_threads();
44 
45  const std::string filename = PINOCCHIO_MODEL_DIR + std::string("/example-robot-data/robots/talos_data/robots/talos_reduced.urdf");
46 
49 
50  std::cout << "nq = " << model.nq << std::endl;
51  std::cout << "nv = " << model.nv << std::endl;
52  std::cout << "name = " << model.name << std::endl;
53 
54 #ifdef PINOCCHIO_WITH_HPP_FCL
55  const std::string package_path = PINOCCHIO_MODEL_DIR;
56  hpp::fcl::MeshLoaderPtr mesh_loader = boost::make_shared<hpp::fcl::CachedMeshLoader>();
57  const std::string srdf_filename = PINOCCHIO_MODEL_DIR + std::string("/example-robot-data/robots/talos_data/srdf/talos.srdf");
58  std::vector<std::string> package_paths(1,package_path);
59  pinocchio::GeometryModel geometry_model;
60  pinocchio::urdf::buildGeom(model,filename,COLLISION,geometry_model,package_paths,mesh_loader);
61 
62  geometry_model.addAllCollisionPairs();
63  pinocchio::srdf::removeCollisionPairs(model,geometry_model,srdf_filename,false);
64 
65  GeometryData geometry_data(geometry_model);
66  {
67  int num_active_collision_pairs = 0;
68  for(size_t k = 0; k < geometry_data.activeCollisionPairs.size(); ++k)
69  {
70  if(geometry_data.activeCollisionPairs[k])
71  num_active_collision_pairs++;
72  }
73  std::cout << "active collision pairs = " << num_active_collision_pairs << std::endl;
74  }
75 #endif
76 
77  std::cout << "--" << std::endl;
78  std::cout << "NUM_THREADS: " << NUM_THREADS << std::endl;
79  std::cout << "BATCH_SIZE: " << BATCH_SIZE << std::endl;
80  std::cout << "--" << std::endl;
81 
82  pinocchio::Data data(model);
83  const VectorXd qmax = Eigen::VectorXd::Ones(model.nq);
84 
85  MatrixXd qs(model.nq,BATCH_SIZE);
86  MatrixXd vs(model.nv,BATCH_SIZE);
87  MatrixXd as(model.nv,BATCH_SIZE);
88  MatrixXd taus(model.nv,BATCH_SIZE);
89  MatrixXd res(model.nv,BATCH_SIZE);
90 
91 // VectorXb cols()
92 
94  for(size_t i=0; i < NBT; ++i)
95  {
96  q_vec[i] = randomConfiguration(model,-qmax,qmax);
97  }
98 
99  for(Eigen::DenseIndex i=0; i < BATCH_SIZE; ++i)
100  {
101  qs.col(i) = randomConfiguration(model,-qmax,qmax);
102  vs.col(i) = Eigen::VectorXd::Random(model.nv);
103  as.col(i) = Eigen::VectorXd::Random(model.nv);
104  taus.col(i) = Eigen::VectorXd::Random(model.nv);
105  }
106 
107  ModelPool pool(model,NUM_THREADS);
108 
109  timer.tic();
110  SMOOTH(NBT)
111  {
112  for(Eigen::DenseIndex i = 0; i < BATCH_SIZE; ++i)
113  res.col(i) = rnea(model,data,qs.col(i),vs.col(i),as.col(i));
114  }
115  std::cout << "mean RNEA = \t\t\t\t"; timer.toc(std::cout,NBT*BATCH_SIZE);
116 
117  for(int num_threads = 1; num_threads <= NUM_THREADS; ++num_threads)
118  {
119  timer.tic();
120  SMOOTH(NBT)
121  {
122  rnea(num_threads,pool,qs,vs,as,res);
123  }
124  double elapsed = timer.toc()/(NBT*BATCH_SIZE);
125  std::stringstream ss;
126  ss << "mean RNEA pool (";
127  ss << num_threads;
128  ss << " threads) = \t\t";
129  ss << elapsed << " us" << std::endl;
130  std::cout << ss.str();
131  }
132 
133  std::cout << "--" << std::endl;
134 
135  timer.tic();
136  SMOOTH(NBT)
137  {
138  for(Eigen::DenseIndex i = 0; i < BATCH_SIZE; ++i)
139  res.col(i) = aba(model,data,qs.col(i),vs.col(i),taus.col(i));
140  }
141  std::cout << "mean ABA = \t\t\t\t"; timer.toc(std::cout,NBT*BATCH_SIZE);
142 
143  for(int num_threads = 1; num_threads <= NUM_THREADS; ++num_threads)
144  {
145  timer.tic();
146  SMOOTH(NBT)
147  {
148  aba(num_threads,pool,qs,vs,taus,res);
149  }
150  double elapsed = timer.toc()/(NBT*BATCH_SIZE);
151  std::stringstream ss;
152  ss << "mean ABA pool (";
153  ss << num_threads;
154  ss << " threads) = \t\t";
155  ss << elapsed << " us" << std::endl;
156  std::cout << ss.str();
157  }
158 
159 #ifdef PINOCCHIO_WITH_HPP_FCL
160  std::cout << "--" << std::endl;
161  const int NBT_COLLISION = math::max(NBT,1);
162  timer.tic();
163  SMOOTH((size_t)NBT_COLLISION)
164  {
165  computeCollisions(model,data,geometry_model,geometry_data,q_vec[_smooth]);
166  }
167  std::cout << "non parallel collision = \t\t\t"; timer.toc(std::cout,NBT_COLLISION);
168 
169  for(int num_threads = 1; num_threads <= NUM_THREADS; ++num_threads)
170  {
171  timer.tic();
172  SMOOTH((size_t)NBT_COLLISION)
173  {
174  computeCollisions(num_threads,model,data,geometry_model,geometry_data,q_vec[_smooth]);
175  }
176  double elapsed = timer.toc()/(NBT_COLLISION);
177  std::stringstream ss;
178  ss << "parallel collision (";
179  ss << num_threads;
180  ss << " threads) = \t\t";
181  ss << elapsed << " us" << std::endl;
182  std::cout << ss.str();
183  }
184 
185  std::cout << "--" << std::endl;
186  GeometryPool geometry_pool(model,geometry_model,NUM_THREADS);
187  VectorXb collision_res(BATCH_SIZE);
188  collision_res.fill(false);
189 
190  timer.tic();
191  SMOOTH((size_t)(NBT_COLLISION/BATCH_SIZE))
192  {
193  for(Eigen::DenseIndex i = 0; i < BATCH_SIZE; ++i)
194  computeCollisions(model,data,geometry_model,geometry_data,qs.col(i));
195  }
196  std::cout << "non parallel collision = \t\t\t"; timer.toc(std::cout,NBT_COLLISION);
197 
198  for(int num_threads = 1; num_threads <= NUM_THREADS; ++num_threads)
199  {
200  timer.tic();
201  SMOOTH((size_t)(NBT_COLLISION/BATCH_SIZE))
202  {
203  computeCollisions(num_threads,geometry_pool,qs,collision_res);
204  }
205  double elapsed = timer.toc()/(NBT_COLLISION);
206  std::stringstream ss;
207  ss << "pool parallel collision (";
208  ss << num_threads;
209  ss << " threads) = \t\t";
210  ss << elapsed << " us" << std::endl;
211  std::cout << ss.str();
212  }
213 #endif
214 
215  std::cout << "--" << std::endl;
216  return 0;
217 }
const DataTpl< Scalar, Options, JointCollectionTpl >::TangentVectorType & aba(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 > &tau)
The Articulated-Body algorithm. It computes the forward dynamics, aka the joint accelerations given t...
int main(int, const char **)
double toc()
Definition: timer.hpp:68
void removeCollisionPairs(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, GeometryModel &geom_model, const std::string &filename, const bool verbose=false)
Deactive all possible collision pairs mentioned in the SRDF file. It throws if the SRDF file is incor...
void rnea(const int num_threads, ModelPoolTpl< Scalar, Options, JointCollectionTpl > &pool, const Eigen::MatrixBase< ConfigVectorPool > &q, const Eigen::MatrixBase< TangentVectorPool1 > &v, const Eigen::MatrixBase< TangentVectorPool2 > &a, const Eigen::MatrixBase< TangentVectorPool3 > &tau)
The Recursive Newton-Euler algorithm. It computes the inverse dynamics, aka the joint torques accordi...
AD< Scalar > max(const AD< Scalar > &x, const AD< Scalar > &y)
std::vector< bool > activeCollisionPairs
Vector of collision pairs.
void randomConfiguration(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const Eigen::MatrixBase< ConfigVectorIn1 > &lowerLimits, const Eigen::MatrixBase< ConfigVectorIn2 > &upperLimits, const Eigen::MatrixBase< ReturnType > &qout)
Generate a configuration vector uniformly sampled among provided limits.
data
#define PINOCCHIO_MODEL_DIR
void tic()
Definition: timer.hpp:63
bool computeCollisions(const int num_threads, const GeometryModel &geom_model, GeometryData &geom_data, const bool stopAtFirstCollision=false)
std::string name
Model name;.
Eigen::Matrix< bool, Eigen::Dynamic, 1 > VectorXb
#define PINOCCHIO_ALIGNED_STD_VECTOR(Type)
int BATCH_SIZE
Definition: continuous.py:34
#define SMOOTH(s)
Definition: timer.hpp:38
ModelTpl< Scalar, Options, JointCollectionTpl > & buildModel(const std::string &filename, const typename ModelTpl< Scalar, Options, JointCollectionTpl >::JointModel &rootJoint, ModelTpl< Scalar, Options, JointCollectionTpl > &model, const bool verbose=false)
Build the model from a URDF file with a particular joint as root of the model tree inside the model g...
GeometryModel & buildGeom(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const std::string &filename, const GeometryType type, GeometryModel &geom_model, const std::vector< std::string > &package_paths=std::vector< std::string >(),::hpp::fcl::MeshLoaderPtr mesh_loader=::hpp::fcl::MeshLoaderPtr())
Build The GeometryModel from a URDF file. Search for meshes in the directories specified by the user ...
void addAllCollisionPairs()
Add all possible collision pairs.
string srdf_filename
Definition: collisions.py:25
Main pinocchio namespace.
Definition: timings.cpp:30
res
int nv
Dimension of the velocity vector space.
JointModelFreeFlyerTpl< double > JointModelFreeFlyer
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > VectorXd
Definition: conversions.cpp:14
int nq
Dimension of the configuration vector representation.


pinocchio
Author(s):
autogenerated on Tue Jun 1 2021 02:45:05