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

查看:24
本文介绍了在 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(尽管如果您已重命名实体或指定 只有子句你必须确保在特定情况下什么是可传递的).

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,当该功能由编译器实现时,也引入了 SUBMODULE,它承诺更好地支持以这种方式处理复杂性.

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天全站免登陆