ifort和gfortran在定义我自己的TYPE时的不同结果 [英] Different results from ifort and gfortran when defining my own TYPE

查看:241
本文介绍了ifort和gfortran在定义我自己的TYPE时的不同结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Fortran的新手,但一般情况下我发现,一旦我掌握了模块和类型,就可以用C或Matlab完成大部分工作。然而,根据我是否使用gfortran(gcc版本4.6.2)或ifort(13.0.2),我被这个结果的差异所困扰。 Gfortran给了我期望的结果,但是ifort给了我3个空行!任何想法为什么?

 模块define_structures 
隐式无
私有

公共modelling_params

类型modelling_params
实数,维(:),allocatable :: freqs
实数,维(:),allocatable ::偏移
复数,维数(: ),allocatable :: data
结束类型modelling_params
结束模块define_structures

程序主体

使用define_structures
隐式无

type(modelling_params):: S

S%data = [(1,1),(2,3),(3,1)]
S%freqs = [1,3,7]
S%抵消= [100,200,300]
print *,S%data
print *,S%freqs
print *,S %抵消


结束计划主要

以下是用gfortran编制

 (1.0000000,1.0000000)(2.0000000,3.0000000)(3.0000000,1.0000000)
1.0000000 3.0000 000 7.0000000
100.00000 200.00000 300.00000

使用ifort,我只得到3个空行,尽管它编译得很好!!



在此先感谢。 解决方案

-assume realloc_lhs 命令行选项被传递给编译器时启用了 ifort 中赋值时可分配变量的重新分配。如果您在第一次分配后立即插入以下内容:

  print *,分配(S%数据)

code>

您会看到 F ,这意味着可分配字段未分配当分配给。代码按照预期的方式工作,使用 -assume realloc_lhs


I'm new to Fortran, but am generally finding that I can do most things that I could with C or Matlab, once I get my head around modules and types. However, I'm stumped by this difference in results, depending on whether I use gfortran (gcc version 4.6.2) or ifort(13.0.2). Gfortran gives me the results I expect, but ifort gives me 3 blank lines! Any ideas why?

module define_structures
implicit none
private

public modelling_params

    type modelling_params
        real, dimension(:), allocatable :: freqs
        real, dimension(:), allocatable :: offsets      
        complex, dimension(:), allocatable :: data
    end type modelling_params   
end module define_structures

program main

use define_structures
    implicit none

    type (modelling_params) :: S

    S%data = [(1,1) ,(2,3), (3,1)]
    S%freqs = [1, 3, 7]
    S%offsets = [100, 200, 300]
    print *,S%data
    print *,S%freqs
    print *,S%offsets


end program main

Here's the output from compiling with gfortran

(  1.0000000    ,  1.0000000    ) (  2.0000000    ,  3.0000000    ) (  3.0000000    ,  1.0000000    )
1.0000000       3.0000000       7.0000000    
100.00000       200.00000       300.00000   

And with ifort, I just get 3 blank lines, though it compiles fine!!

Thanks in advance.

解决方案

Support for reallocation of allocatable variables on assignment in ifort is enabled when the -assume realloc_lhs command line option is passed to the compiler. If you insert the following immediately after the first assignment:

print *, allocated(S%data)

you would see F, which means that the allocatable field is not allocated when assigned to. The code works as expected with -assume realloc_lhs.

这篇关于ifort和gfortran在定义我自己的TYPE时的不同结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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