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

Mitigate (?) memory growth in tests #214

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ pytential/_git_rev.py

*.so
pytential/qbx/target_specific/impl.c

mprofile_*.dat
memray-*.bin
memray-*.html
2 changes: 1 addition & 1 deletion pytential/qbx/direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def get_kernel(self):
def __call__(self, queue, targets, sources, centers, strengths, expansion_radii,
**kwargs):
from sumpy.tools import is_obj_array_like
knl = self.get_cached_optimized_kernel(
knl = self.get_cached_kernel_executor(
targets_is_obj_array=is_obj_array_like(targets),
sources_is_obj_array=is_obj_array_like(sources),
centers_is_obj_array=is_obj_array_like(centers))
Expand Down
7 changes: 4 additions & 3 deletions pytential/qbx/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def copy_targets_kernel(self):
knl = lp.tag_array_axes(knl, "points", "sep, C")

knl = lp.tag_array_axes(knl, "targets", "stride:auto, stride:1")
return lp.tag_inames(knl, {"dim": "ilp"})
knl = lp.tag_inames(knl, {"dim": "ilp"})
return knl.executor(self._setup_actx.context)

@property
@memoize_method
Expand Down Expand Up @@ -192,7 +193,7 @@ def qbx_center_to_target_box_lookup(self, particle_id_dtype, box_id_dtype):
knl = lp.split_iname(knl, "ibox", 128,
inner_tag="l.0", outer_tag="g.0")

return knl
return knl.executor(self._setup_actx.context)

@property
@memoize_method
Expand Down Expand Up @@ -253,7 +254,7 @@ def pick_used_centers(self):
lang_version=MOST_RECENT_LANGUAGE_VERSION)

knl = lp.split_iname(knl, "i", 128, inner_tag="l.0", outer_tag="g.0")
return knl
return knl.executor(self._setup_actx.context)

@property
@memoize_method
Expand Down
8 changes: 4 additions & 4 deletions pytential/qbx/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def __call__(self, queue, **kwargs):
qbx_centers = kwargs.pop("qbx_centers")

