Skip to content
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

[Lang] Add @python_scope decorator for selected MatrixNdarray/VectorNdarray methods #5844

Merged
merged 1 commit into from
Aug 24, 2022
Merged
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
6 changes: 6 additions & 0 deletions python/taichi/lang/_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def fill(self, val):
else:
self._fill_by_kernel(val)

@python_scope
def _ndarray_to_numpy(self):
"""Converts ndarray to a numpy array.

Expand All @@ -88,6 +89,7 @@ def _ndarray_to_numpy(self):
impl.get_runtime().sync()
return arr

@python_scope
def _ndarray_matrix_to_numpy(self, layout, as_vector):
"""Converts matrix ndarray to a numpy array.

Expand All @@ -103,6 +105,7 @@ def _ndarray_matrix_to_numpy(self, layout, as_vector):
impl.get_runtime().sync()
return arr

@python_scope
def _ndarray_from_numpy(self, arr):
"""Loads all values from a numpy array.

Expand All @@ -122,6 +125,7 @@ def _ndarray_from_numpy(self, arr):
ext_arr_to_ndarray(arr, self)
impl.get_runtime().sync()

@python_scope
def _ndarray_matrix_from_numpy(self, arr, layout, as_vector):
"""Loads all values from a numpy array.

Expand Down Expand Up @@ -192,6 +196,7 @@ def _fill_by_kernel(self, val):
"""
raise NotImplementedError()

@python_scope
def _pad_key(self, key):
if key is None:
key = ()
Expand All @@ -200,6 +205,7 @@ def _pad_key(self, key):
assert len(key) == len(self.arr.total_shape())
return key

@python_scope
def _initialize_host_accessor(self):
if self.host_accessor:
return
Expand Down
6 changes: 6 additions & 0 deletions python/taichi/lang/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -1957,17 +1957,20 @@ def from_numpy(self, arr):
"""
self._ndarray_matrix_from_numpy(arr, self.layout, as_vector=0)

@python_scope
def __deepcopy__(self, memo=None):
ret_arr = MatrixNdarray(self.n, self.m, self.dtype, self.shape,
self.layout)
ret_arr.copy_from(self)
return ret_arr

@python_scope
def _fill_by_kernel(self, val):
from taichi._kernels import \
fill_ndarray_matrix # pylint: disable=C0415
fill_ndarray_matrix(self, val)

@python_scope
def __repr__(self):
return f'<{self.n}x{self.m} {self.layout} ti.Matrix.ndarray>'

Expand Down Expand Up @@ -2053,16 +2056,19 @@ def from_numpy(self, arr):
"""
self._ndarray_matrix_from_numpy(arr, self.layout, as_vector=1)

@python_scope
def __deepcopy__(self, memo=None):
ret_arr = VectorNdarray(self.n, self.dtype, self.shape, self.layout)
ret_arr.copy_from(self)
return ret_arr

@python_scope
def _fill_by_kernel(self, val):
from taichi._kernels import \
fill_ndarray_matrix # pylint: disable=C0415
fill_ndarray_matrix(self, val)

@python_scope
def __repr__(self):
return f'<{self.n} {self.layout} ti.Vector.ndarray>'

Expand Down