fix(brep): 消除 mesh_cache 多线程惊群与数据竞争

- ssi_boolean: get_mesh/get_mesh_grid 构建串行化(mesh_build_mutex)+ 双重
  检查,OpenMP 首次冷缓存时球体网格从 6 线程各建一次(各 ~7s)降为 1 次
- 两处 mesh_cache 由 unordered_map 改为 std::map:节点地址稳定,插入新
  实体时不再 rehash 搬移已缓存的 HalfedgeMesh,避免并行执行中在途引用悬垂
- brep_boolean: get_mesh 原先在 6 处 #pragma omp parallel for 内无锁访问
  unordered_map(数据竞争 UB),补锁 + 同样双重检查模式,并将 to_mesh()
  移出锁外执行
- 验证: BoxWithSphere PASSED 44.6s,墙钟无回归,mesh_builds 由 6 降为 1
This commit is contained in:
hm
2026-08-14 22:54:11 +08:00
parent f342d91eea
commit ab159fc08d
2 changed files with 48 additions and 6 deletions
+23 -4
View File
@@ -41,13 +41,32 @@ enum ClassResult { IN = -1, ON = 0, OUT = 1 };
// ── Mesh caching ─────────────────────────────────────────
/// Cache to_mesh() results keyed by BrepModel pointer.
/// Avoids re-tessellating the same body for every face classification.
static std::unordered_map<const BrepModel*, vde::mesh::HalfedgeMesh> mesh_cache;
// std::map keeps element addresses stable across insertions (unordered_map
// would rehash and move cached meshes, invalidating in-flight references
// under the #pragma omp parallel for below).
static std::map<const BrepModel*, vde::mesh::HalfedgeMesh> mesh_cache;
static std::mutex mesh_cache_mutex;
// Serializes construction: prevents N OpenMP threads from all building the
// same mesh when the cache is cold ("thundering herd").
static std::mutex mesh_build_mutex;
/// Get the tessellated mesh for a body, reusing cached result.
static const vde::mesh::HalfedgeMesh& get_mesh(const BrepModel& body) {
auto it = mesh_cache.find(&body);
if (it != mesh_cache.end()) return it->second;
auto [inserted_it, _] = mesh_cache.emplace(&body, body.to_mesh(0.05));
{
std::lock_guard<std::mutex> lock(mesh_cache_mutex);
auto it = mesh_cache.find(&body);
if (it != mesh_cache.end()) return it->second;
}
std::lock_guard<std::mutex> build_lock(mesh_build_mutex);
{
// Double-check: another thread may have completed while we waited.
std::lock_guard<std::mutex> lock(mesh_cache_mutex);
auto it = mesh_cache.find(&body);
if (it != mesh_cache.end()) return it->second;
}
auto mesh = body.to_mesh(0.05);
std::lock_guard<std::mutex> lock(mesh_cache_mutex);
auto [inserted_it, _] = mesh_cache.emplace(&body, std::move(mesh));
return inserted_it->second;
}
+25 -2
View File
@@ -64,8 +64,16 @@ namespace {
// Mesh caching (shared with brep_boolean.cpp pattern)
// ═══════════════════════════════════════════════════════════
static std::unordered_map<const BrepModel*, vde::mesh::HalfedgeMesh> mesh_cache;
// std::map (node-based) keeps element addresses stable across insertions, so
// the references returned by get_mesh()/get_mesh_grid() never dangle when
// other threads insert additional bodies (unordered_map would rehash and move
// the values, invalidating in-flight references during OpenMP execution).
static std::map<const BrepModel*, vde::mesh::HalfedgeMesh> mesh_cache;
static std::mutex mesh_cache_mutex;
// Serializes actual mesh/grid construction: without it, N OpenMP threads that
// all miss the cache build the same 40k-triangle mesh N times ("thundering
// herd", ~7s × N wasted CPU even though wall-clock overlap hides it).
static std::mutex mesh_build_mutex;
// ── Diagnostic counters (only active under VDE_SSI_DIAG) ───────────────
static thread_local long long tls_mesh_builds = 0; // to_mesh() calls
@@ -81,6 +89,14 @@ static const vde::mesh::HalfedgeMesh& get_mesh(const BrepModel& body) {
auto it = mesh_cache.find(&body);
if (it != mesh_cache.end()) return it->second;
}
// Slow path: serialize construction so concurrent misses build once.
std::lock_guard<std::mutex> build_lock(mesh_build_mutex);
{
// Double-check: another thread may have completed while we waited.
std::lock_guard<std::mutex> lock(mesh_cache_mutex);
auto it = mesh_cache.find(&body);
if (it != mesh_cache.end()) return it->second;
}
auto mesh = body.to_mesh(0.05);
tls_mesh_builds++;
std::lock_guard<std::mutex> lock(mesh_cache_mutex);
@@ -102,7 +118,7 @@ struct TriangleGrid {
std::vector<std::vector<int>> cells; // triangle index per cell
};
static std::unordered_map<const vde::mesh::HalfedgeMesh*, TriangleGrid> grid_cache;
static std::map<const vde::mesh::HalfedgeMesh*, TriangleGrid> grid_cache;
static const TriangleGrid& get_mesh_grid(const vde::mesh::HalfedgeMesh& mesh) {
{
@@ -110,6 +126,13 @@ static const TriangleGrid& get_mesh_grid(const vde::mesh::HalfedgeMesh& mesh) {
auto it = grid_cache.find(&mesh);
if (it != grid_cache.end()) return it->second;
}
// Serialize grid construction (same thundering-herd concern as get_mesh).
std::lock_guard<std::mutex> build_lock(mesh_build_mutex);
{
std::lock_guard<std::mutex> lock(mesh_cache_mutex);
auto it = grid_cache.find(&mesh);
if (it != grid_cache.end()) return it->second;
}
TriangleGrid g;
g.mesh = &mesh;