Loading...
Searching...
No Matches
TrainingProductAbi.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3
4// The v1 training-product C ABI (TrainingProductApi.h) over the CPU Scheme II
5// engines, for programs that load a product library by path. GPR_SCHEME2
6// selects the engine with the specification of schemeii::parseOptions
7// (default "scheme2:fp64"); GPR_SCHEME2_THREADS sets the worker count when
8// the specification has none.
9
10#include <algorithm>
11#include <cstdio>
12#include <cstdlib>
13#include <exception>
14#include <memory>
15#include <stdexcept>
16#include <string>
17#include <vector>
18
20#include "SchemeII.h"
21
22namespace {
23void diagnostic(char* error, size_t capacity, const char* text)
24{
25 if (error && capacity) std::snprintf(error, capacity, "%s", text);
26}
27} // namespace
28
29extern "C" __attribute__((visibility("default"))) void*
30gpr_training_product_create_v1(char* error, size_t capacity)
31{
32 try {
33 const char* spec = std::getenv("GPR_SCHEME2");
35 spec && *spec ? spec : "scheme2:fp64");
36 if (options.threads == 0) {
37 const char* threads = std::getenv("GPR_SCHEME2_THREADS");
38 options.threads = threads && *threads ? std::atoi(threads) : 1;
39 if (options.threads < 1) options.threads = 1;
40 }
41 auto context = std::make_unique<gpr::linalg::schemeii::Context>(
42 std::move(options));
43 std::fprintf(stderr, "[training-product] scheme2 %s\n",
44 context->describe(256).c_str());
45 return context.release();
46 } catch (const std::exception& exception) {
47 diagnostic(error, capacity, exception.what());
48 return nullptr;
49 }
50}
51
52extern "C" __attribute__((visibility("default"))) void
54{
55 delete static_cast<gpr::linalg::schemeii::Context*>(workspace);
56}
57
58extern "C" __attribute__((visibility("default"))) int
59gpr_training_product_gemm_v1(void* workspace, int transpose_a, int transpose_b,
60 int64_t m, int64_t n, int64_t k, double alpha,
61 const double* a, int64_t lda, const double* b,
62 int64_t ldb, double beta, double* c, int64_t ldc,
63 char* error, size_t capacity)
64{
65 try {
66 if (!workspace || (transpose_a != 0 && transpose_a != 1) ||
67 (transpose_b != 0 && transpose_b != 1))
68 throw std::invalid_argument("invalid Scheme II training product");
69 if (m < 0 || n < 0 || !c || ldc < m)
70 throw std::invalid_argument("invalid Scheme II training output");
71 // A failed call leaves C untouched: compute into a copy.
72 std::vector<double> result(static_cast<std::size_t>(m * n));
73 if (beta != 0.0)
74 for (int64_t j = 0; j < n; ++j)
75 std::copy_n(c + j * ldc, m, result.data() + j * m);
76 static_cast<const gpr::linalg::schemeii::Context*>(workspace)->gemm(
77 transpose_a, transpose_b, m, n, k, alpha, a, lda, b, ldb, beta,
78 result.data(), std::max<int64_t>(1, m));
79 for (int64_t j = 0; j < n; ++j)
80 std::copy_n(result.data() + j * m, m, c + j * ldc);
81 return 0;
82 } catch (const std::exception& exception) {
83 diagnostic(error, capacity, exception.what());
84 return 1;
85 }
86}
__attribute__((visibility("default"))) void *gpr_training_product_create_v1(char *error
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)
Options parseOptions(std::string_view spec)
Definition SchemeII.cpp:460