PURE, ELEMENTAL and side effects
Two attributes describe what a procedure is allowed to do. pure says it has no side effects: it reads its arguments, writes its result and touches nothing else. elemental adds that it may be applied to an array element by element. Both are promises the compiler checks, and both are what a numerical kernel wants to be.
PURE
A pure procedure may not do input or output, may not stop the program, may not change a variable outside its arguments or its local values, and may not call a procedure that is not pure. What is left is arithmetic: the arguments in, the result out.
The file pure.f90 has a pure function and a pure subroutine:
module puremaths
implicit none
contains
pure function hypot2(a, b) result(h)
real, intent(in) :: a, b
real :: h
h = sqrt(a * a + b * b)
end function hypot2
pure subroutine swap(x, y)
real, intent(inout) :: x, y
real :: t
t = x
x = y
y = t
end subroutine swap
end module puremaths
program pure_demo
use puremaths, only: hypot2, swap
implicit none
real :: p, q
print '(a,f8.2)', "hypot2 : ", hypot2(3.0, 4.0)
p = 1.0
q = 2.0
call swap(p, q)
print '(a,2f6.1)', "swapped : ", p, q
end program pure_demo
gfortran -Wall -Wextra -std=f2018 pure.f90 -o pure
./pure
hypot2 : 5.00
swapped : 2.0 1.0
The subroutine is pure and still changes its arguments, because they are its own: a pure procedure may write an intent(inout) dummy. What it may not do is reach outside, and that is the property the compiler enforces and the property that makes the procedure callable from another pure one.
ELEMENTAL
An elemental procedure is a pure scalar procedure that also accepts arrays: given a scalar it works as written, given an array it is applied to every element and returns an array of the same shape. Every elemental procedure is pure unless it says otherwise.
The file elemental.f90 converts kelvin to celsius:
module helpers
implicit none
contains
elemental function celsius(k) result(c)
real, intent(in) :: k
real :: c
c = k - 273.15
end function celsius
end module helpers
program elemental_demo
use helpers, only: celsius
implicit none
real :: kelvin(4)
kelvin = [273.15, 300.0, 310.0, 250.0]
print '(a,f8.2)', "scalar : ", celsius(300.0)
print '(a,4f8.2)', "array : ", celsius(kelvin)
end program elemental_demo
gfortran -Wall -Wextra -std=f2018 elemental.f90 -o elemental
./elemental
scalar : 26.85
array : 0.00 26.85 36.85 -23.15
One line of Fortran converted four values and needed no loop, and the same function still takes a scalar for the first print. The elements come out in the order of the array, and the last one is negative because 250 kelvin is below freezing, which is the arithmetic the function does and not a special case.
IMPURE ELEMENTAL
A procedure that must print or keep a counter cannot be pure. Fortran 2008 allows it to be elemental anyway, with the prefix impure: the array mapping stays, the promise of no side effects goes, and a call from a pure procedure becomes illegal.
module noisy
implicit none
contains
impure elemental function trace(x) result(y)
real, intent(in) :: x
real :: y
print '(a,f6.1)', "trace: ", x
y = 2.0 * x
end function trace
end module noisy
program impure_demo
use noisy, only: trace
implicit none
real :: v(3), r(3)
v = [1.0, 2.0, 3.0]
r = trace(v)
print '(a,3f6.1)', "result : ", r
end program impure_demo
gfortran -Wall -Wextra -std=f2018 impure.f90 -o impure
./impure
trace: 1.0
trace: 2.0
trace: 3.0
result : 2.0 4.0 6.0
Three lines of trace and one result line, because the write to a variable is a separate statement. The procedure is a debugging tool and not a kernel: it is legal, it is slow, and the order of the three prints is not something the standard fixes, so a log built this way cannot be relied on for a sequence.
What a pure procedure may not do
The program below declares a function pure and prints inside it:
program badpure
implicit none
print *, noisy(1.0)
contains
pure function noisy(x) result(y)
real, intent(in) :: x
real :: y
print *, "called with", x
y = 2.0 * x
end function noisy
end program badpure
gfortran -Wall -Wextra -std=f2018 badpure.f90 -o badpure
badpure.f90:10:29:
10 | print *, "called with", x
| 1
Error: PRINT statement at (1) not allowed within PURE procedure
The fix is one of two: remove the print and keep the promise, or add impure and accept that the function is not a value. The first is what a numerical kernel wants, and the second is what the code above does.
Exercises
- Make swap impure by adding a print to it. Does it still build inside the pure module?
- Call celsius on a two by two matrix. What is the shape of the result?
- Add a pure prefix to the trace function and rebuild. Which error comes first?
- Write an elemental function that converts metres to feet and apply it to an array of three values. Which values come out?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the PURE attribute promise?
Answer
The procedure has no side effects outside its arguments and local values
A pure procedure reads its arguments and writes its result only.
-
What does an elemental function do with an array argument?
Answer
Applies itself to each element and returns an array of the same shape
Elemental means element by element.
-
What does this program print?
program p implicit none print *, twice(3) contains pure function twice(n) result(r) integer, intent(in) :: n integer :: r r = 2 * n end function twice end program pAnswer
6
The pure function returns twice its argument.
-
Which of these is allowed in a pure procedure?
Answer
Assigning an INTENT(INOUT) dummy argument
A dummy argument belongs to the procedure.
-
What does IMPURE ELEMENTAL allow that ELEMENTAL does not?
Answer
Side effects such as printing
The impure prefix drops the no-side-effect promise.
-
What does gfortran report for a PRINT inside a PURE procedure?
Answer
Error: PRINT statement ... not allowed within PURE procedure
Input and output are side effects.
-
May a pure procedure call another procedure?
Answer
Only a pure one
A non-pure callee would bring its side effects in.
-
Is every elemental procedure also pure?
Answer
Yes, unless it is declared IMPURE
ELEMENTAL implies PURE by default.
-
What is an elemental function best for?
Answer
A scalar formula that a program also wants over an array
The element-wise mapping is the point of the attribute.
-
Why is a pure procedure good for a numerical kernel?
Answer
It can be called in any order, from expressions, and optimised freely
No side effects mean no hidden ordering constraints.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev