【发布时间】:2012-06-20 20:39:52
【问题描述】:
我对 fortran 很陌生。而且我使用的是 fortran 内置的库,该库有许多 TYPE 数组。 我尝试使用以下方法通过 c 程序将值分配给 lib 中的 TYPE 数组。 我已经构建了一个 c-fortran 接口,我从 sqlite 数据库获取值到 c prg 中的 ac 结构数组中。然后将此结构数组传递给一个 fortran 子例程,在其中我将其声明为派生类型,匹配声明的 TYPE 变量的定义在 lib 中。然后我将传递的数组中的值复制到 lib 中声明的实际 TYPE 数组中,并将其传递给 fortran 函数。
发生的事情是数组中的值从 c 传递到 fortran 子例程,我打印它们以在 fortran 子例程中检查它们,但是当数组从子例程传递到函数时,值变得乱码。我将数组作为假定的形状数组传递。该函数在模块内声明,因此我认为调用子例程不需要接口。
我不完全理解发生了什么,我也尝试使用 TYPE 声明中的序列。 我正在使用 g95 , gcc 4.0.3 编译器。 数组中的所有值都是 REAL(KIND =8) 类型,c 程序中的等价物是 double 。
考虑一个声明了 TYPE(something), TYPE(Something2) 的库。我将 lib 作为模块导入到 fortran 子例程中。
假设
TYPE(something_lib) is
REAL(kind =8) ::A
REAL(kind=8) ::B
END TYPE
在库中
TYPE(SOMETHING2_lib) !this is also declare in the lib
!I have a C program in which
! in which
/////////////////////////////////////// ///////////////////////////////////////////////p>
// C program
struct SomethingC
{
double a
double b
} ;
struct SomethingC x[2]
struct something2C s[2] // something similar to the first struct
//i fill the values in x ,s from database in proper format.(doubles).
//i call the fortran subroutine in the c program
A_(x,s); //call to fortran subroutine
/////////////////////////////////////// //////////////////////////////////// // fortan 子程序
SUBROUTINE A (x,s)
USE Lib_module ! this LIB_Module also contains the function func
TYPE G
REAL(kind =8) ! this is defined similar to TYPE something(in lib) by me
REAL(kind =8)
END TYPE G
TYPE G2
similar to TYPE Something2 in lib
END TYPE G2
TYPE(something_lib) :: D(2) !derived type declared in lib
TYPE(Something2_lib)::E(2) ! derived type declared in lib
TYPE(G)::x(2)
TYPE(G2)::s(2)
! x, s are struct arrays from c which are now declared in the fortran function
copy code for
copying values from
x to D
s to E
print all values of
D
Print all values of
E
!this prints the values correct as expected for both x,d
func(D,E) ! this function is defined in the lib . The function is in the
! LIB_module
! so no interface will be required (i think)
! IN the Function
FUNCTION func(D,E) (while debugging)
TYPE(something_lib) :: INTENT (IN) D(:)
TYPE (something2_lib)::INTENT (IN) E(:)
when i try to print values of D , E in the
function i get garbled values like
1180333333
2.33419537006E-313
!when out of the function and back in the subroutine i.e after the call(while debugging)
! if I print the values of D,E here they print ok
END SUBROUTINE
因此它们在传入函数后会出现乱码,但在 子程序。 我的问题是为什么会这样? 我该如何解决?
【问题讨论】:
-
我们并不完全了解您在做什么。在你的代码中很难看出问题。
-
在这一点上进行的一种有效方法是给我们代码,包含相关部分的代码的截断版本,或者代码的骨架/示意图版本。当然,还有执行时产生的任何错误。
-
@High Performance 我将编辑我的问题以提供一个框架。
-
@codefor: yukkkk,请正确格式化您的代码。
-
@codefor: yukkkk,请正确格式化您的代码。
标签: arrays fortran derived-types