8000 Modernize by alexfikl · Pull Request #25 · inducer/codepy · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Modernize #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.6, 3.7, 3.8]
python-version: ['3.6', 3.8, '3.9', '3.x']
steps:
- uses: actions/checkout@v2
-
Expand Down
37 changes: 20 additions & 17 deletions codepy/bpl.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"""Convenience interface for using CodePy with Boost.Python."""

from __future__ import absolute_import


class BoostPythonModule(object):
class BoostPythonModule:
def __init__(self, name="module", max_arity=None,
use_private_namespace=True):
self.name = name
Expand Down Expand Up @@ -67,8 +65,8 @@ def expose_vector_type(self, name, py_name=None):
Typedef(Value(name, "cl")),
Line(),
Statement(
"boost::python::class_<cl>(\"%s\")"
".def(codepy::no_compare_indexing_suite<cl>())" % py_name),
f'boost::python::class_<cl>("{py_name}")'
".def(codepy::no_compare_indexing_suite<cl>())"),
]))

def add_function(self, func):
Expand All @@ -80,7 +78,7 @@ def add_function(self, func):
from cgen import Statement
self.init_body.append(
Statement(
"boost::python::def(\"%s\", &%s)" % (
'boost::python::def("{}", &{})'.format(
func.fdecl.name, func.fdecl.name)))

def add_raw_function(self, func):
Expand All @@ -90,14 +88,18 @@ def add_raw_function(self, func):
self.mod_body.append(func)
from cgen import Statement
self.add_raw_function_include()
raw_function = "boost::python::raw_function(&%s)" % func.fdecl.name
raw_function = f"boost::python::raw_function(&{func.fdecl.name})"
self.init_body.append(
Statement(
"boost::python::def(\"%s\", %s)" % (
'boost::python::def("{}", {})'.format(
func.fdecl.name, raw_function)))

def add_struct(self, struct, py_name=None, py_member_name_transform=lambda x: x,
by_value_members=set()):
def add_struct(self,
struct, py_name=None, py_member_name_transform=lambda x: x,
by_value_members=None):
if by_value_members is None:
by_value_members = set()

from cgen import Block, Line, Statement, Typedef, Value

if py_name is None:
Expand All @@ -111,18 +113,19 @@ def add_struct(self, struct, py_name=None, py_member_name_transform=lambda x: x,
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))"
% (py_f_name, f.name))
".def(pyublas::by_value_rw_member"
f'("{py_f_name}", &cl::{f.name}))')
else:
member_defs.append(".def_readwrite(\"%s\", &cl::%s)"
% (py_f_name, f.name))
member_defs.append(
f'.def_readwrite("{py_f_name}", &cl::{f.name})'
)

self.init_body.append(
Block([
Typedef(Value(struct.tpname, "cl")),
Line(),
Statement(
"boost::python::class_<cl>(\"%s\")%s" % (
'boost::python:: 10000 class_<cl>("{}"){}'.format(
py_name, "".join(member_defs))),
]))

Expand All @@ -147,7 +150,7 @@ def generate(self):
body += ([Include("boost/python.hpp")]
+ self.preamble + [Line()]
+ mod_body
+ [Line(), Line("BOOST_PYTHON_MODULE(%s)" % self.name)]
+ [Line(), Line(f"BOOST_PYTHON_MODULE({self.name})")]
+ [Block(self.init_body)])

return Module(body)
Expand All @@ -165,4 +168,4 @@ def compile(self, toolchain, **kwargs):

from codepy.jit import extension_from_string
return extension_from_string(toolchain, self.name,
str(self.generate())+"\n", **kwargs)
"{}\n".format(self.generate()), **kwargs)
26 changes: 15 additions & 11 deletions codepy/cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"""Convenience interface for using CodePy with CUDA"""


class CudaModule(object):
class CudaModule:
def __init__(self, boost_module, name="module"):
"""*boost_module* is a codepy.BoostPythonModule containing host code
which calls CUDA code.
Current limitations of nvcc preclude compiling anything which
references Boost by nvcc, so the code needs to be split in two pieces:
a BoostPythonModule compiled by the host c++ compiler, as well as CUDA
a BoostPythonModule compiled by the host C++ compiler, as well as CUDA
code which is compiled by nvcc. This module allows the construction of
CUDA code to be compiled by nvcc, as well as bridges between the CUDA
code and the BoostPythonModule.
Expand All @@ -18,7 +18,7 @@ def __init__(self, boost_module, name="module"):
self.preamble = []
self.body = []
self.boost_module = boost_module
self.boost_module.add_to_preamble([cgen.Include('cuda.h')])
self.boost_module.add_to_preamble([cgen.Include("cuda.h")])

def add_to_preamble(self, pa):
self.preamble.extend(pa)
Expand All @@ -43,17 +43,21 @@ def generate(self):
module line-by-line.
"""
body = []
body += (self.preamble + [cgen.Line()]
+ self.body)
body += (self.preamble + [cgen.Line()] + self.body)
return cgen.Module(body)

