Skip to content

Commit

Permalink
tests: unify pybind11/cpython
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Mar 13, 2024
1 parent 2d42d5e commit 160aa6c
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 71 deletions.
69 changes: 69 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import cuvec as cu
import cuvec.cpython as cp

# `example_cpython` is defined in ../cuvec/src/example_cpython/
from cuvec import example_cpython # type: ignore # yapf: disable

from . import shape

try:
Expand Down Expand Up @@ -135,3 +138,69 @@ def test_cuda_array_interface(cu):
assert ndarr.dtype == v.dtype
with raises(AttributeError):
ndarr.__cuda_array_interface__


@mark.parametrize("cu,tp", [(cp, 'PyCuVec_f'), (py, 'NDCuVec_f')])
def test_CVector_strides(cu, tp):
if cu is None:
skip("cuvec.pybind11 not available")
v = getattr(cu.cu, tp)(shape)
a = np.asarray(v)
assert a.shape == shape
assert a.strides == (512, 32, 4)


@mark.parametrize("cu", filter(None, [cp, py]))
@mark.timeout(20)
def test_asarray(cu):
v = cu.asarray(np.random.random(shape))
w = cu.CuVec(v)
assert w.cuvec == v.cuvec
assert (w == v).all()
assert np.asarray(w.cuvec).data == np.asarray(v.cuvec).data
x = cu.asarray(w.cuvec)
assert x.cuvec == v.cuvec
assert (x == v).all()
assert np.asarray(x.cuvec).data == np.asarray(v.cuvec).data
y = cu.asarray(x.tolist())
assert y.cuvec != v.cuvec
assert (y == v).all()
assert np.asarray(y.cuvec).data == np.asarray(v.cuvec).data
z = cu.asarray(v[:])
assert z.cuvec != v.cuvec
assert (z == v[:]).all()
assert np.asarray(z.cuvec).data == np.asarray(v.cuvec).data
s = cu.asarray(v[1:])
assert s.cuvec != v.cuvec
assert (s == v[1:]).all()
assert np.asarray(s.cuvec).data != np.asarray(v.cuvec).data


@mark.parametrize("cu,ex,wrap", [(cp, example_cpython, True), (py, example_pybind11, False)])
def test_increment(cu, ex, wrap):
if cu is None:
skip("cuvec.pybind11 not available")
a = cu.zeros((1337, 42), 'f')
assert (a == 0).all()
res = cu.asarray(ex.increment2d_f(a.cuvec, a.cuvec))
assert (a == 1).all()
assert (res == 1).all()

a[:] = 0
assert (a == 0).all()
assert (res == 0).all()

res = cu.asarray(ex.increment2d_f(a if wrap else a.cuvec))
assert (res == 1).all()


@mark.parametrize("cu,ex,wrap", [(cp, example_cpython, True), (py, example_pybind11, False)])
def test_increment_return(cu, ex, wrap):
if cu is None:
skip("cuvec.pybind11 not available")
a = cu.zeros((1337, 42), 'f')
assert (a == 0).all()
res = cu.asarray(ex.increment2d_f(a if wrap else a.cuvec, a if wrap else a.cuvec))
assert (a == 1).all()
del a
assert (res == 1).all()
71 changes: 5 additions & 66 deletions tests/test_cpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

import cuvec.cpython as cu
from cuvec import cuvec_cpython

from . import shape
from cuvec import example_cpython as ex # type: ignore # yapf: disable


@mark.parametrize("tp", list(cu.typecodes))
Expand All @@ -21,73 +20,13 @@ def test_PyCuVec_asarray(tp):
del a, b, v


def test_CVector_strides():
v = cuvec_cpython.PyCuVec_f(shape)
a = np.asarray(v)
assert a.shape == shape
assert a.strides == (512, 32, 4)


