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

Cannot print field in kernel #6566

Open
lucifer1004 opened this issue Nov 10, 2022 · 5 comments
Open

Cannot print field in kernel #6566

lucifer1004 opened this issue Nov 10, 2022 · 5 comments
Assignees
Labels
potential bug Something that looks like a bug but not yet confirmed

Comments

@lucifer1004
Copy link
Contributor

import taichi as ti


@ti.data_oriented
class B():
    def __init__(self):
        self.c = ti.field(dtype=float, shape=(5, 5))


@ti.data_oriented
class A():
    def __init__(self):
        self.b = B()

    @ti.kernel
    def print(self):
        print(self.b.c)


ti.init()
a = A()
a.print()

will cause the following error:

print(self.b.c)
        ^^^^^^^^^^^^^^^
Invalid constant scalar data type: <class 'taichi.lang.field.ScalarField'>

However, both this

import taichi as ti


@ti.data_oriented
class B():
    def __init__(self):
        self.c = ti.field(dtype=float, shape=(5, 5))


@ti.data_oriented
class A():
    def __init__(self):
        self.b = B()

    def print(self):
        print(self.b.c)


ti.init()
a = A()
a.print()

and this

@ti.data_oriented
class B():
    def __init__(self):
        self.c = ti.field(dtype=float, shape=(5, 5))


@ti.data_oriented
class A():
    def __init__(self):
        self.b = B()

    @ti.kernel
    def print(self):
        for i in ti.grouped(self.b.c):
            print(i)


ti.init()
a = A()
a.print()

can work.

@lucifer1004 lucifer1004 added the potential bug Something that looks like a bug but not yet confirmed label Nov 10, 2022
@taichi-gardener taichi-gardener moved this to Untriaged in Taichi Lang Nov 10, 2022
@lin-hitonami
Copy link
Contributor

If I remember correctly we don't support printing a field in a kernel...

@lucifer1004
Copy link
Contributor Author

So what is the obstacle here? I think it should be equivalent to printing each element of a field, which is valid.

@lucifer1004 lucifer1004 changed the title Cannot print field in nested data-oriented class Cannot print field in kernel Nov 10, 2022
@lucifer1004
Copy link
Contributor Author

I have confirmed that this is not related to data_oriented. The following cannot work, either.

import taichi as ti
ti.init()

a = ti.field(ti.f32, shape=(4, 4))

@ti.kernel
def f():
    print(a)

f()

@jim19930609 jim19930609 self-assigned this Nov 11, 2022
@jim19930609 jim19930609 moved this from Untriaged to Todo in Taichi Lang Nov 11, 2022
@jim19930609
Copy link
Contributor

Directly printing a field in Taichi scope hasn't been supported yet. There are some performance concerns especially in terms of sparse fields such as Pointer SNode, Dynamic SNode, etc.

However, we're considering supporting this feature in the future.

@kurt-rhee
Copy link

This also does not work for ndarrays

ti.init()
test = ti.ndarray(dtype=ti.int32, shape=4)
test[2] = 5
test_type = ti.types.ndarray(dtype=ti.int32, ndim=1)

@ti.kernel
def do_test(
    t: test_type
):
    do_test_internal(t)

@ti.func
def do_test_internal(t:test_type):
    print(t)


do_test(t=test)

[Taichi] version 1.6.0, llvm 15.0.1, commit f1c6fbbd, win, python 3.10.4
[Taichi] Starting on arch=x64
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2023.1.3\plugins\python-ce\helpers\pydev\pydevconsole.py", line 364, in runcode
    coro = func()
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2023.1.3\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2023.1.3\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:\Users\krhee\PycharmProjects\shade-engine\test_garbage.py", line 25, in <module>
    do_test(t=test)
  File "C:\Users\krhee\PycharmProjects\shade-engine\venv\lib\site-packages\taichi\lang\kernel_impl.py", line 976, in wrapped
    raise type(e)("\n" + str(e)) from None
taichi.lang.exception.TaichiTypeError: 
File "C:\Users\krhee\PycharmProjects\shade-engine\test_garbage.py", line 18, in do_test:
    do_test_internal(t)
    ^^^^^^^^^^^^^^^^^^^
File "C:\Users\krhee\PycharmProjects\shade-engine\test_garbage.py", line 22, in do_test_internal:
    print(t)
    ^^^^^^^^
Invalid constant scalar data type: <class 'taichi.lang.any_array.AnyArray'>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
potential bug Something that looks like a bug but not yet confirmed
Projects
Status: Todo
Development

No branches or pull requests

4 participants