m_set_random_seed2.F90 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. module m_set_random_seed2
  2. contains
  3. subroutine set_random_seed1
  4. ! Sets a random seed based on the system and wall clock time
  5. ! Used to work on IBM Regatta Power 4 ("TRE") but not on Cray XE6m ("Hexagon")
  6. ! Where it always returned zero.
  7. #if defined (QMPI)
  8. use qmpi
  9. #else
  10. use qmpi_fake
  11. #endif
  12. implicit none
  13. integer , dimension(8)::val
  14. integer cnt
  15. integer sze
  16. integer, allocatable, dimension(:):: pt
  17. #if defined (QMPI)
  18. integer :: q
  19. #endif
  20. call DATE_AND_TIME(values=val)
  21. !if(master)print*,'TIME', val
  22. call SYSTEM_CLOCK(count=cnt)
  23. !if(master)print*,'CLOCK', cnt
  24. call RANDOM_SEED(size=sze)
  25. !if(master)print*,'SEED', sze
  26. allocate(pt(sze))
  27. pt(1) = val(8)*val(3)
  28. pt(2) = cnt
  29. ! KAL --- spread random seed to tiles, this makes sure that m_sample2D
  30. ! KAL --- produces the same perturbations across processes
  31. #if defined (QMPI)
  32. if (master) then
  33. do q=2,qmpi_num_proc
  34. call send(pt,q-1)
  35. end do
  36. else
  37. call receive(pt,0)
  38. end if
  39. #endif
  40. call RANDOM_SEED(put=pt)
  41. !if(master)print*,'RANDOM SEED', pt
  42. deallocate(pt)
  43. end subroutine set_random_seed1
  44. ! --- Sets a random seed based on the wall clock time
  45. ! ES: Tested and approved on Cray
  46. subroutine set_random_seed2
  47. #if defined (QMPI)
  48. use qmpi
  49. #else
  50. use qmpi_fake
  51. #endif
  52. implicit none
  53. integer , dimension(8)::val
  54. integer cnt,q
  55. integer sze
  56. ! --- Arrays for random seed
  57. integer, allocatable, dimension(:):: pt
  58. real , allocatable, dimension(:):: rpt
  59. !
  60. call DATE_AND_TIME(values=val)
  61. if (sum(val) == 0) then
  62. print*, "Check that date_and_time is available on your computer"
  63. call stop_mpi
  64. endif
  65. call RANDOM_SEED(size=sze)
  66. allocate(pt(sze))
  67. allocate(rpt(sze))
  68. ! --- Init - assumes seed is set in some way based on clock,
  69. ! --- date etc. (not specified in fortran standard). Sometimes
  70. ! --- this initial seed is just set every second
  71. call RANDOM_SEED
  72. ! --- Retrieve initialized seed. val(8) is milliseconds -
  73. call RANDOM_SEED(GET=pt)
  74. ! --- this randomizes stuff if random_seed is not updated often
  75. ! --- enough. synchronize seed across tasks (needed if pseudo
  76. ! --- is paralellized some day)
  77. rpt = pt * (val(8)-500)
  78. #if defined (QMPI)
  79. if (master) then
  80. do q=2,qmpi_num_proc
  81. call send(rpt,q-1)
  82. end do
  83. else
  84. call receive(rpt,0)
  85. end if
  86. #endif
  87. pt=int(rpt)
  88. call RANDOM_SEED(put=pt)
  89. deallocate( pt)
  90. deallocate(rpt)
  91. end subroutine set_random_seed2
  92. end module m_set_random_seed2