在Fortran中,REAL(KIND = real_normal)的C中的等价类型是什么? [英] What is the equivalent type in C for REAL(KIND=real_normal) in Fortran?

查看:585
本文介绍了在Fortran中,REAL(KIND = real_normal)的C中的等价类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组从Fortran传递到ac函数的问题:
在fortran数组定义为

I have a problem with passing of an array from Fortran to a c function: In the fortran the array is defined as

REAL(KIND=real_normal) , DIMENSION(:), ALLOCATABLE :: array
call cFunc(array)

如果将cFunc定义为

If define the cFunc as

void cFunc(double *data){...}

比数据只包含垃圾值。这种情况下的问题在哪里? (用整数工作这个解决方案很好)。

Than the data contains only "garbage" values. Where is the problem in this case? (with integers works this solution well).

thx。
编辑:
我的平台:
编译器:VS 2008,Intel编译器11版本
操作系统:Win7

thx. My platform: Compiler: VS 2008, Intel compiler 11 version OS: Win7


我这样定义c函数的接口(代码减少到一个元素,这使问题,真正的函数有更多的参数):

EDIT 2: I define the interface for the c-function like this (the code is reduced to one element, which makes problems, real function has more parameters):

interface c_interface

subroutine cFunc(array) bind (C, name = "cFunc")
use iso_c_binding
REAL(c_double), DIMENSION(*)::array
end subroutine cFunc

ALLOCATE (array(numberOfElements))
call cFunc(array)

此时,我得到一个运行时错误浮点溢出。在某些情况下,数组正确的元素。

At the moment i get an runtime error "Floating-point overflow". In some cases the array correct elements.

推荐答案

@High Performance Mark的建议非常好,我强烈推荐ISO- 2003(由许多Fortran编译器支持)Fortran和C之间的互操作性 - 这里有一个更大的问题,使ISO_C_Binding更有用:Fortran可分配的数组比普通数组更复杂。如果你hack it并直接传递一个指针到C代码,你可能传递一个指向Fortran内部结构描述可分配数组,而不是指向数值序列的指针。可分配的数组不是由ISO_C_Binding直接支持的,但是如果你写一个ISO_C_Binding接口(不像我最初写的),应该工作 - Fortran 95/2003解释的书说,编译器将识别被调用的例程isn' t接收一个可分配的数组,并将执行copy-in / copy-out以匹配数组。

@High Performance Mark's suggestions are very good, and I highly recommend the ISO_C_Binding of Fortran 2003 (supported by numerous Fortran compilers) for interoperability between Fortran and C -- there is a larger issue here that makes the ISO_C_Binding more useful: Fortran allocatable arrays are more complicated then ordinary arrays. If you "hack it" and directly pass a pointer to the C code, you are likely to pass a pointer to a Fortran internal structure describing the allocatable array rather than a pointer to the sequence of numeric values. Allocatable arrays aren't directly supported by the ISO_C_Binding, but should work if you write an ISO_C_Binding interface (unlike what I wrote originally) -- the book "Fortran 95/2003 explained" says that the compiler will recognize the the called routine isn't receiving an allocatable array and will perform copy-in/copy-out to match the arrays.

PS我的猜想是copy-in / copy-out不需要一个简单的可分配的实际参数。当编译器通过显式接口(可以是ISO_C_Binding接口)识别被调用例程的哑变量不是可分配时,编译器应该能够从可分配的描述中提取指向实际数组的指针,通过它作为参数。在一些情况下将需要复制输入/复制输出,例如指向非连续数组的指针,例如具有非单位跨距的指针(例如,指向元素1,3,5,...的指针) 。)。但没有任何接口,编译器可能会传递可分配数组的描述符,这将不是C期望的....

P.S. My guess is that copy-in/copy-out shouldn't be necessary for a simple allocatable actual argument. When the compiler recognizes via an explicit interface (which could be an ISO_C_Binding interface) that the dummy argument of the called routine is not an allocatable, the compiler should just be able to extract the pointer to the actual array from the description of the allocatable and pass that as the argument. Copy-in/copy out will be required in some cases, such as a pointer to a non-contiguous array, such as pointer with a non-unit stride (e.g., a pointer that points to elements 1, 3, 5, ...). But without any interface, the compiler will likely pass the descriptor of the allocatable array, which won't be what C is expecting....

这篇关于在Fortran中,REAL(KIND = real_normal)的C中的等价类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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