diff --git a/fixtures.py b/fixtures.py index 257f58216af3fa307929dec6a4b79d34252ebfc5..73df2fefb9fddbc371eac6297cbdf25cf4245fb2 100644 --- a/fixtures.py +++ b/fixtures.py @@ -8,6 +8,16 @@ import loopy as lp from pytest import approx +_WENO_PRG = [] +_QUEUE = [] + +def get_queue(ctx_factory): + if not _QUEUE: + ctx = ctx_factory() + _QUEUE.append(cl.CommandQueue(ctx)) + return _QUEUE[0] + + def with_root_kernel(prg, root_name): # FIXME This is a little less beautiful than it could be new_prg = prg.copy(name=root_name) @@ -20,70 +30,57 @@ def with_root_kernel(prg, root_name): return new_prg -class LoopyFixture: - _WENO_PRG = [] - _QUEUE = [] - - def __init__(self): - self.prg = self.get_weno_program() - - def get_weno_program(self): - if self._WENO_PRG: - return self._WENO_PRG[0] - - fn = "WENO.F90" +def get_weno_program(): + if _WENO_PRG: + return _WENO_PRG[0] - with open(fn, "r") as infile: - infile_content = infile.read() + fn = "WENO.F90" - prg = lp.parse_transformed_fortran(infile_content, filename=fn) - self._WENO_PRG.append(prg) - return prg + with open(fn, "r") as infile: + infile_content = infile.read() - def get_queue(self, ctx_factory): - if not self._QUEUE: - ctx = ctx_factory() - self._QUEUE.append(cl.CommandQueue(ctx)) - return self._QUEUE[0] + prg = lp.parse_transformed_fortran(infile_content, filename=fn) + _WENO_PRG.append(prg) + return prg - def random_array(self, *args): - return np.random.random_sample(args).astype(np.float32).copy(order="F") +def random_array(*args): + return np.random.random_sample(args).astype(np.float32).copy(order="F") - def mult_mat_vec(self, ctx_factory, alpha, a, b): - queue = self.get_queue(ctx_factory) +def mult_mat_vec(ctx_factory, alpha, a, b): + queue = get_queue(ctx_factory) - c_dev = cl.array.empty(queue, 10, dtype=np.float32) + c_dev = cl.array.empty(queue, 10, dtype=np.float32) - prg = with_root_kernel(self.prg, "mult_mat_vec") - prg(queue, a=a, b=b, c=c_dev, alpha=alpha) + prg = with_root_kernel(get_weno_program(), "mult_mat_vec") + prg(queue, a=a, b=b, c=c_dev, alpha=alpha) - return c_dev.get() + return c_dev.get() - def compute_flux_derivatives(self, ctx_factory, - nvars, ndim, nx, ny, nz, - states, fluxes, metrics, metric_jacobians): +def compute_flux_derivatives(ctx_factory, + nvars, ndim, nx, ny, nz, + states, fluxes, metrics, metric_jacobians): - queue = self.get_queue(ctx_factory) + queue = get_queue(ctx_factory) - prg = self.prg - cfd = prg["compute_flux_derivatives"] + prg = get_weno_program() + cfd = prg["compute_flux_derivatives"] - cfd = lp.assume(cfd, "nx > 0 and ny > 0 and nz > 0") + cfd = lp.assume(cfd, "nx > 0 and ny > 0 and nz > 0") - cfd = lp.set_temporary_scope(cfd, "flux_derivatives_generalized", - lp.AddressSpace.GLOBAL) - cfd = lp.set_temporary_scope(cfd, "generalized_fluxes", - lp.AddressSpace.GLOBAL) - cfd = lp.set_temporary_scope(cfd, "weno_flux_tmp", - lp.AddressSpace.GLOBAL) + cfd = lp.set_temporary_scope(cfd, "flux_derivatives_generalized", + lp.AddressSpace.GLOBAL) + cfd = lp.set_temporary_scope(cfd, "generalized_fluxes", + lp.AddressSpace.GLOBAL) + cfd = lp.set_temporary_scope(cfd, "weno_flux_tmp", + lp.AddressSpace.GLOBAL) - prg = prg.with_kernel(cfd) + prg = prg.with_kernel(cfd) - flux_derivatives_dev = cl.array.empty(queue, (nvars, ndim, nx+6, ny+6, - nz+6), dtype=np.float32, order="F") + flux_derivatives_dev = cl.array.empty(queue, (nvars, ndim, nx+6, ny+6, + nz+6), dtype=np.float32, order="F") - prg(queue, nvars=nvars, ndim=ndim, - states=states, fluxes=fluxes, metrics=metrics, - metric_jacobians=metric_jacobians, - flux_derivatives=flux_derivatives_dev) - return flux_derivatives_dev.get() + prg(queue, nvars=nvars, ndim=ndim, + states=states, fluxes=fluxes, metrics=metrics, + metric_jacobians=metric_jacobians, + flux_derivatives=flux_derivatives_dev) + return flux_derivatives_dev.get() diff --git a/test.py b/test.py index 93890bcff9472def502c033cf228c3d563e14b84..1f4afa31dc186e82e19afa7163a829461a40d02a 100644 --- a/test.py +++ b/test.py @@ -14,17 +14,14 @@ from pyopencl.tools import ( # noqa pytest_generate_tests_for_pyopencl as pytest_generate_tests) -from fixtures import LoopyFixture - - -f = LoopyFixture() +import fixtures def test_matvec(ctx_factory): - a = f.random_array(10, 10) - b = f.random_array(10) + a = fixtures.random_array(10, 10) + b = fixtures.random_array(10) - c = f.mult_mat_vec(ctx_factory, a=a, b=b, alpha=1.0) + c = fixtures.mult_mat_vec(ctx_factory, a=a, b=b, alpha=1.0) assert la.norm(a@b - c, 2)/la.norm(c) < 1e-5 @@ -38,12 +35,12 @@ def test_compute_flux_derivatives(ctx_factory): ny = 10 nz = 10 - states = f.random_array(nvars, nx+6, ny+6, nz+6) - fluxes = f.random_array(nvars, ndim, nx+6, ny+6, nz+6) - metrics = f.random_array(ndim, ndim, nx+6, ny+6, nz+6) - metric_jacobians = f.random_array(nx+6, ny+6, nz+6) + states = fixtures.random_array(nvars, nx+6, ny+6, nz+6) + fluxes = fixtures.random_array(nvars, ndim, nx+6, ny+6, nz+6) + metrics = fixtures.random_array(ndim, ndim, nx+6, ny+6, nz+6) + metric_jacobians = fixtures.random_array(nx+6, ny+6, nz+6) - f.compute_flux_derivatives(ctx_factory, + fixtures.compute_flux_derivatives(ctx_factory, nvars=nvars, ndim=ndim, nx=nx, ny=ny, nz=nz, states=states, fluxes=fluxes, metrics=metrics, metric_jacobians=metric_jacobians) @@ -55,7 +52,7 @@ def f_array(queue, *shape): def get_gpu_transformed_weno(): - prg = f.prg + prg = fixtures.get_weno_program() cfd = prg["compute_flux_derivatives"] @@ -104,7 +101,7 @@ def test_compute_flux_derivatives_gpu(ctx_factory): prg = get_gpu_transformed_weno() - queue = f.get_queue(ctx_factory) + queue = fixtures.get_queue(ctx_factory) ndim = 3 nvars = 5 @@ -144,7 +141,7 @@ def benchmark_compute_flux_derivatives_gpu(ctx_factory): prg = get_gpu_transformed_weno() - queue = f.get_queue(ctx_factory) + queue = fixtures.get_queue(ctx_factory) ndim = 3 nvars = 5