comp.lang.ada
 help / color / mirror / Atom feed
* Passing Unconstrained Arrays from FORTRAN to an Ada95 subunit
@ 2002-07-20  0:57 Karen Carron
  2002-07-20  2:34 ` tmoran
  2002-07-22  1:48 ` Robert Dewar
  0 siblings, 2 replies; 7+ messages in thread
From: Karen Carron @ 2002-07-20  0:57 UTC (permalink / raw)


What I have found is that the offset in referencing individual array
elements in FORTRAN is 1 (ex. the first element is referenced with a
subscript of value 1) but in Ada it is 0.  This is causing a problem
since the subscripts are variables in a fortran common.

Here is an example of what I'm trying to do:

FORTRAN calling routine:
program callada
common /subs/ i1,i2,i3
integer a(10)
i1 = 1
i2 = 2
i3 = 3
a(i1) = 10
a(i2) = 20
a(i3) = 30
call adainit
call pass_array(a)
stop
end

...

Ada units:
package type_decl is 
   type int_array_type is array(integer range <>) of integer;
end type_decl;

...

with type_decl; use type_decl; 
package routines is 
   procedure pass_array (a : in out int_array_type);
   pragma export (fortran, pass_array, "pass_array");
end routines;

...

package common_subs is
   type subs_rec is
      i1, i2, i3 : integer;
   end record;
   subs : subs_rec;
   pragma volatile (subs);
   pragma import_object (subs, "subs");
end common_subs;

...

with common_subs; 
package body routines is
   -- pass_array can be called by more than one unit with varying
   -- sizes of arrays
   procedure pass_array (a : in out int_array_type) is 
      var1 : integer;
      begin
          var1 := a(common_subs.subs.i1) + a(common_subs.subs.i2);
      end pass_array;
end routines; 

In the fortran equivalent of pass_array, the var1 would be 30.  But in
the ada version, var1 is 50.  I tried the following but without any
success:

1)  I modified type_decl package as follows:

-- added:
with interfaces.fortran; use interfaces.fortran;
package type_decl is 
   type int_array_type is array(integer range <>) of integer;
   -- added:
   pragma convention (fortran, int_array_type);
end type_decl;

2)  I modified the original type_decl package as follows:

package type_decl is 
   -- I changed the range to positive from integer
   type int_array_type is array(positive range <>) of integer;
end type_decl;

I would very much appreciate any other ideas.

Thanks!



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

* Re: Passing Unconstrained Arrays from FORTRAN to an Ada95 subunit
  2002-07-20  0:57 Passing Unconstrained Arrays from FORTRAN to an Ada95 subunit Karen Carron
@ 2002-07-20  2:34 ` tmoran
  2002-07-21 13:44   ` Karen Carron
  2002-07-22  1:48 ` Robert Dewar
  1 sibling, 1 reply; 7+ messages in thread
From: tmoran @ 2002-07-20  2:34 UTC (permalink / raw)


What happens if you pass a constrained array, ie,
   type int_array_type is array(integer range 1.. 10) of integer;
to match the Fortran declaration
integer a(10)



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

* Re: Passing Unconstrained Arrays from FORTRAN to an Ada95 subunit
  2002-07-20  2:34 ` tmoran
@ 2002-07-21 13:44   ` Karen Carron
  2002-07-21 18:00     ` tmoran
  0 siblings, 1 reply; 7+ messages in thread
From: Karen Carron @ 2002-07-21 13:44 UTC (permalink / raw)


tmoran@acm.org wrote in message news:<f%3_8.1754$dC.281@sccrnsc03>...
> What happens if you pass a constrained array, ie,
>    type int_array_type is array(integer range 1.. 10) of integer;
> to match the Fortran declaration
> integer a(10)

Thank you for your reply.  Yes, this works, but the routine with the
unconstrained array is called by many other routines which pass it
varying sizes of arrays.
 
One way to get it to work is to write an interface procedure in Ada
for each call that passes the routine a constrained array. But as I
said, there are many fortran units that call this routine, hence many
interface routines would have to be written.

I found some other things to try in reading some other articles. 
Still, any ideas are very welcome.

Thanks!



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

* Re: Passing Unconstrained Arrays from FORTRAN to an Ada95 subunit
  2002-07-21 13:44   ` Karen Carron
@ 2002-07-21 18:00     ` tmoran
  2002-07-21 20:32       ` Dan Nagle
  2002-07-22  1:17       ` Karen Carron
  0 siblings, 2 replies; 7+ messages in thread
From: tmoran @ 2002-07-21 18:00 UTC (permalink / raw)