from sumpy.tools import is_obj_array_like
return self.get_cached_optimized_kernel(
return self.get_cached_kernel_executor(
is_sources_obj_array=is_obj_array_like(sources),
is_centers_obj_array=is_obj_array_like(qbx_centers),
)(queue, sources=sources, qbx_centers=qbx_centers, **kwargs)
Expand Down Expand Up @@ -272,7 +272,7 @@ def __call__(self, queue, **kwargs):

from sumpy.tools import is_obj_array_like
qbx_centers = kwargs.pop("qbx_centers")
return self.get_cached_optimized_kernel(
return self.get_cached_kernel_executor(
is_centers_obj_array=is_obj_array_like(qbx_centers),
)(queue, centers=centers,
qbx_centers=qbx_centers,
Expand Down Expand Up @@ -382,7 +382,7 @@ def __call__(self, queue, **kwargs):

from sumpy.tools import is_obj_array_like
qbx_centers = kwargs.pop("qbx_centers")
return self.get_cached_optimized_kernel(
return self.get_cached_kernel_executor(
is_centers_obj_array=is_obj_array_like(qbx_centers),
)(queue, centers=centers,
qbx_centers=qbx_centers,
Expand Down Expand Up @@ -499,7 +499,7 @@ def __call__(self, queue, **kwargs):
qbx_centers = kwargs.pop("qbx_centers")

from sumpy.tools import is_obj_array_like
return self.get_cached_optimized_kernel(
return self.get_cached_kernel_executor(
is_targets_obj_array=is_obj_array_like(targets),
is_centers_obj_array=is_obj_array_like(qbx_centers),
)(queue, targets=targets, qbx_centers=qbx_centers, **kwargs)
Expand Down
4 changes: 3 additions & 1 deletion pytential/unregularized.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,9 @@ def copy_targets_kernel(self):
knl = lp.tag_array_axes(knl, "points", "sep, C")

knl = lp.tag_array_axes(knl, "targets", "stride:auto, stride:1")
return lp.tag_inames(knl, {"dim": "ilp"})
knl = lp.tag_inames(knl, {"dim": "ilp"})

return knl.executor(self.cl_context)

@property
@memoize_method
Expand Down
26 changes: 26 additions & 0 deletions pytential/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
__copyright__ = """
Copyright (C) 2020 Matt Wala
Copyright (C) 2023 University of Illinois Board of Trustees
"""

__license__ = """
Expand All @@ -22,6 +23,8 @@
THE SOFTWARE.
"""

import sys


def sort_arrays_together(*arys, key=None):
"""Sort a sequence of arrays by considering them
Expand All @@ -32,4 +35,27 @@ def sort_arrays_together(*arys, key=None):
"""
return zip(*sorted(zip(*arys), key=key))


def pytest_teardown_function():
from pyopencl.tools import clear_first_arg_caches
clear_first_arg_caches()

from sympy.core.cache import clear_cache
clear_cache()

import sumpy
sumpy.code_cache.clear_in_mem_cache()

from loopy import clear_in_mem_caches
clear_in_mem_caches()

import gc
gc.collect()

if sys.platform.startswith("linux"):
import ctypes
libc = ctypes.CDLL("libc.so.6")
libc.malloc_trim(0)


# vim: foldmethod=marker
3 changes: 3 additions & 0 deletions test/test_beltrami.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
import logging
logger = logging.getLogger(__name__)

from pytential.utils import ( # noqa: F401
pytest_teardown_function as teardown_function)

pytest_generate_tests = pytest_generate_tests_for_array_contexts([
PytestPyOpenCLArrayContextFactory,
])
Expand Down
4 changes: 3 additions & 1 deletion test/test_cost_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@
from pytential.qbx.cost import (
QBXCostModel, _PythonQBXCostModel, make_pde_aware_translation_cost_model
)

from meshmode import _acf # noqa: F401
from arraycontext import pytest_generate_tests_for_array_contexts
from meshmode.array_context import PytestPyOpenCLArrayContextFactory

import logging
logger = logging.getLogger(__name__)

from pytential.utils import ( # noqa: F401
pytest_teardown_function as teardown_function)

pytest_generate_tests = pytest_generate_tests_for_array_contexts([
PytestPyOpenCLArrayContextFactory,
])
Expand Down
3 changes: 3 additions & 0 deletions test/test_global_qbx.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
import logging
logger = logging.getLogger(__name__)

from pytential.utils import ( # noqa: F401
pytest_teardown_function as teardown_function)

pytest_generate_tests = pytest_generate_tests_for_array_contexts([
PytestPyOpenCLArrayContextFactory,
])
Expand Down
20 changes: 6 additions & 14 deletions test/test_layer_pot.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

__copyright__ = "Copyright (C) 2013 Andreas Kloeckner"

__license__ = """
Expand Down Expand Up @@ -38,6 +40,9 @@
import logging
logger = logging.getLogger(__name__)

from pytential.utils import ( # noqa: F401
pytest_teardown_function as teardown_function)

pytest_generate_tests = pytest_generate_tests_for_array_contexts([
PytestPyOpenCLArrayContextFactory,
])
Expand Down Expand Up @@ -82,10 +87,6 @@ def test_off_surface_eval(actx_factory, use_fmm, visualize=False):

actx = actx_factory()

# prevent cache 'splosion
from sympy.core.cache import clear_cache
clear_cache()

nelements = 30
target_order = 8
qbx_order = 3
Expand Down Expand Up @@ -137,7 +138,7 @@ def test_off_surface_eval(actx_factory, use_fmm, visualize=False):
pt.colorbar()
pt.show()

assert linf_err < 1e-3
assert linf_err < 2e-3

# }}}

Expand All @@ -149,10 +150,6 @@ def test_off_surface_eval_vs_direct(actx_factory, do_plot=False):

actx = actx_factory()

# prevent cache 'splosion
from sympy.core.cache import clear_cache
clear_cache()

nelements = 300
target_order = 8
qbx_order = 3
Expand Down Expand Up @@ -235,10 +232,6 @@ def test_single_plus_double_with_single_fmm(actx_factory, do_plot=False):

actx = actx_factory()

# prevent cache 'splosion
from sympy.core.cache import clear_cache
clear_cache()

nelements = 300
target_order = 8
qbx_order = 3
Expand Down Expand Up @@ -299,7 +292,6 @@ def test_single_plus_double_with_single_fmm(actx_factory, do_plot=False):

fmm_sigma = fmm_density_discr.zeros(actx) + 1
fmm_bound_op = bind(places, op, auto_where=("fmm_qbx", "target"))
print(fmm_bound_op.code)
fmm_fld_in_vol = fmm_bound_op(actx, sigma=fmm_sigma)

err = actx.np.fabs(fmm_fld_in_vol - direct_fld_in_vol)
Expand Down
3 changes: 3 additions & 0 deletions test/test_layer_pot_eigenvalues.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
import logging
logger = logging.getLogger(__name__)

from pytential.utils import ( # noqa: F401
pytest_teardown_function as teardown_function)

pytest_generate_tests = pytest_generate_tests_for_array_contexts([
PytestPyOpenCLArrayContextFactory,
])
Expand Down
7 changes: 3 additions & 4 deletions test/test_layer_pot_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
import logging
logger = logging.getLogger(__name__)

from pytential.utils import ( # noqa: F401
pytest_teardown_function as teardown_function)

pytest_generate_tests = pytest_generate_tests_for_array_contexts([
PytestPyOpenCLArrayContextFactory,
])
Expand Down Expand Up @@ -234,10 +237,6 @@ def test_identity_convergence(actx_factory, case, visualize=False):

actx = actx_factory()

# prevent cache 'splosion
from sympy.core.cache import clear_cache
clear_cache()

target_order = 8

from pytools.convergence import EOCRecorder
Expand Down
3 changes: 3 additions & 0 deletions test/test_linalg_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
import logging
logger = logging.getLogger(__name__)

from pytential.utils import ( # noqa: F401
pytest_teardown_function as teardown_function)

pytest_generate_tests = pytest_generate_tests_for_array_contexts([
PytestPyOpenCLArrayContextFactory,
])
Expand Down
3 changes: 3 additions & 0 deletions test/test_linalg_skeletonization.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
import logging
logger = logging.getLogger(__name__)

from pytential.utils import ( # noqa: F401
pytest_teardown_function as teardown_function)

pytest_generate_tests = pytest_generate_tests_for_array_contexts([
PytestPyOpenCLArrayContextFactory,
])
Expand Down
3 changes: 3 additions & 0 deletions test/test_linalg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
import logging
logger = logging.getLogger(__name__)

from pytential.utils import ( # noqa: F401
pytest_teardown_function as teardown_function)

pytest_generate_tests = pytest_generate_tests_for_array_contexts([
PytestPyOpenCLArrayContextFactory,
])
Expand Down
19 changes: 3 additions & 16 deletions test/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
import logging
logger = logging.getLogger(__name__)

from pytential.utils import ( # noqa: F401
pytest_teardown_function as teardown_function)

pytest_generate_tests = pytest_generate_tests_for_array_contexts([
PytestPyOpenCLArrayContextFactory,
])
Expand Down Expand Up @@ -76,10 +79,6 @@ def test_build_matrix(actx_factory, k, curve_fn, op_type, visualize=False):

actx = actx_factory()

# prevent cache 'splosion
from sympy.core.cache import clear_cache
clear_cache()

case = extra.CurveTestCase(
name="curve",
knl_class_or_helmholtz_k=k,
Expand Down Expand Up @@ -196,10 +195,6 @@ def test_build_matrix_conditioning(actx_factory, side, op_type, visualize=False)

actx = actx_factory()

# prevent cache explosion
from sympy.core.cache import clear_cache
clear_cache()

case = extra.CurveTestCase(
name="ellipse",
curve_fn=lambda t: ellipse(3.0, t),
Expand Down Expand Up @@ -306,10 +301,6 @@ def test_cluster_builder(actx_factory, ambient_dim,

actx = actx_factory()

# prevent cache explosion
from sympy.core.cache import clear_cache
clear_cache()

if ambient_dim == 2:
case = extra.CurveTestCase(
name="ellipse",
Expand Down Expand Up @@ -425,10 +416,6 @@ def test_build_matrix_fixed_stage(actx_factory,

actx = actx_factory()

# prevent cache explosion
from sympy.core.cache import clear_cache
clear_cache()

case = extra.CurveTestCase(
name="starfish",
curve_fn=NArmedStarfish(5, 0.25),
Expand Down
3 changes: 3 additions & 0 deletions test/test_maxwell.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
import logging
logger = logging.getLogger(__name__)

from pytential.utils import ( # noqa: F401
pytest_teardown_function as teardown_function)

pytest_generate_tests = pytest_generate_tests_for_array_contexts([
PytestPyOpenCLArrayContextFactory,
])
Expand Down
7 changes: 3 additions & 4 deletions test/test_scalar_int_eq.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
import logging
logger = logging.getLogger(__name__)

from pytential.utils import ( # noqa: F401
pytest_teardown_function as teardown_function)

pytest_generate_tests = pytest_generate_tests_for_array_contexts([
PytestPyOpenCLArrayContextFactory,
])
Expand Down Expand Up @@ -477,10 +480,6 @@ def test_integral_equation(actx_factory, case, visualize=False):

actx = actx_factory()

# prevent cache 'splosion
from sympy.core.cache import clear_cache
clear_cache()

from pytools.convergence import EOCRecorder
logger.info("\n%s", str(case))

Expand Down
3 changes: 3 additions & 0 deletions test/test_stokes.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
import logging
logger = logging.getLogger(__name__)

from pytential.utils import ( # noqa: F401
pytest_teardown_function as teardown_function)

pytest_generate_tests = pytest_generate_tests_for_array_contexts([
PytestPyOpenCLArrayContextFactory,
])
Expand Down
Loading