Skip to content

Commit

Permalink
Remove fine-grained timing from narrow-phase collision functions, del…
Browse files Browse the repository at this point in the history
…ete `mjTIMER_COL_MID`.

PiperOrigin-RevId: 599225419
Change-Id: Ieca166753c20a0abf660e2821b2844dda8758951
  • Loading branch information
yuvaltassa authored and Copybara-Service committed Jan 17, 2024
1 parent edc254b commit 2feefbc
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 44 deletions.
13 changes: 8 additions & 5 deletions doc/changelog.rst
Expand Up @@ -9,17 +9,20 @@ General
^^^^^^^
1. Improved the :ref:discardvisual<compiler-discardvisual> compiler flag, which now discards all visual-only assets. See
:ref:discardvisual<compiler-discardvisual> for details.
2. Removed the :ref:`timer<mjtTimer>` for midphase colllision detection, it is now folded in with the narrowphase
timer. This is because timing the two phases seperately required fine-grained timers inside the collision
functions; these functions are so small and fast that the timer itself was incurring a measurable cost.

MJX
^^^
2. Added :ref:`dyntype<actuator-general-dyntype>` ``filterexact``.
3. Added :at:`site` transmission.
4. Updated MJX colab tutorial with more stable quadruped environment.
5. Added ``mjx.ray`` which mirrors :ref:`mj_ray` for planes, spheres, capsules, boxes, and meshes.
3. Added :ref:`dyntype<actuator-general-dyntype>` ``filterexact``.
4. Added :at:`site` transmission.
5. Updated MJX colab tutorial with more stable quadruped environment.
6. Added ``mjx.ray`` which mirrors :ref:`mj_ray` for planes, spheres, capsules, boxes, and meshes.

