Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

PURE Functions

Ilie__Daniel
Beginner
1,741 Views

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.

0 Kudos
6 Replies
Arjen_Markus
Honored Contributor II
1,741 Views
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 ;)
0 Kudos
Steven_L_Intel1
Employee
1,741 Views
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.
0 Kudos
Andrew_Smith
Valued Contributor I
1,741 Views
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.
0 Kudos
Steven_L_Intel1
Employee
1,741 Views
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.
0 Kudos
Andrew_Smith
Valued Contributor I
1,741 Views
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?
0 Kudos
Steven_L_Intel1
Employee
1,741 Views
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.
0 Kudos
Reply