Calling the Fortran routine from Julia. Arrays work, but integers don't

I have this simple Fortran 90 program:

subroutine apc_wrapper(i, j, k) implicit none integer*8, intent(in) :: i, j integer*8, intent(out) :: k double precision t k = i + js end subroutine 

compiled as a shared library

 gfortran -O2 -shared -fPIC apc_wrapper.f90 -o apc_wrapper.so 

Now I want to call this routine from Julia with all integer arguments, for example,

 i = 2 j = 3 k = 0 ccall( (:apc_wrapper_, "./apc_wrapper.so"), Void, (Ptr{Int64}, Ptr{Int64}, Ptr{Int64}), &i, &j, &k) 

But that will not work. k will not change its value and continue to estimate 0.

But if I do it

  i = 2 j = 3 kk = [0] ccall( (:apc_wrapper_, "./apc_wrapper.so"), Void, (Ptr{Int64}, Ptr{Int64}, Ptr{Int64}), &i, &j, kk) 

That is, use an array to store the output, it works! After calling the subroutine, kk evaluates to

  1-element Array{Int64,1}: 5 

And I didn’t change Fortran code at all, he didn’t even know that he was dealing with an array, just a block of memory.

So, if Fortran is able to read memory blocks ( i and j were correctly red), why can't they write to them?

I have no problem with this. Actually, I want to use an array as output, but still this behavior surprised me.

+7
fortran fortran90 julia-lang
source share
1 answer

Well, Julia is a fast-paced language, and it turns out that the &variable syntax is deprecated. This would be the correct way:

 i = 2 j = 3 k = 0 i_ref = Ref{Int64}(i) j_ref = Ref{Int64}(j) k_ref = Ref{Int64}(k) ccall( (:apc_wrapper_, "./apc_wrapper.so"), Void, (Ref{Int64}, Ref{Int64}, Ref{Int64}), i_ref, j_ref, k_ref) 

and then k_ref.x will be evaluated as 5 .

+9
source share

All Articles