【求助】 sparse matrix在cuda backend下会报 cusparse64_12.dll lib not found

我在运行官方给出的sparse matrix 例子时将backend改为cuda

import taichi as ti
arch = ti.cuda # or ti.cuda
ti.init(arch=arch)

n = 4
# step 1: create sparse matrix builder
K = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(A: ti.types.sparse_matrix_builder()):
    for i in range(n):
        A[i, i] += i  # Only +=  and -= operators are supported for now.

# step 2: fill the builder with data.
fill(K)

print(">>>> K.print_triplets()")
K.print_triplets()
# outputs:
# >>>> K.print_triplets()
# n=4, m=4, num_triplets=4 (max=100)(0, 0) val=1.0(1, 1) val=1.0(2, 2) val=1.0(3, 3) val=1.0

# step 3: create a sparse matrix from the builder.
A = K.build()
print(">>>> A = K.build()")
print(A)

会在K.build()时报以下错误

[Taichi] version 1.6.0, llvm 15.0.1, commit f1c6fbbd, win, python 3.10.5
[Taichi] Starting on arch=cuda
n=4, m=4, num_triplets=4 (max=100)
[0, 0] = 0.0
[1, 1] = 1.0
[2, 2] = 2.0
[3, 3] = 3.0
[W 07/08/23 10:29:06.388 32924] [cuda_driver.cpp:taichi::lang::CUDADriverBase::load_lib@36] cusparse64_12.dll lib not found.
[E 07/08/23 10:29:06.388 32924] [taichi/program/sparse_matrix.h:taichi::lang::CuSparseMatrix::CuSparseMatrix@228] Failed to load cusparse library!


Traceback (most recent call last):
  File "C:\Users\tzn\Desktop\notes\py\taichi\sparse.py", line 19, in <module>
    A = K.build()
  File "C:\Users\tzn\AppData\Local\Programs\Python\Python310\lib\site-packages\taichi\linalg\sparse_matrix.py", line 291, in build
    sm = self.ptr.build_cuda()
RuntimeError: [taichi/program/sparse_matrix.h:taichi::lang::CuSparseMatrix::CuSparseMatrix@228] Failed to load cusparse library!

这个报错的原因基本可以确定是没有找到 cusparse 库。cusparse 需要你自行安装 CUDA toolkit(和 CUDA driver 不是一个东西),并且安装时注意勾选 CuSparse。
另外,注意 Toolkit 的版本和你的 CUDA driver 版本要一致。报错信息显示你的 driver 是 12 的,那么 Toolkit 也要安装 12 的。参考这个 issue:[sparse]Failed to load cusparse library(cusparse version problem) · Issue #8227 · taichi-dev/taichi · GitHub

问题已解决,感谢 :pray: