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

Error compiling using openMP threadprivate feature in Fortran

Rémy_M
Beginner
556 Views

Hello,

I encounter an error when compiling my software when activating openMP. This is a small program showing what I want to do :

program test_threadsafety

  use omp_lib

  implicit none

  integer :: i, nb

  nb = 20

  !$omp parallel default(shared)
  !$omp do schedule(runtime)
  do i = 1, nb
    call make_save_variable(i)
  end do
  !$omp end do
  !$omp end parallel


contains

  subroutine make_save_variable(i)
    implicit none
    integer, intent(in) :: i
    !$omp threadprivate(thread_id)
    integer, save :: thread_id = -1

    if( thread_id == -1 ) then
      print *,'getting thread id '
      thread_id = omp_get_thread_num()
    end if

    print *, 'in thread ', thread_id, ' iterator : ', i

  end subroutine

end program

I have to say that it works fine with gfortran.... I run a 17.0.4 version of ifort.

Is this a bug or am I doing something wrong ?

Thanks for the help...

0 Kudos
1 Solution
Juergen_R_R
Valued Contributor I
556 Views

Also the NAG compiler has no problem with the code. It looks like that ifort does not like the declaration of thread_id to come after the OMP statement. If I move the declaration up ahead of the OMP directive, the code also compiles with ifort. However, I don't know whether the standard has to say anything about this, but from the logical of the program flow, the declaration should come first. 

View solution in original post

0 Kudos
2 Replies
Juergen_R_R
Valued Contributor I
557 Views

Also the NAG compiler has no problem with the code. It looks like that ifort does not like the declaration of thread_id to come after the OMP statement. If I move the declaration up ahead of the OMP directive, the code also compiles with ifort. However, I don't know whether the standard has to say anything about this, but from the logical of the program flow, the declaration should come first. 

0 Kudos
Rémy_M
Beginner
556 Views

Thanks for the quick reply and the resolution.

That was a really stupid error from me ! Now that you pointed that out it makes so much sense... I will have to fix some bad programming habit.

 

0 Kudos
Reply