17 DAColoring::DAColoring(
68 label maxColors = 10000;
70 PetscInt nCols, nCols2;
72 const PetscScalar*
vals;
73 const PetscScalar* vals2;
75 PetscInt colorStart, colorEnd;
76 VecScatter colorScatter;
85 PetscInt nRowG, nColG;
87 Info <<
"Parallel Distance 2 Graph Coloring...." << endl;
93 VecGetOwnershipRange(colors, &colorStart, &colorEnd);
99 MatGetOwnershipRange(conMat, &Istart, &Iend);
102 MatGetSize(conMat, &nRowG, &nColG);
103 label nRowL = Iend - Istart;
104 label nColL = colorEnd - colorStart;
112 Info <<
"MaxCols: " << maxCols << endl;
113 Info <<
"AllNonZeros: " << allNonZeros << endl;
124 MatSetOption(localCols, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
126 MatZeroEntries(localCols);
131 for (label i = Istart; i < Iend; i++)
133 MatGetRow(conMat, i, &nCols, &cols, &
vals);
135 for (label j = 0; j < nCols; j++)
139 PetscInt idx = cols[j];
140 MatSetValues(localCols, 1, &idxI, 1, &idx, &v, INSERT_VALUES);
143 MatRestoreRow(conMat, i, &nCols, &cols, &
vals);
145 MatAssemblyBegin(localCols, MAT_FINAL_ASSEMBLY);
146 MatAssemblyEnd(localCols, MAT_FINAL_ASSEMBLY);
149 label nUniqueCols = 0;
150 MatGetRow(localCols, 0, &nCols, &cols, &
vals);
151 for (label j = 0; j < nCols; j++)
158 Info <<
"nUniqueCols: " << nUniqueCols << endl;
163 VecCreate(PETSC_COMM_WORLD, &globalVec);
164 VecSetSizes(globalVec, nColL, PETSC_DECIDE);
165 VecSetFromOptions(globalVec);
166 VecSet(globalVec, 0);
168 for (label j = 0; j < nCols; j++)
170 PetscInt idx = cols[j];
173 VecSetValue(globalVec, idx,
vals[j], ADD_VALUES);
176 VecAssemblyBegin(globalVec);
177 VecAssemblyEnd(globalVec);
179 MatRestoreRow(localCols, 0, &nCols, &cols, &
vals);
182 label* localColumnStat =
new label[nUniqueCols];
183 label* globalIndexList =
new label[nUniqueCols];
185 PetscScalar* globalVecArray;
186 VecGetArray(globalVec, &globalVecArray);
188 label colCounter = 0;
189 MatGetRow(localCols, 0, &nCols, &cols, &
vals);
190 for (label j = 0; j < nCols; j++)
195 globalIndexList[colCounter] = col;
196 localColumnStat[colCounter] = 1;
197 if (col >= colorStart && col < colorEnd)
201 localColumnStat[colCounter] = 2;
207 MatRestoreRow(localCols, 0, &nCols, &cols, &
vals);
208 MatDestroy(&localCols);
212 label* localRowList =
new label[nRowL];
213 for (label i = Istart; i < Iend; i++)
215 label idx = i - Istart;
216 MatGetRow(conMat, i, &nCols, &cols, &
vals);
223 for (label j = 0; j < nCols; j++)
225 label matCol = cols[j];
229 kLast = this->
find_index(matCol, kLast + 1, nUniqueCols, globalIndexList);
234 label localVal = localColumnStat[kLast];
237 localRowList[idx] = 1;
242 localRowList[idx] = 0;
247 localRowList[idx] = 0;
251 MatRestoreRow(conMat, i, &nCols, &cols, &
vals);
253 VecRestoreArray(globalVec, &globalVecArray);
258 VecCreateSeq(PETSC_COMM_SELF, nUniqueCols, &colorsLocal);
259 VecSet(colorsLocal, -1);
262 ISCreateGeneral(PETSC_COMM_WORLD, nUniqueCols, globalIndexList, PETSC_COPY_VALUES, &globalIS);
264 VecScatterCreate(colors, globalIS, colorsLocal, NULL, &colorScatter);
268 Vec globalTiebreaker;
269 VecDuplicate(globalVec, &globalTiebreaker);
270 for (label i = colorStart; i < colorEnd; i++)
273 PetscScalar val = rand() % nColG;
274 VecSetValue(globalTiebreaker, i, val, INSERT_VALUES);
279 VecDuplicate(colorsLocal, &localTiebreaker);
280 VecScatterBegin(colorScatter, globalTiebreaker, localTiebreaker, INSERT_VALUES, SCATTER_FORWARD);
281 VecScatterEnd(colorScatter, globalTiebreaker, localTiebreaker, INSERT_VALUES, SCATTER_FORWARD);
284 label* conflictCols =
new label[maxCols];
285 label* conflictLocalColIdx =
new label[maxCols];
286 for (label j = 0; j < maxCols; j++)
288 conflictCols[j] = -1;
289 conflictLocalColIdx[j] = -1;
294 Vec globalColumnStat;
295 VecDuplicate(colors, &globalColumnStat);
296 VecSet(globalColumnStat, 0.0);
297 for (label
k = 0;
k < nUniqueCols;
k++)
299 label localCol = globalIndexList[
k];
300 PetscScalar localVal = localColumnStat[
k];
301 if (localCol >= colorStart && localCol < colorEnd)
303 VecSetValue(globalColumnStat, localCol, localVal, INSERT_VALUES);
306 VecAssemblyBegin(globalColumnStat);
307 VecAssemblyEnd(globalColumnStat);
314 MatDuplicate(conMat, MAT_SHARE_NONZERO_PATTERN, &conIndMat);
318 for (label i = Istart; i < Iend; i++)
320 MatGetRow(conMat, i, &nCols, &cols, &
vals);
322 for (label j = 0; j < nCols; j++)
324 label matCol = cols[j];
327 kLast = this->
find_index(matCol, kLast + 1, nUniqueCols, globalIndexList);
328 PetscScalar val = kLast;
329 MatSetValue(conIndMat, i, matCol, val, INSERT_VALUES);
332 MatRestoreRow(conMat, i, &nCols, &cols, &
vals);
334 MatAssemblyBegin(conIndMat, MAT_FINAL_ASSEMBLY);
335 MatAssemblyEnd(conIndMat, MAT_FINAL_ASSEMBLY);
341 PetscScalar *colColor, *tbkrLocal, *tbkrGlobal, *globalStat;
342 VecGetArray(colors, &colColor);
343 VecGetArray(localTiebreaker, &tbkrLocal);
344 VecGetArray(globalTiebreaker, &tbkrGlobal);
345 VecGetArray(globalColumnStat, &globalStat);
349 for (label n = 0; n < maxColors; n++)
351 if (n % printInterval == 0)
353 Info <<
"ColorSweep: " << n <<
" " <<
mesh_.time().elapsedClockTime() <<
" s" << endl;
358 for (label
k = 0;
k < nUniqueCols;
k++)
360 label localCol = globalIndexList[
k];
361 label localVal = localColumnStat[
k];
362 if (localCol >= colorStart && localCol < colorEnd && localVal == 2)
365 label idx = localCol - colorStart;
374 for (label i = Istart; i < Iend; i++)
378 label idx = i - Istart;
381 label smallest = nColG;
385 if (localRowList[idx] > 0)
390 MatGetRow(conMat, i, &nCols, &cols, &
vals);
393 for (label j = 0; j < nCols; j++)
397 label colIdx = cols[j];
400 if (colIdx >= colorStart && colIdx < colorEnd)
403 label localVal = globalStat[colIdx - colorStart];
412 conflictCols[j] = colIdx;
415 label tbkr = tbkrGlobal[colIdx - colorStart];
428 for (label j = 0; j < nCols; j++)
432 label colIdx = conflictCols[j];
436 if (colIdx >= colorStart && colIdx < colorEnd)
439 label localVal = globalStat[colIdx - colorStart];
443 if (colIdx >= 0 && (colIdx != idxKeep))
445 colColor[colIdx - colorStart] = -1;
452 for (label j = 0; j < nCols; j++)
457 conflictCols[j] = -1;
460 MatRestoreRow(conMat, i, &nCols, &cols, &
vals);
468 label colorCounter = 0;
469 for (label
k = 0;
k < nUniqueCols;
k++)
472 label localVal = localColumnStat[
k];
473 label localCol = globalIndexList[
k];
478 if (localCol >= colorStart && localCol < colorEnd)
480 label idx = localCol - colorStart;
481 label color = colColor[idx];
496 reduce(notColored, sumOp<label>());
497 reduce(colorCounter, sumOp<label>());
499 if (n % printInterval == 0)
501 Info <<
"number of uncolored: " << colorCounter <<
" " << notColored << endl;
506 Info <<
"ColorSweep: " << n <<
" " <<
mesh_.time().elapsedClockTime() <<
" s" << endl;
507 Info <<
"number of uncolored: " << colorCounter <<
" " << notColored << endl;
511 VecRestoreArray(colors, &colColor);
517 for (label i = Istart; i < Iend; i++)
519 label idx = i - Istart;
521 MatGetRow(conMat, i, &nCols, &cols, &
vals);
530 for (label j = 0; j < nCols; j++)
533 label matCol = cols[j];
538 kLast = this->
find_index(matCol, kLast + 1, nUniqueCols, globalIndexList);
543 label localVal = localColumnStat[kLast];
548 localRowList[idx] = 1;
553 localRowList[idx] = 0;
559 localRowList[idx] = 0;
563 MatRestoreRow(conMat, i, &nCols, &cols, &
vals);
569 for (label n = 0; n < maxColors; n++)
571 if (n % printInterval == 0)
573 Info <<
"Global ColorSweep: " << n <<
" " <<
mesh_.time().elapsedClockTime() <<
" s" << endl;
578 PetscScalar* colColor;
579 VecGetArray(colors, &colColor);
581 for (label i = colorStart; i < colorEnd; i++)
583 label idx = i - colorStart;
589 VecRestoreArray(colors, &colColor);
596 for (label conPass = 0; conPass < 2; conPass++)
600 VecScatterBegin(colorScatter, colors, colorsLocal, INSERT_VALUES, SCATTER_FORWARD);
601 VecScatterEnd(colorScatter, colors, colorsLocal, INSERT_VALUES, SCATTER_FORWARD);
607 PetscScalar* colColorLocal;
608 VecGetArray(colorsLocal, &colColorLocal);
609 VecGetArray(colors, &colColor);
625 for (label i = Istart; i < Iend; i++)
627 label idx = i - Istart;
628 if (localRowList[idx] == 1)
632 MatGetRow(conMat, i, &nCols, &cols, &
vals);
633 MatGetRow(conIndMat, i, &nCols2, NULL, &vals2);
636 label smallest = nColG;
641 for (label j = 0; j < nCols; j++)
645 label colIdx = cols[j];
646 label localColIdx = round(vals2[j]);
654 conflictCols[j] = colIdx;
655 conflictLocalColIdx[j] = localColIdx;
664 if (localColumnStat[localColIdx] == 2)
669 else if (tbkrLocal[localColIdx] < smallest and colIdx >= start and colIdx < end)
671 smallest = tbkrLocal[localColIdx];
679 for (label j = 0; j < nCols; j++)
681 label colIdx = conflictCols[j];
682 label localColIdx = conflictLocalColIdx[j];
684 if (colIdx >= start && colIdx < end)
688 if (colIdx != idxKeep)
690 if (localColIdx >= 0)
692 if (localColumnStat[localColIdx] == 2)
694 Pout <<
"local Array Index: " << colIdx << endl;
695 Info <<
"Error, setting a local column!" << endl;
698 PetscScalar valIn = -1;
699 VecSetValue(colors, colIdx, valIn, INSERT_VALUES);
700 colColorLocal[localColIdx] = -1;
708 for (label j = 0; j < nCols; j++)
713 conflictCols[j] = -1;
714 conflictLocalColIdx[j] = -1;
719 MatRestoreRow(conIndMat, i, &nCols2, NULL, &vals2);
720 MatRestoreRow(conMat, i, &nCols, &cols, &
vals);
724 VecRestoreArray(colors, &colColor);
725 VecRestoreArray(colorsLocal, &colColorLocal);
726 VecAssemblyBegin(colors);
727 VecAssemblyEnd(colors);
731 label colorCounter = 0;
733 if (n % printInterval == 0)
735 Info <<
"Number of Uncolored: " << colorCounter <<
" " << notColored << endl;
739 Info <<
"Global ColorSweep: " << n <<
" " <<
mesh_.time().elapsedClockTime() <<
" s" << endl;
740 Info <<
"Number of Uncolored: " << colorCounter <<
" " << notColored << endl;
744 VecRestoreArray(globalTiebreaker, &tbkrGlobal);
745 VecRestoreArray(globalColumnStat, &globalStat);
750 for (label i = colorStart; i < colorEnd; i++)
752 VecGetValues(colors, 1, &i, &color);
754 if (color > currColor)
760 reduce(currColor, maxOp<label>());
762 nColors = currColor + 1;
764 Info <<
"Ncolors: " << nColors << endl;
770 VecRestoreArray(localTiebreaker, &tbkrLocal);
771 delete[] conflictCols;
772 delete[] conflictLocalColIdx;
773 delete[] globalIndexList;
774 delete[] localColumnStat;
775 ISDestroy(&globalIS);
776 VecScatterDestroy(&colorScatter);
777 VecDestroy(&colorsLocal);
778 VecDestroy(&globalTiebreaker);
779 VecDestroy(&globalColumnStat);
780 VecDestroy(&localTiebreaker);
781 delete[] localRowList;
782 VecDestroy(&globalVec);
783 MatDestroy(&conIndMat);
789 scalar& allNonZeros)
const
806 const PetscInt* cols;
807 const PetscScalar*
vals;
816 MatGetOwnershipRange(conMat, &Istart, &Iend);
819 for (label i = Istart; i < Iend; i++)
821 MatGetRow(conMat, i, &nCols, &cols, &
vals);
824 std::cout <<
"Warning! procI: " << Pstream::myProcNo() <<
" nCols <0 at rowI: " << i << std::endl;
825 std::cout <<
"Set nCols to zero " << std::endl;
832 allNonZeros += nCols;
833 MatRestoreRow(conMat, i, &nCols, &cols, &
vals);
837 reduce(maxCols, maxOp<label>());
839 reduce(allNonZeros, sumOp<scalar>());
848 const label* valArray)
const
869 for (label
k = start;
k < size;
k++)
871 if (valArray[
k] == target)
884 label& notColored)
const
900 PetscInt colorStart, colorEnd;
904 VecGetOwnershipRange(colors, &colorStart, &colorEnd);
906 const PetscScalar* colColor;
907 VecGetArrayRead(colors, &colColor);
909 for (label i = colorStart; i < colorEnd; i++)
911 color = colColor[i - colorStart];
923 VecRestoreArrayRead(colors, &colColor);
927 reduce(notColored, sumOp<label>());
928 reduce(colorCounter, sumOp<label>());
960 Info <<
"Validating Coloring..." << endl;
963 const PetscInt* cols;
964 const PetscScalar*
vals;
971 VecScatterCreateToAll(colors, &ctx, &vout);
972 VecScatterBegin(ctx, colors, vout, INSERT_VALUES, SCATTER_FORWARD);
973 VecScatterEnd(ctx, colors, vout, INSERT_VALUES, SCATTER_FORWARD);
975 PetscScalar* colorsArray;
976 VecGetArray(vout, &colorsArray);
979 MatGetOwnershipRange(conMat, &Istart, &Iend);
983 for (label i = Istart; i < Iend; i++)
985 MatGetRow(conMat, i, &nCols, &cols, &
vals);
990 MatRestoreRow(conMat, i, &nCols, &cols, &
vals);
994 labelList rowColors(colMax);
995 for (label i = Istart; i < Iend; i++)
997 MatGetRow(conMat, i, &nCols, &cols, &
vals);
1000 for (label nn = 0; nn < colMax; nn++)
1006 for (label j = 0; j < nCols; j++)
1010 rowColors[j] = round(colorsArray[cols[j]]);
1015 for (label nn = 0; nn < nCols; nn++)
1017 for (label mm = nn + 1; mm < nCols; mm++)
1019 if (rowColors[nn] != -1 && rowColors[nn] == rowColors[mm])
1021 FatalErrorIn(
"Conflicting Colors Found!")
1022 <<
" row: " << i <<
" col1: " << cols[nn] <<
" col2: " << cols[mm]
1023 <<
" color: " << rowColors[nn] << abort(FatalError);
1028 MatRestoreRow(conMat, i, &nCols, &cols, &
vals);
1031 VecRestoreArray(vout, &colorsArray);
1032 VecScatterDestroy(&ctx);
1035 Info <<
"No Conflicting Colors Found!" << endl;