Home > Archive > Fortran > September 2005 > [Fortran 77] swaping two arrays (newbie)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
[Fortran 77] swaping two arrays (newbie)
|
|
| Thomas Baruchel 2005-09-15, 6:59 pm |
| Hi,
I would like to have my two arrays p and q being swapped in
a loop ; of course I can do it by myself, but with a cost in
term of speed. What I would like to have is simply call p the
array that was previously called q and vice-versa. In C, I would
do it with pointers ; is there something like that in Fortran 77 ?
--
Thomas Baruchel
write to baruchel at the host called bluebottle dot com
écrire à baruchel chez l'hôte nommé bluebottle point com
| |
| Michael Metcalf 2005-09-15, 6:59 pm |
|
"Thomas Baruchel" <baruchel@127.0.0.1> wrote in message
news:4329dd19$0$6547$626a14ce@news.free.fr...
> Hi,
>
> I would like to have my two arrays p and q being swapped in
> a loop ; of course I can do it by myself, but with a cost in
> term of speed. What I would like to have is simply call p the
> array that was previously called q and vice-versa. In C, I would
> do it with pointers ; is there something like that in Fortran 77 ?
>
No, but there is in Fortran 95. If you're new to Fortran you will be better
off learning Fortran 95 straightaway. It will spare you much grief.
Regards,
Mike Metcalf
| |
| Richard E Maine 2005-09-15, 6:59 pm |
| In article <4329dd19$0$6547$626a14ce@news.free.fr>,
Thomas Baruchel <baruchel@127.0.0.1> wrote:
> I would like to have my two arrays p and q being swapped in
> a loop ; of course I can do it by myself, but with a cost in
> term of speed. What I would like to have is simply call p the
> array that was previously called q and vice-versa. In C, I would
> do it with pointers ; is there something like that in Fortran 77 ?
In Fortran you could also do it with pointers, at least if you are using
a Fortran from within the past decade and a half. That would not include
Fortran 77. I recommend learning about Fortran 90/95. (Fortran 2003
compilers aren't yet out). Fortran 77 is certainly still good for many
things. Many people do still find that it suits their needs. However,
you've just named a need that it doesn't suit; there are quite a few
others.
The closest thing to a pointer in Fortran 77 is a dummy argument.
(Indeed, I personally find the parallels between pointers and dummy
arguments to be quite strong; they aren't perfect, but they sure are
strong). You might be able to recast your problem in such a way that the
computations are done in a subroutine and you can call the subroutine
with appropriate actual arguments to achieve the desired effect.
Do be aware that pointers can cause performance degradation by limiting
the compiler's optimization possibilities. Thus if your sole reason to
use a pointer is to improve speed, you might want to make sure that the
algorithmic speed gain is not offset by a loss from inferior
optimization. There isn't sufficient data to tell (and testing might
well be the only way to know for sure).
--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain | experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
| |
| meek@skyway.usask.ca 2005-09-15, 6:59 pm |
| In a previous article, Thomas Baruchel <baruchel@127.0.0.1> wrote:
>Hi,
>
>I would like to have my two arrays p and q being swapped in
>a loop ; of course I can do it by myself, but with a cost in
>term of speed. What I would like to have is simply call p the
>array that was previously called q and vice-versa. In C, I would
>do it with pointers ; is there something like that in Fortran 77 ?
>
>--
>Thomas Baruchel
> write to baruchel at the host called bluebottle dot com
> écrire à baruchel chez l'hôte nommé bluebottle point com
I would just add an extra index to a single matrix, and reverse
that. But them I'm a F77 fan.
(and there would be a minor cost in speed)
.. or there are other ways- e.g. duplicate the program segment
but reverse the array names, and call the appropriate segment.
(I'll go to great lengths to avoid pointers!)
Chris
| |
| glen herrmannsfeldt 2005-09-15, 6:59 pm |
| m @skyway.usask.ca wrote:
> In a previous article, Thomas Baruchel <baruchel@127.0.0.1> wrote:
(snip)
(snip)
[color=darkred]
> I would just add an extra index to a single matrix, and reverse
> that. But them I'm a F77 fan.
> (and there would be a minor cost in speed)
That would be the Fortran 77 way. Before dynamic storage allocation
the favorite way was to have one large array, usually in COMMON,
and calculate offsets into that array where each user array
actually started. EQUIVALENCE could be used if different types
were needed.
In some cases I knew, after such allocation a subroutine was called
to do the rest of the work, with the actual argument being the
starting array element in the one large array.
-- glen
| |
| Arjen Markus 2005-09-16, 3:57 am |
| >
> The closest thing to a pointer in Fortran 77 is a dummy argument.
To implement this swapping with dummy arguments, you could do something
like this:
do 110 i = 1,nostep
*
* First step
*
call mysub(p, q)
*
* Second step (exchange the role of p and q)
*
call mysub(q, p)
110 continue
(You may of course need to use two separate subroutines or use a
parameter to select the proper part of the computation ...)
Regards,
Arjen
| |
| Brooks Moses 2005-09-18, 4:14 am |
| Thomas Baruchel wrote:
> I would like to have my two arrays p and q being swapped in
> a loop ; of course I can do it by myself, but with a cost in
> term of speed. What I would like to have is simply call p the
> array that was previously called q and vice-versa. In C, I would
> do it with pointers ; is there something like that in Fortran 77 ?
As many people have said, this functionality does not -- as such --
exist in Fortran 77. This is only a minor barrier to obtaining the
needed functionality, however; you can use the following idiom:
Instead of
real p(x,y), q(x,y)
declare the variables
real pq(x,y,2)
integer p, q
p = 1
q = 2
And then, instead of p(i,j) and q(i,j), write pq(i,j,p) and pq(i,j,q).
Then, instead of swapping the two arrays, you simply swap the values of
p and q as needed.
I'll agree with the other posters that you'd be better off learning
Fortran 95 instead. At this point, even the gcc compiler suite (with
version 4) has moved to a Fortran 95 compiler rather than a Fortran 77
compiler.
- Brooks
--
The "bmoses-nospam" address is valid; no unmunging needed.
| |
| Greg Lindahl 2005-09-18, 6:57 pm |
| In article <432CFA56.8070708@cits1.stanford.edu>,
Brooks Moses <bmoses-nospam@cits1.stanford.edu> wrote:
>I'll agree with the other posters that you'd be better off learning
>Fortran 95 instead.
I'll disagree. The "F77 method" you present is great F95 code; it's
easy for the compiler to optimize. Using pointers so that you can "do
it the C way" is just asking for low performance. And I don't think
the pointer method is more readable than the "F77 method".
-- greg
| |
| glen herrmannsfeldt 2005-09-18, 6:57 pm |
| Greg Lindahl wrote:
> In article <432CFA56.8070708@cits1.stanford.edu>,
> Brooks Moses <bmoses-nospam@cits1.stanford.edu> wrote:
[color=darkred]
> I'll disagree. The "F77 method" you present is great F95 code; it's
> easy for the compiler to optimize. Using pointers so that you can "do
> it the C way" is just asking for low performance. And I don't think
> the pointer method is more readable than the "F77 method".
If you can use static arrays then I agree, the F77 method is fine.
For dynamic arrays, especially where the two (or more) may be
different size, then I would go for pointers. The performance will
be a little worse than static arrays, but that is part of what you
get using dynamic arrays.
-- glen
|
|
|
|
|