我正在尝试使用 Python 和 Scipy 中的线程解决多个线性系统。在使用 Python 线程方面,我是一个绝对的初学者。我附上了代码,概括了我的目标。这段代码可以运行,但随着 totalThreads 的增加,执行时间实际上会增加。我的猜测是 spsolve 被视为关键部分,实际上并没有并行运行。
我的问题如下:
我的问题如下:
- spsolve 是否线程安全?
- 如果 spsolve 是阻塞的,有没有办法解决?
- 是否有其他线性求解器包可以更好地并行化?
- 是否有更好的编写此代码段的方式以提高性能?
def Worker(threadnum, totalThreads):
for i in range(threadnum,N,totalThreads):
x[:,i] = sparse.linalg.spsolve( A, b[:,i] )
threads = []
for threadnum in range(totalThreads):
t = threading.Thread(target=Worker, args=(threadnum, totalThreads))
threads.append(t)
t.start()
for threadnum in range(totalThreads): threads[threadnum].join()