Skip to main content

Simpy with

Project description

SerializableSimpy

SerializableSimpy Project aims to extends the capabilities of traditional SimPy by introducing serializability and parallelism.

Standard SimPy uses Python's yield keyword to implement events, which prevents object serialization and limits scalability. SerializableSimpy provides a "yield-less" implementation of DES logic, enabling:

  • Simulation state checkpointing: pause/resume/rollback
  • Parallelism: Multiprocessing and MPI-based

Why a new Discrete Event Simulator?

We designed SerializableSimpy around SimPy’s proven and intuitive API, which has a strong and active user base. However, SimPy’s reliance on generator-based coroutines limits its applicability in modern simulation workflows.

While inspired by SimPy, SerializableSimpy is not a drop-in replacement and does not implement all of SimPy’s API.

Installation & Dependencies

SerializableSimpy can be installed as a standalone Python module.

pip install serial-simpy

To enable distributed execution via MPI, install:

pip install mpi4py

API difference with Simpy

This code pattern with Simpy

do_something_before(a,b,c)
yield env.timeout(10)
do_something_after(d,e,f)

In SerializableSimpy becomes:

do_something_before(a,b,c)
env.timeout(10, do_something_after, (d,e,f))

You can find many other Simpy examples converted in SerializableSimy here: [URL here]

Checkpointing Example

Here's an example to demonstrate how to use the objects

from SerializableSimpy.core import Environment, Process


class Clock(Process):
    def __init__(self, env, name, tick):
        self.name = name
        self.env = env
        self.tick = tick

    def on_initialize(self, env):
        self.env = env
        self.on_tick()

    def on_tick(self):
        print(self.name, self.env.now)
        self.env.timeout(self.tick, self.on_tick,
                         ())  # `yield env.timeout(tick)` becomes `env.timeout(tick, on_tick, ())`


env = Environment()
env.process(Clock(env, 'fast', 0.5))
env.process(Clock(env, 'slow', 1))

env.run(until=2)

# Saving/Restoring with pickle is possible
import pickle

with open("checkpoint.pkl", "wb") as f:
    pickle.dump(env, f)
del env
print("Restoring")
with open("checkpoint.pkl", "rb") as f:
    env = pickle.load(f)

env.run(until=4)

Prints:

fast 0
slow 0
fast 0.5
slow 1
fast 1.0
fast 1.5
fast 2.0
slow 2
fast 2.5
slow 3
fast 3.0
fast 3.5

Inspired from the official Simpy code: https://gitlab.com/team-simpy/simpy. Simpy in its current shape cannot serialize objects. Let's take the simplest example of the homepage.

import simpy
def clock(env, name, tick):
    while True:
        print(name, env.now)
        yield env.timeout(tick)

env = simpy.Environment()
env.process(clock(env, 'fast', 0.5))
env.process(clock(env, 'slow', 1))
env.run(until=2)

import pickle
with open("checkpoint.pkl","wb") as f:
    pickle.dump(env, f) # ❌ TypeError: cannot pickle 'generator' object

Using Parallel Environments

SerializableSimpy supports multiple runtime backends. Choose based on your use case:

# Sequential environemnt:
from SerializableSimpy.core import Environment 
env=Environment(initial_time=0.) 
env.run(until=1.)

# MPI (Message Passing Interface) environemnt :
from SerializableSimpy.MPI_core import MPIGlobalEnv
env=MPIGlobalEnv(initial_time=0.) 
env.run(until=1., delta=0.1)

# Multiprocessing environemnt (Shared memory):
from SerializableSimpy.MP_core import MPGlobalEnv
env=MPGlobalEnv(initial_time=0.) 
env.run(until=1., delta=0.1)

Note: Parallel simulation requires setting a delta time step to synchronize local environments. A small delta may improve accuracy at the cost of communication/synchronization overhead.

Parallel Simulation Benchmark

In the folder ./application/tuto/parallelism/, you'll find three implementations of a producer-consumer model—a common DES pattern in manufacturing and logistics.

Execution Comparison

Parallel Method User Code Lines Time (sec.) Consumed Items Multi-Machine Compatible
Sequential 54 40.04 20
Multiprocessing 63 20.05 19
MPI 67 20.02 18

Observations:

  • MPI introduces lower overhead than multiprocessing for producer-consumer setups. However, when delta becomes too small, synchronization overhead increases significantly hurting MPI performance more than multiprocessing.
  • Multiprocessing may yield final state closer to sequential. It is because they are not only synchronized every delta, but exchange through a shared memory. In the wost case, Multiprocessing and MPI produces the same final state. In the best case, computation time matches communication time and are equivalent.

Running on MeluXina Supercomputer

See MeluXinaREADME.md for a detailed walkthrough on deploying SerializableSimpy on the MeluXina EuroHPC system.

Acknowledgment

SerializeSimpy contributor(s) would like to express their gratitude for the fruitful discussions and the collaboration that made this project possible between Goodyear company and the University of Luxembourg.

Supported by the Luxembourg National Research Fund 17941664

Supported by the Ministry of Economy (MECO) 17941664

Contact

For any questions or suggestions, please contact mailto:pierrick.pochelu@gmail.com.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

serializable_simpy-0.0.1.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

serializable_simpy-0.0.1-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file serializable_simpy-0.0.1.tar.gz.

File metadata

  • Download URL: serializable_simpy-0.0.1.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.9

File hashes

Hashes for serializable_simpy-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f6d36ef4592d345d1bc469765696c4122d599dfea6a1515f61447462ac3f1146
MD5 993b1f2d401c84ea7ea1881f32ddf817
BLAKE2b-256 970dbd1443259865988278eba17ce989e2727940c7f3a51b06c798d09ce678f0

See more details on using hashes here.

File details

Details for the file serializable_simpy-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for serializable_simpy-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f8f8b31f1c9ca08a0a996d2688c10b85c2f3bf74a4e27e0646b9ed42954f5227
MD5 c27e93381b08f7e2ffc2faadb3a2bbd2
BLAKE2b-256 1cd2cef164ae8da274b686f05db657fe4494a9325f627584f36474a666bdb4fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page