From 00e44a6812cf2bcc5257323b26c1803627fe970e Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Tue, 29 Sep 2020 13:15:18 -0500 Subject: [PATCH] Run pyupgrade --py36-plus --- contrib/c-integer-semantics.py | 1 - doc/conf.py | 13 ++- examples/fortran/matmul-driver.py | 2 +- examples/python/ispc-stream-harness.py | 2 +- loopy/__init__.py | 13 +-- loopy/auto_test.py | 13 +-- loopy/check.py | 104 +++++++++---------- loopy/cli.py | 16 ++- loopy/codegen/__init__.py | 10 +- loopy/codegen/bounds.py | 2 - loopy/codegen/control.py | 3 +- loopy/codegen/instruction.py | 6 +- loopy/codegen/loop.py | 7 +- loopy/codegen/result.py | 5 +- loopy/compiled.py | 4 +- loopy/diagnostic.py | 4 +- loopy/expression.py | 2 - loopy/frontend/fortran/__init__.py | 2 - loopy/frontend/fortran/diagnostic.py | 2 - loopy/frontend/fortran/expression.py | 4 +- loopy/frontend/fortran/translator.py | 11 +- loopy/frontend/fortran/tree.py | 4 +- loopy/ipython_ext.py | 2 - loopy/isl_helpers.py | 9 +- loopy/kernel/__init__.py | 134 ++++++++++++------------- loopy/kernel/array.py | 36 +++---- loopy/kernel/creation.py | 51 +++++----- loopy/kernel/data.py | 39 ++++--- loopy/kernel/instruction.py | 63 ++++++------ loopy/kernel/tools.py | 88 ++++++++-------- loopy/library/function.py | 2 - loopy/library/random123.py | 7 +- loopy/library/reduction.py | 10 +- loopy/loop.py | 5 +- loopy/match.py | 17 ++-- loopy/options.py | 7 +- loopy/preprocess.py | 92 ++++++++--------- loopy/schedule/__init__.py | 104 +++++++++---------- loopy/schedule/device_mapping.py | 2 - loopy/schedule/tools.py | 6 +- loopy/statistics.py | 78 +++++++------- loopy/symbolic.py | 93 +++++++++-------- loopy/target/__init__.py | 9 +- loopy/target/c/__init__.py | 35 +++---- loopy/target/c/c_execution.py | 29 +++--- loopy/target/c/codegen/expression.py | 32 +++--- loopy/target/cuda.py | 11 +- loopy/target/execution.py | 32 +++--- loopy/target/ispc.py | 11 +- loopy/target/numba.py | 7 +- loopy/target/opencl.py | 31 +++--- loopy/target/pyopencl.py | 39 +++---- loopy/target/pyopencl_execution.py | 13 +-- loopy/target/python.py | 23 ++--- loopy/tools.py | 35 +++---- loopy/transform/__init__.py | 2 - loopy/transform/add_barrier.py | 2 - loopy/transform/arithmetic.py | 29 +++--- loopy/transform/array_buffer_map.py | 11 +- loopy/transform/batch.py | 20 ++-- loopy/transform/buffer.py | 13 +-- loopy/transform/data.py | 10 +- loopy/transform/diff.py | 4 +- loopy/transform/fusion.py | 12 +-- loopy/transform/iname.py | 63 ++++++------ loopy/transform/instruction.py | 8 +- loopy/transform/padding.py | 22 ++-- loopy/transform/parameter.py | 7 +- loopy/transform/precompute.py | 24 ++--- loopy/transform/privatize.py | 15 ++- loopy/transform/save.py | 37 ++++--- loopy/transform/subst.py | 31 +++--- loopy/type_inference.py | 20 ++-- loopy/types.py | 7 +- loopy/version.py | 2 +- proto-tests/test_fem_assembly.py | 2 - proto-tests/test_sem.py | 2 - proto-tests/test_sem_tim.py | 2 - proto-tests/test_tim.py | 2 - setup.py | 8 +- test/test_apps.py | 2 - test/test_c_execution.py | 8 +- test/test_dg.py | 2 - test/test_diff.py | 2 - test/test_domain.py | 5 - test/test_expression.py | 17 ++-- test/test_fortran.py | 2 - test/test_linalg.py | 2 - test/test_loopy.py | 27 ++--- test/test_misc.py | 13 +-- test/test_nbody.py | 2 - test/test_numa_diff.py | 3 +- test/test_reduction.py | 2 - test/test_scan.py | 4 +- test/test_sem_reagan.py | 2 - test/test_statistics.py | 7 +- test/test_target.py | 2 - test/test_transform.py | 4 +- test/testlib.py | 2 +- 99 files changed, 780 insertions(+), 1065 deletions(-) diff --git a/contrib/c-integer-semantics.py b/contrib/c-integer-semantics.py index 5e05ec688..23c7cb319 100644 --- a/contrib/c-integer-semantics.py +++ b/contrib/c-integer-semantics.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# coding: utf-8 from os import system import ctypes diff --git a/doc/conf.py b/doc/conf.py index 6c2579ef1..e8e5f592c 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # loopy documentation build configuration file, created by # sphinx-quickstart on Tue Aug 9 13:40:49 2011. @@ -46,8 +45,8 @@ source_suffix = '.rst' master_doc = 'index' # General information about the project. -project = u'loopy' -copyright = u'2016, Andreas Klöckner' +project = 'loopy' +copyright = '2016, Andreas Klöckner' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the @@ -206,8 +205,8 @@ htmlhelp_basename = 'loopydoc' # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ - ('index', 'loopy.tex', u'loopy Documentation', - u'Andreas Kloeckner', 'manual'), + ('index', 'loopy.tex', 'loopy Documentation', + 'Andreas Kloeckner', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of @@ -239,8 +238,8 @@ latex_documents = [ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - ('index', 'loopy', u'loopy Documentation', - [u'Andreas Kloeckner'], 1) + ('index', 'loopy', 'loopy Documentation', + ['Andreas Kloeckner'], 1) ] diff --git a/examples/fortran/matmul-driver.py b/examples/fortran/matmul-driver.py index 9db569480..111ac2411 100644 --- a/examples/fortran/matmul-driver.py +++ b/examples/fortran/matmul-driver.py @@ -8,7 +8,7 @@ import loopy as lp def main(): fn = "matmul.floopy" - with open(fn, "r") as inf: + with open(fn) as inf: source = inf.read() dgemm, = lp.parse_transformed_fortran(source, filename=fn) diff --git a/examples/python/ispc-stream-harness.py b/examples/python/ispc-stream-harness.py index fa581d426..41fddfdee 100644 --- a/examples/python/ispc-stream-harness.py +++ b/examples/python/ispc-stream-harness.py @@ -56,7 +56,7 @@ else: def main(): - with open("tasksys.cpp", "r") as ts_file: + with open("tasksys.cpp") as ts_file: tasksys_source = ts_file.read() def make_knl(name, insn, vars): diff --git a/loopy/__init__.py b/loopy/__init__.py index 463614d4e..66ba75024 100644 --- a/loopy/__init__.py +++ b/loopy/__init__.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -23,9 +21,6 @@ THE SOFTWARE. """ -import six -from six.moves import range, zip - from loopy.symbolic import ( TaggedVariable, Reduction, LinearSubscript, TypeCast) from loopy.diagnostic import LoopyError, LoopyWarning @@ -314,7 +309,7 @@ def set_options(kernel, *args, **kwargs): from loopy.options import _apply_legacy_map, Options kwargs = _apply_legacy_map(Options._legacy_options_map, kwargs) - for key, val in six.iteritems(kwargs): + for key, val in kwargs.items(): if not hasattr(new_opt, key): raise ValueError("unknown option '%s'" % key) @@ -417,7 +412,7 @@ def set_caching_enabled(flag): CACHING_ENABLED = flag -class CacheMode(object): +class CacheMode: """A context manager for setting whether :mod:`loopy` is allowed to use disk caches. """ @@ -464,10 +459,10 @@ def make_copy_kernel(new_dim_tags, old_dim_tags=None): shape = ["n%d" % i for i in range(rank)] commad_indices = ", ".join(indices) bounds = " and ".join( - "0<=%s<%s" % (ind, shape_i) + f"0<={ind}<{shape_i}" for ind, shape_i in zip(indices, shape)) - set_str = "{[%s]: %s}" % ( + set_str = "{{[{}]: {}}}".format( commad_indices, bounds ) diff --git a/loopy/auto_test.py b/loopy/auto_test.py index ca70c8489..12ec80355 100644 --- a/loopy/auto_test.py +++ b/loopy/auto_test.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -22,7 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -from six.moves import range, zip import os from warnings import warn @@ -454,7 +451,7 @@ def auto_test_vs_ref( ref_sched_kernel = knl break - logger.info("%s (ref): trying %s for the reference calculation" % ( + logger.info("{} (ref): trying {} for the reference calculation".format( ref_knl.name, dev)) ref_compiled = CompiledKernel(ref_ctx, ref_sched_kernel) @@ -494,7 +491,7 @@ def auto_test_vs_ref( ref_queue.finish() - logger.info("%s (ref): using %s for the reference calculation" % ( + logger.info("{} (ref): using {} for the reference calculation".format( ref_knl.name, dev)) logger.info("%s (ref): run" % ref_knl.name) @@ -677,7 +674,7 @@ def auto_test_vs_ref( rates = "" for cnt, lbl in zip(op_count, op_label): - rates += " %g %s/s" % (cnt/elapsed_wall, lbl) + rates += " {:g} {}/s".format(cnt/elapsed_wall, lbl) if not quiet: def format_float_or_none(v): @@ -695,9 +692,9 @@ def auto_test_vs_ref( if do_check: ref_rates = "" for cnt, lbl in zip(op_count, op_label): - ref_rates += " %g %s/s" % (cnt/ref_elapsed_event, lbl) + ref_rates += " {:g} {}/s".format(cnt/ref_elapsed_event, lbl) if not quiet: - print("ref: elapsed: %g s event, %g s wall%s" % ( + print("ref: elapsed: {:g} s event, {:g} s wall{}".format( ref_elapsed_event, ref_elapsed_wall, ref_rates)) # }}} diff --git a/loopy/check.py b/loopy/check.py index b3b169d76..e557181be 100644 --- a/loopy/check.py +++ b/loopy/check.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -22,8 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six -from six.moves import range from islpy import dim_type import islpy as isl @@ -78,7 +74,7 @@ def check_identifiers_in_subst_rules(knl): allowed_identifiers = knl.all_variable_names() - for rule in six.itervalues(knl.substitutions): + for rule in knl.substitutions.values(): deps = get_dependencies(rule.expression) rule_allowed_identifiers = allowed_identifiers | frozenset(rule.arguments) @@ -138,7 +134,7 @@ def check_insn_attributes(kernel): """ Check for legality of attributes of every instruction in *kernel*. """ - all_insn_ids = set(insn.id for insn in kernel.instructions) + all_insn_ids = {insn.id for insn in kernel.instructions} for insn in kernel.instructions: if not insn.within_inames <= kernel.all_inames(): @@ -153,14 +149,14 @@ def check_insn_attributes(kernel): % (insn.id, ", ".join( insn.depends_on - all_insn_ids))) - no_sync_with_insn_ids = set(id for id, scope in insn.no_sync_with) + no_sync_with_insn_ids = {id for id, scope in insn.no_sync_with} if not no_sync_with_insn_ids <= all_insn_ids: raise LoopyError("insn '%s' has nosync directive with unknown " "instruction ids: %s" % (insn.id, ", ".join(no_sync_with_insn_ids - all_insn_ids))) - no_sync_with_scopes = set(scope for id, scope in insn.no_sync_with) + no_sync_with_scopes = {scope for id, scope in insn.no_sync_with} if not no_sync_with_scopes <= VALID_NOSYNC_SCOPES: raise LoopyError("insn '%s' has invalid nosync scopes: %s" % (insn.id, @@ -203,11 +199,11 @@ def check_multiple_tags_allowed(kernel): (GroupIndexTag, LocalIndexTag, VectorizeTag, UnrollTag, ForceSequentialTag), (IlpBaseTag, ForceSequentialTag) ] - for iname, tags in six.iteritems(kernel.iname_to_tags): + for iname, tags in kernel.iname_to_tags.items(): for comb in illegal_combinations: if len(filter_iname_tags_by_type(tags, comb)) > 1: - raise LoopyError("iname {0} has illegal combination of " - "tags: {1}".format(iname, tags)) + raise LoopyError("iname {} has illegal combination of " + "tags: {}".format(iname, tags)) def check_for_double_use_of_hw_axes(kernel): @@ -307,16 +303,16 @@ def check_for_write_races(kernel): # Any concurrent tags that are not depended upon by the assignee # will cause write races. - raceable_parallel_insn_inames = set( + raceable_parallel_insn_inames = { iname for iname in kernel.insn_inames(insn) - if kernel.iname_tags_of_type(iname, ConcurrentTag)) + if kernel.iname_tags_of_type(iname, ConcurrentTag)} elif assignee_name in kernel.temporary_variables: temp_var = kernel.temporary_variables[assignee_name] - raceable_parallel_insn_inames = set( + raceable_parallel_insn_inames = { iname for iname in kernel.insn_inames(insn) if any(_is_racing_iname_tag(temp_var, tag) - for tag in kernel.iname_tags(iname))) + for tag in kernel.iname_tags(iname))} else: raise LoopyError("invalid assignee name in instruction '%s'" @@ -338,7 +334,7 @@ def check_for_orphaned_user_hardware_axes(kernel): from loopy.kernel.data import LocalIndexTag for axis in kernel.local_sizes: found = False - for tags in six.itervalues(kernel.iname_to_tags): + for tags in kernel.iname_to_tags.values(): for tag in tags: if isinstance(tag, LocalIndexTag) and tag.axis == axis: found = True @@ -360,9 +356,9 @@ def check_for_data_dependent_parallel_bounds(kernel): for i, dom in enumerate(kernel.domains): dom_inames = set(dom.get_var_names(dim_type.set)) - par_inames = set( + par_inames = { iname for iname in dom_inames - if kernel.iname_tags_of_type(iname, ConcurrentTag)) + if kernel.iname_tags_of_type(iname, ConcurrentTag)} if not par_inames: continue @@ -508,7 +504,7 @@ def check_has_schedulable_iname_nesting(kernel): if not has_schedulable_iname_nesting(kernel): import itertools as it opt = get_iname_duplication_options(kernel) - opt_str = "\n".join("* Duplicate %s within instructions %s" % (i, w) + opt_str = "\n".join(f"* Duplicate {i} within instructions {w}" for i, w in it.islice(opt, 3)) raise LoopyError("Kernel does not have a schedulable iname nesting. " "In order for there to exist a feasible loop nesting, you " @@ -619,11 +615,11 @@ def _check_variable_access_ordered_inner(kernel): *[wmap.get(eq_name, set()) for eq_name in eq_class]) for writer in writers: - required_deps = (readers | writers) - set([writer]) - required_deps = set([req_dep + required_deps = (readers | writers) - {writer} + required_deps = {req_dep for req_dep in required_deps if not declares_nosync_with(kernel, address_space, writer, - req_dep)]) + req_dep)} for req_dep in required_deps: dep_reqs_to_vars.setdefault((writer, req_dep), set()).add(var) @@ -631,11 +627,11 @@ def _check_variable_access_ordered_inner(kernel): # }}} # depends_on: mapping from insn_ids to their dependencies - depends_on = dict((insn.id, set()) for insn in - kernel.instructions) + depends_on = {insn.id: set() for insn in + kernel.instructions} # rev_depends: mapping from insn_ids to their reverse deps. - rev_depends = dict((insn.id, set()) for insn in - kernel.instructions) + rev_depends = {insn.id: set() for insn in + kernel.instructions} # {{{ populate rev_depends, depends_on @@ -679,7 +675,7 @@ def _check_variable_access_ordered_inner(kernel): for successor in edges[insn_id]: predecessors.setdefault(successor, set()).update( - insn_predecessors | set([insn_id])) + insn_predecessors | {insn_id}) # forward dep. graph traversal in reverse topological sort order # (proceeds "end of program" -> "beginning of program") @@ -694,7 +690,7 @@ def _check_variable_access_ordered_inner(kernel): # {{{ handle dependency requirements that weren't satisfied - for (writer_id, other_id), variables in six.iteritems(dep_reqs_to_vars): + for (writer_id, other_id), variables in dep_reqs_to_vars.items(): writer = kernel.id_to_insn[writer_id] other = kernel.id_to_insn[other_id] @@ -833,29 +829,29 @@ def _find_boostable_insn_ids(kernel): writer_map = kernel.writer_map() - arg_names = set(arg.name for arg in kernel.args) + arg_names = {arg.name for arg in kernel.args} - var_names = arg_names | set(six.iterkeys(kernel.temporary_variables)) + var_names = arg_names | set(kernel.temporary_variables.keys()) - reads_map = dict( - (insn.id, insn.read_dependency_names() & var_names) - for insn in kernel.instructions) + reads_map = { + insn.id: insn.read_dependency_names() & var_names + for insn in kernel.instructions} from collections import defaultdict dep_graph = defaultdict(set) for insn in kernel.instructions: - dep_graph[insn.id] = set(writer_id + dep_graph[insn.id] = {writer_id for var in reads_map[insn.id] - for writer_id in writer_map.get(var, set())) + for writer_id in writer_map.get(var, set())} # Find SCCs of dep_graph. These are used for checking if the instruction is # in a dependency cycle. from pytools.graph import compute_sccs - sccs = dict((item, scc) + sccs = {item: scc for scc in compute_sccs(dep_graph) - for item in scc) + for item in scc} non_idempotently_updated_vars = set() boostable_insn_ids = set() @@ -902,8 +898,8 @@ def _check_for_unused_hw_axes_in_kernel_chunk(kernel, sched_index=None): group_size, local_size = kernel.get_grid_sizes_for_insn_ids_as_exprs( get_insn_ids_for_block_at(kernel.schedule, sched_index)) - group_axes = set(ax for ax, length in enumerate(group_size)) - local_axes = set(ax for ax, length in enumerate(local_size)) + group_axes = {ax for ax, length in enumerate(group_size)} + local_axes = {ax for ax, length in enumerate(local_size)} i = sched_index + 1 assert isinstance(kernel.schedule[past_end_i - 1], ReturnFromKernel) @@ -1005,18 +1001,18 @@ def check_that_atomic_ops_are_used_exactly_on_atomic_arrays(kernel): from loopy.kernel.data import ArrayBase, Assignment from loopy.types import AtomicType atomicity_candidates = ( - set(v.name for v in six.itervalues(kernel.temporary_variables) - if isinstance(v.dtype, AtomicType)) + {v.name for v in kernel.temporary_variables.values() + if isinstance(v.dtype, AtomicType)} | - set(v.name for v in kernel.args + {v.name for v in kernel.args if isinstance(v, ArrayBase) - and isinstance(v.dtype, AtomicType))) + and isinstance(v.dtype, AtomicType)}) for insn in kernel.instructions: if not isinstance(insn, Assignment): continue - atomic_accesses = set(a.var_name for a in insn.atomicity) + atomic_accesses = {a.var_name for a in insn.atomicity} if not atomic_accesses <= atomicity_candidates: raise LoopyError("atomic access in instruction '%s' to " "non-atomic variable(s) '%s'" @@ -1082,12 +1078,12 @@ def check_that_temporaries_are_defined_in_subkernels_where_used(kernel): def check_that_all_insns_are_scheduled(kernel): - all_schedulable_insns = set(insn.id for insn in kernel.instructions) + all_schedulable_insns = {insn.id for insn in kernel.instructions} from loopy.schedule import sched_item_to_insn_id - scheduled_insns = set( + scheduled_insns = { insn_id for sched_item in kernel.schedule - for insn_id in sched_item_to_insn_id(sched_item)) + for insn_id in sched_item_to_insn_id(sched_item)} assert scheduled_insns <= all_schedulable_insns @@ -1108,11 +1104,11 @@ def check_that_shapes_and_strides_are_arguments(kernel): from loopy.symbolic import get_dependencies import loopy as lp - integer_arg_names = set( + integer_arg_names = { arg.name for arg in kernel.args if isinstance(arg, ValueArg) - and arg.dtype.is_integral()) + and arg.dtype.is_integral()} for arg in kernel.args: if isinstance(arg, ArrayBase): @@ -1177,7 +1173,7 @@ def check_implemented_domains(kernel, implemented_domains, code=None): last_idomains = None last_insn_inames = None - for insn_id, idomains in six.iteritems(implemented_domains): + for insn_id, idomains in implemented_domains.items(): insn = kernel.id_to_insn[insn_id] assert idomains @@ -1235,9 +1231,9 @@ def check_implemented_domains(kernel, implemented_domains, code=None): i_minus_d = insn_impl_domain - desired_domain d_minus_i = desired_domain - insn_impl_domain - parameter_inames = set( + parameter_inames = { insn_domain.get_dim_name(dim_type.param, i) - for i in range(insn_impl_domain.dim(dim_type.param))) + for i in range(insn_impl_domain.dim(dim_type.param))} lines = [] for bigger, smaller, diff_set, gist_domain in [ @@ -1265,10 +1261,10 @@ def check_implemented_domains(kernel, implemented_domains, code=None): iname, pt.get_coordinate_val(tp, dim).to_python())) lines.append( - "sample point in %s but not %s: %s" % ( + "sample point in {} but not {}: {}".format( bigger, smaller, ", ".join(point_axes))) lines.append( - "gist of constraints in %s but not %s: %s" % ( + "gist of constraints in {} but not {}: {}".format( smaller, bigger, gist_domain)) if code is not None: diff --git a/loopy/cli.py b/loopy/cli.py index a3afa0fb6..4230b7496 100644 --- a/loopy/cli.py +++ b/loopy/cli.py @@ -1,5 +1,3 @@ -from __future__ import print_function - import sys import loopy as lp @@ -48,7 +46,7 @@ def defines_to_python_code(defines_str): raise RuntimeError("#define not understood: '%s'" % line) result.append( - "%s = %s" % (match.group(1), to_python_literal(match.group(2)))) + "{} = {}".format(match.group(1), to_python_literal(match.group(2)))) return "\n".join(result) @@ -112,7 +110,7 @@ def main(): ".f": "fortran", ".f77": "fortran", }.get(ext) - with open(args.infile, "r") as infile_fd: + with open(args.infile) as infile_fd: infile_content = infile_fd.read() if args.lang is not None: @@ -143,15 +141,15 @@ def main(): data_dic["np"] = np if args.occa_defines: - with open(args.occa_defines, "r") as defines_fd: + with open(args.occa_defines) as defines_fd: occa_define_code = defines_to_python_code(defines_fd.read()) exec(compile(occa_define_code, args.occa_defines, "exec"), data_dic) - with open(args.infile, "r") as infile_fd: + with open(args.infile) as infile_fd: exec(compile(infile_content, args.infile, "exec"), data_dic) if args.transform: - with open(args.transform, "r") as xform_fd: + with open(args.transform) as xform_fd: exec(compile(xform_fd.read(), args.transform, "exec"), data_dic) @@ -169,14 +167,14 @@ def main(): elif lang in ["fortran", "floopy", "fpp"]: pre_transform_code = None if args.transform: - with open(args.transform, "r") as xform_fd: + with open(args.transform) as xform_fd: pre_transform_code = xform_fd.read() if args.occa_defines: if pre_transform_code is None: pre_transform_code = "" - with open(args.occa_defines, "r") as defines_fd: + with open(args.occa_defines) as defines_fd: pre_transform_code = ( defines_to_python_code(defines_fd.read()) + pre_transform_code) diff --git a/loopy/codegen/__init__.py b/loopy/codegen/__init__.py index b6dff1575..cbae4eac5 100644 --- a/loopy/codegen/__init__.py +++ b/loopy/codegen/__init__.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -22,8 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six - from loopy.diagnostic import LoopyError, warn from pytools import ImmutableRecord import islpy as isl @@ -130,7 +126,7 @@ class Unvectorizable(Exception): pass -class VectorizationInfo(object): +class VectorizationInfo: """ .. attribute:: iname .. attribute:: length @@ -159,7 +155,7 @@ class SeenFunction(ImmutableRecord): arg_dtypes=arg_dtypes) -class CodeGenerationState(object): +class CodeGenerationState: """ .. attribute:: kernel .. attribute:: implemented_data_info @@ -459,7 +455,7 @@ def generate_code_v2(kernel): raise ValueError("argument type not understood: '%s'" % type(arg)) allow_complex = False - for var in kernel.args + list(six.itervalues(kernel.temporary_variables)): + for var in kernel.args + list(kernel.temporary_variables.values()): if var.dtype.involves_complex(): allow_complex = True diff --git a/loopy/codegen/bounds.py b/loopy/codegen/bounds.py index b736191ec..793260757 100644 --- a/loopy/codegen/bounds.py +++ b/loopy/codegen/bounds.py @@ -1,5 +1,3 @@ -from __future__ import division - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ diff --git a/loopy/codegen/control.py b/loopy/codegen/control.py index 7319b16ac..829b1a2a8 100644 --- a/loopy/codegen/control.py +++ b/loopy/codegen/control.py @@ -1,6 +1,5 @@ """Loop nest build top-level control/hoisting.""" -from __future__ import division, absolute_import __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" @@ -471,7 +470,7 @@ def build_loop_nest(codegen_state, schedule_index): sched_index_info_entries[0:group_length], inner_codegen_state, done_group_lengths=( - done_group_lengths | set([group_length]))) + done_group_lengths | {group_length})) # gen_code returns a list diff --git a/loopy/codegen/instruction.py b/loopy/codegen/instruction.py index bf02131e8..b2e21763d 100644 --- a/loopy/codegen/instruction.py +++ b/loopy/codegen/instruction.py @@ -1,6 +1,5 @@ """Code generation for Instruction objects.""" -from __future__ import division, absolute_import __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" @@ -25,7 +24,6 @@ THE SOFTWARE. """ -from six.moves import range import islpy as isl dim_type = isl.dim_type from loopy.codegen import Unvectorizable @@ -171,7 +169,7 @@ def generate_assignment_instruction_code(codegen_state, insn): gs, ls = kernel.get_grid_size_upper_bounds() - printf_format = "%s.%s[%s][%s]: %s" % ( + printf_format = "{}.{}[{}][{}]: {}".format( kernel.name, insn.id, ", ".join("gid%d=%%d" % i for i in range(len(gs))), @@ -208,7 +206,7 @@ def generate_assignment_instruction_code(codegen_state, insn): else: printf_args_str = "" - printf_insn = S('printf("%s\\n"%s)' % ( + printf_insn = S('printf("{}\\n"{})'.format( printf_format, printf_args_str)) from cgen import Block diff --git a/loopy/codegen/loop.py b/loopy/codegen/loop.py index bc013307f..a54357da8 100644 --- a/loopy/codegen/loop.py +++ b/loopy/codegen/loop.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -22,7 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -from six.moves import range from loopy.diagnostic import warn, LoopyError from loopy.codegen.result import merge_codegen_results @@ -325,7 +322,7 @@ def set_up_hw_parallel_loops(codegen_state, schedule_index, next_func, if len(slabs) > 1: result.append( codegen_state.ast_builder.emit_comment( - "%s slab for '%s'" % (slab_name, iname))) + f"{slab_name} slab for '{iname}'")) # Have the conditional infrastructure generate the # slabbing conditionals. @@ -364,7 +361,7 @@ def generate_sequential_loop_dim_code(codegen_state, sched_index): result = [] for slab_name, slab in slabs: - cmt = "%s slab for '%s'" % (slab_name, loop_iname) + cmt = f"{slab_name} slab for '{loop_iname}'" if len(slabs) == 1: cmt = None diff --git a/loopy/codegen/result.py b/loopy/codegen/result.py index 980fe82ea..685df8fde 100644 --- a/loopy/codegen/result.py +++ b/loopy/codegen/result.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2016 Andreas Kloeckner" __license__ = """ @@ -22,7 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six from pytools import ImmutableRecord @@ -231,7 +228,7 @@ def merge_codegen_results(codegen_state, elements, collapse=True): el.current_program(codegen_state).name == codegen_result.current_program(codegen_state).name) - for insn_id, idoms in six.iteritems(el.implemented_domains): + for insn_id, idoms in el.implemented_domains.items(): implemented_domains.setdefault(insn_id, []).extend(idoms) if not codegen_state.is_generating_device_code: diff --git a/loopy/compiled.py b/loopy/compiled.py index baf57e2f9..f9313c6c9 100644 --- a/loopy/compiled.py +++ b/loopy/compiled.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2016 Andreas Kloeckner" __license__ = """ @@ -38,6 +36,6 @@ class CompiledKernel(PyOpenCLKernelExecutor): warn("CompiledKernel is deprecated. Use LoopKernel.__call__ directly.", DeprecationWarning, stacklevel=2) - super(CompiledKernel, self).__init__(context, kernel) + super().__init__(context, kernel) # }}} diff --git a/loopy/diagnostic.py b/loopy/diagnostic.py index 561bbc7cc..0ae2e530a 100644 --- a/loopy/diagnostic.py +++ b/loopy/diagnostic.py @@ -1,5 +1,3 @@ -from __future__ import division - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -60,7 +58,7 @@ def warn_with_kernel(kernel, id, text, type=LoopyWarning): % id) from warnings import warn - warn("in kernel %s: %s" % (kernel.name, text), type, stacklevel=2) + warn(f"in kernel {kernel.name}: {text}", type, stacklevel=2) warn = MovedFunctionDeprecationWrapper(warn_with_kernel) diff --git a/loopy/expression.py b/loopy/expression.py index b6b85e281..10e193014 100644 --- a/loopy/expression.py +++ b/loopy/expression.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - __copyright__ = "Copyright (C) 2012-15 Andreas Kloeckner" __license__ = """ diff --git a/loopy/frontend/fortran/__init__.py b/loopy/frontend/fortran/__init__.py index 81632084b..00dc837e1 100644 --- a/loopy/frontend/fortran/__init__.py +++ b/loopy/frontend/fortran/__init__.py @@ -1,5 +1,3 @@ -from __future__ import division, with_statement - __copyright__ = "Copyright (C) 2013 Andreas Kloeckner" __license__ = """ diff --git a/loopy/frontend/fortran/diagnostic.py b/loopy/frontend/fortran/diagnostic.py index 7cb3c79cc..b2ea02c05 100644 --- a/loopy/frontend/fortran/diagnostic.py +++ b/loopy/frontend/fortran/diagnostic.py @@ -1,5 +1,3 @@ -from __future__ import division, with_statement - __copyright__ = "Copyright (C) 2009 Andreas Kloeckner" __license__ = """ diff --git a/loopy/frontend/fortran/expression.py b/loopy/frontend/fortran/expression.py index ea724278f..354a769a0 100644 --- a/loopy/frontend/fortran/expression.py +++ b/loopy/frontend/fortran/expression.py @@ -1,5 +1,3 @@ -from __future__ import division, with_statement - __copyright__ = "Copyright (C) 2013 Andreas Kloeckner" __license__ = """ @@ -25,7 +23,7 @@ THE SOFTWARE. from pymbolic.parser import Parser as ExpressionParserBase from loopy.frontend.fortran.diagnostic import TranslationError -from six.moves import intern +from sys import intern import numpy as np import pytools.lex diff --git a/loopy/frontend/fortran/translator.py b/loopy/frontend/fortran/translator.py index 91a5fdc88..ff357ae8a 100644 --- a/loopy/frontend/fortran/translator.py +++ b/loopy/frontend/fortran/translator.py @@ -1,5 +1,3 @@ -from __future__ import division, with_statement - __copyright__ = "Copyright (C) 2013 Andreas Kloeckner" __license__ = """ @@ -24,8 +22,7 @@ THE SOFTWARE. import re -import six -from six.moves import intern +from sys import intern import loopy as lp import numpy as np @@ -81,7 +78,7 @@ class SubscriptIndexBaseShifter(IdentityMapper): # {{{ scope -class Scope(object): +class Scope: def __init__(self, subprogram_name, arg_names=set()): self.subprogram_name = subprogram_name @@ -122,8 +119,8 @@ class Scope(object): def known_names(self): return (self.used_names - | set(six.iterkeys(self.dim_map)) - | set(six.iterkeys(self.type_map))) + | set(self.dim_map.keys()) + | set(self.type_map.keys())) def is_known(self, name): return (name in self.used_names diff --git a/loopy/frontend/fortran/tree.py b/loopy/frontend/fortran/tree.py index c73896774..46911556e 100644 --- a/loopy/frontend/fortran/tree.py +++ b/loopy/frontend/fortran/tree.py @@ -1,5 +1,3 @@ -from __future__ import division, with_statement - __copyright__ = "Copyright (C) 2009 Andreas Kloeckner" __license__ = """ @@ -27,7 +25,7 @@ import re from loopy.diagnostic import LoopyError -class FTreeWalkerBase(object): +class FTreeWalkerBase: def __init__(self, filename): from loopy.frontend.fortran.expression import FortranExpressionParser self.expr_parser = FortranExpressionParser(self) diff --git a/loopy/ipython_ext.py b/loopy/ipython_ext.py index ec1b10f1f..7f9177e0e 100644 --- a/loopy/ipython_ext.py +++ b/loopy/ipython_ext.py @@ -1,5 +1,3 @@ -from __future__ import division - from IPython.core.magic import (magics_class, Magics, cell_magic) import loopy as lp diff --git a/loopy/isl_helpers.py b/loopy/isl_helpers.py index 1b2a83aad..87f90137f 100644 --- a/loopy/isl_helpers.py +++ b/loopy/isl_helpers.py @@ -1,6 +1,5 @@ """isl helpers""" -from __future__ import division, absolute_import __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" @@ -25,8 +24,6 @@ THE SOFTWARE. """ -from six.moves import range, zip - from loopy.diagnostic import StaticValueFindingError import islpy as isl @@ -529,7 +526,7 @@ def _find_noninteracting_dims(obj, dt, idx, other_dt, stop_at=6): if len(conn) > 1: connections.append(conn) - interacting = set([(dt, idx)]) + interacting = {(dt, idx)} while True: changed_something = False @@ -552,9 +549,9 @@ def _find_noninteracting_dims(obj, dt, idx, other_dt, stop_at=6): if not changed_something: break - return set(range(obj.dim(other_dt))) - set( + return set(range(obj.dim(other_dt))) - { idx for dt, idx in interacting - if dt == other_dt) + if dt == other_dt} def _eliminate_noninteracting(obj, dt, idx, other_dt): diff --git a/loopy/kernel/__init__.py b/loopy/kernel/__init__.py index 76785e79d..4116cd497 100644 --- a/loopy/kernel/__init__.py +++ b/loopy/kernel/__init__.py @@ -1,7 +1,5 @@ """Kernel object.""" -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -24,8 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six -from six.moves import range, zip, intern +from sys import intern from collections import defaultdict @@ -52,7 +49,7 @@ from warnings import warn class _UniqueVarNameGenerator(UniqueNameGenerator): def __init__(self, existing_names=set(), forced_prefix=""): - super(_UniqueVarNameGenerator, self).__init__(existing_names, forced_prefix) + super().__init__(existing_names, forced_prefix) array_prefix_pattern = re.compile("(.*)_s[0-9]+$") array_prefixes = set() @@ -98,7 +95,7 @@ class _UniqueVarNameGenerator(UniqueNameGenerator): # {{{ loop kernel object -class _deprecated_KernelState_SCHEDULED(object): # noqa +class _deprecated_KernelState_SCHEDULED: # noqa def __init__(self, f): self.f = f @@ -120,7 +117,7 @@ class KernelState: # noqa # {{{ kernel_state, KernelState compataibility -class _deperecated_kernel_state_class_method(object): # noqa +class _deperecated_kernel_state_class_method: # noqa def __init__(self, f): self.f = f @@ -130,7 +127,7 @@ class _deperecated_kernel_state_class_method(object): # noqa return self.f() -class kernel_state(object): # noqa +class kernel_state: # noqa """Deprecated. Use :class:`loopy.kernel.KernelState` instead. """ @@ -376,7 +373,7 @@ class LoopKernel(ImmutableRecordWithoutPickling): from collections import defaultdict assert not isinstance(iname_to_tags, defaultdict) - for iname, tags in six.iteritems(iname_to_tags): + for iname, tags in iname_to_tags.items(): # don't tolerate empty sets assert tags assert isinstance(tags, frozenset) @@ -479,25 +476,25 @@ class LoopKernel(ImmutableRecordWithoutPickling): @memoize_method def non_iname_variable_names(self): - return (set(six.iterkeys(self.arg_dict)) - | set(six.iterkeys(self.temporary_variables))) + return (set(self.arg_dict.keys()) + | set(self.temporary_variables.keys())) @memoize_method def all_variable_names(self, include_temp_storage=True): return ( - set(six.iterkeys(self.temporary_variables)) - | set(tv.base_storage - for tv in six.itervalues(self.temporary_variables) - if tv.base_storage is not None and include_temp_storage) - | set(six.iterkeys(self.substitutions)) - | set(arg.name for arg in self.args) + set(self.temporary_variables.keys()) + | {tv.base_storage + for tv in self.temporary_variables.values() + if tv.base_storage is not None and include_temp_storage} + | set(self.substitutions.keys()) + | {arg.name for arg in self.args} | set(self.all_inames())) def get_var_name_generator(self): return _UniqueVarNameGenerator(self.all_variable_names()) def get_instruction_id_generator(self, based_on="insn"): - used_ids = set(insn.id for insn in self.instructions) + used_ids = {insn.id for insn in self.instructions} return UniqueNameGenerator(used_ids) @@ -506,7 +503,7 @@ class LoopKernel(ImmutableRecordWithoutPickling): if insns is None: insns = self.instructions - used_ids = set(insn.id for insn in insns) | extra_used_ids + used_ids = {insn.id for insn in insns} | extra_used_ids for id_str in generate_unique_names(based_on): if id_str not in used_ids: @@ -539,7 +536,7 @@ class LoopKernel(ImmutableRecordWithoutPickling): @property @memoize_method def id_to_insn(self): - return dict((insn.id, insn) for insn in self.instructions) + return {insn.id: insn for insn in self.instructions} # }}} @@ -634,10 +631,10 @@ class LoopKernel(ImmutableRecordWithoutPickling): @memoize_method def _get_home_domain_map(self): - return dict( - (iname, i_domain) + return { + iname: i_domain for i_domain, dom in enumerate(self.domains) - for iname in dom.get_var_names(dim_type.set)) + for iname in dom.get_var_names(dim_type.set)} def get_home_domain_index(self, iname): return self._get_home_domain_map()[iname] @@ -813,7 +810,7 @@ class LoopKernel(ImmutableRecordWithoutPickling): @memoize_method def all_referenced_inames(self): result = set() - for inames in six.itervalues(self.all_insn_inames()): + for inames in self.all_insn_inames().values(): result.update(inames) return result @@ -824,8 +821,8 @@ class LoopKernel(ImmutableRecordWithoutPickling): @memoize_method def iname_to_insns(self): - result = dict( - (iname, set()) for iname in self.all_inames()) + result = { + iname: set() for iname in self.all_inames()} for insn in self.instructions: for iname in self.insn_inames(insn): result[iname].add(insn.id) @@ -851,9 +848,9 @@ class LoopKernel(ImmutableRecordWithoutPickling): tag, = tags tag_key_uses[tag.key].append(iname) - multi_use_keys = set( - key for key, user_inames in six.iteritems(tag_key_uses) - if len(user_inames) > 1) + multi_use_keys = { + key for key, user_inames in tag_key_uses.items() + if len(user_inames) > 1} multi_use_inames = set() for iname in cond_inames: @@ -873,13 +870,13 @@ class LoopKernel(ImmutableRecordWithoutPickling): warn("Since version 2018.1, inames can hold multiple tags. Use " "iname_to_tags['iname'] instead. iname_to_tag.get('iname') will be " "removed at version 2019.0.", DeprecationWarning) - for iname, tags in six.iteritems(self.iname_to_tags): + for iname, tags in self.iname_to_tags.items(): if len(tags) > 1: raise LoopyError( - "iname {0} has multiple tags: {1}. " + "iname {} has multiple tags: {}. " "Use iname_to_tags['iname'] instead.".format(iname, tags)) - return dict((k, next(iter(v))) - for k, v in six.iteritems(self.iname_to_tags) if v) + return {k: next(iter(v)) + for k, v in self.iname_to_tags.items() if v} # }}} @@ -929,8 +926,8 @@ class LoopKernel(ImmutableRecordWithoutPickling): result = {} admissible_vars = ( - set(arg.name for arg in self.args) - | set(six.iterkeys(self.temporary_variables))) + {arg.name for arg in self.args} + | set(self.temporary_variables.keys())) for insn in self.instructions: for var_name in insn.read_dependency_names() & admissible_vars: @@ -972,7 +969,7 @@ class LoopKernel(ImmutableRecordWithoutPickling): @memoize_method def get_temporary_to_base_storage_map(self): result = {} - for tv in six.itervalues(self.temporary_variables): + for tv in self.temporary_variables.values(): if tv.base_storage: result[tv.name] = tv.base_storage @@ -983,10 +980,10 @@ class LoopKernel(ImmutableRecordWithoutPickling): written_vars = self.get_written_variables() from loopy.kernel.data import ValueArg - return set( + return { arg.name for arg in self.args - if isinstance(arg, ValueArg) and arg.name not in written_vars) + if isinstance(arg, ValueArg) and arg.name not in written_vars} # }}} @@ -995,7 +992,7 @@ class LoopKernel(ImmutableRecordWithoutPickling): @property @memoize_method def arg_dict(self): - return dict((arg.name, arg) for arg in self.args) + return {arg.name: arg for arg in self.args} @property @memoize_method @@ -1017,13 +1014,13 @@ class LoopKernel(ImmutableRecordWithoutPickling): from loopy.kernel.data import ArrayArg return ( - set( + { arg.name for arg in self.args - if isinstance(arg, ArrayArg)) - | set( + if isinstance(arg, ArrayArg)} + | { tv.name - for tv in six.itervalues(self.temporary_variables) - if tv.address_space == AddressSpace.GLOBAL)) + for tv in self.temporary_variables.values() + if tv.address_space == AddressSpace.GLOBAL}) # }}} @@ -1148,7 +1145,7 @@ class LoopKernel(ImmutableRecordWithoutPickling): forced_sizes = forced_sizes.copy() size_list = [] - sorted_axes = sorted(six.iterkeys(size_dict)) + sorted_axes = sorted(size_dict.keys()) while sorted_axes or forced_sizes: if sorted_axes: @@ -1220,15 +1217,15 @@ class LoopKernel(ImmutableRecordWithoutPickling): @memoize_method def local_var_names(self): from loopy.kernel.data import AddressSpace - return set( + return { tv.name - for tv in six.itervalues(self.temporary_variables) - if tv.address_space == AddressSpace.LOCAL) + for tv in self.temporary_variables.values() + if tv.address_space == AddressSpace.LOCAL} def local_mem_use(self): from loopy.kernel.data import AddressSpace return sum( - tv.nbytes for tv in six.itervalues(self.temporary_variables) + tv.nbytes for tv in self.temporary_variables.values() if tv.address_space == AddressSpace.LOCAL) # }}} @@ -1261,13 +1258,13 @@ class LoopKernel(ImmutableRecordWithoutPickling): "consistent iname nesting order. This is a possible indication " "that the kernel may not schedule successfully, but for now " "it only impacts printing of the kernel.") - embedding = dict((iname, iname) for iname in self.all_inames()) + embedding = {iname: iname for iname in self.all_inames()} return embedding def stringify(self, what=None, with_dependencies=False, use_separators=True, show_labels=True): - all_what = set([ + all_what = { "name", "arguments", "domains", @@ -1277,10 +1274,10 @@ class LoopKernel(ImmutableRecordWithoutPickling): "instructions", "Dependencies", "schedule", - ]) + } - first_letter_to_what = dict( - (w[0], w) for w in all_what) + first_letter_to_what = { + w[0]: w for w in all_what} assert len(first_letter_to_what) == len(all_what) if what is None: @@ -1291,11 +1288,11 @@ class LoopKernel(ImmutableRecordWithoutPickling): if isinstance(what, str): if "," in what: what = what.split(",") - what = set(s.strip() for s in what) + what = {s.strip() for s in what} else: - what = set( + what = { first_letter_to_what[w] - for w in what) + for w in what} if not (what <= all_what): raise LoopyError("invalid 'what' passed: %s" @@ -1340,14 +1337,14 @@ class LoopKernel(ImmutableRecordWithoutPickling): else: tags_str = ", ".join(str(tag) for tag in tags) - line = "%s: %s" % (iname, tags_str) + line = f"{iname}: {tags_str}" lines.append(line) if "variables" in what and kernel.temporary_variables: lines.extend(sep) if show_labels: lines.append("TEMPORARIES:") - for tv in natsorted(six.itervalues(kernel.temporary_variables), + for tv in natsorted(kernel.temporary_variables.values(), key=lambda tv: tv.name): lines.append(str(tv)) @@ -1355,7 +1352,7 @@ class LoopKernel(ImmutableRecordWithoutPickling): lines.extend(sep) if show_labels: lines.append("SUBSTITUTION RULES:") - for rule_name in natsorted(six.iterkeys(kernel.substitutions)): + for rule_name in natsorted(kernel.substitutions.keys()): lines.append(str(kernel.substitutions[rule_name])) if "instructions" in what: @@ -1369,7 +1366,8 @@ class LoopKernel(ImmutableRecordWithoutPickling): dep_lines = [] for insn in kernel.instructions: if insn.depends_on: - dep_lines.append("%s : %s" % (insn.id, ",".join(insn.depends_on))) + dep_lines.append("{} : {}".format( + insn.id, ",".join(insn.depends_on))) if "Dependencies" in what and dep_lines: lines.extend(sep) @@ -1390,11 +1388,7 @@ class LoopKernel(ImmutableRecordWithoutPickling): return "\n".join(lines) def __str__(self): - if six.PY3: - return self.stringify() - else: - # Path of least resistance... - return self.stringify().encode("utf-8") + return self.stringify() def __unicode__(self): return self.stringify() @@ -1459,10 +1453,10 @@ class LoopKernel(ImmutableRecordWithoutPickling): # {{{ pickling def __getstate__(self): - result = dict( - (key, getattr(self, key)) + result = { + key: getattr(self, key) for key in self.__class__.fields - if hasattr(self, key)) + if hasattr(self, key)} result.pop("cache_manager", None) @@ -1493,7 +1487,7 @@ class LoopKernel(ImmutableRecordWithoutPickling): new_fields = set() - for k, v in six.iteritems(attribs): + for k, v in attribs.items(): setattr(self, k, v) new_fields.add(k) diff --git a/loopy/kernel/array.py b/loopy/kernel/array.py index bbaf347ba..6b0248f4f 100644 --- a/loopy/kernel/array.py +++ b/loopy/kernel/array.py @@ -1,6 +1,5 @@ """Implementation tagging of array axes.""" -from __future__ import division, absolute_import __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" @@ -26,10 +25,6 @@ THE SOFTWARE. import re -import six -from six.moves import range, zip -from six import iteritems - from pytools import ImmutableRecord, memoize_method import numpy as np # noqa @@ -322,7 +317,7 @@ def parse_array_dim_tags(dim_tags, n_axes=None, use_increasing_target_axes=False assert n_axes == len(dim_names) dim_tags = [None]*n_axes - for dim_name, val in six.iteritems(dim_tags_dict): + for dim_name, val in dim_tags_dict.items(): try: dim_idx = dim_names.index(dim_name) except ValueError: @@ -388,7 +383,7 @@ def parse_array_dim_tags(dim_tags, n_axes=None, use_increasing_target_axes=False # {{{ check contiguity of nesting levels - for target_axis, ta_nesting_levels in iteritems(nesting_levels): + for target_axis, ta_nesting_levels in nesting_levels.items(): if sorted(ta_nesting_levels) != list( range( min(ta_nesting_levels), @@ -898,7 +893,7 @@ class ArrayBase(ImmutableRecord): if self.dim_names is not None: info_entries.append("shape: (%s)" % ", ".join( - "%s:%s" % (n, i) + f"{n}:{i}" for n, i in zip(self.dim_names, self.shape))) else: info_entries.append("shape: (%s)" @@ -912,7 +907,7 @@ class ArrayBase(ImmutableRecord): if self.offset: info_entries.append("offset: %s" % self.offset) - return "%s: %s" % (self.name, ", ".join(info_entries)) + return "{}: {}".format(self.name, ", ".join(info_entries)) def __str__(self): return self.stringify(include_typename=True) @@ -1106,8 +1101,7 @@ class ArrayBase(ImmutableRecord): offset_for_name=full_name, is_written=False) - for sa in stride_args: - yield sa + yield from stride_args # }}} @@ -1133,13 +1127,12 @@ class ArrayBase(ImmutableRecord): new_stride_arg_axes = stride_arg_axes new_stride_axis = dim_tag.stride - for res in gen_decls(name_suffix, + yield from gen_decls(name_suffix, shape + (new_shape_axis,), strides + (new_stride_axis,), unvec_shape + (new_shape_axis,), unvec_strides + (new_stride_axis,), new_stride_arg_axes, - dtype, user_index + (None,)): - yield res + dtype, user_index + (None,)) elif isinstance(dim_tag, SeparateArrayArrayDimTag): shape_i = array_shape[user_axis] @@ -1149,11 +1142,10 @@ class ArrayBase(ImmutableRecord): self.name, user_axis)) for i in range(shape_i): - for res in gen_decls(name_suffix + "_s%d" % i, + yield from gen_decls(name_suffix + "_s%d" % i, shape, strides, unvec_shape, unvec_strides, stride_arg_axes, dtype, - user_index + (i,)): - yield res + user_index + (i,)) elif isinstance(dim_tag, VectorArrayDimTag): shape_i = array_shape[user_axis] @@ -1162,26 +1154,24 @@ class ArrayBase(ImmutableRecord): "integer axis %d (0-based)" % ( self.name, user_axis)) - for res in gen_decls(name_suffix, + yield from gen_decls(name_suffix, shape, strides, unvec_shape + (shape_i,), # vectors always have stride 1 unvec_strides + (1,), stride_arg_axes, target.vector_dtype(dtype, shape_i), - user_index + (None,)): - yield res + user_index + (None,)) else: raise LoopyError("unsupported array dim implementation tag '%s' " "in array '%s'" % (dim_tag, self.name)) - for res in gen_decls(name_suffix="", + yield from gen_decls(name_suffix="", shape=(), strides=(), unvec_shape=(), unvec_strides=(), stride_arg_axes=(), - dtype=self.dtype, user_index=()): - yield res + dtype=self.dtype, user_index=()) @memoize_method def sep_shape(self): diff --git a/loopy/kernel/creation.py b/loopy/kernel/creation.py index d363eba72..a22fef9e8 100644 --- a/loopy/kernel/creation.py +++ b/loopy/kernel/creation.py @@ -1,6 +1,5 @@ """UI for kernel creation.""" -from __future__ import division, absolute_import, print_function __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" @@ -39,8 +38,7 @@ import islpy as isl from islpy import dim_type from pytools import ProcessLogger -import six -from six.moves import range, zip, intern +from sys import intern import loopy.version import re @@ -987,8 +985,8 @@ def _find_inames_in_set(dom_str): if match is None: raise RuntimeError("invalid syntax for domain '%s'" % dom_str) - result = set(iname.strip() for iname in match.group(1).split(",") - if iname.strip()) + result = {iname.strip() for iname in match.group(1).split(",") + if iname.strip()} return result @@ -997,7 +995,7 @@ EX_QUANT_RE = re.compile(r"\bexists\s+([a-zA-Z0-9])\s*\:") def _find_existentially_quantified_inames(dom_str): - return set(ex_quant.group(1) for ex_quant in EX_QUANT_RE.finditer(dom_str)) + return {ex_quant.group(1) for ex_quant in EX_QUANT_RE.finditer(dom_str)} def parse_domains(domains, defines): @@ -1016,7 +1014,7 @@ def parse_domains(domains, defines): parameters = (_gather_isl_identifiers(dom) - _find_inames_in_set(dom) - _find_existentially_quantified_inames(dom)) - dom = "[%s] -> %s" % (",".join(sorted(parameters)), dom) + dom = "[{}] -> {}".format(",".join(sorted(parameters)), dom) try: dom = isl.BasicSet.read_from_str(isl.DEFAULT_CONTEXT, dom) @@ -1179,7 +1177,7 @@ class ArgumentGuesser: # {{{ find names that are *not* arguments - temp_var_names = set(six.iterkeys(self.temporary_variables)) + temp_var_names = set(self.temporary_variables.keys()) for insn in self.instructions: if isinstance(insn, MultiAssignmentBase): @@ -1273,8 +1271,8 @@ def check_for_multiple_writes_to_loop_bounds(knl): def check_written_variable_names(knl): admissible_vars = ( - set(arg.name for arg in knl.args) - | set(six.iterkeys(knl.temporary_variables))) + {arg.name for arg in knl.args} + | set(knl.temporary_variables.keys())) for insn in knl.instructions: for var_name in insn.assignee_var_names(): @@ -1295,7 +1293,7 @@ class CSEToAssignmentMapper(IdentityMapper): def map_reduction(self, expr, additional_inames): additional_inames = additional_inames | frozenset(expr.inames) - return super(CSEToAssignmentMapper, self).map_reduction( + return super().map_reduction( expr, additional_inames) def map_common_subexpression(self, expr, additional_inames): @@ -1518,7 +1516,7 @@ def determine_shapes_of_temporaries(knl): vars_needing_shape_inference = set() - for tv in six.itervalues(knl.temporary_variables): + for tv in knl.temporary_variables.values(): if tv.shape is lp.auto or tv.base_indices is lp.auto: vars_needing_shape_inference.add(tv.name) @@ -1536,8 +1534,7 @@ def determine_shapes_of_temporaries(knl): if len(var_to_error) > 0: vars_needing_shape_inference = set(var_to_error.keys()) - from six import iteritems - for varname, err in iteritems(var_to_error): + for varname, err in var_to_error.items(): warn_with_kernel(knl, "temp_shape_fallback", "Had to fall back to legacy method of determining " "shape of temporary '%s' because: %s" @@ -1555,7 +1552,7 @@ def determine_shapes_of_temporaries(knl): if len(var_to_error) > 0: # No way around errors: propagate an exception upward. formatted_errors = ( - "\n\n".join("'%s': %s" % (varname, var_to_error[varname]) + "\n\n".join("'{}': {}".format(varname, var_to_error[varname]) for varname in sorted(var_to_error.keys()))) raise LoopyError("got the following exception(s) trying to find the " @@ -1568,7 +1565,7 @@ def determine_shapes_of_temporaries(knl): new_temp_vars = {} - for tv in six.itervalues(knl.temporary_variables): + for tv in knl.temporary_variables.values(): if tv.base_indices is lp.auto: tv = tv.copy(base_indices=var_to_base_indices[tv.name]) if tv.shape is lp.auto: @@ -1597,7 +1594,7 @@ def expand_defines_in_shapes(kernel, defines): processed_args.append(arg) processed_temp_vars = {} - for tv in six.itervalues(kernel.temporary_variables): + for tv in kernel.temporary_variables.values(): processed_temp_vars[tv.name] = tv.map_exprs(expr_map) return kernel.copy( @@ -1759,13 +1756,13 @@ def apply_single_writer_depencency_heuristic(kernel, warn_if_used=True): writer_map = kernel.writer_map() - arg_names = set(arg.name for arg in kernel.args) + arg_names = {arg.name for arg in kernel.args} - var_names = arg_names | set(six.iterkeys(kernel.temporary_variables)) + var_names = arg_names | set(kernel.temporary_variables.keys()) - dep_map = dict( - (insn.id, insn.read_dependency_names() & var_names) - for insn in expanded_kernel.instructions) + dep_map = { + insn.id: insn.read_dependency_names() & var_names + for insn in expanded_kernel.instructions} new_insns = [] for insn in kernel.instructions: @@ -1789,7 +1786,7 @@ def apply_single_writer_depencency_heuristic(kernel, warn_if_used=True): if len(var_writers) == 1: auto_deps.update( var_writers - - set([insn.id])) + - {insn.id}) # }}} @@ -1974,9 +1971,9 @@ def make_kernel(domains, instructions, kernel_data=["..."], **kwargs): from loopy.version import LANGUAGE_VERSION_SYMBOLS - version_to_symbol = dict( - (getattr(loopy.version, lvs), lvs) - for lvs in LANGUAGE_VERSION_SYMBOLS) + version_to_symbol = { + getattr(loopy.version, lvs): lvs + for lvs in LANGUAGE_VERSION_SYMBOLS} lang_version = kwargs.pop("lang_version", None) if lang_version is None: @@ -2016,7 +2013,7 @@ def make_kernel(domains, instructions, kernel_data=["..."], **kwargs): lang_version = FALLBACK_LANGUAGE_VERSION if lang_version not in version_to_symbol: - raise LoopyError("Language version '%s' is not known." % (lang_version,)) + raise LoopyError(f"Language version '{lang_version}' is not known.") # }}} diff --git a/loopy/kernel/data.py b/loopy/kernel/data.py index 8b4c359bd..43770ffb6 100644 --- a/loopy/kernel/data.py +++ b/loopy/kernel/data.py @@ -1,6 +1,5 @@ """Data used by the kernel object.""" -from __future__ import division __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" @@ -25,7 +24,7 @@ THE SOFTWARE. """ -from six.moves import intern +from sys import intern import numpy as np # noqa from pytools import ImmutableRecord from loopy.kernel.array import ArrayBase @@ -68,7 +67,7 @@ __doc__ = """ """ -class auto(object): # noqa +class auto: # noqa """A generic placeholder object for something that should be automatically determined. See, for example, the *shape* or *strides* argument of :class:`ArrayArg`. @@ -89,7 +88,7 @@ def filter_iname_tags_by_type(tags, tag_type, max_num=None, min_num=None): :arg min_num: the minimum number of tags expected to be found. """ - result = set(tag for tag in tags if isinstance(tag, tag_type)) + result = {tag for tag in tags if isinstance(tag, tag_type)} def strify_tag_type(): if isinstance(tag_type, tuple): @@ -99,12 +98,12 @@ def filter_iname_tags_by_type(tags, tag_type, max_num=None, min_num=None): if max_num is not None: if len(result) > max_num: - raise LoopyError("cannot have more than {0} tags " - "of type(s): {1}".format(max_num, strify_tag_type())) + raise LoopyError("cannot have more than {} tags " + "of type(s): {}".format(max_num, strify_tag_type())) if min_num is not None: if len(result) < min_num: - raise LoopyError("must have more than {0} tags " - "of type(s): {1}".format(max_num, strify_tag_type())) + raise LoopyError("must have more than {} tags " + "of type(s): {}".format(max_num, strify_tag_type())) return result @@ -266,7 +265,7 @@ def parse_tag(tag): # {{{ memory address space -class AddressSpace(object): +class AddressSpace: """Storage location of a variable. .. attribute:: PRIVATE @@ -293,7 +292,7 @@ class AddressSpace(object): raise ValueError("unexpected value of AddressSpace") -class _deprecated_temp_var_scope_class_method(object): # noqa +class _deprecated_temp_var_scope_class_method: # noqa def __init__(self, f): self.f = f @@ -303,7 +302,7 @@ class _deprecated_temp_var_scope_class_method(object): # noqa return self.f() -class temp_var_scope(object): # noqa +class temp_var_scope: # noqa """Deprecated. Use :class:`loopy.AddressSpace` instead. """ @@ -387,7 +386,7 @@ class ArrayArg(ArrayBase, KernelArgument): raise TypeError("'address_space' must be specified") kwargs["is_output_only"] = kwargs.pop("is_output_only", False) - super(ArrayArg, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) min_target_axes = 0 max_target_axes = 1 @@ -411,7 +410,7 @@ class ArrayArg(ArrayBase, KernelArgument): """Custom hash computation function for use with :class:`pytools.persistent_dict.PersistentDict`. """ - super(ArrayArg, self).update_persistent_hash(key_hash, key_builder) + super().update_persistent_hash(key_hash, key_builder) key_builder.rec(key_hash, self.address_space) key_builder.rec(key_hash, self.is_output_only) @@ -471,7 +470,7 @@ class ValueArg(KernelArgument): else: type_str = str(self.dtype) - return "%s: ValueArg, type: %s" % (self.name, type_str) + return f"{self.name}: ValueArg, type: {type_str}" def __repr__(self): return "<%s>" % self.__str__() @@ -680,7 +679,7 @@ class TemporaryVariable(ArrayBase): if address_space is not None: kwargs["address_space"] = address_space - return super(TemporaryVariable, self).copy(**kwargs) + return super().copy(**kwargs) @property def nbytes(self): @@ -692,7 +691,7 @@ class TemporaryVariable(ArrayBase): return product(si for si in shape)*self.dtype.itemsize def decl_info(self, target, index_dtype): - return super(TemporaryVariable, self).decl_info( + return super().decl_info( target, is_written=True, index_dtype=index_dtype, shape_override=self.storage_shape) @@ -717,7 +716,7 @@ class TemporaryVariable(ArrayBase): def __eq__(self, other): return ( - super(TemporaryVariable, self).__eq__(other) + super().__eq__(other) and self.storage_shape == other.storage_shape and self.base_indices == other.base_indices and self.address_space == other.address_space @@ -735,7 +734,7 @@ class TemporaryVariable(ArrayBase): :class:`pytools.persistent_dict.PersistentDict`. """ - super(TemporaryVariable, self).update_persistent_hash(key_hash, key_builder) + super().update_persistent_hash(key_hash, key_builder) self.update_persistent_hash_for_shape(key_hash, key_builder, self.storage_shape) key_builder.rec(key_hash, self.base_indices) @@ -783,7 +782,7 @@ class SubstitutionRule(ImmutableRecord): name=name, arguments=arguments, expression=expression) def __str__(self): - return "%s(%s) := %s" % ( + return "{}({}) := {}".format( self.name, ", ".join(self.arguments), self.expression) def update_persistent_hash(self, key_hash, key_builder): @@ -821,7 +820,7 @@ class CallMangleInfo(ImmutableRecord): def __init__(self, target_name, result_dtypes, arg_dtypes): assert isinstance(result_dtypes, tuple) - super(CallMangleInfo, self).__init__( + super().__init__( target_name=target_name, result_dtypes=result_dtypes, arg_dtypes=arg_dtypes) diff --git a/loopy/kernel/instruction.py b/loopy/kernel/instruction.py index 4f50a447c..791ea89a6 100644 --- a/loopy/kernel/instruction.py +++ b/loopy/kernel/instruction.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - __copyright__ = "Copyright (C) 2016 Andreas Kloeckner" __license__ = """ @@ -22,7 +20,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -from six.moves import intern +from sys import intern from pytools import ImmutableRecord, memoize_method from loopy.diagnostic import LoopyError from loopy.tools import Optional @@ -164,7 +162,7 @@ class InstructionBase(ImmutableRecord): pymbolic_fields = set("") # Names of fields that are sets of pymbolic expressions. Needed for key building - pymbolic_set_fields = set(["predicates"]) + pymbolic_set_fields = {"predicates"} def __init__(self, id, depends_on, depends_on_is_final, groups, conflicts_with_groups, @@ -398,7 +396,7 @@ class InstructionBase(ImmutableRecord): # }}} def __setstate__(self, val): - super(InstructionBase, self).__setstate__(val) + super().__setstate__(val) from loopy.tools import intern_frozenset_of_ids @@ -488,7 +486,7 @@ class MemoryOrdering: # noqa # {{{ memory_ordering, MemoryOrdering compatibility -class _deprecated_memory_ordering_class_method(object): # noqa +class _deprecated_memory_ordering_class_method: # noqa def __init__(self, f): self.f = f @@ -498,7 +496,7 @@ class _deprecated_memory_ordering_class_method(object): # noqa return self.f() -class memory_ordering(object): # noqa +class memory_ordering: # noqa """Deprecated. Use :class:`MemoryOrdering` instead. """ @@ -565,7 +563,7 @@ class MemoryScope: # noqa # {{{ memory_scope, MemoryScope compatiability -class _deprecated_memory_scope_class_method(object): # noqa +class _deprecated_memory_scope_class_method: # noqa def __init__(self, f): self.f = f @@ -575,7 +573,7 @@ class _deprecated_memory_scope_class_method(object): # noqa return self.f() -class memory_scope(object): # noqa +class memory_scope: # noqa """Deprecated. Use :class:`MemoryScope` instead. """ @@ -608,7 +606,7 @@ class memory_scope(object): # noqa # }}} -class VarAtomicity(object): +class VarAtomicity: """A base class for the description of how atomic access to :attr:`var_name` shall proceed. @@ -653,13 +651,13 @@ class OrderedAtomic(VarAtomicity): :class:`pytools.persistent_dict.PersistentDict`. """ - super(OrderedAtomic, self).update_persistent_hash(key_hash, key_builder) + super().update_persistent_hash(key_hash, key_builder) key_builder.rec(key_hash, str(self.__class__.__name__)) key_builder.rec(key_hash, self.ordering) key_builder.rec(key_hash, self.scope) def __eq__(self, other): - return (super(OrderedAtomic, self).__eq__(other) + return (super().__eq__(other) and self.ordering == other.ordering and self.scope == other.scope) @@ -668,7 +666,7 @@ class OrderedAtomic(VarAtomicity): raise NotImplementedError def __str__(self): - return "%s[%s]%s/%s" % ( + return "{}[{}]{}/{}".format( self.op_name, self.var_name, MemoryOrdering.to_string(self.ordering), @@ -726,14 +724,14 @@ class AtomicLoad(OrderedAtomic): class MultiAssignmentBase(InstructionBase): """An assignment instruction with an expression as a right-hand side.""" - fields = InstructionBase.fields | set(["expression"]) - pymbolic_fields = InstructionBase.pymbolic_fields | set(["expression"]) + fields = InstructionBase.fields | {"expression"} + pymbolic_fields = InstructionBase.pymbolic_fields | {"expression"} @memoize_method def read_dependency_names(self): from loopy.symbolic import get_dependencies result = ( - super(MultiAssignmentBase, self).read_dependency_names() + super().read_dependency_names() | get_dependencies(self.expression)) for subscript_deps in self.assignee_subscript_deps(): @@ -815,7 +813,7 @@ class Assignment(MultiAssignmentBase): fields = MultiAssignmentBase.fields | \ set("assignee temp_var_type atomicity".split()) - pymbolic_fields = MultiAssignmentBase.pymbolic_fields | set(["assignee"]) + pymbolic_fields = MultiAssignmentBase.pymbolic_fields | {"assignee"} def __init__(self, assignee, expression, @@ -831,7 +829,7 @@ class Assignment(MultiAssignmentBase): temp_var_type=Optional(), atomicity=(), priority=0, predicates=frozenset()): - super(Assignment, self).__init__( + super().__init__( id=id, depends_on=depends_on, depends_on_is_final=depends_on_is_final, @@ -883,7 +881,7 @@ class Assignment(MultiAssignmentBase): # }}} def __str__(self): - result = "%s <- %s" % (self.assignee, self.expression) + result = f"{self.assignee} <- {self.expression}" if self.id is not None: result = "%s: " % self.id + result @@ -915,7 +913,7 @@ class ExpressionInstruction(Assignment): warn("ExpressionInstruction is deprecated. Use Assignment instead", DeprecationWarning, stacklevel=2) - super(ExpressionInstruction, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) # }}} @@ -946,7 +944,7 @@ class CallInstruction(MultiAssignmentBase): fields = MultiAssignmentBase.fields | \ set("assignees temp_var_types".split()) - pymbolic_fields = MultiAssignmentBase.pymbolic_fields | set(["assignees"]) + pymbolic_fields = MultiAssignmentBase.pymbolic_fields | {"assignees"} def __init__(self, assignees, expression, @@ -962,7 +960,7 @@ class CallInstruction(MultiAssignmentBase): temp_var_types=None, priority=0, predicates=frozenset()): - super(CallInstruction, self).__init__( + super().__init__( id=id, depends_on=depends_on, depends_on_is_final=depends_on_is_final, @@ -1032,7 +1030,7 @@ class CallInstruction(MultiAssignmentBase): # }}} def __str__(self): - result = "%s: %s <- %s" % (self.id, + result = "{}: {} <- {}".format(self.id, ", ".join(str(a) for a in self.assignees), self.expression) @@ -1195,7 +1193,7 @@ class CInstruction(InstructionBase): def read_dependency_names(self): result = ( - super(CInstruction, self).read_dependency_names() + super().read_dependency_names() | frozenset(self.read_variables)) from loopy.symbolic import get_dependencies @@ -1233,10 +1231,10 @@ class CInstruction(InstructionBase): # }}} def __str__(self): - first_line = "%s: %s <- CODE(%s|%s)" % (self.id, + first_line = "{}: {} <- CODE({}|{})".format(self.id, ", ".join(str(a) for a in self.assignees), ", ".join(str(x) for x in self.read_variables), - ", ".join("%s=%s" % (name, expr) + ", ".join(f"{name}={expr}" for name, expr in self.iname_exprs)) options = self.get_str_options() @@ -1293,7 +1291,7 @@ class NoOpInstruction(_DataObliviousInstruction): within_inames_is_final=None, within_inames=None, priority=None, predicates=None, tags=None): - super(NoOpInstruction, self).__init__( + super().__init__( id=id, depends_on=depends_on, depends_on_is_final=depends_on_is_final, @@ -1343,8 +1341,8 @@ class BarrierInstruction(_DataObliviousInstruction): ... lbarrier {mem_kind=global} """ - fields = _DataObliviousInstruction.fields | set(["synchronization_kind", - "mem_kind"]) + fields = _DataObliviousInstruction.fields | {"synchronization_kind", + "mem_kind"} def __init__(self, id, depends_on=None, depends_on_is_final=None, groups=None, conflicts_with_groups=None, @@ -1357,7 +1355,7 @@ class BarrierInstruction(_DataObliviousInstruction): if predicates: raise LoopyError("conditional barriers are not supported") - super(BarrierInstruction, self).__init__( + super().__init__( id=id, depends_on=depends_on, depends_on_is_final=depends_on_is_final, @@ -1375,12 +1373,13 @@ class BarrierInstruction(_DataObliviousInstruction): self.mem_kind = mem_kind def __str__(self): - first_line = "%s: ... %sbarrier" % (self.id, self.synchronization_kind[0]) + first_line = \ + "{}: ... {}barrier".format(self.id, self.synchronization_kind[0]) options = self.get_str_options() if self.synchronization_kind == "local": # add the memory kind - options += ["mem_kind={}".format(self.mem_kind)] + options += [f"mem_kind={self.mem_kind}"] if options: first_line += " {%s}" % (": ".join(options)) diff --git a/loopy/kernel/tools.py b/loopy/kernel/tools.py index eb4feb854..f8d908a5b 100644 --- a/loopy/kernel/tools.py +++ b/loopy/kernel/tools.py @@ -1,7 +1,5 @@ -# coding=utf-8 """Operations on the kernel object.""" -from __future__ import division, absolute_import, print_function __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" @@ -27,8 +25,7 @@ THE SOFTWARE. import sys -import six -from six.moves import intern +from sys import intern import numpy as np import islpy as isl @@ -109,7 +106,7 @@ def get_arguments_with_incomplete_dtype(kernel): def add_and_infer_dtypes(kernel, dtype_dict, expect_completion=False): processed_dtype_dict = {} - for k, v in six.iteritems(dtype_dict): + for k, v in dtype_dict.items(): for subkey in k.split(","): subkey = subkey.strip() if subkey: @@ -288,7 +285,7 @@ def find_all_insn_inames(kernel): logger.debug("%s: find_all_insn_inames: done" % kernel.name) - for v in six.itervalues(insn_id_to_inames): + for v in insn_id_to_inames.values(): assert isinstance(v, frozenset) return insn_id_to_inames @@ -482,7 +479,7 @@ def get_dot_dependency_graph(kernel, iname_cluster=True, use_insn_id=False): for insn in kernel.instructions: if isinstance(insn, MultiAssignmentBase): lhs = ", ".join(str(assignee) for assignee in insn.assignees) - op = "%s <- %s" % (lhs, insn.expression) + op = f"{lhs} <- {insn.expression}" if len(op) > 200: op = op[:200] + "..." @@ -533,7 +530,7 @@ def get_dot_dependency_graph(kernel, iname_cluster=True, use_insn_id=False): for insn_1 in dep_graph: for insn_2 in dep_graph.get(insn_1, set()): - lines.append("%s -> %s" % (insn_2, insn_1)) + lines.append(f"{insn_2} -> {insn_1}") if iname_cluster: from loopy.schedule import ( @@ -553,7 +550,7 @@ def get_dot_dependency_graph(kernel, iname_cluster=True, use_insn_id=False): else: raise LoopyError("schedule item not unterstood: %r" % sched_item) - return "digraph %s {\n%s\n}" % ( + return "digraph {} {{\n{}\n}}".format( kernel.name, "\n".join(lines) ) @@ -675,9 +672,9 @@ def get_auto_axis_iname_ranking_by_stride(kernel, insn): # {{{ figure out automatic-axis inames from loopy.kernel.data import AutoLocalIndexTagBase - auto_axis_inames = set( + auto_axis_inames = { iname for iname in kernel.insn_inames(insn) - if kernel.iname_tags_of_type(iname, AutoLocalIndexTagBase)) + if kernel.iname_tags_of_type(iname, AutoLocalIndexTagBase)} # }}} @@ -716,7 +713,7 @@ def get_auto_axis_iname_ranking_by_stride(kernel, insn): if stride is None: continue coeffs = CoefficientCollector()(iexpr_i) - for var, coeff in six.iteritems(coeffs): + for var, coeff in coeffs.items(): if (isinstance(var, Variable) and var.name in auto_axis_inames): # excludes '1', i.e. the constant @@ -728,7 +725,7 @@ def get_auto_axis_iname_ranking_by_stride(kernel, insn): # }}} from pymbolic import evaluate - for iname, stride_expr in six.iteritems(iname_to_stride_expr): + for iname, stride_expr in iname_to_stride_expr.items(): stride = evaluate(stride_expr, approximate_arg_values) aggregate_strides[iname] = aggregate_strides.get(iname, 0) + stride @@ -936,7 +933,7 @@ def assign_automatic_axes(kernel, axis=0, local_size=None): # {{{ array modifier -class ArrayChanger(object): +class ArrayChanger: def __init__(self, kernel, array_name): self.kernel = kernel self.array_name = array_name @@ -1018,8 +1015,8 @@ def guess_var_shape(kernel, var_name): % (var_name, ", ".join( str(i) for i in armap.bad_subscripts))) - n_axes_in_subscripts = set( - len(sub.index_tuple) for sub in armap.bad_subscripts) + n_axes_in_subscripts = { + len(sub.index_tuple) for sub in armap.bad_subscripts} if len(n_axes_in_subscripts) != 1: raise RuntimeError("subscripts of '%s' with differing " @@ -1070,7 +1067,7 @@ def guess_var_shape(kernel, var_name): # {{{ loop nest tracker -class SetTrie(object): +class SetTrie: """ Similar to a trie, but uses an unordered sequence as the key. """ @@ -1085,9 +1082,8 @@ class SetTrie(object): def descend(self, on_found=lambda prefix: None, prefix=frozenset()): on_found(prefix) - from six import iteritems for prefix, child in sorted( - iteritems(self.children), + self.children.items(), key=lambda it: sorted(it[0])): child.descend(on_found, prefix=prefix) @@ -1099,9 +1095,7 @@ class SetTrie(object): if len(key) == 0: return - from six import iteritems - - for child_key, child in iteritems(self.children): + for child_key, child in self.children.items(): common = child_key & key if common: break @@ -1160,16 +1154,16 @@ def get_visual_iname_order_embedding(kernel): iname_trie = SetTrie() for insn in kernel.instructions: - within_inames = set( + within_inames = { iname for iname in insn.within_inames - if iname not in ilp_inames) + if iname not in ilp_inames} iname_trie.add_or_update(within_inames) embedding = {} def update_embedding(inames): embedding.update( - dict((iname, (len(embedding), iname)) for iname in inames)) + {iname: (len(embedding), iname) for iname in inames}) iname_trie.descend(update_embedding) @@ -1270,8 +1264,8 @@ def draw_dependencies_as_unicode_arrows( def make_extender(): result = n_columns[0] * [" "] - for col, (_, pointed_at_insn_id) in six.iteritems(columns_in_use): - result[col] = do_flag_downward(u"│", pointed_at_insn_id) + for col, (_, pointed_at_insn_id) in columns_in_use.items(): + result[col] = do_flag_downward("│", pointed_at_insn_id) return result @@ -1303,28 +1297,28 @@ def draw_dependencies_as_unicode_arrows( # }}} - for col, (starts, pointed_at_insn_id) in list(six.iteritems(columns_in_use)): + for col, (starts, pointed_at_insn_id) in list(columns_in_use.items()): if insn.id == pointed_at_insn_id: if starts: # will continue downward - row[col] = do_flag_downward(u">", pointed_at_insn_id) + row[col] = do_flag_downward(">", pointed_at_insn_id) else: # stops here # placeholder, pending deletion columns_in_use[col] = None - row[col] = do_flag_downward(u"↳", pointed_at_insn_id) + row[col] = do_flag_downward("↳", pointed_at_insn_id) elif insn.id in starts: starts.remove(insn.id) if starts or pointed_at_insn_id not in processed_ids: # will continue downward - row[col] = do_flag_downward(u"├", pointed_at_insn_id) + row[col] = do_flag_downward("├", pointed_at_insn_id) else: # stops here - row[col] = u"└" + row[col] = "└" # placeholder, pending deletion columns_in_use[col] = None @@ -1334,7 +1328,7 @@ def draw_dependencies_as_unicode_arrows( if dep_key not in dep_to_column and rdeps: col = dep_to_column[dep_key] = find_free_column() columns_in_use[col] = (rdeps, insn.id) - row[col] = u"↱" + row[col] = "↱" # }}} @@ -1350,13 +1344,13 @@ def draw_dependencies_as_unicode_arrows( # we're currently handling it. columns_in_use[col] = (set(), dep) - row[col] = do_flag_downward(u"┌", dep) + row[col] = do_flag_downward("┌", dep) # }}} # {{{ delete columns_in_use entry for end-of-life columns - for col, value in list(six.iteritems(columns_in_use)): + for col, value in list(columns_in_use.items()): if value is None: del columns_in_use[col] @@ -1388,7 +1382,7 @@ def draw_dependencies_as_unicode_arrows( .replace(fore.RED, "") .replace(style.RESET_ALL, "")) - return s[:length] + u"…" + return s[:length] + "…" def conform_to_uniform_length(s): len_s = len_without_color_escapes(s) @@ -1507,9 +1501,9 @@ def stringify_instruction_list(kernel): trailing = [] elif isinstance(insn, lp.CInstruction): lhs = ", ".join(str(a) for a in insn.assignees) - rhs = "CODE(%s|%s)" % ( + rhs = "CODE({}|{})".format( ", ".join(str(x) for x in insn.read_variables), - ", ".join("%s=%s" % (name, expr) + ", ".join(f"{name}={expr}" for name, expr in insn.iname_exprs)) trailing = insn.code.split("\n") @@ -1550,7 +1544,7 @@ def stringify_instruction_list(kernel): options.append("mem_kind=%s" % insn.mem_kind) if lhs: - core = "%s = %s" % ( + core = "{} = {}".format( Fore.CYAN+lhs+Style.RESET_ALL, Fore.MAGENTA+rhs+Style.RESET_ALL, ) @@ -1676,16 +1670,16 @@ def find_most_recent_global_barrier(kernel, insn_id): if len(insn.depends_on) == 0: return None - global_barrier_to_ordinal = dict( - (b, i) for i, b in enumerate(global_barrier_order)) + global_barrier_to_ordinal = { + b: i for i, b in enumerate(global_barrier_order)} def get_barrier_ordinal(barrier_id): return (global_barrier_to_ordinal[barrier_id] if barrier_id is not None else -1) - direct_barrier_dependencies = set( - dep for dep in insn.depends_on if _is_global_barrier(kernel, dep)) + direct_barrier_dependencies = { + dep for dep in insn.depends_on if _is_global_barrier(kernel, dep)} if len(direct_barrier_dependencies) > 0: return max(direct_barrier_dependencies, key=get_barrier_ordinal) @@ -1755,7 +1749,7 @@ def get_subkernel_to_insn_id_map(kernel): # {{{ find aliasing equivalence classes -class DisjointSets(object): +class DisjointSets: """ .. automethod:: __getitem__ .. automethod:: find_leader_or_create_group @@ -1777,7 +1771,7 @@ class DisjointSets(object): try: leader = self.element_to_leader[item] except KeyError: - return set([item]) + return {item} else: return self.leader_to_group[leader] @@ -1788,7 +1782,7 @@ class DisjointSets(object): pass self.element_to_leader[el] = el - self.leader_to_group[el] = set([el]) + self.leader_to_group[el] = {el} return el def union(self, a, b): @@ -1827,7 +1821,7 @@ class DisjointSets(object): def find_aliasing_equivalence_classes(kernel): return DisjointSets().union_many( (tv.base_storage, tv.name) - for tv in six.itervalues(kernel.temporary_variables) + for tv in kernel.temporary_variables.values() if tv.base_storage is not None) # }}} diff --git a/loopy/library/function.py b/loopy/library/function.py index 9d557ac9f..99af08169 100644 --- a/loopy/library/function.py +++ b/loopy/library/function.py @@ -1,5 +1,3 @@ -from __future__ import division - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ diff --git a/loopy/library/random123.py b/loopy/library/random123.py index b8633114d..7f24dd3a0 100644 --- a/loopy/library/random123.py +++ b/loopy/library/random123.py @@ -1,6 +1,5 @@ """Library integration with Random123.""" -from __future__ import division, absolute_import __copyright__ = "Copyright (C) 2016 Andreas Kloeckner" @@ -62,12 +61,12 @@ RNG_VARIANTS = [ _threefry_base_info.copy(width=4, bits=64), ] -FUNC_NAMES_TO_RNG = dict( - (v.full_name + suffix, v) +FUNC_NAMES_TO_RNG = { + v.full_name + suffix: v for v in RNG_VARIANTS for suffix in [ "", "_f32", "_f64", - ]) + ]} # }}} diff --git a/loopy/library/reduction.py b/loopy/library/reduction.py index 7e1b7af5a..6ca763442 100644 --- a/loopy/library/reduction.py +++ b/loopy/library/reduction.py @@ -1,5 +1,3 @@ -from __future__ import division - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -47,7 +45,7 @@ __doc__ = """ """ -class ReductionOperation(object): +class ReductionOperation: """Subclasses of this type have to be hashable, picklable, and equality-comparable. """ @@ -132,7 +130,7 @@ class ScalarReductionOperation(ReductionOperation): result = type(self).__name__.replace("ReductionOperation", "").lower() if self.forced_result_type is not None: - result = "%s<%s>" % (result, str(self.forced_result_type)) + result = "{}<{}>".format(result, str(self.forced_result_type)) return result @@ -255,7 +253,7 @@ class _SegmentedScalarReductionOperation(ReductionOperation): return 2 def prefix(self, scalar_dtype, segment_flag_dtype): - return "loopy_segmented_%s_%s_%s" % (self.which, + return "loopy_segmented_{}_{}_{}".format(self.which, scalar_dtype.numpy_dtype.type.__name__, segment_flag_dtype.numpy_dtype.type.__name__) @@ -335,7 +333,7 @@ class _ArgExtremumReductionOperation(ReductionOperation): raise NotImplementedError def prefix(self, scalar_dtype, index_dtype): - return "loopy_arg%s_%s_%s" % (self.which, + return "loopy_arg{}_{}_{}".format(self.which, scalar_dtype.numpy_dtype.type.__name__, index_dtype.numpy_dtype.type.__name__) diff --git a/loopy/loop.py b/loopy/loop.py index 459246382..7f5744b48 100644 --- a/loopy/loop.py +++ b/loopy/loop.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -24,7 +22,6 @@ THE SOFTWARE. import islpy as isl -import six def potential_loop_nest_map(kernel): @@ -65,7 +62,7 @@ def fuse_loop_domains(kernel): new_domains = None - for inner_iname, outer_inames in six.iteritems(lnm): + for inner_iname, outer_inames in lnm.items(): for outer_iname in outer_inames: # {{{ check if it's safe to fuse diff --git a/loopy/match.py b/loopy/match.py index 3c047e463..625d98d4d 100644 --- a/loopy/match.py +++ b/loopy/match.py @@ -1,7 +1,6 @@ """Matching functionality for instruction ids and subsitution rule invocations stacks.""" -from __future__ import division, absolute_import __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" @@ -25,7 +24,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -from six.moves import range, intern +from sys import intern NoneType = type(None) @@ -113,7 +112,7 @@ _PREC_NOT = 30 # {{{ match expression -class MatchExpressionBase(object): +class MatchExpressionBase: def __call__(self, kernel, matchable): raise NotImplementedError @@ -159,7 +158,7 @@ class MultiChildMatchExpressionBase(MatchExpressionBase): return "(%s)" % (joiner.join(str(ch) for ch in self.children)) def __repr__(self): - return "%s(%s)" % ( + return "{}({})".format( type(self).__name__, ", ".join(repr(ch) for ch in self.children)) @@ -196,7 +195,7 @@ class Not(MatchExpressionBase): return "(not %s)" % str(self.child) def __repr__(self): - return "%s(%r)" % (type(self).__name__, self.child) + return "{}({!r})".format(type(self).__name__, self.child) def update_persistent_hash(self, key_hash, key_builder): key_builder.rec(key_hash, "not_match_expr") @@ -223,7 +222,7 @@ class GlobMatchExpressionBase(MatchExpressionBase): return descr.lower() + ":" + self.glob def __repr__(self): - return "%s(%r)" % (type(self).__name__, self. glob) + return "{}({!r})".format(type(self).__name__, self. glob) def update_persistent_hash(self, key_hash, key_builder): key_builder.rec(key_hash, type(self).__name__) @@ -374,7 +373,7 @@ def parse_match(expr): # {{{ stack match objects -class StackMatchComponent(object): +class StackMatchComponent: def __ne__(self, other): return not self.__eq__(other) @@ -443,7 +442,7 @@ class StackWildcardMatchComponent(StackMatchComponent): # {{{ stack matcher -class RuleInvocationMatchable(object): +class RuleInvocationMatchable: def __init__(self, id, tags): self.id = id self.tags = tags @@ -458,7 +457,7 @@ class RuleInvocationMatchable(object): raise TypeError("inames: query may not be applied to rule invocations") -class StackMatch(object): +class StackMatch: def __init__(self, root_component): self.root_component = root_component diff --git a/loopy/options.py b/loopy/options.py index 8f6292696..2dc8f22cd 100644 --- a/loopy/options.py +++ b/loopy/options.py @@ -1,5 +1,3 @@ -from __future__ import division, with_statement - __copyright__ = "Copyright (C) 2013 Andreas Kloeckner" __license__ = """ @@ -23,7 +21,6 @@ THE SOFTWARE. """ -import six from pytools import ImmutableRecord import re @@ -31,7 +28,7 @@ import re ALLOW_TERMINAL_COLORS = True -class _ColoramaStub(object): +class _ColoramaStub: def __getattribute__(self, name): return "" @@ -39,7 +36,7 @@ class _ColoramaStub(object): def _apply_legacy_map(lmap, kwargs): result = {} - for name, val in six.iteritems(kwargs): + for name, val in kwargs.items(): try: lmap_value = lmap[name] except KeyError: diff --git a/loopy/preprocess.py b/loopy/preprocess.py index a231b31ee..12f1cb469 100644 --- a/loopy/preprocess.py +++ b/loopy/preprocess.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -23,7 +21,6 @@ THE SOFTWARE. """ -import six from loopy.diagnostic import ( LoopyError, WriteRaceConditionWarning, warn_with_kernel, LoopyAdvisory) @@ -59,7 +56,7 @@ def prepare_for_caching(kernel): new_args.append(arg) new_temporary_variables = {} - for name, temp in six.iteritems(kernel.temporary_variables): + for name, temp in kernel.temporary_variables.items(): dtype = temp.dtype if dtype is not None and dtype is not lp.auto and dtype.target is not tgt: temp = temp.copy(dtype=dtype.with_target(tgt), target=tgt) @@ -117,7 +114,7 @@ def check_reduction_iname_uniqueness(kernel): for insn in kernel.instructions: insn.with_transformed_expressions(cb_mapper) - for iname, count in six.iteritems(iname_to_reduction_count): + for iname, count in iname_to_reduction_count.items(): nonsimul_count = iname_to_nonsimultaneous_reduction_count.get(iname, 0) if nonsimul_count and count > 1: @@ -136,18 +133,18 @@ def check_reduction_iname_uniqueness(kernel): # {{{ decide temporary address space def _get_compute_inames_tagged(kernel, insn, tag_base): - return set(iname for iname in kernel.insn_inames(insn.id) - if kernel.iname_tags_of_type(iname, tag_base)) + return {iname for iname in kernel.insn_inames(insn.id) + if kernel.iname_tags_of_type(iname, tag_base)} def _get_assignee_inames_tagged(kernel, insn, tag_base, tv_names): - return set(iname + return {iname for aname, adeps in zip( insn.assignee_var_names(), insn.assignee_subscript_deps()) for iname in adeps & kernel.all_inames() if aname in tv_names - if kernel.iname_tags_of_type(iname, tag_base)) + if kernel.iname_tags_of_type(iname, tag_base)} def find_temporary_address_space(kernel): @@ -164,7 +161,7 @@ def find_temporary_address_space(kernel): kernel_var_names = kernel.all_variable_names(include_temp_storage=False) - for temp_var in six.itervalues(kernel.temporary_variables): + for temp_var in kernel.temporary_variables.values(): if temp_var.base_storage is not None: # no nesting allowed if temp_var.base_storage in kernel_var_names: @@ -175,7 +172,7 @@ def find_temporary_address_space(kernel): base_storage_to_aliases.setdefault( temp_var.base_storage, []).append(temp_var.name) - for temp_var in six.itervalues(kernel.temporary_variables): + for temp_var in kernel.temporary_variables.values(): # Only fill out for variables that do not yet know if they're # local. (I.e. those generated by implicit temporary generation.) @@ -227,7 +224,7 @@ def find_temporary_address_space(kernel): if (apin != cpin and bool(apin)): warn_with_kernel( kernel, - "write_race_%s(%s)" % (aspace_descr, insn_id), + f"write_race_{aspace_descr}({insn_id})", "instruction '%s' looks invalid: " "it assigns to indices based on %s IDs, but " "its temporary '%s' cannot be made %s because " @@ -446,7 +443,7 @@ def _try_infer_scan_candidate_from_expr( if len(expr.inames) != 1: raise ValueError( - "Multiple inames in reduction: '%s'" % (", ".join(expr.inames),)) + "Multiple inames in reduction: '{}'".format(", ".join(expr.inames))) scan_iname, = expr.inames @@ -495,9 +492,9 @@ def _try_infer_sweep_iname(domain, scan_iname, candidate_inames): sweep_iname_candidate = None for constr in constrs: - candidate_vars = set([ + candidate_vars = { var for var in constr.get_var_dict() - if var in candidate_inames]) + if var in candidate_inames} # Irrelevant constraint - skip if scan_iname not in candidate_vars: @@ -714,13 +711,12 @@ def _hackily_ensure_multi_assignment_return_values_are_scoped_private(kernel): new_or_updated_instructions = {} new_temporaries = {} - dep_map = dict( - (insn.id, insn.depends_on) for insn in kernel.instructions) + dep_map = { + insn.id: insn.depends_on for insn in kernel.instructions} - inverse_dep_map = dict((insn.id, set()) for insn in kernel.instructions) + inverse_dep_map = {insn.id: set() for insn in kernel.instructions} - import six - for insn_id, deps in six.iteritems(dep_map): + for insn_id, deps in dep_map.items(): for dep in deps: inverse_dep_map[dep].add(insn_id) @@ -944,7 +940,7 @@ def realize_reduction(kernel, insn_id_filter=None, unknown_types_ok=True, # items that are not "plain" tuples here. if not isinstance(inner_expr, tuple): get_args_insn_id = insn_id_gen( - "%s_%s_get" % (insn.id, "_".join(expr.inames))) + "{}_{}_get".format(insn.id, "_".join(expr.inames))) inner_expr = expand_inner_reduction( id=get_args_insn_id, @@ -1029,7 +1025,7 @@ def realize_reduction(kernel, insn_id_filter=None, unknown_types_ok=True, acc_vars = tuple(var(n) for n in acc_var_names) init_id = insn_id_gen( - "%s_%s_init" % (insn.id, "_".join(expr.inames))) + "{}_{}_init".format(insn.id, "_".join(expr.inames))) init_insn = make_assignment( id=init_id, @@ -1043,20 +1039,20 @@ def realize_reduction(kernel, insn_id_filter=None, unknown_types_ok=True, generated_insns.append(init_insn) update_id = insn_id_gen( - based_on="%s_%s_update" % (insn.id, "_".join(expr.inames))) + based_on="{}_{}_update".format(insn.id, "_".join(expr.inames))) update_insn_iname_deps = temp_kernel.insn_inames(insn) | set(expr.inames) if insn.within_inames_is_final: update_insn_iname_deps = insn.within_inames | set(expr.inames) - reduction_insn_depends_on = set([init_id]) + reduction_insn_depends_on = {init_id} # In the case of a multi-argument reduction, we need a name for each of # the arguments in order to pass them to the binary op - so we expand # items that are not "plain" tuples here. if nresults > 1 and not isinstance(expr.expr, tuple): get_args_insn_id = insn_id_gen( - "%s_%s_get" % (insn.id, "_".join(expr.inames))) + "{}_{}_get".format(insn.id, "_".join(expr.inames))) reduction_expr = expand_inner_reduction( id=get_args_insn_id, @@ -1105,7 +1101,7 @@ def realize_reduction(kernel, insn_id_filter=None, unknown_types_ok=True, static_max_of_pw_aff( kernel.get_iname_bounds(iname).size, constants_only=True)) - assert isinstance(size, six.integer_types) + assert isinstance(size, int) return size def _make_slab_set(iname, size): @@ -1176,7 +1172,7 @@ def realize_reduction(kernel, insn_id_filter=None, unknown_types_ok=True, base_iname_deps = outer_insn_inames - frozenset(expr.inames) neutral = expr.operation.neutral_element(*arg_dtypes) - init_id = insn_id_gen("%s_%s_init" % (insn.id, red_iname)) + init_id = insn_id_gen(f"{insn.id}_{red_iname}_init") init_insn = make_assignment( id=init_id, assignees=tuple( @@ -1190,7 +1186,7 @@ def realize_reduction(kernel, insn_id_filter=None, unknown_types_ok=True, ) generated_insns.append(init_insn) - init_neutral_id = insn_id_gen("%s_%s_init_neutral" % (insn.id, red_iname)) + init_neutral_id = insn_id_gen(f"{insn.id}_{red_iname}_init_neutral") init_neutral_insn = make_assignment( id=init_neutral_id, assignees=tuple(var(nvn) for nvn in neutral_var_names), @@ -1202,14 +1198,14 @@ def realize_reduction(kernel, insn_id_filter=None, unknown_types_ok=True, ) generated_insns.append(init_neutral_insn) - transfer_depends_on = set([init_neutral_id, init_id]) + transfer_depends_on = {init_neutral_id, init_id} # In the case of a multi-argument reduction, we need a name for each of # the arguments in order to pass them to the binary op - so we expand # items that are not "plain" tuples here. if nresults > 1 and not isinstance(expr.expr, tuple): get_args_insn_id = insn_id_gen( - "%s_%s_get" % (insn.id, red_iname)) + f"{insn.id}_{red_iname}_get") reduction_expr = expand_inner_reduction( id=get_args_insn_id, @@ -1227,7 +1223,7 @@ def realize_reduction(kernel, insn_id_filter=None, unknown_types_ok=True, else: reduction_expr = expr.expr - transfer_id = insn_id_gen("%s_%s_transfer" % (insn.id, red_iname)) + transfer_id = insn_id_gen(f"{insn.id}_{red_iname}_transfer") transfer_insn = make_assignment( id=transfer_id, assignees=tuple( @@ -1390,7 +1386,7 @@ def realize_reduction(kernel, insn_id_filter=None, unknown_types_ok=True, acc_vars = tuple(var(n) for n in acc_var_names) init_id = insn_id_gen( - "%s_%s_init" % (insn.id, "_".join(expr.inames))) + "{}_{}_init".format(insn.id, "_".join(expr.inames))) init_insn_depends_on = frozenset() @@ -1412,18 +1408,18 @@ def realize_reduction(kernel, insn_id_filter=None, unknown_types_ok=True, generated_insns.append(init_insn) - update_insn_depends_on = set([init_insn.id]) | insn.depends_on + update_insn_depends_on = {init_insn.id} | insn.depends_on updated_inner_exprs = ( preprocess_scan_arguments(insn, expr.expr, nresults, scan_iname, track_iname, update_insn_depends_on)) update_id = insn_id_gen( - based_on="%s_%s_update" % (insn.id, "_".join(expr.inames))) + based_on="{}_{}_update".format(insn.id, "_".join(expr.inames))) - update_insn_iname_deps = temp_kernel.insn_inames(insn) | set([track_iname]) + update_insn_iname_deps = temp_kernel.insn_inames(insn) | {track_iname} if insn.within_inames_is_final: - update_insn_iname_deps = insn.within_inames | set([track_iname]) + update_insn_iname_deps = insn.within_inames | {track_iname} scan_insn = make_assignment( id=update_id, @@ -1530,7 +1526,7 @@ def realize_reduction(kernel, insn_id_filter=None, unknown_types_ok=True, if global_barrier is not None: init_insn_depends_on |= frozenset([global_barrier]) - init_id = insn_id_gen("%s_%s_init" % (insn.id, scan_iname)) + init_id = insn_id_gen(f"{insn.id}_{scan_iname}_init") init_insn = make_assignment( id=init_id, assignees=tuple( @@ -1544,7 +1540,7 @@ def realize_reduction(kernel, insn_id_filter=None, unknown_types_ok=True, ) generated_insns.append(init_insn) - transfer_insn_depends_on = set([init_insn.id]) | insn.depends_on + transfer_insn_depends_on = {init_insn.id} | insn.depends_on updated_inner_exprs = ( preprocess_scan_arguments(insn, expr.expr, nresults, @@ -1555,7 +1551,7 @@ def realize_reduction(kernel, insn_id_filter=None, unknown_types_ok=True, from loopy.symbolic import pw_aff_to_expr sweep_min_value_expr = pw_aff_to_expr(sweep_min_value) - transfer_id = insn_id_gen("%s_%s_transfer" % (insn.id, scan_iname)) + transfer_id = insn_id_gen(f"{insn.id}_{scan_iname}_transfer") transfer_insn = make_assignment( id=transfer_id, assignees=tuple( @@ -1944,7 +1940,7 @@ def realize_ilp(kernel): filter_iname_tags_by_type) privatizing_inames = frozenset( - iname for iname, tags in six.iteritems(kernel.iname_to_tags) + iname for iname, tags in kernel.iname_to_tags.items() if filter_iname_tags_by_type(tags, (IlpBaseTag, VectorizeTag)) ) @@ -1968,25 +1964,25 @@ def check_atomic_loads(kernel): # find atomic variables atomicity_candidates = ( - set(v.name for v in six.itervalues(kernel.temporary_variables) - if isinstance(v.dtype, AtomicType)) + {v.name for v in kernel.temporary_variables.values() + if isinstance(v.dtype, AtomicType)} | - set(v.name for v in kernel.args + {v.name for v in kernel.args if isinstance(v, ArrayBase) - and isinstance(v.dtype, AtomicType))) + and isinstance(v.dtype, AtomicType)}) new_insns = [] for insn in kernel.instructions: if isinstance(insn, Assignment): # look for atomic variables - atomic_accesses = set(a.var_name for a in insn.atomicity) + atomic_accesses = {a.var_name for a in insn.atomicity} accessed_atomic_vars = (insn.dependency_names() & atomicity_candidates)\ - - set([insn.assignee_var_names()[0]]) + - {insn.assignee_var_names()[0]} if not accessed_atomic_vars <= atomic_accesses: #if we're missing some missed = accessed_atomic_vars - atomic_accesses for x in missed: - if set([x]) & atomicity_candidates: + if {x} & atomicity_candidates: insn = insn.copy( atomicity=insn.atomicity + (AtomicLoad(x),)) @@ -2035,7 +2031,7 @@ def preprocess_kernel(kernel, device=None): # {{{ check that there are no l.auto-tagged inames from loopy.kernel.data import AutoLocalIndexTagBase - for iname, tags in six.iteritems(kernel.iname_to_tags): + for iname, tags in kernel.iname_to_tags.items(): if (filter_iname_tags_by_type(tags, AutoLocalIndexTagBase) and iname in kernel.all_inames()): raise LoopyError("kernel with automatically-assigned " diff --git a/loopy/schedule/__init__.py b/loopy/schedule/__init__.py index 4cc4fcd23..1db2903a3 100644 --- a/loopy/schedule/__init__.py +++ b/loopy/schedule/__init__.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -23,7 +21,6 @@ THE SOFTWARE. """ -import six from pytools import ImmutableRecord import sys import islpy as isl @@ -223,17 +220,17 @@ def find_loop_nest_with_map(kernel): from loopy.kernel.data import ConcurrentTag, IlpBaseTag - all_nonpar_inames = set( + all_nonpar_inames = { iname for iname in kernel.all_inames() if not kernel.iname_tags_of_type(iname, - (ConcurrentTag, IlpBaseTag))) + (ConcurrentTag, IlpBaseTag))} iname_to_insns = kernel.iname_to_insns() for iname in all_nonpar_inames: - result[iname] = set(other_iname + result[iname] = {other_iname for insn in iname_to_insns[iname] - for other_iname in kernel.insn_inames(insn) & all_nonpar_inames) + for other_iname in kernel.insn_inames(insn) & all_nonpar_inames} return result @@ -367,8 +364,7 @@ def gen_dependencies_except(kernel, insn_id, except_insn_ids): yield dep_id - for sub_dep_id in gen_dependencies_except(kernel, dep_id, except_insn_ids): - yield sub_dep_id + yield from gen_dependencies_except(kernel, dep_id, except_insn_ids) def get_priority_tiers(wanted, priorities): @@ -410,8 +406,7 @@ def get_priority_tiers(wanted, priorities): wanted = wanted - candidates # Yield recursively - for tier in get_priority_tiers(wanted, priorities): - yield tier + yield from get_priority_tiers(wanted, priorities) def sched_item_to_insn_id(sched_item): @@ -442,7 +437,7 @@ def format_insn(kernel, insn_id): from loopy.kernel.instruction import ( MultiAssignmentBase, NoOpInstruction, BarrierInstruction) if isinstance(insn, MultiAssignmentBase): - return "%s%s%s = %s%s%s {id=%s}" % ( + return "{}{}{} = {}{}{} {{id={}}}".format( Fore.CYAN, ", ".join(str(a) for a in insn.assignees), Style.RESET_ALL, Fore.MAGENTA, str(insn.expression), Style.RESET_ALL, format_insn_id(kernel, insn_id)) @@ -451,16 +446,16 @@ def format_insn(kernel, insn_id): if insn.synchronization_kind == "local": mem_kind = "{mem_kind=%s}" % insn.mem_kind - return "[%s] %s... %sbarrier%s%s" % ( + return "[{}] {}... {}barrier{}{}".format( format_insn_id(kernel, insn_id), Fore.MAGENTA, insn.synchronization_kind[0], mem_kind, Style.RESET_ALL) elif isinstance(insn, NoOpInstruction): - return "[%s] %s... nop%s" % ( + return "[{}] {}... nop{}".format( format_insn_id(kernel, insn_id), Fore.MAGENTA, Style.RESET_ALL) else: - return "[%s] %s%s%s" % ( + return "[{}] {}{}{}".format( format_insn_id(kernel, insn_id), Fore.CYAN, str(insn), Style.RESET_ALL) @@ -479,7 +474,7 @@ def dump_schedule(kernel, schedule): lines.append(indent + "end %s" % sched_item.iname) elif isinstance(sched_item, CallKernel): lines.append(indent + - "CALL KERNEL %s(extra_args=%s, extra_inames=%s)" % ( + "CALL KERNEL {}(extra_args={}, extra_inames={})".format( sched_item.kernel_name, sched_item.extra_args, sched_item.extra_inames)) @@ -852,8 +847,8 @@ def generate_loop_schedules_internal( print(dump_schedule(sched_state.kernel, sched_state.preschedule)) print(75*"=") print("LOOP NEST MAP (inner: outer):") - for iname, val in six.iteritems(sched_state.loop_nest_around_map): - print("%s : %s" % (iname, ", ".join(val))) + for iname, val in sched_state.loop_nest_around_map.items(): + print("{} : {}".format(iname, ", ".join(val))) print(75*"=") if debug.debug_length == len(debug.longest_rejected_schedule): @@ -868,28 +863,26 @@ def generate_loop_schedules_internal( if isinstance(next_preschedule_item, CallKernel): assert sched_state.within_subkernel is False - for result in generate_loop_schedules_internal( + yield from generate_loop_schedules_internal( sched_state.copy( schedule=sched_state.schedule + (next_preschedule_item,), preschedule=sched_state.preschedule[1:], within_subkernel=True, may_schedule_global_barriers=False, enclosing_subkernel_inames=sched_state.active_inames), - debug=debug): - yield result + debug=debug) if isinstance(next_preschedule_item, ReturnFromKernel): assert sched_state.within_subkernel is True # Make sure all subkernel inames have finished. if sched_state.active_inames == sched_state.enclosing_subkernel_inames: - for result in generate_loop_schedules_internal( + yield from generate_loop_schedules_internal( sched_state.copy( schedule=sched_state.schedule + (next_preschedule_item,), preschedule=sched_state.preschedule[1:], within_subkernel=False, may_schedule_global_barriers=True), - debug=debug): - yield result + debug=debug) # }}} @@ -902,12 +895,11 @@ def generate_loop_schedules_internal( if ( isinstance(next_preschedule_item, Barrier) and next_preschedule_item.originating_insn_id is None): - for result in generate_loop_schedules_internal( + yield from generate_loop_schedules_internal( sched_state.copy( schedule=sched_state.schedule + (next_preschedule_item,), preschedule=sched_state.preschedule[1:]), - debug=debug): - yield result + debug=debug) # }}} @@ -1216,7 +1208,7 @@ def generate_loop_schedules_internal( print("reachable insns:", ",".join(reachable_insn_ids)) print("active groups (with insn counts):", ",".join( "%s: %d" % (grp, c) - for grp, c in six.iteritems(sched_state.active_group_counts))) + for grp, c in sched_state.active_group_counts.items())) print(75*"-") if needed_inames: @@ -1298,7 +1290,7 @@ def generate_loop_schedules_internal( usefulness = None # highest insn priority enabled by iname - hypothetically_active_loops = active_inames_set | set([iname]) + hypothetically_active_loops = active_inames_set | {iname} for insn_id in reachable_insn_ids: insn = kernel.id_to_insn[insn_id] @@ -1326,7 +1318,7 @@ def generate_loop_schedules_internal( loop_priority_set = set().union(*[set(prio) for prio in sched_state.kernel.loop_priority]) - useful_loops_set = set(six.iterkeys(iname_to_usefulness)) + useful_loops_set = set(iname_to_usefulness.keys()) useful_and_desired = useful_loops_set & loop_priority_set if useful_and_desired: @@ -1414,7 +1406,7 @@ def generate_loop_schedules_internal( if debug_mode: print(75*"=") - inp = six.moves.input("Hit Enter for next schedule, " + inp = input("Hit Enter for next schedule, " "or enter a number to examine schedules of a " "different length:") if inp: @@ -1495,7 +1487,7 @@ class DependencyRecord(ImmutableRecord): var_kind=var_kind) -class DependencyTracker(object): +class DependencyTracker: """ A utility to help track dependencies between originating from a set of sources (as defined by :meth:`add_source`. For each target, @@ -1603,9 +1595,8 @@ class DependencyTracker(object): ("w", "any", self.base_access_map), ]: - for dep in self.get_conflicting_accesses( - target, tgt_dir, src_dir, src_base_var_to_accessor_map): - yield dep + yield from self.get_conflicting_accesses( + target, tgt_dir, src_dir, src_base_var_to_accessor_map) def get_conflicting_accesses(self, target, tgt_dir, src_dir, src_base_var_to_accessor_map): @@ -1619,11 +1610,11 @@ class DependencyTracker(object): dir_to_getter = {"w": get_written_names, "any": get_accessed_names} def filter_var_set_for_base_storage(var_name_set, base_storage_name): - return set( + return { name for name in var_name_set if (self.temp_to_base_storage.get(name, name) - == base_storage_name)) + == base_storage_name)} tgt_accessed_vars = dir_to_getter[tgt_dir](target) tgt_accessed_vars_base = self.map_to_base_storage(tgt_accessed_vars) @@ -1753,8 +1744,8 @@ def _insn_ids_reaching_end(schedule, kind, reverse): sched_item.synchronization_kind, kind): insn_ids_alive_at_scope[-1].clear() else: - insn_ids_alive_at_scope[-1] |= set( - insn_id for insn_id in sched_item_to_insn_id(sched_item)) + insn_ids_alive_at_scope[-1] |= { + insn_id for insn_id in sched_item_to_insn_id(sched_item)} assert len(insn_ids_alive_at_scope) == 1 return insn_ids_alive_at_scope[-1] @@ -1775,7 +1766,7 @@ def append_barrier_or_raise_error(schedule, dep, verify_only): dep.variable, dep.var_kind)) else: - comment = "for %s (%s)" % ( + comment = "for {} ({})".format( dep.variable, dep.dep_descr.format( tgt=dep.target.id, src=dep.source.id)) schedule.append(Barrier( @@ -1951,8 +1942,7 @@ def generate_loop_schedules(kernel, debug_args={}): """ with MinRecursionLimitForScheduling(kernel): - for sched in generate_loop_schedules_inner(kernel, debug_args=debug_args): - yield sched + yield from generate_loop_schedules_inner(kernel, debug_args=debug_args) def generate_loop_schedules_inner(kernel, debug_args={}): @@ -1970,30 +1960,30 @@ def generate_loop_schedules_inner(kernel, debug_args={}): preschedule = kernel.schedule if kernel.state == KernelState.LINEARIZED else () - prescheduled_inames = set( + prescheduled_inames = { insn.iname for insn in preschedule - if isinstance(insn, EnterLoop)) + if isinstance(insn, EnterLoop)} - prescheduled_insn_ids = set( + prescheduled_insn_ids = { insn_id for item in preschedule - for insn_id in sched_item_to_insn_id(item)) + for insn_id in sched_item_to_insn_id(item)} from loopy.kernel.data import (IlpBaseTag, ConcurrentTag, VectorizeTag, filter_iname_tags_by_type) - ilp_inames = set( + ilp_inames = { iname - for iname, tags in six.iteritems(kernel.iname_to_tags) - if filter_iname_tags_by_type(tags, IlpBaseTag)) - vec_inames = set( + for iname, tags in kernel.iname_to_tags.items() + if filter_iname_tags_by_type(tags, IlpBaseTag)} + vec_inames = { iname - for iname, tags in six.iteritems(kernel.iname_to_tags) - if filter_iname_tags_by_type(tags, VectorizeTag)) - parallel_inames = set( + for iname, tags in kernel.iname_to_tags.items() + if filter_iname_tags_by_type(tags, VectorizeTag)} + parallel_inames = { iname - for iname, tags in six.iteritems(kernel.iname_to_tags) - if filter_iname_tags_by_type(tags, ConcurrentTag)) + for iname, tags in kernel.iname_to_tags.items() + if filter_iname_tags_by_type(tags, ConcurrentTag)} loop_nest_with_map = find_loop_nest_with_map(kernel) loop_nest_around_map = find_loop_nest_around_map(kernel) @@ -2018,7 +2008,7 @@ def generate_loop_schedules_inner(kernel, debug_args={}): schedule=(), - unscheduled_insn_ids=set(insn.id for insn in kernel.instructions), + unscheduled_insn_ids={insn.id for insn in kernel.instructions}, scheduled_insn_ids=frozenset(), within_subkernel=kernel.state != KernelState.LINEARIZED, may_schedule_global_barriers=True, @@ -2049,7 +2039,7 @@ def generate_loop_schedules_inner(kernel, debug_args={}): print(" debug_args=dict(interactive=False)") print("to generate_loop_schedules().") print(75*"-") - six.moves.input("Enter:") + input("Enter:") print() print() diff --git a/loopy/schedule/device_mapping.py b/loopy/schedule/device_mapping.py index d45c1ecbd..089d4e600 100644 --- a/loopy/schedule/device_mapping.py +++ b/loopy/schedule/device_mapping.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - __copyright__ = "Copyright (C) 2016 Matt Wala" __license__ = """ diff --git a/loopy/schedule/tools.py b/loopy/schedule/tools.py index e0129fd98..afcdfb07b 100644 --- a/loopy/schedule/tools.py +++ b/loopy/schedule/tools.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - __copyright__ = "Copyright (C) 2016 Matt Wala" __license__ = """ @@ -88,7 +86,7 @@ def add_extra_args_to_schedule(kernel): temporaries_read_in_subkernel(kernel, subkernel) | temporaries_written_in_subkernel(kernel, subkernel)) - more_args = set(tv + more_args = {tv for tv in used_temporaries if kernel.temporary_variables[tv].address_space @@ -96,7 +94,7 @@ def add_extra_args_to_schedule(kernel): and kernel.temporary_variables[tv].initializer is None and - tv not in sched_item.extra_args) + tv not in sched_item.extra_args} new_schedule.append(sched_item.copy( extra_args=sched_item.extra_args + sorted(more_args))) diff --git a/loopy/statistics.py b/loopy/statistics.py index 46904aeea..eda750120 100755 --- a/loopy/statistics.py +++ b/loopy/statistics.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - __copyright__ = "Copyright (C) 2015 James Stevens" __license__ = """ @@ -22,8 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six - import loopy as lp from islpy import dim_type import islpy as isl @@ -62,7 +58,7 @@ __doc__ = """ # {{{ GuardedPwQPolynomial -class GuardedPwQPolynomial(object): +class GuardedPwQPolynomial: def __init__(self, pwqpolynomial, valid_domain): self.pwqpolynomial = pwqpolynomial self.valid_domain = valid_domain @@ -122,7 +118,7 @@ class GuardedPwQPolynomial(object): # {{{ ToCountMap -class ToCountMap(object): +class ToCountMap: """Maps any type of key to an arithmetic type. .. automethod:: filter_by @@ -142,26 +138,26 @@ class ToCountMap(object): def __add__(self, other): result = self.count_map.copy() - for k, v in six.iteritems(other.count_map): + for k, v in other.count_map.items(): result[k] = self.count_map.get(k, 0) + v return ToCountMap(result, self.val_type) def __radd__(self, other): if other != 0: raise ValueError("ToCountMap: Attempted to add ToCountMap " - "to {0} {1}. ToCountMap may only be added to " + "to {} {}. ToCountMap may only be added to " "0 and other ToCountMap objects." .format(type(other), other)) return self def __mul__(self, other): if isinstance(other, GuardedPwQPolynomial): - return ToCountMap(dict( - (index, self.count_map[index]*other) - for index in self.keys())) + return ToCountMap({ + index: self.count_map[index]*other + for index in self.keys()}) else: raise ValueError("ToCountMap: Attempted to multiply " - "ToCountMap by {0} {1}." + "ToCountMap by {} {}." .format(type(other), other)) __rmul__ = __mul__ @@ -201,9 +197,9 @@ class ToCountMap(object): return ToCountMap(dict(self.count_map), self.val_type) def with_set_attributes(self, **kwargs): - return ToCountMap(dict( - (key.copy(**kwargs), val) - for key, val in six.iteritems(self.count_map)), + return ToCountMap({ + key.copy(**kwargs): val + for key, val in self.count_map.items()}, self.val_type) def filter_by(self, **kwargs): @@ -455,7 +451,7 @@ class ToCountMap(object): def stringify_stats_mapping(m): result = "" for key in sorted(m.keys(), key=lambda k: str(k)): - result += ("%s : %s\n" % (key, m[key])) + result += ("{} : {}\n".format(key, m[key])) return result @@ -534,7 +530,7 @@ class Op(Record): def __repr__(self): # Record.__repr__ overridden for consistent ordering and conciseness - return "Op(%s, %s, %s)" % (self.dtype, self.name, self.count_granularity) + return f"Op({self.dtype}, {self.name}, {self.count_granularity})" # }}} @@ -631,13 +627,13 @@ class MemAccess(Record): def __repr__(self): # Record.__repr__ overridden for consistent ordering and conciseness - return "MemAccess(%s, %s, %s, %s, %s, %s, %s, %s)" % ( + return "MemAccess({}, {}, {}, {}, {}, {}, {}, {})".format( self.mtype, self.dtype, None if self.lid_strides is None else dict( - sorted(six.iteritems(self.lid_strides))), + sorted(self.lid_strides.items())), None if self.gid_strides is None else dict( - sorted(six.iteritems(self.gid_strides))), + sorted(self.gid_strides.items())), self.direction, self.variable, self.variable_tag, @@ -898,7 +894,7 @@ def _get_lid_and_gid_strides(knl, array, index): else: dim_tags = array.dim_tags - for tag, iname in six.iteritems(tag_to_iname_dict): + for tag, iname in tag_to_iname_dict.items(): total_iname_stride = 0 # find total stride of this iname for each axis for idx, axis_tag in zip(index, dim_tags): @@ -977,8 +973,8 @@ class LocalMemAccessCounter(MemAccessCounter): sub_map[MemAccess( mtype="local", dtype=dtype, - lid_strides=dict(sorted(six.iteritems(lid_strides))), - gid_strides=dict(sorted(six.iteritems(gid_strides))), + lid_strides=dict(sorted(lid_strides.items())), + gid_strides=dict(sorted(gid_strides.items())), variable=name, count_granularity=CountGranularity.SUBGROUP)] = 1 @@ -1052,8 +1048,8 @@ class GlobalMemAccessCounter(MemAccessCounter): return ToCountMap({MemAccess( mtype="global", dtype=self.type_inf(expr), - lid_strides=dict(sorted(six.iteritems(lid_strides))), - gid_strides=dict(sorted(six.iteritems(gid_strides))), + lid_strides=dict(sorted(lid_strides.items())), + gid_strides=dict(sorted(gid_strides.items())), variable=name, variable_tag=var_tag, count_granularity=count_granularity @@ -1078,7 +1074,7 @@ class AccessFootprintGatherer(CombineMapper): def merge_dicts(a, b): result = a.copy() - for var_name, footprint in six.iteritems(b): + for var_name, footprint in b.items(): if var_name in result: result[var_name] = result[var_name] | footprint else: @@ -1437,7 +1433,7 @@ def get_op_map(knl, numpy_types=True, count_redundant_work=False, for insn in knl.instructions: if isinstance(insn, (CallInstruction, CInstruction, Assignment)): ops = op_counter(insn.assignee) + op_counter(insn.expression) - for key, val in six.iteritems(ops.count_map): + for key, val in ops.count_map.items(): op_map = ( op_map + ToCountMap({key: val}) @@ -1453,13 +1449,13 @@ def get_op_map(knl, numpy_types=True, count_redundant_work=False, if numpy_types: return ToCountMap( - init_dict=dict( - (Op( + init_dict={ + Op( dtype=op.dtype.numpy_dtype, name=op.name, - count_granularity=op.count_granularity), - ct) - for op, ct in six.iteritems(op_map.count_map)), + count_granularity=op.count_granularity): + ct + for op, ct in op_map.count_map.items()}, val_type=op_map.val_type ) else: @@ -1635,7 +1631,7 @@ def get_mem_access_map(knl, numpy_types=True, count_redundant_work=False, + access_counter_l(insn.assignee) ).with_set_attributes(direction="store") - for key, val in six.iteritems(access_expr.count_map): + for key, val in access_expr.count_map.items(): access_map = ( access_map @@ -1644,7 +1640,7 @@ def get_mem_access_map(knl, numpy_types=True, count_redundant_work=False, count_redundant_work, key.count_granularity)) - for key, val in six.iteritems(access_assignee.count_map): + for key, val in access_assignee.count_map.items(): access_map = ( access_map @@ -1661,8 +1657,8 @@ def get_mem_access_map(knl, numpy_types=True, count_redundant_work=False, if numpy_types: return ToCountMap( - init_dict=dict( - (MemAccess( + init_dict={ + MemAccess( mtype=mem_access.mtype, dtype=mem_access.dtype.numpy_dtype, lid_strides=mem_access.lid_strides, @@ -1670,9 +1666,9 @@ def get_mem_access_map(knl, numpy_types=True, count_redundant_work=False, direction=mem_access.direction, variable=mem_access.variable, variable_tag=mem_access.variable_tag, - count_granularity=mem_access.count_granularity), - ct) - for mem_access, ct in six.iteritems(access_map.count_map)), + count_granularity=mem_access.count_granularity): + ct + for mem_access, ct in access_map.count_map.items()}, val_type=access_map.val_type ) else: @@ -1820,10 +1816,10 @@ def gather_access_footprints(kernel, ignore_uncountable=False): result = {} - for vname, footprint in six.iteritems(write_footprints): + for vname, footprint in write_footprints.items(): result[(vname, "write")] = footprint - for vname, footprint in six.iteritems(read_footprints): + for vname, footprint in read_footprints.items(): result[(vname, "read")] = footprint return result diff --git a/loopy/symbolic.py b/loopy/symbolic.py index 19ff83431..7f89ff125 100644 --- a/loopy/symbolic.py +++ b/loopy/symbolic.py @@ -1,6 +1,5 @@ """Pymbolic mappers for loopy.""" -from __future__ import division, absolute_import __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" @@ -25,8 +24,8 @@ THE SOFTWARE. """ -import six -from six.moves import range, zip, reduce, intern +from functools import reduce +from sys import intern from pytools import memoize, memoize_method, ImmutableRecord import pytools.lex @@ -94,7 +93,7 @@ __doc__ = """ # {{{ mappers with support for loopy-specific primitives -class IdentityMapperMixin(object): +class IdentityMapperMixin: def map_literal(self, expr, *args, **kwargs): return expr @@ -232,13 +231,13 @@ class StringifyMapper(StringifyMapperBase): def map_reduction(self, expr, prec): from pymbolic.mapper.stringifier import PREC_NONE - return "%sreduce(%s, [%s], %s)" % ( + return "{}reduce({}, [{}], {})".format( "simul_" if expr.allow_simultaneous else "", expr.operation, ", ".join(expr.inames), self.rec(expr.expr, PREC_NONE)) def map_tagged_variable(self, expr, prec): - return "%s$%s" % (expr.name, expr.tag) + return f"{expr.name}${expr.tag}" def map_linear_subscript(self, expr, enclosing_prec): from pymbolic.mapper.stringifier import PREC_CALL, PREC_NONE @@ -249,7 +248,7 @@ class StringifyMapper(StringifyMapperBase): enclosing_prec, PREC_CALL) def map_loopy_function_identifier(self, expr, enclosing_prec): - return "%s<%s>" % ( + return "{}<{}>".format( type(expr).__name__, ", ".join(str(a) for a in expr.__getinitargs__())) @@ -258,7 +257,8 @@ class StringifyMapper(StringifyMapperBase): def map_type_cast(self, expr, enclosing_prec): from pymbolic.mapper.stringifier import PREC_NONE - return "cast(%s, %s)" % (repr(expr.type), self.rec(expr.child, PREC_NONE)) + return "cast({}, {})".format( + repr(expr.type), self.rec(expr.child, PREC_NONE)) class EqualityPreservingStringifyMapper(StringifyMapperBase): @@ -273,7 +273,7 @@ class EqualityPreservingStringifyMapper(StringifyMapperBase): """ def __init__(self): - super(EqualityPreservingStringifyMapper, self).__init__() + super().__init__() def map_constant(self, expr, enclosing_prec): if isinstance(expr, np.generic): @@ -281,7 +281,7 @@ class EqualityPreservingStringifyMapper(StringifyMapperBase): # FIXME: This syntax cannot currently be parsed. - return "%s(%s)" % (type(expr).__name__, repr(expr)) + return "{}({})".format(type(expr).__name__, repr(expr)) else: result = repr(expr) @@ -336,10 +336,10 @@ class DependencyMapper(DependencyMapperBase): def map_reduction(self, expr, *args, **kwargs): deps = self.rec(expr.expr, *args, **kwargs) - return deps - set(p.Variable(iname) for iname in expr.inames) + return deps - {p.Variable(iname) for iname in expr.inames} def map_tagged_variable(self, expr, *args, **kwargs): - return set([expr]) + return {expr} def map_loopy_function_identifier(self, expr, *args, **kwargs): return set() @@ -361,7 +361,7 @@ class SubstitutionRuleExpander(IdentityMapper): if expr.name in self.rules: return self.map_substitution(expr.name, self.rules[expr.name], ()) else: - return super(SubstitutionRuleExpander, self).map_variable(expr) + return super().map_variable(expr) def map_call(self, expr): if expr.function.name in self.rules: @@ -370,7 +370,7 @@ class SubstitutionRuleExpander(IdentityMapper): self.rules[expr.function.name], expr.parameters) else: - return super(SubstitutionRuleExpander, self).map_call(expr) + return super().map_call(expr) def map_substitution(self, name, rule, arguments): if len(rule.arguments) != len(arguments): @@ -489,7 +489,7 @@ class TypedCSE(LoopyExpressionBase, p.CommonSubexpression): """ def __init__(self, child, prefix=None, dtype=None): - super(TypedCSE, self).__init__(child, prefix) + super().__init__(child, prefix) self.dtype = dtype def __getinitargs__(self): @@ -505,7 +505,7 @@ class TypeAnnotation(LoopyExpressionBase): """ def __init__(self, type, child): - super(TypeAnnotation, self).__init__() + super().__init__() self.type = type self.child = child @@ -525,7 +525,7 @@ class TypeCast(LoopyExpressionBase): """ def __init__(self, type, child): - super(TypeCast, self).__init__() + super().__init__() from loopy.types import to_loopy_type, NumpyType type = to_loopy_type(type) @@ -565,7 +565,7 @@ class TaggedVariable(LoopyExpressionBase, p.Variable): init_arg_names = ("name", "tag") def __init__(self, name, tag): - super(TaggedVariable, self).__init__(name) + super().__init__(name) self.tag = tag def __getinitargs__(self): @@ -793,11 +793,11 @@ def rename_subst_rules_in_instructions(insns, renames): for insn in insns] -class SubstitutionRuleMappingContext(object): +class SubstitutionRuleMappingContext: def _get_subst_rule_key(self, args, body): - subst_dict = dict( - (arg, RuleArgument(i)) - for i, arg in enumerate(args)) + subst_dict = { + arg: RuleArgument(i) + for i, arg in enumerate(args)} from pymbolic.mapper.substitutor import make_subst_func arg_subst_map = SubstitutionMapper(make_subst_func(subst_dict)) @@ -809,10 +809,10 @@ class SubstitutionRuleMappingContext(object): self.make_unique_var_name = make_unique_var_name # maps subst rule (args, bodies) to (names, original_name) - self.subst_rule_registry = dict( - (self._get_subst_rule_key(rule.arguments, rule.expression), - (name, rule.arguments, rule.expression)) - for name, rule in six.iteritems(old_subst_rules)) + self.subst_rule_registry = { + self._get_subst_rule_key(rule.arguments, rule.expression): + (name, rule.arguments, rule.expression) + for name, rule in old_subst_rules.items()} # maps subst rule (args, bodies) to a list of old names, # which doubles as (a) a histogram of uses and (b) a way @@ -861,8 +861,7 @@ class SubstitutionRuleMappingContext(object): used_names = set() - for key, (name, args, body) in six.iteritems( - self.subst_rule_registry): + for key, (name, args, body) in self.subst_rule_registry.items(): orig_names = self.subst_rule_old_names.get(key, []) # If no orig_names are found, then this particular @@ -889,7 +888,7 @@ class SubstitutionRuleMappingContext(object): subst_renamer = SubstitutionRuleRenamer(renames) renamed_result = {} - for name, rule in six.iteritems(result): + for name, rule in result.items(): renamed_result[name] = rule.copy( expression=subst_renamer(rule.expression)) @@ -932,7 +931,7 @@ class RuleAwareIdentityMapper(IdentityMapper): name, tag = parse_tagged_name(expr.function) if name not in self.rule_mapping_context.old_subst_rules: - return super(RuleAwareIdentityMapper, self).map_call(expr, expn_state) + return super().map_call(expr, expn_state) else: return self.map_substitution(name, tag, self.rec( expr.parameters, expn_state), expn_state) @@ -945,9 +944,9 @@ class RuleAwareIdentityMapper(IdentityMapper): from pymbolic.mapper.substitutor import make_subst_func arg_subst_map = SubstitutionMapper(make_subst_func(arg_context)) - return dict( - (formal_arg_name, arg_subst_map(arg_value)) - for formal_arg_name, arg_value in zip(arg_names, arguments)) + return { + formal_arg_name: arg_subst_map(arg_value) + for formal_arg_name, arg_value in zip(arg_names, arguments)} def map_substitution(self, name, tag, arguments, expn_state): rule = self.rule_mapping_context.old_subst_rules[name] @@ -1008,7 +1007,7 @@ class RuleAwareIdentityMapper(IdentityMapper): class RuleAwareSubstitutionMapper(RuleAwareIdentityMapper): def __init__(self, rule_mapping_context, subst_func, within): - super(RuleAwareSubstitutionMapper, self).__init__(rule_mapping_context) + super().__init__(rule_mapping_context) self.subst_func = subst_func self.within = within @@ -1017,20 +1016,20 @@ class RuleAwareSubstitutionMapper(RuleAwareIdentityMapper): if (expr.name in expn_state.arg_context or not self.within( expn_state.kernel, expn_state.instruction, expn_state.stack)): - return super(RuleAwareSubstitutionMapper, self).map_variable( + return super().map_variable( expr, expn_state) result = self.subst_func(expr) if result is not None: return result else: - return super(RuleAwareSubstitutionMapper, self).map_variable( + return super().map_variable( expr, expn_state) class RuleAwareSubstitutionRuleExpander(RuleAwareIdentityMapper): def __init__(self, rule_mapping_context, rules, within): - super(RuleAwareSubstitutionRuleExpander, self).__init__(rule_mapping_context) + super().__init__(rule_mapping_context) self.rules = rules self.within = within @@ -1063,7 +1062,7 @@ class RuleAwareSubstitutionRuleExpander(RuleAwareIdentityMapper): else: # do not expand - return super(RuleAwareSubstitutionRuleExpander, self).map_substitution( + return super().map_substitution( name, tag, arguments, expn_state) # }}} @@ -1218,7 +1217,7 @@ class LoopyParser(ParserBase): typename, self.parse_expression(pstate, _PREC_UNARY)) else: - return super(LoopyParser, self).parse_prefix(pstate) + return super().parse_prefix(pstate) def parse_postfix(self, pstate, min_precedence, left_exp): from pymbolic.parser import _PREC_CALL, _closebracket @@ -1278,7 +1277,7 @@ class ArrayAccessFinder(CombineMapper): if self.tgt_vector_name is None \ or expr.aggregate.name == self.tgt_vector_name: - return set([expr]) | self.rec(expr.index) + return {expr} | self.rec(expr.index) else: return CombineMapper.map_subscript(self, expr) @@ -1356,7 +1355,7 @@ class PwAffEvaluationMapper(EvaluationMapperBase, IdentityMapperMixin): self.zero = isl.Aff.zero_on_domain(isl.LocalSpace.from_space(space)) context = {} - for name, (dt, pos) in six.iteritems(space.get_var_dict()): + for name, (dt, pos) in space.get_var_dict().items(): if dt == dim_type.set: dt = dim_type.in_ @@ -1368,7 +1367,7 @@ class PwAffEvaluationMapper(EvaluationMapperBase, IdentityMapperMixin): self.pw_zero = isl.PwAff.from_aff(self.zero) - super(PwAffEvaluationMapper, self).__init__(context) + super().__init__(context) def map_constant(self, expr): if isinstance(expr, np.integer): @@ -1677,9 +1676,9 @@ def get_access_range(domain, subscript, assumptions, shape=None, access_map = isl.Set.from_basic_set(access_map) if allowed_constant_names is not None: - allowed_constant_names = set(allowed_constant_names) - set( + allowed_constant_names = set(allowed_constant_names) - { access_map.get_dim_name(dim_type.param, i) - for i in range(access_map.dim(dim_type.param))) + for i in range(access_map.dim(dim_type.param))} par_base = access_map.dim(dim_type.param) access_map = access_map.insert_dims(dim_type.param, par_base, @@ -1814,7 +1813,7 @@ class BatchedAccessRangeMapper(WalkMapper): return self.rec(expr.child, inames) -class AccessRangeMapper(object): +class AccessRangeMapper: """**IMPORTANT** Using this class *will likely* lead to performance bottlenecks. @@ -1849,7 +1848,7 @@ class AccessRangeMapper(object): # {{{ check if access ranges overlap -class AccessRangeOverlapChecker(object): +class AccessRangeOverlapChecker: """Used for checking for overlap between access ranges of instructions.""" def __init__(self, kernel): @@ -1873,7 +1872,7 @@ class AccessRangeOverlapChecker(object): for expr in exprs: arm(expr, self.kernel.insn_inames(insn)) - for name, arange in six.iteritems(arm.access_ranges): + for name, arange in arm.access_ranges.items(): if arm.bad_subscripts[name]: aranges[name] = True continue diff --git a/loopy/target/__init__.py b/loopy/target/__init__.py index 9389872f0..6bad214ec 100644 --- a/loopy/target/__init__.py +++ b/loopy/target/__init__.py @@ -1,6 +1,5 @@ """Base target interface.""" -from __future__ import division, absolute_import __copyright__ = "Copyright (C) 2015 Andreas Kloeckner" @@ -43,7 +42,7 @@ __doc__ = """ """ -class TargetBase(object): +class TargetBase: """Base class for all targets, i.e. different combinations of code that loopy can generate. @@ -141,7 +140,7 @@ class TargetBase(object): raise NotImplementedError() -class ASTBuilderBase(object): +class ASTBuilderBase: """An interface for generating (host or device) ASTs. """ @@ -243,14 +242,14 @@ class ASTBuilderBase(object): # {{{ dummy host ast builder -class _DummyExpressionToCodeMapper(object): +class _DummyExpressionToCodeMapper: def rec(self, expr, prec, type_context=None, needed_dtype=None): return "" __call__ = rec -class _DummyASTBlock(object): +class _DummyASTBlock: def __init__(self, arg): self.contents = [] diff --git a/loopy/target/c/__init__.py b/loopy/target/c/__init__.py index fe9717765..3234da45d 100644 --- a/loopy/target/c/__init__.py +++ b/loopy/target/c/__init__.py @@ -1,6 +1,5 @@ """Plain C target and base for other C-family languages.""" -from __future__ import division, absolute_import __copyright__ = "Copyright (C) 2015 Andreas Kloeckner" @@ -24,8 +23,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six - import numpy as np # noqa from loopy.kernel.data import CallMangleInfo from loopy.target import TargetBase, ASTBuilderBase, DummyHostASTBuilder @@ -52,7 +49,7 @@ __doc__ = """ # {{{ dtype registry wrapper -class DTypeRegistryWrapper(object): +class DTypeRegistryWrapper: def __init__(self, wrapped_registry): self.wrapped_registry = wrapped_registry @@ -166,9 +163,9 @@ def _preamble_generator(preamble_info): """, } - c_funcs = set(func.c_name for func in preamble_info.seen_functions) + c_funcs = {func.c_name for func in preamble_info.seen_functions} - for func_name, func_body in six.iteritems(function_defs): + for func_name, func_body in function_defs.items(): if any((func_name + "_" + tpname) in c_funcs for tpname in integer_type_names): yield def_integer_types_macro @@ -322,7 +319,7 @@ class ASTSubscriptCollector(CASTIdentityMapper): # {{{ lazy expression generation -class CExpression(object): +class CExpression: def __init__(self, to_code_mapper, expr): self.to_code_mapper = to_code_mapper self.expr = expr @@ -344,7 +341,7 @@ class CFamilyTarget(TargetBase): def __init__(self, fortran_abi=False): self.fortran_abi = fortran_abi - super(CFamilyTarget, self).__init__() + super().__init__() def split_kernel_at_global_barriers(self): return False @@ -442,7 +439,7 @@ def c_math_mangler(target, name, arg_dtypes, modify_name=True): elif dtype == np.float128: # pylint:disable=no-member name = name + "l" # fabsl else: - raise LoopyTypeError("%s does not support type %s" % (name, dtype)) + raise LoopyTypeError(f"{name} does not support type {dtype}") return CallMangleInfo( target_name=name, @@ -487,19 +484,19 @@ class CFamilyASTBuilder(ASTBuilderBase): def function_manglers(self): return ( - super(CFamilyASTBuilder, self).function_manglers() + [ + super().function_manglers() + [ c_math_mangler ]) def symbol_manglers(self): return ( - super(CFamilyASTBuilder, self).symbol_manglers() + [ + super().symbol_manglers() + [ c_symbol_mangler ]) def preamble_generators(self): return ( - super(CFamilyASTBuilder, self).preamble_generators() + [ + super().preamble_generators() + [ _preamble_generator, ]) @@ -534,7 +531,7 @@ class CFamilyASTBuilder(ASTBuilderBase): break if is_first_dev_prog: for tv in sorted( - six.itervalues(kernel.temporary_variables), + kernel.temporary_variables.values(), key=lambda tv: tv.name): if tv.address_space == AddressSpace.GLOBAL and ( @@ -624,7 +621,7 @@ class CFamilyASTBuilder(ASTBuilderBase): temporaries_written_in_subkernel(kernel, subkernel)) for tv in sorted( - six.itervalues(kernel.temporary_variables), + kernel.temporary_variables.values(), key=lambda tv: tv.name): decl_info = tv.decl_info(self.target, index_dtype=kernel.index_dtype) @@ -687,7 +684,7 @@ class CFamilyASTBuilder(ASTBuilderBase): cast_tp, cast_d = cast_decl.get_decl_pair() temp_var_decl = Initializer( temp_var_decl, - "(%s %s) (%s + %s)" % ( + "({} {}) ({} + {})".format( " ".join(cast_tp), cast_d, tv.base_storage, offset)) @@ -701,7 +698,7 @@ class CFamilyASTBuilder(ASTBuilderBase): ecm = self.get_expression_to_code_mapper(codegen_state) - for bs_name, bs_sizes in sorted(six.iteritems(base_storage_sizes)): + for bs_name, bs_sizes in sorted(base_storage_sizes.items()): bs_var_decl = Value("char", bs_name) from pytools import single_valued bs_var_decl = self.wrap_temporary_decl( @@ -1048,7 +1045,7 @@ class CFunctionDeclExtractor(CASTIdentityMapper): def map_function_decl_wrapper(self, node): self.decls.append(node.subdecl) - return super(CFunctionDeclExtractor, self)\ + return super()\ .map_function_decl_wrapper(node) @@ -1104,7 +1101,7 @@ class CTarget(CFamilyTarget): class CASTBuilder(CFamilyASTBuilder): def preamble_generators(self): return ( - super(CASTBuilder, self).preamble_generators() + [ + super().preamble_generators() + [ c99_preamble_generator, ]) @@ -1119,7 +1116,7 @@ class ExecutableCTarget(CTarget): """ def __init__(self, compiler=None, fortran_abi=False): - super(ExecutableCTarget, self).__init__(fortran_abi=fortran_abi) + super().__init__(fortran_abi=fortran_abi) from loopy.target.c.c_execution import CCompiler self.compiler = compiler or CCompiler() diff --git a/loopy/target/c/c_execution.py b/loopy/target/c/c_execution.py index 9c147b633..475ebdb4d 100644 --- a/loopy/target/c/c_execution.py +++ b/loopy/target/c/c_execution.py @@ -1,5 +1,3 @@ -from __future__ import division, with_statement, absolute_import - __copyright__ = "Copyright (C) 2017 Nick Curtis" __license__ = """ @@ -32,7 +30,6 @@ from pytools.py_codegen import (Indentation) from pytools.prefork import ExecError from codepy.toolchain import guess_toolchain, ToolchainGuessError, GCCToolchain from codepy.jit import compile_from_string -import six import ctypes import numpy as np @@ -49,12 +46,12 @@ class CExecutionWrapperGenerator(ExecutionWrapperGeneratorBase): def __init__(self): system_args = ["_lpy_c_kernels"] - super(CExecutionWrapperGenerator, self).__init__(system_args) + super().__init__(system_args) def python_dtype_str(self, dtype): if np.dtype(str(dtype)).isbuiltin: return "_lpy_np."+dtype.name - raise Exception("dtype: {0} not recognized".format(dtype)) + raise Exception(f"dtype: {dtype} not recognized") # {{{ handle non numpy arguements @@ -110,7 +107,7 @@ class CExecutionWrapperGenerator(ExecutionWrapperGeneratorBase): var("_lpy_expected_strides_%s" % i) for i in range(num_axes)) - gen("%s = %s.strides" % (strify(expected_strides), arg.name)) + gen("{} = {}.strides".format(strify(expected_strides), arg.name)) #check strides if not skip_arg_checks: @@ -163,7 +160,7 @@ class CExecutionWrapperGenerator(ExecutionWrapperGeneratorBase): if options.return_dict: gen("return None, {%s}" - % ", ".join('"%s": %s' % (arg.name, arg.name) + % ", ".join(f'"{arg.name}": {arg.name}' for arg in implemented_data_info if issubclass(arg.arg_class, KernelArgument) if arg.base_name in kernel.get_written_variables())) @@ -190,7 +187,7 @@ class CExecutionWrapperGenerator(ExecutionWrapperGeneratorBase): return arg.name -class CCompiler(object): +class CCompiler: """ The compiler module handles invocation of compilers to generate a shared lib using codepy, which can subsequently be loaded via ctypes. @@ -252,9 +249,9 @@ class CCompiler(object): "library_dirs": library_dirs, "defines": defines} # filter empty and those equal to toolchain defaults - diff = dict((k, v) for k, v in six.iteritems(diff) + diff = {k: v for k, v in diff.items() if v and (not hasattr(self.toolchain, k) or - getattr(self.toolchain, k) != v)) + getattr(self.toolchain, k) != v)} self.toolchain = self.toolchain.copy(**diff) self.tempdir = tempfile.mkdtemp(prefix="tmp_loopy") self.source_suffix = source_suffix @@ -276,9 +273,9 @@ class CCompiler(object): debug_recompile, False) if recompiled: - logger.debug("Kernel {0} compiled from source".format(name)) + logger.debug(f"Kernel {name} compiled from source") else: - logger.debug("Kernel {0} retrieved from cache".format(name)) + logger.debug(f"Kernel {name} retrieved from cache") # and return compiled return ctypes.CDLL(ext_file) @@ -293,13 +290,13 @@ class CPlusPlusCompiler(CCompiler): include_dirs=[], library_dirs=[], defines=[], source_suffix="cpp"): - super(CPlusPlusCompiler, self).__init__( + super().__init__( toolchain=toolchain, cc=cc, cflags=cflags, ldflags=ldflags, libraries=libraries, include_dirs=include_dirs, library_dirs=library_dirs, defines=defines, source_suffix=source_suffix) -class IDIToCDLL(object): +class IDIToCDLL: """ A utility class that extracts arguement and return type info from a :class:`ImplementedDataInfo` in order to create a :class:`ctype.CDLL` @@ -329,7 +326,7 @@ class IDIToCDLL(object): return basetype -class CompiledCKernel(object): +class CompiledCKernel: """ A CompiledCKernel wraps a loopy kernel, compiling it and loading the result as a shared library, and provides access to the kernel as a @@ -388,7 +385,7 @@ class CKernelExecutor(KernelExecutorBase): """ self.compiler = compiler if compiler else CCompiler() - super(CKernelExecutor, self).__init__(kernel) + super().__init__(kernel) def get_invoker_uncached(self, kernel, codegen_result): generator = CExecutionWrapperGenerator() diff --git a/loopy/target/c/codegen/expression.py b/loopy/target/c/codegen/expression.py index df49679a6..74f1ead8b 100644 --- a/loopy/target/c/codegen/expression.py +++ b/loopy/target/c/codegen/expression.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -23,8 +21,6 @@ THE SOFTWARE. """ -from six.moves import range - import numpy as np from pymbolic.mapper import RecursiveMapper, IdentityMapper @@ -137,7 +133,7 @@ class ExpressionToCExpressionMapper(IdentityMapper): if expr.name in self.codegen_state.var_subst_map: if self.kernel.options.annotate_inames: return var( - "/* %s */ %s" % ( + "/* {} */ {}".format( expr.name, self.rec(self.codegen_state.var_subst_map[expr.name], type_context))) @@ -241,8 +237,8 @@ class ExpressionToCExpressionMapper(IdentityMapper): elif isinstance(ary, (ArrayArg, TemporaryVariable, ConstantArg)): if len(access_info.subscripts) == 0: if ( - (isinstance(ary, (ConstantArg, ArrayArg)) or - (isinstance(ary, TemporaryVariable) and ary.base_storage))): + isinstance(ary, (ConstantArg, ArrayArg)) or + (isinstance(ary, TemporaryVariable) and ary.base_storage)): # unsubscripted global args are pointers result = self.make_subscript( ary, @@ -328,7 +324,7 @@ class ExpressionToCExpressionMapper(IdentityMapper): from loopy.codegen import SeenFunction self.codegen_state.seen_functions.add( SeenFunction( - name, "%s_%s" % (name, suffix), + name, f"{name}_{suffix}", (result_dtype, result_dtype))) if den_nonneg: @@ -338,12 +334,12 @@ class ExpressionToCExpressionMapper(IdentityMapper): self.rec(expr.denominator, type_context)) else: seen_func("%s_pos_b" % base_func_name) - return var("%s_pos_b_%s" % (base_func_name, suffix))( + return var(f"{base_func_name}_pos_b_{suffix}")( self.rec(expr.numerator, "i"), self.rec(expr.denominator, "i")) else: seen_func(base_func_name) - return var("%s_%s" % (base_func_name, suffix))( + return var(f"{base_func_name}_{suffix}")( self.rec(expr.numerator, "i"), self.rec(expr.denominator, "i")) @@ -800,7 +796,7 @@ class CExpressionToCodeMapper(RecursiveMapper): func = self.rec(expr.function, PREC_CALL+1) return self.parenthesize_if_needed( - "%s(%s)" % ( + "{}({})".format( func, self.join_rec(", ", expr.parameters, PREC_NONE)), enclosing_prec, PREC_CALL) @@ -816,13 +812,13 @@ class CExpressionToCodeMapper(RecursiveMapper): def map_lookup(self, expr, enclosing_prec): return self.parenthesize_if_needed( - "%s.%s" % ( + "{}.{}".format( self.rec(expr.aggregate, PREC_CALL), expr.name), enclosing_prec, PREC_CALL) def map_subscript(self, expr, enclosing_prec): return self.parenthesize_if_needed( - "%s[%s]" % ( + "{}[{}]".format( self.rec(expr.aggregate, PREC_CALL+1), self.rec(expr.index, PREC_NONE)), enclosing_prec, PREC_CALL) @@ -834,7 +830,7 @@ class CExpressionToCodeMapper(RecursiveMapper): result = self.rec(children.pop(), PREC_NONE) while children: - result = "%s(%s, %s)" % (what, + result = "{}({}, {})".format(what, self.rec(children.pop(), PREC_NONE), result) @@ -844,7 +840,7 @@ class CExpressionToCodeMapper(RecursiveMapper): def map_if(self, expr, enclosing_prec): from pymbolic.mapper.stringifier import PREC_NONE - return "(%s ? %s : %s)" % ( + return "({} ? {} : {})".format( self.rec(expr.condition, PREC_NONE), self.rec(expr.then, PREC_NONE), self.rec(expr.else_, PREC_NONE), @@ -854,7 +850,7 @@ class CExpressionToCodeMapper(RecursiveMapper): from pymbolic.mapper.stringifier import PREC_COMPARISON return self.parenthesize_if_needed( - "%s %s %s" % ( + "{} {} {}".format( self.rec(expr.left, PREC_COMPARISON), expr.operator, self.rec(expr.right, PREC_COMPARISON)), @@ -939,7 +935,7 @@ class CExpressionToCodeMapper(RecursiveMapper): force_parens_around=self.multiplicative_primitives) return self.parenthesize_if_needed( - "%s %s %s" % ( + "{} {} {}".format( # Space is necessary--otherwise '/*' # (i.e. divide-dererference) becomes # start-of-comment in C. @@ -958,7 +954,7 @@ class CExpressionToCodeMapper(RecursiveMapper): return self._map_division_operator("%", expr, enclosing_prec) def map_power(self, expr, enclosing_prec): - return "pow(%s, %s)" % ( + return "pow({}, {})".format( self.rec(expr.base, PREC_NONE), self.rec(expr.exponent, PREC_NONE)) diff --git a/loopy/target/cuda.py b/loopy/target/cuda.py index 27422abce..2023077bf 100644 --- a/loopy/target/cuda.py +++ b/loopy/target/cuda.py @@ -1,6 +1,5 @@ """CUDA target independent of PyCUDA.""" -from __future__ import division, absolute_import __copyright__ = "Copyright (C) 2015 Andreas Kloeckner" @@ -155,12 +154,12 @@ class ExpressionToCudaCExpressionMapper(ExpressionToCExpressionMapper): raise LoopyError("unexpected index type") def map_group_hw_index(self, expr, type_context): - return var("((%s) blockIdx.%s)" % ( + return var("(({}) blockIdx.{})".format( self._get_index_ctype(self.kernel), self._GRID_AXES[expr.axis])) def map_local_hw_index(self, expr, type_context): - return var("((%s) threadIdx.%s)" % ( + return var("(({}) threadIdx.{})".format( self._get_index_ctype(self.kernel), self._GRID_AXES[expr.axis])) @@ -179,7 +178,7 @@ class CudaTarget(CFamilyTarget): """ self.extern_c = extern_c - super(CudaTarget, self).__init__() + super().__init__() def get_device_ast_builder(self): return CUDACASTBuilder(self) @@ -221,7 +220,7 @@ class CUDACASTBuilder(CFamilyASTBuilder): def function_manglers(self): return ( - super(CUDACASTBuilder, self).function_manglers() + [ + super().function_manglers() + [ cuda_function_mangler ]) @@ -231,7 +230,7 @@ class CUDACASTBuilder(CFamilyASTBuilder): def get_function_declaration(self, codegen_state, codegen_result, schedule_index): - fdecl = super(CUDACASTBuilder, self).get_function_declaration( + fdecl = super().get_function_declaration( codegen_state, codegen_result, schedule_index) from loopy.target.c import FunctionDeclarationWrapper diff --git a/loopy/target/execution.py b/loopy/target/execution.py index a503475d0..74819b939 100644 --- a/loopy/target/execution.py +++ b/loopy/target/execution.py @@ -1,5 +1,3 @@ -from __future__ import division, with_statement, absolute_import - __copyright__ = "Copyright (C) 2012-17 Andreas Kloeckner, Nick Curtis" __license__ = """ @@ -23,7 +21,6 @@ THE SOFTWARE. """ -import six import numpy as np from pytools import ImmutableRecord, memoize_method from loopy.diagnostic import LoopyError @@ -51,7 +48,7 @@ class _PackingInfo(ImmutableRecord): """ -class SeparateArrayPackingController(object): +class SeparateArrayPackingController: """For argument arrays with axes tagged to be implemented as separate arrays, this class provides preprocessing of the incoming arguments so that all sub-arrays may be passed in one object array (under the original, @@ -90,7 +87,7 @@ class SeparateArrayPackingController(object): kernel_kwargs = kernel_kwargs.copy() - for packing_info in six.itervalues(self.packing_info): + for packing_info in self.packing_info.values(): arg_name = packing_info.name if packing_info.name in kernel_kwargs: arg = kernel_kwargs[arg_name] @@ -105,7 +102,7 @@ class SeparateArrayPackingController(object): if not self.packing_info: return outputs - for packing_info in six.itervalues(self.packing_info): + for packing_info in self.packing_info.values(): if not packing_info.is_written: continue @@ -122,7 +119,7 @@ class SeparateArrayPackingController(object): # {{{ ExecutionWrapperGeneratorBase -class ExecutionWrapperGeneratorBase(object): +class ExecutionWrapperGeneratorBase: """ A set of common methods for generating a wrapper for execution @@ -193,12 +190,12 @@ class ExecutionWrapperGeneratorBase(object): gen("# {{{ find integer arguments from shapes") gen("") - for iarg_name, sources in six.iteritems(iarg_to_sources): + for iarg_name, sources in iarg_to_sources.items(): gen("if %s is None:" % iarg_name) with Indentation(gen): if_stmt = "if" for arg_name, value_expr in sources: - gen("%s %s is not None:" % (if_stmt, arg_name)) + gen(f"{if_stmt} {arg_name} is not None:") with Indentation(gen): gen("%s = %s" % (iarg_name, StringifyMapper()(value_expr))) @@ -528,8 +525,9 @@ class ExecutionWrapperGeneratorBase(object): shape = ["_lpy_shape_%d" % i for i in range(ndim)] strides = ["_lpy_stride_%d" % i for i in range(ndim)] - gen("(%s,) = %s.shape" % (", ".join(shape), arg.name)) - gen("(%s,) = %s.strides" % (", ".join(strides), arg.name)) + gen("({},) = {}.shape".format(", ".join(shape), arg.name)) + gen("({},) = {}.strides".format( + ", ".join(strides), arg.name)) gen("if not (%s):" % self.get_strides_check_expr( @@ -553,7 +551,7 @@ class ExecutionWrapperGeneratorBase(object): % arg.name) if not arg.allows_offset: - gen("if hasattr(%s, 'offset') and %s.offset:" % ( + gen("if hasattr({}, 'offset') and {}.offset:".format( arg.name, arg.name)) with Indentation(gen): gen("raise ValueError(\"Argument '%s' does not " @@ -689,7 +687,7 @@ class _KernelInfo(ImmutableRecord): pass -class _Kernels(object): +class _Kernels: pass @@ -705,7 +703,7 @@ invoker_cache = WriteOncePersistentDict( # {{{ kernel executor -class KernelExecutorBase(object): +class KernelExecutorBase: """An object connecting a kernel to a :class:`pyopencl.Context` for execution. @@ -793,7 +791,7 @@ class KernelExecutorBase(object): impl_arg_to_arg = self.kernel.impl_arg_to_arg arg_to_dtype = {} - for arg_name, val in six.iteritems(kwargs): + for arg_name, val in kwargs.items(): arg = impl_arg_to_arg.get(arg_name, None) if arg is None: @@ -808,7 +806,7 @@ class KernelExecutorBase(object): else: arg_to_dtype[arg_name] = dtype - return frozenset(six.iteritems(arg_to_dtype)) + return frozenset(arg_to_dtype.items()) # {{{ debugging aids @@ -829,7 +827,7 @@ class KernelExecutorBase(object): if arg_to_dtype is not None: arg_to_dtype = frozenset( - (k, process_dtype(v)) for k, v in six.iteritems(arg_to_dtype)) + (k, process_dtype(v)) for k, v in arg_to_dtype.items()) kernel = self.get_typed_and_scheduled_kernel(arg_to_dtype) diff --git a/loopy/target/ispc.py b/loopy/target/ispc.py index 322c771b6..d1f92576c 100644 --- a/loopy/target/ispc.py +++ b/loopy/target/ispc.py @@ -1,6 +1,5 @@ """Target for Intel ISPC.""" -from __future__ import division, absolute_import __copyright__ = "Copyright (C) 2015 Andreas Kloeckner" @@ -93,7 +92,7 @@ class ExprToISPCExprMapper(ExpressionToCExpressionMapper): return expr else: - return super(ExprToISPCExprMapper, self).map_variable( + return super().map_variable( expr, type_context) def map_subscript(self, expr, type_context): @@ -125,7 +124,7 @@ class ExprToISPCExprMapper(ExpressionToCExpressionMapper): else: return result - return super(ExprToISPCExprMapper, self).map_subscript( + return super().map_subscript( expr, type_context) # }}} @@ -167,7 +166,7 @@ class ISPCTarget(CFamilyTarget): """ self.occa_mode = occa_mode - super(ISPCTarget, self).__init__() + super().__init__() host_program_name_suffix = "" device_program_name_suffix = "_inner" @@ -273,7 +272,7 @@ class ISPCASTBuilder(CFamilyASTBuilder): result.append( ISPCLaunch( tuple(ecm(gs_i, PREC_NONE) for gs_i in gsize), - "%s(%s)" % ( + "{}({})".format( name, ", ".join(arg_names) ))) @@ -351,7 +350,7 @@ class ISPCASTBuilder(CFamilyASTBuilder): dtype, is_written) def get_value_arg_decl(self, name, shape, dtype, is_written): - result = super(ISPCASTBuilder, self).get_value_arg_decl( + result = super().get_value_arg_decl( name, shape, dtype, is_written) from cgen import Reference, Const diff --git a/loopy/target/numba.py b/loopy/target/numba.py index 6946063ee..2df81ec1f 100644 --- a/loopy/target/numba.py +++ b/loopy/target/numba.py @@ -1,6 +1,5 @@ """Python host AST builder for integration with PyOpenCL.""" -from __future__ import division, absolute_import __copyright__ = "Copyright (C) 2016 Andreas Kloeckner" @@ -44,7 +43,7 @@ def _base_numba_preamble_generator(preamble_info): class NumbaBaseASTBuilder(PythonASTBuilderBase): def preamble_generators(self): return ( - super(NumbaBaseASTBuilder, self).preamble_generators() + [ + super().preamble_generators() + [ _base_numba_preamble_generator ]) @@ -72,7 +71,7 @@ class NumbaBaseASTBuilder(PythonASTBuilderBase): implemented_data_info = codegen_state.implemented_data_info return Statement( - "%s[%s, %s](%s)" % ( + "{}[{}, {}]({})".format( name, ecm(gsize, PREC_NONE), ecm(lsize, PREC_NONE), @@ -155,7 +154,7 @@ def _cuda_numba_preamble_generator(preamble_info): class NumbaCudaASTBuilder(NumbaBaseASTBuilder): def preamble_generators(self): return ( - super(NumbaCudaASTBuilder, self).preamble_generators() + [ + super().preamble_generators() + [ _cuda_numba_preamble_generator ]) diff --git a/loopy/target/opencl.py b/loopy/target/opencl.py index f81c05a39..2ff9ede55 100644 --- a/loopy/target/opencl.py +++ b/loopy/target/opencl.py @@ -1,6 +1,5 @@ """OpenCL target independent of PyOpenCL.""" -from __future__ import division, absolute_import __copyright__ = "Copyright (C) 2015 Andreas Kloeckner" @@ -48,7 +47,7 @@ class DTypeRegistryWrapperWithAtomics(DTypeRegistryWrapper): return super(self.wrapped_registry.get_or_register_dtype( names, NumpyType(dtype.dtype))) - return super(DTypeRegistryWrapperWithAtomics, self).get_or_register_dtype( + return super().get_or_register_dtype( names, dtype) @@ -59,7 +58,7 @@ class DTypeRegistryWrapperWithCL1Atomics(DTypeRegistryWrapperWithAtomics): if isinstance(dtype, AtomicNumpyType): return "volatile " + self.wrapped_registry.dtype_to_ctype(dtype) else: - return super(DTypeRegistryWrapperWithCL1Atomics, self).dtype_to_ctype( + return super().dtype_to_ctype( dtype) # }}} @@ -148,8 +147,8 @@ _CL_SIMPLE_MULTI_ARG_FUNCTIONS = { } -VECTOR_LITERAL_FUNCS = dict( - ("make_%s%d" % (name, count), (name, dtype, count)) +VECTOR_LITERAL_FUNCS = { + "make_%s%d" % (name, count): (name, dtype, count) for name, dtype in [ ("char", np.int8), ("uchar", np.uint8), @@ -163,7 +162,7 @@ VECTOR_LITERAL_FUNCS = dict( ("double", np.float64), ] for count in [2, 3, 4, 8, 16] - ) + } def opencl_function_mangler(kernel, name, arg_dtypes): @@ -315,7 +314,7 @@ class OpenCLTarget(CFamilyTarget): for floating point), ``"cl1-exch"`` (OpenCL 1.1 atomics, using double-exchange for floating point--not yet supported). """ - super(OpenCLTarget, self).__init__() + super().__init__() if atomics_flavor is None: atomics_flavor = "cl1" @@ -371,11 +370,11 @@ class OpenCLCASTBuilder(CFamilyASTBuilder): opencl_function_mangler, partial(c_math_mangler, modify_name=False) ] + - super(OpenCLCASTBuilder, self).function_manglers()) + super().function_manglers()) def symbol_manglers(self): return ( - super(OpenCLCASTBuilder, self).symbol_manglers() + [ + super().symbol_manglers() + [ opencl_symbol_mangler ]) @@ -383,7 +382,7 @@ class OpenCLCASTBuilder(CFamilyASTBuilder): from loopy.library.reduction import reduction_preamble_generator return ( - super(OpenCLCASTBuilder, self).preamble_generators() + [ + super().preamble_generators() + [ opencl_preamble_generator, reduction_preamble_generator, ]) @@ -394,7 +393,7 @@ class OpenCLCASTBuilder(CFamilyASTBuilder): def get_function_declaration(self, codegen_state, codegen_result, schedule_index): - fdecl = super(OpenCLCASTBuilder, self).get_function_declaration( + fdecl = super().get_function_declaration( codegen_state, codegen_result, schedule_index) from loopy.target.c import FunctionDeclarationWrapper @@ -453,7 +452,7 @@ class OpenCLCASTBuilder(CFamilyASTBuilder): mem_kind = mem_kind.upper() from cgen import Statement - return Statement("barrier(CLK_%s_MEM_FENCE)%s" % (mem_kind, comment)) + return Statement(f"barrier(CLK_{mem_kind}_MEM_FENCE){comment}") elif synchronization_kind == "global": raise LoopyError("OpenCL does not have global barriers") else: @@ -478,13 +477,13 @@ class OpenCLCASTBuilder(CFamilyASTBuilder): from loopy.kernel.data import AddressSpace if mem_address_space == AddressSpace.LOCAL: - return CLLocal(super(OpenCLCASTBuilder, self).get_array_arg_decl( + return CLLocal(super().get_array_arg_decl( name, mem_address_space, shape, dtype, is_written)) elif mem_address_space == AddressSpace.PRIVATE: - return super(OpenCLCASTBuilder, self).get_array_arg_decl( + return super().get_array_arg_decl( name, mem_address_space, shape, dtype, is_written) elif mem_address_space == AddressSpace.GLOBAL: - return CLGlobal(super(OpenCLCASTBuilder, self).get_array_arg_decl( + return CLGlobal(super().get_array_arg_decl( name, mem_address_space, shape, dtype, is_written)) else: raise ValueError("unexpected array argument scope: %s" @@ -614,7 +613,7 @@ class OpenCLCASTBuilder(CFamilyASTBuilder): old_val = "*(%s *) &" % ctype + old_val new_val = "*(%s *) &" % ctype + new_val - cast_str = "(%s %s *) " % (var_kind, ctype) + cast_str = f"({var_kind} {ctype} *) " return Block([ POD(self, NumpyType(lhs_dtype.dtype, target=self.target), diff --git a/loopy/target/pyopencl.py b/loopy/target/pyopencl.py index 1c8bf4caa..a17416c47 100644 --- a/loopy/target/pyopencl.py +++ b/loopy/target/pyopencl.py @@ -1,10 +1,5 @@ -# coding: utf-8 """OpenCL target integrated with PyOpenCL.""" -from __future__ import division, absolute_import - -import sys - __copyright__ = "Copyright (C) 2015 Andreas Kloeckner" __license__ = """ @@ -27,9 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six -from six.moves import range - import numpy as np from loopy.kernel.data import CallMangleInfo @@ -56,7 +48,7 @@ def adjust_local_temp_var_storage(kernel, device): from loopy.kernel.data import AddressSpace lmem_size = cl_char.usable_local_mem_size(device) - for temp_var in six.itervalues(kernel.temporary_variables): + for temp_var in kernel.temporary_variables.values(): if temp_var.address_space != AddressSpace.LOCAL: new_temp_vars[temp_var.name] = \ temp_var.copy(storage_shape=temp_var.shape) @@ -69,7 +61,7 @@ def adjust_local_temp_var_storage(kernel, device): other_loctemp_nbytes = [ tv.nbytes - for tv in six.itervalues(kernel.temporary_variables) + for tv in kernel.temporary_variables.values() if tv.address_space == AddressSpace.LOCAL and tv.name != temp_var.name] @@ -218,13 +210,13 @@ def pyopencl_function_mangler(target, name, arg_dtypes): "sinh", "cosh", "tanh", "conj"]: return CallMangleInfo( - target_name="%s_%s" % (tpname, name), + target_name=f"{tpname}_{name}", result_dtypes=(arg_dtype,), arg_dtypes=(arg_dtype,)) if name in ["real", "imag", "abs"]: return CallMangleInfo( - target_name="%s_%s" % (tpname, name), + target_name=f"{tpname}_{name}", result_dtypes=(NumpyType( np.dtype(arg_dtype.numpy_dtype.type(0).real)), ), @@ -264,7 +256,7 @@ def pyopencl_preamble_generator(preamble_info): # {{{ pyopencl tools -class _LegacyTypeRegistryStub(object): +class _LegacyTypeRegistryStub: """Adapts legacy PyOpenCL type registry to be usable with PyOpenCLTarget.""" def get_or_register_dtype(self, names, dtype=None): @@ -297,7 +289,7 @@ class PyOpenCLTarget(OpenCLTarget): # This ensures the dtype registry is populated. import pyopencl.tools # noqa - super(PyOpenCLTarget, self).__init__( + super().__init__( atomics_flavor=atomics_flavor) self.device = device @@ -310,7 +302,7 @@ class PyOpenCLTarget(OpenCLTarget): "pyopencl_module_name",) def __eq__(self, other): - if not super(PyOpenCLTarget, self).__eq__(other): + if not super().__eq__(other): return False if (self.device is None) != (other.device is None): @@ -325,7 +317,7 @@ class PyOpenCLTarget(OpenCLTarget): return True def update_persistent_hash(self, key_hash, key_builder): - super(PyOpenCLTarget, self).update_persistent_hash(key_hash, key_builder) + super().update_persistent_hash(key_hash, key_builder) key_builder.rec(key_hash, getattr( self.device, "hashable_model_and_version_identifier", None)) @@ -520,12 +512,9 @@ def generate_value_arg_setup(kernel, devices, implemented_data_info): if idi.dtype.is_integral(): gen(Comment("cast to Python int to avoid trouble " "with struct packing or Boost.Python")) - if sys.version_info < (3,): - py_type = "long" - else: - py_type = "int" + py_type = "int" - gen(Assign(idi.name, "%s(%s)" % (py_type, idi.name))) + gen(Assign(idi.name, f"{py_type}({idi.name})")) gen(Line()) if idi.dtype.is_composite(): @@ -675,14 +664,14 @@ class PyOpenCLPythonASTBuilder(PythonASTBuilderBase): from genpy import Assign, Comment, Line def alloc_nbytes(tv): - from six.moves import reduce + from functools import reduce from operator import mul return tv.dtype.numpy_dtype.itemsize * reduce(mul, tv.shape, 1) from loopy.kernel.data import AddressSpace global_temporaries = sorted( - (tv for tv in six.itervalues(codegen_state.kernel.temporary_variables) + (tv for tv in codegen_state.kernel.temporary_variables.values() if tv.address_space == AddressSpace.GLOBAL), key=lambda tv: tv.name) @@ -773,7 +762,7 @@ class PyOpenCLCASTBuilder(OpenCLCASTBuilder): def function_manglers(self): from loopy.library.random123 import random123_function_mangler return ( - super(PyOpenCLCASTBuilder, self).function_manglers() + [ + super().function_manglers() + [ pyopencl_function_mangler, random123_function_mangler ]) @@ -783,7 +772,7 @@ class PyOpenCLCASTBuilder(OpenCLCASTBuilder): return ([ pyopencl_preamble_generator, random123_preamble_generator, - ] + super(PyOpenCLCASTBuilder, self).preamble_generators()) + ] + super().preamble_generators()) # }}} diff --git a/loopy/target/pyopencl_execution.py b/loopy/target/pyopencl_execution.py index 7ede6e760..7fc20f191 100644 --- a/loopy/target/pyopencl_execution.py +++ b/loopy/target/pyopencl_execution.py @@ -1,5 +1,3 @@ -from __future__ import division, with_statement, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -22,7 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -from six.moves import range, zip from pytools import memoize_method from pytools.py_codegen import Indentation @@ -50,7 +47,7 @@ class PyOpenCLExecutionWrapperGenerator(ExecutionWrapperGeneratorBase): # ignored if options.no_numpy "out_host=None" ] - super(PyOpenCLExecutionWrapperGenerator, self).__init__(system_args) + super().__init__(system_args) def python_dtype_str(self, dtype): import pyopencl.tools as cl_tools @@ -66,7 +63,7 @@ class PyOpenCLExecutionWrapperGenerator(ExecutionWrapperGeneratorBase): gen("if isinstance(%s, _lpy_np.ndarray):" % arg.name) with Indentation(gen): gen("# retain originally passed array") - gen("_lpy_%s_np_input = %s" % (arg.name, arg.name)) + gen(f"_lpy_{arg.name}_np_input = {arg.name}") gen("# synchronous, nothing to worry about") gen("%s = _lpy_cl_array.to_device(" "queue, %s, allocator=allocator)" @@ -189,7 +186,7 @@ class PyOpenCLExecutionWrapperGenerator(ExecutionWrapperGeneratorBase): for arg in implemented_data_info: if (issubclass(arg.arg_class, ArrayArg) and arg.base_name in kernel.get_written_variables()): - gen("{arg_name}.add_event(_lpy_evt)".format(arg_name=arg.name)) + gen(f"{arg.name}.add_event(_lpy_evt)") # }}} @@ -222,7 +219,7 @@ class PyOpenCLExecutionWrapperGenerator(ExecutionWrapperGeneratorBase): if options.return_dict: gen("return _lpy_evt, {%s}" - % ", ".join('"%s": %s' % (arg.name, arg.name) + % ", ".join(f'"{arg.name}": {arg.name}' for arg in implemented_data_info if issubclass(arg.arg_class, KernelArgument) if arg.base_name in kernel.get_written_variables())) @@ -268,7 +265,7 @@ class PyOpenCLKernelExecutor(KernelExecutorBase): specific arguments. """ - super(PyOpenCLKernelExecutor, self).__init__(kernel) + super().__init__(kernel) self.context = context diff --git a/loopy/target/python.py b/loopy/target/python.py index ce04986d3..e54aa622f 100644 --- a/loopy/target/python.py +++ b/loopy/target/python.py @@ -1,6 +1,5 @@ """Python host AST builder for integration with PyOpenCL.""" -from __future__ import division, absolute_import __copyright__ = "Copyright (C) 2016 Andreas Kloeckner" @@ -24,7 +23,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six import numpy as np from pymbolic.mapper import Mapper @@ -51,7 +49,7 @@ class ExpressionToPythonMapper(StringifyMapper): return Mapper.handle_unsupported_expression(self, victim, enclosing_prec) def rec(self, expr, prec, type_context=None, needed_dtype=None): - return super(ExpressionToPythonMapper, self).rec(expr, prec) + return super().rec(expr, prec) __call__ = rec @@ -66,19 +64,19 @@ class ExpressionToPythonMapper(StringifyMapper): enclosing_prec)) if expr.name in self.kernel.all_inames(): - return super(ExpressionToPythonMapper, self).map_variable( + return super().map_variable( expr, enclosing_prec) var_descr = self.kernel.get_var_descriptor(expr.name) if isinstance(var_descr, ValueArg): - return super(ExpressionToPythonMapper, self).map_variable( + return super().map_variable( expr, enclosing_prec) - return super(ExpressionToPythonMapper, self).map_variable( + return super().map_variable( expr, enclosing_prec) def map_subscript(self, expr, enclosing_prec): - return super(ExpressionToPythonMapper, self).map_subscript( + return super().map_subscript( expr, enclosing_prec) def map_call(self, expr, enclosing_prec): @@ -122,7 +120,7 @@ class ExpressionToPythonMapper(StringifyMapper): mangle_result.target_name, mangle_result.arg_dtypes or par_dtypes)) - return "%s(%s)" % (mangle_result.target_name, ", ".join(str_parameters)) + return "{}({})".format(mangle_result.target_name, ", ".join(str_parameters)) def map_group_hw_index(self, expr, enclosing_prec): raise LoopyError("plain Python does not have group hw axes") @@ -153,8 +151,7 @@ class ExpressionToPythonMapper(StringifyMapper): class Collection(Suite): def generate(self): for item in self.contents: - for item_line in item.generate(): - yield item_line + yield from item.generate() # }}} @@ -191,13 +188,13 @@ class PythonASTBuilderBase(ASTBuilderBase): def function_manglers(self): return ( - super(PythonASTBuilderBase, self).function_manglers() + [ + super().function_manglers() + [ _numpy_single_arg_function_mangler, ]) def preamble_generators(self): return ( - super(PythonASTBuilderBase, self).preamble_generators() + [ + super().preamble_generators() + [ _base_python_preamble_generator ]) @@ -227,7 +224,7 @@ class PythonASTBuilderBase(ASTBuilderBase): from genpy import Assign for tv in sorted( - six.itervalues(kernel.temporary_variables), + kernel.temporary_variables.values(), key=lambda tv: tv.name): if tv.shape: result.append( diff --git a/loopy/tools.py b/loopy/tools.py index a93b918f4..4fd093153 100644 --- a/loopy/tools.py +++ b/loopy/tools.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -22,13 +20,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six - -try: - import collections.abc as abc -except ImportError: - # Python 2 - import collections as abc +import collections.abc as abc import numpy as np from pytools import memoize_method @@ -36,8 +28,7 @@ from pytools.persistent_dict import KeyBuilder as KeyBuilderBase from loopy.symbolic import WalkMapper as LoopyWalkMapper from pymbolic.mapper.persistent_hash import ( PersistentHashWalkMapper as PersistentHashWalkMapperBase) -import six # noqa -from six.moves import intern +from sys import intern def is_integer(obj): @@ -74,7 +65,7 @@ class LoopyKeyBuilder(KeyBuilderBase): def update_for_dict(self, key_hash, key): # Order matters for the hash--insert in sorted order. - for dict_key in sorted(six.iterkeys(key)): + for dict_key in sorted(key.keys()): self.rec(key_hash, (dict_key, key[dict_key])) update_for_defaultdict = update_for_dict @@ -98,7 +89,7 @@ class LoopyKeyBuilder(KeyBuilderBase): % type(key)) def update_for_type_auto(self, key_hash, key): - key_hash.update("auto".encode("utf8")) + key_hash.update(b"auto") def update_for_pymbolic_expression(self, key_hash, key): if key is None: @@ -107,7 +98,7 @@ class LoopyKeyBuilder(KeyBuilderBase): PersistentHashWalkMapper(key_hash)(key) -class PymbolicExpressionHashWrapper(object): +class PymbolicExpressionHashWrapper: def __init__(self, expression): self.expression = expression @@ -126,7 +117,7 @@ class PymbolicExpressionHashWrapper(object): # {{{ eq key builder -class LoopyEqKeyBuilder(object): +class LoopyEqKeyBuilder: """Unlike :class:`loopy.tools.LoopyKeyBuilder`, this builds keys for use in equality comparison, such that `key(a) == key(b)` if and only if `a == b`. The types of objects being compared should satisfy structural equality. @@ -353,7 +344,7 @@ def empty_aligned(shape, dtype, order="C", n=64): # {{{ pickled container value -class _PickledObject(object): +class _PickledObject: """A class meant to wrap a pickled value (for :class:`LazilyUnpicklingDict` and :class:`LazilyUnpicklingList`). """ @@ -426,9 +417,9 @@ class LazilyUnpicklingDict(abc.MutableMapping): return iter(self._map) def __getstate__(self): - return {"_map": dict( - (key, _PickledObject(val)) - for key, val in six.iteritems(self._map))} + return {"_map": { + key: _PickledObject(val) + for key, val in self._map.items()}} # }}} @@ -528,11 +519,11 @@ class LazilyUnpicklingListWithEqAndPersistentHashing(LazilyUnpicklingList): # {{{ optional object -class _no_value(object): # noqa +class _no_value: # noqa pass -class Optional(object): +class Optional: """A wrapper for an optionally present object. .. attribute:: has_value @@ -599,7 +590,7 @@ class Optional(object): def unpickles_equally(obj): - from six.moves.cPickle import loads, dumps + from pickle import loads, dumps return loads(dumps(obj)) == obj diff --git a/loopy/transform/__init__.py b/loopy/transform/__init__.py index f42fd3c8d..625781167 100644 --- a/loopy/transform/__init__.py +++ b/loopy/transform/__init__.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ diff --git a/loopy/transform/add_barrier.py b/loopy/transform/add_barrier.py index 151c47866..bc324d7fa 100644 --- a/loopy/transform/add_barrier.py +++ b/loopy/transform/add_barrier.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2017 Kaushik Kulkarni" __license__ = """ diff --git a/loopy/transform/arithmetic.py b/loopy/transform/arithmetic.py index b7f47c38a..0a3879015 100644 --- a/loopy/transform/arithmetic.py +++ b/loopy/transform/arithmetic.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -23,8 +21,6 @@ THE SOFTWARE. """ -import six - from loopy.diagnostic import LoopyError @@ -38,10 +34,10 @@ def fold_constants(kernel): insn.with_transformed_expressions(cfm) for insn in kernel.instructions] - new_substs = dict( - (sub.name, - sub.copy(expression=cfm(sub.expression))) - for sub in six.itervalues(kernel.substitutions)) + new_substs = { + sub.name: + sub.copy(expression=cfm(sub.expression)) + for sub in kernel.substitutions.values()} return kernel.copy( instructions=new_insns, @@ -74,9 +70,9 @@ def collect_common_factors_on_increment(kernel, var_name, vary_by_axes=()): from loopy.kernel.array import ArrayBase if isinstance(var_descr, ArrayBase): if var_descr.dim_names is not None: - name_to_index = dict( - (name, idx) - for idx, name in enumerate(var_descr.dim_names)) + name_to_index = { + name: idx + for idx, name in enumerate(var_descr.dim_names)} else: name_to_index = {} @@ -140,8 +136,7 @@ def collect_common_factors_on_increment(kernel, var_name, vary_by_axes=()): def iterate_as(cls, expr): if isinstance(expr, cls): - for ch in expr.children: - yield ch + yield from expr.children else: yield expr @@ -216,9 +211,9 @@ def collect_common_factors_on_increment(kernel, var_name, vary_by_axes=()): product_parts = set(iterate_as(Product, term)) - my_common_factors = set( + my_common_factors = { cf for cf in my_common_factors - if unif_subst_map(cf) in product_parts) + if unif_subst_map(cf) in product_parts} common_factors[cf_index] = (index_key, my_common_factors) @@ -263,9 +258,9 @@ def collect_common_factors_on_increment(kernel, var_name, vary_by_axes=()): unif_subst_map = SubstitutionMapper( make_subst_func(unif_result.lmap)) - mapped_my_common_factors = set( + mapped_my_common_factors = { unif_subst_map(cf) - for cf in my_common_factors) + for cf in my_common_factors} new_sum_terms = [] diff --git a/loopy/transform/array_buffer_map.py b/loopy/transform/array_buffer_map.py index b62e13d6b..4ef5fac77 100644 --- a/loopy/transform/array_buffer_map.py +++ b/loopy/transform/array_buffer_map.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012-2015 Andreas Kloeckner" __license__ = """ @@ -22,7 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -from six.moves import range, zip import islpy as isl from islpy import dim_type @@ -195,7 +192,7 @@ def compute_bounds(kernel, domain, stor2sweep, # {{{ array-to-buffer map -class ArrayToBufferMap(object): +class ArrayToBufferMap: def __init__(self, kernel, domain, sweep_inames, access_descriptors, storage_axis_count): self.kernel = kernel @@ -218,8 +215,8 @@ class ArrayToBufferMap(object): self.primed_sweep_inames) self.prime_sweep_inames = SubstitutionMapper(make_subst_func( - dict((sin, var(psin)) - for sin, psin in zip(sweep_inames, self.primed_sweep_inames)))) + {sin: var(psin) + for sin, psin in zip(sweep_inames, self.primed_sweep_inames)})) # # }}} @@ -403,7 +400,7 @@ class ArrayToBufferMap(object): aligned_g_s2s_parm_dom) -class NoOpArrayToBufferMap(object): +class NoOpArrayToBufferMap: non1_storage_axis_names = () storage_base_indices = () non1_storage_shape = () diff --git a/loopy/transform/batch.py b/loopy/transform/batch.py index 2bb70865a..e27e59026 100644 --- a/loopy/transform/batch.py +++ b/loopy/transform/batch.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -23,8 +21,6 @@ THE SOFTWARE. """ -import six - from loopy.symbolic import (RuleAwareIdentityMapper, SubstitutionRuleMappingContext) from loopy.kernel.data import ValueArg, ArrayArg import islpy as isl @@ -55,7 +51,7 @@ def temp_needs_batching_if_not_sequential(tv, batch_varying_args): class _BatchVariableChanger(RuleAwareIdentityMapper): def __init__(self, rule_mapping_context, kernel, batch_varying_args, batch_iname_expr, sequential): - super(_BatchVariableChanger, self).__init__(rule_mapping_context) + super().__init__(rule_mapping_context) self.kernel = kernel self.batch_varying_args = batch_varying_args @@ -78,7 +74,7 @@ class _BatchVariableChanger(RuleAwareIdentityMapper): def map_subscript(self, expr, expn_state): if not self.needs_batch_subscript(expr.aggregate.name): - return super(_BatchVariableChanger, self).map_subscript(expr, expn_state) + return super().map_subscript(expr, expn_state) idx = self.rec(expr.index, expn_state) if not isinstance(idx, tuple): @@ -88,7 +84,7 @@ class _BatchVariableChanger(RuleAwareIdentityMapper): def map_variable(self, expr, expn_state): if not self.needs_batch_subscript(expr.name): - return super(_BatchVariableChanger, self).map_variable(expr, expn_state) + return super().map_variable(expr, expn_state) return expr[self.batch_iname_expr] @@ -129,10 +125,10 @@ def to_batched(kernel, nbatches, batch_varying_args, batch_iname_prefix="ibatch" new_args = [] - batch_dom_str = "{[%(iname)s]: 0 <= %(iname)s < %(nbatches)s}" % { - "iname": batch_iname, - "nbatches": nbatches, - } + batch_dom_str = "{{[{iname}]: 0 <= {iname} < {nbatches}}}".format( + iname=batch_iname, + nbatches=nbatches, + ) if not isinstance(nbatches, int): batch_dom_str = "[%s] -> " % nbatches + batch_dom_str @@ -165,7 +161,7 @@ def to_batched(kernel, nbatches, batch_varying_args, batch_iname_prefix="ibatch" if not sequential: new_temps = {} - for temp in six.itervalues(kernel.temporary_variables): + for temp in kernel.temporary_variables.values(): if temp_needs_batching_if_not_sequential(temp, batch_varying_args): new_temps[temp.name] = temp.copy( shape=(nbatches_expr,) + temp.shape, diff --git a/loopy/transform/buffer.py b/loopy/transform/buffer.py index 7f4779cc7..84503a618 100644 --- a/loopy/transform/buffer.py +++ b/loopy/transform/buffer.py @@ -1,6 +1,3 @@ -from __future__ import division, absolute_import -from six.moves import range - __copyright__ = "Copyright (C) 2012-2015 Andreas Kloeckner" __license__ = """ @@ -45,7 +42,7 @@ logger = logging.getLogger(__name__) class ArrayAccessReplacer(RuleAwareIdentityMapper): def __init__(self, rule_mapping_context, var_name, within, array_base_map, buf_var): - super(ArrayAccessReplacer, self).__init__(rule_mapping_context) + super().__init__(rule_mapping_context) self.within = within @@ -65,7 +62,7 @@ class ArrayAccessReplacer(RuleAwareIdentityMapper): result = self.map_array_access((), expn_state) if result is None: - return super(ArrayAccessReplacer, self).map_variable(expr, expn_state) + return super().map_variable(expr, expn_state) else: self.modified_insn_ids.add(expn_state.insn_id) return result @@ -79,7 +76,7 @@ class ArrayAccessReplacer(RuleAwareIdentityMapper): result = self.map_array_access(expr.index_tuple, expn_state) if result is None: - return super(ArrayAccessReplacer, self).map_subscript(expr, expn_state) + return super().map_subscript(expr, expn_state) else: self.modified_insn_ids.add(expn_state.insn_id) return result @@ -303,8 +300,8 @@ def buffer_array(kernel, var_name, buffer_inames, init_expression=None, if isinstance(var_descr, ArrayBase) and var_descr.dim_names is not None: dim_name = var_descr.dim_names[i] - init_iname = var_name_gen("%s_init_%s" % (var_name, dim_name)) - store_iname = var_name_gen("%s_store_%s" % (var_name, dim_name)) + init_iname = var_name_gen(f"{var_name}_init_{dim_name}") + store_iname = var_name_gen(f"{var_name}_store_{dim_name}") new_iname_to_tag[init_iname] = default_tag new_iname_to_tag[store_iname] = default_tag diff --git a/loopy/transform/data.py b/loopy/transform/data.py index 116a0a349..a50725d20 100644 --- a/loopy/transform/data.py +++ b/loopy/transform/data.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -22,8 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six # noqa - from loopy.diagnostic import LoopyError from islpy import dim_type @@ -293,7 +289,7 @@ def add_prefetch(kernel, var_name, sweep_inames=[], dim_arg_names=None, for i in range(var_descr.num_user_axes()): based_on = "%s_dim_%d" % (c_name, i) if var_descr.dim_names is not None: - based_on = "%s_dim_%s" % (c_name, var_descr.dim_names[i]) + based_on = "{}_dim_{}".format(c_name, var_descr.dim_names[i]) if dim_arg_names is not None and i < len(dim_arg_names): based_on = dim_arg_names[i] @@ -480,7 +476,7 @@ def remove_unused_arguments(kernel): return set() return get_dependencies(expr) - for ary in chain(kernel.args, six.itervalues(kernel.temporary_variables)): + for ary in chain(kernel.args, kernel.temporary_variables.values()): if isinstance(ary, ArrayBase): refd_vars.update( tolerant_get_deps(ary.shape) @@ -565,7 +561,7 @@ def alias_temporaries(kernel, names, base_name_prefix=None, new_insns = kernel.instructions new_temporary_variables = {} - for tv in six.itervalues(kernel.temporary_variables): + for tv in kernel.temporary_variables.values(): if tv.name in names_set: if tv.base_storage is not None: raise LoopyError("temporary variable '{tv}' already has " diff --git a/loopy/transform/diff.py b/loopy/transform/diff.py index b86a38092..59428cde2 100644 --- a/loopy/transform/diff.py +++ b/loopy/transform/diff.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - __copyright__ = "Copyright (C) 2015 Andreas Kloeckner" __license__ = """ @@ -166,7 +164,7 @@ class LoopyDiffMapper(DifferentiationMapper, RuleAwareIdentityMapper): # {{{ diff context -class DifferentiationContext(object): +class DifferentiationContext: def __init__(self, kernel, var_name_gen, by_name, diff_iname_prefix, additional_shape): self.kernel = kernel diff --git a/loopy/transform/fusion.py b/loopy/transform/fusion.py index 838fbbb55..2d12ab92b 100644 --- a/loopy/transform/fusion.py +++ b/loopy/transform/fusion.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -23,8 +21,6 @@ THE SOFTWARE. """ -import six - import islpy as isl from islpy import dim_type @@ -53,7 +49,7 @@ def _rename_temporaries(kernel, suffix, all_identifiers): vng = kernel.get_var_name_generator() new_temporaries = {} - for tv in six.itervalues(kernel.temporary_variables): + for tv in kernel.temporary_variables.values(): if tv.name in all_identifiers: new_tv_name = vng(tv.name+suffix) else: @@ -105,7 +101,7 @@ def _ordered_merge_lists(list_a, list_b): def _merge_dicts(item_name, dict_a, dict_b): result = dict_a.copy() - for k, v in six.iteritems(dict_b): + for k, v in dict_b.items(): if k in result: if v != result[k]: raise LoopyError("inconsistent %ss for key '%s' in merge: %s and %s" @@ -193,7 +189,7 @@ def _fuse_two_kernels(kernela, kernelb): # {{{ fuse temporaries new_temporaries = kernela.temporary_variables.copy() - for b_name, b_tv in six.iteritems(kernelb.temporary_variables): + for b_name, b_tv in kernelb.temporary_variables.items(): assert b_name == b_tv.name new_tv_name = vng(b_name) @@ -240,7 +236,7 @@ def _fuse_two_kernels(kernela, kernelb): domains=new_domains, instructions=new_instructions, args=new_args, - name="%s_and_%s" % (kernela.name, kernelb.name), + name=f"{kernela.name}_and_{kernelb.name}", preambles=_ordered_merge_lists(kernela.preambles, kernelb.preambles), preamble_generators=_ordered_merge_lists( kernela.preamble_generators, kernelb.preamble_generators), diff --git a/loopy/transform/iname.py b/loopy/transform/iname.py index 905c1e64a..241c1492d 100644 --- a/loopy/transform/iname.py +++ b/loopy/transform/iname.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -23,9 +21,6 @@ THE SOFTWARE. """ -import six -from six.moves import zip - import islpy as isl from islpy import dim_type @@ -124,7 +119,7 @@ def prioritize_loops(kernel, loop_priority): class _InameSplitter(RuleAwareIdentityMapper): def __init__(self, rule_mapping_context, within, split_iname, outer_iname, inner_iname, replacement_index): - super(_InameSplitter, self).__init__(rule_mapping_context) + super().__init__(rule_mapping_context) self.within = within @@ -149,7 +144,7 @@ class _InameSplitter(RuleAwareIdentityMapper): self.rec(expr.expr, expn_state), expr.allow_simultaneous) else: - return super(_InameSplitter, self).map_reduction(expr, expn_state) + return super().map_reduction(expr, expn_state) def map_variable(self, expr, expn_state): if (expr.name == self.split_iname @@ -159,7 +154,7 @@ class _InameSplitter(RuleAwareIdentityMapper): expn_state.instruction)): return self.replacement_index else: - return super(_InameSplitter, self).map_variable(expr, expn_state) + return super().map_variable(expr, expn_state) def _split_iname_backend(kernel, split_iname, @@ -464,7 +459,7 @@ def chunk_iname(kernel, split_iname, num_chunks, class _InameJoiner(RuleAwareSubstitutionMapper): def __init__(self, rule_mapping_context, within, subst_func, joined_inames, new_iname): - super(_InameJoiner, self).__init__(rule_mapping_context, + super().__init__(rule_mapping_context, subst_func, within) self.joined_inames = set(joined_inames) @@ -495,7 +490,7 @@ class _InameJoiner(RuleAwareSubstitutionMapper): self.rec(expr.expr, expn_state), expr.allow_simultaneous) else: - return super(_InameJoiner, self).map_reduction(expr, expn_state) + return super().map_reduction(expr, expn_state) def join_inames(kernel, inames, new_iname=None, tag=None, within=None): @@ -628,7 +623,7 @@ def untag_inames(kernel, iname_to_untag, tag_type): knl_iname_to_tags = kernel.iname_to_tags.copy() old_tags = knl_iname_to_tags.get(iname_to_untag, frozenset()) - old_tags = set(tag for tag in old_tags if not isinstance(tag, tag_type)) + old_tags = {tag for tag in old_tags if not isinstance(tag, tag_type)} if old_tags: knl_iname_to_tags[iname_to_untag] = old_tags @@ -674,7 +669,7 @@ def tag_inames(kernel, iname_to_tag, force=False, ignore_nonexistent=False): # convert dict to list of tuples if isinstance(iname_to_tag, dict): - iname_to_tag = list(six.iteritems(iname_to_tag)) + iname_to_tag = list(iname_to_tag.items()) # flatten iterables of tags for each iname @@ -744,7 +739,7 @@ def tag_inames(kernel, iname_to_tag, force=False, ignore_nonexistent=False): # }}} knl_iname_to_tags = kernel.iname_to_tags.copy() - for iname, new_tag in six.iteritems(iname_to_tag): + for iname, new_tag in iname_to_tag.items(): if not new_tag: continue @@ -777,10 +772,10 @@ def tag_inames(kernel, iname_to_tag, force=False, ignore_nonexistent=False): class _InameDuplicator(RuleAwareIdentityMapper): def __init__(self, rule_mapping_context, old_to_new, within): - super(_InameDuplicator, self).__init__(rule_mapping_context) + super().__init__(rule_mapping_context) self.old_to_new = old_to_new - self.old_inames_set = set(six.iterkeys(old_to_new)) + self.old_inames_set = set(old_to_new.keys()) self.within = within def map_reduction(self, expr, expn_state): @@ -800,7 +795,7 @@ class _InameDuplicator(RuleAwareIdentityMapper): self.rec(expr.expr, expn_state), expr.allow_simultaneous) else: - return super(_InameDuplicator, self).map_reduction(expr, expn_state) + return super().map_reduction(expr, expn_state) def map_variable(self, expr, expn_state): new_name = self.old_to_new.get(expr.name) @@ -811,7 +806,7 @@ class _InameDuplicator(RuleAwareIdentityMapper): expn_state.kernel, expn_state.instruction, expn_state.stack)): - return super(_InameDuplicator, self).map_variable(expr, expn_state) + return super().map_variable(expr, expn_state) else: from pymbolic import var return var(new_name) @@ -932,8 +927,7 @@ def _get_iname_duplication_options(insn_iname_sets, old_common_inames=frozenset( common = common.union(old_common_inames) # Go into recursion - for option in _get_iname_duplication_options(insn_iname_sets, common): - yield option + yield from _get_iname_duplication_options(insn_iname_sets, common) # Do not yield anything beyond here! return @@ -960,9 +954,8 @@ def _get_iname_duplication_options(insn_iname_sets, old_common_inames=frozenset( if len(partitioning) > 1: for part in partitioning: working_set = frozenset(s for s in insn_iname_sets if s <= part) - for option in _get_iname_duplication_options(working_set, - old_common_inames): - yield option + yield from _get_iname_duplication_options(working_set, + old_common_inames) # If exactly one set was found, an iname duplication is necessary elif len(partitioning) == 1: inames, = partitioning @@ -1029,10 +1022,10 @@ def get_iname_duplication_options(kernel, use_boostable_into=None): from loopy.kernel.data import ConcurrentTag - concurrent_inames = set( + concurrent_inames = { iname for iname in kernel.all_inames() - if kernel.iname_tags_of_type(iname, ConcurrentTag)) + if kernel.iname_tags_of_type(iname, ConcurrentTag)} # First we extract the minimal necessary information from the kernel insn_iname_sets = ( @@ -1253,7 +1246,7 @@ def remove_any_newly_unused_inames(transformation_func): class _ReductionSplitter(RuleAwareIdentityMapper): def __init__(self, rule_mapping_context, within, inames, direction): - super(_ReductionSplitter, self).__init__( + super().__init__( rule_mapping_context) self.within = within @@ -1288,7 +1281,7 @@ class _ReductionSplitter(RuleAwareIdentityMapper): else: assert False else: - return super(_ReductionSplitter, self).map_reduction(expr, expn_state) + return super().map_reduction(expr, expn_state) def _split_reduction(kernel, inames, direction, within=None): @@ -1426,9 +1419,9 @@ def affine_map_inames(kernel, old_inames, new_inames, equations): from pymbolic.algorithm import solve_affine_equations_for old_inames_to_expr = solve_affine_equations_for(old_inames, equations) - subst_dict = dict( - (v.name, expr) - for v, expr in old_inames_to_expr.items()) + subst_dict = { + v.name: expr + for v, expr in old_inames_to_expr.items()} var_name_gen = kernel.get_var_name_generator() @@ -1484,9 +1477,9 @@ def affine_map_inames(kernel, old_inames, new_inames, equations): if dom_old_inames: dom_equations.append((lhs, rhs)) - this_eqn_old_iname_dim_types = set( + this_eqn_old_iname_dim_types = { dom_var_dict[old_iname][0] - for old_iname in eqn_deps & old_inames_set) + for old_iname in eqn_deps & old_inames_set} if this_eqn_old_iname_dim_types: if len(this_eqn_old_iname_dim_types) > 1: @@ -1630,7 +1623,7 @@ def separate_loop_head_tail_slab(kernel, iname, head_it_count, tail_it_count): class _ReductionInameUniquifier(RuleAwareIdentityMapper): def __init__(self, rule_mapping_context, inames, within): - super(_ReductionInameUniquifier, self).__init__(rule_mapping_context) + super().__init__(rule_mapping_context) self.inames = inames self.old_to_new = [] @@ -1682,7 +1675,7 @@ class _ReductionInameUniquifier(RuleAwareIdentityMapper): expn_state), expr.allow_simultaneous) else: - return super(_ReductionInameUniquifier, self).map_reduction( + return super().map_reduction( expr, expn_state) @@ -1825,7 +1818,7 @@ def add_inames_for_unused_hw_axes(kernel, within=None): for iname, tags in kernel.iname_to_tags.items() if ith_local_axes_tag in tags] if not inames: - raise LoopyError("Unused local hw axes {}.".format(i)) + raise LoopyError(f"Unused local hw axes {i}.") local_axes_to_inames.append(inames[0] if len(inames) == 1 else None) @@ -1835,7 +1828,7 @@ def add_inames_for_unused_hw_axes(kernel, within=None): for iname, tags in kernel.iname_to_tags.items() if ith_group_axes_tag in tags] if not inames: - raise LoopyError("Unused group hw axes {}.".format(i)) + raise LoopyError(f"Unused group hw axes {i}.") group_axes_to_inames.append(inames[0] if len(inames) == 1 else None) diff --git a/loopy/transform/instruction.py b/loopy/transform/instruction.py index e6ecb4093..45ade0eca 100644 --- a/loopy/transform/instruction.py +++ b/loopy/transform/instruction.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -22,8 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six # noqa - from loopy.diagnostic import LoopyError @@ -333,9 +329,9 @@ def uniquify_instruction_ids(kernel): from loopy.kernel.creation import UniqueName - insn_ids = set( + insn_ids = { insn.id for insn in kernel.instructions - if insn.id is not None and not isinstance(insn.id, UniqueName)) + if insn.id is not None and not isinstance(insn.id, UniqueName)} from pytools import UniqueNameGenerator insn_id_gen = UniqueNameGenerator(insn_ids) diff --git a/loopy/transform/padding.py b/loopy/transform/padding.py index 274f181fe..390fafb2b 100644 --- a/loopy/transform/padding.py +++ b/loopy/transform/padding.py @@ -1,7 +1,3 @@ -from __future__ import division -from __future__ import absolute_import -import six - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -31,7 +27,7 @@ from loopy.symbolic import RuleAwareIdentityMapper, SubstitutionRuleMappingConte class ArrayAxisSplitHelper(RuleAwareIdentityMapper): def __init__(self, rule_mapping_context, arg_names, handler): - super(ArrayAxisSplitHelper, self).__init__(rule_mapping_context) + super().__init__(rule_mapping_context) self.arg_names = arg_names self.handler = handler @@ -39,7 +35,7 @@ class ArrayAxisSplitHelper(RuleAwareIdentityMapper): if expr.aggregate.name in self.arg_names: return self.handler(expr) else: - return super(ArrayAxisSplitHelper, self).map_subscript(expr, expn_state) + return super().map_subscript(expr, expn_state) # {{{ split_array_dim (deprecated since June 2016) @@ -88,8 +84,8 @@ def split_array_dim(kernel, arrays_and_axes, count, auto_split_inames=True, if isinstance(arrays_and_axes, tuple): arrays_and_axes = [arrays_and_axes] - array_to_rest = dict( - (tup[0], normalize_rest(tup[1:])) for tup in arrays_and_axes) + array_to_rest = { + tup[0]: normalize_rest(tup[1:]) for tup in arrays_and_axes} if len(arrays_and_axes) != len(array_to_rest): raise RuntimeError("cannot split multiple axes of the same variable") @@ -102,7 +98,7 @@ def split_array_dim(kernel, arrays_and_axes, count, auto_split_inames=True, from loopy.kernel.tools import ArrayChanger - for array_name, (axis, order) in six.iteritems(array_to_rest): + for array_name, (axis, order) in array_to_rest.items(): achng = ArrayChanger(kernel, array_name) ary = achng.get() @@ -233,12 +229,12 @@ def split_array_dim(kernel, arrays_and_axes, count, auto_split_inames=True, rule_mapping_context = SubstitutionRuleMappingContext( kernel.substitutions, var_name_gen) aash = ArrayAxisSplitHelper(rule_mapping_context, - set(six.iterkeys(array_to_rest)), split_access_axis) + set(array_to_rest.keys()), split_access_axis) kernel = rule_mapping_context.finish_kernel(aash.map_kernel(kernel)) if auto_split_inames: from loopy import split_iname - for iname, (outer_iname, inner_iname) in six.iteritems(split_vars): + for iname, (outer_iname, inner_iname) in split_vars.items(): kernel = split_iname(kernel, iname, count, outer_iname=outer_iname, inner_iname=inner_iname, **split_kwargs) @@ -364,7 +360,7 @@ def _split_array_axis_inner(kernel, array_name, axis_nr, count, order="C"): rule_mapping_context = SubstitutionRuleMappingContext( kernel.substitutions, var_name_gen) aash = ArrayAxisSplitHelper(rule_mapping_context, - set([array_name]), split_access_axis) + {array_name}, split_access_axis) kernel = rule_mapping_context.finish_kernel(aash.map_kernel(kernel)) return kernel @@ -441,7 +437,7 @@ def find_padding_multiple(kernel, variable, axis, align_bytes, allowed_waste=0.1 # {{{ add_padding def add_padding(kernel, variable, axis, align_bytes): - arg_to_idx = dict((arg.name, i) for i, arg in enumerate(kernel.args)) + arg_to_idx = {arg.name: i for i, arg in enumerate(kernel.args)} arg_idx = arg_to_idx[variable] new_args = kernel.args[:] diff --git a/loopy/transform/parameter.py b/loopy/transform/parameter.py index fc5dad91d..62a1209f0 100644 --- a/loopy/transform/parameter.py +++ b/loopy/transform/parameter.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -23,7 +21,6 @@ THE SOFTWARE. """ -import six from loopy.symbolic import (RuleAwareSubstitutionMapper, SubstitutionRuleMappingContext) import islpy as isl @@ -113,7 +110,7 @@ def _fix_parameter(kernel, name, value): new_args.append(arg.map_exprs(map_expr)) new_temp_vars = {} - for tv in six.itervalues(kernel.temporary_variables): + for tv in kernel.temporary_variables.values(): new_temp_vars[tv.name] = tv.map_exprs(map_expr) from loopy.match import parse_stack_match @@ -142,7 +139,7 @@ def fix_parameters(kernel, **value_dict): :ref:`arguments`. """ - for name, value in six.iteritems(value_dict): + for name, value in value_dict.items(): kernel = _fix_parameter(kernel, name, value) return kernel diff --git a/loopy/transform/precompute.py b/loopy/transform/precompute.py index b308836c7..cefed807d 100644 --- a/loopy/transform/precompute.py +++ b/loopy/transform/precompute.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -23,8 +21,6 @@ THE SOFTWARE. """ -import six -from six.moves import range, zip import islpy as isl from loopy.symbolic import (get_dependencies, RuleAwareIdentityMapper, RuleAwareSubstitutionMapper, @@ -63,7 +59,7 @@ def storage_axis_exprs(storage_axis_sources, args): class RuleInvocationGatherer(RuleAwareIdentityMapper): def __init__(self, rule_mapping_context, kernel, subst_name, subst_tag, within): - super(RuleInvocationGatherer, self).__init__(rule_mapping_context) + super().__init__(rule_mapping_context) from loopy.symbolic import SubstitutionRuleExpander self.subst_expander = SubstitutionRuleExpander( @@ -88,7 +84,7 @@ class RuleInvocationGatherer(RuleAwareIdentityMapper): expn_state.stack) if not process_me: - return super(RuleInvocationGatherer, self).map_substitution( + return super().map_substitution( name, tag, arguments, expn_state) rule = self.rule_mapping_context.old_subst_rules[name] @@ -96,7 +92,7 @@ class RuleInvocationGatherer(RuleAwareIdentityMapper): name, rule.arguments, arguments, expn_state.arg_context) arg_deps = set() - for arg_val in six.itervalues(arg_context): + for arg_val in arg_context.values(): arg_deps = (arg_deps | get_dependencies(self.subst_expander(arg_val))) @@ -113,7 +109,7 @@ class RuleInvocationGatherer(RuleAwareIdentityMapper): ", ".join(arg_deps - self.kernel.all_inames()), )) - return super(RuleInvocationGatherer, self).map_substitution( + return super().map_substitution( name, tag, arguments, expn_state) args = [arg_context[arg_name] for arg_name in rule.arguments] @@ -138,7 +134,7 @@ class RuleInvocationReplacer(RuleAwareIdentityMapper): non1_storage_axis_names, temporary_name, compute_insn_id, compute_dep_id, compute_read_variables): - super(RuleInvocationReplacer, self).__init__(rule_mapping_context) + super().__init__(rule_mapping_context) self.subst_name = subst_name self.subst_tag = subst_tag @@ -166,7 +162,7 @@ class RuleInvocationReplacer(RuleAwareIdentityMapper): expn_state.instruction, expn_state.stack) and (self.subst_tag is None or self.subst_tag == tag)): - return super(RuleInvocationReplacer, self).map_substitution( + return super().map_substitution( name, tag, arguments, expn_state) # {{{ check if in footprint @@ -181,7 +177,7 @@ class RuleInvocationReplacer(RuleAwareIdentityMapper): self.storage_axis_sources, args)) if not self.array_base_map.is_access_descriptor_in_footprint(accdesc): - return super(RuleInvocationReplacer, self).map_substitution( + return super().map_substitution( name, tag, arguments, expn_state) # }}} @@ -224,7 +220,7 @@ class RuleInvocationReplacer(RuleAwareIdentityMapper): def map_kernel(self, kernel): new_insns = [] - excluded_insn_ids = set([self.compute_insn_id, self.compute_dep_id]) + excluded_insn_ids = {self.compute_insn_id, self.compute_dep_id} for insn in kernel.instructions: self.replaced_something = False @@ -255,7 +251,7 @@ class RuleInvocationReplacer(RuleAwareIdentityMapper): # }}} -class _not_provided(object): # noqa: N801 +class _not_provided: # noqa: N801 pass @@ -616,7 +612,7 @@ def precompute(kernel, subst_use, sweep_inames=[], within=None, name = old_name = subst.arguments[saxis] else: old_name = saxis - name = "%s_%s" % (c_subst_name, old_name) + name = f"{c_subst_name}_{old_name}" if (precompute_inames is not None and i < len(precompute_inames) diff --git a/loopy/transform/privatize.py b/loopy/transform/privatize.py index d4128bd11..8527023bc 100644 --- a/loopy/transform/privatize.py +++ b/loopy/transform/privatize.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2015 Andreas Kloeckner" __license__ = """ @@ -23,7 +21,6 @@ THE SOFTWARE. """ -import six from loopy.diagnostic import LoopyError import logging @@ -120,7 +117,7 @@ def privatize_temporaries_with_inames( # {{{ find variables that need extra indices - for tv in six.itervalues(kernel.temporary_variables): + for tv in kernel.temporary_variables.values(): if only_var_names is not None and tv.name not in only_var_names: continue @@ -158,7 +155,7 @@ def privatize_temporaries_with_inames( from loopy.symbolic import pw_aff_to_expr priv_axis_iname_to_length = {} - for priv_axis_inames in six.itervalues(var_to_new_priv_axis_iname): + for priv_axis_inames in var_to_new_priv_axis_iname.values(): for iname in priv_axis_inames: if iname in priv_axis_iname_to_length: continue @@ -177,7 +174,7 @@ def privatize_temporaries_with_inames( from loopy.kernel.data import VectorizeTag new_temp_vars = kernel.temporary_variables.copy() - for tv_name, inames in six.iteritems(var_to_new_priv_axis_iname): + for tv_name, inames in var_to_new_priv_axis_iname.items(): tv = new_temp_vars[tv_name] extra_shape = tuple(priv_axis_iname_to_length[iname] for iname in inames) @@ -199,9 +196,9 @@ def privatize_temporaries_with_inames( # }}} from pymbolic import var - var_to_extra_iname = dict( - (var_name, tuple(var(iname) for iname in inames)) - for var_name, inames in six.iteritems(var_to_new_priv_axis_iname)) + var_to_extra_iname = { + var_name: tuple(var(iname) for iname in inames) + for var_name, inames in var_to_new_priv_axis_iname.items()} new_insns = [] diff --git a/loopy/transform/save.py b/loopy/transform/save.py index 8ee2899fb..0a9cfb7bc 100644 --- a/loopy/transform/save.py +++ b/loopy/transform/save.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2016 Matt Wala" __license__ = """ @@ -25,7 +23,6 @@ THE SOFTWARE. from loopy.diagnostic import LoopyError import loopy as lp -import six from loopy.kernel.data import auto, AddressSpace from pytools import memoize_method, Record @@ -60,7 +57,7 @@ class LivenessResult(dict): for idx in range(nscheditems)) -class LivenessAnalysis(object): +class LivenessAnalysis: def __init__(self, kernel): self.kernel = kernel @@ -82,10 +79,10 @@ class LivenessAnalysis(object): elif isinstance(next_item, EnterLoop): # Account for empty loop loop_end = block_bounds[sched_idx + 1] - after = successors[loop_end] | set([sched_idx + 1]) + after = successors[loop_end] | {sched_idx + 1} elif isinstance(next_item, (LeaveLoop, RunInstruction, CallKernel, ReturnFromKernel, Barrier)): - after = set([sched_idx + 1]) + after = {sched_idx + 1} else: raise LoopyError("unexpected type of schedule item: {ty}" .format(ty=type(next_item).__name__)) @@ -94,7 +91,7 @@ class LivenessAnalysis(object): if isinstance(item, LeaveLoop): # Account for loop loop_begin = block_bounds[sched_idx] - after |= set([loop_begin]) + after |= {loop_begin} elif not isinstance(item, (EnterLoop, RunInstruction, CallKernel, ReturnFromKernel, Barrier)): raise LoopyError("unexpected type of schedule item: {ty}" @@ -105,8 +102,8 @@ class LivenessAnalysis(object): return successors def get_gen_and_kill_sets(self): - gen = dict((idx, set()) for idx in range(len(self.schedule))) - kill = dict((idx, set()) for idx in range(len(self.schedule))) + gen = {idx: set() for idx in range(len(self.schedule))} + kill = {idx: set() for idx in range(len(self.schedule))} for sched_idx, sched_item in enumerate(self.schedule): if not isinstance(sched_item, RunInstruction): @@ -186,7 +183,7 @@ class LivenessAnalysis(object): # {{{ save and reload implementation -class TemporarySaver(object): +class TemporarySaver: class PromotedTemporary(Record): """ @@ -264,15 +261,15 @@ class TemporarySaver(object): isl.Space.create_from_names( isl.DEFAULT_CONTEXT, set=[], - params=set( + params={ arg.name for arg in kernel.args - if isinstance(arg, ValueArg))))) + if isinstance(arg, ValueArg)}))) def find_accessing_instructions_in_subkernel(self, temporary, subkernel): # Find all accessing instructions in the subkernel. If base_storage is # present, this includes instructions that access aliasing memory. - aliasing_names = set([temporary]) + aliasing_names = {temporary} base_storage = self.kernel.temporary_variables[temporary].base_storage if base_storage is not None: @@ -304,7 +301,7 @@ class TemporarySaver(object): result = defaultdict(set) - for temporary in six.itervalues(self.kernel.temporary_variables): + for temporary in self.kernel.temporary_variables.values(): if temporary.base_storage is None: continue result[temporary.base_storage].add(temporary.name) @@ -510,7 +507,7 @@ class TemporarySaver(object): self.new_subdomain = new_subdomain save_or_load_insn_id = self.insn_name_gen( - "{name}.{mode}".format(name=temporary, mode=mode)) + f"{temporary}.{mode}") def add_subscript_if_subscript_nonempty(agg, subscript=()): from pymbolic.primitives import Subscript, Variable @@ -548,10 +545,10 @@ class TemporarySaver(object): pre_barrier, post_barrier = self.get_enclosing_global_barrier_pair(subkernel) if pre_barrier is not None: - depends_on |= set([pre_barrier]) + depends_on |= {pre_barrier} if post_barrier is not None: - update_deps |= set([post_barrier]) + update_deps |= {post_barrier} # Create the load / store instruction. from loopy.kernel.data import Assignment @@ -587,7 +584,7 @@ class TemporarySaver(object): def finish(self): new_instructions = [] - insns_to_insert = dict((insn.id, insn) for insn in self.insns_to_insert) + insns_to_insert = {insn.id: insn for insn in self.insns_to_insert} for orig_insn in self.kernel.instructions: if orig_insn.id in self.insns_to_update: @@ -764,7 +761,7 @@ def save_and_reload_temporaries(kernel): | temporaries_written_in_subkernel(kernel, subkernel)) for temporary in liveness[sched_idx].live_out & interesting_temporaries: - logger.info("reloading {0} at entry of {1}" + logger.info("reloading {} at entry of {}" .format(temporary, sched_item.kernel_name)) saver.reload(temporary, sched_item.kernel_name) @@ -778,7 +775,7 @@ def save_and_reload_temporaries(kernel): temporaries_written_in_subkernel(kernel, subkernel)) for temporary in liveness[sched_idx].live_in & interesting_temporaries: - logger.info("saving {0} before return of {1}" + logger.info("saving {} before return of {}" .format(temporary, sched_item.kernel_name)) saver.save(temporary, sched_item.kernel_name) diff --git a/loopy/transform/subst.py b/loopy/transform/subst.py index d73ad995c..565c69a49 100644 --- a/loopy/transform/subst.py +++ b/loopy/transform/subst.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -22,8 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six - from loopy.symbolic import ( RuleAwareIdentityMapper, SubstitutionRuleMappingContext) from loopy.diagnostic import LoopyError @@ -101,8 +97,8 @@ def extract_subst(kernel, subst_name, template, parameters=()): ExprDescriptor( insn=insn, expr=expr, - unif_var_dict=dict((lhs.name, rhs) - for lhs, rhs in urec.equations))) + unif_var_dict={lhs.name: rhs + for lhs, rhs in urec.equations})) else: mapper.fallback_mapper(expr) # can't nest, don't recurse @@ -115,7 +111,7 @@ def extract_subst(kernel, subst_name, template, parameters=()): dfmapper(insn.assignees) dfmapper(insn.expression) - for sr in six.itervalues(kernel.substitutions): + for sr in kernel.substitutions.values(): dfmapper(sr.expression) # }}} @@ -182,7 +178,7 @@ def extract_subst(kernel, subst_name, template, parameters=()): expression=template, )} - for subst in six.itervalues(kernel.substitutions): + for subst in kernel.substitutions.values(): new_substs[subst.name] = subst.copy( expression=cbmapper(subst.expression)) @@ -202,7 +198,7 @@ class AssignmentToSubstChanger(RuleAwareIdentityMapper): usage_to_definition, extra_arguments, within): self.var_name_gen = rule_mapping_context.make_unique_var_name - super(AssignmentToSubstChanger, self).__init__(rule_mapping_context) + super().__init__(rule_mapping_context) self.lhs_name = lhs_name self.definition_insn_ids = definition_insn_ids @@ -234,7 +230,7 @@ class AssignmentToSubstChanger(RuleAwareIdentityMapper): if result is not None: return result - return super(AssignmentToSubstChanger, self).map_variable( + return super().map_variable( expr, expn_state) def map_subscript(self, expr, expn_state): @@ -244,7 +240,7 @@ class AssignmentToSubstChanger(RuleAwareIdentityMapper): if result is not None: return result - return super(AssignmentToSubstChanger, self).map_subscript( + return super().map_subscript( expr, expn_state) def transform_access(self, index, expn_state): @@ -382,7 +378,7 @@ def assignment_to_subst(kernel, lhs_name, extra_arguments=(), within=None, # {{{ create new substitution rules new_substs = kernel.substitutions.copy() - for def_id, subst_name in six.iteritems(tts.definition_insn_id_to_subst_name): + for def_id, subst_name in tts.definition_insn_id_to_subst_name.items(): def_insn = kernel.id_to_insn[def_id] from loopy.kernel.data import Assignment @@ -423,7 +419,7 @@ def assignment_to_subst(kernel, lhs_name, extra_arguments=(), within=None, new_args = kernel.args if lhs_name in kernel.temporary_variables: - if not any(six.itervalues(tts.saw_unmatched_usage_sites)): + if not any(tts.saw_unmatched_usage_sites.values()): # All usage sites matched--they're now substitution rules. # We can get rid of the variable. @@ -431,7 +427,7 @@ def assignment_to_subst(kernel, lhs_name, extra_arguments=(), within=None, del new_temp_vars[lhs_name] if lhs_name in kernel.arg_dict and not force_retain_argument: - if not any(six.itervalues(tts.saw_unmatched_usage_sites)): + if not any(tts.saw_unmatched_usage_sites.values()): # All usage sites matched--they're now substitution rules. # We can get rid of the argument @@ -446,11 +442,10 @@ def assignment_to_subst(kernel, lhs_name, extra_arguments=(), within=None, import loopy as lp kernel = lp.remove_instructions( kernel, - set( + { insn_id - for insn_id, still_used in six.iteritems( - tts.saw_unmatched_usage_sites) - if not still_used)) + for insn_id, still_used in tts.saw_unmatched_usage_sites.items() + if not still_used}) return kernel.copy( substitutions=new_substs, diff --git a/loopy/type_inference.py b/loopy/type_inference.py index c1ceb4461..64337864f 100644 --- a/loopy/type_inference.py +++ b/loopy/type_inference.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012-16 Andreas Kloeckner" __license__ = """ @@ -22,8 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six - from pymbolic.mapper import CombineMapper import numpy as np @@ -41,7 +37,7 @@ logger = logging.getLogger(__name__) def _debug(kernel, s, *args): if logger.isEnabledFor(logging.DEBUG): logstr = s % args - logger.debug("%s: %s" % (kernel.name, logstr)) + logger.debug(f"{kernel.name}: {logstr}") # {{{ type inference mapper @@ -66,7 +62,7 @@ class TypeInferenceMapper(CombineMapper): if return_tuple: kwargs["return_tuple"] = True - result = super(TypeInferenceMapper, self).__call__( + result = super().__call__( expr, **kwargs) assert isinstance(result, list) @@ -240,7 +236,7 @@ class TypeInferenceMapper(CombineMapper): def map_type_cast(self, expr): subtype, = self.rec(expr.child) if not issubclass(subtype.dtype.type, np.number): - raise LoopyError("Can't cast a '%s' to '%s'" % (subtype, expr.type)) + raise LoopyError(f"Can't cast a '{subtype}' to '{expr.type}'") return [expr.type] def map_subscript(self, expr): @@ -507,7 +503,7 @@ def infer_unknown_types(kernel, expect_completion=False): names_for_type_inference = [] import loopy as lp - for tv in six.itervalues(kernel.temporary_variables): + for tv in kernel.temporary_variables.values(): assert tv.dtype is not lp.auto if tv.dtype is None: names_for_type_inference.append(tv.name) @@ -524,13 +520,13 @@ def infer_unknown_types(kernel, expect_completion=False): writer_map = kernel.writer_map() - dep_graph = dict( - (written_var, set( + dep_graph = { + written_var: { read_var for insn_id in writer_map.get(written_var, []) for read_var in kernel.id_to_insn[insn_id].read_dependency_names() - if read_var in names_for_type_inference)) - for written_var in names_for_type_inference) + if read_var in names_for_type_inference} + for written_var in names_for_type_inference} from pytools.graph import compute_sccs diff --git a/loopy/types.py b/loopy/types.py index cf59d6cf7..de6208476 100644 --- a/loopy/types.py +++ b/loopy/types.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -22,7 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six # noqa import numpy as np from loopy.diagnostic import LoopyError @@ -40,7 +37,7 @@ __doc__ = """ """ -class LoopyType(object): +class LoopyType: """ Abstract class for dtypes of variables encountered in a :class:`loopy.LoopKernel`. @@ -156,7 +153,7 @@ class NumpyType(LoopyType): else: return any( dtype_involves_complex(f[0]) - for f in six.itervalues(dtype.fields)) + for f in dtype.fields.values()) return dtype_involves_complex(self.dtype) diff --git a/loopy/version.py b/loopy/version.py index 983dee66b..fc6408dd7 100644 --- a/loopy/version.py +++ b/loopy/version.py @@ -60,7 +60,7 @@ except ImportError: else: _cgen_version = cgen.version.VERSION_TEXT -DATA_MODEL_VERSION = "%s-islpy%s-cgen%s-%s-v1" % ( +DATA_MODEL_VERSION = "{}-islpy{}-cgen{}-{}-v1".format( VERSION_TEXT, _islpy_version, _cgen_version, _git_rev) diff --git a/proto-tests/test_fem_assembly.py b/proto-tests/test_fem_assembly.py index 18f2a5bfa..dde093d53 100644 --- a/proto-tests/test_fem_assembly.py +++ b/proto-tests/test_fem_assembly.py @@ -1,5 +1,3 @@ -from __future__ import division - import numpy as np import pyopencl as cl import loopy as lp diff --git a/proto-tests/test_sem.py b/proto-tests/test_sem.py index 4613b74ae..b84d072d0 100644 --- a/proto-tests/test_sem.py +++ b/proto-tests/test_sem.py @@ -1,5 +1,3 @@ -from __future__ import division - import numpy as np import pyopencl as cl import loopy as lp diff --git a/proto-tests/test_sem_tim.py b/proto-tests/test_sem_tim.py index 1bfb437fb..9d8dfcfa6 100644 --- a/proto-tests/test_sem_tim.py +++ b/proto-tests/test_sem_tim.py @@ -1,5 +1,3 @@ -from __future__ import division - import numpy as np import pyopencl as cl import loopy as lp diff --git a/proto-tests/test_tim.py b/proto-tests/test_tim.py index d7061933e..773821dce 100644 --- a/proto-tests/test_tim.py +++ b/proto-tests/test_tim.py @@ -1,5 +1,3 @@ -from __future__ import division - import numpy as np import pyopencl as cl import loopy as lp diff --git a/setup.py b/setup.py index 497fa60ba..bd03daac8 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- import os from setuptools import setup, find_packages @@ -34,9 +33,7 @@ def find_git_revision(tree_root): cwd=tree_root) (git_rev, _) = p.communicate() - import sys - if sys.version_info >= (3,): - git_rev = git_rev.decode() + git_rev = git_rev.decode() git_rev = git_rev.rstrip() @@ -67,7 +64,7 @@ write_git_revision("loopy") setup(name="loo.py", version=ver_dic["VERSION_TEXT"], description="A code generator for array-based code on CPUs and GPUs", - long_description=open("README.rst", "rt").read(), + long_description=open("README.rst").read(), classifiers=[ "Development Status :: 4 - Beta", "Intended Audience :: Developers", @@ -92,7 +89,6 @@ setup(name="loo.py", "genpy>=2016.1.2", "cgen>=2016.1", "islpy>=2019.1", - "six>=1.8.0", "codepy>=2017.1", "colorama", "Mako", diff --git a/test/test_apps.py b/test/test_apps.py index ed5e5ce3d..56f4127ac 100644 --- a/test/test_apps.py +++ b/test/test_apps.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ diff --git a/test/test_c_execution.py b/test/test_c_execution.py index 53fb80be7..75b457100 100644 --- a/test/test_c_execution.py +++ b/test/test_c_execution.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - __copyright__ = "Copyright (C) 2017 Nick Curtis" __license__ = """ @@ -25,7 +23,6 @@ THE SOFTWARE. import numpy as np import loopy as lp import sys -import six import pytest from loopy import CACHING_ENABLED @@ -203,13 +200,14 @@ def test_c_caching(): # ensure that codepy is correctly caching the code from loopy.target.c import ExecutableCTarget - class TestingLogger(object): + class TestingLogger: def start_capture(self, loglevel=logging.DEBUG): """ Start capturing log output to a string buffer. @param newLogLevel: Optionally change the global logging level, e.g. logging.DEBUG """ - self.buffer = six.StringIO() + from io import StringIO + self.buffer = StringIO() self.buffer.write("Log output") logger = logging.getLogger() diff --git a/test/test_dg.py b/test/test_dg.py index 94760fd6d..318b4c369 100644 --- a/test/test_dg.py +++ b/test/test_dg.py @@ -1,5 +1,3 @@ -from __future__ import division - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ diff --git a/test/test_diff.py b/test/test_diff.py index b735ab17a..8af2a2b05 100644 --- a/test/test_diff.py +++ b/test/test_diff.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - __copyright__ = "Copyright (C) 2015 Andreas Kloeckner" __license__ = """ diff --git a/test/test_domain.py b/test/test_domain.py index 7408b338b..6a0d9f255 100644 --- a/test/test_domain.py +++ b/test/test_domain.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -22,9 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six # noqa -from six.moves import range # noqa - import sys import numpy as np import loopy as lp diff --git a/test/test_expression.py b/test/test_expression.py index 50f143ff6..f0c0d3df0 100644 --- a/test/test_expression.py +++ b/test/test_expression.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - __copyright__ = "Copyright (C) 2019 Andreas Kloeckner" __license__ = """ @@ -22,9 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six -from six.moves import range - import sys import numpy as np import loopy as lp @@ -66,12 +61,12 @@ class BoundsCheckError(ValueError): class BoundsCheckingEvaluationMapper(EvaluationMapper): def __init__(self, context, lbound, ubound): - super(BoundsCheckingEvaluationMapper, self).__init__(context) + super().__init__(context) self.lbound = lbound self.ubound = ubound def rec(self, expr): - result = super(BoundsCheckingEvaluationMapper, self).rec(expr) + result = super().rec(expr) if result > self.ubound: raise BoundsCheckError() @@ -326,11 +321,11 @@ def test_fuzz_expression_code_gen(ctx_factory, expr_type, random_seed): shape=())) data.extend([ lp.TemporaryVariable(name, get_numpy_type(val)) - for name, val in six.iteritems(var_values) + for name, val in var_values.items() ]) instructions.extend([ lp.Assignment(name, get_numpy_type(val)(val)) - for name, val in six.iteritems(var_values) + for name, val in var_values.items() ]) instructions.append(lp.Assignment(var_name, expr)) @@ -350,7 +345,7 @@ def test_fuzz_expression_code_gen(ctx_factory, expr_type, random_seed): print(knl) evt, lp_values = knl(queue, out_host=True) - for name, ref_value in six.iteritems(ref_values): + for name, ref_value in ref_values.items(): lp_value = lp_values[name] if expr_type in ["real", "complex"]: err = abs(ref_value-lp_value)/abs(ref_value) @@ -365,7 +360,7 @@ def test_fuzz_expression_code_gen(ctx_factory, expr_type, random_seed): print(80*"-") print(lp.generate_code_v2(knl).device_code()) print(80*"-") - print("WRONG: %s rel error=%g" % (name, err)) + print(f"WRONG: {name} rel error={err:g}") print("reference=%r" % ref_value) print("loopy=%r" % lp_value) print(80*"-") diff --git a/test/test_fortran.py b/test/test_fortran.py index 56e88733a..ff0855fa2 100644 --- a/test/test_fortran.py +++ b/test/test_fortran.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import - __copyright__ = "Copyright (C) 2015 Andreas Kloeckner" __license__ = """ diff --git a/test/test_linalg.py b/test/test_linalg.py index 002a70586..9146e84bf 100644 --- a/test/test_linalg.py +++ b/test/test_linalg.py @@ -1,5 +1,3 @@ -from __future__ import division - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ diff --git a/test/test_loopy.py b/test/test_loopy.py index de4004823..a43bd96c4 100644 --- a/test/test_loopy.py +++ b/test/test_loopy.py @@ -1,5 +1,3 @@ -from __future__ import division, absolute_import, print_function - __copyright__ = "Copyright (C) 2012 Andreas Kloeckner" __license__ = """ @@ -22,9 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ -import six # noqa: F401 -from six.moves import range - import sys import numpy as np import loopy as lp @@ -77,7 +72,7 @@ def test_globals_decl_once_with_multi_subprogram(ctx_factory): knl = lp.split_iname(knl, "i", 2, outer_tag="g.0", inner_tag="l.0") knl = lp.split_iname(knl, "ii", 2, outer_tag="g.0", inner_tag="l.0") evt, (out,) = knl(queue, a=a) - assert np.linalg.norm(out-((2*(a+cnst)+cnst))) <= 1e-15 + assert np.linalg.norm(out-(2*(a+cnst)+cnst)) <= 1e-15 def test_complicated_subst(ctx_factory): @@ -1205,7 +1200,7 @@ def test_save_of_private_array_in_hw_loop(ctx_factory, debug=False): knl = lp.set_temporary_scope(knl, "t", "private") save_and_reload_temporaries_test( - queue, knl, np.vstack((8 * (np.arange(8),))), debug) + queue, knl, np.vstack(8 * (np.arange(8),)), debug) def test_save_of_private_multidim_array(ctx_factory, debug=False): @@ -1228,7 +1223,7 @@ def test_save_of_private_multidim_array(ctx_factory, debug=False): knl = lp.set_temporary_scope(knl, "t", "private") - result = np.array([np.vstack((8 * (np.arange(8),))) for i in range(8)]) + result = np.array([np.vstack(8 * (np.arange(8),)) for i in range(8)]) save_and_reload_temporaries_test(queue, knl, result, debug) @@ -1253,7 +1248,7 @@ def test_save_of_private_multidim_array_in_hw_loop(ctx_factory, debug=False): knl = lp.set_temporary_scope(knl, "t", "private") knl = lp.tag_inames(knl, dict(i="g.0")) - result = np.array([np.vstack((8 * (np.arange(8),))) for i in range(8)]) + result = np.array([np.vstack(8 * (np.arange(8),)) for i in range(8)]) save_and_reload_temporaries_test(queue, knl, result, debug) @@ -2135,7 +2130,7 @@ def test_integer_reduction(ctx_factory): for reduction, function, args in reductions: kstr = ("out" if "arg" not in reduction else "out[0], out[1]") - kstr += " = {0}(k, {1})".format(reduction, args) + kstr += f" = {reduction}(k, {args})" knl = lp.make_kernel("{[k]: 0<=k