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

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

问题描述

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



我有一本书,由S Chapman撰写的 Fortran 90/95 介绍了 pure 关键字,但奇怪地没有提供good编码练习的用法。



我想知道在程序中应该如何使用这个关键字。只是通过环视,对我来说很明显大多数没有副作用的程序都没有必要包含关键字。



那么它最适合用在哪里?只有在程序中希望完全保证没有副作用?或者在程序中计划稍后将其转换为 elemental 程序? (as elemental 程序必须先是 pure 。)

PURE,例如,在规范表达式中调用的过程或从 FORALL 中调用的过程。 code>或 DO CONCURRENT 结构。在这种情况下,需要 PURE 来赋予Fortran处理器在程序调用顺序上的灵活性,同时还能从特定的一段代码中获得合理的确定性结果。



除了这些要求的情况之外,是否使用 PURE 基本上是一个风格问题,它有点主观。



使用 PURE 会有成本(无法在过程中执行IO,无法调用非<$ c ($ c> PURE )和好处(今天编写的纯过程可以从明天编写的需要纯过程的上下文中调用,因为 PURE 过程没有任何副作用,调用这样一个程序的含义可能会更清楚地代码读者),两者之间的折衷取决于具体情况。

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


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

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

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.

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 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.

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

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.

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