title: segmentation fault with openmp legacy code
body: <p>I am trying to applying openmp to legacy fortran code and I tried to do as following but I encounter a runtime error while running the code. </p>
  
  <pre><code>Line 89 of expect.f is the line: rv_avg(m) = rv_avg(m) + psi(k,m)*psi(k,m)*rosc(k)
  
  forrtl: severe (174): SIGSEGV, segmentation fault occurred
  Image              PC                Routine            Line        Source             
  main               0000000000411CE9  expect_                    89  expect.f
  libiomp5.so        00002B13E9690FE3  Unknown               Unknown  Unknown
  Command exited with non-zero status 174
  </code></pre>
  
  <p>I have make stack large enough with ulimit and export KMP_STACKSIZE. I am wondering what went wrong and any suggestions about how to optimizing the legacy code. Thanks a lot!</p>
  
  <pre><code>login1$ ulimit -a
  core file size          (blocks, -c) 0
  data seg size           (kbytes, -d) unlimited
  scheduling priority             (-e) 0
  file size               (blocks, -f) unlimited
  pending signals                 (-i) 514847
  max locked memory       (kbytes, -l) unlimited
  max memory size         (kbytes, -m) unlimited
  open files                      (-n) 1024
  pipe size            (512 bytes, -p) 8
  POSIX message queues     (bytes, -q) 819200
  real-time priority              (-r) 0
  stack size              (kbytes, -s) unlimited
  cpu time               (seconds, -t) unlimited
  max user processes              (-u) 150
  virtual memory          (kbytes, -v) 8388608
  file locks                      (-x) unlimited
  </code></pre>
  
  
  
  <pre><code>!$OMP PARALLEL shared(ng,nstates,natoms,psi,rosc,grt,frt)
  !$OMP DO private(k,m)&amp;
  !$OMP&amp; reduction(+:rv_avg,b_avg)
        do k = 1, ng
           do m = 0, nstates - 1
              rv_avg(m) = rv_avg(m) + psi(k,m)*psi(k,m)*rosc(k)
              b_avg(m) = b_avg(m) + psi(k,m)*psi(k,m)/(mu*rosc(k)**2)
           enddo
        enddo
  !$OMP ENDDO 
  
  !$OMP DO private(k,m,j)&amp;
  !$OMP&amp; reduction(+:grt_avg)     
        do k = 1,ng
           do m = 0, nstates -1
              do j = 1, 3
                 grt_avg(m,j) = grt_avg(m,j) + psi(k,m)*psi(k,m)*grt(k,j)
              enddo
           enddo
        enddo    
  !$OMP ENDDO
  
  !$OMP DO private(k,m,j,l)&amp;
  !$OMP reduction(+:frt_avg)
        do k = 1,ng  
           do m = 0, nstates -1
              do j = 1,3
                 do l = 1, natoms
                    frt_avg(m,l,j) = frt_avg(m,l,j)   &amp;
                         + psi(k,m)*psi(k,m)*frt(k,l,j)
                  enddo
               enddo
           enddo
        enddo
  !$OMP DO private(k,m,j,l,mp)&amp;
  !$OMP reduction(+:d_coup)      
        do k = 1,ng
           do m =0 , nstates - 1
              do j = 1,natoms
                 do l = 1, natoms
                    do mp = 0, nstates - 1
                       d_coup(m,mp,l,j) = d_coup(m,mp,l,j)   &amp;
                            + psi(k,m)*psi(k,mp)*frt(k,l,j)
                    enddo
                 enddo
              enddo
           enddo
        enddo
  !$OMP END DO
  !$OMP END PARALLEL
  </code></pre>