taichi icon indicating copy to clipboard operation
taichi copied to clipboard

How to do atomic operations in parallel

Open evilzhou opened this issue 4 years ago • 1 comments

ti.atomic_xxx() does not behave as expected when computing in parallel

for example, I want to use the following function to find the mesh bounding box:

    @ti.kernel
    def _bounds(self):
        bounds_min = ti.math.vec3(1.0, 1.0, 1.0)
        bounds_max = ti.math.vec3(0.0, 0.0, 0.0)
        for n in self.indices:
            p0 = self.vertices[self.indices[n][0]]
            p1 = self.vertices[self.indices[n][1]]
            p2 = self.vertices[self.indices[n][2]]
            bounds_min_triangle = ti.math.vec3(min(p0[0], p1[0], p2[0]), min(p0[1], p1[1], p2[1]), min(p0[2], p1[2], p2[2]))
            bounds_max_triangle = ti.math.vec3(max(p0[0], p1[0], p2[0]), max(p0[1], p1[1], p2[1]), max(p0[2], p1[2], p2[2]))
            ti.atomic_min(bounds_min, bounds_min_triangle)
            ti.atomic_max(bounds_max, bounds_max_triangle)
        print(bounds_min, bounds_max)

evilzhou avatar Oct 21 '22 02:10 evilzhou

    @ti.kernel
    def _bounds(self, bounds: ti.template()):
        for n in self.indices:
            p0 = self.vertices[self.indices[n][0]]
            p1 = self.vertices[self.indices[n][1]]
            p2 = self.vertices[self.indices[n][2]]
            bounds_min_triangle = ti.math.vec3(min(p0[0], p1[0], p2[0]), min(p0[1], p1[1], p2[1]), min(p0[2], p1[2], p2[2]))
            bounds_max_triangle = ti.math.vec3(max(p0[0], p1[0], p2[0]), max(p0[1], p1[1], p2[1]), max(p0[2], p1[2], p2[2]))
            ti.atomic_min(bounds[0], bounds_min_triangle)
            ti.atomic_max(bounds[1], bounds_max_triangle)

    def bounds(self):
        bounds = ti.Vector.field(3, dtype=ti.cfg.default_fp, shape=2)
        bounds[0] = ti.math.vec3(1.0, 1.0, 1.0)
        bounds[1] = ti.math.vec3(0.0, 0.0, 0.0)
        self._bounds(bounds)
        print(bounds)
        print("---------------")

evilzhou avatar Oct 21 '22 03:10 evilzhou