flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
bench_bottom_factor.cpp
Go to the documentation of this file.
1
20#include <algorithm>
21#include <cmath>
22#include <cstdio>
23#include <cstdlib>
24#include <cstring>
25#include <Kokkos_Core.hpp>
26#include <random>
27#include <string>
28#include <type_traits>
29#include <vector>
30
31#include "mac_cutcell_mg.hpp"
32
33using namespace peclet::flow;
34
35namespace {
36
37template <class V>
38std::vector<char> bytes(const V& v) {
39 auto h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), v);
40 const char* p = reinterpret_cast<const char*>(h.data());
41 return std::vector<char>(p, p + h.span() * sizeof(typename V::value_type));
42}
43std::vector<char> storage(const BottomDirect<float>& D) {
44 std::vector<char> out;
45 for (const auto& b : {bytes(D.Q), bytes(D.Y), bytes(D.e), bytes(D.s), bytes(D.stat)})
46 out.insert(out.end(), b.begin(), b.end());
47 return out;
48}
49
50} // namespace
51
52int main(int argc, char** argv) {
53 Kokkos::initialize(argc, argv);
54 {
55 const int reps = argc > 1 ? std::atoi(argv[1]) : 20;
56 const std::string cname = argc > 2 ? argv[2] : "walls-y";
57 const char* dump = (argc > 3 && std::strcmp(argv[3], "-") != 0) ? argv[3] : nullptr;
58 std::vector<int> Ts;
59 for (int a = 4; a < argc; ++a)
60 Ts.push_back(std::atoi(argv[a]));
61 if (Ts.empty())
62 Ts = {1, 2, 4, 8, 16, 24};
63 int bc[6] = {0, 0, 1, 1, 0, 0};
64 if (cname == "periodic")
65 bc[2] = bc[3] = 0;
66 const int nx = 32, ny = 24, nz = 16;
67 CutcellMG mg;
69 mg.init(nx, ny, nz, 2);
71 const C3 e{nx + 2, ny + 2, nz + 2};
72 const std::size_t n = (std::size_t)e.x * e.y * e.z;
73 std::vector<double> hx(n), hy(n), hz(n);
74 const double lr = std::log(50.0), k = 2.0 * M_PI;
75 for (int z = 0; z < e.z; ++z) // test_bottom_direct's openness (ratio 50)
76 for (int y = 0; y < e.y; ++y)
77 for (int x = 0; x < e.x; ++x) {
78 const std::size_t i = x + (std::size_t)y * e.x + (std::size_t)z * e.x * e.y;
79 const double s = 0.5 + 0.5 * std::sin(k * x / nx) * std::cos(k * (y + 0.5 * z) / ny);
80 const double t = 0.5 + 0.5 * std::cos(k * (x + z) / nz);
81 hx[i] = std::exp(lr * s) / 50.0;
82 hy[i] = std::exp(lr * t) / 50.0;
83 hz[i] = std::exp(lr * (1.0 - s)) / 50.0;
84 }
85 auto dev = [](const std::vector<double>& v, const char* name) {
86 CCField f(name, v.size());
87 auto h = Kokkos::create_mirror_view(f);
88 for (std::size_t i = 0; i < v.size(); ++i)
89 h(i) = v[i];
90 Kokkos::deep_copy(f, h);
91 return f;
92 };
93 CCField ox = dev(hx, "ox"), oy = dev(hy, "oy"), oz = dev(hz, "oz");
94 mg.setOpenness(CCConst(ox), CCConst(oy), CCConst(oz), 1.0, 1.0, 1.0);
96 const BottomPlanes pl = mg.directPlanes();
97 printf("bench_bottom_factor %s: P = %d, b = %d, border %d, %d reps, concurrency %d\n",
98 cname.c_str(), pl.P, pl.b, pl.border, reps, (int)CCExec().concurrency());
99 std::vector<char> ref;
100 const bool host = std::is_same_v<CCMem, Kokkos::HostSpace>;
101 for (int alg = 0; alg < (host ? 2 : 1); ++alg)
102 for (int T : Ts) {
103 const bool team = alg == 1; // on a host backend: 0 = the host schedule, 1 = the team one
104 int Tu = 0;
105 BottomDirect<float> D = mg.directFactorForTest<float>(T, kBottomPivotTol, &Tu, team);
106 if (T != 0 && Tu != T) {
107 printf(" T %3d: not available (ran %d), skipped\n", T, Tu);
108 continue;
109 }
110 for (int w = 0; w < 3; ++w)
111 mg.launchDirectFactor(D, Tu, kBottomPivotTol, /*probe=*/false, team);
112 Kokkos::fence();
113#ifdef PECLET_FLOW_BOTTOM_PROBE
114 Kokkos::deep_copy(mg.bottomProbe_, 0LL);
115#endif
116 std::vector<double> ms;
117 for (int r = 0; r < reps; ++r) {
118 Kokkos::Timer tm;
119 mg.launchDirectFactor(D, Tu, kBottomPivotTol, /*probe=*/false, team);
120 Kokkos::fence();
121 ms.push_back(tm.seconds() * 1e3);
122 }
123 std::sort(ms.begin(), ms.end());
124 const std::vector<char> st = storage(D);
125 const char* same = "reference";
126 if (ref.empty()) {
127 ref = st;
128 if (dump) {
129 FILE* f = std::fopen(dump, "wb");
130 std::fwrite(ref.data(), 1, ref.size(), f);
131 std::fclose(f);
132 }
133 } else {
134 same = (st == ref) ? "bitwise identical" : "DIFFERS";
135 }
136 printf(" %s T %3d: factor min %8.3f ms median %8.3f ms max %8.3f ms (%s)\n",
137 host ? (team ? "team" : "host") : "team", Tu, ms.front(), ms[ms.size() / 2],
138 ms.back(), same);
139#ifdef PECLET_FLOW_BOTTOM_PROBE
140 if (!host || team) {
141 auto h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), mg.bottomProbe_);
142 double tot = 0.0;
143 for (int c = 0; c < 8; ++c)
144 tot += (double)h(c);
145 const char* nm[8] = {"scale+cpl", "assemble", "diag", "panel",
146 "trailing", "W", "Q", "border"};
147 printf(" phase split (ms of the median):");
148 for (int c = 0; c < 8; ++c)
149 if (h(c) > 0)
150 printf(" %s %.3f", nm[c], tot > 0 ? ms[ms.size() / 2] * (double)h(c) / tot : 0.0);
151 printf("\n");
152 }
153#endif
154 }
155 { // the solve: M alone vs the production FCG solve, production team sizes
156 CutcellMG::Level& bt = mg.level(mg.nLevels() - 1);
157 std::vector<double> hb(bt.n);
158 std::mt19937 rng(11);
159 std::uniform_real_distribution<double> U(-1.0, 1.0);
160 for (auto& v : hb)
161 v = U(rng);
162 CCField r = dev(hb, "r"), z("z", bt.n);
164 Kokkos::deep_copy(bt.rhs, r);
165 int it = mg.directSolveForTest(); // refactors (stale) and solves once
166 std::vector<double> mM, mS;
167 for (int rr = 0; rr < reps + 3; ++rr) {
168 Kokkos::Timer tm;
169 mg.directApplyForTest(D0, z, r, 0);
170 const double a = tm.seconds() * 1e3;
171 Kokkos::deep_copy(bt.rhs, r);
172 Kokkos::fence();
173 Kokkos::Timer ts;
174 it = mg.directSolveForTest();
175 const double b = ts.seconds() * 1e3;
176 if (rr >= 3) {
177 mM.push_back(a);
178 mS.push_back(b);
179 }
180 }
181 std::sort(mM.begin(), mM.end());
182 std::sort(mS.begin(), mS.end());
183 printf(
184 " solve: M alone min %.4f median %.4f ms; FCG solve min %.4f median %.4f ms (%d inner "
185 "iterations)\n",
186 mM.front(), mM[mM.size() / 2], mS.front(), mS[mS.size() / 2], it);
187 }
188 }
189 Kokkos::finalize();
190 return 0;
191}
const BottomPlanes & directPlanes() const
void setOpenness(CCConst ox, CCConst oy, CCConst oz, double idx2, double idy2, double idz2)
void setBoundaryConditions(const int bc[6])
int launchDirectFactor(const BottomDirect< FR > &D, int team, double tauPiv0, bool probe=true, bool teamAlgorithm=false, const CCExec &exec=CCExec())
int directApplyForTest(const BottomDirect< FR > &D, CCField z, CCField r, int team, bool noMean=false, bool teamAlgorithm=false)
void setAgglomerationMode(int mode)
void init(int nx, int ny, int nz, int nLevels)
BottomDirect< FR > directFactorForTest(int team, double tauPiv0=kBottomPivotTol, int *Tused=nullptr, bool teamAlgorithm=false)
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
Kokkos::DefaultExecutionSpace CCExec
Kokkos::View< const double *, CCMem > CCConst
Kokkos::View< double *, CCMem > CCField
constexpr double kBottomPivotTol
static constexpr double U
static bool same(double a, double b)