Hacker News new | ask | show | jobs
by SuperFluffy 1943 days ago
Having const generics will also permit much more elegant implementations of low level linear algebra.

For example, when writing a `GEMM` (general matrix multiplication) routine, the core building block is a so called kernel, which gets moved over the matrices like a stencil. The size of the kernel however depends on a) the numerical precision (float, double), b) the available SIMD intrinsics, and c) the architecture the code is executed on (haswell, skylake or zen have different latency and throughput for different intrinsics).

Right now it's surprisingly painful to write an optimal kernel and buffer, which is different for every architecture and which you need to kind of hardcode. With const generics this should become much easier.

2 comments

I do this in D all the time, and the system is actually so flexible you can even use things like simulated annealing to optimize (if you are completely mad) to optimize the parameters.

    struct DiscardPastN(uint len)
    {
        int[len] buffer;
        uint state = 0;
        void put(int x)
        {
         buffer[++state % len] = x;
            //mwuhahahaha
            static if(len == 4)
                asm {
                        ud2;
                }
        }
        int[len] get() const 
        {
         return buffer;
        }
    }
So extremely useful all over the place let alone linear algebra, I'm glad Rust now has it too
Why undefined when Len is 4? Is it just an intentional sort of heisenbug?
Yes - it's quicker than doing something clever but is meant to show you can specialise the asm or intrinsics etc.
Yes!!! Linear algebra!

I am a huge fan of nalgebra and having const generics would make the API and implementation much nicer.

Hopefully the robotics industry can move towards implementing stuff in Rust. Having low level robotics algorithms like state estimation and SLAM in Rust makes perfect sense.

Have you tried working with rust-ndarray (the closest thing to NumPy in Rust)? While I love Rust for general work, unfortunately for numeric programming, compared to NumPy itself, it's like pulling teeth. There's a lot which is completely missing: the main thing for me is (boolean) masks, which means there's no element-wise comparison of ndarrays.
It's a cliche for open-source libraries, but ndarray could use more documentation.

A lot more.

A really whole lot more.

"Pulling teeth" is how I'd describe ndarray currently.