18 PetscMPIInt rank = 0, size = 1;
19 PetscInt local_particles = 0;
20 PetscInt remainder = 0;
21 PetscInt global_particles = 0;
22 PetscInt remainder_min = 0, remainder_max = 0;
23 const PetscInt total_particles = 137;
25 PetscFunctionBeginUser;
26 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
27 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
28 PetscCall(
PicurvAssertBool((PetscBool)(size >= 2),
"unit-mpi requires at least two MPI ranks"));
33 total_particles / size + (((PetscInt)rank < remainder) ? 1 : 0),
35 "local particle share should match quotient+remainder policy"));
37 PetscCallMPI(MPI_Allreduce(&local_particles, &global_particles, 1, MPIU_INT, MPI_SUM, PETSC_COMM_WORLD));
38 PetscCall(
PicurvAssertIntEqual(total_particles, global_particles,
"distributed particle count must conserve total particles"));
40 PetscCallMPI(MPI_Allreduce(&remainder, &remainder_min, 1, MPIU_INT, MPI_MIN, PETSC_COMM_WORLD));
41 PetscCallMPI(MPI_Allreduce(&remainder, &remainder_max, 1, MPIU_INT, MPI_MAX, PETSC_COMM_WORLD));
42 PetscCall(
PicurvAssertIntEqual(remainder_min, remainder_max,
"all ranks should report the same remainder"));
43 PetscFunctionReturn(0);
55 PetscMPIInt rank = 0, size = 1;
56 PetscReal global_min_x = 0.0;
57 PetscReal global_max_x = 0.0;
59 PetscFunctionBeginUser;
60 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
61 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
62 PetscCall(
PicurvAssertBool((PetscBool)(size >= 2),
"unit-mpi requires at least two MPI ranks"));
68 PetscCall(
PicurvAssertBool((PetscBool)(boxes != NULL),
"all ranks should hold the gathered bounding-box table"));
70 for (PetscMPIInt r = 0; r < size; ++r) {
79 PetscCallMPI(MPI_Allreduce(&local_bbox.
min_coords.
x, &global_min_x, 1, MPIU_REAL, MPI_MIN, PETSC_COMM_WORLD));
80 PetscCallMPI(MPI_Allreduce(&local_bbox.
max_coords.
x, &global_max_x, 1, MPIU_REAL, MPI_MAX, PETSC_COMM_WORLD));
81 PetscCall(
PicurvAssertBool((PetscBool)(global_min_x <= 0.0),
"global min x should include domain start"));
82 PetscCall(
PicurvAssertBool((PetscBool)(global_max_x >= 7.0),
"global max x should include domain end"));
86 PetscFunctionReturn(0);
92int main(
int argc,
char **argv)
100 ierr = PetscInitialize(&argc, &argv, NULL,
"PICurv MPI-focused runtime tests");
105 ierr =
PicurvRunTests(
"unit-mpi", cases,
sizeof(cases) /
sizeof(cases[0]));
111 ierr = PetscFinalize();
Header file for Particle Swarm management functions.
PetscErrorCode DistributeParticles(PetscInt numParticles, PetscMPIInt rank, PetscMPIInt size, PetscInt *particlesPerProcess, PetscInt *remainder)
Distributes particles evenly across MPI processes, handling any remainders.
Public interface for grid, solver, and metric setup routines.
PetscErrorCode BroadcastAllBoundingBoxes(UserCtx *user, BoundingBox **bboxlist)
Broadcasts the bounding box information collected on rank 0 to all other ranks.
PetscErrorCode ComputeLocalBoundingBox(UserCtx *user, BoundingBox *localBBox)
Computes the local bounding box of the grid on the current process.
PetscErrorCode GatherAllBoundingBoxes(UserCtx *user, BoundingBox **allBBoxes)
Gathers local bounding boxes from all MPI processes to rank 0.
int main(int argc, char **argv)
Entry point for this unit-test binary.
static PetscErrorCode TestDistributeParticlesCollectiveConsistency(void)
Test-local routine.
static PetscErrorCode TestBoundingBoxCollectivesMultiRank(void)
Test-local routine.
PetscErrorCode PicurvCreateMinimalContexts(SimCtx **simCtx_out, UserCtx **user_out, PetscInt mx, PetscInt my, PetscInt mz)
Shared test-support routine.
PetscErrorCode PicurvAssertRealNear(PetscReal expected, PetscReal actual, PetscReal tol, const char *context)
Shared test-support routine.
PetscErrorCode PicurvDestroyMinimalContexts(SimCtx **simCtx_ptr, UserCtx **user_ptr)
Shared test-support routine.
PetscErrorCode PicurvRunTests(const char *suite_name, const PicurvTestCase *cases, size_t case_count)
Shared test-support routine.
PetscErrorCode PicurvAssertIntEqual(PetscInt expected, PetscInt actual, const char *context)
Shared test-support routine.
PetscErrorCode PicurvAssertBool(PetscBool value, const char *context)
Shared test-support routine.
C test module for PICurv.
Named test case descriptor consumed by PicurvRunTests.
Cmpnts max_coords
Maximum x, y, z coordinates of the bounding box.
Cmpnts min_coords
Minimum x, y, z coordinates of the bounding box.
Defines a 3D axis-aligned bounding box.
The master context for the entire simulation.
User-defined context containing data specific to a single computational grid level.