News for hackers. Stuff that matters.
Re:Nuitka: a Python compiler written in Python by quietbritishjim on Thursday June 17, 2021 @09:52AM (#27539559)
(Standard reponse)

Python does support parallelism, so long as you're calling into a C library to do the work, because they (usually) release the GIL while doing the work. That sounds like a cop out but using C libraries is very common to do CPU intensive work anyway. Examples include numpy (e.g. you can call np.dot from multiple threads in Python and they will genuinely use multiple cores) and all the rest of the scipy stack, and even modules in the standard library (e.g. when using zipfile, even to process an in-memory buffer, the GIL is released).

Re:Nuitka: a Python compiler written in Python by dekhn on Thursday June 17, 2021 @10:40AM (#27540068)
if you call np.dot on the same underlying ndarray from multiple threads, what are the semantics?

I don't know what they are but I think most people want a single invocation of np.dot in a single thread to use multiple worker threads (as described in https://scipy.github.io/old-wiki/pages/ParallelProgramming) by pushing the work to a thread-aware matrix library.

Re:Nuitka: a Python compiler written in Python by quietbritishjim on Thursday June 17, 2021 @12:07PM (#27541175)
> if you call np.dot on the same underlying ndarray from multiple threads, what are the semantics?

Usually, this will be safe because np.dot only reads from its arguments, and produces a fresh new array to hold the result.

I just checked the documentation and found there is an out=argument to pass a destination array. (In any case there are other numpy functions that do modify their argument.) If you pass the same array to that parameter in two different threads then I don't know what happens. Maybe numpy locks the GIL to write its output. Maybe it produces C-style undefined behaviour (this would be my guess ... and my preference).

> I think most people want a single invocation of np.dot in a single thread to use multiple worker threads

Maybe? I'm not convinced that's what I want! But it's pretty irrelevant either way to my point; np.dot was just an example. My real point is that lots of CPU-heavy Python functions are implemented in C and release the GIL, so in practice parallelism with Python is often possible.

Re:Nuitka: a Python compiler written in Python by dekhn on Thursday June 17, 2021 @08:37PM (#27546067)
I've worked in this field awhile.

99% of people want this: np.dot and others use all the cores on your system by default. This accelerates the common use case, which is individual user on a multicore machine who wants single result ASAP. Then, there's an env var that lets you configure the underlying matrix library to use only a single (or maybe another number) of cores, and you work with the multithreaded/multiprocess libraries to run many independent calculations (possibly on shared read-only source data); then that scheduler has a tunable set to saturate the cores or IO on the system.

This is what most BLASses use and it's how data analysts and supercomputer folks can get along.

Re:Nuitka: a Python compiler written in Python by quietbritishjim on Friday June 18, 2021 @05:54AM (#27548852)
To reiterate: None of this contradicts my main point that numpy.dot releases the GIL, which it does even if it calls into a BLAS library that uses multiple cores (although of course that would then have its own global lock - potentially even an interprocess lock - to manage concurrent uses). numpy.dot was only meant to be an example in the first place! I could just as easily have talked about some optimisation routine in scipy or something in the standard library (actually I did mention ZipFile.extract).

> I've worked in this field awhile.

This statement undermines your point more than it supports it. There is no "this field" for numpy. Just working in some field that uses it is no qualification for knowing whether users as a whole would like it multithreaded automatically. "Data analysts" and "supercomputer folks" no doubt include many users but certainly not all and possibly not even most (for all we know).

> 99% of people want this: ...

I can believe this might be true. I don't think I ever claimed otherwise. In fact I said "maybe"! I only claimed that I don't want this.

I will make one more claim though: I don't think there's any way that you could know this, even if it's really true.

But yes, I agree and already knew that it's how BLAS libraries work by default, including OpenBLAS which is what the prebuilt numpy wheels use. Even then, as you're probably already aware by the sounds of it, that's only for sufficiently large matrices. If you're just using small matrices and vectors (e.g. in 3D to represent real-world coordinates) then processing will be done directly in the calling thread (because the overhead of using multiple threads would be greater than the benefit).

Re:Nuitka: a Python compiler written in Python by dekhn on Thursday June 17, 2021 @10:52AM (#27540201)
Hi, would the downvoter mind adding a comment explaining why? What I said is technically correct so it's unclear what the objection is.
Re:Nuitka: a Python compiler written in Python by dekhn on Thursday June 17, 2021 @11:14AM (#27540467)
No need to downvote a request for explanation; this is within HN rules. Otherwise, you're giving an ambiguous signal so I don't know whether you're unhappy with the current state of Python, or disagree with my technical statement (in which case, please provide a counterargument instead of a vote).
Re:Nuitka: a Python compiler written in Python by pitaj on Thursday June 17, 2021 @11:17AM (#27540510)
FYI complaining about votes is against HN comment policy.
Re:Nuitka: a Python compiler written in Python by dekhn on Thursday June 17, 2021 @11:43AM (#27540864)
I'm not complaining about votes. I'm asking for people to clarifying why they are downvoting a technically correct statement.
Re:Nuitka: a Python compiler written in Python by dragonwriter on Thursday June 17, 2021 @12:22PM (#27541383)
Requesting explanation of downvotes is among the things the policy against discussion of the votes on a post is directed at, and, IMO, also demonstrates a fundamental failure to understand (or respect) the purpose of downvotes, which is about managing signal-to-noise ratio by, among other things, not polluting threads with meta-level discussion (abd invariably debates) of why a particular comment is (or is not) inappropriate.
Re:Nuitka: a Python compiler written in Python by dekhn on Thursday June 17, 2021 @08:34PM (#27546041)
I have 11,659 karma. I'm not bragging but that means I have a pretty good understanding and respect for the purpose of downvotes (because I learn from them how to make more upvotes). Nothing is being polluted here, just a reasonable request for clarification (for example I would love to learn that I was factually wrong!). I'm guessing the downvote signal was actually somebody agreeing with me and showing displeasure with the problem, or believing I was technically incorrect, or gauche to be pointing out the problem in the first place.
Re:Nuitka: a Python compiler written in Python by garyrob on Thursday June 17, 2021 @12:34PM (#27541509)
"Python does support parallelism, so long as you're calling into a C library to do the work"

Actually, you can use Numba to get the same effect while still writing Python. Numba allows you to simply apply a decorator to critical Python code and it is JITted into C-like performance. I do this in my code, and it has worked great for my uses.

Re:Nuitka: a Python compiler written in Python by amelius on Thursday June 17, 2021 @10:31AM (#27539972)
Not really. You can't build large data structures (not arrays, but trees, graphs, etc) with structural sharing between threads. Unless of course you translate all of your data to C too, at which point you are really doing everything in C (data and code).
Re:Nuitka: a Python compiler written in Python by quietbritishjim on Thursday June 17, 2021 @12:15PM (#27541287)
OK, let's put it like this: In some contexts, but not all, Python supports a useful amount of parallelism.

I have definitely worked on Python projects in the past that were easily parallisable and worked absoluately fine. I simply created threads with threading.Thread, passed messages around with queue.Queue and used some standard Python modules to process those messages. The messages were independent enough from each other that I didn't have any issues with marshalling access to them, while being large enough that the overhead of mulithreading was still outweighed by its benefits. Rewriting the whole thing in C++ (etc.) would have made negligable difference to performance.

I can certainly imagine a program similar to what you describe, where there are lots of messages that operate on a single monolythic data structure, which couldn't easily be made to work with true parallelisation in Python.

Re:Nuitka: a Python compiler written in Python by incrudible on Thursday June 17, 2021 @10:59AM (#27540272)
> You can't build large data structures (not arrays, but trees, graphs, etc) with structural sharing between threads.

You probably do not want to do that in the first place. The reason that the GIL still exists is that the overhead of fine-grained locking for Python datastructures would negate the speedup from multithreading.

Re:Nuitka: a Python compiler written in Python by amelius on Thursday June 17, 2021 @11:41AM (#27540836)
Which is why Python doesn't really support parallelism.

Other language (like GoLang) do support parallelism but they have a more advanced runtime and don't use a GIL.

Re:Nuitka: a Python compiler written in Python by incrudible on Thursday June 17, 2021 @02:42PM (#27542976)
It's not specific to Python. If you want to do fine-grained "parallelism" on a set of complex shared datastructures, your cores might look busy, but really you're just wasting most of the time locking/unlocking stuff. That's not necessarily going to result in a speedup over a lock-free single-threaded implementation (that you didn't implement to compare against).

In the case of Python, the single-threaded implementation was already there, the implementation that removed the GIL in favor of fine-grained locking failed to deliver the goods.

If you want to actually take advantage of parallelism, you want data structures that are amenable to it, which is basically sections of arrays without a lot of potential read/write conflicts over multiple threads. If you're at that point, you probably want to use C/C++/Rust anyway, you don't want speed up something that's already 100x slower (the interpreter) by parallelizing it. Python offers that with C-extensions like NumPy.

Re:Nuitka: a Python compiler written in Python by amelius on Friday June 18, 2021 @02:33AM (#27547905)
> your cores might look busy, but really you're just wasting most of the time locking/unlocking stuff.

Not if you're reading most of the time and are using a modern concurrent GC.

Re:Nuitka: a Python compiler written in Python by incrudible on Friday June 18, 2021 @08:03AM (#27549470)
It doesn't matter if you're reading or writing, if there's a potential conflict you need to lock, or you need some complex and probably buggy "lock-free" data structure. The complexity and memory overhead of a concurrent GC just goes on top of that.
Re:Nuitka: a Python compiler written in Python by quietbritishjim on Sunday June 20, 2021 @01:32PM (#27572165)
Locking an uncontended lock is cheap (at least compared to a contended one) so actual conflict does matter more than potential conflict.