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
checkpoint before adding wadg + overintegration + fast operator evalu…
…ation
  • Loading branch information
a-alveyblanc committed Sep 12, 2024
commit c8d0a926e89f26e59b4a70e70ca581f89ddc6ca1
167 changes: 83 additions & 84 deletions grudge/op.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
from grudge.dof_desc import (
DD_VOLUME_ALL,
DISCR_TAG_BASE,
DISCR_TAG_QUAD,
FACE_RESTR_ALL,
DOFDesc,
VolumeDomainTag,
Expand Down Expand Up @@ -209,15 +210,16 @@ def _single_axis_derivative_kernel(
actx,
data=tuple(
# r for rst axis
actx.einsum("rej,rij,ej->ei" if metric_in_matvec else "rei,rij,ej->ei",
ijm_i[xyz_axis],
get_diff_mat(
actx,
out_element_group=out_grp,
in_element_group=in_grp),
vec_i,
arg_names=("inv_jac_t", "ref_stiffT_mat", "vec", ),
tagged=(FirstAxisIsElementsTag(),))
actx.einsum(
"rej,rij,ej->ei" if metric_in_matvec else "rei,rij,ej->ei",
ijm_i[xyz_axis],
get_diff_mat(
actx,
out_element_group=out_grp,
in_element_group=in_grp),
vec_i,
arg_names=("inv_jac_t", "ref_stiffT_mat", "vec", ),
tagged=(FirstAxisIsElementsTag(),))

for out_grp, in_grp, vec_i, ijm_i in zip(
out_discr.groups, in_discr.groups, vec,
Expand All @@ -233,18 +235,22 @@ def compute_simplicial_gradient(actx, out_grp, in_grp, vec, ijm,
metric_in_matvec):
return actx.einsum(
"xrej,rij,ej->xei" if metric_in_matvec else "xrei,rij,ej->xei",
ijm,
get_diff_mat(
actx,
out_element_group=out_grp,
in_element_group=in_grp
),
vec,
arg_names=("inv_jac_t", "ref_stiffT_mat", "vec"),
tagged=(FirstAxisIsElementsTag(),))
ijm,
get_diff_mat(
actx,
out_element_group=out_grp,
in_element_group=in_grp),
vec,
arg_names=("inv_jac_t", "ref_stiffT_mat", "vec"),
tagged=(FirstAxisIsElementsTag(),))

def compute_tensor_product_gradient(actx, out_grp, in_grp, vec, ijm,
metric_in_matvec):
vec = actx.einsum(
"xrej,ej->xrej", ijm, vec,
tagged=(FirstAxisIsElementsTag(),),
arg_names=("inv_jac_t", "dofs"))

# expose tensor product structure
if in_grp.dim != 1:
vec = fold(in_grp.space, vec)
Expand All @@ -254,62 +260,57 @@ def compute_tensor_product_gradient(actx, out_grp, in_grp, vec, ijm,
mass_mat, stiff_mat = get_diff_mat(actx, out_grp, in_grp)

ref_grad = []
for rst_axis in range(in_grp.dim):
ref_grad.append(vec)

# apply mass matrix to all axes except current axis
for axis in range(in_grp.dim):
if axis == rst_axis:
continue
ref_grad[rst_axis] = single_axis_contraction(
actx, in_grp.dim, axis, mass_mat, ref_grad[rst_axis],
tagged=(FirstAxisIsElementsTag(),
OutputIsTensorProductDOFArrayOrdered()),
arg_names=("mass_1d", f"dofs_{rst_axis}"))
for xyz_axis in range(in_grp.dim):

ref_grad[rst_axis] = single_axis_contraction(
actx, in_grp.dim, rst_axis, stiff_mat,
ref_grad[rst_axis],
ref_grad.append(0.0)
for rst_axis in range(in_grp.dim):
partial = vec[xyz_axis, rst_axis]

# apply mass matrix to all axes except current axis
for axis in range(in_grp.dim):
if axis == rst_axis:
continue

partial = single_axis_contraction(
actx, in_grp.dim, axis, mass_mat, partial,
tagged=(FirstAxisIsElementsTag(),
OutputIsTensorProductDOFArrayOrdered()),
arg_names=("mass_1d", f"dofs_{rst_axis}"))

partial = single_axis_contraction(
actx, in_grp.dim, rst_axis, stiff_mat, partial,
tagged=(FirstAxisIsElementsTag(),
OutputIsTensorProductDOFArrayOrdered(),),
arg_names=("stiff_mat",
f"dofs_with_mass_{rst_axis}"))

if in_grp.dim != 1:
ref_grad[rst_axis] = unfold(out_grp.space,
ref_grad[rst_axis])
if in_grp.dim != 1:
partial = unfold(out_grp.space, partial)

ref_grad[xyz_axis] += partial

# strong form ref_gradient
else:
diff_mat = get_diff_mat(actx, out_grp, in_grp)

ref_grad = []
for rst_axis in range(in_grp.dim):
partial = single_axis_contraction(
actx, in_grp.dim, rst_axis, diff_mat, vec,
tagged=(FirstAxisIsElementsTag(),
OutputIsTensorProductDOFArrayOrdered(),),
arg_names=("diff_mat", "dofs"))

if out_grp.dim != 1:
ref_grad.append(unfold(out_grp.space, partial))
else:
ref_grad.append(partial)

ref_grad = actx.np.stack(ref_grad)
return tag_axes(
actx,
{
0: DiscretizationAmbientDimAxisTag(),
1: DiscretizationElementAxisTag(),
2: DiscretizationDOFAxisTag()
},
actx.einsum(
"xrej,rej->xej",
ijm,
ref_grad,
tagged=(FirstAxisIsElementsTag(),),
arg_names=("inv_jac_t", "ref_ref_grad")))
for xyz_axis in range(in_grp.dim):

ref_grad.append(0.0)
for rst_axis in range(in_grp.dim):
partial = single_axis_contraction(
actx, in_grp.dim, rst_axis, diff_mat,
vec[xyz_axis, rst_axis],
tagged=(FirstAxisIsElementsTag(),
OutputIsTensorProductDOFArrayOrdered(),),
arg_names=("diff_mat", "dofs"))

if out_grp.dim != 1:
partial = unfold(out_grp.space, partial)

ref_grad[xyz_axis] += partial

return actx.np.stack(ref_grad)

per_group_grads = []
for out_grp, in_grp, vec_i, ijm_i in zip(
Expand Down Expand Up @@ -709,17 +710,18 @@ def get_ref_stiffness_transpose_mat(out_grp, in_grp):

weights = in_grp.quadrature_rule().weights[:in_grp.order+1]

stiff_mat = np.einsum("c,bz,cz->bc", weights, vand_inv_t, grad_vand)
stiff_mat = actx.freeze(
stiff_mat_1d = np.einsum("c,bz,cz->bc",
weights, vand_inv_t, grad_vand)
stiff_mat_1d = actx.freeze(
tag_axes(
actx,
{ i : TensorProductOperatorAxisTag() for i in range(2) },
actx.from_numpy(stiff_mat)))
actx.from_numpy(stiff_mat_1d)))

mass_mat = reference_mass_matrix(
mass_mat_1d = reference_mass_matrix(
actx, in_element_group=in_grp, out_element_group=out_grp)

return mass_mat, stiff_mat
return mass_mat_1d, stiff_mat_1d

else:

Expand Down Expand Up @@ -753,9 +755,10 @@ def _weak_scalar_grad(dcoll, dd_in, vec):
out_discr = dcoll.discr_from_dd(dd_in.with_discr_tag(DISCR_TAG_BASE))

actx = vec.array_context
inverse_jac_mat = inverse_surface_metric_derivative_mat(actx, dcoll, dd=dd_in,
times_area_element=True,
_use_geoderiv_connection=actx.supports_nonscalar_broadcasting)
inverse_jac_mat = inverse_surface_metric_derivative_mat(actx, dcoll,
dd=dd_in,
times_area_element=True,
_use_geoderiv_connection=actx.supports_nonscalar_broadcasting)

return _gradient_kernel(actx, out_discr, in_discr,
_reference_stiffness_transpose_matrices, inverse_jac_mat, vec,
Expand Down Expand Up @@ -948,8 +951,8 @@ def get_ref_mass_mat(out_grp, in_grp):
MassMatrix1D(),
tag_axes(
actx,
{ i :
TensorProductOperatorAxisTag() for i in range(2) },
{ i : TensorProductOperatorAxisTag()
for i in range(2) },
actx.from_numpy(mp.mass_matrix(basis_1d, nodes_1d))))

else:
Expand All @@ -968,13 +971,12 @@ def get_ref_mass_mat(out_grp, in_grp):
1, in_grp.order+1)

vand = vandermonde(basis_1d.functions, nodes_1d_out)
vand_inv_t = np.linalg.inv(vand).T
in_vand = vandermonde(basis_1d.functions, nodes_1d_in)
vand_inv_t = np.linalg.inv(vand)
o_vand = vandermonde(basis_1d.functions, nodes_1d_in)

weights = in_grp.quadrature_rule().weights[:in_grp.order+1]

mass_matrix = np.einsum("j,ik,jk->ij", weights, vand_inv_t, in_vand)

mass_matrix = np.einsum("j,ik,jk->ij",
weights, vand_inv_t, o_vand)
return actx.freeze(
actx.tag(
MassMatrix1D(),
Expand Down Expand Up @@ -1169,6 +1171,8 @@ def _apply_inverse_mass_operator(
)

def tensor_product_apply_inverse_mass(grp, jac_inv, vec):
vec = jac_inv * vec

if grp.dim != 1:
vec = fold(grp.space, vec)

Expand All @@ -1182,14 +1186,9 @@ def tensor_product_apply_inverse_mass(grp, jac_inv, vec):
arg_names=("ref_inv_mass", "dofs"))

if grp.dim != 1:
vec = unfold(grp.space, vec)

return actx.einsum(
"ei,ei->ei",
jac_inv,
vec,
tagged=(FirstAxisIsElementsTag(),))
return unfold(grp.space, vec)

return vec

def simplicial_apply_inverse_mass(grp, jac_inv, vec):
# Based on https://arxiv.org/pdf/1608.03836.pdf
Expand Down
6 changes: 4 additions & 2 deletions test/test_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from arraycontext import pytest_generate_tests_for_array_contexts
from meshmode.discretization.poly_element import (
InterpolatoryEdgeClusteredGroupFactory,
QuadratureGroupFactory,
QuadratureGroupFactory
)
from meshmode.mesh import (
SimplexElementGroup,
Expand Down Expand Up @@ -76,7 +76,8 @@
])
def test_gradient(actx_factory, form, dim, order, vectorize, nested,
warp_mesh, group_cls, visualize=False):
if form == "weak-overint" and group_cls == TensorProductElementGroup:

if group_cls == TensorProductElementGroup and "overint" in form:
pytest.skip()

actx = actx_factory()
Expand Down Expand Up @@ -246,6 +247,7 @@ def get_flux(u_tpair, dcoll=dcoll):
(True, True)
])
@pytest.mark.parametrize("group_cls", [
SimplexElementGroup,
TensorProductElementGroup
])
def test_divergence(actx_factory, form, dim, order, vectorize, nested,
Expand Down