-
Notifications
You must be signed in to change notification settings - Fork 0
/
gaussParallel.cpp
135 lines (108 loc) · 3.62 KB
/
gaussParallel.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
/*
* File: gaussParallel.cpp
* Author: zhakov
*
* Created on 10 Декабрь 2012 г., 23:23
*/
#include "gaussParallel.h"
#include "omp.h"
#include "math.h"
#include "types.h"
#include "stdio.h"
#include "matrixHelpers.h"
gaussParallel::gaussParallel(int size) {
mSize = size;
pSerialPivotIter = new int[size];
pSerialPivotPos = new int[size];
//Заполняем -1, чтобы было понятно, что в эти строки мы ещё не заходили
for (int i = 0; i < size; i++) {
pSerialPivotIter[i] = -1;
}
}
gaussParallel::gaussParallel(const gaussParallel& orig) {
}
gaussParallel::~gaussParallel() {
}
int gaussParallel::resultCalculation(double** pMatrix, double* pVector, double* pResult) {
// Gaussian elimination
gaussianElimination(pMatrix, pVector);
// Back substitution
backSubstitution(pMatrix, pVector, pResult);
return 0;
}
// Function for finding the pivot row
int gaussParallel::findPivotRow(double** pMatrix, int Iter) {
int PivotRow = -1; // The index of the pivot row
double MaxValue = 0; // The value of the pivot element
int i; // Loop variable
// Choose the row, that stores the maximum element
#pragma omp parallel
{
TThreadPivotRow ThreadPivotRow;
ThreadPivotRow.MaxValue = 0;
ThreadPivotRow.PivotRow = -1;
#pragma omp for
for (i = 0; i < mSize; i++) {
if ((pSerialPivotIter[i] == -1) && (fabs(pMatrix[i][Iter]) > ThreadPivotRow.MaxValue)) {
ThreadPivotRow.PivotRow = i;
ThreadPivotRow.MaxValue = fabs(pMatrix[i][Iter]);
}
}
#pragma omp critical
{
if (ThreadPivotRow.MaxValue > MaxValue) {
MaxValue = ThreadPivotRow.MaxValue;
PivotRow = ThreadPivotRow.PivotRow;
}
} // pragma omp critical
}// pragma omp parallel
return PivotRow;
}
// Column elimination
int gaussParallel::columnElimination(double** pMatrix, double* pVector, int Pivot, int Iter) {
double PivotValue, PivotFactor;
PivotValue = pMatrix[Pivot][Iter];
#pragma omp parallel for private (PivotFactor) schedule(dynamic,1)
for (int i = 0; i < mSize; i++) {
if (pSerialPivotIter[i] == -1) {
PivotFactor = pMatrix[i][Iter] / PivotValue;
for (int j = Iter; j < mSize; j++) {
pMatrix[i][j] -= PivotFactor * pMatrix[Pivot][j];
}
pVector[i] -= PivotFactor * pVector[Pivot];
}
}
return 0;
}
// Gaussian elimination
int gaussParallel::gaussianElimination(double** pMatrix, double* pVector) {
int Iter;
// The Number of the iteration of the gaussian
// elimination
int PivotRow;
// The Number of the current pivot row
for (Iter = 0; Iter < mSize; Iter++) {
// Finding the pivot row
PivotRow = findPivotRow(pMatrix, Iter);
pSerialPivotPos[Iter] = PivotRow;
pSerialPivotIter[PivotRow] = Iter;
columnElimination(pMatrix, pVector, PivotRow, Iter);
}
return 0;
}
/* Обратный ход метода Гаусса
*/
int gaussParallel::backSubstitution(double** pMatrix, double* pVector, double* pResult) {
int RowIndex, Row;
for (int i = mSize - 1; i >= 0; i--) {
RowIndex = pSerialPivotPos[i];
pResult[i] = pVector[RowIndex] / pMatrix[RowIndex][i];
#pragma omp parallel for private (Row)
for (int j = 0; j < i; j++) {
Row = pSerialPivotPos[j];
pVector[j] -= pMatrix[Row][i] * pResult[i];
pMatrix[Row][i] = 0;
}
}
return 0;
}