Skip to content

Commit

Permalink
avoiding code duplication (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-zakir authored Sep 4, 2024
1 parent 37cb28d commit ca0ce46
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 76 deletions.
44 changes: 6 additions & 38 deletions tests/end_to_end/benders/test_bendersEndToEnd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import shutil
import subprocess
from pathlib import Path
import sys
import yaml
import numpy as np
from zipfile import ZipFile, ZIP_DEFLATED

from tests.end_to_end.utils_functions import get_conf, get_mpi_command

# File RESULT_FILE_PATH
# Json file containing
Expand All @@ -21,10 +20,6 @@
# value of the variable
RESULT_FILE_PATH = Path('resultTest.json')

# File CONFIG_FILE_PATH
# yaml file containing executable name
CONFIG_FILE_PATH = Path.cwd() / ".." / ".." / ".." / \
"src" / 'python' / 'config.yaml'


def remove_outputs(study_path):
Expand Down Expand Up @@ -122,10 +117,10 @@ def run_solver(install_dir, solver, tmp_path, allow_run_as_root=False, mpi=False
with open(RESULT_FILE_PATH, 'r') as jsonFile:
expected_results_dict = json.load(jsonFile)

if (solver == "MERGE_MPS"):
solver_executable = get_solver_exe(solver)
if solver == "MERGE_MPS":
solver_executable = get_conf(solver)
else:
solver_executable = get_solver_exe("BENDERS")
solver_executable = get_conf("BENDERS")
pre_command = []

if mpi:
Expand Down Expand Up @@ -157,31 +152,4 @@ def run_solver(install_dir, solver, tmp_path, allow_run_as_root=False, mpi=False
expected_results_dict[instance], tmp_study/"expansion/out.json")


def get_solver_exe(solver: str):
solver_executable = ""
with open(CONFIG_FILE_PATH) as file:
content = yaml.full_load(file)
if content is not None:
solver_executable = content.get(solver)
else:
raise RuntimeError(
"Please check file config.yaml, content is empty")

return solver_executable


def get_mpi_command(allow_run_as_root=False, nproc: int = 1):
MPI_LAUNCHER = ""
MPI_N = ""
nproc_str = str(nproc)
if sys.platform.startswith("win32"):
MPI_LAUNCHER = "mpiexec"
MPI_N = "-n"
return [MPI_LAUNCHER, MPI_N, nproc_str]
elif sys.platform.startswith("linux"):
MPI_LAUNCHER = "mpirun"
MPI_N = "-np"
if (allow_run_as_root):
return [MPI_LAUNCHER, "--allow-run-as-root", MPI_N, nproc_str, "--oversubscribe"]
else:
return [MPI_LAUNCHER, MPI_N, nproc_str, "--oversubscribe"]

42 changes: 4 additions & 38 deletions tests/end_to_end/restart/test_restartBendersEndToEnd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import shutil
import subprocess
from pathlib import Path
import sys
from zipfile import ZIP_DEFLATED, ZipFile
import yaml
import numpy as np

from tests.end_to_end.utils_functions import get_conf, get_mpi_command

# File RESULT_FILE_PATH
# Json file containing
# . One entry by instance to test
Expand All @@ -20,10 +19,6 @@
# * one entry by variable with variable name as key : optimal
# value of the variable

# File CONFIG_FILE_PATH
# yaml file containing executable name
CONFIG_FILE_PATH = Path.cwd() / ".." / ".." / ".." / \
"src" / 'python' / 'config.yaml'


def remove_outputs(study_path):
Expand Down Expand Up @@ -113,10 +108,10 @@ def run_solver(install_dir, tmp_study, instance, allow_run_as_root=False, mpi=Fa

pre_command = []

solver_executable = get_solver_exe()
solver_executable = get_conf("BENDERS")

if mpi:
pre_command = get_mpi_command(allow_run_as_root)
pre_command = get_mpi_command(allow_run_as_root, 2)

executable_path = str(
(Path(install_dir) / Path(solver_executable)).resolve())
Expand All @@ -142,32 +137,3 @@ def run_solver(install_dir, tmp_study, instance, allow_run_as_root=False, mpi=Fa
check_optimization_json_output(
instance, tmp_study/"expansion/out.json")


def get_solver_exe():
solver_executable = ""
with open(CONFIG_FILE_PATH) as file:
content = yaml.full_load(file)
if content is not None:
solver_executable = content.get("BENDERS")
else:
raise RuntimeError(
"Please check file config.yaml, content is empty")

return solver_executable


def get_mpi_command(allow_run_as_root=False):
MPI_LAUNCHER = ""
MPI_N = ""
nproc = "2"
if sys.platform.startswith("win32"):
MPI_LAUNCHER = "mpiexec"
MPI_N = "-n"
return [MPI_LAUNCHER, MPI_N, nproc]
elif sys.platform.startswith("linux"):
MPI_LAUNCHER = "mpirun"
MPI_N = "-np"
if (allow_run_as_root):
return [MPI_LAUNCHER, "--allow-run-as-root", MPI_N, nproc, "--oversubscribe"]
else:
return [MPI_LAUNCHER, MPI_N, nproc, "--oversubscribe"]
40 changes: 40 additions & 0 deletions tests/end_to_end/utils_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
import sys
from pathlib import Path

import yaml

# File CONFIG_FILE_PATH
# yaml file containing executable name
CONFIG_FILE_PATH = Path(os.path.abspath(__file__)).parent / ".." / ".." / \
"src" / 'python' / 'config.yaml'


def get_conf(key: str):
solver_executable = ""
with open(CONFIG_FILE_PATH) as file:
content = yaml.full_load(file)
if content is not None:
solver_executable = content.get(key)
else:
raise RuntimeError(
"Please check file config.yaml, content is empty")

return solver_executable


def get_mpi_command(allow_run_as_root=False, nproc: int = 1):
MPI_LAUNCHER = ""
MPI_N = ""
nproc_str = str(nproc)
if sys.platform.startswith("win32"):
MPI_LAUNCHER = "mpiexec"
MPI_N = "-n"
return [MPI_LAUNCHER, MPI_N, nproc_str]
elif sys.platform.startswith("linux"):
MPI_LAUNCHER = "mpirun"
MPI_N = "-np"
if allow_run_as_root:
return [MPI_LAUNCHER, "--allow-run-as-root", MPI_N, nproc_str, "--oversubscribe"]
else:
return [MPI_LAUNCHER, MPI_N, nproc_str, "--oversubscribe"]

0 comments on commit ca0ce46

Please sign in to comment.