flow 1.3.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
probe.cpp
Go to the documentation of this file.
1// Batched evaluation parity: the device drivers must reproduce the host per-point evaluation
2// BIT-IDENTICALLY (same backend), sphere-union + candidate grid + min-image periodicity included.
3#include <cstdint>
4#include <cstdio>
5#include <cstring>
6#include <fstream>
7#include <Kokkos_Core.hpp>
8#include <sstream>
9#include <vector>
10
11#include "peclet/core/geom/device_scene.hpp"
12
13using namespace peclet::core;
14using namespace peclet::core::geom;
15
16int main(int argc, char** argv) {
17 Kokkos::initialize(argc, argv);
18 int bad = 0;
19 {
20 // the RCP bed
21 std::vector<double> cx, cy, cz, r;
22 std::ifstream f("/home/frankp/Codes/suite/core/tests/data/rcp_pack_seed3_unit.txt");
23 std::string line;
24 while (std::getline(f, line)) {
25 if (line.empty() || line[0] == '#')
26 continue;
27 std::istringstream is(line);
28 double x, y, z, rr;
29 if (is >> x >> y >> z >> rr) {
30 cx.push_back(x);
31 cy.push_back(y);
32 cz.push_back(z);
33 r.push_back(rr);
34 }
35 }
36 SphereBedQuery q(cx, cy, cz, r, Vec<3>{0, 0, 0}, Vec<3>{1, 1, 1}, true);
37
38 // device copies of the union + grid
39 const int n = q.sphereUnion().n;
40 auto mkview = [](const char* nm, const double* src, std::size_t cnt) {
41 Kokkos::View<double*> v(nm, cnt);
42 auto h = Kokkos::create_mirror_view(v);
43 for (std::size_t i = 0; i < cnt; ++i)
44 h(i) = src[i];
45 Kokkos::deep_copy(v, h);
46 return v;
47 };
48 auto dcx = mkview("cx", q.sphereUnion().cx, n), dcy = mkview("cy", q.sphereUnion().cy, n),
49 dcz = mkview("cz", q.sphereUnion().cz, n), dr = mkview("r", q.sphereUnion().r, n);
50 const auto& gh = q.grid();
51 const long nbins = (long)gh.nx * gh.ny * gh.nz;
52 Kokkos::View<int*> doff("off", nbins + 1), ditems("items", gh.offsets[nbins]);
53 {
54 auto h1 = Kokkos::create_mirror_view(doff);
55 for (long i = 0; i <= nbins; ++i)
56 h1(i) = gh.offsets[i];
57 Kokkos::deep_copy(doff, h1);
58 auto h2 = Kokkos::create_mirror_view(ditems);
59 for (int i = 0; i < gh.offsets[nbins]; ++i)
60 h2(i) = gh.items[i];
61 Kokkos::deep_copy(ditems, h2);
62 }
63 SphereUnionView<double> du = q.sphereUnion();
64 du.cx = dcx.data();
65 du.cy = dcy.data();
66 du.cz = dcz.data();
67 du.r = dr.data();
68 CandidateGridView<double> dg = gh;
69 dg.offsets = doff.data();
70 dg.items = ditems.data();
71
72 // probe points: box + beyond + surface-hugging
73 const int NP = 200000;
74 Kokkos::View<double* [3]> pts("pts", NP);
75 auto hp = Kokkos::create_mirror_view(pts);
76 std::uint64_t st = 0x9E3779B97F4A7C15ull;
77 auto u01 = [&]() {
78 st ^= st << 13;
79 st ^= st >> 7;
80 st ^= st << 17;
81 return (double)((st >> 11) & ((1ull << 53) - 1)) / (double)(1ull << 53);
82 };
83 for (int i = 0; i < NP; ++i) {
84 if (i % 3 == 0) { // surface-hugging
85 const int s = (int)(u01() * n);
86 const double th = 6.283185307179586 * u01(), uz = 2 * u01() - 1,
87 sr = std::sqrt(1 - uz * uz), eps = (u01() - 0.5) * 4e-3;
88 hp(i, 0) = cx[s] + (r[s] + eps) * sr * std::cos(th);
89 hp(i, 1) = cy[s] + (r[s] + eps) * sr * std::sin(th);
90 hp(i, 2) = cz[s] + (r[s] + eps) * uz;
91 } else {
92 hp(i, 0) = 1.6 * u01() - 0.3;
93 hp(i, 1) = 1.6 * u01() - 0.3;
94 hp(i, 2) = 1.6 * u01() - 0.3;
95 }
96 }
97 Kokkos::deep_copy(pts, hp);
98
99 Kokkos::View<double*> out("out", NP);
100 evalSphereUnionPoints(Kokkos::DefaultExecutionSpace{}, du, q.box(), dg, pts, out);
101 Kokkos::fence();
102 auto ho = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, out);
103
104 int nbit = 0;
105 for (int i = 0; i < NP; ++i) {
106 const double ref = q(Vec<3>{hp(i, 0), hp(i, 1), hp(i, 2)});
107 std::uint64_t a, b;
108 std::memcpy(&a, &ref, 8);
109 const double v = ho(i);
110 std::memcpy(&b, &v, 8);
111 if (a != b) {
112 if (nbit < 3)
113 std::printf(" mismatch %d: dev %.17g host %.17g\n", i, v, ref);
114 ++nbit;
115 }
116 }
117 std::printf(" batched device vs host query: %d/%d bit mismatches\n", nbit, NP);
118 if (nbit)
119 bad = 1;
120 }
121 Kokkos::finalize();
122 std::printf(bad ? "BATCH FAIL\n" : "BATCH OK\n");
123 return bad;
124}
int main()
Definition dbg_scene.cpp:13