diff --git a/image_analysis/scale_merge/RotationScaleMerge.cpp b/image_analysis/scale_merge/RotationScaleMerge.cpp index b9fd129cb..6cf694b13 100644 --- a/image_analysis/scale_merge/RotationScaleMerge.cpp +++ b/image_analysis/scale_merge/RotationScaleMerge.cpp @@ -691,12 +691,6 @@ std::unique_ptr RotationScaleMerge::CloneIngest(int device, c->logger = &log; c->gpu_device = device; c->write_back_per_frame_scale = false; - // Of the ingest arrays, IngestDevice reads only this one (StageObservations); the rest served - // IngestHost alone. - if (ingest_partiality) { - c->ingest_partiality.reset(new float[n_partials_obs]); - std::copy_n(ingest_partiality.get(), n_partials_obs, c->ingest_partiality.get()); - } #ifdef JFJOCH_USE_CUDA if (gpu_) c->gpu_ = std::make_unique(device); @@ -704,13 +698,16 @@ std::unique_ptr RotationScaleMerge::CloneIngest(int device, return c; } -void RotationScaleMerge::IngestDevice() { +void RotationScaleMerge::IngestDevice(const RotationScaleMerge *observations_from) { const ScopedGpuPin pin(gpu_device); // see IngestHost TimingMark mark(*logger, "rsm ingest (device)"); #ifdef JFJOCH_USE_CUDA - if (resident_ingest) + if (gpu_active_ && observations_from) { + gpu_->SetPartialsLayout(n_partials_obs, n_frames, frame_start.data(), frame_count.data()); + gpu_->CopyObservationsFrom(*observations_from->gpu_); + } else if (resident_ingest) StageObservations(); - if (gpu_active_ && !resident_ingest) { + if (gpu_active_ && !resident_ingest && !observations_from) { const int n = static_cast(partials.size()); gpu_->SetPartialsLayout(n, n_frames, frame_start.data(), frame_count.data()); // Staged in bounded slices: fourteen full-length arrays at once are more than half of the diff --git a/image_analysis/scale_merge/RotationScaleMerge.h b/image_analysis/scale_merge/RotationScaleMerge.h index 95da49c97..43a27a527 100644 --- a/image_analysis/scale_merge/RotationScaleMerge.h +++ b/image_analysis/scale_merge/RotationScaleMerge.h @@ -122,7 +122,10 @@ public: // builds every host array, the device half allocates the GPU's arrays and fills them. A caller can // run the host half beside other work and the device half once the GPU has room. void IngestHost(); - void IngestDevice(); + // With `observations_from` - the engine this one was copied from (CloneIngest), its own + // IngestDevice done - the observations are copied from that engine's card arrays rather than + // staged from the reflections again. + void IngestDevice(const RotationScaleMerge *observations_from = nullptr); // Scale (per-frame G) -> smooth G -> 3D combine -> scale fulls -> merge -> error model -> statistics // for the space group currently set on the experiment, reusing the ingested buffers. @@ -157,8 +160,8 @@ public: void WriteBackPerFrameScale() { FinalizePerFrameScale(last_cc, last_cc_n, last_frame_scaled); } // A second engine on this one's ingest, on `device` and logging to `log`, for a merge to run beside - // this engine's: the host half of the ingest is copied, and the copy's IngestDevice() is still to - // be called. Call between IngestHost() and IngestDevice(). No merge depends on the merges run + // this engine's: the host half of the ingest is copied, and the copy's IngestDevice(this) is still + // to be called, after this engine's own. Call between IngestHost() and IngestDevice(). No merge depends on the merges run // before it (see RunArm), so a merge on the copy gives what the same merge gives on this engine. // The copy does not write its per-frame scales back (WriteBackPerFrameScale does). [[nodiscard]] std::unique_ptr CloneIngest(int device, Logger &log) const; @@ -178,6 +181,7 @@ public: // The GPU the engine runs on (device 0 by default). Set before Ingest(). The device only says where // the merge runs: on cards of one model the result is the same on any of them. void SetGpuDevice(int device) { gpu_device = device; } + [[nodiscard]] int GetGpuDevice() const { return gpu_device; } // Whether Run() also hands back the merge's fulls unmerged (Result::scaled_fulls). Off by default: // it is a copy as long as the fulls, wanted only by a caller that writes them. diff --git a/image_analysis/scale_merge/RotationScaleMergeGPU.cu b/image_analysis/scale_merge/RotationScaleMergeGPU.cu index b6aa638a5..02ae8607e 100644 --- a/image_analysis/scale_merge/RotationScaleMergeGPU.cu +++ b/image_analysis/scale_merge/RotationScaleMergeGPU.cu @@ -1367,6 +1367,22 @@ void RotationScaleMergeGPU::SetObsField(ObsField f, int offset, int count, const } } +void RotationScaleMergeGPU::CopyObservationsFrom(const RotationScaleMergeGPU &from) { + DeviceGuard guard(impl_->device, impl_->available); + auto &d = *impl_; + const auto &f = *from.impl_; + const auto copy = [&](auto &dst, const auto &src) { + CudaCheck(cudaMemcpyPeerAsync(dst.get(), d.device, src.get(), f.device, + size_t(d.n_obs) * sizeof(*src.get()), d.s()), "copy observations"); + }; + copy(d.I, f.I); copy(d.sigma, f.sigma); copy(d.prescaling_corr, f.prescaling_corr); + copy(d.partiality, f.partiality); copy(d.zeta, f.zeta); copy(d.corr, f.corr); + copy(d.bkg, f.bkg); copy(d.var_bkg, f.var_bkg); copy(d.image_number, f.image_number); + copy(d.d_obs, f.d_obs); copy(d.px_obs, f.px_obs); copy(d.py_obs, f.py_obs); + copy(d.frame, f.frame); copy(d.on_ice, f.on_ice); copy(d.clipped, f.clipped); + CudaCheck(cudaStreamSynchronize(d.s()), "copy observations"); +} + void RotationScaleMergeGPU::SetObsFrame(int offset, int count, const int32_t *frame) { DeviceGuard guard(impl_->device, impl_->available); UploadChunk(impl_->frame, offset, count, frame, impl_->s()); diff --git a/image_analysis/scale_merge/RotationScaleMergeGPU.h b/image_analysis/scale_merge/RotationScaleMergeGPU.h index 0d4111346..330466d80 100644 --- a/image_analysis/scale_merge/RotationScaleMergeGPU.h +++ b/image_analysis/scale_merge/RotationScaleMergeGPU.h @@ -49,6 +49,11 @@ public: void SetObsOnIce(int offset, int count, const uint8_t *on_ice); void SetObsClipped(int offset, int count, const uint8_t *clipped); + // Every per-observation field the uploads above set, copied from `from` (which has them all, on + // its own device or this one) instead of being staged again from the host. Call after + // SetPartialsLayout with the same layout, before anything changes the fields on `from`. + void CopyObservationsFrom(const RotationScaleMergeGPU &from); + // Per space group: the dense ASU-group id per obs, and a group-ordered permutation of the obs whose // group >= 0, with its CSR - so each group's observations are a contiguous, fixed-order segment for // the reduction. Built on the device from the raw-hkl runs uploaded by SetRawRuns: rawrun_group is diff --git a/rugnux/RugnuxScaleMerge.cpp b/rugnux/RugnuxScaleMerge.cpp index 90b14acf2..1da9881b1 100644 --- a/rugnux/RugnuxScaleMerge.cpp +++ b/rugnux/RugnuxScaleMerge.cpp @@ -1069,16 +1069,20 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { rsm_all->SetSearchMinZeta(0.0); rsm_all->SetExportScaledFulls(false); // as scale_and_merge sets them for a search merge rsm_all->SetFrenchWilson(false); - all_arm_ahead = std::async(std::launch::async, [&rsm_all, &all_arm_log, device] { - pin_gpu(device); // the card and its NUMA node, as the guard's merge has them - TimingMark mark(all_arm_log, "merge P1, all observations"); - rsm_all->IngestDevice(); - return rsm_all->Run(/*for_search=*/true, /*full_stats=*/true, - /*measure_cc_before_corrections=*/false); - }); } } rsm->IngestDevice(); + if (rsm_all) { + // Its observations are copied from this engine's card arrays, not staged from the + // reflections a second time. + rsm_all->IngestDevice(&*rsm); + all_arm_ahead = std::async(std::launch::async, [&rsm_all, &all_arm_log] { + pin_gpu(rsm_all->GetGpuDevice()); // the card and its NUMA node, as the guard's merge has them + TimingMark mark(all_arm_log, "merge P1, all observations"); + return rsm_all->Run(/*for_search=*/true, /*full_stats=*/true, + /*measure_cc_before_corrections=*/false); + }); + } } // The geometry pre-pass reads the per-image reflections exactly twice more: they were just