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.2.tar.gz (23.4 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.2-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for serializable_simpy-0.0.2.tar.gz
Algorithm Hash digest
SHA256 3f2040d25dbb1345de91b8098e1973574d3a2c37d66575e44b1a7acda23c4c0e
MD5 5004769cecf45771affece73bbe4d174
BLAKE2b-256 adb8623bcae83281eba1926bb8802cc8cc3e8accc5202388e9ba154e5fc09710

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for serializable_simpy-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 576c353eae33d525b0d9fe5e04fb951cc2f7320418f39d1ca806a9513f34d36d
MD5 3668991601dae95911879b6928583293
BLAKE2b-256 7a213206cef7ad085d43a8dd939a69dd2bd7318cd40e2cc82e57f8b80487ea9a

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