omp节使用私有(num_threads)子句与默认(不带子句)



我用两种情况执行以下代码:

"omp sections&quot美元;和"!$omp sections private(thread_num)">

在这两种情况下,每个部分都由不同的线程完成吗?

program main
use omp_lib
implicit none
integer, parameter:: ma=100, n=10000, mb= 100
real, dimension (ma,n) :: a 
real, dimension (n,mb) :: b
real, dimension (ma,mb) :: c = 0. 
integer:: i,j,k, threads=2, ppt, thread_num
integer:: toc, tic, rate 
real:: time_parallel, time 
call random_number (a) 
call random_number (b)
!/////////////////////// PARALLEL PRIVATE ///////////////////////
c=0
CALL system_clock(count_rate=rate)
call system_clock(tic)
ppt = ma/threads
!$ call omp_set_num_threads(threads)

!$omp parallel
!$omp sections private(thread_num)  !(HERE IS THE QUESTION TOPIC)
! EXAMPLE PROCESS 1 (it is only an example to test 'omp sections')
!$omp section  
!$ thread_num = omp_get_thread_num()
!$ print*, "Section 1 started by thread number:", thread_num
do i= 1,50
do j= 1,mb
do k= 1,n
c(i,j) = c(i,j) + a(i,k)*b(k,j)
end do 
end do
end do 
!$ print*, "Section 1 finished by thread number:", thread_num
! EXAMPLE PROCESS 2
!$omp section  
!$ thread_num = omp_get_thread_num()
!$ print*, "Section 2 started by thread number:", thread_num
do i= 51,100
do j= 1,mb
do k= 1,n
c(i,j) = c(i,j) + a(i,k)*b(k,j)
end do 
end do
end do 
!$ print*, "Section 2 finished by thread number:", thread_num
!$omp end sections
!$omp end parallel
print*, '//////////////////////////////////////////////////////////////'
print*, 'Result in Parallel' 
!$ print*, c(85:90,40)  

call system_clock(toc)
time_parallel = real(toc-tic)/real(rate)
!/////////////////////// normal execution ///////////////////////
c = 0
CALL system_clock(count_rate=rate)
call system_clock(tic)
call system_clock(tic)
do i= 1,ma
do j= 1,mb
do k= 1,n
c(i,j) = c(i,j) + a(i,k)*b(k,j)
end do 
end do
end do 


call system_clock(toc)
time =  real(toc-tic)/real(rate)
print*, 'Result in serial mode'
print*, c(85:90,40)  
print*, '------------------------------------------------'
print*, 'Threads: ', threads, '|  Time Parallel ', time_parallel, 's '
print*, '                         Time Normal  ', time, 's'
!----------------------------------------------------------------
end program main

这是"!的结果。omp sections&quot美元;和"!$omp sections private(thread_num)"

第1段按线程号启动:1

第2段由线程号开始:1

第1段按螺纹号完成:1

第2段按螺纹号完成:1

//////////////////////////////////////////////////////////////

Result in Parallel

2507.23853 2494.16162 2496.83960 2503.58960 2509.34448
2518.64160

结果为串行模式

2507.23853 2494.16162 2496.83960 2503.58960 2509.34448
2518.64160

Threads: 2 | Time Parallel

Time Normal 0.605000019 s


Section 1由线程号:0启动

第2段由线程号开始:1

第1段按螺纹号完成:0

第2段按螺纹号完成:1

//////////////////////////////////////////////////////////////

Result in Parallel

2523.38281 2501.28369 2517.81860 2502.66235 2503.13940
2532.35791

结果为串行模式

2523.38281 2501.28369 2517.81860 2502.66235 2503.13940
2532.35791

Threads: 2 | Time Parallel 0.432999998

时间正常0.610204018 s


编译使用:

gfortran -Wall -fopenmp -O2 -Wall -o prog.exe prueba.f90。/prog.exe

我笔记本电脑的CPU型号:

AMD A6-6310(4核,每核一个线程)

p。S:主要目的是测试并行子句是否加速矩阵计算

thread_num绝对应该是私有变量。否则,两个线程使用相同的变量,因此您从两个线程获得值1。从两个线程写入同一个变量是一个竞争条件。

您可以将整个并行区域设置为私有,并且仅在区域开始时调用omp_get_thread_num()一次。

!$omp parallel private(thread_num)
!$ thread_num = omp_get_thread_num()
!$omp sections 
!$omp section
!$ print*, "Section 1 started by thread number:", thread_num
...

相关内容

  • 没有找到相关文章

最新更新