Skip to content

Commit

Permalink
pybind11: use properties
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Feb 22, 2024
1 parent 4db216a commit b45074d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
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

0 comments on commit b45074d

Please sign in to comment.