Removed unused reordering functions, file, header inclusions

This commit is contained in:
Tong Dong Qiu
2022-11-17 09:38:23 +01:00
parent 35287a61ff
commit 65128d1616
11 changed files with 5 additions and 409 deletions

View File

@@ -45,7 +45,6 @@ list (APPEND MAIN_SOURCE_FILES
opm/simulators/flow/SimulatorFullyImplicitBlackoilEbos.cpp
opm/simulators/flow/ValidationFunctions.cpp
opm/simulators/linalg/bda/WellContributions.cpp
opm/simulators/linalg/bda/Matrix.cpp
opm/simulators/linalg/bda/MultisegmentWellContribution.cpp
opm/simulators/linalg/ExtractParallelGridInformationToISTL.cpp
opm/simulators/linalg/FlexibleSolver1.cpp

View File

@@ -34,7 +34,6 @@
#if HAVE_CUDA || HAVE_OPENCL || HAVE_AMGCL || HAVE_ROCALUTION
#include <opm/simulators/linalg/bda/BdaBridge.hpp>
#include <opm/simulators/linalg/bda/WellContributions.hpp>
#include <iostream>
#endif
#if HAVE_DUNE_ALUGRID
@@ -212,7 +211,6 @@ prepare(const Grid& grid,
useWellConn,
wellConnectionsGraph_,
numJacobiBlocks_);
std::cout << "Create block-Jacobi pattern" << std::endl;
this->blockJacobiAdjacency(grid, cellPartition, nonzeroes);
}
}

View File

@@ -1,63 +0,0 @@
/*
Copyright 2020 Equinor ASA
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <opm/common/OpmLog/OpmLog.hpp>
#include <opm/common/ErrorMacros.hpp>
#include <opm/simulators/linalg/bda/BlockedMatrix.hpp>
#include <opm/simulators/linalg/bda/Matrix.hpp>
namespace Opm
{
namespace Accelerator
{
/*Sort a row of matrix elements from a CSR-format.*/
void sortRow(int *colIndices, double *data, int left, int right) {
int l = left;
int r = right;
int middle = colIndices[(l + r) >> 1];
do {
while (colIndices[l] < middle)
l++;
while (colIndices[r] > middle)
r--;
if (l <= r) {
int lColIndex = colIndices[l];
colIndices[l] = colIndices[r];
colIndices[r] = lColIndex;
double lDatum = data[l];
data[l] = data[r];
data[r] = lDatum;
l++;
r--;
}
} while (l < r);
if (left < r)
sortRow(colIndices, data, left, r);
if (right > l)
sortRow(colIndices, data, l, right);
}
} // namespace Accelerator
} // namespace Opm

View File

@@ -63,8 +63,6 @@ public:
int nnzs;
};
void sortRow(int *colIndices, double *data, int left, int right);
} // namespace Accelerator
} // namespace Opm

View File

