Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
72 Views

Error compiling using openMP threadprivate feature in Fortran

Jump to solution

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

Accepted Solutions
Highlighted
Valued Contributor I
72 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
Highlighted
Valued Contributor I
73 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
Highlighted
Beginner
72 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