distribute.F90 3.0 KB

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