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

Can not using map to apply function #8480

Open
NinjaCats opened this issue Mar 3, 2024 · 1 comment
Open

Can not using map to apply function #8480

NinjaCats opened this issue Mar 3, 2024 · 1 comment

Comments

@NinjaCats
Copy link

Describe the bug
When using map to apply a function to fields does not work, list comprehension works well.

To Reproduce

# %%
import taichi as ti

ti.init(ti.gpu)

# %%
@ti.kernel
def random_fill(x:ti.template()):
    for I in ti.grouped(x):
        x[I] = ti.random()

x = ti.field(ti.f16, 4)
y = ti.field(ti.f16, (2, 4))

map(random_fill, [x, y])
print(x, y)
[random_fill(x) for x in [x, y]]
print(x, y)

Log/Screenshots

[Taichi] version 1.7.0, llvm 15.0.1, commit 2fd24490, win, python 3.10.13
[Taichi] Starting on arch=cuda
[0. 0. 0. 0.] [[0. 0. 0. 0.]
 [0. 0. 0. 0.]]
[0.0903 0.6514 0.3926 0.7803] [[0.7163  0.359   0.506   0.6934 ]
...
@Ives0721
Copy link

Ives0721 commented Mar 19, 2024

The map function in Python would only return a map object (actually a iterator). If you want to execute every object, yield every object inside iterator. The simplest way is: turn the map object into a list.

So your example code may look like this

import taichi as ti

ti.init(arch=ti.gpu)

# %%
@ti.kernel
def random_fill(x: ti.template()):
    for I in ti.grouped(x):
        x[I] = ti.random()

x = ti.field(ti.f16, 4)
y = ti.field(ti.f16, (2, 4))

_ = list(map(random_fill, [x, y]))
print(x, y)

And here's my output:

[Taichi] version 1.6.0, llvm 15.0.1, commit f1c6fbbd, win, python 3.9.16
[Taichi] Starting on arch=cuda
[0.0903 0.6514 0.3926 0.7803] [[0.7163  0.359   0.506   0.6934 ]
 [0.03262 0.3179  0.05496 0.4727 ]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Untriaged
Development

No branches or pull requests

2 participants