-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathSymMatrixSolver.cxx
More file actions
185 lines (166 loc) · 5.34 KB
/
SymMatrixSolver.cxx
File metadata and controls
185 lines (166 loc) · 5.34 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
// 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 SymMatrixSolver.cxx
/// \brief Implementation of SymMatrixSolver class
///
/// \author Sergey Gorbunov <sergey.gorbunov@cern.ch>
#include "SymMatrixSolver.h"
#include "GPUCommonLogger.h"
#include <iostream>
#include <random>
#include <iomanip>
#include <chrono>
using namespace std;
using namespace o2::gpu;
ClassImp(o2::gpu::SymMatrixSolver);
void SymMatrixSolver::solve()
{
// Upper Triangulization
for (int32_t i = 0; i < mN; i++) {
double* rowI = &mA[i * mShift];
double* rowIb = &mA[i * mShift + mN];
double c = (fabs(rowI[i]) > 1.e-10) ? 1. / rowI[i] : 0.;
double* rowJ = rowI + mShift;
for (int32_t j = i + 1; j < mN; j++, rowJ += mShift) { // row j
if (rowI[j] != 0.) {
double aij = c * rowI[j]; // A[i][j] / A[i][i]
for (int32_t k = j; k < mShift; k++) {
rowJ[k] -= aij * rowI[k]; // A[j][k] -= A[i][k]/A[i][i]*A[j][i]
}
rowI[j] = aij; // A[i][j] /= A[i][i]
}
}
for (int32_t k = 0; k < mM; k++) {
rowIb[k] *= c;
}
}
// Diagonalization
for (int32_t i = mN - 1; i >= 0; i--) {
double* rowIb = &mA[i * mShift + mN];
double* rowJb = rowIb - mShift;
for (int32_t j = i - 1; j >= 0; j--, rowJb -= mShift) { // row j
double aji = mA[j * mShift + i];
if (aji != 0.) {
for (int32_t k = 0; k < mM; k++) {
rowJb[k] -= aji * rowIb[k];
}
}
}
}
}
void SymMatrixSolver::print()
{
for (int32_t i = 0; i < mN; i++) {
LOG(info) << "";
for (int32_t j = 0; j < mN; j++) {
LOG(info) << std::fixed << std::setw(5) << std::setprecision(2) << A(i, j) << " ";
}
LOG(info) << " | ";
for (int32_t j = 0; j < mM; j++) {
LOG(info) << std::fixed << std::setw(5) << std::setprecision(2) << B(i, j) << " ";
}
}
LOG(info) << std::setprecision(-1);
}
int32_t SymMatrixSolver::test(bool prn)
{
constexpr int32_t n = 30;
constexpr int32_t d = 3;
// std::random_device rd; // Will be used to obtain a seed for the random
std::mt19937 gen(1); // Standard mersenne_twister_engine seeded with 1
std::uniform_real_distribution<> uniform(-.999, .999);
double maxDiff = 0.;
int32_t nTries = 10000;
auto tmpTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(tmpTime - tmpTime);
auto durationMult = duration;
for (int32_t iter = 0; iter < nTries; iter++) {
double x[n][d];
double A[n][n];
{
for (int32_t i = 0; i < n; i++) {
for (int32_t j = 0; j < d; j++) {
x[i][j] = 1. * uniform(gen);
}
}
for (int32_t i = 0; i < n; i++) {
A[i][i] = fabs(2. + uniform(gen));
}
for (int32_t i = 0; i < n; i++) {
for (int32_t j = i + 1; j < n; j++) {
A[i][j] = A[i][i] * A[j][j] * uniform(gen);
A[j][i] = A[i][j];
}
}
for (int32_t i = 0; i < n; i++) {
A[i][i] = A[i][i] * A[i][i];
}
if (prn && iter == nTries - 1) {
for (int32_t i = 0; i < n; i++) {
LOG(info) << "";
for (int32_t j = 0; j < n; j++) {
LOG(info) << std::fixed << std::setw(5) << std::setprecision(2) << A[i][j] << " ";
}
}
LOG(info) << "";
}
}
double b[n][d];
auto startMult = std::chrono::high_resolution_clock::now();
for (int32_t i = 0; i < n; i++) {
for (int32_t k = 0; k < d; k++) {
b[i][k] = 0.;
}
for (int32_t j = 0; j < n; j++) {
for (int32_t k = 0; k < d; k++) {
b[i][k] += x[j][k] * A[i][j];
}
}
}
auto stopMult = std::chrono::high_resolution_clock::now();
durationMult += std::chrono::duration_cast<std::chrono::nanoseconds>(stopMult - startMult);
SymMatrixSolver sym(n, d);
for (int32_t i = 0; i < n; i++) {
for (int32_t k = 0; k < d; k++) {
sym.B(i, k) = b[i][k];
}
for (int32_t j = i; j < n; j++) {
sym.A(i, j) = A[i][j];
}
}
auto start = std::chrono::high_resolution_clock::now();
sym.solve();
auto stop = std::chrono::high_resolution_clock::now();
duration += std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
double diff = 0.;
for (int32_t i = 0; i < n; i++) {
for (int32_t k = 0; k < d; k++) {
double t = fabs(x[i][k] - sym.B(i, k));
if (diff < t) {
diff = t;
}
}
}
if (maxDiff < diff) {
maxDiff = diff;
}
// LOG(info) << std::defaultfloat ;
// LOG(info) << "\n\n max diff " <<diff << "\n";
}
int32_t ok = (maxDiff < 1.e-7);
if (prn || !ok) {
LOG(info) << std::defaultfloat;
LOG(info) << "\n\n Overall max diff " << maxDiff << "\n";
LOG(info) << " time " << duration.count() / nTries;
LOG(info) << " time multiplication " << durationMult.count() / nTries;
}
return ok;
}