22#if defined(USE_OPENMP)
32void* kernelFloatLibrary()
34 static void*
const handle = []() ->
void* {
35 const char* path = std::getenv(
"GPR_KERNEL_FLOAT_LIB");
36 if (path ==
nullptr || path[0] ==
'\0')
37 path =
"libscheme_ii_product.so";
38 void* library = dlopen(path, RTLD_NOW | RTLD_LOCAL);
39 if (library ==
nullptr) {
40 std::fprintf(stderr,
"GPR_PREDICT_SUM=kernel_float: %s\n",
48template <
class Launch>
49Launch kernelFloatEntry(
const char* name)
51 void*
const library = kernelFloatLibrary();
52 if (library ==
nullptr)
53 throw std::runtime_error(
54 "GPR_PREDICT_SUM=kernel_float: the device product library is "
55 "not loadable on this host");
56 const auto entry =
reinterpret_cast<Launch
>(dlsym(library, name));
57 if (entry ==
nullptr) {
58 const char* reason = dlerror();
59 throw std::runtime_error(std::string(
"GPR_PREDICT_SUM=kernel_float: ") +
60 (reason ? reason : name));
68bool nativeBinary64Fallback(
const double* left,
const double* right,
int length,
75 bool nonfinite =
false;
77 for (
int i = 0; i < length; ++i) {
79 nonfinite || !std::isfinite(left[i]) || !std::isfinite(right[i]);
80 all_zero = all_zero && (left[i] == 0.0 || right[i] == 0.0);
82 if (!nonfinite && !all_zero)
return false;
83 native = left[0] * right[0];
84 for (
int i = 1; i < length; ++i)
85 native += left[i] * right[i];
89void* emulatedProductLibrary()
91 static void*
const handle = []() ->
void* {
92 const char* path = std::getenv(
"GPR_EMULATED_PRODUCT_LIB");
93 if (path ==
nullptr || path[0] ==
'\0')
return nullptr;
94 void* library = dlopen(path, RTLD_NOW | RTLD_LOCAL);
95 if (library ==
nullptr) {
96 std::fprintf(stderr,
"GPR_EMULATED_PRODUCT_LIB: %s\n", dlerror());
104template <
class Launch>
105Launch emulatedEntry(
const char* name)
108 reinterpret_cast<Launch
>(dlsym(emulatedProductLibrary(), name));
109 if (entry ==
nullptr) {
110 std::fprintf(stderr,
"GPR_EMULATED_PRODUCT_LIB: %s\n", dlerror());
116double deviceSchemeProduct(
const char* symbol,
const double* left,
117 const double* right,
int length)
119 using Launch = int (*)(
const double*,
const double*, int,
double*);
120 const Launch launch = emulatedEntry<Launch>(symbol);
122 if (launch(left, right, length, &total) != 0) {
123 std::fprintf(stderr,
"%s: device product failed\n", symbol);
129double kernelFloatProduct(
const double* left,
const double* right,
int length)
131 using Launch = int (*)(
const double*,
const double*, int,
double*);
132 static const Launch launch =
133 kernelFloatEntry<Launch>(
"scheme_ii_product_launch_v2");
135 if (launch(left, right, length, &total) != 0)
136 throw std::runtime_error(
137 "GPR_PREDICT_SUM=kernel_float: device product failed");
141void kernelFloatProducts(
const StoredProduct* products,
int count,
144 using Launch = int (*)(
const double*
const*,
const double*
const*,
145 const int*, int,
double*);
146 static const Launch launch =
147 kernelFloatEntry<Launch>(
"scheme_ii_products_launch_v1");
148 std::vector<const double*> left(count), right(count);
149 std::vector<int> lengths(count);
150 for (
int i = 0; i < count; ++i) {
151 left[i] = products[i].left;
152 right[i] = products[i].right;
153 lengths[i] = products[i].length;
156 launch(left.data(), right.data(), lengths.data(), count, values);
158 throw std::runtime_error(
159 "GPR_PREDICT_SUM=kernel_float: device batch failed (" +
160 std::to_string(
status) +
")");
167 return kernelFloatLibrary() !=
nullptr;
173 if (count < 0 || (count > 0 && (products ==
nullptr || values ==
nullptr)))
174 throw std::invalid_argument(
"invalid product batch");
175 if (count == 0)
return;
176 for (
int i = 0; i < count; ++i) {
177 const auto& p = products[i];
178 if (p.length < 0 || (p.length > 0 && (!p.left || !p.right)))
179 throw std::invalid_argument(
"invalid operands in product batch");
184 bool any_terms =
false;
185 for (
int i = 0; i < count; ++i)
186 any_terms = any_terms || products[i].length > 0;
188 std::fill(values, values + count, 0.0);
191 bool exceptional =
false;
192 for (
int i = 0; i < count; ++i) {
193 if (products[i].length <= 0)
continue;
195 if (nativeBinary64Fallback(products[i].left, products[i].right,
196 products[i].length, native))
200 kernelFloatProducts(products, count, values);
202 for (
int i = 0; i < count; ++i)
205 products[i].length, mode);
208 for (
int i = 0; i < count; ++i)
210 products[i].left, products[i].right, products[i].length, mode);
215 const std::vector<const PredictProduct*>& products,
216 std::vector<double>& values)
218 if (products.size() >
static_cast<size_t>(std::numeric_limits<int>::max()))
219 throw std::length_error(
"too many prediction products");
220 bool device_batch =
true;
221 for (
const auto* product: products) {
222 if (product ==
nullptr)
223 throw std::invalid_argument(
"null prediction product");
227 values.resize(products.size());
229 for (
size_t i = 0; i < products.size(); ++i)
230 values[i] = products[i]->
value();
233 std::vector<StoredProduct> operands;
234 operands.reserve(products.size());
235 for (
const auto* product: products) {
236 const auto* terms = product->terms_.get();
238 operands.push_back({
nullptr,
nullptr, 0});
241 if (terms->left.size() >
242 static_cast<size_t>(std::numeric_limits<int>::max()))
243 throw std::length_error(
"prediction product is too long");
244 operands.push_back({terms->left.data(), terms->right.data(),
245 static_cast<int>(terms->left.size())});
252 const double* coefficients,
double* output,
255 if (cols < 0 || rows < 0 ||
256 (cols > 0 && (column_major ==
nullptr || output ==
nullptr)) ||
257 (rows > 0 && coefficients ==
nullptr))
258 throw std::invalid_argument(
"invalid prediction contraction");
264 2.0 *
static_cast<double>(rows) *
static_cast<double>(cols);
265 const bool parallel =
267#if defined(USE_OPENMP)
268#pragma omp parallel for schedule(static) if (parallel)
270 for (
int col = 0; col < cols; ++col) {
272 column_major +
static_cast<std::ptrdiff_t
>(col) * rows,
273 coefficients, rows, mode);
280 if (length <= 0)
return 0.0;
286 if (nativeBinary64Fallback(left, right, length, native))
return native;
293 const char* device_library = std::getenv(
"GPR_EMULATED_PRODUCT_LIB");
294 const bool on_device =
295 device_library !=
nullptr && device_library[0] !=
'\0';
298 return deviceSchemeProduct(
"scheme_i_product_launch_v1", left,
300 Eigen::Map<const Eigen::VectorXd> left_terms(left, length);
301 Eigen::Map<const Eigen::VectorXd> right_terms(right, length);
306 return deviceSchemeProduct(
"scheme_ii_native_launch_v1", left,
311 return kernelFloatProduct(left, right, length);
317 for (
int i = 0; i < length; ++i)
Exact floating-point accumulator (Kulisch long accumulator).
void addProduct(double a, double b) noexcept
double value() const noexcept
The exact sum rounded once to the nearest double, ties to even.
static void evaluateTogether(const std::vector< const PredictProduct * > &products, std::vector< double > &values)
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
void contractPredictionMatrix(const double *column_major, int rows, int cols, const double *coefficients, double *output, ProductMode mode)
double schemeIProduct(const Eigen::MatrixBase< Left > &left, const Eigen::MatrixBase< Right > &right)
bool kernelFloatProductAvailable()
double evaluateStoredProduct(const double *left, const double *right, int length, ProductMode mode)
void evaluateStoredProducts(const StoredProduct *products, int count, ProductMode mode, double *values)
bool teamParallelPays(double flops)
bool tryCertifiedProduct(const double *left, const double *right, int length, double &result)
double schemeIIProductRaw(const double *left, const double *right, int length)