How to print debug messages?

Hello! I’m trying to re-implement my javascript 3d mpm code in Taichi. At the moment i don’t get the same results, and I’m trying to find the cause for the difference. However, I can’t find a way of printing debugging messages in the code. For example, print("p2g") inside a ti.kernel throws

TypeError: make_const_expr_f32(): incompatible function arguments. The following argument types are supported:
    1. (arg0: float) -> taichi_core.Expr

Invoked with: 'p2g'.

Is there a way of doing it?

thank you!

For now, taichi doesn’t support printing strings inside a kernel. Only numbers are allowed due to hardware limitations. (sadly)
Btw, you can print debug info by setting environment variables:
TI_LOG_LEVEL=debug python your_file_name.py

2 个赞

thank you @archibate!
I found the print command reading the docs one more time :smiley: and also realised that it only works with numbers. So, for printing a vector I do several separate prints: print(x[0]); print(x[1]); etc., and for matrices print(m[0,0]), etc.
I managed to debug my code and even running on the CPU it’s impressively fast!

1 个赞

Btw, we are trying to implement print for matrix/vector/string now, see https://github.com/taichi-dev/taichi/issues/923!

1 个赞

cool! i’ll check it out. Thank you : )

News: printing a matrix or string is now supported in v0.6.6!

1 个赞

Thank you so much!! (I’m a very optimistic person: I took my first online course of Mandarin to try to understand your Taichi-con videos :star_struck:! If we meet one day, I should be able to introduce myself :stuck_out_tongue: )

1 个赞

Sounds cool! Feel free to ask help from native speakers when you need. I’m looking forward to meet you too :wink: Btw, it’s heard that @yuanming would like to held the TaichiCon 1 in English, would you like to participate? I will be there as an audience of course :smile:

I would love to participate! I’ve been using a script based on @yuanming’s 88-lines code for MPM, first transformed to 3D, and now using Taichi, for simulating brain growth and folding. I’d love to have your input!

2 个赞

你好,我使用的ide是spyder,在ti.kernel里,无论print什么,控制台都没有输出。有听到胡老师在课上说输出到了另一个地方,能说一下具体的情况吗,谢谢!

你是否在使用ti.init(ti.gpu)? 可否给出重现代码?
如果是ti.init(ti.gpu),请在内核结束后调用ti.sync():

@ti.kernel
def kern():
  print('Hello')

kern()
ti.sync()

我试了用gpu和cpu都没有输出,但kernel里的其他语句是执行了的

import taichi as ti
ti.init(arch = ti.cpu)

@ti.kernel
def myprint():
    x = 233
    print(x)  #没有输出
    print(5)  #没有输出
    print("输出") #没有输出

myprint()
#ti.sync()

使用python或Ipython,在命令行中是有输出的。但用spyder没有输出

那很可能是Taichi不兼容spyder的问题,记得群里有人提到过Jupyter也是这样。

了解!谢谢 :wink: