Skip to content

Commit 2db52ca

Browse files
committed
bitwise identical grid variables with parallel I/O
1 parent 86a0294 commit 2db52ca

4 files changed

Lines changed: 167 additions & 84 deletions

File tree

src/common/m_boundary_common.fpp

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,7 +2562,7 @@ contains
25622562
#ifndef MFC_PRE_PROCESS
25632563
! Populating cell-width distribution buffer at bc%x%beg
25642564
if (bc%x%beg >= 0) then
2565-
call s_mpi_sendrecv_grid_variables_buffers(1, -1)
2565+
if (.not. parallel_io) call s_mpi_sendrecv_grid_variables_buffers(1, -1)
25662566
else if (bc%x%beg <= BC_GHOST_EXTRAP) then
25672567
do i = 1, buff_size
25682568
dx(-i) = dx(0)
@@ -2577,18 +2577,21 @@ contains
25772577
end do
25782578
end if
25792579

2580-
! Computing the cell-boundary and center locations buffer at bc%x%beg
2581-
do i = 1, offset_x%beg
2582-
x_cb(-1 - i) = x_cb(-i) - dx(-i)
2583-
end do
2580+
! Computing the cell-boundary and center locations buffer at bc%x%beg. Skip when parallel_io has already filled the ghost
2581+
! x_cb / x_cc bitwise from the global grid file via s_apply_grid_from_global_dim
2582+
if (.not. (bc%x%beg >= 0 .and. parallel_io)) then
2583+
do i = 1, offset_x%beg
2584+
x_cb(-1 - i) = x_cb(-i) - dx(-i)
2585+
end do
25842586

2585-
do i = 1, buff_size
2586-
x_cc(-i) = x_cc(1 - i) - (dx(1 - i) + dx(-i))/2._wp
2587-
end do
2587+
do i = 1, buff_size
2588+
x_cc(-i) = x_cc(1 - i) - (dx(1 - i) + dx(-i))/2._wp
2589+
end do
2590+
end if
25882591

25892592
! Populating the cell-width distribution buffer at bc%x%end
25902593
if (bc%x%end >= 0) then
2591-
call s_mpi_sendrecv_grid_variables_buffers(1, 1)
2594+
if (.not. parallel_io) call s_mpi_sendrecv_grid_variables_buffers(1, 1)
25922595
else if (bc%x%end <= BC_GHOST_EXTRAP) then
25932596
do i = 1, buff_size
25942597
dx(m + i) = dx(m)
@@ -2603,20 +2606,22 @@ contains
26032606
end do
26042607
end if
26052608

2606-
! Populating the cell-boundary and center locations buffer at bc%x%end
2607-
do i = 1, offset_x%end
2608-
x_cb(m + i) = x_cb(m + (i - 1)) + dx(m + i)
2609-
end do
2609+
! Populating the cell-boundary and center locations buffer at bc%x%end. Skip when parallel_io has already filled the ghost.
2610+
if (.not. (bc%x%end >= 0 .and. parallel_io)) then
2611+
do i = 1, offset_x%end
2612+
x_cb(m + i) = x_cb(m + (i - 1)) + dx(m + i)
2613+
end do
26102614

2611-
do i = 1, buff_size
2612-
x_cc(m + i) = x_cc(m + (i - 1)) + (dx(m + (i - 1)) + dx(m + i))/2._wp
2613-
end do
2615+
do i = 1, buff_size
2616+
x_cc(m + i) = x_cc(m + (i - 1)) + (dx(m + (i - 1)) + dx(m + i))/2._wp
2617+
end do
2618+
end if
26142619

26152620
! Populating cell-width distribution buffer at bc%y%beg
26162621
if (n == 0) then
26172622
return
26182623
else if (bc%y%beg >= 0) then
2619-
call s_mpi_sendrecv_grid_variables_buffers(2, -1)
2624+
if (.not. parallel_io) call s_mpi_sendrecv_grid_variables_buffers(2, -1)
26202625
else if (bc%y%beg <= BC_GHOST_EXTRAP .and. bc%y%beg /= BC_AXIS) then
26212626
do i = 1, buff_size
26222627
dy(-i) = dy(0)
@@ -2631,18 +2636,20 @@ contains
26312636
end do
26322637
end if
26332638

