coaster_blas/plugin.rs
1//! Provides the IBlas library trait for Coaster implementation.
2
3use super::binary::IBlasBinary;
4use super::transpose::*;
5use coaster::binary::IBinary;
6use coaster::tensor::SharedTensor;
7
8/// Provides the functionality for a backend to support Basic Linear Algebra Subprogram operations.
9pub trait IBlas<F> {}
10
11/// Provides the asum operation.
12pub trait Asum<F> {
13    /// Computes the absolute sum of vector `x`.
14    ///
15    /// Saves the result to `result`.
16    /// This is a Level 1 BLAS operation.
17    fn asum(
18        &self,
19        x: &SharedTensor<F>,
20        result: &mut SharedTensor<F>,
21    ) -> Result<(), ::coaster::error::Error>;
22}
23
24/// Provides the axpy operation.
25pub trait Axpy<F> {
26    /// Computes a vector `x` times a constant `a` plus a vector `y` aka. `a * x + y`.
27    ///
28    /// Saves the resulting vector back into `y`.
29    /// This is a Level 1 BLAS operation.
30    fn axpy(
31        &self,
32        a: &SharedTensor<F>,
33        x: &SharedTensor<F>,
34        y: &mut SharedTensor<F>,
35    ) -> Result<(), ::coaster::error::Error>;
36}
37
38/// Provides the copy operation.
39pub trait Copy<F> {
40    /// Copies `x.len()` elements of vector `x` into vector `y`.
41    ///
42    /// Saves the result to `y`.
43    /// This is a Level 1 BLAS operation.
44    fn copy(
45        &self,
46        x: &SharedTensor<F>,
47        y: &mut SharedTensor<F>,
48    ) -> Result<(), ::coaster::error::Error>;
49}
50
51/// Provides the dot operation.
52pub trait Dot<F> {
53    /// Computes the [dot product][dot-product] over x and y.
54    /// [dot-product]: https://en.wikipedia.org/wiki/Dot_product
55    ///
56    /// Saves the resulting value into `result`.
57    /// This is a Level 1 BLAS operation.
58    fn dot(
59        &self,
60        x: &SharedTensor<F>,
61        y: &SharedTensor<F>,
62        result: &mut SharedTensor<F>,
63    ) -> Result<(), ::coaster::error::Error>;
64}
65
66/// Provides the nrm2 operation.
67pub trait Nrm2<F> {
68    /// Computes the L2 norm aka. euclidean length of vector `x`.
69    ///
70    /// Saves the result to `result`.
71    /// This is a Level 1 BLAS operation.
72    fn nrm2(
73        &self,
74        x: &SharedTensor<F>,
75        result: &mut SharedTensor<F>,
76    ) -> Result<(), ::coaster::error::Error>;
77}
78
79/// Provides the scal operation.
80pub trait Scal<F> {
81    /// Scales a vector `x` by a constant `a` aka. `a * x`.
82    ///
83    /// Saves the resulting vector back into `x`.
84    /// This is a Level 1 BLAS operation.
85    fn scal(
86        &self,
87        a: &SharedTensor<F>,
88        x: &mut SharedTensor<F>,
89    ) -> Result<(), ::coaster::error::Error>;
90}
91
92/// Provides the swap operation.
93pub trait Swap<F> {
94    /// Swaps the content of vector `x` and vector `y` with complete memory management.
95    ///
96    /// Saves the resulting vector back into `x`.
97    /// This is a Level 1 BLAS operation.
98    fn swap(
99        &self,
100        x: &mut SharedTensor<F>,
101        y: &mut SharedTensor<F>,
102    ) -> Result<(), ::coaster::error::Error>;
103}
104
105/// Provides the gbmv operation
106pub trait Gbmv<F> {
107    /// Computes a matrix-vector product with a band matrix
108    ///
109    /// Saves the resulting vector into `c`.
110    /// This is a Level 2 BLAS operation.
111    #[allow(clippy::too_many_arguments)]
112    fn gbmv(
113        &self,
114        alpha: &SharedTensor<F>,
115        at: Transpose,
116        a: &SharedTensor<F>,
117        kl: &SharedTensor<u32>,
118        ku: &SharedTensor<u32>,
119        x: &SharedTensor<F>,
120        beta: &SharedTensor<F>,
121        c: &mut SharedTensor<F>,
122    ) -> Result<(), ::coaster::error::Error>;
123}
124
125/// Provides the gemm operation.
126pub trait Gemm<F> {
127    /// Computes a matrix-matrix product with general matrices.
128    ///
129    /// Saves the result into `c`.
130    /// This is a Level 3 BLAS operation.
131    #[allow(clippy::too_many_arguments)]
132    fn gemm(
133        &self,
134        alpha: &SharedTensor<F>,
135        at: Transpose,
136        a: &SharedTensor<F>,
137        bt: Transpose,
138        b: &SharedTensor<F>,
139        beta: &SharedTensor<F>,
140        c: &mut SharedTensor<F>,
141    ) -> Result<(), ::coaster::error::Error>;
142}
143
144/// Allows a BlasBinary to be provided which is used for a IBlas implementation.
145pub trait BlasBinaryProvider<F, B: IBlasBinary<F> + IBinary> {
146    /// Returns the binary representation
147    fn binary(&self) -> &B;
148}
149
150impl<F, B: IBlasBinary<F> + IBinary> IBlas<F> for dyn BlasBinaryProvider<F, B> {}