Skip to content

Commit

Permalink
Enhances pgen to have access to environment specified in a study. (#209)
Browse files Browse the repository at this point in the history
* Add the OUTPUT_PATH and SPECROOT to kwargs for pgen.

* Addition of the spec constructed environment.

* Remove "study_env" from pgen kwargs.

* Update to pgen function parameters.

* Update lulesh examples to have pgen vars.

* Correction to docstring ordering.
  • Loading branch information
Francesco Di Natale authored Dec 17, 2019
1 parent 6679aba commit 9a1409d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
27 changes: 17 additions & 10 deletions maestrowf/maestro.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,13 @@ def cancel_study(args):
return 0


def load_parameter_generator(path, kwargs):
def load_parameter_generator(path, env, kwargs):
"""
Import and load custom parameter Python files.
:param path: Path to a Python file containing the function \
'get_custom_generator'.
:param env: A StudyEnvironment object containing custom information.
:param kwargs: Dictionary containing keyword arguments for the function \
'get_custom_generator'.
:returns: A populated ParameterGenerator instance.
Expand All @@ -109,20 +110,20 @@ def load_parameter_generator(path, kwargs):
spec = importlib.util.spec_from_file_location("custom_gen", path)
f = importlib.util.module_from_spec(spec)
spec.loader.exec_module(f)
return f.get_custom_generator(**kwargs)
return f.get_custom_generator(env, **kwargs)
except ImportError:
try:
# Python 3.3
from importlib.machinery import SourceFileLoader
LOGGER.debug("Using Python 3.4 SourceFileLoader...")
f = SourceFileLoader("custom_gen", path).load_module()
return f.get_custom_generator(**kwargs)
return f.get_custom_generator(env, **kwargs)
except ImportError:
# Python 2
import imp
LOGGER.debug("Using Python 2 imp library...")
f = imp.load_source("custom_gen", path)
return f.get_custom_generator(**kwargs)
return f.get_custom_generator(env, **kwargs)
except Exception as e:
LOGGER.exception(str(e))
raise e
Expand Down Expand Up @@ -183,22 +184,28 @@ def run_study(args):
LOGGER.exception(msg)
raise ArgumentError(msg)

# Addition of the $(SPECROOT) to the environment.
spec_root = os.path.split(args.specification)[0]
spec_root = Variable("SPECROOT", os.path.abspath(spec_root))
environment.add(spec_root)

# Handle loading a custom ParameterGenerator if specified.
if args.pgen:
# 'pgen_args' has a default of an empty list, which should translate
# to an empty dictionary.
kwargs = create_dictionary(args.pargs)
# Copy the Python file used to generate parameters.
shutil.copy(args.pgen, output_path)
parameters = load_parameter_generator(args.pgen, kwargs)

# Add keywords and environment from the spec to pgen args.
kwargs["OUTPUT_PATH"] = output_path
kwargs["SPECROOT"] = spec_root

# Load the parameter generator.
parameters = load_parameter_generator(args.pgen, environment, kwargs)
else:
parameters = spec.get_parameters()

# Addition of the $(SPECROOT) to the environment.
spec_root = os.path.split(args.specification)[0]
spec_root = Variable("SPECROOT", os.path.abspath(spec_root))
environment.add(spec_root)

# Setup the study.
study = Study(spec.name, spec.description, studyenv=environment,
parameters=parameters, steps=steps, out_path=output_path)
Expand Down
6 changes: 6 additions & 0 deletions samples/lulesh/lulesh_sample1_macosx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ description:

env:
variables:
# DEFAULTS FOR MONTECARLO PGEN EXAMPLE
SMIN: 10
SMAX: 30
TRIALS: 50
ITER: 100

OUTPUT_PATH: ./sample_output/lulesh

labels:
Expand Down
6 changes: 6 additions & 0 deletions samples/lulesh/lulesh_sample1_unix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ description:

env:
variables:
# DEFAULTS FOR MONTECARLO PGEN EXAMPLE
SMIN: 10
SMAX: 30
TRIALS: 50
ITER: 100

OUTPUT_PATH: ./sample_output/lulesh

labels:
Expand Down
2 changes: 1 addition & 1 deletion samples/parameterization/custom_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from maestrowf.datastructures.core import ParameterGenerator


def get_custom_generator():
def get_custom_generator(env, **kwargs):
"""
Create a custom populated ParameterGenerator.
Expand Down
2 changes: 1 addition & 1 deletion samples/parameterization/lulesh_custom_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from maestrowf.datastructures.core import ParameterGenerator


def get_custom_generator():
def get_custom_generator(env, **kwargs):
"""
Create a custom populated ParameterGenerator.
Expand Down
10 changes: 5 additions & 5 deletions samples/parameterization/lulesh_montecarlo_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from maestrowf.datastructures.core import ParameterGenerator


def get_custom_generator(**kwargs):
def get_custom_generator(env, **kwargs):
"""
Create a custom populated ParameterGenerator.
Expand All @@ -17,10 +17,10 @@ def get_custom_generator(**kwargs):
:returns: A ParameterGenerator populated with parameters.
"""
p_gen = ParameterGenerator()
trials = int(kwargs.get("trials"))
size_min = int(kwargs.get("smin"))
size_max = int(kwargs.get("smax"))
iterations = int(kwargs.get("iter"))
trials = int(kwargs.get("trials", env.find("TRIALS").value))
size_min = int(kwargs.get("smin", env.find("SMIN").value))
size_max = int(kwargs.get("smax", env.find("SMAX").value))
iterations = int(kwargs.get("iter", env.find("ITER").value))
params = {
"TRIAL": {
"values": [i for i in range(1, trials)],
Expand Down

0 comments on commit 9a1409d

Please sign in to comment.