MFC
Exascale flow solver
Loading...
Searching...
No Matches
m_amr_cluster.fpp.f90
Go to the documentation of this file.
1# 1 "/home/runner/work/MFC/MFC/src/simulation/m_amr_cluster.fpp"
2!>
3!!@file
4!!@brief Contains module m_amr_cluster
5
6# 1 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp" 1
7# 1 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp" 1
8# 1 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 1
9# 2 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
10# 3 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
11# 4 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
12# 5 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
13# 6 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
14
15# 8 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
16# 9 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
17# 10 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
18
19# 12 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
20# 1 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp" 1
21! Default for Fypp runs outside the toolchain (e.g. documentation). Searched last, so the
22! build/include/<target>/thermochem.fpp the toolchain writes from m_thermochem takes precedence.
23# 4 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
24# 5 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
25# 13 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 2
26
27# 16 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
28# 17 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
29
30# 24 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
31
32# 53 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
33
34# 65 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
35
36# 75 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
37
38# 105 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
39
40# 117 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
41
42# 127 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
43
44# 174 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
45! New line at end of file is required for FYPP
46# 2 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp" 2
47# 1 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp" 1
48# 1 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 1
49# 2 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
50# 3 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
51# 4 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
52# 5 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
53# 6 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
54
55# 8 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
56# 9 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
57# 10 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
58
59# 12 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
60# 1 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp" 1
61! Default for Fypp runs outside the toolchain (e.g. documentation). Searched last, so the
62! build/include/<target>/thermochem.fpp the toolchain writes from m_thermochem takes precedence.
63# 4 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
64# 5 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
65# 13 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 2
66
67# 16 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
68# 17 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
69
70# 24 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
71
72# 53 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
73
74# 65 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
75
76# 75 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
77
78# 105 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
79
80# 117 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
81
82# 127 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
83
84# 174 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
85! New line at end of file is required for FYPP
86# 2 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp" 2
87
88# 4 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
89# 5 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
90# 6 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
91# 7 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
92# 8 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
93
94# 20 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
95
96# 49 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
97
98# 54 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
99
100# 59 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
101
102# 64 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
103
104# 69 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
105
106# 74 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
107
108# 87 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
109
110# 101 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
111
112# 106 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
113
114# 111 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
115
116# 116 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
117
118# 121 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
119
120# 126 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
121
122# 131 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
123
124# 136 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
125
126# 141 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
127
128# 171 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
129
130# 231 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
131
132# 245 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
133
134# 270 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
135
136# 281 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
137
138# 283 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
139# 294 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
140
141# 343 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
142
143# 353 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
144
145# 363 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
146
147# 372 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
148
149# 389 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
150
151# 399 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
152
153# 406 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
154
155# 412 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
156
157# 418 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
158
159# 424 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
160
161# 430 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
162
163# 436 "/home/runner/work/MFC/MFC/src/common/include/omp_macros.fpp"
164! New line at end of file is required for FYPP
165# 3 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp" 2
166# 1 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp" 1
167# 1 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 1
168# 2 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
169# 3 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
170# 4 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
171# 5 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
172# 6 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
173
174# 8 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
175# 9 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
176# 10 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
177
178# 12 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
179# 1 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp" 1
180! Default for Fypp runs outside the toolchain (e.g. documentation). Searched last, so the
181! build/include/<target>/thermochem.fpp the toolchain writes from m_thermochem takes precedence.
182# 4 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
183# 5 "/home/runner/work/MFC/MFC/src/common/include/defaults/thermochem.fpp"
184# 13 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp" 2
185
186# 16 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
187# 17 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
188
189# 24 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
190
191# 53 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
192
193# 65 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
194
195# 75 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
196
197# 105 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
198
199# 117 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
200
201# 127 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
202
203# 174 "/home/runner/work/MFC/MFC/src/common/include/shared_parallel_macros.fpp"
204! New line at end of file is required for FYPP
205# 2 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp" 2
206
207# 7 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
208
209# 17 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
210
211# 22 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
212
213# 27 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
214
215# 32 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
216
217# 37 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
218
219# 42 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
220
221# 47 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
222
223# 52 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
224
225# 57 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
226
227# 62 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
228
229# 73 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
230
231# 78 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
232
233# 83 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
234
235# 88 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
236
237# 103 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
238
239# 131 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
240
241# 161 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
242
243# 176 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
244
245# 194 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
246
247# 216 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
248
249# 245 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
250
251# 260 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
252
253# 270 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
254
255# 279 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
256
257# 295 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
258
259# 305 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
260
261# 312 "/home/runner/work/MFC/MFC/src/common/include/acc_macros.fpp"
262! New line at end of file is required for FYPP
263# 4 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp" 2
264
265! GPU parallel region (scalar reductions, maxval/minval)
266# 23 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
267
268! GPU parallel loop over threads (most common GPU macro)
269# 44 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
270
271! Required closing for GPU_PARALLEL_LOOP
272# 56 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
273
274! Mark routine for device compilation
275# 113 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
276
277! Declare device-resident data
278# 131 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
279
280! Inner loop within a GPU parallel region
281# 146 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
282
283! Scoped GPU data region
284# 165 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
285
286! Host code with device pointers (for MPI with GPU buffers)
287# 191 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
288
289! Allocate device memory (unscoped)
290# 205 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
291
292! Free device memory
293# 217 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
294
295! Atomic operation on device
296# 229 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
297
298! End atomic capture block
299# 240 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
300
301! Copy data between host and device
302# 252 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
303
304! Synchronization barrier
305# 264 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
306
307! Import GPU library module (openacc or omp_lib)
308# 273 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
309
310! Emit code only for AMD compiler
311# 280 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
312
313! Emit code for non-Cray compilers
314# 287 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
315
316! Emit code only for Cray compiler
317# 294 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
318
319! Cray compiler directive, emitted from Fypp so the formatter cannot turn it into a comment
320# 301 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
321
322! Emit code for non-NVIDIA compilers
323# 308 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
324
325# 310 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
326# 311 "/home/runner/work/MFC/MFC/src/common/include/parallel_macros.fpp"
327! New line at end of file is required for FYPP
328# 2 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp" 2
329
330# 14 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
331
332! Caution: This macro requires the use of a binding script to set CUDA_VISIBLE_DEVICES, such that we have one GPU device per MPI
333! rank. That's because for both cudaMemAdvise (preferred location) and cudaMemPrefetchAsync we use location = device_id = 0. For an
334! example see misc/nvidia_uvm/bind.sh.
335# 52 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
336
337! Allocate and create GPU device memory
338# 72 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
339
340! Free GPU device memory and deallocate
341# 80 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
342
343! Cray-specific GPU pointer setup for vector fields
344# 104 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
345
346! Cray-specific GPU pointer setup for scalar fields
347# 120 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
348
349! Cray-specific GPU pointer teardown for scalar fields - exact inverse of ACC_SETUP_SFs. Removes the
350! present-table entries ACC_SETUP_SFs added (the scalar_field descriptor and its %sf copyin). REQUIRED when
351! an ACC_SETUP'd field is freed MID-RUN (e.g. AMR slot free on regrid/restart): Cray 'exit data delete'
352! decrements the reference counter, so the lone @:DEALLOCATE(arg%sf) only undoes the @:ALLOCATE create ref
353! and leaves the descriptor + the ACC_SETUP %sf copyin dangling. Call BEFORE @:DEALLOCATE(arg%sf).
354# 140 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
355
356! Cray-specific GPU pointer setup for acoustic source spatials
357# 165 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
358
359! Whether stress index idx is a shear component. Spelled out because NVHPC 26.9 at -O2 vectorizes any(idx == shear_indices)
360! inside a loop to always true, which doubled the normal-stress elastic energy.
361# 171 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
362
363# 177 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
364
365# 184 "/home/runner/work/MFC/MFC/src/common/include/macros.fpp"
366! New line at end of file is required for FYPP
367# 6 "/home/runner/work/MFC/MFC/src/simulation/m_amr_cluster.fpp" 2
368
369!> @brief Berger-Rigoutsos clustering of a sparse tag list into separated block boxes, identically on every rank: tag signatures,
370!! bisection, canonical Morton order and min-separation merge.
372
373#ifdef MFC_MPI
374 use mpi
375#endif
376
377 use m_derived_types ! t_box
378 use m_box, only: f_morton
380 use m_amr_state
382
383 implicit none
384
385 private
386 public :: s_amr_cluster
387
388 !> A node is wide when its box spans more ranks than this; wide nodes use the batched collective, narrow ones reduce among their
389 !! few overlapping ranks. 8 is one 2x2x2 brick of ranks.
390 integer, parameter :: amr_cl_wide = 8
391 !> Scratch of the narrow point-to-point reduction (pidx is all-zero between depths)
392 integer, allocatable :: pidx(:), plist(:), scnt(:), rcnt(:), sdsp2(:), rdsp2(:), soff(:), roff(:), sbuf(:), rbuf(:), creq(:), &
393 & wbuf(:)
394
395contains
396
397 !> Concatenated 1D tag signatures of box [blo0:bhi0] from tags ts:te: sig(off(d) + t - blo0(d)) counts the in-box tagged cells
398 !! at position t along d. One signature serves the trim, the count and every split. nsig is the used length.
399 impure subroutine s_amr_box_sig(tags, ts, te, blo0, bhi0, sig, off, nsig)
400
401 integer, intent(in) :: tags(:,:), ts, te, blo0(3), bhi0(3)
402 integer, intent(out) :: sig(:), off(3), nsig
403 integer :: d, t, c(3)
404
405 nsig = 0
406 do d = 1, 3
407 off(d) = nsig + 1
408 if (d <= num_dims) nsig = nsig + (bhi0(d) - blo0(d) + 1)
409 end do
410 sig(1:nsig) = 0
411 do t = ts, te
412 c = tags(:,t)
413 if (c(1) < blo0(1) .or. c(1) > bhi0(1)) cycle
414 if (c(2) < blo0(2) .or. c(2) > bhi0(2)) cycle
415 if (c(3) < blo0(3) .or. c(3) > bhi0(3)) cycle
416 do d = 1, num_dims
417 sig(off(d) + c(d) - blo0(d)) = sig(off(d) + c(d) - blo0(d)) + 1
418 end do
419 end do
420
421 end subroutine s_amr_box_sig
422
423 !> Shrink box [blo:bhi] to the tight bbox of its tagged cells (first/last nonzero of each axis signature) and return their
424 !! count; ok = .false. if none tagged.
425 pure subroutine s_amr_trim_from_sig(sig, off, blo0, bhi0, blo, bhi, ok, ntag)
426
427 integer, intent(in) :: sig(:), off(3), blo0(3), bhi0(3)
428 integer, intent(inout) :: blo(3), bhi(3)
429 logical, intent(out) :: ok
430 integer, intent(out) :: ntag
431 integer :: d, t, lo, hi
432
433 ok = .false.
434 ntag = 0
435 do t = blo0(1), bhi0(1)
436 ntag = ntag + sig(off(1) + t - blo0(1))
437 end do
438 if (ntag == 0) return
439 do d = 1, num_dims
440 lo = -1; hi = -1
441 do t = blo0(d), bhi0(d)
442 if (sig(off(d) + t - blo0(d)) > 0) then
443 if (lo < 0) lo = t
444 hi = t
445 end if
446 end do
447 blo(d) = lo; bhi(d) = hi
448 end do
449 ok = .true.
450
451 end subroutine s_amr_trim_from_sig
452
453 !> Bisect a trimmed box: on the longest splittable axis prefer the widest zero-signature hole, else the strongest signature
454 !! inflection (Laplacian sign change). ok = .false. if no axis admits a split leaving both children >= min_child cells.
455 pure subroutine s_amr_find_split_sig(sig, off, blo0, blo, bhi, sax, spos, ok)
456
457 integer, intent(in) :: sig(:), off(3), blo0(3)
458 integer, intent(in) :: blo(3), bhi(3)
459 integer, intent(out) :: sax, spos
460 logical, intent(out) :: ok
461 !> smallest box the bisection may produce; amr_blocking_factor raises it to stop over-generation
462 integer :: min_child
463 integer :: axord(3), ext(3), d, ax, t, s, b
464 integer :: run, run_start, best_run, best_start, lap, prevlap, bestmag, bestpos
465
466 min_child = max(2, amr_blocking_factor)
467 ok = .false.; sax = 0; spos = 0
468 ext = bhi - blo + 1
469 axord = [1, 2, 3] ! axes by descending extent
470 do d = 1, 2
471 do ax = 1, 3 - d
472 if (ext(axord(ax)) < ext(axord(ax + 1))) then
473 s = axord(ax); axord(ax) = axord(ax + 1); axord(ax + 1) = s
474 end if
475 end do
476 end do
477 do d = 1, 3
478 ax = axord(d)
479 if (ax > num_dims) cycle
480 if (ext(ax) < 2*min_child) cycle
481 b = off(ax) - blo0(ax) ! signature at position t is sig(b + t)
482 ! widest zero run (interior, since the box is trimmed)
483 best_run = 0; best_start = -1; run = 0; run_start = -1
484 do t = blo(ax), bhi(ax)
485 if (sig(b + t) == 0) then
486 if (run == 0) run_start = t
487 run = run + 1
488 else
489 if (run > best_run) then; best_run = run; best_start = run_start; end if
490 run = 0
491 end if
492 end do
493 if (best_start > blo(ax)) then
494 spos = best_start
495 if (spos - blo(ax) >= min_child .and. bhi(ax) - spos + 1 >= min_child) then
496 sax = ax; ok = .true.; return
497 end if
498 end if
499 ! strongest inflection: Laplacian sign change with the largest jump
500 bestmag = -1; bestpos = -1; prevlap = 0
501 do t = blo(ax) + 1, bhi(ax) - 1
502 lap = sig(b + t - 1) - 2*sig(b + t) + sig(b + t + 1)
503 if (t > blo(ax) + 1) then
504 if (((lap < 0) .neqv. (prevlap < 0)) .and. abs(lap - prevlap) > bestmag .and. t - blo(ax) >= min_child &
505 & .and. bhi(ax) - t + 1 >= min_child) then
506 bestmag = abs(lap - prevlap); bestpos = t
507 end if
508 end if
509 prevlap = lap
510 end do
511 if (bestpos > 0) then
512 sax = ax; spos = bestpos; ok = .true.; return
513 end if
514 end do
515
516 end subroutine s_amr_find_split_sig
517
518 !> Partition wt(:, ts:te) in place by coord(sax) < spos; the low child is ts:lo-1 and the high child lo:te.
519 pure subroutine s_cl_partition(wt, ts, te, sax, spos, lo)
520
521 integer, intent(inout) :: wt(:,:)
522 integer, intent(in) :: ts, te, sax, spos
523 integer, intent(out) :: lo
524 integer :: hi, tmp(3)
525
526 lo = ts; hi = te
527 do while (lo <= hi)
528 if (wt(sax, lo) < spos) then
529 lo = lo + 1
530 else
531 tmp = wt(:,lo); wt(:,lo) = wt(:,hi); wt(:,hi) = tmp
532 hi = hi - 1
533 end if
534 end do
535
536 end subroutine s_cl_partition
537
538#ifdef MFC_MPI
539 !> Nonblocking exchange: receive rc(q) words into rb at rd(q) and send sc(q) words from sb at sd(q), per peer plist(q).
540 impure subroutine s_cl_exchange(tagc, np2, sb, sc, sd, rb, rc, rd)
541
542 integer, intent(in) :: tagc, np2, sc(:), sd(:), rc(:), rd(:)
543 integer, intent(inout) :: sb(:), rb(:)
544 integer :: q, nreq2, ierr
545
546 nreq2 = 0
547 do q = 1, np2
548 if (rc(q) > 0) then
549 nreq2 = nreq2 + 1
550 call mpi_irecv(rb(rd(q) + 1), rc(q), mpi_integer, plist(q), tagc, mpi_comm_world, creq(nreq2), ierr)
551 end if
552 end do
553 do q = 1, np2
554 if (sc(q) > 0) then
555 nreq2 = nreq2 + 1
556 call mpi_isend(sb(sd(q) + 1), sc(q), mpi_integer, plist(q), tagc, mpi_comm_world, creq(nreq2), ierr)
557 end if
558 end do
559 if (nreq2 > 0) call mpi_waitall(nreq2, creq, mpi_statuses_ignore, ierr)
560
561 end subroutine s_cl_exchange
562
563 !> Register rank rr as a peer of the narrow reduction if not yet listed.
564 impure subroutine s_cl_add_peer(rr, np2)
565
566 integer, intent(in) :: rr
567 integer, intent(inout) :: np2
568
569 if (pidx(rr) == 0) then; np2 = np2 + 1; plist(np2) = rr; pidx(rr) = np2; end if
570
571 end subroutine s_cl_add_peer
572
573 !> Sum the depth's batched signatures across ranks. Wide nodes (every rank overlaps) ride one ALLREDUCE. Narrow nodes reduce
574 !! point-to-point within their small rank brick: members send to the root bovr(1,:), which sums and sends the total back. Both
575 !! ends enumerate nodes in the same global order, so one aggregated message per peer per phase matches without negotiation.
576 impure subroutine s_cl_reduce_depth(nbat, bsig, bofs, blen, bnov, bwide, bovr)
577
578 integer, intent(in) :: nbat, bofs(:), blen(:), bnov(:), bovr(:,:)
579 integer, intent(inout) :: bsig(:)
580 logical, intent(in) :: bwide(:)
581 integer :: j, t, q, rr, o1, nwb, np2, nsnd, nrcv, tagc, ierr
582
583 nwb = 0
584 do j = 1, nbat
585 if (bwide(j)) nwb = nwb + blen(j)
586 end do
587 if (nwb > 0) then
588 call s_amr_size_int(wbuf, nwb)
589 o1 = 0
590 do j = 1, nbat
591 if (.not. bwide(j)) cycle
592 wbuf(o1 + 1:o1 + blen(j)) = bsig(bofs(j) + 1:bofs(j) + blen(j)); o1 = o1 + blen(j)
593 end do
594 call mpi_allreduce(mpi_in_place, wbuf, nwb, mpi_integer, mpi_sum, mpi_comm_world, ierr)
595 o1 = 0
596 do j = 1, nbat
597 if (.not. bwide(j)) cycle
598 bsig(bofs(j) + 1:bofs(j) + blen(j)) = wbuf(o1 + 1:o1 + blen(j)); o1 = o1 + blen(j)
599 end do
600 end if
601 ! peers: whoever roots a node I hold, plus whoever holds a node I root
602 np2 = 0
603 do j = 1, nbat
604 if (bwide(j)) cycle
605 if (bovr(1, j) == proc_rank) then
606 do t = 2, bnov(j)
607 call s_cl_add_peer(bovr(t, j), np2)
608 end do
609 else
610 call s_cl_add_peer(bovr(1, j), np2)
611 end if
612 end do
613 if (np2 == 0) return
614 scnt(1:np2) = 0; rcnt(1:np2) = 0
615 do j = 1, nbat
616 if (bwide(j)) cycle
617 if (bovr(1, j) == proc_rank) then
618 do t = 2, bnov(j); q = pidx(bovr(t, j)); rcnt(q) = rcnt(q) + blen(j); end do
619 else
620 q = pidx(bovr(1, j)); scnt(q) = scnt(q) + blen(j)
621 end if
622 end do
623 sdsp2(1) = 0; rdsp2(1) = 0
624 do q = 2, np2
625 sdsp2(q) = sdsp2(q - 1) + scnt(q - 1); rdsp2(q) = rdsp2(q - 1) + rcnt(q - 1)
626 end do
627 nsnd = sdsp2(np2) + scnt(np2); nrcv = rdsp2(np2) + rcnt(np2)
628 call s_amr_size_int(sbuf, max(nsnd, 1)); call s_amr_size_int(rbuf, max(nrcv, 1))
629 call s_amr_size_int(creq, 2*np2)
630 ! phase A: members send their contribution to the root
631 soff(1:np2) = sdsp2(1:np2)
632 do j = 1, nbat
633 if (bwide(j) .or. bovr(1, j) == proc_rank) cycle
634 q = pidx(bovr(1, j))
635 sbuf(soff(q) + 1:soff(q) + blen(j)) = bsig(bofs(j) + 1:bofs(j) + blen(j)); soff(q) = soff(q) + blen(j)
636 end do
637 tagc = amr_tag_narrow + int(mod(amr_mesh_epoch, 50_8))
638 call s_cl_exchange(tagc, np2, sbuf, scnt, sdsp2, rbuf, rcnt, rdsp2)
639 ! integer SUM is exact and order-independent, so the total equals a machine-wide reduction
640 roff(1:np2) = rdsp2(1:np2)
641 do j = 1, nbat
642 if (bwide(j) .or. bovr(1, j) /= proc_rank) cycle
643 do t = 2, bnov(j)
644 q = pidx(bovr(t, j))
645 bsig(bofs(j) + 1:bofs(j) + blen(j)) = bsig(bofs(j) + 1:bofs(j) + blen(j)) + rbuf(roff(q) + 1:roff(q) + blen(j))
646 roff(q) = roff(q) + blen(j)
647 end do
648 end do
649 ! phase B: the total goes back down; counts mirror phase A, so the buffers swap roles
650 roff(1:np2) = rdsp2(1:np2)
651 do j = 1, nbat
652 if (bwide(j) .or. bovr(1, j) /= proc_rank) cycle
653 do t = 2, bnov(j)
654 q = pidx(bovr(t, j))
655 rbuf(roff(q) + 1:roff(q) + blen(j)) = bsig(bofs(j) + 1:bofs(j) + blen(j)); roff(q) = roff(q) + blen(j)
656 end do
657 end do
658 call s_cl_exchange(tagc + 50, np2, rbuf, rcnt, rdsp2, sbuf, scnt, sdsp2)
659 soff(1:np2) = sdsp2(1:np2)
660 do j = 1, nbat
661 if (bwide(j) .or. bovr(1, j) == proc_rank) cycle
662 q = pidx(bovr(1, j))
663 bsig(bofs(j) + 1:bofs(j) + blen(j)) = sbuf(soff(q) + 1:soff(q) + blen(j)); soff(q) = soff(q) + blen(j)
664 end do
665 do q = 1, np2 ! clear only what was touched: a full wipe would be O(P) per depth
666 pidx(plist(q)) = 0
667 end do
668
669 end subroutine s_cl_reduce_depth
670
671 !> Union the per-rank accepted boxes (rank-local subtrees are walked only by their owner). Rank order is not serial acceptance
672 !! order; the Morton sort that follows removes the dependence. alo/ahi grow to the union (the cap applies to the merged set).
673 impure subroutine s_cl_union(alo, ahi, nacc)
674
675 integer, allocatable, intent(inout) :: alo(:,:), ahi(:,:)
676 integer, intent(inout) :: nacc
677 integer, allocatable :: gcnt(:), gdsp(:), sbx(:,:), gbx(:,:)
678 integer :: i, ntot, ierr
679
680 allocate (gcnt(num_procs), gdsp(num_procs))
681 call mpi_allgather(nacc, 1, mpi_integer, gcnt, 1, mpi_integer, mpi_comm_world, ierr)
682 gdsp(1) = 0
683 do i = 2, num_procs
684 gdsp(i) = gdsp(i - 1) + gcnt(i - 1)
685 end do
686 ntot = gdsp(num_procs) + gcnt(num_procs)
687 allocate (sbx(6, max(nacc, 1)), gbx(6, max(ntot, 1)))
688 do i = 1, nacc
689 sbx(1:3,i) = alo(:,i); sbx(4:6,i) = ahi(:,i)
690 end do
691 gcnt = gcnt*6; gdsp = gdsp*6
692 call mpi_allgatherv(sbx, nacc*6, mpi_integer, gbx, gcnt, gdsp, mpi_integer, mpi_comm_world, ierr)
693 if (ntot > size(alo, 2)) then
694 deallocate (alo, ahi)
695 allocate (alo(3, ntot), ahi(3, ntot))
696 end if
697 nacc = ntot
698 do i = 1, nacc
699 alo(:,i) = gbx(1:3,i); ahi(:,i) = gbx(4:6,i)
700 end do
701 deallocate (gcnt, gdsp, sbx, gbx)
702
703 end subroutine s_cl_union
704#endif
705
706 !> Level-order Berger-Rigoutsos descent: accepts boxes into alo/ahi(:, 1:nacc). Every shared node of a depth rides one
707 !! reduction, so the collective count is O(tree depth). A child's overlapping ranks are a subset of its parent's, so every rank
708 !! walks the whole shared subtree in the same order. Children are appended past ncur and shifted down when the depth closes
709 !! (peak occupancy 3*ncur <= the 4*cap + 8 the arrays carry).
710 impure subroutine s_cl_bisect(tags, ntag_in, reduce, alo, ahi, nacc, capped)
711
712 integer, intent(in) :: tags(:,:), ntag_in
713 logical, intent(in) :: reduce
714 integer, intent(out) :: alo(:,:), ahi(:,:), nacc
715 logical, intent(out) :: capped
716 integer, allocatable :: slo(:,:), shi(:,:), sts(:), ste(:), wt(:,:), sdep(:), sig(:), ovr(:)
717 integer, allocatable :: kpos(:), kbat(:), bofs(:), blen(:), boff(:,:), bsig(:), bnov(:), bovr(:,:)
718 logical, allocatable :: bwide(:)
719 integer :: cap, mg, ng, pg, t, i, j, ncur, nnxt, nkeep, nbat, nbuf, novr, nsig, ntag, dep, near
720 integer :: blo0(3), bhi0(3), off(3), blo(3), bhi(3), ts, te, lo, sax, spos
721 integer(8) :: vol !< a global-bbox first pass can exceed 2**31 cells
722 logical :: ok, force, mine
723 real(wp) :: eff
724
725 mg = m_glb; ng = 0; pg = 0
726 if (n_glb > 0) ng = n_glb
727 if (p_glb > 0) pg = p_glb
728 cap = amr_max_blocks
729 allocate (slo(3, 4*cap + 8), shi(3, 4*cap + 8), sts(4*cap + 8), ste(4*cap + 8), wt(3, ntag_in), sdep(4*cap + 8))
730 allocate (sig(mg + ng + pg + 3)) ! the three full domain extents; reused by every node
731 allocate (ovr(amr_cl_wide)) ! only narrow nodes are enumerated, so this does not size with P
732 allocate (kpos(4*cap + 8), kbat(4*cap + 8), bofs(4*cap + 8), blen(4*cap + 8), boff(3, 4*cap + 8))
733 allocate (bsig(4*(mg + ng + pg + 3)), bnov(4*cap + 8), bwide(4*cap + 8), bovr(amr_cl_wide, 4*cap + 8))
734 allocate (pidx(0:max(num_procs - 1, 0)), plist(max(num_procs, 1)))
735 allocate (scnt(max(num_procs, 1)), rcnt(max(num_procs, 1)), sdsp2(max(num_procs, 1)), rdsp2(max(num_procs, 1)))
736 allocate (soff(max(num_procs, 1)), roff(max(num_procs, 1)))
737 allocate (wbuf(1), sbuf(1), rbuf(1), creq(1))
738 pidx = 0
739 ! working copy of the tags, partitioned in place as the tree descends
740 do t = 1, ntag_in
741 wt(:,t) = tags(:,t)
742 end do
743 ncur = 1; slo(:,1) = [0, 0, 0]; shi(:,1) = [mg, ng, pg] ! first node trims to the global tagged bbox
744 sts(1) = 1; ste(1) = ntag_in; sdep(1) = 0
745 nacc = 0; capped = .false.
746 do while (ncur > 0)
747 nkeep = 0; nbat = 0; nbuf = 0; nnxt = 0
748 ! pass 1: classify nodes and stash the signatures that need reducing (rank-local nodes recompute theirs in pass 2)
749 do i = 1, ncur
750 blo0 = slo(:,i); bhi0 = shi(:,i)
751 novr = f_amr_overlap_count(blo0, bhi0)
752 mine = (num_procs == 1) .or. f_amr_rank_overlaps(blo0, bhi0, proc_rank)
753 ! A rank holds tags only in its subdomain, so a narrow node it does not reach would add only zeros: drop the
754 ! subtree. Wide nodes are never dropped (every rank must enter their collective with the identical batch).
755 if (reduce .and. num_procs > 1 .and. .not. mine .and. novr <= amr_cl_wide) cycle
756 call s_amr_box_sig(wt, sts(i), ste(i), blo0, bhi0, sig, off, nsig)
757 nkeep = nkeep + 1; kpos(nkeep) = i; kbat(nkeep) = 0
758 if (reduce .and. num_procs > 1 .and. novr > 1) then
759 call s_amr_size_int(bsig, nbuf + nsig + 1)
760 nbat = nbat + 1; kbat(nkeep) = nbat
761 bofs(nbat) = nbuf; blen(nbat) = nsig + 1; boff(:,nbat) = off ! + the near-cap flag, set below
762 bnov(nbat) = novr; bwide(nbat) = (novr > amr_cl_wide)
763 bovr(1, nbat) = -1 ! defined for wide nodes too: Fortran does not promise .or. short-circuits
764 if (.not. bwide(nbat)) then
765 call s_amr_ranks_overlapping(blo0, bhi0, ovr, novr)
766 bovr(1:novr,nbat) = ovr(1:novr)
767 end if
768 bsig(nbuf + 1:nbuf + nsig) = sig(1:nsig)
769 nbuf = nbuf + nsig + 1
770 end if
771 end do
772 ! Near the amr_max_blocks cap (every kept node splitting bounds the pass-2 count below) a node is accepted unsplit. A
773 ! shared node must get the same decision on every rank it overlaps, or their next-depth batches differ: each rank's
774 ! flag rides the node's reduction, and the node is forced if any of them is set.
775 near = merge(1, 0, nacc + 2*nkeep >= cap)
776 do j = 1, nbat
777 bsig(bofs(j) + blen(j)) = near
778 end do
779#ifdef MFC_MPI
780 call s_cl_reduce_depth(nbat, bsig, bofs, blen, bnov, bwide, bovr)
781#endif
782 ! pass 2: trim, accept or split every kept node
783 do j = 1, nkeep
784 i = kpos(j); blo = slo(:,i); bhi = shi(:,i); ts = sts(i); te = ste(i); dep = sdep(i)
785 blo0 = blo; bhi0 = bhi
786 ! guard against overflowing the amr_max_blocks cap: pending = rest of this depth + children queued so far
787 force = (nacc + (nkeep - j) + nnxt + 1 >= cap)
788 if (kbat(j) > 0) then
789 off = boff(:,kbat(j))
790 nsig = blen(kbat(j)) - 1
791 sig(1:nsig) = bsig(bofs(kbat(j)) + 1:bofs(kbat(j)) + nsig)
792 force = bsig(bofs(kbat(j)) + nsig + 1) > 0
793 else
794 ! rank-local node: recompute (safe, pass 2 only partitions this node's own wt range)
795 call s_amr_box_sig(wt, ts, te, blo0, bhi0, sig, off, nsig)
796 end if
797 call s_amr_trim_from_sig(sig(1:nsig), off, blo0, bhi0, blo, bhi, ok, ntag)
798 if (.not. ok) cycle
799 vol = 1_8
800 do t = 1, num_dims; vol = vol*int(bhi(t) - blo(t) + 1, 8); end do
801 eff = real(ntag, wp)/real(max(vol, 1_8), wp)
802 call s_amr_find_split_sig(sig(1:nsig), off, blo0, blo, bhi, sax, spos, ok)
803 if (eff >= amr_cluster_eff .or. .not. ok .or. force) then
804 if (nacc < cap) then; nacc = nacc + 1; alo(:,nacc) = blo; ahi(:,nacc) = bhi; end if
805 if (force .and. ok .and. eff < amr_cluster_eff) capped = .true.
806 else
807 call s_cl_partition(wt, ts, te, sax, spos, lo)
808 slo(:,ncur + nnxt + 1) = blo; shi(:,ncur + nnxt + 1) = bhi; shi(sax, ncur + nnxt + 1) = spos - 1
809 sts(ncur + nnxt + 1) = ts; ste(ncur + nnxt + 1) = lo - 1; sdep(ncur + nnxt + 1) = dep + 1
810 slo(:,ncur + nnxt + 2) = blo; shi(:,ncur + nnxt + 2) = bhi; slo(sax, ncur + nnxt + 2) = spos
811 sts(ncur + nnxt + 2) = lo; ste(ncur + nnxt + 2) = te; sdep(ncur + nnxt + 2) = dep + 1
812 nnxt = nnxt + 2
813 end if
814 end do
815 do i = 1, nnxt ! the children become the next depth
816 slo(:,i) = slo(:,ncur + i); shi(:,i) = shi(:,ncur + i)
817 sts(i) = sts(ncur + i); ste(i) = ste(ncur + i); sdep(i) = sdep(ncur + i)
818 end do
819 ncur = nnxt
820 end do
821 deallocate (pidx, plist, scnt, rcnt, sdsp2, rdsp2, soff, roff, wbuf, sbuf, rbuf, creq)
822
823 end subroutine s_cl_bisect
824
825 !> Sort boxes by the Morton key of lo so the merge input depends on the box set alone, not on the rank-dependent acceptance
826 !! order. Accepted boxes are disjoint, so the keys are distinct; the sort is stable for any collision.
827 impure subroutine s_cl_morton_sort(alo, ahi, nacc)
828
829 integer, intent(inout) :: alo(:,:), ahi(:,:)
830 integer, intent(in) :: nacc
831 integer(8), allocatable :: akey(:)
832 integer, allocatable :: sperm(:), t2lo(:,:), t2hi(:,:)
833 integer :: i
834
835 allocate (akey(nacc), sperm(nacc), t2lo(3, nacc), t2hi(3, nacc))
836 do i = 1, nacc
837 akey(i) = f_morton(alo(1, i), alo(2, i), alo(3, i))
838 end do
839 call s_amr_sort_by_key(akey, nacc, sperm)
840 do i = 1, nacc
841 t2lo(:,i) = alo(:,sperm(i)); t2hi(:,i) = ahi(:,sperm(i))
842 end do
843 alo(:,1:nacc) = t2lo; ahi(:,1:nacc) = t2hi
844
845 end subroutine s_cl_morton_sort
846
847 !> Min-separation merge: two boxes are separated only if some active dim's gap reaches thr; else fuse to their bounding box.
848 !! Fusing only shrinks gaps, so the fixed point (groups with mutually separated bounding boxes) does not depend on the fusion
849 !! order; a group keeps its lowest index, so the output order does not either. Each box in list order sweeps the 3^d bins around
850 !! its lo (bin width ext_max + thr holds every too-close partner), absorbs every later box found too close and is absorbed by
851 !! the lowest earlier one, until a sweep finds nothing. Bins are rebuilt when a grown box outgrows the cell width.
852 impure subroutine s_cl_merge(alo, ahi, nacc)
853
854 integer, intent(inout) :: alo(:,:), ahi(:,:), nacc
855 integer, allocatable :: nxt(:) !< survivor list in canonical order
856 integer, allocatable :: prv(:) !< predecessor links
857 integer, allocatable :: bp(:), bidx(:) !< bin back-links and current bin of each live box
858 integer, allocatable :: bh(:), bc(:) !< bin heads and per-box chains
859 integer :: head, nlive, thr, i, k, d, aa, nfused, extd
860 integer :: blo3(3), bhi3(3), nbmax, rng, ext_max, cellw, nbx, nby, nbz, nb_tot
861
862 thr = buff_size + 2*amr_buf
863 allocate (nxt(max(nacc, 1)), prv(max(nacc, 1)), bp(max(nacc, 1)), bidx(max(nacc, 1)))
864 do i = 1, nacc - 1
865 nxt(i) = i + 1
866 end do
867 if (nacc >= 1) nxt(nacc) = 0
868 do i = 1, nacc
869 prv(i) = i - 1
870 end do
871 ! head must be 0 when nothing is tagged (nxt(1) is then unwritten)
872 head = merge(1, 0, nacc >= 1); nlive = nacc
873 cellw = 0
874 if (head /= 0) call s_mrg_build()
875 i = head
876 do while (i /= 0)
877 do
878 extd = 1
879 do d = 1, num_dims
880 extd = max(extd, ahi(d, i) - alo(d, i) + 1)
881 end do
882 if (extd > cellw - thr) then
883 do while (extd > cellw - thr)
884 cellw = cellw*2
885 end do
886 call s_mrg_build()
887 else if (f_mrg_binof(i) /= bidx(i)) then
888 call s_mrg_binun(i)
889 call s_mrg_binreg(i)
890 end if
891 call s_mrg_sweep(i, aa, nfused)
892 if (aa /= 0) then
893 call s_mrg_fuse(aa, i)
894 i = aa
895 else if (nfused == 0) then
896 exit
897 end if
898 end do
899 i = nxt(i)
900 end do
901 k = 0 ! compact in list order
902 i = head
903 do while (i /= 0)
904 k = k + 1
905 if (k /= i) then
906 alo(:,k) = alo(:,i); ahi(:,k) = ahi(:,i)
907 end if
908 i = nxt(i)
909 end do
910 nacc = nlive
911
912 contains
913
914 subroutine s_mrg_build()
915
916 integer :: ii, d2
917
918 ext_max = 1; blo3 = huge(0); bhi3 = -huge(0)
919 ii = head
920 do while (ii /= 0)
921 do d2 = 1, num_dims
922 ext_max = max(ext_max, ahi(d2, ii) - alo(d2, ii) + 1)
923 blo3(d2) = min(blo3(d2), alo(d2, ii)); bhi3(d2) = max(bhi3(d2), alo(d2, ii))
924 end do
925 ii = nxt(ii)
926 end do
927 nbmax = max(2, int(real(nlive)**(1.0/3.0)) + 1)*2
928 cellw = max(cellw, ext_max + thr)
929 do d2 = 1, num_dims
930 rng = bhi3(d2) - blo3(d2) + 1
931 if (rng > cellw*nbmax) cellw = (rng + nbmax - 1)/nbmax
932 end do
933 nbx = (bhi3(1) - blo3(1))/cellw + 1; nby = 1; nbz = 1
934 if (n_glb > 0) nby = (bhi3(2) - blo3(2))/cellw + 1
935 if (p_glb > 0) nbz = (bhi3(3) - blo3(3))/cellw + 1
936 nb_tot = nbx*nby*nbz
937 if (allocated(bh)) then
938 if (size(bh) < nb_tot) deallocate (bh)
939 end if
940 if (.not. allocated(bh)) allocate (bh(nb_tot))
941 if (.not. allocated(bc)) allocate (bc(size(nxt)))
942 bh(1:nb_tot) = 0
943 ii = head
944 do while (ii /= 0)
945 call s_mrg_binreg(ii)
946 ii = nxt(ii)
947 end do
948
949 end subroutine s_mrg_build
950
951 integer function f_mrg_binof(ii) result(bb)
952
953 integer, intent(in) :: ii
954 integer :: b3(3)
955
956 b3 = merge((alo(:,ii) - blo3)/cellw, 0, amr_dim)
957 bb = 1 + b3(1) + nbx*(b3(2) + nby*b3(3))
958
959 end function f_mrg_binof
960
961 subroutine s_mrg_binreg(ii)
962
963 integer, intent(in) :: ii
964 integer :: bb
965
966 bb = f_mrg_binof(ii)
967 bc(ii) = bh(bb)
968 if (bh(bb) /= 0) bp(bh(bb)) = ii
969 bp(ii) = 0
970 bh(bb) = ii
971 bidx(ii) = bb
972
973 end subroutine s_mrg_binreg
974
975 subroutine s_mrg_binun(ii)
976
977 integer, intent(in) :: ii
978
979 if (bp(ii) /= 0) then
980 bc(bp(ii)) = bc(ii)
981 else
982 bh(bidx(ii)) = bc(ii)
983 end if
984 if (bc(ii) /= 0) bp(bc(ii)) = bp(ii)
985
986 end subroutine s_mrg_binun
987
988 subroutine s_mrg_fuse(x, y)
989
990 integer, intent(in) :: x, y
991
992 alo(:,x) = min(alo(:,x), alo(:,y)); ahi(:,x) = max(ahi(:,x), ahi(:,y))
993 nxt(prv(y)) = nxt(y)
994 if (nxt(y) /= 0) prv(nxt(y)) = prv(y)
995 call s_mrg_binun(y)
996 nlive = nlive - 1
997
998 end subroutine s_mrg_fuse
999
1000 !> One sweep of the bins around box ii: fuse every too-close later box into it (nfused of them) and return the lowest
1001 !! too-close earlier box in below (0 if none). ii grows during the sweep, so the caller repeats until nothing is found.
1002 subroutine s_mrg_sweep(ii, below, nfused)
1003
1004 integer, intent(in) :: ii
1005 integer, intent(out) :: below, nfused
1006 integer :: b3(3), bx, by, bz, dx1, dy1, dz1, jj, jn, d2, zl, zh, yl, yh
1007 logical :: tc
1008
1009 below = 0; nfused = 0
1010 b3 = merge((alo(:,ii) - blo3)/cellw, 0, amr_dim); bx = b3(1); by = b3(2); bz = b3(3)
1011 zl = 0; zh = 0; yl = 0; yh = 0
1012 if (p_glb > 0) then; zl = max(0, bz - 1); zh = min(nbz - 1, bz + 1); end if
1013 if (n_glb > 0) then; yl = max(0, by - 1); yh = min(nby - 1, by + 1); end if
1014 do dz1 = zl, zh
1015 do dy1 = yl, yh
1016 do dx1 = max(0, bx - 1), min(nbx - 1, bx + 1)
1017 jj = bh(1 + dx1 + nbx*(dy1 + nby*dz1))
1018 do while (jj /= 0)
1019 jn = bc(jj) ! the fuse unlinks jj from its bin
1020 tc = jj /= ii
1021 do d2 = 1, num_dims
1022 if (max(alo(d2, ii), alo(d2, jj)) - min(ahi(d2, ii), ahi(d2, jj)) - 1 >= thr) tc = .false.
1023 end do
1024 if (tc .and. jj > ii) then
1025 call s_mrg_fuse(ii, jj); nfused = nfused + 1
1026 else if (tc .and. (below == 0 .or. jj < below)) then
1027 below = jj
1028 end if
1029 jj = jn
1030 end do
1031 end do
1032 end do
1033 end do
1034
1035 end subroutine s_mrg_sweep
1036
1037 end subroutine s_cl_merge
1038
1039 !> Cluster a sparse tag list tags(1:3, 1:ntag_in) of level-0 cell coords into separated block boxes, identically on every rank.
1040 !! Berger-Rigoutsos bisection until a box's tag efficiency reaches amr_cluster_eff (or it is atomic or the amr_max_blocks cap is
1041 !! hit), then merge boxes whose amr_buf-padded extents come within buff_size. Boxes are raw tagged extents; the caller pads,
1042 !! clamps and size-caps each. reduce: tags is this rank's local list and node signatures are reduced across ranks; otherwise
1043 !! tags is already replicated.
1044 impure subroutine s_amr_cluster(tags, ntag_in, boxes, nboxes, reduce)
1045
1046 integer, intent(in) :: tags(:,:), ntag_in
1047 logical, intent(in) :: reduce
1048 type(t_box), allocatable, intent(out) :: boxes(:)
1049 integer, intent(out) :: nboxes
1050 integer, allocatable :: alo(:,:), ahi(:,:)
1051 integer :: nacc, i
1052 logical :: capped
1053
1054 nboxes = 0
1055 ! with reduce, a rank without tags must still walk the tree and enter every collective
1056 if (.not. reduce .and. ntag_in == 0) return
1057 allocate (alo(3, amr_max_blocks), ahi(3, amr_max_blocks))
1058 call s_cl_bisect(tags, ntag_in, reduce, alo, ahi, nacc, capped)
1059#ifdef MFC_MPI
1060 if (reduce .and. num_procs > 1) call s_cl_union(alo, ahi, nacc)
1061#endif
1062 call s_cl_morton_sort(alo, ahi, nacc)
1063 call s_cl_merge(alo, ahi, nacc)
1064 if (capped .and. proc_rank == 0) print '(A,I0)', ' [amr] WARNING: tag clustering capped at amr_max_blocks = ', &
1065 & amr_max_blocks
1066 if (nacc > amr_max_blocks) then ! boxes past the cap never refine
1067 if (proc_rank == 0) print '(A,I0,A,I0)', ' [amr] WARNING: merged box set truncated: ', nacc, ' boxes, keeping ', &
1068 & amr_max_blocks
1069 nacc = amr_max_blocks
1070 end if
1071 nboxes = nacc
1072 allocate (boxes(nboxes))
1073 do i = 1, nboxes
1074 boxes(i)%lo = alo(:,i); boxes(i)%hi = ahi(:,i)
1075 end do
1076
1077 end subroutine s_amr_cluster
1078
1079end module m_amr_cluster
subroutine s_mrg_binreg(ii)
subroutine s_mrg_binun(ii)
subroutine s_mrg_fuse(x, y)
integer function f_mrg_binof(ii)
subroutine s_mrg_build()
subroutine s_mrg_sweep(ii, below, nfused)
One sweep of the bins around box ii: fuse every too-close later box into it (nfused of them) and retu...
integer, intent(in) k
integer, intent(in) j
Berger-Rigoutsos clustering of a sparse tag list into separated block boxes, identically on every ran...
integer, dimension(:), allocatable soff
integer, dimension(:), allocatable plist
integer, dimension(:), allocatable rcnt
integer, dimension(:), allocatable pidx
Scratch of the narrow point-to-point reduction (pidx is all-zero between depths).
impure subroutine s_cl_bisect(tags, ntag_in, reduce, alo, ahi, nacc, capped)
Level-order Berger-Rigoutsos descent: accepts boxes into alo/ahi(:, 1:nacc). Every shared node of a d...
pure subroutine s_amr_find_split_sig(sig, off, blo0, blo, bhi, sax, spos, ok)
Bisect a trimmed box: on the longest splittable axis prefer the widest zero-signature hole,...
integer, dimension(:), allocatable creq
impure subroutine, public s_amr_cluster(tags, ntag_in, boxes, nboxes, reduce)
Cluster a sparse tag list tags(1:3, 1:ntag_in) of level-0 cell coords into separated block boxes,...
impure subroutine s_cl_morton_sort(alo, ahi, nacc)
Sort boxes by the Morton key of lo so the merge input depends on the box set alone,...
impure subroutine s_cl_exchange(tagc, np2, sb, sc, sd, rb, rc, rd)
Nonblocking exchange: receive rc(q) words into rb at rd(q) and send sc(q) words from sb at sd(q),...
impure subroutine s_cl_union(alo, ahi, nacc)
Union the per-rank accepted boxes (rank-local subtrees are walked only by their owner)....
integer, dimension(:), allocatable sdsp2
integer, dimension(:), allocatable scnt
pure subroutine s_amr_trim_from_sig(sig, off, blo0, bhi0, blo, bhi, ok, ntag)
Shrink box [blo:bhi] to the tight bbox of its tagged cells (first/last nonzero of each axis signature...
impure subroutine s_cl_merge(alo, ahi, nacc)
Min-separation merge: two boxes are separated only if some active dim's gap reaches thr; else fuse to...
impure subroutine s_cl_reduce_depth(nbat, bsig, bofs, blen, bnov, bwide, bovr)
Sum the depth's batched signatures across ranks. Wide nodes (every rank overlaps) ride one ALLREDUCE....
pure subroutine s_cl_partition(wt, ts, te, sax, spos, lo)
Partition wt(:, ts:te) in place by coord(sax) < spos; the low child is ts:lo-1 and the high child lo:...
impure subroutine s_cl_add_peer(rr, np2)
Register rank rr as a peer of the narrow reduction if not yet listed.
integer, dimension(:), allocatable wbuf
integer, dimension(:), allocatable rbuf
integer, dimension(:), allocatable rdsp2
integer, dimension(:), allocatable sbuf
integer, dimension(:), allocatable roff
impure subroutine s_amr_box_sig(tags, ts, te, blo0, bhi0, sig, off, nsig)
Concatenated 1D tag signatures of box [blo0:bhi0] from tags ts:te: sig(off(d) + t - blo0(d)) counts t...
integer, parameter amr_cl_wide
A node is wide when its box spans more ranks than this; wide nodes use the batched collective,...
Rank decomposition, box arithmetic and block ownership (SFC cut, owner lookups, owned-block lists).
pure integer function, public f_amr_overlap_count(blo, bhi)
Overlap count only (allocation sizing), = product of the per-dim coord-range widths.
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,...
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...
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 =...
Module-scope AMR state (block pool, flat field store, wave buffers) and the leaf helpers every AMR mo...
integer amr_tag_narrow
Tag base of the clusterer's narrow-node reductions, above the per-box tag space [1....
impure subroutine s_amr_size_int(a, n)
Grow-on-demand sizing for append-only tables and pools: a grow preserves the entries already appended...
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.
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 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 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 num_procs
Number of processors.
An index-space rectangle in global cell indices. In v1, one t_box = one rank's subdomain....