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: William Clodius Subject: Re: Final kick at the "Merced Guessing Game" Date: 1997/10/08 Message-ID: <343C0932.167E@lanl.gov>#1/1 X-Deja-AN: 278816947 References: <61g30t$o2n@news-rocq.inria.fr> Organization: Los Alamos National Lab Newsgroups: comp.sys.intel,comp.arch,comp.sys.dec Robert Harley wrote: > > > 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. There are two points here, how arrays are laid out, and how they are accessed. The compiler/machine determines how they are laid out (given additional restrictions implicit in the code or language), the programmer determines how they are accessed. Fortran has fewer assumptions about how the machine lays out memory than C does. It allows users the same flexibility in access that C does, at least for teh examples that you give. > 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. Fortran doesn't force this, it simply povides a less error prone, more easilly understood and optimized construct for doing the equivalent. > > 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). As KAI about pre-mungers for C/C++. > > 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. Same in Fortran. > > 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. Fortran does not require the initialization of unused space. > > 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. Same in Fortran > > Also you can allocate exactly as much memory as you want or need. Same in Fortran > > Furthermore you can swap rows (or columns) with a single pointer swap. Try Fortran array operations, not as good as APL but still... or Fortran POINTER's > > 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]; Different syntax using derived types in Fortran TYPE rectMatWithSizes INTEGER :: width, height REAL, POINTER :: data(:) END TYPE TYPE(rectMatWithSizes) :: funky ALLOCATE(funky%data(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. > I'll take a language that lets you say more things more clearly. -- William B. Clodius Phone: (505)-665-9370 Los Alamos Nat. Lab., NIS-2 FAX: (505)-667-3815 PO Box 1663, MS-C323 Group office: (505)-667-5776 Los Alamos, NM 87545 Email: wclodius@lanl.gov