Skip to content
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
27 changes: 24 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8, '3.10', '3.x']
python-version: ['3.10', '3.12', '3.x']
steps:
- uses: actions/checkout@v4
-
Expand All @@ -35,8 +35,29 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: "Main Script"
run: |
curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/build-and-test-py-project.sh
. ./build-and-test-py-project.sh
curl -L -O https://tiker.net/ci-support-v0
. ./ci-support-v0

build_py_project_in_venv
test_py_project

mypy:
name: Mypy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
-
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: "Main Script"
run: |
EXTRA_INSTALL="mypy pytest"
curl -L -O https://tiker.net/ci-support-v0
. ./ci-support-v0

build_py_project_in_venv
python -m mypy codepy test

docs:
name: Documentation
Expand Down
10 changes: 0 additions & 10 deletions MANIFEST.in

This file was deleted.

28 changes: 3 additions & 25 deletions codepy/elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ def __call__(self, *args):

from pytools import single_valued
size = single_valued(args[i].size for i in self.vec_arg_indices
if not (isinstance(args[i], (int, float)) and args[i] == 0))
if not (isinstance(args[i], int | float) and args[i] == 0))
for i in self.vec_arg_indices:
if isinstance(args[i], (int, float)) and args[i] == 0:
if isinstance(args[i], int | float) and args[i] == 0:
args[i] = numpy.zeros(size, dtype=self.arguments[i].dtype)

# no need to do type checking--pyublas does that for us
arg_struct = self.module.ArgStruct()
for arg_descr, arg in zip(self.arguments, args):
for arg_descr, arg in zip(self.arguments, args, strict=True):
setattr(arg_struct, arg_descr.arg_name(), arg)

assert not arg_struct.__dict__
Expand Down Expand Up @@ -194,25 +194,3 @@ def make_linear_comb_kernel(scalar_dtypes, vector_dtypes):

return make_linear_comb_kernel_with_result_dtype(
result_dtype, scalar_dtypes, vector_dtypes), result_dtype


if __name__ == "__main__":
import pyublas # noqa: F401

a = numpy.random.rand(50000)
b = numpy.random.rand(50000)

dtype = a.dtype

lin_comb = ElementwiseKernel([
ScalarArg(dtype, "a_fac"), VectorArg(dtype, "a"),
ScalarArg(dtype, "b_fac"), VectorArg(dtype, "b"),
VectorArg(dtype, "c"),
],
"c[i] = a_fac*a[i] + b_fac*b[i]")

c = numpy.empty_like(a)
lin_comb(5, a, 6, b, c)

import numpy.linalg as la
print(la.norm(c - (5*a+6*b)))
32 changes: 10 additions & 22 deletions codepy/jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import logging
from dataclasses import dataclass
from typing import List, NamedTuple
from typing import NamedTuple

from codepy import CompileError

Expand Down Expand Up @@ -223,7 +223,7 @@ class _Dependency(NamedTuple):

@dataclass(frozen=True)
class _SourceInfo:
dependencies: List[NamedTuple]
dependencies: list[_Dependency]
source_name: str


