comp.lang.ada
 help / color / mirror / Atom feed
* Re: help on interfacing with fortran
  2000-01-16  0:00 help on interfacing with fortran Paolo M. Pumilia
@ 2000-01-16  0:00 ` Gisle S�lensminde
  2000-01-25  0:00   ` pumilia
  0 siblings, 1 reply; 7+ messages in thread
From: Gisle S�lensminde @ 2000-01-16  0:00 UTC (permalink / raw)


In article <38818E32.FD90616B@cstc.org>, Paolo M. Pumilia wrote:
>
>I would like to interface a set of fortran soubroutines and functions
>to main Ada procedure.
>Here are enclosed three functions and a subroutine which i am working
>on, at present. They are all stored in a single file, that i used to
>compile and link to its fortran main code.
>
>The Ada procedure makes one call to the subroutine start_carry(idum),
>getting an integer number (idum) in return
>
>Then the function rnor(idum) will be called, returnig a real*8 random
>number.
>
>How to perform such calls?
>
>I took a look at tha ARM, but its contents appears rather cryptic to me.
>I have been reading a few introductive manuals (Lovelace, Dodrill-tutor,
>etc) but they don't cover this topic very well.

>Interfacing is completely new in my experience. Could those four fortran
>codes be kept within a single file or should they be included into an
>Ada package or procedure?

>A procedure for each function or subroutine should be prepared? How do
>calls between fortran functions (not involving Ada code) will be 
>performed?

Here is a pice of code that do what you tried to do. Since it seems
that you have some problem with the Ada syntax in general, I have 
tried to make some hopefully useful comments in the code. The code
seemed to work on SGI IRIX 6.5 with gnat 3.11b and g77. The code was
compiled with:

g77 -c -fno-second-underscore rnor.f
gnatmake ada_main.adb -largs rnor.o

with Interfaces.Fortran;
use Interfaces.Fortran;
procedure Ada_Main is
   package Fortran renames Interfaces.Fortran;

   -- I use the "Fortran types" decleard in interfaces.fortran,
   -- which match the types used by the fortran compiler.

   dummy : Fortran.Fortran_Integer;

   -- This is how external fortran subroutines is declared.
   -- Fist a specification, followed by a pragma import.

   procedure start_carry (dummy : in out Fortran.Fortran_Integer);
   pragma Import (Fortran, Start_Carry,"start_carry_");

   -- Unlike Fortran, functions in Ada do not allow
   -- out parameters in functions.
   function rnor(dum : in Fortran.Fortran_Integer) return Fortran.Real;

   -- The pragma imports use the optional third parameter that
   -- specifies the link name. Somtimes this is neccesary, since
   -- different compilers on a single platform may give functions
   -- and subroutines different link names, by inserting underscores.
   pragma Import (Fortran, Rnor, "rnor_");

   A : Fortran.Real;
begin
   -- And here comes the calls
   start_carry(dummy);
   a := rnor(dummy);
end Ada_Main ;


 

>Here is my guess applied to Ada_Main procedure:

- Gisle S�lensminde (gisle@ii.uib.no)




^ permalink raw reply	[flat|nested] 7+ messages in thread

* help on interfacing with fortran
@ 2000-01-16  0:00 Paolo M. Pumilia
  2000-01-16  0:00 ` Gisle S�lensminde
  0 siblings, 1 reply; 7+ messages in thread
From: Paolo M. Pumilia @ 2000-01-16  0:00 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 1714 bytes --]

I would like to interface a set of fortran soubroutines and functions
to main Ada procedure.
Here are enclosed three functions and a subroutine which i am working
on, at present. They are all stored in a single file, that i used to
compile and link to its fortran main code.

The Ada procedure makes one call to the subroutine start_carry(idum),
getting an integer number (idum) in return

Then the function rnor(idum) will be called, returnig a real*8 random
number.

How to perform such calls?

I took a look at tha ARM, but its contents appears rather cryptic to me.

I have been reading a few introductive manuals (Lovelace, Dodrill-tutor,
etc)
but they don't cover this topic very well.
Interfacing is completely new in my experience. Could those four fortran

codes be kept within a single file or should they be included into an
Ada package or procedure?
A procedure for each function or subroutine should be prepared? How do
calls
between fortran functions (not involving Ada code) will be performed?

Here is my guess applied to Ada_Main procedure:

 with Interfaces.Fortran;
 use Interfaces.Fortran;
 procedure Ada_Main;

 dummy : integer
 function rnor(dum : integer)  : return float;
 pragma Convention (Fortran, dummy);
 pragma Convention (Fortran, dum);

 procedure start_carry (dummy : in out integer) is
   pragma Import (Fortran, start_carry);
   ... fortran code ? .......

 function rnor(dum : integer)  : return float is
   pragma Import (Fortran, rnor);
   ... fortran code ? .......


 begin Ada_Main
 ....
 start_carry(dummy);

 a := rnor(dum);  -- supposing a is a float variable

 ....
 end Ada_Main ;


Any suggestions and examples are welcome

thank you  for your help

Paolo Pumilia




[-- Attachment #2: rnor.f --]
[-- Type: text/plain, Size: 2619 bytes --]

      subroutine start_carry(idum)
      idum=-10231
      do iloop=1,1000
         j=icarry(idum)
      enddo
      return
      end

      function icarry(idum)
      implicit real*8(a-h,q-z)
      parameter (ib=2**24,b=1.d0/ib)
      integer*4 xn(-29:30)
      if(idum.le.0)then
         if(idum.eq.0)idum=13
         do i=1,30
            xn(i)=mod(iabs(idum)*i,37)
            xn(i-30)=xn(i)
         enddo
         ic=0
         idum=30
      endif  
      i1=idum-24
      i2=idum-10
      idn=xn(i2)-xn(i1)-ic
      if(idn.lt.0)then
         idn=idn+ib
         ic=1
        else
         ic=0
      endif
      idum=idum+1
      if(idum.gt.30)idum=1
      xn(idum)=idn
      xn(idum-30)=idn
      icarry=idn
      return
      end


      function rcarry(idum)
      implicit real*8(a-h,q-z)
      parameter (ib=2**24,b=1.d0/ib)
      rcarry=b*icarry(idum)
      return
      end
C
C MARSAGLIA,G. & TSANG,W.W. ,SIAM J.SCIE. & STAT.COMP.,5,349(1984)
C
      FUNCTION RNOR(SEED)
      IMPLICIT REAL*8(A-H,M,Q-Z)
      ! pol 29.jun.1997 	?? real*8 pc
      INTEGER*4 SEED
      parameter (i23=2**23,rmax=1.d0/i23)
      DIMENSION V(65)
      DATA   V  /.3409450d0,.4573146d0,.5397792d0,.6062427d0,.6631690d0,
     1.7136974d0,.7596124d0,.8020356d0,.8417227d0,.8792102d0,.9148948d0,
     2.9490791d0,.9820005d0,1.013848d0,1.044780d0,1.074924d0,1.104391d0,
     31.133273d0,1.161653d0,1.189601d0,1.217181d0,1.244452d0,1.271463d0,
     41.298265d0,1.324901d0,1.351412d0,1.377839d0,1.404221d0,1.430593d0,
     51.456991d0,1.483452d0,1.510012d0,1.536706d0,1.563571d0,1.590645d0,
     61.617968d0,1.645579d0,1.673525d0,1.701850d0,1.730604d0,1.759842d0,
     71.789622d0,1.820009d0,1.851076d0,1.882904d0,1.915583d0,1.949216d0,
     81.983924d0,2.019842d0,2.057135d0,2.095992d0,2.136644d0,2.179371d0,
     92.224517d0,2.272518d0,2.323934d0,2.379500d0,2.440222d0,2.507511d0,
     +2.583466d0,2.671391d0,2.776994d0,2.776994d0,2.776994d0,2.776994d0/
      DATA AA,B,C/12.37586d0,.4878992d0,12.67706d0/  
     1     C1,C2,PC,XN/.9689279d0,1.301198d0,.1958303d-1,2.776994d0/
      I=icarry(seed)-i23
      J=MOD(IABS(I),64)+1
      RNOR=I*RMAX*V(J+1)
      IF(DABS(RNOR).LE.V(J))RETURN
C
      X=(DABS(RNOR)-V(J))/(V(J+1)-V(J))
      Y=rcarry(seed)
      S=X+Y
      IF(S.GT.C2)GOTO 11
      IF(S.LE.C1)RETURN
      IF(Y.GT.C-AA*DEXP(-.5D0*(B-B*X)**2))GOTO 11
      IF(DEXP(-.5D0*V(J+1)**2)+Y*PC/V(J+1).LE.DEXP(-.5D0*RNOR**2))RETURN
C
 22      X=.3601016D0*DLOG(rcarry(seed))
         IF(-2.D0*DLOG(rcarry(seed)).LE.X**2)GOTO 22
         RNOR=DSIGN(XN-X,RNOR)
      RETURN
 11   RNOR=DSIGN(B-B*X,RNOR)
      RETURN
      END


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: help on interfacing with fortran
       [not found] <CB1575D4D198D311A3F800600803947E70B5F5@scopent3.mar.hp.com>
@ 2000-01-20  0:00 ` Oliver Kellogg
  2000-01-22  0:00   ` Paolo M. Pumilia
  0 siblings, 1 reply; 7+ messages in thread
From: Oliver Kellogg @ 2000-01-20  0:00 UTC (permalink / raw)


In article <CB1575D4D198D311A3F800600803947E70B5F5@scopent3.mar.hp.com>,
  pumilia@est.it wrote:
> I would like to interface a set of fortran soubroutines and functions
> to main Ada procedure.

You could also rewrite the Fortran in Ada.
There is a free Fortran-77 To Ada95 translator perl script
available at

  ftp://ftp.usafa.af.mil/pub/dfcs/carlisle/usafa/for2ada95/

which might help you doing this.

Hope this helps.

Oliver M. Kellogg
-- okellogg at freenet dot de


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: help on interfacing with fortran
  2000-01-22  0:00   ` Paolo M. Pumilia
