Skip to content

Commit f41bd4a

Browse files
committed
Short-term fix to Issue #236 - global timestepping now accounts for Method::schedule()
In the longer-term additional changes will be required to get ScheduleInterval working when using wall-clock time for outputs. Complications arise when trying to call Schedule::next for the schedules configured for Method objects. In the future, we will need to either: 1. refactor control flow so that the method only gets called once per PE per cycle 2. refactor internals of Schedule so that its okay to call Schedule::next multiple times on a given PE during a single cycle.
1 parent f999ad2 commit f41bd4a

File tree

5 files changed

+284
-7
lines changed

5 files changed

+284
-7
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Problem: Method-Scheduling test
2+
# Author: Matthew Abruzzo (matthewabruzzo@gmail.com)
3+
#
4+
# The purpose of this test is to ensure that the Enzo-E executes methods at the
5+
# exact simulation times when they are scheduled
6+
7+
Domain {
8+
lower = [-2.0, -2.0];
9+
upper = [ 2.0, 2.0];
10+
}
11+
12+
Mesh {
13+
root_rank = 2;
14+
root_size = [8,8];
15+
root_blocks = [2,2];
16+
}
17+
18+
Boundary {
19+
type = "periodic";
20+
}
21+
22+
Field {
23+
list = ["test"]; # just a dummy field
24+
ghost_depth = 2;
25+
}
26+
27+
Initial {
28+
list = ["value"];
29+
value { test = 0.0; }
30+
}
31+
32+
Stopping {
33+
time = 100.0;
34+
}
35+
36+
Method {
37+
list = ["null", "output_step", "output_list"];
38+
null { dt = 100.0; }
39+
40+
output_step{
41+
field_list = ["test"];
42+
type = "output";
43+
file_name = ["data-%03d.h5", "proc"];
44+
path_name = ["outstep_t_%03.f", "time"];
45+
schedule {
46+
start = 0.0;
47+
step = 25.0;
48+
stop = 75.0;
49+
var = "time";
50+
};
51+
}
52+
53+
output_list{
54+
field_list = ["test"];
55+
type = "output";
56+
file_name = ["data-%03d.h5", "proc"];
57+
path_name = ["outlist_t_%03.f", "time"];
58+
schedule {
59+
list = [1.0,7.0];
60+
var = "time";
61+
}
62+
}
63+
}

src/Cello/control_stopping.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,16 @@ void Block::stopping_begin_()
6565
Method * method;
6666
double dt_block = std::numeric_limits<double>::max();
6767
while ((method = problem->method(index++))) {
68+
// update dt_block based on the maximum timestep allowed by method for
69+
// data stored on the current block (this is done even when method
70+
// might not be scheduled to run)
6871
dt_block = std::min(dt_block,method->timestep(this));
72+
73+
// if applicable, reduce timestep to coincide with method's schedule
74+
Schedule * schedule = method->schedule();
75+
if (schedule != nullptr){
76+
dt_block = schedule->update_timestep(time_,dt_block);
77+
}
6978
}
7079

7180
// Reduce timestep to coincide with scheduled output if needed

src/Cello/io_ScheduleInterval.cpp

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ void ScheduleInterval::set_seconds_interval
7272

7373
//----------------------------------------------------------------------
7474

