diff --git a/examples/autoignition-mpi.py b/examples/autoignition-mpi.py index 440fba8f7..df033ed65 100644 --- a/examples/autoignition-mpi.py +++ b/examples/autoignition-mpi.py @@ -26,7 +26,6 @@ import logging import numpy as np import pyopencl as cl -import pyopencl.tools as cl_tools from functools import partial from meshmode.mesh import BTAG_ALL, BTAG_NONE # noqa @@ -101,13 +100,13 @@ def main(actx_class, ctx_factory=cl.create_some_context, use_logmgr=True, else: queue = cl.CommandQueue(cl_ctx) + from mirgecom.simutil import get_reasonable_memory_pool + alloc = get_reasonable_memory_pool(cl_ctx, queue) + if lazy: - actx = actx_class(comm, queue, mpi_base_tag=12000, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue))) + actx = actx_class(comm, queue, mpi_base_tag=12000, allocator=alloc) else: - actx = actx_class(comm, queue, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)), - force_device_scalars=True) + actx = actx_class(comm, queue, allocator=alloc, force_device_scalars=True) # Some discretization parameters dim = 2 diff --git a/examples/doublemach-mpi.py b/examples/doublemach-mpi.py index 788552e82..816ff0be7 100644 --- a/examples/doublemach-mpi.py +++ b/examples/doublemach-mpi.py @@ -27,7 +27,6 @@ import logging import numpy as np import pyopencl as cl -import pyopencl.tools as cl_tools from functools import partial from meshmode.mesh import BTAG_ALL, BTAG_NONE # noqa @@ -146,14 +145,13 @@ def main(ctx_factory=cl.create_some_context, use_logmgr=True, else: queue = cl.CommandQueue(cl_ctx) + from mirgecom.simutil import get_reasonable_memory_pool + alloc = get_reasonable_memory_pool(cl_ctx, queue) + if lazy: - actx = actx_class(comm, queue, mpi_base_tag=12000, - allocator=cl_tools.MemoryPool( - cl_tools.ImmediateAllocator(queue))) + actx = actx_class(comm, queue, mpi_base_tag=12000, allocator=alloc) else: - actx = actx_class(comm, queue, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)), - force_device_scalars=True) + actx = actx_class(comm, queue, allocator=alloc, force_device_scalars=True) # Timestepping control current_step = 0 diff --git a/examples/heat-source-mpi.py b/examples/heat-source-mpi.py index d8a3277f7..4aff5c7ff 100644 --- a/examples/heat-source-mpi.py +++ b/examples/heat-source-mpi.py @@ -39,7 +39,6 @@ NeumannDiffusionBoundary) from mirgecom.mpi import mpi_entry_point from mirgecom.utils import force_evaluation -import pyopencl.tools as cl_tools from mirgecom.logging_quantities import (initialize_logmgr, logmgr_add_cl_device_info, @@ -69,13 +68,13 @@ def main(actx_class, ctx_factory=cl.create_some_context, use_logmgr=True, else: queue = cl.CommandQueue(cl_ctx) + from mirgecom.simutil import get_reasonable_memory_pool + alloc = get_reasonable_memory_pool(cl_ctx, queue) + if lazy: - actx = actx_class(comm, queue, mpi_base_tag=12000, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue))) + actx = actx_class(comm, queue, mpi_base_tag=12000, allocator=alloc) else: - actx = actx_class(comm, queue, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)), - force_device_scalars=True) + actx = actx_class(comm, queue, allocator=alloc, force_device_scalars=True) from meshmode.distributed import MPIMeshDistributor, get_partition_by_pymetis mesh_dist = MPIMeshDistributor(comm) diff --git a/examples/hotplate-mpi.py b/examples/hotplate-mpi.py index b5e13884c..b31ca225c 100644 --- a/examples/hotplate-mpi.py +++ b/examples/hotplate-mpi.py @@ -26,7 +26,6 @@ import logging import numpy as np import pyopencl as cl -import pyopencl.tools as cl_tools from functools import partial from meshmode.mesh import BTAG_ALL, BTAG_NONE # noqa @@ -111,14 +110,13 @@ def main(ctx_factory=cl.create_some_context, use_logmgr=True, else: queue = cl.CommandQueue(cl_ctx) + from mirgecom.simutil import get_reasonable_memory_pool + alloc = get_reasonable_memory_pool(cl_ctx, queue) + if lazy: - actx = actx_class(comm, queue, mpi_base_tag=12000, - allocator=cl_tools.MemoryPool( - cl_tools.ImmediateAllocator(queue))) + actx = actx_class(comm, queue, mpi_base_tag=12000, allocator=alloc) else: - actx = actx_class(comm, queue, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)), - force_device_scalars=True) + actx = actx_class(comm, queue, allocator=alloc, force_device_scalars=True) # timestepping control timestepper = rk4_step diff --git a/examples/lump-mpi.py b/examples/lump-mpi.py index fcac632b6..16dd46e05 100644 --- a/examples/lump-mpi.py +++ b/examples/lump-mpi.py @@ -26,7 +26,6 @@ import logging import numpy as np import pyopencl as cl -import pyopencl.tools as cl_tools from functools import partial from meshmode.mesh import BTAG_ALL, BTAG_NONE # noqa @@ -93,13 +92,13 @@ def main(actx_class, ctx_factory=cl.create_some_context, use_logmgr=True, else: queue = cl.CommandQueue(cl_ctx) + from mirgecom.simutil import get_reasonable_memory_pool + alloc = get_reasonable_memory_pool(cl_ctx, queue) + if lazy: - actx = actx_class(comm, queue, mpi_base_tag=12000, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue))) + actx = actx_class(comm, queue, mpi_base_tag=12000, allocator=alloc) else: - actx = actx_class(comm, queue, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)), - force_device_scalars=True) + actx = actx_class(comm, queue, allocator=alloc, force_device_scalars=True) # timestepping control if use_leap: diff --git a/examples/mixture-mpi.py b/examples/mixture-mpi.py index f2871c961..f0c67c36e 100644 --- a/examples/mixture-mpi.py +++ b/examples/mixture-mpi.py @@ -26,7 +26,6 @@ import logging import numpy as np import pyopencl as cl -import pyopencl.tools as cl_tools from functools import partial from meshmode.mesh import BTAG_ALL, BTAG_NONE # noqa @@ -95,13 +94,13 @@ def main(actx_class, ctx_factory=cl.create_some_context, use_logmgr=True, else: queue = cl.CommandQueue(cl_ctx) + from mirgecom.simutil import get_reasonable_memory_pool + alloc = get_reasonable_memory_pool(cl_ctx, queue) + if lazy: - actx = actx_class(comm, queue, mpi_base_tag=12000, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue))) + actx = actx_class(comm, queue, mpi_base_tag=12000, allocator=alloc) else: - actx = actx_class(comm, queue, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)), - force_device_scalars=True) + actx = actx_class(comm, queue, allocator=alloc, force_device_scalars=True) # timestepping control if use_leap: diff --git a/examples/nsmix-mpi.py b/examples/nsmix-mpi.py index c46416eae..fc160a0dc 100644 --- a/examples/nsmix-mpi.py +++ b/examples/nsmix-mpi.py @@ -26,7 +26,6 @@ import logging import numpy as np import pyopencl as cl -import pyopencl.tools as cl_tools from functools import partial from pytools.obj_array import make_obj_array @@ -102,13 +101,13 @@ def main(ctx_factory=cl.create_some_context, use_logmgr=True, else: queue = cl.CommandQueue(cl_ctx) + from mirgecom.simutil import get_reasonable_memory_pool + alloc = get_reasonable_memory_pool(cl_ctx, queue) + if lazy: - actx = actx_class(comm, queue, mpi_base_tag=12000, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue))) + actx = actx_class(comm, queue, mpi_base_tag=12000, allocator=alloc) else: - actx = actx_class(comm, queue, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)), - force_device_scalars=True) + actx = actx_class(comm, queue, allocator=alloc, force_device_scalars=True) # Timestepping control # This example runs only 3 steps by default (to keep CI ~short) diff --git a/examples/poiseuille-mpi.py b/examples/poiseuille-mpi.py index e133d37ad..fe05bfab8 100644 --- a/examples/poiseuille-mpi.py +++ b/examples/poiseuille-mpi.py @@ -26,7 +26,6 @@ import logging import numpy as np import pyopencl as cl -import pyopencl.tools as cl_tools from pytools.obj_array import make_obj_array from functools import partial @@ -111,13 +110,13 @@ def main(ctx_factory=cl.create_some_context, use_logmgr=True, else: queue = cl.CommandQueue(cl_ctx) + from mirgecom.simutil import get_reasonable_memory_pool + alloc = get_reasonable_memory_pool(cl_ctx, queue) + if lazy: - actx = actx_class(comm, queue, mpi_base_tag=12000, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue))) + actx = actx_class(comm, queue, mpi_base_tag=12000, allocator=alloc) else: - actx = actx_class(comm, queue, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)), - force_device_scalars=True) + actx = actx_class(comm, queue, allocator=alloc, force_device_scalars=True) # timestepping control timestepper = rk4_step diff --git a/examples/pulse-mpi.py b/examples/pulse-mpi.py index 290227ae9..fee981624 100644 --- a/examples/pulse-mpi.py +++ b/examples/pulse-mpi.py @@ -29,7 +29,6 @@ import numpy as np from functools import partial import pyopencl as cl -import pyopencl.tools as cl_tools from meshmode.mesh import BTAG_ALL, BTAG_NONE # noqa from grudge.shortcuts import make_visualizer @@ -101,13 +100,13 @@ def main(actx_class, ctx_factory=cl.create_some_context, use_logmgr=True, else: queue = cl.CommandQueue(cl_ctx) + from mirgecom.simutil import get_reasonable_memory_pool + alloc = get_reasonable_memory_pool(cl_ctx, queue) + if lazy: - actx = actx_class(comm, queue, mpi_base_tag=12000, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue))) + actx = actx_class(comm, queue, mpi_base_tag=12000, allocator=alloc) else: - actx = actx_class(comm, queue, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)), - force_device_scalars=True) + actx = actx_class(comm, queue, allocator=alloc, force_device_scalars=True) # timestepping control current_step = 0 diff --git a/examples/scalar-lump-mpi.py b/examples/scalar-lump-mpi.py index 087b10704..3f7759e7b 100644 --- a/examples/scalar-lump-mpi.py +++ b/examples/scalar-lump-mpi.py @@ -26,7 +26,6 @@ import logging import numpy as np import pyopencl as cl -import pyopencl.tools as cl_tools from functools import partial from pytools.obj_array import make_obj_array @@ -94,13 +93,13 @@ def main(actx_class, ctx_factory=cl.create_some_context, use_logmgr=True, else: queue = cl.CommandQueue(cl_ctx) + from mirgecom.simutil import get_reasonable_memory_pool + alloc = get_reasonable_memory_pool(cl_ctx, queue) + if lazy: - actx = actx_class(comm, queue, mpi_base_tag=12000, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue))) + actx = actx_class(comm, queue, mpi_base_tag=12000, allocator=alloc) else: - actx = actx_class(comm, queue, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)), - force_device_scalars=True) + actx = actx_class(comm, queue, allocator=alloc, force_device_scalars=True) # timestepping control current_step = 0 diff --git a/examples/sod-mpi.py b/examples/sod-mpi.py index 122c4ec80..01802067c 100644 --- a/examples/sod-mpi.py +++ b/examples/sod-mpi.py @@ -26,7 +26,6 @@ import logging import numpy as np # noqa import pyopencl as cl -import pyopencl.tools as cl_tools from functools import partial from meshmode.mesh import BTAG_ALL, BTAG_NONE # noqa @@ -93,13 +92,13 @@ def main(actx_class, ctx_factory=cl.create_some_context, use_logmgr=True, else: queue = cl.CommandQueue(cl_ctx) + from mirgecom.simutil import get_reasonable_memory_pool + alloc = get_reasonable_memory_pool(cl_ctx, queue) + if lazy: - actx = actx_class(comm, queue, mpi_base_tag=12000, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue))) + actx = actx_class(comm, queue, mpi_base_tag=12000, allocator=alloc) else: - actx = actx_class(comm, queue, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)), - force_device_scalars=True) + actx = actx_class(comm, queue, allocator=alloc, force_device_scalars=True) # timestepping control if use_leap: diff --git a/examples/vortex-mpi.py b/examples/vortex-mpi.py index fccd7b7aa..cea15ed90 100644 --- a/examples/vortex-mpi.py +++ b/examples/vortex-mpi.py @@ -26,7 +26,6 @@ import logging import numpy as np import pyopencl as cl -import pyopencl.tools as cl_tools from functools import partial from meshmode.mesh import BTAG_ALL, BTAG_NONE # noqa @@ -95,13 +94,13 @@ def main(actx_class, ctx_factory=cl.create_some_context, use_logmgr=True, else: queue = cl.CommandQueue(cl_ctx) + from mirgecom.simutil import get_reasonable_memory_pool + alloc = get_reasonable_memory_pool(cl_ctx, queue) + if lazy: - actx = actx_class(comm, queue, mpi_base_tag=12000, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue))) + actx = actx_class(comm, queue, mpi_base_tag=12000, allocator=alloc) else: - actx = actx_class(comm, queue, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)), - force_device_scalars=True) + actx = actx_class(comm, queue, allocator=alloc, force_device_scalars=True) # timestepping control current_step = 0 diff --git a/examples/wave-mpi.py b/examples/wave-mpi.py index b95fa643c..643c26c35 100644 --- a/examples/wave-mpi.py +++ b/examples/wave-mpi.py @@ -40,8 +40,6 @@ from mirgecom.wave import wave_operator from mirgecom.utils import force_evaluation -import pyopencl.tools as cl_tools - from logpyle import IntervalTimer, set_dt from mirgecom.logging_quantities import (initialize_logmgr, @@ -88,13 +86,13 @@ def main(actx_class, snapshot_pattern="wave-mpi-{step:04d}-{rank:04d}.pkl", else: queue = cl.CommandQueue(cl_ctx) + from mirgecom.simutil import get_reasonable_memory_pool + alloc = get_reasonable_memory_pool(cl_ctx, queue) + if lazy: - actx = actx_class(comm, queue, mpi_base_tag=12000, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue))) + actx = actx_class(comm, queue, mpi_base_tag=12000, allocator=alloc) else: - actx = actx_class(comm, queue, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)), - force_device_scalars=True) + actx = actx_class(comm, queue, allocator=alloc, force_device_scalars=True) if restart_step is None: diff --git a/examples/wave.py b/examples/wave.py index 763637719..9f275362e 100644 --- a/examples/wave.py +++ b/examples/wave.py @@ -26,7 +26,6 @@ import numpy.linalg as la # noqa import pyopencl as cl import pyopencl.array as cla # noqa -import pyopencl.tools as cl_tools from pytools.obj_array import flat_obj_array @@ -76,33 +75,25 @@ def main(use_profiling=False, use_logmgr=False, lazy: bool = False): logmgr = initialize_logmgr(use_logmgr, filename="wave.sqlite", mode="wu") + from mirgecom.simutil import get_reasonable_memory_pool + if use_profiling: if lazy: raise RuntimeError("Cannot run lazy with profiling.") queue = cl.CommandQueue(cl_ctx, properties=cl.command_queue_properties.PROFILING_ENABLE) - actx = \ - PyOpenCLProfilingArrayContext( - queue, - allocator=cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)), - ) + + alloc = get_reasonable_memory_pool(cl_ctx, queue) + actx = PyOpenCLProfilingArrayContext(queue, allocator=alloc) else: queue = cl.CommandQueue(cl_ctx) + alloc = get_reasonable_memory_pool(cl_ctx, queue) + if lazy: - actx = \ - PytatoPyOpenCLArrayContext( - queue, - allocator=cl_tools.MemoryPool( - cl_tools.ImmediateAllocator(queue)), - ) + actx = PytatoPyOpenCLArrayContext(queue, allocator=alloc) else: - actx = \ - PyOpenCLArrayContext( - queue, - allocator=cl_tools.MemoryPool( - cl_tools.ImmediateAllocator(queue)), - force_device_scalars=True - ) + actx = PyOpenCLArrayContext(queue, allocator=alloc, + force_device_scalars=True) dim = 2 nel_1d = 16 diff --git a/mirgecom/simutil.py b/mirgecom/simutil.py index c7e3c5bd3..ca1114a4d 100644 --- a/mirgecom/simutil.py +++ b/mirgecom/simutil.py @@ -7,6 +7,7 @@ .. autofunction:: get_sim_timestep .. autofunction:: write_visfile .. autofunction:: global_reduce +.. autofunction:: get_reasonable_memory_pool Diagnostic utilities -------------------- @@ -434,6 +435,29 @@ def create_parallel_grid(comm, generate_grid): return generate_and_distribute_mesh(comm=comm, generate_mesh=generate_grid) +def get_reasonable_memory_pool(ctx, queue): + """Return an SVM or buffer memory pool based on what the device supports.""" + from pyopencl.characterize import has_coarse_grain_buffer_svm + import pyopencl.tools as cl_tools + + if has_coarse_grain_buffer_svm(queue.device) and hasattr(cl_tools, "SVMPool"): + logger.info("Using SVM-based memory pool") + return cl_tools.SVMPool(cl_tools.SVMAllocator( # pylint: disable=no-member + ctx, alignment=0, queue=queue)) + else: + from warnings import warn + + if not has_coarse_grain_buffer_svm(queue.device): + warn(f"No SVM support on {queue.device}, returning a CL buffer-based " + "memory pool. If you are running with PoCL-cuda, please update " + "your PoCL installation.") + else: + warn("No SVM memory pool support with your version of PyOpenCL, " + "returning a CL buffer-based memory pool. " + "Please update your PyOpenCL version.") + return cl_tools.MemoryPool(cl_tools.ImmediateAllocator(queue)) + + def configurate(config_key, config_object=None, default_value=None): """Return a configured item from a configuration object.""" if config_object is not None: