Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Example] Refactor example library #2475

Merged
merged 20 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jobs:
export PATH=$TAICHI_REPO_DIR/bin:$PATH
export PATH=$TAICHI_REPO_DIR/taichi-llvm/bin/:$PATH
export PYTHONPATH=$TAICHI_REPO_DIR/python
python examples/laplace.py
python examples/algorithm/laplace.py
ti diagnose
./build/taichi_cpp_tests
ti test -vr2 -t2
Expand Down Expand Up @@ -139,7 +139,7 @@ jobs:
export PATH=$TAICHI_REPO_DIR/bin:$PATH
export PATH=$TAICHI_REPO_DIR/taichi-llvm/bin/:$PATH
export PYTHONPATH=$TAICHI_REPO_DIR/python
python examples/laplace.py
python examples/algorithm/laplace.py
ti diagnose
[ "$RUN_CPP_TESTS" = "ON" ] && ./build/taichi_cpp_tests
ti test -vr2 -t2
Expand Down Expand Up @@ -173,7 +173,7 @@ jobs:
export PYTHONPATH=$TAICHI_REPO_DIR/python
export DISPLAY=:1
glewinfo
$PYTHON examples/laplace.py
$PYTHON examples/algorithm/laplace.py
ti diagnose
ti test -vr2 -t2

Expand Down Expand Up @@ -230,7 +230,7 @@ jobs:
$env:PATH += ";C:\taichi_clang\bin"
$env:PATH += ";$env:TAICHI_REPO_DIR\bin"
python -c "import taichi"
python examples/laplace.py
python examples/algorithm/laplace.py
python bin/taichi diagnose
python bin/taichi test -Cvr2 -t2
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
export PATH=$TAICHI_REPO_DIR/bin:$PATH
export PATH=$TAICHI_REPO_DIR/taichi-llvm/bin/:$PATH
export PYTHONPATH=$TAICHI_REPO_DIR/python
python examples/laplace.py
python examples/algorithm/laplace.py
ti diagnose
ti test -vr2 -t2

Expand Down
12 changes: 7 additions & 5 deletions examples/laplace.py → examples/algorithm/laplace.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import taichi as ti

ti.init(arch=ti.cpu)

N = 16

x, y = ti.field(ti.f32), ti.field(ti.f32)

ti.root.dense(ti.ij, 16).place(x, y)
ti.root.dense(ti.ij, N).place(x, y)


@ti.kernel
def laplace():
for i, j in x:
if (i + j) % 3 == 0:
y[i, j] = 4.0 * x[i, j] - x[i - 1,
j] - x[i + 1,
j] - x[i, j - 1] - x[i, j + 1]
if (i + j) % 3 == 0 and 1 <= i < N - 1 and 1 <= j < N - 1:
y[i, j] = 4.0 * x[i, j] \
- x[i - 1, j] - x[i + 1, j] - x[i, j - 1] - x[i, j + 1]
else:
y[i, j] = 0.0

Expand Down
107 changes: 107 additions & 0 deletions examples/algorithm/marching_squares.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Marching squares algorithm
# https://en.wikipedia.org/wiki/Marching_squares

import numpy as np

import taichi as ti

ti.init(arch=ti.cpu)

N = 128

edge_table_np = np.array(
[
[[-1, -1], [-1, -1]], # Case 0
[[3, 0], [-1, -1]], # Case 1
[[0, 1], [-1, -1]], # Case 2
[[1, 3], [-1, -1]], # Case 3
[[1, 2], [-1, -1]], # Case 4
[[0, 1], [2, 3]], # Case 5
[[0, 2], [-1, -1]], # Case 6
[[2, 3], [-1, -1]], # Case 7
[[2, 3], [-1, -1]], # Case 8
[[0, 2], [-1, -1]], # Case 9
[[0, 3], [1, 2]], # Case 10
[[1, 2], [-1, -1]], # Case 11
[[1, 3], [-1, -1]], # Case 12
[[0, 1], [-1, -1]], # Case 13
[[3, 0], [-1, -1]], # Case 14
[[-1, -1], [-1, -1]], # Case 15
],
dtype=np.int32)

pixels = ti.field(float, (N, N))

edge_coords = ti.Vector.field(2, float, (N**2, 2)) # generated edges
edge_table = ti.Vector.field(2, int, (16, 2)) # edge table (constant)
edge_table.from_numpy(edge_table_np)


@ti.func
def gauss(x, sigma):
# Un-normalized Gaussian distribution
return ti.exp(-x**2 / (2 * sigma**2))


@ti.kernel
def touch(mx: float, my: float, size: float):
for I in ti.grouped(pixels):
mouse_pos = ti.Vector([mx, my]) + 0.5 / N
peak_center = gauss((I / N - 0.5).norm(), size)
peak_mouse = gauss((I / N - mouse_pos).norm(), size)
pixels[I] = peak_center + peak_mouse


@ti.func
def get_vertex(vertex_id):
# bottom edge
v = ti.Vector([0.5, 0.0])
# right edge
if vertex_id == 1:
v = ti.Vector([1.0, 0.5])
# top edge
elif vertex_id == 2:
v = ti.Vector([0.5, 1.0])
# left edge
elif vertex_id == 3:
v = ti.Vector([0.0, 0.5])
return v


@ti.kernel
def march(level: float) -> int:
n_edges = 0

for i, j in ti.ndrange(N - 1, N - 1):
case_id = 0
if pixels[i, j] > level: case_id |= 1
if pixels[i + 1, j] > level: case_id |= 2
if pixels[i + 1, j + 1] > level: case_id |= 4
if pixels[i, j + 1] > level: case_id |= 8

for k in range(2):
if edge_table[case_id, k][0] == -1:
break

n = ti.atomic_add(n_edges, 1)
for l in ti.static(range(2)):
vertex_id = edge_table[case_id, k][l]
edge_coords[n, l] = ti.Vector([i, j
]) + get_vertex(vertex_id) + 0.5

return n_edges


level = 0.2

gui = ti.GUI('Marching squares')
while gui.running and not gui.get_event(gui.ESCAPE):
touch(*gui.get_cursor_pos(), 0.05)
n_edges = march(level)
edge_coords_np = edge_coords.to_numpy()[:n_edges] / N
gui.set_image(ti.imresize(pixels, *gui.res) / level)
gui.lines(edge_coords_np[:, 0],
edge_coords_np[:, 1],
color=0xff66cc,
radius=1.5)
gui.show()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion examples/quadtree.py → examples/algorithm/quadtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
R = 7
N = K**R

Broot = ti.root
Broot = None
B = ti.root
for r in range(R):
B = B.bitmasked(ti.ij, (K, K))
if r == 0:
Broot = B

qt = ti.field(ti.f32)
B.place(qt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import taichi as ti

ti.init(arch=ti.cpu)

n = 8
x = ti.field(dtype=ti.f32, shape=n, needs_grad=True)
y = ti.field(dtype=ti.f32, shape=n)
Expand Down
2 changes: 2 additions & 0 deletions examples/mnist.py → examples/autodiff/mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import taichi as ti

ti.init(arch=ti.gpu)

# ti.runtime.print_preprocessed = True
# ti.cfg.print_ir = True

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# https://github.com/hsjeong5/MNIST-for-Numpy

import numpy as np
from urllib import request
import gzip
import pickle
from urllib import request

import numpy as np

filename = [["training_images", "train-images-idx3-ubyte.gz"],
["test_images", "t10k-images-idx3-ubyte.gz"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import taichi as ti
import taichi as tc

ti.init(arch=ti.cpu)

tc.set_gdb_trigger(True)

number_coeffs = 4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import taichi as ti

ti.init(arch=ti.cpu)

N = 2048
x, y = ti.field(ti.f32), ti.field(ti.f32)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import taichi as ti

ti.init(arch=ti.cpu)

pixel = ti.field(ti.u8, shape=(512, 512, 3))


Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import taichi as ti
import numpy as np
import os

import numpy as np

import taichi as ti

# A 2D grid with quad faces
# y
# |
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
95 changes: 0 additions & 95 deletions examples/mciso.py

This file was deleted.

Loading