2634-
! Computing the cell-boundary and center locations buffer at bc%y%beg
2635-
do i = 1, offset_y%beg
2636-
y_cb(-1 - i) = y_cb(-i) - dy(-i)
2637-
end do
2639+
! Computing the cell-boundary and center locations buffer at bc%y%beg. Skip when parallel_io has already filled the ghost.
2640+
if (.not. (bc%y%beg >= 0 .and. parallel_io)) then
2641+
do i = 1, offset_y%beg
2642+
y_cb(-1 - i) = y_cb(-i) - dy(-i)
2643+
end do
26382644

2639-
do i = 1, buff_size
2640-
y_cc(-i) = y_cc(1 - i) - (dy(1 - i) + dy(-i))/2._wp
2641-
end do
2645+
do i = 1, buff_size
2646+
y_cc(-i) = y_cc(1 - i) - (dy(1 - i) + dy(-i))/2._wp
2647+
end do
2648+
end if
26422649

26432650
! Populating the cell-width distribution buffer at bc%y%end
26442651
if (bc%y%end >= 0) then
2645-
call s_mpi_sendrecv_grid_variables_buffers(2, 1)
2652+
if (.not. parallel_io) call s_mpi_sendrecv_grid_variables_buffers(2, 1)
26462653
else if (bc%y%end <= BC_GHOST_EXTRAP) then
26472654
do i = 1, buff_size
26482655
dy(n + i) = dy(n)
@@ -2657,20 +2664,22 @@ contains
26572664
end do
26582665
end if
26592666

2660-
! Populating the cell-boundary and center locations buffer at bc%y%end
2661-
do i = 1, offset_y%end
2662-
y_cb(n + i) = y_cb(n + (i - 1)) + dy(n + i)
2663-
end do
2667+
! Populating the cell-boundary and center locations buffer at bc%y%end. Skip when parallel_io has already filled the ghost.
2668+
if (.not. (bc%y%end >= 0 .and. parallel_io)) then
2669+
do i = 1, offset_y%end
2670+
y_cb(n + i) = y_cb(n + (i - 1)) + dy(n + i)
2671+
end do
26642672

2665-
do i = 1, buff_size
2666-
y_cc(n + i) = y_cc(n + (i - 1)) + (dy(n + (i - 1)) + dy(n + i))/2._wp
2667-
end do
2673+
do i = 1, buff_size
2674+
y_cc(n + i) = y_cc(n + (i - 1)) + (dy(n + (i - 1)) + dy(n + i))/2._wp
2675+
end do
2676+
end if
26682677

26692678
! Populating cell-width distribution buffer at bc%z%beg
26702679
if (p == 0) then
26712680
return
26722681
else if (bc%z%beg >= 0) then
2673-
call s_mpi_sendrecv_grid_variables_buffers(3, -1)
2682+
if (.not. parallel_io) call s_mpi_sendrecv_grid_variables_buffers(3, -1)
26742683
else if (bc%z%beg <= BC_GHOST_EXTRAP) then
26752684
do i = 1, buff_size
26762685
dz(-i) = dz(0)
@@ -2685,18 +2694,20 @@ contains
26852694
end do
26862695
end if
26872696

2688-
! Computing the cell-boundary and center locations buffer at bc%z%beg
2689-
do i = 1, offset_z%beg
2690-
z_cb(-1 - i) = z_cb(-i) - dz(-i)
2691-
end do
2697+
! Computing the cell-boundary and center locations buffer at bc%z%beg. Skip when parallel_io has already filled the ghost.
2698+
if (.not. (bc%z%beg >= 0 .and. parallel_io)) then
2699+
do i = 1, offset_z%beg
2700+
z_cb(-1 - i) = z_cb(-i) - dz(-i)
2701+
end do
26922702

2693-
do i = 1, buff_size
2694-
z_cc(-i) = z_cc(1 - i) - (dz(1 - i) + dz(-i))/2._wp
2695-
end do
2703+
do i = 1, buff_size
2704+
z_cc(-i) = z_cc(1 - i) - (dz(1 - i) + dz(-i))/2._wp
2705+
end do
2706+
end if
26962707

26972708
! Populating the cell-width distribution buffer at bc%z%end
26982709
if (bc%z%end >= 0) then
2699-
call s_mpi_sendrecv_grid_variables_buffers(3, 1)
2710+
if (.not. parallel_io) call s_mpi_sendrecv_grid_variables_buffers(3, 1)
27002711
else if (bc%z%end <= BC_GHOST_EXTRAP) then
27012712
do i = 1, buff_size
27022713
dz(p + i) = dz(p)
@@ -2711,14 +2722,16 @@ contains
27112722
end do
27122723
end if
27132724

2714-
! Populating the cell-boundary and center locations buffer at bc%z%end
2715-
do i = 1, offset_z%end
2716-
z_cb(p + i) = z_cb(p + (i - 1)) + dz(p + i)
2717-
end do
2725+
! Populating the cell-boundary and center locations buffer at bc%z%end. Skip when parallel_io has already filled the ghost.
2726+
if (.not. (bc%z%end >= 0 .and. parallel_io)) then
2727+
do i = 1, offset_z%end
2728+
z_cb(p + i) = z_cb(p + (i - 1)) + dz(p + i)
2729+
end do
27182730

2719-
do i = 1, buff_size
2720-
z_cc(p + i) = z_cc(p + (i - 1)) + (dz(p + (i - 1)) + dz(p + i))/2._wp
2721-
end do
2731+
do i = 1, buff_size
2732+
z_cc(p + i) = z_cc(p + (i - 1)) + (dz(p + (i - 1)) + dz(p + i))/2._wp
2733+
end do
2734+
end if
27222735
#endif
27232736

27242737
end subroutine s_populate_grid_variables_buffers

src/common/m_mpi_common.fpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,6 +1932,99 @@ contains
19321932
#endif
19331933

