-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequential.cpp
More file actions
167 lines (156 loc) · 4.26 KB
/
Copy pathsequential.cpp
File metadata and controls
167 lines (156 loc) · 4.26 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
#include <bits/stdc++.h>
#include <fstream>
#include <chrono>
#include <time.h>
#include "constants.h"
using namespace std;
vector<vector<double>> A(N, vector<double>(N, 0.0));
vector<vector<double>> PA(N, vector<double>(N, 0.0));
vector<vector<double>> LU(N, vector<double>(N, 0.0));
vector<vector<double>> residual(N, vector<double>(N, 0.0));
double temp_A[N][N];
double L[N][N], U[N][N];
int P[N][N];
int pi[N];
void inputMatrix(){
random_device rd;
default_random_engine generator(rd());
uniform_real_distribution<double> distribution(0.0, 1.0);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
A[i][j] = distribution(generator);
}
}
}
void initOutputs(){
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
U[i][j] = A[i][j];
L[i][j] = A[i][j];
temp_A[i][j] = A[i][j];
}
}
for(int i = 0; i < N; i++){
pi[i] = i;
L[i][i] = 1.0;
}
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
if(i>j){
U[i][j] = 0;
}
if(i<j){
L[i][j] = 0;
}
}
}
}
void LUdecompose(){
ofstream fout;
#ifdef TIMING
fout.open(DEBUG_OUT_FILE_2, ios::app);
double total_A_time = 0.0;
#endif
auto start_time = chrono::high_resolution_clock::now();
for (int k=0; k<N; k++){
double maxi = 0.0;
int temp_k = k;
for (int i=k; i<N; i++){
if (maxi < temp_A[i][k]){
maxi = temp_A[i][k];
temp_k = i;
}
}
#ifdef DEBUG
if(maxi == 0.0){
perror("Singular matrix");
}
#endif
U[k][k] = temp_A[temp_k][k];
swap(pi[k], pi[temp_k]);
swap(temp_A[k], temp_A[temp_k]);
for(int i=0; i<k; i++){
swap(L[k][i], L[temp_k][i]);
}
for(int i=k+1; i<N; i++){
L[i][k] = (temp_A[i][k]*1.0)/U[k][k];
U[k][i] = temp_A[k][i];
}
#ifdef TIMING
auto start_A_time = chrono::high_resolution_clock::now();
#endif
for(int i=k+1; i<N; i++){
for(int j=k+1; j<N; j++){
temp_A[i][j] -= L[i][k]*U[k][j];
}
}
#ifdef TIMING
auto end_A_time = chrono::high_resolution_clock::now();
double time_taken_A = chrono::duration_cast<chrono::nanoseconds>(end_A_time - start_A_time).count();
total_A_time += time_taken_A;
fout << "A time: " << time_taken_A << " ns" << endl;
#endif
}
#ifdef TIMING
fout << "Total A time: " << total_A_time << " ns" << endl;
fout.close();
#endif
for(int i=0; i<N; i++){
P[i][pi[i]] = 1;
}
auto end_time = chrono::high_resolution_clock::now();
double time_taken = chrono::duration_cast<chrono::milliseconds>(end_time - start_time).count();
fout.open(LOG_OUT_FILE, ios::app);
fout << "-----------------------------------------------\n";
fout << "N=" << N << ", Sequential,"<< " Threads=1, " << time_taken << " ms" << endl;
#ifdef TIMING
fout << "Total A update time SEQ: " << total_A_time << " ns" << endl;
#endif
fout.close();
return;
}
void verifyLU(){
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
PA[i][j] = 0.0;
LU[i][j] = 0.0;
for(int k=0; k<N; k++){
PA[i][j] += P[i][k]*A[k][j];
LU[i][j] += L[i][k]*U[k][j];
}
}
}
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
residual[i][j] = PA[i][j] - LU[i][j];
}
}
#ifdef DEBUG_LU_VERIFY
ofstream fout;
fout.open(LU_VERIFY_OUT, ios::app);
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
fout << residual[i][j] << " ";
}
fout << endl;
}
fout.close();
#endif
double norm = 0.0;
for(int j=0; j<N; j++){
double col_eucledian_norm = 0.0;
for(int i=0; i<N; i++){
col_eucledian_norm += pow(residual[i][j], 2);
}
norm += sqrt(col_eucledian_norm);
}
cout << "L-2,1 Norm of residual: " << norm << endl;
}
int main(){
inputMatrix();
initOutputs();
LUdecompose();
verifyLU();
}