Skip to content

[libc] Add loader option to force serial execution of GPU region #101601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions libc/utils/gpu/loader/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@
#include "llvm/BinaryFormat/Magic.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/WithColor.h"

#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sys/file.h>

using namespace llvm;

Expand Down Expand Up @@ -62,6 +67,12 @@ static cl::opt<bool>
cl::desc("Output resource usage of launched kernels"),
cl::init(false), cl::cat(loader_category));

static cl::opt<bool>
no_parallelism("no-parallelism",
cl::desc("Allows only a single process to use the GPU at a "
"time. Useful to suppress out-of-resource errors"),
cl::init(false), cl::cat(loader_category));

static cl::opt<std::string> file(cl::Positional, cl::Required,
cl::desc("<gpu executable>"),
cl::cat(loader_category));
Expand All @@ -75,6 +86,12 @@ static cl::list<std::string> args(cl::ConsumeAfter,
exit(EXIT_FAILURE);
}

std::string get_main_executable(const char *name) {
void *ptr = (void *)(intptr_t)&get_main_executable;
auto cow_path = sys::fs::getMainExecutable(name, ptr);
return sys::path::parent_path(cow_path).str();
}

int main(int argc, const char **argv, const char **envp) {
sys::PrintStackTraceOnErrorSignal(argv[0]);
cl::HideUnrelatedOptions(loader_category);
Expand All @@ -98,12 +115,28 @@ int main(int argc, const char **argv, const char **envp) {
llvm::transform(args, std::back_inserter(new_argv),
[](const std::string &arg) { return arg.c_str(); });

// Claim a file lock on the executable so only a single process can enter this
// region if requested. This prevents the loader from spurious failures.
int fd = -1;
if (no_parallelism) {
fd = open(get_main_executable(argv[0]).c_str(), O_RDONLY);
if (flock(fd, LOCK_EX) == -1)
report_error(createStringError("Failed to lock '%s': %s", argv[0],
strerror(errno)));
}

// Drop the loader from the program arguments.
LaunchParameters params{threads_x, threads_y, threads_z,
blocks_x, blocks_y, blocks_z};
int ret = load(new_argv.size(), new_argv.data(), envp,
const_cast<char *>(image.getBufferStart()),
image.getBufferSize(), params, print_resource_usage);

if (no_parallelism) {
if (flock(fd, LOCK_UN) == -1)
report_error(createStringError("Failed to unlock '%s': %s", argv[0],
strerror(errno)));
}

return ret;
}
Loading