From 13df06c38c3cc6ecb1899450ea2c7a98817831d5 Mon Sep 17 00:00:00 2001 From: Alejandro Campoy Nieves Date: Wed, 3 Jul 2024 15:21:11 +0200 Subject: [PATCH] (v3.4.0) Sinergym parallelism on the same machine (#424) * Modeling: Applying fcntl functionality to output folder name creation for robust parallelism * Add .lock file to gitignore * Updated Sinergym version from 3.3.8 to 3.4.0 --- .gitignore | 5 ++++- sinergym/config/modeling.py | 26 ++++++++++++++++++-------- sinergym/version.txt | 2 +- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 48ec158cb8..355ab7cbda 100644 --- a/.gitignore +++ b/.gitignore @@ -49,4 +49,7 @@ codecov coverage.xml #wandb -wandb/ \ No newline at end of file +wandb/ + +#fcntl +.lock \ No newline at end of file diff --git a/sinergym/config/modeling.py b/sinergym/config/modeling.py index b51e466a31..8f33da3e40 100644 --- a/sinergym/config/modeling.py +++ b/sinergym/config/modeling.py @@ -1,4 +1,5 @@ """Class and utilities for backend modeling in Python with Sinergym (extra params, weather_variability, building model modification and files management)""" +import fcntl import json import os import random @@ -525,14 +526,23 @@ def _set_experiment_working_dir(self, env_name: str) -> str: Returns: str: Experiment path for directory created. """ - # Generate experiment dir path - experiment_path = self._get_working_folder( - directory_path=CWD, - base_name='-%s-res' % - (env_name)) - # Create dir - os.makedirs(experiment_path) - # set path like config attribute + # CRITICAL SECTION for paralell execution + # In order to avoid several folders with the same name + with open(CWD + '/.lock', 'w') as f: + # Exclusive lock + fcntl.flock(f, fcntl.LOCK_EX) + try: + # Generate experiment dir path + experiment_path = self._get_working_folder( + directory_path=CWD, + base_name='-%s-res' % + (env_name)) + # Create dir + os.makedirs(experiment_path) + finally: + # Release lock + fcntl.flock(f, fcntl.LOCK_UN) + # Set path like config attribute self.experiment_path = experiment_path self.logger.info( diff --git a/sinergym/version.txt b/sinergym/version.txt index 7cb75caa9d..fbcbf73806 100644 --- a/sinergym/version.txt +++ b/sinergym/version.txt @@ -1 +1 @@ -3.3.8 \ No newline at end of file +3.4.0 \ No newline at end of file