从C递延形状的数组调用Fortran [英] Calling Fortran from C with deferred shape array

查看:177
本文介绍了从C递延形状的数组调用Fortran的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能从调用C / C ++,这里的Fortran语言参数之一是延迟形数组,Fortran子例程? (希望我使用术语延迟形正确。)

在下面的例子中, subr1()使用显式形状,做工精细,但 subr2()使用延迟形状,并导致段错误。 <一href=\"http://stackoverflow.com/questions/13058743/how-to-pass-allocatable-arrays-to-subroutines-in-fortran?rq=1\">This问题表示一个需要明确的接口来调用 subr2()从另一个Fortran的子程序,但我想从C调用难道仅仅是不可能的做这样?

在实际问题的数组的长度会比较复杂 - 这就是为什么,在一个理想的世界,我想用递延型版本。 (当然,在一个理想的世界,我也不会混合Fortran和C在所有。)

test_array_c.c

 的#include&LT;&malloc.h所GT;EXTERN无效subr1_为(int *,诠释*);
EXTERN无效subr2_为(int *,诠释*);INT主(INT ARGC,字符** argv的){  INT N,I;
  为int *的数据;  //创建一个数组
  N = 3;
  数据=(INT *)malloc的(N * sizeof的(INT));
  对于(i = 0; I&LT; N;我++)数据[我] =我;  //通数组FORTRAN功能
  subr1 _(&放大器; N,数据);
  subr2 _(&放大器; N,数据);  // 自由
  免费的(数据);
}

test_array_f90.f90

 子程序subr1(N,数据)
  隐无  整数,意图(中)::ñ
  整数,意图(中)::数据(N)
  整数::我  做I = 1,N
    打印*,数据(i)
  ENDDO结束子程序子程序subr2(N,数据)
  隐无  整数,意图(中)::ñ
  整数,意图(中)::数据(:)
  整数::我  做I = 1,N
    打印*,数据(i)
  ENDDO结束子程序

命令行编译/运行

 用户@主机:〜$ gcc的-g -O0 -c test_array_c.c
用户@主持人:〜$ gfortran -g -O0 -c test_array_f90.f90
用户@主持人:〜$ gcc的-o test_array test_array_c.o test_array_f90.o -lgfortran
用户@主持人:〜$ ./test_array
           0
           1
           2
分段错误(核心转储)


解决方案

术语是假定形阵列。作为编译器实现的一个实际问题,其可能为某种结构的不会是一致C.这解释了分段错误传递。

在这个时代,我建议通过ISO C绑定混合Fortran和C。但是,这没有任何帮助,因为假定形状数组由ISO C绑定不支持,或者至少不是Fortran 2003的版本。

Is it possible to call a Fortran subroutine from C/C++, where one of the Fortran arguments is a deferred-shape array? (Hopefully I'm using the term "deferred-shape" correctly.)

In the example below, subr1() uses explicit-shape, and works fine, but subr2() uses deferred-shape and causes a segfault. This question indicates that one needs an explicit interface to call subr2() from another Fortran subroutine, but I'm trying to call from C. Is it just not possible to do it this way?

In the real problem the length of the array would be more complicated -- which is why, in an ideal world, I would like to use the deferred-shape version. (Of course in an ideal world, I wouldn't be mixing Fortran and C at all.)

test_array_c.c

#include <malloc.h>

extern void subr1_(int*, int*);
extern void subr2_(int*, int*);

int main(int argc, char **argv){

  int N,i;
  int *data;

  // create an array
  N=3;
  data=(int*)malloc(N*sizeof(int));
  for (i=0;i<N;i++) data[i]=i;

  // pass array to fortran functions
  subr1_(&N,data);
  subr2_(&N,data);

  // free
  free(data);
}

test_array_f90.f90

subroutine subr1(n,data)
  implicit none

  integer,intent(in) :: n
  integer,intent(in) :: data(n)
  integer :: i

  do i=1,n
    print*, data(i)
  enddo

end subroutine

subroutine subr2(n,data)
  implicit none

  integer,intent(in) :: n
  integer,intent(in) :: data(:)
  integer :: i

  do i=1,n
    print*, data(i)
  enddo

end subroutine

command line build/run

user@host:~$ gcc -g -O0 -c test_array_c.c
user@host:~$ gfortran -g -O0 -c test_array_f90.f90
user@host:~$ gcc -o test_array test_array_c.o test_array_f90.o -lgfortran
user@host:~$ ./test_array
           0
           1
           2
Segmentation fault (core dumped)

解决方案

The term is "assumed-shape" array. As a practical matter of compiler implementation, its likely to be passed as some sort of structure that won't be consistent C. Which explains the segmentation fault.

In this era, I recommend mixing Fortran and C via the ISO C Binding. But that won't help here because assumed-shape arrays aren't supported by the ISO C Binding, or at least not by the Fortran 2003 version.

这篇关于从C递延形状的数组调用Fortran的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