正确使用 PURE 关键字 Fortran [英] Proper use of the PURE keyword Fortran

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

问题描述

我目前正在研究 Fortran,我遇到了 pure 关键字指定没有副作用的函数/子例程.

I'm currently delving into Fortran and I've come across the pure keyword specifying functions/subroutines that have no side effects.

我有一本书,Fortran 90/95,作者是 S Chapman,它介绍了 pure 关键字,但奇怪的是没有提供良好的编码实践"用途.

I have a book, Fortran 90/95 by S Chapman which introduces the pure keyword but strangely provides no "good coding practice" uses.

我想知道在他们的程序中应该如何自由地使用这个关键字.环顾四周,对我来说很明显大多数没有副作用的程序都没有必要包含 pure 关键字.

I'm wondering how liberally one should use this keyword in their procedures. Just by looking around it's obvious to me that most procedures without side effects don't find it necessary to include the pure keyword.

那么它最好用在哪里呢?只有在程序中要完全保证没有副作用?或者也许在程序中计划稍后转换为 elemental 程序?(因为elemental程序必须首先是pure.)

So where is it best used? Only in procedures one wants to completely guarantee have no side effects? Or perhaps in procedures one plans to convert to elemental procedures later? (As elemental procedures must first be pure.)

推荐答案

PURE 在某些情况下是必需的 - 例如,在规范表达式或从 FORALLDO CONCURRENT 构造.在这些情况下,需要 PURE 以使 Fortran 处理器在过程调用的排序方面具有灵活性,同时仍然具有来自特定代码段的合理确定性结果.

PURE is required in some cases - for example, procedures called within specification expressions or from FORALL or DO CONCURRENT constructs. PURE is required in these case to give the Fortran processor flexibility in the ordering of procedure invocations while still having a reasonably deterministic outcome from a particular stretch of code.

除了那些必需的情况,是否使用PURE基本上是风格问题,有点主观.

Beyond those required cases, whether to use PURE or not is basically a question of style, which is somewhat subjective.

使用 PURE 有成本(无法在过程中进行 IO,无法调用非 PURE 的过程)和好处(今天编写的纯过程可以从明天编写的需要纯过程的上下文中调用,因为 PURE 过程没有副作用,调用此类过程的含义对于代码的读者来说可能更清楚),行业两者之间的-off取决于具体情况.

There are costs to use of PURE (inability to do IO within the procedure, inability to call procedures that are not PURE) and benefits (a pure procedure written today can be called from a context written tomorrow that requires a pure procedure, because PURE procedures have no side effects the implications of an invocation of such a procedure may be clearer to a reader of the code), the trade-off between the two depends on specifics.

该标准可能为 Fortran 处理器在评估表达式和表达式中的函数引用的方式提供了相当大的余地.它肯定会在某些方面围绕函数执行的副作用和函数参数的修改限制程序.对纯函数的要求与该 lee-way 和那些约束是一致的,因此有些人使用大多数函数都是纯函数的样式.同样,它可能仍取决于具体情况,并且可能必须存在异常,例如 C 互操作性或与外部 API 的交互.

The standard may give Fortran processors considerable lee-way in how they evaluate expressions and function references within expressions. It definitely constrains programs in some ways around side effects of function execution and modification of function arguments. The requirements on a pure function are consistent with that lee-way and those constraints, consequently some people use a style where most functions are pure. Again, it may still depend on specifics, and exceptions may have to exist for things like C interoperability or interaction with external API's.

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

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