sandbox/acastillo/grid/readme
Multi-root (“Brick-of-Roots”) adaptive trees
This directory provides a drop-in extension of Basilisk’s
quadtree.h and octree.h grids supporting
non-cubic domains via dimensions(nx, ny, nz) — a
rectangular brick of n_x \times n_y \times
n_z root cells, with full adaptive mesh refinement, periodicity,
dump/restore, and MPI load balancing.
Usage
Include the grid header from this sandbox (with
BASILISK_INCLUDE_PATH pointing to
sandbox/):
#include "acastillo/grid/quadtree.h" // 2D
#include "acastillo/grid/octree.h" // 3DThen set the domain aspect ratio with dimensions(nx, ny)
(2D) or dimensions(nx, ny, nz) (3D) before
init_grid(N) (where N is a multiple of
nx by a power of two).
Summary of changes
vs. $BASILISK/src/grid/
- quadtree.h, octree.h: Unchanged entry points (byte-identical to upstream) so relative includes resolve locally.
- memindex/virtual.h: Generalizes the
sparse virtual array size from scalar
int lento per-axisivec lenfor rectangular bricks (nx != ny != nz), and fixes themem_free1dslice bound. - foreach_cell.h: Adds
for_each_root()and extendsforeach_cell*/foreach_cell_restoreto traverse all n_x \times n_y \times n_z roots while avoiding periodic alias double-visits. - tree.h: Adds
dimensions(nx, ny, nz),Dimensions_scale/_DELTA, per-axisLayer.len, multi-rootinit_grid(),periodic_function,locate(),tree_is_full(),tree_periodic(), 1D/2D/3D periodic-awarecheck_two_one(), and independent(kx, ky)corner loops indiagonal_neighbor_2D()forBGHOSTS >= 2at level 0. - tree-common.h: Generalizes
periodic_clamp()to per-axis extents and handlesrefine_level(0)MPI updates. - tree-mpi.h: Adds level-0 inter-root halo
exchange,
sync_root_pids(), multi-rootrestore_mpi(), and root-prefix-sumz_weights()/z_indexing(). - balance.h: Migrates level-0 root 5^d neighborhoods, synchronizes root and
prolongation-child
pids before rebuilding halo buffers, and updatesgrid->tn/grid->maxdepthfornpe() == 1.
Multi-GPU
(CUDA + MPI), Rectangular Sub-Brick MPI & FP64 GPU
Multigrid
This directory also provides:
- Double- and single-precision GPU multigrid backends
for both CUDA (
libbuda64.a,libbuda.a) and OpenGL/GLSL (libgpu64.a,libgpu.a) in 2D and 3D. - Multi-GPU (
CUDA + MPI) and arbitrary-rank rectangular sub-brick MPI multigrid (LocalDims), removing upstreammultigrid-mpi.h’snpe = nx*ny*nz * (2^d)^isingle-root-per-rank restriction on both CUDA GPUs and CPUs, with CUDA-aware MPI (GPUDirect P2P / RDMA) and automatic pinned-host fallback (BASILISK_CUDA_MPI=0|1).
Building the runtime libraries
Build the static libraries once inside
sandbox/acastillo/grid/ (libbuda*.a are pure
CUDA with zero mpi.h dependency, so a single build serves
both serial and MPI runs):
make -C $BASILISK_INCLUDE_PATH/acastillo/grid/cuda all
make -C $BASILISK_INCLUDE_PATH/acastillo/grid/gpu allUsage
Include the desired backend header (with
BASILISK_INCLUDE_PATH pointing to sandbox/),
and compile with either CC=gcc (1 GPU / 1 CPU) or
CC='mpicc -D_MPI=N' (N
GPUs or N CPU ranks):
#include "acastillo/grid/cuda/multigrid_fp64.h" // 2D CUDA FP64 (1 or N GPUs)
#include "acastillo/grid/cuda/multigrid3D_fp64.h" // 3D CUDA FP64 (1 or N GPUs)
#include "acastillo/grid/cuda/multigrid.h" // 2D CUDA FP32 (1 or N GPUs)
#include "acastillo/grid/cuda/multigrid3D.h" // 3D CUDA FP32 (1 or N GPUs)
#include "acastillo/grid/gpu/multigrid_fp64.h" // 2D OpenGL FP64
#include "acastillo/grid/gpu/multigrid3D_fp64.h" // 3D OpenGL FP64
#include "acastillo/grid/multigrid.h" // 2D CPU / rectangular MPI
#include "acastillo/grid/multigrid3D.h" // 3D CPU / rectangular MPIBy default under _MPI, mpi_boundary_new()
automatically factors npe() into
MpiDims = {px, py, pz} to preserve the coarsest root grid
(S = 1 when p_d | nx_d) and minimize halo
surface area; an explicit decomposition can also be requested via
mpi_dims(px, py, pz) before init_grid(). For
canonical multi-root dump() / restore() and 3D
output_ppm_slice(), see acastillo/output_fields/dump_restore.h
and acastillo/output_fields/output_ppm_slice.h.
Summary of changes
vs. $BASILISK/src/grid/
- multigrid.h, multigrid1D.h, multigrid3D.h: Introduces
UserDims,MpiDims,LocalDims,mpi_dims(), andglobal_root_index(), unifies_ND(),SET_DIMENSIONS(), andlocate()across serial and MPI, skips internal MPI seam faces inbox_boundary_level(), and definesforeach_cell()overLocalDims. - multigrid-mpi.h: Adds rectangular
sub-brick factorization, pointer-keyed MPI
+reduction helpers,foreach_dimension()CPU halo slices, canonicalfor_each_root (LocalDims)z_indexing(), and the batched GPU-residentmpi_boundary_level_gpu()halo exchange. - gpu-multigrid.h: Derives
SINGLE_PRECISION/DOUBLE_PRECISIONcleanly, includesmultigrid-mpi.hunder_MPI, and syncssubtree_size()to host when read on CPU. - gpu/gpu.h: Merges
listfintolistcunder_MPIto prevent rank divergence on wall-normal face BCs, enables host-syncedforeach_regionon_CUDA, and dispatches GPU-resident fields tompi_boundary_level_gpu(). - gpu/grid.h: Adds
dvec2/dvec3and native FP64 device math, 64-bitsize_t_CUDASSBO indexing (> 4Gelements), non-divisorLocalDimsworkgroup sizing,_mpi_wallphysical-BC mask,_mpi_seamface reduction mask, lazy per-levels_phys_gpu/mpi_cleanhalo schedule,MPI_Allreduceon GPU reductions,ferr/foutexternal filtering, and opt-in-DSTRICT_GPU=1. - cuda/cuda.c: Adds FP64 support
(
typedef double real,reset_scalar,cuda_reduce, non-divisor register block clamping), local MPI rank GPU binding (cuda_set_device),dlsym("MPIX_Query_cuda_support")detection (keepinglibbuda{,64}.ampi.h-free), atomic PID-suffixed NVRTC PTX caching, FP32 subnormal literal clamping (1e-50f), sub-brickforeach_pointoffsets, and GPUhalo_pack/halo_unpackkernels. - gpu/opengl.c, gpu/reduction.c: Adds FP64 support and fixes
non-square
Dimensionsscratch-SSBO sizing andforeach_pointoffsets in OpenGL reductions. - cuda/Makefile, gpu/Makefile, and
{cuda,gpu}/multigrid*.h: Build and autolinklibbuda{,64}.a(-ldl) andlibgpu{,64}.avia$BASILISK_INCLUDE_PATH/acastillo/grid/{cuda,gpu}.
