将FORTRAN DEC UNION/MAP扩展转换为其他任何文件 [英] Convert FORTRAN DEC UNION/MAP extensions to anything else

查看:157
本文介绍了将FORTRAN DEC UNION/MAP扩展转换为其他任何文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gfortran 6现在支持以下扩展:)

我有一些旧的f77代码,广泛使用UNION和MAP.我需要使用不支持这些扩展的gfortran进行编译.我已经弄清楚了如何转换除这些之外的所有不受支持的扩展,我很茫然.我对可能的方法有一些想法,但是还没有成功实现任何东西.我需要以与当前相同的方式访问现有的UDT.我可以重新实现UDT,但是它们的接口一定不能更改.

I have some old f77 code that extensively uses UNIONs and MAPs. I need to compile this using gfortran, which does not support these extensions. I have figured out how to convert all non-supported extensions except for these and I am at a loss. I have had several thoughts on possible approaches, but haven't been able to successfully implement anything. I need for the existing UDTs to be accessed in the same way that they currently are; I can reimplement the UDTs but their interfaces must not change.

我所拥有的例子:

TYPE TEST
  UNION
    MAP
      INTEGER*4 test1
      INTEGER*4 test2
    END MAP
    MAP
      INTEGER*8 test3
    END MAP
  END UNION
END TYPE

必须通过以下方式访问元素:TEST%test1,TEST%test2,TEST%test3

Access to the elements has to be available in the following manners: TEST%test1, TEST%test2, TEST%test3

到目前为止我的想法:

  1. 以某种方式用fortran等效代替.
  2. 在C/C ++中定义结构,并以某种方式使它们对FORTRAN代码可见(怀疑是否可行)

我认为,当将UNION和MAP排除在标准之外时,必须将f77重构为f90/95.如果/完全解决了该怎么办?

I imagine that there must have been lots of refactoring of f77 to f90/95 when the UNION and MAP were excluded from the standard. How if at all was/is this handled?

可接受的答案有一种解决方法,可以允许内存重叠,但是就保留API而言,这是不可能的.

The accepted answer has a workaround to allow memory overlap, but as far as preserving the API, it is not possible.

推荐答案

UNION和MAP从来都不是任何FORTRAN标准的一部分,它们是供应商的扩展. (例如,请参见 http://fortranwiki.org/fortran/show/Modernizing+Old+ Fortran ).因此,它们并没有真正从Fortran 90/95标准中排除.它们导致变量在内存中重叠.如果代码实际上使用了此功能,那么您将需要使用equivalence.在不同类型的变量之间不进行转换而移动数据的首选方法是transfer内在函数,但是对您来说,您必须标识出需要转换的每个位置,而对于equivalence则隐式地进行转换.当然,这会使代码难以理解.如果内存覆盖只是为了节省空间,并且不使用变量的等效项,那么您可以摆脱此功能".如果代码像您的示例一样,带有小的整数,那么我猜想正在使用内存覆盖.如果覆盖物是大阵列,则可能是为了节省内存.如果这些声明还创建了新类型,则可以使用用户定义的类型,这些类型肯定是Fortran> = 90的一部分.

UNION and MAP were never part of any FORTRAN standard, they are vendor extensions. (See, e.g., http://fortranwiki.org/fortran/show/Modernizing+Old+Fortran). So they weren't really excluded from the Fortran 90/95 standard. They cause variables to overlap in memory. If the code actually uses this feature, then you will need to use equivalence. The preferred way to move data between variables of different types without conversion is the transfer intrinsic, but to you that you would have to identify every place where a conversion is necessary, while with equivalence it is taking place implicitly. Of course, that makes the code less understandable. If the memory overlays are just to save space and the equivalence of the variables is not used, then you could get rid of this "feature". If the code is like your example, with small integers, then I'd guess that the memory overlay is being used. If the overlays are large arrays, it might have been done to conserve memory. If these declarations were also creating new types, you could use user defined types, which are definitely part of Fortran >=90.

如果代码使用不同类型的变量的内存等效性,则这可能不是可移植的,例如,整数和实数的内部表示可能在此代码最初运行的计算机和当前计算机之间不同.也许变量只是用来存储位.有很多事情要解决.

If the code is using memory equivalence of variables of different types, this might not be portable, e.g., the internal representation of integers and reals are probably different between the machine on which this code originally ran and the current machine. Or perhaps the variables are just being used to store bits. There is a lot to figure out.

P.S.为了回应评论中的问题,这是一个代码示例.但是……要明确……我不认为使用等效性是好的编码实践.通过我通常与gfortran一起使用的编译器选项来调试代码,gfortran拒绝了该代码.使用更宽松的选项,gfortran将对其进行编译. ifort也会如此.

P.S. In response to the question in the comment, here is a code sample. But .... to be clear ... I do not think that using equivalence is good coding pratice. With the compiler options that I normally use with gfortran to debug code, gfortran rejects this code. With looser options, gfortran will compile it. So will ifort.

module my_types

use ISO_FORTRAN_ENV

type test_p1_type
    sequence
    integer (int32) :: int1
    integer (int32) :: int2
end type test_p1_type

type test_p2_type
   sequence
   integer (int64) :: int3
end type test_p2_type

end module my_types


program test

use my_types

type (test_p1_type) :: test_p1
type (test_p2_type) :: test_p2

equivalence (test_p1, test_p2)

test_p1 % int1 = 2
test_p1 % int1 = 4

write (*, *) test_p1 % int1, test_p1 % int2, test_p2 % int3

end program test

这篇关于将FORTRAN DEC UNION/MAP扩展转换为其他任何文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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