Skip to content

Commit a37c1c3

Browse files
MVS: Reuse sampled current costs (GPU perf)
## Summary Reuse exact current-source costs already loaded by the probability pass instead of loading them again during the ordered candidate fold. - 1536 px: 24.222 -> 23.720 s, 2.1% lower wall time (N=2 per version). - 3072 px: 89.252 -> 88.051 s, 1.3% lower wall time (N=2 per version). - All 44 depth/normal maps remain byte-identical in every call. - All 29 GPU tests pass; focused memory, synchronization and race checks pass. ## Measurements Incremental base: 1344b32, including automatic reference pairing and bulk float-map writes. Fountain P11: 11 images, originally 3072x2048 px; tested at 1536x1024 and 3072x2048. Complete default photometric + geometric PatchMatch pipeline, 12 CPU input workers and one CUDA worker. Fresh outputs, tiny default-path prefix then ABBA, N=2 per version at each resolution. Ryzen 9 3900X / RTX 3080 Ti, GCC 12.4 / CUDA 13.2, WSL2, sm_86. Arithmetic means; memory columns are maximum observed per arm: | Resolution | Wall (s) | CPU (s) | Instructions (G) | Peak RSS (MiB) | GPU peak (MiB) | | --- | --- | --- | --- | --- | --- | | 1536 | 24.222 -> 23.720 | 25.789 -> 25.265 | 99.344 -> 96.990 | 1200.4 -> 1182.0 | 2697.4 -> 2701.4 | | 3072 | 89.252 -> 88.051 | 98.582 -> 96.561 | 378.469 -> 367.217 | 2339.4 -> 2338.5 | 3327.4 -> 3357.4 | Wall ranges are 24.196-24.248 / 23.694-23.747 s at 1536 and 88.873-89.632 / 88.028-88.075 s at 3072. User cycles fall 94.721 -> 92.566 G / 358.127 -> 346.591 G; cache misses remain roughly flat, 86.681 -> 86.838 M / 163.882 -> 163.872 M. Eight same-pass CPU counters report 100% running fractions. perf attaches to the native PID with inheritance; startup gaps and full thread coverage are not proved. CPU includes driver/wait work. GPU memory is a one-second whole-device sample peak, not process allocation accounting; no memory-saving claim. Clocks use dynamic boost, so the small N2 gains describe these matched runs. A separate first-3072-photo-Sweep diagnostic records 14.951 -> 15.182 G executed warp instructions (+1.5%): the shared cache adds bookkeeping while avoiding repeated global reads. Both kernels retain 80 registers and exact mutable state. Profiler timings are excluded from the wall-time figures. ## Changes and validation The cooperative eight-column CUDA tile retains up to 16 source costs in 512 bytes of shared memory per block. Existing barriers publish/reuse it; larger source IDs keep the original global load. Arithmetic, draw order, RNG, defaults and noncooperative/HIP paths remain unchanged. The compiled default photo/geometric kernels retain 80 registers, zero spills and six resident blocks per SM; the 1536 controller still admits five reference pairs per phase in both versions. One CUDA unit and the GPU test unit rebuilt incrementally, with no configure or dependency changes. Both unchanged and patched kernels pass all 29 tests without skips. The new fixture compares identical-source geometry at 9 and 33 source layers, covering the shared cache and global fallback. Focused Compute Sanitizer memcheck and synccheck report zero errors on both boundary tests; racecheck reports zero hazards on the new 9/33-source fixture. Initial racecheck attempts exhausted a 150-second cap; the completed focused rerun used a 225-second cap. Every full11 call has 44 finite, correctly sized, byte-identical maps; the tiny full11 gate also passes. Both changed files pass clang-format 23.1.3 and git diff --check. HIP, other GPU architectures, a new full C++/Python suite and new ground-truth or rendered-quality comparisons were not tested. Exact map equality is the quality evidence here. The earlier parallel result-fold prototype was flat and is excluded; this patch only reuses already loaded current costs. AI assistance disclosure: totally vibe coded. AI tools assisted investigation, implementation and review; reported checks cover the final production change.
1 parent 5c05c49 commit a37c1c3

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

‎src/colmap/mvs/patch_match_cuda.cu‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,14 @@ __global__ void SweepFromTopToBottom(
10751075
cooperative_geometric_costs[kSampleWindowSize]
10761076
[kCooperative && kGeomConsistencyTerm ? 3 : 1]
10771077
[kBlockWidth];
1078+
#ifndef COLMAP_HIP_ENABLED
1079+
// The probability pass already loads current cost for each source. The
1080+
// default CUDA tile can reuse that exact float during the ordered draw fold.
1081+
__shared__ float
1082+
source_current_cost[kCooperative && kBlockWidth == 8 ? kSampleWindowSize
1083+
: 1]
1084+
[kCooperative && kBlockWidth == 8 ? kBlockWidth : 1];
1085+
#endif
10781086

10791087
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ == 610 && !defined(__HIPCC__)
10801088
// Checkpoint construction bounds both orientations by INT_MAX. Keep
@@ -1311,6 +1319,13 @@ __global__ void SweepFromTopToBottom(
13111319
for (int image_idx = first_source; image_idx < cost_map.GetDepth();
13121320
image_idx += source_step) {
13131321
const float cost = cost_map.Get(row, col, image_idx);
1322+
#ifndef COLMAP_HIP_ENABLED
1323+
if constexpr (kCooperative && kBlockWidth == 8) {
1324+
if (image_idx < kSampleWindowSize) {
1325+
source_current_cost[image_idx][threadIdx.x] = cost;
1326+
}
1327+
}
1328+
#endif
13141329
const float alpha = likelihood_computer.ComputeForwardMessage(
13151330
cost, forward_message[image_idx * source_stride]);
13161331
const float beta =
@@ -1562,7 +1577,16 @@ __global__ void SweepFromTopToBottom(
15621577
if (source < 0) continue;
15631578
// Reuse identical evaluations, but retain every original sample
15641579
// add.
1565-
costs[0] += cost_map.Get(row, col, source);
1580+
#ifndef COLMAP_HIP_ENABLED
1581+
if constexpr (kBlockWidth == 8) {
1582+
costs[0] += source < kSampleWindowSize
1583+
? source_current_cost[source][threadIdx.x]
1584+
: cost_map.Get(row, col, source);
1585+
} else
1586+
#endif
1587+
{
1588+
costs[0] += cost_map.Get(row, col, source);
1589+
}
15661590
if constexpr (kGeomConsistencyTerm) {
15671591
for (int i = 0; i < 3; ++i) {
15681592
geometric_costs[i] =

‎src/colmap/mvs/patch_match_cuda_test.cc‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,44 @@ TEST_F(CudaTest, SampleWindowBoundariesPreserveOutputs) {
596596
}
597597
}
598598

599+
TEST_F(CudaTest, DuplicatedSourcesPreservePhotoGeometryAcrossCacheBoundary) {
600+
// The same source bitmap, calibration, and pose occupy every source layer.
601+
// Sampling can choose different IDs as the CDF length changes, but each
602+
// selected source contributes the same photo cost. Compare geometry only:
603+
// selection maps necessarily have different source dimensions.
604+
const Inputs seed = MakeInputs(0, 1);
605+
const auto duplicate_source = [&seed](const int num_sources) {
606+
Inputs inputs;
607+
inputs.images.push_back(seed.images[0]);
608+
inputs.depths.push_back(seed.depths[0]);
609+
inputs.normals.push_back(seed.normals[0]);
610+
for (int i = 0; i < num_sources; ++i) {
611+
inputs.images.push_back(seed.images[1]);
612+
inputs.depths.push_back(seed.depths[1]);
613+
inputs.normals.push_back(seed.normals[1]);
614+
}
615+
return inputs;
616+
};
617+
618+
PatchMatchOptions options = MakeOptions(false, false, false);
619+
options.window_radius = 5;
620+
options.window_step = 1;
621+
options.num_samples = 15;
622+
options.num_iterations = 5;
623+
options.sigma_spatial = options.window_radius;
624+
625+
Inputs nine_sources = duplicate_source(9);
626+
Inputs thirty_three_sources = duplicate_source(33);
627+
const Result nine = RunPatchMatch(&nine_sources, options);
628+
const Result thirty_three = RunPatchMatch(&thirty_three_sources, options);
629+
ExpectNonempty(nine, nine_sources, false);
630+
ExpectNonempty(thirty_three, thirty_three_sources, false);
631+
EXPECT_EQ(nine.selection.GetDepth(), 9);
632+
EXPECT_EQ(thirty_three.selection.GetDepth(), 33);
633+
ExpectExactlyEqual(nine.depths, thirty_three.depths);
634+
ExpectExactlyEqual(nine.normals, thirty_three.normals);
635+
}
636+
599637
TEST_P(PatchMatchCudaTest, CheckpointHmmPreservesAlgorithmOutputs) {
600638
const auto [geometry, filter] = GetParam();
601639
for (const int num_sources : {1, 9, 17}) {

0 commit comments

Comments
 (0)