-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
133 lines (108 loc) · 3.48 KB
/
main.c
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
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "matrix.h"
#include "vector.h"
#include "optimization.h"
#include "optimization_omp.h"
#include "matrix_omp.h"
#include "optimization_mpi.h"
#include <time.h>
#include <mpi.h>
#include "matrix_mpi.h"
static struct Vector minusGrad(struct Vector x, struct SquareMatrix hessian, struct Vector rightEqVector) {
struct Vector hessX = dotProduct(hessian, x);
struct Vector diffVector = subtractVector(hessX, rightEqVector);
struct Vector result = minus(diffVector);
freeVector(hessX);
freeVector(diffVector);
return result;
}
static struct Vector minusGradOMP(struct Vector x, struct SquareMatrix hessian, struct Vector rightEqVector) {
struct Vector hessX = dotProductOMP(hessian, x);
struct Vector diffVector = subtractVector(hessX, rightEqVector);
struct Vector result = minus(diffVector);
freeVector(hessX);
freeVector(diffVector);
return result;
}
static struct Vector minusGradMPI(struct Vector x, struct SquareMatrix hessian, struct Vector rightEqVector) {
struct Vector hessX = dotProductMPI(hessian, x);
struct Vector diffVector = subtractVector(hessX, rightEqVector);
struct Vector result = minus(diffVector);
freeVector(hessX);
freeVector(diffVector);
return result;
}
void stdInMain() {
size_t size = 0;
scanf("%d", &size);
struct SquareMatrix hessian = readMatrixFromStdInSized(size);
struct Vector b = readVectorFromStdInSized(size);
struct Vector xPredicted = optimizeFletcherReevesOMP(hessian, b, minusGradOMP);
printVectorPrecise(xPredicted);
freeMatrix(hessian);
freeVector(b);
freeVector(xPredicted);
}
void stdInMPIMain(int argc, char *argv[]) {
int rank, procNum;
int size;
struct SquareMatrix hessian;
struct Vector b, xPredicted;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
scanf("%d", &size);
hessian = readMatrixFromStdInSized(size);
b = readVectorFromStdInSized(size);
}
MPI_Barrier(MPI_COMM_WORLD);
xPredicted = optimizeFletcherReevesMPI(hessian, b, minusGradMPI);
if (rank == 0) {
printVectorPrecise(xPredicted);
freeMatrix(hessian);
freeVector(b);
freeVector(xPredicted);
}
MPI_Finalize();
}
void testMPIMain(int argc, char *argv[]) {
int rank, procNum;
int size = 175;
struct SquareMatrix hessian = randomSymmetricMatrix(size);
struct Vector b = randomVector(size);
struct Vector xPredicted;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
xPredicted = optimizeFletcherReevesMPI(hessian, b, minusGradMPI);
if (rank == 0) {
freeMatrix(hessian);
freeVector(b);
freeVector(xPredicted);
}
MPI_Finalize();
}
void testMatMulMPI(int argc, char *argv[]) {
int rank, procNum;
int size = 9000;
struct SquareMatrix A = randomMatrix(size);
struct Vector b = randomVector(size);
struct Vector resMPI = initVector(size);
struct Vector resSequential;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
resSequential = dotProduct(A, b);
}
dotProductMPIBuffered(A, b, resMPI.vector);
if (rank == 0) {
double mae = meanAbsoluteErrorVector(resSequential, resMPI);
printf("Mean Absolute Error with sequential result: %lf", mae);
}
MPI_Finalize();
}
int main(int argc, char *argv[]) {
testMatMulMPI(argc, argv);
return 0;
}