Using multi-core CPU in 1 simulation

Hello,

I would like to use multiple cores CPU at the same time and 1 (large-scale) environment. Some documentation online mentions multi-core but I don't see any example. https://simpy.readthedocs.io/_/downloads/en/3.0.11/pdf/

Let's start with this code sample:

import simpy
import time

def work(): # dummy work for simulating long-run time
    j=0
    for i in range(100000000):
        j+=1

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

env = simpy.Environment()
env.process(clock(env, 'A', 1))
env.process(clock(env, 'B', 1))

st=time.time()
env.run(until=20)
print("time :", time.time()-st)

The multi-threaded version (it works but 1 core is active at the same time due to GIL):

import simpy
import threading
import time

def work(): # dummy work for simulating long-run time
    j = 0
    for i in range(100000000):
        j += 1

def clock(env, name, tick):
    while True:
        work()
        yield env.timeout(tick)

def simulate_environment(env, process_name, tick):
    env.process(clock(env, process_name, tick))

if __name__ == "__main__":
    env = simpy.Environment()

    # Create two threads for the clocks
    thread1 = threading.Thread(target=simulate_environment, args=(env, 'A', 1))
    thread2 = threading.Thread(target=simulate_environment, args=(env, 'B', 1))

    # Start the threads
    thread1.start()
    thread2.start()

    # Run the simulation until a certain time
    st = time.time()
    env.run(until=20)
    print("time:", time.time() - st)

    # Join the threads to ensure they complete before exiting
    thread1.join()
    thread2.join()

The multi-processing is not working properly due to A and B are ignored

import simpy
import time
import multiprocessing

def work():
    j = 0
    for i in range(100000000):
        j += 1

def clock(env, name, tick):
    while True:
        work()
        print("name:", name)
        yield env.timeout(tick)

def simulate_environment(env, process_name, tick):
    env.process(clock(env, process_name, tick))

if __name__ == "__main__":
    env = simpy.Environment()

    process1 = multiprocessing.Process(target=simulate_environment, args=(env, 'A', 1))
    process2 = multiprocessing.Process(target=simulate_environment, args=(env, 'B', 1))

    process1.start()
    process2.start()

    st = time.time()
    env.run(until=20) # don't see A and B processes !!!!!!!!!!!!!!
    print("time:", time.time() - st)

    process1.join()
    process2.join()

If we dive in core.py code: https://gitlab.com/team-simpy/simpy/-/blob/master/src/simpy/core.py?ref_type=heads#L188

it seems that "step()" deals with only 1 event at the same time (CPU time), while in the simulator time, multiple events could be computed at the same time (utilizing different CPU cores). There is probably a way to do a multicore Environment.

I could contribute directly to Simpy with guidance.

Regards,

Edited by PierrickPochelu