You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi - I have a 2D ndarray that I need to convert into a vector of pointers, which each point to a row of the ndarray. My problems lie in the fact that the ndarrays seem to only be metadata until the python runs, so don't seem to want to give me a memory address beyond the first one at ndarray.data(). I have tried using strides like this:
template <typename Scalar, typename... Other>
std::vector<void*> make_pointer_array_2d(const nb::ndarray<Scalar, Other...> &arr) {
// 1) Check that we have a 2D arrayif (arr.ndim() != 2) {
throwstd::runtime_error("make_pointer_array_2d() only handles 2D arrays!");
}
// 2) Number of rows, columnsint64_t nrows = arr.shape(0);
// 3) stride in "elements" not bytesint64_t row_stride = arr.stride(0);
// 4) The base pointer
Scalar* base_ptr = (Scalar*) arr.data(); // device or host, depending on array domain// 5) Build a vector of row pointers
std::vector<void*> pointers(nrows);
for (int64_t i = 0; i < nrows; i++) {
pointers[i] = static_cast<void*>(base_ptr + i * row_stride);
}
return pointers;
}
This seems to not work, however I am not sure why. I have also tried to use numpy indexing like ndarray(i); to get a row of a 2D matrix, but it seems I can only retrieve a single element with two indices: ndarray(i, i);
What should I do in this situation? I would rather not pass every row from python separately. Also I am very much a c++ beginner so sorry if I am making silly mistakes here!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi - I have a 2D ndarray that I need to convert into a vector of pointers, which each point to a row of the ndarray. My problems lie in the fact that the ndarrays seem to only be metadata until the python runs, so don't seem to want to give me a memory address beyond the first one at ndarray.data(). I have tried using strides like this:
This seems to not work, however I am not sure why. I have also tried to use numpy indexing like ndarray(i); to get a row of a 2D matrix, but it seems I can only retrieve a single element with two indices: ndarray(i, i);
What should I do in this situation? I would rather not pass every row from python separately. Also I am very much a c++ beginner so sorry if I am making silly mistakes here!
Beta Was this translation helpful? Give feedback.
All reactions