feat(cuda): make the GPU architecture configurable, native by default

nvcc ran with -arch=sm_89 for every build, which is code for that one
architecture and its PTX, whatever GPU the program was for. Jorijn asked
for the architecture to be configurable, and native when nothing sets
it, as the CPU's is.

cfg.cudaArch is nvcc's -arch now, and it defaults to "native": nvcc
compiles for the GPUs it sees, and where it sees none it warns and uses
its own default architecture. Like -march=native it is for a program
that runs where it is built, since it carries no PTX, and
`crafter-build bundle` warns of a bundle whose CUDA code, in the program
or a library it is built on, is compiled for native, as it warns of
-march=native. ApplyStandardArgs sets it from --cuda-arch= and
CRAFTER_BUILD_CUDA_ARCH as it sets march from --march= and
CRAFTER_BUILD_MARCH; AddTest and AddMarchVariants give their tests the
parent's, as they give them its sysroot and debug flag; and
ForceHostTooling gives host tooling the host's, as it gives it the
host's march, since project.so runs it on the machine that loads it.

A .cu is compiled again only when it is newer than its object, so the
architecture keys VariantId, and so does the nvcc that compiles it, by
what it is rather than where: Toolchain::nvccStamp, its canonical path,
size and time, read without a process, changes when CUDA_PATH names
another toolkit and when an update replaces the one at the same path, as
a package does at /opt/cuda. native is keyed as well, by
Toolchain::cudaNativeKey: the NVIDIA display devices sysfs lists, by PCI
address and device ID, and CUDA_VISIBLE_DEVICES and CUDA_DEVICE_ORDER,
which choose and number them, each empty value apart from an unset one,
read without a process. It is crafter-build's reading of the machine,
not nvcc's. The word alone would keep an object compiled for a GPU the
machine no longer has, after a GPU change or on another machine that
shares the checkout, and that object carries no PTX for the GPU it has
now, where sm_89's carried its PTX. Only a configuration with CUDA
sources is keyed, so every other one keeps the directory it had, but for
one built with --cuda-arch=, which was a project argument, hashed as
one, and is crafter-build's own now.
This commit is contained in:
Jorijn van der Graaf 2026-11-11 19:34:52 +01:00
commit 967dd12b06
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
10 changed files with 262 additions and 15 deletions

View file

@ -67,6 +67,17 @@ export namespace Crafter {
// for want of a CUDA toolkit.
fs::path nvcc;
std::string nvccError;
// What that nvcc is, for VariantId: its canonical path, size and
// modification time, which change when the toolkit is updated or
// replaced at the same path, as the path alone does not. Empty
// without an nvcc.
std::string nvccStamp;
// What -arch=native compiles for on this machine, for VariantId: the
// NVIDIA display devices the PCI bus lists, by address and device ID,
// and CUDA_VISIBLE_DEVICES and CUDA_DEVICE_ORDER, which choose among
// them and number them, each unset apart from empty. Empty without an
// nvcc.
std::string cudaNativeKey;
// The Flatpak runtime crafter-build runs on, from the
// runtime=runtime/<id>/<arch>/<branch> line of /.flatpak-info:
// "x86_64" and "26.08" in the Flatpak, both empty outside one. A
@ -125,6 +136,19 @@ export namespace Crafter {
// order they are tried and as the host names them: GetToolchain()
// passes /opt/cuda, Arch's, and /usr/local/cuda, NVIDIA's installers'.
std::vector<fs::path> cudaRoots;
// The PCI devices sysfs lists, one directory each with its vendor,
// device and class: GetToolchain() passes /sys/bus/pci/devices, which
// the sandbox shows too. What -arch=native compiles for is keyed by
// the NVIDIA display devices among them.
fs::path pciDevices;
// CUDA_VISIBLE_DEVICES, which chooses the GPUs -arch=native compiles
// for, when it is set: set but empty hides them all, which is not the
// same as unset, so it is kept apart from env, which holds non-empty
// values alone.
std::optional<std::string> cudaVisibleDevices;
// CUDA_DEVICE_ORDER, when it is set: it decides which GPU a number in
// CUDA_VISIBLE_DEVICES means, by PCI address or by speed.
std::optional<std::string> cudaDeviceOrder;
};
// Pure path resolution: an env override wins, then a toolchain installed
// beside crafter-build (<crafterPrefix>/bin/clang++), then bare names on