@mark.timeout(20)
def test_asarray():
v = cu.asarray(np.random.random(shape))
w = cu.CuVec(v)
assert w.cuvec == v.cuvec
assert (w == v).all()
assert np.asarray(w.cuvec).data == np.asarray(v.cuvec).data
x = cu.asarray(w.cuvec)
assert x.cuvec == v.cuvec
assert (x == v).all()
assert np.asarray(x.cuvec).data == np.asarray(v.cuvec).data
y = cu.asarray(x.tolist())
assert y.cuvec != v.cuvec
assert (y == v).all()
assert np.asarray(y.cuvec).data == np.asarray(v.cuvec).data
z = cu.asarray(v[:])
assert z.cuvec != v.cuvec
assert (z == v[:]).all()
assert np.asarray(z.cuvec).data == np.asarray(v.cuvec).data
s = cu.asarray(v[1:])
assert s.cuvec != v.cuvec
assert (s == v[1:]).all()
assert np.asarray(s.cuvec).data != np.asarray(v.cuvec).data


def test_increment():
# `example_cpython` is defined in ../cuvec/src/example_cpython/
from cuvec.example_cpython import increment2d_f
a = cu.zeros((1337, 42), 'f')
assert (a == 0).all()
res = cu.asarray(increment2d_f(a.cuvec, a.cuvec))
assert (a == 1).all()
assert (res == 1).all()

a[:] = 0
assert (a == 0).all()
assert (res == 0).all()

res = cu.asarray(increment2d_f(a))
assert (res == 1).all()


def test_increment_return():
from cuvec.example_cpython import increment2d_f
a = cu.zeros((1337, 42), 'f')
assert (a == 0).all()
res = cu.asarray(increment2d_f(a, a))
assert (a == 1).all()
del a
assert (res == 1).all()


def test_np_types():
from cuvec.example_cpython import increment2d_f
f = cu.zeros((1337, 42), 'f')
d = cu.zeros((1337, 42), 'd')
cu.asarray(increment2d_f(f))
cu.asarray(increment2d_f(f, f))
cu.asarray(ex.increment2d_f(f))
cu.asarray(ex.increment2d_f(f, f))
with raises(TypeError):
cu.asarray(increment2d_f(d))
cu.asarray(ex.increment2d_f(d))
with raises(SystemError):
# the TypeError is suppressed since a new output is generated
cu.asarray(increment2d_f(f, d))
cu.asarray(ex.increment2d_f(f, d))
25 changes: 20 additions & 5 deletions tests/test_pybind11.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import re

import numpy as np
from pytest import importorskip, mark
from pytest import importorskip, mark, raises

cu = importorskip("cuvec.pybind11")
cuvec_pybind11 = importorskip("cuvec.cuvec_pybind11")
ex = importorskip("cuvec.example_pybind11")


@mark.parametrize("tp", list(cu.typecodes))
def test_Pybind11Vector_asarray(tp):
v = cu.Pybind11Vector(tp, (1, 2, 3))
assert repr(v) == f"Pybind11Vector('{tp}', (1, 2, 3))"
def test_NDCuVec_asarray(tp):
v = getattr(cuvec_pybind11, f"NDCuVec_{tp}")((1, 2, 3))
assert re.match(f"<cuvec.cuvec_pybind11.NDCuVec_{tp} object at 0x[0-9a-f]+>", str(v))
a = np.asarray(v)
assert not a.any()
a[0, 0] = 42
b = np.asarray(v)
assert (b[0, 0] == 42).all()
assert not b[1:, 1:].any()
assert a.dtype == np.dtype(tp)
assert a.dtype.char == tp
del a, b, v


def test_np_types():
f = cu.zeros((1337, 42), 'f')
d = cu.zeros((1337, 42), 'd')
cu.asarray(ex.increment2d_f(f.cuvec))
cu.asarray(ex.increment2d_f(f.cuvec, f.cuvec))
with raises(TypeError):
cu.asarray(ex.increment2d_f(d.cuvec))
with raises(TypeError):
cu.asarray(ex.increment2d_f(f.cuvec, d.cuvec))

0 comments on commit 160aa6c

Please sign in to comment.