Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
From: Craig Burley <burley@tweedledumb.cygnus.com>
Subject: Re: fortran error?
Date: 1998/03/18	[thread overview]
Message-ID: <y6g1kf9563.fsf@tweedledumb.cygnus.com> (raw)
In-Reply-To: 350EDA13.2781@still3.chem.columbia.edu


Peter Shenkin <shenkin@still3.chem.columbia.edu> writes:

> Craig Burley wrote:
> > 
> > Peter Shenkin <shenkin@still3.chem.columbia.edu> writes:
> > 
> > > I would really like to see an addition to the standard which requires
> > > inability to "allocate" an automatic to be treated similarly to
> > > the treatment of inability to ALLOCATE an ALLOCATABLE when no STATUS
> > > variable is supplied:  namely, stop the program with a descriptive
> > > error message.  I believe the Absoft compilers do this (at least
> > > the LINUX one.)
> 
> ...
> > Yes, like you might, I can certainly expound on many reasons I
> > know of, having expertise in *those* topics, that your suggestions
> > are infeasible or, at least, would be unpopular among Fortran
> > implementors of most any stripe.
> > 
> > However, as someone whose seen this issue come up over and over
> > again for, well, decades, in various guises, I can really only
> > say "Hallelujah!" and, then, "...we'll figure out later how
> > to make it work reliably *and* fast".

I gather from Pete's reply that my paragraph (above) wasn't clear
enough.  I agree 100% with what Pete proposes regarding the
Fortran language/standard, IMO it's just a matter of the
implementors making it work.  If whatever results is too slow for
some users in some situations, they can use directives to disable
the facilities in question.  But also IMO better compiler and
linker technology can optimize away most, maybe all, of the run-time
checking that would otherwise done.

The way I look at it, stack allocation is almost *never* a
bottleneck in any real application, but *heap* allocation often
is.  If Fortran users wanted straight-out speed at the cost of
inadequate detection of running code, there'd be no reason to
mandate elegant detection of any kind for detecting *heap*
overflows!  (In practice, few applications where heap allocation
are likely to be written in Fortran -- maybe C, C++, Lisp, etc.,
but not usually Fortran, but that's just IMO.)

So, the only reason I can think of that stack allocation isn't
required to be as robust as heap allocation in Fortran is that
implementors knew how they wanted to implement stack allocation
(which, as Peter correctly points out, isn't really called that
in the Fortran standard, so it *can* be done on the heap in
practice), so it didn't occur to them, or they didn't want to
think, to require that *all* dynamic allocation behave the same
way when the allocation cannot be carried out.

E.g. it might even have made some sense to provide a facility like

	REAL, STATUS=ISTAT:: A(N)

to specify "if you can't dynamically allocate an N-element array
named A, instead of bombing with a nice diagnostic, set ISTAT to
some non-zero value, else allocate A and set ISTAT to zero".
That wouldn't be used real often (maybe less often than ALLOCATE's
STATUS arg), but it would be a bit more consistent perhaps.  Even
without this facility, the nice diagnostic would be the key thing!

> I suspect it's not less efficient, or not much less efficient, or
> even more efficient, to test whether the unused part of the stack
> is large enough to hold the automatics that needs to fit there, and
> abort the program with a descriptive message if there's not.

I agree, and, to me, the point is that the *standard* (or default)
behavior should be to incur a bit less extra overhead, if necessary,
to produce a nice diagnostic.  If that last little bit of performance
really needs to be squeezed out, compiler/linker technology can be
improved generally, and the user can be given a compiler/linker
option to dismiss the detection facilities for use in specific
instances.

> Furthermore, the Fortran standard does not have the concept of
> "stack" and "heap", and from the point of view of the language
> definition, the absence of a mechanism for handling errors
> due to too-large automatics is a glaring omission.

Exactly.

> VMS has always been known to run Fortran codes "fast", and Steve
> Lionel mentioned that VMS has had a dynamically growable stack
> since 1980.  Certainly, some of the precedents I cited would be
> be more efficient than that.

I'm not quite sure I know why you conclude that -- on some
architectures and OSes, the dynamically growable stack is
not really at all inefficient, I believe.

> > In the meantime, maybe someone can look into fixing Linux so its
> > default behavior for handling SEGV that seems to involve a
> > stack overflow includes printing some message like "stack
> > overflow".
> 
> I don't know where this comment comes from;  it's certainly not
> properly in response to anything I said -- but perhaps it was
> not intended to be.

I forget what I was responding to, but don't think it was your
comment about Absoft under Linux.  I do know I've run into
this problem myself under Linux, and had to document it
as a non-intuitive thing to consider in the g77 docs, but
I think an earlier post might have made more specific reference
to this issue.

> The Absoft compiler under LINUX is the only one I know that
> terminates the program gracefully with a descriptive message
> when allocation of an automatic fails.  In fact, no UNIX system
> I've ever encountered delivers the message you propose.  Can
> you cite one that does?

Not offhand, but I'm not a UNIX expert.  Maybe the problem is
that SEGV is too widely assumed to be *the* (software-level)
signal for relevant errors, so adding a new one to indicate
stack overflow is impractical.  Certainly under PRIMOS, the only
system I really knew extremely well (and that long ago), the
exception-handling mechanism was quite mature (in design, at
least), coming from the Multics background that largely influenced
the hardware and software, so if you got a stack overflow, you
usually knew it (but the stack could *usually* grow dynamically,
so...).  And, you *could* catch it and continue running, if you
wanted, and even catch a stack overflow in *that* exception, etc.
(It was a lot of "fun" to write code for, in some ways.  ;-)

> Finally, if this can be done, then your proposal is not too bad
> a match for the suggestion of mine that you deprecate:  namely,
> that a descriptive message be printed if allocation of an automatic
> fails due to unavailability of resources.  In fact, I suppose
> I would settle for your less precise diagnostic;  it would be
> better than what we have now.  But if your suggestion can be
> carried out efficiently, I suspect mine can be carried out nearly
> as efficiently.  To UNIX, however, all SEGVs are created equal.

Again, I was *not* deprecating your suggestion, rather, trying to
find a politick (and somewhat humorous) way to suggest that it
was only us implementors who would tend to brush it off, and that
I, as an implementor, saw no valid reason to do so.  I hadn't
wanted to get into a discussion of all the possible implementation
issues, though.

What I don't understand is how your solution of "stop the program
with a descriptive error message" substantially differs from mine
of "have the error message that gets printed when a program stops
(under Linux) due to overflowing the stack be more descriptive".

