module library use omp_lib!, only: & !omp_get_thread_num, & !omp_get_num_thread use, intrinsic:: iso_c_binding, only: & c_int use types, only: & int32, real64 implicit none interface subroutine usleep(us) bind (C) import c_int integer(c_int), value :: us end subroutine usleep end interface contains subroutine hello() implicit none integer(kind=int32) :: & nthreads, tid real(kind=real64) :: & r, t ! Fork a team of threads giving them their own copies of variables !$OMP PARALLEL PRIVATE(NTHREADS, TID, R, T) ! Obtain thread number tid = omp_get_thread_num() call random_number(r) call usleep(int(r * 1.d6)) t = r print *, 'Hello World from thread = ', 10+tid, ', t = ', t call random_number(r) call usleep(int(r * 1.d6)) t = t + r print *, 'Hello Planet from thread = ', 10+tid, ', t = ', t call random_number(r) call usleep(int(r * 1.d6)) t = t + r print *, 'I am done from thread = ', 10+tid, ', t = ', t ! Only master thread does this if (tid == 0) THEN nthreads = omp_get_num_threads() PRINT *, 'Number of threads = ', nthreads END IF ! All threads join master thread and disband !$OMP END PARALLEL PRINT *, 'All done.' end subroutine hello end module library