将Fortran代码集成到Julia中 [英] Integrating Fortran code in Julia
问题描述
我为自己的模块使用了GNU gfortran编译器(在Cygwin上)。一个很好的例子有望从编译阶段开始,解决出现错位的名称,并通过 ccall
从Julia调用子例程。我看过的大多数示例都跳过了前两个阶段。
所以想象一下,我在Fortran 90文件中命名为'f90tojl.f90'中有以下模块:
模块m
包含
整数函数five()
five = 5
结束函数five
end module m
这个例子来自 here 。我使用gfortran编译它以创建一个共享库:
gfortran -shared -O2 f90tojl.f90 -o -fPIC f90tojl .so
我的阅读茱莉亚文档的理解是,我应该能够像这样调用函数5: 它并不适合我。我得到 在官方 doc ,重点在C.我也知道这是C ++的。在R和Python中,动力 - 我记得Cython和Rcpp--似乎是C / C ++。与此问题类似,我想了解一下如何将Julia与Fortran与Julia的接口与C / C ++。正如文档所说,调用Fortran和C基本上是一样的。我想,只有少得多的例子,因为人们想要包装的代码少得多。在Julia包装C是非常容易的,非常愉快。它通常不是为了加速Julia,就像使用R或Python一样,但更多的是利用已写好的代码。
pre $
'错误编译匿名:无法加载模块f90tojl ...
。任何人都在意开导我吗?我得到了偷偷摸摸的感觉,我正在做一些愚蠢的事情。
Julia的标准库本身就是一个很好的例子与Fortran代码集成,例如这里是 Julia包装的.com / opencollab / ARPACK-NG> ARPARK 。在 Base
之外, glmnet 是用Fortran编写的,在那里是一个它的Julia包装(GLMNet.jl)。
我不明白现代Fortran区别如何。
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.
So imagine that I have the following module in Fortran 90 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 gfortran 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....
In the official doc, the emphasis is on C. I'm also aware of this for C++. In R and Python, the momentum -- I have Cython and Rcpp in mind -- seems to be C/C++. Similar to this question, I want to get a sense of how easy it is to interface Julia with Fortran vs Julia with C/C++.
Calling Fortran and C are essentially the same, as the documentation says. There are just far fewer examples because there is far less code people want to wrap, I suppose. Wrapping C is super easy in Julia, very pleasant. Its not typically done for speeding up Julia, like with R or Python, but more to take advantage of quality code already written.
Julia's standard library itself is a great example of integrating with Fortran code, e.g. here is the Julia wrapper for ARPARK. Outside of Base
, glmnet is written in Fortran, and there is a Julia wrapper for it (GLMNet.jl).
I don't see how the modern Fortran distinction matters.
这篇关于将Fortran代码集成到Julia中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!