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"    // 3D

    Then 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 len to per-axis ivec len for rectangular bricks (nx != ny != nz), and fixes the mem_free1d slice bound.
    • foreach_cell.h: Adds for_each_root() and extends foreach_cell* / foreach_cell_restore to 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-axis Layer.len, multi-root init_grid(), periodic_function, locate(), tree_is_full(), tree_periodic(), 1D/2D/3D periodic-aware check_two_one(), and independent (kx, ky) corner loops in diagonal_neighbor_2D() for BGHOSTS >= 2 at level 0.
    • tree-common.h: Generalizes periodic_clamp() to per-axis extents and handles refine_level(0) MPI updates.
    • tree-mpi.h: Adds level-0 inter-root halo exchange, sync_root_pids(), multi-root restore_mpi(), and root-prefix-sum z_weights() / z_indexing().
    • balance.h: Migrates level-0 root 5^d neighborhoods, synchronizes root and prolongation-child pids before rebuilding halo buffers, and updates grid->tn / grid->maxdepth for npe() == 1.

    Multi-GPU (CUDA + MPI), Rectangular Sub-Brick MPI & FP64 GPU Multigrid

    This directory also provides:

    1. 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.
    2. Multi-GPU (CUDA + MPI) and arbitrary-rank rectangular sub-brick MPI multigrid (LocalDims), removing upstream multigrid-mpi.h’s npe = nx*ny*nz * (2^d)^i single-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 all

    Usage

    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 MPI

    By 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(), and global_root_index(), unifies _ND(), SET_DIMENSIONS(), and locate() across serial and MPI, skips internal MPI seam faces in box_boundary_level(), and defines foreach_cell() over LocalDims.
    • multigrid-mpi.h: Adds rectangular sub-brick factorization, pointer-keyed MPI + reduction helpers, foreach_dimension() CPU halo slices, canonical for_each_root (LocalDims) z_indexing(), and the batched GPU-resident mpi_boundary_level_gpu() halo exchange.
    • gpu-multigrid.h: Derives SINGLE_PRECISION / DOUBLE_PRECISION cleanly, includes multigrid-mpi.h under _MPI, and syncs subtree_size() to host when read on CPU.
    • gpu/gpu.h: Merges listf into listc under _MPI to prevent rank divergence on wall-normal face BCs, enables host-synced foreach_region on _CUDA, and dispatches GPU-resident fields to mpi_boundary_level_gpu().
    • gpu/grid.h: Adds dvec2/dvec3 and native FP64 device math, 64-bit size_t _CUDA SSBO indexing (> 4G elements), non-divisor LocalDims workgroup sizing, _mpi_wall physical-BC mask, _mpi_seam face reduction mask, lazy per-level s_phys_gpu / mpi_clean halo schedule, MPI_Allreduce on GPU reductions, ferr/fout external 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 (keeping libbuda{,64}.a mpi.h-free), atomic PID-suffixed NVRTC PTX caching, FP32 subnormal literal clamping (1e-50f), sub-brick foreach_point offsets, and GPU halo_pack / halo_unpack kernels.
    • gpu/opengl.c, gpu/reduction.c: Adds FP64 support and fixes non-square Dimensions scratch-SSBO sizing and foreach_point offsets in OpenGL reductions.
    • cuda/Makefile, gpu/Makefile, and {cuda,gpu}/multigrid*.h: Build and autolink libbuda{,64}.a (-ldl) and libgpu{,64}.a via $BASILISK_INCLUDE_PATH/acastillo/grid/{cuda,gpu}.