On 03/02/2015 22:25, Michael Black wrote:
Hi Mike and anyone else interested in the issues of multi-threaded 
programming,

Below is a version of Mike's example Fortran code. I have updated it to 
Fortran 90 and rather than just suggesting potential fixes, I have 
actually tested the code as well ;)

I have also written it in a way that it also runs without OpemMP i.e. 
single threaded by simply not passing the '-fopenmp' flag to the compiler.

For the single threaded version:

$ gfortran -o workshare1 workshare1.f90

and for the multi-threaded version:

$ gfortran -fopenmp -o workshare1_mt workshare1.f90

The source of the hang on exit is something to do with the Fortran I/O 
system after it has been used from multiple threads. Possibly a defect 
but by simply writing something to the console after all the threads are 
joined, the program no longer intermittently hangs on exit.

+++++++++++++++++++++++++++++++++++++++++++
program workshare1

   implicit none

   !$ interface
   !$  integer function omp_get_num_threads ()
   !$  end function
   !$  integer function omp_get_thread_num ()
   !$  end function
   !$ end interface

   !$ integer :: tid, CHUNKSIZE, chunk
   integer :: N, i
   parameter (N=100)
   !$ parameter (CHUNKSIZE=10)
   real, dimension (N) :: a, b, c

   !     Some initializations
   do i = 1, N
      a(i) = i * 1.0
      b(i) = a(I)
   end do
   !$ chunk = CHUNKSIZE

   !$omp parallel shared(a,b,c) private(i,tid)

   !$ tid = omp_get_thread_num ()
   !$omp critical(io)
   !$ if (tid .eq. 0) then
   !$   print *, 'Number of threads =', omp_get_num_threads ()
   !$ end if
   !$ print *, 'Thread', tid, ' starting...'
   !$omp end critical(io)

   !$omp do schedule(dynamic,chunk)
   do i = 1, N
      c(i) = a(i) + b(i)
      !$omp critical(io)
      !$ write(*,100,advance='no') tid
      write(*,101) i, c(i)
      !$omp end critical(io)
100  format(' Thread', i2, ':')
101  format(' C(', i3,')=', F8.2)
   end do
   !$omp end do nowait

   !$omp critical(io)
   !$ print *, 'Thread', tid, ' done.'
   !$omp end critical(io)

   !$omp end parallel

   print *, 'All done'

end program workshare1
-------------------------------------------------------

73
Bill
G4WJS.

------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
wsjt-devel mailing list
wsjt-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wsjt-devel

Reply via email to