我已经实现了使用 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
这就像随机数不是并行生成的。
这里有什么提示吗?
谢谢,
埃里克。