- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
Is there a benefit for declaring a function as PURE?
Is the compiler going to generate faster code or is the function going to be used differently?
Thank you for your time.
Daniel.
Link Copied
6 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Pure subroutines and functions guarantee (*) to the user and to the compiler that there are no side effects.
That does mean they have to comply to a small set of restrictions. It may help the compiler to optimise
the use of these routines. Whether it will really be beneficial greatly depends on the actual situation.
I would use it mostly as a means of documenting that the routine/function has no side effects and not rely
on possible optimisation benefits.
Regards,
Arjen
(*) One of the restrictions is that you can not write to external files. So debugging by printing is not possible.
Unless you use some trickery - define aninterface block that actually lies to the compiler about the character
of the routine/function. Do not do it unless you know what you are doing ;)
That does mean they have to comply to a small set of restrictions. It may help the compiler to optimise
the use of these routines. Whether it will really be beneficial greatly depends on the actual situation.
I would use it mostly as a means of documenting that the routine/function has no side effects and not rely
on possible optimisation benefits.
Regards,
Arjen
(*) One of the restrictions is that you can not write to external files. So debugging by printing is not possible.
Unless you use some trickery - define aninterface block that actually lies to the compiler about the character
of the routine/function. Do not do it unless you know what you are doing ;)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There are some places where the standard requires the use of PURE functions, such as in specification expressions. If the function is PURE, the compiler knows there are no side-effects and this can improve code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It would be good to have an impure prefix and a project setting to have pure by default. It would save a lot of typing and it would promote better code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I don't think you understand what that would mean. It is a severe restriction without a real benefit for most code. PURE is most useful for small routines that have simple functions, like math routines. It's not something general purpose.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Er, wrong. I well understand the restrictions of PURE.
Where the pure code really helps me is with implementing openMP. I can more easily ensure changes are not made to shared data which would require bits of single threaded code. PURE also leeds to a better style of programming where we isolate things into smaller and more easily understood functions or subroutines.
I would say 80% of my Fortran is pure. I have around 150,000 lines of code. Nearly all the code is mathematical and I don't do much IO which both help.
If impure Fortran code really is the majority and most must be impure, what a nightmare that must be for maintenance, never knowing if there are side effects. Could it be a legacy from F77 that most code is so bad?
The previous poster was asking what the benefits were and you said "If the function is PURE, the compiler knows there are no side-effects and this can improve code.", and now you add: "without a real benefit for most code", so does that mean the compiler does not make use of PURE in many situations?
Where the pure code really helps me is with implementing openMP. I can more easily ensure changes are not made to shared data which would require bits of single threaded code. PURE also leeds to a better style of programming where we isolate things into smaller and more easily understood functions or subroutines.
I would say 80% of my Fortran is pure. I have around 150,000 lines of code. Nearly all the code is mathematical and I don't do much IO which both help.
If impure Fortran code really is the majority and most must be impure, what a nightmare that must be for maintenance, never knowing if there are side effects. Could it be a legacy from F77 that most code is so bad?
The previous poster was asking what the benefits were and you said "If the function is PURE, the compiler knows there are no side-effects and this can improve code.", and now you add: "without a real benefit for most code", so does that mean the compiler does not make use of PURE in many situations?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I meant that most Fortran applications do not have the majority of their routines that would qualify as PURE. Your application is unusual, but is interesting.
I will note that the compiler has a feature that can check for unintended use of shared variables, Source Checker. This is being improved in future versions.
I will note that the compiler has a feature that can check for unintended use of shared variables, Source Checker. This is being improved in future versions.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page