Quantcast
Channel: Intel Developer Zone Articles
Viewing all articles
Browse latest Browse all 3384

Contiguity Checking for Pointer Assignments in the Intel® Fortran Compiler

$
0
0

The Fortran 2008 Standard introduced the CONTIGUOUS attribute for assumed shape arrays and pointers. The CONTIGUOUS attribute may help the compiler optimize more effectively, see https://software.intel.com/en-us/articles/vectorization-and-array-contiguity. However, specifying the CONTIGUOUS attribute for an assumed shape array or pointer that may not be contiguous may lead to incorrect behavior at run-time. The Intel® Fortran Compiler version 18 introduces a new feature that checks at run-time whether the targets of contiguous pointer assignments are indeed contiguous.

Consider the following example:

module mymod         !  source file inc.F90
contains
  subroutine inc(b)
    real, dimension(:),             pointer :: b
#ifdef CONTIG
    real, dimension(:), contiguous, pointer :: c
#else
    real, dimension(:),             pointer :: c
#endif

    c => b
    c =  1.
  end subroutine inc
end module mymod

program drive_inc     ! source file drive_inc.F90
  use mymod
  implicit none
  integer, parameter          :: n=25, stride=5
  real, target,  dimension(n) :: a=0.
  real, pointer, dimension(:) :: b

  b => a(:n:stride)
  call inc(b)

  print '(5F5.0)', a
end program drive_inc

$ ifort inc.F90 drive_inc.F90; ./a.out
   1.   0.   0.   0.   0.
   1.   0.   0.   0.   0.
   1.   0.   0.   0.   0.
   1.   0.   0.   0.   0.
   1.   0.   0.   0.   0.

Without the contiguity attribute, results are correct.

$ ifort -DCONTIG inc.F90 drive_inc.F90; ./a.out
   1.   1.   1.   1.   1.
   0.   0.   0.   0.   0.
   0.   0.   0.   0.   0.
   0.   0.   0.   0.   0.
   0.   0.   0.   0.   0.

Here, pointer c is declared as contiguous, but the actual target is not, leading to incorrect results.

$ ifort -DCONTIG -check contiguous -traceback inc.F90 drive_inc.F90; ./a.out
forrtl: severe (408): fort: (32): A pointer with the CONTIGUOUS attributes is being made to a non-contiguous target.

Image               PC                Routine            Line        Source
a.out                 0000000000405220    Unknown                Unknown  Unknown
a.out                 0000000000402AEF   mymod_mp_inc_       11          inc.F90
a.out                 0000000000402C23   MAIN__                      9           drive_inc.F90
a.out                 0000000000402A9E   Unknown                Unknown  Unknown
libc-2.17.so       00007F1A0A6B0B35  __libc_start_main  Unknown  Unknown
a.out                 00000000004029A9   Unknown                Unknown  Unknown

With the new option -check contiguous (Linux* or OS X*) or /check:contiguous (Windows*), the version 18 compiler detects the assignment of a contiguous pointer to a non-contiguous target. The option -traceback (/traceback) identifies the function and source file line number at which the incorrect assignment took place. It is not necessary to compile with the debugging option -g (Linux or OS X) or /Zi (Windows) in order to get this traceback.


Viewing all articles
Browse latest Browse all 3384

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>