空闲线程,而新线程可以分配给嵌套循环 [英] Idle threads while new threads can be assigned to a nested loop

查看:166
本文介绍了空闲线程,而新线程可以分配给嵌套循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个嵌套循环:

 !$ omp parallel 
!$ omp do
do i = 1,4
...
!$ omp parallel
!$ omp do
do j = 1,4
call job(i,j)

我的电脑可以并行运行四个线程。对于外部循环,创建这样的四个线程。由于 i = 4 job 的费用是前者的四倍,所以快速完成。



现在我预计在内部并行区域,新线程共享工作。但是这种情况不会发生:CPU负载保持在1/4,就好像第四个线程在内部循环上串行工作一样。



如何分配并行CPU时间到内部并行循环?

解决方案



 !$ omp parallel do collapse(2)
do i = 1,4
do j = 1,4
call job(i ,j)
end do
end do

它应该表现得更好。 / p>

I have two nested loops:

!$omp parallel 
!$omp do
do i=1,4
  ...
  !$omp parallel 
  !$omp do
  do j=1,4
    call job(i,j)

My computer can run four threads in parallel. For the outer loop such four threads are created. The first three finish quickly since for i=4, the job is four times more expensive.

Now I expect that in the inner parallel region, new threads share the work. But this doesn't happen: The CPU load stays at 1/4, just as if the 4th thread works serially on the inner loop.

How can I allocate parallel CPU time to the inner parallel loop?

解决方案

Did you try the following approach?

!$omp parallel do collapse(2)
do i = 1,4
do j = 1,4
    call job(i,j)
end do
end do

It should behave better.

这篇关于空闲线程,而新线程可以分配给嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