mpi.hpp:分布式批量推进接口

这个文件做什么

每个 MPI 进程持有自己的粒子和背景,DistributedMccStepper 协调各进程完成同一时间步。主程序负责初始化 MPI,并在粒子迁移时保留 ID。所有相关进程都必须参加 step 调用。只在启用 MPI 时使用,不能只靠 mcc.hpp 获得此接口。

主要类型与入口

DistributedMccStepper(MccEngine, MPI_Comm, StepOptions); step; communicator

上面列出的是入口名称,具体参数以源码声明为准。

What this file does

Each rank owns local particles and backgrounds. The host initializes MPI and preserves IDs during migration. All participating ranks must call step. This optional API requires mpi.hpp explicitly.

Main types and entry points

DistributedMccStepper(MccEngine, MPI_Comm, StepOptions); step; communicator

The names above are an interface index; consult the source declarations for exact parameters.

源码 / Source

mpi.hpp

 1// SPDX-License-Identifier: Apache-2.0
 2// MPI 把工作分给多个进程;rank 是进程在 communicator(通信组)中的编号。
 3// 每个进程推进自己持有的粒子,主程序负责空间划分、迁移和背景更新。
 4// MPI 并不替代 OpenMP:每个进程内部仍可用多个线程处理本地粒子。
 5#pragma once
 6
 7#include "batch.hpp"
 8
 9#include <mpi.h>
10#include <string>
11
12namespace algoplasma::mcc {
13
14// Each rank owns its particles, cells and background. The host initializes MPI,
15// preserves particle IDs during migration and calls step() on every rank.
16class DistributedMccStepper {
17public:
18    DistributedMccStepper(MccEngine engine, MPI_Comm communicator,
19                          StepOptions options = {});
20    ~DistributedMccStepper();
21    DistributedMccStepper(const DistributedMccStepper&) = delete;
22    DistributedMccStepper& operator=(const DistributedMccStepper&) = delete;
23
24    [[nodiscard]] StepReport step(ParticleAdapter& particles,
25                                  const std::vector<CellBackground>& background,
26                                  Real dt_s, std::uint64_t global_step,
27                                  std::uint64_t seed);
28    [[nodiscard]] MPI_Comm communicator() const noexcept { return communicator_; }
29
30private:
31    MccStepper local_;
32    MccWorkspace workspace_;
33    MPI_Comm communicator_{MPI_COMM_NULL};
34    int rank_{0};
35    int ranks_{1};
36    std::uint64_t high_watermark_{0};
37
38    void agree(bool failed, const std::string& message) const;
39    [[nodiscard]] std::vector<std::uint64_t> allocate_child_ids(
40        const std::vector<BirthKey>& keys, std::uint64_t global_max,
41        std::uint64_t global_births) const;
42};
43
44} // namespace algoplasma::mcc

文件归属 / File location

G_Collision/G02_MCC_network/mpi.hpp

MPI