-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlect3.cpp
166 lines (130 loc) · 3.12 KB
/
lect3.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
#include <stdio.h>
#include <ctime>
#include <iostream>
#include <cilk/cilk.h>
#include <cilk/reducer_opadd.h>
#include <chrono>
using namespace std::chrono;
using namespace std;
const int MATRIX_SIZE = 1500;
void InitMatrix(double** matrix)
{
for (int i = 0; i < MATRIX_SIZE; ++i)
{
matrix[i] = new double[MATRIX_SIZE + 1];
}
for (int i = 0; i < MATRIX_SIZE; ++i)
{
for (int j = 0; j <= MATRIX_SIZE; ++j)
{
matrix[i][j] = rand() % 2500 + 1;
}
}
}
void SerialGaussMethod(double **matrix, const int rows, double* result)
{
int k;
double koef;
high_resolution_clock::time_point start;
high_resolution_clock::time_point end;
duration<double> duration;
start = high_resolution_clock::now();
for (k = 0; k < rows; ++k)
{
for (int i = k + 1; i < rows; ++i)
{
koef = -matrix[i][k] / matrix[k][k];
for (int j = k; j <= rows; ++j)
{
matrix[i][j] += koef * matrix[k][j];
}
}
}
end = high_resolution_clock::now();
duration = (end - start);
cout << "Serial inner cycle duration : " << duration.count() << " seconds" << endl;
result[rows - 1] = matrix[rows - 1][rows] / matrix[rows - 1][rows - 1];
for (k = rows - 2; k >= 0; --k)
{
result[k] = matrix[k][rows];
for (int j = k + 1; j < rows; ++j)
{
result[k] -= matrix[k][j] * result[j];
}
result[k] /= matrix[k][k];
}
}
void ParallelGauss(double **matrix, const int rows, double* result)
{
int k;
double koef;
high_resolution_clock::time_point start;
high_resolution_clock::time_point end;
duration<double> duration;
start = high_resolution_clock::now();
for (k = 0; k < rows; ++k)
{
cilk_for (int i = k + 1; i < rows; ++i)
{
koef = -matrix[i][k] / matrix[k][k];
for (int j = k; j <= rows; ++j)
{
matrix[i][j] += koef * matrix[k][j];
}
}
}
end = high_resolution_clock::now();
duration = (end - start);
cout << "Parallel inner cycle duration : " << duration.count() << " seconds" << endl;
result[rows - 1] = matrix[rows - 1][rows] / matrix[rows - 1][rows - 1];
for (k = rows - 2; k >= 0; --k)
{
result[k] = matrix[k][rows];
for (int j = k + 1; j < rows; ++j)
{
result[k] -= matrix[k][j] * result[j];
}
result[k] /= matrix[k][k];
}
}
void ParallelGaussJordon(double **matrix, const int rows, double* result)
{
int k;
double koef;
for (k = 0; k < rows; ++k)
cilk_for (int i = 0; i < rows; ++i)
{
if (i != k)
{
koef = -matrix[i][k] / matrix[k][k];
for(int j = k; j <= rows; ++j)
matrix[i][j] += koef * matrix[k][j];
}
}
for (int i = 0; i < rows; ++i)
result[i] = matrix[i][rows] / matrix[i][i];
}
int main()
{
srand((unsigned)time(0));
int i;
const int test_matrix_lines = MATRIX_SIZE;
double **test_matrix = new double*[test_matrix_lines];
InitMatrix(test_matrix);
double *result = new double[test_matrix_lines];
SerialGaussMethod(test_matrix, test_matrix_lines, result);
ParallelGauss(test_matrix, test_matrix_lines, result);
for (i = 0; i < test_matrix_lines; ++i)
{
delete[]test_matrix[i];
}
/*
cout << "Solution:" << endl;
for (i = 0; i < test_matrix_lines; ++i)
{
printf("x(%d) = %lf\n", i, result[i]);
}
*/
delete[] result;
return 0;
}