Actual source code: color.c
1: /*
2: Routines that call the kernel minpack coloring subroutines
3: */
5: #include <petsc/private/matimpl.h>
6: #include <petsc/private/isimpl.h>
7: #include <../src/mat/graphops/color/impls/minpack/color.h>
9: /*
10: MatFDColoringDegreeSequence_Minpack - Calls the MINPACK routine seqr() that
11: computes the degree sequence required by MINPACK coloring routines.
12: */
13: PETSC_INTERN PetscErrorCode MatFDColoringDegreeSequence_Minpack(PetscInt m, const PetscInt *cja, const PetscInt *cia, const PetscInt *rja, const PetscInt *ria, PetscInt **seq)
14: {
15: PetscInt *work;
17: PetscFunctionBegin;
18: PetscCall(PetscMalloc1(m, &work));
19: PetscCall(PetscMalloc1(m, seq));
21: PetscCall(MINPACKdegr(&m, cja, cia, rja, ria, *seq, work));
23: PetscCall(PetscFree(work));
24: PetscFunctionReturn(PETSC_SUCCESS);
25: }
27: static PetscErrorCode MatColoringApply_SL(MatColoring mc, ISColoring *iscoloring)
28: {
29: PetscInt *list, *work, clique, *seq, *coloring, n;
30: const PetscInt *ria, *rja, *cia, *cja;
31: PetscInt ncolors, i;
32: PetscBool done;
33: Mat mat = mc->mat;
34: Mat mat_seq = mat;
35: PetscMPIInt size;
36: MPI_Comm comm;
37: ISColoring iscoloring_seq;
38: PetscInt bs = 1, rstart, rend, N_loc, nc;
39: ISColoringValue *colors_loc;
40: PetscBool flg1, flg2;
42: PetscFunctionBegin;
43: PetscCheck(mc->dist == 2, PETSC_COMM_SELF, PETSC_ERR_SUP, "SL may only do distance 2 coloring");
44: /* this is ugly way to get blocksize but cannot call MatGetBlockSize() because AIJ can have bs > 1 */
45: PetscCall(PetscObjectBaseTypeCompare((PetscObject)mat, MATSEQBAIJ, &flg1));
46: PetscCall(PetscObjectBaseTypeCompare((PetscObject)mat, MATMPIBAIJ, &flg2));
47: if (flg1 || flg2) PetscCall(MatGetBlockSize(mat, &bs));
49: PetscCall(PetscObjectGetComm((PetscObject)mat, &comm));
50: PetscCallMPI(MPI_Comm_size(comm, &size));
51: if (size > 1) {
52: /* create a sequential iscoloring on all processors */
53: PetscCall(MatGetSeqNonzeroStructure(mat, &mat_seq));
54: }
56: PetscCall(MatGetRowIJ(mat_seq, 1, PETSC_FALSE, PETSC_TRUE, &n, &ria, &rja, &done));
57: PetscCall(MatGetColumnIJ(mat_seq, 1, PETSC_FALSE, PETSC_TRUE, &n, &cia, &cja, &done));
58: PetscCheck(done, PETSC_COMM_SELF, PETSC_ERR_SUP, "Ordering requires IJ");
60: PetscCall(MatFDColoringDegreeSequence_Minpack(n, cja, cia, rja, ria, &seq));
62: PetscCall(PetscMalloc2(n, &list, 4 * n, &work));
64: PetscCall(MINPACKslo(&n, cja, cia, rja, ria, seq, list, &clique, work, work + n, work + 2 * n, work + 3 * n));
66: PetscCall(PetscMalloc1(n, &coloring));
67: PetscCall(MINPACKseq(&n, cja, cia, rja, ria, list, coloring, &ncolors, work));
69: PetscCall(PetscFree2(list, work));
70: PetscCall(PetscFree(seq));
71: PetscCall(MatRestoreRowIJ(mat_seq, 1, PETSC_FALSE, PETSC_TRUE, NULL, &ria, &rja, &done));
72: PetscCall(MatRestoreColumnIJ(mat_seq, 1, PETSC_FALSE, PETSC_TRUE, NULL, &cia, &cja, &done));
74: /* shift coloring numbers to start at zero and shorten */
75: PetscCheck(ncolors <= IS_COLORING_MAX - 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "Maximum color size exceeded");
76: {
77: ISColoringValue *s = (ISColoringValue *)coloring;
78: for (i = 0; i < n; i++) s[i] = (ISColoringValue)(coloring[i] - 1);
79: PetscCall(MatColoringPatch(mat_seq, ncolors, n, s, iscoloring));
80: }
82: if (size > 1) {
83: PetscCall(MatDestroySeqNonzeroStructure(&mat_seq));
85: /* convert iscoloring_seq to a parallel iscoloring */
86: iscoloring_seq = *iscoloring;
87: rstart = mat->rmap->rstart / bs;
88: rend = mat->rmap->rend / bs;
89: N_loc = rend - rstart; /* number of local nodes */
91: /* get local colors for each local node */
92: PetscCall(PetscMalloc1(N_loc + 1, &colors_loc));
93: for (i = rstart; i < rend; i++) colors_loc[i - rstart] = iscoloring_seq->colors[i];
94: /* create a parallel iscoloring */
95: nc = iscoloring_seq->n;
96: PetscCall(ISColoringCreate(comm, nc, N_loc, colors_loc, PETSC_OWN_POINTER, iscoloring));
97: PetscCall(ISColoringDestroy(&iscoloring_seq));
98: }
99: PetscFunctionReturn(PETSC_SUCCESS);
100: }
102: /*MC
103: MATCOLORINGSL - implements the SL (smallest last) coloring routine {cite}`more:coloring`
105: Level: beginner
107: Notes:
108: Supports only distance two colorings (for computation of Jacobians/Hessians)
110: This is a sequential algorithm
112: .seealso: `MatColoringCreate()`, `MatColoring`, `MatColoringSetType()`, `MATCOLORINGGREEDY`, `MatColoringType`
113: M*/
115: PETSC_EXTERN PetscErrorCode MatColoringCreate_SL(MatColoring mc)
116: {
117: PetscFunctionBegin;
118: mc->dist = 2;
119: mc->data = NULL;
120: mc->ops->apply = MatColoringApply_SL;
121: PetscFunctionReturn(PETSC_SUCCESS);
122: }
124: static PetscErrorCode MatColoringApply_LF(MatColoring mc, ISColoring *iscoloring)
125: {
126: PetscInt *list, *work, *seq, *coloring, n;
127: const PetscInt *ria, *rja, *cia, *cja;
128: PetscInt n1, none, ncolors, i;
129: PetscBool done;
130: Mat mat = mc->mat;
131: Mat mat_seq = mat;
132: PetscMPIInt size;
133: MPI_Comm comm;
134: ISColoring iscoloring_seq;
135: PetscInt bs = 1, rstart, rend, N_loc, nc;
136: ISColoringValue *colors_loc;
137: PetscBool flg1, flg2;
139: PetscFunctionBegin;
140: PetscCheck(mc->dist == 2, PETSC_COMM_SELF, PETSC_ERR_SUP, "LF may only do distance 2 coloring");
141: /* this is ugly way to get blocksize but cannot call MatGetBlockSize() because AIJ can have bs > 1 */
142: PetscCall(PetscObjectBaseTypeCompare((PetscObject)mat, MATSEQBAIJ, &flg1));
143: PetscCall(PetscObjectBaseTypeCompare((PetscObject)mat, MATMPIBAIJ, &flg2));
144: if (flg1 || flg2) PetscCall(MatGetBlockSize(mat, &bs));
146: PetscCall(PetscObjectGetComm((PetscObject)mat, &comm));
147: PetscCallMPI(MPI_Comm_size(comm, &size));
148: if (size > 1) {
149: /* create a sequential iscoloring on all processors */
150: PetscCall(MatGetSeqNonzeroStructure(mat, &mat_seq));
151: }
153: PetscCall(MatGetRowIJ(mat_seq, 1, PETSC_FALSE, PETSC_TRUE, &n, &ria, &rja, &done));
154: PetscCall(MatGetColumnIJ(mat_seq, 1, PETSC_FALSE, PETSC_TRUE, &n, &cia, &cja, &done));
155: PetscCheck(done, PETSC_COMM_SELF, PETSC_ERR_SUP, "Ordering requires IJ");
157: PetscCall(MatFDColoringDegreeSequence_Minpack(n, cja, cia, rja, ria, &seq));
159: PetscCall(PetscMalloc2(n, &list, 4 * n, &work));
161: n1 = n - 1;
162: none = -1;
163: PetscCall(MINPACKnumsrt(&n, &n1, seq, &none, list, work + 2 * n, work + n));
164: PetscCall(PetscMalloc1(n, &coloring));
165: PetscCall(MINPACKseq(&n, cja, cia, rja, ria, list, coloring, &ncolors, work));
167: PetscCall(PetscFree2(list, work));
168: PetscCall(PetscFree(seq));
170: PetscCall(MatRestoreRowIJ(mat_seq, 1, PETSC_FALSE, PETSC_TRUE, NULL, &ria, &rja, &done));
171: PetscCall(MatRestoreColumnIJ(mat_seq, 1, PETSC_FALSE, PETSC_TRUE, NULL, &cia, &cja, &done));
173: /* shift coloring numbers to start at zero and shorten */
174: PetscCheck(ncolors <= IS_COLORING_MAX - 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "Maximum color size exceeded");
175: {
176: ISColoringValue *s = (ISColoringValue *)coloring;
177: for (i = 0; i < n; i++) s[i] = (ISColoringValue)(coloring[i] - 1);
178: PetscCall(MatColoringPatch(mat_seq, ncolors, n, s, iscoloring));
179: }
181: if (size > 1) {
182: PetscCall(MatDestroySeqNonzeroStructure(&mat_seq));
184: /* convert iscoloring_seq to a parallel iscoloring */
185: iscoloring_seq = *iscoloring;
186: rstart = mat->rmap->rstart / bs;
187: rend = mat->rmap->rend / bs;
188: N_loc = rend - rstart; /* number of local nodes */
190: /* get local colors for each local node */
191: PetscCall(PetscMalloc1(N_loc + 1, &colors_loc));
192: for (i = rstart; i < rend; i++) colors_loc[i - rstart] = iscoloring_seq->colors[i];
194: /* create a parallel iscoloring */
195: nc = iscoloring_seq->n;
196: PetscCall(ISColoringCreate(comm, nc, N_loc, colors_loc, PETSC_OWN_POINTER, iscoloring));
197: PetscCall(ISColoringDestroy(&iscoloring_seq));
198: }
199: PetscFunctionReturn(PETSC_SUCCESS);
200: }
202: /*MC
203: MATCOLORINGLF - implements the LF (largest first) coloring routine {cite}`more:coloring`
205: Level: beginner
207: Notes:
208: Supports only distance two colorings (for computation of Jacobians/Hessians)
210: This is a sequential algorithm
212: .seealso: `MatColoringTpe`, `MatColoringCreate()`, `MatColoring`, `MatColoringSetType()`, `MATCOLORINGGREEDY`, `MatColoringType`
213: M*/
215: PETSC_EXTERN PetscErrorCode MatColoringCreate_LF(MatColoring mc)
216: {
217: PetscFunctionBegin;
218: mc->dist = 2;
219: mc->data = NULL;
220: mc->ops->apply = MatColoringApply_LF;
221: PetscFunctionReturn(PETSC_SUCCESS);
222: }
224: static PetscErrorCode MatColoringApply_ID(MatColoring mc, ISColoring *iscoloring)
225: {
226: PetscInt *list, *work, clique, *seq, *coloring, n;
227: const PetscInt *ria, *rja, *cia, *cja;
228: PetscInt ncolors;
229: PetscBool done;
230: Mat mat = mc->mat;
231: Mat mat_seq = mat;
232: PetscMPIInt size;
233: MPI_Comm comm;
234: ISColoring iscoloring_seq;
235: PetscInt bs = 1, rstart, rend, N_loc, nc;
236: ISColoringValue *colors_loc;
237: PetscBool flg1, flg2;
239: PetscFunctionBegin;
240: PetscCheck(mc->dist == 2, PETSC_COMM_SELF, PETSC_ERR_SUP, "IDO may only do distance 2 coloring");
241: /* this is ugly way to get blocksize but cannot call MatGetBlockSize() because AIJ can have bs > 1 */
242: PetscCall(PetscObjectBaseTypeCompare((PetscObject)mat, MATSEQBAIJ, &flg1));
243: PetscCall(PetscObjectBaseTypeCompare((PetscObject)mat, MATMPIBAIJ, &flg2));
244: if (flg1 || flg2) PetscCall(MatGetBlockSize(mat, &bs));
246: PetscCall(PetscObjectGetComm((PetscObject)mat, &comm));
247: PetscCallMPI(MPI_Comm_size(comm, &size));
248: if (size > 1) {
249: /* create a sequential iscoloring on all processors */
250: PetscCall(MatGetSeqNonzeroStructure(mat, &mat_seq));
251: }
253: PetscCall(MatGetRowIJ(mat_seq, 1, PETSC_FALSE, PETSC_TRUE, &n, &ria, &rja, &done));
254: PetscCall(MatGetColumnIJ(mat_seq, 1, PETSC_FALSE, PETSC_TRUE, &n, &cia, &cja, &done));
255: PetscCheck(done, PETSC_COMM_SELF, PETSC_ERR_SUP, "Ordering requires IJ");
257: PetscCall(MatFDColoringDegreeSequence_Minpack(n, cja, cia, rja, ria, &seq));
259: PetscCall(PetscMalloc2(n, &list, 4 * n, &work));
261: PetscCall(MINPACKido(&n, &n, cja, cia, rja, ria, seq, list, &clique, work, work + n, work + 2 * n, work + 3 * n));
263: PetscCall(PetscMalloc1(n, &coloring));
264: PetscCall(MINPACKseq(&n, cja, cia, rja, ria, list, coloring, &ncolors, work));
266: PetscCall(PetscFree2(list, work));
267: PetscCall(PetscFree(seq));
269: PetscCall(MatRestoreRowIJ(mat_seq, 1, PETSC_FALSE, PETSC_TRUE, NULL, &ria, &rja, &done));
270: PetscCall(MatRestoreColumnIJ(mat_seq, 1, PETSC_FALSE, PETSC_TRUE, NULL, &cia, &cja, &done));
272: /* shift coloring numbers to start at zero and shorten */
273: PetscCheck(ncolors <= IS_COLORING_MAX - 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "Maximum color size exceeded");
274: {
275: ISColoringValue *s = (ISColoringValue *)coloring;
276: for (PetscInt i = 0; i < n; i++) s[i] = (ISColoringValue)(coloring[i] - 1);
277: PetscCall(MatColoringPatch(mat_seq, ncolors, n, s, iscoloring));
278: }
280: if (size > 1) {
281: PetscCall(MatDestroySeqNonzeroStructure(&mat_seq));
283: /* convert iscoloring_seq to a parallel iscoloring */
284: iscoloring_seq = *iscoloring;
285: rstart = mat->rmap->rstart / bs;
286: rend = mat->rmap->rend / bs;
287: N_loc = rend - rstart; /* number of local nodes */
289: /* get local colors for each local node */
290: PetscCall(PetscMalloc1(N_loc + 1, &colors_loc));
291: for (PetscInt i = rstart; i < rend; i++) colors_loc[i - rstart] = iscoloring_seq->colors[i];
292: /* create a parallel iscoloring */
293: nc = iscoloring_seq->n;
294: PetscCall(ISColoringCreate(comm, nc, N_loc, colors_loc, PETSC_OWN_POINTER, iscoloring));
295: PetscCall(ISColoringDestroy(&iscoloring_seq));
296: }
297: PetscFunctionReturn(PETSC_SUCCESS);
298: }
300: /*MC
301: MATCOLORINGID - implements the ID (incidence degree) coloring routine {cite}`more:coloring`
303: Level: beginner
305: Notes:
306: Supports only distance two colorings (for computation of Jacobians/Hessians)
308: This is a sequential algorithm
310: .seealso: `MatColoringCreate()`, `MatColoring`, `MatColoringSetType()`, `MATCOLORINGGREEDY`, `MatColoringType`
311: M*/
313: PETSC_EXTERN PetscErrorCode MatColoringCreate_ID(MatColoring mc)
314: {
315: PetscFunctionBegin;
316: mc->dist = 2;
317: mc->data = NULL;
318: mc->ops->apply = MatColoringApply_ID;
319: PetscFunctionReturn(PETSC_SUCCESS);
320: }