Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions gstaichi/program/program.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ class TI_DLL_EXPORT Program {
return program_impl_.get();
}

size_t unsupported_get_num_ndarrays() const {
return ndarrays_.size();
}

// TODO(zhanlue): Move these members and corresponding interfaces to
// ProgramImpl Ideally, Program should serve as a pure interface class and all
// the implementations should fall inside ProgramImpl
Expand Down
2 changes: 2 additions & 0 deletions gstaichi/python/export_lang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ void export_lang(py::module &m) {
[](Program *program, SNode *snode, int element_ndim, int n, int m) {
return field_to_dlpack(program, snode, element_ndim, n, m);
})
.def("_unsupported_get_num_ndarrays",
&Program::unsupported_get_num_ndarrays)
Comment thread
duburcqa marked this conversation as resolved.
Outdated
.def("config", &Program::compile_config,
py::return_value_policy::reference)
.def("sync_kernel_profiler",
Expand Down
14 changes: 8 additions & 6 deletions python/gstaichi/lang/_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ def __init__(self):
# we register with runtime, in order to enable reset to work later
impl.get_runtime().ndarrays.add(self)

def __del__(self):
if impl is not None and impl.get_runtime is not None and impl.get_runtime() is not None:
arr = getattr(self, "arr")
if arr is not None:
prog = impl.get_runtime()._prog
if prog is not None:
prog.delete_ndarray(self.arr)
Comment thread
duburcqa marked this conversation as resolved.
Outdated

Comment on lines +42 to +48
Copy link

Copilot AI Nov 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overly complex 'del' method.

Suggested change
if impl is not None and impl.get_runtime is not None and impl.get_runtime() is not None:
prog = impl.get_runtime()._prog
if prog is not None:
prog.delete_ndarray(self.arr)
pass
def release(self):
if impl is not None and impl.get_runtime is not None and impl.get_runtime() is not None:
prog = impl.get_runtime()._prog
if prog is not None:
prog.delete_ndarray(self.arr)

Copilot uses AI. Check for mistakes.
def to_dlpack(self):
return impl.get_runtime().prog.ndarray_to_dlpack(self, self.arr)

Expand Down Expand Up @@ -270,12 +278,6 @@ def __init__(self, dtype, arr_shape):
self.shape = tuple(self.arr.shape)
self.element_type = dtype

def __del__(self):
if impl is not None and impl.get_runtime is not None and impl.get_runtime() is not None:
prog = impl.get_runtime()._prog
if prog is not None:
prog.delete_ndarray(self.arr)

@property
def element_shape(self):
return ()
Expand Down
11 changes: 11 additions & 0 deletions tests/python/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,3 +1199,14 @@ def my_kernel({args}) -> None:
my_kernel(*arg_objs_l)
for i in range(num_args):
assert arg_objs_l[i][0] == i + 1


@pytest.mark.parametrize("dtype", [ti.i32, ti.types.vector(3, ti.f32), ti.types.matrix(2, 2, ti.f32)])
@test_utils.test()
def test_ndarray_del(dtype) -> None:
def foo():
nd = ti.ndarray(dtype, (1000,))
assert ti.lang.impl.get_runtime().prog._unsupported_get_num_ndarrays() == 1

foo()
assert ti.lang.impl.get_runtime().prog._unsupported_get_num_ndarrays() == 0
Loading