distribute.f90 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 distribute
  10. use qmpi
  11. !
  12. ! public stuff
  13. !
  14. integer, public :: my_number_of_iterations, my_first_iteration, my_last_iteration
  15. integer, dimension(:), allocatable, public :: number_of_iterations, first_iteration, last_iteration
  16. integer, dimension(:), allocatable, public :: randommap
  17. contains
  18. subroutine distribute_iterations(nz)
  19. implicit none
  20. integer, intent(in) :: nz
  21. integer :: i, j
  22. real(8) :: num_procs_real, mean_iterations
  23. if (.not. allocated(number_of_iterations)) then
  24. allocate(number_of_iterations(qmpi_num_proc))
  25. end if
  26. if (.not. allocated(first_iteration)) then
  27. allocate(first_iteration(qmpi_num_proc))
  28. end if
  29. if (.not. allocated(last_iteration)) then
  30. allocate(last_iteration(qmpi_num_proc))
  31. end if
  32. if (master) then
  33. print *, 'Distribution of iterations:'
  34. end if
  35. num_procs_real = qmpi_num_proc
  36. mean_iterations = nz / num_procs_real
  37. j = -1
  38. if (int(mean_iterations) .eq. mean_iterations) then
  39. my_number_of_iterations = nz/qmpi_num_proc
  40. if (master) then
  41. number_of_iterations(:) = nz / qmpi_num_proc
  42. print *, 'All procs get ', number_of_iterations(1), 'iterations'
  43. endif
  44. j = qmpi_num_proc
  45. else
  46. do i = 1, qmpi_num_proc
  47. if (i * floor(mean_iterations) +&
  48. (qmpi_num_proc-i) * ceiling(mean_iterations) .eq. nz) then
  49. j = i
  50. exit
  51. endif
  52. end do
  53. if (qmpi_proc_num + 1 .le. j) then
  54. my_number_of_iterations = floor(mean_iterations)
  55. else
  56. my_number_of_iterations = ceiling(mean_iterations)
  57. endif
  58. if (master) then
  59. number_of_iterations(1:j) = floor(mean_iterations)
  60. number_of_iterations(j+1:qmpi_num_proc) = ceiling(mean_iterations)
  61. if ((j * floor(mean_iterations) +&
  62. (qmpi_num_proc - j) * ceiling(mean_iterations)) .ne. nz) then
  63. print *, 'ERROR in distribute_iteration()'
  64. stop
  65. endif
  66. if (nz .lt. qmpi_num_proc) then
  67. print *, 'Number of cells in z-direction than number of processors'
  68. stop
  69. endif
  70. endif
  71. endif
  72. if (master) then
  73. first_iteration(1) = 1;
  74. last_iteration(1) = number_of_iterations(1)
  75. do i = 2, qmpi_num_proc
  76. first_iteration(i) = last_iteration(i - 1) + 1
  77. last_iteration(i) = first_iteration(i) + number_of_iterations(i)-1
  78. end do
  79. endif
  80. if (qmpi_proc_num + 1 .le. j) then
  81. my_first_iteration = qmpi_proc_num*my_number_of_iterations + 1
  82. else
  83. my_first_iteration = j * (my_number_of_iterations - 1) +&
  84. (qmpi_proc_num - j) * my_number_of_iterations + 1
  85. endif
  86. my_last_iteration = my_first_iteration + my_number_of_iterations - 1
  87. print *, 'I am', qmpi_proc_num, ', my_first_ind =', my_first_iteration,&
  88. ', my_last_ind =', my_last_iteration
  89. end subroutine distribute_iterations
  90. end module distribute