Fortran Allocatable Arrays in OpenMP 3.0Prior to OpenMP 3.0 there were the following restrictions on Fortran allocatable arrays: (a) An allocatable array may appear in a private clause. If an allocatable array appears in a private clause, then it must have an allocation status of "not currently allocated" on entry to and on exit from the construct. The restrictions on Fortran allocatable arrays have been loosened in OpenMP Specification Version 3.0 (ref. 2). Fortran allocatable arrays may now appear in private, firstprivate, lastprivate, reduction, copyprivate, and copyin clauses. Here is the relevant text from OpenMP Specification Version 3.0. (a) PRIVATE Clause (Section 2.9.3.3, p. 90): For a list item with the ALLOCATABLE attribute:
(b) FIRSTPRIVATE Clause (Section 2.9.3.4, p. 92): (c) LASTPRIVATE Clause (Section 2.9.3.5, p. 96): (d) REDUCTION Clause (Section 2.9.4.6, p. 100): (e) COPYPRIVATE Clause (Section 2.9.4.2, p. 104): (f) COPYIN Clause (Section 2.9.4.1, p. 102): ExamplesThe following examples illustrate the behavior of Fortran allocatable arrays in OpenMP 3.0. Example 1 (Fortran):
program example1
use omp_lib
PARAMETER ( n = 200 )
integer, allocatable, dimension (:) :: A
integer i
call omp_set_dynamic(.false.)
call omp_set_num_threads(16)
allocate (A(n))
!$omp parallel private (A, i)
do i = 1, n
A(i) = i
end do
print *, A(38)
!$omp end parallel
end
% f90 -xopenmp -xO3 example1.f
% a.out
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
38
Example 2 (Fortran):
program example2
real, dimension(:), allocatable :: a
real, dimension(:), pointer :: b
call omp_set_dynamic (.false.)
call omp_set_num_threads (15)
!$omp parallel private (a, b)
allocate (a(5))
allocate (b(10))
!$omp single
a = 22
b = 33
!$omp end single copyprivate (a, b)
print *, a, b
!$omp end parallel
end
% f90 -xopenmp -xO3 example2.f
% a.out
22.0 22.0 22.0 22.0 22.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0
22.0 22.0 22.0 22.0 22.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0
22.0 22.0 22.0 22.0 22.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0
22.0 22.0 22.0 22.0 22.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0
22.0 22.0 22.0 22.0 22.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0
22.0 22.0 22.0 22.0 22.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0
22.0 22.0 22.0 22.0 22.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0
22.0 22.0 22.0 22.0 22.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0
22.0 22.0 22.0 22.0 22.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0
22.0 22.0 22.0 22.0 22.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0
22.0 22.0 22.0 22.0 22.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0
22.0 22.0 22.0 22.0 22.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0
22.0 22.0 22.0 22.0 22.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0
22.0 22.0 22.0 22.0 22.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0
22.0 22.0 22.0 22.0 22.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0 33.0
Example 3 (Fortran):
program example3
use omp_lib
real, dimension(:), allocatable :: a
real, dimension(:), allocatable :: b
call omp_set_dynamic (.false.)
call omp_set_num_threads (15)
allocate (a(5))
allocate (b(10))
a = 10
b = 20
!$omp parallel reduction (+:a, b) private (i, j)
do i = 1, 5
a(i) = a(i) + 1
end do
do j = 1, 10
b(j) = b(j) + 20
end do
!$omp end parallel
print *, a
print *, b
end
% f90 -xopenmp -xO3 example3.f
% a.out
25.0 25.0 25.0 25.0 25.0
320.0 320.0 320.0 320.0 320.0 320.0 320.0 320.0 320.0 320.0
REFERENCES1. OpenMP Specification Version 2.5 2. OpenMP Specification Version 3.0 |
OpenMP 3.0 Features in Express 11.08
|