GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
Loading...
Searching...
No Matches
EnvironmentGpuExecutor.cpp
Go to the documentation of this file.
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) gpridge developers
3
#include "
EnvironmentGpuExecutor.h
"
4
5
#include <dlfcn.h>
6
7
#include <limits>
8
#include <mutex>
9
#include <stdexcept>
10
11
#include "
../covariance_functions/EnvironmentGpuApi.h
"
12
13
namespace
gpr::linalg
{
14
namespace
{
15
std::mutex default_mutex;
16
std::shared_ptr<const EnvironmentGpuExecutor> default_executor;
17
}
// namespace
18
19
struct
EnvironmentGpuExecutor::Impl
{
20
using
Destroy
=
decltype
(&
gpr_stdpar_environment_destroy_v1
);
21
using
Assemble
=
decltype
(&
gpr_stdpar_environment_blocks_v1
);
22
void
*
library
=
nullptr
;
23
void
*
workspace
=
nullptr
;
24
Destroy
destroy
=
nullptr
;
25
Assemble
assemble
=
nullptr
;
26
27
~Impl
()
28
{
29
if
(
workspace
)
destroy
(
workspace
);
30
if
(
library
) dlclose(
library
);
31
}
32
};
33
34
EnvironmentGpuExecutor::EnvironmentGpuExecutor
(
const
std::string& library)
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
58
EnvironmentGpuExecutor::~EnvironmentGpuExecutor
() =
default
;
59
60
void
EnvironmentGpuExecutor::assemble
(
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
83
void
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
93
std::shared_ptr<const EnvironmentGpuExecutor>
configuredEnvironmentGpu
()
94
{
95
std::lock_guard guard(default_mutex);
96
return
default_executor;
97
}
98
}
// namespace gpr::linalg
EnvironmentGpuApi.h
gpr_stdpar_environment_destroy_v1
void gpr_stdpar_environment_destroy_v1(void *workspace) noexcept
Definition
EnvironmentStdparGpu.cpp:182
gpr_stdpar_environment_blocks_v1
int gpr_stdpar_environment_blocks_v1(void *workspace, const gpr::detail::EnvironmentPairBatch *batch, double *blocks, char *error, std::size_t capacity) noexcept
Definition
EnvironmentStdparGpu.cpp:187
gpr_stdpar_environment_create_v1
void * gpr_stdpar_environment_create_v1(char *error, std::size_t capacity) noexcept
Definition
EnvironmentStdparGpu.cpp:169
EnvironmentGpuExecutor.h
gpr::linalg::EnvironmentGpuExecutor::EnvironmentGpuExecutor
EnvironmentGpuExecutor(const std::string &library)
Definition
EnvironmentGpuExecutor.cpp:34
gpr::linalg::EnvironmentGpuExecutor::assemble
void assemble(const gpr::detail::EnvironmentPairBatch &batch, std::span< double > output) const
Definition
EnvironmentGpuExecutor.cpp:60
gpr::linalg::EnvironmentGpuExecutor::~EnvironmentGpuExecutor
~EnvironmentGpuExecutor()
gpr::linalg
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
Definition
ConditionedKernel.h:14
gpr::linalg::configuredEnvironmentGpu
std::shared_ptr< const EnvironmentGpuExecutor > configuredEnvironmentGpu()
Definition
EnvironmentGpuExecutor.cpp:93
gpr::linalg::configureEnvironmentGpu
void configureEnvironmentGpu(const std::string &library)
Definition
EnvironmentGpuExecutor.cpp:83
std
STL namespace.
gpr::linalg::EnvironmentGpuExecutor::Impl
Definition
EnvironmentGpuExecutor.cpp:19
gpr::linalg::EnvironmentGpuExecutor::Impl::Assemble
decltype(&gpr_stdpar_environment_blocks_v1) Assemble
Definition
EnvironmentGpuExecutor.cpp:21
gpr::linalg::EnvironmentGpuExecutor::Impl::library
void * library
Definition
EnvironmentGpuExecutor.cpp:22
gpr::linalg::EnvironmentGpuExecutor::Impl::workspace
void * workspace
Definition
EnvironmentGpuExecutor.cpp:23
gpr::linalg::EnvironmentGpuExecutor::Impl::Destroy
decltype(&gpr_stdpar_environment_destroy_v1) Destroy
Definition
EnvironmentGpuExecutor.cpp:20
gpr::linalg::EnvironmentGpuExecutor::Impl::assemble
Assemble assemble
Definition
EnvironmentGpuExecutor.cpp:25
gpr::linalg::EnvironmentGpuExecutor::Impl::~Impl
~Impl()
Definition
EnvironmentGpuExecutor.cpp:27
gpr::linalg::EnvironmentGpuExecutor::Impl::destroy
Destroy destroy
Definition
EnvironmentGpuExecutor.cpp:24
gpr
linalg
EnvironmentGpuExecutor.cpp
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf