Skip to content

Commit

Permalink
[Lang] Add cast to field.fill() interface (taichi-dev#7899)
Browse files Browse the repository at this point in the history
Issue: #taichi-dev#7678

There's one potential hazard: if the user mistakenly filled a ti.field
with out-of-scope values (like filling a ti.field(ti.u8) with > 255),
it's just gonna overflow silently.
  • Loading branch information
jim19930609 authored and quadpixels committed May 13, 2023
1 parent c79e8dc commit 630922a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions python/taichi/_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@

# A set of helper (meta)functions
@kernel
def fill_tensor(tensor: template(), val: template()):
for I in grouped(tensor):
tensor[I] = val
def fill_field(field: template(), val: template()):
value = ops.cast(val, field.dtype)
for I in grouped(field):
field[I] = value


@kernel
Expand Down
4 changes: 2 additions & 2 deletions python/taichi/lang/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ def __init__(self, var):
def fill(self, val):
"""Fills this scalar field with a specified value."""
if in_python_scope():
from taichi._kernels import fill_tensor # pylint: disable=C0415
from taichi._kernels import fill_field # pylint: disable=C0415

fill_tensor(self, val)
fill_field(self, val)
else:
from taichi._funcs import field_fill_taichi_scope # pylint: disable=C0415

Expand Down

0 comments on commit 630922a

Please sign in to comment.