19341934
end subroutine s_mpi_sendrecv_grid_variables_buffers
1935+
1936+
!> Populate the local cell-boundary, cell-center, and cell-width arrays in one direction directly from the global cell-boundary
1937+
!! array. This guarantees that every rank sees bitwise-identical values at any shared physical cell or boundary
1938+
!!
1939+
!! Ghost-cell extents may be asymmetric between x_cb_loc and dx_loc/x_cc_loc (post_process uses offset_x%beg/end for x_cb but
1940+
!! buff_size for dx/x_cc), so the buffer widths are supplied explicitly per side and per array.
1941+
!! @param x_cb_glb Global cell-boundary array indexed -1:m_dim_glb
1942+
!! @param m_dim_glb Global cell count in this direction
1943+
!! @param m_dim Local cell count in this direction
1944+
!! @param sidx Start index of this rank's interior in the global array (x_cb_loc(0) == x_cb_glb(sidx))
1945+
!! @param bc_beg bc_x/y/z%beg value after MPI cart setup (>=0 means an MPI neighbor exists)
1946+
!! @param bc_end bc_x/y/z%end value after MPI cart setup
1947+
!! @param cb_lo,cb_hi Available ghost cells on each side of x_cb_loc (x_cb_loc(-1-cb_lo:m_dim+cb_hi))
1948+
!! @param cw_lo,cw_hi Available ghost cells on each side of dx_loc/x_cc_loc (dx_loc(-cw_lo:m_dim+cw_hi))
1949+
!! @param x_cb_loc Local cell-boundary array, fully sized including ghost cells
1950+
!! @param x_cc_loc Local cell-center array, fully sized including ghost cells
1951+
!! @param dx_loc Local cell-width array, fully sized including ghost cells
1952+
subroutine s_apply_grid_from_global_dim(x_cb_glb, m_dim_glb, m_dim, sidx, bc_beg, bc_end, cb_lo, cb_hi, cw_lo, cw_hi, &
1953+
& x_cb_loc, x_cc_loc, dx_loc)
1954+
1955+
integer, intent(in) :: m_dim_glb, m_dim, sidx, bc_beg, bc_end
1956+
integer, intent(in) :: cb_lo, cb_hi, cw_lo, cw_hi
1957+
real(wp), intent(in) :: x_cb_glb(-1:m_dim_glb)
1958+
real(wp), intent(inout) :: x_cb_loc(-1 - cb_lo:m_dim + cb_hi)
1959+
real(wp), intent(inout) :: x_cc_loc(-cw_lo:m_dim + cw_hi)
1960+
real(wp), intent(inout) :: dx_loc(-cw_lo:m_dim + cw_hi)
1961+
real(wp) :: domain_len
1962+
integer :: i, gidx, lo, hi
1963+
1964+
domain_len = x_cb_glb(m_dim_glb) - x_cb_glb(-1)
1965+
1966+
! Interior cell boundaries sliced directly from the global list
1967+
do i = -1, m_dim
1968+
x_cb_loc(i) = x_cb_glb(sidx + i)
1969+
end do
1970+
1971+
! Left ghost cell boundaries
1972+
if (bc_beg >= 0) then
1973+
if (sidx == 0) then
1974+
! Leftmost rank with a neighbor -> periodic+multirank, so wrap from the global right end
1975+
do i = 1, cb_lo
1976+
x_cb_loc(-1 - i) = x_cb_glb(m_dim_glb - i) - domain_len
1977+
end do
1978+
else
1979+
do i = 1, cb_lo
1980+
gidx = sidx - 1 - i
1981+
if (gidx >= -1) then
1982+
x_cb_loc(-1 - i) = x_cb_glb(gidx)
1983+
else
1984+
x_cb_loc(-1 - i) = x_cb_glb(m_dim_glb + 1 + gidx) - domain_len
1985+
end if
1986+
end do
1987+
end if
1988+
end if
1989+
1990+
! Right ghost cell boundaries
1991+
if (bc_end >= 0) then
1992+
if (sidx + m_dim == m_dim_glb) then
1993+
! Rightmost rank with a neighbor -> periodic+multirank, wrap from the global left end
1994+
do i = 1, cb_hi
1995+
x_cb_loc(m_dim + i) = x_cb_glb(i - 1) + domain_len
1996+
end do
1997+
else
1998+
do i = 1, cb_hi
1999+
gidx = sidx + m_dim + i
2000+
if (gidx <= m_dim_glb) then
2001+
x_cb_loc(m_dim + i) = x_cb_glb(gidx)
2002+
else
2003+
x_cb_loc(m_dim + i) = x_cb_glb(gidx - m_dim_glb - 1) + domain_len
2004+
end if
2005+
end do
2006+
end if
2007+
end if
2008+
2009+
! Recompute dx and x_cc over the range where x_cb is now valid using one formula so values are bitwise-identical
2010+
if (bc_beg >= 0) then
2011+
lo = -min(cw_lo, cb_lo)
2012+
else
2013+
lo = 0
2014+
end if
2015+
2016+
if (bc_end >= 0) then
2017+
hi = m_dim + min(cw_hi, cb_hi)
2018+
else
2019+
hi = m_dim
2020+
end if
2021+
2022+
do i = lo, hi
2023+
dx_loc(i) = x_cb_loc(i) - x_cb_loc(i - 1)
2024+
x_cc_loc(i) = (x_cb_loc(i) + x_cb_loc(i - 1))/2._wp
2025+
end do
2026+
2027+
end subroutine s_apply_grid_from_global_dim
19352028
#endif
19362029
19372030
!> Module deallocation and/or disassociation procedures

