I want to call functions in my Fortran library from Julia. In this case, I have a function eye
that takes an Integer, and returns a two-dimensional array of integers.
The Fortran module is compiled into a shared library using
$ gfortran -shared -fPIC -o matrix_routines.so matrix_routines.f90
And thereafter I am attempting to call it from the interactive Julia interpreter like that (name obtained from nm
):
julia> n=5
5
julia> ccall( (:__matrix_routines_MOD_eye, "/path/to/library/matrix_routines.so"), Array{Int64,2} , (Ptr{Int64},), &n )
This, however, immediately results in Julia throwing a segfault at me:
signal (11): Segmentation fault
__matrix_routines_MOD_eye at /path/to/library/matrix_routines.so (unknown line)
anonymous at no file:0
unknown function (ip: -1137818532)
jl_f_top_eval at /usr/bin/../lib/julia/libjulia.so (unknown line)
eval_user_input at REPL.jl:53
jlcall_eval_user_input_19998 at (unknown line)
jl_apply_generic at /usr/bin/../lib/julia/libjulia.so (unknown line)
anonymous at task.jl:95
jl_handle_stack_switch at /usr/bin/../lib/julia/libjulia.so (unknown line)
julia_trampoline at /usr/bin/../lib/julia/libjulia.so (unknown line)
unknown function (ip: 4199613)
__libc_start_main at /usr/bin/../lib/libc.so.6 (unknown line)
unknown function (ip: 4199667)
unknown function (ip: 0)
zsh: segmentation fault (core dumped) julia
Am I calling the function the wrong way? What is the correct name of the function? (It doesn't appear to be just eye
, as that doesn't work either.)
As an additional question: does Julia do anything with the memory-orientation of the resulting arrays? Fortran and Julia are both column-major, but I wonder if due to ccall() Julia might think it should tranpose them?
module matrix_routines
implicit none
private
public :: eye
contains
pure function eye(n,offset) result(um) !{{{
integer, intent(in) :: n
integer, intent(in), optional :: offset
integer, dimension(n,n) :: um
integer :: i, l, u, os
um = 0
l = 1
u = n
os = 0
if (present(offset)) then
os = offset
end if
if (abs(os) < n) then
if (os > 0) then
u = n - os
else if (os < 0) then
l = 1 - os
end if
do i=l, u
um(i, i+os) = 1
end do
end if
end function eye !}}}
end module matrix_routines
gfortran -Wall -fcheck=all ...
? - rickhg12hsuse
ing the module in my Fortranprogram
, which of course has an explicit interface through the.mod
file. Note, I have removed theoptional
argument, but this still results in a segfault. Are you suggesting that I have to useiso_c_binding
? @rickhg12hs: Nope, nothing at all. No warnings. - mSSM