Example #1
0
 def func():
     ti.loop_config(parallelize=1)
     for i in range(2):
         t = 0.0
         if x[i] > 0:
             t = 1 / x[i]
         y[i] = t
Example #2
0
 def foo():
     ti.loop_config(block_dim=32)
     for i in range(32):
         a[i] = i
     ti.simt.warp.sync(ti.u32(0xFFFFFFFF))
     for i in range(16):
         a[i] = a[i + 16]
Example #3
0
 def foo():
     ti.loop_config(block_dim=32)
     for i in range(32):
         for j in range(5):
             offset = 1 << j
             a[i] += ti.simt.warp.shfl_xor_i32(ti.u32(0xFFFFFFFF), a[i],
                                               offset)
Example #4
0
    def foo():
        ti.loop_config(block_dim=32)
        for i in range(16):
            a[i] = 0
            a[i + 16] = 1

        for i in range(32):
            b[i] = ti.simt.warp.match_any(ti.u32(0xFFFFFFFF), a[i])
Example #5
0
    def func():
        ti.loop_config(serialize=True)
        for i in range(n):
            for j in range(n):
                ti.append(x.parent(), j, i)

        for i in range(n):
            l[i] = ti.length(x.parent(), i)
Example #6
0
 def foo():
     ti.loop_config(block_dim=N)
     for i in range(N):
         # Make the 0-th thread runs slower intentionally
         for j in range(N - i):
             a[i] = j
         ti.simt.block.sync()
         if i > 0:
             a[i] = a[0]
    def foo() -> ti.i32:
        a = 0
        ti.loop_config(serialize=True)
        for i in range(100):
            a = a + 1
            if a == 50:
                break

        return a
Example #8
0
    def foo():
        ti.loop_config(block_dim=32)
        for i in range(32):
            a[i] = 1
        for i in range(32):
            b[i] = ti.simt.warp.match_all(ti.u32(0xFFFFFFFF), a[i])

        a[0] = 2
        for i in range(32):
            c[i] = ti.simt.warp.match_all(ti.u32(0xFFFFFFFF), a[i])
Example #9
0
    def foo():

        block_counter = 0
        ti.loop_config(block_dim=BLOCK_SIZE)
        for i in range(N):

            a[i] = 1
            ti.simt.grid.memfence()

            # Execute a prefix sum after all blocks finish
            actual_order_of_block = ti.atomic_add(block_counter, 1)
            if actual_order_of_block == N - 1:
                for j in range(1, N):
                    a[j] += a[j - 1]
 def evolve_vectorized(x: ti.template(), y: ti.template()):
     ti.loop_config(bit_vectorize=True)
     for i, j in x:
         num_active_neighbors = 0
         num_active_neighbors += ti.cast(x[i - 1, j - 1], ti.u32)
         num_active_neighbors += ti.cast(x[i - 1, j], ti.u32)
         num_active_neighbors += ti.cast(x[i - 1, j + 1], ti.u32)
         num_active_neighbors += ti.cast(x[i, j - 1], ti.u32)
         num_active_neighbors += ti.cast(x[i, j + 1], ti.u32)
         num_active_neighbors += ti.cast(x[i + 1, j - 1], ti.u32)
         num_active_neighbors += ti.cast(x[i + 1, j], ti.u32)
         num_active_neighbors += ti.cast(x[i + 1, j + 1], ti.u32)
         y[i, j] = (num_active_neighbors == 3) | \
                   ((num_active_neighbors == 2) & (x[i, j] == 1))
