Skip to content

Commit

Permalink
[Lang] Add @python_scope decorator for selected MatrixNdarray/VectorN…
Browse files Browse the repository at this point in the history
…darray methods
  • Loading branch information
jim19930609 committed Aug 23, 2022
1 parent 1b4006f commit 2050072
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
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

0 comments on commit 2050072

Please sign in to comment.