Expand Down Expand Up @@ -286,14 +286,12 @@ def compile_from_string(toolchain, name, source_string,
import os

if cache_dir is None:
try:
import platformdirs as appdirs
except ImportError:
import appdirs

import sys

import platformdirs

cache_dir = os.path.join(
appdirs.user_cache_dir("codepy", "codepy"),
platformdirs.user_cache_dir("codepy", "codepy"),
"codepy-compiler-cache-v5-py{}".format(
".".join(str(i) for i in sys.version_info)))

Expand All @@ -305,13 +303,8 @@ def compile_from_string(toolchain, name, source_string,
raise

def get_file_md5sum(fname):
try:
import hashlib
checksum = hashlib.md5()
except ImportError:
# for Python << 2.5
import md5
checksum = md5.new()
import hashlib
checksum = hashlib.md5()

inf = open(fname, "rb")
checksum.update(inf.read())
Expand All @@ -331,13 +324,8 @@ def write_source(name):
outf.close()

def calculate_hex_checksum():
try:
import hashlib
checksum = hashlib.md5()
except ImportError:
# for Python << 2.5
import md5
checksum = md5.new()
import hashlib
checksum = hashlib.md5()

for source in source_string:
if source_is_binary:
Expand Down
Empty file added codepy/py.typed
Empty file.
46 changes: 24 additions & 22 deletions codepy/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

from abc import ABC, abstractmethod
from dataclasses import dataclass, replace
from typing import AbstractSet, Any, List
from typing import Any

from codepy import CompileError

Expand All @@ -35,14 +35,14 @@
class Toolchain(ABC):
"""Abstract base class for tools used to link dynamic Python modules."""

#: A list of directories where libraries are found.
library_dirs: List[str]
#: A list of libraries used.
libraries: List[str]
#: A list of directories from which to include header files.
include_dirs: List[str]
library_dirs: list[str]
"""A list of directories where libraries are found."""
libraries: list[str]
"""A list of libraries used."""
include_dirs: list[str]
"""A list of directories from which to include header files."""

features: AbstractSet[str]
features: set[str]

def copy(self, **kwargs: Any) -> "Toolchain":
from warnings import warn
Expand Down Expand Up @@ -89,7 +89,7 @@ def add_library(self, feature, include_dirs, library_dirs, libraries):
if ldir not in self.library_dirs:
self.library_dirs.append(ldir)

self.libraries = libraries + self.libraries
object.__setattr__(self, "libraries", libraries + self.libraries)

@abstractmethod
def get_dependencies(self, source_files):
Expand Down Expand Up @@ -139,25 +139,25 @@ def with_optimization_level(self, level, **extra):

@dataclass(frozen=True)
class GCCLikeToolchain(Toolchain):
#: Path to the C compiler.
cc: str
#: Path to linker.
"""Path to the compiler executable."""
ld: str
"""Path to linker executable."""

#: A list of flags to pass to the C compiler.
cflags: List[str]
#: A list of linker flags.
ldflags: List[str]
cflags: list[str]
"""A list of flags to pass to the compiler."""
ldflags: list[str]
"""A list of linker flags."""

#: A list of defines to pass to the C compiler.
defines: List[str]
# A list of variables to undefine.
undefines: List[str]
defines: list[str]
"""A list of defines to pass to the compiler."""
undefines: list[str]
"""A list of variables to undefine."""

#: Extension for shared library generated by the compiler.
so_ext: str
#: Extension of the object file generated by the compiler.
"""Extension for shared library generated by the compiler."""
o_ext: str
"""Extension of the object file generated by the compiler."""

def _cmdline(self, source_files, object=False):
raise NotImplementedError
Expand All @@ -169,7 +169,9 @@ def get_version(self):
return stdout

def enable_debugging(self):
self.cflags = [f for f in self.cflags if not f.startswith("-O")] + ["-g"]
object.__setattr__(
self, "cflags",
[f for f in self.cflags if not f.startswith("-O")] + ["-g"])

def get_dependencies(self, source_files):
from codepy.tools import join_continued_lines
Expand Down
3 changes: 3 additions & 0 deletions codepy/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def load_dynamic(name: str, path: str):
from importlib.util import module_from_spec, spec_from_loader

spec = spec_from_loader(name, loader)
if spec is None:
raise RuntimeError(f"Could not load module '{name}' in '{path}'")

module = module_from_spec(spec)

# The module is always executed and not cached in sys.modules.
Expand Down
2 changes: 1 addition & 1 deletion doc/upload-docs.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#! /bin/sh

rsync --verbose --archive --delete _build/html/* doc-upload:doc/codepy
rsync --verbose --archive --delete _build/html/ doc-upload:doc/codepy
2 changes: 1 addition & 1 deletion examples/nvcc-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
b = module.adjacentDifference(a).get()

golden = [constant_value] + [0] * (length - 1)
difference = [(x-y)*(x-y) for x, y in zip(b, golden)]
difference = [(x-y)*(x-y) for x, y in zip(b, golden, strict=True)]
error = sum(difference)
if error == 0:
print("Test passed!")
Expand Down
32 changes: 15 additions & 17 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
[build-system]
build-backend = "setuptools.build_meta"
requires = [
"setuptools>=63",
]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "codepy"
version = "2019.1"
description = "Generate and execute native code at run time."
version = "2024.1"
description = "Generate and execute native code at run time"
readme = "README.rst"
license = { text = "MIT" }
authors = [
{ name = "Andreas Kloeckner", email = "inform@tiker.net" },
]
requires-python = ">=3.8"
requires-python = ">=3.10"
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
Expand Down Expand Up @@ -43,16 +41,6 @@ test = [
Documentation = "https://documen.tician.de/codepy"
Homepage = "https://github.com/inducer/codepy"

[tool.setuptools.packages.find]
include = [
"codepy*",
]

[tool.setuptools.package-data]
codepy = [
"include/codepy/bpl.hpp",
]

[tool.ruff]
preview = true

Expand Down Expand Up @@ -90,3 +78,13 @@ combine-as-imports = true
known-first-party = [ "cgen", "pytools" ]
known-local-folder = [ "codepy" ]
lines-after-imports = 2

[tool.mypy]
python_version = "3.10"
warn_unused_ignores = true

[[tool.mypy.overrides]]
module = [
"cgen.*",
]
ignore_missing_imports = true
Loading