Skip to content

Commit 82c4568

Browse files
committed
Improved distribution of work among processes.
1 parent c18a6ea commit 82c4568

7 files changed

Lines changed: 52 additions & 34 deletions

File tree

src/cmfd_execute.F90

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ subroutine cmfd_reweight(new_weights)
281281
use constants, only: ZERO, ONE
282282
use error, only: warning, fatal_error
283283
use global, only: n_particles, meshes, source_bank, work, &
284-
n_user_meshes, message, cmfd, master, mpi_err, &
285-
bank_first, bank_last
284+
n_user_meshes, message, cmfd, master, mpi_err
286285
use mesh_header, only: StructuredMesh
287286
use mesh, only: count_bank_sites, get_mesh_indices
288287
use search, only: binary_search
@@ -312,9 +311,6 @@ subroutine cmfd_reweight(new_weights)
312311
nz = cmfd%indices(3)
313312
ng = cmfd%indices(4)
314313

315-
! compute size of source bank
316-
size_bank = bank_last - bank_first + 1_8
317-
318314
! allocate arrays in cmfd object (can take out later extend to multigroup)
319315
if (.not.allocated(cmfd%sourcecounts)) then
320316
allocate(cmfd%sourcecounts(ng,nx,ny,nz))
@@ -337,7 +333,7 @@ subroutine cmfd_reweight(new_weights)
337333

338334
! count bank sites in mesh
339335
call count_bank_sites(m, source_bank, cmfd%sourcecounts, egrid, &
340-
sites_outside=outside, size_bank = size_bank)
336+
sites_outside=outside, size_bank=work)
341337

342338
! check for sites outside of the mesh
343339
if (master .and. outside) then

src/eigenvalue.F90

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ subroutine synchronize_bank()
378378
end if
379379

380380
! the last processor should not be sending sites to right
381-
finish = bank_last
381+
finish = work_index(rank + 1)
382382
end if
383383

384384
call time_bank_sample % stop()
@@ -394,11 +394,11 @@ subroutine synchronize_bank()
394394
if (start < n_particles) then
395395
! Determine the index of the processor which has the first part of the
396396
! source_bank for the local processor
397-
neighbor = start / maxwork
397+
neighbor = binary_search(work_index, n_procs + 1, start) - 1
398398

399399
SEND_SITES: do while (start < finish)
400400
! Determine the number of sites to send
401-
n = min((neighbor + 1)*maxwork, finish) - start
401+
n = min(work_index(neighbor + 1), finish) - start
402402

403403
! Initiate an asynchronous send of source sites to the neighboring
404404
! process
@@ -423,7 +423,7 @@ subroutine synchronize_bank()
423423
! ==========================================================================
424424
! RECEIVE BANK SITES FROM NEIGHBORS OR TEMPORARY BANK
425425

426-
start = bank_first - 1
426+
start = work_index(rank)
427427
index_local = 1
428428

429429
! Determine what process has the source sites that will need to be stored at
@@ -435,13 +435,12 @@ subroutine synchronize_bank()
435435
neighbor = binary_search(bank_position, n_procs, start) - 1
436436
end if
437437

438-
RECV_SITES: do while (start < bank_last)
438+
RECV_SITES: do while (start < work_index(rank + 1))
439439
! Determine how many sites need to be received
440440
if (neighbor == n_procs - 1) then
441-
n = min(n_particles, (rank+1)*maxwork) - start
441+
n = work_index(rank + 1) - start
442442
else
443-
n = min(bank_position(neighbor+2), min(n_particles, &
444-
(rank+1)*maxwork)) - start
443+
n = min(bank_position(neighbor + 2), work_index(rank + 1)) - start
445444
end if
446445

447446
if (neighbor /= rank) then

src/fixed_source.F90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ subroutine run_fixedsource()
5050
PARTICLE_LOOP: do i = 1, work
5151

5252
! Set unique particle ID
53-
p % id = (current_batch - 1)*n_particles + bank_first + i - 1
53+
p % id = (current_batch - 1)*n_particles + work_index(rank) + i
5454

5555
! set particle trace
5656
trace = .false.
5757
if (current_batch == trace_batch .and. current_gen == trace_gen .and. &
58-
bank_first + i - 1 == trace_particle) trace = .true.
58+
work_index(rank) + i == trace_particle) trace = .true.
5959

6060
! set random number seed
6161
call set_particle_seed(p % id)

src/global.F90

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,8 @@ module global
160160
type(Bank), allocatable, target :: source_bank(:)
161161
type(Bank), allocatable, target :: fission_bank(:)
162162
integer(8) :: n_bank ! # of sites in fission bank
163-
integer(8) :: bank_first ! index of first particle in bank
164-
integer(8) :: bank_last ! index of last particle in bank
165163
integer(8) :: work ! number of particles per processor
166-
integer(8) :: maxwork ! maximum number of particles per processor
164+
integer(8), allocatable :: work_index(:) ! starting index in source bank for each process
167165
integer(8) :: current_work ! index in source bank of current history simulated
168166

169167
! Temporary k-effective values
@@ -442,6 +440,9 @@ subroutine free_memory()
442440
if (allocated(source_bank)) deallocate(source_bank)
443441
if (allocated(entropy_p)) deallocate(entropy_p)
444442

443+
! Deallocate array of work indices
444+
if (allocated(work_index)) deallocate(work_index)
445+
445446
! Deallocate cmfd
446447
call deallocate_cmfd(cmfd)
447448

src/initialize.F90

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/output_interface.F90

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ subroutine write_source_bank()
947947
call h5dget_space_f(dset, dspace, hdf5_err)
948948

949949
! Select hyperslab for this dataspace
950-
offset(1) = bank_first - 1_8
950+
offset(1) = work_index(rank)
951951
call h5sselect_hyperslab_f(dspace, H5S_SELECT_SET_F, offset, dims1, hdf5_err)
952952

953953
! Set up the property list for parallel writing
@@ -1009,7 +1009,7 @@ subroutine write_source_bank()
10091009

10101010
! Set the proper offset for source data on this processor
10111011
call MPI_TYPE_SIZE(MPI_BANK, size_bank, mpi_err)
1012-
offset = offset + size_bank*maxwork*rank
1012+
offset = offset + size_bank*work_index(rank)
10131013

10141014
! Write all source sites
10151015
call MPI_FILE_WRITE_AT(mpi_fh, offset, source_bank(1), work, MPI_BANK, &
@@ -1056,7 +1056,7 @@ subroutine read_source_bank()
10561056
call h5dget_space_f(dset, dspace, hdf5_err)
10571057

10581058
! Select hyperslab for this dataspace
1059-
offset(1) = bank_first - 1_8
1059+
offset(1) = work_index(rank)
10601060
call h5sselect_hyperslab_f(dspace, H5S_SELECT_SET_F, offset, dims1, hdf5_err)
10611061

10621062
! Set up the property list for parallel writing
@@ -1109,7 +1109,7 @@ subroutine read_source_bank()
11091109

11101110
! Set the proper offset for source data on this processor
11111111
call MPI_TYPE_SIZE(MPI_BANK, size_bank, mpi_err)
1112-
offset = offset + size_bank*maxwork*rank
1112+
offset = offset + size_bank*work_index(rank)
11131113

11141114
! Write all source sites
11151115
call MPI_FILE_READ_AT(mpi_fh, offset, source_bank(1), work, MPI_BANK, &

src/source.F90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ subroutine initialize_source()
4747
src => source_bank(i)
4848

4949
! initialize random number seed
50-
id = bank_first + i - 1
50+
id = work_index(rank) + i
5151
call set_particle_seed(id)
5252

5353
! sample external source distribution
@@ -165,7 +165,7 @@ subroutine get_source_particle(p, index_source)
165165
call copy_source_attributes(p, src)
166166

167167
! set identifier for particle
168-
p % id = bank_first + index_source - 1
168+
p % id = work_index(rank) + index_source
169169

170170
! set random number seed
171171
particle_seed = (overall_gen - 1)*n_particles + p % id

0 commit comments

Comments
 (0)