定义PI的动机是什么?
PI=4.D0*DATAN(1.D0)
Run Code Online (Sandbox Code Playgroud)
在Fortran 77代码中?我理解它是如何工作的,但是,理由是什么?
我在 Fortran 和 C++ 中分别实现了一个函数:
#include <math.h>
void dbl_sqrt_c(double *x, double *y){
*y = sqrt(*x - 1.0);
return;
}
Run Code Online (Sandbox Code Playgroud)
pure subroutine my_dbl_sqrt(x,y) bind(c, name="dbl_sqrt_fort")
USE, INTRINSIC :: ISO_C_BINDING
implicit none
real(kind=c_double), intent(in) :: x
real(kind=c_double), intent(out) :: y
y = sqrt(x - 1d0)
end subroutine my_dbl_sqrt
Run Code Online (Sandbox Code Playgroud)
我在编译器资源管理器中比较了它们:
Fortran:https : //godbolt.org/z/froz4rx97
C++:https : //godbolt.org/z/45aex99Yz
我阅读汇编程序的方式,它们基本上做相同的事情,但是 C++ 检查 sqrt 的参数是否为负,而 Fortran 则没有。我使用 googles 基准比较了它们的性能,但它们非常匹配:
--------------------------------------------------------
Benchmark Time CPU Iterations
--------------------------------------------------------
bm_dbl_c/8 2.07 ns 2.07 ns 335965892
bm_dbl_fort/8 2.06 ns 2.06 …Run Code Online (Sandbox Code Playgroud) 我可以使用gcc分别使用g ++或gfortran在c和c ++之间或c和fortran之间进行调用.但是如果我尝试在c ++和fortran之间进行过程调用,那么在使用g ++或gfortran编译时会出现错误,因为它们都不知道其他所需的库.如何链接使用c ++和fortran编写的源代码的项目?
$ cat print_hi.f90
subroutine print_hi() bind(C)
implicit none
write(*,*) "Hello from Fortran."
end subroutine print_hi
$ cat main.cpp
#include <iostream>
extern "C" void print_hi(void);
using namespace std;
int main() {
print_hi();
cout << "Hello from C++" << endl;
return 0;
}
$ gfortran -c print_hi.f90 -o print_hi.o
$ g++ -c main.cpp -o main.o
Run Code Online (Sandbox Code Playgroud)
我尝试用g ++链接:
$ g++ main.o print_hi.o -o main
print_hi.o: In function `print_hi':
print_hi.f90:(.text+0x3f): undefined reference to `_gfortran_st_write'
Run Code Online (Sandbox Code Playgroud)
以及有关未定义引用的更多错误.
并与gfortran:
$ gfortran …Run Code Online (Sandbox Code Playgroud) 我是否需要将implicit none每个功能和子程序放入其中?
或者它是否足以将它放在包含这些函数和子例程的模块的开头?
或者它是否足以将它放在使用这些模块的程序的开头?
从观察其他人的工作代码,implicit none包含在所有这些地方.我不确定这是否是冗余的,因为implicit none从子程序中删除仍然编译并产生相同的输出.
顺便说一句,我正在使用gfortran fortran 90.
我一直在网上搜索,但我仍然对此话题感到困惑.谁能更清楚地解释这个?我来自航空航天工程背景(不是计算机科学专业),所以当我在网上阅读有关OpenMP/CUDA等的内容时.和多线程我真的不太了解所说的很多内容.
我目前正在尝试并行化用FORTRAN编写的内部CFD软件.这些是我的疑惑:
OpenMP使用来自CPU的多个线程共享工作负载.它可以用来让GPU也能完成一些工作吗?
我读过OpenACC.它是否类似于OpenMP(易于使用)?
我也读过有关CUDA和内核的内容,但我对并行编程没有太多经验,而且我对内核的内容并不了解.
你能给我一个"傻瓜"类型的答案吗?
我不明白fortran中未格式化文件的格式.
例如:
open (3,file=filename,form="unformatted",access="sequential")
write(3) matrix(i,:)
Run Code Online (Sandbox Code Playgroud)
将矩阵列输出到文件中.我发现它在两端填充了4个字节的文件,但是我真的不明白为什么,或者如何控制这种行为.有没有办法删除填充?
谢谢
我试图读取一些Fortran代码,但无法确定%(百分号)的作用.
这是一个像:
x = a%rho * g * (-g*a%sigma + m%gb * m%ca * (1.6 * a%rho+g))
Run Code Online (Sandbox Code Playgroud)
它有什么作用?
我正在研究一种模拟波浪能转换器的工具,我需要将两个软件包相互耦合.一个程序用Fortran编写,另一个用C++编写.我需要在每个时间步骤将Fortran程序中的信息发送到C++程序.但是,在将数据发送到C++程序之前,首先需要在Python中处理数据.我收到了一条使用MPI在程序之间传输数据的提示.
我现在正在尝试从Fortran代码向Python发送一个简单的字符串,但Python代码卡在receive命令中.
我的Fortran代码如下所示:
USE GlobalVariables
USE MPI
IMPLICIT NONE
CHARACTER(LEN=10):: astring
INTEGER :: comm, rank, size, mpierr
! Initialize MPI on first timestep
IF(tstep .LT. 2) THEN
call MPI_INIT(mpierr)
ENDIF
! make string to send to python
astring = "TEST"
! MPI Test
call MPI_Comm_size(MPI_COMM_WORLD, size, mpierr)
call MPI_Comm_rank(MPI_COMM_WORLD, rank, mpierr)
! Send message to python
CALL MPI_SEND(astring, len(astring), MPI_CHARACTER, 0, 22, MPI_COMM_WORLD, mpierr)
print *, 'MPI MESSAGE SENT ', mpierr
! Initialize MPI on first timestep
IF(tstep .EQ. Nsteps-1) …Run Code Online (Sandbox Code Playgroud) 我正在编写将使用Fortran的C互操作性机制从Fortran调用C函数的代码(在Fortran 2003中引入并在较新版本的gfortran和ifort中实现).
这个答案几乎就是我所需要的,但是我不能完全理解我应该在Fortran中使用哪个接口声明来获得一个看起来像这样的C函数:
int use_array(int n, char * array[]){
int i;
for(i=0; i<n; i++){
printf("Item %d = %s\n",i,array[i]);
}
return n;
}
Run Code Online (Sandbox Code Playgroud)
我不清楚Fortran端的接口应该是什么声明:
interface
function use_array(n, x) bind(C)
use iso_c_binding
integer (c_int) use_array
integer (c_int), value :: n
character(c_char) WHAT_SHOULD_GO_HERE? :: x
end function use_array
end interface
Run Code Online (Sandbox Code Playgroud)
我知道我也必须处理空终止问题.