@@ -754,15 +754,37 @@ end subroutine normalize_ao
754754
755755 subroutine calculate_work ()
756756
757- ! Determine maximum amount of particles to simulate on each processor
758- maxwork = ceiling (real (n_particles)/ n_procs,8 )
757+ integer :: i ! loop index
758+ integer :: remainder ! Number of processors with one extra particle
759+ integer (8 ) :: i_bank ! Running count of number of particles
760+ integer (8 ) :: min_work ! Minimum number of particles on each proc
761+ integer (8 ) :: work_i ! Number of particles on rank i
762+
763+ allocate (work_index(0 :n_procs))
764+
765+ ! Determine minimum amount of particles to simulate on each processor
766+ min_work = n_particles/ n_procs
767+
768+ ! Determine number of processors that have one extra particle
769+ remainder = int (mod (n_particles, int (n_procs,8 )), 4 )
770+
771+ i_bank = 0
772+ work_index(0 ) = 0
773+ do i = 0 , n_procs - 1
774+ ! Number of particles for rank i
775+ if (i < remainder) then
776+ work_i = min_work + 1
777+ else
778+ work_i = min_work
779+ end if
759780
760- ! ID's of first and last source particles
761- bank_first = rank* maxwork + 1
762- bank_last = min ((rank+1 )* maxwork, n_particles)
781+ ! Set number of particles
782+ if (rank == i) work = work_i
763783
764- ! number of particles for this processor
765- work = bank_last - bank_first + 1
784+ ! Set index into source bank for rank i
785+ i_bank = i_bank + work_i
786+ work_index(i+1 ) = i_bank
787+ end do
766788
767789 end subroutine calculate_work
768790
@@ -775,7 +797,7 @@ subroutine allocate_banks()
775797 integer :: alloc_err ! allocation error code
776798
777799 ! Allocate source bank
778- allocate (source_bank(maxwork ), STAT= alloc_err)
800+ allocate (source_bank(work ), STAT= alloc_err)
779801
780802 ! Check for allocation errors
781803 if (alloc_err /= 0 ) then
@@ -784,7 +806,7 @@ subroutine allocate_banks()
784806 end if
785807
786808 ! Allocate fission bank
787- allocate (fission_bank(3 * maxwork ), STAT= alloc_err)
809+ allocate (fission_bank(3 * work ), STAT= alloc_err)
788810
789811 ! Check for allocation errors
790812 if (alloc_err /= 0 ) then
0 commit comments