def compile(self, host_toolchain, nvcc_toolchain, host_kwargs={},
nvcc_kwargs={}, **kwargs):
def compile(self, host_toolchain, nvcc_toolchain,
host_kwargs=None, nvcc_kwargs=None, **kwargs):
"""Return the extension module generated from the code described
by *self*. If necessary, build the code using *toolchain* with
:func:`codepy.jit.extension_from_string`. Any keyword arguments
accept by that latter function may be passed in *kwargs*.
"""
if host_kwargs is None:
host_kwargs = {}

if nvcc_kwargs is None:
nvcc_kwargs = {}

from codepy.libraries import add_boost_python, add_cuda
host_toolchain = host_toolchain.copy()
Expand All @@ -63,8 +67,8 @@ def compile(self, host_toolchain, nvcc_toolchain, host_kwargs={},
nvcc_toolchain = nvcc_toolchain.copy()
add_cuda(nvcc_toolchain)

host_code = str(self.boost_module.generate()) + "\n"
device_code = str(self.generate()) + "\n"
host_code = "{}\n".format(self.boost_module.generate())
device_code = "{}\n".format(self.generate())

from codepy.jit import compile_from_string
from codepy.jit import link_extension
Expand All @@ -82,12 +86,12 @@ def compile(self, host_toolchain, nvcc_toolchain, host_kwargs={},
object=True, **local_host_kwargs)
device_checksum, device_mod_name, device_object, device_compiled = \
compile_from_string(
nvcc_toolchain, 'gpu', device_code, 'gpu.cu',
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
mod_name = "codepy.temp.%s.%s.module" % (host_checksum, device_checksum)
mod_name = f"codepy.temp.{host_checksum}.{device_checksum}.module"

if host_compiled or device_compiled:
return link_extension(host_toolchain,
Expand Down
29 changes: 15 additions & 14 deletions codepy/elementwise.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Elementwise functionality."""

from __future__ import division

__copyright__ = "Copyright (C) 2009 Andreas Kloeckner"

Expand All @@ -16,19 +15,20 @@ def __init__(self, dtype, name):
self.name = name

def __repr__(self):
return "%s(%r, %s)" % (
return "{}({!r}, {})".format(
self.__class__.__name__,
self.name,
self.dtype)


class VectorArg(Argument):
def declarator(self):
return Value("numpy_array<%s >" % dtype_to_ctype(self.dtype),
self.name+"_ary")
return Value(
"numpy_array<{} >".format(dtype_to_ctype(self.dtype)),
f"{self.name}_ary")

def arg_name(self):
return self.name + "_ary"
return f"{self.name}_ary"

struct_char = "P"

Expand Down Expand Up @@ -67,14 +67,14 @@ def get_elwise_module_descriptor(arguments, operation, name="kernel"):

body = Block([
Initializer(
Value("numpy_array<%s >::iterator"
% dtype_to_ctype(varg.dtype),
Value(
"numpy_array<{} >::iterator".format(dtype_to_ctype(varg.dtype)),
varg.name),
"args.%s_ary.begin()" % varg.name)
f"args.{varg.name}_ary.begin()")
for varg in arguments if isinstance(varg, VectorArg)]
+ [
Initializer(
sarg.declarator(), "args." + sarg.name)
sarg.declarator(), f"args.{sarg.name}")
for sarg in arguments if isinstance(sarg, ScalarArg)]
)

Expand Down Expand Up @@ -163,17 +163,18 @@ def make_linear_comb_kernel_with_result_dtype(
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),
VectorArg(vector_dtypes[i], "a%d" % i))
(ScalarArg(scalar_dtypes[i], f"a{i}_fac"),
VectorArg(vector_dtypes[i], f"a{i}"))
for i in range(comp_count))),
"result[i] = " + " + ".join("a%d_fac*a%d[i]" % (i, i)
for i in range(comp_count)))
"result[i] = " + " + ".join(
f"a{i}_fac*a{i}[i]" for i in range(comp_count)
))


@memoize
def make_linear_comb_kernel(scalar_dtypes, vector_dtypes):
from pytools import common_dtype
result_dtype = common_dtype(scalar_dtypes+vector_dtypes)
result_dtype = common_dtype(scalar_dtypes + vector_dtypes)

return make_linear_comb_kernel_with_result_dtype(
result_dtype, scalar_dtypes, vector_dtypes), result_dtype
Expand Down
Loading
0