Skip to content

Commit

Permalink
Added a Cython extension to be built in place and used in tests to te…
Browse files Browse the repository at this point in the history
…st Cython API functions

The extension is built in temp folder.

Added cython as Cython test dependency

Pass --cov-config option to pytest with coverage.

This works around coverage config discrepancy between the main
process and a subprocess. See pytest-dev/pytest-cov#243

added _cython_api.pyx to flake exception list

Exclude dpctl/tests/* and dpctl/_version from coverage reporting

Fix loading of Cython extension _cython_api on Windows

Add test for property=int usage
  • Loading branch information
oleksandr-pavlyk committed Sep 9, 2021
1 parent 9ac1e2f commit 97c9702
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 3 deletions.
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ per-file-ignores =
dpctl/program/_program.pyx: E999, E225, E226, E227
dpctl/tensor/_usmarray.pyx: E999, E225, E226, E227
dpctl/tensor/numpy_usm_shared.py: F821
dpctl/tests/_cython_api.pyx: E999, E225, E227, E402
examples/cython/sycl_buffer/_buffer_example.pyx: E999, E225, E402
examples/cython/sycl_direct_linkage/_buffer_example.pyx: E999, E225, E402
examples/cython/usm_memory/blackscholes.pyx: E999, E225, E226, E402
2 changes: 1 addition & 1 deletion .github/workflows/generate-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
source /opt/intel/oneapi/setvars.sh
python setup.py develop --coverage=True
python -c "import dpctl; print(dpctl.__version__); dpctl.lsplatform()"
pytest -q -ra --disable-warnings --cov dpctl --cov-report term-missing --pyargs dpctl -vv
pytest -q -ra --disable-warnings --cov-config pyproject.toml --cov dpctl --cov-report term-missing --pyargs dpctl -vv
- name: Install coverall dependencies
shell: bash -l {0}
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ include dpctl/memory/_memory_api.h
include dpctl/tensor/_usmarray.h
include dpctl/tensor/_usmarray_api.h
include dpctl/tests/input_files/*
include dpctl/tests/*.pyx
1 change: 1 addition & 0 deletions conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ requirements:

test:
requires:
- cython
- pytest
- pytest-cov

Expand Down
18 changes: 18 additions & 0 deletions dpctl/tests/_cython_api.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# cython: language=c++
# cython: language_level=3

cimport dpctl as c_dpctl

import dpctl


def call_create_from_context_and_devices():
cdef c_dpctl.SyclQueue q
d = dpctl.SyclDevice()
ctx = dpctl.SyclContext(d)
# calling static method
q = c_dpctl.SyclQueue._create_from_context_and_device(
<c_dpctl.SyclContext> ctx,
<c_dpctl.SyclDevice> d
)
return q
12 changes: 12 additions & 0 deletions dpctl/tests/setup_cython_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import setuptools

import dpctl

ext = setuptools.Extension(
"_cython_api",
["_cython_api.pyx"],
include_dirs=[dpctl.get_include()],
language="c++",
)

setuptools.setup(name="_cython_api", version="0.0.0", ext_modules=[ext])
46 changes: 44 additions & 2 deletions dpctl/tests/test_sycl_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,15 @@ def test_queue_submit_barrier(valid_filter):


def test_queue__repr__():
q1 = dpctl.SyclQueue()
q1 = dpctl.SyclQueue(property=0)
r1 = q1.__repr__()
q2 = dpctl.SyclQueue(property="in_order")
r2 = q2.__repr__()
q3 = dpctl.SyclQueue(property="enable_profiling")
r3 = q3.__repr__()
q4 = dpctl.SyclQueue(property="default")
r4 = q4.__repr__()
q5 = dpctl.SyclQueue(property=["in_order", "enable_profiling"])
q5 = dpctl.SyclQueue(property=["in_order", "enable_profiling", 0])
r5 = q5.__repr__()
assert type(r1) is str
assert type(r2) is str
Expand Down Expand Up @@ -552,3 +552,45 @@ def test_queue_memops():
q.prefetch(list(), 512)
with pytest.raises(TypeError):
q.mem_advise(list(), 512, 0)


@pytest.fixture(scope="session")
def dpctl_cython_extension(tmp_path_factory):
import os.path
import shutil
import subprocess
import sys
import sysconfig

curr_dir = os.path.dirname(__file__)
dr = tmp_path_factory.mktemp("_cython_api")
for fn in ["_cython_api.pyx", "setup_cython_api.py"]:
shutil.copy(
src=os.path.join(curr_dir, fn),
dst=dr,
follow_symlinks=False,
)
res = subprocess.run(
[sys.executable, "setup_cython_api.py", "build_ext", "--inplace"],
cwd=dr,
)
if res.returncode == 0:
import glob
from importlib.util import module_from_spec, spec_from_file_location

sfx = sysconfig.get_config_vars()["EXT_SUFFIX"]
pth = glob.glob(os.path.join(dr, "_cython_api*" + sfx))
if not pth:
pytest.skip("Cython extension was not built")
spec = spec_from_file_location("_cython_api", pth[0])
builder_module = module_from_spec(spec)
spec.loader.exec_module(builder_module)
return builder_module
else:
pytest.skip("Cython extension could not be built")


def test_cython_api(dpctl_cython_extension):
q = dpctl_cython_extension.call_create_from_context_and_devices()
d = dpctl.SyclDevice()
assert q.sycl_device == d
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ omit = [
"dpctl/_version.py",
]

[tool.coverage.report]
omit = [
"dpctl/tests/*",
"dpctl/_version.py",
]

[tool.pytest.ini.options]
minversion = "6.0"
norecursedirs= [
Expand Down

0 comments on commit 97c9702

Please sign in to comment.