News for hackers. Stuff that matters.
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.