From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1164f8,45b50f14dadfce5b X-Google-Attributes: gid1164f8,public X-Google-Thread: 102b75,45b50f14dadfce5b X-Google-Attributes: gid102b75,public X-Google-Thread: 110292,45b50f14dadfce5b X-Google-Attributes: gid110292,public From: harley@pauillac.inria.fr (Robert Harley) Subject: Re: Final kick at the "Merced Guessing Game" Date: 1997/10/08 Message-ID: <61g30t$o2n@news-rocq.inria.fr>#1/1 X-Deja-AN: 278715918 Organization: I.N.R.I.A Rocquencourt Newsgroups: comp.sys.intel,comp.arch,comp.sys.dec xcc@hilbert.ncsc.org (Carlie Coats) writes: >When I'm programming in C or C++, why the *_hell_* can't >I do easy things like > > void foo( const int m, > const int n, > const int p, > float a[p][n][m] ) {...} I think your problem is that C is too flexible for you. It doesn't wrongly assume that there is one way of laying out and accessing arrays that is appropriate for all purposes. For instance you can choose a flat array, 'float *a', or a hierarchy like 'float ***a'. Suppose you choose the flat array, Fortran style. You can access a_{i,j,k} as a[(i*n+j)*m+k] or several other linear combinations depending on whether you like row-wise or column-wise access or whatever. Your hand isn't forced like in Fortran. If you're doing simple constant-stride access, decent compilers will strength-reduce the multiplications just like decent Fortran compilers (or the pre-mungers that they often require to get top performance). However a non-trivial access pattern will use a lot of slow multiplications, just like in Fortran. In that case you have the option of strength-reducing by hand if you know better than the compiler. Also, this often forces you to waste a lot of space, doubling the size of triangular matrices with useless zeroes, increasing the size of simplicial 3D arrays sixfold etc., just like in Fortran. Furthermore to swap rows, you have to swap all the elements, which takes forever just like in Fortran. Suppose that you choose a hierarchy instead. Then non-trivial access patterns require pointer accesses, however these are typically fast since the number of pointers is very small compared to the size of the data. For instance on this Alpha with a 100x100x100 array the top level fits in L1 cache easily, the second level in L2, whereas the data is twice as big as L3. Also you can allocate exactly as much memory as you want or need. Furthermore you can swap rows (or columns) with a single pointer swap. In some cases the former is best, in others the latter. Of course, C also lets you choose intermediate layouts such as an array 'float **a' of pointers to rectangular matrices, if that is best for your application. Perhaps you'd like to tag those sub-matrices with their dimensions: typedef struct { int width, height; float *data; } rectMatWithSizes; rectMatWithSizes funky[100]; and set it up whatever way you like. In other languages you might be forced to use training wheels, or you might be lucky enough to be permitted to hack up some imitation of the C style. I'd just use C. Bye, Rob. .-. Robert.Harley@inria.fr .-. / \ .-. .-. / \ / \ / \ .-. _ .-. / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / `-' `-' \ / \ / \ \ / `-' `-' \ / `-' Linux + 500MHz Alpha + 256MB SDRAM = heaven `-'