Skip to content

[WIP] Python: in-memory version of load_chunk #914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/binding/python/RecordComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,29 @@ void init_RecordComponent(py::module &m) {
py::arg_v("offset", Offset(1, 0u), "np.zeros(Record_Component.shape)"),
py::arg_v("extent", Extent(1, -1u), "Record_Component.shape")
)
/*
.def("load_chunk", [](RecordComponent & r, py::array & a, Offset const & offset_in, Extent const & extent_in) {
// default arguments
// offset = {0u}: expand to right dim {0u, 0u, ...}
Offset offset = offset_in;
if( offset_in.size() == 1u && offset_in.at(0) == 0u && a.ndim() > 1u )
offset = Offset(a.ndim(), 0u);

// extent = {-1u}: take full size
Extent extent(a.ndim(), 1u);
if( extent_in.size() == 1u && extent_in.at(0) == -1u )
for( auto d = 0; d < a.ndim(); ++d )
extent.at(d) = a.shape()[d];
else
extent = extent_in;

load_chunk(r, a, offset, extent);
},
py::arg("array"),
py::arg_v("offset", Offset(1, 0u), "np.zeros_like(array)"),
py::arg_v("extent", Extent(1, -1u), "array.shape")
)
*/

// deprecated: pass-through C++ API
.def("store_chunk", [](RecordComponent & r, py::array & a, Offset const & offset_in, Extent const & extent_in) {
Expand Down
14 changes: 14 additions & 0 deletions test/python/unittest/API/APITest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,12 @@ def writeFromTemporaryStore(self, E_x):
[2, 0])

def loadToTemporaryStore(self, r_E_x):
if found_numpy and False:
data = np.zeros((2, 3,), dtype=np.dtype("int"))
r_E_x.load_chunk(data, [0, 0], [2, 3])
data2 = np.zeros((2, 3,), dtype=np.dtype("int"))
r_E_x.load_chunk(data2)

# not catching the return value shall not result in a use-after-free:
r_E_x.load_chunk()
# we keep a reference on the data until we are done flush()ing
Expand Down Expand Up @@ -1899,6 +1905,10 @@ def writeFromTemporary(self, ext):
self.loadToTemporaryStore(r_E_x)
gc.collect() # trigger removal of temporary data to check its copied

if found_numpy:
r_d_inmem = np.zeros((2, 3,), dtype=np.dtype("int"))
r_E_x.load_chunk(r_d_inmem)

read.flush()

if found_numpy:
Expand All @@ -1907,6 +1917,10 @@ def writeFromTemporary(self, ext):
np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
dtype=np.dtype("int"))
)
np.testing.assert_allclose(
r_d_inmem,
np.array([[1, 2, 3], [4, 5, 6]], dtype=np.dtype("int"))
)

def testWriteFromTemporary(self):
for ext in tested_file_extensions:
Expand Down