Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #88419 > unrolled thread

best way to convert a Fortran implied do loop to C++

Started byLynn McGuire <lynnmcguire5@gmail.com>
First post2023-01-06 23:00 -0600
Last post2023-01-11 14:16 -0600
Articles 8 — 3 participants

Back to article view | Back to comp.lang.c++


Contents

  best way to convert a Fortran implied do loop to C++ Lynn McGuire <lynnmcguire5@gmail.com> - 2023-01-06 23:00 -0600
    Re: best way to convert a Fortran implied do loop to C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-01-07 12:55 +0000
      Re: best way to convert a Fortran implied do loop to C++ Richard Damon <Richard@Damon-Family.org> - 2023-01-07 11:07 -0500
        Re: best way to convert a Fortran implied do loop to C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-01-07 21:48 +0000
          Re: best way to convert a Fortran implied do loop to C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-01-07 22:41 +0000
            Re: best way to convert a Fortran implied do loop to C++ Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-01-07 22:46 +0000
            Re: best way to convert a Fortran implied do loop to C++ Lynn McGuire <lynnmcguire5@gmail.com> - 2023-01-07 21:29 -0600
    Re: best way to convert a Fortran implied do loop to C++ Lynn McGuire <lynnmcguire5@gmail.com> - 2023-01-11 14:16 -0600

#88419 — best way to convert a Fortran implied do loop to C++

FromLynn McGuire <lynnmcguire5@gmail.com>
Date2023-01-06 23:00 -0600
Subjectbest way to convert a Fortran implied do loop to C++
Message-ID<tpau9h$3e4fi$1@dont-email.me>
So what is the best way to convert a Fortran implied do loop to C++ ?

       WRITE (2,2) (N (I), I = 1, 80)
     2 FORMAT(' ',80A1)

Thanks,
Lynn

[toc] | [next] | [standalone]


#88421

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-01-07 12:55 +0000
Message-ID<87k01yijwp.fsf@bsb.me.uk>
In reply to#88419
Lynn McGuire <lynnmcguire5@gmail.com> writes:

> So what is the best way to convert a Fortran implied do loop to C++ ?
>
>       WRITE (2,2) (N (I), I = 1, 80)
>     2 FORMAT(' ',80A1)

Presumably the bounds are not always literals, and you probably also
want to general solution the works for multiple implied loops, some of
which might be nested?  So not much!

My first question would be how you translate WRITE/FORMAT combinations
with no loops as that's going to form the basis of a solution.

(I presume you know Fortran well enough to know that this is not the
same as putting a loop round the write.)

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#88422

FromRichard Damon <Richard@Damon-Family.org>
Date2023-01-07 11:07 -0500
Message-ID<6NguL.537322$GNG9.514912@fx18.iad>
In reply to#88421
On 1/7/23 7:55 AM, Ben Bacarisse wrote:
> Lynn McGuire <lynnmcguire5@gmail.com> writes:
> 
>> So what is the best way to convert a Fortran implied do loop to C++ ?
>>
>>        WRITE (2,2) (N (I), I = 1, 80)
>>      2 FORMAT(' ',80A1)
> 
> Presumably the bounds are not always literals, and you probably also
> want to general solution the works for multiple implied loops, some of
> which might be nested?  So not much!
> 
> My first question would be how you translate WRITE/FORMAT combinations
> with no loops as that's going to form the basis of a solution.
> 
> (I presume you know Fortran well enough to know that this is not the
> same as putting a loop round the write.)
> 

Althogh in C++ you COULD put the loop around the write equivalent since 
C++ doesn't automatically take the first character as carriage control 
and add the new line to the end (if you use the right functions).

[toc] | [prev] | [next] | [standalone]


#88428

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-01-07 21:48 +0000
Message-ID<87eds6hv8c.fsf@bsb.me.uk>
In reply to#88422
Richard Damon <Richard@Damon-Family.org> writes:

