File: TestMultibodyPerformance.cpp

package info (click to toggle)
simbody 3.7%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 72,896 kB
  • sloc: cpp: 248,827; ansic: 18,240; sh: 29; makefile: 24
file content (619 lines) | stat: -rw-r--r-- 25,275 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/* -------------------------------------------------------------------------- *
 *                               Simbody(tm)                                  *
 * -------------------------------------------------------------------------- *
 * This is part of the SimTK biosimulation toolkit originating from           *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody.  *
 *                                                                            *
 * Portions copyright (c) 2011-15 Stanford University and the Authors.        *
 * Authors: Peter Eastman                                                     *
 * Contributors: Michael Sherman                                              *
 *                                                                            *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License. You may obtain a  *
 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0.         *
 *                                                                            *
 * Unless required by applicable law or agreed to in writing, software        *
 * distributed under the License is distributed on an "AS IS" BASIS,          *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 * -------------------------------------------------------------------------- */

#include "SimTKsimbody.h"
#include <string>
#include <ctime>

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

#ifdef _MSC_VER
#pragma warning(disable:4996) // don't warn about strerror, sprintf, etc.
#endif

using namespace SimTK;

/**
 * This test measures the speed of various multibody calculations.  It executes a collection of operations
 * on various systems.  The CPU time required to perform each operation 1000 times is measured and
 * printed to the console.
 *
 * Each test system contains 256 identical bodies (plus ground), but they differ in the type of
 * bodies and their arrangement into a multibody tree.  The arrangements include 1) all bodies attached
 * directly to ground, 2) the bodies linked in a single chain, and 3) the bodies arranged to form
 * a binary tree.
 */

// The following routines define the operations to be profiled.

void doRealizeTime(MultibodySystem& system, State& state) {
    state.invalidateAllCacheAtOrAbove(Stage::Time);
    system.realize(state, Stage::Time);
}

void doRealizePosition(MultibodySystem& system, State& state) {
    state.invalidateAllCacheAtOrAbove(Stage::Position);
    system.realize(state, Stage::Position);
}

void doRealizeVelocity(MultibodySystem& system, State& state) {
    state.invalidateAllCacheAtOrAbove(Stage::Velocity);
    system.realize(state, Stage::Velocity);
}

void doRealizePositionKinematics(MultibodySystem& system, State& state) {
    const SimbodyMatterSubsystem& matter = system.getMatterSubsystem();
    matter.invalidatePositionKinematics(state);
    matter.realizePositionKinematics(state);
}

void doRealizeVelocityKinematics(MultibodySystem& system, State& state) {
    const SimbodyMatterSubsystem& matter = system.getMatterSubsystem();
    matter.invalidateVelocityKinematics(state);
    matter.realizeVelocityKinematics(state);
}

void doRealizeArticulatedBodyInertias(MultibodySystem& system, State& state) {
    const SimbodyMatterSubsystem& matter = system.getMatterSubsystem();
    matter.invalidateArticulatedBodyInertias(state);
    matter.realizeArticulatedBodyInertias(state);
}

void doRealizeArticulatedBodyVelocity(MultibodySystem& system, State& state) {
    const SimbodyMatterSubsystem& matter = system.getMatterSubsystem();
    matter.invalidateArticulatedBodyVelocity(state);
    matter.realizeArticulatedBodyVelocity(state);
}

void doRealizeDynamics(MultibodySystem& system, State& state) {
    state.invalidateAllCacheAtOrAbove(Stage::Dynamics);
    system.realize(state, Stage::Dynamics);
}

void doRealizeAcceleration(MultibodySystem& system, State& state) {
    state.invalidateAllCacheAtOrAbove(Stage::Acceleration);
    system.realize(state, Stage::Acceleration);
}

// Cost to re-evaluate accelerations after applying some new forces, but leaving
// the state variables unchanged.
void doRealizeDynamics2Acceleration(MultibodySystem& system, State& state) {
    state.invalidateAllCacheAtOrAbove(Stage::Dynamics);
    system.realize(state, Stage::Acceleration);
}

// Cost to re-evaluate accelerations after updating velocities, but leaving
// the positions unchanged (e.g. semi-implicit Euler iterating velocities).
void doRealizeVelocity2Acceleration(MultibodySystem& system, State& state) {
    state.updU(); // should invalidate velocity kinematics
    state.invalidateAllCacheAtOrAbove(Stage::Velocity);
    system.realize(state, Stage::Acceleration);
}

// Cost for a complete acceleration calculation at a new time and state.
// This includes the cost of articulated body inertias.
void doRealizeTime2Acceleration(MultibodySystem& system, State& state) {
    state.updQ(); // should invalidate position & velocity kinematics
    state.invalidateAllCacheAtOrAbove(Stage::Time);
    system.realize(state, Stage::Acceleration);
}

void doMultiplyByM(MultibodySystem& system, State& state) {
    const SimbodyMatterSubsystem& matter = system.getMatterSubsystem();
    Vector v(matter.getNumMobilities(), 1.0);
    Vector mv;
    matter.multiplyByM(state, v, mv);
}

void doMultiplyByMInv(MultibodySystem& system, State& state) {
    const SimbodyMatterSubsystem& matter = system.getMatterSubsystem();
    Vector v(matter.getNumMobilities(), 1.0);
    Vector minvv;
    matter.multiplyByMInv(state, v, minvv);
}

void doCalcResidualForceIgnoringConstraints(MultibodySystem& system, State& state) {
    const SimbodyMatterSubsystem& matter = system.getMatterSubsystem();
    Vector appliedMobilityForces(matter.getNumMobilities(), 1.0);
    Vector_<SpatialVec> appliedBodyForces(matter.getNumBodies(), SpatialVec(Vec3(1, 0, 0), Vec3(0, 1, 0)));
    Vector knownUdot, residualMobilityForces;
    matter.calcResidualForceIgnoringConstraints(state, appliedMobilityForces, appliedBodyForces, knownUdot, residualMobilityForces);
}

void doCalcMobilizerReactionForces(MultibodySystem& system, State& state) {
    Vector_<SpatialVec> forces;
    system.getMatterSubsystem().calcMobilizerReactionForces(state, forces);
}

void doMultiplyBySystemJacobianTranspose(MultibodySystem& system, State& state) {
    const SimbodyMatterSubsystem& matter = system.getMatterSubsystem();
    Vector_<SpatialVec> dEdR(matter.getNumBodies(), SpatialVec(Vec3(1, 0, 0), Vec3(0, 1, 0)));
    Vector dEdQ;
    matter.multiplyBySystemJacobianTranspose(state, dEdR, dEdQ);
}

void doCalcCompositeBodyInertias(MultibodySystem& system, State& state) {
    Array_<SpatialInertia, MobilizedBodyIndex> r;
    system.getMatterSubsystem().calcCompositeBodyInertias(state, r);
}

static double flopTimeInNs;

/**
 * Time how long it takes to perform an operation 1000 times.  The test is repeated 5 times,
 * and the average is returned.  The return value represents CPU time, *not* clock time.
 */
void timeComputation(MultibodySystem& system, void function(MultibodySystem& system, State& state), 
                     const string& name, int iterations, bool useEulerAngles) {
    const int repeats = 3;
    Vector cpuTimes(repeats);
    State state = system.getDefaultState();
    if (useEulerAngles) {
        system.getMatterSubsystem().setUseEulerAngles(state, true);
        system.realizeModel(state);
    }
    system.realize(state, Stage::Acceleration);

    const int ndof = system.getMatterSubsystem().getNumMobilities();
    const int nmovbod = system.getMatterSubsystem().getNumBodies()-1; // not Ground

    // Repeatedly measure the CPU time for performing the operation 1000 times.

    for (int i = 0; i < repeats; i++) {
        double startCpu = threadCpuTime();
        for (int j = 0; j < iterations; j++)
            function(system, state);
        double endCpu = threadCpuTime();
        cpuTimes[i] = Real(endCpu-startCpu);
    }

    double timePerIterUs = mean(cpuTimes)*1000000/iterations; // us
    double flopTimeUs = flopTimeInNs / 1000;
    double flopTimePerIter = timePerIterUs/flopTimeUs;
    std::printf("%40s:%6.4gus -> %4d flp/dof, %4d flp/bod\n",
        name.c_str(), timePerIterUs, (int)(flopTimePerIter/ndof),
        (int)(flopTimePerIter/nmovbod));
}

/**
 * Time all the different calculations for one system.
 */
void runAllTests(MultibodySystem& system, bool useEulerAngles=false) {
    std::cout << "# dofs=" << system.getMatterSubsystem().getNumMobilities() << "\n";
    timeComputation(system, doRealizeTime, "realizeTime", 5000, useEulerAngles);
    timeComputation(system, doRealizePositionKinematics, "realizePositionKinematics", 5000, useEulerAngles);
    timeComputation(system, doRealizePosition, "realizePosition", 5000, useEulerAngles);
    timeComputation(system, doRealizeVelocityKinematics, "realizeVelocityKinematics", 5000, useEulerAngles);
    timeComputation(system, doRealizeVelocity, "realizeVelocity", 5000, useEulerAngles);
    timeComputation(system, doRealizeArticulatedBodyInertias, "doRealizeArticulatedBodyInertias", 3000, useEulerAngles);
    timeComputation(system, doRealizeArticulatedBodyVelocity, "doRealizeArticulatedBodyVelocity", 5000, useEulerAngles);
    timeComputation(system, doRealizeDynamics, "realizeDynamics", 5000, useEulerAngles);
    timeComputation(system, doRealizeAcceleration, "realizeAcceleration", 5000, useEulerAngles);
    timeComputation(system, doRealizeDynamics2Acceleration, "doRealizeDynamics2Acceleration", 5000, useEulerAngles);
    timeComputation(system, doRealizeVelocity2Acceleration, "realizeVelocity2Acceleration", 3000, useEulerAngles);
    timeComputation(system, doRealizeTime2Acceleration, "realizeTime2Acceleration", 2000, useEulerAngles);
    timeComputation(system, doMultiplyByM, "multiplyByM", 5000, useEulerAngles);
    timeComputation(system, doMultiplyByMInv, "multiplyByMInv", 5000, useEulerAngles);
    timeComputation(system, doCalcResidualForceIgnoringConstraints, "calcResidualForceIgnoringConstraints", 5000, useEulerAngles);
    timeComputation(system, doCalcMobilizerReactionForces, "calcMobilizerReactionForces", 1000, useEulerAngles);
    timeComputation(system, doMultiplyBySystemJacobianTranspose, "multiplyBySystemJacobianTranspose", 5000, useEulerAngles);
    timeComputation(system, doCalcCompositeBodyInertias, "calcCompositeBodyInertias", 5000, useEulerAngles);
}

// The following routines create the systems to be profiled.

void createParticles(MultibodySystem& system) {
    SimbodyMatterSubsystem matter(system);
    Body::Rigid body;
    for (int i = 0; i < 256; i++)
        MobilizedBody::Translation next(matter.updGround(), Vec3(1, 0, 0), body, Vec3(0));
    system.realizeTopology();
}

void createFreeBodies(MultibodySystem& system) {
    SimbodyMatterSubsystem matter(system);
    Body::Rigid body;
    for (int i = 0; i < 256; i++)
        MobilizedBody::Free next(matter.updGround(), Vec3(1, 0, 0), body, Vec3(0));
    system.realizeTopology();
}

void createPinChain(MultibodySystem& system) {
    SimbodyMatterSubsystem matter(system);
    Body::Rigid body;
    MobilizedBody last = matter.updGround();
    for (int i = 0; i < 256; i++) {
        MobilizedBody::Pin next(last, Vec3(1, 0, 0), body, Vec3(0));
        last = next;
    }
    system.realizeTopology();
}

void createSliderChain(MultibodySystem& system) {
    SimbodyMatterSubsystem matter(system);
    Body::Rigid body;
    MobilizedBody last = matter.updGround();
    for (int i = 0; i < 256; i++) {
        MobilizedBody::Slider next(last, Vec3(1, 0, 0), body, Vec3(0));
        last = next;
    }
    system.realizeTopology();
}

void createBallChain(MultibodySystem& system) {
    SimbodyMatterSubsystem matter(system);
    Body::Rigid body;
    MobilizedBody last = matter.updGround();
    for (int i = 0; i < 256; i++) {
        MobilizedBody::Ball next(last, Vec3(1, 0, 0), body, Vec3(0));
        last = next;
    }
    system.realizeTopology();
}


void createGimbalChain(MultibodySystem& system) {
    SimbodyMatterSubsystem matter(system);
    Body::Rigid body;
    MobilizedBody last = matter.updGround();
    for (int i = 0; i < 256; i++) {
        MobilizedBody::Gimbal next(last, Vec3(1, 0, 0), body, Vec3(0));
        last = next;
    }
    system.realizeTopology();
}

void createPinTree(MultibodySystem& system) {
    SimbodyMatterSubsystem matter(system);
    Body::Rigid body;
    for (int i = 0; i < 256; i++) {
        MobilizedBody& parent = matter.updMobilizedBody(MobilizedBodyIndex(i/2));
        MobilizedBody::Pin next(parent, Vec3(1, 0, 0), body, Vec3(0));
    }
    system.realizeTopology();
}

void createBallTree(MultibodySystem& system) {
    SimbodyMatterSubsystem matter(system);
    Body::Rigid body;
    for (int i = 0; i < 256; i++) {
        MobilizedBody& parent = matter.updMobilizedBody(MobilizedBodyIndex(i/2));
        MobilizedBody::Ball next(parent, Vec3(1, 0, 0), body, Vec3(0));
    }
    system.realizeTopology();
}

static int tenInts[10];
static Real tenReals[10];
// These should multiply out to about 1.
static Real tenMults[10] = 
    {Real(0.501),Real(0.2501),Real(0.201),Real(0.101),Real(1.000000001),
    Real(1/1.000000002),Real(1/.101), Real(1/.201), Real(1/.2501), Real(1/.501)};
