Skip to content

Commit

Permalink
Python buffer objects can have negative strides.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cris Luengo authored and dean0x7d committed May 7, 2017
1 parent 2b941b3 commit d400f60
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 33 deletions.
10 changes: 5 additions & 5 deletions docs/advanced/pycpp/numpy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ completely avoid copy operations with Python expressions like
py::format_descriptor<float>::format(), /* Python struct-style format descriptor */
2, /* Number of dimensions */
{ m.rows(), m.cols() }, /* Buffer dimensions */
{ sizeof(float) * m.rows(), /* Strides (in bytes) for each index */
sizeof(float) }
{ (ssize_t)( sizeof(float) * m.rows() ),/* Strides (in bytes) for each index */
(ssize_t)( sizeof(float) ) }
);
});
Expand All @@ -61,7 +61,7 @@ specification.
std::string format;
int ndim;
std::vector<size_t> shape;
std::vector<size_t> strides;
std::vector<ssize_t> strides;
};
To create a C++ function that can take a Python buffer object as an argument,
Expand Down Expand Up @@ -121,8 +121,8 @@ as follows:
{ (size_t) m.rows(),
(size_t) m.cols() },
/* Strides (in bytes) for each index */
{ sizeof(Scalar) * (rowMajor ? m.cols() : 1),
sizeof(Scalar) * (rowMajor ? 1 : m.rows()) }
{ (ssize_t)( sizeof(Scalar) * (rowMajor ? m.cols() : 1) ),
(ssize_t)( sizeof(Scalar) * (rowMajor ? 1 : m.rows()) ) }
);
})
Expand Down
50 changes: 26 additions & 24 deletions include/pybind11/numpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,14 @@ class unchecked_reference {
const unsigned char *data_;
// Storing the shape & strides in local variables (i.e. these arrays) allows the compiler to
// make large performance gains on big, nested loops, but requires compile-time dimensions
conditional_t<Dynamic, const size_t *, std::array<size_t, (size_t) Dims>>
shape_, strides_;
conditional_t<Dynamic, const size_t *, std::array<size_t, (size_t) Dims>> shape_;
conditional_t<Dynamic, const ssize_t *, std::array<ssize_t, (size_t) Dims>> strides_;
const size_t dims_;

friend class pybind11::array;
// Constructor for compile-time dimensions:
template <bool Dyn = Dynamic>
unchecked_reference(const void *data, const size_t *shape, const size_t *strides, enable_if_t<!Dyn, size_t>)
unchecked_reference(const void *data, const size_t *shape, const ssize_t *strides, enable_if_t<!Dyn, size_t>)
: data_{reinterpret_cast<const unsigned char *>(data)}, dims_{Dims} {
for (size_t i = 0; i < dims_; i++) {
shape_[i] = shape[i];
Expand All @@ -281,7 +281,7 @@ class unchecked_reference {
}
// Constructor for runtime dimensions:
template <bool Dyn = Dynamic>
unchecked_reference(const void *data, const size_t *shape, const size_t *strides, enable_if_t<Dyn, size_t> dims)
unchecked_reference(const void *data, const size_t *shape, const ssize_t *strides, enable_if_t<Dyn, size_t> dims)
: data_{reinterpret_cast<const unsigned char *>(data)}, shape_{shape}, strides_{strides}, dims_{dims} {}

public:
Expand Down Expand Up @@ -573,12 +573,12 @@ class array : public buffer {
}

/// Strides of the array
const size_t* strides() const {
return reinterpret_cast<const size_t *>(detail::array_proxy(m_ptr)->strides);
const ssize_t* strides() const {
return reinterpret_cast<const ssize_t *>(detail::array_proxy(m_ptr)->strides);
}

/// Stride along a given axis
size_t strides(size_t dim) const {
ssize_t strides(size_t dim) const {
if (dim >= ndim())
fail_dim_check(dim, "invalid axis");
return strides()[dim];
Expand Down Expand Up @@ -702,9 +702,9 @@ class array : public buffer {
throw std::domain_error("array is not writeable");
}

static std::vector<Py_intptr_t> default_strides(const std::vector<Py_intptr_t>& shape, size_t itemsize) {
static std::vector<ssize_t> default_strides(const std::vector<size_t>& shape, size_t itemsize) {
auto ndim = shape.size();
std::vector<Py_intptr_t> strides(ndim);
std::vector<ssize_t> strides(ndim);
if (ndim) {
std::fill(strides.begin(), strides.end(), itemsize);
for (size_t i = 0; i < ndim - 1; i++)
Expand Down Expand Up @@ -1133,7 +1133,7 @@ array_iterator<T> array_end(const buffer_info& buffer) {

class common_iterator {
public:
using container_type = std::vector<size_t>;
using container_type = std::vector<ssize_t>;
using value_type = container_type::value_type;
using size_type = container_type::size_type;

Expand Down Expand Up @@ -1175,7 +1175,7 @@ template <size_t N> class multi_array_iterator {
for (size_t i = 0; i < shape.size(); ++i)
m_shape[i] = static_cast<container_type::value_type>(shape[i]);

container_type strides(shape.size());
std::vector<ssize_t> strides(shape.size());
for (size_t i = 0; i < N; ++i)
init_common_iterator(buffers[i], shape, m_common_iterator[i], strides);
}
Expand Down Expand Up @@ -1203,15 +1203,15 @@ template <size_t N> class multi_array_iterator {

void init_common_iterator(const buffer_info &buffer,
const std::vector<size_t> &shape,
common_iter &iterator, container_type &strides) {
common_iter &iterator, std::vector<ssize_t> &strides) {
auto buffer_shape_iter = buffer.shape.rbegin();
auto buffer_strides_iter = buffer.strides.rbegin();
auto shape_iter = shape.rbegin();
auto strides_iter = strides.rbegin();

while (buffer_shape_iter != buffer.shape.rend()) {
if (*shape_iter == *buffer_shape_iter)
*strides_iter = static_cast<size_t>(*buffer_strides_iter);
*strides_iter = *buffer_strides_iter;
else
*strides_iter = 0;

Expand Down Expand Up @@ -1283,10 +1283,11 @@ broadcast_trivial broadcast(const std::array<buffer_info, N> &buffers, size_t &n

// Check for C contiguity (but only if previous inputs were also C contiguous)
if (trivial_broadcast_c) {
size_t expect_stride = buffers[i].itemsize;
ssize_t expect_stride = static_cast<ssize_t>(buffers[i].itemsize);
auto end = buffers[i].shape.crend();
for (auto shape_iter = buffers[i].shape.crbegin(), stride_iter = buffers[i].strides.crbegin();
trivial_broadcast_c && shape_iter != end; ++shape_iter, ++stride_iter) {
auto shape_iter = buffers[i].shape.crbegin();
auto stride_iter = buffers[i].strides.crbegin();
for (; trivial_broadcast_c && shape_iter != end; ++shape_iter, ++stride_iter) {
if (expect_stride == *stride_iter)
expect_stride *= *shape_iter;
else
Expand All @@ -1296,10 +1297,11 @@ broadcast_trivial broadcast(const std::array<buffer_info, N> &buffers, size_t &n

// Check for Fortran contiguity (if previous inputs were also F contiguous)
if (trivial_broadcast_f) {
size_t expect_stride = buffers[i].itemsize;
ssize_t expect_stride = static_cast<ssize_t>(buffers[i].itemsize);
auto end = buffers[i].shape.cend();
for (auto shape_iter = buffers[i].shape.cbegin(), stride_iter = buffers[i].strides.cbegin();
trivial_broadcast_f && shape_iter != end; ++shape_iter, ++stride_iter) {
auto shape_iter = buffers[i].shape.cbegin();
auto stride_iter = buffers[i].strides.cbegin();
for (; trivial_broadcast_f && shape_iter != end; ++shape_iter, ++stride_iter) {
if (expect_stride == *stride_iter)
expect_stride *= *shape_iter;
else
Expand Down Expand Up @@ -1336,20 +1338,20 @@ struct vectorize_helper {
auto trivial = broadcast(buffers, ndim, shape);

size_t size = 1;
std::vector<size_t> strides(ndim);
std::vector<ssize_t> strides(ndim);
if (ndim > 0) {
if (trivial == broadcast_trivial::f_trivial) {
strides[0] = sizeof(Return);
strides[0] = static_cast<ssize_t>(sizeof(Return));
for (size_t i = 1; i < ndim; ++i) {
strides[i] = strides[i - 1] * shape[i - 1];
strides[i] = strides[i - 1] * static_cast<ssize_t>(shape[i - 1]);
size *= shape[i - 1];
}
size *= shape[ndim - 1];
}
else {
strides[ndim-1] = sizeof(Return);
strides[ndim-1] = static_cast<ssize_t>(sizeof(Return));
for (size_t i = ndim - 1; i > 0; --i) {
strides[i - 1] = strides[i] * shape[i];
strides[i - 1] = strides[i] * static_cast<ssize_t>(shape[i]);
size *= shape[i];
}
size *= shape[0];
Expand Down
4 changes: 2 additions & 2 deletions tests/test_buffers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ test_initializer buffers([](py::module &m) {
py::format_descriptor<float>::format(), /* Python struct-style format descriptor */
2, /* Number of dimensions */
{ m.rows(), m.cols() }, /* Buffer dimensions */
{ sizeof(float) * m.rows(), /* Strides (in bytes) for each index */
sizeof(float) }
{ static_cast<ssize_t>(sizeof(float) * m.rows()), /* Strides (in bytes) for each index */
static_cast<ssize_t>(sizeof(float)) }
);
})
;
Expand Down
3 changes: 2 additions & 1 deletion tests/test_numpy_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <pybind11/stl.h>

#include <cstdint>
#include <vector>

using arr = py::array;
using arr_t = py::array_t<uint16_t, 0>;
Expand Down Expand Up @@ -294,4 +295,4 @@ test_initializer numpy_array([](py::module &m) {
std::fill(a.mutable_data(), a.mutable_data() + a.size(), 42.);
return a;
});
});
});
4 changes: 4 additions & 0 deletions tests/test_numpy_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ def assert_references(a, b, base=None):
a2 = wrap(a1d)
assert_references(a1d, a2, a1)

a1m = a1[::-1, ::-1, ::-1]
a2 = wrap(a1m)
assert_references(a1m, a2, a1)


def test_numpy_view(capture):
from pybind11_tests.array import ArrayClass
Expand Down
2 changes: 1 addition & 1 deletion tests/test_numpy_dtypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ py::array_t<int32_t, 0> test_array_ctors(int i) {

std::vector<int32_t> data { 1, 2, 3, 4, 5, 6 };
std::vector<size_t> shape { 3, 2 };
std::vector<size_t> strides { 8, 4 };
std::vector<ssize_t> strides { 8, 4 };

auto ptr = data.data();
auto vptr = (void *) ptr;
Expand Down

0 comments on commit d400f60

Please sign in to comment.