4 ms·
> 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
by 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.
- cygx 8y agoHeap-allocated dynamically-sized NxM matrix in C99: double (*mat)[M] = calloc(N * M, sizeof (double));
- int_19h 8y ago
- 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