Live data from Hacker News

KlongPy: High-Performance Array Programming in Python

github.com

11–20 of 90 posts

Re: KlongPy: High-Performance Array Programming in Python

#12

Naive array programming is not really high performance in my book, because performing a lot of trivial arithmetic on large arrays leads to poor locality and high bandwidth pressure. The better alternative is SPMD, i.e. something like CUDA or ISPC for CPUs. This is possible with some type of JIT if the numpy style of programming is to be maintained, for example tinygrad.

You are absolutely right, naive array programming might be much faster than raw python but it will never be high performance because you can't use caches and memory bandwidth effectively.

The unstated major premise here is that you're working with data that's big enough that this becomes your major bottleneck.

There's also a subset - possibly even a silent majority - of problems where you're doing fairly intensive calculation on data that fairly comfortably fits into a modern CPU cache. For those, I'd still expect SIMD to be a great choice from a performance perspective.

Re: KlongPy: High-Performance Array Programming in Python

#13

Naive array programming is not really high performance in my book, because performing a lot of trivial arithmetic on large arrays leads to poor locality and high bandwidth pressure. The better alternative is SPMD, i.e. something like CUDA or ISPC for CPUs. This is possible with some type of JIT if the numpy style of programming is to be maintained, for example tinygrad.

Memory traffic is certainly the biggest problem that the array paradigm presents for implementation, yes. I'd quibble with calling that "poor locality": when working with large arrays, if any part of a cache line is accessed it's very likely the entire line will be used in short order, which is the definition of good locality as I understand it. The issue is simply high memory use and a large number of accesses.

I think it's an oversimplification to say SPMD is a better model full stop. Array programming has the advantage that the implementer optimizes a specific function, and its interaction with the rest of the program is much simpler: all the data is available at the start, and the result can be produced in any order. So things like multi-pass algorithms and temporary lookup tables are possible, particularly valuable if the program isn't spending that much time on arithmetic but rather "heavy" searching and sorting operations. Not that I claim NumPy or CuPy do a good job of this. Sections "Fusion versus fission" and "Dynamic versus static" are relevant here, I think: https://mlochbaum.github.io/BQN/implementation/versusc.html#...

Re: KlongPy: High-Performance Array Programming in Python

#14

I thought I knew python, but then I saw this: ?> sum::{+/x} :" sum + over / the array x :monad ?> sum([1 2 3]) 6 ?> count::{#x} :monad ?> count([1 2 3]) 3 What?

oooooooh I think I'm starting to see...

sum::{} is the creation of a monadic function (which I just learned about because I don't have a CS background)

:" is a comment

sum([...]) is calling the function with a list, and the function will iterate over the list adding each of the elements

count::{#x} is another monadic function that returns the number of elements, which then gets called

Re: KlongPy: High-Performance Array Programming in Python

#16

I thought I knew python, but then I saw this: ?> sum::{+/x} :" sum + over / the array x :monad ?> sum([1 2 3]) 6 ?> count::{#x} :monad ?> count([1 2 3]) 3 What?

The langiage in the examples is Klong, not Python. KlongPy translates Klong to Python and then executes it.

Re: KlongPy: High-Performance Array Programming in Python

#17
post #7

I gather where it says `:monad` it is referring to an operation that had an effect on the interpreter state?

No. In APL deriverd array programming languages, verbs (or functions) are monadic or dyadic : they accept only one or two arguments : In '1 + 1', + is a dyadic operator, while in 'exp 5', exp is monadic. In J, and in APL I guess, left arg is usually understood as 'control data', while right arg is the data upon which calculation is done. Left argument is usually left unchanged after calculations. In this way, is it p…

It sounds unrelated to the monads popularized by Haskell tutorials. Naming coincidence or am I missing something?

Re: KlongPy: High-Performance Array Programming in Python

#18

I thought I knew python, but then I saw this: ?> sum::{+/x} :" sum + over / the array x :monad ?> sum([1 2 3]) 6 ?> count::{#x} :monad ?> count([1 2 3]) 3 What?

Sorry if this is a dumb question, why is this weird in Python? I use R as my main language and this is exactly how I would expect it to work. Does Python not do this naturally?

Re: KlongPy: High-Performance Array Programming in Python

#20
post #7

Earlier quoted context omitted.

No. In APL deriverd array programming languages, verbs (or functions) are monadic or dyadic : they accept only one or two arguments : In '1 + 1', + is a dyadic operator, while in 'exp 5', exp is monadic. In J, and in APL I guess, left arg is usually understood as 'control data', while right arg is the data upon which calculation is done. Left argument is usually left unchanged after calculations. In this way, is it p…

It sounds unrelated to the monads popularized by Haskell tutorials. Naming coincidence or am I missing something?

Naming coincidence, they are in fact unrelated.
Post reply on HN