-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathLUdecomposition.cpp
488 lines (397 loc) · 13.3 KB
/
LUdecomposition.cpp
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/****************************************************************************/
/* File: LUdecomposition.cpp */
/* Author: Joachim Schoeberl */
/* Date: Nov 2020 */
/****************************************************************************/
#include <core/simd.hpp>
#include <ngs_stdcpp_include.hpp>
#include <matrix.hpp>
#include <vector.hpp>
#include <triangular.hpp>
#include <ngblas.hpp>
namespace ngbla
{
/*
void CalcLU (SliceMatrix<double> a, FlatArray<int> p)
{
size_t n = a.Height();
for (size_t i = 0; i < n; i++) p[i] = i;
for (size_t i = 0; i < n; i++)
{
size_t imax = i;
for (size_t j = i+1; j < n; j++)
if (fabs(a(j,i)) > fabs(a(imax,i)))
imax = j;
if (imax != i)
{
Swap (p[i], p[imax]);
for (int j = 0; j < n; j++)
Swap (a(i,j), a(imax, j));
}
a.Row(i).Range(i,n) -= Trans (a.Rows(0,i).Cols(i,n)) * a.Row(i).Range(0,i);
a.Col(i).Range(i+1,n) -= a.Cols(0,i).Rows(i+1,n) * a.Col(i).Range(0,i);
a.Col(i).Range(i+1,n) *= 1.0/a(i,i);
}
}
*/
void SwapVectors (FlatVector<> a, BareVector<> b)
{
size_t n = a.Size();
/*
for (size_t i = 0; i < n; i++)
Swap (a(i), b(i));
*/
size_t i = 0;
for ( ; i+SIMD<double>::Size() <= n; i+=SIMD<double>::Size())
{
auto va = SIMD<double>(&a(i));
auto vb = SIMD<double>(&b(i));
va.Store(&b(i));
vb.Store(&a(i));
}
if (i==n) return;
// handle SIMD rest
Switch<SIMD<double>::Size()> ( (n-i), [&] (auto r)
{
Vec<r.value> ha = a.Range(i, i+r.value);
Vec<r.value> hb = b.Range(i, i+r.value);
a.Range(i,i+r.value) = hb;
b.Range(i,i+r.value) = ha;
});
}
void CalcLU1 (SliceMatrix<double> a, FlatArray<int> p)
{
size_t n = a.Height();
static Timer t("CalcLU"); RegionTimer reg(t);
t.AddFlops (n*n*n/3);
// static Timer tmm1("CalcLU - mat mat 1");
// static Timer tmm2("CalcLU - mat mat 2");
// static Timer tmm3("CalcLU - mat mat 3");
for (size_t i = 0; i < n; i++) p[i] = i;
size_t bs = 48;
size_t bs2 = 8;
for (size_t i1 = 0; i1 < n; i1+=bs)
{
size_t end1 = min(n, i1+bs);
for (size_t i2 = i1; i2 < end1; i2 += bs2)
{
size_t end2 = min(n, i2+bs2);
for (size_t i = i2; i < end2; i++)
{
size_t imax = i;
double valmax = fabs(a(i,i));
for (size_t j = i+1; j < n; j++)
if (double valj = fabs(a(j,i)) > valmax)
{
valmax = valj;
imax = j;
}
if (imax != i)
{
Swap (p[i], p[imax]);
SwapVectors (a.Row(i), a.Row(imax));
}
if (i+1 < n)
{
a.Col(i).Range(i+1,n) *= 1.0/a(i,i);
// RegionTimer rmm3(tmm3);
a.Rows(i+1,n).Cols(i+1,end2) -= a.Rows(i+1,n).Cols(i,i+1) * a.Rows(i,i+1).Cols(i+1,end2);
}
}
// RegionTimer rmm2(tmm2);
if (end2 < end1)
{
TriangularSolve<LowerLeft,Normalized> (a.Rows(i2, end2).Cols(i2, end2), a.Rows(i2, end2).Cols(end2, end1));
a.Rows(end2,n).Cols(end2, end1) -= a.Cols(i2,end2).Rows(end2,n) * a.Rows(i2,end2).Cols(end2, end1);
}
}
// RegionTimer rmm1(tmm1);
if (end1 < n)
{
TriangularSolve<LowerLeft,Normalized> (a.Rows(i1, end1).Cols(i1, end1), a.Rows(i1, end1).Cols(end1, n));
a.Rows(end1,n).Cols(end1, n) -= a.Cols(i1,end1).Rows(end1,n) * a.Rows(i1,end1).Cols(end1, n);
}
}
}
/*
static Timer calcLUSolveL("CalcLU - SolveL");
static Timer calcLUMatMat("CalcLU - MatMat");
static Timer calcLUSimple("CalcLU - simple");
static Timer calcLUSimple_simd("CalcLU - simple SIMD");
static Timer calcLUSimple2("CalcLU - simple2x");
static Timer calcLUSearch("CalcLU - search");
static Timer calcLUSwap("CalcLU - swap");
*/
void CalcLURec (SliceMatrix<double> a, FlatArray<int> p, IntRange r)
{
size_t n = a.Height();
/*
if (r.Size() == 0) return;
if (r.Size() == 1)
{
size_t i = r.First();
size_t imax = i;
double valmax = fabs(a(i,i));
for (size_t j = i+1; j < n; j++)
if (double valj = fabs(a(j,i)) > valmax)
{
valmax = valj;
imax = j;
}
if (imax != i)
{
Swap (p[i], p[imax]);
SwapVectors (a.Row(i), a.Row(imax));
}
if (i+1 < n)
a.Col(i).Range(i+1,n) *= 1.0/a(i,i);
return;
}
*/
constexpr size_t bs = 4;
if (r.Size() <= bs)
{
// RegionTimer reg(calcLUSimple);
for (auto i : r)
{
size_t imax = i;
double valmax = fabs(a(i,i));
{
// RegionTimer reg(calcLUSearch);
for (size_t j = i+1; j < n; j++)
{
double valj = fabs(a(j,i));
if (valj > valmax)
{
valmax = valj;
imax = j;
}
}
}
if (imax != i)
{
// RegionTimer reg(calcLUSwap);
Swap (p[i], p[imax]);
SwapVectors (a.Row(i), a.Row(imax));
}
if (r.Size() == 4)
{
// RegionTimer reg(calcLUSimple_simd);
size_t rest = i-r.First();
double * ptr = &a(i, r.First());
double invaii = 1.0/ptr[rest];
/*
double scale[4] = { 0, 0, 0, 0 };
// for (size_t k = 0; k < rest; k++)
// scale[k] = 0.0;
scale[rest] = invaii-1;
for (size_t k = rest+1; k < 4; k++)
scale[k] = -a(i,r.First()+k)*invaii;
SIMD<double,4> scale1(&scale[0]);
*/
SIMD<double,4> scale1 = 0.0;
SIMD<mask64,4> m1(rest);
scale1 = If (m1, scale1, SIMD<double,4>(invaii-1));
SIMD<mask64,4> m2(rest+1);
scale1 = If (m2, scale1, -SIMD<double,4>(ptr)*invaii);
ptr += a.Dist();
for (size_t j = i+1; j < n; j++, ptr+=a.Dist())
{
SIMD<double,4> row1(ptr);
double fac = ptr[rest];
row1 += fac*scale1;
row1.Store(ptr);
}
}
/*
else if (r.Size() == 8)
{
double scale[8];
size_t rest = i-r.First();
for (size_t k = 0; k < rest; k++)
scale[k] = 0.0;
double invaii = 1.0/a(i,i);
scale[rest] = invaii-1;
for (size_t k = rest+1; k < 8; k++)
scale[k] = -a(i,r.First()+k)*invaii;
SIMD<double,4> scale1(&scale[0]), scale2(&scale[4]);
double * ptr = &a(i+1, r.First());
for (size_t j = i+1; j < n; j++, ptr+=a.Dist())
{
SIMD<double,4> row1(ptr);
SIMD<double,4> row2(ptr+4);
double fac = ptr[rest];
row1 += fac*scale1;
row2 += fac*scale2;
row1.Store(ptr);
row2.Store(ptr+4);
}
}
*/
else
{
if (i+1 < n)
a.Col(i).Range(i+1,n) *= 1.0/a(i,i);
if (i+1 < r.Next())
{
// RegionTimer reg(calcLUSimple2);
a.Rows(i+1,n).Cols(i+1,r.Next()) -= a.Rows(i+1,n).Cols(i,i+1) * a.Rows(i,i+1).Cols(i+1,r.Next());
/*
double mem[bs];
FlatMatrix<> row(1, r.Size(), &mem[0]);
row.Row(0) = a.Row(i).Range(r);
row.Row(0).Range(i-r.First()+1) = 0.0;
a.Rows(i+1,n).Cols(r) -= a.Rows(i+1,n).Cols(i,i+1) * row;
*/
}
}
}
return;
}
size_t half = r.Size()/2;
if (half > bs)
half = half - (half % bs);
size_t mid = r.First() + half;
IntRange r1(r.First(), mid);
IntRange r2(mid, r.Next());
CalcLURec (a, p, r1);
{
// RegionTimer r(calcLUSolveL);
TriangularSolve<LowerLeft,Normalized> (a.Rows(r1).Cols(r1), a.Rows(r1).Cols(r2));
}
{
// RegionTimer r(calcLUMatMat);
a.Rows(mid,n).Cols(r2) -= a.Rows(mid,n).Cols(r1) * a.Rows(r1).Cols(r2);
}
CalcLURec (a, p, r2);
}
void CalcLU (SliceMatrix<double> a, FlatArray<int> p)
{
size_t n = a.Height();
// static Timer t("CalcLU - rec"); RegionTimer reg(t);
// t.AddFlops (n*n*n/3);
for (size_t i = 0; i < n; i++) p[i] = i;
CalcLURec (a, p, IntRange(n));
}
// U .. upper right,
// L .. lower left, normalized
// static Timer tmulul1 ("MultUL - matmat");
// static Timer tmulul2 ("MultUL - trigmultR");
// static Timer tmulul3 ("MultUL - trigmultL");
void MultUL (SliceMatrix<> A)
{
size_t n = A.Height();
if (n <= 1) return;
if (n <= 8)
{
for (size_t i = 0; i < n; i++)
{
auto rowi = A.Row(i);
for (size_t j = 0; j < i; j++)
{
double sum = 0;
for (size_t k = i; k < n; k++)
sum += rowi(k) * A(k,j);
rowi(j) = sum;
}
for (size_t j = i; j < n; j++)
{
double sum = rowi(j);
for (size_t k = j+1; k < n; k++)
sum += rowi(k) * A(k,j);
rowi(j) = sum;
}
}
return;
}
IntRange r1(0,n/2), r2(n/2,n);
auto A11 = A.Rows(r1).Cols(r1);
auto A12 = A.Rows(r1).Cols(r2);
auto A21 = A.Rows(r2).Cols(r1);
auto A22 = A.Rows(r2).Cols(r2);
MultUL (A11);
// tmulul1.Start();
A11 += A12 * A21;
// tmulul1.Stop();
// tmulul1.AddFlops (r1.Size()*r1.Size()*r2.Size());
// tmulul2.Start();
TriangularMult<UpperRight> (A22, A21);
// tmulul2.Stop();
// tmulul3.Start();
// TriangularMult<UpperRight,Normalized> (Trans(A22), Trans(A12));
MultTriangular<LowerLeft,Normalized> (A12, A22);
// tmulul3.Stop();
MultUL (A22);
}
void InverseFromLU (SliceMatrix<double> A, FlatArray<int> p)
{
size_t n = A.Height();
/*
// testing: lapack-version
Matrix tmp = Trans(A);
ArrayMem<integer,100> ipiv(A.Height());
for (int i = 0; i < n; i++)
ipiv[i] = i+1;
integer lda = tmp.Dist();
integer lwork = 32 * A.Height();
Array<double> work(lwork);
integer info;
integer ni = A.Height();
dgetri(&ni, &tmp(0,0), &lda, &ipiv[0], work.Data(), &lwork, &info);
A = Trans(tmp);
if (info != 0)
cout << "info = " << info << endl;
*/
// static Timer t("InverseFromLU"); RegionTimer reg(t);
// t.AddFlops (2*n*n*n/3);
// static Timer tl("InvertL");
// static Timer tu("InvertU");
// static Timer tperm("permutation");
// tl.Start();
TriangularInvert<LowerLeft,Normalized> (A);
// tl.Stop();
// tl.AddFlops (n*n*n/6);
// tu.Start();
TriangularInvert<UpperRight> (A);
// tu.Stop();
// tu.AddFlops (n*n*n/6);
MultUL (A);
// RegionTimer rperm(tperm);
VectorMem<100> row(n);
for (size_t i = 0; i < n; i++)
{
auto rowi = A.Row(i);
for (size_t j = 0; j < n; j++)
row(p[j]) = rowi(j);
rowi = row;
}
}
void SolveFromLU (SliceMatrix<double> A, FlatArray<int> p, SliceMatrix<double,ColMajor> X)
{
size_t n = X.Height();
VectorMem<100,double> hv(n);
for (size_t i = 0; i < X.Width(); i++)
{
auto coli = X.Col(i);
hv = coli;
for (size_t j = 0; j < n; j++)
coli(j) = hv(p[j]);
}
TriangularSolve<LowerLeft,Normalized> (A, X);
TriangularSolve<UpperRight> (A, X);
}
void SolveTransFromLU (SliceMatrix<double> A, FlatArray<int> p, SliceMatrix<double,ColMajor> X)
{
TriangularSolve<LowerLeft> (Trans(A).Bare(), X);
TriangularSolve<UpperRight,Normalized> (Trans(A).Bare(), X);
size_t n = X.Height();
VectorMem<100,double> hv(n);
for (size_t i = 0; i < X.Width(); i++)
{
auto coli = X.Col(i);
hv = coli;
for (size_t j = 0; j < n; j++)
coli(p[j]) = hv(j);
}
}
}