Files
Jungfraujoch/image_analysis/image_preprocessing/ImagePreprocessorBufferGPU.cu
T
leonarski_fandFilip Leonarski 19b135186d
Build Packages / Create release (push) Successful in 7s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 4m56s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 9m10s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 13m51s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 16m56s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 20m59s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 21m25s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 21m17s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 14m14s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 25m59s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 25m48s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 15m52s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 19m33s
Build Packages / Generate python client (push) Successful in 29s
Build Packages / Build documentation (push) Successful in 1m19s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 20m49s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 25m31s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 25m24s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 26m5s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 22m19s
Build Packages / Unit tests (push) Failing after 2h28m24s
Build Packages / Validation Battery (push) Successful in 40m33s
v1.0.0-rc.175 (#85)
* Rugnux: Processes a collection live while jfjoch_broker writes it (`rugnux <master.h5> <broker URL>`).
* Rugnux: Processing starts on the first images and confirms its choices once all are in, so analysis can begin during collection.
* Rugnux: Faster processing, especially on servers with several GPUs and CPU sockets and on computers without a GPU (`--no-gpu`, macOS).
* Rugnux: Uses much less GPU memory, so large data sets stay on the GPU of smaller cards, and a data set too large for the card is processed on the CPU instead.
* Rugnux: On macOS, Bragg integration runs on the Apple GPU.
* Rugnux: Better handling of twinning and pseudo-symmetry, with the twin hypothesis written as `<prefix>_twin_<SG>.mtz`, and a wrong `--model` no longer confirms the data or decides the hand.
* Rugnux: `--model` accepts predicted (AlphaFold) models and falls back to molecular replacement when the model is not placed as a rigid body (experimental `--mr` forces it), settling space group, hand and twinning and writing a different answer as `<prefix>_mr.mtz`.
* Rugnux: Molecular replacement is faster and settles the space group in more cases with several copies in the asymmetric unit.
* Rugnux: Deposition-ready mmCIF, a richer report, and numbered steps on the terminal with the full log in `<prefix>_log.txt`; `_P1.mtz`, `--estimate-beam-center` and `--beam-center-search` are removed.
* Rugnux: The beam-stop shadow is found correctly on tilted detectors.
* Rugnux: Stills files that record a stationary goniometer axis get the default stills geometry refinement.
* jfjoch_viewer: "Analyze dataset" uses the same defaults as a bare `rugnux <file>`, and shows the results of processed CBF, marCCD and SMV sweeps.
* jfjoch_viewer: The Files panel refreshes on F5 and every few seconds, and the plot type and other selections stay put during live collection.
* jfjoch_writer: Memory stays bounded and old data files are closed efficiently on long runs, any failure ends the run in an error state the broker is told about, and the master file appears only after its data files; the ZeroMQ pusher logs when a stalled writer makes it drop images.
* Fixes: The TCP image pusher no longer blocks the broker's receiver, GPU out-of-memory failures stop with a clear message, and the XDS plugin masks module and chip edges.

---------

Co-authored-by: Filip Leonarski <jungfrau@filips-mac-mini.home>
Reviewed-on: #85
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-10-16 15:34:43 +02:00

109 lines
4.8 KiB
Plaintext

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "ImagePreprocessorBufferGPU.h"
#include <algorithm>
#include <climits>
static inline void cuda_err(cudaError_t val) {
if (val != cudaSuccess)
throw JFJochException(JFJochExceptionCategory::GPUCUDAError, cudaGetErrorString(val));
}
__global__ void gather_kernel(const int32_t *__restrict__ image,
const uint32_t *__restrict__ npixel,
int32_t *__restrict__ values,
int count) {
for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < count; i += blockDim.x * gridDim.x)
values[i] = image[npixel[i]];
}
__global__ void gather_tiles_kernel(const int32_t *__restrict__ image, int width, int height,
const int32_t *__restrict__ centre, int half,
int32_t *__restrict__ tiles, size_t count) {
const int side = 2 * half + 1;
const size_t area = static_cast<size_t>(side) * side;
for (size_t i = blockIdx.x * blockDim.x + threadIdx.x; i < count; i += static_cast<size_t>(blockDim.x) * gridDim.x) {
const size_t tile = i / area;
const int p = static_cast<int>(i - tile * area);
const int px = centre[2 * tile] - half + p % side;
const int py = centre[2 * tile + 1] - half + p / side;
tiles[i] = (px < 0 || py < 0 || px >= width || py >= height)
? INT32_MIN : image[static_cast<size_t>(py) * width + px];
}
}
ImagePreprocessorBufferGPU::ImagePreprocessorBufferGPU(size_t npixel, bool host_mirror)
: ImagePreprocessorBuffer(npixel, host_mirror),
gpu_image(npixel),
// A no-op when the mirror was not allocated: CudaRegisteredVector skips an empty vector.
buffer_reg(buffer),
max_gather(StrongPixelLimit(npixel)),
gpu_gather_index(max_gather),
gpu_gather_value(max_gather) {
}
int32_t *ImagePreprocessorBufferGPU::getGPUBuffer() {
return gpu_image;
}
const int32_t *ImagePreprocessorBufferGPU::getGPUBuffer() const {
return gpu_image;
}
void ImagePreprocessorBufferGPU::Gather(const std::vector<uint32_t> &npixel, std::vector<int32_t> &values) const {
values.resize(npixel.size());
if (npixel.empty())
return;
const int count = static_cast<int>(npixel.size());
cudaMemcpyAsync(gpu_gather_index.get(), npixel.data(), count * sizeof(uint32_t),
cudaMemcpyHostToDevice, gather_stream);
gather_kernel<<<(count + 255) / 256, 256, 0, gather_stream>>>(
gpu_image.get(), gpu_gather_index.get(), gpu_gather_value.get(), count);
cuda_err(cudaGetLastError());
cudaMemcpyAsync(values.data(), gpu_gather_value.get(), count * sizeof(int32_t),
cudaMemcpyDeviceToHost, gather_stream);
cudaStreamSynchronize(gather_stream);
}
const int32_t *ImagePreprocessorBufferGPU::GatherTiles(const std::vector<int32_t> &x, const std::vector<int32_t> &y,
int width, int half) {
const size_t side = 2 * half + 1;
const size_t count = x.size() * side * side;
if (count == 0)
return nullptr;
if (count > tile_capacity) {
tile_capacity = std::max(count, 2 * tile_capacity);
gpu_tiles = CudaDevicePtr<int32_t>(tile_capacity);
host_tiles.resize(tile_capacity);
}
if (2 * x.size() > tile_centre.size()) {
tile_centre.resize(std::max(2 * x.size(), 2 * tile_centre.size()));
gpu_tile_centre = CudaDevicePtr<int32_t>(tile_centre.size());
}
for (size_t i = 0; i < x.size(); i++) {
tile_centre[2 * i] = x[i];
tile_centre[2 * i + 1] = y[i];
}
cuda_err(cudaMemcpyAsync(gpu_tile_centre.get(), tile_centre.data(), 2 * x.size() * sizeof(int32_t),
cudaMemcpyHostToDevice, gather_stream));
const int blocks = static_cast<int>(std::min<size_t>((count + 255) / 256, 4096));
gather_tiles_kernel<<<blocks, 256, 0, gather_stream>>>(gpu_image.get(), width, static_cast<int>(npixels / width),
gpu_tile_centre.get(), half, gpu_tiles.get(), count);
cuda_err(cudaGetLastError());
cuda_err(cudaMemcpyAsync(host_tiles.data(), gpu_tiles.get(), count * sizeof(int32_t),
cudaMemcpyDeviceToHost, gather_stream));
cuda_err(cudaStreamSynchronize(gather_stream));
return host_tiles.data();
}
const int32_t *ImagePreprocessorBufferGPU::HostImage() {
buffer.resize(npixels);
cuda_err(cudaMemcpyAsync(buffer.data(), gpu_image.get(), npixels * sizeof(int32_t),
cudaMemcpyDeviceToHost, gather_stream));
cuda_err(cudaStreamSynchronize(gather_stream));
return buffer.data();
}