Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Solving Multiple Linear Systems using Threads

Tags:

python

math

scipy

I am attempting to solve multiple linear systems using python and scipy using threads. I am an absolute beginner when it comes to python threads. I have attached code which distils what I'm trying to accomplish. This code works but the execution time actually increases as one increases totalThreads. My guess is that spsolve is being treated as a critical section and is not actually being run concurrently.

My questions are as follows:

  • Is spsolve thread-safe?
  • If spsolve is blocking, is there a way around it?
  • Is there another linear solver package that I can be using which parallelizes better?
  • Is there a better way to write this code segment that will increase performance?

I have been searching the web for answers but with no luck. Perhaps, I am just using the wrong keywords. Thanks for everyone's help.

    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()
like image 953
Eldila Avatar asked Mar 14 '12 19:03

Eldila


1 Answers

The first thing you should understand is that, counterintuitively, Python's threading module will not let you take advantage of multiple cores. This is due to something called the Global Interpreter Lock (GIL), which is a critical part of the standard cPython implementation. See here for more info: What is a global interpreter lock (GIL)?

You should consider using the multiprocessing module instead, which gets around the GIL by spinning up multiple independent Python processes. This is a bit more difficult to work with, because different processes have different memory spaces, so you can't just share a thread-safe object between all processes and expect the object to stay synchronized between all processes. Here is a great introduction to multiprocessing: http://www.doughellmann.com/PyMOTW/multiprocessing/

like image 117
Brendan Wood Avatar answered Oct 27 '22 00:10

Brendan Wood