Skip to main content

collenchyma_blas/
plugin.rs

1//! Provides the IBlas library trait for Collenchyma implementation.
2
3use super::binary::IBlasBinary;
4use super::transpose::*;
5use collenchyma::binary::IBinary;
6use collenchyma::tensor::SharedTensor;
7use collenchyma::device::DeviceType;
8
9/// Provides the functionality for a backend to support Basic Linear Algebra Subprogram operations.
10pub trait IBlas<F> { }
11
12/// Provides the asum operation.
13pub trait Asum<F> {
14    /// Computes the absolute sum of vector `x` with complete memory management.
15    ///
16    /// Saves the result to `result`.
17    /// This is a Level 1 BLAS operation.
18    ///
19    /// For a no-memory managed version see `asum_plain`.
20    fn asum(&self, x: &mut SharedTensor<F>, result: &mut SharedTensor<F>) -> Result<(), ::collenchyma::error::Error>;
21
22    /// Computes the absolute sum of vector `x` without any memory management.
23    ///
24    /// Saves the result to `result`.
25    /// This is a Level 1 BLAS operation.
26    ///
27    /// *Attention*:<br/>
28    /// For a correct computation result, you need to manage the memory allocation and synchronization yourself.<br/>
29    /// For a memory managed version see `asum`.
30    fn asum_plain(&self, x: &SharedTensor<F>, result: &mut SharedTensor<F>) -> Result<(), ::collenchyma::error::Error>;
31}
32
33/// Provides the axpy operation.
34pub trait Axpy<F> {
35    /// Computes a vector `x` times a constant `a` plus a vector `y` aka. `a * x + y` with complete memory management.
36    ///
37    /// Saves the resulting vector back into `y`.
38    /// This is a Level 1 BLAS operation.
39    ///
40    /// For a no-memory managed version see `axpy_plain`.
41    fn axpy(&self, a: &mut SharedTensor<F>, x: &mut SharedTensor<F>, y: &mut SharedTensor<F>) -> Result<(), ::collenchyma::error::Error>;
42
43    /// Computes a vector `x` times a constant `a` plus a vector `y` aka. `a * x + y` without any memory management.
44    ///
45    /// Saves the resulting vector back into `y`.
46    /// This is a Level 1 BLAS operation.
47    ///
48    /// *Attention*:<br/>
49    /// For a correct computation result, you need to manage the memory allocation and synchronization yourself.<br/>
50    /// For a memory managed version see `axpy`.
51    fn axpy_plain(&self, a: &SharedTensor<F>, x: &SharedTensor<F>, y: &mut SharedTensor<F>) -> Result<(), ::collenchyma::error::Error>;
52}
53
54/// Provides the copy operation.
55pub trait Copy<F> {
56    /// Copies `x.len()` elements of vector `x` into vector `y` with complete memory management.
57    ///
58    /// Saves the result to `y`.
59    /// This is a Level 1 BLAS operation.
60    ///
61    /// For a no-memory managed version see `copy_plain`.
62    fn copy(&self, x: &mut SharedTensor<F>, y: &mut SharedTensor<F>) -> Result<(), ::collenchyma::error::Error>;
63
64    /// Copies `x.len()` elements of vector `x` into vector `y` without any memory management.
65    ///
66    /// Saves the result to `y`.
67    /// This is a Level 1 BLAS operation.
68    ///
69    /// *Attention*:<br/>
70    /// For a correct computation result, you need to manage the memory allocation and synchronization yourself.<br/>
71    /// For a memory managed version see `copy`.
72    fn copy_plain(&self, x: &SharedTensor<F>, y: &mut SharedTensor<F>) -> Result<(), ::collenchyma::error::Error>;
73}
74
75/// Provides the dot operation.
76pub trait Dot<F> {
77    /// Computes the [dot product][dot-product] over x and y with complete memory management.
78    /// [dot-product]: https://en.wikipedia.org/wiki/Dot_product
79    ///
80    /// Saves the resulting value into `result`.
81    /// This is a Level 1 BLAS operation.
82    ///
83    /// For a no-memory managed version see `dot_plain`.
84    fn dot(&self, x: &mut SharedTensor<F>, y: &mut SharedTensor<F>, result: &mut SharedTensor<F>) -> Result<(), ::collenchyma::error::Error>;
85
86    /// Computes the [dot product][dot-product] over x and y without any memory management.
87    /// [dot-product]: https://en.wikipedia.org/wiki/Dot_product
88    ///
89    /// Saves the resulting value into `result`.
90    /// This is a Level 1 BLAS operation.
91    ///
92    /// *Attention*:<br/>
93    /// For a correct computation result, you need to manage the memory allocation and synchronization yourself.<br/>
94    /// For a memory managed version see `dot`.
95    fn dot_plain(&self, x: &SharedTensor<F>, y: &SharedTensor<F>, result: &mut SharedTensor<F>) -> Result<(), ::collenchyma::error::Error>;
96}
97
98/// Provides the nrm2 operation.
99pub trait Nrm2<F> {
100    /// Computes the L2 norm aka. euclidean length of vector `x` with complete memory management.
101    ///
102    /// Saves the result to `result`.
103    /// This is a Level 1 BLAS operation.
104    ///
105    /// For a no-memory managed version see `nrm2_plain`.
106    fn nrm2(&self, x: &mut SharedTensor<F>, result: &mut SharedTensor<F>) -> Result<(), ::collenchyma::error::Error>;
107
108    /// Computes the L2 norm aka. euclidean length of vector `x` without any memory management.
109    ///
110    /// Saves the result to `result`.
111    /// This is a Level 1 BLAS operation.
112    ///
113    /// *Attention*:<br/>
114    /// For a correct computation result, you need to manage the memory allocation and synchronization yourself.<br/>
115    /// For a memory managed version see `nrm2`.
116    fn nrm2_plain(&self, x: &SharedTensor<F>, result: &mut SharedTensor<F>) -> Result<(), ::collenchyma::error::Error>;
117}
118
119/// Provides the scal operation.
120pub trait Scal<F> {
121    /// Scales a vector `x` by a constant `a` aka. `a * x` with complete memory management.
122    ///
123    /// Saves the resulting vector back into `x`.
124    /// This is a Level 1 BLAS operation.
125    ///
126    /// For a no-memory managed version see `scale_plain`.
127    fn scal(&self, a: &mut SharedTensor<F>, x: &mut SharedTensor<F>) -> Result<(), ::collenchyma::error::Error>;
128
129    /// Scales a vector `x` by a constant `a` aka. `a * x` without any memory management.
130    ///
131    /// Saves the resulting vector back into `x`.
132    /// This is a Level 1 BLAS operation.
133    ///
134    /// *Attention*:<br/>
135    /// For a correct computation result, you need to manage the memory allocation and synchronization yourself.<br/>
136    /// For a memory managed version see `scale`.
137    fn scal_plain(&self, a: &SharedTensor<F>, x: &mut SharedTensor<F>) -> Result<(), ::collenchyma::error::Error>;
138}
139
140/// Provides the swap operation.
141pub trait Swap<F> {
142    /// Swaps the content of vector `x` and vector `y` with complete memory management.
143    ///
144    /// Saves the resulting vector back into `x`.
145    /// This is a Level 1 BLAS operation.
146    ///
147    /// For a no-memory managed version see `swap_plain`.
148    fn swap(&self, x: &mut SharedTensor<F>, y: &mut SharedTensor<F>) -> Result<(), ::collenchyma::error::Error>;
149
150    /// Swaps the content of vector `x` and vector `y` without any memory management.
151    ///
152    /// Saves the resulting vector back into `x`.
153    /// This is a Level 1 BLAS operation.
154    ///
155    /// *Attention*:<br/>
156    /// For a correct computation result, you need to manage the memory allocation and synchronization yourself.<br/>
157    /// For a memory managed version see `swap`.
158    fn swap_plain(&self, x: &mut SharedTensor<F>, y: &mut SharedTensor<F>) -> Result<(), ::collenchyma::error::Error>;
159}
160
161/// Provides the gemm operation.
162pub trait Gemm<F> {
163    /// Computes a matrix-matrix product with general matrices.
164    ///
165    /// Saves the result into `c`.
166    /// This is a Level 3 BLAS operation.
167    ///
168    /// For a no-memory managed version see `gemm_plain`.
169    fn gemm(&self, alpha: &mut SharedTensor<F>, at: Transpose, a: &mut SharedTensor<F>, bt: Transpose, b: &mut SharedTensor<F>, beta: &mut SharedTensor<F>, c: &mut SharedTensor<F>) -> Result<(), ::collenchyma::error::Error>;
170
171    /// Computes a matrix-matrix product with general matrices.
172    ///
173    /// Saves the result into `c`.
174    /// This is a Level 3 BLAS operation.
175    ///
176    /// *Attention*:<br/>
177    /// For a correct computation result, you need to manage the memory allocation and synchronization yourself.<br/>
178    /// For a memory managed version see `gemm`.
179    fn gemm_plain(&self, alpha: &SharedTensor<F>, at: Transpose, a: &SharedTensor<F>, bt: Transpose, b: &SharedTensor<F>, beta: &SharedTensor<F>, c: &mut SharedTensor<F>) -> Result<(), ::collenchyma::error::Error>;
180}
181
182/// Allows a BlasBinary to be provided which is used for a IBlas implementation.
183pub trait BlasBinaryProvider<F, B: IBlasBinary<F> + IBinary> {
184    /// Returns the binary representation
185    fn binary(&self) -> &B;
186    /// Returns the device representation
187    fn device(&self) -> &DeviceType;
188}
189
190impl<F, B: IBlasBinary<F> + IBinary> IBlas<F> for BlasBinaryProvider<F, B> { }