Build Packages / Create release (push) Successful in 24s
Build Packages / build:viewer:macos-arm64:nocuda (push) Successful in 3m29s
Build Packages / build:rugnux:macos-arm64:nocuda (push) Successful in 2m43s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 8m27s
Build Packages / build:rugnux:linux-x86_64:cuda (push) Successful in 9m53s
Build Packages / build:viewer:linux-x86_64:nocuda (push) Successful in 9m58s
Build Packages / build:viewer:linux-x86_64:cuda (push) Successful in 11m22s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 13m39s
Build Packages / build:viewer:windows-x86_64:nocuda (push) Successful in 18m37s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 16m32s
Build Packages / build:viewer:windows-x86_64:cuda (push) Successful in 24m11s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 25m30s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 19m3s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 20m23s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 19m41s
Build Packages / Generate python client (push) Successful in 50s
Build Packages / Build documentation (push) Successful in 1m16s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 21m0s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 18m38s
Build Packages / build:rugnux:windows-x86_64:cuda (push) Successful in 14m33s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 17m55s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 20m50s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 18m38s
Build Packages / Unit tests (push) Successful in 1h46m14s
* jfjoch_broker: Optional per-dataset authentication - statistics, images and plots can require a bearer token, which jfjoch_viewer supports. * jfjoch_viewer: Dark mode and a theme-matched colour scheme, a magnifier panel, and simpler contrast and background controls. * Rugnux: Multiple performance improvements on GPU and CPU (CPU-only processing up to 40% faster, faster image decoding on ARM), with unchanged results. * Rugnux: `--model` rigid-body refinement runs on the GPU, and the model-validation check is faster and more reliable. * Rugnux: Improved scaling and merging - error model, outlier rejection, absorption correction and French-Wilson amplitudes now agree more closely with XDS and ctruncate. * Rugnux: Improved integration - radial background on powder and ice rings, crowded rotation data keep their reflections, and CPU-only builds integrate large unit cells as GPU builds do. * Rugnux: More robust detector geometry - measured beam centre, X-ray bandwidth and goniometer rate, and geometry refinement accepted only on significant evidence. * Rugnux: Merged files are written in the standard setting, or in the setting of a reference MTZ, structure-factor mmCIF or model, with its free-R flags. * Rugnux: Richer report - ice and powder rings, further lattices, superstructure candidates and mosaicity, with warnings worded as prompts to check. * Rugnux: Clear error messages when a data set needs more GPU or host memory than is available. Reviewed-on: #83 Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
186 lines
7.3 KiB
C++
186 lines
7.3 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include "HDF5ImageSource.h"
|
|
#include "../common/JFJochException.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#else
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
// Positional reads: pread() on POSIX, ReadFile() with an OVERLAPPED offset on Windows. Both take the
|
|
// offset as an argument instead of moving a shared file position, which is what lets every worker
|
|
// thread read through one handle at the same time.
|
|
HDF5ImageSource::RawFile::RawFile(const std::string &path) {
|
|
#ifdef _WIN32
|
|
HANDLE h = CreateFileA(path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
|
handle_ = (h == INVALID_HANDLE_VALUE) ? -1 : reinterpret_cast<intptr_t>(h);
|
|
#else
|
|
handle_ = ::open(path.c_str(), O_RDONLY);
|
|
#endif
|
|
}
|
|
|
|
HDF5ImageSource::RawFile::~RawFile() {
|
|
if (handle_ == -1)
|
|
return;
|
|
#ifdef _WIN32
|
|
CloseHandle(reinterpret_cast<HANDLE>(handle_));
|
|
#else
|
|
::close(static_cast<int>(handle_));
|
|
#endif
|
|
}
|
|
|
|
void HDF5ImageSource::RawFile::ReadAt(void *dst, size_t size, uint64_t address) const {
|
|
auto *out = static_cast<uint8_t *>(dst);
|
|
size_t done = 0;
|
|
while (done < size) {
|
|
#ifdef _WIN32
|
|
OVERLAPPED ov{};
|
|
ov.Offset = static_cast<DWORD>((address + done) & 0xFFFFFFFFULL);
|
|
ov.OffsetHigh = static_cast<DWORD>((address + done) >> 32);
|
|
DWORD got = 0;
|
|
const bool ok = ReadFile(reinterpret_cast<HANDLE>(handle_), out + done,
|
|
static_cast<DWORD>(size - done), &got, &ov);
|
|
const long long n = ok ? static_cast<long long>(got) : -1;
|
|
#else
|
|
const long long n = ::pread(static_cast<int>(handle_), out + done, size - done, address + done);
|
|
#endif
|
|
if (n <= 0)
|
|
throw JFJochException(JFJochExceptionCategory::HDF5, "Error reading image chunk from file");
|
|
done += static_cast<size_t>(n);
|
|
}
|
|
}
|
|
|
|
void HDF5ImageSource::Configure(HDF5ImageLocator::Layout layout) {
|
|
dataset_cache_.clear();
|
|
locator_.Configure(std::move(layout));
|
|
}
|
|
|
|
void HDF5ImageSource::Clear() {
|
|
dataset_cache_.clear();
|
|
locator_.Clear();
|
|
}
|
|
|
|
HDF5ImageLocator::Location HDF5ImageSource::Resolve(int64_t global) const {
|
|
return locator_.Resolve(global);
|
|
}
|
|
|
|
StoredPixelFormat HDF5ImageSource::GetStoredPixelFormat() const {
|
|
auto loc = locator_.Resolve(0);
|
|
HDF5DataSet dataset(*loc.file, loc.dataset);
|
|
HDF5DataType datatype(dataset);
|
|
return {static_cast<int64_t>(datatype.GetElemSize()) * 8, datatype.IsSigned()};
|
|
}
|
|
|
|
std::vector<HDF5DataSourceMessage> HDF5ImageSource::GetSourceMapping(uint64_t first_image,
|
|
std::optional<uint64_t> image_count,
|
|
uint64_t total_images,
|
|
uint64_t stride) const {
|
|
return locator_.GetSourceMapping(first_image, image_count, total_images, stride);
|
|
}
|
|
|
|
const HDF5ImageSource::OpenDataset &
|
|
HDF5ImageSource::GetDataset(const HDF5ImageLocator::Location &loc) const {
|
|
auto key = std::make_pair(loc.file.get(), loc.dataset);
|
|
if (auto it = dataset_cache_.find(key); it != dataset_cache_.end())
|
|
return it->second;
|
|
|
|
OpenDataset entry;
|
|
entry.file = loc.file;
|
|
entry.dataset = std::make_unique<HDF5DataSet>(*loc.file, loc.dataset);
|
|
|
|
HDF5DataSpace dataspace(*entry.dataset);
|
|
HDF5DataType datatype(*entry.dataset);
|
|
HDF5Dcpl dcpl(*entry.dataset);
|
|
|
|
const auto rank = dataspace.GetNumOfDimensions();
|
|
if (rank != 3 && rank != 4)
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
loc.dataset + " dataset must be 3D or 4D");
|
|
entry.multichannel = (rank == 4);
|
|
|
|
if (datatype.IsFloat())
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Float datasets not supported at this time");
|
|
|
|
auto dim = dataspace.GetDimensions();
|
|
entry.height = dim[rank - 2];
|
|
entry.width = dim[rank - 1];
|
|
entry.mode = CalcImageMode(datatype.GetElemSize(), datatype.IsFloat(), datatype.IsSigned());
|
|
|
|
// One chunk per image: [1, h, w], or [1, 1, h, w] for a multichannel dataset
|
|
auto chunk_size = dcpl.GetChunking();
|
|
entry.direct_chunk = (chunk_size.size() == rank)
|
|
&& std::all_of(chunk_size.begin(), chunk_size.end() - 2, [](hsize_t c) { return c == 1; })
|
|
&& (chunk_size[rank - 2] == entry.height) && (chunk_size[rank - 1] == entry.width);
|
|
if (entry.direct_chunk)
|
|
entry.algorithm = dcpl.GetCompression();
|
|
|
|
if (entry.direct_chunk) {
|
|
// A positional read needs the file the PIXELS are in, and that is not always the file that
|
|
// was opened: a dataset reached through an external link lives elsewhere, and a chunk
|
|
// address is an offset into ITS file - as is the user block the address counts from. Ask
|
|
// HDF5 which file the dataset ended up in rather than assuming it is the one we asked.
|
|
std::string path = loc.path;
|
|
const hid_t owner = H5Iget_file_id(entry.dataset->GetID());
|
|
if (owner >= 0) {
|
|
const ssize_t name_size = H5Fget_name(owner, nullptr, 0);
|
|
if (name_size > 0) {
|
|
std::string name(static_cast<size_t>(name_size), '\0');
|
|
if (H5Fget_name(owner, name.data(), name.size() + 1) >= 0)
|
|
path = name;
|
|
}
|
|
const hid_t fcpl = H5Fget_create_plist(owner);
|
|
if (fcpl >= 0) {
|
|
hsize_t user_block = 0;
|
|
if (H5Pget_userblock(fcpl, &user_block) >= 0)
|
|
entry.user_block = user_block;
|
|
H5Pclose(fcpl);
|
|
}
|
|
H5Fclose(owner);
|
|
}
|
|
if (!path.empty()) {
|
|
entry.raw = std::make_shared<RawFile>(path);
|
|
if (!entry.raw->IsOpen())
|
|
entry.raw.reset();
|
|
}
|
|
}
|
|
|
|
return dataset_cache_.emplace(std::move(key), std::move(entry)).first->second;
|
|
}
|
|
|
|
std::optional<HDF5ImageSource::DirectChunk>
|
|
HDF5ImageSource::PrepareDirectRead(const HDF5ImageLocator::Location &loc) const {
|
|
const auto &ds = GetDataset(loc);
|
|
if (!ds.raw)
|
|
return {};
|
|
|
|
const hsize_t coord_3d[3] = {loc.local_index, 0, 0};
|
|
const hsize_t coord_4d[4] = {loc.local_index, loc.channel, 0, 0};
|
|
const hsize_t *coord = ds.multichannel ? coord_4d : coord_3d;
|
|
unsigned filter_mask = 0;
|
|
haddr_t address = HADDR_UNDEF;
|
|
hsize_t size = 0;
|
|
if (H5Dget_chunk_info_by_coord(ds.dataset->GetID(), coord, &filter_mask, &address, &size) < 0)
|
|
return {};
|
|
// A chunk nobody ever wrote has no address and no bytes; only HDF5 knows it reads as the fill
|
|
// value, so hand those back to it.
|
|
if (address == HADDR_UNDEF || size == 0)
|
|
return {};
|
|
|
|
return DirectChunk{ds.raw, ds.user_block + address, static_cast<uint32_t>(size),
|
|
ds.width, ds.height, ds.mode, ds.algorithm};
|
|
}
|
|
|
|
CompressedImage HDF5ImageSource::ReadDirect(RawByteBuffer &buffer, const DirectChunk &chunk) {
|
|
buffer.resize(chunk.size);
|
|
chunk.file->ReadAt(buffer.data(), chunk.size, chunk.address);
|
|
return {buffer.data(), buffer.size(), chunk.width, chunk.height, chunk.mode, chunk.algorithm};
|
|
}
|