13#include <gpulite/gpulite.hpp>
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;
31 void* handle =
nullptr;
38std::filesystem::path libraryDirectory()
41 if (dladdr(
reinterpret_cast<void*
>(&libraryDirectory), &info) == 0 ||
42 info.dli_fname ==
nullptr) {
45 return std::filesystem::path(info.dli_fname).parent_path();
48std::string& configuredPluginPath()
50 static std::string path;
54std::vector<std::string> pluginCandidates()
56 std::vector<std::string> candidates;
57 if (!configuredPluginPath().empty()) {
58 candidates.emplace_back(configuredPluginPath());
61 const std::filesystem::path lib_dir = libraryDirectory();
62 if (!lib_dir.empty()) {
63 candidates.emplace_back((lib_dir /
"libgpr_cutlass_gemm.so").
string());
65 candidates.emplace_back(
"libgpr_cutlass_gemm.so");
70Fn loadSymbol(
void* handle,
const char* name)
72 void* sym = dlsym(handle, name);
74 throw std::runtime_error(std::string(
"missing symbol ") + name);
76 return reinterpret_cast<Fn
>(sym);
82 std::vector<std::string> errors;
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();
90 (err !=
nullptr ? err :
"unknown dlopen error"));
96 loadSymbol<CompiledFn>(handle,
"gpr_cutlass_compiled");
98 loadSymbol<AvailableFn>(handle,
"gpr_cutlass_available");
99 plugin.gemm_nn = loadSymbol<GemmFn>(handle,
"gpr_cutlass_gemm_nn");
100 plugin.handle = handle;
102 }
catch (
const std::exception& exc) {
103 errors.emplace_back(candidate +
": " + exc.what());
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];
122 static Plugin loaded = loadPlugin();
126int cudaDeviceCount() noexcept
128 if (!gpulite::CUDART::loaded())
return 0;
129 int device_count = 0;
130 if (gpulite::CUDART::instance().cudaGetDeviceCount(&device_count) !=
137bool cudaRuntimeAvailable() noexcept
139 return cudaDeviceCount() > 0;
146 configuredPluginPath() = std::move(path);
151 if (!cudaRuntimeAvailable())
return false;
152 Plugin& p = plugin();
153 return p.compiled !=
nullptr && p.compiled();
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) !=
170 Plugin& p = plugin();
171 return p.available !=
nullptr && p.available();
176 if (A.cols() != B.rows()) {
177 throw std::invalid_argument(
178 "CUTLASS GEMM dimension mismatch: A.cols() != B.rows()");
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());
185 if (m == 0 || n == 0 || k == 0)
return C;
187 if (!cudaRuntimeAvailable()) {
188 throw std::runtime_error(
189 "CUTLASS GEMM requested without a CUDA runtime/device");
191 Plugin& p = plugin();
192 if (p.gemm_nn ==
nullptr) {
193 throw std::runtime_error(p.error);
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());
202 throw std::runtime_error(error.data());
Optional CUTLASS GEMM facade for rank-local dense products.
bool available() noexcept
bool availableForLocalRank(int local_rank, int local_nprocs) noexcept
EigenMatrix gemm_nn(const EigenMatrix &A, const EigenMatrix &B)
void setPluginPath(std::string path)
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.
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix