I’ve been trying to make a very simple example of Julia calling fortran to work but it seems I’m missing something, right now what I’ve done is:
Fortran code:
module ipmf
use, intrinsic :: iso_c_binding
implicit none
private
public :: wavg_f
subroutine wavg_f(x, w, n, wavg) bind(C, name="wavg_f_")
integer(kind = c_int), intent(in), value :: n !Length of x and w
real(kind = c_double), intent(in), dimension(n) :: x !Vector of values
real(kind = c_double), intent(in), dimension(n) :: w !Vector of weights
real(kind = c_double), intent(out) :: wavg !output
real(kind = c_double) :: wsum !Sum of weights
integer :: i !Internal count
wavg = 0.0
wsum = 0.0
do i = 1, n
wsum = wsum + w(i)
end do
do i = 1, n
wavg = wavg + x(i)*w(i)/wsum
end do
end subroutine wavg_f
end module ipmf
Compiled the code using:
> gfortran -shared -fPIC ipmf.f95 -o ipmf.so
and finally, my Julia code looks like this:
using Libdl
lib = Libdl.dlopen("libs/ipmf.so")
sym = Libdl.dlsym(lib, :wavg_f_)
x = Float64[1,2,3]
w = Float64[1,1,1]
n = 3
wavg = 0.0
ccall(sym, Cvoid, (Ref{Float64}, Ref{Float64}, Ref{Int64}, Ref{Float64}), x, w, Ref(n), Ref(wavg))
but instead of getting wavg = 2.0, I’m getting this:
julia> wavg
0.0
I’m sure I’m doing something wrong, I do not have that much experience using Julia… I’d appreciate any help. Thanks!