I'm trying to write a C++ extension with pybind11. The function ultimately will take several numpy-arrays as input and return several numpy-arrays as output. I have been trying to pass the target numpy-arrays directly and change the values in-place in the C++ function.
I am testing with the following code:
C++
#include "pybind11/pybind11.h"
#include "pybind11/numpy.h"
namespace py = pybind11;
int nparraytest(py::array_t<float> A, py::array_t<float> B, py::array_t<float> C, py::array_t<float> D) {
py::buffer_info Abuf = A.request();
py::buffer_info Bbuf = B.request();
py::buffer_info Cbuf = C.request();
py::buffer_info Dbuf = D.request();
int lxA = Abuf.shape[0];
double* Aptr = (double*)Abuf.ptr;
double* Bptr = (double*)Bbuf.ptr;
double* Cptr = (double*)Cbuf.ptr;
double* Dptr = (double*)Dbuf.ptr;
for (int i = 0; i < lxA; i++) {
int i30 = 3 * i;
int i31 = i30 + 1;
int i32 = i30 + 2;
//optionA
//Cptr[i30] = Aptr[i30];
//Cptr[i31] = Aptr[i31];
//Cptr[i32] = Aptr[i32];
//Option B
Cptr[i30] = Aptr[i30];
Cptr[i31] = Aptr[i31];
Cptr[i32] = Bptr[2];
Dptr[i30] = Aptr[i30];
Dptr[i31] = Aptr[i31];
Dptr[i32] = Aptr[i32];
}
return lxA;
}
PYBIND11_MODULE(SeqRT, m) {
m.def("nparraytest", &nparraytest, "Test");
}
python
import numpy as np
import SeqRT
if __name__ == '__main__':
#initialize arrays
A = np.arange(15, dtype = np.float32).reshape((5,3))
B = np.arange(15, dtype = np.float32).reshape((5,3))
C = np.zeros((5,3), dtype = np.float32)
D = np.zeros((5,3), dtype = np.float32)
lxA = SeqRT.nparraytest(A, B, C, D)
print(lxA)
print(A)
print(B)
print(C)
print(D)
Now, whether I use the code in Option A or Option B, arrays A, B and D always end up as expected, as well as array C, i.e.
[[ 0. 1. 2.]
[ 3. 4. 5.]
[ 6. 7. 8.]
[ 9. 10. 11.]
[12. 13. 14.]]
However, with option B I get this result for array C
[[ 0. 1. 2.]
[ 3. 4. 5.]
[ 6. 7. 8.]
[ 9. 4. 5.]
[12. 13. 14.]]
As you can see, the values 10. and 11. are different. In fact, with other input as array B also the values 4. and 5. can end up pretty random. Instead, I would expect this:
[[ 0. 1. 2.]
[ 3. 4. 2.]
[ 6. 7. 2.]
[ 9. 10. 2.]
[12. 13. 2.]]
I don't know what my mistake is:
- Is it due to some wrong usage of pybind/numpy arrays?
- Is it a general C++ indexing mistake?
- Furthermore, is this in-place method even good or should I create two buffer arrrays and pack them as a tuple for return?