@ 2000-01-22  0:00     ` Oliver Kellogg
  2000-01-23  0:00     ` Oliver Kellogg
  1 sibling, 0 replies; 7+ messages in thread
From: Oliver Kellogg @ 2000-01-22  0:00 UTC (permalink / raw)


In article <388994BF.B4B52F29@cstc.org>,
  pumilia@est.it wrote:
> I tried to use your perl conversion script,  but it seems the result
is
> not yet satisfying.

As is mentioned in the comments in f2a.pl, the script
is not meant to deliver a one-hundred percent automatic
translation. Instead, it just does most of the primitive
substitution work in going from Fortran to Ada.

I actually see this is an *advantage* -- the programmer
is forced to rethink the overall design and the logic
of the program. That's usually what should be done
anyway in goingfrom one programming language to another.

Apart from that, for2ada95 is also still in development,
and I will try to fix the problems you report.

-- Oliver Kellogg



Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: help on interfacing with fortran
  2000-01-20  0:00 ` Oliver Kellogg
@ 2000-01-22  0:00   ` Paolo M. Pumilia
  2000-01-22  0:00     ` Oliver Kellogg
  2000-01-23  0:00     ` Oliver Kellogg
  0 siblings, 2 replies; 7+ messages in thread
From: Paolo M. Pumilia @ 2000-01-22  0:00 UTC (permalink / raw)
  To: Ed Falis

