Fortran 90的 - "分割故障 - 无效的内存引用"可扩展的3D阵列 [英] Fortran 90 - "Segmentation fault - invalid memory reference" with scalable 3D array

查看:823
本文介绍了Fortran 90的 - "分割故障 - 无效的内存引用"可扩展的3D阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编一个Fortran 90的程序与gfortran它建立在某种程度上我想要一个可扩展的三维阵列。一旦运行,我收到以下错误:

 计划接收信号SIGSEGV:段错误 - 无效的内存引用。回溯跟踪此错误:
#0 0x10542ee42
#1 0x10542f60e
#2 0x7fff8d7895a9
#3 0x10542575e
#4 0x105425975
#5 0x105425d0e
段故障:11

我相信这是一个内存问题与大型3D阵列,因为它的工作原理,如果我降低了尺寸,但反正是有解决这个问题?这里是我的code:

 程序phantomtest
隐式NONE
  INTEGER,参数::列= 34,行数= 34,DIAGS =((4 *列)-6)中,m =(4 *列)-6+(2 *列)
  REAL,可分配,外形尺寸(:,:,:) ::幻象
  INTEGER :: I,J,K
  CHARACTER(LEN = 3):: nstring,nullstring =''ALLOCATE(幻影(列,行,M))
幻影= 0CALL Phantom_Making(幻影,列,行,DIAGS,M)WRITE(nstring,FMT =(I3)),列
PRINT *,nullstring
DO K = 1,M
  我= 1时,柱
    WRITE(*,FMT =(// // nstringI2))幻影(我,:,K)
  END DO
  PRINT *,nullstring
END DOEND PROGRAM phantomtest
!---------------------------
SUBROUTINE Phantom_Making(幻影,列,行,DIAGS,M)
隐式NONE
  INTEGER,意向(IN)::列,行,DIAGS,男
  REAL,DIMENSION(列,行,男),INTENT(INOUT)::幻象
  INTEGER :: I,J,K,L!垂直和水平射线
我= 1,行
  幻影(:,我,我)= 1
  幻影(我:I +(列)+(DIAGS / 2))= 1
END DO!会是怎么样。
幻影(1,2,1 +列)= 1
幻影(2,1,1 +列)= 1
幻象(1,列-1,1- +列+(DIAGS / 2)+行)= 1
幻象(2,列1 +列+(DIAGS / 2)+行)= 1
J =列-1DO K = 2 +柱,(DIAGS / 2)+列
  幻象(2:柱,:,k)的=幻象(1:(列-1),:,K-1)
  IF(((K + 1)-columns).LE.columns)幻象(1,K + 1列中,k)= 1
END DODO L =列数+(DIAGS / 2)+行+ 2列+(DIAGS / 2)+行+ 1 +(DIAGS / 2)
  当J = J-1
  幻象(2:柱,:,L)=幻象(1:(列-1),:,L-1)
  中频(j.GT.0)虚线(1,J,L)= 1
END DOEND SUBROUTINE


解决方案

许多编译器都可以帮助您在调试检查。例如用gfortran:

  gfortran -g -fcheck =所有-Wall segf.f90
segf.f90:5.17:  INTEGER :: I,J,K
                 1
警告:未使用的变量'J'宣布在(1)
〜/ F /暴躁/计算器> ./a.out
在文件segf.f90行50
Fortran运行时错误:3维指数'199'阵'幻影'之外的预期范围(1:198)

其他编译器有类似的选项。

您的循环运行在升这样的值您正在访问数组幻影出界。

确认的正确的价值观, 1:M ,或数组幻影的尺寸正确指定要这个范围。( 1:M

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

解决方案

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).

这篇关于Fortran 90的 - "分割故障 - 无效的内存引用"可扩展的3D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