From 890811d82f9913d85e778261ced7498519151cbc Mon Sep 17 00:00:00 2001 From: Stephan Seitz <stephan.seitz@fau.de> Date: Thu, 14 Nov 2019 19:36:17 +0100 Subject: [PATCH] Execute LBM scenario with OpenCL It compiles so it's correct I guess... :shrug: --- lbmpy/lbstep.py | 13 ++++++++----- lbmpy/scenarios.py | 10 ++++++++-- lbmpy_tests/test_lbstep.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/lbmpy/lbstep.py b/lbmpy/lbstep.py index b21a9afe..f9f3e8b4 100644 --- a/lbmpy/lbstep.py +++ b/lbmpy/lbstep.py @@ -18,7 +18,7 @@ from pystencils.timeloop import TimeLoop class LatticeBoltzmannStep: def __init__(self, domain_size=None, lbm_kernel=None, periodicity=False, - kernel_params=MappingProxyType({}), data_handling=None, name="lbm", optimization=None, + kernel_params=MappingProxyType({}), data_handling=None, name="lbm", optimization={}, velocity_data_name=None, density_data_name=None, density_data_index=None, compute_velocity_in_every_step=False, compute_density_in_every_step=False, velocity_input_array_name=None, time_step_order='stream_collide', flag_interface=None, @@ -29,11 +29,15 @@ class LatticeBoltzmannStep: if domain_size is not None: raise ValueError("When passing a data_handling, the domain_size parameter can not be specified") + target = optimization.get('target', 'cpu') if data_handling is None: if domain_size is None: raise ValueError("Specify either domain_size or data_handling") - data_handling = create_data_handling(domain_size, default_ghost_layers=1, - periodicity=periodicity, parallel=False) + data_handling = create_data_handling(domain_size, + default_ghost_layers=1, + periodicity=periodicity, + default_target=target, + parallel=False) if 'stencil' not in method_parameters: method_parameters['stencil'] = 'D2Q9' if data_handling.dim == 2 else 'D3Q27' @@ -47,7 +51,6 @@ class LatticeBoltzmannStep: q = len(lbm_kernel.method.stencil) else: q = len(get_stencil(method_parameters['stencil'])) - target = optimization['target'] self.name = name self._data_handling = data_handling @@ -58,7 +61,7 @@ class LatticeBoltzmannStep: self.density_data_index = density_data_index self._optimization = optimization - self._gpu = target == 'gpu' + self._gpu = target == 'gpu' or target == 'opencl' layout = optimization['field_layout'] alignment = False diff --git a/lbmpy/scenarios.py b/lbmpy/scenarios.py index 6d838bbe..e47251f6 100644 --- a/lbmpy/scenarios.py +++ b/lbmpy/scenarios.py @@ -85,7 +85,13 @@ def create_lid_driven_cavity(domain_size=None, lid_velocity=0.005, lbm_kernel=No """ assert domain_size is not None or data_handling is not None if data_handling is None: - data_handling = create_data_handling(domain_size, periodicity=False, default_ghost_layers=1, parallel=parallel) + optimization = kwargs.get('optimization', None) + target = optimization.get('target', None) if optimization else None + data_handling = create_data_handling(domain_size, + periodicity=False, + default_ghost_layers=1, + parallel=parallel, + default_target=target) step = LatticeBoltzmannStep(data_handling=data_handling, lbm_kernel=lbm_kernel, name="ldc", **kwargs) my_ubb = UBB(velocity=[lid_velocity, 0, 0][:step.method.dim]) @@ -99,7 +105,7 @@ def create_lid_driven_cavity(domain_size=None, lid_velocity=0.005, lbm_kernel=No def create_channel(domain_size=None, force=None, pressure_difference=None, u_max=None, diameter_callback=None, duct=False, wall_boundary=NoSlip(), parallel=False, data_handling=None, **kwargs): """Create a channel scenario (2D or 3D). - + The channel can be driven either by force, velocity inflow or pressure difference. Choose one and pass exactly one of the parameters 'force', 'pressure_difference' or 'u_max'. diff --git a/lbmpy_tests/test_lbstep.py b/lbmpy_tests/test_lbstep.py index 7d6d395c..eabbf262 100644 --- a/lbmpy_tests/test_lbstep.py +++ b/lbmpy_tests/test_lbstep.py @@ -48,6 +48,35 @@ def test_data_handling_3d(): np.testing.assert_almost_equal(results[0], arr) +def test_data_handling_2d_opencl(): + pytest.importorskip('pyopencl') + import pystencils.opencl.opencljit + pystencils.opencl.opencljit.init_globally() + print("--- LDC 2D test ---") + results = [] + for parallel in [True, False] if parallel_available else [False]: + for gpu in [True, False] if gpu_available else [False]: + if parallel and gpu and not hasattr(wLB, 'cuda'): + continue + + print("Testing parallel: %s\tgpu: %s" % (parallel, gpu)) + opt_params = {'target': 'opencl' if gpu else 'cpu', + 'gpu_indexing_params': {'block_size': (8, 4, 2)}} + if parallel: + from pystencils.datahandling import ParallelDataHandling + blocks = wLB.createUniformBlockGrid(blocks=(2, 3, 1), cellsPerBlock=(5, 5, 1), + oneBlockPerProcess=False) + dh = ParallelDataHandling(blocks, dim=2) + rho = ldc_setup(data_handling=dh, optimization=opt_params) + results.append(rho) + else: + rho = ldc_setup(domain_size=(10, 15), parallel=False, optimization=opt_params) + results.append(rho) + for i, arr in enumerate(results[1:]): + print("Testing equivalence version 0 with version %d" % (i + 1,)) + np.testing.assert_almost_equal(results[0], arr) + + def test_data_handling_2d(): print("--- LDC 2D test ---") results = [] -- GitLab