Bug fixes
^^^^^^^^^
6. Fixed a bug that prevented the use of pins with plugins if flexes are not in the worldbody. Fixes
7. Fixed a bug that prevented the use of pins with plugins if flexes are not in the worldbody. Fixes
:github:issue:`1270`.
7. Fixed a bug in the :ref:`muscle model<CMuscle>` that led to non-zero values outside the lower
bound of the length range. Fixes :github:issue:`1342`.
Expand Down
1 change: 0 additions & 1 deletion doc/includes/references.h
Expand Up @@ -75,7 +75,6 @@ typedef enum mjtTimer_ { // internal timers

// breakdown of mj_collision
mjTIMER_COL_BROAD, // broadphase
mjTIMER_COL_MID, // midphase
mjTIMER_COL_NARROW, // narrowphase

mjNTIMER // number of timers
Expand Down
1 change: 0 additions & 1 deletion include/mujoco/mjdata.h
Expand Up @@ -87,7 +87,6 @@ typedef enum mjtTimer_ { // internal timers

// breakdown of mj_collision
mjTIMER_COL_BROAD, // broadphase
mjTIMER_COL_MID, // midphase
mjTIMER_COL_NARROW, // narrowphase

mjNTIMER // number of timers
Expand Down
5 changes: 2 additions & 3 deletions introspect/enums.py
Expand Up @@ -458,9 +458,8 @@
('mjTIMER_POS_MAKE', 11),
('mjTIMER_POS_PROJECT', 12),
('mjTIMER_COL_BROAD', 13),
('mjTIMER_COL_MID', 14),
('mjTIMER_COL_NARROW', 15),
('mjNTIMER', 16),
('mjTIMER_COL_NARROW', 14),
('mjNTIMER', 15),
]),
)),
('mjtCatBit',
Expand Down
2 changes: 1 addition & 1 deletion introspect/structs.py
Expand Up @@ -4154,7 +4154,7 @@
name='timer',
type=ArrayType(
inner_type=ValueType(name='mjTimerStat'),
extents=(16,),
extents=(15,),
),
doc='timer statistics',
),
Expand Down
2 changes: 1 addition & 1 deletion sample/testspeed.cc
Expand Up @@ -278,7 +278,7 @@ int main(int argc, char** argv) {

// components of mjTIMER_POS_COLLISION
if (i == mjTIMER_POS_COLLISION) {
for (int j : {mjTIMER_COL_BROAD, mjTIMER_COL_MID, mjTIMER_COL_NARROW}) {
for (int j : {mjTIMER_COL_BROAD, mjTIMER_COL_NARROW}) {
int number = d[0]->timer[j].number;
mjtNum jstep = number ? d[0]->timer[j].duration/number : 0.0;
mjtNum percent = number ? 100*jstep/tstep : 0.0;
Expand Down
30 changes: 3 additions & 27 deletions src/engine/engine_collision_driver.c
Expand Up @@ -291,12 +291,9 @@ void mj_collision(const mjModel* m, mjData* d) {
unsigned int last_signature = -1;
TM_END(mjTIMER_COL_BROAD);

// midphase collision detector
// narrowphase and midphase collision detector
TM_RESTART;

// save current narrowphase duration
mjtNum tmNarrow = d->timer[mjTIMER_COL_NARROW].duration;

// process bodyflex pairs returned by broadphase, merge with predefined geom pairs
int pairadr = 0;
for (int i=0; i < nbfpair; i++) {
Expand Down Expand Up @@ -471,14 +468,8 @@ void mj_collision(const mjModel* m, mjData* d) {
}
}

// end midphase timer
TM_END(mjTIMER_COL_MID);

// subtract nested narrowphase timing from midphase timer
d->timer[mjTIMER_COL_MID].duration -= (d->timer[mjTIMER_COL_NARROW].duration - tmNarrow);

// increment narrowphase counter
d->timer[mjTIMER_COL_NARROW].number++;
// end narrowphase and midphase timer
TM_END(mjTIMER_COL_NARROW);

mj_freeStack(d);
TM_END1(mjTIMER_POS_COLLISION);
Expand Down Expand Up @@ -1444,8 +1435,6 @@ static void mj_makeCapsule(const mjModel* m, mjData* d, int f, const int vid[2],

// test two geoms for collision, apply filters, add to contact list
void mj_collideGeoms(const mjModel* m, mjData* d, int g1, int g2) {
TM_START;

int num, type1, type2, condim;
mjtNum margin, gap, friction[5], solref[mjNREF], solimp[mjNIMP];
mjtNum solreffriction[mjNREF] = {0};
Expand Down Expand Up @@ -1635,9 +1624,6 @@ void mj_collideGeoms(const mjModel* m, mjData* d, int g1, int g2) {

// move arena pointer back to the end of the contact array
resetArena(d);

// add duration without incrementing counter
TM_ADD(mjTIMER_COL_NARROW);
}


Expand Down Expand Up @@ -1850,8 +1836,6 @@ void mj_collideFlexSAP(const mjModel* m, mjData* d, int f) {

// test a geom and an elem for collision, add to contact list
void mj_collideGeomElem(const mjModel* m, mjData* d, int g, int f, int e) {
TM_START;

mjtNum margin = mj_assignMargin(m, mju_max(m->geom_margin[g], m->flex_margin[f]));
int dim = m->flex_dim[f], type = m->geom_type[g];
int num;
Expand Down Expand Up @@ -1965,17 +1949,12 @@ void mj_collideGeomElem(const mjModel* m, mjData* d, int g, int f, int e) {

// move arena pointer back to the end of the contact array
resetArena(d);

// add duration without incrementing counter
TM_ADD(mjTIMER_COL_NARROW);
}



// test two elems for collision, add to contact list
void mj_collideElems(const mjModel* m, mjData* d, int f1, int e1, int f2, int e2) {
TM_START;

mjtNum margin = mj_assignMargin(m, mju_max(m->flex_margin[f1], m->flex_margin[f2]));
int dim1 = m->flex_dim[f1], dim2 = m->flex_dim[f2];
int num;
Expand Down Expand Up @@ -2070,9 +2049,6 @@ void mj_collideElems(const mjModel* m, mjData* d, int f1, int e1, int f2, int e2

// move arena pointer back to the end of the contact array
resetArena(d);

// add duration without incrementing counter
TM_ADD(mjTIMER_COL_NARROW);
}


Expand Down
1 change: 0 additions & 1 deletion src/engine/engine_support.c
Expand Up @@ -89,7 +89,6 @@ const char* mjTIMERSTRING[mjNTIMER]= {
"pos_make",
"pos_project",
"col_broadphase",
"col_midphase",
"col_narrowphase"
};

Expand Down
6 changes: 2 additions & 4 deletions unity/Runtime/Bindings/MjBindings.cs
Expand Up @@ -138,9 +138,8 @@ public enum mjtTimer : int{
mjTIMER_POS_MAKE = 11,
mjTIMER_POS_PROJECT = 12,
mjTIMER_COL_BROAD = 13,
mjTIMER_COL_MID = 14,
mjTIMER_COL_NARROW = 15,
mjNTIMER = 16,
mjTIMER_COL_NARROW = 14,
mjNTIMER = 15,
}
public enum mjtDisableBit : int{
mjDSBL_CONSTRAINT = 1,
Expand Down Expand Up @@ -769,7 +768,6 @@ public unsafe struct mjData_ {
public mjTimerStat_ timer12;
public mjTimerStat_ timer13;
public mjTimerStat_ timer14;
public mjTimerStat_ timer15;
public mjSolverStat_ solver0;
public mjSolverStat_ solver1;
public mjSolverStat_ solver2;
Expand Down

0 comments on commit 2feefbc

Please sign in to comment.