From 6e6799c94440050e028d99ed3224009ce07ac8f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Kl=C3=B6ckner?= Date: Sun, 7 Jul 2019 01:08:54 +0200 Subject: [PATCH 1/2] Fix module name in Flake8 CI script --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3d3ecaf..f99e97f 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,7 +1,7 @@ Flake8: script: - curl -L -O -k https://gitlab.tiker.net/inducer/ci-support/raw/master/prepare-and-run-flake8.sh - - ". ./prepare-and-run-flake8.sh pyopencl test" + - ". ./prepare-and-run-flake8.sh codepy test" tags: - python3 except: -- GitLab From 905cb03ee3d1ddfcbf3cc44c51fd820adc296101 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Sat, 6 Jul 2019 18:13:49 -0500 Subject: [PATCH 2/2] Placate flake8 --- codepy/bpl.py | 6 ++---- codepy/cgen/__init__.py | 7 +++---- codepy/cuda.py | 20 ++++++++++---------- codepy/elementwise.py | 39 +++++++++++++-------------------------- codepy/jit.py | 2 +- codepy/toolchain.py | 4 ++-- 6 files changed, 31 insertions(+), 47 deletions(-) diff --git a/codepy/bpl.py b/codepy/bpl.py index 7885e8f..d7688c1 100644 --- a/codepy/bpl.py +++ b/codepy/bpl.py @@ -3,8 +3,6 @@ from __future__ import absolute_import - - class BoostPythonModule(object): def __init__(self, name="module", max_arity=None, use_private_namespace=True): @@ -112,7 +110,8 @@ class BoostPythonModule(object): py_f_name = py_member_name_transform(f.name) tp_lines, declarator = f.get_decl_pair() if f.name in by_value_members or tp_lines[0].startswith("numpy_"): - member_defs.append(".def(pyublas::by_value_rw_member(\"%s\", &cl::%s))" + member_defs.append( + ".def(pyublas::by_value_rw_member(\"%s\", &cl::%s))" % (py_f_name, f.name)) else: member_defs.append(".def_readwrite(\"%s\", &cl::%s)" @@ -167,4 +166,3 @@ class BoostPythonModule(object): from codepy.jit import extension_from_string return extension_from_string(toolchain, self.name, str(self.generate())+"\n", **kwargs) - diff --git a/codepy/cgen/__init__.py b/codepy/cgen/__init__.py index 51ae335..fa11c78 100644 --- a/codepy/cgen/__init__.py +++ b/codepy/cgen/__init__.py @@ -1,9 +1,8 @@ -from cgen import * -import cgen.cuda as cuda -import cgen.opencl as opencl +from cgen import * # noqa +import cgen.cuda as cuda # noqa: F401 +import cgen.opencl as opencl # noqa: F401 from warnings import warn as _warn _warn("codepy.cgen is deprecated. Use the separate 'cgen' module instead, see " "the Python package index.", DeprecationWarning, stacklevel=2) - diff --git a/codepy/cuda.py b/codepy/cuda.py index 65a90a2..b50c581 100644 --- a/codepy/cuda.py +++ b/codepy/cuda.py @@ -2,8 +2,6 @@ import cgen """Convenience interface for using CodePy with CUDA""" - - class CudaModule(object): def __init__(self, boost_module, name="module"): """*boost_module* is a codepy.BoostPythonModule containing host code @@ -68,7 +66,7 @@ class CudaModule(object): host_code = str(self.boost_module.generate()) + "\n" device_code = str(self.generate()) + "\n" - from codepy.jit import compile_from_string, extension_from_string + from codepy.jit import compile_from_string from codepy.jit import link_extension local_host_kwargs = kwargs.copy() @@ -78,12 +76,14 @@ class CudaModule(object): # Don't compile shared objects, just normal objects # (on some platforms, they're different) - host_checksum, host_mod_name, host_object, host_compiled = compile_from_string( - host_toolchain, self.boost_module.name, host_code, - object=True, **local_host_kwargs) - device_checksum, device_mod_name, device_object, device_compiled = compile_from_string( - nvcc_toolchain, 'gpu', device_code, 'gpu.cu', - object=True, **local_nvcc_kwargs) + host_checksum, host_mod_name, host_object, host_compiled = \ + compile_from_string( + host_toolchain, self.boost_module.name, host_code, + object=True, **local_host_kwargs) + device_checksum, device_mod_name, device_object, device_compiled = \ + compile_from_string( + nvcc_toolchain, 'gpu', device_code, 'gpu.cu', + object=True, **local_nvcc_kwargs) # The name of the shared lib depends on the hex checksums of both # host and device code to prevent accidentially returned a cached # module with wrong linkage @@ -102,7 +102,7 @@ class CudaModule(object): try: from imp import load_dynamic return load_dynamic(mod_name, module_path) - except: + except Exception: return link_extension(host_toolchain, [host_object, device_object], mod_name, **kwargs) diff --git a/codepy/elementwise.py b/codepy/elementwise.py index d587c96..32501bc 100644 --- a/codepy/elementwise.py +++ b/codepy/elementwise.py @@ -5,15 +5,11 @@ from __future__ import division __copyright__ = "Copyright (C) 2009 Andreas Kloeckner" - - from pytools import memoize import numpy from cgen import POD, Value, dtype_to_ctype - - class Argument: def __init__(self, dtype, name): self.dtype = numpy.dtype(dtype) @@ -25,6 +21,7 @@ class Argument: self.name, self.dtype) + class VectorArg(Argument): def declarator(self): return Value("numpy_array<%s >" % dtype_to_ctype(self.dtype), @@ -35,6 +32,7 @@ class VectorArg(Argument): struct_char = "P" + class ScalarArg(Argument): def declarator(self): return POD(self.dtype, self.name) @@ -47,8 +45,6 @@ class ScalarArg(Argument): return self.dtype.char - - def get_elwise_module_descriptor(arguments, operation, name="kernel"): from codepy.bpl import BoostPythonModule @@ -56,7 +52,7 @@ def get_elwise_module_descriptor(arguments, operation, name="kernel"): Value, POD, Struct, For, Initializer, Include, Statement, \ Line, Block - S = Statement + S = Statement # noqa: N806 mod = BoostPythonModule() mod.add_to_preamble([ @@ -76,9 +72,10 @@ def get_elwise_module_descriptor(arguments, operation, name="kernel"): varg.name), "args.%s_ary.begin()" % varg.name) for varg in arguments if isinstance(varg, VectorArg)] - +[Initializer( - sarg.declarator(), "args." + sarg.name) - for sarg in arguments if isinstance(sarg, ScalarArg)] + + [ + Initializer( + sarg.declarator(), "args." + sarg.name) + for sarg in arguments if isinstance(sarg, ScalarArg)] ) body.extend([ @@ -90,7 +87,7 @@ def get_elwise_module_descriptor(arguments, operation, name="kernel"): ) ]) - arg_struct = Struct("arg_struct", + arg_struct = Struct("arg_struct", [arg.declarator() for arg in arguments]) mod.add_struct(arg_struct, "ArgStruct") mod.add_to_module([Line()]) @@ -106,7 +103,6 @@ def get_elwise_module_descriptor(arguments, operation, name="kernel"): return mod - def get_elwise_module_binary(arguments, operation, name="kernel", toolchain=None): if toolchain is None: from codepy.toolchain import guess_toolchain @@ -122,15 +118,11 @@ def get_elwise_module_binary(arguments, operation, name="kernel", toolchain=None .compile(toolchain) - - def get_elwise_kernel(arguments, operation, name="kernel", toolchain=None): return getattr(get_elwise_module_binary( arguments, operation, name, toolchain), name) - - class ElementwiseKernel: def __init__(self, arguments, operation, name="kernel", toolchain=None): self.arguments = arguments @@ -142,11 +134,10 @@ class ElementwiseKernel: if isinstance(arg, VectorArg)] assert self.vec_arg_indices, \ - "ElementwiseKernel can only be used with functions that have at least one " \ - "vector argument" + "ElementwiseKernel can only be used with functions " \ + "that have at least one vector argument" def __call__(self, *args): - vectors = [] args = list(args) from pytools import single_valued @@ -166,23 +157,19 @@ class ElementwiseKernel: self.func(size, arg_struct) - - @memoize def make_linear_comb_kernel_with_result_dtype( result_dtype, scalar_dtypes, vector_dtypes): comp_count = len(vector_dtypes) from pytools import flatten return ElementwiseKernel([VectorArg(result_dtype, "result")] + list(flatten( - (ScalarArg(scalar_dtypes[i], "a%d_fac" % i), + (ScalarArg(scalar_dtypes[i], "a%d_fac" % i), VectorArg(vector_dtypes[i], "a%d" % i)) for i in range(comp_count))), - "result[i] = " + " + ".join("a%d_fac*a%d[i]" % (i, i) + "result[i] = " + " + ".join("a%d_fac*a%d[i]" % (i, i) for i in range(comp_count))) - - @memoize def make_linear_comb_kernel(scalar_dtypes, vector_dtypes): from pytools import common_dtype @@ -193,7 +180,7 @@ def make_linear_comb_kernel(scalar_dtypes, vector_dtypes): if __name__ == "__main__": - import pyublas + import pyublas # noqa: F401 a = numpy.random.rand(50000) b = numpy.random.rand(50000) diff --git a/codepy/jit.py b/codepy/jit.py index d434357..10048b6 100644 --- a/codepy/jit.py +++ b/codepy/jit.py @@ -442,7 +442,7 @@ def compile_from_string(toolchain, name, source_string, info_file.close() return hex_checksum, mod_name, ext_file, True - except: + except Exception: cleanup_m.error_clean_up() raise finally: diff --git a/codepy/toolchain.py b/codepy/toolchain.py index 4e23f7f..7378dcc 100644 --- a/codepy/toolchain.py +++ b/codepy/toolchain.py @@ -187,8 +187,8 @@ class GCCLikeToolchain(Toolchain): if result != 0: import sys - print("FAILED compiler invocation:" + - " ".join(cc_cmdline), file=sys.stderr) + print("FAILED compiler invocation:" + + " ".join(cc_cmdline), file=sys.stderr) raise CompileError("module compilation failed") def build_extension(self, ext_file, source_files, debug=False): -- GitLab