Skip to content

Commit 06b802e

Browse files
committed
fix: lazy-allocate patch_ib to restore FP-stability CI
num_ib_patches_max = 2050000 caused a ~2.25 GB unconditional heap allocation + full init loop in s_assign_default_values_to_user_inputs on every startup, crashing the CI debug build even for cases with no IBM. Also, s_reduce_ib_patch_array had a 2.25 GB stack-allocated local array that caused SIGSEGV for IBM cases. Introduce num_ib_patches_max_namelist = 50000 (restoring the pre-particle-bed budget) for the initial allocation. s_generate_particle_beds grows patch_ib to num_ib_patches_max via MOVE_ALLOC only when particle beds are actually being generated. s_reduce_ib_patch_array now uses a heap-allocated local array sized to num_ibs instead of the full num_ib_patches_max.
1 parent ef31786 commit 06b802e

4 files changed

Lines changed: 36 additions & 19 deletions

File tree

src/common/m_constants.fpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ module m_constants
1616
real(wp), parameter :: verysmall = 1.e-12_wp !< Very small number
1717
!> Radius cutoff to avoid division by zero for 3D spherical harmonic patch (geometry 14)
1818
real(wp), parameter :: small_radius = 1.e-32_wp
19-
integer, parameter :: num_stcls_min = 5 !< Minimum # of stencils
20-
integer, parameter :: path_len = 400 !< Maximum path length
21-
integer, parameter :: name_len = 50 !< Maximum name length
22-
integer, parameter :: dflt_int = -100 !< Default integer value
23-
integer, parameter :: fourier_rings = 5 !< Fourier filter ring limit
24-
integer, parameter :: num_fluids_max = 10 !< Maximum number of fluids in the simulation
25-
integer, parameter :: num_probes_max = 10 !< Maximum number of flow probes in the simulation
26-
integer, parameter :: num_patches_max = 10 !< Maximum number of IC patches
27-
integer, parameter :: num_ib_patches_max = 2050000 !< Maximum number of immersed boundary patches (patch_ib)
19+
integer, parameter :: num_stcls_min = 5 !< Minimum # of stencils
20+
integer, parameter :: path_len = 400 !< Maximum path length
21+
integer, parameter :: name_len = 50 !< Maximum name length
22+
integer, parameter :: dflt_int = -100 !< Default integer value
23+
integer, parameter :: fourier_rings = 5 !< Fourier filter ring limit
24+
integer, parameter :: num_fluids_max = 10 !< Maximum number of fluids in the simulation
25+
integer, parameter :: num_probes_max = 10 !< Maximum number of flow probes in the simulation
26+
integer, parameter :: num_patches_max = 10 !< Maximum number of IC patches
27+
integer, parameter :: num_ib_patches_max = 2050000 !< Maximum number of immersed boundary patches (patch_ib)
28+
!> Max patches readable from the namelist; patch_ib grows to num_ib_patches_max only when particle beds are used
29+
integer, parameter :: num_ib_patches_max_namelist = 50000
2830
integer, parameter :: num_local_ibs_max = 2000 !< Maximum number of immersed boundary patches (patch_ib)
2931
integer, parameter :: num_particle_beds_max = 10 !< Maximum number of particle bed patch specifications
3032
integer, parameter :: num_bc_patches_max = 10 !< Maximum number of boundary condition patches

src/simulation/m_global_parameters.fpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,8 +829,8 @@ contains
829829
particle_bed(i)%seed = 0
830830
end do
831831
832-
allocate (patch_ib(num_ib_patches_max))
833-
do i = 1, num_ib_patches_max
832+
allocate (patch_ib(num_ib_patches_max_namelist))
833+
do i = 1, num_ib_patches_max_namelist
834834
patch_ib(i)%gbl_patch_id = i
835835
patch_ib(i)%geometry = dflt_int
836836
patch_ib(i)%x_centroid = 0._wp

src/simulation/m_particle_bed.fpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ contains
4040

4141
if (num_particle_beds == 0) return
4242

43+
! Grow patch_ib from the namelist-sized allocation to the full capacity needed for particle beds
44+
if (size(patch_ib) < num_ib_patches_max) then
45+
block
46+
type(ib_patch_parameters), allocatable :: tmp(:)
47+
integer :: n
48+
n = size(patch_ib)
49+
call move_alloc(patch_ib, tmp)
50+
allocate (patch_ib(num_ib_patches_max))
51+
patch_ib(1:n) = tmp
52+
end block
53+
end if
54+
4355
do b = 1, num_particle_beds
4456
xmin = particle_bed(b)%x_centroid - 0.5_wp*particle_bed(b)%length_x
4557
xmax = particle_bed(b)%x_centroid + 0.5_wp*particle_bed(b)%length_x

src/simulation/m_start_up.fpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ contains
10221022
"on CPUs"
10231023
#endif
10241024
else
1025-
allocate (patch_ib(num_ib_patches_max))
1025+
allocate (patch_ib(num_ib_patches_max_namelist))
10261026
end if
10271027

10281028
call s_mpi_bcast_user_inputs()
@@ -1210,11 +1210,11 @@ contains
12101210
!! the local computational domain.
12111211
subroutine s_reduce_ib_patch_array()
12121212

1213-
type(ib_patch_parameters), dimension(num_ib_patches_max) :: patch_ib_gbl
1214-
real(wp), dimension(3) :: centroid
1215-
integer :: i, j
1216-
integer :: num_aware_ibs
1217-
logical :: is_in_neighborhood, is_local
1213+
type(ib_patch_parameters), allocatable :: patch_ib_gbl(:)
1214+
real(wp), dimension(3) :: centroid
1215+
integer :: i, j
1216+
integer :: num_aware_ibs
1217+
logical :: is_in_neighborhood, is_local
12181218

12191219
! do all set up for moving immersed boundaries
12201220

@@ -1226,6 +1226,7 @@ contains
12261226
end if
12271227
end do
12281228

1229+
allocate (patch_ib_gbl(num_ibs))
12291230
patch_ib_gbl(1:num_ibs) = patch_ib(1:num_ibs)
12301231
call get_neighbor_bounds() ! make sure the bounds of the neighbors are correctly set up
12311232
call s_compute_ib_neighbor_ranks() ! build lookup of all neighbor MPI ranks
@@ -1244,7 +1245,7 @@ contains
12441245
#ifdef MFC_MPI
12451246
! fallback for 1-rank case
12461247
if (num_procs == 1) then
1247-
patch_ib(:) = patch_ib_gbl(1:num_aware_ibs)
1248+
patch_ib(1:num_gbl_ibs) = patch_ib_gbl(1:num_gbl_ibs)
12481249
else
12491250
! determine the set of patches owned by local rank
12501251
num_local_ibs = 0
@@ -1269,9 +1270,11 @@ contains
12691270
end if
12701271
#else
12711272
! reduce the size of the array for local simulation in no-MPI case
1272-
patch_ib(:) = patch_ib_gbl(1:num_aware_ibs)
1273+
patch_ib(1:num_gbl_ibs) = patch_ib_gbl(1:num_gbl_ibs)
12731274
#endif
12741275

1276+
deallocate (patch_ib_gbl)
1277+
12751278
@:ALLOCATE(ib_gbl_idx_lookup(1:num_gbl_ibs))
12761279

12771280
end subroutine s_reduce_ib_patch_array

0 commit comments

Comments
 (0)