-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.h
82 lines (63 loc) · 3.89 KB
/
runner.h
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
#ifndef MATRIX_RUNNER_H
#define MATRIX_RUNNER_H
#include <Eigen/Sparse>
#include <Eigen/Dense>
#include <solver/IterativeSolver.h>
#include <solver/Solver.h>
#include <util/MatrixReader.h>
#include <iostream>
#include <updateStrategy/ConjugateGradientUpdateStrategy.h>
#include <updateStrategy/GradientUpdateStrategy.h>
#include <updateStrategy/GaussSeidelUpdateStrategy.h>
#include <updateStrategy/JacobiUpdateStrategy.h>
#include <util/Benchmark.h>
#include <memory>
#include <solver/NormType.h>
using namespace UpdateStrategy;
template<typename Precision>
std::vector<IterativeBenchmark<Precision, Eigen::SparseMatrix<Precision>>> testMethods(const std::string &filename,
Precision tolerance,
bool skipMatrixCheck,
Precision gaussW,
Precision jacobiW,
std::string matrixName = "",
NormType normType = NormType::EUCLIDEAN) {
Eigen::SparseMatrix<Precision> A = MatrixReader::readSparseFromFile<Precision>(filename);
Eigen::Matrix<Precision, Eigen::Dynamic, 1> x(A.rows(), 1);
x.setOnes();
Eigen::Matrix<Precision, Eigen::Dynamic, 1> b = A * x;
std::vector<std::shared_ptr<Strategy<Precision, Eigen::SparseMatrix<Precision>>>> methods {
std::make_shared<GradientUpdateStrategy<Precision, Eigen::SparseMatrix<Precision>>>(),
std::make_shared<ConjugateGradientUpdateStrategy<Precision, Eigen::SparseMatrix<Precision>>>(),
std::make_shared<JacobiUpdateStrategy<Precision, Eigen::SparseMatrix<Precision>>>(jacobiW),
std::make_shared<GaussSeidelUpdateStrategy<Precision, Eigen::SparseMatrix<Precision>>>(gaussW),
};
std::vector<IterativeBenchmark<Precision, Eigen::SparseMatrix<Precision>>> results;
for (auto &strategyPointer : methods) {
auto benchmark = IterativeBenchmark<Precision, Eigen::SparseMatrix<Precision>>(matrixName);
benchmark.run(A, b, 20000, tolerance, strategyPointer, x, skipMatrixCheck, normType);
results.push_back(benchmark);
for (int i = 0; i < b.rows(); ++i) {
std::cout << (*benchmark.solution())(i, 0) << "; ";
}
std:: cout << std::endl;
}
std::cout << results[0].conditionNumber() << std::endl;
return results;
}
template<typename Precision>
std::vector<IterativeBenchmark<Precision, Eigen::SparseMatrix<Precision>>> testMethods(const std::string &filename,
bool skipMatrixCheck,
std::string matrixName = "",
Precision gaussW = 1,
Precision jacobiW = 1,
NormType normType = NormType::EUCLIDEAN) {
std::vector<Precision> testTolerances = {1e-4, 1e-6, 1e-8, 1e-10};
std::vector<IterativeBenchmark<Precision, Eigen::SparseMatrix<Precision>>> results;
for (Precision tol : testTolerances) {
std::vector<IterativeBenchmark<Precision, Eigen::SparseMatrix<Precision>>> benchMethods = testMethods<Precision>(filename, tol, skipMatrixCheck, gaussW, jacobiW, matrixName, normType);
results.insert(results.end(), benchMethods.begin(), benchMethods.end());
}
return results;
}
#endif