Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will multi-threading necessarily decrease runtime?

If I have a series of CPU-intensive operations, will multi-threading my program necessarily decrease its runtime? What are the trade-offs of doing so? In this case, I'm trying to compute the nullspace of a very large matrix. I'm using Python and, specifically, the numpy package:

def nullspace(A, eps=1e-15):
    """Computes the null space of the real matrix A."""
    n, m = shape(A)
    if n > m :
        return nullspace(transpose(A), eps)
    _, s, vh = linalg.svd(A)
    s = append(s, zeros(m))[0:m]
    null_mask = (s <= eps)
    null_space = compress(null_mask, vh, axis=0)
    return null_space.tolist()

Also, I would be interested to know just how one would go about multi-threading such a function. Thanks in advance.

like image 976
arshajii Avatar asked Jul 05 '26 02:07

arshajii


1 Answers

Python has the Global Interpreter Lock (GIL), which only allows one thread to interact with the interpreter at a time -- effectively, this means that you can only run one thread of Python at a time. This is a severe disadvantage when trying to run multiple threads.

However, numpy is built on top of a heavily-optimised library for numerical linear algebra called LAPACK. If you install the right version of LAPACK for your system, it will parallelise its computations for you. You can then install numpy on top of your LAPACK, and the Python computations will be parallelised.

This also means that many numpy operations release the GIL, so that you can fire off a long numpy computation in a Python thread and simultaneously execute other Python. Thanks @JFSebastian.

like image 179
Katriel Avatar answered Jul 07 '26 15:07

Katriel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!