75+
namespace { // functions in anonymous namespace are local to this file
76+
77+
int steps_to_time_or_before_(double time, double time_start, double time_step) {
78+
// implicit assumption that time_start < time and time_step > 0
79+
int nsteps = int(std::trunc((time - time_start) / time_step));
80+
if ((nsteps * time_step + time_start) > time){
81+
return nsteps - 1;
82+
} else {
83+
return nsteps;
84+
}
85+
}
86+
87+
};
88+
89+
//----------------------------------------------------------------------
90+
7591
bool ScheduleInterval::write_this_cycle ( int cycle, double time) throw()
7692
{
7793
#ifdef DEBUG_SCHEDULE
@@ -92,9 +108,21 @@ bool ScheduleInterval::write_this_cycle ( int cycle, double time) throw()
92108
case schedule_type_time:
93109
{
94110
const bool in_range = (time_start_ <= time && time <= time_stop_);
95-
const bool below_tol = (cello::err_abs(time_next(), time) < tol);
96111

97-
result = in_range && below_tol;
112+
if (in_range >= 0){
113+
int nsteps = steps_to_time_or_before_(time, time_start_, time_step_);
114+
double min_err_abs =
115+
std::min(cello::err_abs(nsteps * time_step_ + time_start_, time),
116+
cello::err_abs((nsteps+1) * time_step_ + time_start_, time));
117+
result = min_err_abs < tol;
118+
} else {
119+
result = false;
120+
}
121+
// the above if-statement is a crude workaround to make this method
122+
// return the correct answer in the scenario when last_ isn't up-to-date.
123+
// It replaced the following code:
124+
// const bool below_tol = (cello::err_abs(time_next(), time) < tol);
125+
// result = in_range && below_tol;
98126

99127
}
100128

@@ -151,9 +179,14 @@ double ScheduleInterval::update_timestep ( double time, double dt)
151179

152180
if (in_range) {
153181

154-
double time_next = this->time_next();
182+
int nsteps = 1 + steps_to_time_or_before_(time, time_start_,
183+
time_step_);
184+
double time_next = nsteps * time_step_ + time_start_;
185+
// the abrove 3 lines implement a crude-workaround to the following to
186+
// ensure correct behavior when last_ isn't properly updated...
187+
//double time_next = this->time_next();
155188

156-
if (time < time_next && time_next < time + dt) {
189+
if (time < time_next && time_next < (time + dt)) {
157190
new_dt = time_next - time;
158191
}
159192
}

src/Cello/io_ScheduleList.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,12 @@ double ScheduleList::update_timestep ( double time, double dt) const throw()
138138

139139
const double time_next = time + dt;
140140

141-
double time_dump;
142-
143141
switch (type_) {
144142

145143
case schedule_type_time:
146144

147145
for (size_t i=0; i<time_list_.size(); i++) {
148-
time_dump = time_list_[i];
146+
double time_dump = time_list_[i];
149147
if (time < time_dump && time_dump < time_next) {
150148
new_dt = time_dump - time;
151149
break;
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
from math import ceil
2+
import os
3+
import os.path
4+
import yt
5+
import libconf # for help reading the parameter file (dependency of yt)
6+
7+
from answer_testing import EnzoETest
8+
9+
_base_file = os.path.basename(__file__)
10+
11+
def _sched_var_type(name):
12+
d = {"cycle" : int, "time" : float,
13+
"seconds" : float, "minutes" : float, "hours" : float}
14+
try:
15+
return d[name]
16+
except KeyError:
17+
raise ValueError(
18+
f"{name!r} is not a known scheduling variable. Those include "
19+
f"{list(name_type_pairs.keys())!r}."
20+
)
21+
22+
def _all_schedule_var_vals(sched_params, var_name, start_val, last_val):
23+
"""
24+
Returns a sequence holding all values of a schedule variable in the
25+
inclusive interval [start_val, last_val]
26+
"""
27+
28+
if sched_params.get('var', None) != var_name:
29+
raise ValueError("This function is expecting for there to be a 'var' "
30+
f"parameter in sched_params with value {var_name!r}")
31+
32+
var_type = _sched_var_type(var_name)
33+
def coerce_arg_(arg_name, arg):
34+
out = var_type(arg)
35+
if out != arg:
36+
raise ValueError(
37+
f"the argument {arg_name!r} has a value {arg} that can't be "
38+
f"losslessly coerced to the {var_type.__name__} dtype "
39+
f"associated with the {var_name!r} scheduling variable."
40+
)
41+
return out
42+
43+
start_val = coerce_arg_("start_val", start_val)
44+
last_val = coerce_arg_("last_val", last_val)
45+
assert last_val > start_val
46+
47+
if ('list' in sched_params) and (len(sched_params) == 2):
48+
return sorted([e for e in sched_params['list']
49+
if ((e >= start_val) and (e <= last_val))])
50+
elif all(k in ('var', 'start', 'stop', 'step') for k in sched_params):
51+
# parse parameters
52+
sched_start = sched_params.get('start', 0)
53+
sched_step = sched_params.get('step', 1)
54+
if (int(sched_start)!=sched_start) or (int(sched_step)!=sched_step):
55+
# we can run into problems with exact floating point values if we
56+
# don't do this...
57+
raise RuntimeError("This function only handles schedule:start and "
58+
"schedule:step values that are integers")
59+
60+
# the schedule:stop parameter is inclusive, so fall back to last_val
61+
# (also inclusive) when schedule:stop isn't specified
62+
sched_stop = sched_params.get('stop', last_val)
63+
64+
# compute vals
65+
if sched_stop < start_val:
66+
return []
67+
cur_val = sched_start
68+
# if applicable, advance cur_val until, cur_val >= start_val
69+
while cur_val <= start_val:
70+
cur_val += sched_step
71+
out = []
72+
while cur_val <= min(last_val, sched_stop):
73+
out.append(cur_val)
74+
cur_val += sched_step
75+
return out
76+
else:
77+
raise ValueError(f"{sched_params!r} doesn't describe a valid schedule")
78+
79+
def scheduled_output_dirs(parameters, var_name, start_val, last_val):
80+
"""
81+
Constructs a generator that iterates over the expected output directories
82+
produced for a given configuration of a given output-method that uses a
83+
schedule parameterized by the scheduling-variable, var_name.
84+
85+
Specifically, this iterates over directories when the scheduling-variable
86+
has values in the inclusive interval [start_val, last_val]. This yields a
87+
2-tuple where the first element gives the directory name and the second
88+
gives the associated value of the scheduling-variable.
89+
90+
Notes
91+
-----
92+
When the scheduling-variable is associated with a floating point variable
93+
(e.g. ``var_name == "time"``), one could imagine that this generator might
94+
slightly mis-predict the exact values of the scheduling-variable when the
95+
output-directories are produced. For that reason, this generator requires
96+
that the schedule:start and schedule:stop parameters are integers, when
97+
they are specified.
98+
99+
This is a fairly generic operation that could be helpful in a lot of
100+
scenarios.
101+
"""
102+
103+
# construct a function to format the path_names parameter for a given value
104+
# of the scheduling parameter
105+
if ('path_name' not in parameters):
106+
raise ValueError("output-method is missing the path_name parameter")
107+
elif not isinstance(parameters['path_name'], (list,tuple,str)):
108+
raise ValueError("path_name parameter has unexpected type")
109+
elif isinstance(parameters['path_name'], str):
110+
def format_path(val): return parameters['path_name']
111+
elif len(parameters['path_name']) == 1:
112+
def format_path(val): return parameters['path_name'][0]
113+
elif any(fmt_v != var_name for fmt_v in parameters['path_name'][1:]):
114+
# we could relax this requirement if we knew the exact mapping between
115+
# the various scheduling variables (i.e. from parsing the log)
116+
raise ValueError("{var_name!r} is expected to be the only variable "
117+
"used for formatting the path_name parameter")
118+
else:
119+
def format_path(val):
120+
n_vars = len(parameters['path_name']) - 1
121+
fmt_arg = tuple(val for i in range(n_vars))
122+
return parameters['path_name'][0] % fmt_arg
123+
124+
if (('schedule' not in parameters) or
125+
(parameters['schedule']['var'] != var_name)):
126+
raise ValueError("output-method must have an associated schedule that "
127+
f"is parameterized by the {var_name!r} variable.")
128+
vals = _all_schedule_var_vals(parameters['schedule'], var_name,
129+
start_val, last_val)
130+
131+
for val in vals:
132+
yield format_path(val), val
133+
134+
135+
class TestMethodScheduleTime(EnzoETest):
136+
"""
137+
The purpose of this test is to ensure that the Enzo-E executes methods at
138+
the exact simulation times at when they are scheduled.
139+
140+
In short, we setup a minimal simulation and use the "null" Method to
141+
enforce an upper-bound on the timestep during each cycle. We also setup 2
142+
occurences of the output method with schedules based on simulation-times,
143+
that include execution times that shouldn't coincide occur when just
144+
considering the maximum timestep. This python code checks when the outputs
145+
are found.
146+
147+
Unlike many other tests - this test does not require comparison against
148+
previous versions of the code. There is an unambiguous "right answer" that
149+
shouldn't change...
150+
"""
151+
152+
parameter_file = "Schedule/Method_Scheduling_time.in"
153+
max_runtime = 5
154+
ncpus = 1
155+
156+
def test_method_schedule_time(self):
157+
# parse the parameters (after they were translated to libconfig format)
158+
with open('parameters.libconfig', 'r') as f:
159+
config = libconf.load(f)
160+
161+
start_t = 0.0
162+
stop_t = config["Stopping"]["time"]
163+
164+
itr = scheduled_output_dirs(config["Method"]["output_step"],
165+
"time", start_t, stop_t)
166+
for dirname, t in itr:
167+
if t != stop_t:
168+
assert os.path.isdir(dirname)
169+
170+
itr = scheduled_output_dirs(config["Method"]["output_list"],
171+
"time", start_t, stop_t)
172+
for dirname, t in itr:
173+
if t != stop_t:
174+
assert os.path.isdir(dirname)

0 commit comments

Comments
 (0)