Internal memory representation of Fortran allocatable

荒凉一梦 提交于 2019-12-10 21:05:53

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/42442512/internal-memory-representation-of-fortran-allocatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!