Fortran:openmp笔记8

Program test_omp_in_parallel
  use omp_lib
  implicit none
  Integer :: tid, Nthreads

  Nthreads = omp_get_num_threads()  !.. 获得串行域线程数,Nthreads =1,只有主线程一个线程
  tid = omp_get_thread_num()  !.. 获得当前线程号,主线程号一般为0
  If ( omp_in_parallel() ) then
    write( *,'(1x,a,g0)' ) 'In the parallel region!, id = ', tid
    write( *,'(1x,a,g0)' ) 'number of threads:', Nthreads
  else
    write( *,'(1x,a,g0)' ) 'In the serial region!, id = ', tid
    write( *,'(1x,a,g0)' ) 'number of threads:', Nthreads
  End if
  write( *,'(1x,a)' ) '------Before parallel region------'

  !$omp parallel private( tid, Nthreads )
  Nthreads = omp_get_num_threads()  !.. 获得并行域线程数,Nthreads为默认的机器线程
  tid = omp_get_thread_num()  !.. 获得当前线程号
  If ( omp_in_parallel() ) then
    write( *,'(1x,a,g0)' ) 'In the parallel region!, id = ', tid
    write( *,'(1x,a,g0)' ) 'number of threads:', Nthreads
  else
    write( *,'(1x,a,g0)' ) 'In the serial region!, id = ', tid
    write( *,'(1x,a,g0)' ) 'number of threads:', Nthreads
  End if
  !$omp end parallel
  write( *,'(1x,a)' ) '------After parallel region------'

  Nthreads = omp_get_num_threads()  !.. 获得串行域线程数,Nthreads =1,只有主线程一个线程
  tid = omp_get_thread_num()  !.. 获得当前线程号,主线程号一般为0
  If ( omp_in_parallel() ) then
    write( *,'(1x,a,g0)' ) 'In the parallel region!, id = ', tid
    write( *,'(1x,a,g0)' ) 'number of threads:', Nthreads
  else
    write( *,'(1x,a,g0)' ) 'In the serial region!, id = ', tid
    write( *,'(1x,a,g0)' ) 'number of threads:', Nthreads
  End if
  write( *,'(1x,a)' ) '------Before parallel region------'

End program test_omp_in_parallel
!.. 上述程序有如下特点
!.. 1. parallel指令对之前的代码段采用单线程串行执行,线程号为0
!.. 2. 子线程的执行次序是随机的
!.. 3. parallel指令对之后的代码段采用单线程穿行执行,线程号为0
!.. 4. 采用omp_get_num_threads函数获得程序执行过程中(串行域为1,并行域如是默认,则线程数为机器总线程数)
!.. 5. 采用omp_get_thread_num函数获得目前正在执行的子线程的线程号
!.. 6. 采用omp_in_parallel函数来检测正在执行的代码是在串行域还是在并行域

!.. 线程数为默认模式的优缺点
!.. 优点
!.. 1. 扩展性好,能充分利用机器的性能

!.. 缺点
!.. 1. 如果并行的执行结果依赖于线程的数量和线程号,那么默认模式就会给出错误的结果
!.. 2. 如果多用户同时使用服务器,采用默认模式就会抢占资源,影响其他用户
!.. 3. 如果计算负载小,线程过多有时会造成实际计算时间的延长。所以并不是线程数越多越好,需要根据自己的计算量适量选择线程数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值