Examples of integrating Fortran code in Julia

1,283 views
Skip to first unread message

Vathy M. Kamulete

unread,
Dec 16, 2014, 9:04:43 PM12/16/14
to julia...@googlegroups.com
I posted this on StackOverflow. It was recommended I post here. See here for background.

Where can I find good examples of integrating (modern) Fortran code with Julia?

I am using the GNU gfortran compiler (on Cygwin) for my own module. A good example will hopefully start from the compilation stage, address mangled names and call the subroutine from Julia via ccall. Most examples I've seen skip the first two stages. On the SO post, I refer to Modern Fortran explicitly because what I've seen so far tends to be for legacy code -- think punchcard-style fixed-width formatting Fortran (that goes for GLMNet, which was allegedly written in 2008 but adheres to those conventions).

So imagine that I have the following module in Fortran90 file named 'f90tojl.f90':

module m
contains
   integer function five()
      five = 5
   end function five
end module m

This example is from here. I compile it with gfortan as follows to create a shared library:

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

And my, admittedly shaky, understanding from reading the julia docs suggest that I should be able to call the function five like so:

ccall( (:__m_MOD_five, "f90tojl"), Int, () )

It didn't work for me. I get ''error compiling anonymous: could not load module f90tojl... ". Anyone cares to enlighten me? I got the sneaky sense I'm doing something silly....

Thanks in advance,

V.

Isaiah Norton

unread,
Dec 16, 2014, 11:27:41 PM12/16/14
to julia...@googlegroups.com
With the following changes, your example works for me on mingw64:

"-o" needs to be paired with a filename:

  gfortran -shared -O2 f90tojl.f90 -fPIC -o f90tojl.dll

This will allow your original ccall invocation to work. Julia doesn't look at ".so" files by default on windows, but the following pair will also work; the shared library extension is given explicitly in the ccall:

  gfortran -shared -O2 f90tojl.f90 -fPIC -o f90tojl.so
  julia> ccall( (:__m_MOD_five, "f90tojl.so"), Int, () )

