# 如何让kernel中的并行不反复擦写某个值？

``````from taichi.math import *
import taichi as ti
import numpy as np
ti.init(arch=ti.gpu)
index_ti_1 = ti.field(ti.i32,1)
array_ti_1 = ti.field(ti.i32,10)
index_ti_1[0] = 0
index_ti_2 = ti.field(ti.i32,1)
array_ti_2 = ti.field(ti.i32,10)
index_ti_2[0] = 0
@ti.kernel
def fill_ti_1():
for i in range(10):
array_ti_1[index_ti_1[0]] = i
index_ti_1[0] +=1
@ti.kernel
def fill_ti_2():
for i in ti.static(range(10)):
array_ti_2[index_ti_2[0]] = i
index_ti_2[0] +=1
fill_ti_1()
print(array_ti_1)
fill_ti_2()
print(array_ti_2)
``````

``````@ti.kernel
def fill_ti_3():
for i in range(10):
array_ti_1[i] = i
``````
1 Like

``````data_field数据容器
for （在另一个遍历搜索中）;
if （x满足条件）:#但不知道x的数量
把x添加到data_field中
``````

``````from taichi.math import *
import taichi as ti
import numpy as np
ti.init(arch=ti.gpu)
index_ti_1 = ti.field(ti.i32,1)
array_ti_1 = ti.field(ti.i32,10)
index_ti_1[0] = 0
index_ti_2 = ti.field(ti.i32,1)
array_ti_2 = ti.field(ti.i32,10)
index_ti_2[0] = 0
@ti.kernel
def fill_ti_1():
for i in range(10):
array_ti_1[ti.atomic_add(index_ti_1[0], 1)] = i
@ti.kernel
def fill_ti_2():
for i in ti.static(range(10)):
array_ti_2[ti.atomic_add(index_ti_2[0], 1)] = i
fill_ti_1()
print(array_ti_1)
fill_ti_2()
print(array_ti_2)
``````
3 Likes