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:
parent
2fd45133a0
commit
967dd12b06
10 changed files with 262 additions and 15 deletions
|
|
@ -1145,7 +1145,8 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
|
|||
// Still .cu-mtime only: the header tracking the C and C++ paths get
|
||||
// comes from asking the compiler for a depfile, and nvcc's -MD/-MF are
|
||||
// untested here (no CUDA toolkit in CI), so a .cu that only sees a
|
||||
// changed header does not rebuild yet.
|
||||
// changed header does not rebuild yet. cfg.cudaArch needs no check:
|
||||
// it is part of VariantId, so another one builds in another directory.
|
||||
if (!fs::exists(objPath) || (fs::exists(srcPath) && fs::last_write_time(srcPath) > fs::last_write_time(objPath))) {
|
||||
const Toolchain& toolchain = GetToolchain();
|
||||
if (toolchain.nvcc.empty()) {
|
||||
|
|
@ -1155,11 +1156,11 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
|
|||
}
|
||||
break;
|
||||
}
|
||||
threads.emplace_back([&cFile, &buildError, &buildCancelled, &toolchain, srcPath, objPath]() {
|
||||
threads.emplace_back([&cFile, &buildError, &buildCancelled, &config, &toolchain, srcPath, objPath]() {
|
||||
Progress::Task task(std::format("Compiling {}.cu", cFile.filename().string()));
|
||||
if (buildCancelled.load(std::memory_order_relaxed)) return;
|
||||
|
||||
std::string result = RunBuildCommand(std::format("{} {} -c -o {} -O3 -arch=sm_89", ShellQuote(toolchain.nvcc.string()), ShellQuote(srcPath), ShellQuote(objPath)));
|
||||
std::string result = RunBuildCommand(std::format("{} {} -c -o {} -O3 -arch={}", ShellQuote(toolchain.nvcc.string()), ShellQuote(srcPath), ShellQuote(objPath), ShellQuote(config.cudaArch)));
|
||||
if (result.empty()) return;
|
||||
|
||||
bool expected = false;
|
||||
|
|
@ -1739,6 +1740,21 @@ std::string Crafter::InstallFlatpakApp(const Configuration& config) {
|
|||
return "";
|
||||
}
|
||||
|
||||
namespace {
|
||||
// CUDA sources compiled for native, code for the GPUs of the machine
|
||||
// that builds and no PTX, in `config`'s program: its own, and those of
|
||||
// every library Build hands its link, which it does through an
|
||||
// executable dependency too. That executable's own objects are run by
|
||||
// path and stay out of the bundle; `linked` is false for them alone,
|
||||
// and it follows from a configuration's type, so `seen` holds either
|
||||
// way.
|
||||
bool HasNativeCuda(const Configuration& config, std::unordered_set<const Configuration*>& seen, bool linked = true) {
|
||||
if (!seen.insert(&config).second) return false;
|
||||
if (linked && !config.cuda.empty() && config.cudaArch == "native") return true;
|
||||
return std::ranges::any_of(config.dependencies, [&seen](const Configuration* dep) { return HasNativeCuda(*dep, seen, dep->type != ConfigurationType::Executable); });
|
||||
}
|
||||
}
|
||||
|
||||
std::string Crafter::BundleFlatpakApp(const Configuration& config) {
|
||||
if (const std::string refused = CannotExport(config, "bundle"); !refused.empty()) return refused;
|
||||
const FlatpakExport app = ExportOf(config);
|
||||
|
|
@ -1753,6 +1769,9 @@ std::string Crafter::BundleFlatpakApp(const Configuration& config) {
|
|||
if (config.march == "native") {
|
||||
Progress::Diagnostic(std::format("{} is built with -march=native, so it may stop with SIGILL on a CPU older than this one; for others, build with --march=x86-64-v3", bundle.string()));
|
||||
}
|
||||
if (std::unordered_set<const Configuration*> seen; HasNativeCuda(config, seen)) {
|
||||
Progress::Diagnostic(std::format("{} holds CUDA code built with --cuda-arch=native, for this machine's GPUs and with no PTX, so it may not run on another machine's GPU; for others, build with --cuda-arch=all-major", bundle.string()));
|
||||
}
|
||||
if (Progress::GetVerbosity() != Progress::Verbosity::Quiet) std::println("\nBundled {} as {}: `flatpak install --user {}` installs it.", app.appId, bundle.string(), bundle.filename().string());
|
||||
return "";
|
||||
}
|
||||
|
|
@ -2000,6 +2019,9 @@ ArgQuery Crafter::ApplyStandardArgs(Configuration& cfg, std::span<const std::str
|
|||
if (const char* envMtune = std::getenv("CRAFTER_BUILD_MTUNE"); envMtune && *envMtune) {
|
||||
cfg.mtune = envMtune;
|
||||
}
|
||||
if (const char* envCudaArch = std::getenv("CRAFTER_BUILD_CUDA_ARCH"); envCudaArch && *envCudaArch) {
|
||||
cfg.cudaArch = envCudaArch;
|
||||
}
|
||||
bool sawLib = false;
|
||||
bool sawShared = false;
|
||||
for (std::string_view a : args) {
|
||||
|
|
@ -2009,6 +2031,7 @@ ArgQuery Crafter::ApplyStandardArgs(Configuration& cfg, std::span<const std::str
|
|||
else if (a.starts_with("--target=")) cfg.target = std::string(a.substr(std::string_view("--target=").size()));
|
||||
else if (a.starts_with("--march=")) cfg.march = std::string(a.substr(std::string_view("--march=").size()));
|
||||
else if (a.starts_with("--mtune=")) cfg.mtune = std::string(a.substr(std::string_view("--mtune=").size()));
|
||||
else if (a.starts_with("--cuda-arch=")) cfg.cudaArch = std::string(a.substr(std::string_view("--cuda-arch=").size()));
|
||||
else if (a.starts_with("--sysroot=")) cfg.sysroot = std::string(a.substr(std::string_view("--sysroot=").size()));
|
||||
else if (a.starts_with("--packaging=")) {
|
||||
const std::string_view kind = a.substr(std::string_view("--packaging=").size());
|
||||
|
|
@ -2210,14 +2233,17 @@ Project args:
|
|||
Any flag not consumed above is forwarded verbatim to CrafterBuildProject as
|
||||
part of its `args` span. Project-specific flags (e.g. --target=, custom
|
||||
feature toggles) live there. ApplyStandardArgs reads --debug, --target=,
|
||||
--march=, --mtune=, --sysroot=, --lib, --shared and --packaging=none|flatpak,
|
||||
the last for the project crafter-build was pointed at only. Flags it does not
|
||||
itself interpret are folded into the variant hash, so switching one lands in
|
||||
its own bin/ and build/ directory instead of overwriting the other setting's.
|
||||
--march=, --mtune=, --cuda-arch=, --sysroot=, --lib, --shared and
|
||||
--packaging=none|flatpak, the last for the project crafter-build was pointed
|
||||
at only. Flags it does not itself interpret are folded into the variant
|
||||
hash, so switching one lands in its own bin/ and build/ directory instead of
|
||||
overwriting the other setting's.
|
||||
|
||||
Environment:
|
||||
CRAFTER_BUILD_MARCH Override -march (default: native).
|
||||
CRAFTER_BUILD_MTUNE Override -mtune (default: native).
|
||||
CRAFTER_BUILD_CUDA_ARCH Override nvcc's -arch for cfg.cuda
|
||||
(default: native).
|
||||
CUDA_PATH CUDA toolkit whose bin/nvcc compiles
|
||||
cfg.cuda. Default: /opt/cuda, then
|
||||
/usr/local/cuda (in the Flatpak, the
|
||||
|
|
|
|||
Loading…
Reference in a new issue