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

Add fast operator evaluation for tensor-product discretizations #362

Draft
wants to merge 65 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
8bd3969
start updating grad
a-alveyblanc Jul 25, 2024
0afe732
add TODOs
a-alveyblanc Jul 25, 2024
9c4c150
add strong form tensor product gradient
a-alveyblanc Jul 26, 2024
c264cf9
Merge branch 'main' of https://github.com/inducer/grudge into tensor-…
a-alveyblanc Aug 31, 2024
e105a64
start working on weak-overint case for TP elements
a-alveyblanc Sep 5, 2024
a3f7f7e
start brainstorming better ways to handle weak overintegration
a-alveyblanc Sep 6, 2024
830c2e0
small changes
a-alveyblanc Sep 6, 2024
102f6e0
do not support overintegration (yet)
a-alveyblanc Sep 6, 2024
597fcc9
fix gradient tests
a-alveyblanc Sep 6, 2024
790ab13
fix tensor product gradient
a-alveyblanc Sep 6, 2024
91e3121
do not compute stiffness matrix; apply mass to all axes and diff op t…
a-alveyblanc Sep 6, 2024
9ff324b
arg name change
a-alveyblanc Sep 6, 2024
7b50e32
add fast operator eval for weak and strong divergence
a-alveyblanc Sep 6, 2024
230c1bd
add tensor product inverse mass
a-alveyblanc Sep 6, 2024
30f984e
tag mass operator as such
a-alveyblanc Sep 6, 2024
92b54a2
add fast operator eval for mass operator application
a-alveyblanc Sep 7, 2024
34b1cd6
toward overintegration support
a-alveyblanc Sep 7, 2024
b8e312e
minor changes to adjust for changes in pytato
a-alveyblanc Sep 8, 2024
a79a603
adjust operators to accept 1D tensor product discretizations
a-alveyblanc Sep 8, 2024
51884a7
Merge branch 'main' of https://github.com/inducer/grudge into tensor-…
a-alveyblanc Sep 9, 2024
c8d0a92
checkpoint before adding wadg + overintegration + fast operator evalu…
a-alveyblanc Sep 12, 2024
ad0d2e4
tag axes of grad result
a-alveyblanc Sep 12, 2024
ca22480
add WADG + overintegration for simplices
a-alveyblanc Sep 12, 2024
092a6cc
add WADG + overintegration for simplices
a-alveyblanc Sep 12, 2024
bea91ad
start fixing up fast operator eval + overintegration + wadg
a-alveyblanc Sep 13, 2024
8182571
small changes
a-alveyblanc Sep 16, 2024
4cacc6b
overintegration + fast operator evaluation
a-alveyblanc Sep 19, 2024
73ba29a
first round of clean-ups
a-alveyblanc Sep 19, 2024
0a431d4
start updating face mass for TP
a-alveyblanc Sep 23, 2024
3dfaeec
undo face mass changes; add rough version of bilinear form evaluator
a-alveyblanc Oct 4, 2024
851a1f9
rename bilinear forms file; add rough draft of SeparableBilinearForm
a-alveyblanc Oct 4, 2024
91c87df
minor change: fix typing on generic dispatching function
a-alveyblanc Oct 4, 2024
351ae62
changes after review; bilinear forms now only internal
a-alveyblanc Oct 8, 2024
d174c40
get things working with quadrature; improve test_mass_operator_inverse
a-alveyblanc Oct 13, 2024
21a5855
remove unused variable
a-alveyblanc Oct 13, 2024
4e8b5f8
remove large refined mesh files
a-alveyblanc Oct 13, 2024
16f7f36
add default quadrature for computing bilinear forms
a-alveyblanc Oct 22, 2024
4d8d468
get all tests passing with quadrature rules + numpy array context
a-alveyblanc Oct 27, 2024
5b53482
fix merge conflicts
a-alveyblanc Oct 27, 2024
0a91985
some ruff fixes
a-alveyblanc Oct 28, 2024
53abda5
fix failing MPI wave op test
a-alveyblanc Oct 29, 2024
943f9ca
add redundant mass/inverse mass mappers
a-alveyblanc Oct 31, 2024
fa5b24e
remove 2x refined gh-339 mesh
a-alveyblanc Nov 1, 2024
5c74b6e
toward transformations
a-alveyblanc Nov 9, 2024
8898863
resolve merge conflicts
a-alveyblanc Nov 9, 2024
19f7a49
some dag rewriter work; restrict fast operator eval to non-overintegr…
a-alveyblanc Nov 13, 2024
2dfd7ff
rewrite operators to be more predictable in the DAG
a-alveyblanc Nov 13, 2024
9359246
remove tagging for now
a-alveyblanc Nov 13, 2024
da6dff7
bypass WADG if base and quad discretizations are the same
a-alveyblanc Nov 14, 2024
3f88f04
initial algebraic dag xforms for tp
a-alveyblanc Nov 16, 2024
5bd67ed
algebraic transforms v0.1
a-alveyblanc Nov 17, 2024
5a95360
basic (more like primitive) parallelization scheme; add more dag tran…
a-alveyblanc Nov 17, 2024
8c5be5a
ruff fixes
a-alveyblanc Nov 17, 2024
aad468a
changes to make ruff and pylint happy
a-alveyblanc Nov 17, 2024
7f8fb0e
ruff fix
a-alveyblanc Nov 17, 2024
654dd36
remove ghost nodes left over after TP DAG xforms
a-alveyblanc Nov 18, 2024
7819616
update some docs; fix dim from num faces computation
a-alveyblanc Nov 19, 2024
6a84f74
op refactor updates + transform updates
a-alveyblanc Dec 16, 2024
f6fd35d
move matrices to their own file and update operators accordingly
a-alveyblanc Dec 20, 2024
5b23379
all tests passing except mpi tests
a-alveyblanc Dec 24, 2024
3b69323
all tests passing; disable xforms for now
a-alveyblanc Dec 24, 2024
65cfe02
attempt to resolve missing axis tags; minor transform changes
a-alveyblanc Dec 30, 2024
d46246a
track down final culprit for untagged axes
a-alveyblanc Dec 31, 2024
a70cdb3
add name hints and propert tags to matrices
a-alveyblanc Jan 3, 2025
567637c
re-implementation of tp transforms
a-alveyblanc Jan 5, 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
Prev Previous commit
Next Next commit
basic (more like primitive) parallelization scheme; add more dag tran…
…sforms
  • Loading branch information
a-alveyblanc committed Nov 17, 2024
commit 5a95360bd9a73fb622d09d92816042ee3cee14f1
11 changes: 10 additions & 1 deletion examples/euler/acoustic_pulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,20 @@ def rhs(t, q):
vis = make_visualizer(dcoll)

# {{{ time stepping
import time

step = 0
t = 0.0
elapsed = 0.0
while t < final_time:
if step % 10 == 0:
norm_q = actx.to_numpy(op.norm(dcoll, fields, 2))
logger.info("[%04d] t = %.5f |q| = %.5e", step, t, norm_q)
if step != 0:
logger.info("[%04d] t = %.5f |q| = %.5e time per step = %.5f",
step, t, norm_q, elapsed / step)
else:
logger.info("[%04d] t = %.5f |q| = %.5e time per step = %.5f",
step, t, norm_q, 0)
if visualize:
vis.write_vtk_file(
f"{exp_name}-{step:04d}.vtu",
Expand All @@ -204,8 +211,10 @@ def rhs(t, q):
)
assert norm_q < 5

start = time.time()
fields = actx.thaw(actx.freeze(fields))
fields = rk4_step(fields, t, dt, compiled_rhs)
elapsed += time.time() - start
t += dt
step += 1

Expand Down
118 changes: 104 additions & 14 deletions grudge/array_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
# {{{ imports

import logging

import loopy as lp

from collections.abc import Callable, Mapping
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Optional
Expand All @@ -59,6 +62,7 @@

from grudge.transform.metadata import (
OutputIsTensorProductDOFArrayOrdered,
TensorProductDOFAxisTag,
)


Expand Down Expand Up @@ -221,6 +225,9 @@ class PytatoPyOpenCLArrayContext(_PytatoPyOpenCLArrayContextBase):
which there isn't any, for now.)
"""

dot_codes_before: list[str] = []
dot_codes_after: list[str] = []

def __init__(self, queue, allocator=None,
*,
compile_trace_callback: Callable[[Any, str, Any], None] | None
Expand All @@ -246,25 +253,29 @@ def transform_dag(self, dag):
# {{{ tensor-product algebraic DAG rewrites

num_nodes_before = get_num_nodes(dag)
# step 1: distribute mass inverse through DAG, across index lambdas
dag = InverseMassPropagator()(dag)
self.dot_codes_before.append(pt.get_dot_graph(dag))
if 1:
# step 1: distribute mass inverse through DAG, across index lambdas
dag = InverseMassPropagator()(dag)

# step 2: remove mass-times-mass-inverse einsums
dag = InverseMassRemover()(dag)
# step 2: remove mass-times-mass-inverse einsums
dag = InverseMassRemover()(dag)

# step 3: create new operator out of inverse mass times stiffness
dag = MassInverseTimesStiffnessSimplifier()(dag)
num_nodes_after = get_num_nodes(dag)

if num_nodes_before != num_nodes_after:
logger.info("tensor-product xforms: %d nodes via tensor-product "
"algebraic transformations ",
num_nodes_before - num_nodes_after)
# step 3: create new operator out of inverse mass times stiffness
dag = MassInverseTimesStiffnessSimplifier()(dag)

# }}}

dag = pt.transform.materialize_with_mpms(dag)
# dag = pt.transform.materialize_with_mpms(dag)
dag = deduplicate_data_wrappers(dag)
num_nodes_after = get_num_nodes(dag)
self.dot_codes_after.append(pt.get_dot_graph(dag))

if num_nodes_before != num_nodes_after:
logger.info("tensor-product xforms: removed %d nodes via "
"tensor-product algebraic transformations + "
"deduplication",
(num_nodes_before - num_nodes_after))

def eliminate_reshapes_of_data_wrappers(ary):
if (isinstance(ary, pt.Reshape)
Expand All @@ -278,10 +289,89 @@ def eliminate_reshapes_of_data_wrappers(ary):
dag = pt.transform.map_and_copy(dag,
eliminate_reshapes_of_data_wrappers)

def materialize_all_einsums_or_reduces(expr):
from pytato.raising import (index_lambda_to_high_level_op,
ReduceOp)

if isinstance(expr, pt.Einsum):
return expr.tagged(pt.tags.ImplStored())
elif (isinstance(expr, pt.IndexLambda)
and isinstance(index_lambda_to_high_level_op(expr), ReduceOp)):
return expr.tagged(pt.tags.ImplStored())
else:
return expr

# logger.info("transform_dag.materialize_all_einsums_or_reduces")
# dag = pt.transform.map_and_copy(dag, materialize_all_einsums_or_reduces)

logger.info("transform_dag.infer_axes_tags")
from grudge.transform.metadata import unify_discretization_entity_tags
dag = unify_discretization_entity_tags(dag)

def untag_loopy_call_results(expr):
from pytato.loopy import LoopyCallResult
if isinstance(expr, LoopyCallResult):
return expr.copy(tags=frozenset(),
axes=(pt.Axis(frozenset()),)*expr.ndim)
else:
return expr

dag = pt.transform.map_and_copy(dag, untag_loopy_call_results)

def _untag_impl_stored(expr):
if isinstance(expr, pt.InputArgumentBase):
return expr
else:
return expr.without_tags(pt.tags.ImplStored(),
verify_existence=False)

dag = pt.make_dict_of_named_arrays({
name: _untag_impl_stored(named_ary.expr)
for name, named_ary in dag.items()})

return dag

def transform_loopy_program(self, t_unit):
# NOTE: fix tag propagation before trying to implement transforms here
knl = t_unit.default_entrypoint

redn_inames = []
for insn in knl.instructions:
redn_inames = redn_inames + list(insn.reduction_inames())
redn_inames = frozenset(redn_inames)

discr_iname_to_inames = {}
for iname in knl.inames:
if knl.inames[iname].tags_of_type(DiscretizationElementAxisTag):
key = "iel"

if iname not in redn_inames:
discr_iname_to_inames.setdefault(key, []).append(iname)

if knl.inames[iname].tags_of_type(TensorProductDOFAxisTag):
key = "idof"
tag, = knl.inames[iname].tags_of_type(TensorProductDOFAxisTag)
key += f"_{tag.iaxis}"

if iname not in redn_inames:
discr_iname_to_inames.setdefault(key, []).append(iname)

for discr_iname, inames in discr_iname_to_inames.items():
if discr_iname == "iel":
knl = lp.rename_inames(knl, inames, discr_iname,
existing_ok=True)
if discr_iname == "idof":
knl = lp.rename_inames(knl, inames, discr_iname,
existing_ok=True)

for iname in knl.inames:
if iname == "iel":
knl = lp.tag_inames(knl, [(iname, "g.0")])
if "idof" in iname:
# knl = lp.tag_inames(knl, [(iname, f"l.{iname[-1]}")])
knl = lp.split_iname(knl, iname, 8, inner_tag=f"l.{iname[-1]}")

t_unit = t_unit.with_kernel(knl)
t_unit = lp.set_options(t_unit, "insert_gbarriers")
return t_unit

# }}}
Expand Down
11 changes: 6 additions & 5 deletions grudge/transform/mappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ def map_einsum(self, expr, *args, **kwargs):

d = mass_inverse @ stiffness

from grudge.transform.metadata import AxisIgnoredForPropagationTag
d_axes = []
for ax in d.axes:
d_axes.append(ax.tagged(AxisIgnoredForPropagationTag()))
d = d.copy(axes=tuple(d_axes))

new_args = [d, data]
new_access_descriptors = [
expr.access_descriptors[iarg_stiffness],
Expand Down Expand Up @@ -167,11 +173,6 @@ class InverseMassPropagator(CopyMapperWithExtraArgs):
The goal of this mapper is to remove redundant mass-times-mass inverse
operations from an expression graph of operations involved with a
tensor-product discretization.

Once an inverse mass operator is identified, this mapper uses
:class:`MassRemoverMapper` to find and remove the corresponding mass
operator based on the output axis of the einsum that the inverse mass is an
argument of.
"""
def map_einsum(self, expr, *args, **kwargs):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't do this varargs-style.

new_args = []
Expand Down
39 changes: 20 additions & 19 deletions grudge/transform/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,26 @@ class TensorProductStiffnessOperatorTag(Tag):
class AxesTagsEquationCollector(BaseAxesTagsEquationCollector):
def map_reshape(self, expr: pt.Reshape) -> None:
super().map_reshape(expr)

if (expr.size > 0
and (1 not in (expr.array.shape)) # leads to ambiguous newaxis
and (set(expr.shape) <= (set(expr.array.shape) | {1}))):
i_in_axis = 0
for i_out_axis, dim in enumerate(expr.shape):
if dim != 1:
assert dim == expr.array.shape[i_in_axis]
self.record_equation(
self.get_var_for_axis(expr.array,
i_in_axis),
self.get_var_for_axis(expr,
i_out_axis)
)
i_in_axis += 1
else:
# print(f"Skipping: {expr.array.shape} -> {expr.shape}")
# Wacky reshape => bail.
pass
#
# if (expr.size > 0
# and (1 not in (expr.array.shape)) # leads to ambiguous newaxis
# and (set(expr.shape) <= (set(expr.array.shape) | {1}))):
# i_in_axis = 0
# for i_out_axis, dim in enumerate(expr.shape):
# if dim != 1:
# assert dim == expr.array.shape[i_in_axis]
# self.record_equation(
# self.get_var_for_axis(expr.array,
# i_in_axis),
# self.get_var_for_axis(expr,
# i_out_axis)
# )
# i_in_axis += 1
# else:
# # print(f"Skipping: {expr.array.shape} -> {expr.shape}")
# # Wacky reshape => bail.
# pass
# pass


def unify_discretization_entity_tags(expr: ArrayContainer | ArrayOrNames
Expand Down
Loading