forked from bovulpes/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBandMatrixSolver.h
More file actions
265 lines (238 loc) · 6.68 KB
/
BandMatrixSolver.h
File metadata and controls
265 lines (238 loc) · 6.68 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file BandMatrixSolver.h
/// \brief Definition of BandMatrixSolver class
///
/// \author Sergey Gorbunov <sergey.gorbunov@cern.ch>
#ifndef ALICEO2_GPUCOMMON_TPCFASTTRANSFORMATION_BANDMATRIXSOLVER_H
#define ALICEO2_GPUCOMMON_TPCFASTTRANSFORMATION_BANDMATRIXSOLVER_H
#include "GPUCommonDef.h"
#include "GPUCommonRtypes.h"
#include <vector>
#include <cassert>
#include <cstdlib>
#include <algorithm>
#include <limits>
namespace GPUCA_NAMESPACE
{
namespace gpu
{
/// Linear Equation Solver for a symmetric positive-definite band matrix A[n x n].
///
/// The matrix has a pattern of BandWidthT adjacent non-zero entries right to the diagonal in each row
/// Here is an example with n==10, BandWidthT==4. (*) means non-zero element, (+) means symmetric element):
/// (**** )
/// (+**** )
/// (++**** )
/// (+++**** )
/// A = ( +++**** )
/// ( +++**** )
/// ( +++****)
/// ( +++***)
/// ( +++**)
/// ( +++*)
///
/// The non-zero matrix elements are stored in [n x BandWidthT] array mA
///
/// The equation to sove is A[n][n] x X[n][Bdim] = B[n][Bdim].
/// During calculations, the initial values of mA and mB get lost, so one can call solve() only once.
/// The solution X is stored in mB.
///
template <int BandWidthT>
class BandMatrixSolver
{
public:
/// Consructor
BandMatrixSolver(int N, int Bdim) : mN(N), mBdim(Bdim)
{
assert(N > 0 && Bdim > 0);
mA.resize(mN * BandWidthT, 0.);
mB.resize(mN * mBdim, 0.);
}
/// debug tool: init arrays with NaN's
void initWithNaN()
{
// Assign NaN's to ensure that uninitialized elements (for the matrix type 1) are not used in calculations.
mA.assign(mA.size(), std::numeric_limits<double>::signaling_NaN());
mB.assign(mB.size(), std::numeric_limits<double>::signaling_NaN());
}
/// access to A elements
double& A(int i, int j)
{
auto ij = std::minmax(i, j);
assert(ij.first >= 0 && ij.second < mN);
int k = ij.second - ij.first;
assert(k < BandWidthT);
return mA[ij.first * BandWidthT + k];
}
/// access to B elements
double& B(int i, int j)
{
assert(i >= 0 && i < mN && j >= 0 && j < mBdim);
return mB[i * mBdim + j];
}
/// solve the equation
void solve();
/// solve an equation of a special type
void solveType1();
/// Test the class functionality. Returns 1 when ok, 0 when not ok
static int test(bool prn = 0)
{
return BandMatrixSolver<0>::test(prn);
}
private:
template <int nRows>
void triangulateBlock(double AA[], double bb[]);
template <int nCols>
void dioganalizeBlock(double A[], double b[]);
private:
int mN = 0;
int mBdim = 0;
std::vector<double> mA;
std::vector<double> mB;
#ifndef GPUCA_ALIROOT_LIB
ClassDefNV(BandMatrixSolver, 0);
#endif
};
template <>
int BandMatrixSolver<0>::test(bool prn);
template <int BandWidthT>
template <int nRows>
inline void BandMatrixSolver<BandWidthT>::triangulateBlock(double AA[], double bb[])
{
{
int m = BandWidthT;
double* A = AA;
for (int rows = 0; rows < nRows; rows++) {
double c = 1. / A[0];
A[0] = c; // store 1/a[0][0]
double* rowi = A + BandWidthT - 1;
for (int i = 1; i < m; i++) { // row 0+i
double ai = c * A[i]; // A[0][i]
for (int j = i; j < m; j++) {
rowi[j] -= ai * A[j]; // A[i][j] -= A[0][j]/A[0][0]*A[i][0]
}
A[i] = ai; // A[0][i] /= A[0][0]
rowi += BandWidthT - 1;
}
m--;
A += BandWidthT;
}
}
for (int k = 0; k < mBdim; k++) {
int m = BandWidthT;
double* A = AA;
double* b = bb;
for (int rows = 0; rows < nRows; rows++) {
double bk = b[k];
for (int i = 1; i < m; i++) {
b[mBdim * i + k] -= A[i] * bk;
}
b[k] *= A[0];
m--;
A += BandWidthT;
b += mBdim;
}
}
}
template <int BandWidthT>
template <int nCols>
inline void BandMatrixSolver<BandWidthT>::dioganalizeBlock(double AA[], double bb[])
{
for (int k = 0; k < mBdim; k++) {
int rows = BandWidthT;
double* A = AA;
double* b = bb;
for (int col = 0; col < nCols; col++) {
double bk = b[k];
for (int i = 1; i < rows; i++) {
b[-i * mBdim + k] -= A[BandWidthT * (-i) + i] * bk;
}
A -= BandWidthT;
b -= mBdim;
rows--;
}
}
}
template <int BandWidthT>
inline void BandMatrixSolver<BandWidthT>::solve()
{
/// Solution slover
const int stepA = BandWidthT;
const int stepB = mBdim;
// Upper Triangulization
{
int k = 0;
double* Ak = &mA[0];
double* bk = &mB[0];
for (; k < mN - BandWidthT; k += 1, Ak += stepA, bk += stepB) { // for each row k
triangulateBlock<1>(Ak, bk);
}
// last m rows
triangulateBlock<BandWidthT>(Ak, bk);
}
// Diagonalization
{
int k = mN - 1;
double* Ak = &mA[BandWidthT * k];
double* bk = &mB[mBdim * k];
for (; k > BandWidthT - 1; k -= 1, Ak -= stepA, bk -= stepB) { // for each row k
dioganalizeBlock<1>(Ak, bk);
}
// first m rows
dioganalizeBlock<BandWidthT>(Ak, bk);
}
}
template <int BandWidthT>
inline void BandMatrixSolver<BandWidthT>::solveType1()
{
/// A special solver for a band matrix were every second row has 0 at the end of the band.
/// An example with n==10, BandWidthT==4:
///
/// (**** )
/// (+***0 )
/// (++**** )
/// (+++***0 )
/// A = ( 0++**** )
/// ( +++***0 )
/// ( 0++****)
/// ( +++***)
/// ( 0++**)
/// ( +++*)
///
const int stepA = 2 * BandWidthT;
const int stepB = 2 * mBdim;
// Upper Triangulization
{
int k = 0;
double* Ak = &mA[0];
double* bk = &mB[0];
for (; k < mN - BandWidthT; k += 2, Ak += stepA, bk += stepB) { // for each row k
triangulateBlock<2>(Ak, bk);
}
// last m rows
triangulateBlock<BandWidthT>(Ak, bk);
}
// Diagonalization
{
int k = mN - 1;
double* Ak = &mA[BandWidthT * k];
double* bk = &mB[mBdim * k];
for (; k > BandWidthT - 1; k -= 2, Ak -= stepA, bk -= stepB) { // for each row k
dioganalizeBlock<2>(Ak, bk);
}
// first m rows
dioganalizeBlock<BandWidthT>(Ak, bk);
}
}
} // namespace gpu
} // namespace GPUCA_NAMESPACE
#endif