Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Fortran Programming Language

fortran maintenance tools


Hi,

For the past 10 or so years I've been working on other people's
Fortran codes, mostly to parallelize and optimize. These codes evolve,
they're not created. That and the fact that they're in the 100k-200k
line size means they're too big for me to understand and I just hate
modifying them because I'm afraid of screwing things up. Needless to
say, I constantly have to do things like figure out where values come
from or will be used, rewrite a loop nest so it will pipeline on an
Itanium, put code around every subroutine call to collect performance
information, or pull out a large section of code from a ridiculously
large subroutine and turn it into its own subroutine.  It's the usual
maintenance on large programs where performance is important. I do it
all the time, but I'd like help doing it faster.  What I'd like is an
interactive tool that would help me with this. It would help me
understand such programs and help me make changes in a systematic
way. By systematic I mean it can check to make sure I haven't violated
certain assertions or, better yet, can make the changes for me. I want
something that's interactive and also has abilities found in a
compiler.

I haven't seen anything like this. There are tools that show call
graphs, will do pretty printing, and even some documentation, but I
haven't even found anything that will show data flow information, not
to mention something that can modify code.

So, I'm thinking of building something. I've built compiler tools
before so I have a certain amount of code already available. But this
is still anything but trivial. The question I have is would anyone
else be interested in it?

Matt

MattR wrote:
> Hi,

Hi.

> For the past 10 or so years I've been working on other people's
> Fortran codes, mostly to parallelize and optimize.

Sound like you might have many interesting examples
and stories to share.  Are you on the lecture/training
circuit?

> I haven't seen anything like this. There are tools that show call
> graphs, will do pretty printing, and even some documentation, but I
> haven't even found anything that will show data flow information, not
> to mention something that can modify code.

FWIW, Phortran aims to do some of what you're after -- see

  http://www.eclipse.org/photran/

Regards,
--
Bil Kleb
http://funit.rubyforge.org

I sure hope they rename it.  The name sounds sorta goofy to me.

> Regards,
> --
> Bil Kleb
> http://funit.rubyforge.org

--

Gary Scott
mailto:garylscott@sbcglobal dot net

Fortran Library:  http://www.fortranlib.com

Support the Original G95 Project:  http://www.g95.org
-OR-
Support the GNU GFortran Project:  http://gcc.gnu.org/fortran/index.html

If you want to do the impossible, don't hire an expert because he knows
it can't be done.

-- Henry Ford

On May 2, 12:11 pm, Bil Kleb <Bil.K@NASA.gov> wrote:

> Sound like you might have many interesting examples
> and stories to share.  Are you on the lecture/training
> circuit?

I'm not sure what the name of my circuit is :) I did a post-doc
at ICASE, and at PNNL (a DOE lab). Now I do contract work, mostly
for PNNL.

> FWIW, Phortran aims to do some of what you're after -- see

>  http://www.eclipse.org/photran/

I looked at that and I want the next generation past that.  Let me
give a more concrete example. I started with a roughly 100k line
F95 code that was getting 1.5% peak performance on one Itanium
processor. After nine months I got it up to 20% of peak performance
across 1800 processors. The first six months of this was wasted
vectorizing and rewriting loops. Afterwards I figured I was going to
have to customize the code to a specific problem configuration. i.e.,
there are hundreds of variables that don't change within the time
stepping loop and I essentially rewrote the time loop based on a
specific set of configuration variables. That removed all sorts of
conditional code that was messing up the optimizer.

The kinds of things I need to do include finding all the variables
within a loop that don't change (data flow across multiple files and
subroutines), insert code to capture those values, create a module
with
parameter statements that represent those values, and replace every
use of one of the variables with the correct parameter. I also need to
rewrite loop nests to enable pipelining. I did it by hand but it was
slow
and I don't trust the code.

Tell me if I'm wrong, but Phortran doesn't have much more than
syntax highligting and that isn't enough. I want an editor that
understands the semantics of the code as well. That and the ability
to
write macros and I could automate the optimization process I went
through. Increasing the performance by a factor of 10 is probably not
worth three months (especially if I don't trust the code) but it would
be
worth it if I could do it in an hour (since the run is at least eight
hours
anyway).

Thanks,

Matt

Would you care to maybe show some examples here? So far what you
suggest sounds like quite a global task to me. Almost like writing a
compiler that can read humans mind.

Cheers,
Victor.

My experience has mostly been with code that was pretty ugly to start
with, and documented only via scattered references to publications.  Just
as important as performance improvements is making the code more
maintainable.  Literate programming could be a big help, but the best tool
a few years ago was fweb, which itself isn't being maintained.

It is one thing to put the effort into improving the code if you end up
with something readable, well organized, and with some confidence that the
code matches the documentation, another thing all together if the code
runs faster but is no easier to understand.  Literate programming makes it
easy to sprinkle comments of the sort that explain why a line of code
really does implement equation 73 from that 1952 tech. report, assuming
the arguments satisfy the constraints unique to the case under
consideration (and when put into practice often reveals where a later
coder copied the same line of code for use in a situation where the
constraints are not satisfied!).

With automated tools that actually change code you run the risk that the
result will be not just difficult but actually impossible to understand.

What is useful are the sort of tools that can tell how many times a
particular variable is read or changed.  It needs to be smart enough to
detect that the variable is simple being reset to same value over and over
because it is computed in some loop as:

    a = 1.0 + exp(i-really_big_value)

such that in machine code a is always 1.0.

--
George N. White III  <a@chebucto.ns.ca>

MattR wrote:
> Bil Kleb wrote:
>> FWIW, Phortran aims to do some of what you're after -- see

>>  http://www.eclipse.org/photran/

> I looked at that and I want the next generation past that.

That's why I wrote "aims" -- they have been advertising refactoring
support is "planned for the near future" for some time now.

Maybe they need your help, as what you are talking about
is refactoring[1].

> Tell me if I'm wrong, but Phortran doesn't have much more than
> syntax highligting and that isn't enough. I want an editor that
> understands the semantics of the code as well.

To tackle their goal of refactoring, they need a Fortran parser.
Maybe this is why they seem to have stalled... Fortran parsers
are not trivial.

It's an open source project; so theoretically, you could help
them implement some of the specific refactorings you want.

Regards,
--
Bil Kleb
http://fun3d.larc.nasa.gov

[1] http://en.wikipedia.org/wiki/Refactoring

On May 2, 2:47 pm, "George N. White III" <a@chebucto.ns.ca> wrote:

> With automated tools that actually change code you run the risk that the
> result will be not just difficult but actually impossible to understand.

Good point. Here's how I get around that problem. All of the code
touched by
humans is written for a single processor and is generic and easy to
understand.
The automatic munging generates temporary source files read by the
compiler.
So an automated tool gives me the best of both worlds. I get to
understand the
code and also generate efficient code.  This is what I currently do
with a set of
tools I built years ago. This all depends on keeping a history of the
commands
used to modify the code. I do that using directives.

> What is useful are the sort of tools that can tell how many times a
> particular variable is read or changed.  It needs to be smart enough to
> detect that the variable is simple being reset to same value over and over
> because it is computed in some loop as:

>     a = 1.0 + exp(i-really_big_value)

> such that in machine code a is always 1.0.

So you'd like something that will look around your program for 'a',
even if aliased
to something else, and insert code after each write to see if it
changed?

Matt

On May 2, 2:28 pm, Fly Away <victor.proso@gmail.com> wrote:

> Would you care to maybe show some examples here? So far what you
> suggest sounds like quite a global task to me. Almost like writing a
> compiler that can read humans mind.

> Cheers,
> Victor.

Hi Victor,

I thought I replied but it never showed up, let me try this again.

An example would be good. Here's a made up on the fly example. This is
just for flavor, there's nothing concrete.  The problem is to find all
the variables within a loop that are not modified.

A program is represented by a tree structure (an abstract syntax tree
if
you write compilers). For each variable in each expressions there's
information about where the variable is declared, its type, etc.
There's
also data flow information. This entire data structure is generated
using
normal compiler stuff. So far, all the user has done is load in all
the program files.

There're also pointers into the tree you can move around. You can
point to statements, expressions, declarations, whatever. There are
also some intrinsic functions and types (all caps in code below)

So, here's some pseudo code. It's vaguely perl-ish like in that there
are sets and iterators over sets.

---------------------------------------
define POINTER p                !used to look around the code
define SET var_read_only_list   !list of vars in loop that are read
only
define SET var_list             !list of vars in loop
define SET var_write_list       !list of vars in loop that contain a
write
define VAR_DATA info            !info contains data about a specific
variable

var_read_only_list = 0
var_list = 0
var_write_list = 0

move p to time loop                !get to the start of the loop
walk p over loop doing             !look at every node in the tree
  if( p is pointing to a variable) !if node is a variable
    info = VAR_INFO(p)             !get info
    SET_INCLUDE( var_list, info)   !make sure variable is in list
    if( info.is_write)             !if this variable was written
      SET_INCLUDE( var_write_list, info)  !remember that

!read-only set is all vars minus any that were written
var_read_only_list = SET_SUBTRACT( var_list, var_write_list)
--------------------------------------

This example doesn't include aliasing issues but that's what the data
flow information is for.

This could be turned into a function. The whole idea of writing
scripts is similar to matlab, only instead of working on matrices
these functions work on programs. One thing that would be nice would
be a gui to make it easier to select a section of code and call a
function on it, but the basic idea is a special language that operates
on programs and the user either writes functions or calls functions
that have already been written. At the bottom level there are
intrinsic functions that access or modify the program. For simple
things like figuring out where a value comes from or will be used, I
would think a lot of people would need it and it would just
evolve. For more complex and specific stuff people would have to write
their own libraries. An example is rewriting really long loop nests
such that the Intel Itanium compiler can do a good job pipelining
it. I found the compiler does great if it has about 10 to 20
statements in it. So a function that tiles loop nests would be fairly
specific and is also complicated as some variables have to be replaced
with vectors. This is something that the current Intel compiler won't
do but this was crucial for getting a 10 fold improvement.

Hope this helps. Unfortunately, pseudo code can be confusing.

Matt

Matt,

What you are asking for requires an entire compiler, not just a
parser. I never had much luck interesting my compiler colleagues into
providing such info, but here are some clues as to how you can
browbeat the compiler into doing what you want with less effort.

If you have constants which you know are constants but are read out of
datafiles, e.g. a "geometry" variable that effects a lot of your
equations, then you should make it a parameter. When you read the
file, make sure it matches up. Then a smart compiler (with IPA) will
be able to propagate it all over your program. This is a modest source
change. To make this work with any compiler without good IPA, you'd
have to modify your source to make the parameter visible as a
parameter in all the important files. That's a much more intrusive
change.

Now, let's say you compute with 2 geometries in every run. Here's how
Open64 (and the PathScale) compiler would treat that. When you have a
procedure call wich is called with an argument which is a constant, it
might create a specialized clone procedure in which it knows that
argument is a constant.  This works best when you use IPA, or the
procedure is a leaf (or is made so by inlining), and there are only a
couple of different constants visible. So, to force it to do what you
want, first make your computational kernel its own subroutine, set up
some "must inlines" to make it a leaf, and then write code like this:

  if (geom .eq. 1) then
      call kernel (..., 1)
  else if (geom .eq. 2) then
      call kernel (..., 2)
  else
      call kernel (..., geom)
  endif

I don't recall if Open64 has a user-visible flag to report on what
clones are made, but I'm sure there's a magic debugging flag which
does report on it.

In theory feedback optimization is capable of noticing that geom only
has 2 different values, causing clones to be made automagically.

Now you're using Itaniums and the Intel compiler, and I have no idea
how Intel does this, but I will note that Open64 3.0 might be
industrial strength enough to work for you on Itaniums.

-- greg

Exactly, and count the number of real changes, so you see that a changes a
few times each time the inner loop is entered but b changes for every
iteration.

Some of the biggest safe speedups I've found amount to running a
simplified inner loop for cases where the changes from the previous
iteration can be predicted.  A good example is an underwater light
spectrum that is summed to get total light at each depth -- as you go
deeper, many wavelengths have been attenuated, so you only need to
calculate a few wavelengths to get the total.  It probably needs a bit
more physics than you expect from the compiler to realize this happens,
not to mention sorting (on wavelength) to put the largest contributions
first so you can exit the loop once the sum stops changing.

--
George N. White III  <a@chebucto.ns.ca>

On May 3, 12:52 pm, lind@pbm.com (Greg Lindahl) wrote:

> If you have constants which you know are constants but are read out of
> datafiles, e.g. a "geometry" variable that effects a lot of your
> equations, then you should make it a parameter. When you read the
> file, make sure it matches up. Then a smart compiler (with IPA) will
> be able to propagate it all over your program. This is a modest source

I haven't seen any smart compilers. All the compilers I've ever dealt
with have to be spoon fed when it comes to optimization. I have to
reverse engineer the compiler to know what it wants. So I want a tool
that will aid this process.

> change. To make this work with any compiler without good IPA, you'd
> have to modify your source to make the parameter visible as a
> parameter in all the important files. That's a much more intrusive
> change.

> Now, let's say you compute with 2 geometries in every run. Here's how
> Open64 (and the PathScale) compiler would treat that. When you have a
> procedure call wich is called with an argument which is a constant, it
> might create a specialized clone procedure in which it knows that

What if the configuration variable is in a module? And if there are
hundreds of such variables?

> argument is a constant.  This works best when you use IPA, or the
> procedure is a leaf (or is made so by inlining), and there are only a
> couple of different constants visible. So, to force it to do what you
> want, first make your computational kernel its own subroutine, set up
> some "must inlines" to make it a leaf, and then write code like this:

>   if (geom .eq. 1) then
>       call kernel (..., 1)
>   else if (geom .eq. 2) then
>       call kernel (..., 2)
>   else
>       call kernel (..., geom)
>   endif

But there are hundreds of configuration variables. I don't want to
write code like that. Not to mention that it will make the resulting
code difficult to understand.

> I don't recall if Open64 has a user-visible flag to report on what
> clones are made, but I'm sure there's a magic debugging flag which
> does report on it.

> In theory feedback optimization is capable of noticing that geom only
> has 2 different values, causing clones to be made automagically.

I tried the feedback optimization on the Intel compiler and saw no
change in performance.

> Now you're using Itaniums and the Intel compiler, and I have no idea
> how Intel does this, but I will note that Open64 3.0 might be
> industrial strength enough to work for you on Itaniums.

But I use other machines besides Itanium based. And the Open64
compiler
won't work on my PC.

My point is, although I can do all of these things, and I've done a
lot more
than you describe, I want a faster way to do them. I can't depend on
one
compiler and one architecture because machines change every few years,
compilers change every year, but the Fortran code lasts for decades.
The
code I'm working on used to run on the Connection Machine. Nobody was
thinking of pipelining for Itanium, not to mention cell processors.
Every
time there's a change we have to mess with 10's of thousands of lines
of
code. This is expensive.

Matt

So you are talking about optimizing loops, right? What if a loop
contains some function/subroutine calls? You don't know what happens
inside them.
I still think what you suggest would be a way too hard to implement
thing. Even if you get it working in some particular cases it'll be
hard to generalize it. Another thing is that it should still be
checked what your "optimizer" has done in order to make sure it didn't
mess stuff up.
But maybe I just don't quite understand your idea...

Victor.

In article <1178226310.077218.175@n59g2000hsh.googlegroups.com>,

MattR  <ros@peakfive.com> wrote:
>What if the configuration variable is in a module? And if there are
>hundreds of such variables?

If they're constants in a module, the compiler should already be doing
the right thing.

>But there are hundreds of configuration variables. I don't want to
>write code like that. Not to mention that it will make the resulting
>code difficult to understand.

Suit yourself, I was just explaining how to spoon-feed the compiler
with less effort than you're currently putting out.

>But I use other machines besides Itanium based. And the Open64
>compiler won't work on my PC.

The PathScale compiler works on your PC.
The Open64 compiler works on your Itanium.

Other compilers than these understand the idiom I was suggesting; it
is not that unusual of a feature.

>My point is, although I can do all of these things, and I've done a
>lot more than you describe, I want a faster way to do them.

I know you have big wants. I didn't realize you were so reistant to useful
advice. Now I can stop wasting my time.

-- greg

> -- greg

Hi Greg, I'm sorry if I sounded short with you. I didn't intend to
make you angry.

My original question was to find out if anyone would like a tool to
help them maintain fortran and I'll just take your vote as no.

Thanks,

Matt

On May 3, 10:34 pm, MattR <ros@peakfive.com> wrote:

> > -- greg

> Hi Greg, I'm sorry if I sounded short with you. I didn't intend to
> make you angry.

> My original question was to find out if anyone would like a tool to
> help them maintain fortran and I'll just take your vote as no.

I would summarize what you have been proposing in this thread as a
source-to-source translation tool designed to improve performance.
Along with a few others in this thread, I wonder how feasible it is to
build such a tool, but if my programs can be sped up without my
manually changing the code, I am all in favor :).

I wonder if the envisaged tool would be free or commercial. If the
former, maybe it could be integrated with gfortran.

On May 4, 6:23 am, Beliavsky <beliav@aol.com> wrote:

> I would summarize what you have been proposing in this thread as a
> source-to-source translation tool designed to improve performance.
> Along with a few others in this thread, I wonder how feasible it is to
> build such a tool, but if my programs can be sped up without my
> manually changing the code, I am all in favor :).

Sorry, but I can't build anything that will do it all for you. I just
want to build a tool that will aid you in manually doing it yourself.

> I wonder if the envisaged tool would be free or commercial. If the
> former, maybe it could be integrated with gfortran.

It would be an interactive preprocessor. I built a non-interactive
preprocessor before that allows users to write their own directives.
It doesn't have data flow information and couldn't look at an entire
program, but it's quite flexible and powerful. The strength of it is
in being able to write custom directives. That makes it simple to
build but can solve real problems. I would use the same approach with
a new tool. As far as being free or commercial, it depends on whether
I could make money selling services using it, or writing scripts for
it. Unfortunately, based on this thread, it looks like a tough sell.
This is surprising to me since the biggest complaint I've seen about
using parallel computers seems to be maintaining the software.
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc