Skip to content

Commit

Permalink
Merge pull request #40 from AMYPAD/devel
Browse files Browse the repository at this point in the history
next release v4.1.0
  • Loading branch information
casperdcl authored Feb 22, 2024
2 parents 62e4994 + 2f5ef97 commit b7f57e6
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 34 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ make CUVEC_DEBUG=1 build-editable
Once installed in development/editable mode, tests may be run using:

```sh
pytest -k "not perf"
make test-cov
```

To run performance tests, build with debugging disabled (`CUVEC_DEBUG=0`), then run:

```sh
pytest -k "perf" -n=0
make test-perf
python tests/test_perf.py
```
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ CCACHE=
ifneq ($(CCACHE),)
BUILD_CMAKE_FLAGS+= -Ccmake.define.CMAKE_CXX_COMPILER_LAUNCHER=ccache
endif
.PHONY: build-editable clean deps-build deps-run build-wheel deps-docs docs docs-serve
.PHONY: build-editable test test-cov test-perf clean deps-build deps-run build-wheel deps-docs docs docs-serve
build-editable:
git diff --exit-code --quiet '*/src/**' || (echo "Uncommitted changes in */src"; exit 1)
pip install --no-build-isolation --check-build-dependencies -Cbuild-dir=build --no-deps -t . -U -v . $(BUILD_CMAKE_FLAGS)
git restore '*/src/**'
test:
test: test-cov test-perf
test-cov:
pytest -k "not perf" -n=3
test-perf:
pytest -k "perf" -n=0 --cov-append
clean:
git clean -Xdf
Expand Down
7 changes: 4 additions & 3 deletions cuvec/include/cuvec_pybind11.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ PYBIND11_MAKE_OPAQUE(NDCuVec<double>);
pybind11::class_<NDCuVec<T>>(m, PYBIND11_TOSTRING(NDCuVec_##typechar)) \
.def(pybind11::init<>()) \
.def(pybind11::init<std::vector<size_t>>()) \
.def("reshape", &NDCuVec<T>::reshape) \
.def("shape", [](const NDCuVec<T> &v) { return v.shape; }) \
.def("address", [](NDCuVec<T> &v) { return (size_t)v.vec.data(); })
.def_property( \
"shape", [](const NDCuVec<T> *v) { return &(v->shape); }, &NDCuVec<T>::reshape) \
.def_property_readonly("address", \
[](const NDCuVec<T> *v) { return (size_t)v->vec.data(); })

#endif // _CUVEC_PYBIND11_H_
6 changes: 3 additions & 3 deletions cuvec/pybind11.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ def __init__(self, typechar: str, shape: Shape, cuvec=None):

@property
def shape(self) -> tuple:
return tuple(self.cuvec.shape())
return tuple(self.cuvec.shape)

@shape.setter
def shape(self, shape: Shape):
shape = cu.Shape(shape if isinstance(shape, Sequence) else (shape,))
self.cuvec.reshape(shape)
self.cuvec.shape = shape

@property
def address(self) -> int:
return self.cuvec.address()
return self.cuvec.address


Pybind11Vector.vec_types = {np.dtype(c): partial(Pybind11Vector, c) for c in typecodes}
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ C++:
if (!dst) throw pybind11::value_error("could not allocate output");


mycudafunction(dst->vec.data(), src->vec.data(), dst_shape.data());
mycudafunction(dst->vec.data(), src.vec.data(), dst_shape.data());


return dst;
Expand Down
33 changes: 19 additions & 14 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@
import logging

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

import cuvec as cu
import cuvec.cpython as cp

try:
# `cuvec.swig` alternative to `cuvec.cpython`
# `example_swig` is defined in ../cuvec/src/example_swig/
from cuvec import example_swig # type: ignore # yapf: disable
from cuvec import swig as sw
except ImportError:
sw, example_swig = None, None # type: ignore # yapf: disable

try:
# `cuvec.pybind11` alternative to `cuvec.cpython`
# `example_pybind11` is defined in ../cuvec/src/example_pybind11/
Expand All @@ -23,6 +15,13 @@
except ImportError:
py, example_pybind11 = None, None # type: ignore # yapf: disable

try:
# `cuvec.swig` alternative to `cuvec.cpython`
# `example_swig` is defined in ../cuvec/src/example_swig/
from cuvec import example_swig # type: ignore # yapf: disable
from cuvec import swig as sw
except ImportError:
sw, example_swig = None, None # type: ignore # yapf: disable

shape = 127, 344, 344

Expand All @@ -44,6 +43,8 @@ def test_cmake_prefix():

@mark.parametrize("cu,CVector", [(py, 'Pybind11Vector'), (sw, 'SWIGVector')])
def test_CVector_strides(cu, CVector):
if cu is None:
skip("cuvec.pybind11 or cuvec.swig not available")
v = getattr(cu, CVector)('f', shape)
a = np.asarray(v)
assert a.shape == shape
Expand All @@ -52,7 +53,7 @@ def test_CVector_strides(cu, CVector):

@mark.parametrize("spec,result", [("i", np.int32), ("d", np.float64)])
@mark.parametrize("init", ["zeros", "ones"])
@mark.parametrize("cu", [cp, py, sw])
@mark.parametrize("cu", filter(None, [cp, py, sw]))
def test_create(cu, init, spec, result):
a = np.asarray(getattr(cu, init)(shape, spec))
assert a.dtype == result
Expand All @@ -64,7 +65,7 @@ def test_create(cu, init, spec, result):
assert b.dtype == a.dtype


@mark.parametrize("cu", [cp, py, sw])
@mark.parametrize("cu", filter(None, [cp, py, sw]))
def test_copy(cu):
a = np.random.random(shape)
b = np.asarray(cu.copy(a))
Expand All @@ -77,6 +78,8 @@ def test_copy(cu):
(py, "pyraw <class 'cuvec.pybind11.Pybind11Vector'>"),
(sw, "swraw <class 'cuvec.swig.SWIGVector'>")])
def test_CuVec_creation(cu, classname, caplog):
if cu is None:
skip("cuvec.pybind11 or cuvec.swig not available")
with raises(TypeError):
cu.CuVec()

Expand Down Expand Up @@ -112,7 +115,7 @@ def test_CuVec_creation(cu, classname, caplog):
assert not caplog.record_tuples


@mark.parametrize("cu", [py, sw])
@mark.parametrize("cu", filter(None, [py, sw]))
def test_asarray(cu):
v = cu.asarray(np.random.random(shape))
w = cu.CuVec(v)
Expand Down Expand Up @@ -144,7 +147,7 @@ def test_asarray(cu):
cu.asarray(s._vec.cuvec, ownership='error')


@mark.parametrize("cu", [py, sw])
@mark.parametrize("cu", filter(None, [py, sw]))
def test_resize(cu):
v = cu.asarray(np.random.random(shape))
v.resize(shape[::-1])
Expand All @@ -155,7 +158,7 @@ def test_resize(cu):
assert v._vec.shape == v.shape


@mark.parametrize("cu", [cp, py, sw])
@mark.parametrize("cu", filter(None, [cp, py, sw]))
def test_cuda_array_interface(cu):
cupy = importorskip("cupy")
from cuvec import dev_sync
Expand Down Expand Up @@ -190,6 +193,8 @@ def test_cuda_array_interface(cu):

@mark.parametrize("cu,ex", [(py, example_pybind11), (sw, example_swig)])
def test_increment(cu, ex):
if cu is None:
skip("cuvec.pybind11 or cuvec.swig not available")
a = cu.zeros((1337, 42), 'f')
assert (a == 0).all()
ex.increment2d_f(a.cuvec, a.cuvec)
Expand Down
18 changes: 9 additions & 9 deletions tests/test_perf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@
# `example_cpython` is defined in ../cuvec/src/example_cpython/
from cuvec import example_cpython # type: ignore # yapf: disable

try:
# `cuvec.swig` alternative to `cuvec.cpython`
# `example_swig` is defined in ../cuvec/src/example_swig/
from cuvec import example_swig # type: ignore # yapf: disable
from cuvec import swig as sw
except ImportError:
sw, example_swig = None, None # type: ignore # yapf: disable

try:
# `cuvec.pybind11` alternative to `cuvec.cpython`
# `example_pybind11` is defined in ../cuvec/src/example_pybind11/
Expand All @@ -25,6 +17,14 @@
except ImportError:
py, example_pybind11 = None, None # type: ignore # yapf: disable

try:
# `cuvec.swig` alternative to `cuvec.cpython`
# `example_swig` is defined in ../cuvec/src/example_swig/
from cuvec import example_swig # type: ignore # yapf: disable
from cuvec import swig as sw
except ImportError:
sw, example_swig = None, None # type: ignore # yapf: disable


def _time_overhead():
tic = time()
Expand Down Expand Up @@ -65,7 +65,7 @@ def test_inner(*args, **kwargs):
@retry_on_except()
def test_perf(cu, ex, shape=(1337, 42), quiet=False, return_time=False):
if cu is None:
skip("SWIG not available")
skip("cuvec.pybind11 or cuvec.swig not available")
retarray = getattr(cu, 'retarray', cu.asarray)
overhead = np.mean([_time_overhead() for _ in range(100)])
t = {}
Expand Down

0 comments on commit b7f57e6

Please sign in to comment.