-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
209 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,38 @@ | ||
### A simple version of CG (Conjugate Gradient). | ||
|
||
##### Benchmark results in file `bench_results.txt` | ||
##### Benchmark results | ||
|
||
Benchmark CG | ||
............................................................................... | ||
|
||
benchmark name samples iterations est run time | ||
mean low mean high mean | ||
std dev low std dev high std dev | ||
------------------------------------------------------------------------------- | ||
Size 256 5 1 158.155 ms | ||
30.6362 ms 30.3948 ms 30.8777 ms | ||
278.589 us 168.836 us 367.73 us | ||
|
||
Size 512 5 1 474.875 ms | ||
93.5246 ms 93.1857 ms 93.8352 ms | ||
384.98 us 251.424 us 502.811 us | ||
|
||
Size 1024 5 1 1.28954 s | ||
254.104 ms 253.491 ms 254.573 ms | ||
642.205 us 488.519 us 765.256 us | ||
|
||
Size 2048 5 1 3.11173 s | ||
618.631 ms 616.151 ms 621.485 ms | ||
2.99239 ms 2.11292 ms 3.53132 ms | ||
|
||
Size 4096 5 1 6.72483 s | ||
1.33877 s 1.33262 s 1.3448 s | ||
7.21186 ms 4.33359 ms 10.6794 ms | ||
|
||
Size 8192 5 1 17.1783 s | ||
3.45489 s 3.44111 s 3.48345 s | ||
21.1881 ms 4.29948 ms 29.4589 ms | ||
|
||
Size 16384 5 1 38.4892 s | ||
7.63827 s 7.62766 s 7.65727 s | ||
15.5452 ms 5.99529 ms 21.0671 ms |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
#pragma once | ||
#include <sparse_matrix.h> | ||
#include <matrix.h> | ||
#include <vector.h> | ||
|
||
bool ConjugateGradient(const SparseMatrix<double>& a, const Vector<double>& b, Vector<double>& x); | ||
bool ConjugateGradient(const SparseMatrix<double>& a, const Vector<double>& b, Vector<double>& x); | ||
bool ConjugateGradient(const Matrix<double>& a, const Vector<double>& b, Vector<double>& x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,93 @@ | ||
#include <filesystem> | ||
#include <fstream> | ||
#include <sstream> | ||
#include "conjugate_gradient.h" | ||
#include "helpers.h" | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
#include <catch2/benchmark/catch_benchmark.hpp> | ||
|
||
void Prepare(TMatrix<double>& A, TVector<double>& b, const size_t n) { | ||
std::srand(std::time(nullptr)); | ||
A = TMatrix<double>::GenerateRandomSymmetricDefinite(n, EDefiniteness::Positive); | ||
b = TVector<double>::CreateRandom(n); | ||
void Prepare(SparseMatrix<double>& a, Vector<double>& b, const size_t n) { | ||
std::stringstream file_name; | ||
file_name << std::filesystem::current_path().string() << "/../" << "matrix_examples/sparse_spd/" << n; | ||
std::ifstream fstream; | ||
fstream.open(file_name.str()); | ||
REQUIRE(fstream.is_open()); | ||
fstream >> a; | ||
fstream.close(); | ||
REQUIRE(a.data_); | ||
NHelpers::GenRandomVector(b, n, true); | ||
Vector<double> x(n); | ||
} | ||
|
||
TEST_CASE("Benchmark CG") { | ||
TMatrix<double> A; | ||
TVector<double> b; | ||
TVector<double> x; | ||
TVector<double> result; | ||
{ | ||
Prepare(A, b, 10); | ||
BENCHMARK("Size 10") { | ||
ConjugateGradient(A, b, x); | ||
SparseMatrix<double> a_sparse; | ||
Matrix<double> a_dense; | ||
Vector<double> b; | ||
Vector<double> x; | ||
Prepare(a_sparse, b, 256); | ||
BENCHMARK("Size 256") { | ||
ConjugateGradient(a_sparse, b, x); | ||
}; | ||
REQUIRE(TMatrix<double>::MVMultiply(A, x, result)); | ||
REQUIRE(RoughEq<double, double>(TVector<double>::Norm2(result - b), 0, 0.001)); | ||
} | ||
{ | ||
Prepare(A, b, 500); | ||
BENCHMARK("Size 500") { | ||
ConjugateGradient(A, b, x); | ||
SparseMatrix<double> a_sparse; | ||
Matrix<double> a_dense; | ||
Vector<double> b; | ||
Vector<double> x; | ||
Prepare(a_sparse, b, 512); | ||
BENCHMARK("Size 512") { | ||
ConjugateGradient(a_sparse, b, x); | ||
}; | ||
REQUIRE(TMatrix<double>::MVMultiply(A, x, result)); | ||
REQUIRE(RoughEq<double, double>(TVector<double>::Norm2(result - b), 0, 0.001)); | ||
} | ||
{ | ||
Prepare(A, b, 1000); | ||
BENCHMARK("Size 1000") { | ||
ConjugateGradient(A, b, x); | ||
SparseMatrix<double> a_sparse; | ||
Matrix<double> a_dense; | ||
Vector<double> b; | ||
Vector<double> x; | ||
Prepare(a_sparse, b, 1024); | ||
BENCHMARK("Size 1024") { | ||
ConjugateGradient(a_sparse, b, x); | ||
}; | ||
} | ||
{ | ||
SparseMatrix<double> a_sparse; | ||
Matrix<double> a_dense; | ||
Vector<double> b; | ||
Vector<double> x; | ||
Prepare(a_sparse, b, 2048); | ||
BENCHMARK("Size 2048") { | ||
ConjugateGradient(a_sparse, b, x); | ||
}; | ||
} | ||
{ | ||
SparseMatrix<double> a_sparse; | ||
Matrix<double> a_dense; | ||
Vector<double> b; | ||
Vector<double> x; | ||
Prepare(a_sparse, b, 4096); | ||
BENCHMARK("Size 4096") { | ||
ConjugateGradient(a_sparse, b, x); | ||
}; | ||
} | ||
{ | ||
SparseMatrix<double> a_sparse; | ||
Matrix<double> a_dense; | ||
Vector<double> b; | ||
Vector<double> x; | ||
Prepare(a_sparse, b, 8192); | ||
BENCHMARK("Size 8192") { | ||
ConjugateGradient(a_sparse, b, x); | ||
}; | ||
} | ||
{ | ||
SparseMatrix<double> a_sparse; | ||
Matrix<double> a_dense; | ||
Vector<double> b; | ||
Vector<double> x; | ||
Prepare(a_sparse, b, 16384); | ||
BENCHMARK("Size 16384") { | ||
ConjugateGradient(a_sparse, b, x); | ||
}; | ||
REQUIRE(TMatrix<double>::MVMultiply(A, x, result)); | ||
REQUIRE(RoughEq<double, double>(TVector<double>::Norm2(result - b), 0, 0.01)); | ||
} | ||
} |
Oops, something went wrong.