Fortran 可分配的内部存储器表示

Reg*_*lez 5 fortran allocatable-array

我想知道 fortran 可分配数组的内部存储器表示是什么。

我理解这比原始指针更复杂一点,因为形状和等级也必须存储。

我还猜测它依赖于实现,因为我在Fortran 2003 标准中找不到信息

但是,我想知道使用哪种结构来表示可分配数组(即使只针对一个编译器)。

我知道这个问题有点宽泛,但我们将不胜感激。

Vla*_*r F 5

可分配数组、指针数组以及假定形状数组参数都使用数组描述符(也称为 dope 向量)进行处理。

\n

任何编译器都可以有自己的数组描述符结构。它可以在编译器手册中找到。但是描述符有一个标准化格式,用于与 C 进行通信(以及 Fortran 之外可能与 C 进行通信的其他软件)。

\n

这个标准描述符可能不会被编译器内部使用,但它可以。如果它也在内部使用,那么编译器在调用 C 互操作过程时不必准备新的描述符。例如,gfortran 计划“最好作为本机格式”支持标准描述符。

\n

Intel 在https://software.intel.com/en-us/node/678452中描述了与 C 互操作描述符不同的本机数组描述符示例。

\n

C 互操作数组参数的数组描述符的结构由关于 Fortran 与 C 的进一步互操作性的技术规范 ISO/IEC TS 29113:2012定义,该技术规范将成为 Fortran 2015 的一部分。

\n

在 C 头文件中ISO_Fortran_binding.h定义了一个 C 结构,它是用 Fortran 描述符(假定的形状、指针或可分配的)定义的。

\n

它看起来如下(来自IBM 网站,某些细节可能是编译器特定的):

\n
CFI_cdesc_t \n    A type definition that describes a C descriptor. It contains the following structure members:\n\nvoid *base_addr\n    The base address of the data object that is described. For deallocated allocatable objects, base_addr is NULL. \nsize_t elem_len\n\n        For scalars: The size in bytes of the data object that is described.\n        For arrays: The size in bytes of one element of the array.\n\nint version\n    The version number of the C descriptor. Currently, the only valid value is available by using the CFI_VERSION macro.\nCFI_attribute_t attribute\n    The attribute code of the C descriptor. For the valid values for attribute, see Table 1.\nCFI_type_t type\n    The type code of the C descriptor. Describes the type of the object that is described by the C descriptor. For the valid values for type, see Table 2. \n\nCFI_rank_t rank\n    The rank of the object that is described by the C descriptor. Its value must be in the range 0 \xe2\x89\xa4 rank \xe2\x89\xa4 CFI_MAX_RANK. A value of 0 indicates that the object is a scalar. Otherwise, the object is an array.\nCFI_dim_t dim[]\n    An array of size rank that describes the lower bound, extent, and stride of each dimension.\n\nThere is a reserved area between rank and dim. The size of the reserved area is 12 words in 32-bit mode and 9 words in 64-bit mode.\n
Run Code Online (Sandbox Code Playgroud)\n

引用的CFI_类型也在ISO_Fortran_binding.h标头中定义。

\n

因此,尽管此描述符可能与编译器内部使用的不完全相同,但它是一个很好的示例,说明 Fortran 数组描述符中应该包含哪种类型的数据组件。

\n

但是,请注意,gfortran(一种非常常见的编译器)尚未使用这种类型的描述符。只有带有新描述符的实验版本,当前描述符在手册中进行了描述。Fortran 与 C 的进一步互操作性中也提到了该状态

\n

  • 弗拉基米尔的回答是不正确的。CFI 描述符仅与从 Fortran 传递到可互操作 (BIND(C)) 例程的某些参数相关,这不一定反映 Fortran 编译器的内部实现。此外,该规范使“C 描述符”的某些方面依赖于实现。您唯一可以依赖的是,如果 C 例程接收其中之一,则布局由 ISO_Fortran_binding.h 中的 CFI_CDESC_T 指定 - 这可能因实现而异。尽管如此,如果您正在与 C 通信,则可以这样做它。 (4认同)