1

我已经实现了使用 MKL VSL 库生成随机数向量的代码:

! ifort -mkl test1.f90 -cpp -openmp

include "mkl_vsl.f90"

#define ITERATION 1000000
#define LENGH 10000

program test
use mkl_vsl_type
use mkl_vsl
use mkl_service
use omp_lib
implicit none 

integer i,brng, method, seed, dm,n,errcode
real(kind=8) r(LENGH) , s
real(kind=8) a, b, start,endd
TYPE (VSL_STREAM_STATE) :: stream
integer(4) :: nt

!     ***** 

brng   = VSL_BRNG_SOBOL
method = VSL_RNG_METHOD_UNIFORM_STD
seed = 777

a = 0.0
b = 1.0
s = 0.0

!call omp_set_num_threads(4)
call omp_set_dynamic(0)
nt = omp_get_max_threads()

!     ***** 

print *,'max OMP threads number',nt

if (1 == omp_get_dynamic()) then
  print '(" Intel OMP may use less than "I0" threads for a large problem")', nt
else
  print '(" Intel OMP should use "I0" threads for a large problem")', nt
end if

if (1 == omp_get_max_threads()) print *, "Intel MKL does not employ threading" 

!call mkl_set_num_threads(4)
call mkl_set_dynamic(0)
nt = mkl_get_max_threads()

print *,'max MKL threads number',nt

if (1 == mkl_get_dynamic()) then
  print '(" Intel MKL may use less than "I0" threads for a large problem")', nt
else
  print '(" Intel MKL should use "I0" threads for a large problem")', nt
end if

if (1 == mkl_get_max_threads()) print *, "Intel MKL does not employ threading"      

!     ***** Initialize *****

      errcode=vslnewstream( stream, brng,  seed )

!     ***** Call RNG *****

start=omp_get_wtime()

do i=1,ITERATION 
      errcode=vdrnguniform( method, stream, LENGH, r, a, b ) 
      s = s + sum(r)/LENGH
end do      

endd=omp_get_wtime()    

!     ***** DEleting the stream *****      

      errcode=vsldeletestream(stream)

!     ***** 

print *, s/ITERATION, endd-start

end program test

例如,使用 4 和 32 线程时,我看不到任何加速。
我使用英特尔编译器版本 13.1.3 并编译

ifort -mkl test1.f90 -cpp -openmp

这就像随机数不是并行生成的。
这里有什么提示吗?

谢谢,

埃里克。

4

1 回答 1

3

您的代码不包含任何 OpenMP 指令来实际并行化工作,当它执行时它只运行 1 个线程。use omp_lib仅仅分散一些对诸如周围的函数的调用是不够的omp_get_wtime,您实际上必须插入一些工作共享指令。

如果我按原样运行您的代码,我的性能监视器显示只有一个线程处于活动状态,并且您的代码报告

 max OMP threads number 16
 Intel OMP should use 16 threads for a large problem
 max MKL threads number 16
 Intel MKL should use 16 threads for a large problem
 0.499972674509302 11.2807227574035

如果我只是将循环包装在 OpenMP 工作共享指令中,就像这样

!$omp parallel do
do i=1,ITERATION 
      errcode=vdrnguniform( method, stream, LENGH, r, a, b ) 
      s = s + sum(r)/LENGH
end do      
!$omp end parallel do

然后我的双四核超线程 PC 上的性能监视器显示 16 个线程处于活动状态并且您的程序报告

 max OMP threads number 16
 Intel OMP should use 16 threads for a large problem
 max MKL threads number 16
 Intel MKL should use 16 threads for a large problem
 0.380979220384302 7.17352125150956

我想我会提供的提示是:学习您最喜欢的 OpenMP 教程,特别是涵盖并行执行指令的部分。我不保证我所做的简单修改不会破坏您的程序;特别是我不保证我没有引入竞争条件。

我让你确定从 1 到 16(超)线程的加速是否可以接受,以及为什么它看起来如此温和的任何分析。

于 2013-12-13T11:56:53.837 回答