Loading...
Searching...
No Matches
CutlassGemm.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#include "CutlassGemm.h"
6
7#ifdef GPR_HAVE_CUTLASS
8
9#include <dlfcn.h>
10
11#include <array>
12#include <filesystem>
13#include <gpulite/gpulite.hpp>
14#include <mutex>
15#include <stdexcept>
16#include <string>
17#include <utility>
18#include <vector>
19
20namespace gpr {
21namespace linalg {
22namespace cutlass {
23namespace {
24
25using CompiledFn = bool (*)() noexcept;
26using AvailableFn = bool (*)() noexcept;
27using GemmFn = int (*)(int, int, int, const double*, int, const double*, int,
28 double*, int, char*, std::size_t) noexcept;
29
30struct Plugin {
31 void* handle = nullptr;
32 CompiledFn compiled = nullptr;
33 AvailableFn available = nullptr;
34 GemmFn gemm_nn = nullptr;
35 std::string error;
36};
37
38std::filesystem::path libraryDirectory()
39{
40 Dl_info info{};
41 if (dladdr(reinterpret_cast<void*>(&libraryDirectory), &info) == 0 ||
42 info.dli_fname == nullptr) {
43 return {};
44 }
45 return std::filesystem::path(info.dli_fname).parent_path();
46}
47
48std::string& configuredPluginPath()
49{
50 static std::string path;
51 return path;
52}
53
54std::vector<std::string> pluginCandidates()
55{
56 std::vector<std::string> candidates;
57 if (!configuredPluginPath().empty()) {
58 candidates.emplace_back(configuredPluginPath());
59 }
60
61 const std::filesystem::path lib_dir = libraryDirectory();
62 if (!lib_dir.empty()) {
63 candidates.emplace_back((lib_dir / "libgpr_cutlass_gemm.so").string());
64 }
65 candidates.emplace_back("libgpr_cutlass_gemm.so");
66 return candidates;
67}
68
69template <typename Fn>
70Fn loadSymbol(void* handle, const char* name)
71{
72 void* sym = dlsym(handle, name);
73 if (sym == nullptr) {
74 throw std::runtime_error(std::string("missing symbol ") + name);
75 }
76 return reinterpret_cast<Fn>(sym);
77}
78
79Plugin loadPlugin()
80{
81 Plugin plugin;
82 std::vector<std::string> errors;
83
84 for (const std::string& candidate: pluginCandidates()) {
85 void* handle = dlopen(candidate.c_str(), RTLD_NOW | RTLD_LOCAL);
86 if (handle == nullptr) {
87 const char* err = dlerror();
88 errors.emplace_back(
89 candidate + ": " +
90 (err != nullptr ? err : "unknown dlopen error"));
91 continue;
92 }
93
94 try {
95 plugin.compiled =
96 loadSymbol<CompiledFn>(handle, "gpr_cutlass_compiled");
97 plugin.available =
98 loadSymbol<AvailableFn>(handle, "gpr_cutlass_available");
99 plugin.gemm_nn = loadSymbol<GemmFn>(handle, "gpr_cutlass_gemm_nn");
100 plugin.handle = handle;
101 return plugin;
102 } catch (const std::exception& exc) {
103 errors.emplace_back(candidate + ": " + exc.what());
104 dlclose(handle);
105 }
106 }
107
108 plugin.error = "CUTLASS plugin unavailable";
109 if (!errors.empty()) {
110 plugin.error += " (";
111 for (std::size_t i = 0; i < errors.size(); ++i) {
112 if (i > 0) plugin.error += "; ";
113 plugin.error += errors[i];
114 }
115 plugin.error += ")";
116 }
117 return plugin;
118}
119
120Plugin& plugin()
121{
122 static Plugin loaded = loadPlugin();
123 return loaded;
124}
125
126int cudaDeviceCount() noexcept
127{
128 if (!gpulite::CUDART::loaded()) return 0;
129 int device_count = 0;
130 if (gpulite::CUDART::instance().cudaGetDeviceCount(&device_count) !=
131 cudaSuccess) {
132 return 0;
133 }
134 return device_count;
135}
136
137bool cudaRuntimeAvailable() noexcept
138{
139 return cudaDeviceCount() > 0;
140}
141
142} // namespace
143
144void setPluginPath(std::string path)
145{
146 configuredPluginPath() = std::move(path);
147}
148
149bool compiled() noexcept
150{
151 if (!cudaRuntimeAvailable()) return false;
152 Plugin& p = plugin();
153 return p.compiled != nullptr && p.compiled();
154}
155
156bool available() noexcept
157{
158 return availableForLocalRank(0, 1);
159}
160
161bool availableForLocalRank(int local_rank, int local_nprocs) noexcept
162{
163 if (local_rank < 0 || local_nprocs < 1) return false;
164 const int device_count = cudaDeviceCount();
165 if (device_count <= 0 || local_nprocs > device_count) return false;
166 if (gpulite::CUDART::instance().cudaSetDevice(local_rank % device_count) !=
167 cudaSuccess) {
168 return false;
169 }
170 Plugin& p = plugin();
171 return p.available != nullptr && p.available();
172}
173
174EigenMatrix gemm_nn(const EigenMatrix& A, const EigenMatrix& B)
175{
176 if (A.cols() != B.rows()) {
177 throw std::invalid_argument(
178 "CUTLASS GEMM dimension mismatch: A.cols() != B.rows()");
179 }
180
181 const int m = static_cast<int>(A.rows());
182 const int n = static_cast<int>(B.cols());
183 const int k = static_cast<int>(A.cols());
184 EigenMatrix C = EigenMatrix::Zero(m, n);
185 if (m == 0 || n == 0 || k == 0) return C;
186
187 if (!cudaRuntimeAvailable()) {
188 throw std::runtime_error(
189 "CUTLASS GEMM requested without a CUDA runtime/device");
190 }
191 Plugin& p = plugin();
192 if (p.gemm_nn == nullptr) {
193 throw std::runtime_error(p.error);
194 }
195
196 std::array<char, 1024> error{};
197 const int status = p.gemm_nn(
198 m, n, k, A.data(), static_cast<int>(A.outerStride()), B.data(),
199 static_cast<int>(B.outerStride()), C.data(),
200 static_cast<int>(C.outerStride()), error.data(), error.size());
201 if (status != 0) {
202 throw std::runtime_error(error.data());
203 }
204 return C;
205}
206
207} // namespace cutlass
208} // namespace linalg
209} // namespace gpr
210
211#endif // GPR_HAVE_CUTLASS
Optional CUTLASS GEMM facade for rank-local dense products.
int status
bool available() noexcept
Definition CutlassGemm.h:43
bool availableForLocalRank(int local_rank, int local_nprocs) noexcept
Definition CutlassGemm.h:53
EigenMatrix gemm_nn(const EigenMatrix &A, const EigenMatrix &B)
Definition CutlassGemm.h:61
bool compiled() noexcept
Definition CutlassGemm.h:38
void setPluginPath(std::string path)
Definition CutlassGemm.h:48
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20