Skip to content

Commit

Permalink
implement hierachical matrix solver
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfikl committed Sep 14, 2022
1 parent d067a49 commit 871c491
Show file tree
Hide file tree
Showing 11 changed files with 1,265 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ examples/*.pdf

*.vtu
*.vts
*.png
*.gif

.cache

Expand Down
198 changes: 198 additions & 0 deletions examples/scaling-study-hmatrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
__copyright__ = "Copyright (C) 2022 Alexandru Fikl"

__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.
"""

from dataclasses import dataclass

import numpy as np

from meshmode.array_context import PyOpenCLArrayContext
from pytools.convergence import EOCRecorder

from pytential import GeometryCollection, sym

import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


@dataclass(frozen=True)
class Timings:
build: float
matvec: float


def run_hmatrix_matvec(
actx: PyOpenCLArrayContext,
places: GeometryCollection, *,
dofdesc: sym.DOFDescriptor) -> None:
from sumpy.kernel import LaplaceKernel
kernel = LaplaceKernel(places.ambient_dim)
sym_u = sym.var("u")
sym_op = -0.5 * sym_u + sym.D(kernel, sym_u, qbx_forced_limit="avg")

density_discr = places.get_discretization(dofdesc.geometry, dofdesc.discr_stage)
u = actx.thaw(density_discr.nodes()[0])

def build_hmat():
from pytential.linalg.hmatrix import build_hmatrix_by_proxy
return build_hmatrix_by_proxy(
actx, places, sym_op, sym_u,
domains=[dofdesc],
context={},
auto_where=dofdesc,
id_eps=1.0e-10,
_tree_kind="adaptive-level-restricted",
_approx_nproxy=64,
_proxy_radius_factor=1.15)

# warmup
from pytools import ProcessTimer
with ProcessTimer() as pt:
hmat = build_hmat()
actx.queue.finish()

logger.info("build(warmup): %s", pt)

# build
with ProcessTimer() as pt:
hmat = build_hmat()
actx.queue.finish()

t_build = pt.wall_elapsed
logger.info("build: %s", pt)

# matvec
with ProcessTimer() as pt:
du = hmat @ u
assert du is not None
actx.queue.finish()

t_matvec = pt.wall_elapsed
logger.info("matvec: %s", pt)

return Timings(t_build, t_matvec)


def run_scaling_study(
ambient_dim: int, *,
target_order: int = 4,
source_ovsmp: int = 4,
qbx_order: int = 4,
) -> None:
dd = sym.DOFDescriptor(f"d{ambient_dim}", discr_stage=sym.QBX_SOURCE_STAGE2)

import pyopencl as cl
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
actx = PyOpenCLArrayContext(queue, force_device_scalars=True)

eoc_build = EOCRecorder()
eoc_matvec = EOCRecorder()

import meshmode.mesh.generation as mgen
import meshmode.discretization.poly_element as mpoly
resolutions = [64, 128, 256, 512, 1024, 1536, 2048, 2560, 3072]

for n in resolutions:
mesh = mgen.make_curve_mesh(
mgen.NArmedStarfish(5, 0.25),
np.linspace(0, 1, n),
order=target_order)

from meshmode.discretization import Discretization
pre_density_discr = Discretization(actx, mesh,
mpoly.InterpolatoryQuadratureGroupFactory(target_order))

from pytential.qbx import QBXLayerPotentialSource
qbx = QBXLayerPotentialSource(
pre_density_discr,
fine_order=source_ovsmp * target_order,
qbx_order=qbx_order,
fmm_order=False, fmm_backend=None,
)
places = GeometryCollection(qbx, auto_where=dd.geometry)
density_discr = places.get_discretization(dd.geometry, dd.discr_stage)

logger.info("ndofs: %d", density_discr.ndofs)
logger.info("nelements: %d", density_discr.mesh.nelements)

timings = run_hmatrix_matvec(actx, places, dofdesc=dd)
eoc_build.add_data_point(density_discr.ndofs, timings.build)
eoc_matvec.add_data_point(density_discr.ndofs, timings.matvec)

for name, eoc in [("build", eoc_build), ("matvec", eoc_matvec)]:
logger.info("%s\n%s",
name, eoc.pretty_print(
abscissa_label="dofs",
error_label=f"{name} (s)",
abscissa_format="%d",
error_format="%.3fs",
eoc_format="%.2f",
)
)
visualize_eoc(f"scaling-study-hmatrix-{name}", eoc, 1)


def visualize_eoc(
filename: str, eoc: EOCRecorder, order: int,
overwrite: bool = False) -> None:
try:
import matplotlib.pyplot as plt
except ImportError:
logger.info("matplotlib not available for plotting")
return

fig = plt.figure(figsize=(10, 10), dpi=300)
ax = fig.gca()

h, error = np.array(eoc.history).T # type: ignore[no-untyped-call]
ax.loglog(h, error, "o-")

max_h = np.max(h)
min_e = np.min(error)
max_e = np.max(error)
min_h = np.exp(np.log(max_h) + np.log(min_e / max_e) / order)

ax.loglog(
[max_h, min_h], [max_e, min_e], "k-", label=rf"$\mathcal{{O}}(h^{order})$"
)

# }}}

ax.grid(True, which="major", linestyle="-", alpha=0.75)
ax.grid(True, which="minor", linestyle="--", alpha=0.5)

ax.set_xlabel("$N$")
ax.set_ylabel("$T~(s)$")

import pathlib
filename = pathlib.Path(filename)
if not overwrite and filename.exists():
raise FileExistsError(f"output file '{filename}' already exists")

fig.savefig(filename)
plt.close(fig)


if __name__ == "__main__":
run_scaling_study(ambient_dim=2)
19 changes: 19 additions & 0 deletions pytential/linalg/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,25 @@ def make_block(i: int, j: int):
else:
raise ValueError(f"unsupported ndarray dimension: '{ndim}'")


def uncluster(ary: np.ndarray, index: IndexList, clevel: ClusterLevel) -> np.ndarray:
assert ary.shape == (clevel.parent_map.size,)

if index.nclusters == 1:
return ary

result = np.empty(index.nclusters, dtype=object)
for ifrom, ppm in enumerate(clevel.parent_map):
offset = 0
for ito in ppm:
cluster_size = index.cluster_size(ito)
result[ito] = ary[ifrom][offset:offset + cluster_size]
offset += cluster_size

assert ary[ifrom].shape == (offset,)

return result

# }}}


Expand Down
12 changes: 8 additions & 4 deletions pytential/linalg/direct_solver_symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
THE SOFTWARE.
"""

from pytools.obj_array import make_obj_array
import numpy as np

from pytools.obj_array import make_obj_array
from pytential.symbolic.mappers import (
IdentityMapper, OperatorCollector, LocationTagger)

Expand All @@ -44,9 +45,12 @@ class PROXY_SKELETONIZATION_TARGET: # noqa: N801

def prepare_expr(places, exprs, auto_where=None):
from pytential.symbolic.execution import _prepare_expr
return make_obj_array([
_prepare_expr(places, expr, auto_where=auto_where)
for expr in exprs])
if isinstance(exprs, np.ndarray):
return make_obj_array([
_prepare_expr(places, expr, auto_where=auto_where)
for expr in exprs])

return _prepare_expr(places, exprs, auto_where=auto_where)


def prepare_proxy_expr(places, exprs, auto_where=None):
Expand Down
Loading

0 comments on commit 871c491

Please sign in to comment.