为什么这段代码在使用pgi而不是intel时会出错(在分配期间)?

时间:2014-06-20 18:31:28

标签: segmentation-fault fortran dynamic-memory-allocation pgi

此代码在intel编译器上运行时有效。但是当使用pgi运行时,它会在列出的子例程中的*和**之间产生错误。我使用pgi编译器和-mcmodel-medium。我需要使用PGI开始使用openacc来获取此代码。什么想法可能是错的?提前谢谢!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!Reading in trajectories from 'md.traj' file
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SUBROUTINE READ_INPUT(time_i,time_f,dt,ION_COUNT,x,y,z,lf,l,nsteps)

    IMPLICIT NONE
    Integer*4, Intent(IN)                           ::time_i,time_f,dt
    Real*4,Intent(OUT),DIMENSION(:,:), Allocatable  ::x,y,z
    Real*4                                          ::vx,vy,vz
    Real*8,  Intent(OUT)                            ::lf,l
    Integer*4                                       ::time,t,j,nsteps
    Integer*4                                       ::ION_COUNT
    Character*100                                   ::filename
    Character*5                                     ::timezone
    Character*6                                     ::xfiletype
    Character*8                                     ::xcode_version,date
    Character*10                                    ::xcode_name,daytime
    Character*20                                    ::xsim_type
    Real*8                                         ::time_set,aspect(3),ev,ek,ppx,pp(9),rho
write (*,*) '*'         
    Allocate(x(1:51200,0:nsteps))
    Allocate(y(1:51200,0:nsteps))
    Allocate(z(1:51200,0:nsteps))
write (*,*) '**'

    open(UNIT=20,FILE='md.traj.00007000000.xv4b',status='OLD',form='unformatted',convert='BIG_ENDIAN')
    Do 20, time=time_i, time_f, dt
     write (*,*) time
     write(*,1001) time
1001    format ('md.',1I11.11,'.x4b')
     write (filename,1001) time
     read(20) xfiletype
     read(20) xcode_name, xcode_version
     read(20) date,daytime,timezone
     read(20) xsim_type
     read(20) time_set,rho,aspect(1),aspect(2),aspect(3),ev, ek, ppx, pp, ION_COUNT
     !rho=ION_COUNT/(xl(1)*xl(2)*xl(3))
     t=(time-time_i)/dt

!没有在子程序代码中分配seg此行的错误

     read (20) (x(j,t),y(j,t),z(j,t),vx,vy,vz, j=1,ION_COUNT) 
     l=(ION_COUNT/(rho))**(1.0/3.0)
    !write (*,*) x(500,5),'in read input'
     lf=(2*3.1415)/l
    20 continue

    RETURN
    END SUBROUTINE READ_INPUT
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

这是主要代码

  Program SofQ

  Implicit NONE  
  Integer            :: time,time_u,j,i,k,OMP_NUM_THREADS
  Integer            :: t1,t3,t4,K2
  Integer            :: dw,t,time1,time2
  Real*4             :: sumbin(0:50000)
  Real*8             :: f(0:500,0:10000)
  Integer            :: Q1,Q2,Q3,Q4,Q5,N1,N2,N3,N4,N5,Qcur
  Real*4             :: CI,co(0:6537673),si(0:6537673),b(0:6537673)
  Real*8             :: dotprod,co_temp,si_temp
  Real*4             :: Qavg,zz,qmax,pfrac,nfrac
  Complex*16,Allocatable:: q(:,:)
  Complex            :: sumbin1(0:50000),sumbin2(0:50000)
  Double Precision   :: yy 
  Character          :: sofq_output,dumb
  Parameter (CI=(0,-1))
  Real*8             ::rtemp1,rtemp2,rtemp3
  Character*5        :: simtype

  Integer*4          ::p_start,p_end,nsteps

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Declarations for READ_INPUT
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  Integer*4                          ::time_i,time_f,dt,ION_COUNT
  Real*4,Dimension(:,:),Allocatable  ::x,y,z
  Real*8                             ::lf,l
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!Declarations for READ_VECTORS
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  Integer*4                         ::vmax,lines,i_vmax
  Integer*4, Dimension(1:6537673)   ::rx,ry,rz,mag,degen
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!Declarations for COMPUTE_SQ
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  Integer*4                         ::ions,t2
  Real*8, Allocatable               ::s(:)
  Complex*16,Allocatable            ::r(:,:)


open (UNIT=11,File='sqt_n1_gpu.dat')
write (*,*) 'begin'     
call system_clock(time1)      

  dt=20
  time_i= 7000000
  time_f= 7000500 !7050000

    nsteps=(time_f-time_i)/dt
    Allocate(x(1:51200,0:nsteps))
    Allocate(y(1:51200,0:nsteps))
    Allocate(z(1:51200,0:nsteps))  

  ION_COUNT=51200
  qmax=0.6
  simtype='pfrac'

!      OMP_NUM_THREADS=16

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Parallel Test!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!       !$OMP PARALLEL
!       write (*,*) 'Parallel?'
!       !$OMP END PARALLEL
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    CALL READ_INPUT(time_i,time_f,dt,ION_COUNT,x,y,z,lf,l,nsteps)
write (*,*) 'input'

所以一些评论者询问子程序内部的重新分配。我之所以这样做是因为我首先尝试了它而没有第二次分配,并且当它在第一个x()值中读取时,在子程序中收到了分段错误。我再次尝试了这一点,在子程序中注释了分配,发现PGI seg在读取x()期间发生了故障,并且intel编译器做了同样的事情。这一行在上面用粗体表示。

1 个答案:

答案 0 :(得分:0)

当您使用intent(OUT)作为可分配数组的参数时,数组变量(在您的情况下为x,y和z)在进入子例程时变为“未分配”。因此,在这种情况下,您必须在开始使用它之前在子程序中分配它。

当您在子例程中分配可分配数组时,因为它与伪参数相关联,所以在子例程的末尾不会释放它,就像阵列是本地例程时那样。

所以,你可以简单地用intent(inout)声明x,y和z(或者没有,因为在这种情况下“intent(INOUT)”将产生与不定义意图相同的影响)并且没有必要在子例程中再次分配数组。或者您只能在例程中分配它,并避免在其外部进行分配。但是,一旦看起来你想在子程序中使用数组OUTSIDE,我认为这是不好的做法。

当您在子例程中使用代码而没有“分配”时,两个编译器“seg fault”的事实是正确的行为,因为数组在进入子例程时将被取消分配(因为intent(out))。在子例程中“再次”分配数组的情况下PGI“seg fault”的事实似乎是某种编译器错误。在这种情况下,如果使用PGI,则在子例程外部分配可能是唯一可用的选项。