Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

multi-thread random_number

hougj
Beginner
973 Views
hi all,
I'm a rookie and I'm experiencing some problems to get random_number() work with OpenMP, random numbers generated by different processors are usually the same. Here is a sample code demostrated the way I'm using it,
program rand
implicit none
integer :: i,j
real :: rnd(1:24)
integer :: th(1:24)
integer :: omp_get_thread_num
!$omp paralleldo default(none)
do i=1,8 !the mechine I'm using has 8 processors, this is to initialize the RNG
print *,omp_get_thread_num() !to confirm every processor is initialized
call random_seed()
end do
!$OMP PARALLELDO DEFAULT(NONE) PRIVATE(I) SHARED(RND, th)
do i=1,24
call random_number(rnd(i))
end do
do j=1,8
write(*,'(3(f11.9,2x))')rnd((j-1)*3+1:j*3)
end do
end program
This is the result I'm getting:
0
1
2
4
6
3
7
5
0.972639918 0.881229877 0.594391227
0.972639918 0.881229877 0.594391227
0.972639918 0.881229877 0.594391227
0.972639918 0.881229877 0.594391227
0.972639918 0.881229877 0.594391227
0.402969360 0.031995680 0.221246824
0.972639918 0.881229877 0.594391227
0.972639918 0.881229877 0.594391227
I also tried to call random_seed() once or not to call random_seed(), the results were worse..
Thanks in advance.
0 Kudos
3 Replies
Steven_L_Intel1
Employee
973 Views
You should call random_seed only once at the beginning of the program, or not at all. I don't see results like yours - which compiler version are you using?
0 Kudos
hougj
Beginner
973 Views
Steve,
I'm using ifort for Linux 10.1 20070913
If I call random_seed() for once, here is one result:

0.615034342 0.626231551 0.531687737
0.000000392 0.025480442 0.352516145
0.000000392 0.025480442 0.352516145
0.000000392 0.025480442 0.352516145
0.000000392 0.025480442 0.352516145
0.000000392 0.025480442 0.352516145
0.000000392 0.025480442 0.352516145
0.000000392 0.025480442 0.352516145

If I don't call random_seed(), the result will be like this:

0.000000392 0.025480442 0.352516145
0.000000392 0.025480442 0.352516145
0.000000392 0.025480442 0.352516145
0.000000392 0.025480442 0.352516145
0.000000392 0.025480442 0.352516145
0.000000392 0.025480442 0.352516145
0.000000392 0.025480442 0.352516145
0.000000392 0.025480442 0.352516145

Quoting - Steve Lionel (Intel)
You should call random_seed only once at the beginning of the program, or not at all. I don't see results like yours - which compiler version are you using?

0 Kudos
Steven_L_Intel1
Employee
973 Views
Here's what I get using 11.1 with the call to random_seed:

0
0
1
1
2
2
3
3
0.817219734 0.104807064 0.379983515
0.583199739 0.792860270 0.173353985
0.208930358 0.373894215 0.305989832
0.362445623 0.486917466 0.139199391
0.188994333 0.676810563 0.982293963
0.886496782 0.382345438 0.172872305
0.295758396 0.474151313 0.157240897
0.645729303 0.361581087 0.968771875

and without:

0
0
1
1
3
3
2
2
0.963055491 0.382701963 0.775439918
0.777333379 0.946657956 0.711797237
0.686883211 0.710611343 0.315871298
0.089636147 0.204167530 0.367890567
0.000000392 0.025480442 0.352516145
0.666914463 0.313309848 0.915075898
0.919936955 0.455012798 0.597476482
0.179469123 0.906562090 0.750906527


0 Kudos
Reply