fortran - 在 Fedora Linux 上使用 gfortran 与 LAPACK 链接
问题描述
我需要一些关于在 Fedora 机器上安装 LAPACK(Fortran 95)的帮助。
在尝试了此论坛中相关查询的所有答案后,我正在写这篇文章。
我尝试了两种不同的选择:
方法一
我直接从这里下载了lapack95.a库(http://www.netlib.org/lapack95/,文件名为lapack95_linux_redhat.tgz。解压后,我可以得到文件lapack95.a
在寻找有关如何链接和使用库的一些答案后,我编写了一个试用代码并保存在文件 try.f90 中,然后执行以下操作
gfortran -c try.f90
gfortran -o try try.o -L/(name of directory where lapack95.a
was present. I put it in the same directory
as try.f90)/ -llapack95
(fortran 文件 try.f90 和 lapack95.a 在同一目录中)
我收到以下错误:
/usr/bin/ld: cannot find -llapack95
collect2: error: ld returned 1 exit status
我在互联网上搜索,发现这里 ( https://forums.fedoraforum.org/archive/index.php/t-248227.html ) 我们需要安装 lapack-devel。我这样做了,结果也一样。
方法二
我也尝试运行上述命令(在方法 1 中),但 / 和 -llapack95 之间没有空格,即:
gfortran -o try try.o -L/(name of directory where lapack95.a
was present I put it in the same directory
as try.f90)/-llapack95
并收到以下错误:
try.o: In function `MAIN__':
try.f90:(.text+0xdb): undefined reference to `sgesv_'
collect2: error: ld returned 1 exit status
方法三
没有成功后,我觉得直接下载的lapack95.a可能不好用。我应该从原始文件构建它。
我意识到我可能需要通过适当的方式安装 lapack95,然后从同一个链接 http://www.netlib.org/lapack95
我下载了文件 lapack95.tgz 并按照自述文件中的说明进行操作。(说只是在 SRC 中运行 make 文件)
我做了一些改变:
FC1 = f95 -fixed to
FC1 = f95
和
OPTS0 = -u -V -dcfuns -dusty -ieee=full
至
OPTS0 = -u -V
我预计该库将在 /usr/lib/ 或 usr/local/lib 中创建为 LAPACK3 但找不到它。
事实上有一个错误(因此不能有任何 .a 文件)
f95 -free -c -u -V - -I./../lapack95_modules f77_lapack_double.f90 f95: 错误:当输入来自标准输入时需要 -E 或 -x make: *** [../make.inc: 45: f77_lapack_double.o] 错误 1
我不确定我的任何方法是否都可以。我希望方法 1 能够工作,因为我在本地链接库,但在所有 3 种情况下都失败了。
请建议。谢谢
解决方案
Lapack 库是一个非常有用的工具,可以快速进行矩阵运算。在 linux 机器上,使用内置工具安装 lapack 非常简单:dnf/yum 或 apt-get [2]。在我的 Fedora 桌面上,安装是这样的:
sudo dnf install lapack64-devel.x86_64
这是一个关于如何使用 lapack lib 的小例子。源代码是求解线性方程为linear_eqs.f [3]。
Program LinearEquations
c solving the matrix equation A*x=b using LAPACK
Implicit none
c declarations, notice single precision
Real*4 A(3,3), b(3)
integer i, j, pivot(3), ok
c define matrix A
A(1,1)=3.1
A(1,2)=1.3
A(1,3)=-5.7
A(2,1)=1.0
A(2,2)=-6.9
A(2,3)=5.8
A(3,1)=3.4
A(3,2)=7.2
A(3,3)=-8.8
c define vector b, make b a matrix and you can solve multiple
c equations with the same A but different b
b(1)=-1.3
b(2)=-0.1
b(3)=1.8
c find the solution using the LAPACK routine SGESV
call SGESV(3, 1, A, 3, pivot, b, 3, ok)
c
c parameters in the order as they appear in the function call
c order of matrix A, number of right hand sides (b), matrix A,
c leading dimension of A, array that records pivoting,
c result vector b on entry, x on exit, leading dimension of b
c return value
c
c print the vector x
do i=1, 3
write(*,*) b(i)
end do
end
在此代码中,使用了一个 lapack 函数“sgesv()”。要执行此文件,请运行以下命令:
gfortran linear_eqs.f -llapack -o lineqs.out
./lineqs.out
输出将是:
1.00000024
1.00000036
1.00000036
参考
[1] https://github.com/fxcoudert/gfortran-for-macOS/releases
[3] http://physics.oregonstate.edu/~landaur/nacphy/lapack/codes/linear-f.html
推荐阅读
- typescript - 如何描述该函数通过引用改变对象?
- c# - 当代码遇到异常情况时程序立即停止,需要程序在异常抛出后重新提示用户
- python-2.7 - 如何获取 dag 状态,如运行、成功或失败
- go - 简单服务的微 rpc 请求慢
- multithreading - ZeroMQ:从多个线程将有序消息推送到服务器的 inproc 客户端的推荐模式?
- git - .gitignore 在任何情况下都不起作用,这里的失败点是什么?
- javascript - 延迟加载的 jQuery 数据表上的多行选择
- api - NetSuite API - 在销售订单下添加“项目组”
- swiftui - 第十一 HStack(或其他 UI 组件)创建编译错误,“参数传递给不带参数的调用”
- php - 在 PHP 类中,调用内部方法显示错误消息