-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
221 lines (165 loc) · 3.92 KB
/
main.cpp
File metadata and controls
221 lines (165 loc) · 3.92 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
#include <iostream>
#include <fstream>
#include <cmath>
#include <omp.h>
#include <vector>
#include <array>
#include <algorithm>
const double EPS = 0.0000001;
using namespace std;
// Template alias - C++11
//template <size_t LEN>
// Upgraded typedef - C++11
using Matrix1D = std::vector<double>;
//template <class T, size_t ROW, size_t COL>
using Matrix2D = std::vector<std::vector<double> >;
class LinearSystem
{
public:
int dimension;
Matrix2D A;
Matrix1D F;
Matrix1D X;
double time_simp;
double time_omp;
void load_data();
void solve_casual();
void solve_omp();
void init_data();
void display_1d(Matrix1D);
void display_2d(Matrix2D);
LinearSystem(int N) {
srand(time(0));
dimension = N;
A.resize(dimension);
F.resize(dimension);
X.resize(dimension);
for (size_t i = 0; i<N; i++) {
A[i].resize(N);
}
time_omp = 0;
time_simp = 0;
cout << "Constructor is ok!" << endl;
}
// ~LinearSystem(void)
};
void LinearSystem::load_data()
{
cout << "Alive load data" << endl;
for (int i = 0; i < dimension; i++)
{
for (int j = 0; j < dimension; j++)
{
if (i == j)
A[i][j] = 100 * dimension + rand() % 300 * dimension;
else
A[i][j] = 1 + rand() % 100;
}
F[i] = 1 + rand() % 10;
X[i] = 1;
}
}
void LinearSystem::display_1d(Matrix1D arg)
{
for (size_t i = 0; i < dimension; i++)
cout << arg[i] << endl;
cout << "=========================";
}
void LinearSystem::display_2d(Matrix2D arg)
{
for (size_t i = 0; i < dimension; i++) {
for (int g = 0; g < dimension; g++)
cout << arg[i][g] << " ";
cout << endl;
}
cout << endl;
}
/// N - размерность матрицы; A[N][N] - матрица коэффициентов, F[N] - столбец свободных членов,
/// X[N] - начальное приближение, также ответ записывается в X[N];
void LinearSystem::solve_casual()
{
int N = dimension;
int g;
double time1, time2;
Matrix1D TempX(N);
// Норма, определяемая как наибольшая разность столбца иксов соседней итерации
double norm;
time1 = omp_get_wtime();
do {
for (int i = 0; i < N; i++)
{
TempX[i] = F[i];
for (g = 0; g < N; g++)
{
if (i != g)
TempX[i] -= A[i][g] * X[g];
}
TempX[i] /= A[i][i];
}
norm = fabs(X[0] - TempX[0]);
for (int h = 0; h < N; h++) {
if (fabs(X[h] - TempX[h]) > norm)
norm = fabs(X[h] - TempX[h]);
X[h] = TempX[h];
}
}
while (norm > EPS);
time2 = omp_get_wtime();
//display_data(X);
time_simp = time2 - time1;
}
void LinearSystem::solve_omp()
{
int N = dimension;
int g;
int i;
double t1, t2;
Matrix1D TempX(N);
double norm;
t1 = omp_get_wtime();
do
{
#pragma omp parallel for private(i,g,norm) shared(TempX)
for (i = 0; i < N; i++)
{
TempX[i] = F[i];
for (g = 0; g < N; g++)
{
if (i != g)
TempX[i] -= A[i][g] * X[g];
}
TempX[i] /= A[i][i];
}
norm = fabs(X[0] - TempX[0]);
for (int h = 0; h < N; h++) {
if (fabs(X[h] - TempX[h]) > norm)
norm = fabs(X[h] - TempX[h]);
X[h] = TempX[h];
}
}
while (norm > EPS);
t2 = omp_get_wtime();
//display_data(X);
time_omp = t2 - t1;
}
void LinearSystem::init_data() {
for (size_t i = 0; i < dimension; i++)
X[i] = 1;
}
int main(int argc, char* argv[])
{
int num; // = atoi(argv[2]);
cin >> num;
int dimension; //= atoi(argv[1]);
cin >> dimension;
omp_set_num_threads(num);
cout << "I started\n";
LinearSystem *ls = new LinearSystem(dimension);
ls->load_data();
ls->solve_omp();
ls->init_data();
ls->solve_casual();
cout << "Casual time: " << ls->time_simp << endl;
cout << "OpenMP time: " << ls->time_omp << endl;
return 0;
}