Fortran中模块的正确使用 [英] Proper use of modules in Fortran

查看:39
本文介绍了Fortran中模块的正确使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用 FORTRAN,但我从未获得过以正确方式编写源代码的正式指导.我目前使用模块来存储全局变量,但我知道您也可以使用它们来存储子例程和函数.我使用的代码有很多子程序,因为它们非常庞大和复杂.所有的函数和子程序都应该在模块中吗?如果是,为什么?

I work with FORTRAN a lot, but I never had formal instruction in the proper way to write source code. I currently use modules to store global variables, but I understand you could also use them to store subroutines and functions. The codes I work with have many subroutines, as they are very large and complex. Should all functions and subroutines be in modules? If so, why?

推荐答案

一般来说,您第一个问题的答案是,我稍后会回答您的第二个问题.首先请注意,这是对一般性问题的一般性回答,围绕 SO Fortran 问题的火花很可能会出现模块不适用的特殊情况.我提前反驳说,这个答案是针对模块的新手.一旦您不再是新手,您就可以为自己的问题制定自己的答案.

In general the answer to your first question is Yes and I'll come to an answer to your second question in a moment. Note first that this is a general answer to a general question and the bright sparks who hang around SO Fortran questions may well come up with special circumstances in which modules are inapplicable. I retort, in advance, that this answer is aimed at a newcomer to modules. Once you are no longer a newcomer you can formulate your own answer to your questions.

模块对于程序员来说是最有用的,它可以帮助组织和构建一个程序或一组程序.它们提供了一种用于封装用户定义类型和对这些类型进行操作的函数/子例程的定义的机制.在 Fortran 90 和 95 中,这种封装在某种程度上是临时的,因为它依赖于程序员关于如何将程序分解为多个部分的想法.随着 Fortran 2003 中面向对象工具的引入,现在有更清晰的规则"来识别每个模块中的元素.

Modules are most useful to the programmer as an aid to organising and structuring a program or suite of programs. They provide a mechanism for encapsulating the definitions of user-defined types and functions/subroutines which operate on those types. In Fortran 90 and 95 this encapsulation was somewhat ad-hoc in the sense that it relied on the programmer's ideas about how to decompose a program into parts. With the introduction of the object-oriented facilities in Fortran 2003 there are now even clearer 'rules' for identifying what elements belong in each module.

例如,您可以为有理算术的类型和过程设想一个模块.通过将所有实现你的伟大想法的代码保存在一个模块中,你可以对程序的其他部分(不需要知道细节)隐藏实现,只公开那些你想公开的部分(查看 PRIVATEPUBLIC 关键字).您可以立即看到将代码组织成模块的另一个优势;在一个新程序中USE你的有理算术模块比将代码从你的大型源文件剪切并粘贴到另一个大型源文件中要容易得多.当您想处理有理算术时,您可以在一个模块中处理代码,而不是散布在文件中的代码.

You could, for example, conceive of a module for types and procedures for rational arithmetic. By keeping all the code which implements your great ideas in one module you can hide the implementation from other parts of your program (which do not need to know the details) and expose only those parts you wish to expose (look at the PRIVATE and PUBLIC keywords). You can, right away, see another advantage to organising your code into modules; it's much easier to USE your rational arithmetic module in a new program than it is to cut and past the code from your mega-source file into another mega-source file. When you want to work on your rational arithmetic, you work on code in one module, not in code spread all around your files.

模块还允许您管理名称冲突.例如,您的有理算术模块可能定义了一个名为 add 的运算,您也可能有一个多精度整数算术模块,它定义了一个名为 add 的运算.如果您尝试在程序(或另一个模块)中USE这两个模块,那么编译器将警告(可能引发错误)在使用这些模块的范围内定义了两次相同的名称.当您使用关联模块实体时,您可以使用重命名.您还可以使用 ONLY 子句仅导入用户需要的那些模块实体.

Modules also allow you to manage name clashes. For example, your rational arithmetic module might define an operation called add, and you might also have a multiple-precision integer arithmetic module which defines an operation called add. If you attempt to USE both these modules in a program (or another module) then compiler will warn (possibly raise an error) that the same name is defined twice within the scope that uses the modules. You can use renaming when you use associate module entities. You can also use an ONLY clause to import only those module entities that the user needs.

请注意,模块 USE 是可传递的,如果 A 使用 B 而 B 使用 C,则您不必还声明 A 使用 C(尽管如果您重命名实体或指定了 ONLY 子句,您必须确保在特定情况下什么是可传递的).

Note that module USE is transitive, if A uses B and B uses C you don't have to also declare that A uses C (though if you've renamed entities or specified ONLY clauses you'll have to make sure what is transitive in a particular case).

简而言之,模块是主要的 Fortran 机制,用于通过将程序分解为可管理的块来处理程序中的复杂性.Fortran 2008,当该功能由编译器实现时,也引入了 SUBMODULEs,这有望更好地支持以这种方式处理复杂性.

In a nutshell, modules are the principal Fortran mechanism for dealing with complexity in programs by breaking them into manageable chunks. Fortran 2008, when the feature is implemented by compilers, introduces SUBMODULEs too, which promise even better support for dealing with complexity in this way.

模块也很有用,因为语言标准要求编译器为模块中定义的过程生成显式接口,以便在编译时根据参数进行类型检查.请注意,这些接口(您从未真正看到过)被称为显式接口,以与隐式接口形成对比,隐式接口是未在模块内部定义的过程(或在使用它们的程序单元中CONTAIN)所具有的.当然,您可以为此类过程编写显式接口,但无论是短期还是长期,让编译器为您完成几乎总是更容易.

Modules are also useful in that the language standards require that compilers generate explicit interfaces to procedures defined in modules for type-checking against the arguments at compile time. Note that these interfaces (which you never really see) are called explicit to contrast with implicit interfaces which is what procedures which are not defined inside modules (or CONTAINed within a program unit which uses them) have. You can, of course, write explicit interfaces for such procedures, but it's almost always easier in the short and long runs to let the compiler do it for you.

正如@Telgin 已经指出的,模块也有助于增量编译.

As @Telgin has already noted, modules are an aid to incremental compilation too.

这篇关于Fortran中模块的正确使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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