在 Julia 中集成 Fortran 代码 [英] Integrating Fortran code in Julia
问题描述
我正在为我自己的模块使用 GNU gfortran 编译器(在 Cygwin 上).一个很好的例子有望从编译阶段开始,处理错误的名称并通过 ccall
从 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.
假设我在 Fortran 90 文件中有以下模块,名为f90tojl.f90":
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
这个例子来自这里.我用 gfortran 编译它来创建一个共享库:
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
我的,诚然,从阅读 Julia 文档的理解中,我的理解表明我应该能够像这样调用函数 5:
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, () )
它对我不起作用.我得到 '错误编译匿名:无法加载模块 f90tojl...
.有人关心我吗?我有一种偷偷摸摸的感觉,我正在做一些愚蠢的事情......
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....
在官方 doc,重点是 C.我也知道 C++ 的 this.在 R 和 Python 中,动力——我想到了 Cython 和 Rcpp——似乎是 C/C++.类似于这个 问题,我想了解 Julia 与 Fortran 与 Julia 与 C/的接口是多么容易C++.
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++.
推荐答案
如文档所述,调用 Fortran 和 C 本质上是相同的.我想,示例要少得多,因为人们想要包装的代码要少得多.在 Julia 中包装 C 非常简单,非常愉快.它通常不是为了加速 Julia,就像使用 R 或 Python 一样,而是更多地利用已经编写的高质量代码.
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 的标准库本身就是与 Fortran 代码集成的一个很好的例子,例如这是 Julia 包装器 的 ARPARK.在 Base
之外,glmnet 是用 Fortran 编写的,并且有一个 Julia 包装器 (GLMNet.jl).
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).
我不明白现代 Fortran 的区别有多重要.
I don't see how the modern Fortran distinction matters.
这篇关于在 Julia 中集成 Fortran 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!