Reputation: 99
I digged on the forum looking for a solution but I failed. My main problem is I am too noob with C language and Fortran-C interoperability in order to understand what I am doing wrong exactly.
I want to call a C routine from Fortran but I'm having problem with the declaration of the variables. I made an example.
This is the C routine:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>
#undef I
int photon_trace(double x_init[4], double x_final[4])
//***************************************************
{
double r,m,t,phi;
t = x_init[0];
r = x_init[1];
m = x_init[2];
phi = x_init[3];
t=t+1.0;
r=r+1.0;
m=m+1.0;
phi=phi+1.0;
x_final[0] = t;
x_final[1] = r;
x_final[2] = m;
x_final[3] = phi;
return 0;
}
This is the main program in Fortran:
program main_dummy
! compile: f95 raytracing.f90 main_dummy.f90 dummy_trace.o -o main
use, intrinsic :: ISO_C_BINDING
use raytracing
implicit none
real(C_FLOAT), dimension(0:3) :: x_in, x_fin
x_in = (/1,2,3,4/)
x_fin = (/0,0,0,0/)
write(*,*)'x_in, x_fin before = ', x_in, x_fin
call photon_trace(C_FLOAT(x_in),C_FLOAT(x_fin))
write(*,*)'x_in, x_fin after = ', x_in, x_fin
end program main_dummy
And this is the module with the interface:
module raytracing
Interface
integer (C_INT) function photon_trace(x_init, x_final) &
bind(C, name='photon_trace')
use , intrinsic :: ISO_C_BINDING
implicit none
type (c_ptr), value :: x_init, x_final
end function photon_trace
end interface
end module raytracing
According to the guy who gave me the routine in C, x_init
and x_final
should be pointers (right?).
When I try to compile it gives me an error in argument list when I call photon_trace
in the main program.
Any suggestions? I'm using gcc 4.8
P.S. Does gcc 4.4 have the same capabilities with respect to iso_c_binding
or will I need to do something different for using it?
EDIT AFTER VLADIMIR COMMENT:
Thanks Vladimir! I was calling the function because I saw it on this example https://stackoverflow.com/tags/fortran-iso-c-binding/info
I made the modification you suggested and it compiles now! Problem is that the C routine seems to work with the address of the variables and not the variables itself.
I put some printing in the C routine:
int photon_trace(double x_init[4], double x_final[4])
//***************************************************
{
double r,m,t,phi;
t = x_init[0];
r = x_init[1];
m = x_init[2];
phi = x_init[3];
printf("t0 %f\n", t);
printf("r0 %f\n", r);
printf("m0 %f\n", t);
printf("phi0 %f\n", r);
t=t+1.0;
r=r+1.0;
m=m+1.0;
phi=phi+1.0;
printf("t1 %f\n", t);
printf("r1 %f\n", r);
printf("m1 %f\n", t);
printf("phi1 %f\n", r);
x_final[0] = t;
x_final[1] = r;
x_final[2] = m;
x_final[3] = phi;
return 0;
}
and also in the main:
program main_dummy
! compile: gcc -c dummy_trace.c
! f95 raytracing.f90 main_dummy.f90 dummy_trace.o -o main
use, intrinsic :: ISO_C_BINDING
use raytracing
implicit none
!real(kind=8) :: x_in(4), x_fin(4)
real(C_FLOAT), dimension(0:3) :: x_in, x_fin
integer :: rt_ok
x_in = (/1,2,3,4/)
x_fin = (/0,0,0,0/)
write(*,*)'x_in, x_fin before = ', x_in, x_fin
rt_ok = photon_trace(x_in,x_fin)
write(*,*)'return rt = ', rt_ok
write(*,*)'x_in, x_fin after = ', x_in, x_fin
end program main_dummy
and that's what is on the screen:
x_in, x_fin before = 1.00000000 2.00000000 3.00000000 4.00000000 0.00000000 0.00000000 0.00000000 0.00000000
t0 2.000000
r0 512.000123
m0 2.000000
phi0 512.000123
t1 3.000000
r1 513.000123
m1 3.000000
phi1 513.000123
return rt = 0
x_in, x_fin after = 1.00000000 2.00000000 3.00000000 4.00000000 1.00000000 2.12500000 3.00000000 4.00097656
What's happening here?
Many thanks in advance for the precious help!!
Upvotes: 2
Views: 716
Reputation: 60008
Do not over-complicate it. Don't pass the pointers, but pass the arrays by reference
Also, a function must be used in an expression, not in a call statement.
module raytracing
Interface
integer (C_INT) function photon_trace(x_init, x_final) &
bind(C, name='photon_trace')
use , intrinsic :: ISO_C_BINDING
implicit none
real(c_double) :: x_init(4), x_final(4)
end function photon_trace
end interface
end module raytracing
program main_dummy
! compile: f95 raytracing.f90 main_dummy.f90 dummy_trace.o -o main
use, intrinsic :: ISO_C_BINDING
use raytracing
implicit none
real(c_double), dimension(0:3) :: x_in, x_fin
integer ie
x_in = (/1,2,3,4/)
x_fin = (/0,0,0,0/)
write(*,*)'x_in, x_fin before = ', x_in, x_fin
ie = photon_trace(x_in,x_fin)
write(*,*)'x_in, x_fin after = ', x_in, x_fin
end program main_dummy
In C interoperable procedures Fortran passes pointers to the variables, unless you use value
. This is called pass by reference.
> gfortran ray.c ray.f90
> ./a.out
x_in, x_fin before = 1.0000000000000000 2.0000000000000000 3.0000000000000000 4.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000
x_in, x_fin after = 1.0000000000000000 2.0000000000000000 3.0000000000000000 4.0000000000000000 2.0000000000000000 3.0000000000000000 4.0000000000000000 5.0000000000000000
P.S. gcc 4.4 very old, but it theoretically it knows the C interop stuff. Try it and you will see.
Upvotes: 4