Software Archive
Read-only legacy content
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
17060 Discussions

Problem using MKL random number generation in offloaded code

Dean_B_
Beginner
659 Views

I am trying to create a separate random number generator for each thread in a parallel section of code.

.The following is a simple toy example of what is desired. However it does not work.

      include 'mkl_vsl.f90'
 
      program uniform
      USE MKL_VSL_TYPE
      USE MKL_VSL
 
      real(kind=4) r(1000)  ! buffer for random numbers
      real(kind=4) a, b
 
      TYPE (VSL_STREAM_STATE) :: stream
 
      integer(kind=4) errcode
      integer(kind=4) i,tid
      integer brng,method,seed,n
      n = 1000
      a = 0.0
      b = 1.0
      brng=VSL_BRNG_MT19937
      method=VSL_RNG_METHOD_UNIFORM_STD
     !dir$ attributes offload:mic :: omp_get_thread_num,errcode

     !dir$ omp offload target(mic)
     !$omp parallel private(errcode,tid,n,i)   
      tid = omp_get_thread_num()
      seed=777 * tid
 
!     ***** Initializing *****
      errcode=vslnewstream(stream, brng, seed)
 
!     ***** Generating *****
          errcode=vsrnguniform( method, stream, n, r, a, b )
          do i=1,10
              print *, tid, r(i)
          end do
 
 
!     ***** Deinitialize *****
      errcode=vsldeletestream( stream )
    !$omp end parallel
 
      end

 

These errors are reported by the compliler

Error    1     error #8535: A procedure called in an OFFLOAD region must have the OFFLOAD:TARGET attribute.   [VSLNEWSTREAM]  

Error    2     error #8535: A procedure called in an OFFLOAD region must have the OFFLOAD:TARGET attribute.   [VSRNGUNIFORM] 

Error    3     error #8535: A procedure called in an OFFLOAD region must have the OFFLOAD:TARGET attribute.   [VSLDELETESTREAM]   

However I can not find a way to set the offload target attribute for these functions.
.Presumably this would need to be done in the associated module(s), but I do not have access to their source.

Any ideas on how to accomplish this?

Thanks, Dean

 

 

 

0 Kudos
3 Replies
Kevin_D_Intel
Employee
659 Views

You would compile your example as follows:

ifort -offload-attribute-target=mic -openmp -mkl example.f90

This decorates the subprogram definitions within the mkl_vsl.f90 include.

I'm not 100% certain about the correctness of calling the MKL routines within the OpenMP region. At least stream, seed, and r appear to need to be private and then the example at least compiles and executes. I’ll contact someone more knowledgeable about MKL to assist with using the MKL routines w/OpenMP.

0 Kudos
Kevin_D_Intel
Employee
659 Views

The problem I was uncertain about appears to be addressed by adding the declaration:

integer :: omp_get_thread_num

I also added an !$omp barrier ahead of the do i=1,10 and forgot to mention in my previous reply that I removed n from private.

Here's the complete test case:

      include 'mkl_vsl.f90'

      program uniform
      USE MKL_VSL_TYPE
      USE MKL_VSL

      integer :: omp_get_thread_num

      real(kind=4) r(1000)  ! buffer for random numbers
      real(kind=4) a, b

      TYPE (VSL_STREAM_STATE) :: stream

      integer(kind=4) errcode
      integer(kind=4) i,tid
      integer brng,method,seed,n
      n = 1000
      a = 0.0
      b = 1.0
      brng=VSL_BRNG_MT19937
      method=VSL_RNG_METHOD_UNIFORM_STD
     !dir$ attributes offload:mic :: omp_get_thread_num,errcode

     !dir$ omp offload target(mic)
     !$omp parallel private(errcode,tid,i,seed,r,stream)
      tid = omp_get_thread_num()
      seed=777 * tid

!     ***** Initializing *****
      errcode=vslnewstream(stream, brng, seed)

!     ***** Generating *****
      errcode=vsrnguniform( method, stream, n, r, a, b )

      !$omp barrier
          do i=1,10
              print *, tid, r(i)
          end do

!     ***** Deinitialize *****
      errcode=vsldeletestream( stream )
     !$omp end parallel

      end

 

0 Kudos
Andrey_N_Intel
Employee
659 Views

Additionally, it makes sense to have a quick look at Intel(R) MKL examples that demonstrate use of RNGs in the offload mode. The examples are available in the folder $MKLROOT/examples/mic_offload/vslc. While the examples are in C, they give the idea.

We do not recommend to use MT19937 in parallel computations in the format shown in the example above, as use of different seeds in different threads does not look the best approach. In the latest version of the library we support Skipahead feature for this generator which allows using MT19937 in parallel computations. Alternatively, Intel(R) MKL provides another MT algorithm which supports parallel computations, MT2203 BRNG. This generator allows creating up to 6024 independent random number streams. See VSL Notes at http://software.intel.com/sites/products/documentation/doclib/mkl_sa/111/vslnotes/index.htm for additional details. I suggest to run experiments with MT2203.

Another comment is about array of the results, r. Depending on how you are going to use the output of random number generation, you might want to save the results in the local buffers and process them locally as shown in the example above. Alternatively, you can store random numbers into the common buffer (different threads stores random numbers in the different areas of the memory r). In this case r would be shared between threads.

Andrey

 

 

 

0 Kudos
Reply