Grid2op / lightsim2grid

LightSim2grid implements a c++ backend targeting the Grid2Op (https://github.com/rte-france/grid2op) platform.
https://lightsim2grid.readthedocs.io/en/latest/
Mozilla Public License 2.0
51 stars 10 forks source link

Multithreading support from Grid2Op using lightsim2grid backend #12

Closed Hello-World-Py closed 4 years ago

Hello-World-Py commented 4 years ago

Environment

Bug description

I am unable to run grid2op in a multi-threaded manner using the light2sim backend. The following code replicates the error I am seeing. The code works without any error using self.threads=1 or with the pandapower backend

import threading
import grid2op
from lightsim2grid import LightSimBackend
backend = LightSimBackend()

class A3CAgent:
    def __init__(self):
        self.threads = 3
    def test_multi(self):
        agents = [Agent(i) for i in range(self.threads)]
        for agent in agents:
            agent.start()

class Agent(threading.Thread):
    def __init__(self, index):
        threading.Thread.__init__(self)
        self.index = index
    def run(self):
        print("Running Thread ", self.index)
        env = grid2op.make("l2rpn_neurips_2020_track1_small", difficulty=0) # does not raise error
        env = grid2op.make("l2rpn_neurips_2020_track1_small", backend = backend, difficulty=0) # raises error
        env.reset()

if __name__ == '__main__':
    global_agent = A3CAgent()
    global_agent.test_multi()

Current output

There are different errors depending on the number of threads. One error raised is the following:

index 36 is out of bounds for axis 0 with size 36

Expected output

There should be no error in the code.

BDonnot commented 4 years ago

Hello,

This is not a bug on lightsim side, so I marked it as enhancement.

Lightsim is not compatible with multi processing / multi threading yet because for this support it needs to be compatible with pickle (and today it is not)

That being say, i am not sure the error you point out is related to this feature.

BDonnot commented 4 years ago

Hello, after a look to your code, it appears to not be related to pickle or anything, but rather to the use of grid2op you are trying to do.

You need to build one backend per environment, otherwise yes, there will be some issue for sure.

So this is a correct working code:

import threading
import grid2op
from lightsim2grid import LightSimBackend

class A3CAgent:
    def __init__(self, env_name="l2rpn_neurips_2020_track1_small"):
        self.threads = 3
        self.env_name = env_name
        self.env = grid2op.make(self.env_name)

    def test_multi(self):
        agents = [Agent(i, env_name=self.env_name) for i in range(self.threads)]
        for agent in agents:
            agent.start()

class Agent(threading.Thread):
    def __init__(self, index, env_name):
        threading.Thread.__init__(self)
        self.index = index
        self.env_name = env_name

    def run(self):
        print("Running Thread ", self.index)
        backend = LightSimBackend()
        env = grid2op.make(self.env_name, backend=backend, difficulty=0)  # does not raise error
        env.reset()

if __name__ == '__main__':
    env_name = "l2rpn_neurips_2020_track1_small"
    global_agent = A3CAgent(env_name)
    global_agent.test_multi()
Hello-World-Py commented 4 years ago

Thanks!