Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28634 Discussions

Ifort 15.0.2 segfaults on shared pointer access within OpenMP tasks

Alfredo
Beginner
252 Views

Hello,

I have a problem with OpenMP tasks in the Fortran code below:

program helloworld
  use omp_lib          
  implicit none

  integer, pointer :: a(:)

  allocate(a(40))
  a = 0
  
  !$omp parallel shared(a)
  !$omp master
  call foo(a)
  write(*,*)'out'
  !$omp end master

  !$omp end parallel

  deallocate(a)

contains

  subroutine foo(a)
    use iso_c_binding
    integer :: i, omp_num
    integer, pointer :: a(:)

    do i=1,40
       !$omp task firstprivate(i)
       omp_num = omp_get_thread_num()
       write(*,*) "end", i,omp_num, a(1)
       if(omp_num.eq.1) a(1:40) = omp_num
       !$omp end task
    enddo

  end subroutine foo

end program helloworld


What happens is that the first task executed right after the "out" message is printed segfaults due to an invalid access to the a pointer. It seems that the a pointer passed to tasks goes out of scope when the master thread returns from the foo routine although it shouldn't since a is not a local variable of the foo routine. This code works in gfortran (4.6 and 4.9). Is this code OpenMP compliant or is it a problem in ifort ?

One workaround to this problem is to declare, within foo, a pointer p which points to a and to pass it to the tasks like this:

  subroutine foo(a)
    use iso_c_binding
    integer :: i, omp_num
    integer, pointer :: a(:), p(:)

    p => a
    do i=1,40
       !$omp task firstprivate(i) firstprivate(p)
       omp_num = omp_get_thread_num()
       write(*,*) "end", i,omp_num, p(1)
       if(omp_num.eq.1) p(1:40) = omp_num
       !$omp end task
    enddo

  end subroutine foo

The firstprivate ensures that a private copy of p (i.e., the pointer, not the actual memory area) is passed to every task which prevents the data from going out of scope.

 

Can anyone help?

 

Regards,

Alfredo

 

 

0 Kudos
0 Replies
Reply