Repository navigation
Conversation
(Mismatching prototype vs. system-provided sort on macOS)
…appers. (Work remains, some of the comments need to go back in etc...)
) Ported from #2968 (review for UB/OpenACC hazards): - findroots/findroots_gpu wrote into vf_list[20] without a bound check. The inserts are now bounded by BVK_MAX_ROOTS (= size of vf_list). - nf == 0 now ABSORBs (before, it read an unset vf_list[0]), and the index is clamped to nf-1 because rand01() may return 1.0. - SHARE used Phonon_simple's PHONON_SIMPLE guard while defining incompatible nbose/fatalerror/omega_q/zridd/findroots, so instruments combining it with Phonon_simple or Magnon_bcc could not compile. The guard is now PHONON_BVK_PG, and the helpers are prefixed bvk_. - #define DV became BVK_DV: it clobbered the DECLARE variable DV of Phonon_simple, Magnon_bcc and others. - #define pi removed: it broke components that declare a variable named pi. Test_Phonon_BvK_PG relied on the leaked macro and now uses PI. No numerical change: Test_Phonon_BvK_PG (-n 1e6 --seed=1) is bit-identical. Phonon_BvK_PG + Phonon_simple + Magnon_bcc now builds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…SHARE The component is NOACC (its root finder takes a function pointer), so it runs on the CPU. The routine directives still made nvc build these helpers, and by implication the James Avery eigen solver they call, for the device. eigen.c's fprintf then fails to compile: "NVC++-S-1058-Call to variadic function not supported - fprintf". CPU output is unchanged (Test_Phonon_BvK_PG bit-identical). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…op NOACC eigen.c/.h (James Avery Hermitian eigen solver): - Replace the per-call malloc scratch in eigensystem_hermitian, QHQ, T_QTQ, reflect_region, apply_reflection and matrix_inplace_multiply with fixed-size stack arrays of EIGEN_MAXN (default 12, overridable). On the CPU, n > EIGEN_MAXN aborts with a message. This also removes the leaks of the old row/vHA/col/Q_tmp buffers. - memset/memcpy become plain loops. - Guard the non-convergence fprintf with #ifndef OPENACC. The TIME_EIGENSYSTEMS counter/timing is now CPU-only. - Mark the device-path functions "#pragma acc routine seq" (prototypes and definitions). Phonon_BvK_PG.comp: - TRACE no longer writes shared state. The phonon mode moves from a SHARE global into p_call[13], read by bvk_omega_q. p_call, eigenvectormode, q and q_x/q_y/q_z move from DECLARE into TRACE locals; unused DECLARE members (Matrix, i, j, qx, qy, qz) are gone. - One root finder for CPU and GPU: bvk_zridd/bvk_findroots no longer take a function pointer, and the last_fh/last_x2 globals become findroots locals passed by pointer. bvk_zridd_gpu and bvk_findroots_gpu are removed. - The globals read on the device (verbose, shape, M, Delta, astar, cstar, r_j13, Phi1..Phi14) get "acc declare create" and an "acc update device" at the end of INITIALIZE. - The NaN check in bvk_omega_q (fprintf/abort) is CPU-only. - Restore "#pragma acc routine seq" on the SHARE helpers and remove NOACC. CPU output is bit-identical to bd2d044: Test_Phonon_BvK_PG, --seed=1, 8 parameter sets on 12 MPI ranks and 4 single-process sets, including Verbose=7, DISP=1 and DISP=2. GPU execution is not tested. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The sqrt3 macro had an L suffix, so the TRACE lines qk/qh and q_Bragg[1] (x / sqrt3) were computed in long double. nvc cannot do x87 long double on the device and failed with "Accelerator restriction: unsupported operation: X87TODP" in class_Phonon_BvK_PG_trace. Numerical behaviour: on arm64 and MSVC (long double == double) output is bit-identical. On x86_64 (80-bit long double) weights change in the last bits: Test_Phonon_BvK_PG --seed=1 -n 1e5, built for x86_64 and run under Rosetta, has identical counts in every bin and integrated intensities within ~1e-8 relative. A few near-zero-weight events change more, probably because the nearest-Bragg-point choice flips on a tie. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…o 'exit') - nrerror(): guard the body (printf + exit) with #ifndef OPENACC, like bvk_fatalerror. The illegal-mode branch in TRACE now ABSORBs after nrerror; only the GPU reaches that line, and there it avoids indexing eigenvalue/index arrays with an out-of-range mode. - TRACE "Did not hit sample surface from inside": exit(1) on the CPU, ABSORB under OPENACC. The rest of the device path (TRACE, bvk_omega_q, bvk_zridd, bvk_findroots, eigen.c) has no exit/abort/fprintf left when preprocessed with -DOPENACC. CPU output is bit-identical to cfe4e8a on arm64 and x86_64 (Test_Phonon_BvK_PG --seed=1: defaults, phononmode=3, phononmode=12, E_steps=1, DISP=1; data and stdout). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- eigen.c: free per-call scratch buffers (memory grew ~10 kB per eigensolve) - eigen.c: #undef generic macros (G, ROWS, COLS, ...) that broke e.g. Guide_gravity - Phonon_BvK_PG: random mode via floor(), clamped to 11 (was -1 or 12 at rand01() edges) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- mode/verbose passed to SHARE functions via parms[13..14]; shape/verbose in DECLARE - zridd bracket cache passed explicitly instead of globals - per-particle q, p_call, eigenvectormode moved from DECLARE to TRACE - exit(-1) on missing geometry; MCDISPLAY follows the chosen shape - use runtime HBAR/MNEUTRON (results change by ~1e-4 relative) - clamp negative eigenvalues, ABSORB non-finite weights and near-tangent exits - warn once when roots exceed BVK_MAX_ROOTS - exit(-1) instead of exit(1)/abort; errors to stderr - drop always-on ROT120 dump and eigensolver timing output - comment: rows of Q hold conjugate eigenvectors Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- drop dead code: GPU root-finder variants, unused helpers/arrays, MATRIX_TEST and TEST_MATRICES scaffolding - call complexexp_qdotr directly - doc fixes: %L link, spelling - test instrument: header name, %P docs, drop commented-out components and unused variables - apply mccode-clangformat Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Component taken from #2344 with the OpenACC port re-applied: no NOACC, acc routine pragmas, device globals, single root finder without the function pointer, CPU-only error paths, double sqrt3. eigen.c keeps the stack arrays (frees dropped) and gains the #undef block. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Plain C99 inline has no external definition, so the link fails with "undefined reference to cmul" whenever the compiler does not inline a call (e.g. at -O0, or Phonon_BvK_PG's eigen solver with clang on Ubuntu). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 of 4 tasks
Port of the dynamical-matrix update from the Sept 27 2026 version by K. Lefmann: - in-plane couplings use phases from unit-cell origins (r_1uc, r_3uc, r_4uc) instead of atom-to-atom vectors - the C/D layer gets its own 6x6 block; it was a copy of the A/B block, although D is mirrored relative to B in Delta - the structure factor uses the full q instead of the nearest Bragg point, to match the new phases The Sept 27 equations are kept as comments above the complex-lib versions. Numerical change: results along (0,0,l) are unchanged; off-axis intensities shift, e.g. -0.7% at (0.4,0,6) and -19% at (2,0,0) on e_mon_beforeana_zoom. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…et al. paper draft
The tolerance was a fixed 1e4*eps (about 2.2e-12). For eigenvalues around 1.8e3 that is close to roundoff, so QR iteration sometimes stalled just above it and printed "Cannot converge eigenvalue ...". The tolerance is now scaled by max(1, max_norm(A)). Results are unchanged for the Test_Phonon_BvK_PG case checked (1e7 rays, seed 1000, MPI); its 2 warnings are gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings in the Sept 27 2026 dynamical matrix (separate C/D block, unit-cell phases, full-q structure factor), the relative eigen.c tolerance, new parameter defaults and the %Test lines. OpenACC port kept: no NOACC, the new r_1uc/r_3uc/r_4uc vectors are added to the acc declare/update lists, and the eigen.c warning stays CPU-only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Author
|
Emulated GPU run under way at https://github.com/mccode-dev/McCode/actions/runs/43924317757/job/133499614875 (killed by timeout) |
Contributor
Author
|
The code seems to run - but is quite slow / likely hampered by the big stack requirement for the 12x12's... Will likely just leave this draft PR for potential later investigation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #2344. It replaces the stop-gap in bd2d044 (pragmas removed, component NOACC) with a real OpenACC port.
What changed
mcstas-comps/share/eigen.c/eigen.h(James Avery Hermitian eigen solver)eigensystem_hermitian,QHQ,T_QTQ,reflect_region,apply_reflectionandmatrix_inplace_multiplyused tomallocscratch on every call, many times per neutron. They now use fixed-size stack arrays sized byEIGEN_MAXN. It defaults to 12 and can be overridden with#ifndef. On the CPU,n > EIGEN_MAXNaborts with a message. This also removes the leaks:row,vHA,colandQ_tmpwere never freed.memset/memcpyare now plain loops.fprintfis wrapped in#ifndef OPENACC. TheTIME_EIGENSYSTEMScall counter and timing are CPU-only (#if TIME_EIGENSYSTEMS && !defined(OPENACC)).#pragma acc routine seqis on every device-path function, on both the prototype and the definition.mccode-complex-libwrappers, with no type-punning.D/Lineigensystem_hermitianare sizedEIGEN_MAXN+1, becauseT_QTQ(n)readsDin[n]. That element is unused padding, but with exactlynelements the read was out of bounds, as it already was on the heap before.mcstas-comps/contrib/Phonon_BvK_PG.compmodemoves intop_call[13]and is read bybvk_omega_q.p_call,eigenvectormode,qandq_x/q_y/q_zmove from DECLARE to TRACE locals.Matrix,i,j,qx,qy,qz.bvk_zridd/bvk_findrootsno longer take a function pointer.last_fh/last_x2globals becomebvk_findrootslocals passed by pointer.bvk_zridd_gpu/bvk_findroots_gpuare removed. They used a different bracketing, and their high-side upper bound used the wrong range.bvk_fatalerror,bvk_zriddreturnsUNUSEDinstead of0.0. That line is only reachable on the GPU, where the bracket is now dropped instead of adding av_f = 0root.M,Delta,astar,cstar,r_j13,r_1ucA/C,r_3ucA/C,r_4ucAandPhi1..Phi14get#pragma acc declare create(...)in SHARE and#pragma acc update device(...)at the end of INITIALIZE.bvk_omega_q(fprintf/abort) is CPU-only.#pragma acc routine seqlines are back on the SHARE helpers, andNOACCis removed.sqrt3is now adoubleliteral (3a6b482). It used to have anLsuffix, soqk,qhandq_Bragg[1]in TRACE were computed in long double. nvc failed on that withAccelerator restriction: unsupported operation: X87TODPinclass_Phonon_BvK_PG_trace.Numerical behaviour
Changed in the last bits on x86_64 only, because of the
sqrt3fix. b49a15d alone is bit-identical on both arm64 and x86_64.Test_Phonon_BvK_PG --seed=1 -n 1e5, built for x86_64 and run under Rosetta, was checked for the default parameters,phononmode=3,phononmode=12andphononmode=7 l=2.5 E=8. The per-bin counts are identical, so the trajectories are the same. Integrated intensities agree to about 1e-8 relative, far below the statistical error. A few events with near-zero weight (about 1e-36) change more, probably because the nearest-Bragg-point choice flips on a tie.phononmode=7stays bit-identical.What was tested
macOS arm64, clang,
mccode-devmicromamba env. Base was bd2d044, new was b49a15d.Test_Phonon_BvK_PG,--seed=1, throughmcrun(12 MPI ranks for both base and new), with 8 parameter sets:phononmode=3phononmode=12phononmode=7 l=2.5 E=8E_steps_low=1 E_steps_high=1 phononmode=2DISP=1 phononmode=5DISP=2 phononmode=1Verbose=2 phononmode=4Each set is
-n 1e5, except theDISPsets (1e4) and theVerboseset (1e3). Every monitor's data is bit-identical. Only the# Dateheader differs.Single-process runs (binary run directly), comparing data and stdout exactly, with 4 sets:
-n 1e5DISP=1DISP=2Verbose=7 phononmode=12: about 195k lines that print every eigen-solver Q matrix and eigenvalue setAll are bit-identical, data and stdout.
Warnings: the generated C compiled with
clang -std=c99 -Wall -Wextragives the same 77 warnings as the base. All of them are pre-existing, and none are new.-DOPENACCsyntax check: the generated C was syntax-checked with clang and stubopenacc.h/accelmath.hheaders, so the#ifndef OPENACCbranches are at least well-formed.Not tested:
nvclocally. Theubuntu-24.04.conda.classic.gcc.openmpi.openaccCI job buildsTest_Phonon_BvK_PGwith nvc (-n0) and passes, but it does not run anything. Theubuntu-24.04.conda.openacc.h100-simjob, which runs on a simulated H100, is the first runtime check. Nobody has run it on a real GPU.Open questions
mccode-complex-lib. Several McXtrace components call it in TRACE without NOACC:PSD_monitor_coh(cexp),Capillary(cabs,cdiv),Mirror_curved(conj,cmul),Source_genesis13andSource_simplex(cabs). In McStas,SANSPDBFastincludes it but does not call it in TRACE, so this is the first McStas component to use it on the device, and the first to run a complex eigen solver on the device. The device path callscreal,cimag,conjandcabs(eigen.c) andcexp(TRACE), plus the library'sinlinewrappers, which have noacc routine. The wrappers are nowstatic inline(a33edef), and the nvc build job accepts the device path. Whether the results are right on the device still depends on the H100-sim run.bvk_omega_q→eigensystem_hermitianuses about 15 KB of stack per thread, mostly 12×12cdoublematrices. This is not measured yet:-Minfo=accel -gpu=ptxinfogives the real frame sizes, andNV_ACC_CUDA_STACKSIZEraises the limit if a run overflows. If it needs shrinking, three changes that keep the CPU output bit-identical save about 6.5 KB: mirrorPhi_12Dimin place instead of copying it tomatrix, do the finalQhat·Qin place one column at a time instead of usingQ_tmp, and writePhi_12Dimdirectly without the 6×6 blocks. Separately, computing eigenvalues only during root finding would save time as well, but changes results in the last bits.Global state shared between instances.Resolved by Initial commit of Phonon_BvK_PG and test instrument #2344's 6757727.shapeandverboseare in DECLARE, andverbosereachesbvk_omega_qthroughparms[14].Resolved by Initial commit of Phonon_BvK_PG and test instrument #2344's 2238aae. The mode is nowrand01()==1.0edge.floor(12*rand01()), clamped to 11. The root index is clamped tonf-1, andnf == 0absorbs (d3793dd).Resolved. Everyexiton the device.exit(-1)that TRACE can reach is now behind#ifndef OPENACC, and on the GPU the illegal-mode path doesABSORB. The remainingexitcalls are in INITIALIZE (missing geometry) and in CPU-only helpers.Refs #2988
🤖 Generated with Claude Code