-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[async] Add advection benchmark (#1914)
- Loading branch information
1 parent
3aeff7c
commit 75d5ecc
Showing
10 changed files
with
184 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import taichi as ti | ||
import math | ||
|
||
from utils import benchmark_async | ||
|
||
# TODO: staggerred grid | ||
|
||
|
||
@benchmark_async | ||
def advection_2d(scale): | ||
n = 128 * 2**int((math.log(scale, 2)) // 2) | ||
x = ti.Vector.field(3, dtype=ti.f32, shape=(n, n)) | ||
new_x = ti.Vector.field(3, dtype=ti.f32, shape=(n, n)) | ||
v = ti.Vector.field(2, dtype=ti.f32, shape=(n, n)) | ||
dx = 1 / n | ||
inv_dx = 1 / dx | ||
dt = 0.01 | ||
|
||
stagger = ti.Vector([0.5, 0.5]) | ||
|
||
@ti.func | ||
def Vector2(x, y): | ||
return ti.Vector([x, y]) | ||
|
||
@ti.kernel | ||
def init(): | ||
for i, j in v: | ||
v[i, j] = ti.Vector([j / n - 0.5, 0.5 - i / n]) | ||
|
||
for i, j in ti.ndrange(n * 4, n * 4): | ||
ret = ti.taichi_logo(ti.Vector([i, j]) / (n * 4)) | ||
x[i // 4, j // 4][0] += ret / 16 | ||
x[i // 4, j // 4][1] += ret / 16 | ||
x[i // 4, j // 4][2] += ret / 16 | ||
|
||
@ti.func | ||
def vec(x, y): | ||
return ti.Vector([x, y]) | ||
|
||
@ti.func | ||
def clamp(p): | ||
for d in ti.static(range(p.n)): | ||
p[d] = min(1 - 1e-4 - dx + stagger[d] * dx, | ||
max(p[d], stagger[d] * dx)) | ||
return p | ||
|
||
@ti.func | ||
def sample_bilinear(x, p): | ||
p = clamp(p) | ||
|
||
p_grid = p * inv_dx - stagger | ||
|
||
I = ti.cast(ti.floor(p_grid), ti.i32) | ||
f = p_grid - I | ||
g = 1 - f | ||
|
||
return x[I] * (g[0] * g[1]) + x[I + vec(1, 0)] * (f[0] * g[1]) + x[ | ||
I + vec(0, 1)] * (g[0] * f[1]) + x[I + vec(1, 1)] * (f[0] * f[1]) | ||
|
||
@ti.func | ||
def velocity(p): | ||
return sample_bilinear(v, p) | ||
|
||
@ti.func | ||
def sample_min(x, p): | ||
p = clamp(p) | ||
p_grid = p * inv_dx - stagger | ||
I = ti.cast(ti.floor(p_grid), ti.i32) | ||
|
||
return min(x[I], x[I + vec(1, 0)], x[I + vec(0, 1)], x[I + vec(1, 1)]) | ||
|
||
@ti.func | ||
def sample_max(x, p): | ||
p = clamp(p) | ||
p_grid = p * inv_dx - stagger | ||
I = ti.cast(ti.floor(p_grid), ti.i32) | ||
|
||
return max(x[I], x[I + vec(1, 0)], x[I + vec(0, 1)], x[I + vec(1, 1)]) | ||
|
||
@ti.func | ||
def backtrace(I, dt): # RK3 | ||
p = (I + stagger) * dx | ||
v1 = velocity(p) | ||
p1 = p - 0.5 * dt * v1 | ||
v2 = velocity(p1) | ||
p2 = p - 0.75 * dt * v2 | ||
v3 = velocity(p2) | ||
p -= dt * (2 / 9 * v1 + 1 / 3 * v2 + 4 / 9 * v3) | ||
return p | ||
|
||
@ti.func | ||
def semi_lagrangian(x, new_x, dt): | ||
for I in ti.grouped(x): | ||
new_x[I] = sample_bilinear(x, backtrace(I, dt)) | ||
|
||
@ti.kernel | ||
def advect(): | ||
semi_lagrangian(x(0), new_x(0), dt) | ||
semi_lagrangian(x(1), new_x(1), dt) | ||
semi_lagrangian(x(2), new_x(2), dt) | ||
|
||
for I in ti.grouped(x): | ||
x[I] = new_x[I] | ||
|
||
init() | ||
|
||
def task(): | ||
for i in range(10): | ||
advect() | ||
|
||
ti.benchmark(task, repeat=100) | ||
|
||
visualize = False | ||
|
||
if visualize: | ||
gui = ti.GUI('Advection schemes', (n, n)) | ||
for i in range(10): | ||
for _ in range(10): | ||
advect() | ||
gui.set_image(x.to_numpy()) | ||
gui.show() | ||
|
||
|
||
if __name__ == '__main__': | ||
advection_2d() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import taichi as ti | ||
import functools | ||
import os | ||
|
||
|
||
def benchmark_async(func): | ||
@functools.wraps(func) | ||
def body(): | ||
for arch in [ti.cpu, ti.cuda]: | ||
for async_mode in [True, False]: | ||
os.environ['TI_CURRENT_BENCHMARK'] = func.__name__ | ||
ti.init(arch=arch, async_mode=async_mode, kernel_profiler=True) | ||
if arch == ti.cpu: | ||
scale = 2 | ||
else: | ||
# Use more data to hide compilation overhead | ||
# (since CUDA runs much faster than CPUs) | ||
scale = 64 | ||
func(scale) | ||
|
||
return body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters