Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a call to an initialize function to generic_cylinders per a sug… #498

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ccf8bba
added a call to an initialize function to generic_cylinders per a sug…
DLWoodruff Mar 25, 2025
3c00a1d
added a custom writer callout to generic cylinders
DLWoodruff Mar 26, 2025
136f4e1
switch to a function that creates the class that is used as the 'modu…
DLWoodruff Mar 27, 2025
d9d9533
Update mpisppy/generic_cylinders.py
DLWoodruff Apr 3, 2025
411f600
implement Ben and Tomas' suggestions
DLWoodruff Apr 3, 2025
1806fb3
Merge branch 'main' into initialize
DLWoodruff Apr 3, 2025
03a9a17
add a test for a class in the module
DLWoodruff Apr 3, 2025
e20ba01
Merge branch 'initialize' of https://github.com/DLWoodruff/mpi-sppy-1…
DLWoodruff Apr 3, 2025
76abab7
fix error in tree creator
DLWoodruff Apr 3, 2025
7e854e5
changed custom writers so there are now four of them; need a test
DLWoodruff Apr 3, 2025
0ab7b0e
fix error in call to getattr
DLWoodruff Apr 4, 2025
71c300c
fix error in use of getattr in generic_cylinders.py
DLWoodruff Apr 4, 2025
57c651f
fixing ef writer call in generic cylinders (working in a bad environm…
DLWoodruff Apr 4, 2025
b9efc49
name change in docs
bknueven Apr 4, 2025
e8e845b
added a smoke test for user supplied write functions for generic_cyli…
DLWoodruff Apr 4, 2025
4dfbaca
Merge branch 'initialize' of https://github.com/DLWoodruff/mpi-sppy-1…
DLWoodruff Apr 4, 2025
a894a9f
fix an error in custom writer for generic cylinders
DLWoodruff Apr 4, 2025
fec12f7
function signature for custom writer corrected
DLWoodruff Apr 4, 2025
975cc8c
needed to give more itertations to test run
DLWoodruff Apr 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions doc/src/generic_cylinders.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,34 @@ ignored.
probably needed on the command line. Consistency with the files in the
pickle directory might not be checked by the program.

A Class in the module
---------------------

If you want to have a class in the module to help create return values
for functions, you will need to have an additional helper file called
``initialize(cfg)``. It is called by ``generic_cylinders.py``
after cfg is populated and can
be used to create a class. Note that the function ``inparser_adder`` cannot
make use of the class because that function is called before ``initialize``.
Here is some sudo-code for this idea:

.. code_block:: python

my_object = None

def inparser_adder(cfg):
cfg.add_to_config('json_file_with_model_params',
...)

def initalize(cfg):
global my_object
my_object = MyClass(cfg)

def scenario_names_creator(num_scens,...):
return my_object.scenario_names_creator(num_scens,...)

def scenario_creator(scen_name, ...):
return my_object.scenario_creator(scen_name, ...)

def scenario_denouement(rank, ...):
return my_object.scenario_denoument(rank, ...)
5 changes: 5 additions & 0 deletions examples/sizes/sizes.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ def sample_tree_scen_creator(sname, stage, sample_branching_factors, seed,
sca["num_scens"] = sample_branching_factors[0] # two-stage problem
return scenario_creator(sname, **sca)

def initialize(cfg):
print("initialize was successfully called."
" BTW: it does nothing for the sizes problem,"
" it is just here for testing purposes.")

######## end helper functions #########

########## a customized rho setter #############
Expand Down
6 changes: 5 additions & 1 deletion mpisppy/generic_cylinders.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,13 @@ def _proper_bundles(cfg):
fname = os.path.basename(model_fname)
sys.path.append(dpath)
module = importlib.import_module(fname)

cfg = _parse_args(module)

# the initialize function is particularly useful for a class in the module
if hasattr(module, "initialize"):
module.initialize(cfg)

bundle_wrapper = None # the default
if _proper_bundles(cfg):
# TBD: remove the need for dill if you are not reading or writing
Expand Down
Loading