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
|
||||
|
|
|
|||
|
|
@ -482,13 +482,14 @@ namespace {
|
|||
// compiles project.so with, so the dependency's std/Crafter.Build PCMs
|
||||
// land in the cache dir project.so is built against. Recursive, because
|
||||
// the dependency's project.cpp is loaded with no arguments and so passes
|
||||
// none on, while its own deps still read CRAFTER_BUILD_MARCH from the
|
||||
// environment.
|
||||
// none on, while its own deps still read CRAFTER_BUILD_MARCH and
|
||||
// CRAFTER_BUILD_CUDA_ARCH from the environment.
|
||||
void ForceHostTooling(Configuration& cfg, const Configuration& host, std::unordered_set<Configuration*>& seen) {
|
||||
if (!seen.insert(&cfg).second) return;
|
||||
cfg.target = host.target;
|
||||
cfg.march = host.march;
|
||||
cfg.mtune = host.mtune;
|
||||
cfg.cudaArch = host.cudaArch;
|
||||
cfg.debug = false;
|
||||
// project.so is a shared object, so the archive it links has to be
|
||||
// position-independent; static libraries aren't by default. Part of
|
||||
|
|
|
|||
|
|
@ -370,6 +370,7 @@ TestBuilder Configuration::AddTest(std::string_view name, std::span<fs::path> in
|
|||
t.config.mtune = this->mtune;
|
||||
t.config.sysroot = this->sysroot;
|
||||
t.config.debug = this->debug;
|
||||
t.config.cudaArch = this->cudaArch;
|
||||
// Inherited so a project flag that changes what the library contains also
|
||||
// moves the test's own outputs, rather than letting two flag settings share
|
||||
// one test bin dir.
|
||||
|
|
@ -398,6 +399,7 @@ void Configuration::AddMarchVariants(std::string_view name, std::span<fs::path>
|
|||
t.config.mtune = tier.mtune;
|
||||
t.config.sysroot = this->sysroot;
|
||||
t.config.debug = this->debug;
|
||||
t.config.cudaArch = this->cudaArch;
|
||||
t.config.projectArgs = this->projectArgs;
|
||||
t.config.type = ConfigurationType::Executable;
|
||||
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
Loading…
Reference in a new issue