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>
135 lines
5.3 KiB
Metal
135 lines
5.3 KiB
Metal
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only AND MPL-2.0
|
|
|
|
// The refinement gradient's sums over the map (AtomGradientMetal.h), compiled when it is first used from this source,
|
|
// which CMakeLists.txt copies into the binary. The place of a point and the test against the radius are spelled as
|
|
// GatherMapPoints() (RefinementXray.cpp) spells them, with no a*b+c contracted, so that the GPU takes the CPU's points.
|
|
|
|
#include <metal_stdlib>
|
|
using namespace metal;
|
|
|
|
#pragma clang fp contract(off)
|
|
|
|
struct Atom { // ModelDensityMetalAtom
|
|
float nctr[3];
|
|
int c[3], d[3];
|
|
float max_r2;
|
|
float occ;
|
|
int aniso;
|
|
float a[5];
|
|
float b[5][6];
|
|
};
|
|
|
|
struct Grid { // ModelDensityMetalGrid
|
|
int nu, nv, nw;
|
|
float m00, m01, m02, m11, m12, m22;
|
|
};
|
|
|
|
struct Sums { // AtomGradientMetalSums
|
|
float s[9];
|
|
};
|
|
|
|
// gemmi's unsafe_expapprox() (formfact.hpp, MPL-2.0; https://github.com/project-gemmi/gemmi), as in
|
|
// ModelDensityMetal.metal.
|
|
static float expapprox(float x) {
|
|
const float val = 12102203.1615614f * x + 1065353216.f;
|
|
const int vali = static_cast<int>(val);
|
|
const int xu1 = vali & 0x7F800000;
|
|
const int xu2 = (vali & 0x7FFFFF) | 0x3F800000;
|
|
const float a = as_type<float>(xu1);
|
|
const float b = as_type<float>(xu2);
|
|
return a * (0.509871020f + b * (0.312146713f + b * (0.166617139f + b *
|
|
(-2.190619930e-3f + b * 1.3555747234e-2f))));
|
|
}
|
|
|
|
static int wrap(int i, int n) {
|
|
const int m = i % n;
|
|
return m < 0 ? m + n : m;
|
|
}
|
|
|
|
// One SIMD group of 32 threads per atom: the lanes take the box's points in turn, u fastest, each summing its own,
|
|
// and the group's sums are added at the end.
|
|
kernel void atom_gradient(device const Atom *atoms [[buffer(0)]], constant Grid &g [[buffer(1)]],
|
|
device const float *map [[buffer(2)]], device Sums *out [[buffer(3)]],
|
|
constant uint &n [[buffer(4)]],
|
|
uint group [[threadgroup_position_in_grid]], uint sg [[simdgroup_index_in_threadgroup]],
|
|
uint groups_per_tg [[simdgroups_per_threadgroup]], uint lane [[thread_index_in_simdgroup]]) {
|
|
const uint i = group * groups_per_tg + sg;
|
|
if (i >= n)
|
|
return;
|
|
device const Atom &at = atoms[i];
|
|
const int span[3] = {2 * at.d[0] + 1, 2 * at.d[1] + 1, 2 * at.d[2] + 1};
|
|
const int lo[3] = {at.c[0] - at.d[0], at.c[1] - at.d[1], at.c[2] - at.d[2]};
|
|
const int total = span[0] * span[1] * span[2];
|
|
float s[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
|
|
// the lane's point, as offsets in the box, moved on by 32 points at a time
|
|
int iu = int(lane), iv = 0, iw = 0;
|
|
while (iu >= span[0]) {
|
|
iu -= span[0];
|
|
if (++iv == span[1]) {
|
|
iv = 0;
|
|
iw++;
|
|
}
|
|
}
|
|
for (int t = int(lane); t < total; t += 32) {
|
|
const int u = lo[0] + iu, v = lo[1] + iv, w = lo[2] + iw;
|
|
iu += 32;
|
|
while (iu >= span[0]) {
|
|
iu -= span[0];
|
|
if (++iv == span[1]) {
|
|
iv = 0;
|
|
iw++;
|
|
}
|
|
}
|
|
// GatherMapPoints()'s place and test
|
|
const float fz = at.nctr[2] - float(w);
|
|
const float z = g.m22 * fz;
|
|
const float fy = at.nctr[1] - float(v);
|
|
const float y = g.m11 * fy + g.m12 * fz;
|
|
const float yz = y * y + z * z;
|
|
const float x_yz = g.m01 * fy + g.m02 * fz;
|
|
const float x = g.m00 * (at.nctr[0] - float(u)) + x_yz;
|
|
const float r2 = yz + x * x;
|
|
if (r2 > at.max_r2)
|
|
continue;
|
|
const float value = map[uint(wrap(u, g.nu)) + uint(g.nu) * (uint(wrap(v, g.nv)) + uint(g.nv) * uint(wrap(w, g.nw)))];
|
|
if (at.aniso == 0) {
|
|
float dr2 = 0, db = 0;
|
|
for (int q = 0; q < 5; q++) {
|
|
const float b = at.b[q][0];
|
|
const float e = at.a[q] * expapprox(max(b * r2, -88.f));
|
|
dr2 += e * b;
|
|
db += e * b * (1.5f + b * r2);
|
|
}
|
|
s[0] += value * dr2 * x;
|
|
s[1] += value * dr2 * y;
|
|
s[2] += value * dr2 * z;
|
|
s[3] += value * db;
|
|
} else {
|
|
for (int q = 0; q < 5; q++) {
|
|
device const float *m = at.b[q]; // u11 u22 u33 u12 u13 u23
|
|
// gemmi's SMat33::r_u_r(), in its order
|
|
const float rur = x * x * m[0] + y * y * m[1] + z * z * m[2] + 2 * (x * y * m[3] + x * z * m[4] + y * z * m[5]);
|
|
const float e = value * at.a[q] * expapprox(max(rur, -88.f));
|
|
const float mr0 = m[0] * x + m[3] * y + m[4] * z;
|
|
const float mr1 = m[3] * x + m[1] * y + m[5] * z;
|
|
const float mr2 = m[4] * x + m[5] * y + m[2] * z;
|
|
s[0] += e * mr0;
|
|
s[1] += e * mr1;
|
|
s[2] += e * mr2;
|
|
s[3] += e * (m[0] + 2 * mr0 * mr0);
|
|
s[4] += e * (m[1] + 2 * mr1 * mr1);
|
|
s[5] += e * (m[2] + 2 * mr2 * mr2);
|
|
s[6] += e * (m[3] + 2 * mr0 * mr1);
|
|
s[7] += e * (m[4] + 2 * mr0 * mr2);
|
|
s[8] += e * (m[5] + 2 * mr1 * mr2);
|
|
}
|
|
}
|
|
}
|
|
for (int k = 0; k < 9; k++)
|
|
s[k] = simd_sum(s[k]);
|
|
if (lane == 0)
|
|
for (int k = 0; k < 9; k++)
|
|
out[i].s[k] = s[k];
|
|
}
|