> On 1/7/23 7:55 AM, Ben Bacarisse wrote:
>> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>> 
>>> So what is the best way to convert a Fortran implied do loop to C++ ?
>>>
>>>        WRITE (2,2) (N (I), I = 1, 80)
>>>      2 FORMAT(' ',80A1)
>> Presumably the bounds are not always literals, and you probably also
>> want to general solution the works for multiple implied loops, some of
>> which might be nested?  So not much!
>> My first question would be how you translate WRITE/FORMAT combinations
>> with no loops as that's going to form the basis of a solution.
>> (I presume you know Fortran well enough to know that this is not the
>> same as putting a loop round the
>
> Althogh in C++ you COULD put the loop around the write equivalent
> since C++ doesn't automatically take the first character as carriage
> control and add the new line to the end (if you use the right
> functions).

I would expect that with a clean translation the carriage control
handling (if wanted) would just come out in the wash.  Even in modern
Fortran (where the "carriage control" malarkey is obsolete) you can't
just turn an implied loop into an explicit one.  Presumably the
translating software already has some mechanism to iterate through the
format and that's what has to be driven by the implied loop.

If, as I suppose is possible, the first character is still to be treated
specially, that should just work out correctly as the code iterates
through the format.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#88429

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-01-07 22:41 +0000
Message-ID<871qo6hsrf.fsf@bsb.me.uk>
In reply to#88428
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> Richard Damon <Richard@Damon-Family.org> writes:
>
>> On 1/7/23 7:55 AM, Ben Bacarisse wrote:
>>> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>>> 
>>>> So what is the best way to convert a Fortran implied do loop to C++ ?
>>>>
>>>>        WRITE (2,2) (N (I), I = 1, 80)
>>>>      2 FORMAT(' ',80A1)
>>> Presumably the bounds are not always literals, and you probably also
>>> want to general solution the works for multiple implied loops, some of
>>> which might be nested?  So not much!
>>> My first question would be how you translate WRITE/FORMAT combinations
>>> with no loops as that's going to form the basis of a solution.
>>> (I presume you know Fortran well enough to know that this is not the
>>> same as putting a loop round the
>>
>> Althogh in C++ you COULD put the loop around the write equivalent
>> since C++ doesn't automatically take the first character as carriage
>> control and add the new line to the end (if you use the right
>> functions).
>
> I would expect that with a clean translation the carriage control
> handling (if wanted) would just come out in the wash.  Even in modern
> Fortran (where the "carriage control" malarkey is obsolete) you can't
> just turn an implied loop into an explicit one.  Presumably the
> translating software already has some mechanism to iterate through the
> format and that's what has to be driven by the implied loop.
>
> If, as I suppose is possible, the first character is still to be treated
> specially, that should just work out correctly as the code iterates
> through the format.

To add a bit more detail, I image that formats will be turned into
run-time objects containing a 'compiled' version of the format
mini-language.  A statement like

      WRITE(*, 2) A, X
    2 FORMAT(I2, F5.2)

would turn into

  format_2.start();
  format_2.write(UNIT_STAR, A);
  format_2.write(UNIT_STAR, X);
  format_2.end();

where the write method is a templated method that advances an internal
pointer in the format "list" (it may, in fact, be a cyclic graph).  And

      WRITE(*,2) (N(I), X(I), I = 1, 6)

would turn into

  format_2.start();
  for (int i = 1; i <= 6; i++) {
      format_2.write(STAR, N[i]);
      format_2.write(STAR, X[i]);
  }
  format_2.end();

The member function 'end' should be idempotent, because the write
function will also call it when the last format specification is used.

I'm sure this is turn out to be way more complicated that it looks to me
right now, but this feel like the gist of what's needed.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#88430

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-01-07 22:46 +0000
Message-ID<87v8ligdyx.fsf@bsb.me.uk>
In reply to#88429
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> To add a bit more detail, I image that formats will be turned into
> run-time objects containing a 'compiled' version of the format
> mini-language.  A statement like
>
>       WRITE(*, 2) A, X
>     2 FORMAT(I2, F5.2)

It might have made more sense to use a format that could be used by an
external loop like

      DO 10 I = 1, 6
        WRITE(*, 2) A(I), X(I)
   10 END DO

but which while still being sensible would give a different result.  For
example, I might have used

    2 FORMAT(3(I2, F5.2))

The rest remains the same.

> would turn into
>
>   format_2.start();
>   format_2.write(UNIT_STAR, A);
>   format_2.write(UNIT_STAR, X);
>   format_2.end();
>
> where the write method is a templated method that advances an internal
> pointer in the format "list" (it may, in fact, be a cyclic graph).  And
>
>       WRITE(*,2) (N(I), X(I), I = 1, 6)
>
> would turn into
>
>   format_2.start();
>   for (int i = 1; i <= 6; i++) {
>       format_2.write(STAR, N[i]);
>       format_2.write(STAR, X[i]);
>   }
>   format_2.end();
>
> The member function 'end' should be idempotent, because the write
> function will also call it when the last format specification is used.
>
> I'm sure this is turn out to be way more complicated that it looks to me
> right now, but this feel like the gist of what's needed.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#88433

FromLynn McGuire <lynnmcguire5@gmail.com>
Date2023-01-07 21:29 -0600
Message-ID<tpddbb$14v4$1@gioia.aioe.org>
In reply to#88429
On 1/7/2023 4:41 PM, Ben Bacarisse wrote:
> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> 
>> Richard Damon <Richard@Damon-Family.org> writes:
>>
>>> On 1/7/23 7:55 AM, Ben Bacarisse wrote:
>>>> Lynn McGuire <lynnmcguire5@gmail.com> writes:
>>>>
>>>>> So what is the best way to convert a Fortran implied do loop to C++ ?
>>>>>
>>>>>         WRITE (2,2) (N (I), I = 1, 80)
>>>>>       2 FORMAT(' ',80A1)
>>>> Presumably the bounds are not always literals, and you probably also
>>>> want to general solution the works for multiple implied loops, some of
>>>> which might be nested?  So not much!
>>>> My first question would be how you translate WRITE/FORMAT combinations
>>>> with no loops as that's going to form the basis of a solution.
>>>> (I presume you know Fortran well enough to know that this is not the
>>>> same as putting a loop round the
>>>
>>> Althogh in C++ you COULD put the loop around the write equivalent
>>> since C++ doesn't automatically take the first character as carriage
>>> control and add the new line to the end (if you use the right
>>> functions).
>>
>> I would expect that with a clean translation the carriage control
>> handling (if wanted) would just come out in the wash.  Even in modern
>> Fortran (where the "carriage control" malarkey is obsolete) you can't
>> just turn an implied loop into an explicit one.  Presumably the
>> translating software already has some mechanism to iterate through the
>> format and that's what has to be driven by the implied loop.
>>
>> If, as I suppose is possible, the first character is still to be treated
>> specially, that should just work out correctly as the code iterates
>> through the format.
> 
> To add a bit more detail, I image that formats will be turned into
> run-time objects containing a 'compiled' version of the format
> mini-language.  A statement like
> 
>        WRITE(*, 2) A, X
>      2 FORMAT(I2, F5.2)
> 
> would turn into
> 
>    format_2.start();
>    format_2.write(UNIT_STAR, A);
>    format_2.write(UNIT_STAR, X);
>    format_2.end();
> 
> where the write method is a templated method that advances an internal
> pointer in the format "list" (it may, in fact, be a cyclic graph).  And
> 
>        WRITE(*,2) (N(I), X(I), I = 1, 6)
> 
> would turn into
> 
>    format_2.start();
>    for (int i = 1; i <= 6; i++) {
>        format_2.write(STAR, N[i]);
>        format_2.write(STAR, X[i]);
>    }
>    format_2.end();
> 
> The member function 'end' should be idempotent, because the write
> function will also call it when the last format specification is used.
> 
> I'm sure this is turn out to be way more complicated that it looks to me
> right now, but this feel like the gist of what's needed.

This is close to the way that F2C mimics the implied loop.

I have been using a std::string as an accumulator to maintain type safety.

Thanks,
Lynn

[toc] | [prev] | [next] | [standalone]


#88489

FromLynn McGuire <lynnmcguire5@gmail.com>
Date2023-01-11 14:16 -0600
Message-ID<tpn5eu$10oga$1@dont-email.me>
In reply to#88419
On 1/6/2023 11:00 PM, Lynn McGuire wrote:
> So what is the best way to convert a Fortran implied do loop to C++ ?
> 
>        WRITE (2,2) (N (I), I = 1, 80)
>      2 FORMAT(' ',80A1)
> 
> Thanks,
> Lynn

BTW, the code I ended up using is:

	char *n_input;
	...
	std::string msg = n_input;
	outwri (msg);

Thanks,
Lynn

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web