Fortran Allocatable Arrays

Fortran Allocatable Arrays in OpenMP 3.0

Prior 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.
(b) Allocatable arrays may not appear in a firstprivate clause.
(c) Allocatable arrays may not appear in a lastprivate clause.
(d) Allocatable arrays may not appear in a reduction clause.
(e) Allocatable arrays may not appear in a copyin clause.
(f) Allocatable arrays may not appear in a copyprivate clause.

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):
A new list item of the same type is allocated once for each implicit task in the parallel region, or for each task generated by a task construct, if the construct references the list item in any statement. The initial value of the new list item is undefined. Within a parallel, worksharing, or task region, the initial status of a private pointer is undefined.

For a list item with the ALLOCATABLE attribute:

  • if the list item is "not currently allocated", the new list item will have an initial state of "not currently allocated";
  • if the list item is allocated, the new list item will have an initial state of allocated with the same array bounds.

(b) FIRSTPRIVATE Clause (Section 2.9.3.4, p. 92):
A list item that appears in a FIRSTPRIVATE clause is subject to the private clause semantics described in Section 2.9.3.3 on page 89. In addition, the new list item is initialized from the original list item existing before the construct.

(c) LASTPRIVATE Clause (Section 2.9.3.5, p. 96):
An original list item with the ALLOCATABLE attribute must be in the allocated state at entry to the construct containing the lastprivate clause. The list item in the sequentially last iteration or lexically last section must be in the allocated state upon exit from that iteration or section with the same bounds as the corresponding original list item.

(d) REDUCTION Clause (Section 2.9.4.6, p. 100):
An original list item with the ALLOCATABLE attribute must be in the allocated state at entry to the construct containing the reduction clause. Additionally, the list item must not be deallocated and/or allocated within the region.

(e) COPYPRIVATE Clause (Section 2.9.4.2, p. 104):
An array with the ALLOCATABLE attribute must be in the allocated state with the same bounds in all threads affected by the copyprivate clause.

(f) COPYIN Clause (Section 2.9.4.1, p. 102):
An array with the ALLOCATABLE attribute must be in the allocated state. Each thread's copy of that array must be allocated with the same bounds. Note: This feature not yet fully implemented by the Fortran compiler in this Express release. An ALLOCATABLE array in a COPYIN clause will get a diagnostic message

Examples

The 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

REFERENCES

1. OpenMP Specification Version 2.5
http://www.openmp.org/mp-documents/spec25.pdf

2. OpenMP Specification Version 3.0
http://www.openmp.org/mp-documents/spec30.pdf

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Sign up or Log in to add a comment or watch this page.


The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

Copyright 1994-2009 Sun Microsystems, Inc.
Powered by Atlassian Confluence
Sun Guidelines on Public Discourse Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact