openai / mujoco-py

MuJoCo is a physics engine for detailed, efficient rigid body simulations with contacts. mujoco-py allows using MuJoCo from Python 3.
Other
2.81k stars 810 forks source link

How to do parallel processing #729

Closed mitsu3291 closed 2 years ago

mitsu3291 commented 2 years ago

I tried to do parallel processing using mujoco environment, but I could not do that. How can I do parallel processing?

Environment:

I wrote the code like this:

import mujoco_py
from multiprocessing import Pool

def rollout(sim):
    for _ in range(100):
        sim.step()

if __name__ == "__main__":
    xml_path = "..."
    model = mujoco_py.load_model_from_path(xml_path)
    sim = mujoco_py.MjSim(model)

    p = Pool(2)
    sim_list = [sim]*10
    p.map(rollout, sim_list)

And I got the error:

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    p.map(rollout, sim_list)
  File "/usr/lib/python3.8/multiprocessing/pool.py", line 364, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File "/usr/lib/python3.8/multiprocessing/pool.py", line 771, in get
    raise self._value
  File "/usr/lib/python3.8/multiprocessing/pool.py", line 537, in _handle_tasks
    put(task)
  File "/usr/lib/python3.8/multiprocessing/connection.py", line 206, in send
    self._send_bytes(_ForkingPickler.dumps(obj))
  File "/usr/lib/python3.8/multiprocessing/reduction.py", line 51, in dumps
    cls(buf, protocol).dump(obj)
  File "mjbatchrenderer.pyx", line 2, in mujoco_py.cymj
TypeError: no default __reduce__ due to non-trivial __cinit__
EylonCohen commented 1 year ago

hi, it seams i have the same error: File "mjbatchrenderer.pyx", line 2, in mujoco_py.cymj TypeError: no default reduce due to non-trivial cinit

can you tell me how did you solved it?

mitsu3291 commented 1 year ago

I stopped using Pool module.

I wrote code using Process module like this: then it works.

xml_path = "..."
process_num = ""
def test(self, id, seed):
    model = mujoco_py.load_model_from_path(xml_path)
    sim = mujoco_py.MjSim(model)
    for _ in range(100):
          sim.step()

def rollout(self):
    processes = []
    seeds = np.random.randint(0, 2**32-1, process_num)
    for id in range(process_num):
        processes.append(Process(target=test, args=[id, seeds[id]]))
    for id in range(process_num):
        processes[id].start()
    for id in range(process_num):
        processes[id].join()

and also you can use Value module and decide how many rollouts executed if you want.