From 65128d16164d767cb7435377e0eed4fb00e194b5 Mon Sep 17 00:00:00 2001 From: Tong Dong Qiu Date: Thu, 17 Nov 2022 09:38:23 +0100 Subject: [PATCH] Removed unused reordering functions, file, header inclusions --- CMakeLists_files.cmake | 1 - opm/simulators/linalg/ISTLSolverEbos.cpp | 2 - opm/simulators/linalg/bda/Matrix.cpp | 63 ----- opm/simulators/linalg/bda/Matrix.hpp | 2 - opm/simulators/linalg/bda/Reorder.cpp | 256 +----------------- opm/simulators/linalg/bda/Reorder.hpp | 73 ----- .../linalg/bda/WellContributions.cpp | 3 +- .../linalg/bda/WellContributions.hpp | 7 +- .../linalg/bda/cuda/cuWellContributions.cu | 2 - .../linalg/bda/cuda/cuWellContributions.hpp | 3 - .../bda/opencl/openclWellContributions.cpp | 2 - 11 files changed, 5 insertions(+), 409 deletions(-) delete mode 100644 opm/simulators/linalg/bda/Matrix.cpp diff --git a/CMakeLists_files.cmake b/CMakeLists_files.cmake index d5e536d49..fe2df62c0 100644 --- a/CMakeLists_files.cmake +++ b/CMakeLists_files.cmake @@ -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 diff --git a/opm/simulators/linalg/ISTLSolverEbos.cpp b/opm/simulators/linalg/ISTLSolverEbos.cpp index beee1906f..ac16c3a77 100644 --- a/opm/simulators/linalg/ISTLSolverEbos.cpp +++ b/opm/simulators/linalg/ISTLSolverEbos.cpp @@ -34,7 +34,6 @@ #if HAVE_CUDA || HAVE_OPENCL || HAVE_AMGCL || HAVE_ROCALUTION #include #include -#include #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); } } diff --git a/opm/simulators/linalg/bda/Matrix.cpp b/opm/simulators/linalg/bda/Matrix.cpp deleted file mode 100644 index b0f8ae3e4..000000000 --- a/opm/simulators/linalg/bda/Matrix.cpp +++ /dev/null @@ -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 . -*/ - -#include - -#include -#include - -#include -#include - -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 diff --git a/opm/simulators/linalg/bda/Matrix.hpp b/opm/simulators/linalg/bda/Matrix.hpp index ded6331c4..2d0e48741 100644 --- a/opm/simulators/linalg/bda/Matrix.hpp +++ b/opm/simulators/linalg/bda/Matrix.hpp @@ -63,8 +63,6 @@ public: int nnzs; }; -void sortRow(int *colIndices, double *data, int left, int right); - } // namespace Accelerator } // namespace Opm diff --git a/opm/simulators/linalg/bda/Reorder.cpp b/opm/simulators/linalg/bda/Reorder.cpp index d53e71c80..4d6bcb41c 100644 --- a/opm/simulators/linalg/bda/Reorder.cpp +++ b/opm/simulators/linalg/bda/Reorder.cpp @@ -23,32 +23,9 @@ #include -#include - -#include - -#include -#include -#include -#include -#include #include -#include +#include -namespace { - - std::mt19937 make_urng() - { - std::random_device rd; - std::array 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 -int colorBlockedNodes(int rows, const int *CSRRowPointers, const int *CSRColIndices, const int *CSCColPointers, const int *CSCRowIndices, std::vector& colors, int maxRowsPerColor, int maxColsPerColor) -{ - auto left = static_cast::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 visitedColumns(rows, false); - - auto gen = make_urng(); - - std::vector randoms(rows); - for (unsigned int t = 0; t < max_tries; t++) { - // (re)initialize data for coloring process - std::uniform_int_distribution 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(maxColsPerColor)) { - break; - } - colsInColor += additionalColsInRow; - colors[i] = c; - rowsInColor += block_size; - if ((rowsInColor + block_size - 1) >= static_cast(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& reordermapping_nonzeroes, int *toOrder, int *fromOrder, BlockedMatrix *rmat){ - - int rIndex = 0; - std::vector 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& 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& colors, int numColors, int *toOrder, int *fromOrder, std::vector& 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 -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 -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& rowsPerColor) { - std::vector rowColor(Nb); - - *numColors = colorBlockedNodes(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(int, const int *, const int *, const int *, const int *, std::vector&, int, int); \ -template void reorderBlockedVectorByPattern(int, double*, int*, double*); \ -template void findGraphColoring(const int *, const int *, const int *, const int *, int, int, int, int *, int *, int *, std::vector&); \ - -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 diff --git a/opm/simulators/linalg/bda/Reorder.hpp b/opm/simulators/linalg/bda/Reorder.hpp index 545a817cf..27d960557 100644 --- a/opm/simulators/linalg/bda/Reorder.hpp +++ b/opm/simulators/linalg/bda/Reorder.hpp @@ -22,68 +22,11 @@ #include -#include - 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 -int colorBlockedNodes(int rows, const int *CSRRowPointers, const int *CSRColIndices, const int *CSCColPointers, const int *CSCRowIndices, std::vector& 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& 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& 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& colors, int numColors, int *toOrder, int *fromOrder, std::vector& 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 -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& 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 -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& 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 diff --git a/opm/simulators/linalg/bda/WellContributions.cpp b/opm/simulators/linalg/bda/WellContributions.cpp index 3be495600..34898da0c 100644 --- a/opm/simulators/linalg/bda/WellContributions.cpp +++ b/opm/simulators/linalg/bda/WellContributions.cpp @@ -18,8 +18,7 @@ */ #include // CMake -#include -#include + #include #include diff --git a/opm/simulators/linalg/bda/WellContributions.hpp b/opm/simulators/linalg/bda/WellContributions.hpp index 619ce2466..896dc7c8d 100644 --- a/opm/simulators/linalg/bda/WellContributions.hpp +++ b/opm/simulators/linalg/bda/WellContributions.hpp @@ -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. diff --git a/opm/simulators/linalg/bda/cuda/cuWellContributions.cu b/opm/simulators/linalg/bda/cuda/cuWellContributions.cu index 50457c6c1..0cfdd03eb 100644 --- a/opm/simulators/linalg/bda/cuda/cuWellContributions.cu +++ b/opm/simulators/linalg/bda/cuda/cuWellContributions.cu @@ -19,8 +19,6 @@ #include // CMake -#include -#include #include "opm/simulators/linalg/bda/cuda/cuWellContributions.hpp" diff --git a/opm/simulators/linalg/bda/cuda/cuWellContributions.hpp b/opm/simulators/linalg/bda/cuda/cuWellContributions.hpp index b8e2e0baa..892a2411b 100644 --- a/opm/simulators/linalg/bda/cuda/cuWellContributions.hpp +++ b/opm/simulators/linalg/bda/cuda/cuWellContributions.hpp @@ -24,9 +24,6 @@ #include -#include -#include - namespace Opm { diff --git a/opm/simulators/linalg/bda/opencl/openclWellContributions.cpp b/opm/simulators/linalg/bda/opencl/openclWellContributions.cpp index ff84782c1..2e23ac4e7 100644 --- a/opm/simulators/linalg/bda/opencl/openclWellContributions.cpp +++ b/opm/simulators/linalg/bda/opencl/openclWellContributions.cpp @@ -21,8 +21,6 @@ #include -#include -#include #include #include