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 and FrankD412 committed May 28, 2022
1 parent 53bac8e commit aa3de08
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
22 changes: 17 additions & 5 deletions maestrowf/maestro.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,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 @@ -103,20 +104,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 @@ -194,14 +195,25 @@ 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()

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 aa3de08

Please sign in to comment.