News for hackers. Stuff that matters.
Re:Make your programs run faster by better using the data ca... by behnamoh on Friday June 23, 2023 @10:41PM (#36454943)
Recently I worked on a simulation analysis which taught me several things:

- To get the most out of Python, you really need efficient libraries like Numpy. I started off my project in pure Python and some Pandas. But soon I realized it was painfully slow and looked for better solutions.

- Polars looked like a good Pandas alternative, but some googling revealed that it could be slower than Numpy. Although it's easy to work with dataframes (e.g., you can select a column by calling its name), I converted all my code to use Numpy and suddenly saw huge speed boost. But that wasn't the end of it. I was now in a rabbit hole...

- Vectorization helped a lot with speed as well. I learnt that using the right tool (in this case, Numpy) guides you to do better and vectorize operations, whereas Pandas didn't enforce that technique on me. Despite the initial hurdle, I realized that vectorization made my code much more readable and easier to follow.

- I then parallelized my code to utilized all 10 cores of the M1 Pro chip. Seeing that CPU graph get full was just pleasing. For this, I used Python's ProcessPoolExecutor, which is better than ThreadPoolExecutor.

- At this point, my simulations had gone from taking several hours to a couple minutes! Pretty impressive, but then I noticed that increasing the resolution of some variables resulted in RAM issues. I have 32GB of RAM and working with huge matrices was just not feasible. I remembered that the OS community has been using quantization techniques to reduce the memory footprint of LLMs. The way they work often involves going from np.float32 to np.float16. To my surprise, doing this solved my RAM issues without much effect on the accuracy of the results.

- Looking for further improvements, I discovered Dask and used it instead of ProcessPoolExecutor (although, I still used `schedulor="processes"` in Dask). Unlike ProcessPoolExecutor, Dask didn't utilize all my CPU cores all the time, but somehow I found it more capable of handling parallel computation.

- But increasing the resolution of parameters even further would still cause problems. Specifically, I noticed that my Python code would halt. Doing CTRL-Z would stop the program, but the RAM wouldn't get released. In this case, I had to manually `pkill -f python` to throw away all the Python objects that were taking up RAM space.

- This is where I discovered garbage collection. Using `import gc`, I was able to `del <obj>` at key parts of my code and immediately do `gc.collect()` to collect garbage. This was the first time I ever did this and I was pleased to see my program run again w/o RAM issues.

- I then optimized some search algorithms in my code which further sped up the program while also reducing the RAM usage.

- I converted all Numpy arrays to ds.array for further improvements, but some functionalities were not immediately available and I went back to Numpy.

- Looking for further improvements, I thought about running my Python code on GPU. I tried `import cupy as np` but got to problems that I couldn't solve.

- So I tried converting all numpy arrays to Pytorch tensors. Pytorch is available on M1 Mac so I thought why not? Unfor, I got memory limits errors. Reducing the resolution of parameters fixed that, but then I got a mysterious error for which I couldn't find any help:

    -[_MTLCommandBuffer addCompletedHandler:]:871: failed assertion `Completed handler provided after commit call'

If you know what this means, please let me know!

This was an educational journey and I learnt much. I think projects like this are the best way to learn new stuff.

Re:Make your programs run faster by better using the data ca... by wudangmonk on Friday June 23, 2023 @10:57PM (#36455023)
This is honest question for those that use python for these compute intensive tasks and is not meant as a critique.

If you ultimately end up having to use c/c++/fortran native libraries and work around pretty much everything python. What is the point of using python instead of c/c++?

Re:Make your programs run faster by better using the data ca... by behnamoh on Friday June 23, 2023 @11:39PM (#36455294)
Others answered this correctly. Python is more like a glue language that is easy to use. I ended up using type annotations in my code and in the end it looked a lot like a typed language. But the nice thing about Python is that types, manual garbage collection, etc. are not necessary for most applications.
Re:Make your programs run faster by better using the data ca... by jameson71 on Friday June 23, 2023 @11:02PM (#36455056)
The code outside the critical path is easier and quicker
Re:Make your programs run faster by better using the data ca... by nathants on Friday June 23, 2023 @11:11PM (#36455111)
think of python wrapping other tools the same way bash does.
Re:Make your programs run faster by better using the data ca... by keithalewis on Friday June 23, 2023 @11:55PM (#36455375)
Tricycle riders will never become Formula 1 drivers.
Re:Make your programs run faster by better using the data ca... by wiseowise on Saturday June 24, 2023 @07:06AM (#36457559)
You’re not Plato either, didn’t stop you from trying to come up with sound analogy.
Re:Make your programs run faster by better using the data ca... by keithalewis on Sunday June 25, 2023 @05:45AM (#36466456)
But I can recognize an ad hominem argument when I see it. Merely attempting to illuminate the intelligible with truth.[^1] OP said "If you ultimately end up having to use c/c++/fortran native libraries and work around pretty much everything python. What is the point of using python instead of c/c++?" The answers boiled down to making it easy for people with no formal computer training to leverage C, C++, and Fortran libraries written by professionals. Hopefully that is something we can agree on.

[^1]: BTW, I am not the sun either.

Re:Make your programs run faster by better using the data ca... by jameson71 on Friday June 23, 2023 @11:01PM (#36455053)
And then after all that you discovered Java?
Re:Make your programs run faster by better using the data ca... by nathants on Friday June 23, 2023 @11:16PM (#36455145)
this is a great journey! what volume of data were you analyzing?

more you could try:

- span multiple machines, with lambda/ec2-spot and s3

- write c/cpp, call via python subprocess or cffi

- pytorch gpu on ec2-spot

Re:Make your programs run faster by better using the data ca... by behnamoh on Friday June 23, 2023 @11:36PM (#36455278)
Thanks for the input! I'll look them up.

Depending on the resolution of my parameters, I'm dealing with a 32-40GB-ish data.

Re:Make your programs run faster by better using the data ca... by chaxor on Friday June 23, 2023 @11:39PM (#36455296)
I'm confused by some of this. Specifically, pandas columns are essentially just a thin wrapper around record arrays in numpy right? How could it be that much faster to use numpy over pandas if it's effectively the same thing?
Re:Make your programs run faster by better using the data ca... by anywhichway on Saturday June 24, 2023 @01:51AM (#36455950)
Right, each pandas column is a numpy array (or optionally pyarrow in pandas 2.0), but numpy is going to be faster for things that needs to be vectorized across columns, such as matrix operations.
Re:Make your programs run faster by better using the data ca... by jamann on Saturday June 24, 2023 @06:44AM (#36457437)
Thats when you do polars.DataFrame.to_numpy()
Re:Make your programs run faster by better using the data ca... by winstonewert on Saturday June 24, 2023 @12:16AM (#36455513)
I used to do this sort of thing in Python. But then I realized that if I wrote my code in something like C++, I had fast code without running through all these hoops. Then I learned Rust which has many of advantages of Python in terms of higher-level niceties. Today, its just way easier to write some Rust code for a simulation then go through all the hoops to make Python code fast enough.
Re:Make your programs run faster by better using the data ca... by Ygg2 on Saturday June 24, 2023 @03:01AM (#36456295)
As the adage goes, to write fast Python first learn to code in C (or Rust if you prefer that).
Re:Make your programs run faster by better using the data ca... by sigg3 on Saturday June 24, 2023 @12:34AM (#36455617)
You ignored an important lesson IMO.

Use the right tool for the job.

Your entire journey here is battling with Python and not the problem space. Your improvements are solely measured by how your code leaves Python problem solving space, so you end up writing what's probably unpythonic and unmaintainable Python.

Why?

Rewrite in Go, Rust or C++ or whatever.

If you need to go faster than Python, don't use Python. Python is great for everything else. Its overhead and sluggish execution are consequences of its greatest strength.

Re:Make your programs run faster by better using the data ca... by carlmr on Saturday June 24, 2023 @03:40AM (#36456481)
Exactly my thought. Whenever I see wild optimization goose chases it's Python causing all the issues.

The problem is that a lot is still possible to optimize "in Python" although as you say the code becomes less and less readable to your average Python developer, so that people don't notice they just picked the wrong tool for the job.

You need more speed? Use multiprocessing? Allocate memory appropriately? Rust, or if you like pain, C or C++.

Re:Make your programs run faster by better using the data ca... by Etherlord87 on Saturday June 24, 2023 @03:41AM (#36456487)
You need to tell that to the authors TensorFlow.
Re:Make your programs run faster by better using the data ca... by pzo on Saturday June 24, 2023 @04:13AM (#36456635)
Doesn't Tensorflow has C++ API? I think tensorflow and pytorch under the hood using c++/c. Tensorflow has even official Rust bindings. The same applies to libraries like Open3d and OpenCV - although mostly people consume their python API under the hood is very optimized C++ code parallelized for many CPU and SIMD instructions.
Re:Make your programs run faster by better using the data ca... by Buttons840 on Saturday June 24, 2023 @03:10AM (#36456336)
All this reminds me of writing a Go (the board game) engine in Python. There's a lot of looping involved to identify groups and their liberties, and to detect Kos, etc. I profiled my Python code and optimized the algorithm, and it was faster. Then I rewrote my Python "Board" class in Rust and called it from the rest of my Python code, and it was faster again.

Then I wrote the same algorithms in Julia, and it was as fast as the Python / Rust solution. Then I learned that Julia lives up to its claim.

If you're reaching for Numpy / Pandas as an optimization, and not as a convenience, then consider rewriting your code in Julia.

Re:Make your programs run faster by better using the data ca... by TickleSteve on Saturday June 24, 2023 @05:03AM (#36456914)
ctrl-z doesnt kill the runnning program, just puts it to the background. The process is still present with all RAM still held by it... you ca use "fg" to bring it back to the foreground. I think you meant ctrl-c which kills the process.
Re:Make your programs run faster by better using the data ca... by behnamoh on Saturday June 24, 2023 @10:11AM (#36458830)
Sorry, yes, you’re right, I meant CTRL-C.
Re:Make your programs run faster by better using the data ca... by jamann on Saturday June 24, 2023 @06:52AM (#36457478)
Using polars and rust might be the best option right now, offering high level operations such as SQL like joins as well as performance. Although im not a rust programmer and i only use tbe python library polars, so please correct me as necessary.
Re:Make your programs run faster by better using the data ca... by sn9 on Saturday June 24, 2023 @05:45PM (#36462927)
You should check out scalene: https://www.youtube.com/watch?v=vVUnCXKuNOg