From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 102b75,45b50f14dadfce5b X-Google-Attributes: gid102b75,public X-Google-Thread: 110292,45b50f14dadfce5b X-Google-Attributes: gid110292,public X-Google-Thread: 1164f8,45b50f14dadfce5b X-Google-Attributes: gid1164f8,public From: Craig Burley Subject: Re: Final kick at the "Merced Guessing Game" Date: 1997/10/13 Message-ID: X-Deja-AN: 280168401 References: <61g30t$o2n@news-rocq.inria.fr> <343C34A7.B715A348@tonys.ecology.umn.edu> <3440F805.C98BB347@tonys.ecology.umn.edu> <61tfaa$9fr@sis.cambridge.arm.com> Organization: Cygnus Support Newsgroups: comp.sys.intel,comp.arch,comp.sys.dec michael.williams@arm-sponge.com (Michael Williams) writes: > In article <3440F805.C98BB347@tonys.ecology.umn.edu>, > boo wrote: > >I did not say there was ONE efficient way. If you are just looking > >for ONE efficient way, then perhaps you should stick with Fortran. > >As I said before, Fortran is for the lazy, the feeble-minded, > >the incompetent, and for those who aren't doing anything beyond > >simple-minded number crunching. > > I've tried to read both sides of the argument in this thread so > far. As far as I can tell, the argument is that, whereas it is > possible to do multi-dimensional arrays in C, it's not possible to > deal with them efficiently, if you write the 'natural' code. It's *possible* for a compiler to deal with them efficiently. It's rare for a compiler to *actually* do so in practice. Programmers with sufficient time and/or interest can always "lower" their code in C -- or Fortran, a fact which nobody seems to have bothered with, since Fortran already lets you just type it in the "natural" way and let the compiler do the work for you. For example, a Fortran compiler in beta test that is nevertheless widely used, GNU Fortran (g77), in some cases generates substantially better code if you replace array declarations and references like "REAL A(M,N)...A(I,J)" with "lowered" ones like "REAL A(0:M*N)... A((J-1)*M+(I-1))" (or whatever the equivalence would be). The reason is that the back end it uses is C-oriented, and it has been pretty well optimized to produce good code for "lowered" C code to do some of these things, whereas g77, given the higher-level Fortran constructs (which cannot be expressed in C, ANSI C, or C++), implements them via back-end mechanisms that are not as well optimized. (Although this is being worked on....) However, some g77 users have discovered that turning on an old macro definition in the g77 sources causes it to convert the higher-level Fortran constructs directly into the lower-level C-like form when it comes to calling on the back end -- and this *also* results in generating (the same, or similar) faster code. In other words, while using C *requires* you to "lower" your code to get anything like decent efficiency *and* use a pretty sophisticated optimizing compiler to "discern" what you're really trying to do, using Fortran *allows* you to leave your code in its "original", higher form. Then, if your Fortran compiler produces great code from it, wonderful; if not, you can then lower your Fortran code just like you can with C (though a C preprocessor is not standard with Fortran, you can use statement functions where you'd use C macros for this) *or* you can just modify your compiler -- the latter being far easier to do when it's a Fortran compiler than a C compiler, as illustrated by the g77 example. Since g77 is in beta test and has only recently had performance issues come under some scrutiny in terms of actual improvements to the compiler, I suspect few production Fortran compilers *require* lowering of Fortran code to get substantially better performance, except perhaps in rare cases. Of course, once you "lower" your code -- either by changing your Fortran code, or by writing your application in C -- you've lost some *other* features, like ease of debugging, and catching out-of-bounds references (especially the kind that are out of the bounds of their dimension, where the complete array-offset calculation is *not* out of bounds -- which is nevertheless a programming error). A similar example of C *requiring* lowering because it doesn't provide linguistic features other languages (like Fortran) have is illustrated by the following Fortran snippet: IMPLICIT LOGICAL(A-Z) BOOL = F() .AND. (G() .OR. H()) C has no way to express the above. It *requires* you to lower it, to, for example, bool = f() & (g() | h()) or: bool = f() && (g() || h()) C programmers typically claim this "low-level" nature of their language *allows* them to write "faster" code for a particular machine, but, in practice, that isn't true in cases like the above, because even Fortran allows such a thing. The Fortran versions of the above "lowered" code are BOOL1 = F() BOOL2 = G() BOOL3 = H() BOOL = BOOL1 .AND. (BOOL2 .OR. BOOL3) and IF (F()) THEN IF (G()) THEN BOOL = .TRUE. ELSE IF (H()) THEN BOOL = .TRUE. ELSE BOOL = .FALSE. END IF ELSE BOOL = .FALSE. END IF (respectively). In both cases clunkier, but in both cases *semantically equivalent* when it comes to code generation. That is, aside from other, unrelated semantic differences between C and Fortran, the compilers have pretty much the exact same degrees of freedom when it comes to generating fast code. E.g. in the first lowered versions, each compiler *must* ensure that all three functions are called (and thus their side effects performed), whereas in the second lowered versions, each compiler must ensure that the functions are called in order and only depending on the results from the previous calls, again, due to side effects. However, the *original* Fortran code is (assuming it's correct) most faithful in expressing the original problem. As soon as you write it in C, you've lost information regarding how flexibly the implementation can be in calculating the answer, but while it's in Fortran, the implementation can even do things like run-time determination of which of F(), G(), and H() will take longest to run and call them in any order, or call them in parallel, or call only one or two of them, or whatever -- stuff a C compiler can do *only* if it does far more global analysis than the Fortran compiler typically needs to do. (Although, just as C programmers often overstep what the standard lets them do, meaning compilers must sometimes avoid doing otherwise-okay optimizations, the same is true for Fortran. E.g. a C compiler could, but users might otherwise complain if it does, optimize "*p1 ^= *p2 ^= *p3 ^= *p4;" in a way that "breaks" if p1==p3 and/or p2==p4. But, similarly, Fortran users might complain if "BOOL = F() .AND. G()" doesn't actually perform the side effects of F() when G() evaluates to .FALSE.. In both cases, the users are requiring the compiler to implement a more strict form of their respective language, something real-world compiler writers must take into account.) In summary, C often *requires* you to write *lowered* code instead of expressing what you mean directly, whereas languages like Fortran merely *allow* this. In fact, I was talking with someone at Dakota Scientific Systems (sp?) a few months ago. They take (normally Fortran) code doing real number-crunching and optimize the heck out of it for a particular platform/chipset/whatever. E.g. stuff like BLAS, IIRC. Anyway, what *really* surprised me was that the guy explained that they do *not* simply take the compiler's output and optimize the assembly -- instead, they optimize the *original Fortran*, leaving the optimized code in Fortran! That is, they move assignment statements around, and, I'd guess, "lower" the code when necessary (probably rare), to get the *compiler* to produce the native code. And, apparently, they get at or near industry-best results this way. Proof, or persuasive evidence, that pretty much any lowering C requires you to do, Fortran *allows* you to do, especially in the realm of numerical computing. There's little doubt in my mind that C offers great notational convenience, though. I'm much happier, for now, writing compilers in C than Fortran or, say, PL/I, *because* it has `&&' and `||', among other things. Yes, sometimes I'd like to use the (missing) higher-level constructs and let the compiler decide, but in the kind of code I write (non-numerical), it doesn't usually matter -- the performance relates more to algorithm selection and overall design than to low-level implementation issues. But, in my experience, the numerical types who use Fortran *already* have their algorithm issues worked out, because they're engineers, not programmers, and because they *have* to. E.g. it doesn't bother me much if my compiler is twice as slow as it could otherwise be on my home PC, at least, not compared to how much these numerical types are bothered when their latest number-cruncher is 2% slower than it could otherwise be, when that 2% costs them $10,000 per week due to it being run on a supercomputer on which they're buying time. C programmers just don't usually have as much vested interest in squeezing out that last 2% or so -- after all, their users can just wait for the next Pentium to come out anyway, while Fortran programmers include a pretty substantial group that depends on today's fastest and most expensive computers, and a newer, faster computer only means they can stuff more data into, or make more accurate calculations in, their programs, instead of using the next generation to cover up their programming inefficiencies. (My take on the difference between an engineer and a programmer: an engineer takes real-world complexities into account when designing his system, and thus tends to produce a simple, robust system; a programmer ignores real-world complexities so he can design an unnecessarily complicated system. For example, if Microsoft designed the Golden Gate Bridge, it'd allow traffic in only one direction at any given time; if two cars got on at the same time in opposite directions, it'd fall into the sea ("user error"); you'd have to drive a Microsoft-approved car to go 10mph, otherwise you'd have to go below 5mph; you'd have to use that bridge to drive from LA to San Diego in your Microsoft-approved car; you'd be unable to drive over any *non*-Microsoft bridge in your Microsoft-approved car; and they'd expect to be funded to "upgrade" the bridge two years later, which would mean they'd take the money, tear down the old bridge, and build a new bridge, requiring slower speeds, somewhere else. :) > The other part of the argument probably centers around the way the > various Fortran advocates[1] seem to know what they're talking about, > whilst being insulted and derided by their opponents. I don't know > about the intel and dec groups, but it's a bit out of place on > comp.arch. Well, if the Dallas Cowboys played a "real" game against some high school's team, I wouldn't consider the high-school team to be their "opponents", no matter how good that team was at trash-talking. Even if the score ended up in favor of the high-school team, nobody in their right mind would assume that meant they were "better" than the Cowboys or (most) any other NFL team. In other words: there's been no real "contest" here. Childish insults from the (umn).edu side of things, yes, but no contest. -- "Practice random senselessness and act kind of beautiful." James Craig Burley, Software Craftsperson burley@gnu.ai.mit.edu