Loading...
Searching...
No Matches
EnvironmentGpuExecutor.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
4
5#include <dlfcn.h>
6
7#include <limits>
8#include <mutex>
9#include <stdexcept>
10
12
13namespace gpr::linalg {
14namespace {
15std::mutex default_mutex;
16std::shared_ptr<const EnvironmentGpuExecutor> default_executor;
17} // namespace
18
22 void* library = nullptr;
23 void* workspace = nullptr;
24 Destroy destroy = nullptr;
25 Assemble assemble = nullptr;
26
28 {
30 if (library) dlclose(library);
31 }
32};
33
35 : implementation_(std::make_unique<Impl>())
36{
37 auto& state = *implementation_;
38 // Device libraries bind private runtime symbols at link time, including
39 // absent optional hooks. Ordinary loading preserves driver instrumentation.
40 // The boundary carries plain data and opaque handles; C++ allocation and
41 // exceptions stay in their owner.
42 state.library = dlopen(library.c_str(), RTLD_NOW | RTLD_LOCAL);
43 if (!state.library) throw std::runtime_error(dlerror());
44 const auto create =
45 reinterpret_cast<decltype(&gpr_stdpar_environment_create_v1)>(
46 dlsym(state.library, "gpr_stdpar_environment_create_v1"));
47 state.destroy = reinterpret_cast<Impl::Destroy>(
48 dlsym(state.library, "gpr_stdpar_environment_destroy_v1"));
49 state.assemble = reinterpret_cast<Impl::Assemble>(
50 dlsym(state.library, "gpr_stdpar_environment_blocks_v1"));
51 if (!create || !state.destroy || !state.assemble)
52 throw std::runtime_error("incompatible environment GPU library");
53 char error[1024]{};
54 state.workspace = create(error, sizeof(error));
55 if (!state.workspace) throw std::runtime_error(error);
56}
57
59
61 const gpr::detail::EnvironmentPairBatch& batch,
62 std::span<double> output) const
63{
64 if (batch.channel_count <= 0 || batch.left.dimensions <= 0 ||
65 batch.right.dimensions <= 0)
66 throw std::invalid_argument("invalid environment output dimensions");
67 std::size_t required = batch.pair_count;
68 for (const std::size_t extent: {std::size_t(batch.channel_count),
69 std::size_t(batch.left.dimensions) + 1,
70 std::size_t(batch.right.dimensions) + 1}) {
71 if (required > std::numeric_limits<std::size_t>::max() / extent)
72 throw std::length_error("environment output dimensions overflow");
73 required *= extent;
74 }
75 if (output.size() != required)
76 throw std::invalid_argument("environment output size differs");
77 char error[1024]{};
78 if (implementation_->assemble(implementation_->workspace, &batch,
79 output.data(), error, sizeof(error)))
80 throw std::runtime_error(error);
81}
82
83void configureEnvironmentGpu(const std::string& library)
84{
85 auto executor =
86 library.empty()
87 ? nullptr
88 : std::make_shared<const EnvironmentGpuExecutor>(library);
89 std::lock_guard guard(default_mutex);
90 default_executor = std::move(executor);
91}
92
93std::shared_ptr<const EnvironmentGpuExecutor> configuredEnvironmentGpu()
94{
95 std::lock_guard guard(default_mutex);
96 return default_executor;
97}
98} // namespace gpr::linalg
void gpr_stdpar_environment_destroy_v1(void *workspace) noexcept
int gpr_stdpar_environment_blocks_v1(void *workspace, const gpr::detail::EnvironmentPairBatch *batch, double *blocks, char *error, std::size_t capacity) noexcept
void * gpr_stdpar_environment_create_v1(char *error, std::size_t capacity) noexcept
EnvironmentGpuExecutor(const std::string &library)
void assemble(const gpr::detail::EnvironmentPairBatch &batch, std::span< double > output) const
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
std::shared_ptr< const EnvironmentGpuExecutor > configuredEnvironmentGpu()
void configureEnvironmentGpu(const std::string &library)
STL namespace.
decltype(&gpr_stdpar_environment_blocks_v1) Assemble
decltype(&gpr_stdpar_environment_destroy_v1) Destroy