Python: solving multiple linear systems using threads

I am trying to solve several linear systems using python and scipy using threads. I am an absolute newbie when it comes to python threads. I have attached code that repels what I'm trying to execute. This code works, but the execution time actually increases, since each increases totalThreads. I assume that spsolve is seen as a critical section and does not actually start at the same time.

My questions are as follows:

  • Is spsolve thread safe?
  • If spsolve blocks, is there a way around it?
  • Is there another linear solver package I can use that is best parallelized?
  • Is there a better way to write this code segment that will increase performance?

I searched the Internet for answers, but no luck. Maybe I'm just using the wrong keywords. Thanks for helping everyone.

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() 
+6
source share
1 answer

The first thing you should understand is that, counterintuitively, the Python streaming module will not allow you to use multiple cores. This is due to something called Global Interpreter Lock (GIL), which is an important part of the standard cPython implementation. See here for more information: What is global interpreter lock (GIL)?

Instead, you should use a multiprocessing module that bypasses the GIL by creating several independent Python processes. This is a bit more difficult to work with, because different processes have different memory spaces, so you cannot just share a thread-safe object between all processes and expect the object to remain synchronized between all processes. Here is a great introduction to multiprocessing: http://www.doughellmann.com/PyMOTW/multiprocessing/

+3
source

Source: https://habr.com/ru/post/910742/


All Articles