MultiprocessingΒΆ

In principle, MISCore simulates all individuals sequentially. This might take very long for large simulations. As all individuals within a Model are simulated independently from each other, a simulation can perfectly be run in parallel. MISCore has a built-in option for this which relies on the multiprocessing module.

Usually, there are three adaptations you should make to your code:

  1. When using multiprocessing, you should put MISCore code in a function.

  2. The call to this function should be wrapped in a if __name__ == "__main__" statement.

  3. You should specify the cores argument in the run() function.

The following example shows a minimal working example with only an OC Process:

 1from miscore import Model, Universe
 2from miscore.processes import OC
 3
 4
 5def main():
 6    # OC process
 7    oc = OC(
 8        age=80
 9    )
10
11    # Universe
12    universe = Universe(
13        name="universe",
14        processes=[oc]
15    )
16
17    # Model
18    model = Model(
19        universes=[universe]
20    )
21
22    # Run
23    result = model.run(
24        n=1000,
25        cores="all",
26        verbose=True
27    )
28
29    result.events.to_csv("events.csv")
30    result.durations.to_csv("durations.csv")
31
32
33if __name__ == "__main__":
34    main()

MISCore will now use all available cores for its simulations. You can also specify a number, like cores=8. Simulations will then be run with the specified number of cores.

Note

There is almost always some overhead when using multiprocessing. Thus, having 8 CPU cores will likely not result in a 8x speedup, and will even slow down small runs like this example run. It may be useful to specify one core less than is available on your system.

Note

If multiprocessing is used in some higher level of the code, multiprocessing should not be used for running MISCore. Make sure to leave the cores option unspecified or to set cores=None.