62int main(
int argc,
char** argv) {
63 MPI_Init(&argc, &argv);
64 Kokkos::initialize(argc, argv);
65 int fail = 0, size = 1, rank = 0;
67 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
68 MPI_Comm_size(MPI_COMM_WORLD, &size);
69 const std::vector<double> gsdf =
sphereSdf();
73 peclet::flow::CutcellMG::decomposition(
static_cast<std::size_t
>(size),
N,
N,
N, 0, 1.05);
74 auto blk = dec.block(rank);
75 const int org[3] = {(int)blk.origin[0], (
int)blk.origin[1], (int)blk.origin[2]};
76 const int ln[3] = {(int)blk.size[0], (
int)blk.size[1], (int)blk.size[2]};
77 std::vector<double> lsdf((std::size_t)ln[0] * ln[1] * ln[2]);
78 for (
int z = 0; z < ln[2]; ++z)
79 for (
int y = 0; y < ln[1]; ++y)
80 for (
int x = 0; x < ln[0]; ++x)
81 lsdf[(std::size_t)x + (std::size_t)y * ln[0] + (std::size_t)z * ln[0] * ln[1]] =
82 gsdf[(std::size_t)(x + org[0]) + (std::size_t)(y + org[1]) *
N +
83 (std::size_t)(z + org[2]) *
N *
N];
85 sd.initMpi(
N,
N,
N, MPI_COMM_WORLD);
88 long gammaMismatch = 0;
92 for (
int k = 0; k <
STEPS; ++k) {
94 std::vector<double> g = acc.core().gamma();
95 int head[2] = {(int)g.size(), (int)acc.core().status()};
96 int head0[2] = {head[0], head[1]};
97 MPI_Bcast(head0, 2, MPI_INT, 0, MPI_COMM_WORLD);
98 std::vector<double> g0 = g;
99 g0.resize((std::size_t)head0[0]);
100 MPI_Bcast(g0.data(), head0[0], MPI_DOUBLE, 0, MPI_COMM_WORLD);
101 if (head[0] != head0[0] || head[1] != head0[1] ||
102 std::memcmp(g.data(), g0.data(),
sizeof(
double) * g.size()) != 0)
105 lastRes = acc.residual();
107 std::printf(
" distributed: status %s, residual %.6e, %d columns, %d restarts\n",
108 acc.statusName(), acc.residual(), acc.numColumns(), acc.numRestarts());
111 MPI_Allreduce(&gammaMismatch, &gm, 1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
114 std::vector<std::vector<double>> ug(3);
115 for (
int c = 0; c < 3; ++c) {
117 std::vector<int> meta(6 * size);
118 int mine[6] = {org[0], org[1], org[2], ln[0], ln[1], ln[2]};
119 MPI_Gather(mine, 6, MPI_INT, meta.data(), 6, MPI_INT, 0, MPI_COMM_WORLD);
120 std::vector<int> counts(size), displs(size);
121 int nloc = (int)loc.size();
122 MPI_Gather(&nloc, 1, MPI_INT, counts.data(), 1, MPI_INT, 0, MPI_COMM_WORLD);
123 std::vector<double> all;
126 for (
int r = 0; r < size; ++r) {
130 all.resize((std::size_t)off);
132 MPI_Gatherv(loc.data(), nloc, MPI_DOUBLE, all.data(), counts.data(), displs.data(),
133 MPI_DOUBLE, 0, MPI_COMM_WORLD);
135 ug[c].assign((std::size_t)
N *
N *
N, 0.0);
136 for (
int r = 0; r < size; ++r) {
137 const int* m = &meta[6 * r];
138 for (
int z = 0; z < m[5]; ++z)
139 for (
int y = 0; y < m[4]; ++y)
140 for (
int x = 0; x < m[3]; ++x)
141 ug[c][(std::size_t)(x + m[0]) + (std::size_t)(y + m[1]) *
N +
142 (std::size_t)(z + m[2]) *
N *
N] =
143 all[(std::size_t)displs[r] + (std::size_t)x + (std::size_t)y * m[3] +
144 (std::size_t)z * m[3] * m[4]];
153 ref.setSolid(gsdf,
true);
155 for (
int k = 0; k <
STEPS; ++k)
157 std::printf(
" single-rank: status %s, residual %.6e (distributed %.6e)\n", acc.statusName(),
158 acc.residual(), lastRes);
160 for (
int c = 0; c < 3; ++c) {
161 const std::vector<double> u1 = ref.getVelocity(c);
162 double du = 0.0, umax = 0.0;
163 for (std::size_t i = 0; i < u1.size(); ++i) {
164 du = std::max(du, std::fabs(ug[c][i] - u1[i]));
165 umax = std::max(umax, std::fabs(u1[i]));
167 const double rel = umax > 0.0 ? du / umax : du;
168 worst = std::max(worst, rel);
169 std::printf(
" component %d: max|u_np - u_1| / max|u_1| = %.3e (max|u_1| = %.6e)\n", c, rel,
172 std::printf(
" gamma / window / status mismatches across ranks: %ld\n", gm);
173 if (worst > 1e-8 || gm != 0)
178 MPI_Allreduce(&fail, &totalFail, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
181 std::printf(
"OK (np=%d): distributed Anderson march == single-rank, gamma identical\n", size);
183 std::fprintf(stderr,
"FAILED (np=%d)\n", size);
187 return totalFail == 0 ? 0 : 1;