From 567555e034e29470d7133fbed376290eb23c6c5d Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Thu, 20 Jun 2019 21:26:57 -0500 Subject: [PATCH 1/6] memoize calls to with_root_kernel --- test.py | 6 ++---- utilities.py | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/test.py b/test.py index e972734..bc9cbe6 100644 --- a/test.py +++ b/test.py @@ -55,7 +55,6 @@ def test_roe_uniform_grid(ctx_factory, states_str, fluxes_str, direction): Rinv_dev = empty_array_on_device(queue, *params.mat_bounds()) lam_dev = empty_array_on_device(queue, params.vec_bound()) - prg = with_root_kernel(prg, "roe_eigensystem") prg(queue, nvars=params.nvars, ndim=params.ndim, d=params.d, states=states, metrics_frozen=metrics_frozen, R=R_dev, R_inv=Rinv_dev, lambda_roe=lam_dev) @@ -75,7 +74,7 @@ def test_roe_uniform_grid(ctx_factory, states_str, fluxes_str, direction): compare_arrays(R@temp, dFlux) queue = get_queue(ctx_factory) - prg = get_weno_program() + prg = get_weno_program_with_root_kernel("roe_eigensystem") params = setup_roe_params(nvars=5, ndim=3, direction=direction) states = array_from_string(states_str) @@ -89,7 +88,7 @@ def test_roe_uniform_grid(ctx_factory, states_str, fluxes_str, direction): def test_matvec(ctx_factory): - prg = get_weno_program() + prg = get_weno_program_with_root_kernel("mult_mat_vec") queue = get_queue(ctx_factory) a = random_array_on_device(queue, 10, 10) @@ -97,7 +96,6 @@ def test_matvec(ctx_factory): c = empty_array_on_device(queue, 10) - prg = with_root_kernel(prg, "mult_mat_vec") prg(queue, alpha=1.0, a=a, b=b, c=c) compare_arrays(a.get()@b.get(), c.get()) diff --git a/utilities.py b/utilities.py index 306c28e..05ee24a 100644 --- a/utilities.py +++ b/utilities.py @@ -63,7 +63,7 @@ def split_map_to_list(string, map_func, splitter): ### Device ### -_QUEUE = [] +_QUEUE = {} def get_queue(ctx_factory): @@ -74,18 +74,30 @@ def get_queue(ctx_factory): def setup_queue(ctx_factory): ctx = ctx_factory() - _QUEUE.append(cl.CommandQueue(ctx)) + _QUEUE[0] = cl.CommandQueue(ctx) ### Program / Kernel ### -_WENO_PRG = [] +_WENO_PRG = {} + + +def get_weno_program_with_root_kernel(knl): + if not knl in _WENO_PRG: + setup_weno_program_with_root_kernel(knl) + return _WENO_PRG[knl] + + +def setup_weno_program_with_root_kernel(knl): + prg = get_weno_program() + prg = with_root_kernel(prg, knl) + _WENO_PRG[knl] = prg def get_weno_program(): if not _WENO_PRG: parse_weno() - return _WENO_PRG[0] + return _WENO_PRG["default"] def parse_weno(): @@ -95,7 +107,7 @@ def parse_weno(): infile_content = infile.read() prg = lp.parse_transformed_fortran(infile_content, filename=fn) - _WENO_PRG.append(prg) + _WENO_PRG["default"] = prg def with_root_kernel(prg, root_name): -- GitLab From 202248d790227adc87debd1e554eb15c53c570af Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Fri, 21 Jun 2019 00:47:35 -0500 Subject: [PATCH 2/6] get rid of some too-complicated code in the Roe test --- test.py | 49 ++++++++++++++++--------------------------------- 1 file changed, 16 insertions(+), 33 deletions(-) diff --git a/test.py b/test.py index bc9cbe6..01dec36 100644 --- a/test.py +++ b/test.py @@ -31,36 +31,9 @@ from utilities import * ("2 1,4 1,8 2,12 3,64 11", "12 3,24 3,48 6,75.2 10.6,403.2 37.8", "z") ]) def test_roe_uniform_grid(ctx_factory, states_str, fluxes_str, direction): - class RoeParams: - def __init__(self, nvars, ndim, d): - self.nvars = nvars - self.ndim = ndim - self.d = d - - def mat_bounds(self): - return self.nvars, self.nvars - - def vec_bound(self): - return self.nvars - - def setup_roe_params(nvars, ndim, direction): - dirs = {"x" : 1, "y" : 2, "z" : 3} - return RoeParams(nvars, ndim, dirs[direction]) - def identity_matrix(n): return np.identity(n).astype(np.float32).copy(order="F") - def kernel_roe_eigensystem(queue, prg, params, states, metrics_frozen): - R_dev = empty_array_on_device(queue, *params.mat_bounds()) - Rinv_dev = empty_array_on_device(queue, *params.mat_bounds()) - lam_dev = empty_array_on_device(queue, params.vec_bound()) - - prg(queue, nvars=params.nvars, ndim=params.ndim, d=params.d, - states=states, metrics_frozen=metrics_frozen, - R=R_dev, R_inv=Rinv_dev, lambda_roe=lam_dev) - - return R_dev.get(), Rinv_dev.get(), lam_dev.get() - def check_roe_identity(states, R, Rinv): dState = states[:,1] - states[:,0] compare_arrays(R@(Rinv@dState), dState) @@ -73,17 +46,27 @@ def test_roe_uniform_grid(ctx_factory, states_str, fluxes_str, direction): temp = np.multiply(lam, temp) compare_arrays(R@temp, dFlux) - queue = get_queue(ctx_factory) prg = get_weno_program_with_root_kernel("roe_eigensystem") + queue = get_queue(ctx_factory) - params = setup_roe_params(nvars=5, ndim=3, direction=direction) + dirs = {"x" : 1, "y" : 2, "z" : 3} states = array_from_string(states_str) - metrics_frozen = identity_matrix(params.ndim) - R, Rinv, lam = kernel_roe_eigensystem(queue, prg, params, states, metrics_frozen) + fluxes = array_from_string(fluxes_str) + metrics_frozen = identity_matrix(3) - check_roe_identity(states, R, Rinv) + R_dev = empty_array_on_device(queue, 5, 5) + Rinv_dev = empty_array_on_device(queue, 5, 5) + lam_dev = empty_array_on_device(queue, 5) - fluxes = array_from_string(fluxes_str) + prg(queue, nvars=5, ndim=3, d=dirs[direction], + states=states, metrics_frozen=metrics_frozen, + R=R_dev, R_inv=Rinv_dev, lambda_roe=lam_dev) + + R = R_dev.get() + Rinv = Rinv_dev.get() + lam = lam_dev.get() + + check_roe_identity(states, R, Rinv) check_roe_property(states, fluxes, R, Rinv, lam) -- GitLab From 259d5cf2ffa4ddb638ad4d948e2e72de753110e7 Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Fri, 21 Jun 2019 21:46:51 -0500 Subject: [PATCH 3/6] refactor Roe test so input arrays are transposed for easier viewing --- test.py | 35 +++++++++++++++++++---------------- utilities.py | 4 ++++ 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/test.py b/test.py index 27b583b..412090d 100644 --- a/test.py +++ b/test.py @@ -20,15 +20,15 @@ from utilities import * @pytest.mark.xfail @pytest.mark.parametrize("states_str,fluxes_str,direction", [ - ("2 1,4 1,4 1,4 1,20 5.5", "4 1,11.2 2.6,8 1,8 1,46.4 7.1", "x"), - ("2 1,4 1,4 1,4 1,20 5.5", "4 1,8 1,11.2 2.6,8 1,46.4 7.1", "y"), - ("2 1,4 1,4 1,4 1,20 5.5", "4 1,8 1,8 1,11.2 2.6,46.4 7.1", "z"), - ("1 2,-1 -4,-1 -4,-1 -4,5.5 20", "-1 -4,2.6 11.2,1 8,1 8,-7.1 -46.4", "x"), - ("1 2,-1 -4,-1 -4,-1 -4,5.5 20", "-1 -4,1 8,2.6 11.2,1 8,-7.1 -46.4", "y"), - ("1 2,-1 -4,-1 -4,-1 -4,5.5 20", "-1 -4,1 8,1 8,2.6 11.2,-7.1 -46.4", "z"), - ("2 1,4 1,8 2,12 3,64 11", "4 1,11.2 2.6,16 2,24 3,134.4 12.6", "x"), - ("2 1,4 1,8 2,12 3,64 11", "8 2,16 2,35.2 5.6,48 6,268.8 25.2", "y"), - ("2 1,4 1,8 2,12 3,64 11", "12 3,24 3,48 6,75.2 10.6,403.2 37.8", "z") + ("2 4 4 4 20,1 1 1 1 5.5", "4 11.2 8 8 46.4,1 2.6 1 1 7.1", "x"), + ("2 4 4 4 20,1 1 1 1 5.5", "4 8 11.2 8 46.4,1 1 2.6 1 7.1", "y"), + ("2 4 4 4 20,1 1 1 1 5.5", "4 8 8 11.2 46.4,1 1 1 2.6 7.1", "z"), + ("1 -1 -1 -1 5.5,2 -4 -4 -4 20", "-1 2.6 1 1 -7.1,-4 11.2 8 8 -46.4", "x"), + ("1 -1 -1 -1 5.5,2 -4 -4 -4 20", "-1 1 2.6 1 -7.1,-4 8 11.2 8 -46.4", "y"), + ("1 -1 -1 -1 5.5,2 -4 -4 -4 20", "-1 1 1 2.6 -7.1,-4 8 8 11.2 -46.4", "z"), + ("2 4 8 12 64,1 1 2 3 11", "4 11.2 16 24 134.4,1 2.6 2 3 12.6", "x"), + ("2 4 8 12 64,1 1 2 3 11", "8 16 35.2 48 268.8,2 2 5.6 6 25.2", "y"), + ("2 4 8 12 64,1 1 2 3 11", "12 24 48 75.2 403.2,3 3 6 10.6 37.8", "z") ]) def test_roe_uniform_grid(ctx_factory, states_str, fluxes_str, direction): def identity_matrix(n): @@ -49,16 +49,19 @@ def test_roe_uniform_grid(ctx_factory, states_str, fluxes_str, direction): prg = get_weno_program_with_root_kernel("roe_eigensystem") queue = get_queue(ctx_factory) + nvars = 5 + ndim = 3 dirs = {"x" : 1, "y" : 2, "z" : 3} - states = array_from_string(states_str) - fluxes = array_from_string(fluxes_str) - metrics_frozen = identity_matrix(3) - R_dev = empty_array_on_device(queue, 5, 5) - Rinv_dev = empty_array_on_device(queue, 5, 5) - lam_dev = empty_array_on_device(queue, 5) + states = transposed_array_from_string(states_str) + fluxes = transposed_array_from_string(fluxes_str) + metrics_frozen = identity_matrix(ndim) - prg(queue, nvars=5, ndim=3, d=dirs[direction], + R_dev = empty_array_on_device(queue, nvars, nvars) + Rinv_dev = empty_array_on_device(queue, nvars, nvars) + lam_dev = empty_array_on_device(queue, nvars) + + prg(queue, nvars=nvars, ndim=ndim, d=dirs[direction], states=states, metrics_frozen=metrics_frozen, R=R_dev, R_inv=Rinv_dev, lambda_roe=lam_dev) diff --git a/utilities.py b/utilities.py index 171f78e..e4981a3 100644 --- a/utilities.py +++ b/utilities.py @@ -28,6 +28,10 @@ def arrays_from_string(string_arrays): return split_map_to_list(string_arrays, array_from_string, ":") +def transposed_array_from_string(string_array): + return array_from_string(string_array).transpose().copy(order="F") + + def array_from_string(string_array): def array_from_string_1d(string_array): if string_array[0] == "i": -- GitLab From f511e9207ae196d48d9342f5901462a9f0b48f23 Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Fri, 21 Jun 2019 22:31:18 -0500 Subject: [PATCH 4/6] bugfix in Roe eigensystem --- WENO.F90 | 6 +++--- test.py | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/WENO.F90 b/WENO.F90 index 6bfad5c..829080e 100644 --- a/WENO.F90 +++ b/WENO.F90 @@ -303,9 +303,9 @@ subroutine roe_eigensystem(nvars, ndim, d, states, metrics_frozen, R, R_inv, lam if (im > 3) im = im - 3 do j=1,2 - u_orig(1,j) = states(ik+1,j)/states(1,j) - u_orig(2,j) = states(il+1,j)/states(1,j) - u_orig(3,j) = states(im+1,j)/states(1,j) + do i=1,ndim + u_orig(i,j) = states(i+1,j)/states(1,j) + end do end do do j=1,2 diff --git a/test.py b/test.py index 412090d..741a691 100644 --- a/test.py +++ b/test.py @@ -18,7 +18,6 @@ from pyopencl.tools import ( # noqa from utilities import * -@pytest.mark.xfail @pytest.mark.parametrize("states_str,fluxes_str,direction", [ ("2 4 4 4 20,1 1 1 1 5.5", "4 11.2 8 8 46.4,1 2.6 1 1 7.1", "x"), ("2 4 4 4 20,1 1 1 1 5.5", "4 8 11.2 8 46.4,1 1 2.6 1 7.1", "y"), @@ -69,6 +68,8 @@ def test_roe_uniform_grid(ctx_factory, states_str, fluxes_str, direction): Rinv = Rinv_dev.get() lam = lam_dev.get() + print(lam) + check_roe_identity(states, R, Rinv) check_roe_property(states, fluxes, R, Rinv, lam) -- GitLab From 97654c94b5b7f7e17a78d8e61ac5a5ba16c80d12 Mon Sep 17 00:00:00 2001 From: "Timothy A. Smith" Date: Mon, 24 Jun 2019 09:53:50 -0500 Subject: [PATCH 5/6] added new test cases for Roe test --- test.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test.py b/test.py index 741a691..97ccfe7 100644 --- a/test.py +++ b/test.py @@ -27,7 +27,10 @@ from utilities import * ("1 -1 -1 -1 5.5,2 -4 -4 -4 20", "-1 1 1 2.6 -7.1,-4 8 8 11.2 -46.4", "z"), ("2 4 8 12 64,1 1 2 3 11", "4 11.2 16 24 134.4,1 2.6 2 3 12.6", "x"), ("2 4 8 12 64,1 1 2 3 11", "8 16 35.2 48 268.8,2 2 5.6 6 25.2", "y"), - ("2 4 8 12 64,1 1 2 3 11", "12 24 48 75.2 403.2,3 3 6 10.6 37.8", "z") + ("2 4 8 12 64,1 1 2 3 11", "12 24 48 75.2 403.2,3 3 6 10.6 37.8", "z"), + ("1 -1 -2 -3 11,2 -4 -8 -12 64", "-1 2.6 2 3 -12.6,-4 11.2 16 24 -134.4", "x"), + ("1 -1 -2 -3 11,2 -4 -8 -12 64", "-2 2 5.6 6 -25.2,-8 16 35.2 48 -268.8", "y"), + ("1 -1 -2 -3 11,2 -4 -8 -12 64", "-3 3 6 10.6 -37.8,-12 24 48 75.2 -403.2", "z") ]) def test_roe_uniform_grid(ctx_factory, states_str, fluxes_str, direction): def identity_matrix(n): -- GitLab From 26e26a532d1016bbab56495fcf1a4fb8672a9ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kl=C3=B6ckner?= Date: Tue, 25 Jun 2019 20:51:10 +0200 Subject: [PATCH 6/6] Reorder so that callers follow callees --- utilities.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/utilities.py b/utilities.py index e4981a3..b825fcb 100644 --- a/utilities.py +++ b/utilities.py @@ -79,24 +79,6 @@ def get_queue(ctx_factory): _WENO_PRG = {} -def get_weno_program_with_root_kernel(knl): - if not knl in _WENO_PRG: - setup_weno_program_with_root_kernel(knl) - return _WENO_PRG[knl] - - -def setup_weno_program_with_root_kernel(knl): - prg = get_weno_program() - prg = with_root_kernel(prg, knl) - _WENO_PRG[knl] = prg - - -def get_weno_program(): - if not _WENO_PRG: - parse_weno() - return _WENO_PRG["default"] - - def parse_weno(): fn = "WENO.F90" @@ -107,6 +89,12 @@ def parse_weno(): _WENO_PRG["default"] = prg +def get_weno_program(): + if not _WENO_PRG: + parse_weno() + return _WENO_PRG["default"] + + 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) @@ -119,6 +107,18 @@ def with_root_kernel(prg, root_name): return new_prg +def setup_weno_program_with_root_kernel(knl): + prg = get_weno_program() + prg = with_root_kernel(prg, knl) + _WENO_PRG[knl] = prg + + +def get_weno_program_with_root_kernel(knl): + if not knl in _WENO_PRG: + setup_weno_program_with_root_kernel(knl) + return _WENO_PRG[knl] + + def transform_weno_for_gpu(prg, print_kernel=False): cfd = prg["compute_flux_derivatives"] -- GitLab