首页 > 解决方案 > 将功能分配给多个任务

问题描述

我想使用 OpenMP 将子例程分配给不同的任务。在我的代码中,我实现了这个:

!$omp parallel
!$omp single
   do thread = 1, omp_get_num_threads()
       !$omp task
            write(*,*) "Task,", thread, "is computing"
            call find_pairs(me, thread, points)
            call count_neighbors(me, thread, neighbors(:, thread))
       !$omp end task
   end do
!$omp end single
!$omp end parallel

子程序 find_neighbors 和 count_neighbors 进行一些计算。我之前在我的程序中设置了线程数:

nr_threads = 4
call omp_set_num_threads(nr_threads)

用 GNU Fortran (Ubuntu 8.3.0-6ubuntu1) 8.3.0 编译它并运行,只给了我一个线程,当用 top 监视时,运行速度接近 100%。尽管如此,它打印的权利

任务,1是计算

任务,2是计算

任务,3是计算

任务,4是计算

我使用以下方法编译它:

gfortran -fopenmp main.f90 -o program

我想要的是根据OpenMP线程的数量分配子程序的不同调用,并行工作。

据我了解,创建了一个线程来创建不同的任务。

标签: parallel-processingfortranopenmp

解决方案


推荐阅读