Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 16m43s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 28m46s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 32m5s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 39m44s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m7s
Build Packages / Create release (push) Successful in 8s
Build Packages / Build documentation (push) Successful in 49s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 22m30s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 31m54s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 11m29s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 23m51s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 26m7s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 29m38s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 28m6s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 29m44s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 33m22s
Build Packages / Generate python client (push) Successful in 47s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 34m6s
Build Packages / Unit tests (push) Successful in 1h2m47s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 35m59s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 34m8s
Build Packages / Validation Battery (push) Successful in 38m31s
* Rugnux: Refinement several times faster, one protocol on the CPU and the GPU, parts of it on the Apple GPU; its random draws are the same on every platform. * Rugnux: Refinement follows common practice: the free set decides only the restraint weights and the displacement model (isotropic, TLS or anisotropic, by Hamilton's test), everything else is decided on the working set; lower R-free for placed and predicted models. * Rugnux: Restraint dictionaries via `--restraints` (or among the inputs) and more built-in ligands; a model whose space group is a subgroup of the data's is refined in its own group; reflections plus a model select `--mode refine` on their own. * Rugnux: `-C` fixes the crystal system on the axes as typed; `-S` alone fixes the space group only, the cell is still found and reduced; a space group or cell stored in the input file is ignored, with a line in the log. * Rugnux: More reliable multi-sweep merging; `--mode scale` merges several stills runs as one; `_plot.txt` and the report name each input; a second crystal form is reported for stills given a cell. * jfjoch_broker (experimental): Live Rugnux on FPGA detectors (JUNGFRAU) takes the same path as a DECTRIS stream, rotation and serial alike, model step included, with analysis threads per GPU; frames waiting for the analysis can be viewed; no host stage drops a frame - a source faster than the analysis is held at the card. * jfjoch_broker: Summation of more than 256 frames per image is split between the card and the host (`summation_mode` is then `hybrid`), and live Rugnux runs on such collections; pedestal runs take a receiver of their own. * jfjoch_broker (experimental): `stills_downsample` analyses 1 of every N serial images (chosen by a hash; the first 2000 all; the others written without analysis; reset to 1 by `/initialize`); `max_spot_count` left out means 250 for per-image indexing and up to 1000 for live stills; the live status gives the merge before the model work, the fitted resolution and the anomalous peak height of each unexplained blob. * jfjoch_broker: Without a `pcie` entry the broker runs on a mock of the card instead of the HLS simulation; its spot, ROI and azimuthal results are zero then. * Web frontend: Redesigned Rugnux live page; Vite 8, TypeScript 6, Mol* 5.13 and dependency security updates. * Tools: `jfjoch_fpga_stills_test` measures live stills on the cards with a stored dataset; `jfjoch_fpga_test` loses the HLS-simulation option. * Other: FFTW no longer fetched or linked (the CPU FFT indexer keeps the name `FFTW`); PCIe driver now GPL-2.0-or-later and third-party notices complete; several crash fixes. Reviewed-on: #98 Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
139 lines
5.5 KiB
Plaintext
139 lines
5.5 KiB
Plaintext
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#import <Metal/Metal.h>
|
|
|
|
#include "ShrinkMaskMetal.h"
|
|
|
|
#include <algorithm>
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <limits>
|
|
#include <string>
|
|
#include <unistd.h>
|
|
|
|
#include "../../common/CUDAWrapper.h" // gpus_are_hidden
|
|
#include "../../common/JFJochException.h"
|
|
|
|
// The kernel's source, ShrinkMaskMetal.metal, copied in at configure time (CMakeLists.txt).
|
|
extern const char *const shrink_mask_metal_source;
|
|
|
|
namespace {
|
|
|
|
std::atomic<size_t> calls_on_gpu{0};
|
|
|
|
struct Kernel {
|
|
id<MTLDevice> device = nil; // nil: no Metal device
|
|
id<MTLComputePipelineState> pipeline = nil;
|
|
id<MTLCommandQueue> queue = nil;
|
|
};
|
|
|
|
// Compiled once per process, the first time it is asked for.
|
|
const Kernel &GetKernel() {
|
|
static const Kernel kernel = [] {
|
|
Kernel k;
|
|
k.device = MTLCreateSystemDefaultDevice();
|
|
if (k.device == nil)
|
|
return k;
|
|
MTLCompileOptions *options = [MTLCompileOptions new];
|
|
options.languageVersion = MTLLanguageVersion3_0;
|
|
NSError *error = nil;
|
|
id<MTLLibrary> library = [k.device newLibraryWithSource:@(shrink_mask_metal_source) options:options
|
|
error:&error];
|
|
if (library == nil)
|
|
throw JFJochException(JFJochExceptionCategory::GPUMetalError,
|
|
std::string("mask shrink kernel: ") + error.localizedDescription.UTF8String);
|
|
id<MTLFunction> function = [library newFunctionWithName:@"shrink_mask"];
|
|
k.pipeline = [k.device newComputePipelineStateWithFunction:function error:&error];
|
|
if (k.pipeline == nil)
|
|
throw JFJochException(JFJochExceptionCategory::GPUMetalError,
|
|
std::string("shrink_mask: ") + error.localizedDescription.UTF8String);
|
|
k.queue = [k.device newCommandQueue];
|
|
return k;
|
|
}();
|
|
return kernel;
|
|
}
|
|
|
|
id<MTLBuffer> NewBuffer(id<MTLDevice> device, size_t bytes) {
|
|
id<MTLBuffer> b = [device newBufferWithLength:std::max<size_t>(bytes, 4) options:MTLResourceStorageModeShared];
|
|
if (b == nil)
|
|
throw JFJochException(JFJochExceptionCategory::GPUMetalError, "mask shrink: out of GPU memory");
|
|
return b;
|
|
}
|
|
|
|
// `data` as it is where its pages can be handed to the GPU, nil otherwise.
|
|
id<MTLBuffer> InPlace(id<MTLDevice> device, const float *data, size_t bytes) {
|
|
const size_t page = static_cast<size_t>(getpagesize());
|
|
if (reinterpret_cast<uintptr_t>(data) % page != 0)
|
|
return nil;
|
|
return [device newBufferWithBytesNoCopy:const_cast<float *>(data) length:(bytes + page - 1) / page * page
|
|
options:MTLResourceStorageModeShared deallocator:nil];
|
|
}
|
|
|
|
struct Params {
|
|
int32_t nu, nv, nw, n_near, n_far;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
bool ShrinkMaskMetal(int nu, int nv, int nw, const std::vector<std::array<int, 3>> &near,
|
|
const std::vector<std::array<int, 3>> &far, const float *in, float *out) {
|
|
// In a pool of its own, as ModelDensityMetal(): the threads this runs on have none.
|
|
@autoreleasepool {
|
|
const size_t points = static_cast<size_t>(nu) * nv * nw;
|
|
if (gpus_are_hidden() || points > std::numeric_limits<uint32_t>::max())
|
|
return false;
|
|
const Kernel &k = GetKernel();
|
|
if (k.device == nil)
|
|
return false;
|
|
const size_t bytes = points * sizeof(float);
|
|
id<MTLBuffer> d_in = InPlace(k.device, in, bytes);
|
|
if (d_in == nil) {
|
|
d_in = NewBuffer(k.device, bytes);
|
|
std::memcpy(d_in.contents, in, bytes);
|
|
}
|
|
id<MTLBuffer> d_out = InPlace(k.device, out, bytes);
|
|
const bool out_in_place = d_out != nil;
|
|
if (!out_in_place)
|
|
d_out = NewBuffer(k.device, bytes);
|
|
// the offsets as int3, which Metal pads to four ints
|
|
id<MTLBuffer> d_offsets = NewBuffer(k.device, (near.size() + far.size()) * 4 * sizeof(int32_t));
|
|
auto *o = static_cast<int32_t *>(d_offsets.contents);
|
|
size_t n = 0;
|
|
for (const auto *v : {&near, &far})
|
|
for (const std::array<int, 3> &d : *v) {
|
|
o[4 * n] = d[0];
|
|
o[4 * n + 1] = d[1];
|
|
o[4 * n + 2] = d[2];
|
|
o[4 * n + 3] = 0;
|
|
n++;
|
|
}
|
|
const Params p{nu, nv, nw, static_cast<int32_t>(near.size()), static_cast<int32_t>(far.size())};
|
|
|
|
id<MTLCommandBuffer> cb = [k.queue commandBuffer];
|
|
id<MTLComputeCommandEncoder> e = [cb computeCommandEncoder];
|
|
[e setComputePipelineState:k.pipeline];
|
|
[e setBuffer:d_in offset:0 atIndex:0];
|
|
[e setBuffer:d_out offset:0 atIndex:1];
|
|
[e setBuffer:d_offsets offset:0 atIndex:2];
|
|
[e setBytes:&p length:sizeof(p) atIndex:3];
|
|
[e dispatchThreads:MTLSizeMake(points, 1, 1) threadsPerThreadgroup:MTLSizeMake(256, 1, 1)];
|
|
[e endEncoding];
|
|
[cb commit];
|
|
[cb waitUntilCompleted];
|
|
if (cb.status != MTLCommandBufferStatusCompleted)
|
|
throw JFJochException(JFJochExceptionCategory::GPUMetalError,
|
|
cb.error ? cb.error.localizedDescription.UTF8String
|
|
: "mask shrink: command buffer failed");
|
|
if (!out_in_place)
|
|
std::memcpy(out, d_out.contents, bytes);
|
|
calls_on_gpu++;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
size_t ShrinkMaskMetalCalls() {
|
|
return calls_on_gpu;
|
|
}
|