# When does Intel MKL perform faster than intrinsic functions?

**URL:** <https://fortran-lang.discourse.group/t/when-does-intel-mkl-perform-faster-than-intrinsic-functions/3111>\
**Category:** Uncategorized\
**Created:** [April 3, 2022, 5:20pm UTC](https://fortran-lang.discourse.group/t/when-does-intel-mkl-perform-faster-than-intrinsic-functions/3111 "2022-04-03T17:20:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![CRquantum](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/crquantum/32/730_2.png) [@CRquantum](https://fortran-lang.discourse.group/u/CRquantum)\
**Post date:** [April 3, 2022, 5:20pm UTC](https://fortran-lang.discourse.group/t/when-does-intel-mkl-perform-faster-than-intrinsic-functions/3111/1 "2022-04-03T17:20:40Z")

</div>

Dear all,

I have tested some functions in Intel MKL using Intel OneAPI 2022.0.3, for arrays and matrices (at the level of about 10^9 elements). Such as MKL’s `vdsin` .vs. intrinsic `sin`, MKL’s `dgemm` and `vdadd` and `domatadd` vs intrinsic `matmul` and `+`.  
However I found it seems MKL’s functions are slower than intrinsic functions.  
A simple example shows MKL’s `vdsin` seems slower than just `sin` is below

> [@Why MKL's vdsin is slower than the intrinsic sin?](https://fortran-lang.discourse.group/t/why-mkls-vdsin-is-slower-than-the-intrinsic-sin/3108/3):
>
> Thanks @septc ! Yeah, uhm, well I just try to do the same thing as @certik did in that thread, his code is, program avx implicit none integer, parameter :: dp = kind(0.d0) real(dp) :: t1, t2, r call cpu\_time(t1) r = f(100000000) call cpu\_time(t2) print \*, "Time", t2-t1 print \*, r contains real(dp) function f(N) result(r) integer, intent(in) :: N integer :: i r = 0 do i = 1, N r = r + sin(real(i,dp)) end do end function end program If I do r = sum(sin(…

On the other hand, I do found MKL’s gaussian random number generator is about 3 times faster than any other fastest random number generator I have ever used (such as the `rnor()` in ziggurat).

I just wonder, guys do you use Intel MKL?  
When does Intel MKL perform faster than intrinsic functions?  
What functions/subroutines in MKL are very fast?  
Thanks much in advance!

PS. A sloppy illustration subroutine about how to use MKL’s gaussian random number is below , in the real code you may need to add a line before the main program, `include 'mkl_vsl.f90' `,

```auto
  subroutine stochastic_rk_init(np,nd,nstep)
  use random
  USE MKL_VSL
  USE MKL_VSL_TYPE
  integer(kind=i8) :: np, nd, nstep, nsize
  integer(kind=4) errcode
  real(kind=8) mean,sigma
  integer :: brng,method,seed
  TYPE (VSL_STREAM_STATE) :: stream
  
  nsize = np*nd*4*nstep ! np is about 10^5, nstep is about 10^3, nd=2.
  if (allocated(normal_1d)) deallocate(normal_1d)   
  allocate(normal_1d(nsize)) 
  
      !brng=VSL_BRNG_MCG31  
      !brng=VSL_BRNG_MRG32k3a    
      !brng=VSL_BRNG_MT19937     
      brng=VSL_BRNG_SFMT19937
      method=VSL_RNG_METHOD_GAUSSIAN_ICDF
      seed=777  
      mean=zero
      sigma=one
      
! *****Initialize*****
      errcode=vslnewstream( stream, brng, seed )  
! *****Call RNG*****
      errcode=vdrnggaussian( method, stream, int(nsize), normal_1d, mean, sigma)  
  
  !normal_1d = gaussian(nsize) ! rnor_vec(nsize)
  normal(1:np,1:nd,1:4,1:nstep) => normal_1d
  
  return
  end subroutine stochastic_rk_init

```