src/post_process/m_data_input.f90

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,9 @@ impure subroutine s_read_parallel_data_files(t_step)
334334
call s_mpi_abort('File ' // trim(file_loc) // ' is missing. Exiting.')
335335
end if
336336

337-
! Assigning local cell boundary locations
338-
x_cb(-1:m) = x_cb_glb((start_idx(1) - 1):(start_idx(1) + m))
339-
! Computing the cell width distribution
340-
dx(0:m) = x_cb(0:m) - x_cb(-1:m - 1)
341-
! Computing the cell center location
342-
x_cc(0:m) = x_cb(-1:m - 1) + dx(0:m)/2._wp
337+
! Bitwise-consistent grid distribution from the global file
338+
call s_apply_grid_from_global_dim(x_cb_glb, m_glb, m, start_idx(1), bc_x%beg, bc_x%end, offset_x%beg, offset_x%end, &
339+
& buff_size, buff_size, x_cb, x_cc, dx)
343340

344341
if (n > 0) then
345342
! Read in cell boundary locations in y-direction
@@ -362,12 +359,8 @@ impure subroutine s_read_parallel_data_files(t_step)
362359
call s_mpi_abort('File ' // trim(file_loc) // ' is missing. Exiting.')
363360
end if
364361

365-
! Assigning local cell boundary locations
366-
y_cb(-1:n) = y_cb_glb((start_idx(2) - 1):(start_idx(2) + n))
367-
! Computing the cell width distribution
368-
dy(0:n) = y_cb(0:n) - y_cb(-1:n - 1)
369-
! Computing the cell center location
370-
y_cc(0:n) = y_cb(-1:n - 1) + dy(0:n)/2._wp
362+
call s_apply_grid_from_global_dim(y_cb_glb, n_glb, n, start_idx(2), bc_y%beg, bc_y%end, offset_y%beg, offset_y%end, &
363+
& buff_size, buff_size, y_cb, y_cc, dy)
371364

372365
if (p > 0) then
373366
! Read in cell boundary locations in z-direction
@@ -390,12 +383,8 @@ impure subroutine s_read_parallel_data_files(t_step)
390383
call s_mpi_abort('File ' // trim(file_loc) // ' is missing. Exiting.')
391384
end if
392385

393-
! Assigning local cell boundary locations
394-
z_cb(-1:p) = z_cb_glb((start_idx(3) - 1):(start_idx(3) + p))
395-
! Computing the cell width distribution
396-
dz(0:p) = z_cb(0:p) - z_cb(-1:p - 1)
397-
! Computing the cell center location
398-
z_cc(0:p) = z_cb(-1:p - 1) + dz(0:p)/2._wp
386+
call s_apply_grid_from_global_dim(z_cb_glb, p_glb, p, start_idx(3), bc_z%beg, bc_z%end, offset_z%beg, &
387+
& offset_z%end, buff_size, buff_size, z_cb, z_cc, dz)
399388
end if
400389
end if
401390

src/simulation/m_start_up.fpp

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,8 @@ contains
369369
call s_mpi_abort('File ' // trim(file_loc) // ' is missing. Exiting.')
370370
end if
371371

372-
! Assigning local cell boundary locations
373-
x_cb(-1:m) = x_cb_glb((start_idx(1) - 1):(start_idx(1) + m))
374-
! Computing the cell width distribution
375-
dx(0:m) = x_cb(0:m) - x_cb(-1:m - 1)
376-
! Computing the cell center locations
377-
x_cc(0:m) = x_cb(-1:m - 1) + dx(0:m)/2._wp
372+
call s_apply_grid_from_global_dim(x_cb_glb, m_glb, m, start_idx(1), bc_x%beg, bc_x%end, buff_size, buff_size, buff_size, &
373+
& buff_size, x_cb, x_cc, dx)
378374

379375
if (ib) then
380376
do i = 1, num_ibs
@@ -400,12 +396,8 @@ contains
400396
call s_mpi_abort('File ' // trim(file_loc) // ' is missing. Exiting.')
401397
end if
402398

403-
! Assigning local cell boundary locations
404-
y_cb(-1:n) = y_cb_glb((start_idx(2) - 1):(start_idx(2) + n))
405-
! Computing the cell width distribution
406-
dy(0:n) = y_cb(0:n) - y_cb(-1:n - 1)
407-
! Computing the cell center locations
408-
y_cc(0:n) = y_cb(-1:n - 1) + dy(0:n)/2._wp
399+
call s_apply_grid_from_global_dim(y_cb_glb, n_glb, n, start_idx(2), bc_y%beg, bc_y%end, buff_size, buff_size, &
400+
& buff_size, buff_size, y_cb, y_cc, dy)
409401

410402
if (p > 0) then
411403
! Read in cell boundary locations in z-direction
@@ -421,12 +413,8 @@ contains
421413
call s_mpi_abort('File ' // trim(file_loc) // 'is missing. Exiting.')
422414
end if
423415

424-
! Assigning local cell boundary locations
425-
z_cb(-1:p) = z_cb_glb((start_idx(3) - 1):(start_idx(3) + p))
426-
! Computing the cell width distribution
427-
dz(0:p) = z_cb(0:p) - z_cb(-1:p - 1)
428-
! Computing the cell center locations
429-
z_cc(0:p) = z_cb(-1:p - 1) + dz(0:p)/2._wp
416+
call s_apply_grid_from_global_dim(z_cb_glb, p_glb, p, start_idx(3), bc_z%beg, bc_z%end, buff_size, buff_size, &
417+
& buff_size, buff_size, z_cb, z_cc, dz)
430418
end if
431419
end if
432420

0 commit comments

Comments
 (0)