!    Copyright (c) 2014-2015 Alin Marin Elena <alinm.elena@gmail.com>
!    The MIT License http://opensource.org/licenses/MIT
module threads
  use omp_lib, only : omp_get_max_threads, &
    omp_get_thread_num, &
    omp_get_num_threads, &
    openmp_version
  implicit none
  private
  public :: testThreads

contains 
  pure character(len=10) function version(ov)
    integer, intent(in) :: ov

    version="0.0"
    select case(ov)
    case(200011)
      version="2.0"
    case(200505)
      version="2.5"
    case(200805)
      version="3.0"
    case(201107)
      version="3.1"
    case(201307)
      version="4.0"
    case(201511)
      version="4.5"
    end select 
  end function version 

  subroutine testThreads()
    integer         :: tCount
    integer         :: threadsMax
    integer         :: tid

    write(*,'(1x,a)')"Openmp API Version F: "//trim(version(openmp_version))
    write(*,'(1x,a)')"Openmp API Version C: "//trim(version(_OPENMP))
    threadsMax=omp_get_max_threads()
    !$omp parallel default(none) private(tid) shared(tCount,threadsMax)
    tid = omp_get_thread_num()
    !$omp critical
    write(*,'(a,i0)') "Hello from thread id ",tid
    !$omp end critical
    !$omp barrier 
    !$omp master
    tCount = omp_get_num_threads()
    write(*, '(a,i0)') "The number of threads is ", tCount
    write(*, '(a,i0)') "Maximum number of threads is ", threadsMax
    write(*,'(a)') "================================"
    !$omp end master
    !$omp end parallel

  end subroutine testThreads
end module threads

program main
  use threads
  implicit none

  call testThreads()
end program main