> > What happens if you pass a constrained array, ie,
> >    type int_array_type is array(integer range 1.. 10) of integer;
> Yes, this works, but the routine with the unconstrained array is called
> by many other routines which pass it varying sizes of arrays.
  One technique used in interfacing to C is to declare
    type int_array_type is array(positive) of integer;
That is, declare the array type large enough for any actual instance
and then make sure in the Ada routine that you don't index beyond
the actual data.  It used to be common practice in Fortran to pass
the length of the array as a separate parameter, eg
  procedure something(a,n)
  integer n,a(n)
which you could write in Ada as
  procedure something(a : in out int_array_type;
                      n : in positive);
Does modern Fortran pass the length implicitly?  Apparently your Ada
compiler doesn't think so.



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

* Re: Passing Unconstrained Arrays from FORTRAN to an Ada95 subunit
  2002-07-21 18:00     ` tmoran
@ 2002-07-21 20:32       ` Dan Nagle
  2002-07-22  1:17       ` Karen Carron
  1 sibling, 0 replies; 7+ messages in thread
From: Dan Nagle @ 2002-07-21 20:32 UTC (permalink / raw)


Hello,

That depends on how the array is declared in the Fortran.

integer, dimension(n) :: a          ! pass starting address
integer, dimension(*) :: a          ! pass starting address, no length
integer, dimension(:) :: a          ! pass descriptor

For the (:) case, check the Fortran compiler's documentation
for the descriptor definition.  Most compilers supply this
data, a few :-( consider this to be a proprietary secret.
A few experiments should indicate how it works anyway.

-- 
Cheers!

Dan Nagle
Purple Sage Computing Solutions, Inc.

<snip>
>Does modern Fortran pass the length implicitly?  Apparently your Ada
>compiler doesn't think so.




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

* Re: Passing Unconstrained Arrays from FORTRAN to an Ada95 subunit
  2002-07-21 18:00     ` tmoran
  2002-07-21 20:32       ` Dan Nagle
@ 2002-07-22  1:17       ` Karen Carron
  1 sibling, 0 replies; 7+ messages in thread
From: Karen Carron @ 2002-07-22  1:17 UTC (permalink / raw)


tmoran@acm.org wrote in message news:<sDC_8.81853$Wt3.54908@rwcrnsc53>...
> > > What happens if you pass a constrained array, ie,
> > >    type int_array_type is array(integer range 1.. 10) of integer;
> > Yes, this works, but the routine with the unconstrained array is called
> > by many other routines which pass it varying sizes of arrays.
>   One technique used in interfacing to C is to declare
>     type int_array_type is array(positive) of integer;
> That is, declare the array type large enough for any actual instance
> and then make sure in the Ada routine that you don't index beyond
> the actual data.  It used to be common practice in Fortran to pass
> the length of the array as a separate parameter, eg
>   procedure something(a,n)
>   integer n,a(n)
> which you could write in Ada as
>   procedure something(a : in out int_array_type;
>                       n : in positive);
> Does modern Fortran pass the length implicitly?  Apparently your Ada
> compiler doesn't think so.

Thanks for your suggestions.  I got it to work by constraining the
array to the subtype positive.  So type int_array_type is now:

type int_array_type (positive) of integer;

but it performed the same as before when I constrained int_array_type
to integer (!!!!), although it did get rid of the compiler warnings. 
I was thinking it worked because the range was now constrained, but
that appears to not be the case; not only must it be constrained, but
the lower bounds must be 1.



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

* Re: Passing Unconstrained Arrays from FORTRAN to an Ada95 subunit
  2002-07-20  0:57 Passing Unconstrained Arrays from FORTRAN to an Ada95 subunit Karen Carron
  2002-07-20  2:34 ` tmoran
@ 2002-07-22  1:48 ` Robert Dewar
  1 sibling, 0 replies; 7+ messages in thread
From: Robert Dewar @ 2002-07-22  1:48 UTC (permalink / raw)


kcarron@belcan.com (Karen Carron) wrote in message news:<5489a352.0207191657.5fac87a8@posting.google.com>...
> What I have found is that the offset in referencing individual array
> elements in FORTRAN is 1 (ex. the first element is referenced with a
> subscript of value 1) but in Ada it is 0.  This is causing a problem
> since the subscripts are variables in a fortran common.

What you are doing cannot possibly work. You need to
read the RM section on interfacing to Fortran.



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

end of thread, other threads:[~2002-07-22  1:48 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-20  0:57 Passing Unconstrained Arrays from FORTRAN to an Ada95 subunit Karen Carron
2002-07-20  2:34 ` tmoran
2002-07-21 13:44   ` Karen Carron
2002-07-21 18:00     ` tmoran
2002-07-21 20:32       ` Dan Nagle
2002-07-22  1:17       ` Karen Carron
2002-07-22  1:48 ` Robert Dewar

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