opm-simulators/tests/test_graphcoloring.cpp

398 lines
13 KiB
C++
Raw Normal View History

2018-06-11 05:03:36 -05:00
#include <config.h>
#include <dune/common/fmatrix.hh>
#include <dune/istl/bcrsmatrix.hh>
#include <dune/istl/paamg/graph.hh>
#include <opm/simulators/linalg/GraphColoring.hpp>
2018-06-11 05:03:36 -05:00
#define BOOST_TEST_MODULE GraphColoringTest
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
2018-06-12 06:18:07 -05:00
///! \brief check that all indices are represented in the new ordering.
void checkAllIndices(const std::vector<std::size_t>& ordering)
{
std::vector<int> counters(ordering.size(), 0);
2023-11-21 08:19:11 -06:00
for (auto index : ordering) {
2018-06-12 06:18:07 -05:00
++counters[index];
}
2023-11-21 08:19:11 -06:00
for (auto count : counters) {
BOOST_CHECK(count == 1);
2018-06-12 06:18:07 -05:00
}
}
2018-06-11 05:03:36 -05:00
BOOST_AUTO_TEST_CASE(TestWelschPowell)
{
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<double,1,1>>;
using Graph = Dune::Amg::MatrixGraph<Matrix>;
int N = 10;
Matrix matrix(N*N, N*N, 5, 0.4, Matrix::implicit);
2023-11-21 08:19:11 -06:00
for (int j = 0; j < N; j++) {
for (int i = 0; i < N; i++) {
auto index = j * 10 + i;
2018-06-11 05:03:36 -05:00
matrix.entry(index,index) = 1;
2023-11-21 08:19:11 -06:00
if (i > 0) {
2018-06-11 05:03:36 -05:00
matrix.entry(index,index-1) = 1;
}
2023-11-21 08:19:11 -06:00
if (i < N - 1) {
2018-06-11 05:03:36 -05:00
matrix.entry(index,index+1) = 1;
}
2023-11-21 08:19:11 -06:00
if (j > 0) {
2018-06-11 05:03:36 -05:00
matrix.entry(index,index-N) = 1;
}
2023-11-21 08:19:11 -06:00
if (j < N - 1) {
2018-06-11 05:03:36 -05:00
matrix.entry(index,index+N) = 1;
}
}
}
matrix.compress();
2018-06-12 06:18:07 -05:00
Graph graph(matrix);
auto colorsTuple = Opm::colorVerticesWelshPowell(graph);
2018-06-11 05:03:36 -05:00
const auto& colors = std::get<0>(colorsTuple);
2018-06-12 06:18:07 -05:00
const auto& verticesPerColor = std::get<2>(colorsTuple);
2018-06-11 05:03:36 -05:00
auto noColors = std::get<1>(colorsTuple);
auto firstCornerColor = colors[0];
BOOST_CHECK(noColors == 2);
// Check for checkerboard coloring
2023-11-21 08:19:11 -06:00
for (int j = 0, index = 0; j < N; j++) {
2018-06-11 05:03:36 -05:00
auto expectedColor = firstCornerColor;
2023-11-21 08:19:11 -06:00
for (int i = 0; i < N; i++) {
BOOST_CHECK(colors[index] == expectedColor);
2018-06-11 05:03:36 -05:00
index++;
expectedColor = (expectedColor + 1) % 2;
}
firstCornerColor=(firstCornerColor + 1) % 2;
}
2018-06-12 06:18:07 -05:00
auto newOrder = Opm::reorderVerticesPreserving(colors, noColors, verticesPerColor,
graph);
std::vector<std::size_t> colorIndex(noColors, 0);
std::partial_sum(verticesPerColor.begin(),
verticesPerColor.begin()+verticesPerColor.size()-1,
colorIndex.begin()+1);
2023-11-21 08:19:11 -06:00
for (auto vertex : graph) {
2018-06-12 06:18:07 -05:00
BOOST_CHECK(colorIndex[colors[vertex]]++ == newOrder[vertex]);
}
checkAllIndices(newOrder);
newOrder = Opm::reorderVerticesSpheres(colors, noColors, verticesPerColor,
graph, 0);
checkAllIndices(newOrder);
2018-06-11 05:03:36 -05:00
}
2023-11-20 08:35:27 -06:00
// The following tests verify the graph coloring in the context of revealing which rows
// can be operated on at the same time in the DILU preconditioner
BOOST_AUTO_TEST_CASE(TestColoredDiluParallelisms3x3Matrix)
{
/*
Matrix on this form:
|x |
| xx|
| xx|
We only expect a DILU dependency from the second to the third row,
hence row 1 and 2 should have color 0, row 3 should have color 1
*/
const int N = 3;
2023-12-04 05:40:55 -06:00
constexpr int bz = 3;
2023-11-20 08:35:27 -06:00
const int nonZeroes = 5;
// creating some shorthand typenames
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<double, bz, bz>>;
Matrix testMatrix(N, N, nonZeroes, Matrix::row_wise);
for (auto row = testMatrix.createbegin(); row != testMatrix.createend(); ++row) {
if (row.index() == 0) {
row.insert(row.index());
2023-11-21 08:19:11 -06:00
} else if (row.index() == 1) {
2023-11-20 08:35:27 -06:00
row.insert(row.index());
row.insert(row.index() + 1);
2023-11-21 08:19:11 -06:00
} else if (row.index() == 2) {
2023-11-20 08:35:27 -06:00
row.insert(row.index() - 1);
row.insert(row.index());
}
}
testMatrix[0][0][0][0] = 3.0;
testMatrix[1][1][0][0] = 1.0;
testMatrix[1][2][0][0] = 1.0;
testMatrix[2][1][0][0] = 1.0;
testMatrix[2][2][0][0] = 1.0;
Opm::SparseTable<std::size_t> coloring = Opm::getMatrixRowColoring(testMatrix, Opm::ColoringType::SYMMETRIC);
std::vector<std::vector<std::size_t>> correctColor = {{0, 1}, {2}};
2023-11-21 08:19:11 -06:00
for (std::size_t i = 0; i < correctColor.size(); ++i) {
for (std::size_t j = 0; j < correctColor[i].size(); ++j) {
2023-11-20 08:35:27 -06:00
BOOST_CHECK(coloring[i][j] == correctColor[i][j]);
}
}
coloring = Opm::getMatrixRowColoring(testMatrix, Opm::ColoringType::UPPER);
correctColor = {{0, 2}, {1}};
2023-11-21 08:19:11 -06:00
for (std::size_t i = 0; i < correctColor.size(); ++i) {
for (std::size_t j = 0; j < correctColor[i].size(); ++j) {
2023-11-20 08:35:27 -06:00
BOOST_CHECK(coloring[i][j] == correctColor[i][j]);
}
}
coloring = Opm::getMatrixRowColoring(testMatrix, Opm::ColoringType::LOWER);
correctColor = {{0, 1}, {2}};
2023-11-21 08:19:11 -06:00
for (std::size_t i = 0; i < correctColor.size(); ++i) {
for (std::size_t j = 0; j < correctColor[i].size(); ++j) {
2023-11-20 08:35:27 -06:00
BOOST_CHECK(coloring[i][j] == correctColor[i][j]);
}
}
}
BOOST_AUTO_TEST_CASE(TestColoredDiluParallelisms5x5Simple)
{
/*
Test matrix:
|xxx |
|xx |
|x xx |
| x |
| xx|
*/
const int N = 5;
2023-12-04 05:40:55 -06:00
constexpr int bz = 3;
2023-11-20 08:35:27 -06:00
const int nonZeroes = 11;
// creating some shorthand typenames
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<double, bz, bz>>;
Matrix testMatrix(N, N, nonZeroes, Matrix::row_wise);
for (auto row = testMatrix.createbegin(); row != testMatrix.createend(); ++row) {
if (row.index() == 0) {
row.insert(row.index());
row.insert(row.index()+1);
row.insert(row.index()+2);
2023-11-21 08:19:11 -06:00
} else if (row.index() == 1) {
2023-11-20 08:35:27 -06:00
row.insert(row.index());
row.insert(row.index() - 1);
2023-11-21 08:19:11 -06:00
} else if (row.index() == 2) {
2023-11-20 08:35:27 -06:00
row.insert(row.index() - 2);
row.insert(row.index());
row.insert(row.index() + 1);
2023-11-21 08:19:11 -06:00
} else if (row.index() == 3) {
2023-11-20 08:35:27 -06:00
row.insert(row.index());
2023-11-21 08:19:11 -06:00
} else if (row.index() == 4) {
2023-11-20 08:35:27 -06:00
row.insert(row.index() - 1);
row.insert(row.index());
}
}
testMatrix[0][0][0][0] = 1.0;
testMatrix[0][1][0][0] = 1.0;
testMatrix[0][2][0][0] = 1.0;
testMatrix[1][0][0][0] = 1.0;
testMatrix[1][1][0][0] = 1.0;
testMatrix[2][0][0][0] = 1.0;
testMatrix[2][2][0][0] = 1.0;
testMatrix[2][3][0][0] = 1.0;
testMatrix[3][3][0][0] = 1.0;
testMatrix[4][3][0][0] = 1.0;
testMatrix[4][4][0][0] = 1.0;
Opm::SparseTable<std::size_t> coloring = Opm::getMatrixRowColoring(testMatrix, Opm::ColoringType::SYMMETRIC);
std::vector<std::vector<std::size_t>> correctColor = {{0, 3, 4}, {1, 2}};
2023-11-21 08:19:11 -06:00
for (std::size_t i = 0; i < correctColor.size(); ++i) {
for (std::size_t j = 0; j < correctColor[i].size(); ++j) {
2023-11-20 08:35:27 -06:00
BOOST_CHECK(coloring[i][j] == correctColor[i][j]);
}
}
coloring = Opm::getMatrixRowColoring(testMatrix, Opm::ColoringType::UPPER);
correctColor = {{1, 3, 4}, {2}, {0}};
2023-11-21 08:19:11 -06:00
for (std::size_t i = 0; i < correctColor.size(); ++i) {
for (std::size_t j = 0; j < correctColor[i].size(); ++j) {
2023-11-20 08:35:27 -06:00
BOOST_CHECK(coloring[i][j] == correctColor[i][j]);
}
}
coloring = Opm::getMatrixRowColoring(testMatrix, Opm::ColoringType::LOWER);
correctColor = {{0, 3}, {1, 2, 4}};
2023-11-21 08:19:11 -06:00
for (std::size_t i = 0; i < correctColor.size(); ++i) {
for (std::size_t j = 0; j < correctColor[i].size(); ++j) {
2023-11-20 08:35:27 -06:00
BOOST_CHECK(coloring[i][j] == correctColor[i][j]);
}
}
}
BOOST_AUTO_TEST_CASE(TestColoredDiluParallelisms5x5Tridiagonal)
{
/*
Test matrix:
|xx |
|xxx |
| xxx |
| xxx|
| xx|
The tridiagonal structure will force a strictly serial computation stage
*/
const int N = 5;
2023-12-04 05:40:55 -06:00
constexpr int bz = 3;
2023-11-20 08:35:27 -06:00
const int nonZeroes = 13;
// creating some shorthand typenames
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<double, bz, bz>>;
Matrix testMatrix(N, N, nonZeroes, Matrix::row_wise);
for (auto row = testMatrix.createbegin(); row != testMatrix.createend(); ++row) {
if (row.index() == 0) {
row.insert(row.index());
row.insert(row.index()+1);
2023-11-21 08:19:11 -06:00
} else if (row.index() > 0 && row.index() < 4) {
2023-11-20 08:35:27 -06:00
row.insert(row.index() - 1);
row.insert(row.index());
row.insert(row.index() + 1);
2023-11-21 08:19:11 -06:00
} else if (row.index() == 4) {
2023-11-20 08:35:27 -06:00
row.insert(row.index() - 1);
row.insert(row.index());
}
}
testMatrix[0][0][0][0] = 1.0;
testMatrix[0][1][0][0] = 1.0;
testMatrix[1][0][0][0] = 1.0;
testMatrix[1][1][0][0] = 1.0;
testMatrix[1][2][0][0] = 1.0;
testMatrix[2][1][0][0] = 1.0;
testMatrix[2][2][0][0] = 1.0;
testMatrix[2][3][0][0] = 1.0;
testMatrix[3][2][0][0] = 1.0;
testMatrix[3][3][0][0] = 1.0;
testMatrix[3][4][0][0] = 1.0;
testMatrix[4][3][0][0] = 1.0;
testMatrix[4][4][0][0] = 1.0;
Opm::SparseTable<std::size_t> coloring = Opm::getMatrixRowColoring(testMatrix, Opm::ColoringType::SYMMETRIC);
std::vector<std::vector<std::size_t>> correctColor = {{0}, {1}, {2}, {3}, {4}};
for (std::size_t i = 0; i < correctColor.size(); ++i){
2023-11-21 08:19:11 -06:00
for (std::size_t j = 0; j < correctColor[i].size(); ++j) {
2023-11-20 08:35:27 -06:00
BOOST_CHECK(coloring[i][j] == correctColor[i][j]);
}
}
coloring = Opm::getMatrixRowColoring(testMatrix, Opm::ColoringType::LOWER);
2023-11-21 08:19:11 -06:00
for (std::size_t i = 0; i < correctColor.size(); ++i) {
for (std::size_t j = 0; j < correctColor[i].size(); ++j) {
2023-11-20 08:35:27 -06:00
BOOST_CHECK(coloring[i][j] == correctColor[i][j]);
}
}
coloring = Opm::getMatrixRowColoring(testMatrix, Opm::ColoringType::UPPER);
correctColor = {{4}, {3}, {2}, {1}, {0}};
2023-11-21 08:19:11 -06:00
for (std::size_t i = 0; i < correctColor.size(); ++i) {
for (std::size_t j = 0; j < correctColor[i].size(); ++j) {
2023-11-20 08:35:27 -06:00
BOOST_CHECK(coloring[i][j] == correctColor[i][j]);
}
}
}
BOOST_AUTO_TEST_CASE(TestColoredDiluParallelisms5x5Complex)
{
/*
Test matrix:
|xxx x|
|xx x |
|x x x|
| x x |
|x x x|
*/
const int N = 5;
2023-12-04 05:40:55 -06:00
constexpr int bz = 3;
2023-11-20 08:35:27 -06:00
const int nonZeroes = 15;
// creating some shorthand typenames
using Matrix = Dune::BCRSMatrix<Dune::FieldMatrix<double, bz, bz>>;
Matrix testMatrix(N, N, nonZeroes, Matrix::row_wise);
for (auto row = testMatrix.createbegin(); row != testMatrix.createend(); ++row) {
if (row.index() == 0) {
row.insert(row.index());
row.insert(row.index()+1);
row.insert(row.index()+2);
row.insert(row.index()+4);
2023-11-21 08:19:11 -06:00
} else if (row.index() == 1) {
2023-11-20 08:35:27 -06:00
row.insert(row.index() - 1);
row.insert(row.index());
row.insert(row.index() + 2);
2023-11-21 08:19:11 -06:00
} else if (row.index() == 2) {
2023-11-20 08:35:27 -06:00
row.insert(row.index() - 2);
row.insert(row.index());
row.insert(row.index() + 2);
2023-11-21 08:19:11 -06:00
} else if (row.index() == 3) {
2023-11-20 08:35:27 -06:00
row.insert(row.index() - 2);
row.insert(row.index());
2023-11-21 08:19:11 -06:00
} else if (row.index() == 4) {
2023-11-20 08:35:27 -06:00
row.insert(row.index() - 4);
row.insert(row.index() - 2);
row.insert(row.index());
}
}
testMatrix[0][0][0][0] = 1.0;
testMatrix[0][1][0][0] = 1.0;
testMatrix[0][2][0][0] = 1.0;
testMatrix[0][4][0][0] = 1.0;
testMatrix[1][0][0][0] = 1.0;
testMatrix[1][1][0][0] = 1.0;
testMatrix[1][3][0][0] = 1.0;
testMatrix[2][0][0][0] = 1.0;
testMatrix[2][2][0][0] = 1.0;
testMatrix[2][4][0][0] = 1.0;
testMatrix[3][1][0][0] = 1.0;
testMatrix[3][3][0][0] = 1.0;
testMatrix[4][0][0][0] = 1.0;
testMatrix[4][2][0][0] = 1.0;
testMatrix[4][4][0][0] = 1.0;
Opm::SparseTable<std::size_t> coloring = Opm::getMatrixRowColoring(testMatrix, Opm::ColoringType::SYMMETRIC);
std::vector<std::vector<std::size_t>> correctColor = {{0}, {1, 2}, {3, 4}};
2023-11-21 08:19:11 -06:00
for (std::size_t i = 0; i < correctColor.size(); ++i) {
for (std::size_t j = 0; j < correctColor[i].size(); ++j) {
2023-11-20 08:35:27 -06:00
BOOST_CHECK(coloring[i][j] == correctColor[i][j]);
}
}
coloring = Opm::getMatrixRowColoring(testMatrix, Opm::ColoringType::LOWER);
2023-11-21 08:19:11 -06:00
for (std::size_t i = 0; i < correctColor.size(); ++i) {
for (std::size_t j = 0; j < correctColor[i].size(); ++j) {
2023-11-20 08:35:27 -06:00
BOOST_CHECK(coloring[i][j] == correctColor[i][j]);
}
}
coloring = Opm::getMatrixRowColoring(testMatrix, Opm::ColoringType::UPPER);
correctColor = {{3, 4}, {1, 2}, {0}};
2023-11-21 08:19:11 -06:00
for (std::size_t i = 0; i < correctColor.size(); ++i) {
for (std::size_t j = 0; j < correctColor[i].size(); ++j) {
2023-11-20 08:35:27 -06:00
BOOST_CHECK(coloring[i][j] == correctColor[i][j]);
}
}
2023-11-21 08:19:11 -06:00
}