Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
7227 Discussions

Passing random number streams (state) to subroutines in Fortran

Henrik_S_
Beginner
1,894 Views

I can compile and run Fortran 90 code with MKL random number generators. I come from a Fortran 77 background , and I am struggling to figure out how to pass the MKL random number generator info to subroutines. I want to setup the random number generator at the beginning of the program and then I have several subroutines that generate random numbers. Is there any example documentation anywhere to show how this is done (or and example program)? I could not find any in the MKL documentation.

- Henrik

0 Kudos
12 Replies
Gennady_F_Intel
Moderator
1,894 Views

Hello Henrik,

The standard MKL pakage contains examples for the mostly part of MKL functionality.

Please look at the following folder ..examplesvslf. You can find there a lot of Fortran examples about VSL functionality.

Regards,

Gennady

0 Kudos
Andrey_N_Intel
Employee
1,894 Views

In addition to VSL examples available in MKL installation package the example below also demonstrates how random number generation could be done in a function whose parameter is VSL random stream. First, we initialize VSL Random stream in usual way:

include 'mkl_vsl.fi'
program MKL_VSL_TEST

USE MKL_VSL_TYPE
USE MKL_VSL

integer n
integer(kind=4) errcode
real(kind=8) a,sigma
real(kind=8) r(1000)
integer brng,method,seed
TYPE (VSL_STREAM_STATE) :: stream

n=1000
brng=VSL_BRNG_MCG31
method=VSL_METHOD_DGAUSSIAN_BOXMULLER
seed=1

a=0.0
sigma=1.0

errcode=vslnewstream( stream, brng, seed )

Instead of direct call to VSL vdrnggaussian function we make call into its wrapper and pass stream and other generation parameters into the function:

errcode=vslgaussian( stream, method, n, r, a, sigma )

Computation is completed with de-allocation of system resources:

errcode=vsldeletestream( stream )

end program MKL_VSL_TEST

Finally, we define function vslgaussian:

function vslgaussian( rstr, meth, nn, rr, aa, sig )
USE MKL_VSL_TYPE
USE MKL_VSL

TYPE (VSL_STREAM_STATE), intent(in) :: rstr
integer, intent(in) :: meth
integer(kind=4), intent(in) :: nn
real(kind=8), intent(out) :: rr(nn)
real(kind=8), intent(in) :: aa
real(kind=8), intent(in) :: sig

vslgaussian=vdrnggaussian( meth, rstr, nn, rr, aa, sig )
end function vslgaussian
< /P>

Please, let us know how this works for you. Thanks, Andrey

0 Kudos
Henrik_S_
Beginner
1,894 Views

Andrey - Thank You!!! This is exactly what I was looking for. I tested your example solution and it works for me.

- Henrik
0 Kudos
Henrik_S_
Beginner
1,894 Views
Hello Gennady,
thank you for your reply. I was aware of the examples directory and I made use of it to figure out the basics of generating MKL random numbers. I could, however, not find any examples on how to pass streams to subroutines and/or functions.Maybe I missed it. Andrey's post gave me what I needed.

Thanks again!

Sincerely,

- Henrik
0 Kudos
guillaume_c
Beginner
1,894 Views

Hello,

Could you please tell what command line instruction you would use to compile this program? I'm struggling a little with linking... Thanks!

Guillaume

0 Kudos
Gennady_F_Intel
Moderator
1,894 Views

Ok, Please let me know:

What is OS? Win, Lin or Mac OS

Is it ia32 or em64 system?

Static or dynamic?

How about threading?

--Gennady

0 Kudos
guillaume_c
Beginner
1,894 Views

Ok, Please let me know:

What is OS? Win, Lin or Mac OS

Is it ia32 or em64 system?

Static or dynamic?

How about threading?

--Gennady


I'm with Mac OS 10.5.6, and running a Core 2 Duo. I suppose that static linking will make computations faster as the library as to be loaded only one time, and not everytime it is called? I would like to have multi-threading enabled (but I'm not sure where and how this would be used in the sample code above). Thanks.
0 Kudos
Gennady_F_Intel
Moderator
1,894 Views
Is it 32bit or 64bit OS?

0 Kudos
guillaume_c
Beginner
1,894 Views
Is it 32bit or 64bit OS?


I'm not sure how I can check, but it looks 64 bits

http://www.apple.com/macosx/technology/64bit.html
0 Kudos
Andrey_G_Intel2
Employee
1,894 Views
Quoting - guillaume_c

Hello,

Could you please tell what command line instruction you would use to compile this program? I'm struggling a little with linking... Thanks!

Guillaume


You can use following command line for building your example with MKL libraries (MacOS 64bits, Intel Compiler, static linking):
ifort -I$(PATH_TO_MKL_INCLUDE_FOLDER) your_source_name.f $(PATH_TO_MKL_LIB_EM64T_FOLDER)/libmkl_intel_lp64.a $(PATH_TO_MKL_LIB_EM64T_FOLDER)/libmkl_intel_thread.a $(PATH_TO_MKL_LIB_EM64T_FOLDER)/libmkl_core.a $(PATH_TO_MKL_LIB_EM64T_FOLDER)/libmkl_intel_lp64.a $(PATH_TO_MKL_LIB_EM64T_FOLDER)/libmkl_intel_thread.a $(PATH_TO_MKL_LIB_EM64T_FOLDER)/libmkl_core.a -L$(PATH_TO_MKL_LIB_EM64T_FOLDER) -liomp5

You can receive examples of build`s command lines by runningMKL examples. Go to examples folder, enter to one of subfolders and run make. Inside each makefile you can find short description of makefile`s parameters.

Andrey
0 Kudos
ymharshe
Beginner
1,894 Views

Hello all,

I wish to use Intel Fortran Math Kernel Library for generating random vectors (numbers) of multivariance. I have Intel Fortran 10.x with the corresponding MKL installed on a Mac with Mac OS X 10.4 (Tiger 32 bit).I tried using many examples that I found on the net but none of them worked, including the one here! :(

Now, the things I am interested in are the following.

1. Does someone have a working sample code for a configuration like that of mine?

2. Please let me know the commands ( meaning the list of library files and include files) to be compiled with it.

I tried using the command list given in the previous post as

fort -I$/Library/Frameworks/Intel_MKL.framework/Versions/10.0.3.020/include/ g.f /Library/Frameworks/Intel_MKL.framework/Versions/10.0.3.020/lib/universal/libmkl_intel_lp64.a /Library/Frameworks/Intel_MKL.framework/Versions/10.0.3.020/lib/universal/libmkl_intel_thread.a /Library/Frameworks/Intel_MKL.framework/Versions/10.0.3.020/lib/universal/libmkl_core.a /Library/Frameworks/Intel_MKL.framework/Versions/10.0.3.020/lib/universal/libmkl_intel_lp64.a /Library/Frameworks/Intel_MKL.framework/Versions/10.0.3.020/lib/universal/libmkl_intel_thread.a /Library/Frameworks/Intel_MKL.framework/Versions/10.0.3.020/lib/universal/libmkl_core.a -L/Library/Frameworks/Intel_MKL.framework/Versions/10.0.3.020/lib/universal -liomp5

but it it results in the following msg when I try to run a.out

dyld: Library not loaded: libiomp5.dylib
Referenced from: /Users/harshey/Documents/phd_project/model/./a.out
Reason: image not found
Trace/BPT trap

Help required urgently.

Thanks in advance.

Yogesh

0 Kudos
Gennady_F_Intel
Moderator
1,894 Views
Quoting - ymharshe

Yogesh,
I believe the linker adviser ( follow the link: http://software.intel.com/en-us/forums/showthread.php?t=65510) will helps you.
If not, please let us know.
--Gennady

0 Kudos
Reply