@@ -1276,6 +1276,57 @@ MuJoCo Forum for an example; the plots below are generated with that model.
12761276
12771277|image18 | |image19 |
12781278
1279+ .. _CSize :
1280+
1281+ Memory allocation
1282+ ~~~~~~~~~~~~~~~~~
1283+
1284+ MuJoCo preallocates all the memory needed at runtime in ``mjData ``, and does not access the heap allocator after
1285+ model creation. Memory in ``mjData `` is allocated by :ref: `mj_makeData ` in two contiguous blocks:
1286+
1287+ - ``mjData.buffer `` contains fixed-size arrays.
1288+ - ``mjData.arena `` contains dynamically-sized arrays.
1289+
1290+ There are two types of dynamic arrays allocated in the ``arena `` memory space.
1291+
1292+ - contacts and constraint-related arrays are laid out from the beginning of the ``arena ``.
1293+ - :ref: `stack <siStack >` arrays are laid out from the end of the ``arena ``.
1294+
1295+ By allocating dynamic quantities from both sides of the ``arena `` space, variable-sized memory allocation is controlled
1296+ by a single number: the :at: `memory ` attribute of the :ref: `size <size >` MJCF element. Unlike the fixed-size arrays in
1297+ the ``buffer ``, variable-sized arrays in the arena can be ``NULL ``, for example after a call to :ref: `mj_resetData `.
1298+ When ``arena `` memory runs out, one of three things will happen, depending on the type of memory requested:
1299+
1300+ - If memory runs out during contact allocation, a warning will be raised and subsequent contacts will not be added in
1301+ this step, but simulation continues as usual.
1302+ - If memory runs out during constraint-related allocation, a warning will be raised and the constraint solver will be
1303+ disabled in this step, but simulation continues as usual. Note that physics without the constraint solver will
1304+ generally be very different, but allowing the simulation to continue can still be useful, e.g. during
1305+ scene initialization when many bodies are temporarily overlapping.
1306+ - If memory runs out during stack array allocation, a hard error will occur.
1307+
1308+ Unlike the size of the ``buffer ``, the size of the ``arena `` cannot be pre-computed, since the number of contacts and
1309+ stack usage is not known in advance. So how should one choose it? The following simple heuristic is currently used,
1310+ though it may be improved in the future: enough memory is allocated for 100 contacts and 500 scalar constraints, under
1311+ worst-case conditions. If this heuristic is insufficient, we recommend the following procedure. Increase the ``arena ``
1312+ memory significantly using the :at: `memory ` attribute, and inspect the actual memory used at runtime.
1313+ ``mjData.maxuse_arena `` keeps track of the maximum ``arena `` memory utilization since the last reset. The :ref: `simulate
1314+ <saSimulate>` viewer shows this number as a fraction of the total arena space (in the info window in the lower-left
1315+ corner). So one can start with a large number, simulate for a while, and if the fractions are small go back to the XML
1316+ and reduce the allocation size. Keep in mind though that memory utilization can change dramatically in the course of the
1317+ simulation, depending on how many constraints are active and which constraint solver is used. The CG solver is the most
1318+ memory efficient, followed by the Newton solver, while the PGS solver is the most memory intensive. When we design
1319+ models, we usually aim for 50% utilization in the worst-case scenario encountered while exploring the model. If you only
1320+ intend to use the CG solver, you can get away with significantly smaller arena allocation.
1321+
1322+ .. attention ::
1323+
1324+ Memory allocation behaviour changed in MuJoCo 2.3.0. Before this version, the :at: `njmax `, :at: `nconmax ` and
1325+ :at: `nstack ` attributes of the :ref: `size <size >` MJCF element had the semantics of maximum memory allocated for
1326+ contacts, constraints and stack, respectively. If you are using an earlier version of MuJoCo, please switch to an
1327+ `earlier <https://mujoco.readthedocs.io/en/2.2.2/modeling.html#model-sizes >`_ documentation version to read about the
1328+ previous behaviour.
1329+
12791330.. _Tips :
12801331
12811332Tips and tricks
@@ -1377,32 +1428,6 @@ in a visible way, and the energy fluctuates around the initial value instead of
13771428 </body >
13781429 </worldbody >
13791430
1380- .. _CSize :
1381-
1382- Model sizes
1383- ~~~~~~~~~~~
1384-
1385- MuJoCo preallocates all the memory needed at runtime in mjData, and does not access the C/C++ memory manager after
1386- model creation. It is therefore essential to allocate enough memory. The allocation is controlled by three size
1387- parameters specified in the :ref: `size <size >` element, namely the stack size :at: `nstack `, the
1388- maximum number of contacts :at: `nconmax `, and the maximum number of scalar constraints :at: `njmax `. The default
1389- size settings use heuristics to allocate sufficient memory, but the true memory needs for a given model can only be
1390- determined during simulation. If nstack is insufficient the simulator calls mju_error and gives up. If nconmax or
1391- njmax are insufficient the remaining contacts or other constraints are discarded, and the simulation continues but the
1392- results are not as desired. If on the other hand the allocation is too large, clearing mjData with mj_reset takes
1393- longer, and in multi-threaded applications simulating many large models in parallel the machine could run out of
1394- memory, or cache performance could be adversely affected. And even if nothing bad happens, allocating a lot more
1395- memory than needed is just poor style.
1396-
1397- So how do we know how much memory to allocate? mjData has fields maxuse_stack, maxuse_con and maxuse_efc which keep
1398- track of the maximum memory utilization in each category since the last reset. The code sample :ref: `simulate.cc <saSimulate >`
1399- shows this data as a fraction of the maximum allocation (in the info window in the lower-left corner). So one can start with
1400- the defaults, simulate for a while, and if the fractions are too small go back to the XML and set the allocation sizes
1401- explicitly. Keep in mind though that memory utilization can change dramatically in the course of the simulation,
1402- depending on how many constraints are active and also which constraint solver is used.
1403- For example if the stack size is just sufficient for the CG solver, the Newton and PGS solvers will run out of stack.
1404- When we design models, we usually aim for 50% utilization in the worst-case scenario encountered while exploring the
1405- model. If you only intend to use the CG solver, you can get away with significantly smaller stack allocation.
14061431
14071432 .. |image0 | image :: images/modeling/impedance.png
14081433 :width: 600px
0 commit comments