3 ms·
Lack of multidimensional arrays as a language primitive is a problem. It was a problem in C, C++, and Go, too. There, you either have arrays of arrays, or some
by Animats 9y ago
Lack of multidimensional arrays as a language primitive is a problem. It was a problem in C, C++, and Go, too. There, you either have arrays of arrays, or some macro/template that does a multiply on the subscript. The reply in one of the Rust forums is that multidimensional arrays are only found in "domain-specific languages", like FORTRAN.
When someone brings this up the usual excuses are trotted out:
- "It can be done with templates".
- "You can have an array of arrays".
Then there's bikeshedding. Some people want to be able to extract an arbitrary subarray slice from an N-dimensional array and treat that like a regular array. This is possible but complicated, and if implemented slows down the simple operations. Then there are people who want N-dimensional arrays where N is determined at run time. (This now includes the machine learning people.) Endless arguments between these groups resulted in nothing being done in Go. I haven't followed that discussion for Rust.
The big advantage of doing this in the language is that there's a lot of hardware support for doing number-crunching fast on dense arrays, and you want the compiler to be using it effectively. Matrix multiply on arrays of arrays is much slower than on a dense array where the compiler knows the spacing between the rows at compile time.
At least have arrays as good as FORTRAN. This is why some heavy number-crunching work is still done in FORTRAN.
- danieldk 9y agoLack of multidimensional arrays as a language primitive is a problem. [...] "It can be done with templates" I am wondering what your point is. There are excellent and performant C++ template-based libraries for tensors (Eigen, Armadillo, etc.). In Rust I have used ndarray a bit for linear algebra and it is quite good [1]. If you have parametric polymorphism and operator overloading, whet is the problem of doing it in a library? The big advantage of doing this in the language is that there's a lot of hardware support for doing number-crunching fast on dense arrays, and you want the compiler to be using it effectively. Last time I checked Intel MKL (and wrappers like Armadillo) was still the fastest for doing number crunching of dense arrays on CPUs. [1] The primary problem I ran into was that some common expressions were not backed by BLAS yet.
- camus2 9y ago> Endless arguments between these groups resulted in nothing being done in Go Do you have some links to these discussions?
- sidlls 9y ago> At least have arrays as good as FORTRAN Yes. This is one reason why I simply didn't care to convert research code I was using for my research from the decade(s) old Fortran into C or C++, when I was in academia. I've been writing some machine learning products with Rust for a while now and I have to say I quickly abandoned using Rust for the computational components. The best way to use Rust is as it was meant to be: at the systems level, in this specific case for data and infrastructure code. Computation should be done using a language more suitable (e.g. Fortran) as one part of the pipeline. Rust is fantastic for writing pipelines that leverage other libraries for computation in this regard: the relatively strong static type system and easy error handling make it quite nice to work with.
- nine_k 9y agoAre there any more efficient ways to represent a dense multi-dimensional array than a one-dimensional array + index multiplication? Is there any difference between said multiplication done by some internal function inside the compiler, or by a regular (and trivial) function generated by a macro / template system?
- Animats 9y agoYes. And yes. One dimensional array + index multiplication doesn't make it clear to the compiler that the rows are disjoint. So the compiler can't assume there's no aliasing. It also probably can't strength-reduce the multiply to an add when advancing through an array in the "wrong" direction. FORTRAN compilers have been doing subscripting optimizations like that since 1956. After those optimizations, it may be possible to use some of the SSE instructions on x86.
- mamcx 9y agoI wonder how are FORTRAN arrays implemented. I was also assuming was a bing single arrays + indexes... If I wanna add this to a language, what I need to consider to be as FORTRAN?
- Animats 9y agoLook at the generated code for a matrix multiply in different languages. How tight is the inner loop?
- nine_k 9y agoThe whole idea of aliasing comes with C and arbitrary pointer arithmetics. It's not a necessity. In languages without it (beyond Fortran, Rust comes to mind), the compiler may assume the lack of aliasing and optimize accordingly. I wonder what JVM's JIT would come up with (likely not with SSE, though). I can agree that however you choose to represent a multi-dimensional array in RAM, giving the compiler some idea about them, or 2D matrices at least, could lead to some special optimizations.
- tiziano88 9y agoI think an array of arrays in Rust would be allocated sequentially and packed, so you can efficiently index any particular item, right?