From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,1ef3b84ca62fad7b X-Google-Attributes: gidfee84,public X-Google-Thread: 1094ba,1ef3b84ca62fad7b X-Google-Attributes: gid1094ba,public From: Craig Burley Subject: Re: fortran error? Date: 1998/03/20 Message-ID: X-Deja-AN: 336063877 References: <3507A4A5.F8A7DDC2@matd.tu-graz.ac.at> <6ejfii$kbd@usenet.pa.dec.com> <350E95A7.41C6@still3.chem.columbia.edu> <350EDA13.2781@still3.chem.columbia.edu> <351181DB.2781@still3.chem.columbia.edu> <351296BC.167E@still3.chem.columbia.edu> Organization: Cygnus Support Newsgroups: comp.lang.fortran,comp.unix.osf.osf1 Peter Shenkin writes: > Craig Burley wrote: > ... > > Essentially, using FORTRAN 77, it is fairly straightforward to > > analyze a complete program and determine whether it will run > > to completion in a given amount of memory, because there's > > no ability to explicitly *or* implicitly demand dynamic > > allocation of memory resources (stack or heap). (Note that I corrected myself in private email who misunderstood what I wrote about. Replace "...will run to completion in a given amount of memory..." with "...will run without ever exhausting a given amount of memory...", or some such thing.) > I think this isn't completely true, but it certainly became a lot > less true, as you point out, with the advent of the new features > of F90. > > Even in F77, it would have been difficult to determine the maximum > depth of subroutine calls that would be possible for a given program. > It would have been possible to compute an upper bound, since, in > the absence of recursion, an analyzer could assume that every procedure > was chained together in one single call chain, and compute the stack > requirements of this chain. But this would almost always be a severe > overestimate of what the real program does. It's quite easy to compute a call tree and thus flatten the estimate of the maximum, though. What I'm trying to say is probably not what I'm saying, I'm afraid, but I'll try again: in FORTRAN 77, it is fairly easy and straightforward to determine that a given program will not only *fit* into a given amount of memory, but will never ask for any *more* memory while running. Some factors make this more difficult, e.g. if the underlying run-time implementation dynamically allocates memory, but it's not the language definition or even many typical extensions that require this -- though practical needs might (e.g. an implementation that wishes to support 1000 simultaneously OPEN'ed files might choose to dynamically allocate unit info to reduce demands on virtual memory when many fewer files are open). > But the even simpler analysis of determining the stack resources > required for each subroutine, and refusing to start if the stack was > too small, was never, as far as I know, done. Oh, sure it was, in *effect* if not explicitly. On some now-ancient machines, you compiled your Fortran modules, you linked them together, and you got an executable. If it was too big, you couldn't run it in a given amount of memory -- heck, in some cases, that meant you couldn't *link* it in the first place! But, if it was *not* too big, then you *could* run it, for as long as you wanted, with no more memory being requested, guaranteed by the facts that your code was FORTRAN 77 and that the underlying implementation didn't dynamically allocate more memory than was initially requested by the executable. E.g. the Prime 400 FTN compiler was basically done this way, and maybe F77 as well (though I think that always generated 64V-mode or 32I-mode stuff, which means it could have had a code generator that assumed it could dynamically allocate stack and/or heap). There might have been some "hiccups" in the underlying libraries that could cause stack overflow, but I think, in practice, we just figured out where to set the stack based on some straightforward analysis of a given application, and could assume, with a reasonable amount of assurance (and could get more if we really wanted), that, once successfully invoked, the executable could run "forever" without exhausting memory. (I think we're now so spoiled by instant gratification, resource-wise, that we forget how much time and energy was spent *designing* systems, not to mention implementing them, to not only elegantly cope with dynamically discovering a lack of sufficient resources, but knowing *ahead of time* not to even *start* a job without first being sure all the resources that job needed would be available. Sadly, I can only point to MVS JCL in comparison to, say, UNIX `sh' to illustrate this offhand, but, though JCL was incredibly hard to read and write, it did allow, perhaps require, expressing the sum total of resources needed by a job so the system could know when and whether to start it up. FORTRAN's memory model fit into this world-view beautifully. That sort of thing seems like overkill at this particular point in time, when hardware keeps getting bigger and faster while also getting cheaper, and when reasonable responses to "my program crashed with SEGV" include "increase your stack size" *and* "buy more memory". But if the hardware trend ever smooths out, our software needs will begin to catch up and, once again, we'll return to seeing the need for a system to have a much better idea of whether a given task will actually run to completion, based on resource needs, *before* having to start that task...because our tasks will keep growing, I would assume, even when hardware stops growing so fast. After all, the answer to "buy more memory" a couple of decades ago might have been "but that'll cost me $100,000!!".) > For instance, if > some subroutine declared a local as "REAL A(100000000)", and if > locals are allocated on the stack, and if there is not enough stack > space for 10^8 reals, it should be possible, in principle for the > run-time system to refuse to start up the program, rather than to > incur a SEGV when the subroutine is entered. There's no reason it has to be put on the stack. There might not even *be* a stack. This is FORTRAN 77, after all -- there's no recursion, no automatic arrays, no allocatable arrays, hence no need for a stack *or* a heap! For *efficiency*, I believe it makes a lot of sense to put stuff on the stack. That can lead to efficiency of CPU time (stack frames for different procedures more likely to overlay each other in memory, thus be in cache) and, of course, memory. But, there's basically no technical impediment for a fairly vanilla implementation of FORTRAN 77 to produce a pretty decent, and highly useful, value for maximum amount of memory needed by a given program even *if* that implementation decides to put some local variables and arrays on the stack for some procedures. After all, since the call tree is so easy to produce, so is the "overlayability" of stack frames. That is, two procedures that do REAL A(10000) might require a total of 20000 32-bit words for those arrays *unless* the call tree shows that neither procedure is called while the other is active, in which case use of the same static space, or use of the stack frame for each procedure's implementation, allows both arrays to use a total of 10000 32-bit words. (And maybe other local variables for other procedures can be easily combined with this as well.) To do this today, portably, would require somewhat better linker/library technology than we have now. The problem is 1% technical, 99% writing and getting agreement on all the specifications. Okay, maybe I'm exaggerating, but, trust me, if I devoted myself to making a version of g77 that ran on, say, only GNU/Linux/Alpha systems, with no regard for portability, providing a facility whereby the linker can warn if the program won't certainly run in x bytes of virtual memory would be fairly straightforward, I believe. And putting its computation of the lowest x it can safely determine into an executable, so the OS (Linux) can be sure to reserve that amount of VM for the duration of the program on invocation, or refuse to invoke, would be straightforward as well. And I'm not talking about some wild exaggeration of x either, *except* once features beyond FORTRAN 77 are discussed. (I'm a bit uncertain how analyzable the current libraries would be using the techniques I imagine, but it might be easy enough to replace the more intricate stuff with simpler implementations that would be easy to automatically analyze while still meeting the needs of FORTRAN 77.) I just don't think many people care about this sort of thing these days. When they run into a problem, it's fixed either by adding memory (real cheap), fixing a memory leak in the program, or adding specialized code to the program to detect when *likely* candidates for running out of memory do so. None of these solutions *eliminate* out-of-memory problems for that application, but they succeed often enough, and system configurations, applications, and Fortran implementations change so fast, that it probably seems a waste of resources to make a bigger effort to get it all "right". I think this is another example of how the whole "bigger, faster, cheaper" drive in computer software (and sometimes hardware) design also has led to "buggier, crashier, feature-creepier" as well. To wit, others here, with different experiences than mine, talk about how easy debugging used to seem back when array bounds checking was *automatically* on, even though they had to debug via batch jobs! (For myself, I guess I can defend my choices for g77 only by saying I've tried to make g77 what it is supposed to be -- a replacement for UNIX f77 at least. I'd like to make it more like the kind of safe, robust compiler I think would be really useful, but I'm also quite aware that *most* of what people want out of it are "bigger, faster, cheaper"-type things. g77's biggest failings right now are mostly in the arenas of ease of debugging, support of extensions and F90 features, and packaging, whereas lots of people do sing praises about its performance, especially the egcs version. g77 doesn't even have the array-bounds-checking option that f2c does -- yet.) > One more thing that you referred to, but that deserves to be > emphasized, is something that Sergio Gelato also mentioned to > me in email. I started this thread by saying that I wished that > the Fortran standard would mandate a program STOP when an automatic > cannot be allocated. Sergio -- and you, but less explicitly, I > think -- pointed out that this also pertains to temporary arrays > that the run-time system allocates. Yup. > As we go from ALLOCATABLE arrays to automatic arrays, we are going > from an explicit to an implicit dynamic memory request. When we go > from these to a compiler-requested temporary array, we are going to > a completely hidden memory request. Clearly, these can cause the > same problems as automatics when allocation fails, and the standard > should mandate more graceful error handling for these arrays as > well -- even if the program is merely stopped on failure to allocate. Right. As Sergio (I think it was) said in private email, the simple one-line answer is that Fortran needs a (IMO very robust) exception mechanism. In the meantime, as long as programmers are made aware of the dangers, and have at least some of the facilities in question (ALLOCATE's STATUS=, or whatever), they at least have the ability to avoid some of the most common problems, especially if they know to avoid expressions that tend to allocate huge temporaries on various systems. One trick, for example, before entering a region of code that might crash due to implicitly allocated temporaries, is to allocate and then deallocate a "guard" array of sufficient size to ensure that, if it succeeds, then the implicit allocations will. To avoid being thwarted by a system that does "lazy allocation", i.e. one that can return "success" for an allocation but later seg-fault a reference to the allocate area because there turned out to be insufficient memory after all, the "guard" array would have to be written to, etc. The end result is that, if enough programmers find themselves doing tricks like the above to guard against their applications bombing out unexpectedly, that'll illustrate the practical need for something like an exception mechanism to replace these. But these tricks are haphazard, i.e. it's quite easy to forget to use them in a crucial place. They are certainly kludges, so using them "in the meantime" does not mean the problem has been solved, even in the short term. -- "Practice random senselessness and act kind of beautiful." James Craig Burley, Software Craftsperson burley@gnu.org