19struct ProductLibrary {
20 void* library =
nullptr;
21 void* workspace =
nullptr;
28 if (workspace) destroy(workspace);
29 if (library) dlclose(library);
32std::mutex configuration;
33std::shared_ptr<ProductLibrary> configured;
34std::shared_ptr<const schemeii::Context> configured_scheme;
36std::shared_ptr<ProductLibrary> current()
38 std::lock_guard lock(configuration);
42int linalgThreadSource()
50 std::lock_guard lock(configuration);
51 return configured_scheme;
56 std::shared_ptr<ProductLibrary> replacement;
57 std::shared_ptr<schemeii::Context> scheme;
60 scheme = std::make_shared<schemeii::Context>(
62 scheme->setThreadSource(&linalgThreadSource);
63 std::fprintf(stderr,
"[training-product] scheme2 %s\n",
64 scheme->describe(256).c_str());
65 }
else if (!library.empty()) {
66 replacement = std::make_shared<ProductLibrary>();
67 auto& state = *replacement;
68 state.library = dlopen(library.c_str(), RTLD_NOW | RTLD_LOCAL);
69 if (!state.library)
throw std::runtime_error(dlerror());
72 dlsym(state.library,
"gpr_training_product_create_v1"));
75 dlsym(state.library,
"gpr_training_product_destroy_v1"));
77 dlsym(state.library,
"gpr_training_product_gemm_v1"));
78 if (!create || !state.destroy || !state.gemm)
79 throw std::runtime_error(
"incompatible training product library");
81 state.workspace = create(error,
sizeof(error));
82 if (!state.workspace)
throw std::runtime_error(error);
84 std::lock_guard lock(configuration);
85 configured = std::move(replacement);
86 configured_scheme = std::move(scheme);
91 std::lock_guard lock(configuration);
92 return configured !=
nullptr || configured_scheme !=
nullptr;
97 return current() !=
nullptr;
102 double alpha,
double beta,
bool partitioned)
104 const auto state = current();
106 if (!state && !scheme)
return false;
107 const Eigen::Index m = transpose_a ? a.cols() : a.rows();
108 const Eigen::Index k = transpose_a ? a.rows() : a.cols();
109 const Eigen::Index n = transpose_b ? b.rows() : b.cols();
110 if (k != (transpose_b ? b.cols() : b.rows()) || c.rows() != m ||
112 throw std::invalid_argument(
"invalid training product dimensions");
115 if (std::min({m, n, k}) == 0 || (!partitioned && std::min({m, n, k}) < 128))
118 scheme->gemm(transpose_a, transpose_b, m, n, k, alpha, a.data(),
119 a.outerStride(), b.data(), b.outerStride(), beta, c.data(),
123 std::lock_guard lock(state->execution);
125 if (state->gemm(state->workspace, transpose_a, transpose_b, m, n, k, alpha,
126 a.data(), a.outerStride(), b.data(), b.outerStride(), beta,
127 c.data(), c.outerStride(), error,
sizeof(error)))
128 throw std::runtime_error(error);
133 bool transpose_a,
bool transpose_b)
135 const Eigen::Index m = transpose_a ? a.cols() : a.rows();
136 const Eigen::Index k = transpose_a ? a.rows() : a.cols();
137 const Eigen::Index n = transpose_b ? b.rows() : b.cols();
138 if (k != (transpose_b ? b.cols() : b.rows()))
139 throw std::invalid_argument(
"invalid training product dimensions");
140 Eigen::MatrixXd c(m, n);
142 if (transpose_a && transpose_b)
143 c.noalias() = a.transpose() * b.transpose();
144 else if (transpose_a)
145 c.noalias() = a.transpose() * b;
146 else if (transpose_b)
147 c.noalias() = a * b.transpose();
void * gpr_training_product_create_v1(char *error, size_t capacity)
int gpr_training_product_gemm_v1(void *workspace, int transpose_a, int transpose_b, int64_t m, int64_t n, int64_t k, double alpha, const double *a, int64_t lda, const double *b, int64_t ldb, double beta, double *c, int64_t ldc, char *error, size_t capacity)
void gpr_training_product_destroy_v1(void *workspace)
bool isSpec(std::string_view spec) noexcept
Options parseOptions(std::string_view spec)
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
Eigen::Ref< const Eigen::MatrixXd, 0, Eigen::OuterStride<> > ConstProductMatrix
Eigen::MatrixXd trainingProduct(ConstProductMatrix a, ConstProductMatrix b, bool transpose_a, bool transpose_b)
Allocating form with a native Eigen product when no device product runs.
Eigen::Ref< Eigen::MatrixXd, 0, Eigen::OuterStride<> > ProductMatrix
bool trainingProductLibraryLoaded()
A device library (not an in-process engine) is configured.
void configureTrainingProductLibrary(const std::string &library)
bool tryTrainingProduct(ConstProductMatrix a, ConstProductMatrix b, ProductMatrix c, bool transpose_a, bool transpose_b, double alpha, double beta, bool partitioned)
int configuredLinalgThreads() noexcept
Requested dense numerical budget, independent of nested BLAS controls.
std::shared_ptr< const schemeii::Context > schemeIIContext()
bool trainingProductConfigured()
A device library or an in-process engine is configured.