From f724bd1117f8561f51ec7a68b774b6efb45b2600 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Mon, 26 Oct 2026 20:37:07 +0100 Subject: [PATCH] ModelScaleGPU: the host polls for a scale fit's launches instead of sleeping on them A Levenberg-Marquardt step is a few short launches the host then reads the sums of, a hundred or so steps per fit, and the process has every synchronisation sleep until the device raises the wake-up (set_gpu_blocking_sync): for these launches the wake-up costs more than the work. The fit's waits poll the stream instead. Outputs are byte-identical; the refinement's scale refit counter drops 20-27% and the molecular replacement's scale fits gain a little on the demo-sized sets. Summing the block partials on the host instead of in a kernel was tried alongside and dropped: the coarse solvent grid of 88 fits made it a megabyte of read-back per step, slower than the kernel. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_015CeEGgpKdfkCTJb3YNMJcu --- image_analysis/structure_refinement/ModelScaleGPU.cu | 12 +++++++++--- image_analysis/structure_refinement/ModelScaleGPU.h | 4 ++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/image_analysis/structure_refinement/ModelScaleGPU.cu b/image_analysis/structure_refinement/ModelScaleGPU.cu index d16649fcf..bfa19dce9 100644 --- a/image_analysis/structure_refinement/ModelScaleGPU.cu +++ b/image_analysis/structure_refinement/ModelScaleGPU.cu @@ -457,10 +457,16 @@ const double *ModelScaleGPU::Reduce(int nfits) { sum_blocks<<>>(partial_, sums_); cuda_err(cudaGetLastError()); cuda_err(cudaMemcpyAsync(host_sums_, sums_, nfits * SLOTS * sizeof(double), cudaMemcpyDeviceToHost, stream_)); - cuda_err(cudaStreamSynchronize(stream_)); + Wait(); return host_sums_; } +void ModelScaleGPU::Wait() { + cudaError_t err; + while ((err = cudaStreamQuery(stream_)) == cudaErrorNotReady) {} + cuda_err(err); +} + // fit_isotropic_b_approximately() then fit_parameters() for every fit in `fits`, which start from the // k_overall and b_star they hold, and are left holding the answer. void ModelScaleGPU::FitBatch(const float2 *d_fcmol, const float2 *d_fmask, std::vector &fits) { @@ -567,14 +573,14 @@ void ModelScaleGPU::RobustBatch(std::vector &fits) { residual_median<<(nfits), 1024, 0, stream_>>>(residual_, n_, fits_, median_); cuda_err(cudaGetLastError()); cuda_err(cudaMemcpyAsync(host_median_, median_, nfits * sizeof(double), cudaMemcpyDeviceToHost, stream_)); - cuda_err(cudaStreamSynchronize(stream_)); + Wait(); for (size_t i = 0; i < nfits; i++) fits[i].c = host_median_[i]; UploadFits(fits); } robust_weights<<>>(residual_, n_, fits_, weight_); cuda_err(cudaGetLastError()); - cuda_err(cudaStreamSynchronize(stream_)); // host_fits_ is written again below + Wait(); // host_fits_ is written again below for (ModelScaleFitState &f : fits) f.weighted = f.c > 0; LevMarBatch(fits); diff --git a/image_analysis/structure_refinement/ModelScaleGPU.h b/image_analysis/structure_refinement/ModelScaleGPU.h index c68cf2b3d..178508864 100644 --- a/image_analysis/structure_refinement/ModelScaleGPU.h +++ b/image_analysis/structure_refinement/ModelScaleGPU.h @@ -78,6 +78,10 @@ private: // The sums of the first `nfits` uploaded fits over all points, one row of SLOTS doubles per fit, on // the host. const double *Reduce(int nfits); + // The stream's queued work done, polled for: a fit's launches are short and the host waits for every one + // of them, so sleeping until the device raises the wake-up (set_gpu_blocking_sync) would cost more than + // the work itself. + void Wait(); const double *Sums(const std::vector &fits); void FitBatch(const float2 *d_fcmol, const float2 *d_fmask, std::vector &fits); // gemmi's Levenberg-Marquardt for every fit in `fits` from the parameters it holds.