@@ -23,32 +23,9 @@
#include <opm/simulators/linalg/bda/Reorder.hpp>
#include <opm/simulators/linalg/bda/BlockedMatrix.hpp>
#include <opm/common/ErrorMacros.hpp>
#include <algorithm>
#include <array>
#include <functional>
#include <random>
#include <sstream>
#include <vector>
#include <cstring>
#include <cassert>
namespace {
std::mt19937 make_urng()
{
std::random_device rd;
std::array<unsigned int, std::mt19937::state_size> seed_data{};
std::generate_n(seed_data.begin(), seed_data.size(), std::ref(rd));
std::seed_seq seq(seed_data.begin(), seed_data.end());
return std::mt19937{ seq };
}
}
namespace Opm
{
@@ -56,212 +33,6 @@ namespace Accelerator
{
/* Give every node in the matrix (of which only the sparsity pattern in the
* form of row pointers and column indices arrays are in the input), a color
* in the colors array. Also return the amount of colors in the return integer.
* This graph-coloring algorithm is based on the Jones-Plassmann algorithm, proposed in:
* "A Parallel Graph Coloring Heuristic" by M.T. Jones and P.E. Plassmann in SIAM Journal of Scientific Computing 14 (1993) */
template <unsigned int block_size>
int colorBlockedNodes(int rows, const int *CSRRowPointers, const int *CSRColIndices, const int *CSCColPointers, const int *CSCRowIndices, std::vector<int>& colors, int maxRowsPerColor, int maxColsPerColor)
{
auto left = static_cast<std::vector<int>::difference_type>(colors.size());
int c = -1;
const int max_tries = 100; // since coloring is random, it is possible that a coloring fails. In that case, try again.
std::vector<bool> visitedColumns(rows, false);
auto gen = make_urng();
std::vector<int> randoms(rows);
for (unsigned int t = 0; t < max_tries; t++) {
// (re)initialize data for coloring process
std::uniform_int_distribution<int> uniform{}; // 0 .. INT_MAX
std::generate(randoms.begin(), randoms.end(),
[&uniform, &gen]()
{
return uniform(gen);
});
std::fill(colors.begin(), colors.end(), -1);
// actually perform coloring
for (c = 0; c < MAX_COLORS; c++) {
unsigned int rowsInColor = 0u;
unsigned int colsInColor = 0u;
for (int i = 0; i < rows; i++)
{
bool iMax = true; // true iff you have max random
// ignore nodes colored earlier
if ((colors[i] != -1))
continue;
int ir = randoms[i];
// look at all nodex that node i is connected to
for (int k = CSRRowPointers[i]; k < CSRRowPointers[i + 1]; k++) {
// ignore nodes colored earlier (and yourself)
int j = CSRColIndices[k];
int jc = colors[j];
if (((jc != -1) && (jc != c)) || (i == j)) {
continue;
}
// node i is not in the current color if one of its neighbours shares this color,
if (jc == c) {
iMax = false;
break;
}
// or if one of its uncolored neighbours has a higher random value
int jr = randoms[j];
if (ir <= jr) {
iMax = false;
break;
}
}
// look at all nodes that have a connection to node i
for (int k = CSCColPointers[i]; k < CSCColPointers[i + 1]; k++) {
// ignore nodes colored earlier (and yourself)
int j = CSCRowIndices[k];
int jc = colors[j];
if (((jc != -1) && (jc != c)) || (i == j)) {
continue;
}
// node i is not in the current color if one of its neighbours shares this color,
if (jc == c) {
iMax = false;
break;
}
// or if one of its uncolored neighbours has a higher random value
int jr = randoms[j];
if (ir <= jr) {
iMax = false;
break;
}
}
// assign color if you have the maximum random number
if (iMax) {
unsigned int additionalColsInRow = 0u;
for (int k = CSRRowPointers[i]; k < CSRRowPointers[i + 1]; k++) {
int j = CSRColIndices[k];
if (!visitedColumns[j]) {
visitedColumns[j] = true;
additionalColsInRow += block_size;
}
}
if ((colsInColor + additionalColsInRow) > static_cast<unsigned int>(maxColsPerColor)) {
break;
}
colsInColor += additionalColsInRow;
colors[i] = c;
rowsInColor += block_size;
if ((rowsInColor + block_size - 1) >= static_cast<unsigned int>(maxRowsPerColor)) {
break;
}
}
}
// Check if graph coloring is done.
left = std::count_if(colors.begin(), colors.end(),
[](const int color) { return color == -1; });
if (left == 0) {
return c + 1;
}
}
}
std::ostringstream oss;
oss << "Error could not find a graph coloring with " << c << " colors after " << max_tries << " tries.\nNumber of colorless nodes: " << left;
OPM_THROW(std::logic_error, oss.str());
return -1;
}
/* Reorder a matrix by a specified input order.
* Both a to order array, which contains for every node from the old matrix where it will move in the new matrix,
* and the from order, which contains for every node in the new matrix where it came from in the old matrix.
* reordermapping_nonzeroes is filled with increasing indices, and reordered using the translated colIndices as keys,
* this means the resulting reordermapping_nonzeroes array contains the mapping
*/
void reorderBlockedMatrixByPattern(BlockedMatrix *mat, std::vector<int>& reordermapping_nonzeroes, int *toOrder, int *fromOrder, BlockedMatrix *rmat){
int rIndex = 0;
std::vector<int> tmp(mat->nnzbs);
reordermapping_nonzeroes.resize(mat->nnzbs);
for(int i = 0; i < mat->nnzbs; ++i){
reordermapping_nonzeroes[i] = i;
}
rmat->rowPointers[0] = 0;
for(int i = 0; i < mat->Nb; i++){
int thisRow = fromOrder[i];
// put thisRow from the old matrix into row i of the new matrix
rmat->rowPointers[i+1] = rmat->rowPointers[i] + mat->rowPointers[thisRow+1] - mat->rowPointers[thisRow];
for(int k = mat->rowPointers[thisRow]; k < mat->rowPointers[thisRow+1]; k++){
tmp[rIndex] = reordermapping_nonzeroes[k]; // only get 1 entry per block
rmat->colIndices[rIndex] = mat->colIndices[k];
rIndex++;
}
}
// re-assign column indices according to the new positions of the nodes referenced by the column indices
for(int i = 0; i < mat->nnzbs; i++){
rmat->colIndices[i] = toOrder[rmat->colIndices[i]];
}
// re-sort the column indices of every row.
for(int i = 0; i < mat->Nb; i++){
sortRow(rmat->colIndices, tmp.data(), rmat->rowPointers[i], rmat->rowPointers[i+1]-1);
}
for(int i = 0; i < mat->nnzbs; i++){
reordermapping_nonzeroes[i] = tmp[i];
}
// std::copy();
}
/* Reorder an array of nonzero blocks into another array, using a mapping */
void reorderNonzeroes(BlockedMatrix *mat, std::vector<int>& reordermapping_nonzeroes, BlockedMatrix *rmat){
assert(mat->block_size == rmat->block_size);
const unsigned int bs = mat->block_size;
for(int i = 0; i < mat->nnzbs; i++){
int old_idx = reordermapping_nonzeroes[i];
memcpy(rmat->nnzValues+i*bs*bs, mat->nnzValues+old_idx*bs*bs, sizeof(double)*bs*bs); // copy nnz block
}
}
/* Find a reorder mapping according to the colors that every node of the matrix has received */
void colorsToReordering(int Nb, std::vector<int>& colors, int numColors, int *toOrder, int *fromOrder, std::vector<int>& rowsPerColor) {
int reordered = 0;
// Find reordering patterns
for (int c = 0; c < numColors; c++) {
for (int i = 0; i < Nb; i++) {
if (colors[i] == c) {
rowsPerColor[c]++;
toOrder[i] = reordered;
fromOrder[reordered] = i;
reordered++;
}
}
}
}
// Reorder a vector according to a reordering pattern
template <unsigned int block_size>
void reorderBlockedVectorByPattern(int Nb, double *vector, int *fromOrder, double *rVector) {
for (int i = 0; i < Nb; i++) {
for (unsigned int j = 0; j < block_size; j++) {
rVector[block_size * i + j] = vector[block_size * fromOrder[i] + j];
}
}
}
/* Check is operations on a node in the matrix can be started
* A node can only be started if all nodes that it depends on during sequential execution have already completed.*/
@@ -344,17 +115,6 @@ void findLevelScheduling(int *CSRColIndices, int *CSRRowPointers, int *CSCRowInd
*numColors = rowsPerColor.size();
}
/* Perform the complete graph coloring algorithm on a matrix. Return an array with the amount of nodes per color.*/
template <unsigned int block_size>
void findGraphColoring(const int *CSRColIndices, const int *CSRRowPointers, const int *CSCRowIndices, const int *CSCColPointers, int Nb, int maxRowsPerColor, int maxColsPerColor, int *numColors, int *toOrder, int *fromOrder, std::vector<int>& rowsPerColor) {
std::vector<int> rowColor(Nb);
*numColors = colorBlockedNodes<block_size>(Nb, CSRRowPointers, CSRColIndices, CSCColPointers, CSCRowIndices, rowColor, maxRowsPerColor, maxColsPerColor);
rowsPerColor.resize(*numColors);
colorsToReordering(Nb, rowColor, *numColors, toOrder, fromOrder, rowsPerColor);
}
// based on the scipy package from python, scipy/sparse/sparsetools/csr.h on github
void csrPatternToCsc(int *CSRColIndices, int *CSRRowPointers, int *CSCRowIndices, int *CSCColPointers, int Nb) {
@@ -393,19 +153,5 @@ void csrPatternToCsc(int *CSRColIndices, int *CSRRowPointers, int *CSCRowIndices
}
#define INSTANTIATE_BDA_FUNCTIONS(n) \
template int colorBlockedNodes<n>(int, const int *, const int *, const int *, const int *, std::vector<int>&, int, int); \
template void reorderBlockedVectorByPattern<n>(int, double*, int*, double*); \
template void findGraphColoring<n>(const int *, const int *, const int *, const int *, int, int, int, int *, int *, int *, std::vector<int>&); \
INSTANTIATE_BDA_FUNCTIONS(1);
INSTANTIATE_BDA_FUNCTIONS(2);
INSTANTIATE_BDA_FUNCTIONS(3);
INSTANTIATE_BDA_FUNCTIONS(4);
INSTANTIATE_BDA_FUNCTIONS(5);
INSTANTIATE_BDA_FUNCTIONS(6);
#undef INSTANTIATE_BDA_FUNCTIONS
} // namespace Accelerator
} // namespace Opm

View File

@@ -22,68 +22,11 @@
#include <vector>
#include <opm/simulators/linalg/bda/BlockedMatrix.hpp>
namespace Opm
{
namespace Accelerator
{
#define MAX_COLORS 256
/// Give every node in the matrix a color so that no neighbouring nodes share a color
/// The color array must be allocated already
/// This function with throw an error if no coloring can be found within the given restrictions
/// This function does graph coloring based on random numbers
/// \param[in] rows number of rows in the matrix
/// \param[in] CSRRowPointers array of row pointers of the sparsity pattern stored in the CSR format
/// \param[in] CSRColIndices array of column indices of the sparsity pattern stored in the CSR format
/// \param[in] CSCColPointers array of column pointers of the sparsity pattern stored in the CSC format
/// \param[in] CSCRowIndices array of row indices of the sparsity pattern stored in the CSC format
/// \param[inout] colors output array containing the number of the color that each row is assigned to
/// \param[in] maxRowsPerColor the maximum number of rows that are allowed in one color (so: the maximum number of nodes per color)
/// \param[in] maxColsPerColor the maximum number of columns that the rows in a color are allowed to share (so: the maximum number of nodes that the nodes in one color may be connected to)
/// \return the number of colors needed for the coloring
template <unsigned int block_size>
int colorBlockedNodes(int rows, const int *CSRRowPointers, const int *CSRColIndices, const int *CSCColPointers, const int *CSCRowIndices, std::vector<int>& colors, int maxRowsPerColor, int maxColsPerColor);
/// Reorder the sparsity pattern of the matrix according to the mapping in toOrder and fromOrder
/// Also find mapping for nnz blocks
/// rmat must be allocated already
/// \param[in] mat matrix to be reordered
/// \param[out] reordermapping_nonzeroes contains new index for every nnz block
/// \param[in] toOrder reorder pattern that lists for each index in the original order, to which index in the new order it should be moved
/// \param[in] fromOrder reorder pattern that lists for each index in the new order, from which index in the original order it was moved
/// \param[out] rmat reordered Matrix
void reorderBlockedMatrixByPattern(BlockedMatrix *mat, std::vector<int>& reordermapping_nonzeroes, int *toOrder, int *fromOrder, BlockedMatrix *rmat);
/// Write nnz blocks from mat to rmat, according to the mapping in reordermapping_nonzeroes
/// rmat must be allocated already
/// \param[in] mat matrix to be reordered
/// \param[in] reordermapping_nonzeroes contains old index for every nnz block, so rmat_nnz[i] == mat_nnz[mapping[i]]
/// \param[inout] rmat reordered Matrix
void reorderNonzeroes(BlockedMatrix *mat, std::vector<int>& reordermapping_nonzeroes, BlockedMatrix *rmat);
/// Compute reorder mapping from the color that each node has received
/// The toOrder, fromOrder and iters arrays must be allocated already
/// \param[in] Nb number of blocks in the vector
/// \param[in] colors array containing the number of the color that each row is assigned to
/// \param[in] numColors the total number of colors into which all rows have been divided
/// \param[inout] toOrder reorder pattern that lists for each index in the original order, to which index in the new order it should be moved
/// \param[inout] fromOrder reorder pattern that lists for each index in the new order, from which index in the original order it was moved
/// \param[inout] rowsPerColor array containing for each color the number of rows that it contains
void colorsToReordering(int Nb, std::vector<int>& colors, int numColors, int *toOrder, int *fromOrder, std::vector<int>& rowsPerColor);
/// Reorder a vector according to the mapping in fromOrder
/// The rVector array must be allocated already
/// \param[in] Nb number of blocks in the vector
/// \param[in] vector vector to be reordered
/// \param[in] toOrder reorder pattern that lists for each index in the original order, to which index in the new order it should be moved
/// \param[in] fromOrder reorder pattern that lists for each index in the new order, from which index in the original order it was moved
/// \param[inout] rVector reordered vector
template <unsigned int block_size>
void reorderBlockedVectorByPattern(int Nb, double *vector, int *fromOrder, double *rVector);
/// Determine whether all rows that a certain row depends on are done already
/// \param[in] rowIndex index of the row that needs to be checked for
/// \param[in] rowPointers row pointers of the matrix that the row is in
@@ -105,22 +48,6 @@ bool canBeStarted(const int rowIndex, const int *rowPointers, const int *colIn
/// \param[out] rowsPerColor for each color, an array of all rowIndices in that color, this function uses emplace_back() to fill
void findLevelScheduling(int *CSRColIndices, int *CSRRowPointers, int *CSCRowIndices, int *CSCColPointers, int Nb, int *numColors, int *toOrder, int* fromOrder, std::vector<int>& rowsPerColor);
/// Find a graph coloring reordering for an input matrix
/// The toOrder and fromOrder arrays must be allocated already
/// \param[in] CSRColIndices column indices of the input sparsity pattern stored in the CSR format
/// \param[in] CSRRowPointers row pointers of the input sparsity pattern stored in the CSR format
/// \param[in] CSCRowIndices row indices of the input sparsity pattern stored in the CSC format
/// \param[in] CSCColPointers column pointers of the input sparsity pattern stored in the CSC format
/// \param[in] Nb number of blockrows in the matrix
/// \param[in] maxRowsPerColor the maximum number of rows that are allowed in one color (so: the maximum number of nodes per color)
/// \param[in] maxColsPerColor the maximum number of columns that the rows in a color are allowed to share (so: the maximum number of nodes that the nodes in one color may be connected to)
/// \param[out] numColors the number of colors used in the found graph coloring
/// \param[inout] toOrder the reorder pattern that was found, which lists for each index in the original order, to which index in the new order it should be moved
/// \param[inout] fromOrder the reorder pattern that was found, which lists for each index in the new order, from which index in the original order it was moved
/// \param[inout] rowsPerColor for each used color, the number of rows assigned to that color, this function will resize()
template <unsigned int block_size>
void findGraphColoring(const int *CSRColIndices, const int *CSRRowPointers, const int *CSCRowIndices, const int *CSCColPointers, int Nb, int maxRowsPerColor, int maxColsPerColor, int *numColors, int *toOrder, int *fromOrder, std::vector<int>& rowsPerColor);
/// Convert a sparsity pattern stored in the CSR format to the CSC format
/// CSCRowIndices and CSCColPointers arrays must be allocated already
/// Based on the csr_tocsc() function from the scipy package from python, https://github.com/scipy/scipy/blob/master/scipy/sparse/sparsetools/csr.h

View File

@@ -18,8 +18,7 @@
*/
#include <config.h> // CMake
#include <cstdlib>
#include <cstring>
#include <opm/common/OpmLog/OpmLog.hpp>
#include <opm/common/ErrorMacros.hpp>

View File

@@ -32,10 +32,9 @@
namespace Opm
{
/// This class serves to eliminate the need to include the WellContributions into the matrix (with --matrix-add-well-contributions=true) for the cusparseSolver
/// If the --matrix-add-well-contributions commandline parameter is true, this class should not be used
/// So far, StandardWell and MultisegmentWell are supported
/// StandardWells are only supported for cusparseSolver (CUDA), MultisegmentWells are supported for both cusparseSolver and openclSolver
/// This class serves to eliminate the need to include the WellContributions into the matrix (with --matrix-add-well-contributions=true) for the cusparseSolver or openclSolver.
/// If the --matrix-add-well-contributions commandline parameter is true, this class should still be used, but be empty.
/// StandardWell and MultisegmentWell are supported for both cusparseSolver and openclSolver.
/// A single instance (or pointer) of this class is passed to the BdaSolver.
/// For StandardWell, this class contains all the data and handles the computation. For MultisegmentWell, the vector 'multisegments' contains all the data. For more information, check the MultisegmentWellContribution class.

View File

@@ -19,8 +19,6 @@
#include <config.h> // CMake
#include <cstdlib>
#include <cstring>
#include "opm/simulators/linalg/bda/cuda/cuWellContributions.hpp"

View File

@@ -24,9 +24,6 @@
#include <cuda_runtime.h>
#include <memory>
#include <vector>
namespace Opm
{

View File

@@ -21,8 +21,6 @@
#include <opm/simulators/linalg/bda/opencl/openclWellContributions.hpp>
#include <cstdlib>
#include <cstring>
#include <opm/common/OpmLog/OpmLog.hpp>
#include <opm/common/ErrorMacros.hpp>