Phillip Helbig---undress to reply <
hel...@astro.multiCLOTHESvax.de>
wrote:
> ...optional output parameters...
First note a terminology quibble. They are arguments - not parameters. A
parameter in Fortran is a named constant.
> If the output parameters don't depend on anything
> except the input parameters (i.e. they are "pure"), then, assuming they
> are SAVEd parameters, one doesn't have to calculate them again. So,
> would pseudocode like the following work?
>
> IF (same input parameters as last time) THEN RETURN
No. Not at all. First, your assumption is never true. Arguments cannot
have the SAVE attribute at all. The initial value (if any) of a dummy
argument never depends on that dummy argument in a previous invocation.
(Well, there used to be some old and nonstandard features sort of
related to that, but nothing close to standard or in any compiler you
are likely to be using). If it is an intent(out) argument, then it
becomes undefined when the procedure starts, possibly then modified by
default initialization if applicable.
If it is intent(inout), then it gets its initial value from that of the
actual argument. If the actual argument is the same as the previous
call, that might have the effect you are looking for. But it would also
make the correctness of the procedure's internal implementation depend
on details of how it was called. Sounds error-prone to me. And confusing
to document, as your "output" argument is no longer actually that. (See
notes below about clarity and correctness relative to optimization).
You could possibly save the results in a local variable, which you then
copy to the dummy argument as appropriate, but note that makes the
routine impure and might be counter-productive because of that.
> Could this be avoided by the following pseudocode?
....
> CONTAINS
>
> internal1 = blabla
> internal2 = blabla
I'm afraid I have no idea what this pseudo-code implies. You can't put
executable code after a CONTAINS - only procedures (or bindings in a
derived type). If your pseudo-contains is something other than that of
Fortran, I can't guess what it is.
> In other words, if variables are module variables rather than local
> variables in the subroutine, does this prevent them from being optimized
> away?
>
> If not, would separate compilation prevent them from being optimized
> away? That is, first compile the module containing the subroutine then
> link it to a separately compiled caller program?
I don't think I understand those questions. You appear to be talking
about "optimizations" that your code implements rather than compiler
optimizations. In that case, I don't quite see why the question. The
code does... well... whatever it does. I don't see how anything would
"prevent" it from doing so. I suppose you might be asking whether SAVE
works for module variables. Yes, it does. In fact, there have been
proposals to make all module variables SAVEd by default. That part isn't
so (at least as of f2003 - I can't speak to f2008), but you can
certainly declare them to be SAVE'd explicitly.
*HOWEVER*...
My initial impression of this whole idea is that it seems like the kind
of micro-optimization that is often counter-productive. Concentrate
first on making code clear (and correct). All of this is highly
counter-productive in that regard. Just calculating the outputs based on
the inputs is clear and simple. Getting the output values from somewhere
that they might or might not have been saved is not nearly so simple,
even before getting into questions about making it work correctly.
There are certainly cases where saving previously computed values can be
a big win. Heck, that's the basis of some data compression schemes
(including some I have done myself). In that case, the saving is more in
file size and communication bandwidth than directly in computation time
(it tends to cost in computation time).
But such schemes pretty much inherently hurt you in clarity, simplicity,
and also in getting them right. There's an old, old adage about
optimization, which lists a lot of things to do before optimization.
Those things to do before optimization include making the code clear and
correct. That adage is as important now as it ever was... and some
people ignore it now, just as they always have. The result of ignoring
it so often is code that is unclear, buggy.... oh, and sometimes not
actually faster anyway.
I would certainly say that the kind of "optimization" you are talking
about should not at all be your usual practice. *IF* you have concluded
that the computation some particular variable is a performance problem
significant enough to merit special treatment, then and only then might
it be worth looking at tricks like this for that particular variable.
And in that case, you should also be looking at other approaches.
--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain