m_set_random_seed2.f90 2.6 KB

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