Hope this helps to get started. I agree that it would be great to have a working Fortran example -- if you have a suggested example (or even this one) please consider making a pull request (see https://github.com/JuliaLang/julia/blob/master/CONTRIBUTING.md).

Mauro

unread,
Dec 17, 2014, 1:46:43 AM12/17/14
to julia...@googlegroups.com
I've posted the link to this blog post before, maybe it can help:
http://maurow.bitbucket.org/notes/calling_fortran_from_misc.html

On Tue, 2014-12-16 at 20:27, Isaiah Norton <isaiah...@gmail.com> wrote:
> With the following changes, your example works for me on mingw64:
>
> "-o" needs to be paired with a filename:
>
> gfortran -shared -O2 f90tojl.f90 -fPIC -o f90tojl.dll
>
> This will allow your original ccall invocation to work. Julia doesn't look
> at ".so" files by default on windows, but the following pair will also
> work; the shared library extension is given explicitly in the ccall:
>
> gfortran -shared -O2 f90tojl.f90 -fPIC -o f90tojl.so
> julia> ccall( (:__m_MOD_five, "f90tojl.so"), Int, () )
>
> Hope this helps to get started. I agree that it would be great to have a
> working Fortran example -- if you have a suggested example (or even this
> one) please consider making a pull request (see
> https://github.com/JuliaLang/julia/blob/master/CONTRIBUTING.md).
>
>
> On Tue, Dec 16, 2014 at 9:04 PM, Vathy M. Kamulete <vath...@gmail.com>
> wrote:
>
>> I posted this on StackOverflow. It was recommended I post here. See here
>> <http://stackoverflow.com/q/27498755/1965432>for background.
>>
>> Where can I find good examples of integrating (modern) Fortran code with
>> Julia?
>>
>> I am using the GNU gfortran compiler (on Cygwin) for my own module. A good
>> example will hopefully start from the compilation stage, address mangled
>> names and call the subroutine from Julia via ccall. Most examples I've seen
>> skip the first two stages. On the SO post, I refer to Modern Fortran
>> explicitly because what I've seen so far tends to be for legacy code --
>> think punchcard-style fixed-width formatting Fortran (that goes for GLMNet,
>> which was allegedly written in 2008 but adheres to those conventions).
>>
>> So imagine that I have the following module in Fortran90 file named
>> 'f90tojl.f90':
>>
>> module m
>> contains
>> integer function five()
>> five = 5
>> end function five
>> end module m
>>
>> This example is from here
>> <http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_Fortran>. I

Tony Kelman

unread,
Dec 17, 2014, 6:03:36 PM12/17/14
to julia...@googlegroups.com
Also you're going to be better off using the MinGW-w64 cross-compilers, rather than the Cygwin's own gfortran. Try installing mingw64-x86_64-gcc-fortran through Cygwin's setup for 64 bit, or mingw64-i686-gcc-fortran for 32 bit. Then instead of calling gfortran to compile your Fortran code, call x86_64-w64-mingw32-gfortran (or i686-w64-mingw32-gfortran for 32 bit). Everything else should work more or less the same.

Vathy M. Kamulete

unread,
Dec 21, 2014, 12:16:24 PM12/21/14
to julia...@googlegroups.com
Maybe I'll need to switch to mingw64. On Cygwin, trying to compile issues a warning:

f951: warning: -fPIC ignored for target (all code is position independent) [enabled by default]

That's for both ways of compiling: .so and dll.
And I still get the dreaded error:


''error compiling anonymous: could not load module f90tojl... "

I think CodeBlocks comes with mingw64 so I'll give that a try soon...

Thanks,
V.

Isaiah Norton

unread,
Dec 21, 2014, 12:20:37 PM12/21/14
to julia...@googlegroups.com
The warning doesn't matter - you can just remove -fPIC. Try running the ccall from Julia in the same directory as your .dll file (which must exist..) or add the path to DL_LOAD_PATH.

pokerho...@googlemail.com

unread,
Jan 18, 2016, 3:48:17 PM1/18/16
to julia-users
Hi,

first of all, I am new to Julia (and Fortran). I tried to follow OP's example and call Fortran from Julia. First, I was using the Intel Fortran compiler and tried to compile the following .f90 file (saved as f90tojl.f90)


module m
contains
   integer function five()
      five = 5
   end function five
end module m

as a shared library, by entering the following in the command line:

ifort f90tojl.f90 -O2 -dll -fPIC -o  ifortlib.dll

the compiler ignores -fPIC since that's an unknown option apparently but two files are created, one object file and the dll. I then try to call the function from Julia and that's where the trouble starts.

The suggested command in this thread doesn't work because I guess the Intel compiler has different name mangling than the gfortran compiler. So I tried to find the name of my function, wikipedia suggests m_MP_five_ for ifort, so I tried

julia: ccall( (:m_MP_five, "ifortlib.dll"), Int, () )
LoadError: ccall: could not find function m_MP_five in library ifortlib.dll


So my guess is that I am not using the correct name mangling. I couldn't find anything online so I tried to view the function name via the object manager in visual studio and an external dll viewer program.
In visual studio I got a meaningless error the external viewer just didn't do anything (although it worked for other dll files). When I type

julia: Libdl.dlopen("ifortlib.dll")

Ptr{Void} @0x000000002a9d8fa0

fwiw. At this point I got so pissed that I decided to install the gfortran compiler and just follow this thread step by step. So in the cmd window, I type:

gfortran -shared -O2 f90tojl.f90 -fPIC -o gfortlib.dll

(I get a warning that -fPIC is ignored, as written previously in this thread). I use the dll viewer to determine the name of the function, its __m_MOD_five indeed. Then

julia: ccall( (:__m_MOD_five, "gfortlib.dll"), Int, () )

LoadError: error compiling anonymous: could not load library "gfortlib.dll" The specified module could not be found.


And

julia: Libdl.dlopen("gfortlib.dll")

LoadError: could not load library "gfortlib.dll" The specified module could not be found.


And I have no clue what to do now.




Pieterjan Robbe

unread,
Jan 18, 2016, 5:06:52 PM1/18/16
to julia-users
I think you need to specify the path that points to the module, not just the module name.

Mauro

unread,
Jan 19, 2016, 4:08:20 AM1/19/16
to julia...@googlegroups.com
Welcome to Julia!

You probably shouldn't necromance such an old thread, instead make a new
one and link to the old ones you researched.

Anyway, in the old thread, I linked to a blog post of mine on this topic
(I just saw that its formatting was all messed up: now fixed):
http://maurow.bitbucket.org/notes/calling_fortran_from_python.html
http://maurow.bitbucket.org/notes/calling_fortran_from_c.html

The post outlines the steps I did to make it work and how you can use
the iso_c_binding module which prevents name-mangling and provides
C-datatypes. Maybe this helps.

pokerho...@googlemail.com

unread,
Jan 19, 2016, 7:15:45 PM1/19/16
to julia-users

Yes, sorry, next time I will open a new thread. I actually already have a new problem, so I am going to post that in a new thread.

This particular problem here has been solved. I didn't manage to get it working with ifort (I guess there is no hope?) and with gfortran I noticed that the standard installation of this mingw was somehow 32bit. So I had to reinstall it using the 64bit version and then calling Fortran from Julia worked.


Reply all
Reply to author
Forward
0 new messages