Actual source code: fdaij.c
2: #include <../src/mat/impls/aij/seq/aij.h>
6: /*
7: This routine is shared by AIJ and BAIJ matrices, since it operators only on the nonzero structure of the elements or blocks.
8: This is why it has the ugly code with the MatGetBlockSize()
9: */
10: PetscErrorCode MatFDColoringCreate_SeqAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
11: {
13: PetscInt i,n,nrows,N,j,k,m,*rows,*ci,*cj,ncols,col;
14: const PetscInt *is;
15: PetscInt nis = iscoloring->n,*rowhit,*columnsforrow,l,bs = 1;
16: IS *isa;
17: PetscBool done,flg = PETSC_FALSE;
18: PetscBool flg1,flg2;
21: if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled by calls to MatAssemblyBegin/End();");
23: ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);
24: /* this is ugly way to get blocksize but cannot call MatGetBlockSize() because AIJ can have bs > 1 */
25: PetscTypeCompare((PetscObject)mat,MATSEQBAIJ,&flg1);
26: PetscTypeCompare((PetscObject)mat,MATMPIBAIJ,&flg2);
27: if (flg1 || flg2) {
28: MatGetBlockSize(mat,&bs);
29: }
31: N = mat->cmap->N/bs;
32: c->M = mat->rmap->N/bs; /* set total rows, columns and local rows */
33: c->N = mat->cmap->N/bs;
34: c->m = mat->rmap->N/bs;
35: c->rstart = 0;
37: c->ncolors = nis;
38: PetscMalloc(nis*sizeof(PetscInt),&c->ncolumns);
39: PetscMalloc(nis*sizeof(PetscInt*),&c->columns);
40: PetscMalloc(nis*sizeof(PetscInt),&c->nrows);
41: PetscMalloc(nis*sizeof(PetscInt*),&c->rows);
42: PetscMalloc(nis*sizeof(PetscInt*),&c->columnsforrow);
44: MatGetColumnIJ(mat,0,PETSC_FALSE,PETSC_FALSE,&ncols,&ci,&cj,&done);
45: if (!done) SETERRQ1(((PetscObject)mat)->comm,PETSC_ERR_SUP,"MatGetColumnIJ() not supported for matrix type %s",((PetscObject)mat)->type_name);
47: /*
48: Temporary option to allow for debugging/testing
49: */
50: PetscOptionsGetBool(PETSC_NULL,"-matfdcoloring_slow",&flg,PETSC_NULL);
52: PetscMalloc((N+1)*sizeof(PetscInt),&rowhit);
53: PetscMalloc((N+1)*sizeof(PetscInt),&columnsforrow);
55: for (i=0; i<nis; i++) {
56: ISGetLocalSize(isa[i],&n);
57: ISGetIndices(isa[i],&is);
58: c->ncolumns[i] = n;
59: if (n) {
60: PetscMalloc(n*sizeof(PetscInt),&c->columns[i]);
61: PetscMemcpy(c->columns[i],is,n*sizeof(PetscInt));
62: } else {
63: c->columns[i] = 0;
64: }
66: if (!flg) { /* ------------------------------------------------------------------------------*/
67: /* fast, crude version requires O(N*N) work */
68: PetscMemzero(rowhit,N*sizeof(PetscInt));
69: /* loop over columns*/
70: for (j=0; j<n; j++) {
71: col = is[j];
72: rows = cj + ci[col];
73: m = ci[col+1] - ci[col];
74: /* loop over columns marking them in rowhit */
75: for (k=0; k<m; k++) {
76: rowhit[*rows++] = col + 1;
77: }
78: }
79: /* count the number of hits */
80: nrows = 0;
81: for (j=0; j<N; j++) {
82: if (rowhit[j]) nrows++;
83: }
84: c->nrows[i] = nrows;
85: PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);
86: PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);
87: nrows = 0;
88: for (j=0; j<N; j++) {
89: if (rowhit[j]) {
90: c->rows[i][nrows] = j;
91: c->columnsforrow[i][nrows] = rowhit[j] - 1;
92: nrows++;
93: }
94: }
95: } else { /*-------------------------------------------------------------------------------*/
96: /* slow version, using rowhit as a linked list */
97: PetscInt currentcol,fm,mfm;
98: rowhit[N] = N;
99: nrows = 0;
100: /* loop over columns */
101: for (j=0; j<n; j++) {
102: col = is[j];
103: rows = cj + ci[col];
104: m = ci[col+1] - ci[col];
105: /* loop over columns marking them in rowhit */
106: fm = N; /* fm points to first entry in linked list */
107: for (k=0; k<m; k++) {
108: currentcol = *rows++;
109: /* is it already in the list? */
110: do {
111: mfm = fm;
112: fm = rowhit[fm];
113: } while (fm < currentcol);
114: /* not in list so add it */
115: if (fm != currentcol) {
116: nrows++;
117: columnsforrow[currentcol] = col;
118: /* next three lines insert new entry into linked list */
119: rowhit[mfm] = currentcol;
120: rowhit[currentcol] = fm;
121: fm = currentcol;
122: /* fm points to present position in list since we know the columns are sorted */
123: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Detected invalid coloring");
124: }
125: }
126: c->nrows[i] = nrows;
127: PetscMalloc((nrows+1)*sizeof(PetscInt),&c->rows[i]);
128: PetscMalloc((nrows+1)*sizeof(PetscInt),&c->columnsforrow[i]);
129: /* now store the linked list of rows into c->rows[i] */
130: nrows = 0;
131: fm = rowhit[N];
132: do {
133: c->rows[i][nrows] = fm;
134: c->columnsforrow[i][nrows++] = columnsforrow[fm];
135: fm = rowhit[fm];
136: } while (fm < N);
137: } /* ---------------------------------------------------------------------------------------*/
138: ISRestoreIndices(isa[i],&is);
139: }
140: MatRestoreColumnIJ(mat,0,PETSC_FALSE,PETSC_FALSE,&ncols,&ci,&cj,&done);
142: PetscFree(rowhit);
143: PetscFree(columnsforrow);
145: /* Optimize by adding the vscale, and scaleforrow[][] fields */
146: /*
147: see the version for MPIAIJ
148: */
149: VecCreateGhost(((PetscObject)mat)->comm,mat->rmap->n,PETSC_DETERMINE,0,PETSC_NULL,&c->vscale);
150: PetscMalloc(c->ncolors*sizeof(PetscInt*),&c->vscaleforrow);
151: for (k=0; k<c->ncolors; k++) {
152: PetscMalloc((c->nrows[k]+1)*sizeof(PetscInt),&c->vscaleforrow[k]);
153: for (l=0; l<c->nrows[k]; l++) {
154: col = c->columnsforrow[k][l];
155: c->vscaleforrow[k][l] = col;
156: }
157: }
158: ISColoringRestoreIS(iscoloring,&isa);
159: return(0);
160: }