From d2abb984bc5253421f9f41933b534dc570d9635c Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Thu, 1 Mar 2018 20:21:48 -0600 Subject: [PATCH 1/5] [ci skip] Realize state -> phase rename for state-machine style "states". Depends on dagrt!8 --- examples/implicit_euler/implicit_euler.py | 4 +- .../implicit_euler/test_implicit_euler.py | 2 +- examples/stability/mr-max-eigvals.py | 2 +- examples/stability/mr-stability-diagram.py | 2 +- examples/stability/multirate-step-matrix.py | 2 +- examples/stability/step-matrix.py | 2 +- leap/implicit.py | 10 +-- leap/multistep/__init__.py | 20 ++--- leap/multistep/multirate/__init__.py | 12 +-- leap/rk/__init__.py | 17 ++-- leap/step_matrix.py | 22 +++--- leap/transform.py | 78 +++++++++---------- test/test_step_matrix.py | 14 ++-- 13 files changed, 94 insertions(+), 93 deletions(-) diff --git a/examples/implicit_euler/implicit_euler.py b/examples/implicit_euler/implicit_euler.py index 2a073b5..f52e919 100644 --- a/examples/implicit_euler/implicit_euler.py +++ b/examples/implicit_euler/implicit_euler.py @@ -53,8 +53,8 @@ class ImplicitEulerMethod(Method): with CodeBuilder(label="primary") as cb: self._make_primary(cb) - code = DAGCode.create_with_steady_state( - dep_on=cb.state_dependencies, + code = DAGCode.create_with_steady_phase( + dep_on=cb.phase_dependencies, instructions=cb.instructions) from leap.implicit import replace_AssignSolved diff --git a/examples/implicit_euler/test_implicit_euler.py b/examples/implicit_euler/test_implicit_euler.py index 0ad7c6c..313b016 100755 --- a/examples/implicit_euler/test_implicit_euler.py +++ b/examples/implicit_euler/test_implicit_euler.py @@ -131,5 +131,5 @@ if __name__ == "__main__": if len(sys.argv) > 1: exec(sys.argv[1]) else: - from py.test.cmdline import main + from py.test import main main([__file__]) diff --git a/examples/stability/mr-max-eigvals.py b/examples/stability/mr-max-eigvals.py index 4c784b7..f8055e6 100644 --- a/examples/stability/mr-max-eigvals.py +++ b/examples/stability/mr-max-eigvals.py @@ -47,7 +47,7 @@ def main(): }, exclude_variables=["

bootstrap_step"]) - mat = finder.get_state_step_matrix("primary") + mat = finder.get_phase_step_matrix("primary") left = -3 right = 1 diff --git a/examples/stability/mr-stability-diagram.py b/examples/stability/mr-stability-diagram.py index 74b8f61..2cb1fe9 100644 --- a/examples/stability/mr-stability-diagram.py +++ b/examples/stability/mr-stability-diagram.py @@ -38,7 +38,7 @@ def main(): }, exclude_variables=["

bootstrap_step"]) - mat = finder.get_state_step_matrix("primary") + mat = finder.get_phase_step_matrix("primary") if 0: print('Variables: %s' % finder.variables) diff --git a/examples/stability/multirate-step-matrix.py b/examples/stability/multirate-step-matrix.py index c321c00..1c60cb5 100644 --- a/examples/stability/multirate-step-matrix.py +++ b/examples/stability/multirate-step-matrix.py @@ -33,7 +33,7 @@ def main(): }, exclude_variables=["

bootstrap_step"]) - mat = finder.get_state_step_matrix("primary") + mat = finder.get_phase_step_matrix("primary") if 0: print('Variables: %s' % finder.variables) diff --git a/examples/stability/step-matrix.py b/examples/stability/step-matrix.py index 2ad82be..2cb942a 100644 --- a/examples/stability/step-matrix.py +++ b/examples/stability/step-matrix.py @@ -24,7 +24,7 @@ def main(): exclude_variables=["

step"]) print(finder.get_maxima_expressions("primary")) - mat = finder.get_state_step_matrix("primary") + mat = finder.get_phase_step_matrix("primary") print('Variables: %s' % finder.variables) np.set_printoptions(formatter={"all": str}) diff --git a/leap/implicit.py b/leap/implicit.py index 79f7021..f7d891e 100644 --- a/leap/implicit.py +++ b/leap/implicit.py @@ -53,10 +53,10 @@ def replace_AssignSolved(dag, solver_hooks): from dagrt.language import AssignExpression, AssignSolved - new_states = {} + new_phases = {} - for state_name, state in dag.states.items(): - for insn in state.instructions: + for phase_name, phase in dag.phases.items(): + for insn in phase.instructions: if not isinstance(insn, AssignSolved): new_instructions.append(insn) @@ -81,6 +81,6 @@ def replace_AssignSolved(dag, solver_hooks): condition=insn.condition, depends_on=insn.depends_on)) - new_states[state_name] = state.copy(instructions=new_instructions) + new_phases[phase_name] = phase.copy(instructions=new_instructions) - return dag.copy(states=new_states) + return dag.copy(phases=new_phases) diff --git a/leap/multistep/__init__.py b/leap/multistep/__init__.py index 0787df3..3eff65b 100644 --- a/leap/multistep/__init__.py +++ b/leap/multistep/__init__.py @@ -328,11 +328,11 @@ class AdamsBashforthMethod(Method): if self.hist_length == 1: # The first order method requires no bootstrapping. return DAGCode( - states={ - "initial": cb_init.as_execution_state(next_state="primary"), - "primary": cb_primary.as_execution_state(next_state="primary") + phases={ + "initial": cb_init.as_execution_phase(next_phase="primary"), + "primary": cb_primary.as_execution_phase(next_phase="primary") }, - initial_state="initial") + initial_phase="initial") # Bootstrap with CodeBuilder(label="bootstrap") as cb_bootstrap: @@ -343,15 +343,15 @@ class AdamsBashforthMethod(Method): time_id='', time=self.t) cb_bootstrap(self.step, self.step + 1) with cb_bootstrap.if_(self.step, "==", self.hist_length): - cb_bootstrap.state_transition("primary") + cb_bootstrap.phase_transition("primary") return DAGCode( - states={ - "initialization": cb_init.as_execution_state("bootstrap"), - "bootstrap": cb_bootstrap.as_execution_state("bootstrap"), - "primary": cb_primary.as_execution_state("primary"), + phases={ + "initialization": cb_init.as_execution_phase("bootstrap"), + "bootstrap": cb_bootstrap.as_execution_phase("bootstrap"), + "primary": cb_primary.as_execution_phase("primary"), }, - initial_state="initialization") + initial_phase="initialization") def eval_rhs(self, t, y): """Return a node that evaluates the RHS at the given time and diff --git a/leap/multistep/multirate/__init__.py b/leap/multistep/multirate/__init__.py index 421e670..6c55997 100644 --- a/leap/multistep/multirate/__init__.py +++ b/leap/multistep/multirate/__init__.py @@ -729,7 +729,7 @@ class MultiRateMultiStepMethod(Method): if isubstep == 0: with cb.if_(self.bootstrap_step, "==", bootstrap_steps): - cb.state_transition("primary") + cb.phase_transition("primary") cb.exit_step() cb.fence() @@ -1167,12 +1167,12 @@ class MultiRateMultiStepMethod(Method): self.emit_rk_bootstrap(cb_bootstrap) return DAGCode( - states={ - "initialization": cb_init.as_execution_state("bootstrap"), - "bootstrap": cb_bootstrap.as_execution_state("bootstrap"), - "primary": cb_primary.as_execution_state("primary"), + phases={ + "initialization": cb_init.as_execution_phase("bootstrap"), + "bootstrap": cb_bootstrap.as_execution_phase("bootstrap"), + "primary": cb_primary.as_execution_phase("primary"), }, - initial_state="initialization") + initial_phase="initialization") # }}} diff --git a/leap/rk/__init__.py b/leap/rk/__init__.py index 408d404..c421e1c 100644 --- a/leap/rk/__init__.py +++ b/leap/rk/__init__.py @@ -149,6 +149,7 @@ class ButcherTableauMethod(Method): coeff_sum = sum(stage_coeff_sets[name][istage]) assert abs(coeff_sum - self.c[istage]) < 1e-12, ( name, istage, coeff_sum, self.c[istage]) + # }}} # {{{ initialization @@ -340,11 +341,11 @@ class ButcherTableauMethod(Method): # }}} return DAGCode( - states={ - "initial": cb_init.as_execution_state(next_state="primary"), - "primary": cb_primary.as_execution_state(next_state="primary") + phases={ + "initial": cb_init.as_execution_phase(next_phase="primary"), + "primary": cb_primary.as_execution_phase(next_phase="primary") }, - initial_state="initial") + initial_phase="initial") def finish(self, cb, estimate_names, estimate_vars): cb(self.state, estimate_vars[0]) @@ -769,11 +770,11 @@ class LSRK4Method(Method): from dagrt.language import DAGCode return DAGCode( - states={ - "initial": cb_init.as_execution_state(next_state="primary"), - "primary": cb_primary.as_execution_state(next_state="primary") + phases={ + "initial": cb_init.as_execution_phase(next_phase="primary"), + "primary": cb_primary.as_execution_phase(next_phase="primary") }, - initial_state="initial") + initial_phase="initial") # }}} diff --git a/leap/step_matrix.py b/leap/step_matrix.py index 2c000f7..693666c 100644 --- a/leap/step_matrix.py +++ b/leap/step_matrix.py @@ -94,8 +94,8 @@ class StepMatrixFinder(object): def _get_state_variables(self): """Extract all state-related variables from the code.""" all_var_ids = set() - for state in six.itervalues(self.code.states): - for inst in state.instructions: + for phase in six.itervalues(self.code.phases): + for inst in phase.instructions: all_var_ids |= inst.get_written_variables() all_var_ids |= inst.get_read_variables() @@ -112,11 +112,11 @@ class StepMatrixFinder(object): VectorComponent = namedtuple("VectorComponent", "name, index") - def run_symbolic_step(self, state_name, shapes={}): + def run_symbolic_step(self, phase_name, shapes={}): """ `shapes` maps variable names to vector lengths. """ - state = self.code.states[state_name] + phase = self.code.phases[phase_name] from pymbolic import var @@ -143,14 +143,14 @@ class StepMatrixFinder(object): self.context[""] = 0 self.exec_controller.reset() - self.exec_controller.update_plan(state, state.depends_on) - for event in self.exec_controller(state, self): + self.exec_controller.update_plan(phase, phase.depends_on) + for event in self.exec_controller(phase, self): pass return components, initial_vals - def get_maxima_expressions(self, state_name, shapes={}): - components, initial_vals = self.run_symbolic_step(state_name, shapes) + def get_maxima_expressions(self, phase_name, shapes={}): + components, initial_vals = self.run_symbolic_step(phase_name, shapes) lines = [] @@ -184,7 +184,7 @@ class StepMatrixFinder(object): return "\n".join(lines) - def get_state_step_matrix(self, state_name, shapes={}, sparse=False): + def get_phase_step_matrix(self, phase_name, shapes={}, sparse=False): """ `shapes` maps variable names to vector lengths. @@ -193,7 +193,7 @@ class StepMatrixFinder(object): Otherwise returns a numpy object array. """ - components, initial_vals = self.run_symbolic_step(state_name, shapes) + components, initial_vals = self.run_symbolic_step(phase_name, shapes) from pymbolic.mapper.differentiator import DifferentiationMapper from pymbolic.mapper.dependency import DependencyMapper @@ -270,7 +270,7 @@ class StepMatrixFinder(object): def exec_FailStep(self, insn): raise FailStepException() - def exec_StateTransition(self, insn): + def exec_PhaseTransition(self, insn): pass # }}} diff --git a/leap/transform.py b/leap/transform.py index e0fdc68..9e21ffd 100644 --- a/leap/transform.py +++ b/leap/transform.py @@ -58,14 +58,14 @@ def _update_t_by_dt_factor(factor, instructions): for insn in instructions] -def strang_splitting(dag1, dag2, stepping_state): +def strang_splitting(dag1, dag2, stepping_phase): """Given two time advancement routines (in *dag1* and *dag2*), returns a single second-order accurate time advancement routine representing the sum of both of those advancements. :arg dag1: a :class:`dagrt.language.DAGCode` :arg dag2: a :class:`dagrt.language.DAGCode` - :arg stepping_state: the name of the state in *dag1* and *dag2* that carries + :arg stepping_phase: the name of the phase in *dag1* and *dag2* that carries out time stepping to which Strang splitting is to be applied. :returns: a :class:`dagrt.language.DAGCode` """ @@ -91,39 +91,39 @@ def strang_splitting(dag1, dag2, stepping_state): # }}} - all_states = frozenset(dag1.states) | frozenset(dag2.states) - from dagrt.language import DAGCode, ExecutionState - new_states = {} - for state_name in all_states: - state1 = dag1.states.get(state_name) - state2 = dag2.states.get(state_name) + all_phases = frozenset(dag1.phases) | frozenset(dag2.phases) + from dagrt.language import DAGCode, ExecutionPhase + new_phases = {} + for phase_name in all_phases: + phase1 = dag1.phases.get(phase_name) + phase2 = dag2.phases.get(phase_name) substed_s2_insns = [ insn.map_expressions(subst2_mapper) - for insn in state2.instructions] + for insn in phase2.instructions] - if state_name == stepping_state: - assert state1 is not None - assert state2 is not None + if phase_name == stepping_phase: + assert phase1 is not None + assert phase2 is not None from pymbolic import var dt_half = SubstitutionMapper( make_subst_func({"

": var("
") / 2})) - state1_half_dt = [ + phase1_half_dt = [ insn.map_expressions(dt_half) - for insn in state1.instructions] + for insn in phase1.instructions] - if state1.next_state != state2.next_state: + if phase1.next_phase != phase2.next_phase: raise ValueError("DAGs don't agree on default " - "state transition out of state '%s'" - % state_name) + "phase transition out of phase '%s'" + % phase_name) - s2_name = state_name + "_s2" - s3_name = state_name + "_s3" + s2_name = phase_name + "_s2" + s3_name = phase_name + "_s3" - assert s2_name not in all_states - assert s3_name not in all_states + assert s2_name not in all_phases + assert s3_name not in all_phases """ du/dt = A + B @@ -133,31 +133,31 @@ def strang_splitting(dag1, dag2, stepping_state): 3. Starting with u2, solve du / dt = A from t = 1/2 to 1, get u3 4. Return u3 """ - new_states[state_name] = ExecutionState( - next_state=s2_name, - depends_on=state1.depends_on, + new_phases[phase_name] = ExecutionPhase( + next_phase=s2_name, + depends_on=phase1.depends_on, instructions=( _update_t_by_dt_factor(0, _elide_yield_state( - state1_half_dt)))) - new_states[s2_name] = ExecutionState( - next_state=s3_name, - depends_on=state2.depends_on, + phase1_half_dt)))) + new_phases[s2_name] = ExecutionPhase( + next_phase=s3_name, + depends_on=phase2.depends_on, instructions=( _update_t_by_dt_factor(1/2, _elide_yield_state( substed_s2_insns)))) - new_states[s3_name] = ExecutionState( - next_state=state1.next_state, - depends_on=state1.depends_on, - instructions=state1_half_dt) + new_phases[s3_name] = ExecutionPhase( + next_phase=phase1.next_phase, + depends_on=phase1.depends_on, + instructions=phase1_half_dt) else: - from dagrt.transform import fuse_two_states - new_states[state_name] = fuse_two_states(state_name, - state1, - state2.copy(instructions=substed_s2_insns)) + from dagrt.transform import fuse_two_phases + new_phases[phase_name] = fuse_two_phases(phase_name, + phase1, + phase2.copy(instructions=substed_s2_insns)) - if dag1.initial_state != dag2.initial_state: - raise ValueError("DAGs don't agree on initial state") + if dag1.initial_phase != dag2.initial_phase: + raise ValueError("DAGs don't agree on initial phase") - return DAGCode(new_states, dag1.initial_state) + return DAGCode(new_phases, dag1.initial_phase) diff --git a/test/test_step_matrix.py b/test/test_step_matrix.py index ed70271..d077d21 100755 --- a/test/test_step_matrix.py +++ b/test/test_step_matrix.py @@ -63,7 +63,7 @@ def test_step_matrix(method, show_matrix=True, show_dag=False): finder = StepMatrixFinder(code, function_map={"" + component_id: rhs_sym}) - mat = finder.get_state_step_matrix("primary") + mat = finder.get_phase_step_matrix("primary") if show_matrix: print('Variables: %s' % finder.variables) @@ -82,17 +82,17 @@ def test_step_matrix(method, show_matrix=True, show_dag=False): interp = NumpyInterpreter(code, function_map={"" + component_id: rhs}) interp.set_up(t_start=0, dt_start=dt, context={component_id: 15}) - assert interp.next_state == "initial" + assert interp.next_phase == "initial" for event in interp.run_single_step(): pass - assert interp.next_state == "primary" + assert interp.next_phase == "primary" start_values = np.array( [interp.context[v] for v in finder.variables]) for event in interp.run_single_step(): pass - assert interp.next_state == "primary" + assert interp.next_phase == "primary" stop_values = np.array( [interp.context[v] for v in finder.variables]) @@ -139,7 +139,7 @@ def test_step_matrix_vector_state(show_matrix=True, show_dag=False): code, function_map={"" + component_id: rhs_sym}, variables=["" + component_id]) - mat = finder.get_state_step_matrix("primary", + mat = finder.get_phase_step_matrix("primary", shapes={"" + component_id: 3}) if show_matrix: @@ -168,7 +168,7 @@ def test_step_matrix_fast_eval(): code, function_map={"" + component_id: rhs_sym}, variables=["" + component_id]) - mat = finder.get_state_step_matrix("primary", + mat = finder.get_phase_step_matrix("primary", shapes={"" + component_id: 3}) eval_mat = fast_evaluator(mat) @@ -192,7 +192,7 @@ def test_step_matrix_sparse(): dt = var("
") - mat = finder.get_state_step_matrix("primary", + mat = finder.get_phase_step_matrix("primary", shapes={"" + component_id: 3}, sparse=True) -- GitLab From cf9c1d95c1c415ba3c0e39f0f6781f9daf2c3db7 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Thu, 1 Mar 2018 20:22:52 -0600 Subject: [PATCH 2/5] [ci skip] Bump version --- leap/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/leap/version.py b/leap/version.py index 1da70a1..aac0098 100644 --- a/leap/version.py +++ b/leap/version.py @@ -1,2 +1,2 @@ -VERSION = (2015, 1) +VERSION = (2018, 1) VERSION_TEXT = ".".join(str(i) for i in VERSION) -- GitLab From c3a40ee498e4283e29ba6736297350f01a39df5c Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Thu, 1 Mar 2018 20:23:24 -0600 Subject: [PATCH 3/5] [ci skip] Bump dagrt version in setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index a104777..9d4bceb 100644 --- a/setup.py +++ b/setup.py @@ -45,7 +45,7 @@ def main(): "pytools>=2014.1", "pymbolic>=2014.1", "pytest>=2.3", - "dagrt", + "dagrt>=2018.1", "mako", "six", ], -- GitLab From a479572fd11a2ac8bf534f1917b0257cf740318b Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Thu, 1 Mar 2018 22:28:22 -0600 Subject: [PATCH 4/5] Point leap at state-to-phase dagrt temporarily --- examples/implicit_euler/test_implicit_euler.py | 2 +- requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/implicit_euler/test_implicit_euler.py b/examples/implicit_euler/test_implicit_euler.py index 313b016..8066452 100755 --- a/examples/implicit_euler/test_implicit_euler.py +++ b/examples/implicit_euler/test_implicit_euler.py @@ -131,5 +131,5 @@ if __name__ == "__main__": if len(sys.argv) > 1: exec(sys.argv[1]) else: - from py.test import main + from pytest import main main([__file__]) diff --git a/requirements.txt b/requirements.txt index e22f333..65f2ce3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ git+git://github.com/inducer/pytools git+git://github.com/inducer/pymbolic -git+https://gitlab.tiker.net/inducer/dagrt.git \ No newline at end of file +git+https://gitlab.tiker.net/inducer/dagrt.git@state-to-phase -- GitLab From 0307191e82e898ad4c3c65ef1fc453a71cd33a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kl=C3=B6ckner?= Date: Thu, 1 Mar 2018 23:56:58 -0500 Subject: [PATCH 5/5] Point dagrt back requirements.txt back at master --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 65f2ce3..e22f333 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ git+git://github.com/inducer/pytools git+git://github.com/inducer/pymbolic -git+https://gitlab.tiker.net/inducer/dagrt.git@state-to-phase +git+https://gitlab.tiker.net/inducer/dagrt.git \ No newline at end of file -- GitLab