C++ programming (was Newbie Alert: Beginning parallel program ming with Scyld)
Toon Moene
toon at moene.indiv.nluug.nl
Wed Oct 16 15:11:34 PDT 2002
Robert G. Brown wrote:
> Different compilers are suitable for different kinds of tasks, and even
> now it could easily be that fortran, written a certain way, e.g.
> vectorizes better than C. For one thing, C programmers (really speaking
> only for myself, not necessarily ALL C programmers:-) tend to use
> pointers a lot because we tend to use dynamic allocation of memory a
> lot, and use this dynamic memory in lots of "interesting" ways to build
> data structures (such as linked lists, trees, arrays of structs) that
> defeat certain kinds of optimization, which like to be able to presume
> that an array starts on a given index, is a given size, and can be
> unrolled in a certain way to give optimal performance.
Yep, I can clearly see that Fortran wouldn't buy you much then. You can
do all of the above with Fortran 95, but the programs would be more
verbose than the equivalent C programs, and I would be surprised if the
Fortran compilers were actually capable of taking advantage of the more
strict aliasing syntax (you have to indicate each item in your program
that can be the target of a pointer as TARGET, so any that aren't can be
assumed alias-free).
What I think the strength of Fortran is, is the ease to use multi-rank
arrays - which is the reason Fortran is such a natural language for,
e.g. weather forecasting code.
In Fortran, you can write:
READ*,NX,NY,NZ ! Read dimensions of simulation
... allocation of PS, T, and Q ...
CALL COMPUTE(PS,T,Q,NX,NY,NZ)
...
SUBROUTINE COMPUTE(PS,T,Q,NX,NY,NZ)
DIMENSION PS(NX,NY) ! Surface pressure
DIMENSION T(NX,NY,NZ) ! Temperature
DIMENSION Q(NX,NY,NZ) ! Specific humidity
DIMENSION TD(NX,NY,NZ) ! Dew point temperature
...
where TD is an array local to subroutine `COMPUTE' that has the same
dimensions and layout as T and Q. Although C99 will give you the
equivalent of automatic arrays (like TD), an array in C is still
basically a pointer augmented with some funny syntax and it will not
allow such clear declarations as the above.
It gets more interesting if the problem at hand has a most `natural'
translation using variable upper *and lower* bounds, like
SUBROUTINE MATCHTHIS(A, B, N, M)
DIMENSION A(N-M: N+M), B(-M: M), C(N, M)
...
and mutatis mutandis for higher-than-1-rank.
Perhaps C++ solves that problem - I do not know enough of the language
to tell.
--
Toon Moene - mailto:toon at moene.indiv.nluug.nl - phoneto: +31 346 214290
Saturnushof 14, 3738 XG Maartensdijk, The Netherlands
Maintainer, GNU Fortran 77: http://gcc.gnu.org/onlinedocs/g77_news.html
Join GNU Fortran 95: http://g95.sourceforge.net/ (under construction)
More information about the Beowulf
mailing list