SparseMatrix的创建问题

N=20时,使用ti.linalg.SparseMatrix的build_from_ndarray创建矩阵时,运行到build_from_ndarray这行没有报错就直接结束运行了,N=10时就没有这个问题


import numpy as np

import scipy.sparse as sp

import taichi as ti

ti.init(arch=ti.cpu)

N = 10

A_scipy = sp.rand(N, N, density=0.5, format='csr')

A_scipy = (A_scipy + A_scipy.T) / 2

A_scipy = A_scipy.tocoo()

A_numpy = np.vstack((A_scipy.row, A_scipy.col, A_scipy.data)).T

triplets = ti.ndarray(dtype=ti.math.vec3, shape=A_numpy.shape[0])

triplets.from_numpy(A_numpy)

A = ti.linalg.SparseMatrix(n=N, m=N, dtype=ti.f32)

A.build_from_ndarray(triplets)

b = np.random.rand(N)

solver = ti.linalg.SparseCG(A, b)

x, exit_code = solver.solve()

print(f">>>> Computation was successful?: {exit_code}")

SparseMatrix那个这么用好像有bug,试试SparseMatrixBuilder

import numpy as np
import scipy.sparse as sp
import taichi as ti
ti.init(arch=ti.cpu)
N = 20
A_scipy = sp.rand(N, N, density=0.5, format='csr')
A_scipy = (A_scipy + A_scipy.T) / 2
A_scipy = A_scipy.tocoo()
A_numpy = np.vstack((A_scipy.row, A_scipy.col, A_scipy.data)).T
K = ti.linalg.SparseMatrixBuilder(N, N, max_num_triplets=300)
@ti.kernel
def fill(A: ti.types.sparse_matrix_builder(),A_numpy:ti.types.ndarray()): # type: ignore
    for i in range(A_numpy.shape[0]):
        A[int(A_numpy[i, 0]), int(A_numpy[i, 1])] += A_numpy[i, 2]

fill(K, A_numpy)
A = K.build()
b = np.random.rand(N)
solver = ti.linalg.SparseCG(A, b)
x, exit_code = solver.solve()
print(f">>>> Computation was successful?: {exit_code}")