Sumanta Kundu
Sumanta Kundu

Reputation: 11

Why I am getting segmentation fault in Openmp when adding numbers stored in an array?

I am very new in Openmp. I was summing up N integer numbers stored in an array and compiling the code using gfortran. Upto N=10^6, the results obtained from the serial and parallel codes are exactly same. For N=10^7, the serial code is running but, the parallel code (after compilation using -fopenmp flag) is giving "Segmentation fault". I have given my code here. Could anyone please help me why it is happening?

  use omp_lib
  REAL*8 r,summ,sl
  parameter (N=10000000)
  dimension r(N)

  do i=1,N
  r(i)=i
  enddo

  summ=0.0d00
  sl=0.0d00

  !$OMP PARALLEL FIRSTPRIVATE(sl) SHARED(r,summ)
  !$OMP DO 
  do i=1,N
  sl=sl+r(i)
  enddo
  !$OMP END DO
  !$OMP CRITICAL
  summ=summ+sl
  !$OMP END CRITICAL
  !$OMP END PARALLEL

  write(*,*)'SUM',summ

  end

Upvotes: 0

Views: 318

Answers (1)

Noureddine
Noureddine

Reputation: 190

I have experienced the same problem before. The problem is that your code seems requiring a large memory.

Be sure that you use compiler option when you compile your code -mcmodel=medium. Also, when you use the -fopenmp your compiler calls systematically -frecursive that limit the size of your stack to a default value. Therefore, your code try to wrtie outside of the stack limitation that leads to a segmentation fault. To get rid of this problem you have to cancel the default limitation of the stack. One way to do this rapidely is to run on a terminal the command ulimit -s unlimited and then launch your code within the same terminal. You can also use compilation option -fmax-stack-var-size=n with the good value of n to set the size of the stack such that it fits your data.

Also, I suggest calculating your sum with using a reduction (+:sum) clause instead of declaring a cretical region that is ineficient and avoidable in this case.

I hope that this helps you.

Upvotes: 1

Related Questions