163 lines
7.0 KiB
C++
163 lines
7.0 KiB
C++
// Phase5-TessPerf benchmark: serial to_mesh vs true OpenMP to_mesh_parallel
|
|
// vs render-only to_triangle_soup.
|
|
//
|
|
// Model: N UV-sphere B-Reps (make_sphere(1.0, 16, 8) → 128 B-Rep faces each),
|
|
// matching the Lead probe scale: 40/200/800 spheres = 5120/25600/102400 B-Rep
|
|
// faces (~1.02M/5.12M/20.5M triangles at deflection=0.1).
|
|
//
|
|
// Build (MinGW, from repo root) — see run_tess_parallel_bench.ps1.
|
|
// Output: one machine-readable line per scale (timing = API call only, no checks):
|
|
// [tess-bench] n=... brep_faces=... tris=... serial_ms=... parallel_ms=...
|
|
// soup_ms=... speedup=... soup_speedup=... ok=1
|
|
#include "vde/brep/brep.h"
|
|
#include "vde/brep/modeling.h"
|
|
#include "vde/mesh/halfedge_mesh.h"
|
|
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace vde;
|
|
using vde::brep::BrepModel;
|
|
using vde::mesh::HalfedgeMesh;
|
|
|
|
namespace {
|
|
|
|
double ms_since(std::chrono::steady_clock::time_point t0) {
|
|
return std::chrono::duration<double, std::milli>(
|
|
std::chrono::steady_clock::now() - t0).count();
|
|
}
|
|
|
|
double mesh_area(const HalfedgeMesh& mesh) {
|
|
double area = 0.0;
|
|
for (size_t fi = 0; fi < mesh.num_faces(); ++fi) {
|
|
auto fv = mesh.face_vertices(static_cast<int>(fi));
|
|
if (fv.size() < 3) continue;
|
|
const auto a = mesh.vertex(fv[0]);
|
|
const auto b = mesh.vertex(fv[1]);
|
|
const auto c = mesh.vertex(fv[2]);
|
|
area += 0.5 * (b - a).cross(c - a).norm();
|
|
}
|
|
return area;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
int threads = 8;
|
|
double deflection = 0.1;
|
|
std::vector<int> counts = {40, 200, 800};
|
|
for (int i = 1; i < argc; ++i) {
|
|
const std::string a = argv[i];
|
|
if (a == "--threads" && i + 1 < argc) threads = std::atoi(argv[++i]);
|
|
else if (a == "--deflection" && i + 1 < argc) deflection = std::atof(argv[++i]);
|
|
else if (a == "--counts" && i + 1 < argc) {
|
|
counts.clear();
|
|
std::string s = argv[++i];
|
|
size_t pos = 0;
|
|
while (pos <= s.size()) {
|
|
const size_t comma = s.find(',', pos);
|
|
const std::string tok =
|
|
s.substr(pos, comma == std::string::npos ? std::string::npos
|
|
: comma - pos);
|
|
if (!tok.empty()) counts.push_back(std::atoi(tok.c_str()));
|
|
if (comma == std::string::npos) break;
|
|
pos = comma + 1;
|
|
}
|
|
} else {
|
|
std::fprintf(stderr,
|
|
"usage: %s [--threads N] [--deflection D] [--counts n1,n2,...]\n",
|
|
argv[0]);
|
|
return 2;
|
|
}
|
|
}
|
|
std::printf("# tess_parallel_bench threads=%d deflection=%.4f\n", threads,
|
|
deflection);
|
|
std::printf("# scale brep_faces serial_ms parallel_ms soup_ms sp_par sp_soup\n");
|
|
int all_ok = 1;
|
|
for (int n : counts) {
|
|
std::vector<BrepModel> bodies;
|
|
bodies.reserve(static_cast<size_t>(n));
|
|
for (int i = 0; i < n; ++i) bodies.push_back(brep::make_sphere(1.0, 16, 8));
|
|
const size_t brep_faces =
|
|
static_cast<size_t>(n) * bodies.front().num_faces();
|
|
|
|
// ---- serial to_mesh (timed: API call only) ----
|
|
const auto t0 = std::chrono::steady_clock::now();
|
|
size_t sv = 0, st = 0;
|
|
for (const auto& b : bodies) {
|
|
const auto m = b.to_mesh(deflection);
|
|
sv += m.num_vertices();
|
|
st += m.num_faces();
|
|
}
|
|
const double serial_ms = ms_since(t0);
|
|
|
|
// ---- parallel to_mesh (warm-up first) ----
|
|
(void)bodies.front().to_mesh_parallel(deflection, threads);
|
|
const auto t1 = std::chrono::steady_clock::now();
|
|
size_t pv = 0, pt = 0;
|
|
for (const auto& b : bodies) {
|
|
const auto m = b.to_mesh_parallel(deflection, threads);
|
|
pv += m.num_vertices();
|
|
pt += m.num_faces();
|
|
}
|
|
const double parallel_ms = ms_since(t1);
|
|
|
|
// ---- triangle soup (render path, no halfedge topology) ----
|
|
(void)bodies.front().to_triangle_soup(deflection, true, threads);
|
|
const auto t2 = std::chrono::steady_clock::now();
|
|
size_t soup_tris = 0, soup_verts = 0;
|
|
for (const auto& b : bodies) {
|
|
const auto s = b.to_triangle_soup(deflection, true, threads);
|
|
soup_verts += s.num_vertices();
|
|
soup_tris += s.num_triangles();
|
|
}
|
|
const double soup_ms = ms_since(t2);
|
|
|
|
// ---- correctness (untimed): compare first body in detail ----
|
|
const auto mesh_s = bodies.front().to_mesh(deflection);
|
|
const auto mesh_p = bodies.front().to_mesh_parallel(deflection, threads);
|
|
const auto soup = bodies.front().to_triangle_soup(deflection, true, threads);
|
|
const double serial_area = mesh_area(mesh_s);
|
|
const double parallel_area = mesh_area(mesh_p);
|
|
double soup_area = 0.0;
|
|
for (size_t ti = 0; ti < soup.num_triangles(); ++ti) {
|
|
const auto& idx = soup.indices[ti];
|
|
const auto& a = soup.positions[static_cast<size_t>(idx[0])];
|
|
const auto& b = soup.positions[static_cast<size_t>(idx[1])];
|
|
const auto& c = soup.positions[static_cast<size_t>(idx[2])];
|
|
soup_area += 0.5 * (b - a).cross(c - a).norm();
|
|
}
|
|
const bool tri_equal = (st == pt) && (sv == pv);
|
|
const double area_rel = (serial_area > 0.0)
|
|
? std::abs(parallel_area - serial_area) / serial_area
|
|
: 0.0;
|
|
const bool soup_equal = (soup.num_triangles() == mesh_s.num_faces()) &&
|
|
(soup.num_vertices() == mesh_s.num_vertices());
|
|
const double soup_rel = (serial_area > 0.0)
|
|
? std::abs(soup_area - serial_area) / serial_area
|
|
: 0.0;
|
|
const bool ok = tri_equal && area_rel <= 1e-12 && soup_equal &&
|
|
soup_rel <= 1e-12;
|
|
all_ok = all_ok && ok;
|
|
|
|
const double speedup = (parallel_ms > 1e-9) ? serial_ms / parallel_ms : 0.0;
|
|
const double soup_speedup = (soup_ms > 1e-9) ? serial_ms / soup_ms : 0.0;
|
|
std::printf("n=%-4d brep_faces=%-6zu tris=%-9zu serial_ms=%9.2f "
|
|
"parallel_ms=%9.2f soup_ms=%9.2f sp_par=%5.2fx sp_soup=%5.2fx "
|
|
"tri_equal=%d soup_equal=%d area_rel=%.3e soup_rel=%.3e %s\n",
|
|
n, brep_faces, st, serial_ms, parallel_ms, soup_ms, speedup,
|
|
soup_speedup, tri_equal ? 1 : 0, soup_equal ? 1 : 0, area_rel,
|
|
soup_rel, ok ? "OK" : "FAIL");
|
|
std::printf("[tess-bench] n=%d brep_faces=%zu tris=%zu serial_ms=%.3f "
|
|
"parallel_ms=%.3f soup_ms=%.3f speedup=%.3f soup_speedup=%.3f ok=%d\n",
|
|
n, brep_faces, st, serial_ms, parallel_ms, soup_ms, speedup,
|
|
soup_speedup, ok ? 1 : 0);
|
|
std::fflush(stdout);
|
|
}
|
|
return all_ok ? 0 : 1;
|
|
}
|