# 作业1

## 代码链接

Code

4 Likes

Taichi 里不同 backend 实现 `ti.random()` 的方法确实不同，但是 cpu 的运行效果应该是由bug导致的。

4 Likes

``````@ti.kernel
def evolve():
neighbors = 0
for i in range(cells.shape[0]):
for j in range(cells.shape[1]):
neighbors = int(cells[(i-1) % N, (j-1) % N]+cells[i, (j-1) % N] +
cells[(i+1) % N, (j-1) % N] + cells[(i-1) % N, j] +
cells[(i+1) % N, j] + cells[(i-1) % N, (j+1) % N] +
cells[i, (j+1) % N]+cells[(i+1) % N, (j+1) % N])
if cells[i, j] == 1:
if (neighbors < 2) or (neighbors > 3):
next_gen[i, j] = 0
else:
if neighbors == 3:
next_gen[i, j] = 1
``````

3 Likes

``````@ti.kernel
def evolve():
neighbors = 0
for i, j in cells:
neighbors = int(cells[(i-1) % N, (j-1) % N]+cells[i, (j-1) % N] +
cells[(i+1) % N, (j-1) % N] + cells[(i-1) % N, j] +
cells[(i+1) % N, j] + cells[(i-1) % N, (j+1) % N] +
cells[i, (j+1) % N]+cells[(i+1) % N, (j+1) % N])
if cells[i, j] == 1:
if (neighbors < 2) or (neighbors > 3):
cells[i, j] = 0
else:
if neighbors == 3:
cells[i, j] = 1
``````

2 Likes

``````@ti.kernel
def evolve():
for i, j in cells:
new_cells[i, j] = cells[i, j]

neighbors = 0
for i, j in cells:
neighbors = (cells[(i-1) % N, (j-1) % N]+cells[i, (j-1) % N]
+ cells[(i+1) % N, (j-1) % N] + cells[(i-1) % N, j]
+ cells[(i+1) % N, j] + cells[(i-1) % N, (j+1) % N]
+ cells[i, (j+1) % N]+cells[(i+1) % N, (j+1) % N])
if cells[i, j] == 1:
if (neighbors < 2) or (neighbors > 3):
new_cells[i, j] = 0
else:
if neighbors == 3:
new_cells[i, j] = 1

for i, j in cells:
cells[i, j] = new_cells[i, j]
``````
1 Like