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>
109 lines
4.8 KiB
Metal
109 lines
4.8 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 model density's gather (ModelDensityMetal.h), compiled when it is first used from this source, which
|
|
// CMakeLists.txt copies into the binary - with fast math off and no a*b+c contracted, as on the host, so that
|
|
// every operation below rounds as the CPU's does.
|
|
|
|
#include <metal_stdlib>
|
|
using namespace metal;
|
|
|
|
#pragma clang fp contract(off)
|
|
|
|
// A brick's edge in points (ModelDensityMetal.mm says the same): every point of a brick tests each atom whose box
|
|
// reaches the brick, and with 4 rather than CUDA's 8 far fewer of them test atoms that miss them - the kernel
|
|
// took a half to a third of the time on an Apple M6.
|
|
constant int BRICK = 4;
|
|
|
|
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];
|
|
};
|
|
|
|
// Where an atom's box starts along each axis, wrapped into the grid, and its width less one (2 d): the test
|
|
// whether a point is in the box, without a division.
|
|
struct Box {
|
|
int first[3];
|
|
int span[3];
|
|
};
|
|
|
|
struct Grid { // ModelDensityMetalGrid
|
|
int nu, nv, nw;
|
|
float m00, m01, m02, m11, m12, m22;
|
|
};
|
|
|
|
// gemmi's unsafe_expapprox() (formfact.hpp, MPL-2.0; https://github.com/project-gemmi/gemmi), which is based on
|
|
// expapprox() from https://github.com/jhjourdan/SIMD-math-prims: the same operations, the bits moved with as_type
|
|
// where gemmi uses memcpy.
|
|
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))));
|
|
}
|
|
|
|
// The point p's offset in the box along an axis of n points, 0 to span where it is in it.
|
|
static int box_offset(int p, int first, int n) {
|
|
const int m = p - first;
|
|
return m < 0 ? m + n : m;
|
|
}
|
|
|
|
// One threadgroup per brick of BRICK^3 points: each point adds the brick's atoms in model order, as
|
|
// UseDensityBoxesByPlane() and AtomDensity (ModelGrid.cpp) add them on the CPU.
|
|
kernel void model_density(device const Atom *atoms [[buffer(0)]], constant Grid &g [[buffer(1)]],
|
|
device const uint *pair_atom [[buffer(2)]], device const uint *brick_first [[buffer(3)]],
|
|
device float *out [[buffer(4)]], device const Box *boxes [[buffer(5)]],
|
|
uint3 group [[threadgroup_position_in_grid]], uint3 t [[thread_position_in_threadgroup]]) {
|
|
const uint brick = group.x;
|
|
const int nbu = (g.nu + BRICK - 1) / BRICK, nbv = (g.nv + BRICK - 1) / BRICK;
|
|
const int u = int(brick % uint(nbu)) * BRICK + int(t.x);
|
|
const int v = int(brick / uint(nbu) % uint(nbv)) * BRICK + int(t.y);
|
|
const int w = int(brick / uint(nbu * nbv)) * BRICK + int(t.z);
|
|
if (u >= g.nu || v >= g.nv || w >= g.nw)
|
|
return;
|
|
float acc = 0.0f;
|
|
for (uint k = brick_first[brick]; k < brick_first[brick + 1]; k++) {
|
|
const uint i = pair_atom[k];
|
|
device const Box &box = boxes[i];
|
|
const int mu = box_offset(u, box.first[0], g.nu), mv = box_offset(v, box.first[1], g.nv),
|
|
mw = box_offset(w, box.first[2], g.nw);
|
|
if (mu > box.span[0] || mv > box.span[1] || mw > box.span[2])
|
|
continue;
|
|
device const Atom &at = atoms[i];
|
|
// The point's index in the box's own (unwrapped) range, as the CPU walks it.
|
|
const int uu = at.c[0] - at.d[0] + mu, vv = at.c[1] - at.d[1] + mv, ww = at.c[2] - at.d[2] + mw;
|
|
const float fx = at.nctr[0] - float(uu), fy = at.nctr[1] - float(vv), fz = at.nctr[2] - float(ww);
|
|
const float z = g.m22 * fz;
|
|
const float y = g.m11 * fy + g.m12 * fz;
|
|
const float yz = y * y + z * z;
|
|
const float x = g.m00 * fx + (g.m01 * fy + g.m02 * fz);
|
|
const float r2 = yz + x * x;
|
|
if (r2 > at.max_r2)
|
|
continue;
|
|
float density = 0.0f;
|
|
if (at.aniso == 0) {
|
|
for (int q = 0; q < 5; q++)
|
|
density += at.a[q] * expapprox(max(at.b[q][0] * r2, -88.f));
|
|
} else {
|
|
for (int q = 0; q < 5; q++) {
|
|
device const float *b = at.b[q];
|
|
// gemmi's SMat33::r_u_r(), in its order
|
|
const float rur = x * x * b[0] + y * y * b[1] + z * z * b[2]
|
|
+ 2 * (x * y * b[3] + x * z * b[4] + y * z * b[5]);
|
|
density += at.a[q] * expapprox(max(rur, -88.f));
|
|
}
|
|
}
|
|
acc += at.occ * density;
|
|
}
|
|
out[uint(u) + uint(g.nu) * (uint(v) + uint(g.nv) * uint(w))] = acc;
|
|
}
|