6 ms·
FORTRAN has multidimensional arrays, and the compilers know about them and optimize aggressively. Most other languages don't. C people just do not get multidime
by Animats 8y ago
FORTRAN has multidimensional arrays, and the compilers know about them and optimize aggressively. Most other languages don't. C people just do not get multidimensional arrays. They think an array of arrays is good enough. I tried to convince the Go people to put in multidimensional arrays, with no success.
Years ago, I had an amusing fight with the C++ committee over this.
In C++, you can overload the "[]" operator:
int& operator[] (int i)
But you can't do that for multiple arguments.
int& operator[] (int i, int j)
Why? An obscure feature of C and C++ is that the precedence of the comma is different for function argument lists and subscript lists. In
foo(a,b)
is a function call with two arguments.
foo[a,b]
is an invocation of the comma operator, which ignores the second operand. The syntax charts reflect this. So I proposed that the syntax for comma usage be the same in both
"()" and "[]" forms. If you see "foo[a,b]" in a C/C++ program, it's almost certainly an error. I searched through large volumes of code and was unable to find a single use of the comma operator inside brackets.
But no, there's a "save the comma operator" faction.
C people just do not get multidimensional arrays.
- cf498 8y ago> C people just do not get multidimensional arrays. They think an array of arrays is good enough. I have to say, i dont get what you are trying to say. Do you have a source that explains it? What is the difference between a multi dimensional array and arrays of arrays? And what does a FORTRAN compiler optimize here? It is a piece of memory written and organized in a certain way. What is there to organize. You basically have a pointer to a memory region with an offset for the position at row X and column Y. What am I missing? Are you only talking about the syntax for the final address? What has that to do with the compiler? Do you mean the parser?
- sparky_z 8y agoI'm not a Fortran or C programmer (so take this with a grain of salt), but as I understand it, Fortran is so optimizable because it's arrays are not fundamentally just a memory layout with pointer math. In Fortran, arrays are abstract entities, which allows the compiler to lay them out in the best possible way to take advantage of vectorization for the current code. Originally, pointers weren't even a feature of the language, though they were bolted on in a later version. Sure, an expert programmer can replicate this speed in C by being careful about memory layouts, etc. But in Fortran, everyone gets it for free.
- geoalchimista 8y agoI would add that since Fortran 90, you can also get scalar-array and array-array operations for free. The `elemental` keyword that automatically broadcasts pure scalar functions to multidimensional arrays works almost in a way reminiscent of NumPy (I'm a weird one who came to do some serious work in Fortran long after known C and Python).
- alfalfasprout 8y agoan array of arrays is not necessarily contiguous in C(++). Indeed, if allocating on the heap, you end up with a bunch of discontiguous memory that's not necessarily correctly aligned. A good tensor implementation accounts for strides that are SIMD compatible (eg; each dimension is a multiple of the SIMD register width).
- Const-me 8y ago> A good tensor implementation accounts for strides that are SIMD compatible (eg; each dimension is a multiple of the SIMD register width). Never implemented any tensors, but in my experience, sometimes you better do what GPUs do with some texture formats: switch from linear layout into dense blocks layout. E.g. for dense float32 matrix and SSE code, a good choice is a 2D array of 4x4 blocks: exactly 1 cache line, and only consumes 4 out of 16 registers while being processed i.e. you can do a lot within a block while not hitting any RAM latency.
- int_19h 8y agoAn array of arrays is necessarily contiguous in C - this is implied by the type. An array of pointers to arrays will, of course, not be contiguous - and is the only way to get a dynamically sized heap-allocated 2D array in C (VLAs give you stack-allocated arrays, with all the size limits that entails). In C++, this all is best handled by a library class.
- cf498 8y ago>and is the only way to get a dynamically sized heap-allocated 2D array in C (VLAs give you stack-allocated arrays, with all the size limits that entails). Why wouldnt you be able to create a dynamic array of arrays with placement new and a cast.
- int_19h 8y agoA cast to what, though? You need to be able to write the type of that array, but you can't do that unless dimensions are compile-time constants.
- bane 8y agoThis might help https://dzone.com/articles/memory-layout-of-multi-dimensional-arrays-1 https://dzone.com/articles/memory-layout-of-multi-dimensiona... http://exodus.physics.ucla.edu/Fortran95/scf95.lect3.pdf http://exodus.physics.ucla.edu/Fortran95/scf95.lect3.pdf
- slavik81 8y agoThat's a current topic of discussion for C++20 and beyond. https://cor3ntin.github.io/posts/sandiego/#deprecate-comma-operator-in-subscript-expressions https://cor3ntin.github.io/posts/sandiego/#deprecate-comma-o...
- Animats 8y agoWow, two decades after I was pushing for it.
- xiii1408 8y agoThere's a better solution, to your problem: pass an "index object" as the argument to operator[]. So `T& operator[](index_t)`, where `index_t` could just be a tuple. a[{1, 2}] = 12; This has virtually the same syntax, will be compiled in the same way by any modern compiler, and lets you create special index types that have invariants (suppose you want i0 < i1).
- beojan 8y agoThat's not a "better solution", it's a workaround.
- kitd 8y agoI disagree. The tuple is an index into an array. The multiplicity of the tuple indicates (literally, in fact) the multiple dimensions of the array. Ie to the coder, it is the index that takes on the multi-dimensionality, not the array.
- deleted 8y ago[deleted]
- chewxy 8y agoDo you mean syntax? Because if layout, then Gorgonia's tensors are actually flat dense layouts controlled by an access pattern. Gorgonia supports fortran layout as well as C layout. Syntax is a bit painful though.
- shaklee3 8y agoIf this was true then the BLAS Fortran library would run faster when calling with Fortran as opposed to C, but it doesn't. As someone else pointed out, multi-dimensional arrays are contiguous in C, by definition. It's not like Fortran has some magical powers to unlock instructions only available to that language.
- jacoblambda 8y agoThe argument is not that because Fortran has multidimensional arrays it is faster, it is that because Fortran has multidimensional arrays it can and will often be faster in most cases for free. Obviously an extremely optimised library such as BLAS will run just about as fast in C and Fortran. For a good majority of code out there however people just don't want to put in the effort to carefully optimise array layouts. The feature moves the responsibility of optimisation to the compiler which can in many cases do a better job than a normal programmer with reasonable assurances that the optimisations will not introduce bugs and / or side effects into the code.
- celrod 8y agoLanguages like C(++), Rust, and Julia have vector intrinsics that are unfortunately lacking in Fortran. Is there a way to wrap [SLEEF](https://github.com/shibatch/sleef https://github.com/shibatch/sleef) in Fortran? While writing fast code in Fortran is easy, I think it is unfortunately harder to write "extremely optimised" libraries like BLAS in Fortran than in these other languages for that reason. Without the low level control, you can't be as explicit about vectorization patterns. Eg, for BLAS, you normally write a lot of kernels, which you then apply to blocks of the matrices. The blocks will not be contiugous; each column will be separated by the matrix stride (or rows separated by a stride, if row major). gfortran 8.2 will not succesfully vectorize a matmul kernel (I did file an issue on bugzilla). C (with vector intrinsics) does great. Julia does well too, but not perfect: there are a couple redundant move instructions per for loop iteration. When it comes to applying the kernel to blocks, Fortran will also try and make array temporaries of each block, which would cripple performance. In languages with pointers, all you'd have to do is pass a pointer and the stride between columns. I haven't tried playing with pointers in Fortran, but I guess that would work too? Julia often refuses to autovectorize anything as soon as you start playing with pointers; does Fortran's stance on aliasing mean it fairs better? If not, Julia at least has the LLVM vector intrinsics and metaprogramming that let you force it to generate nearly optimal code despite its protests. But to say something nice about Fortran: dynamically-sized-mutable-stack-allocated-arrays. (-fstack-arrays) In Julia, stack-allocated objects are immutable, and (partly for that reason) also generally very small. You wouldn't want to create a new 30x30 array every time you want to edit a single value. You also can't get stack pointers, meaning you can't use masked load/store operations to vectorize code when the array dimensions aren't a multiple of SIMD-vector-width. This means to write fast code, I normally heap allocate everything. And to avoid triggering the GC, that means keeping the same arrays alive, and avoiding any temporaries. With Fortran, you don't have to worry about any of that. You can always write convenient and clear code, and it's likely to be very fast. If you hold vectorization in mind while laying out your data and computations, compilers will normally do a great job figuring it out.
- cheez 8y agoI just overload operator() for multidimensional data.
- jhasse 8y agoSo the problem with C++ is that you need to write one of foo[a][b] foo[{a,b}] foo(a,b) for multidimensional data instead of foo[a,b] ? Doesn't sound like that big of a deal to me.
- std_throwawayay 8y agoIt's one of those many tiny things that add up. There's good reasons why C++ gets so much hate and there's good reasons why it's used in so many applications. I see no good reason to not have multiple array indices.
- jhasse 8y ago> I see no good reason to not have multiple array indices. It would add a little bit complexity to the language with only a very smal benefit: The same can be achieved with just a few characters more or () instead of [].
- std_throwawayay 8y agoUsing Matrix[i,j] is the most intuitive way for most people. It's what you'd use if you don't know anything and it's what you'd understand immediately. That's the benefit for the user.
- AndrewGaspar 8y agoAnd to be honest, in the list of things that the C++ folks seems to think is important, multi-index index operators seems like it would rank substantially higher than a lot of the other things in the list in terms of value, and a lot lower in-terms of additional complexity.