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

[Lang] Use ti.linalg.sparse_matrix_builder as kernel parameters #3210

Merged
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 examples/simulation/implicit_mass_spring.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def init_edges(self):
rest_len[idx] = (pos[idx1] - pos[idx2]).norm()

@ti.kernel
def init_mass_sp(self, M: ti.sparse_matrix_builder()):
def init_mass_sp(self, M: ti.linalg.sparse_matrix_builder()):
for i in range(self.NV):
if self.invMass[i] != 0.0:
mass = 1.0 / self.invMass[i]
Expand Down Expand Up @@ -141,7 +141,7 @@ def compute_Jacobians(self):
self.Jf[1] = ti.Matrix([[self.kf, 0], [0, self.kf]])

@ti.kernel
def assemble_K(self, K: ti.sparse_matrix_builder()):
def assemble_K(self, K: ti.linalg.sparse_matrix_builder()):
for i in self.spring:
idx1, idx2 = self.spring[i][0], self.spring[i][1]
for m, n in ti.static(ti.ndrange(2, 2)):
Expand All @@ -154,7 +154,7 @@ def assemble_K(self, K: ti.sparse_matrix_builder()):
K[2 * (self.NV - 1) + m, 2 * (self.NV - 1) + n] += self.Jf[1][m, n]

@ti.kernel
def assemble_D(self, D: ti.sparse_matrix_builder()):
def assemble_D(self, D: ti.linalg.sparse_matrix_builder()):
for i in self.spring:
idx1, idx2 = self.spring[i][0], self.spring[i][1]
for m, n in ti.static(ti.ndrange(2, 2)):
Expand Down Expand Up @@ -188,7 +188,7 @@ def update(self, h):
force = self.force.to_numpy().reshape(2 * self.NV)
b = (force + h * K @ vel) * h
# Sparse solver
solver = ti.SparseSolver(solver_type="LDLT")
solver = ti.linalg.SparseSolver(solver_type="LDLT")
solver.analyze_pattern(A)
solver.factorize(A)
# Solve the linear system
Expand Down
2 changes: 1 addition & 1 deletion examples/simulation/stable_fluid.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def swap(self):
if use_sparse_matrix:
# use a sparse matrix to solve Poisson's pressure equation.
@ti.kernel
def fill_laplacian_matrix(A: ti.sparse_matrix_builder()):
def fill_laplacian_matrix(A: ti.linalg.sparse_matrix_builder()):
for i, j in ti.ndrange(res, res):
row = i * res + j
center = 0.0
Expand Down
3 changes: 2 additions & 1 deletion misc/spMv_linear_solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@


@ti.kernel
def fill(A: ti.sparse_matrix_builder(), b: ti.template(), interval: ti.i32):
def fill(A: ti.linalg.sparse_matrix_builder(), b: ti.template(),
interval: ti.i32):
for i in range(n):
A[i, i] += 2.0

Expand Down
4 changes: 2 additions & 2 deletions misc/sparse_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@


@ti.kernel
def fill(A: ti.sparse_matrix_builder(), b: ti.sparse_matrix_builder(),
interval: ti.i32):
def fill(A: ti.linalg.sparse_matrix_builder(),
b: ti.linalg.sparse_matrix_builder(), interval: ti.i32):
for i in range(n):
if i > 0:
A[i - 1, i] += -1.0
Expand Down
1 change: 0 additions & 1 deletion python/taichi/lang/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from taichi.lang.enums import Layout
from taichi.lang.exception import InvalidOperationError
from taichi.lang.impl import *
from taichi.lang.kernel_arguments import sparse_matrix_builder
from taichi.lang.kernel_impl import (KernelArgError, KernelDefError,
data_oriented, func, kernel, pyfunc)
from taichi.lang.matrix import Matrix, Vector
Expand Down
5 changes: 0 additions & 5 deletions python/taichi/lang/kernel_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ def subscript(self, i, j):
return SparseMatrixEntry(self.ptr, i, j)


sparse_matrix_builder = SparseMatrixBuilder
"""Alias for :class:`~taichi.lang.sparse_matrix.SparseMatrixBuilder`.
"""


def decl_scalar_arg(dtype):
dtype = cook_dtype(dtype)
arg_id = _ti_core.decl_arg(dtype, False)
Expand Down
4 changes: 2 additions & 2 deletions python/taichi/lang/kernel_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from taichi.lang.ast.transformer import ASTTransformerTotal
from taichi.lang.enums import Layout
from taichi.lang.exception import TaichiSyntaxError
from taichi.lang.kernel_arguments import sparse_matrix_builder
from taichi.lang.shell import _shell_pop_print, oinspect
from taichi.lang.util import to_taichi_type
from taichi.linalg.sparse_matrix import sparse_matrix_builder
from taichi.misc.util import obsolete
from taichi.type import any_arr, primitive_types, template

Expand Down Expand Up @@ -527,7 +527,7 @@ def func__(*args):
raise KernelArgError(i, needed.to_string(), provided)
launch_ctx.set_arg_int(actual_argument_slot, int(v))
elif isinstance(needed, sparse_matrix_builder):
# Pass only the base pointer of the ti.sparse_matrix_builder() argument
# Pass only the base pointer of the ti.linalg.sparse_matrix_builder() argument
launch_ctx.set_arg_int(actual_argument_slot, v.get_addr())
elif isinstance(needed, any_arr) and (
self.match_ext_arr(v)
Expand Down
2 changes: 1 addition & 1 deletion python/taichi/lang/stmt_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def transform_as_kernel():
if isinstance(ctx.func.argument_annotations[i], ti.template):
continue
if isinstance(ctx.func.argument_annotations[i],
ti.sparse_matrix_builder):
ti.linalg.sparse_matrix_builder):
arg_init = parse_stmt(
'x = ti.lang.kernel_arguments.decl_sparse_matrix()')
arg_init.targets[0].id = arg.arg
Expand Down
3 changes: 2 additions & 1 deletion python/taichi/linalg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from taichi.linalg.sparse_matrix import SparseMatrix, SparseMatrixBuilder
from taichi.linalg.sparse_matrix import (SparseMatrix, SparseMatrixBuilder,
sparse_matrix_builder)
from taichi.linalg.sparse_solver import SparseSolver
4 changes: 4 additions & 0 deletions python/taichi/linalg/sparse_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,7 @@ def build(self, dtype=f32, format='CSR'):
"""Create a sparse matrix using the triplets"""
sm = self.ptr.build()
return SparseMatrix(sm=sm)


sparse_matrix_builder = SparseMatrixBuilder
# Alias for :class:`SparseMatrixBuilder`
4 changes: 2 additions & 2 deletions tests/python/test_sparse_linear_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def test_sparse_LLT_solver(solver_type):
b = ti.field(ti.f32, shape=n)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder(), InputArray: ti.ext_arr(),
b: ti.template()):
def fill(Abuilder: ti.linalg.sparse_matrix_builder(),
InputArray: ti.ext_arr(), b: ti.template()):
for i, j in ti.ndrange(n, n):
Abuilder[i, j] += InputArray[i, j]
for i in range(n):
Expand Down
30 changes: 15 additions & 15 deletions tests/python/test_sparse_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_sparse_matrix_builder():
Abuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder()):
def fill(Abuilder: ti.linalg.sparse_matrix_builder()):
for i, j in ti.ndrange(n, n):
Abuilder[i, j] += i + j

Expand All @@ -24,7 +24,7 @@ def test_sparse_matrix_element_access():
Abuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder()):
def fill(Abuilder: ti.linalg.sparse_matrix_builder()):
for i in range(n):
Abuilder[i, i] += i

Expand All @@ -40,7 +40,7 @@ def test_sparse_matrix_element_modify():
Abuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder()):
def fill(Abuilder: ti.linalg.sparse_matrix_builder()):
for i in range(n):
Abuilder[i, i] += i

Expand All @@ -57,8 +57,8 @@ def test_sparse_matrix_addition():
Bbuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder(),
Bbuilder: ti.sparse_matrix_builder()):
def fill(Abuilder: ti.linalg.sparse_matrix_builder(),
Bbuilder: ti.linalg.sparse_matrix_builder()):
for i, j in ti.ndrange(n, n):
Abuilder[i, j] += i + j
Bbuilder[i, j] += i - j
Expand All @@ -79,8 +79,8 @@ def test_sparse_matrix_subtraction():
Bbuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder(),
Bbuilder: ti.sparse_matrix_builder()):
def fill(Abuilder: ti.linalg.sparse_matrix_builder(),
Bbuilder: ti.linalg.sparse_matrix_builder()):
for i, j in ti.ndrange(n, n):
Abuilder[i, j] += i + j
Bbuilder[i, j] += i - j
Expand All @@ -100,7 +100,7 @@ def test_sparse_matrix_scalar_multiplication():
Abuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder()):
def fill(Abuilder: ti.linalg.sparse_matrix_builder()):
for i, j in ti.ndrange(n, n):
Abuilder[i, j] += i + j

Expand All @@ -118,7 +118,7 @@ def test_sparse_matrix_transpose():
Abuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder()):
def fill(Abuilder: ti.linalg.sparse_matrix_builder()):
for i, j in ti.ndrange(n, n):
Abuilder[i, j] += i + j

Expand All @@ -137,8 +137,8 @@ def test_sparse_matrix_elementwise_multiplication():
Bbuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder(),
Bbuilder: ti.sparse_matrix_builder()):
def fill(Abuilder: ti.linalg.sparse_matrix_builder(),
Bbuilder: ti.linalg.sparse_matrix_builder()):
for i, j in ti.ndrange(n, n):
Abuilder[i, j] += i + j
Bbuilder[i, j] += i - j
Expand All @@ -159,8 +159,8 @@ def test_sparse_matrix_multiplication():
Bbuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder(),
Bbuilder: ti.sparse_matrix_builder()):
def fill(Abuilder: ti.linalg.sparse_matrix_builder(),
Bbuilder: ti.linalg.sparse_matrix_builder()):
for i, j in ti.ndrange(n, n):
Abuilder[i, j] += i + j
Bbuilder[i, j] += i - j
Expand All @@ -182,8 +182,8 @@ def test_sparse_matrix_nonsymmetric_multiplication():
Bbuilder = ti.linalg.SparseMatrixBuilder(k, m, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder(),
Bbuilder: ti.sparse_matrix_builder()):
def fill(Abuilder: ti.linalg.sparse_matrix_builder(),
Bbuilder: ti.linalg.sparse_matrix_builder()):
for i, j in ti.ndrange(n, k):
Abuilder[i, j] += i + j
for i, j in ti.ndrange(k, m):
Expand Down
6 changes: 3 additions & 3 deletions tests/python/test_spmv.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_sparse_matrix_vector_multiplication1():
b = ti.field(ti.f32, shape=n)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder(), b: ti.template()):
def fill(Abuilder: ti.linalg.sparse_matrix_builder(), b: ti.template()):
for i, j in ti.ndrange(n, n):
Abuilder[i, j] += i

Expand All @@ -29,7 +29,7 @@ def test_sparse_matrix_vector_multiplication2():
b = ti.field(ti.f32, shape=n)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder(), b: ti.template()):
def fill(Abuilder: ti.linalg.sparse_matrix_builder(), b: ti.template()):
for i, j in ti.ndrange(n, n):
Abuilder[i, j] += i - j

Expand All @@ -53,7 +53,7 @@ def test_sparse_matrix_vector_multiplication3():
b = ti.field(ti.f32, shape=n)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder(), b: ti.template()):
def fill(Abuilder: ti.linalg.sparse_matrix_builder(), b: ti.template()):
for i, j in ti.ndrange(n, n):
Abuilder[i, j] += i + j

Expand Down