Skip to content
Snippets Groups Projects
dagrt-fusion.py 44.1 KiB
Newer Older
Matt Wala's avatar
Matt Wala committed
#!/usr/bin/env python3
Matt Wala's avatar
Matt Wala committed
"""Study of operator fusion (inlining) for time integration operators in Grudge.
from __future__ import division, print_function

Matt Wala's avatar
Matt Wala committed
__copyright__ = """
Copyright (C) 2015 Andreas Kloeckner
Copyright (C) 2019 Matt Wala
"""

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""


import contextlib
import logging
import numpy as np
import pyopencl as cl
Matt Wala's avatar
Matt Wala committed
import pyopencl.array  # noqa
Matt Wala's avatar
Matt Wala committed
import pytest

import dagrt.language as lang
import pymbolic.primitives as p

from meshmode.dof_array import DOFArray
from meshmode.array_context import PyOpenCLArrayContext

import grudge.symbolic.mappers as gmap
import grudge.symbolic.operators as sym_op
Matt Wala's avatar
Matt Wala committed
from grudge.execution import ExecutionMapper
from grudge.function_registry import base_function_registry
from pymbolic.mapper import Mapper
from pymbolic.mapper.evaluator import EvaluationMapper \
        as PymbolicEvaluationMapper
from pytools import memoize
from pytools.obj_array import flat_obj_array

from grudge import sym, bind, DGDiscretizationWithBoundaries
from leap.rk import LSRK4MethodBuilder
Matt Wala's avatar
Matt Wala committed
from pyopencl.tools import (  # noqa
        pytest_generate_tests_for_pyopencl as pytest_generate_tests)


logging.basicConfig(level=logging.INFO)

logger = logging.getLogger(__name__)

SKIP_TESTS = int(os.environ.get("SKIP_TESTS", 0))
PAPER_OUTPUT = int(os.environ.get("PAPER_OUTPUT", 0))
OUT_DIR = os.environ.get("OUT_DIR", ".")


@contextlib.contextmanager
def open_output_file(filename):
    if not PAPER_OUTPUT:
Matt Wala's avatar
Matt Wala committed
        yield sys.stdout
Matt Wala's avatar
Matt Wala committed
        sys.stdout.flush()
        try:
            outfile = open(os.path.join(OUT_DIR, filename), "w")
            yield outfile
        finally:
            outfile.close()
def dof_array_nbytes(ary: np.ndarray):
    if isinstance(ary, np.ndarray) and ary.dtype.char == "O":
        return sum(
                dof_array_nbytes(ary[idx])
                for idx in np.ndindex(ary.shape))
    else:
        return ary.nbytes
# {{{ topological sort

def topological_sort(stmts, root_deps):
    id_to_stmt = {stmt.id: stmt for stmt in stmts}

    ordered_stmts = []
    satisfied = set()

    def satisfy_dep(name):
        if name in satisfied:
            return

        stmt = id_to_stmt[name]
Matt Wala's avatar
Matt Wala committed
        for dep in sorted(stmt.depends_on):
            satisfy_dep(dep)
        ordered_stmts.append(stmt)
        satisfied.add(name)

    for d in root_deps:
        satisfy_dep(d)

    return ordered_stmts

# }}}


Matt Wala's avatar
Matt Wala committed
# {{{ leap to grudge translation

# Use evaluation, not identity mappers to propagate symbolic vectors to
# outermost level.

class DagrtToGrudgeRewriter(PymbolicEvaluationMapper):
    def __init__(self, context):
        self.context = context

    def map_variable(self, expr):
        return self.context[expr.name]

    def map_call(self, expr):
        raise ValueError("function call not expected")


class GrudgeArgSubstitutor(gmap.SymbolicEvaluator):
    def __init__(self, args):
        super().__init__(context={})
        self.args = args

    def map_grudge_variable(self, expr):
        if expr.name in self.args:
            return self.args[expr.name]
Matt Wala's avatar
Matt Wala committed
        return super().map_variable(expr)


def transcribe_phase(dag, field_var_name, field_components, phase_name,
                     sym_operator):
Matt Wala's avatar
Matt Wala committed
    """Generate a Grudge operator for a Dagrt time integrator phase.
Matt Wala's avatar
Matt Wala committed
    Arguments:

        dag: The Dagrt code object for the time integrator
Matt Wala's avatar
Matt Wala committed

        field_var_name: The name of the simulation variable

        field_components: The number of components (fields) in the variable

Matt Wala's avatar
Matt Wala committed
        phase_name: The name of the phase to transcribe
Matt Wala's avatar
Matt Wala committed

        sym_operator: The Grudge symbolic expression to substitue for the
            right-hand side evaluation in the Dagrt code
    """
    sym_operator = gmap.OperatorBinder()(sym_operator)
    phase = dag.phases[phase_name]

    ctx = {
            "<t>": sym.var("input_t", sym.DD_SCALAR),
            "<dt>": sym.var("input_dt", sym.DD_SCALAR),
            f"<state>{field_var_name}": sym.make_sym_array(
                f"input_{field_var_name}", field_components),
            "<p>residual": sym.make_sym_array(
                "input_residual", field_components),
    }

    rhs_name = f"<func>{field_var_name}"
    output_vars = [v for v in ctx]
    yielded_states = []

    from dagrt.codegen.transform import isolate_function_calls_in_phase
    ordered_stmts = topological_sort(
            isolate_function_calls_in_phase(
                phase,
                dag.get_stmt_id_generator(),
                dag.get_var_name_generator()).statements,
            phase.depends_on)

    for stmt in ordered_stmts:
        if stmt.condition is not True:
            raise NotImplementedError(
                "non-True condition (in statement '%s') not supported"
                % stmt.id)

        if isinstance(stmt, lang.Nop):
            pass

Matt Wala's avatar
Matt Wala committed
        elif isinstance(stmt, lang.Assign):
Loading
Loading full blame...