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

@ -302,6 +302,65 @@ int main() {
Check(ResolveToolchain(in).nvcc.empty(), "a link to itself ends, and finds nothing");
fs::remove_all(host);
}
{
// What VariantId keys a CUDA configuration's nvcc by: its canonical
// path, size and time, which an nvcc replaced at its path changes,
// as it does not change the path.
const fs::path toolkit = scratch / "stamped";
Program(toolkit / "bin/nvcc");
ToolchainInputs in;
in.env["CUDA_PATH"] = toolkit.string();
const std::string before = ResolveToolchain(in).nvccStamp;
Check(before.starts_with(std::format("{}|", fs::canonical(toolkit / "bin/nvcc").string())), std::format("the stamp names the nvcc where its links lead (got '{}')", before));
std::ofstream(toolkit / "bin/nvcc", std::ios::app) << "13.5";
Check(ResolveToolchain(in).nvccStamp != before, "an nvcc replaced at its path stamps differently");
Check(ResolveToolchain({}).nvccStamp.empty(), "no nvcc, no stamp");
}
{
// What VariantId keys -arch=native by: the NVIDIA display devices the
// PCI bus lists, by slot and model, and CUDA_VISIBLE_DEVICES and
// CUDA_DEVICE_ORDER. Another GPU, the same GPUs in each other's slots,
// or another choice or numbering among them keys it differently; a
// GPU's audio function, another NVIDIA class, and another vendor's GPU
// do not count.
const fs::path toolkit = scratch / "native/toolkit";
Program(toolkit / "bin/nvcc");
const fs::path pci = scratch / "native/pci";
const auto device = [&pci](std::string_view address, std::string_view vendor, std::string_view id, std::string_view type) {
fs::create_directories(pci / address);
std::ofstream(pci / address / "vendor") << vendor << "\n";
std::ofstream(pci / address / "device") << id << "\n";
std::ofstream(pci / address / "class") << type << "\n";
};
device("0000:34:00.0", "0x10de", "0x2684", "0x030000");
device("0000:34:00.1", "0x10de", "0x22ba", "0x040300");
device("0000:00:02.0", "0x8086", "0xa780", "0x030000");
ToolchainInputs in;
in.env["CUDA_PATH"] = toolkit.string();
in.pciDevices = pci;
const std::string one = ResolveToolchain(in).cudaNativeKey;
Check(one.contains("0x2684") && !one.contains("0x22ba") && !one.contains("0xa780"), std::format("native is keyed by the NVIDIA display devices alone (got '{}')", one));
device("0000:ca:00.0", "0x10de", "0x2b85", "0x030200");
const std::string two = ResolveToolchain(in).cudaNativeKey;
Check(two != one && two.contains("0x2b85"), std::format("another GPU keys it differently (got '{}')", two));
in.cudaVisibleDevices = "1";
const std::string chosen = ResolveToolchain(in).cudaNativeKey;
Check(chosen != two, "and so does CUDA_VISIBLE_DEVICES");
in.cudaVisibleDevices = "";
const std::string hidden = ResolveToolchain(in).cudaNativeKey;
Check(hidden != two && hidden != chosen, std::format("an empty CUDA_VISIBLE_DEVICES, which hides every GPU, keys apart from an unset one (got '{}')", hidden));
in.cudaVisibleDevices.reset();
Check(ResolveToolchain(in).cudaNativeKey == two, "unset again, the key is as it was");
in.cudaDeviceOrder = "PCI_BUS_ID";
Check(ResolveToolchain(in).cudaNativeKey != two, "CUDA_DEVICE_ORDER, which numbers the GPUs, keys it differently");
in.cudaDeviceOrder.reset();
device("0000:34:00.0", "0x10de", "0x2b85", "0x030200");
device("0000:ca:00.0", "0x10de", "0x2684", "0x030000");
const std::string swapped = ResolveToolchain(in).cudaNativeKey;
Check(swapped != two, std::format("so do the same GPUs in each other's slots (got '{}')", swapped));
in.env.erase("CUDA_PATH");
Check(ResolveToolchain(in).cudaNativeKey.empty(), "no nvcc, no key");
}
{
// The cache key separates toolchains that would build different
// PCMs and nothing else.