From 86360a6dbe42f5aa09e79def6313a03025f1d863 Mon Sep 17 00:00:00 2001 From: xywei Date: Mon, 9 Aug 2021 22:24:46 -0500 Subject: [PATCH 1/3] Refactors and updates --- examples/laplace2d.py | 1 + volumential/expansion_wrangler_fpnd.py | 39 +++++--------------------- volumential/list1.py | 10 +++++-- volumential/symbolic.py | 6 ++-- volumential/tools.py | 25 ++++++----------- 5 files changed, 26 insertions(+), 55 deletions(-) diff --git a/examples/laplace2d.py b/examples/laplace2d.py index 383951f..0d879b8 100644 --- a/examples/laplace2d.py +++ b/examples/laplace2d.py @@ -38,6 +38,7 @@ else: import numpy as np import pyopencl as cl +import pyopencl.array from volumential.tools import ScalarFieldExpressionEvaluation as Eval import pymbolic as pmbl diff --git a/volumential/expansion_wrangler_fpnd.py b/volumential/expansion_wrangler_fpnd.py index b691f63..95b47d7 100644 --- a/volumential/expansion_wrangler_fpnd.py +++ b/volumential/expansion_wrangler_fpnd.py @@ -121,6 +121,7 @@ class FPNDSumpyExpansionWrangler( kernel_extra_kwargs=None, self_extra_kwargs=None, list1_extra_kwargs=None, + translation_classes_data=None, ): """ near_field_table can either one of three things: @@ -128,10 +129,12 @@ class FPNDSumpyExpansionWrangler( 2. a list of tables, when len(target_kernels) = 1 (multiple levels) 3. otherwise, a dictionary from kernel.__repr__() to a list of its tables """ + if source_extra_kwargs is None: + source_extra_kwargs = {} - self.code = code_container - self.queue = queue - self.tree = tree + super().__init__( + code_container, queue, tree, dtype, fmm_level_to_order, + source_extra_kwargs, kernel_extra_kwargs, translation_classes_data) self.near_field_table = {} # list of tables for a single out kernel @@ -236,39 +239,11 @@ class FPNDSumpyExpansionWrangler( # deferred until trav.target_boxes is passed when invoking # eval_direct - self.dtype = dtype - - if source_extra_kwargs is None: - source_extra_kwargs = {} - - if kernel_extra_kwargs is None: - kernel_extra_kwargs = {} - - if self_extra_kwargs is None: - self_extra_kwargs = {} - if list1_extra_kwargs is None: list1_extra_kwargs = {} - - if not callable(fmm_level_to_order): - raise TypeError("fmm_level_to_order not passed") - - base_kernel = code_container.get_base_kernel() - kernel_arg_set = frozenset(kernel_extra_kwargs.items()) - self.level_orders = [ - fmm_level_to_order(base_kernel, kernel_arg_set, tree, lev) - for lev in range(tree.nlevels) - ] - - # print("Multipole order = ",self.level_orders) - - self.source_extra_kwargs = source_extra_kwargs - self.kernel_extra_kwargs = kernel_extra_kwargs - self.self_extra_kwargs = self_extra_kwargs self.list1_extra_kwargs = list1_extra_kwargs - self.extra_kwargs = source_extra_kwargs.copy() - self.extra_kwargs.update(self.kernel_extra_kwargs) + # print("Multipole order = ",self.level_orders) # }}} End constructor diff --git a/volumential/list1.py b/volumential/list1.py index e845589..10bb1bf 100644 --- a/volumential/list1.py +++ b/volumential/list1.py @@ -465,6 +465,8 @@ class NearFieldFromCSR(NearFieldEvalBase): def __call__(self, queue, **kwargs): knl = self.get_cached_optimized_kernel() + from loopy.target.opencl import OpenCLCallable + knl = loopy.register_callable(knl, "round", OpenCLCallable("round")) result = kwargs.pop("result") box_centers = kwargs.pop("box_centers") @@ -501,9 +503,11 @@ class NearFieldFromCSR(NearFieldEvalBase): integral_kernel_init_kargs[key] = np.float64(val) extra_knl_args_from_init = {} - for key, val in integral_kernel_init_kargs.items(): - if key in knl.arg_dict: - extra_knl_args_from_init[key] = val + if 0: + # loopy kernel no longer has arg_dict + for key, val in integral_kernel_init_kargs.items(): + if key in knl.arg_dict: + extra_knl_args_from_init[key] = val evt, res = knl( queue, diff --git a/volumential/symbolic.py b/volumential/symbolic.py index 1c5f9ec..a16e98e 100644 --- a/volumential/symbolic.py +++ b/volumential/symbolic.py @@ -126,9 +126,7 @@ def get_evaluator(dim, expression, variables=None): assert len(variables) == dim return ScalarFieldExpressionEvaluation( - dim=dim, - expression=expression, - variables=variables, - function_manglers=[math_func_mangler]) + dim=dim, expression=expression, + variables=variables) # }}} End evaluation helper diff --git a/volumential/tools.py b/volumential/tools.py index d5bc213..66dfc14 100644 --- a/volumential/tools.py +++ b/volumential/tools.py @@ -1,4 +1,3 @@ -from __future__ import absolute_import, division, print_function import six __copyright__ = "Copyright (C) 2018 Xiaoyu Wei" @@ -133,7 +132,7 @@ class ScalarFieldExpressionEvaluation(KernelCacheWrapper): """ def __init__(self, dim, expression, variables=None, dtype=np.float64, - function_manglers=None, preamble_generators=None): + preamble_generators=None): """ :arg dim :arg expression A pymbolic expression for the function @@ -142,19 +141,22 @@ class ScalarFieldExpressionEvaluation(KernelCacheWrapper): assert dim > 0 self.dim = dim - assert isinstance(expression, (ExpressionType, int, float, complex)) - self.expr = expression + if isinstance(expression, (ExpressionType, int, float, complex)): + self.expr = expression + else: + from pymbolic.interop.sympy import SympyToPymbolicMapper + stpm = SympyToPymbolicMapper() + self.expr = stpm(expression) if variables is None: self.vars = [pmbl.var("x%d" % d) for d in range(self.dim)] else: assert isinstance(variables, list) - for var in variables: - assert isinstance(var, VariableType) + variables = [var if isinstance(var, VariableType) + else stpm(var) for var in variables] self.vars = variables self.dtype = dtype - self.function_manglers = function_manglers self.preamble_generators = preamble_generators self.name = "ScalarFieldExpressionEvaluation" @@ -239,11 +241,6 @@ class ScalarFieldExpressionEvaluation(KernelCacheWrapper): loopy_knl = lp.set_options(loopy_knl, write_cl=False) loopy_knl = lp.set_options(loopy_knl, return_dict=True) - if self.function_manglers is not None: - loopy_knl = lp.register_function_manglers( - loopy_knl, - self.function_manglers) - if self.preamble_generators is not None: loopy_knl = lp.register_preamble_generators( loopy_knl, @@ -289,10 +286,6 @@ class ScalarFieldExpressionEvaluation(KernelCacheWrapper): knl = self.get_cached_optimized_kernel() - # FIXME: caching loses function mangler information - if self.function_manglers is not None: - knl = lp.register_function_manglers(knl, self.function_manglers) - if self.preamble_generators is not None: knl = lp.register_preamble_generators(knl, self.preamble_generators) -- GitLab From 8d9367b8a874b98a58a09ff65d56796983485aa0 Mon Sep 17 00:00:00 2001 From: xywei Date: Tue, 10 Aug 2021 08:33:04 -0500 Subject: [PATCH 2/3] Add import --- examples/laplace3d.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/laplace3d.py b/examples/laplace3d.py index 31e9615..eed3063 100644 --- a/examples/laplace3d.py +++ b/examples/laplace3d.py @@ -29,6 +29,7 @@ import logging import numpy as np import pyopencl as cl +import pyopencl.array import pymbolic as pmbl import pymbolic.functions -- GitLab From 37722be2eea4fd6f31a4b60e23f1d09158c00216 Mon Sep 17 00:00:00 2001 From: xywei Date: Tue, 10 Aug 2021 16:47:05 -0500 Subject: [PATCH 3/3] Help loopy type inference --- volumential/droste.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/volumential/droste.py b/volumential/droste.py index e594bf9..4836225 100644 --- a/volumential/droste.py +++ b/volumential/droste.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - __copyright__ = "Copyright (C) 2018 Xiaoyu Wei" __license__ = """ @@ -268,14 +266,14 @@ class DrosteBase(KernelCacheWrapper): ] from sumpy.symbolic import SympyToPymbolicMapper - + from sumpy.tools import ScalingAssignmentTag sympy_conv = SympyToPymbolicMapper() scaling_assignment = lp.Assignment( id=None, assignee="knl_scaling", expression=sympy_conv(self.integral_knl.get_global_scaling_const()), - temp_var_type=lp.Optional(), - ) + temp_var_type=lp.Optional(lp.auto), + tags=frozenset([ScalingAssignmentTag()])) return quad_kernel_insns + [scaling_assignment] @@ -524,10 +522,10 @@ class DrosteBase(KernelCacheWrapper): for iaxis template_target[iaxis] = if( template_target[iaxis] > 1, - 1, + 1.0, if( template_target[iaxis] < 0, - 0, + 0.0, template_target[iaxis]) ) {id=tplt_tgt4,dup=iaxis,dep=tplt_tgt3} end -- GitLab