Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Fortran > Re: Call Array ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 11 Topic 8155 of 8775
Post > Topic >>

Re: Call Array valued Fortran function from C

by "James Van Buskirk" <not_valid@[EMAIL PROTECTED] > Apr 19, 2008 at 05:14 PM

"FX" <coudert@[EMAIL PROTECTED]
> wrote in message 
news:fudrd5$9ts$1@[EMAIL PROTECTED]
>> But you should ask the gfortran folks if they can round up some
>> do***entation about gfortran array descriptors for you.  They are
>> different in every compiler.

> Yep, that's on the TODO list. Here's the short version (in C syntax).

Thanks for the do***entation, FX!  Since I can never seem to get all
my ducks in a row on the first attempt, I tried a pure Fortran test
of my initial recommendation and found some syntax errors.  After
fixing them up I got:

C:\gfortran\clf\repeatdouble>type repeatdouble.f90
function repeatdouble( N, d ) result( array )
use iso_c_binding
   implicit none
   integer, intent( in ) :: N
   double precision, dimension( N ) :: array
   double precision, intent( in ) :: d
   integer I
   print *, "repeat ", N, " times ..."
   print *, "double ", d, "."
   print *, "array :", array
   do I = 1,N
      print *, "making copy nb", i, " of ", N
      array(I) = d
   end do
end function repeatdouble

C:\gfortran\clf\repeatdouble>type test3.f90
program main
   use ISO_C_BINDING
   implicit none
   type, bind(C) :: descr
      type(C_PTR) address
      integer(C_INTPTR_T) unknown1
      integer(C_INTPTR_T) unknown2
      integer(C_INTPTR_T) stride
      integer(C_INTPTR_T) unknown3
      integer(C_INTPTR_T) last_element
   end type descr
   interface
      subroutine repeatdouble(DD,n,d) bind(C,name='repeatdouble_')
         use ISO_C_BINDING
         im****t descr
         implicit none
         type(descr) DD
         integer(C_INT) n
         real(C_DOUBLE) d
      end subroutine repeatdouble
   end interface
   type(descr) DD
   double precision, target :: buf(9)
   double precision d
   integer n

   buf = [(n,n=1,9)]
   d = 3.141592654
   n = 4

   DD = descr(C_LOC(buf), 0, 537, 1, 0, n-1)
   call repeatdouble(DD, n, d)
   write(*,'(9(f4.1))') buf
end program main

C:\gfortran\clf\repeatdouble>c:\gcc_equation\bin\x86_64-pc-mingw32-gfortran
 
-c r
epeatdouble.f90

C:\gfortran\clf\repeatdouble>c:\gcc_equation\bin\x86_64-pc-mingw32-gfortran

test
3.f90 repeatdouble.o -otest3

C:\gfortran\clf\repeatdouble>test3
 repeat            4  times ...
 double    3.1415927410125732      .
 array :
 making copy nb           1  of            4

So the test failed.  I found the reason in test3.f90:

C:\gfortran\clf\repeatdouble>type test3.f90
program main
   use ISO_C_BINDING
   implicit none
   type, bind(C) :: descr
      type(C_PTR) address
      integer(C_INTPTR_T) unknown1
      integer(C_INTPTR_T) unknown2
      integer(C_INTPTR_T) stride
      integer(C_INTPTR_T) unknown3
      integer(C_INTPTR_T) last_element
   end type descr
   interface
      subroutine repeatdouble(DD,n,d) bind(C,name='repeatdouble_')
         use ISO_C_BINDING
         im****t descr
         implicit none
         type(descr) DD
         integer(C_INT) n
         real(C_DOUBLE) d
      end subroutine repeatdouble
   end interface
   type(descr) DD
   double precision, target :: buf(9)
   double precision d
   integer n

   buf = [(n,n=1,9)]
   d = 3.141592654
   n = 4

   DD = descr(C_LOC(buf), 0, 537, 1, 0, n-1)
! Shows the error: start
write(*,*) DD
write(*,*) C_LOC(buf)
DD%address = C_LOC(buf)
write(*,*) DD
! Shows the error: end
   call repeatdouble(DD, n, d)
   write(*,'(9(f4.1))') buf
end program main

C:\gfortran\clf\repeatdouble>c:\gcc_equation\bin\x86_64-pc-mingw32-gfortran
 
-c r
epeatdouble.f90

C:\gfortran\clf\repeatdouble>c:\gcc_equation\bin\x86_64-pc-mingw32-gfortran

test
3.f90 repeatdouble.o -otest3

C:\gfortran\clf\repeatdouble>test3
                    0                    0                  537
   1                    0                    3
              2293200
              2293200                    0                  537
   1                    0                    3
 repeat            4  times ...
 double    3.1415927410125732      .
 array :  1.00000000000000000        2.0000000000000000 
3.000000000000000
0        4.0000000000000000
 making copy nb           1  of            4
 making copy nb           2  of            4
 making copy nb           3  of            4
 making copy nb           4  of            4
 3.1 3.1 3.1 3.1 5.0 6.0 7.0 8.0 9.0

So it seems that the structure constructor for type(descr) isn't
assigning that first member, address.  If you do it by hand
everything works.  This is a pretty recent version of gfortran,
so I think that the above example shows a bug in the compiler.

-- 
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end
 




 11 Posts in Topic:
Call Array valued Fortran function from C
koentjepoppe@[EMAIL PROTE  2008-04-19 10:33:39 
Re: Call Array valued Fortran function from C
glen herrmannsfeldt <g  2008-04-19 12:11:20 
Re: Call Array valued Fortran function from C
"James Van Buskirk&q  2008-04-19 15:54:39 
Re: Call Array valued Fortran function from C
"FX" <couder  2008-04-19 22:23:33 
Re: Call Array valued Fortran function from C
"James Van Buskirk&q  2008-04-19 17:14:50 
Re: Call Array valued Fortran function from C
"FX" <couder  2008-04-20 10:49:38 
Re: Call Array valued Fortran function from C
"James Van Buskirk&q  2008-04-20 09:26:43 
Re: Call Array valued Fortran function from C
"James Van Buskirk&q  2008-04-20 21:21:14 
Re: Call Array valued Fortran function from C
Sebastian Hanigk <hani  2008-04-20 09:44:34 
Re: Call Array valued Fortran function from C
nf.editor@[EMAIL PROTECTE  2008-04-20 08:56:10 
Re: Call Array valued Fortran function from C
"FX" <couder  2008-04-20 16:11:16 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 13 8:42:50 CDT 2008.