Fortran可分配的内部内存表示 [英] Internal memory representation of Fortran allocatable

查看:303
本文介绍了Fortran可分配的内部内存表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我明白这比原始指针复杂一点,因为形状和行列也必须存储。

我也猜测它是依赖于实现的,因为我没有找到 Fortran 2003标准。不过,我想知道用什么样的结构来表示可分配数组(即使只有一个编译器)。

我知道这个问题有点宽泛,但任何帮助都将不胜感激。

解决方案

可分配数组,指针数组,也假定​​形状数组参数使用数组描述符处理 (也称为dope向量)。

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

这个标准描述符可能编译器不能在内部使用,但它可以是,如果它也在内部使用,那么编译器在调用C-interoperable过程时不需要准备新的描述符,例如,gfortran计划支持标准描述符 最好是原生格式



本地数组描述符的一个示例,与C可互操作的描述符不同,由Intel在 https://software.intel.com/zh-CN/node/678452

结构C-interoperable数组参数的数组描述符由技术规范I定义SO / IEC TS 29113:2012关于Fortran与C 的进一步互操作性,它将成为Fortran 2015的一部分。在b头文件 ISO_Fortran_binding.h 定义了一个用Fortran描述符(假定形状,指针或可分配)定义的C结构。



它看起来如下(从 IBM网站,某些细节可能与编译器有关):

  CFI_cdesc_t 
描述C描述符的类型定义。它包含以下结构成员:

void * base_addr
描述的数据对象的基地址。对于已分配的可分配对象,base_addr为NULL。
size_t elem_len

对于标量:描述的数据对象的大小(以字节为单位)。
对于数组:数组中一个元素的字节大小。

int版本
C描述符的版本号。目前,通过使用CFI_VERSION宏,唯一有效的值是可用的。
CFI_attribute_t属性
C描述符的属性代码。有关属性的有效值,请参阅表1.
CFI_type_t类型
C描述符的类型代码。描述由C描述符描述的对象的类型。有关类型的有效值,请参见表2.

CFI_rank_t rank
由C描述符描述的对象的等级。其值必须在0≤rank≤CFI_MAX_RANK的范围内。值为0表示该对象是标量。否则,该对象是一个数组。
CFI_dim_t dim []
描述每个维度的下限,范围和步幅的大小排名数组。

排名和昏暗之间有一个保留区域。保留区域的大小在32位模式下为12个字,在64位模式下为9个字。

引用的 CFI _ 类型也在 ISO_Fortran_binding.h 标题。

即使这个描述符可能不完全相同您的编译器在内部使用,这是一个很好的例子,您可以在Fortran数组描述符中预期什么样的数据组件。 然而,要小心gfortran是一个非常常见的编译器,它尚未使用这种类型的描述符。新手描述符只有一个实验版本,手册中描述了当前描述符。

I'd like to know what is the internal memory representation of a fortran allocatable array.

I understand this a bit more complex than a raw pointer, since shape and ranks must be stored as well.

I also guess it's implementation dependent, since I don't find the info in the Fortran 2003 standard.

However, I'd like to know what kind of structures are used to represent allocable arrays (even for just one compiler).

I know the question is a bit wide, but any help would be greatly appreciated.

解决方案

Allocatable arrays, pointer arrays, but also assumed shape array arguments are treated using an array descriptor (also called dope vector).

Any compiler can have its own structure of the array descriptor. It can be found in the compiler's manual. But there is a standardized format for the descriptor, which is used for communication with C (and potentially other software outside of Fortran that can communicate with C.

This standard descriptor may not be used by the compiler internally, but it can be. If it is used also internally, then the compiler does not have to prepare a new descriptor when calling a C-interoperable procedure. For example, gfortran plans that the standard descriptor is supported "preferably as native format".

An example of a native array descriptor, different from the C-interoperable one, is described by Intel at https://software.intel.com/en-us/node/678452.

The structure of the array descriptor for C-interoperable array arguments is defined by the Technical Specification ISO/IEC TS 29113:2012 on further interoperability of Fortran with C, which is to become a part of Fortran 2015.

In the C header file ISO_Fortran_binding.h is defined a C structure which is defined with a Fortran descriptor (assumed shape, pointer or allocatable).

It looks as follows (from the IBM website, certain details may be compiler specific):

CFI_cdesc_t 
    A type definition that describes a C descriptor. It contains the following structure members:

void *base_addr
    The base address of the data object that is described. For deallocated allocatable objects, base_addr is NULL. 
size_t elem_len

        For scalars: The size in bytes of the data object that is described.
        For arrays: The size in bytes of one element of the array.

int version
    The version number of the C descriptor. Currently, the only valid value is available by using the CFI_VERSION macro.
CFI_attribute_t attribute
    The attribute code of the C descriptor. For the valid values for attribute, see Table 1.
CFI_type_t type
    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. 

CFI_rank_t rank
    The rank of the object that is described by the C descriptor. Its value must be in the range 0 ≤ rank ≤ CFI_MAX_RANK. A value of 0 indicates that the object is a scalar. Otherwise, the object is an array.
CFI_dim_t dim[]
    An array of size rank that describes the lower bound, extent, and stride of each dimension.

There 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.

The referenced CFI_ types are also defined in the ISO_Fortran_binding.h header.

So, even-though this descriptor may not be exactly the same that your compiler is using internally, it is a good example of what kind of data components one should expect in a Fortran array descriptor.

However, be careful that gfortran, a very common compiler, does not yet use this type of descriptor. There is only an experimental version with the new descriptor and the current descriptor is described in the manual.

这篇关于Fortran可分配的内部内存表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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