@@ -25,21 +25,19 @@ using systems::System;
25
25
// In the benchmark case instantiations at the bottom of this file, we'll use
26
26
// a bitmask for the case's "Arg" to denote which quantities are in scope as
27
27
// either gradients (for T=AutoDiffXd) or variables (for T=Expression).
28
- constexpr int kWantNoGrad = 0x0 ;
29
- constexpr int kWantGradQ = 0x1 ;
30
- constexpr int kWantGradV = 0x2 ;
31
- constexpr int kWantGradX = kWantGradQ | kWantGradV ;
28
+ constexpr int kWantNoGrad = 0x0 ;
29
+ constexpr int kWantGradQ = 0x1 ;
30
+ constexpr int kWantGradV = 0x2 ;
31
+ constexpr int kWantGradX = kWantGradQ | kWantGradV ;
32
32
constexpr int kWantGradVdot = 0x4 ;
33
- constexpr int kWantGradU = 0x8 ;
33
+ constexpr int kWantGradU = 0x8 ;
34
34
35
35
// Fixture that holds a Cassie robot model and offers helper functions to
36
36
// configure the benchmark case.
37
37
template <typename T>
38
38
class Cassie : public benchmark ::Fixture {
39
39
public:
40
- Cassie () {
41
- tools::performance::AddMinMaxStatistics (this );
42
- }
40
+ Cassie () { tools::performance::AddMinMaxStatistics (this ); }
43
41
44
42
void SetUp (benchmark::State& state) override {
45
43
SetUpNonZeroState ();
@@ -89,24 +87,28 @@ class Cassie : public benchmark::Fixture {
89
87
// those would get computed once and re-used (like in real applications) but
90
88
// with caching off they would get recalculated repeatedly, affecting the
91
89
// timing results.
92
- void InvalidateInput () {
93
- input_.GetMutableData ();
94
- }
95
- void InvalidateState () {
96
- context_->NoteContinuousStateChange ();
97
- }
90
+ void InvalidateInput () { input_.GetMutableData (); }
91
+ void InvalidateState () { context_->NoteContinuousStateChange (); }
98
92
99
93
// Runs the PositionKinematics benchmark.
100
94
// NOLINTNEXTLINE(runtime/references)
101
95
void DoPositionKinematics (benchmark::State& state) {
102
96
DRAKE_DEMAND (want_grad_vdot (state) == false );
103
97
DRAKE_DEMAND (want_grad_u (state) == false );
104
- // A distal body. Asking for pose of one body calculates poses of all
105
- // of them in the PositionKinematicsCache.
106
- const RigidBody<T>& toe_right = plant_->GetBodyByName (" toe_right" );
107
98
for (auto _ : state) {
108
99
InvalidateState ();
109
- plant_->EvalBodyPoseInWorld (*context_, toe_right);
100
+ plant_->EvalPositionKinematics (*context_);
101
+ }
102
+ }
103
+
104
+ // Runs the CompositeBodyInertiaInWorld benchmark.
105
+ // NOLINTNEXTLINE(runtime/references)
106
+ void DoCompositeBodyInertiaInWorld (benchmark::State& state) {
107
+ DRAKE_DEMAND (want_grad_vdot (state) == false );
108
+ DRAKE_DEMAND (want_grad_u (state) == false );
109
+ for (auto _ : state) {
110
+ InvalidateState ();
111
+ plant_->EvalCompositeBodyInertiaInWorldCache (*context_);
110
112
}
111
113
}
112
114
@@ -115,13 +117,21 @@ class Cassie : public benchmark::Fixture {
115
117
void DoPosAndVelKinematics (benchmark::State& state) {
116
118
DRAKE_DEMAND (want_grad_vdot (state) == false );
117
119
DRAKE_DEMAND (want_grad_u (state) == false );
118
- // A distal body. Asking for kinematics of one body calculates it for all
119
- // of them in the PositionKinematicsCache and VelocityKinematicsCache.
120
- const RigidBody<T>& toe_right = plant_->GetBodyByName (" toe_right" );
121
120
for (auto _ : state) {
122
121
InvalidateState ();
123
122
// This requires both position and velocity kinematics.
124
- plant_->EvalBodySpatialVelocityInWorld (*context_, toe_right);
123
+ plant_->EvalVelocityKinematics (*context_);
124
+ }
125
+ }
126
+
127
+ // Runs the MassMatrixViaID benchmark.
128
+ // NOLINTNEXTLINE(runtime/references)
129
+ void DoMassMatrixViaID (benchmark::State& state) {
130
+ DRAKE_DEMAND (want_grad_vdot (state) == false );
131
+ DRAKE_DEMAND (want_grad_u (state) == false );
132
+ for (auto _ : state) {
133
+ InvalidateState ();
134
+ plant_->CalcMassMatrixViaInverseDynamics (*context_, &mass_matrix_out_);
125
135
}
126
136
}
127
137
@@ -201,8 +211,8 @@ void Cassie<T>::SetUpNonZeroState() {
201
211
VectorX<T>::LinSpaced (nq_ + nv_, 0.1 , 0.9 ));
202
212
for (const BodyIndex& index : plant_->GetFloatingBaseBodies ()) {
203
213
const RigidBody<T>& body = plant_->get_body (index);
204
- const RigidTransform<T> pose (
205
- RollPitchYaw<T>( 0.1 , 0.2 , 0.3 ), Vector3<T>(0.4 , 0.5 , 0.6 ));
214
+ const RigidTransform<T> pose (RollPitchYaw<T>( 0.1 , 0.2 , 0.3 ),
215
+ Vector3<T>(0.4 , 0.5 , 0.6 ));
206
216
plant_->SetFreeBodyPose (context_.get (), body, pose);
207
217
}
208
218
@@ -303,6 +313,15 @@ BENCHMARK_REGISTER_F(CassieDouble, PositionKinematics)
303
313
->Unit (benchmark::kMicrosecond )
304
314
->Arg(kWantNoGrad );
305
315
316
+ BENCHMARK_DEFINE_F (CassieDouble, CompositeBodyInertiaInWorld)
317
+ // NOLINTNEXTLINE(runtime/references)
318
+ (benchmark::State& state) {
319
+ DoCompositeBodyInertiaInWorld (state);
320
+ }
321
+ BENCHMARK_REGISTER_F (CassieDouble, CompositeBodyInertiaInWorld)
322
+ ->Unit (benchmark::kMicrosecond )
323
+ ->Arg(kWantNoGrad );
324
+
306
325
// NOLINTNEXTLINE(runtime/references)
307
326
BENCHMARK_DEFINE_F (CassieDouble, PosAndVelKinematics)(benchmark::State& state) {
308
327
DoPosAndVelKinematics (state);
@@ -311,29 +330,37 @@ BENCHMARK_REGISTER_F(CassieDouble, PosAndVelKinematics)
311
330
->Unit (benchmark::kMicrosecond )
312
331
->Arg(kWantNoGrad );
313
332
333
+ // NOLINTNEXTLINE(runtime/references)
334
+ BENCHMARK_DEFINE_F (CassieDouble, MassMatrixViaID)(benchmark::State& state) {
335
+ DoMassMatrixViaID (state);
336
+ }
337
+ BENCHMARK_REGISTER_F (CassieDouble, MassMatrixViaID)
338
+ ->Unit (benchmark::kMicrosecond )
339
+ ->Arg(kWantNoGrad );
340
+
314
341
// NOLINTNEXTLINE(runtime/references)
315
342
BENCHMARK_DEFINE_F (CassieDouble, MassMatrix)(benchmark::State& state) {
316
343
DoMassMatrix (state);
317
344
}
318
345
BENCHMARK_REGISTER_F (CassieDouble, MassMatrix)
319
- ->Unit (benchmark::kMicrosecond )
320
- ->Arg(kWantNoGrad );
346
+ ->Unit (benchmark::kMicrosecond )
347
+ ->Arg(kWantNoGrad );
321
348
322
349
// NOLINTNEXTLINE(runtime/references)
323
350
BENCHMARK_DEFINE_F (CassieDouble, InverseDynamics)(benchmark::State& state) {
324
351
DoInverseDynamics (state);
325
352
}
326
353
BENCHMARK_REGISTER_F (CassieDouble, InverseDynamics)
327
- ->Unit (benchmark::kMicrosecond )
328
- ->Arg(kWantNoGrad );
354
+ ->Unit (benchmark::kMicrosecond )
355
+ ->Arg(kWantNoGrad );
329
356
330
357
// NOLINTNEXTLINE(runtime/references)
331
358
BENCHMARK_DEFINE_F (CassieDouble, ForwardDynamics)(benchmark::State& state) {
332
359
DoForwardDynamics (state);
333
360
}
334
361
BENCHMARK_REGISTER_F (CassieDouble, ForwardDynamics)
335
- ->Unit (benchmark::kMicrosecond )
336
- ->Arg(kWantNoGrad );
362
+ ->Unit (benchmark::kMicrosecond )
363
+ ->Arg(kWantNoGrad );
337
364
338
365
BENCHMARK_DEFINE_F (CassieAutoDiff, PositionKinematics)
339
366
// NOLINTNEXTLINE(runtime/references)
@@ -362,41 +389,41 @@ BENCHMARK_DEFINE_F(CassieAutoDiff, MassMatrix)(benchmark::State& state) {
362
389
DoMassMatrix (state);
363
390
}
364
391
BENCHMARK_REGISTER_F (CassieAutoDiff, MassMatrix)
365
- ->Unit (benchmark::kMicrosecond )
366
- ->Arg(kWantNoGrad )
367
- ->Arg(kWantGradQ )
368
- ->Arg(kWantGradV )
369
- ->Arg(kWantGradX );
392
+ ->Unit (benchmark::kMicrosecond )
393
+ ->Arg(kWantNoGrad )
394
+ ->Arg(kWantGradQ )
395
+ ->Arg(kWantGradV )
396
+ ->Arg(kWantGradX );
370
397
371
398
// NOLINTNEXTLINE(runtime/references)
372
399
BENCHMARK_DEFINE_F (CassieAutoDiff, InverseDynamics)(benchmark::State& state) {
373
400
DoInverseDynamics (state);
374
401
}
375
402
BENCHMARK_REGISTER_F (CassieAutoDiff, InverseDynamics)
376
- ->Unit (benchmark::kMicrosecond )
377
- ->Arg(kWantNoGrad )
378
- ->Arg(kWantGradQ )
379
- ->Arg(kWantGradV )
380
- ->Arg(kWantGradX )
381
- ->Arg(kWantGradVdot )
382
- ->Arg(kWantGradQ | kWantGradVdot )
383
- ->Arg(kWantGradV | kWantGradVdot )
384
- ->Arg(kWantGradX | kWantGradVdot );
403
+ ->Unit (benchmark::kMicrosecond )
404
+ ->Arg(kWantNoGrad )
405
+ ->Arg(kWantGradQ )
406
+ ->Arg(kWantGradV )
407
+ ->Arg(kWantGradX )
408
+ ->Arg(kWantGradVdot )
409
+ ->Arg(kWantGradQ | kWantGradVdot )
410
+ ->Arg(kWantGradV | kWantGradVdot )
411
+ ->Arg(kWantGradX | kWantGradVdot );
385
412
386
413
// NOLINTNEXTLINE(runtime/references)
387
414
BENCHMARK_DEFINE_F (CassieAutoDiff, ForwardDynamics)(benchmark::State& state) {
388
415
DoForwardDynamics (state);
389
416
}
390
417
BENCHMARK_REGISTER_F (CassieAutoDiff, ForwardDynamics)
391
- ->Unit (benchmark::kMicrosecond )
392
- ->Arg(kWantNoGrad )
393
- ->Arg(kWantGradQ )
394
- ->Arg(kWantGradV )
395
- ->Arg(kWantGradX )
396
- ->Arg(kWantGradU )
397
- ->Arg(kWantGradQ | kWantGradU )
398
- ->Arg(kWantGradV | kWantGradU )
399
- ->Arg(kWantGradX | kWantGradU );
418
+ ->Unit (benchmark::kMicrosecond )
419
+ ->Arg(kWantNoGrad )
420
+ ->Arg(kWantGradQ )
421
+ ->Arg(kWantGradV )
422
+ ->Arg(kWantGradX )
423
+ ->Arg(kWantGradU )
424
+ ->Arg(kWantGradQ | kWantGradU )
425
+ ->Arg(kWantGradV | kWantGradU )
426
+ ->Arg(kWantGradX | kWantGradU );
400
427
401
428
BENCHMARK_DEFINE_F (CassieExpression, PositionKinematics)
402
429
// NOLINTNEXTLINE(runtime/references)
@@ -425,38 +452,38 @@ BENCHMARK_DEFINE_F(CassieExpression, MassMatrix)(benchmark::State& state) {
425
452
DoMassMatrix (state);
426
453
}
427
454
BENCHMARK_REGISTER_F (CassieExpression, MassMatrix)
428
- ->Unit (benchmark::kMicrosecond )
429
- ->Arg(kWantNoGrad )
430
- ->Arg(kWantGradQ )
431
- ->Arg(kWantGradV )
432
- ->Arg(kWantGradX );
455
+ ->Unit (benchmark::kMicrosecond )
456
+ ->Arg(kWantNoGrad )
457
+ ->Arg(kWantGradQ )
458
+ ->Arg(kWantGradV )
459
+ ->Arg(kWantGradX );
433
460
434
461
// NOLINTNEXTLINE(runtime/references)
435
462
BENCHMARK_DEFINE_F (CassieExpression, InverseDynamics)(benchmark::State& state) {
436
463
DoInverseDynamics (state);
437
464
}
438
465
BENCHMARK_REGISTER_F (CassieExpression, InverseDynamics)
439
- ->Unit (benchmark::kMicrosecond )
440
- ->Arg(kWantNoGrad )
441
- ->Arg(kWantGradQ )
442
- ->Arg(kWantGradV )
443
- ->Arg(kWantGradX )
444
- ->Arg(kWantGradVdot )
445
- ->Arg(kWantGradQ | kWantGradVdot )
446
- ->Arg(kWantGradV | kWantGradVdot )
447
- ->Arg(kWantGradX | kWantGradVdot );
466
+ ->Unit (benchmark::kMicrosecond )
467
+ ->Arg(kWantNoGrad )
468
+ ->Arg(kWantGradQ )
469
+ ->Arg(kWantGradV )
470
+ ->Arg(kWantGradX )
471
+ ->Arg(kWantGradVdot )
472
+ ->Arg(kWantGradQ | kWantGradVdot )
473
+ ->Arg(kWantGradV | kWantGradVdot )
474
+ ->Arg(kWantGradX | kWantGradVdot );
448
475
449
476
// NOLINTNEXTLINE(runtime/references)
450
477
BENCHMARK_DEFINE_F (CassieExpression, ForwardDynamics)(benchmark::State& state) {
451
478
DoForwardDynamics (state);
452
479
}
453
480
BENCHMARK_REGISTER_F (CassieExpression, ForwardDynamics)
454
- ->Unit (benchmark::kMicrosecond )
455
- ->Arg(kWantNoGrad )
456
- // N.B. MbP does not support forward dynamics with Variables in 'q'.
457
- ->Arg(kWantGradV )
458
- ->Arg(kWantGradU )
459
- ->Arg(kWantGradV | kWantGradU );
481
+ ->Unit (benchmark::kMicrosecond )
482
+ ->Arg(kWantNoGrad )
483
+ // N.B. MbP does not support forward dynamics with Variables in 'q'.
484
+ ->Arg(kWantGradV )
485
+ ->Arg(kWantGradU )
486
+ ->Arg(kWantGradV | kWantGradU );
460
487
461
488
} // namespace
462
489
} // namespace multibody
0 commit comments