Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran 90 - "Segmentation fault - invalid memory reference" with scalable 3D array

I have compiled a fortran 90 program with gfortran which builds a scalable 3D array in a way I want. Upon running, I get the following error:

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x10542ee42
#1  0x10542f60e
#2  0x7fff8d7895a9
#3  0x10542575e
#4  0x105425975
#5  0x105425d0e
Segmentation fault: 11

I believe this is a memory issue with the large 3D array, as it works if I decrease the dimensions, but is there anyway to get around this? Here is my code:

PROGRAM phantomtest
IMPLICIT NONE
  INTEGER, PARAMETER:: columns=34, rows=34, diags=((4*columns)-6),  m=(4*columns)-6+(2*columns)
  REAL, ALLOCATABLE, DIMENSION(:,:,:)::phantom
  INTEGER :: i, j, k
  CHARACTER (LEN=3) :: nstring, nullstring=''

ALLOCATE(phantom(columns,rows,m))
phantom=0

CALL Phantom_Making(phantom,columns,rows,diags,m)

WRITE(nstring,FMT="(I3)"), columns
PRINT*, nullstring
DO k=1,m
  DO i=1,columns
    WRITE(*,FMT="("//nstring//"I2)") phantom(i,:,k)
  END DO
  PRINT *, nullstring
END DO

END PROGRAM phantomtest
!---------------------------
SUBROUTINE Phantom_Making(phantom,columns,rows,diags,m)
IMPLICIT NONE
  INTEGER, INTENT(IN):: columns, rows, diags, m
  REAL, DIMENSION(columns,rows,m), INTENT(INOUT) :: phantom
  INTEGER :: i, j, k, l

!Vertical and horizontal rays
DO i=1,rows
  phantom(:,i,i) = 1
  phantom(i,:,i+(columns)+(diags/2)) = 1
END DO

!Diagonal rays
phantom(1,2,1+columns) = 1
phantom(2,1,1+columns) = 1
phantom(1,columns-1,1+columns+(diags/2)+rows) = 1
phantom(2,columns,1+columns+(diags/2)+rows) = 1
j = columns-1

DO k=2+columns, (diags/2)+columns
  phantom(2:columns,:,k) = phantom(1:(columns-1),:,k-1)
  IF (((k+1)-columns).LE.columns) phantom(1,k+1-columns,k)=1
END DO

DO l=columns+(diags/2)+rows+2, columns+(diags/2)+rows+1+(diags/2)
  j = j-1
  phantom(2:columns,:,l) = phantom(1:(columns-1),:,l-1)
  IF (j.GT.0) phantom(1,j,l) = 1
END DO

END SUBROUTINE
like image 421
Lorde555 Avatar asked Apr 17 '14 09:04

Lorde555


1 Answers

The checks which many compilers have can assist you in debugging. For example with gfortran:

gfortran -g -fcheck=all -Wall segf.f90
segf.f90:5.17:

  INTEGER :: i, j, k
                 1
Warning: Unused variable 'j' declared at (1)
~/f/testy/stackoverflow> ./a.out 
At line 50 of file segf.f90
Fortran runtime error: Index '199' of dimension 3 of array 'phantom' outside of expected range (1:198)

Other compilers have similar options.

Your loops runs on such values of l that you are accessing the arrayphantom out of bounds.

Make sure l has the correct values, 1:m, or that the dimensions of the array phantom are specified correctly to be this range (1:m).

like image 146
Vladimir F Героям слава Avatar answered Nov 15 '22 17:11

Vladimir F Героям слава