-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab2.cpp
More file actions
122 lines (106 loc) · 2.61 KB
/
lab2.cpp
File metadata and controls
122 lines (106 loc) · 2.61 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
#include <omp.h>
#include <stdio.h>
#include <cmath>
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <locale>
using namespace std;
const double THETA = 0.1;
const int MAXITER = 10000;
const double EPS = 1e-7;
void generate(double* &matrix, double* &vector, int size)
{
int max = size * size;
double rowSum;
matrix = new double[max];
vector = new double[size];
for (int i = 0; i < size; i++)
{
rowSum = 0;
for (int j = 0; j < size; j++)
{
matrix[i * size + j] = i == j ? 1.0 : (rand() % size / static_cast<double>(max));
rowSum += matrix[i *size + j];
}
vector[i] = rowSum;
}
}
void showUsage()
{
cerr << "Usage: ./lab2 num_omp_thread task_size\r\n";
cerr << "(int)num_omp_thread - OMP threads count\r\n";
cerr << "(int)task_size - Matrix size\r\n";
}
int main(int argc, char** argv)
{
int num_threads, taskSize;
int iteration;
double timeStart;
double error;
double rightNorm;
double* matrix;
double* result;
double* vector;
double* prev;
double duration;
if (argc != 3 || sscanf(argv[1], "%d", &num_threads) != 1 || sscanf(argv[2], "%d", &taskSize) != 1)
{
showUsage();
return 1;
}
std::cerr << "Start generation.\r\n";
generate(matrix, vector, taskSize);
std::cerr << "Generation complete\r\n";
result = new double[taskSize];
prev = new double[taskSize];
rightNorm = 0;
for (int i = 0; i < taskSize; i++)
{
rightNorm += vector[i] * vector[i];
result[i] = prev[i] = 0.0;
}
rightNorm = sqrt(rightNorm);
error = 1.0;
iteration = 1;
timeStart = omp_get_wtime();
omp_set_num_threads(num_threads);
#pragma omp parallel
while (iteration < MAXITER && error > EPS)
{
error = 0.0;
#pragma omp parallel for
for (int i = 0; i < taskSize; i++)
{
result[i] = 0.0;
#pragma omp parallel for
for (int j = 0; j < taskSize; j++)
{
result[i] += prev[j] * matrix[i * taskSize + j];
}
result[i] -= vector[i];
error += result[i] * result[i];
}
error = sqrt(error) / rightNorm;
#pragma omp parallel for
for (int i = 0; i < taskSize; i++)
{
result[i] = prev[i] - THETA * result[i];
}
//std::cerr << "Iteration " << iteration <<". Error = " << error << "\r\n";
swap(prev, result);
iteration++;
}
swap(prev, result);
duration = (omp_get_wtime() - timeStart);
error = 0.0;
for (int i = 0; i < taskSize; i++)
{
error += (result[i] - 1.0) * (result[i] - 1.0);
}
cerr << "Calculation complete.\r\n";
cerr << "Iterations: " << iteration << "\r\n";
cerr << "Error: " << error << "\r\n";
cerr << "Time: " << duration << "\r\n";
cerr << "[" << result[0] << "," << result[1] << "," << result[2] << ",...," << result[taskSize - 1] << "]\r\n";
}