void testFunctions(double& flopTime, bool flopTimeOnly=false) {
    Real addRes=1,subRes=1,mulRes=1,divRes=1,sqrtRes=1,oosqrtRes=1,
         sinRes=1,cosRes=1,atan2Res=1,logRes=1,expRes=1;
    int intAddRes=1;
    Random::Uniform rand; rand.setMin(-5); rand.setMax(5);
    for (int i=0; i<10; i++) tenInts[i] = rand.getIntValue();
    for (int i=0; i<10; i++) tenReals[i] = rand.getValue();

    double tprev = threadCpuTime();
    for (int i = 0; i < 10*100000000; i++) {
        intAddRes += tenInts[0];
        intAddRes -= tenInts[1];
        intAddRes += tenInts[2];
        intAddRes -= tenInts[3];
        intAddRes += tenInts[4];
        intAddRes -= tenInts[5];
        intAddRes += tenInts[6];
        intAddRes -= tenInts[7];
        intAddRes += tenInts[8];
        intAddRes -= tenInts[9];
    }
    double t = threadCpuTime(); double intAddTime = (t-tprev)/10; // time for 1e9 ops
    printf("intAdd %gs\n", t-tprev);
    tprev = threadCpuTime();
    for (int i = 0; i < 5*100000000; i++) {
        addRes += Real(1.1);
        addRes += Real(1.2);
        addRes += Real(1.3);
        addRes += Real(1.4);
        addRes += Real(1.501);
        addRes += Real(1.6);
        addRes += Real(1.7);
        addRes += Real(1.8);
        addRes += Real(1.9);
        addRes += Real(2.007);
    }
    t = threadCpuTime(); double addTime = (t-tprev)/5;
    printf("add %gs\n", t-tprev);
    tprev = threadCpuTime();
    for (int i = 0; i < 5*100000000; i++) {
        subRes -= Real(1.1);
        subRes -= Real(1.2);
        subRes -= Real(1.3);
        subRes -= Real(1.4);
        subRes -= Real(1.501);
        subRes -= Real(1.6);
        subRes -= Real(1.7);
        subRes -= Real(1.8);
        subRes -= Real(1.9);
        subRes -= Real(2.007);
    }
    t = threadCpuTime(); double subTime = (t-tprev)/5;
    printf("sub %gs\n", t-tprev);
    tprev = threadCpuTime();
    for (int i = 0; i < 3*100000000; i++) {
        mulRes *= Real(0.501);
        mulRes *= Real(0.2501);
        mulRes *= Real(0.201);
        mulRes *= Real(0.101);
        mulRes *= Real(1.000000001);
        mulRes *= Real(1/1.000000002); // done at compile time
        mulRes *= Real(1/.101);
        mulRes *= Real(1/.201);
        mulRes *= Real(1/.2501);
        mulRes *= Real(1/.501);
    }
    t = threadCpuTime(); double mulTime=(t-tprev)/3;
    printf("mul %gs\n", t-tprev);
    flopTime = (addTime+mulTime)/2;
    std::cout << "1 flop=avg(add,mul)=" << flopTime << "ns\n";
    if (flopTimeOnly)
        return;

    tprev = threadCpuTime();
    for (int i = 0; i < 100000000; i++) {
        divRes /= tenMults[7];
        divRes /= tenMults[9];
        divRes /= tenMults[8];
        divRes /= tenMults[6];
        divRes /= tenMults[2];
        divRes /= tenMults[3];
        divRes /= tenMults[1];
        divRes /= tenMults[0];
        divRes /= tenMults[4];
        divRes /= tenMults[5];
        // prevent clever optimization VC10 did to turn divides
        // into multiplies.
        tenMults[i%10]     = Real(tenMults[i%10]*1.0000000000001); 
        tenMults[(i+5)%10] = Real(tenMults[(i+5)%10]*0.9999999999999); 
    }
    t = threadCpuTime(); double divTime=(t-tprev);
    printf("div %gs\n", t-tprev);
    tprev = threadCpuTime();

    for (int i = 0; i < 100000000/2; i++) {
        const Real ir = (Real)i;
        sqrtRes += std::sqrt(ir+(Real)0.001); // two adds
        sqrtRes += std::sqrt(ir+(Real)0.1);
        sqrtRes += std::sqrt(ir+(Real)0.2);
        sqrtRes += std::sqrt(ir+(Real)0.3);
        sqrtRes += std::sqrt(ir+(Real)0.4);
        sqrtRes += std::sqrt(ir+(Real)0.501);
        sqrtRes += std::sqrt(ir+(Real)0.6);
        sqrtRes += std::sqrt(ir+(Real)0.7);
        sqrtRes += std::sqrt(ir+(Real)0.8);
        sqrtRes += std::sqrt(ir+(Real)0.9);
    }
    t = threadCpuTime(); double sqrtTime=2*(t-tprev)-2*addTime;
    printf("sqrt %gs\n", t-tprev);
    tprev = threadCpuTime();
    for (int i = 0; i < 100000000/4; i++) {
        const Real ir = (Real)i;
        oosqrtRes += 1/std::sqrt(ir+(Real)0.001); // two adds
        oosqrtRes += 1/std::sqrt(ir+(Real)0.1);
        oosqrtRes += 1/std::sqrt(ir+(Real)0.2);
        oosqrtRes += 1/std::sqrt(ir+(Real)0.3);
        oosqrtRes += 1/std::sqrt(ir+(Real)0.4);
        oosqrtRes += 1/std::sqrt(ir+(Real)0.501);
        oosqrtRes += 1/std::sqrt(ir+(Real)0.6);
        oosqrtRes += 1/std::sqrt(ir+(Real)0.7);
        oosqrtRes += 1/std::sqrt(ir+(Real)0.8);
        oosqrtRes += 1/std::sqrt(ir+(Real)0.9);
    }
    t = threadCpuTime(); double oosqrtTime=4*(t-tprev)-2*addTime;
    printf("1/sqrt %gs\n", t-tprev);
    tprev = threadCpuTime();
    for (int i = 0; i < 100000000/5; i++) {
        const Real ir = (Real)i;
        logRes += std::log(ir+(Real)0.001); // two adds
        logRes += std::log(ir+(Real)0.1);
        logRes += std::log(ir+(Real)0.2);
        logRes += std::log(ir+(Real)0.3);
        logRes += std::log(ir+(Real)0.4);
        logRes += std::log(ir+(Real)0.501);
        logRes += std::log(ir+(Real)0.6);
        logRes += std::log(ir+(Real)0.7);
        logRes += std::log(ir+(Real)0.8);
        logRes += std::log(ir+(Real)0.9);
    }
    t = threadCpuTime(); double logTime=(t-tprev)*5-2*addTime;
    printf("log %gs\n", t-tprev);
    tprev = threadCpuTime();
    for (int i = 0; i < 100000000/5; i++) {
        const Real ir = (Real).000000001*(Real)i;
        expRes += std::exp(ir+(Real)0.001); // two adds
        expRes += std::exp(ir+(Real)0.1);
        expRes += std::exp(ir+(Real)0.2);
        expRes += std::exp(ir+(Real)0.3);
        expRes += std::exp(ir+(Real)0.4);
        expRes += std::exp(ir+(Real)0.501);
        expRes += std::exp(ir+(Real)0.6);
        expRes += std::exp(ir+(Real)0.7);
        expRes += std::exp(ir+(Real)0.8);
        expRes += std::exp(ir+(Real)0.9);
    }
    t = threadCpuTime(); double expTime=(t-tprev)*5-2*addTime;
    printf("exp %gs\n", t-tprev);
    tprev = threadCpuTime();
    for (int i = 0; i < 100000000/10; i++) {
        const Real ir = (Real)i;
        sinRes += std::sin(ir+(Real)0.001); // two adds
        sinRes += std::sin(ir+(Real)0.1);
        sinRes += std::sin(ir+(Real)0.2);
        sinRes += std::sin(ir+(Real)0.3);
        sinRes += std::sin(ir+(Real)0.4);
        sinRes += std::sin(ir+(Real)0.501);
        sinRes += std::sin(ir+(Real)0.6);
        sinRes += std::sin(ir+(Real)0.7);
        sinRes += std::sin(ir+(Real)0.8);
        sinRes += std::sin(ir+(Real)0.9);
    }
    t = threadCpuTime(); double sinTime=(t-tprev)*10-2*addTime;
    printf("sin %gs\n", t-tprev);
    tprev = threadCpuTime();
    for (int i = 0; i < 100000000/10; i++) {
        const Real ir = (Real)i;
        cosRes += std::cos(ir+(Real)0.001); // two adds
        cosRes += std::cos(ir+(Real)0.1);
        cosRes += std::cos(ir+(Real)0.2);
        cosRes += std::cos(ir+(Real)0.3);
        cosRes += std::cos(ir+(Real)0.4);
        cosRes += std::cos(ir+(Real)0.501);
        cosRes += std::cos(ir+(Real)0.6);
        cosRes += std::cos(ir+(Real)0.7);
        cosRes += std::cos(ir+(Real)0.8);
        cosRes += std::cos(ir+(Real)0.9);
    }
    t = threadCpuTime(); double cosTime=(t-tprev)*10-2*addTime;
    printf("cos %gs\n", t-tprev);
    tprev = threadCpuTime();
    for (int i = 0; i < 100000000/10; i++) {
        const Real ir = (Real)i;
        atan2Res += std::atan2(ir+(Real)0.001,ir-(Real)0.001); // three adds
        atan2Res += std::atan2(ir+(Real)0.1,ir-(Real)0.1);
        atan2Res += std::atan2(ir+(Real)0.2,ir-(Real)0.2);
        atan2Res += std::atan2(ir+(Real)0.3,ir-(Real)0.3);
        atan2Res += std::atan2(ir+(Real)0.4,ir-(Real)0.4);
        atan2Res += std::atan2(ir+(Real)0.501,ir-(Real)0.501);
        atan2Res += std::atan2(ir+(Real)0.6,ir-(Real)0.6);
        atan2Res += std::atan2(ir+(Real)0.7,ir-(Real)0.7);
        atan2Res += std::atan2(ir+(Real)0.8,ir-(Real)0.8);
        atan2Res += std::atan2(ir+(Real)0.9,ir-(Real)0.9);
    }
    t = threadCpuTime(); double atan2Time=(t-tprev)*10-3*addTime;
    printf("atan2 %gs\n", t-tprev);
    tprev = threadCpuTime();

    std::cout << std::setprecision(5);
    std::cout << "1 flop=avg(add,mul)=" << flopTime << "ns\n";
    printf("op\t t/10^9\t flops\t final result\n");
    std::cout << "int+:\t"  <<intAddTime<<"\t"<<intAddTime   /flopTime<<"\t"<<intAddRes<< "\n";
    std::cout << "+:\t"     <<addTime<<"\t"<<addTime   /flopTime<<"\t"<<addRes<< "\n";
    std::cout << "-:\t"     <<subTime<<"\t"<<subTime   /flopTime<<"\t"<<subRes<< "\n";
    std::cout << "*:\t"     <<mulTime<<"\t"<<mulTime   /flopTime<<"\t"<<mulRes<< "\n";
    std::cout << "/:\t"     <<divTime<<"\t"<<divTime   /flopTime<<"\t"<<divRes<< "\n";
    std::cout << "sqrt:\t"  <<sqrtTime<<"\t"<<sqrtTime  /flopTime<<"\t"<<sqrtRes<< "\n";
    std::cout << "1/sqrt:\t"<<oosqrtTime<<"\t"<<oosqrtTime/flopTime<<"\t"<<oosqrtRes<< "\n";
    std::cout << "log:\t"   <<logTime<<"\t"<<logTime   /flopTime<<"\t"<<logRes<< "\n";
    std::cout << "exp:\t"   <<expTime<<"\t"<<expTime   /flopTime<<"\t"<<expRes<< "\n";
    std::cout << "sin:\t"   <<sinTime<<"\t"<<sinTime   /flopTime<<"\t"<<sinRes<< "\n";
    std::cout << "cos:\t"   <<cosTime<<"\t"<<cosTime   /flopTime<<"\t"<<cosRes<< "\n";
    std::cout << "atan2:\t" <<atan2Time<<"\t"<<atan2Time /flopTime<<"\t"<<atan2Res<< "\n";
}