Example #11
0
def substep(g_x: float, g_y: float, g_z: float):
    for I in ti.grouped(F_grid_m):
        F_grid_v[I] = ti.zero(F_grid_v[I])
        F_grid_m[I] = 0
    ti.loop_config(block_dim=n_grid)
    for p in F_x:
        if F_used[p] == 0:
            continue
        Xp = F_x[p] / dx
        base = int(Xp - 0.5)
        fx = Xp - base
        w = [0.5 * (1.5 - fx)**2, 0.75 - (fx - 1)**2, 0.5 * (fx - 0.5)**2]

        F_dg[p] = (ti.Matrix.identity(float, 3) +
                   dt * F_C[p]) @ F_dg[p]  # deformation gradient update
        # Hardening coefficient: snow gets harder when compressed
        h = ti.exp(10 * (1.0 - F_Jp[p]))
        if F_materials[p] == JELLY:  # jelly, make it softer
            h = 0.3
        mu, la = mu_0 * h, lambda_0 * h
        if F_materials[p] == WATER:  # liquid
            mu = 0.0

        U, sig, V = ti.svd(F_dg[p])
        J = 1.0
        for d in ti.static(range(3)):
            new_sig = sig[d, d]
            if F_materials[p] == SNOW:  # Snow
                new_sig = min(max(sig[d, d], 1 - 2.5e-2),
                              1 + 4.5e-3)  # Plasticity
            F_Jp[p] *= sig[d, d] / new_sig
            sig[d, d] = new_sig
            J *= new_sig
        if F_materials[p] == WATER:
            # Reset deformation gradient to avoid numerical instability
            new_F = ti.Matrix.identity(float, 3)
            new_F[0, 0] = J
            F_dg[p] = new_F
        elif F_materials[p] == SNOW:
            # Reconstruct elastic deformation gradient after plasticity
            F_dg[p] = U @ sig @ V.transpose()
        stress = 2 * mu * (F_dg[p] - U @ V.transpose()) @ F_dg[p].transpose(
        ) + ti.Matrix.identity(float, 3) * la * J * (J - 1)
        stress = (-dt * p_vol * 4) * stress / dx**2
        affine = stress + p_mass * F_C[p]

        for offset in ti.static(ti.grouped(ti.ndrange(*neighbour))):
            dpos = (offset - fx) * dx
            weight = 1.0
            for i in ti.static(range(dim)):
                weight *= w[offset[i]][i]
            F_grid_v[base +
                     offset] += weight * (p_mass * F_v[p] + affine @ dpos)
            F_grid_m[base + offset] += weight * p_mass
    for I in ti.grouped(F_grid_m):
        if F_grid_m[I] > 0:
            F_grid_v[I] /= F_grid_m[I]
        F_grid_v[I] += dt * ti.Vector([g_x, g_y, g_z])
        cond = (I < bound) & (F_grid_v[I] < 0) | \
               (I > n_grid - bound) & (F_grid_v[I] > 0)
        F_grid_v[I] = 0 if cond else F_grid_v[I]
    ti.loop_config(block_dim=n_grid)
    for p in F_x:
        if F_used[p] == 0:
            continue
        Xp = F_x[p] / dx
        base = int(Xp - 0.5)
        fx = Xp - base
        w = [0.5 * (1.5 - fx)**2, 0.75 - (fx - 1)**2, 0.5 * (fx - 0.5)**2]
        new_v = ti.zero(F_v[p])
        new_C = ti.zero(F_C[p])
        for offset in ti.static(ti.grouped(ti.ndrange(*neighbour))):
            dpos = (offset - fx) * dx
            weight = 1.0
            for i in ti.static(range(dim)):
                weight *= w[offset[i]][i]
            g_v = F_grid_v[base + offset]
            new_v += weight * g_v
            new_C += 4 * weight * g_v.outer_product(dpos) / dx**2
        F_v[p] = new_v
        F_x[p] += dt * F_v[p]
        F_C[p] = new_C
Example #12
0
 def build_pid(self):
     ti.loop_config(block_dim=256)
     for p in self.x:
         base = ti.floor(self.x[p] * self.inv_dx - 0.5).cast(int) + 1
         ti.append(self.pid.parent(), base, p)
 def assign_vectorized(dx: ti.template(), dy: ti.template()):
     ti.loop_config(bit_vectorize=True)
     for i, j in x:
         y[i, j] = x[i + dx, j + dy]
         z[i, j] = x[i + dx, j + dy]
 def fill():
     ti.loop_config(block_dim_adaptive=False)
     for i in range(n):
         val[i] = i
Example #15
0
 def test() -> ti.i32:
     hit_time = 1
     ti.loop_config(block_dim=8)
     for i in range(8):
         ti.atomic_min(hit_time, 1)
     return hit_time
 def fill():
     ti.loop_config(parallelize=8, block_dim=8)
     for i in range(n):
         val[i] = i
Example #17
0
 def foo():
     ti.loop_config(block_dim=16)
     for i in range(32):
         a[i] = ti.simt.warp.active_mask()
Example #18
0
 def foo():
     ti.loop_config(block_dim=32)
     for i in range(32):
         a[i] = ti.simt.warp.shfl_sync_f32(ti.u32(0xFFFFFFFF), a[i], 0)
Example #19
0
 def foo():
     ti.loop_config(block_dim=32)
     for i in range(32):
         a[i] = ti.simt.warp.shfl_down_i32(ti.u32(0xFFFFFFFF), b[i], 1)
Example #20
0
 def foo():
     ti.loop_config(block_dim=32)
     for i in range(32):
         a[i] = ti.simt.warp.ballot(b[i])
Example #21
0
 def func():
     ti.loop_config(serialize=True)
     for i in range(n // 4):
         x[i * 4][1, 0] = i
 def assign_vectorized():
     ti.loop_config(bit_vectorize=True)
     for i, j in x:
         y[i, j] = x[i, j]
Example #23
0
 def foo():
     ti.loop_config(block_dim=32)
     for i in range(32):
         a[i] = ti.simt.warp.any_nonzero(ti.u32(0xFFFFFFFF), b[i])
Example #24
0
 def check():
     ti.loop_config(block_dim=32)
     for i in range(32):
         a[i] = ti.simt.warp.unique(ti.u32(0xFFFFFFFF), b[i])