From a6dcb57cff9a1dd9de7e5fafdc87230489be87b9 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Tue, 29 Oct 2019 19:25:38 +0100 Subject: Move further generator arguments into its constructor --- boltzgen.py | 16 +++++++----- boltzgen/kernel/generator.py | 47 +++++++++++++++++------------------- boltzgen/kernel/target/__init__.py | 2 +- boltzgen/kernel/target/cell_index.py | 18 -------------- boltzgen/kernel/target/index.py | 18 ++++++++++++++ boltzgen/kernel/target/layout/AOS.py | 6 ++--- boltzgen/kernel/target/layout/SOA.py | 6 ++--- 7 files changed, 57 insertions(+), 56 deletions(-) delete mode 100644 boltzgen/kernel/target/cell_index.py create mode 100644 boltzgen/kernel/target/index.py diff --git a/boltzgen.py b/boltzgen.py index 34d641d..1c8e931 100755 --- a/boltzgen.py +++ b/boltzgen.py @@ -10,7 +10,7 @@ argparser.add_argument('language', help = 'Target language (currently either "cl argparser.add_argument('--lattice', required = True, help = 'Lattice type (D2Q9, D3Q7, D3Q19, D3Q27)') argparser.add_argument('--layout', required = True, help = 'Memory layout ("AOS" or "SOA")') -argparser.add_argument('--indexing', required = False, help = 'Cell indexing ("XYZ" or "ZYX")') +argparser.add_argument('--index', required = False, help = 'Cell indexing ("XYZ" or "ZYX")') argparser.add_argument('--precision', required = True, help = 'Floating precision ("single" or "double")') argparser.add_argument('--geometry', required = True, help = 'Size of the block geometry ("x:y(:z)")') argparser.add_argument('--tau', required = True, help = 'BGK relaxation time') @@ -23,14 +23,18 @@ args = argparser.parse_args() lattice = eval("lbm.model.%s" % args.lattice) +if args.index is None: + args.index = 'XYZ' + lbm = LBM(lattice) generator = Generator( descriptor = lattice, moments = lbm.moments(optimize = not args.disable_cse), - collision = lbm.bgk(f_eq = lbm.equilibrium(), tau = float(args.tau), optimize = not args.disable_cse)) - -if args.indexing is None: - args.indexing = 'XYZ' + collision = lbm.bgk(f_eq = lbm.equilibrium(), tau = float(args.tau), optimize = not args.disable_cse), + language = args.language, + precision = args.precision, + index = args.index, + layout = args.layout) geometry = Geometry.parse(args.geometry) @@ -44,5 +48,5 @@ if 'default' in functions: extras = sum(args.extras, []) -src = generator.kernel(args.language, args.precision, args.layout, args.indexing, geometry, functions, extras) +src = generator.kernel(geometry, functions, extras) print(src) diff --git a/boltzgen/kernel/generator.py b/boltzgen/kernel/generator.py index da32a76..b2a632c 100644 --- a/boltzgen/kernel/generator.py +++ b/boltzgen/kernel/generator.py @@ -3,23 +3,36 @@ import sympy from mako.template import Template from pathlib import Path -import boltzgen.kernel.target +from . import target class Generator: - def __init__(self, descriptor, moments, collision): + def __init__(self, descriptor, moments, collision, language, precision, index, layout): self.descriptor = descriptor self.moments = moments self.collision = collision + self.target = language + self.float_type = eval("target.precision.%s" % language).get_float_type(precision) - def instantiate(self, target, template, float_type, layout_impl, geometry, extras = []): - template_path = Path(__file__).parent/("template/%s.%s.mako" % (template, target)) + try: + self.index_impl = eval("target.index.%s" % index) + except AttributeError: + raise Exception("There is no cell indexing scheme '%s'" % index) from None + + try: + self.layout_impl = eval("target.layout.%s" % layout) + except AttributeError: + raise Exception("There is no layout '%s'" % layout) from None + + def instantiate(self, template, geometry, extras = []): + template_path = Path(__file__).parent/("template/%s.%s.mako" % (template, self.target)) if not template_path.exists(): - raise Exception("Target '%s' doesn't provide '%s'" % (target, template)) + raise Exception("Target '%s' doesn't provide '%s'" % (self.target, template)) return Template(filename = str(template_path)).render( descriptor = self.descriptor, geometry = geometry, - layout = layout_impl, + index = self.index_impl(geometry), + layout = self.layout_impl(self.descriptor, self.index_impl, geometry), moments_subexpr = self.moments[0], moments_assignment = self.moments[1], @@ -27,29 +40,13 @@ class Generator: collision_assignment = self.collision[1], ccode = sympy.ccode, - float_type = float_type, + float_type = self.float_type, extras = extras ) - def kernel(self, target, precision, layout, cell_index, geometry, functions, extras = []): - cell_index_impl = eval("boltzgen.kernel.target.cell_index.%s" % cell_index) - if cell_index_impl is None: - raise Exception("There is no cell indexing scheme '%s'" % (target, layout)) - else: - cell_index_impl = cell_index_impl(geometry) - - layout_impl = eval("boltzgen.kernel.target.layout.%s" % layout) - if layout_impl is None: - raise Exception("There is no layout '%s'" % (target, layout)) - else: - layout_impl = layout_impl(self.descriptor, cell_index_impl, geometry) - + def kernel(self, geometry, functions, extras = []): if geometry.dimension() != self.descriptor.d: raise Exception('Geometry dimension must match descriptor dimension') - float_type = eval("boltzgen.kernel.target.precision.%s" % target).get_float_type(precision) - - return "\n".join(map( - lambda f: self.instantiate(target, f, float_type, layout_impl, geometry, extras), - functions)) + return "\n".join(map(lambda f: self.instantiate(f, geometry, extras), functions)) diff --git a/boltzgen/kernel/target/__init__.py b/boltzgen/kernel/target/__init__.py index d39f48b..5927d1b 100644 --- a/boltzgen/kernel/target/__init__.py +++ b/boltzgen/kernel/target/__init__.py @@ -1,3 +1,3 @@ from . import layout from . import precision -from . import cell_index +from . import index diff --git a/boltzgen/kernel/target/cell_index.py b/boltzgen/kernel/target/cell_index.py deleted file mode 100644 index 7e74dc5..0000000 --- a/boltzgen/kernel/target/cell_index.py +++ /dev/null @@ -1,18 +0,0 @@ -class CellIndexer: - def __init__(self, geometry): - self.geometry = geometry - - def neighbor(self, c_i): - return self.gid(*[ c for _, c in enumerate(c_i) ]) - -class XYZ(CellIndexer): - pass - - def gid(self, x, y, z=0): - return x*self.geometry.size_y*self.geometry.size_z + y*self.geometry.size_z + z - -class ZYX(CellIndexer): - pass - - def gid(self, x, y, z=0): - return z*self.geometry.size_x*self.geometry.size_y + y*self.geometry.size_x + x diff --git a/boltzgen/kernel/target/index.py b/boltzgen/kernel/target/index.py new file mode 100644 index 0000000..e89e9ee --- /dev/null +++ b/boltzgen/kernel/target/index.py @@ -0,0 +1,18 @@ +class CellIndex: + def __init__(self, geometry): + self.geometry = geometry + + def neighbor(self, c_i): + return self.gid(*[ c for _, c in enumerate(c_i) ]) + +class XYZ(CellIndex): + pass + + def gid(self, x, y, z=0): + return x*self.geometry.size_y*self.geometry.size_z + y*self.geometry.size_z + z + +class ZYX(CellIndex): + pass + + def gid(self, x, y, z=0): + return z*self.geometry.size_x*self.geometry.size_y + y*self.geometry.size_x + x diff --git a/boltzgen/kernel/target/layout/AOS.py b/boltzgen/kernel/target/layout/AOS.py index c603869..88e5377 100644 --- a/boltzgen/kernel/target/layout/AOS.py +++ b/boltzgen/kernel/target/layout/AOS.py @@ -1,7 +1,7 @@ class AOS: - def __init__(self, descriptor, cell_index, geometry): + def __init__(self, descriptor, index, geometry): self.descriptor = descriptor - self.cell_index = cell_index + self.index = index(geometry) self.geometry = geometry def cell_preshift(self, gid): @@ -11,4 +11,4 @@ class AOS: return i def neighbor_offset(self, c_i): - return self.descriptor.q * self.cell_index.neighbor(c_i) + return self.descriptor.q * self.index.neighbor(c_i) diff --git a/boltzgen/kernel/target/layout/SOA.py b/boltzgen/kernel/target/layout/SOA.py index 8f126ff..05e9e84 100644 --- a/boltzgen/kernel/target/layout/SOA.py +++ b/boltzgen/kernel/target/layout/SOA.py @@ -1,7 +1,7 @@ class SOA: - def __init__(self, descriptor, cell_index, geometry): + def __init__(self, descriptor, index, geometry): self.descriptor = descriptor - self.cell_index = cell_index + self.index = index(geometry) self.geometry = geometry def cell_preshift(self, gid): @@ -11,4 +11,4 @@ class SOA: return i * self.geometry.volume def neighbor_offset(self, c_i): - return self.cell_index.neighbor(c_i) + return self.index.neighbor(c_i) -- cgit v1.2.3