section-properties icon indicating copy to clipboard operation
section-properties copied to clipboard

Speed Up with Numba

Open Marco-Pellegrino opened this issue 3 years ago • 6 comments

Hi Robbie. Have you ever tried to use Numba instead of Numpy? It seens it can speed up everything :)

Is it worth it to have a look at?

Marco-Pellegrino avatar Sep 13 '21 15:09 Marco-Pellegrino

Hi Marco,

Thanks for the suggestion, I must admit this is the first time I've heard of Numba!

Do you have any experience in using it? At the moment (from what I remember), a lot of the computational time is spent in performing element level calculations and assembling these results into sparse matrices. Do you think this is something numba would improve significantly?

If you are interested, you could do a small test case to see if there are any performance gains?

Robbie

robbievanleeuwen avatar Sep 16 '21 05:09 robbievanleeuwen

Hi @robbievanleeuwen . I don't have personal experience with numba unfortunately but it looks like that can become 10-100x faster. Your tool is cool anyway but... you know. People like to have a solution in 300 ms ;)

Marco-Pellegrino avatar Sep 19 '21 21:09 Marco-Pellegrino

Numba could be a good solution to some of the python for loops (depending on how exactly they are written), as it essentially turns them into machine code using LLVM. Numba is a really cool project. That said, I think it would be even better to fully vectorize the code in numpy, which is something I wanted to do and one of the reasons I wanted to get some benchmarking code in the project using pytest-benchmark.

Spectre5 avatar Sep 19 '21 22:09 Spectre5

Hi @Spectre5 ! I thought that Section properties was already using most of the numpy vectorisation power. If not, it will definitely get a good amount of speed. Can you point me to some part of the code where there is no use of Numpy?

I do like the way this conversation is going

Marco-Pellegrino avatar Sep 19 '21 22:09 Marco-Pellegrino

See all of these Python loops in this file and this file to calculate the results? It loops on each element and each Gauss point of each element. So it uses all (slow) Python loops. Those might be sped up using Numba, or at least could be with some reorganization. However, you could also setup the analysis using larger numpy arrays and then perform the computations once using fully vectorized calculations, at the expense of using more memory. This would be ever better than Numba.

One idea that I've played around with is to make a fully vectorized version (max speed with higher memory usage) and then also keep the Python loop version (and/or using Numba) that would be slower, but less memory usage. Just in case some really large or complicated section actually got big enough to be a memory problem. But we'd need to see how much memory a large section actually uses to know if that'd be needed.

Spectre5 avatar Sep 20 '21 00:09 Spectre5

I am no Numba expert, but I have done a fair bit of Numba coding recently forming structural stiffness matrices (for solution with Scipy functions). The main problem I found was Numba reverting to Python code, with the end result being even slower than pure Python. The main two lessons I took from this were:

  1. Use the "no Python" option, so the problem is picked up at compile time, rather than being hidden by reverting to Python.
  2. Work from the lowest level routines upwards, making sure all the bottom level routines work in no Python mode, before starting on the next level up.

More details at: https://newtonexcelbach.com/2021/08/15/more-on-numba/

DougAJ4 avatar Jan 15 '22 04:01 DougAJ4