Improved rowsPerColor usage

This commit is contained in:
T.D. (Tongdong) Qiu 2020-07-07 11:00:38 +02:00
parent 5aa8dda487
commit 478e2ee971
4 changed files with 39 additions and 59 deletions

View File

@ -48,7 +48,6 @@ namespace bda
{
delete[] invDiagVals;
delete[] diagIndex;
delete[] rowsPerColor;
delete[] toOrder;
delete[] fromOrder;
}
@ -63,8 +62,8 @@ namespace bda
this->nnz = mat->nnzbs * block_size * block_size;
this->nnzbs = mat->nnzbs;
toOrder = new int[N];
fromOrder = new int[N];
toOrder = new int[Nb];
fromOrder = new int[Nb];
int *CSCRowIndices = new int[nnzbs];
int *CSCColPointers = new int[Nb + 1];
@ -81,12 +80,9 @@ namespace bda
rmat = std::make_shared<BlockedMatrix<block_size> >(mat->Nb, mat->nnzbs);
LUmat = std::make_unique<BlockedMatrix<block_size> >(*rmat);
if (level_scheduling) {
rowsPerColor = findLevelScheduling(mat->colIndices, mat->rowPointers, CSCRowIndices, CSCColPointers, mat->Nb, &numColors, toOrder, fromOrder);
findLevelScheduling(mat->colIndices, mat->rowPointers, CSCRowIndices, CSCColPointers, mat->Nb, &numColors, toOrder, fromOrder, rowsPerColor);
} else if (graph_coloring) {
rowsPerColor = findGraphColoring<block_size>(mat->colIndices, mat->rowPointers, CSCRowIndices, CSCColPointers, mat->Nb, mat->Nb, mat->Nb, &numColors, toOrder, fromOrder);
}
if (rowsPerColor == nullptr) {
return false;
findGraphColoring<block_size>(mat->colIndices, mat->rowPointers, CSCRowIndices, CSCColPointers, mat->Nb, mat->Nb, mat->Nb, &numColors, toOrder, fromOrder, rowsPerColor);
}
if(verbosity >= 3){
std::ostringstream out;

View File

@ -42,7 +42,8 @@ namespace bda
std::unique_ptr<BlockedMatrix<block_size> > Lmat = nullptr, Umat = nullptr, LUmat = nullptr;
std::shared_ptr<BlockedMatrix<block_size> > rmat = nullptr; // only used with PAR_SIM
double *invDiagVals = nullptr;
int *diagIndex = nullptr, *rowsPerColor = nullptr;
int *diagIndex = nullptr;
std::vector<int> rowsPerColor; // color i contains rowsPerColor[i] rows, which are processed in parallel
int *toOrder = nullptr, *fromOrder = nullptr;
int numColors;
int verbosity;

View File

@ -199,12 +199,9 @@ void reorderBlockedMatrixByPattern(BlockedMatrix<block_size> *mat, int *toOrder,
/* Reorder a matrix 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, int *rowsPerColor) {
void colorsToReordering(int Nb, std::vector<int>& colors, int numColors, int *toOrder, int *fromOrder, std::vector<int>& rowsPerColor) {
int reordered = 0;
int i, c;
for (i = 0; i < numColors; i++) {
rowsPerColor[i] = 0;
}
// Find reordering patterns
for (c = 0; c < numColors; c++) {
@ -212,7 +209,6 @@ void colorsToReordering(int Nb, std::vector<int>& colors, int numColors, int *to
if (colors[i] == c) {
rowsPerColor[c]++;
toOrder[i] = reordered;
fromOrder[reordered] = i;
reordered++;
}
@ -259,13 +255,11 @@ bool canBeStarted(const int rowIndex, const int *rowPointers, const int *colIndi
* "Iterative methods for Sparse Linear Systems" by Yousef Saad in section 11.6.3
*/
int *findLevelScheduling(int *CSRColIndices, int *CSRRowPointers, int *CSCRowIndices, int *CSCColPointers, int Nb, int *numColors, int *toOrder, int* fromOrder) {
void findLevelScheduling(int *CSRColIndices, int *CSRRowPointers, int *CSCRowIndices, int *CSCColPointers, int Nb, int *numColors, int *toOrder, int* fromOrder, std::vector<int>& rowsPerColor) {
int activeRowIndex = 0, colorEnd, nextActiveRowIndex = 0;
int thisRow;
std::vector<bool> doneRows(Nb, false);
std::vector<int> rowsPerColor;
rowsPerColor.reserve(Nb);
int *resRowsPerColor;
std::vector <int> rowsToStart;
@ -310,32 +304,21 @@ int *findLevelScheduling(int *CSRColIndices, int *CSRRowPointers, int *CSCRowInd
colorEnd = nextActiveRowIndex;
rowsPerColor.emplace_back(nextActiveRowIndex - activeRowIndex);
}
// Crop the rowsPerColor array to it minimum size.
resRowsPerColor = new int[rowsPerColor.size()];
for (unsigned int i = 0; i < rowsPerColor.size(); i++) {
resRowsPerColor[i] = rowsPerColor[i];
}
*numColors = rowsPerColor.size();
return resRowsPerColor;
}
/* Perform the complete graph coloring algorithm on a matrix. Return an array with the amount of nodes per color.*/
template <unsigned int block_size>
int* 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) {
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;
rowColor.resize(Nb);
int *rowsPerColor = new int[MAX_COLORS];
*numColors = colorBlockedNodes<block_size>(Nb, CSRRowPointers, CSRColIndices, CSCColPointers, CSCRowIndices, rowColor, maxRowsPerColor, maxColsPerColor);
rowsPerColor.resize(*numColors);
colorsToReordering(Nb, rowColor, *numColors, toOrder, fromOrder, rowsPerColor);
// The rowsPerColor array contains a non-zero value for each color denoting how many rows are in that color. It has a size of *numColors.
return rowsPerColor;
}
// based on the scipy package from python, scipy/sparse/sparsetools/csr.h on github
@ -375,11 +358,11 @@ 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 reorderBlockedMatrixByPattern<n>(BlockedMatrix<n> *, int *, int *, BlockedMatrix<n> *); \
template void reorderBlockedVectorByPattern<n>(int, double*, int*, double*); \
template int* findGraphColoring<n>(const int *, const int *, const int *, const int *, int, int, int, int *, int *, int *); \
#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 reorderBlockedMatrixByPattern<n>(BlockedMatrix<n> *, int *, int *, BlockedMatrix<n> *); \
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);

View File

@ -60,7 +60,7 @@ void reorderBlockedMatrixByPattern(BlockedMatrix<block_size> *mat, int *toOrder,
/// \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, int *rowsPerColor);
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
@ -81,33 +81,33 @@ void reorderBlockedVectorByPattern(int Nb, double *vector, int *fromOrder, doubl
bool canBeStarted(const int rowIndex, const int *rowPointers, const int *colIndices, const std::vector<bool>& doneRows);
/// Find a level scheduling reordering for an input matrix
/// The toOrder and fromOrder arrays must be allocated already
/// \param[in] CSRColIndices column indices array, obtained from storing the input matrix in the CSR format
/// \param[in] CSRRowPointers row pointers array, obtained from storing the input matrix in the CSR format
/// \param[in] CSCRowIndices row indices array, obtained from storing the input matrix in the CSC format
/// \param[in] CSCColPointers column pointers array, obtained from storing the input matrix in the CSC format
/// \param[in] Nb number of blockrows in the matrix
/// \param[out] numColors a pointer to the number of colors needed for the level scheduling
/// \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
/// \return a pointer to an array that contains for each color, the number of rows that that color contains
int* findLevelScheduling(int *CSRColIndices, int *CSRRowPointers, int *CSCRowIndices, int *CSCColPointers, int Nb, int *numColors, int *toOrder, int* fromOrder);
/// The toOrder and fromOrder arrays must be allocated already
/// \param[in] CSRColIndices column indices array, obtained from storing the input matrix in the CSR format
/// \param[in] CSRRowPointers row pointers array, obtained from storing the input matrix in the CSR format
/// \param[in] CSCRowIndices row indices array, obtained from storing the input matrix in the CSC format
/// \param[in] CSCColPointers column pointers array, obtained from storing the input matrix in the CSC format
/// \param[in] Nb number of blockrows in the matrix
/// \param[out] numColors a pointer to the number of colors needed for the level scheduling
/// \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
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
/// \return a pointer to an array that contains for each color, the number of rows that that color contains
/// \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
template <unsigned int block_size>
int* 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);
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