// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only #import #include "SideChainFieldMetal.h" #include #include #include #include #include #include #include "../../common/CUDAWrapper.h" // gpus_are_hidden #include "../../common/JFJochException.h" // The kernel's source, SideChainFieldMetal.metal, copied in at configure time (CMakeLists.txt). extern const char *const side_chain_field_metal_source; namespace { std::atomic states_on_gpu{0}; static_assert(sizeof(FieldAtom) == 6 * 4, "the kernel's Atom is six words"); struct Kernel { id device = nil; // nil: no Metal device id pipeline = nil; id 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]; // Fast math off: nothing reassociated or approximated, as on the CPU. options.fastMathEnabled = NO; options.languageVersion = MTLLanguageVersion3_0; NSError *error = nil; id library = [k.device newLibraryWithSource:@(side_chain_field_metal_source) options:options error:&error]; if (library == nil) throw JFJochException(JFJochExceptionCategory::GPUMetalError, std::string("side-chain field kernel: ") + error.localizedDescription.UTF8String); id function = [library newFunctionWithName:@"contribution"]; k.pipeline = [k.device newComputePipelineStateWithFunction:function error:&error]; if (k.pipeline == nil) throw JFJochException(JFJochExceptionCategory::GPUMetalError, std::string("contribution: ") + error.localizedDescription.UTF8String); k.queue = [k.device newCommandQueue]; return k; }(); return kernel; } id NewBuffer(id device, size_t bytes) { id b = [device newBufferWithLength:std::max(bytes, 4) options:MTLResourceStorageModeShared]; if (b == nil) throw JFJochException(JFJochExceptionCategory::GPUMetalError, "side-chain field: out of GPU memory"); return b; } id Upload(id device, const std::vector &v) { id b = NewBuffer(device, v.size() * sizeof(float)); std::memcpy(b.contents, v.data(), v.size() * sizeof(float)); return b; } struct Params { uint32_t rows, n_ops, first; }; } // namespace struct SideChainFieldMetal::Impl { uint32_t rows, n_ops; id hx, hy, hz, ht, f0, stol2; }; SideChainFieldMetal::SideChainFieldMetal(std::unique_ptr impl) : impl_(std::move(impl)) {} SideChainFieldMetal::~SideChainFieldMetal() = default; std::unique_ptr SideChainFieldMetal::Make(size_t rows, size_t n_ops, const std::vector &hx, const std::vector &hy, const std::vector &hz, const std::vector &ht, const std::vector &f0, const std::vector &stol2) { @autoreleasepool { if (gpus_are_hidden() || rows * std::max(n_ops, 1) > std::numeric_limits::max()) return nullptr; const Kernel &k = GetKernel(); if (k.device == nil) return nullptr; auto impl = std::make_unique(); impl->rows = static_cast(rows); impl->n_ops = static_cast(n_ops); impl->hx = Upload(k.device, hx); impl->hy = Upload(k.device, hy); impl->hz = Upload(k.device, hz); impl->ht = Upload(k.device, ht); impl->f0 = Upload(k.device, f0); impl->stol2 = Upload(k.device, stol2); return std::unique_ptr(new SideChainFieldMetal(std::move(impl))); } } void SideChainFieldMetal::Contributions(const FieldStates &states, int first, std::vector>> &out) { // In a pool of its own, as ModelDensityMetal(): the threads this runs on have none. @autoreleasepool { const Kernel &k = GetKernel(); const size_t rows = impl_->rows, count = out.size(); if (count == 0) return; id d_atoms = NewBuffer(k.device, states.atoms.size() * sizeof(FieldAtom)); std::memcpy(d_atoms.contents, states.atoms.data(), states.atoms.size() * sizeof(FieldAtom)); id d_start = NewBuffer(k.device, states.start.size() * sizeof(int)); std::memcpy(d_start.contents, states.start.data(), states.start.size() * sizeof(int)); id d_out = NewBuffer(k.device, count * rows * sizeof(std::complex)); const Params p{impl_->rows, impl_->n_ops, static_cast(first)}; id cb = [k.queue commandBuffer]; id e = [cb computeCommandEncoder]; [e setComputePipelineState:k.pipeline]; id tables[] = {impl_->hx, impl_->hy, impl_->hz, impl_->ht, impl_->f0, impl_->stol2}; for (int i = 0; i < 6; i++) [e setBuffer:tables[i] offset:0 atIndex:i]; [e setBuffer:d_atoms offset:0 atIndex:6]; [e setBuffer:d_start offset:0 atIndex:7]; [e setBytes:&p length:sizeof(p) atIndex:8]; [e setBuffer:d_out offset:0 atIndex:9]; [e dispatchThreads:MTLSizeMake(rows, count, 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 : "side-chain field: command buffer failed"); const auto *f = static_cast *>(d_out.contents); for (size_t s = 0; s < count; s++) out[s].assign(f + s * rows, f + (s + 1) * rows); states_on_gpu += count; } } size_t SideChainFieldMetal::States() { return states_on_gpu; }