Any clarification would be welcome.  (Of course, I realize that
fixing Linux is *not* the same as fixing the standard!  My point
is that, if Linux can be fixed, that's a strong argument *against*
not fixing the standard as you propose on the theory that "it would
be too hard and inefficient".)

> > (As far as systems that cannot handle invoking user-defined
> > signal handlers when the stack overflow...well, I don't think
> > I'm used to that, guess there's not as much call for that
> > sort of thing as there was long ago.
> 
> Perhaps it would be slower.  This, following your line of
> reasoning, would be a good reason for not doing it.

No, following *my* line of reasoning, it would be a reasonable
*cost* for an important feature...just as catching the case where
*heap* allocation cannot proceed (due to fragmentation or
too little free memory, whatever) has got to cost *some* cycles
in heap-intensive applications.  IMO, it's up to the implementors
to improve the technology to make it fast enough.

This *is* a bit different an issue from array bounds checking,
but the same thing applies: the implementors *can* improve the
technology to make enabling the feature faster than when done
the "naive" way, and if users (or the standard, in the case
of your stack-allocation proposal) demand both the feature and
high performance, I think I know enough about the technical
issues involved to say that they can and will get both.

> With automatic arrays likely to become a feature of ANSI C (under
> the name "variable-length arrays"), I suspect that omission of a
> more user-friendly mechanism than we now have to handle stack
> overruns will be seen as as a severe quality-of-implementation
> deficit.  People who need to supply reliable code to customers
> will demand it, and will be willing to pay for it with a small
> amount of execution time.  This price, while not nill, is likely
> to be less than the robust alternative of using local ALLOCATABLEs
> instead of automatics.

Agreed.  Quite a long time ago, when this issue came up, I sent
Steve Lionel some email pointing out pretty much what you're saying
-- though more along the lines that, *internally*, stack allocation
could be viewed as an optimization, and use of that optimization
perhaps should defer to *not* using it if doing so will allow
a program to continue running.  I don't think he responded, probably
because it was mostly just speculation as to how Fortran compilers
should work by default (in a quality-of-implementation sense),
but I could dig it out of my archives and post it here (at least
my original material) if desired.

(I have some concerns with any switching from stack to heap
allocation that is based on anything *other* than "stack allocation
of this object is impossible given the current stack situation",
however, which I won't go into here, and since my ideal
basis might be hard or nearly impossible, or too slow, to
determine, I'm not saying this approach is a panacea.)

> For those who want maximum speed for their in-house research
> applications, perhaps this sort of "feature" could be turned off.

Exactly.

> I suspect, however, that one or two runtime tests per procedure
> call are not going to kill the performance of a typical program.

Especially if those tests are only to determine whether to
stop and print a nice diagnosis if "safe" allocation on the
stack cannot be assured.  (Other stuff might be quite
expensive to determine in enough cases to be problematic.)

Note that all this is moot if stack allocation happens to fail
on *procedure* invocation, which of course can happen on most
systems, since the stack is not just where local "user" data
goes!

Perhaps that's where the *real* problem is -- verifying that
each and every "call" instruction (or signal, for the day
when we get exceptions) will either succeed or will produce
a clean diagnostic.  That might become more than one or two
instructions per stack allocation of a user variable -- it
might become one or two (or more) per *call* (plus any
implementation-chosen stack allocation, e.g. to implement
arbitrary CHARACTER expressions or I/O).

And this is where I begin to wonder if whether even the
behavior of ALLOCATE (with or without STATUS=) is all that
worthwhile.  That is, maybe the problem isn't that stack
allocation *doesn't* offer elegant catching of overflow, but
that dynamic (heap) allocation *does*, because, in practice,
it won't always work -- the standard does *not*, AFAIK,
guarantee that exhausting the heap (or stack) results in a
graceful stop or setting some condition code in cases where
the resource is exhausted during *implicit* activities
(like I/O, expression evaluation, etc.).

Basically, we're talking about allocation of fixed resources among
competing entities and how to cope with situations where
the desired resources are not available.

My guess is that, whether we're talking about Fortran 90 or
about Fortran-with-Peter's-feature, we're *still* not talking
about a system that gracefully *and* seamlessly catches
case where insufficient memory is available.

Here are some observations I can come up with offhand that
involve the core issues:

  -  If user code can trap (or at least get a decent diagnostic)
     when some resource *it* wants is unavailable, can it do the
     same when some resource any entity it *invokes* is
     unavailable?

  -  Are there any entities that user code can invoke "unawares"
     that might compete for finite resources?  E.g. can a
     statement like "A = B * C + D", where all names are of
     complicated arrays, or a statement like "CALL FOO(C//'HI')",
     where FOO is CHARACTER*(*), cause the same resources -- like
     the heap or stack -- to be drawn down and thus, possibly,
     exhausted?

  -  If the answer to the above is "yes", does it make any
     practical or worthwhile difference whether the stack or
     heap is exhausted due to an *explicit* user-code allocation
     or to an *implicit* one done, perhaps, in the very next
     statement?  Is that difference sufficient to provide the
     facility throughout?

  -  If the answer was "no", does that mean that partitioning
     of resources is employed in a way that might result in
     "premature" exhausting of one or the other partition?

  -  If not, is the dynamic relocation of the partition boundary
     expensive -- for code that doesn't need to do it, or for
     code that does?

  -  Etc.

Consider this code:

	PROGRAM CRASHME
	IMPLICIT NONE
	INTEGER N

	PRINT *, 'Give me a big number to try and crash on.'
	READ *, N

	CALL MRBIG(N)
	END

	SUBROUTINE MRBIG(N)
	IMPLICIT NONE
	INTEGER I, N
	CHARACTER*(N) C
	CHARACTER*1 CFUNC
	EXTERNAL CFUNC

	DO I = 1, N
	   C(I:I) = 'X'
	END DO

	PRINT *, 'Success so far... ', CFUNC(C//'FOO')
	END

	CHARACTER*1 FUNCTION CFUNC(C)
	IMPLICIT NONE
	CHARACTER*(*) C

	CFUNC = C(LEN(C)-2:)
	END

I haven't tried this myself, so it might need some fixing up, but
the important aspects are:

  1.  Dynamic (stack) allocation of C in MRBIG which should be "caught",
      but at least is explicit

  2.  Dynamic (stack? heap? separately partitioned?) allocation for
      the temporary C//'FOO' in the call to CFUNC, which is implicit

The question I'm asking is, is it sufficiently useful to have
the Fortran standard, or even an implementation, elegantly catch
stack overflow (or other resource exhaustion) in case #1 but *not*
case #2?  If so, why, since the cases are, in practice,
indistinguishable in a running program?  How is it useful for
this, or any similar, program to think it's succeeded at
allocating an object and then proceed to do computations that
are "rudely interrupted" by a graceless, uncatchable resource
exhaustion shortly thereafter?

The way to try this out is to repeatedly invoke the program, and
enter numbers (using a binary-search technique) to find the
region of numbers where the program neither runs to completion
*nor* elegantly catches stack/heap full the way it does
when it happens in case #1 above.  (I'd love to know what
results someone gets from Absoft under Linux, for example.
Note that the program might need editing to fit the dialect
provided by any given compiler...it won't work under g77 as is,
for example.)

If that region is not empty, then that suggests the implementation
does *not* provide a graceful *and* seamless way to catch stack
or heap exhaustion.

Note that a third element that might factor into the above is:

  3.  Dynamic (stack? heap? separately partitioned?) allocation for
      the PRINT and END statements themselves (that is, the I/O
      and other such things).

In summary, there might be *some* utility in elegantly catching
stack/heap exhaustion *only* on explicit *user-code* allocations,
but that utility does not scale up well as the complexity of
applications -- which might mix stack and heap allocation, make
lots of small-stack allocations (making procedure calls more
likely to overflow the stack), and so on -- scales up.  And
some implementations that work well in small, simple cases might
actually cause more trouble (other than just run-time performance)
in more complex cases, e.g. always, or even sometimes, allocating
on the heap instead of on the stack (which can lead to fragmentation
that is not apparently going to result by examining the Fortran
code in question).

However, regarding the Fortran standard, little of the above
necessarily matters whether Peter's proposal is included --
because the standard mandates catching problems only with
explicit, *user-triggered* exhaustion of resources, so
implementations that do just that and not anything else can
be standard-conforming, even if they might serve as poor platforms
for large-scale applications due to not being able to catch
problems resulting from implicit exhaustion of resources.

In other words, to *really* address the problem of elegantly
catching exhaustion of resources (like memory), the *entire*
language and its implementations must be designed to do so.
Fortran is not an example of this (yet), but other languages
(like PL/I perhaps, or Ada) probably exist that are.
-- 

"Practice random senselessness and act kind of beautiful."
James Craig Burley, Software Craftsperson    burley@gnu.org



  parent reply	other threads:[~1998-03-18  5:00 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-03-12  5:00 fortran error? Armin Leuprecht
1998-03-12  5:00 ` robin
1998-03-16  5:00 ` Steve Lionel
1998-03-17  5:00   ` Peter Shenkin
1998-03-17  5:00     ` Craig Burley
1998-03-17  5:00       ` Peter Shenkin
1998-03-18  5:00         ` Toon Moene
1998-03-18  5:00           ` Jeff Drummond
1998-03-19  5:00           ` Peter Shenkin
1998-03-18  5:00         ` Craig Burley [this message]
1998-03-19  5:00           ` Peter Shenkin
1998-03-19  5:00             ` Craig Burley
1998-03-20  5:00               ` Peter Shenkin
1998-03-20  5:00                 ` Craig Burley
1998-03-20  5:00                   ` Nick Maclaren
1998-03-20  5:00                     ` Richard Maine
1998-03-20  5:00                   ` Peter Shenkin
1998-03-23  5:00                     ` Craig Burley
1998-03-23  5:00                       ` Craig Burley
1998-03-23  5:00                         ` Peter Shenkin
1998-03-24  5:00                           ` Craig Burley
1998-03-24  5:00                             ` Nick Maclaren
1998-03-23  5:00                       ` Summary (I hope) failure of automatic allocation; was, " Peter Shenkin
1998-03-24  5:00                         ` Craig Burley
1998-03-20  5:00                 ` Richard Maine
1998-03-20  5:00               ` Nick Maclaren
1998-03-20  5:00             ` Dick Hendrickson
1998-03-20  5:00               ` Peter Shenkin
1998-03-20  5:00               ` Craig Burley
1998-03-20  5:00                 ` Peter Shenkin
1998-03-23  5:00                   ` Craig Burley
1998-03-18  5:00     ` Steve Lionel
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox