简体   繁体   English

c-fortran互操作性-带有指针的派生类型

[英]c-fortran interoperability - derived types with pointers

I have long fortran code that has to be made usable from python. 我有很长的fortran代码,必须从python使其可用。 I decided to do Fortran->C->Python interface. 我决定做Fortran-> C-> Python接口。

I got a problem: I have derived types within Fortran modules that contain 我遇到一个问题:我在包含以下内容的Fortran模块中派生了类型

double precision, allocatable  

type as members. 输入成员。

When trying to compile with ifort I get (with gfortran something similar): 当尝试使用ifort编译时,我得到了(使用gfortran进行了类似的处理):

Each component of a derived type with the BIND attribute shall be a nonpointer,         
nonallocatable data component with interoperable type and type parameters

This is actually with agreement with Intel compiler documentation and Fortran 2003 standard: point 15.2.5. 这实际上与Intel编译器文档和Fortran 2003标准一致:第15.2.5点。

Is there any way to access Fortran "type" with allocatable or pointer inside? 有什么方法可以访问内部带有可分配或指针的Fortran“类型”?

Not really. 并不是的。 Even the brand new TS 29113 does not offer a solution. 即使是全新的TS 29113也无法提供解决方案。 It has the TYPE(*) but you have to know, how to decode the structure in C. You can do it by reverse engineering. 它具有TYPE(*)但是您必须知道如何在C中解码结构。您可以通过逆向工程来实现。

There is actually a macro for the size of the structure in the referenced TS. 实际上,在引用的TS中有一个用于结构大小的宏。 See CFI_attribute_allocatable . 请参阅CFI_attribute_allocatable But the problem with dereferencing remains. 但是取消引用的问题仍然存在。 There is a standard array descriptor proposed, but this TS is not yet supported by some compilers (notably by gfortran). 提出了一个标准的数组描述符,但是某些编译器(尤其是gfortran)尚不支持此TS。

What is possible, is to just pass the pointer to the structure to a generic function such as qsort . 可能的是,仅将结构的指针传递给通用函数(例如qsort Then you use just a piece of memory, that happens to contain also some pointer or allocatable descriptor, but C does not have to know about them. 然后,您仅使用一块内存,恰好还包含一些指针或可分配的描述符,但是C不必了解它们。

Another possibility is to construct a derived type which holds only type(c_ptr) , instead of Fortran pointers and use c_loc() to fill them. 另一种可能性是构造一个仅保留type(c_ptr)而不是Fortran指针的派生类型,并使用c_loc()填充它们。 Be sure to have only contiguous arrays then. 然后确保只有连续的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM