Skip to content

Commit f6263f3

Browse files
author
zhakov
committed
parallel gauss, parallel CG added, but tests failed
1 parent 2ccf82a commit f6263f3

10 files changed

+186
-20
lines changed

Diff for: CGParallel.cpp

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* File: CGParallel.cpp
3+
* Author: zhakov
4+
* Решение СЛАУ методом сопряжённых градиентов
5+
*
6+
* Created on 13 Декабрь 2012 г., 21:37
7+
*/
8+
9+
#include "CGParallel.h"
10+
#include "stdlib.h"
11+
#include "math.h"
12+
13+
CGParallel::CGParallel() {
14+
}
15+
16+
CGParallel::CGParallel(const CGParallel& orig) {
17+
}
18+
19+
CGParallel::~CGParallel() {
20+
}
21+
22+
void CGParallel::resultCalculation(double** pMatrix, double* pVector, double* pResult, int Size) {
23+
double *CurrentApproximation, *PreviousApproximation;
24+
double *CurrentGradient, *PreviousGradient;
25+
double *CurrentDirection, *PreviousDirection;
26+
double *Denom, *tempPointer;
27+
double Step;
28+
int Iter = 1, MaxIter = Size + 1;
29+
float Accuracy = 0.0001f;
30+
//Allocating memory
31+
CurrentApproximation = new double[Size];
32+
PreviousApproximation = new double[Size];
33+
CurrentGradient = new double[Size];
34+
PreviousGradient = new double[Size];
35+
CurrentDirection = new double[Size];
36+
PreviousDirection = new double[Size];
37+
Denom = new double[Size];
38+
39+
for (int i = 0; i < Size; i++) {
40+
PreviousApproximation[i] = 0;
41+
PreviousDirection[i] = 0;
42+
PreviousGradient[i] = -pVector[i];
43+
}
44+
do {
45+
if (Iter > 1) {
46+
tempPointer = PreviousApproximation;
47+
PreviousApproximation = CurrentApproximation;
48+
CurrentApproximation = tempPointer;
49+
//SwapPointers(PreviousApproximation, CurrentApproximation);
50+
tempPointer = PreviousGradient;
51+
PreviousGradient = CurrentGradient;
52+
CurrentGradient = tempPointer;
53+
//SwapPointers(PreviousGradient, CurrentGradient);
54+
tempPointer = PreviousDirection;
55+
PreviousDirection = CurrentDirection;
56+
CurrentDirection = tempPointer;
57+
//SwapPointers(PreviousDirection, CurrentDirection);
58+
}
59+
//compute gradient
60+
#pragma omp parallel for
61+
for (int i = 0; i < Size; i++) {
62+
CurrentGradient[i] = -pVector[i];
63+
for (int j = 0; j < Size; j++)
64+
CurrentGradient[i] += pMatrix[i][j] * PreviousApproximation[j];
65+
}
66+
//compute direction
67+
double IP1 = 0, IP2 = 0;
68+
#pragma omp parallel for reduction(+:IP1,IP2)
69+
for (int i = 0; i < Size; i++) {
70+
IP1 += CurrentGradient[i] * CurrentGradient[i];
71+
IP2 += PreviousGradient[i] * PreviousGradient[i];
72+
}
73+
#pragma omp parallel for
74+
for (int i = 0; i < Size; i++) {
75+
CurrentDirection[i] = -CurrentGradient[i] +
76+
PreviousDirection[i] * IP1 / IP2;
77+
}
78+
//compute size step
79+
IP1 = 0;
80+
IP2 = 0;
81+
#pragma omp parallel for reduction(+:IP1,IP2)
82+
for (int i = 0; i < Size; i++) {
83+
Denom[i] = 0;
84+
for (int j = 0; j < Size; j++)
85+
Denom[i] += pMatrix[i][j] * CurrentDirection[j];
86+
IP1 += CurrentDirection[i] * CurrentGradient[i];
87+
IP2 += CurrentDirection[i] * Denom[i];
88+
}
89+
Step = -IP1 / IP2;
90+
//compute new approximation
91+
#pragma omp parallel for
92+
for (int i = 0; i < Size; i++) {
93+
CurrentApproximation[i] = PreviousApproximation[i] + Step * CurrentDirection[i];
94+
}
95+
Iter++;
96+
} while
97+
((diff(PreviousApproximation, CurrentApproximation, Size) > Accuracy)
98+
&& (Iter < MaxIter));
99+
for (int i = 0; i < Size; i++)
100+
pResult[i] = CurrentApproximation[i];
101+
102+
103+
delete CurrentApproximation;
104+
delete PreviousApproximation;
105+
delete CurrentGradient;
106+
delete PreviousGradient;
107+
delete CurrentDirection;
108+
delete PreviousDirection;
109+
delete Denom;
110+
}
111+
112+
double CGParallel::diff(double *vector1, double* vector2, int Size) {
113+
double sum = 0;
114+
115+
for (int i = 0; i < Size; i++) {
116+
sum += abs(vector1[i] - vector2[i]);
117+
}
118+
119+
return sum;
120+
}

Diff for: CGParallel.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* File: CGParallel.h
3+
* Author: zhakov
4+
*
5+
* Created on 13 Декабрь 2012 г., 21:37
6+
*/
7+
8+
#ifndef CGPARALLEL_H
9+
#define CGPARALLEL_H
10+
11+
class CGParallel {
12+
public:
13+
CGParallel();
14+
CGParallel(const CGParallel& orig);
15+
virtual ~CGParallel();
16+
void resultCalculation(double** pMatrix, double* pVector, double* pResult, int Size);
17+
private:
18+
double diff(double *vector1, double* vector2, int Size);
19+
};
20+
21+
#endif /* CGPARALLEL_H */
22+

Diff for: gaussParallel.cpp

+18-10
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ int gaussParallel::resultCalculation(double** pMatrix, double* pVector, double*
3232

3333

3434
// Gaussian elimination
35-
serialGaussianElimination(pMatrix, pVector);
35+
gaussianElimination(pMatrix, pVector);
3636
// Back substitution
37-
serialBackSubstitution(pMatrix, pVector, pResult);
37+
backSubstitution(pMatrix, pVector, pResult);
3838

3939
return 0;
4040
}
@@ -61,18 +61,25 @@ int gaussParallel::findPivotRow(double** pMatrix, int Iter) {
6161
ThreadPivotRow.MaxValue = fabs(pMatrix[i][Iter]);
6262
}
6363
}
64-
printf("Local thread(id = %i) pivot row: %i\n", omp_get_thread_num(), ThreadPivotRow.PivotRow);
65-
}
64+
#pragma omp critical
65+
{
66+
if (ThreadPivotRow.MaxValue > MaxValue) {
67+
MaxValue = ThreadPivotRow.MaxValue;
68+
PivotRow = ThreadPivotRow.PivotRow;
69+
}
70+
} // pragma omp critical
71+
}// pragma omp parallel
6672

6773
return PivotRow;
6874
}
6975

7076

7177
// Column elimination
7278

73-
int gaussParallel::serialColumnElimination(double** pMatrix, double* pVector, int Pivot, int Iter) {
79+
int gaussParallel::columnElimination(double** pMatrix, double* pVector, int Pivot, int Iter) {
7480
double PivotValue, PivotFactor;
7581
PivotValue = pMatrix[Pivot][Iter];
82+
#pragma omp parallel for private (PivotFactor)
7683
for (int i = 0; i < mSize; i++) {
7784
if (pSerialPivotIter[i] == -1) {
7885
PivotFactor = pMatrix[i][Iter] / PivotValue;
@@ -89,7 +96,7 @@ int gaussParallel::serialColumnElimination(double** pMatrix, double* pVector, in
8996

9097
// Gaussian elimination
9198

92-
int gaussParallel::serialGaussianElimination(double** pMatrix, double* pVector) {
99+
int gaussParallel::gaussianElimination(double** pMatrix, double* pVector) {
93100
int Iter;
94101
// The Number of the iteration of the gaussian
95102
// elimination
@@ -101,21 +108,22 @@ int gaussParallel::serialGaussianElimination(double** pMatrix, double* pVector)
101108
PivotRow = findPivotRow(pMatrix, Iter);
102109
pSerialPivotPos[Iter] = PivotRow;
103110
pSerialPivotIter[PivotRow] = Iter;
104-
serialColumnElimination(pMatrix, pVector, PivotRow, Iter);
111+
columnElimination(pMatrix, pVector, PivotRow, Iter);
105112
}
106113

107114

108115

109116
return 0;
110117
}
111118

112-
//Обратный ход метода Гаусса
113-
114-
int gaussParallel::serialBackSubstitution(double** pMatrix, double* pVector, double* pResult) {
119+
/* Обратный ход метода Гаусса
120+
*/
121+
int gaussParallel::backSubstitution(double** pMatrix, double* pVector, double* pResult) {
115122
int RowIndex, Row;
116123
for (int i = mSize - 1; i >= 0; i--) {
117124
RowIndex = pSerialPivotPos[i];
118125
pResult[i] = pVector[RowIndex] / pMatrix[RowIndex][i];
126+
#pragma omp parallel for private (Row)
119127
for (int j = 0; j < i; j++) {
120128
Row = pSerialPivotPos[j];
121129
pVector[j] -= pMatrix[Row][i] * pResult[i];

Diff for: gaussParallel.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class gaussParallel {
2121
int* pSerialPivotIter; // The iterations, at which the rows were pivots
2222

2323
int findPivotRow(double** pMatrix, int Iter);
24-
int serialGaussianElimination(double** pMatrix, double* pVector);
25-
int serialBackSubstitution(double** pMatrix, double* pVector, double* pResult);
26-
int serialColumnElimination(double** pMatrix, double* pVector, int Pivot, int Iter);
24+
int gaussianElimination(double** pMatrix, double* pVector);
25+
int backSubstitution(double** pMatrix, double* pVector, double* pResult);
26+
int columnElimination(double** pMatrix, double* pVector, int Pivot, int Iter);
2727
};
2828

2929
#endif /* GAUSSPARALLEL_H */

Diff for: main.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ int main(int argc, char** argv) {
126126

127127

128128

129-
129+
//Проверяем результат
130+
matrixHelpers::testSolvingResult(pMatrix, pVector, pResult, mSize);
131+
130132

131133

132134
//Потраченное время

Diff for: matrixHelpers.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ int matrixHelpers::testSolvingResult(double** pMatrix, double* pVector, double*
6666
}
6767
}
6868
if (equal == 1) {
69-
printf("The result of the parallel Gauss algorithm is NOT correct."
70-
"Check your code.");
69+
printf("The result of the algorithm is NOT correct."
70+
"Check your code.\n");
7171
} else {
72-
printf("The result of the parallel Gauss algorithm is correct.");
72+
printf("The result of the algorithm is correct.\n");
7373
}
7474

7575
delete [] pRightPartVector;

Diff for: nbproject/Makefile-Debug.mk

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}
3636
# Object Files
3737
OBJECTFILES= \
3838
${OBJECTDIR}/main.o \
39+
${OBJECTDIR}/CGParallel.o \
3940
${OBJECTDIR}/matrixHelpers.o \
4041
${OBJECTDIR}/dataGen.o \
4142
${OBJECTDIR}/gaussSerial.o \
@@ -71,6 +72,11 @@ ${OBJECTDIR}/main.o: main.cpp
7172
${RM} $@.d
7273
$(COMPILE.cc) -g -MMD -MP -MF $@.d -o ${OBJECTDIR}/main.o main.cpp
7374

75+
${OBJECTDIR}/CGParallel.o: CGParallel.cpp
76+
${MKDIR} -p ${OBJECTDIR}
77+
${RM} $@.d
78+
$(COMPILE.cc) -g -MMD -MP -MF $@.d -o ${OBJECTDIR}/CGParallel.o CGParallel.cpp
79+
7480
${OBJECTDIR}/matrixHelpers.o: matrixHelpers.cpp
7581
${MKDIR} -p ${OBJECTDIR}
7682
${RM} $@.d

Diff for: nbproject/Makefile-Release.mk

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}
3636
# Object Files
3737
OBJECTFILES= \
3838
${OBJECTDIR}/main.o \
39+
${OBJECTDIR}/CGParallel.o \
3940
${OBJECTDIR}/matrixHelpers.o \
4041
${OBJECTDIR}/dataGen.o \
4142
${OBJECTDIR}/gaussSerial.o \
@@ -71,6 +72,11 @@ ${OBJECTDIR}/main.o: main.cpp
7172
${RM} $@.d
7273
$(COMPILE.cc) -O2 -MMD -MP -MF $@.d -o ${OBJECTDIR}/main.o main.cpp
7374

75+
${OBJECTDIR}/CGParallel.o: CGParallel.cpp
76+
${MKDIR} -p ${OBJECTDIR}
77+
${RM} $@.d
78+
$(COMPILE.cc) -O2 -MMD -MP -MF $@.d -o ${OBJECTDIR}/CGParallel.o CGParallel.cpp
79+
7480
${OBJECTDIR}/matrixHelpers.o: matrixHelpers.cpp
7581
${MKDIR} -p ${OBJECTDIR}
7682
${RM} $@.d

Diff for: nbproject/configurations.xml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<logicalFolder name="SourceFiles"
55
displayName="Исходные файлы"
66
projectFiles="true">
7+
<itemPath>CGParallel.cpp</itemPath>
8+
<itemPath>CGParallel.h</itemPath>
79
<itemPath>dataGen.cpp</itemPath>
810
<itemPath>dataGen.h</itemPath>
911
<itemPath>gaussParallel.cpp</itemPath>

Diff for: nbproject/private/configurations.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@
2424
</nativedebugger>
2525
<runprofile version="9">
2626
<runcommandpicklist>
27-
<runcommandpicklistitem>"${OUTPUT_PATH}"</runcommandpicklistitem>
2827
<runcommandpicklistitem>"${OUTPUT_PATH}" -m 64</runcommandpicklistitem>
2928
<runcommandpicklistitem>"${OUTPUT_PATH}" -m asdf</runcommandpicklistitem>
3029
<runcommandpicklistitem>"${OUTPUT_PATH}" -m 5</runcommandpicklistitem>
3130
<runcommandpicklistitem>"${OUTPUT_PATH}" -m 3000</runcommandpicklistitem>
3231
<runcommandpicklistitem>"${OUTPUT_PATH}" -m 500</runcommandpicklistitem>
3332
<runcommandpicklistitem>"${OUTPUT_PATH}" -m 500 -algorithm </runcommandpicklistitem>
3433
<runcommandpicklistitem>"${OUTPUT_PATH}" -m 500 -algorithm gauss-serial</runcommandpicklistitem>
35-
<runcommandpicklistitem>"${OUTPUT_PATH}" -msize 500 -algorithm gauss-serial</runcommandpicklistitem>
3634
<runcommandpicklistitem>"${OUTPUT_PATH}" -msize 500 -algorithm gauss-parallel</runcommandpicklistitem>
35+
<runcommandpicklistitem>"${OUTPUT_PATH}" -msize 500 -algorithm gauss-serial</runcommandpicklistitem>
36+
<runcommandpicklistitem>"${OUTPUT_PATH}" -msize 500 -algorithm CG-parallel</runcommandpicklistitem>
3737
</runcommandpicklist>
38-
<runcommand>"${OUTPUT_PATH}" -msize 500 -algorithm gauss-parallel</runcommand>
38+
<runcommand>"${OUTPUT_PATH}" -msize 500 -algorithm CG-parallel</runcommand>
3939
<rundir></rundir>
4040
<buildfirst>true</buildfirst>
4141
<terminal-type>0</terminal-type>

0 commit comments

Comments
 (0)