Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 6 additions & 6 deletions python/gstaichi/lang/_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ 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:
prog = impl.get_runtime()._prog
if prog is not None:
Comment thread
duburcqa marked this conversation as resolved.
Outdated
prog.delete_ndarray(self.arr)

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 +276,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
13 changes: 13 additions & 0 deletions tests/python/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,3 +1199,16 @@ 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(ti.i32, (1000,))
Comment thread
duburcqa marked this conversation as resolved.
Outdated
assert nd.arr.shape != []
return nd.arr

arr = foo()
# after deletion, the shape is now empty, so this shows the arr was deleted
assert arr.shape == []
Comment thread
duburcqa marked this conversation as resolved.
Outdated
Comment thread
duburcqa marked this conversation as resolved.
Outdated
Loading