Using MuJoCo's Stack

Discussion in 'Simulation' started by Henrique G., Mar 2, 2017.

  1. Hi!

    I'm starting to use MuJoCo's stack to speed up my code.

    I need to use a lot of matrices. I mean... a lot. Allocating every single one (without reusing previously allocated arrays) takes more than 50MB.

    If I understood it right, I set nstack (which means the number of doubles. In other words, something like 6.5e3 in the 50MB case) in the model in order to have a stack in which my arrays fit.

    If I'm right, here is the but: inside a function, a new MuJoCo data is made (with its 50MB stack, right?) to do some steps without affecting the main data, but the stack will not be used.

    The question: Is there a way to make the data (mj_makeData) without the MuJoCo's stack allocation?

    Thanks!
     
  2. Emo Todorov

    Emo Todorov Administrator Staff Member

    mj_makeData always allocates the stack in mjData. It is needed for internal MuJoCo functions, plus it can also be used in user functions.

    If you want a second mjData for auxiliary computations, you can set mjModel.nstack to something smaller. This can be done programmatically just before calling mj_makeData.
     
  3. Nice! Thanks!

    And to which value makes mjData.nstack be the default one?
     
  4. Emo Todorov

    Emo Todorov Administrator Staff Member

    Depends on the model. There is no good way to set this automatically, because in the worst case you could have lots of pairwise contacts, but in practice this depends on how you use your model. This is why the parameter is user-adjustable. If it is too low, you will get a stack overflow error during the simulation. The field mjData.maxuse_stack keeps track of the maximum stack usage - so you can run your simulation for a while, look at this field, and set mjModel.nstack to something slightly larger.

    None of this should be a concern on a modern machine though. You could simply allocate a very large mjData.
     
  5. Thank you for the answer.

    Indeed this shouldn't be a problem. But I'm managing things to allocate the less as possible memory. This also helps me to understand how things work. :)
     
  6. Also, the Modeling says that a negative value for nstack (-1 is the default) makes MuJoCo calculate a size for the stack.

    As advised, I tried to set model->nstack=-1 at runtime, but it didn't work (on the other hand, a positive integer works like a charm. thanks :) ) .

    Setting a negative value for nstack at runtime doesn't have the same effect as setting in the model?
     
  7. Emo Todorov

    Emo Todorov Administrator Staff Member

    Indeed, -1 is a magic value which tells the model compiler to use a default. At runtime the model is already compiled, so it needs an actual value.
     
  8. Ok. Fair enough.

    Thanks!