Developer Reference for Intel® oneAPI Math Kernel Library for Fortran

ID 766686
Date 10/31/2024
Public
Document Table of Contents

VS RNG Usage ModelIntel® oneMKL RNG Usage Model

A typical algorithm for VSoneMKL random number generators is as follows:

  1. Create and initialize stream/streams. Functions vslNewStream, vslNewStreamEx, vslCopyStream, vslCopyStreamState, vslLeapfrogStream, vslSkipAheadStream, vslSkipAheadStreamEx.

  2. Call one or more RNGs.

  3. Process the output.

  4. Delete the stream or streams with the function vslDeleteStream.

NOTE:

You may reiterate steps 2-3. Random number streams may be generated for different threads.

The following example demonstrates generation of a random stream that is output of basic generator MT19937. The seed is equal to 777. The stream is used to generate 10,000 normally distributed random numbers in blocks of 1,000 random numbers with parameters a = 5 and sigma = 2. Delete the streams after completing the generation. The purpose of the example is to calculate the sample mean for normal distribution with the given parameters.

Example of VS RNG Usage

      include 'mkl_vsl.f90'
 
      program MKL_VSL_GAUSSIAN
 
      USE MKL_VSL_TYPE
      USE MKL_VSL
 
      real(kind=8) r(1000)  ! buffer for random numbers
      real(kind=8) s        ! average
      real(kind=8) a, sigma ! parameters of normal distribution
 
      TYPE (VSL_STREAM_STATE) :: stream
 
      integer(kind=4) errcode
      integer(kind=4) i,j
      integer brng,method,seed,n
 
      n = 1000
      s = 0.0
      a = 5.0
      sigma  = 2.0
      brng=VSL_BRNG_MT19937
      method=VSL_RNG_METHOD_GAUSSIAN_ICDF
      seed=777
 
!     ***** Initializing *****
      errcode=vslnewstream( stream, brng,  seed )
 
!     ***** Generating *****
      do i = 1,10
          errcode=vdrnggaussian( method, stream, n, r, a, sigma )
          do j = 1, 1000
              s = s + r(j)
          end do
      end do
 
      s = s / 10000.0
 
!     ***** Deinitialize *****
      errcode=vsldeletestream( stream )
 
!     ***** Printing results *****
      print *,"Sample mean of normal distribution = ", s
 
      end

Additionally, examples that demonstrate usage of VS random number generators are available in:

${MKL}/examples/vslf/source