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

@ -309,6 +309,45 @@ Toolchain Crafter::ResolveToolchain(const ToolchainInputs& in) {
tc.nvccError = std::format("no CUDA toolkit found: looked for {}; set CUDA_PATH to the directory a toolkit is installed in", places);
}
}
// What VariantId keys a CUDA configuration's nvcc by. Its path stays
// when the toolkit behind it is updated, by a package at /opt/cuda or by
// /usr/local/cuda pointed at another release, and the objects the old
// nvcc compiled would be kept. The binary's canonical path, size and
// time change with it, and reading them starts no process.
if (!tc.nvcc.empty()) {
std::error_code ec;
fs::path real = fs::canonical(tc.nvcc, ec);
if (ec) real = tc.nvcc;
const std::uintmax_t size = fs::file_size(real, ec);
const auto time = fs::last_write_time(real, ec).time_since_epoch().count();
tc.nvccStamp = std::format("{}|{}|{}", real.string(), size, time);
}
// What VariantId keys -arch=native by: nvcc compiles it for the GPUs it
// finds, so a GPU of another model, the same GPUs in each other's slots,
// or CUDA_VISIBLE_DEVICES and CUDA_DEVICE_ORDER choosing another or,
// empty, none, has to build in a directory of its own, where the word
// alone would keep an object compiled for the old GPU, with no PTX for the
// new one. The NVIDIA display devices (class 0x03) by PCI address and
// device ID, in the order of their addresses, from sysfs, which starts no
// process; an entry that cannot be read counts as none, and nothing here
// throws. It is crafter-build's reading of the machine, not nvcc's: a
// change it cannot see, such as a GPU bound to another driver, keeps the
// key.
if (!tc.nvcc.empty()) {
std::vector<std::string> gpus;
std::error_code ec;
for (fs::directory_iterator it(in.pciDevices, ec), end; !ec && it != end; it.increment(ec)) {
const auto field = [&it](std::string_view name) {
std::string value;
std::ifstream(it->path() / name) >> value;
return value;
};
if (field("vendor") == "0x10de" && field("class").starts_with("0x03")) gpus.push_back(std::format("{}={}", it->path().filename().string(), field("device")));
}
std::ranges::sort(gpus);
const auto spelled = [](const std::optional<std::string>& value) { return value ? std::format("={}", *value) : std::string("unset"); };
tc.cudaNativeKey = std::format("{}|{}|{}", std::views::join_with(gpus, std::string(",")) | std::ranges::to<std::string>(), spelled(in.cudaVisibleDevices), spelled(in.cudaDeviceOrder));
}
std::tie(tc.flatpakArch, tc.flatpakBranch) = FlatpakRuntimeOf(in.flatpakInfo);
return tc;
}
@ -328,6 +367,9 @@ const Toolchain& Crafter::GetToolchain() {
} catch (const std::exception&) {}
if (fs::exists("/.flatpak-info")) in.hostRoot = "/run/host";
in.cudaRoots = { "/opt/cuda", "/usr/local/cuda" };
in.pciDevices = "/sys/bus/pci/devices";
if (const char* value = std::getenv("CUDA_VISIBLE_DEVICES")) in.cudaVisibleDevices = value;
if (const char* value = std::getenv("CUDA_DEVICE_ORDER")) in.cudaDeviceOrder = value;
{
auto it = in.env.find("CRAFTER_BUILD_FLATPAK_INFO");
const fs::path info = it != in.env.end() ? fs::path(it->second) : fs::path("/.flatpak-info");