确定内存中的Fortran派生类型大小 [英] Determine Fortran derived type size in memory

查看:292
本文介绍了确定内存中的Fortran派生类型大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Fortran内部函数 transfer 可用于将派生类型转换为实数或整数数组。当在依赖原始类型(整数,实数等)的数组进行持久化的遗留系统中工作时,这可能非常有用。



下面的代码至少运行在 ifort gfortran 并将一个简单派生类型示例转换为一个整型数组(使用解决方案更新):

  program main 
隐式无

整数,参数:: int_mem_size = storage_size(1)

类型子类型
整数a
双精度b
结束类型子类型

类型:: mytype
整数:: foo
双精度:: bar
类型(子类型):: some_type
end类型

类型(mytype):: my_var
类型(子类型):: my_subtype

!旧版本:integer :: x(30)
整数,allocatable :: x(:)
整数:: mem_size

!分配所需大小的数组
mem_size = storage_size(my_var)
allocate(x(mem_size / int_mem_size))

my_subtype%a = 1
my_subtype%b = 2.7

my_var% foo = 42
my_var%bar = 3.14
my_var%some_type = my_subtype

write(*,*)transfering ...
x = transfer(my_var, x)
write(*,*)Integer transformation:,x

end program main

在我的电脑上,这是输出(这个结果至少取决于平台):

 转移... 
整数转换:42 0 1610612736 1074339512
999 0 -1610612736 1074108825

我的问题是我猜到一个30个元素的长整型数组足够大,可以存储这个数据结构。有没有一种方法可以确定数组需要存储整个数据结构的大小? 解决方案

如果您有符合Fortran 2008的编译器或足够兼容的编译器,您会发现内部函数 storage_size ,它返回用于存储其参数的位数。如果大多数我熟悉的编译器实现一个非标准函数来做到这一点,英特尔Fortran编译器有一个名为 sizeof 的函数,它返回存储其参数所需的字节数。


The Fortran intrinsic function transfer can be used to covert a derived type into a real or integer array. This is potentially very useful when working in legacy systems which relies on arrays of primitive types (integer, real etc.) for persistence.

The code below runs at least on ifort and gfortran and converts a simple derived type example to an integer array (updated with solution):

program main
    implicit none

    integer, parameter :: int_mem_size = storage_size(1)

    type subtype
       integer a
       double precision b
    end type subtype

    type :: mytype
         integer :: foo
         double precision :: bar
         type(subtype) :: some_type
    end type

    type(mytype)  :: my_var
    type(subtype) :: my_subtype

    ! Old version: integer :: x(30)
    integer, allocatable :: x(:)
    integer :: mem_size

    !Allocate array with required size
    mem_size = storage_size(my_var)
    allocate(x(mem_size/int_mem_size))

    my_subtype%a = 1
    my_subtype%b = 2.7

    my_var%foo = 42
    my_var%bar = 3.14
    my_var%some_type = my_subtype

    write(*,*) "transfering..."
    x = transfer(my_var, x)
    write(*,*) "Integer transformation:", x

end program main

On my PC, this is the output (this result is at least platform dependent):

 transfering...
 Integer transformation:                    42           0  1610612736  1074339512         
 999           0    -1610612736  1074108825

My problem is that I have "guessed" that a 30 element long integer array is large enough to store this data structure. Is there a way I can determine how large the array needs to be to store the whole data structure?

解决方案

If you have a Fortran 2008 compliant compiler, or one that is compliant enough, you will find the intrinsic function storage_size which returns the number of bits used to store its argument. Failing that most compilers that I am familiar with implement a non-standard function to do this; the Intel Fortran compiler has a function called sizeof which returns the number of bytes required to store its argument.

这篇关于确定内存中的Fortran派生类型大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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