I tried to use your perl conversion script,  but it seems the result is
not yet satisfying.

Here are my first observations:

- The function  icarry  was strangely  defined as Real rather than
integer
- The vector  defined in the statement
        DIMENSION V(65)
    is not recognized, V  being  defined as a simple Double variable.
- The long line of the data assigned to V is not split. GNAT complains
that such
    line is too long


-  The stemement
         DATA AA,B,C/12.37586d0,.4878992d0,12.67706d0/
       1    C1,C2,PC,XN/.9689279d0,1.301198d0,.1958303d-1,2.776994d0
     is not properly treated.

After the output from  f2a.pl  has been fixed by hand, GNAT terminates
with error:
>  rnor.adb:36:21: missing operand
>  rnor.adb:113:12: reserved word "mod" cannot be used as identifier
>  gnatmake: "rnor.adb" compilation error


Line 36 is:                XN(i) := mod (iabs(idum) * i, 37);
Line 113 is:              J := MOD(IABS(I), 64) + 1;

thank you for your help, anyway

Pol





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: help on interfacing with fortran
  2000-01-22  0:00   ` Paolo M. Pumilia
  2000-01-22  0:00     ` Oliver Kellogg
@ 2000-01-23  0:00     ` Oliver Kellogg
  1 sibling, 0 replies; 7+ messages in thread
From: Oliver Kellogg @ 2000-01-23  0:00 UTC (permalink / raw)


Pol,

I tried to send this as a private e-mail to you, but the
mail bounced. Anyway, the updated for2ada95 version 1.0
corrects most of the problems you reported.
It sould appear shortly at
ftp://ftp.usafa.af.mil/pub/dfcs/carlisle/usafa/for2ada95/

I suggest you report further problems to me via e-mail
as I doubt these are of interest to the general
comp.lang.ada public.

Take care.

-- okellogg at freenet dot de


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: help on interfacing with fortran
  2000-01-16  0:00 ` Gisle S�lensminde
@ 2000-01-25  0:00   ` pumilia
  0 siblings, 0 replies; 7+ messages in thread
From: pumilia @ 2000-01-25  0:00 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1232 bytes --]

In article <slrn883q7d.899.gisle@apal.ii.uib.no>,
  gisle@apal.ii.uib.no (Gisle S�lensminde) wrote:
> In article <38818E32.FD90616B@cstc.org>, Paolo M. Pumilia wrote:
> >
> >I would like to interface a set of fortran soubroutines and functions
> >to main Ada procedure.
> > ......
> Here is a pice of code that do what you tried to do. Since it seems
> that you have some problem with the Ada syntax in general, I have
> tried to make some hopefully useful comments in the code. The code
> seemed to work on SGI IRIX 6.5 with gnat 3.11b and g77. The code was
> compiled with:
>
> g77 -c -fno-second-underscore rnor.f
> gnatmake ada_main.adb -largs rnor.o
>
Thank you for your help;
It works perfectly now. Pseudo random numbers generated by rnor.f are
exactly the same wether is is calld by a fortran main program or by ada
procedure.
Compilation of rnor.o on a intel i686 platform has been done using
different options:

% g77 -fno-second-underscore -fno-automatic -finit-local-zero -c  rnor.f

to initialize to binary zeros parameters not passed as an argument and
to save current values of local variables in functions and subroutines.
Thank you again

Paolo Pumilia


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2000-01-25  0:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-01-16  0:00 help on interfacing with fortran Paolo M. Pumilia
2000-01-16  0:00 ` Gisle S�lensminde
2000-01-25  0:00   ` pumilia
     [not found] <CB1575D4D198D311A3F800600803947E70B5F5@scopent3.mar.hp.com>
2000-01-20  0:00 ` Oliver Kellogg
2000-01-22  0:00   ` Paolo M. Pumilia
2000-01-22  0:00     ` Oliver Kellogg
2000-01-23  0:00     ` Oliver Kellogg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox