1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// //! Interface to batched gemm operations
// use crate::dense::array::DynamicArray;
// use crate::dense::base_array::BaseArray;
// use crate::dense::traits::{RawAccess, RawAccessMut, Shape, Stride};
// use crate::dense::types::RlstScalar;
// use crate::dense::types::TransMode;
// use crate::{rlst_dynamic_array2, MultInto, RlstResult, SliceArray, SliceArrayMut};
// /// Batched matrix-matrix products.
// ///
// /// Implementations of this trait allow batched matrix-matrix products.
// pub trait BatchedGemm {
// /// The scalar type.
// type Item: RlstScalar;
// /// Access the left matrix with given index.
// fn left_matrix(&self, index: usize) -> Option<SliceArray<'_, Self::Item, 2>>;
// /// Mutably access the left matrix with given index/
// fn left_matrix_mut(&mut self, index: usize) -> Option<SliceArrayMut<'_, Self::Item, 2>>;
// /// Access the right matrix with given index.
// fn right_matrix(&self, index: usize) -> Option<SliceArray<'_, Self::Item, 2>>;
// /// Mutably access the right matrix with given index.
// fn right_matrix_mut(&mut self, index: usize) -> Option<SliceArrayMut<'_, Self::Item, 2>>;
// /// Access the result matrix with given index.
// fn result_matrix(&self, index: usize) -> Option<SliceArray<'_, Self::Item, 2>>;
// /// Mutably access the result matrix with given index.
// fn result_matrix_mut(&mut self, index: usize) -> Option<SliceArrayMut<'_, Self::Item, 2>>;
// /// Evaluate the batched matrix product.
// fn evaluate(&mut self) -> RlstResult<()>;
// }
// /// Batched matrix multiplication on CPU.
// ///
// /// This implementation simply uses the available BLAS to perform
// /// each matrix matrix multiplication.
// pub struct DefaultCpuBatchedGemm<Item: RlstScalar> {
// left_matrices: Vec<DynamicArray<Item, 2>>,
// right_matrices: Vec<DynamicArray<Item, 2>>,
// result_matrices: Vec<DynamicArray<Item, 2>>,
// number_of_matrices: usize,
// alpha: Item,
// beta: Item,
// }
// impl<Item: RlstScalar> DefaultCpuBatchedGemm<Item> {
// /// Initialize a new COPU batched matrix multiplication.
// pub fn new(
// left_dim: (usize, usize),
// right_dim: (usize, usize),
// number_of_matrices: usize,
// alpha: Item,
// beta: Item,
// ) -> Self {
// assert_eq!(left_dim.1, right_dim.0);
// let mut left_matrices = Vec::<DynamicArray<Item, 2>>::with_capacity(number_of_matrices);
// let mut right_matrices = Vec::<DynamicArray<Item, 2>>::with_capacity(number_of_matrices);
// let mut result_matrices = Vec::<DynamicArray<Item, 2>>::with_capacity(number_of_matrices);
// for _ in 0..number_of_matrices {
// left_matrices.push(rlst_dynamic_array2!(Item, [left_dim.0, left_dim.1]));
// right_matrices.push(rlst_dynamic_array2!(Item, [right_dim.0, right_dim.1]));
// result_matrices.push(rlst_dynamic_array2!(Item, [left_dim.0, right_dim.1]));
// }
// Self {
// left_matrices,
// right_matrices,
// result_matrices,
// number_of_matrices,
// alpha,
// beta,
// }
// }
// }
// impl<Item: RlstScalar> BatchedGemm for DefaultCpuBatchedGemm<Item> {
// type Item = Item;
// fn left_matrix(&self, index: usize) -> Option<SliceArray<'_, Self::Item, 2>> {
// self.left_matrices.get(index).map(|mat| {
// let slice_container = crate::SliceContainer::new(mat.data());
// SliceArray::new(BaseArray::new_with_stride(
// slice_container,
// mat.shape(),
// mat.stride(),
// ))
// })
// }
// fn left_matrix_mut(&mut self, index: usize) -> Option<SliceArrayMut<'_, Self::Item, 2>> {
// self.left_matrices.get_mut(index).map(|mat| {
// let shape = mat.shape();
// let stride = mat.stride();
// let slice_container = crate::SliceContainerMut::new(mat.data_mut());
// SliceArrayMut::new(BaseArray::new_with_stride(slice_container, shape, stride))
// })
// }
// fn right_matrix(&self, index: usize) -> Option<SliceArray<'_, Self::Item, 2>> {
// self.right_matrices.get(index).map(|mat| {
// let slice_container = crate::SliceContainer::new(mat.data());
// SliceArray::new(BaseArray::new_with_stride(
// slice_container,
// mat.shape(),
// mat.stride(),
// ))
// })
// }
// fn right_matrix_mut(&mut self, index: usize) -> Option<SliceArrayMut<'_, Self::Item, 2>> {
// self.right_matrices.get_mut(index).map(|mat| {
// let shape = mat.shape();
// let stride = mat.stride();
// let slice_container = crate::SliceContainerMut::new(mat.data_mut());
// SliceArrayMut::new(BaseArray::new_with_stride(slice_container, shape, stride))
// })
// }
// fn result_matrix(&self, index: usize) -> Option<SliceArray<'_, Self::Item, 2>> {
// self.result_matrices.get(index).map(|mat| {
// let slice_container = crate::SliceContainer::new(mat.data());
// SliceArray::new(BaseArray::new_with_stride(
// slice_container,
// mat.shape(),
// mat.stride(),
// ))
// })
// }
// fn result_matrix_mut(&mut self, index: usize) -> Option<SliceArrayMut<'_, Self::Item, 2>> {
// self.result_matrices.get_mut(index).map(|mat| {
// let shape = mat.shape();
// let stride = mat.stride();
// let slice_container = crate::SliceContainerMut::new(mat.data_mut());
// SliceArrayMut::new(BaseArray::new_with_stride(slice_container, shape, stride))
// })
// }
// fn evaluate(&mut self) -> RlstResult<()> {
// for index in 0..self.number_of_matrices {
// let left_matrix = self.left_matrices[index].r();
// let right_matrix = self.right_matrices[index].r();
// let result_matrix = self.result_matrices[index].r_mut();
// result_matrix.mult_into(
// TransMode::NoTrans,
// TransMode::NoTrans,
// self.alpha,
// left_matrix,
// right_matrix,
// self.beta,
// );
// }
// Ok(())
// }
// }
// #[cfg(test)]
// mod test {
// use super::*;
// use crate::dense::traits::DefaultIterator;
// use crate::dense::traits::MultIntoResize;
// #[test]
// pub fn test_batched_cpu_gemm() {
// let mut batched_matmul = DefaultCpuBatchedGemm::<f64>::new((2, 3), (3, 5), 2, 1.0, 0.0);
// batched_matmul
// .left_matrix_mut(0)
// .unwrap()
// .fill_from_seed_equally_distributed(0);
// batched_matmul
// .left_matrix_mut(1)
// .unwrap()
// .fill_from_seed_equally_distributed(1);
// batched_matmul
// .right_matrix_mut(0)
// .unwrap()
// .fill_from_seed_equally_distributed(2);
// batched_matmul
// .right_matrix_mut(1)
// .unwrap()
// .fill_from_seed_equally_distributed(3);
// batched_matmul.evaluate().unwrap();
// for index in 0..2 {
// let expected = crate::dense::array::empty_array().simple_mult_into_resize(
// batched_matmul.left_matrix(index).unwrap(),
// batched_matmul.right_matrix(index).unwrap(),
// );
// crate::assert_array_relative_eq!(
// expected,
// batched_matmul.result_matrix(index).unwrap(),
// 1E-12
// );
// }
// }
// }