int main() {
    time_t now;
    time(&now);
    printf("Starting: %s\n", ctime(&now));
    {   std::cout << "\nCPU performance\n" << std::endl;
        testFunctions(flopTimeInNs, true /*flop time only*/);
    }
    double startClock  = realTime();
    double startCpu    = cpuTime();
    double startThread = threadCpuTime();

    {
        std::cout << "\nParticles:\n" << std::endl;
        MultibodySystem system;
        createParticles(system);
        runAllTests(system);
    }
    
    {
        std::cout << "\nFree Bodies (Quaternions):\n" << std::endl;
        MultibodySystem system;
        createFreeBodies(system);
        runAllTests(system, false);
    }
    {
        std::cout << "\nFree Bodies (Euler angles):\n" << std::endl;
        MultibodySystem system;
        createFreeBodies(system);
        runAllTests(system, true);
    }
    
    {
        std::cout << "\nPin Chain:\n" << std::endl;
        MultibodySystem system;
        createPinChain(system);
        runAllTests(system);
    }
    {
        std::cout << "\nSlider Chain:\n" << std::endl;
        MultibodySystem system;
        createSliderChain(system);
        runAllTests(system);
    }
    {
        std::cout << "\nBall Chain (Quaternions):\n" << std::endl;
        MultibodySystem system;
        createBallChain(system);
        runAllTests(system, false);
    }
    {
        std::cout << "\nBall Chain (Euler angles):\n" << std::endl;
        MultibodySystem system;
        createBallChain(system);
        runAllTests(system, true);
    }
    {
        std::cout << "\nGimbal Chain:\n" << std::endl;
        MultibodySystem system;
        createGimbalChain(system);
        runAllTests(system);
    }
    
    {
        std::cout << "\nPin Tree:\n" << std::endl;
        MultibodySystem system;
        createPinTree(system);
        runAllTests(system);
    }
    {
        std::cout << "\nBall Tree:\n" << std::endl;
        MultibodySystem system;
        createBallTree(system);
        runAllTests(system);
    }
    

    std::cout << "Total time:\n";
    std::cout << "  process CPU=" << cpuTime()-startCpu << "s\n";
    std::cout << "  thread CPU =" << threadCpuTime()-startThread << "s\n";
    std::cout << "  real time  =" << realTime()-startClock << "s\n";
}