Skip to content

Instantly share code, notes, and snippets.

@Gabrielcarvfer
Last active July 6, 2022 22:18
Show Gist options
  • Save Gabrielcarvfer/2cf1f1d796b3cacef17554743275e5f8 to your computer and use it in GitHub Desktop.
Save Gabrielcarvfer/2cf1f1d796b3cacef17554743275e5f8 to your computer and use it in GitHub Desktop.
launch_sims.py
import pickle
import os
import glob
import subprocess
import lzma
# Simulation_path is the CWD for the simulation (where results will be saved)
# Base_dir is the path to the directory containing the simulation program
# args is a list of arguments for the simulation program
def execute_simulation(simulation_path, base_dir, args):
simulation_results_compressed_pickle_path = simulation_path + os.sep + "simulationResults.pickle.lzma"
# If simulation was already executed and results compiled into a resulting json, skip execution
if os.path.exists(simulation_results_compressed_pickle_path):
try:
with lzma.open(simulation_results_compressed_pickle_path, "rb") as f:
simulation_results = pickle.load(f)
return simulation_path, simulation_results
except Exception:
# Failed to load results, delete file and re-run the simulation
os.remove(simulation_results_compressed_pickle_path)
print("Failed to load results. Rerunning simulation. Source path: ",
simulation_path)
# Execute simulation
try:
cmd = [base_dir + os.sep + "program_name", *args]
simProc = subprocess.run(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=simulation_path,
env={"OPENBLAS_NUM_THREADS": "1",
},
timeout=8000
)
if simProc.returncode != 0:
raise Exception("ProgramFailed")
except Exception:
print("Failed to execute simulation. Source path: ",
simulation_path)
return simulation_path, None
# Extract and save simulation_results to the simulation path (the program CWD)
# Try dumping results into compressed pickle to skip reprocessing later
try:
with lzma.open(simulation_results_compressed_pickle_path, "wb") as f:
pickle.dump(simulation_results, f)
except Exception:
print("Failed dumping results to compressed pickle file. ",
"Skipping to next simulation. Source path: ",
simulation_path)
return simulation_path, None
print("Finished working on ", simulation_path)
return simulation_path, simulation_results
from concurrent.futures.process import ProcessPoolExecutor
executor = ProcessPoolExecutor(max_workers=15)
sims = []
for (simulation_path, args) in simulationParameterList:
# Run simulation if necessary and try to extract results into a single output file
sims.append(executor.submit(execute_simulation, simulation_path, baseDir, args))
# Simulation results will be in the sims lists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment