Skip to content

Commit

Permalink
[ci] Enable pylint on examples (#5222)
Browse files Browse the repository at this point in the history
* [ci] Enable pylint on examples

* comply review requirements

* fix remaining errors
  • Loading branch information
feisuzhu authored Jun 23, 2022
1 parent 5cd3339 commit 37b1dbd
Show file tree
Hide file tree
Showing 50 changed files with 1,370 additions and 1,161 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ _build
imgui.ini
/venv/
/_skbuild/
.cache
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ repos:
- id: pylint
args: ['-rn', '-sn']
files: ^python/taichi/
exclude: ^python/taichi/examples/.*.py
41 changes: 25 additions & 16 deletions python/taichi/examples/algorithm/marching_squares.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ def march(level: float) -> int:

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
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:
Expand All @@ -92,16 +96,21 @@ def march(level: float) -> int:
return n_edges


level = 0.2
def main():
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.tools.imresize(pixels, *gui.res) / level)
gui.lines(edge_coords_np[:, 0],
edge_coords_np[:, 1],
color=0xff66cc,
radius=1.5)
gui.show()
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.tools.imresize(pixels, *gui.res) / level)
gui.lines(edge_coords_np[:, 0],
edge_coords_np[:, 1],
color=0xff66cc,
radius=1.5)
gui.show()


if __name__ == '__main__':
main()
45 changes: 29 additions & 16 deletions python/taichi/examples/algorithm/mciso_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,31 +327,43 @@ def march(self) -> int:
r_n = 0

for I in ti.grouped(ti.ndrange(*[[1, self.N - 1]] * self.dim)):
id = 0
idx = 0
if ti.static(self.dim == 2):
i, j = I
if self.m[i, j] > 1: id |= 1
if self.m[i + 1, j] > 1: id |= 2
if self.m[i, j + 1] > 1: id |= 4
if self.m[i + 1, j + 1] > 1: id |= 8
if self.m[i, j] > 1:
idx |= 1
if self.m[i + 1, j] > 1:
idx |= 2
if self.m[i, j + 1] > 1:
idx |= 4
if self.m[i + 1, j + 1] > 1:
idx |= 8
else:
i, j, k = I
if self.m[i, j, k] > 1: id |= 1
if self.m[i + 1, j, k] > 1: id |= 2
if self.m[i + 1, j + 1, k] > 1: id |= 4
if self.m[i, j + 1, k] > 1: id |= 8
if self.m[i, j, k + 1] > 1: id |= 16
if self.m[i + 1, j, k + 1] > 1: id |= 32
if self.m[i + 1, j + 1, k + 1] > 1: id |= 64
if self.m[i, j + 1, k + 1] > 1: id |= 128
if self.m[i, j, k] > 1:
idx |= 1
if self.m[i + 1, j, k] > 1:
idx |= 2
if self.m[i + 1, j + 1, k] > 1:
idx |= 4
if self.m[i, j + 1, k] > 1:
idx |= 8
if self.m[i, j, k + 1] > 1:
idx |= 16
if self.m[i + 1, j, k + 1] > 1:
idx |= 32
if self.m[i + 1, j + 1, k + 1] > 1:
idx |= 64
if self.m[i, j + 1, k + 1] > 1:
idx |= 128

for m in range(self.et.shape[1]):
if self.et[id, m][0] == -1:
if self.et[idx, m][0] == -1:
break

n = ti.atomic_add(r_n, 1)
for l in ti.static(range(self.dim)):
e = self.et[id, m][l]
e = self.et[idx, m][l]
R = float(I)

if ti.static(self.dim == 2):
Expand Down Expand Up @@ -435,8 +447,9 @@ def march(self) -> int:


class MCISO_Example(MCISO):
@staticmethod
@ti.func
def gauss(self, x):
def gauss(x):
return ti.exp(-6 * x**2)

@ti.kernel
Expand Down
132 changes: 69 additions & 63 deletions python/taichi/examples/algorithm/mgpcg.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@
Ap = ti.field(dtype=real) # matrix-vector product
alpha = ti.field(dtype=real) # step size
beta = ti.field(dtype=real) # step size
sum = ti.field(dtype=real) # storage for reductions
sum_ = ti.field(dtype=real) # storage for reductions
pixels = ti.field(dtype=real, shape=(N_gui, N_gui)) # image buffer

ti.root.pointer(ti.ijk, [N_tot // 4]).dense(ti.ijk, 4).place(x, p, Ap)

for l in range(n_mg_levels):
ti.root.pointer(ti.ijk, [N_tot // (4 * 2**l)]).dense(ti.ijk,
4).place(r[l], z[l])
for lvl in range(n_mg_levels):
ti.root.pointer(ti.ijk,
[N_tot // (4 * 2**lvl)]).dense(ti.ijk,
4).place(r[lvl], z[lvl])

ti.root.place(alpha, beta, sum)
ti.root.place(alpha, beta, sum_)


@ti.kernel
Expand Down Expand Up @@ -65,9 +66,9 @@ def compute_Ap():


@ti.kernel
def reduce(p: ti.template(), q: ti.template()):
for I in ti.grouped(p):
sum[None] += p[I] * q[I]
def reduce(p_: ti.template(), q_: ti.template()):
for I in ti.grouped(p_):
sum_[None] += p_[I] * q_[I]


@ti.kernel
Expand Down Expand Up @@ -144,69 +145,74 @@ def paint():
pixels[i, j] = x[ii, jj, kk] / N_tot


gui = ti.GUI("mgpcg", res=(N_gui, N_gui))

init()

sum[None] = 0.0
reduce(r[0], r[0])
initial_rTr = sum[None]

# r = b - Ax = b since x = 0
# p = r = r + 0 p
if use_multigrid:
apply_preconditioner()
else:
z[0].copy_from(r[0])

update_p()

sum[None] = 0.0
reduce(z[0], r[0])
old_zTr = sum[None]
def main():
gui = ti.GUI("mgpcg", res=(N_gui, N_gui))

# CG
for i in range(400):
# alpha = rTr / pTAp
compute_Ap()
sum[None] = 0.0
reduce(p, Ap)
pAp = sum[None]
alpha[None] = old_zTr / pAp
init()

# x = x + alpha p
update_x()

# r = r - alpha Ap
update_r()

# check for convergence
sum[None] = 0.0
sum_[None] = 0.0
reduce(r[0], r[0])
rTr = sum[None]
if rTr < initial_rTr * 1.0e-12:
break
initial_rTr = sum_[None]

# z = M^-1 r
# r = b - Ax = b since x = 0
# p = r = r + 0 p
if use_multigrid:
apply_preconditioner()
else:
z[0].copy_from(r[0])

# beta = new_rTr / old_rTr
sum[None] = 0.0
reduce(z[0], r[0])
new_zTr = sum[None]
beta[None] = new_zTr / old_zTr

# p = z + beta p
update_p()
old_zTr = new_zTr

print(' ')
print(f'Iter = {i:4}, Residual = {rTr:e}')
paint()
gui.set_image(pixels)
gui.show()

ti.profiler.print_kernel_profiler_info()
sum_[None] = 0.0
reduce(z[0], r[0])
old_zTr = sum_[None]

# CG
for i in range(400):
# alpha = rTr / pTAp
compute_Ap()
sum_[None] = 0.0
reduce(p, Ap)
pAp = sum_[None]
alpha[None] = old_zTr / pAp

# x = x + alpha p
update_x()

# r = r - alpha Ap
update_r()

# check for convergence
sum_[None] = 0.0
reduce(r[0], r[0])
rTr = sum_[None]
if rTr < initial_rTr * 1.0e-12:
break

# z = M^-1 r
if use_multigrid:
apply_preconditioner()
else:
z[0].copy_from(r[0])

# beta = new_rTr / old_rTr
sum_[None] = 0.0
reduce(z[0], r[0])
new_zTr = sum_[None]
beta[None] = new_zTr / old_zTr

# p = z + beta p
update_p()
old_zTr = new_zTr

print(' ')
print(f'Iter = {i:4}, Residual = {rTr:e}')
paint()
gui.set_image(pixels)
gui.show()

ti.profiler.print_kernel_profiler_info()


if __name__ == '__main__':
main()
8 changes: 4 additions & 4 deletions python/taichi/examples/algorithm/mgpcg_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ def solve(self,
old_zTr = self.sum[None]

# Conjugate gradients
iter = 0
while max_iters == -1 or iter < max_iters:
it = 0
while max_iters == -1 or it < max_iters:
# self.alpha = rTr / pTAp
self.compute_Ap()
self.reduce(self.p, self.Ap)
Expand All @@ -219,7 +219,7 @@ def solve(self,
rTr = self.sum[None]

if verbose:
print(f'iter {iter}, |residual|_2={math.sqrt(rTr)}')
print(f'iter {it}, |residual|_2={math.sqrt(rTr)}')

if rTr < tol:
break
Expand All @@ -239,7 +239,7 @@ def solve(self,
self.update_p()
old_zTr = new_zTr

iter += 1
it += 1


class MGPCG_Example(MGPCG):
Expand Down
Loading

0 comments on commit 37b1dbd

Please sign in to comment.