Graph coloring now uses CSC-format version of input pattern as well.

This commit is contained in:
Tom Hogervorst
2020-07-01 16:36:10 +02:00
committed by T.D. (Tongdong) Qiu
parent 38c58bffae
commit c8eb14aaac
3 changed files with 50 additions and 27 deletions

View File

@@ -70,6 +70,7 @@ namespace bda
toOrder = new int[N];
fromOrder = new int[N];
if (level_scheduling) {
CSCRowIndices = new int[nnzbs];
CSCColPointers = new int[Nb + 1];
@@ -90,7 +91,7 @@ namespace bda
if (level_scheduling) {
rowsPerColor = findLevelScheduling(mat->colIndices, mat->rowPointers, CSCRowIndices, CSCColPointers, mat->Nb, &numColors, toOrder, fromOrder);
} else if (graph_coloring) {
rowsPerColor = findGraphColoring<block_size>(mat->colIndices, mat->rowPointers, mat->Nb, mat->Nb, mat->Nb, &numColors, toOrder, fromOrder);
rowsPerColor = findGraphColoring<block_size>(mat->colIndices, mat->rowPointers, CSCRowIndices, CSCColPointers, mat->Nb, mat->Nb, mat->Nb, &numColors, toOrder, fromOrder);
}
if (rowsPerColor == nullptr) {
return false;
@@ -109,11 +110,8 @@ namespace bda
LUmat->nnzValues = new double[mat->nnzbs * bs * bs];
if (level_scheduling) {
delete[] CSCRowIndices;
delete[] CSCColPointers;
}
delete[] CSCRowIndices;
delete[] CSCColPointers;
s.Lvals = cl::Buffer(*context, CL_MEM_READ_WRITE, sizeof(double) * bs * bs * Lmat->nnzbs);
s.Uvals = cl::Buffer(*context, CL_MEM_READ_WRITE, sizeof(double) * bs * bs * Umat->nnzbs);

View File

@@ -40,7 +40,7 @@ namespace bda
* "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 *rowPointers, const int *colIndices, std::vector<int>& colors, int maxRowsPerColor, int maxColsPerColor)
int colorBlockedNodes(int rows, const int *CSRRowPointers, const int *CSRColIndices, const int *CSCColPointers, const int *CSCRowIndices, std::vector<int>& colors, int maxRowsPerColor, int maxColsPerColor)
{
int left, c;
const int max_tries = 100; // since coloring is random, it is possible that a coloring fails. In that case, try again.
@@ -80,31 +80,52 @@ int colorBlockedNodes(int rows, const int *rowPointers, const int *colIndices, s
int ir = randoms[i];
// look at neighbors to check their random number
for (int k = rowPointers[i]; k < rowPointers[i + 1]; k++) {
// 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 = colIndices[k];
int j = CSRColIndices[k];
int jc = colors[j];
if (((jc != -1) && (jc != c)) || (i == j)) {
continue;
}
// The if statement below makes it both true graph coloring and no longer guaranteed to converge
// 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) {
additionalColsInRow = 0;
for (int k = rowPointers[i]; k < rowPointers[i + 1]; k++) {
int j = colIndices[k];
for (int k = CSRRowPointers[i]; k < CSRRowPointers[i + 1]; k++) {
int j = CSRColIndices[k];
if (!visitedColumns[j]) {
visitedColumns[j] = true;
additionalColsInRow += block_size;
@@ -303,12 +324,12 @@ int *findLevelScheduling(int *CSRColIndices, int *CSRRowPointers, int *CSCRowInd
/* 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 *colIndices, const int *rowPointers, int Nb, int maxRowsPerColor, int maxColsPerColor, int *numColors, int *toOrder, int* fromOrder) {
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) {
std::vector<int> rowColor;
rowColor.resize(Nb);
int *rowsPerColor = new int[MAX_COLORS];
*numColors = colorBlockedNodes<block_size>(Nb, rowPointers, colIndices, rowColor, maxRowsPerColor, maxColsPerColor);
*numColors = colorBlockedNodes<block_size>(Nb, CSRRowPointers, CSRColIndices, CSCColPointers, CSCRowIndices, rowColor, maxRowsPerColor, maxColsPerColor);
colorsToReordering(Nb, rowColor, *numColors, toOrder, fromOrder, rowsPerColor);
@@ -355,10 +376,10 @@ void csrPatternToCsc(int *CSRColIndices, int *CSRRowPointers, int *CSCRowIndices
#define INSTANTIATE_BDA_FUNCTIONS(n) \
template int colorBlockedNodes<n>(int, const int *, const int *, std::vector<int>&, int, int); \
template int colorBlockedNodes<n>(int, const int *, const int *, const int *, const int *, std::vector<int>&, int, int); \
template void reorderBlockedMatrixByPattern<n>(BlockedMatrix *, int *, int *, BlockedMatrix *); \
template void reorderBlockedVectorByPattern<n>(int, double*, int*, double*); \
template int* findGraphColoring<n>(const int *, const int *, int, int, int, int *, int *, int *); \
template int* findGraphColoring<n>(const int *, const int *, const int *, const int *, int, int, int, int *, int *, int *); \
INSTANTIATE_BDA_FUNCTIONS(1);
INSTANTIATE_BDA_FUNCTIONS(2);

View File

@@ -32,14 +32,16 @@ namespace bda
/// 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] rowPointers array of row pointers
/// \param[in] colIndices array of column indices
/// \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 *rowPointers, const int *colIndices, std::vector<int>& colors, int maxRowsPerColor, int maxColsPerColor);
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 rows of the matrix according to the mapping in toOrder and fromOrder
/// rMat must be allocated already
@@ -82,19 +84,21 @@ bool canBeStarted(const int rowIndex, const int *rowPointers, const int *colIn
/// 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] CSCColIndices row indices array, obtained from storing the input matrix in the CSC format
/// \param[in] CSCRowPointers column pointers array, obtained from storing the input matrix in the CSC 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 *CSCColIndices, int *CSCRowPointers, int Nb, int *numColors, int *toOrder, int* fromOrder);
int* findLevelScheduling(int *CSRColIndices, int *CSRRowPointers, int *CSCRowIndices, int *CSCColPointers, int Nb, int *numColors, int *toOrder, int* fromOrder);
/// Find a graph coloring reordering for an input matrix
/// The toOrder and fromOrder arrays must be allocated already
/// \param[in] colIndices column indices of the input matrix
/// \param[in] rowPointers row pointers of the input matrix
/// \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)
@@ -103,7 +107,7 @@ int* findLevelScheduling(int *CSRColIndices, int *CSRRowPointers, int *CSCColInd
/// \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
template <unsigned int block_size>
int* findGraphColoring(const int *colIndices, const int *rowPointers, int Nb, int maxRowsPerColor, int maxColsPerColor, int *numColors, int *toOrder, int* fromOrder);
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);
/// Convert a sparsity pattern stored in the CSR format to the CSC format
/// CSCRowIndices and CSCColPointers arrays must be allocated already