Files
ViewDesignEngine/tools/vde_gpu_probe.cpp
T
hm ea8aaf0759 perf(gpu): OpenCL point-cloud normal/SOR batch + cl_mem_flags fix (1.0.142)
- add ComputeBackend::point_normal_stats_batch (non-pure, CPU default fallback)
- OpenCL: spatial-hash grid build + 27-neighbourhood PCA normal/mean-distance kernels
- PointCloud normal_estimation / statistical_outlier_removal: GPU path above 20k points,
  always fall back to CPU; report capability honestly via point_cloud_gpu_available()
- fix cl_mem_flags constants to OpenCL spec (READ_ONLY=4, WRITE_ONLY=2, READ_WRITE=1);
  wrong values made new read-write buffers fail with CL_INVALID_VALUE(-30)
- fix tool selftest registration: use BUILD_TESTS and enable_testing() before tools/
- 100k pts: normals 162.7x, SOR 123.3x; real STL 100k pts: 57-111x; exact CPU agreement
- full CTest 8128/8128, release check 14/14
2026-11-04 07:59:36 +08:00

307 lines
13 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file vde_gpu_probe.cpp
* @brief GPU/CPU 计算后端探测与批处理基准(OpenCL/CUDA 动态后端)。
*
* 用法: vde_gpu_probe [--mode sphere|tri|cloud] [--points N] [--k K] [--json]
* - sphere/tri: 默认后端 SDF/三角形批处理,与 CPU SIMD 逐点误差 + 耗时对比。
* - cloud(VDE-1278): 点云 kNN 法线估计(vde::mesh::PointCloud::normal_estimation)
* 的 CPU vs GPU 耗时与法线夹角对比。通过 VDE_GPU_CLOUD_MIN_POINTS 分别强制
* CPU/GPU 路径;无 GPU 时如实报告 CPU 回退(gpu=false、speedup≈1)。
*
* 诚实口径:磁盘/文件读写带宽本身不受 GPU 加速,本工具的 cloud 模式衡量的是
* 导入后可并行后处理(kNN + PCA)的收益。
*/
#include "vde/compute/compute_backend.h"
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <random>
#include <string>
#include <vector>
#if defined(VDE_GPU_PROBE_HAVE_MESH)
#include "vde/mesh/point_cloud.h"
#endif
#if defined(VDE_GPU_PROBE_HAVE_IO)
#include "vde/foundation/io_stl.h"
#endif
namespace {
using Clock = std::chrono::steady_clock;
double elapsed_ms(Clock::time_point a, Clock::time_point b) {
return std::chrono::duration<double, std::milli>(b - a).count();
}
const char* type_name(vde::compute::BackendType type) {
using vde::compute::BackendType;
switch (type) {
case BackendType::CpuSimd: return "cpu-simd";
case BackendType::Cuda: return "cuda";
case BackendType::Hip: return "hip";
case BackendType::OpenCl: return "opencl";
}
return "unknown";
}
void set_env(const char* name, const char* value) {
#if defined(_WIN32)
_putenv_s(name, value ? value : "");
#else
if (value) {
setenv(name, value, 1);
} else {
unsetenv(name);
}
#endif
}
} // namespace
int main(int argc, char** argv) {
int n = 1000000;
bool points_given = false;
int k = 10;
bool json = false;
std::string mode = "sphere";
std::string file;
for (int i = 1; i < argc; ++i) {
if (std::strcmp(argv[i], "--json") == 0) json = true;
else if (std::strcmp(argv[i], "--mode") == 0 && i + 1 < argc) {
mode = argv[++i];
if (mode != "sphere" && mode != "tri" && mode != "cloud") {
std::fprintf(stderr, "invalid --mode (sphere|tri|cloud)\n"); return 2;
}
}
else if (std::strcmp(argv[i], "--points") == 0 && i + 1 < argc) {
n = std::atoi(argv[++i]);
points_given = true;
if (n <= 0) { std::fprintf(stderr, "invalid --points\n"); return 2; }
}
else if (std::strcmp(argv[i], "--file") == 0 && i + 1 < argc) {
file = argv[++i];
}
else if (std::strcmp(argv[i], "--k") == 0 && i + 1 < argc) {
k = std::atoi(argv[++i]);
if (k < 1 || k > 64) { std::fprintf(stderr, "invalid --k (1..64)\n"); return 2; }
} else {
std::fprintf(stderr,
"usage: vde_gpu_probe [--mode sphere|tri|cloud] [--points N] "
"[--k K] [--file PATH] [--json]\n");
return 2;
}
}
if (!file.empty() && mode != "cloud") {
std::fprintf(stderr, "--file is only supported with --mode cloud\n");
return 2;
}
vde::compute::CpuSimdComputeBackend cpu;
vde::compute::ComputeBackend& gpu = vde::compute::default_backend();
const auto info = gpu.info();
double max_diff = 0.0;
double cpu_ms = 0.0, gpu_ms = 0.0;
bool cloud_gpu_available = false;
if (mode == "cloud") {
#if defined(VDE_GPU_PROBE_HAVE_MESH)
// 点云模式 CPU 路径是 O(n·c log c);未显式给点数时用较小的默认值。
if (!points_given) n = 100000;
std::vector<vde::core::Point3D> pts;
if (!file.empty()) {
#if defined(VDE_GPU_PROBE_HAVE_IO)
std::vector<vde::foundation::StlTriangle> tris;
try {
tris = vde::foundation::read_stl(file);
} catch (const std::exception& e) {
std::fprintf(stderr, "read_stl failed: %s\n", e.what());
return 2;
}
if (tris.empty()) {
std::fprintf(stderr, "STL contains no triangles: %s\n", file.c_str());
return 2;
}
// 三角面 v0 近似均匀采样曲面点;strided 采样控制点数 = --points 目标。
const std::size_t tris_count = tris.size();
std::size_t stride = tris_count / static_cast<std::size_t>(n);
if (stride == 0) stride = 1;
for (std::size_t i = 0; i < tris_count; i += stride) {
pts.push_back(tris[i].v0);
if (static_cast<int>(pts.size()) >= n) break;
}
std::printf("source=stl:%s triangles=%zu stride=%zu\n", file.c_str(),
tris_count, stride);
#else
std::fprintf(stderr, "--file requires vde_foundation (STL reader)\n");
return 2;
#endif
} else {
const int side = static_cast<int>(std::ceil(std::sqrt(static_cast<double>(n))));
pts.reserve(static_cast<std::size_t>(side) * static_cast<std::size_t>(side));
for (int iy = 0; iy < side; ++iy) {
for (int ix = 0; ix < side; ++ix) {
const double x = ix * 0.02;
const double y = iy * 0.02;
pts.emplace_back(x, y, 2.0 * std::sin(0.5 * x) * std::cos(0.5 * y));
}
}
}
const vde::mesh::PointCloud cloud(std::move(pts));
n = static_cast<int>(cloud.size());
cloud_gpu_available = vde::mesh::point_cloud_gpu_available();
set_env("VDE_GPU_CLOUD_MIN_POINTS", "999999999");
const auto t0 = Clock::now();
const vde::mesh::PointCloud cpu_normals = cloud.normal_estimation(k);
const auto t1 = Clock::now();
set_env("VDE_GPU_CLOUD_MIN_POINTS", "1");
const vde::mesh::PointCloud gpu_normals = cloud.normal_estimation(k);
const auto t2 = Clock::now();
set_env("VDE_GPU_CLOUD_MIN_POINTS", nullptr);
cpu_ms = elapsed_ms(t0, t1);
gpu_ms = elapsed_ms(t1, t2);
set_env("VDE_GPU_CLOUD_MIN_POINTS", "999999999");
const auto t3 = Clock::now();
const vde::mesh::PointCloud sor_cpu = cloud.statistical_outlier_removal(10, 1.0);
const auto t4 = Clock::now();
set_env("VDE_GPU_CLOUD_MIN_POINTS", "1");
const vde::mesh::PointCloud sor_gpu = cloud.statistical_outlier_removal(10, 1.0);
const auto t5 = Clock::now();
set_env("VDE_GPU_CLOUD_MIN_POINTS", nullptr);
const double sor_cpu_ms = elapsed_ms(t3, t4);
const double sor_gpu_ms = elapsed_ms(t4, t5);
const std::size_t sor_out_cpu = sor_cpu.size();
const std::size_t sor_out_gpu = sor_gpu.size();
const bool sor_counts_equal = sor_out_cpu == sor_out_gpu;
double min_dot = 1.0;
double sum_dot = 0.0;
std::size_t compared = 0;
for (std::size_t i = 0; i < cpu_normals.normals.size() &&
i < gpu_normals.normals.size(); ++i) {
const double dot = cpu_normals.normals[i].dot(gpu_normals.normals[i]);
min_dot = std::min(min_dot, dot);
sum_dot += dot;
++compared;
}
max_diff = 1.0 - min_dot;
const double mean_dot =
compared > 0 ? sum_dot / static_cast<double>(compared) : 0.0;
const double speedup = gpu_ms > 0.0 ? cpu_ms / gpu_ms : 0.0;
if (json) {
std::printf("{\"backend\":\"%s\",\"type\":\"%s\",\"gpu\":%s,"
"\"device_count\":%d,\"points\":%d,\"mode\":\"cloud\","
"\"k\":%d,\"source\":\"%s\",\"cloud_gpu_available\":%s,"
"\"cpu_ms\":%.3f,\"gpu_ms\":%.3f,\"speedup\":%.3f,"
"\"max_abs_diff\":%.3g,\"mean_normal_dot\":%.6f,"
"\"normals_compared\":%zu,\"sor_cpu_ms\":%.3f,"
"\"sor_gpu_ms\":%.3f,\"sor_speedup\":%.3f,"
"\"sor_out_cpu\":%zu,\"sor_out_gpu\":%zu,"
"\"sor_counts_equal\":%s}\n",
info.name.c_str(), type_name(info.type),
info.gpu ? "true" : "false", info.device_count, n, k,
file.empty() ? "synthetic" : "stl",
cloud_gpu_available ? "true" : "false", cpu_ms, gpu_ms,
speedup, max_diff, mean_dot, compared, sor_cpu_ms,
sor_gpu_ms, sor_gpu_ms > 0.0 ? sor_cpu_ms / sor_gpu_ms : 0.0,
sor_out_cpu, sor_out_gpu, sor_counts_equal ? "true" : "false");
} else {
std::printf("backend=%s type=%s gpu=%d device_count=%d points=%d mode=cloud k=%d\n",
info.name.c_str(), type_name(info.type), info.gpu ? 1 : 0,
info.device_count, n, k);
std::printf("device_vendor=%s device_name=%s cloud_gpu_available=%d\n",
info.device_vendor.c_str(), info.device_name.c_str(),
cloud_gpu_available ? 1 : 0);
std::printf("normal_estimation: cpu=%.3f ms gpu=%.3f ms speedup=%.3fx "
"min_normal_dot=%.6f mean_normal_dot=%.6f normals=%zu\n",
cpu_ms, gpu_ms, speedup, min_dot, mean_dot, compared);
std::printf("statistical_outlier_removal: cpu=%.3f ms gpu=%.3f ms "
"speedup=%.3fx out_cpu=%zu out_gpu=%zu counts_equal=%s\n",
sor_cpu_ms, sor_gpu_ms,
sor_gpu_ms > 0.0 ? sor_cpu_ms / sor_gpu_ms : 0.0,
sor_out_cpu, sor_out_gpu, sor_counts_equal ? "yes" : "no");
if (!cloud_gpu_available)
std::printf("note: OpenCL GPU 点云后处理不可用,两列均走 CPU(如实回退)\n");
}
if (!cloud_gpu_available) return 0;
return min_dot > 0.999 ? 0 : 1;
#else
std::fprintf(stderr, "cloud mode requires vde_mesh (build with VDE_ENABLE_mesh)\n");
return 2;
#endif
}
std::vector<double> xs(static_cast<size_t>(n)), ys(xs.size()), zs(xs.size());
std::mt19937 rng(42);
std::uniform_real_distribution<double> u(-10.0, 10.0);
for (int i = 0; i < n; ++i) { xs[i] = u(rng); ys[i] = u(rng); zs[i] = u(rng); }
if (mode == "sphere") {
std::vector<double> out_cpu(xs.size()), out_gpu(xs.size());
const auto t0 = Clock::now();
cpu.sdf_sphere_batch(xs.data(), ys.data(), zs.data(), out_cpu.data(), n,
0.25, -0.1, 0.3, 5.0);
const auto t1 = Clock::now();
gpu.sdf_sphere_batch(xs.data(), ys.data(), zs.data(), out_gpu.data(), n,
0.25, -0.1, 0.3, 5.0);
const auto t2 = Clock::now();
cpu_ms = elapsed_ms(t0, t1);
gpu_ms = elapsed_ms(t1, t2);
for (int i = 0; i < n; ++i)
max_diff = std::max(max_diff, std::abs(out_cpu[i] - out_gpu[i]));
} else {
// Triangle/triangle batch: per pair 6 vertices (a[9], b[9]).
std::vector<double> ta(static_cast<size_t>(n) * 9);
std::vector<double> tb(static_cast<size_t>(n) * 9);
std::uniform_real_distribution<double> c(-1.0, 1.0);
for (int i = 0; i < n; ++i)
for (int kk = 0; kk < 9; ++kk) {
ta[static_cast<size_t>(i) * 9 + static_cast<size_t>(kk)] = c(rng);
tb[static_cast<size_t>(i) * 9 + static_cast<size_t>(kk)] = c(rng);
}
std::vector<int> ia(static_cast<size_t>(n)), ic(static_cast<size_t>(n));
const auto t0 = Clock::now();
const size_t nc = cpu.triangle_intersect_batch(ta.data(), tb.data(),
static_cast<size_t>(n), ic.data());
const auto t1 = Clock::now();
const size_t ng = gpu.triangle_intersect_batch(ta.data(), tb.data(),
static_cast<size_t>(n), ia.data());
const auto t2 = Clock::now();
cpu_ms = elapsed_ms(t0, t1);
gpu_ms = elapsed_ms(t1, t2);
ia.resize(ng); ic.resize(nc);
std::sort(ia.begin(), ia.end());
std::sort(ic.begin(), ic.end());
max_diff = (ia == ic) ? 0.0 : 1.0;
}
const double speedup = gpu_ms > 0.0 ? cpu_ms / gpu_ms : 0.0;
if (json) {
std::printf("{\"backend\":\"%s\",\"type\":\"%s\",\"gpu\":%s,"
"\"device_count\":%d,\"points\":%d,\"mode\":\"%s\","
"\"cpu_ms\":%.3f,\"gpu_ms\":%.3f,\"speedup\":%.3f,"
"\"max_abs_diff\":%.3g}\n",
info.name.c_str(), type_name(info.type),
info.gpu ? "true" : "false", info.device_count, n, mode.c_str(),
cpu_ms, gpu_ms, speedup, max_diff);
} else {
std::printf("backend=%s type=%s gpu=%d device_count=%d points=%d mode=%s\n",
info.name.c_str(), type_name(info.type), info.gpu ? 1 : 0,
info.device_count, n, mode.c_str());
std::printf("device_vendor=%s device_name=%s\n",
info.device_vendor.c_str(), info.device_name.c_str());
std::printf("%s: cpu=%.3f ms default=%.3f ms speedup=%.3fx "
"max_abs_diff=%.3g\n",
mode.c_str(), cpu_ms, gpu_ms, speedup, max_diff);
}
return max_diff < 1e-9 ? 0 : 1;
}