0

I have searched a lot but haven't found any answer. Here's part of my code.

program simulation
  use random, only: random_normal
  use omp_lib
  real(8),parameter:: r_critical=0.4,dt=10.**(-3)
  real,parameter:: Estar=8.86
  real,parameter::A=236.
  real(8),dimension(0:4):: a0
  real(8) lens,rs,ss,cs,zs,is,js,ks,ls,ms
  real(8):: lens0,rs0,ss0,cs0,zs0 
  real(8) Edef, Esh, Eld, AH, cm12 
  real,dimension(5):: shape_para
  integer,dimension(5):: ii
  integer::stat
  integer :: N
  real :: x 
  integer :: counts
  real,dimension(0:28,0:20,0:18,0:18,0:24)::nuc
  real(8) deltal,deltar,deltaz,deltac,deltas  

  ! read the data--len,r,s,c,z,U
  open(1,iostat=stat,FILE="236.txt")

  if(stat==0) then
     write(*,*) 'READING SUCCESSFUL'
  end if
  nuc(:,:,:,:,:)=100.

  do
     read(1,*,end=100) cm12, ii(1:5), shape_para(1:5),Esh,Eld,Edef,AH 
     nuc(ii(1),ii(2),ii(5),ii(4),ii(3))=Edef
  enddo
  100 continue

  write(*,*) 'END READING DATA'
  lens0=1.665;rs0=0.585;zs0=0.320;cs0=0.570;ss0=-0.158

  write(*,*) 'START POINT',translen(lens0),transr(rs0,lens0),transz(zs0),transc(cs0,lens0),transs(ss0)
  open(2,asynchronous='yes',file='output.txt')

  call omp_set_num_threads(24)                           
  !$omp parallel do &
  !$omp default(none) &
  !$omp private(N,lens,rs,zs,cs,ss,deltal,deltar,deltaz,deltac,deltas,counts,a0,AH) &
  !$omp shared(nuc,lens0,rs0,zs0,cs0,ss0)
  do N=1,10000
     lens=lens0;rs=rs0;zs=zs0;cs=cs0;ss=ss0
     counts=0
     do while (rs > r_critical)   !j > j-critical
        deltal=interpoFl(nuc,lens,rs,ss,cs,zs)*dt + sqrt(2*sqrt((Estar-interpoU(nuc,lens,rs,ss,cs,zs))*8/A)*dt)*random_normal()
        deltar=interpoFr(nuc,lens,rs,ss,cs,zs)*dt + sqrt(2*sqrt((Estar-interpoU(nuc,lens,rs,ss,cs,zs))*8/A)*dt)*random_normal()
        deltas=interpoFs(nuc,lens,rs,ss,cs,zs)*dt + sqrt(2*sqrt((Estar-interpoU(nuc,lens,rs,ss,cs,zs))*8/A)*dt)*random_normal()
        deltac=interpoFc(nuc,lens,rs,ss,cs,zs)*dt + sqrt(2*sqrt((Estar-interpoU(nuc,lens,rs,ss,cs,zs))*8/A)*dt)*random_normal()
        deltaz=interpoFz(nuc,lens,rs,ss,cs,zs)*dt + sqrt(2*sqrt((Estar-interpoU(nuc,lens,rs,ss,cs,zs))*8/A)*dt)*random_normal()
        if(not_exist(lens+deltal,rs+deltar,ss+deltas,cs+deltac,zs+deltaz)) cycle
        if(Estar<interpoU(nuc,lens+deltal,rs+deltar,ss+deltas,cs+deltac,zs+deltaz)) cycle
        lens=lens+deltal;rs=rs+deltar;ss=ss+deltas;cs=cs+deltac;zs=zs+deltaz
        counts=counts+1
        if(counts>100000) exit
     end do
     call cal_shape_a0(lens,rs,ss,cs,zs,a0(0:4))
     call CalCenterofMass(lens,zs,a0(0:4),AH)
     write(2,'(f10.2)') AH
  end do

contains
...
end program

compilation is of no problem, but when I use ./a.out to execute the program, it gives me segmentation fault(core dumped), no other messages. And I used gdb to find where the fault happened. Then I found that it was something wrong with the open file line.

open(1,iostat=stat,FILE="236.txt")

so I don't know why. It can execute correctly when squentially operate it, but wrong when I use openmp. Does anyone know why it is?

zmwang
  • 519
  • 1
  • 7
  • 13
  • 2
    When a program apparently crashes on its first executable statement always ask yourself *What went wrong with my declarations ?* In this case, as you will find if you dig around a little on SO where this question has been asked, and answered before, the answer is *OpenMP places static arrays in a different location to the one that a program without OpenMP does.* The size of the array `nuc` is likely to exceed some limit. Right now I haven't the time to write a proper answer. – High Performance Mark May 06 '16 at 06:43
  • First issue `ulimit -s unlimited` to remove the stack size limit of the main thread, then run your executable. – Hristo Iliev May 06 '16 at 07:05
  • @HighPerformanceMark thank you for you advice: "When a program apparently crashes on its first executable statement always ask yourself What went wrong with my declarations ?" I've never encountered this problem before. I did see some topics about the stack size problem, but I didn't realize I had the same problem with them. – zmwang May 06 '16 at 07:34
  • @HristoIliev It works! As you mentioned in your answer, using `ulimit -s unlimited` to remove the stack size of the main thread. While I think this is the command to remove the limit of all thread, am I right? And is there any command to remove the limit of only the main thread? – zmwang May 06 '16 at 07:37
  • 1
    No, no such command. Uae allocatable arrays us suggested in several duplicaes here to avoid the problem. No time to search for tye duplicates now. BTW `real(8)` ia ugly code smell and not portable. – Vladimir F Героям слава May 06 '16 at 07:45
  • On most Unix-like OSes for x86, there can be only one stack of unlimited size per process, namely the stack of the main thread. – Hristo Iliev May 06 '16 at 08:52

0 Answers0