MFC
Exascale flow solver
Loading...
Searching...
No Matches
m_amr_distribution.fpp.f90
Go to the documentation of this file.
1# 1 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
2!>
3!!@file
4!!@brief Contains module m_amr_distribution
5
6# 7 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
7# 8 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
8# 1 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp" 1
9# 1 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp" 1
10# 1 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 1
11# 2 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
12# 3 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
13# 4 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
14# 5 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
15# 6 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
16
17# 8 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
18# 9 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
19# 10 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
20
21# 12 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
22# 1 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp" 1
23! Default for Fypp runs outside the toolchain (e.g. documentation). Searched last, so the
24! build/include/<target>/thermochem.fpp the toolchain writes from m_thermochem takes precedence.
25# 4 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
26# 5 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
27# 13 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 2
28
29# 16 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
30# 17 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
31
32# 24 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
33
34# 53 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
35
36# 65 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
37
38# 75 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
39
40# 105 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
41
42# 117 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
43
44# 127 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
45
46# 174 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
47! New line at end of file is required for FYPP
48# 2 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp" 2
49# 1 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp" 1
50# 1 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 1
51# 2 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
52# 3 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
53# 4 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
54# 5 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
55# 6 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
56
57# 8 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
58# 9 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
59# 10 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
60
61# 12 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
62# 1 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp" 1
63! Default for Fypp runs outside the toolchain (e.g. documentation). Searched last, so the
64! build/include/<target>/thermochem.fpp the toolchain writes from m_thermochem takes precedence.
65# 4 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
66# 5 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
67# 13 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 2
68
69# 16 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
70# 17 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
71
72# 24 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
73
74# 53 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
75
76# 65 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
77
78# 75 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
79
80# 105 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
81
82# 117 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
83
84# 127 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
85
86# 174 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
87! New line at end of file is required for FYPP
88# 2 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp" 2
89
90# 4 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
91# 5 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
92# 6 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
93# 7 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
94# 8 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
95
96# 20 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
97
98# 49 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
99
100# 54 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
101
102# 59 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
103
104# 64 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
105
106# 69 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
107
108# 74 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
109
110# 87 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
111
112# 101 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
113
114# 106 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
115
116# 111 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
117
118# 116 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
119
120# 121 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
121
122# 126 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
123
124# 131 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
125
126# 136 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
127
128# 141 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
129
130# 171 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
131
132# 231 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
133
134# 245 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
135
136# 270 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
137
138# 281 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
139
140# 283 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
141# 294 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
142
143# 343 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
144
145# 353 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
146
147# 363 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
148
149# 372 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
150
151# 389 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
152
153# 399 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
154
155# 406 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
156
157# 412 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
158
159# 418 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
160
161# 424 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
162
163# 430 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
164
165# 436 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
166! New line at end of file is required for FYPP
167# 3 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp" 2
168# 1 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp" 1
169# 1 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 1
170# 2 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
171# 3 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
172# 4 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
173# 5 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
174# 6 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
175
176# 8 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
177# 9 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
178# 10 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
179
180# 12 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
181# 1 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp" 1
182! Default for Fypp runs outside the toolchain (e.g. documentation). Searched last, so the
183! build/include/<target>/thermochem.fpp the toolchain writes from m_thermochem takes precedence.
184# 4 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
185# 5 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
186# 13 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 2
187
188# 16 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
189# 17 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
190
191# 24 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
192
193# 53 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
194
195# 65 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
196
197# 75 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
198
199# 105 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
200
201# 117 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
202
203# 127 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
204
205# 174 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
206! New line at end of file is required for FYPP
207# 2 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp" 2
208
209# 7 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
210
211# 17 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
212
213# 22 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
214
215# 27 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
216
217# 32 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
218
219# 37 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
220
221# 42 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
222
223# 47 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
224
225# 52 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
226
227# 57 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
228
229# 62 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
230
231# 73 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
232
233# 78 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
234
235# 83 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
236
237# 88 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
238
239# 103 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
240
241# 131 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
242
243# 161 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
244
245# 176 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
246
247# 194 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
248
249# 216 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
250
251# 245 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
252
253# 260 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
254
255# 270 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
256
257# 279 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
258
259# 295 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
260
261# 305 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
262
263# 312 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
264! New line at end of file is required for FYPP
265# 4 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp" 2
266
267! GPU parallel region (scalar reductions, maxval/minval)
268# 23 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
269
270! GPU parallel loop over threads (most common GPU macro)
271# 44 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
272
273! Required closing for GPU_PARALLEL_LOOP
274# 56 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
275
276! Mark routine for device compilation
277# 113 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
278
279! Declare device-resident data
280# 131 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
281
282! Inner loop within a GPU parallel region
283# 146 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
284
285! Scoped GPU data region
286# 165 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
287
288! Host code with device pointers (for MPI with GPU buffers)
289# 191 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
290
291! Allocate device memory (unscoped)
292# 205 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
293
294! Free device memory
295# 217 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
296
297! Atomic operation on device
298# 229 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
299
300! End atomic capture block
301# 240 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
302
303! Copy data between host and device
304# 252 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
305
306! Synchronization barrier
307# 264 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
308
309! Import GPU library module (openacc or omp_lib)
310# 273 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
311
312! Emit code only for AMD compiler
313# 280 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
314
315! Emit code for non-Cray compilers
316# 287 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
317
318! Emit code only for Cray compiler
319# 294 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
320
321! Cray compiler directive, emitted from Fypp so the formatter cannot turn it into a comment
322# 301 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
323
324! Emit code for non-NVIDIA compilers
325# 308 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
326
327# 310 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
328# 311 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
329! New line at end of file is required for FYPP
330# 2 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp" 2
331
332# 14 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
333
334! Caution: This macro requires the use of a binding script to set CUDA_VISIBLE_DEVICES, such that we have one GPU device per MPI
335! rank. That's because for both cudaMemAdvise (preferred location) and cudaMemPrefetchAsync we use location = device_id = 0. For an
336! example see misc/nvidia_uvm/bind.sh.
337# 52 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
338
339! Allocate and create GPU device memory
340# 72 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
341
342! Free GPU device memory and deallocate
343# 80 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
344
345! Cray-specific GPU pointer setup for vector fields
346# 104 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
347
348! Cray-specific GPU pointer setup for scalar fields
349# 120 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
350
351! Cray-specific GPU pointer teardown for scalar fields - exact inverse of ACC_SETUP_SFs. Removes the
352! present-table entries ACC_SETUP_SFs added (the scalar_field descriptor and its %sf copyin). REQUIRED when
353! an ACC_SETUP'd field is freed MID-RUN (e.g. AMR slot free on regrid/restart): Cray 'exit data delete'
354! decrements the reference counter, so the lone @:DEALLOCATE(arg%sf) only undoes the @:ALLOCATE create ref
355! and leaves the descriptor + the ACC_SETUP %sf copyin dangling. Call BEFORE @:DEALLOCATE(arg%sf).
356# 140 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
357
358! Cray-specific GPU pointer setup for acoustic source spatials
359# 165 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
360
361! Whether stress index idx is a shear component. Spelled out because NVHPC 26.9 at -O2 vectorizes any(idx == shear_indices)
362! inside a loop to always true, which doubled the normal-stress elastic energy.
363# 171 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
364
365# 177 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
366
367# 184 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
368! New line at end of file is required for FYPP
369# 9 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp" 2
370
371!> @brief Rank decomposition, box arithmetic and block ownership (SFC cut, owner lookups, owned-block lists).
373
374#ifdef MFC_MPI
375 use mpi !< mpi-io for the parallel_io amr restart file
376#endif
377
378 use m_derived_types ! scalar_field, t_box, int_bounds_info
379 use m_box, only: f_morton
381 use m_constants, only: k_ib
382 use m_mpi_proxy, only: s_mpi_abort
383 use m_ibm, only: ib_markers
384 use m_amr_state
385
386 implicit none
387
388 private
393
394contains
395
396 !> Rank r's coarse-grid decomposition (start_idx + local extent m/n/p), computed O(1) from its cartesian coords. The domain cart
397 !! is MPI_CART_CREATE(reorder=.false.), so ranks keep MPI_COMM_WORLD order and coords are row-major in r. The per-dim split is
398 !! the integer split-with-remainder of s_mpi_decompose_computational_domain.
399 pure subroutine s_amr_rank_decomp(r, sidx, ext)
400
401 integer, intent(in) :: r
402 integer, intent(out) :: sidx(3), ext(3)
403 integer :: coords(3), gd(3), pd(3), base, rem, d
404
405 gd(1) = m_glb; gd(2) = n_glb; gd(3) = p_glb
406 pd(1) = num_procs_x; pd(2) = num_procs_y; pd(3) = num_procs_z
407 ! row-major: r = cx*(py*pz) + cy*pz + cz (py=pz=1 when that dim is not decomposed)
408 coords(1) = r/(pd(2)*pd(3))
409 coords(2) = mod(r, pd(2)*pd(3))/pd(3)
410 coords(3) = mod(r, pd(3))
411 sidx = 0; ext = 0
412 do d = 1, num_dims
413 base = (gd(d) + 1)/pd(d)
414 rem = mod(gd(d) + 1, pd(d))
415 ext(d) = base - 1 + merge(1, 0, coords(d) < rem)
416 sidx(d) = coords(d)*base + min(coords(d), rem)
417 end do
418
419 end subroutine s_amr_rank_decomp
420
421 !> Guard: the computed decomposition must reproduce this rank's actual one (each rank checks itself, so all r are covered).
422 impure subroutine s_amr_validate_decomp()
423
424 integer :: sidx(3), ext(3)
425
426 call s_amr_rank_decomp(proc_rank, sidx, ext)
427 if (any(sidx /= amr_sidx .or. ext /= amr_ext)) then
428 call s_mpi_abort('s_amr_rank_decomp does not reproduce this rank''s decomposition - computed split disagrees with ' &
429 & // 's_mpi_decompose_computational_domain')
430 end if
431
432 end subroutine s_amr_validate_decomp
433
434 !> Closed-form inverse of the per-dim split in s_amr_rank_decomp: the cart coord owning global coarse cell g, with
435 !! base=(gd+1)/pd, rem=mod(gd+1,pd). Exact for g in [0,gd]; callers clamp for out-of-range g (ghost reach).
436 pure integer function f_amr_cell_coord(g, base, rem) result(c)
437
438 integer, intent(in) :: g, base, rem
439
440 if (g < rem*(base + 1)) then
441 c = g/(base + 1)
442 else
443 c = rem + (g - rem*(base + 1))/base
444 end if
445
446 end function f_amr_cell_coord
447
448 !> Per-dim contiguous rank-coord range [clo:chi] whose owned coarse slab meets box [blo:bhi], clamped to [0,pd-1]. The boundary
449 !! coords' coarse_range is extended by buff_size at the domain edge (s_amr_rank_coarse_range), and clamping reproduces both the
450 !! interior (scatter) and coarse_range (gather) sets. Collapsed dims contribute coord 0.
451 pure subroutine s_amr_coord_range(blo, bhi, clo, chi)
452
453 integer, intent(in) :: blo(3), bhi(3)
454 integer, intent(out) :: clo(3), chi(3)
455 integer :: gd(3), pd(3), base, rem, d
456
457 gd(1) = m_glb; gd(2) = n_glb; gd(3) = p_glb
458 pd(1) = num_procs_x; pd(2) = num_procs_y; pd(3) = num_procs_z
459 clo = 0; chi = 0
460 do d = 1, num_dims
461 base = (gd(d) + 1)/pd(d)
462 rem = mod(gd(d) + 1, pd(d))
463 clo(d) = min(max(f_amr_cell_coord(blo(d), base, rem), 0), pd(d) - 1)
464 chi(d) = min(max(f_amr_cell_coord(bhi(d), base, rem), 0), pd(d) - 1)
465 end do
466
467 end subroutine s_amr_coord_range
468
469 !> Ascending rank list overlapping coarse box [blo:bhi]: the coord brick is enumerated cx->cy->cz so r = cx*(Py*Pz)+cy*Pz+cz
470 !! ascends, reproducing the 0..num_procs-1 scan order. Owner not excluded. Caller sizes ranks(:) >= f_amr_overlap_count.
471 pure subroutine s_amr_ranks_overlapping(blo, bhi, ranks, nr)
472
473 integer, intent(in) :: blo(3), bhi(3)
474 integer, intent(out) :: ranks(:)
475 integer, intent(out) :: nr
476 integer :: clo(3), chi(3), cx, cy, cz, pyz
477
478 call s_amr_coord_range(blo, bhi, clo, chi)
479 pyz = num_procs_y*num_procs_z
480 nr = 0
481 do cx = clo(1), chi(1)
482 do cy = clo(2), chi(2)
483 do cz = clo(3), chi(3)
484 nr = nr + 1
485 ranks(nr) = cx*pyz + cy*num_procs_z + cz
486 end do
487 end do
488 end do
489
490 end subroutine s_amr_ranks_overlapping
491
492 !> Does rank r's subdomain overlap coarse box [blo:bhi]? Answers membership without enumerating the overlap set, which is
493 !! O(num_procs) for a box spanning the machine.
494 pure logical function f_amr_rank_overlaps(blo, bhi, r) result(hit)
495
496 integer, intent(in) :: blo(3), bhi(3), r
497 integer :: clo(3), chi(3), c(3)
498
499 call s_amr_coord_range(blo, bhi, clo, chi)
500 c(1) = r/(num_procs_y*num_procs_z) ! same coord -> rank map as the enumeration
501 c(2) = mod(r/num_procs_z, num_procs_y)
502 c(3) = mod(r, num_procs_z)
503 hit = all(c >= clo) .and. all(c <= chi)
504
505 end function f_amr_rank_overlaps
506
507 !> Overlap count only (allocation sizing), = product of the per-dim coord-range widths.
508 pure integer function f_amr_overlap_count(blo, bhi) result(nr)
509
510 integer, intent(in) :: blo(3), bhi(3)
511 integer :: clo(3), chi(3)
512
513 call s_amr_coord_range(blo, bhi, clo, chi)
514 nr = (chi(1) - clo(1) + 1)*(chi(2) - clo(2) + 1)*(chi(3) - clo(3) + 1)
515
516 end function f_amr_overlap_count
517
518 !> Rank r's contiguous owned coarse-cell range per dim: the interior plus buff_size ghosts where the subdomain touches the
519 !! domain edge. One span, so box intersections identify contributors without a per-cell scan.
520 pure subroutine s_amr_rank_coarse_range(r, crlo, crhi)
521
522 integer, intent(in) :: r
523 integer, intent(out) :: crlo(3), crhi(3)
524 integer :: sidx(3), ext(3)
525
526 call s_amr_rank_decomp(r, sidx, ext)
527 crlo = merge(merge(-buff_size, sidx, sidx == 0), 0, amr_dim)
528 crhi = merge(sidx + ext + merge(buff_size, 0, sidx + ext == [m_glb, n_glb, p_glb]), 0, amr_dim)
529
530 end subroutine s_amr_rank_coarse_range
531
532 !> Per-dim intersection of two global boxes [alo:ahi] and [blo:bhi] -> [olo:ohi] (empty when olo > ohi in some dim).
533 pure subroutine s_amr_box_isect(alo, ahi, blo, bhi, olo, ohi)
534
535 integer, intent(in) :: alo(3), ahi(3), blo(3), bhi(3)
536 integer, intent(out) :: olo(3), ohi(3)
537
538 olo = max(alo, blo); ohi = min(ahi, bhi)
539
540 end subroutine s_amr_box_isect
541
542 !> Per-block cost over each block's level-0 footprint (1 per cell + K_ib per IB-marked cell), summed over each rank's owned
543 !! coarse cells and replicated by one MPI_ALLREDUCE(SUM).
544 impure subroutine s_amr_block_cost(cost)
545
546 real(wp), intent(out) :: cost(:)
547 integer :: k, j, kk, l, lo(3), hi(3)
548 real(wp) :: c
549
550#ifdef MFC_MPI
551 integer :: ierr
552#endif
553
554 if (ib) then
555
556# 194 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
557#if defined(MFC_OpenACC)
558# 194 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
559!$acc update host(ib_markers%sf)
560# 194 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
561#elif defined(MFC_OpenMP)
562# 194 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
563!$omp target update from(ib_markers%sf)
564# 194 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
565#endif
566# 195 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
567 end if
568 do k = 1, amr_num_blocks
569 ! block footprint /\ this rank's coarse subdomain, in local interior indices (empty -> no-trip loops)
570 lo = merge(max(amr_region_lo_all(:,k) - amr_sidx, 0), 0, amr_dim)
571 hi = merge(min(amr_region_hi_all(:,k) - amr_sidx, amr_ext), 0, amr_dim)
572 c = 0._wp
573 do l = lo(3), hi(3)
574 do kk = lo(2), hi(2)
575 do j = lo(1), hi(1)
576 c = c + 1._wp
577 if (ib) then
578 if (ib_markers%sf(j, kk, l) /= 0) c = c + k_ib
579 end if
580 end do
581 end do
582 end do
583 cost(k) = c
584 end do
585#ifdef MFC_MPI
586 call mpi_allreduce(mpi_in_place, cost, amr_num_blocks, mpi_p, mpi_sum, mpi_comm_world, ierr)
587#endif
588
589 end subroutine s_amr_block_cost
590
591 !> Cost-weighted SFC partition of n items into num_procs contiguous Morton-key ranges: walk the key-sorted items (stable merge
592 !! sort) and advance the owner rank when the running weight crosses the next even share. Writes owner(1:n) and
593 !! cut(0:num_procs-1) (running Morton upper bound per rank; ranks with no item inherit the predecessor's bound, so cut is
594 !! non-decreasing).
595 subroutine s_amr_sfc_cut(keys, wt, n, cut, owner)
596
597 integer, intent(in) :: n
598 integer(kind=8), intent(in) :: keys(n)
599 real(wp), intent(in) :: wt(n)
600 integer(kind=8), intent(out) :: cut(0:num_procs - 1)
601 integer, intent(out) :: owner(n)
602 integer :: ord(n), k, r
603 real(wp) :: total, cum, tgt, tol
604
605 cut = -1_8
606 if (n < 1) return
607
608 call s_amr_sort_by_key(keys, n, ord)
609
610 total = 0._wp
611 do k = 1, n
612 total = total + wt(k)
613 end do
614
615 ! chains-on-chains in SFC order; replicated weights in a fixed order, so every rank computes the identical assignment
616 r = 0; cum = 0._wp
617 do k = 1, n
618 tgt = real(r + 1, wp)*total/real(num_procs, wp)
619 ! tolerance = accumulated rounding bound: at an exact share boundary cum and tgt differ by rounding, and a fractional
620 ! cost term could otherwise split identical weights unevenly (e.g. 5/3 instead of 4/4)
621 tol = spacing(tgt)*real(n, wp)
622 if (cum >= tgt - tol .and. r < num_procs - 1) r = r + 1
623 owner(ord(k)) = r
624 cut(r) = keys(ord(k)) ! items visited in ascending Morton key => running upper bound for rank r
625 cum = cum + wt(ord(k))
626 end do
627 ! ranks with no item inherit the predecessor's bound so the search never lands on them
628 do r = 1, num_procs - 1
629 if (cut(r) < cut(r - 1)) cut(r) = cut(r - 1)
630 end do
631
632 end subroutine s_amr_sfc_cut
633
634 !> ord(1:n) = item indices in ascending key order. Stable iterative bottom-up merge sort, a pure function of the input so every
635 !! rank derives identical orders from replicated keys (differing assignments would deadlock or abort).
636 pure subroutine s_amr_sort_by_key(keys, n, ord)
637
638 integer, intent(in) :: n
639 integer(kind=8), intent(in) :: keys(n)
640 integer, intent(out) :: ord(n)
641 integer :: mrg(n), k, width, lo_m, mid_m, hi_m, i_m, j_m, t_m
642
643 do k = 1, n
644 ord(k) = k
645 end do
646 width = 1
647 do while (width < n)
648 lo_m = 1
649 do while (lo_m <= n - width)
650 mid_m = lo_m + width - 1
651 hi_m = min(lo_m + 2*width - 1, n)
652 i_m = lo_m; j_m = mid_m + 1; t_m = lo_m
653 do while (i_m <= mid_m .and. j_m <= hi_m)
654 ! <= keeps the left run first on ties: stability
655 if (keys(ord(i_m)) <= keys(ord(j_m))) then
656 mrg(t_m) = ord(i_m); i_m = i_m + 1
657 else
658 mrg(t_m) = ord(j_m); j_m = j_m + 1
659 end if
660 t_m = t_m + 1
661 end do
662 do while (i_m <= mid_m); mrg(t_m) = ord(i_m); i_m = i_m + 1; t_m = t_m + 1; end do
663 do while (j_m <= hi_m); mrg(t_m) = ord(j_m); j_m = j_m + 1; t_m = t_m + 1; end do
664 ord(lo_m:hi_m) = mrg(lo_m:hi_m)
665 lo_m = lo_m + 2*width
666 end do
667 width = 2*width
668 end do
669
670 end subroutine s_amr_sort_by_key
671
672 !> Refresh the owned-block list if any owner write has happened since the last refresh. Lazy, since s_amr_assign_block_owners is
673 !! not the only writer of amr_block_owner.
674 impure subroutine s_amr_refresh_my_blocks()
675
676 integer :: b
677
678 if (.not. amr_myblk_dirty) return
679 if (allocated(amr_my_blk)) then
680 if (size(amr_my_blk) < amr_max_blocks) deallocate (amr_my_blk)
681 end if
682 if (.not. allocated(amr_my_blk)) allocate (amr_my_blk(amr_max_blocks))
683 amr_n_my = 0
684 do b = 1, amr_num_blocks
685 if (amr_block_owner(b) /= proc_rank) cycle
686 amr_n_my = amr_n_my + 1
688 end do
689 amr_myblk_dirty = .false.
690
691 end subroutine s_amr_refresh_my_blocks
692
693 !> Rebuild the epoch-keyed block lists when the mesh epoch has moved (one O(global blocks) walk per regrid, not per RK stage).
694 !! Also caches the parent index (f_amr_parent_block is an O(global blocks) scan) and the children adjacency used by
695 !! s_amr_sibling_face_weights.
696 impure subroutine s_amr_refresh_lists()
697
698 integer :: b, rlo(3), rhi(3), milo(3), mihi(3), bl(3), bh(3)
699 integer :: crlo(3), crhi(3), plo(3), phi(3), pl(3), ph(3), mar, pblk, acc, nxt
700
702# 331 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
703 if (allocated(amr_l1r_blk)) then
704# 332 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
705 if (size(amr_l1r_blk) < amr_max_blocks) deallocate (amr_l1r_blk)
706# 333 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
707 end if
708 if (.not. allocated(amr_l1r_blk)) allocate (amr_l1r_blk(amr_max_blocks))
709# 335 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
710# 331 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
711 if (allocated(amr_l1p_blk)) then
712# 332 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
713 if (size(amr_l1p_blk) < amr_max_blocks) deallocate (amr_l1p_blk)
714# 333 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
715 end if
716 if (.not. allocated(amr_l1p_blk)) allocate (amr_l1p_blk(amr_max_blocks))
717# 335 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
718# 331 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
719 if (allocated(amr_fch_blk)) then
720# 332 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
721 if (size(amr_fch_blk) < amr_max_blocks) deallocate (amr_fch_blk)
722# 333 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
723 end if
724 if (.not. allocated(amr_fch_blk)) allocate (amr_fch_blk(amr_max_blocks))
725# 335 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
726# 331 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
727 if (allocated(amr_own_blk)) then
728# 332 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
729 if (size(amr_own_blk) < amr_max_blocks) deallocate (amr_own_blk)
730# 333 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
731 end if
732 if (.not. allocated(amr_own_blk)) allocate (amr_own_blk(amr_max_blocks))
733# 335 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
734# 331 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
735 if (allocated(amr_parent_blk)) then
736# 332 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
737 if (size(amr_parent_blk) < amr_max_blocks) deallocate (amr_parent_blk)
738# 333 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
739 end if
740 if (.not. allocated(amr_parent_blk)) allocate (amr_parent_blk(amr_max_blocks))
741# 335 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
742# 331 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
743 if (allocated(amr_child_idx)) then
744# 332 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
745 if (size(amr_child_idx) < amr_max_blocks) deallocate (amr_child_idx)
746# 333 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
747 end if
748 if (.not. allocated(amr_child_idx)) allocate (amr_child_idx(amr_max_blocks))
749# 335 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
750# 336 "/home/runner/work/MFC/MFC/src/simulation/m_amr_distribution.fpp"
751 if (allocated(amr_child_ptr)) then
752 if (size(amr_child_ptr) < amr_max_blocks + 1) deallocate (amr_child_ptr)
753 end if
754 if (.not. allocated(amr_child_ptr)) allocate (amr_child_ptr(0:amr_max_blocks))
755 call s_amr_rank_interior(proc_rank, milo, mihi)
756 call s_amr_rank_coarse_range(proc_rank, crlo, crhi)
757 mar = amr_cpat_mar
758 amr_n_l1p = 0
759 amr_n_l1r = 0
760 amr_n_fch = 0
761 amr_n_own = 0
763 do b = 1, amr_num_blocks
764 if (amr_owns_all(b)) then
765 amr_n_own = amr_n_own + 1
767 end if
768 amr_parent_blk(b) = 0
769 if (amr_block_level(b) >= 2) then
770 pblk = f_amr_parent_block(b)
771 amr_parent_blk(b) = pblk
772 ! pblk == 0 is impossible on a nested mesh, but counting it would land in amr_child_ptr(0) and corrupt the lists
773 if (pblk > 0) then
774 amr_child_ptr(pblk) = amr_child_ptr(pblk) + 1
775 if (amr_block_owner(pblk) == proc_rank .and. amr_block_owner(b) /= proc_rank) then
776 amr_n_fch = amr_n_fch + 1
778 end if
779 end if
780 cycle
781 end if
782 if (amr_block_level(b) /= 1) cycle
783 if (amr_block_owner(b) == proc_rank) cycle
784 call s_amr_region_box(b, rlo, rhi)
785 call s_amr_box_isect(rlo, rhi, milo, mihi, bl, bh)
786 if (all(bl <= bh)) then
787 amr_n_l1r = amr_n_l1r + 1
789 end if
790 call s_amr_patch_box(b, plo, phi)
791 call s_amr_box_isect(plo, phi, crlo, crhi, pl, ph)
792 if (all(pl <= ph)) then
793 amr_n_l1p = amr_n_l1p + 1
795 end if
796 end do
797 ! counts -> exclusive prefix, then the ascending fill bumps each amr_child_ptr(p) back to p's inclusive end
798 acc = 0
799 do b = 1, amr_num_blocks
800 nxt = amr_child_ptr(b); amr_child_ptr(b) = acc; acc = acc + nxt
801 end do
802 do b = 1, amr_num_blocks
803 if (amr_block_level(b) < 2) cycle
804 pblk = amr_parent_blk(b)
805 if (pblk == 0) cycle ! uncounted above; a bump here would clobber a real entry
806 amr_child_ptr(pblk) = amr_child_ptr(pblk) + 1
808 end do
811
812 end subroutine s_amr_refresh_lists
813
814 !> Coarse-side overlap per rank of block k's region grown by g coarse cells: level 1 against the level-0 rank bricks, level >= 2
815 !! against the owners of the level-(l-1) blocks. Adds into vol(0:) and lists touched ranks in rk(1:nr); the caller zeroes
816 !! vol(rk(1:nr)) after use.
817 impure subroutine s_amr_place_overlap(k, g, vol, rk, nr)
818
819 integer, intent(in) :: k, g
820 real(wp), intent(inout) :: vol(0:)
821 integer, intent(out) :: rk(:), nr
822 integer :: lo(3), hi(3), ilo(3), ihi(3), ol(3), oh(3), j, q
823
824 lo = amr_region_lo_all(:,k) - merge(g, 0, amr_dim); hi = amr_region_hi_all(:,k) + merge(g, 0, amr_dim)
825 nr = 0
826 if (amr_block_level(k) == 1) then
827 call s_amr_ranks_overlapping(lo, hi, rk, nr)
828 do j = 1, nr
829 call s_amr_rank_interior(rk(j), ilo, ihi)
830 call s_amr_box_isect(lo, hi, ilo, ihi, ol, oh)
831 vol(rk(j)) = product(real(max(oh - ol + 1, 0), wp))
832 end do
833 return
834 end if
835 do j = 1, amr_num_blocks
836 if (amr_block_level(j) /= amr_block_level(k) - 1) cycle
837 call s_amr_box_isect(lo, hi, amr_region_lo_all(:,j), amr_region_hi_all(:,j), ol, oh)
838 if (any(ol > oh)) cycle
839 q = amr_block_owner(j)
840 if (vol(q) <= 0._wp) then
841 nr = nr + 1; rk(nr) = q
842 end if
843 vol(q) = vol(q) + product(real(oh - ol + 1, wp))
844 end do
845
846 end subroutine s_amr_place_overlap
847
848 !> Owner-aware placement (amr_place_owner) of one level's na blocks (slots aidx, weights wt, Morton keys key): each block goes
849 !! to the rank owning most of the coarse cells under it. A rank above (1 + place_tol) x the mean sheds its last blocks in Morton
850 !! order, first to the least-loaded rank next to the block, then to the least-loaded rank anywhere, never pushing the receiver
851 !! above the cap. Replicated inputs in a fixed order, so every rank computes the same owners.
852 impure subroutine s_amr_place_owner(key, wt, aidx, na, own)
853
854 integer, intent(in) :: na, aidx(na)
855 integer(kind=8), intent(in) :: key(na)
856 real(wp), intent(in) :: wt(na)
857 integer, intent(inout) :: own(na)
858 real(wp), parameter :: place_tol = 0.05_wp
859 real(wp), allocatable :: vol(:), load(:)
860 integer, allocatable :: rk(:), ord(:)
861 integer :: a, b, j, r, m, nr, pass
862 real(wp) :: cap
863
864 allocate (vol(0:num_procs - 1), load(0:num_procs - 1), rk(num_procs), ord(na))
865 vol = 0._wp; load = 0._wp
866 do a = 1, na
867 call s_amr_place_overlap(aidx(a), 0, vol, rk, nr)
868 do j = 1, nr ! nr = 0 (no coarse owner found) keeps the SFC owner
869 if (j == 1) own(a) = rk(1)
870 if (vol(rk(j)) > vol(own(a)) .or. (vol(rk(j)) == vol(own(a)) .and. rk(j) < own(a))) own(a) = rk(j)
871 end do
872 vol(rk(1:nr)) = 0._wp
873 load(own(a)) = load(own(a)) + wt(a)
874 end do
875
876 cap = (1._wp + place_tol)*sum(load)/real(num_procs, wp)
877 call s_amr_sort_by_key(key, na, ord)
878 do pass = 1, 2 ! 1: to a coarse neighbour, 2: anywhere
879 do r = 0, num_procs - 1
880 do b = na, 1, -1
881 if (load(r) <= cap) exit
882 a = ord(b)
883 if (own(a) /= r) cycle
884 if (pass == 1) then
885 call s_amr_place_overlap(aidx(a), 1, vol, rk, nr)
886 vol(rk(1:nr)) = 0._wp
887 m = -1
888 do j = 1, nr
889 if (rk(j) == r) cycle
890 if (m < 0) then
891 m = rk(j)
892 else if (load(rk(j)) < load(m) .or. (load(rk(j)) == load(m) .and. rk(j) < m)) then
893 m = rk(j)
894 end if
895 end do
896 else
897 m = minloc(load, 1) - 1
898 end if
899 if (m < 0) cycle
900 if (load(m) + wt(a) > cap) cycle
901 own(a) = m; load(r) = load(r) - wt(a); load(m) = load(m) + wt(a)
902 end do
903 end do
904 end do
905 deallocate (vol, load, rk, ord)
906
907 end subroutine s_amr_place_owner
908
909 impure subroutine s_amr_assign_block_owners()
910
911 integer :: k, a, lev, maxlev, na
912 ! heap, not stack: O(global boxes)
913 integer, allocatable :: aidx(:), aown(:)
914 integer(kind=8), allocatable :: key(:), akey(:)
915 real(wp), allocatable :: wt(:), cost(:), awt(:)
916
917 if (amr_num_blocks < 1) return
918
919 allocate (aidx(amr_num_blocks), aown(amr_num_blocks), key(amr_num_blocks), akey(amr_num_blocks), wt(amr_num_blocks), &
920 & cost(amr_num_blocks), awt(amr_num_blocks))
921
922 call s_amr_block_cost(cost)
923
924 ! per-block fine-work weight = footprint cost x amr_ref_ratio**(level*active dims)
925 do k = 1, amr_num_blocks
926 wt(k) = cost(k)*real(amr_ref_ratio, wp)**amr_block_level(k)
927 if (n_glb > 0) wt(k) = wt(k)*real(amr_ref_ratio, wp)**amr_block_level(k)
928 if (p_glb > 0) wt(k) = wt(k)*real(amr_ref_ratio, wp)**amr_block_level(k)
930 end do
931
932 ! Per-level distribution: one cut per level on each block's own weight, so a deep tower does not pin its subtree to one
933 ! rank. Not one mixed cut: same-level boxes have distinct Morton keys, which f_amr_owner's cut search needs. amr_fine_cut(:,
934 ! lev) holds each level's cut.
935 maxlev = maxval(amr_block_level(1:amr_num_blocks))
936 do lev = 1, maxlev
937 na = 0
938 do k = 1, amr_num_blocks
939 if (amr_block_level(k) /= lev) cycle
940 na = na + 1
941 akey(na) = key(k); awt(na) = wt(k); aidx(na) = k
942 end do
943 if (na < 1) cycle
944 call s_amr_sfc_cut(akey, awt, na, amr_fine_cut(:,lev), aown)
945 if (amr_place_owner) call s_amr_place_owner(akey, awt, aidx, na, aown)
946 do a = 1, na
947 amr_block_owner(aidx(a)) = aown(a); amr_myblk_dirty = .true.
948 end do
949 end do
950
952 call s_amr_report_balance(wt, maxlev)
953
954 deallocate (aidx, aown, key, akey, wt, cost, awt)
955
956 end subroutine s_amr_assign_block_owners
957
958 !> Install a recorded owner table instead of recomputing it (restart at the writer's rank count): the cost weights read live
959 !! state (body markers) that differs between the regrid before the write and the restart. Each level's cut is the running max
960 !! Morton key of each rank's blocks; s_amr_validate_owner rejects a table not contiguous in that order (unless amr_place_owner).
961 impure subroutine s_amr_adopt_block_owners(own)
962
963 integer, intent(in) :: own(:)
964 integer :: k, r, lev, maxlev
965 integer(kind=8) :: key
966
967 maxlev = maxval(amr_block_level(1:amr_num_blocks))
968 do lev = 1, maxlev
969 amr_fine_cut(:,lev) = -1_8
970 do k = 1, amr_num_blocks
971 if (amr_block_level(k) /= lev) cycle
973 amr_fine_cut(own(k), lev) = max(amr_fine_cut(own(k), lev), key)
974 amr_block_owner(k) = own(k); amr_myblk_dirty = .true.
975 end do
976 do r = 1, num_procs - 1
977 if (amr_fine_cut(r, lev) < amr_fine_cut(r - 1, lev)) amr_fine_cut(r, lev) = amr_fine_cut(r - 1, lev)
978 end do
979 end do
981
982 end subroutine s_amr_adopt_block_owners
983
984 !> Per-level and total load-balance report (rank 0, rank_time_wrt): max/mean assigned block weight over ranks (1 = perfect).
985 !! no_blocks_ranks is the granularity floor of a level with fewer boxes than ranks, not idleness.
986 impure subroutine s_amr_report_balance(wt, maxlev)
987
988 real(wp), intent(in) :: wt(:)
989 integer, intent(in) :: maxlev
990 real(wp), allocatable :: rw(:), tw(:), rc(:)
991 real(wp) :: mx, mean, cmx, cmean
992 integer :: k, lev, nb, empty
993
994 if (.not. rank_time_wrt .or. proc_rank /= 0) return
995
996 ! heap: num_procs long
997 allocate (rw(0:num_procs - 1), tw(0:num_procs - 1), rc(0:num_procs - 1))
998 tw = 0._wp
999 do lev = 1, maxlev
1000 rw = 0._wp; rc = 0._wp
1001 nb = 0
1002 do k = 1, amr_num_blocks
1003 if (amr_block_level(k) /= lev) cycle
1004 rw(amr_block_owner(k)) = rw(amr_block_owner(k)) + wt(k)
1005 rc(amr_block_owner(k)) = rc(amr_block_owner(k)) + 1._wp
1006 nb = nb + 1
1007 end do
1008 if (nb == 0) cycle
1009 tw = tw + rw
1010 mx = maxval(rw); mean = sum(rw)/real(num_procs, wp)
1011 empty = count(rw <= 0._wp)
1012 ! box-count imbalance beside weight: a block's advance cost has a large fixed component
1013 cmx = maxval(rc); cmean = sum(rc)/real(num_procs, wp)
1014 if (mean > 0._wp) print '(A,I0,A,I0,A,I0,A,F8.3,A,F8.3,A,I0,A,I0)', ' [amr-balance] level ', lev, ': boxes ', nb, &
1015 & '/ranks ', num_procs, ' max/mean ', mx/mean, ' boxes_max/mean ', cmx/cmean, ' no_blocks_ranks ', empty, ' of ', &
1016 & num_procs
1017 end do
1018 ! per-rank weight (fine cells)
1019 write (*, '(A)', advance='no') ' [amr-balance] per-rank fine_work :'
1020 do k = 0, num_procs - 1; write (*, '(I12)', advance='no') nint(tw(k), kind=8); end do
1021 write (*, '(A)') ''
1022 mean = sum(tw)/real(num_procs, wp)
1023 ! fine_work = sum of assigned weights = fine cells advanced per step
1024 if (mean > 0._wp) print '(A,F8.3,A,I0,A,I0)', ' [amr-balance] TOTAL : max/mean ', maxval(tw)/mean, &
1025 & ' ranks_with_no_fine_block ', count(tw <= 0._wp), ' fine_work ', nint(sum(tw), kind=8)
1026 deallocate (rw, tw, rc)
1027
1028 end subroutine s_amr_report_balance
1029
1030 !> Owner rank of block k by binary search of its Morton key in its level's cut amr_fine_cut(:, level); reproduces
1031 !! s_amr_assign_block_owners' assignment exactly.
1032 pure integer function f_amr_owner(k) result(r)
1033
1034 integer, intent(in) :: k
1035 integer(kind=8) :: mk, cut(0:num_procs - 1)
1036 integer :: lo, hi, mid
1037
1040 lo = 0; hi = num_procs - 1
1041 do while (lo < hi)
1042 mid = (lo + hi)/2
1043 if (mk <= cut(mid)) then
1044 hi = mid
1045 else
1046 lo = mid + 1
1047 end if
1048 end do
1049 r = lo
1050
1051 end function f_amr_owner
1052
1053 !> Guard: the SFC cut-point accessor must reproduce the stored owner table exactly.
1054 impure subroutine s_amr_validate_owner()
1055
1056 integer :: k
1057
1058 do k = 1, amr_num_blocks
1059 if (amr_place_owner) cycle ! placed owners are not SFC-contiguous
1060 if (f_amr_owner(k) /= amr_block_owner(k)) &
1061 & call s_mpi_abort('SFC cut-point owner disagrees with amr_block_owner - cut capture or search is wrong')
1062 end do
1063
1064 end subroutine s_amr_validate_owner
1065
1066 !> Set this rank's intersection of box lo:hi with its subdomain (amr_isect_lo/hi) and whether it holds fine cells
1067 !! (amr_rank_owns_block: nonempty in all active dims). Call with the coarse grid state in m/n/p, never inside the fine advance.
1068 impure subroutine s_amr_compute_isect(lo, hi)
1069
1070 integer, intent(in) :: lo(3), hi(3)
1071
1072 amr_isect_lo = max(lo, amr_sidx); amr_isect_hi = min(hi, amr_sidx + amr_ext)
1074
1075 end subroutine s_amr_compute_isect
1076
1077 !> Rank r's coarse interior box (global, no ghosts) from s_amr_rank_decomp; restriction targets are identified by interior
1078 !! overlap alone.
1079 pure subroutine s_amr_rank_interior(r, ilo, ihi)
1080
1081 integer, intent(in) :: r
1082 integer, intent(out) :: ilo(3), ihi(3)
1083 integer :: sidx(3), ext(3)
1084
1085 call s_amr_rank_decomp(r, sidx, ext)
1086 ilo = merge(sidx, 0, amr_dim); ihi = merge(sidx + ext, 0, amr_dim)
1087
1088 end subroutine s_amr_rank_interior
1089
1090 !> Margin-padded global coarse-index bounding box of immersed body i (supported analytic geometries only; aborts on others).
1091 !! Reads the body's live centroid and orientation, so a moving body's box tracks it.
1092 impure subroutine s_amr_body_bbox(i, mrg, blo, bhi)
1093
1094 integer, intent(in) :: i, mrg
1095 integer, intent(out) :: blo(3), bhi(3)
1096 real(wp) :: c(3), half(3)
1097
1098 c = [patch_ib(i)%x_centroid, patch_ib(i)%y_centroid, patch_ib(i)%z_centroid]
1099 select case (patch_ib(i)%geometry)
1100 case (2, 8) ! circle, sphere
1101 half = patch_ib(i)%radius
1102 case (3, 9, 10) ! rectangle, box, cylinder (axis x): body-frame half-extents, rotated by the body's angles
1103 if (patch_ib(i)%geometry == 10) then
1104 half = [0.5_wp*patch_ib(i)%length_x, patch_ib(i)%radius, patch_ib(i)%radius]
1105 else
1106 half = 0.5_wp*[patch_ib(i)%length_x, patch_ib(i)%length_y, patch_ib(i)%length_z]
1107 end if
1108 half = matmul(abs(patch_ib(i)%rotation_matrix), half)
1109 case default
1110 call s_mpi_abort('amr dynamic regrid with ib: unsupported body geometry for the ' &
1111 & // 'containment bounding box (supported: circle/rectangle/sphere/box/cylinder)')
1112 end select
1113 call s_amr_phys_to_cells(c - half, c + half, mrg, blo, bhi)
1114
1115 end subroutine s_amr_body_bbox
1116
1117 !> Expand a candidate regrid box (global indices) to fully contain every immersed body it overlaps, with a margin; aborts if a
1118 !! body exceeds the per-rank block cap.
1119 impure subroutine s_amr_expand_box_over_bodies(lo, hi)
1120
1121 integer, intent(inout) :: lo(3), hi(3)
1122 integer :: i, blo(3), bhi(3), mrg
1123
1124 ! margin: the IB image-point stencil reaches a few cells past the surface (buff_size would exceed the block cap). For
1125 ! amr_max_level > 1 add amr_cpat_mar per nesting level so the finest C/F boundary stays a stencil off the surface.
1126
1127 mrg = max(amr_buf, 4) + max(0, amr_max_level - 1)*amr_cpat_mar
1128
1129 do i = 1, num_ibs
1130 call s_amr_body_bbox(i, mrg, blo, bhi)
1131 ! blocks stay buff_size inside the domain: fail with a named message instead of a clipped body
1132 if (any(amr_dim .and. (blo < buff_size .or. bhi > [m_glb, n_glb, p_glb] - buff_size))) then
1133 call s_mpi_abort('amr dynamic regrid with ib: the immersed body plus its containment ' &
1134 & // 'margin does not fit inside the refinable domain interior (blocks stay buff_size off the edges)')
1135 end if
1136 if (.not. f_amr_boxes_overlap(lo, hi, blo, bhi)) cycle
1137 lo = min(lo, blo); hi = max(hi, bhi)
1138 if (any(amr_dim .and. hi - lo + 1 > amr_maxc_fit)) then
1139 call s_mpi_abort('amr dynamic regrid with ib: containing the immersed body plus margin ' &
1140 & // 'exceeds the per-rank block size cap; use fewer ranks or a larger amr_maxc_fit')
1141 end if
1142 end do
1143
1144 end subroutine s_amr_expand_box_over_bodies
1145
1146 !> Split box [lo:hi] into an even grid of adjacent sub-boxes each <= amr_maxc_fit per dim (the max a rank can whole-own),
1147 !! appended to out(nt+1:); sets capped=1 and stops if the cap is hit. Collapsed dims stay [0:0].
1148 pure subroutine s_amr_tile_box(lo, hi, out, nt, cap, capped, tsz)
1149
1150 integer, intent(in) :: lo(3), hi(3), cap
1151 type(t_box), intent(inout) :: out(:)
1152 integer, intent(inout) :: nt, capped
1153 !> per-dim tile size (default amr_maxc_fit)
1154 integer, intent(in), optional :: tsz(3)
1155 integer :: ntl(3), s(3), t1, t2, t3, qlo(3), qhi(3), tc(3)
1156
1157 tc = amr_maxc_fit; if (present(tsz)) tc = tsz
1158 tc = max(tc, 1) ! a 0 tile size would divide by zero; 1 cell is the floor
1159 ntl = (hi - lo + tc)/tc; s = (hi - lo + ntl)/ntl ! a collapsed dim ([0:0]) tiles once with size 1
1160 do t3 = 0, ntl(3) - 1
1161 qlo(3) = lo(3) + t3*s(3); qhi(3) = min(lo(3) + (t3 + 1)*s(3) - 1, hi(3))
1162 do t2 = 0, ntl(2) - 1
1163 qlo(2) = lo(2) + t2*s(2); qhi(2) = min(lo(2) + (t2 + 1)*s(2) - 1, hi(2))
1164 do t1 = 0, ntl(1) - 1
1165 if (nt >= cap) then; capped = 1; return; end if
1166 qlo(1) = lo(1) + t1*s(1); qhi(1) = min(lo(1) + (t1 + 1)*s(1) - 1, hi(1))
1167 nt = nt + 1; out(nt)%lo = qlo; out(nt)%hi = qhi
1168 end do
1169 end do
1170 end do
1171
1172 end subroutine s_amr_tile_box
1173
1174end module m_amr_distribution
integer, intent(in) k
integer, intent(in) j
integer, intent(in) l
Rank decomposition, box arithmetic and block ownership (SFC cut, owner lookups, owned-block lists).
impure subroutine, public s_amr_refresh_my_blocks()
Refresh the owned-block list if any owner write has happened since the last refresh....
pure integer function, public f_amr_overlap_count(blo, bhi)
Overlap count only (allocation sizing), = product of the per-dim coord-range widths.
impure subroutine, public s_amr_body_bbox(i, mrg, blo, bhi)
Margin-padded global coarse-index bounding box of immersed body i (supported analytic geometries only...
pure integer function f_amr_owner(k)
Owner rank of block k by binary search of its Morton key in its level's cut amr_fine_cut(:,...
impure subroutine s_amr_block_cost(cost)
Per-block cost over each block's level-0 footprint (1 per cell + K_ib per IB-marked cell),...
pure subroutine s_amr_coord_range(blo, bhi, clo, chi)
Per-dim contiguous rank-coord range [clo:chi] whose owned coarse slab meets box [blo:bhi],...
subroutine, public s_amr_sfc_cut(keys, wt, n, cut, owner)
Cost-weighted SFC partition of n items into num_procs contiguous Morton-key ranges: walk the key-sort...
impure subroutine, public s_amr_adopt_block_owners(own)
Install a recorded owner table instead of recomputing it (restart at the writer's rank count): the co...
impure subroutine s_amr_place_overlap(k, g, vol, rk, nr)
Coarse-side overlap per rank of block k's region grown by g coarse cells: level 1 against the level-0...
pure subroutine, public s_amr_tile_box(lo, hi, out, nt, cap, capped, tsz)
Split box [lo:hi] into an even grid of adjacent sub-boxes each <= amr_maxc_fit per dim (the max a ran...
pure subroutine, public s_amr_sort_by_key(keys, n, ord)
ord(1:n) = item indices in ascending key order. Stable iterative bottom-up merge sort,...
impure subroutine, public s_amr_compute_isect(lo, hi)
Set this rank's intersection of box lo:hi with its subdomain (amr_isect_lo/hi) and whether it holds f...
impure subroutine, public s_amr_refresh_lists()
Rebuild the epoch-keyed block lists when the mesh epoch has moved (one O(global blocks) walk per regr...
impure subroutine, public s_amr_validate_decomp()
Guard: the computed decomposition must reproduce this rank's actual one (each rank checks itself,...
pure subroutine, public s_amr_rank_decomp(r, sidx, ext)
Rank r's coarse-grid decomposition (start_idx + local extent m/n/p), computed O(1) from its cartesian...
impure subroutine, public s_amr_assign_block_owners()
impure subroutine s_amr_place_owner(key, wt, aidx, na, own)
Owner-aware placement (amr_place_owner) of one level's na blocks (slots aidx, weights wt,...
pure subroutine, public s_amr_rank_interior(r, ilo, ihi)
Rank r's coarse interior box (global, no ghosts) from s_amr_rank_decomp; restriction targets are iden...
pure logical function, public f_amr_rank_overlaps(blo, bhi, r)
Does rank r's subdomain overlap coarse box [blo:bhi]? Answers membership without enumerating the over...
impure subroutine, public s_amr_validate_owner()
Guard: the SFC cut-point accessor must reproduce the stored owner table exactly.
pure subroutine, public s_amr_ranks_overlapping(blo, bhi, ranks, nr)
Ascending rank list overlapping coarse box [blo:bhi]: the coord brick is enumerated cx->cy->cz so r =...
pure subroutine, public s_amr_rank_coarse_range(r, crlo, crhi)
Rank r's contiguous owned coarse-cell range per dim: the interior plus buff_size ghosts where the sub...
pure subroutine, public s_amr_box_isect(alo, ahi, blo, bhi, olo, ohi)
Per-dim intersection of two global boxes [alo:ahi] and [blo:bhi] -> [olo:ohi] (empty when olo > ohi i...
pure integer function f_amr_cell_coord(g, base, rem)
Closed-form inverse of the per-dim split in s_amr_rank_decomp: the cart coord owning global coarse ce...
impure subroutine s_amr_report_balance(wt, maxlev)
Per-level and total load-balance report (rank 0, rank_time_wrt): max/mean assigned block weight over ...
impure subroutine, public s_amr_expand_box_over_bodies(lo, hi)
Expand a candidate regrid box (global indices) to fully contain every immersed body it overlaps,...
Module-scope AMR state (block pool, flat field store, wave buffers) and the leaf helpers every AMR mo...
integer amr_cpat_mar
coarse-cell stencil reach (f_amr_cpat_mar)
logical amr_myblk_dirty
Set wherever amr_block_owner is written (assigner, inheritance, restart/migration),...
integer(8) amr_l1r_epoch
pure subroutine s_amr_phys_to_cells(pmin, pmax, pad_cells, blo, bhi)
Convert a physical-space bbox to a global coarse-index bbox padded by pad_cells (uniform spacing; the...
integer amr_l1r_nblk
second key half, matching s_amr_reg_prepare's (epoch, num_blocks) pair
integer, dimension(:), allocatable amr_child_ptr
children of p = amr_child_idx(amr_child_ptr(p-1)+1 : amr_child_ptr(p)), ascending
pure subroutine s_amr_patch_box(k, plo, phi)
Block k's padded coarse patch box [plo, phi] (region +/- amr_cpat_mar; collapsed dims 0:0).
integer, dimension(:), allocatable amr_child_idx
(amr_child_ptr(0) = 0)
integer(kind=8), dimension(:,:), allocatable amr_fine_cut
(0:num_procs-1, 1:amr_max_level) SFC Morton-key upper bound per rank, one independent cost-weighted c...
integer, dimension(:), allocatable amr_fch_blk
foreign children of my owned parents (level >= 2, powner == me, cowner /= me)
integer, dimension(:), allocatable amr_parent_blk
cached f_amr_parent_block (0 for level <= 1), avoiding an O(global blocks) scan per block per stage
pure integer function f_amr_parent_block(k)
Index of the level-(level(k)-1) block that block k refines, or 0 for level 1 (parent is the L0 grid)....
pure subroutine s_amr_region_box(k, rlo, rhi)
Block k's region box in global coarse cells; collapsed dimensions read 0:0.
pure logical function f_amr_boxes_overlap(alo, ahi, blo, bhi)
Do two coarse-index boxes [alo:ahi] and [blo:bhi] overlap? Collapsed dims never disqualify.
integer, dimension(:), allocatable amr_own_blk
blocks this rank intersects (amr_owns_all, the multi-owner notion, all levels)
integer, dimension(:), allocatable amr_l1r_blk
Level-1 blocks this rank receives restriction for (not its own, overlapping its interior),...
integer, dimension(:), allocatable amr_l1p_blk
padded variant (region +/- amr_cpat_mar vs my coarse range): stage-fill gather
integer, dimension(:), allocatable amr_my_blk
Block indices this rank owns, ascending (the paired MPI_SENDRECVs depend on the order); built in s_am...
integer, dimension(3) amr_maxc_fit
Regrid box size cap per dim (fixed for the run, identical on all ranks; 1 in collapsed dims): a box o...
Box type, per-rank partition arithmetic (one box per rank) and the Morton key.
pure integer(kind=8) function, public f_morton(ix, iy, iz)
3D Morton (Z-order) key of (ix, iy, iz), 21 bits/dim; negative coords clamp to 0.
Compile-time constant parameters: default values, tolerances, and physical constants.
real(wp), parameter k_ib
AMR block-owner cost per IB cell, relative to a fluid cell.
Shared derived types for field data, patch geometry, bubble dynamics, and MPI I/O structures.
Global parameters for the computational domain, fluid properties, and simulation algorithm configurat...
integer, dimension(3) amr_sidx
integer, dimension(:,:), allocatable amr_region_lo_all
Per-slot mirrors (allocated 1:amr_max_blocks by m_amr): region box, rank intersection and ownership f...
integer proc_rank
Rank of the local processor.
integer(8) amr_mesh_epoch
Monotone mesh epoch, incremented wherever amr_seam_pairs_dirty is set and at the end of each slot rec...
integer, dimension(3) amr_isect_lo
The block's coarse footprint driving the coarse<->fine gather/scatter, per dim (collapsed dims 0:0): ...
integer, dimension(:,:), allocatable amr_region_hi_all
logical, dimension(:), allocatable amr_owns_all
integer, dimension(:), allocatable amr_block_level
Refinement level of each active block (1..amr_max_level) and the deepest level populated....
integer buff_size
Number of ghost cells for boundary condition storage.
logical, dimension(3) amr_dim
The active dimensions, and this rank's coarse subdomain origin and extent padded to 3-vectors (0 in c...
integer, dimension(3) amr_isect_hi
integer num_procs
Number of processors.
integer, dimension(:), allocatable amr_block_owner
Fine-level distribution map: SFC/work-balanced single-owner rank per active block,...
integer amr_num_blocks
Number of active AMR fine-block slots (>= 1) and the working slot index selecting which one the per-b...
integer, dimension(3) amr_ext
logical amr_rank_owns_block
true on the current block's single owner rank: amr_block_owner(amr_cur) == proc_rank; kept by s_set_a...
Ghost-node immersed boundary method: locates ghost/image points, computes interpolation coefficients,...
type(integer_field), public ib_markers
MPI halo exchange, domain decomposition, and buffer packing/unpacking for the simulation solver.
An index-space rectangle in global cell indices. In v1, one t_box = one rank's subdomain....