AssertionError: All layout must be specified before the first kernel launch/data access

I have a for-loop iterating over multiple solvers, with a module of taichi inside each solver
gen() includes randomization and Solver() is a pytorch class wrapping taichi kernels

[T 01/27/20 20:21:37.296] [logging.cpp:taichi::Logger::Logger@68] Taichi core started. The started. Thread ID = 8700
[Taichi version 0.3.20, cpu only, commit 1c85d8e1]
Traceback (most recent call last):
gen(1000,16)

solver = Solver(iter_step=100, grid_size=grid_size)
File “d:\Repo\poisson_net\New folder\solver.py”, line 56, in init
self.ti_p = ti.var(dt=real, shape=(iter_step, grid_size+2), needs_grad=True)
File “D:\anaconda3\lib\site-packages\taichi\lang\impl.py”, line 209, in global_var
@layout
File “D:\anaconda3\lib\site-packages\taichi\lang\impl.py”, line 235, in layout
assert not pytaichi.materialized, “All layout must be specified before the first kernel launch / data access.”
AssertionError: All layout must be specified before the first
kernel launch / data access.

Single solver like

solver = Solver(iter_step=100, grid_size=grid_size).double()
ans= solver(u, fx, a)

works smoothly

I am wondering if I should specify anything in advance. Thank you!

The error message provides some clue:

AssertionError: All layout must be specified before the first
kernel launch / data access.

Let’s say you have two solvers, A and B. Your program is probably doing something like

A.declare_tensors()
A.kernels()
B.declare_tensors()
B.kernels()

However, in Taichi global tensor declaration (e.g. ti.layout, ti.var/Vector/Matrx(..., shape=...)) must happen before you invoke any kernels. This means the code structure should be slightly reorganized into

A.declare_tensors()
B.declare_tensors()

A.kernels()
B.kernels()

Also note that operations such as x[i, j, k] = 1 in Python scope will result in a writer/reader kernel launches, since

Data accessors in Python-scope are implemented as special Taichi kernels. For example, x[1, 2, 3] = 3 will call the writing accessor of x , and print(y[42]) will call the reading accessor of y .

I see. I’m removing the for loop.

Tyvm!