From 4e43c1477f5417734b4247d8e49ec7bca170f1f0 Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" <2timothy18@gmail.com> Date: Mon, 4 Nov 2019 00:21:30 -0600 Subject: [PATCH 1/3] added pytest_generate_tests and queue fixture --- test/conftest.py | 59 +++++++++++++++++++++++++-- test/test_eigensystem.py | 15 ++----- test/test_flux_derivatives.py | 6 +-- test/test_weno_flux.py | 30 ++++---------- test/utilities.py | 10 ----- test/weno_reference_implementation.py | 2 +- 6 files changed, 71 insertions(+), 51 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index 29be9d5..fee56d9 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,11 +1,17 @@ +import pytest +import pyopencl as cl # noqa: F401 +from pyopencl.tools import ( + get_test_platforms_and_devices, + clear_first_arg_caches + ) + +# {{{ mark for slow tests + # setup to mark slow tests with @pytest.mark.slow, so that they don't run by # default, but can be forced to run with the command-line option --runslow # taken from # https://docs.pytest.org/en/latest/example/simple.html#control-skipping-of-tests-according-to-command-line-option -import pytest - - def pytest_addoption(parser): parser.addoption("--runslow", action="store_true", default=False, help="run slow tests") @@ -19,3 +25,50 @@ def pytest_collection_modifyitems(config, items): for item in items: if "slow" in item.keywords: item.add_marker(skip_slow) + +# }}} + +# {{{ pytest_generate_tests + +def pytest_generate_tests(metafunc): + class ContextFactory: + def __init__(self, device): + self.device = device + + def __call__(self): + # Get rid of leftovers from past tests. + # CL implementations are surprisingly limited in how many + # simultaneous contexts they allow... + + clear_first_arg_caches() + + from gc import collect + collect() + + return cl.Context([self.device]) + + def __str__(self): + # Don't show address, so that parallel test collection works + return ("" % + (self.device.name.strip(), + self.device.platform.name.strip())) + + test_plat_and_dev = get_test_platforms_and_devices() + + if "ctx_factory" in metafunc.fixturenames: + factories = [] + + for platform, plat_devs in test_plat_and_dev: + for device in plat_devs: + factories.append(ContextFactory(device)) + + metafunc.parametrize("ctx_factory", factories, ids=str, scope="session") + +# }}} + +@pytest.fixture(scope="session") +def queue(ctx_factory): + return cl.CommandQueue(ctx_factory()) + + +# vim: foldmethod=marker diff --git a/test/test_eigensystem.py b/test/test_eigensystem.py index de37e89..3c48c71 100644 --- a/test/test_eigensystem.py +++ b/test/test_eigensystem.py @@ -10,9 +10,6 @@ import sys import logging import pytest -from pyopencl.tools import ( # noqa - pytest_generate_tests_for_pyopencl - as pytest_generate_tests) import utilities as u from data_for_test import ( # noqa: F401 @@ -21,11 +18,10 @@ from data_for_test import ( # noqa: F401 ) -def test_pointwise_eigenvalues_ideal_gas(ctx_factory, flux_test_data_fixture): +def test_pointwise_eigenvalues_ideal_gas(queue, flux_test_data_fixture): data = flux_test_data_fixture prg = u.get_weno_program_with_root_kernel("pointwise_eigenvalues") - queue = u.get_queue(ctx_factory) lam_dev = u.empty_array_on_device(queue, data.nvars, 6) @@ -35,7 +31,7 @@ def test_pointwise_eigenvalues_ideal_gas(ctx_factory, flux_test_data_fixture): u.compare_arrays(lam_dev.get(), data.lam_pointwise) -def test_roe_uniform_grid_ideal_gas(ctx_factory, flux_test_data_fixture): +def test_roe_uniform_grid_ideal_gas(queue, flux_test_data_fixture): data = flux_test_data_fixture def check_roe_identity(states, R, R_inv): @@ -51,7 +47,6 @@ def test_roe_uniform_grid_ideal_gas(ctx_factory, flux_test_data_fixture): u.compare_arrays(R@temp, d_flux) prg = u.get_weno_program_with_root_kernel("roe_eigensystem") - queue = u.get_queue(ctx_factory) R_dev = u.empty_array_on_device(queue, data.nvars, data.nvars) R_inv_dev = u.empty_array_on_device(queue, data.nvars, data.nvars) @@ -77,9 +72,8 @@ def test_roe_uniform_grid_ideal_gas(ctx_factory, flux_test_data_fixture): ("3 2 9 4 5,2 6 6 12 10", "-1 -4 -3 -8 -15", "3.3 6.6 9.9 13.2 16.5") ]) def test_lax_wavespeeds( - ctx_factory, lam_pointwise_str, lam_roe_str, lam_expected_str): + queue, lam_pointwise_str, lam_roe_str, lam_expected_str): prg = u.get_weno_program_with_root_kernel("lax_wavespeeds") - queue = u.get_queue(ctx_factory) nvars = 5 @@ -95,9 +89,8 @@ def test_lax_wavespeeds( u.compare_arrays(lam_dev.get(), lam_expected) -def test_matvec(ctx_factory): +def test_matvec(queue): prg = u.get_weno_program_with_root_kernel("mult_mat_vec") - queue = u.get_queue(ctx_factory) a = u.random_array_on_device(queue, 10, 10) b = u.random_array_on_device(queue, 10) diff --git a/test/test_flux_derivatives.py b/test/test_flux_derivatives.py index b599be0..252fa52 100644 --- a/test/test_flux_derivatives.py +++ b/test/test_flux_derivatives.py @@ -10,9 +10,6 @@ import sys import logging import pytest -from pyopencl.tools import ( # noqa - pytest_generate_tests_for_pyopencl - as pytest_generate_tests) import utilities as u from data_for_test import ( # noqa: F401 @@ -20,11 +17,10 @@ from data_for_test import ( # noqa: F401 ) -def test_compute_flux_derivatives_uniform_grid(ctx_factory, cfd_test_data_fixture): +def test_compute_flux_derivatives_uniform_grid(queue, cfd_test_data_fixture): data = cfd_test_data_fixture prg = u.get_weno_program() - queue = u.get_queue(ctx_factory) prg = prg.copy(target=lp.PyOpenCLTarget(queue.device)) flux_derivatives_dev = u.empty_array_on_device(queue, *data.flux_dims) diff --git a/test/test_weno_flux.py b/test/test_weno_flux.py index 76976cd..f6587c6 100644 --- a/test/test_weno_flux.py +++ b/test/test_weno_flux.py @@ -10,9 +10,6 @@ import sys import logging import pytest -from pyopencl.tools import ( # noqa - pytest_generate_tests_for_pyopencl - as pytest_generate_tests) import utilities as u from data_for_test import ( # noqa: F401 @@ -21,11 +18,10 @@ from data_for_test import ( # noqa: F401 ) -def test_weno_flux_uniform_grid(ctx_factory, flux_test_data_fixture): +def test_weno_flux_uniform_grid(queue, flux_test_data_fixture): data = flux_test_data_fixture prg = u.get_weno_program_with_root_kernel("weno_flux") - queue = u.get_queue(ctx_factory) flux_dev = u.empty_array_on_device(queue, data.nvars) @@ -40,11 +36,10 @@ def test_weno_flux_uniform_grid(ctx_factory, flux_test_data_fixture): u.compare_arrays(flux_dev.get(), data.weno_flux) -def test_consistent_part_uniform_grid(ctx_factory, flux_test_data_fixture): +def test_consistent_part_uniform_grid(queue, flux_test_data_fixture): data = flux_test_data_fixture prg = u.get_weno_program_with_root_kernel("consistent_part") - queue = u.get_queue(ctx_factory) consistent_dev = u.empty_array_on_device(queue, data.nvars) @@ -55,11 +50,10 @@ def test_consistent_part_uniform_grid(ctx_factory, flux_test_data_fixture): u.compare_arrays(consistent_dev.get(), data.consistent) -def test_dissipation_part_pos_uniform_grid(ctx_factory, flux_test_data_fixture): +def test_dissipation_part_pos_uniform_grid(queue, flux_test_data_fixture): data = flux_test_data_fixture prg = u.get_weno_program_with_root_kernel("dissipation_part_pos") - queue = u.get_queue(ctx_factory) dissipation_dev = u.empty_array_on_device(queue, data.nvars) @@ -72,11 +66,10 @@ def test_dissipation_part_pos_uniform_grid(ctx_factory, flux_test_data_fixture): u.compare_arrays(dissipation_dev.get(), data.dissipation_pos) -def test_dissipation_part_neg_uniform_grid(ctx_factory, flux_test_data_fixture): +def test_dissipation_part_neg_uniform_grid(queue, flux_test_data_fixture): data = flux_test_data_fixture prg = u.get_weno_program_with_root_kernel("dissipation_part_neg") - queue = u.get_queue(ctx_factory) dissipation_dev = u.empty_array_on_device(queue, data.nvars) @@ -89,11 +82,10 @@ def test_dissipation_part_neg_uniform_grid(ctx_factory, flux_test_data_fixture): u.compare_arrays(dissipation_dev.get(), data.dissipation_neg) -def test_weno_weights_pos_uniform_grid(ctx_factory, flux_test_data_fixture): +def test_weno_weights_pos_uniform_grid(queue, flux_test_data_fixture): data = flux_test_data_fixture prg = u.get_weno_program_with_root_kernel("weno_weights_pos") - queue = u.get_queue(ctx_factory) weights_dev = u.empty_array_on_device(queue, data.nvars, 3) @@ -108,11 +100,10 @@ def test_weno_weights_pos_uniform_grid(ctx_factory, flux_test_data_fixture): u.compare_arrays(weights_dev.get(), data.weno_weights_pos) -def test_weno_weights_neg_uniform_grid(ctx_factory, flux_test_data_fixture): +def test_weno_weights_neg_uniform_grid(queue, flux_test_data_fixture): data = flux_test_data_fixture prg = u.get_weno_program_with_root_kernel("weno_weights_neg") - queue = u.get_queue(ctx_factory) weights_dev = u.empty_array_on_device(queue, data.nvars, 3) @@ -127,11 +118,10 @@ def test_weno_weights_neg_uniform_grid(ctx_factory, flux_test_data_fixture): u.compare_arrays(weights_dev.get(), data.weno_weights_neg) -def test_oscillation_pos_uniform_grid(ctx_factory, flux_test_data_fixture): +def test_oscillation_pos_uniform_grid(queue, flux_test_data_fixture): data = flux_test_data_fixture prg = u.get_weno_program_with_root_kernel("oscillation_pos") - queue = u.get_queue(ctx_factory) oscillation_dev = u.empty_array_on_device(queue, data.nvars, 3) @@ -142,11 +132,10 @@ def test_oscillation_pos_uniform_grid(ctx_factory, flux_test_data_fixture): u.compare_arrays(oscillation_dev.get(), data.oscillation_pos) -def test_oscillation_neg_uniform_grid(ctx_factory, flux_test_data_fixture): +def test_oscillation_neg_uniform_grid(queue, flux_test_data_fixture): data = flux_test_data_fixture prg = u.get_weno_program_with_root_kernel("oscillation_neg") - queue = u.get_queue(ctx_factory) oscillation_dev = u.empty_array_on_device(queue, data.nvars, 3) @@ -157,11 +146,10 @@ def test_oscillation_neg_uniform_grid(ctx_factory, flux_test_data_fixture): u.compare_arrays(oscillation_dev.get(), data.oscillation_neg) -def test_flux_splitting_uniform_grid(ctx_factory, flux_test_data_fixture): +def test_flux_splitting_uniform_grid(queue, flux_test_data_fixture): data = flux_test_data_fixture prg = u.get_weno_program_with_root_kernel("split_characteristic_fluxes") - queue = u.get_queue(ctx_factory) fluxes_pos_dev = u.empty_array_on_device(queue, data.nvars, 6) fluxes_neg_dev = u.empty_array_on_device(queue, data.nvars, 6) diff --git a/test/utilities.py b/test/utilities.py index bd78fb4..b83409e 100644 --- a/test/utilities.py +++ b/test/utilities.py @@ -79,16 +79,6 @@ def expand_to_n(pair, n): # }}} - -# {{{ device - -def get_queue(ctx_factory): - ctx = ctx_factory() - return cl.CommandQueue(ctx) - -# }}} - - # {{{ program / kernel _WENO_PRG = {} diff --git a/test/weno_reference_implementation.py b/test/weno_reference_implementation.py index 2b18fd7..e9e1b79 100644 --- a/test/weno_reference_implementation.py +++ b/test/weno_reference_implementation.py @@ -22,7 +22,7 @@ def roe_eigensystem(state_pair, frozen_metrics, direction): ndim = frozen_metrics.shape[0] prg = u.get_weno_program_with_root_kernel("roe_eigensystem") - queue = u.get_queue(cl._csc) + queue = cl.CommandQueue(cl._csc()) R_dev = u.empty_array_on_device(queue, nvars, nvars) R_inv_dev = u.empty_array_on_device(queue, nvars, nvars) -- GitLab From 28cc13d9cc527455e38ee503cdfdbac885f2e2b5 Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" <2timothy18@gmail.com> Date: Mon, 4 Nov 2019 00:33:29 -0600 Subject: [PATCH 2/3] actually we do need get_queue because sometimes we want both the context and the queue in the same test --- test/conftest.py | 2 +- test/utilities.py | 3 +++ test/weno_reference_implementation.py | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index fee56d9..e336864 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -68,7 +68,7 @@ def pytest_generate_tests(metafunc): @pytest.fixture(scope="session") def queue(ctx_factory): - return cl.CommandQueue(ctx_factory()) + return u.get_queue(ctx_factory) # vim: foldmethod=marker diff --git a/test/utilities.py b/test/utilities.py index b83409e..dbfcc25 100644 --- a/test/utilities.py +++ b/test/utilities.py @@ -79,6 +79,9 @@ def expand_to_n(pair, n): # }}} +def get_queue(ctx_factory): + return cl.CommandQueue(ctx_factory()) + # {{{ program / kernel _WENO_PRG = {} diff --git a/test/weno_reference_implementation.py b/test/weno_reference_implementation.py index e9e1b79..2b18fd7 100644 --- a/test/weno_reference_implementation.py +++ b/test/weno_reference_implementation.py @@ -22,7 +22,7 @@ def roe_eigensystem(state_pair, frozen_metrics, direction): ndim = frozen_metrics.shape[0] prg = u.get_weno_program_with_root_kernel("roe_eigensystem") - queue = cl.CommandQueue(cl._csc()) + queue = u.get_queue(cl._csc) R_dev = u.empty_array_on_device(queue, nvars, nvars) R_inv_dev = u.empty_array_on_device(queue, nvars, nvars) -- GitLab From ca864ef3660d0af240b755149918796281e767e8 Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" <2timothy18@gmail.com> Date: Mon, 4 Nov 2019 23:01:52 -0600 Subject: [PATCH 3/3] fix missing include --- test/conftest.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/conftest.py b/test/conftest.py index e336864..de6aa26 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -5,6 +5,8 @@ from pyopencl.tools import ( clear_first_arg_caches ) +import utilities as u + # {{{ mark for slow tests # setup to mark slow tests with @pytest.mark.slow, so that they don't run by -- GitLab