0

For a uni assignment I have written a 2D square domain flow solver in MATLAB. To study Python I have converted the MATLAB code to Python. I have used NumPy to do all matrix-vector multiplications and I have used scipy.sparse.linalg.spsolve() to solve Ax=b, where A is 40x40 and sparse.

In the end I wasn't too happy about the speed of the solver. So I used the profiler included in Spyder to track down the bottleneck. It basically turned out that all linear algebra operations are quite fast except for the system solve (using the aforementioned method). No surprise there because solving a system is always more expensive than just multiplying some vectors and matrices.

I turned to Cython to accelerate my solver. I read http://wiki.cython.org/tutorials/numpy and I went haywire by giving each and every variable a static type (yes I know this is not the smartest or most efficient way, but I am in a hurry to see results and will do a proper job thereafter). The only thing I haven't given a static type is the sparse matrix A, because it is a CSR sparse matrix and I do not yet know how to static type it. And yes I know that it is the most crucial part, because profiling showed the system solve to be the bottleneck.

After finally managing to compile everything with Cython the result was exactly the same as without Cython... I do understand that the Cython performance gain would not be great because I did not tackle the bottleneck, however I do not understand why the Cython version did not run even just 1% faster.

Could someone please help me out with benefiting from Cython? How can I make my code run faster? And how should I give a CSR sparse matrix from scipy a static type?

My code can be downloaded using this google drive link: https://docs.google.com/file/d/0B-nchNKLtgjeWTE4OXZrVUpfcWs/edit?usp=sharing

Aeronaelius
  • 1,291
  • 3
  • 12
  • 31
  • 1
    Cython would really help if you were to write _your own_ `spsolve` that is somehow faster than `sparse.linalg.spsolve`. Otherwise, a program that is 99% calls to numpy/scipy won't benefit from Cython per se. – Nikita Nemkin May 28 '13 at 10:08

2 Answers2

6

Because you didn't tackle the bottleneck.

It sounds to me that all you have done now is to make the methods calls to NumPy a bit faster. That will only help if you make a lot of calls to NumPy, but you say that that's not where the Bottleneck is for you.

Cython enables you to speed up Python code. It won't help you speed up NumPy code.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
4

Because most Numpy codes are already written in C. C codes won't benefit from Cython of course.

If it runs too slow, you should suspect your algorithm instead.

Have a look here for comparisons of different way to speed up python.

Jiaming Lu
  • 875
  • 6
  • 20
  • Looking at the url you provided, you see actually that the Cython version runs faster than the NumPy version. Although NumPy arrays are used in the Cython version, they omit using slicing and use a nested loop over NumPy arrays instead. Might my code benefit from switching from slicing to loops using Cython+NumPy like in the url? – Aeronaelius May 28 '13 at 07:27
  • Ah wonderful this might actually help, although the gain is not that big. – Aeronaelius May 28 '13 at 10:24