Actual source code: sorder.c
1: #define PETSCMAT_DLL
3: /*
4: Provides the code that allows PETSc users to register their own
5: sequential matrix Ordering routines.
6: */
7: #include include/private/matimpl.h
8: #include petscmat.h
10: PetscFList MatOrderingList = 0;
11: PetscTruth MatOrderingRegisterAllCalled = PETSC_FALSE;
13: EXTERN PetscErrorCode MatOrdering_Flow_SeqAIJ(Mat,const MatOrderingType,IS *,IS *);
17: PetscErrorCode MatOrdering_Flow(Mat mat,const MatOrderingType type,IS *irow,IS *icol)
18: {
20: SETERRQ(PETSC_ERR_SUP,"Cannot do default flow ordering for matrix type");
21: #if !defined(PETSC_USE_DEBUG)
22: return(0);
23: #endif
24: }
29: PetscErrorCode MatOrdering_Natural(Mat mat,const MatOrderingType type,IS *irow,IS *icol)
30: {
32: PetscInt n,i,*ii;
33: PetscTruth done;
34: MPI_Comm comm;
37: PetscObjectGetComm((PetscObject)mat,&comm);
38: MatGetRowIJ(mat,0,PETSC_FALSE,PETSC_TRUE,&n,PETSC_NULL,PETSC_NULL,&done);
39: MatRestoreRowIJ(mat,0,PETSC_FALSE,PETSC_TRUE,&n,PETSC_NULL,PETSC_NULL,&done);
40: if (done) { /* matrix may be "compressed" in symbolic factorization, due to i-nodes or block storage */
41: /*
42: We actually create general index sets because this avoids mallocs to
43: to obtain the indices in the MatSolve() routines.
44: ISCreateStride(PETSC_COMM_SELF,n,0,1,irow);
45: ISCreateStride(PETSC_COMM_SELF,n,0,1,icol);
46: */
47: PetscMalloc(n*sizeof(PetscInt),&ii);
48: for (i=0; i<n; i++) ii[i] = i;
49: ISCreateGeneral(PETSC_COMM_SELF,n,ii,irow);
50: ISCreateGeneral(PETSC_COMM_SELF,n,ii,icol);
51: PetscFree(ii);
52: } else {
53: PetscInt start,end;
55: MatGetOwnershipRange(mat,&start,&end);
56: ISCreateStride(comm,end-start,start,1,irow);
57: ISCreateStride(comm,end-start,start,1,icol);
58: }
59: ISSetIdentity(*irow);
60: ISSetIdentity(*icol);
61: return(0);
62: }
66: /*
67: Orders the rows (and columns) by the lengths of the rows.
68: This produces a symmetric Ordering but does not require a
69: matrix with symmetric non-zero structure.
70: */
73: PetscErrorCode MatOrdering_RowLength(Mat mat,const MatOrderingType type,IS *irow,IS *icol)
74: {
76: PetscInt n,*ia,*ja,*permr,*lens,i;
77: PetscTruth done;
80: MatGetRowIJ(mat,0,PETSC_FALSE,PETSC_TRUE,&n,&ia,&ja,&done);
81: if (!done) SETERRQ(PETSC_ERR_SUP,"Cannot get rows for matrix");
83: PetscMalloc(2*n*sizeof(PetscInt),&lens);
84: permr = lens + n;
85: for (i=0; i<n; i++) {
86: lens[i] = ia[i+1] - ia[i];
87: permr[i] = i;
88: }
89: MatRestoreRowIJ(mat,0,PETSC_FALSE,PETSC_TRUE,&n,&ia,&ja,&done);
91: PetscSortIntWithPermutation(n,lens,permr);
93: ISCreateGeneral(PETSC_COMM_SELF,n,permr,irow);
94: ISCreateGeneral(PETSC_COMM_SELF,n,permr,icol);
95: PetscFree(lens);
96: return(0);
97: }
102: PetscErrorCode MatOrderingRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(Mat,const MatOrderingType,IS*,IS*))
103: {
105: char fullname[PETSC_MAX_PATH_LEN];
108: PetscFListConcat(path,name,fullname);
109: PetscFListAdd(&MatOrderingList,sname,fullname,(void (*)(void))function);
110: return(0);
111: }
115: /*@
116: MatOrderingRegisterDestroy - Frees the list of ordering routines.
118: Not collective
120: Level: developer
121:
122: .keywords: matrix, register, destroy
124: .seealso: MatOrderingRegisterDynamic(), MatOrderingRegisterAll()
125: @*/
126: PetscErrorCode MatOrderingRegisterDestroy(void)
127: {
131: PetscFListDestroy(&MatOrderingList);
132: return(0);
133: }
135: #include src/mat/impls/aij/mpi/mpiaij.h
138: /*@C
139: MatGetOrdering - Gets a reordering for a matrix to reduce fill or to
140: improve numerical stability of LU factorization.
142: Collective on Mat
144: Input Parameters:
145: + mat - the matrix
146: - type - type of reordering, one of the following:
147: $ MATORDERING_NATURAL - Natural
148: $ MATORDERING_ND - Nested Dissection
149: $ MATORDERING_1WD - One-way Dissection
150: $ MATORDERING_RCM - Reverse Cuthill-McKee
151: $ MATORDERING_QMD - Quotient Minimum Degree
153: Output Parameters:
154: + rperm - row permutation indices
155: - cperm - column permutation indices
158: Options Database Key:
159: . -mat_view_ordering_draw - plots matrix nonzero structure in new ordering
161: Level: intermediate
162:
163: Notes:
164: This DOES NOT actually reorder the matrix; it merely returns two index sets
165: that define a reordering. This is usually not used directly, rather use the
166: options PCFactorSetMatOrderingType()
168: The user can define additional orderings; see MatOrderingRegisterDynamic().
170: .keywords: matrix, set, ordering, factorization, direct, ILU, LU,
171: fill, reordering, natural, Nested Dissection,
172: One-way Dissection, Cholesky, Reverse Cuthill-McKee,
173: Quotient Minimum Degree
175: .seealso: MatOrderingRegisterDynamic(), PCFactorSetMatOrderingType()
176: @*/
177: PetscErrorCode MatGetOrdering(Mat mat,const MatOrderingType type,IS *rperm,IS *cperm)
178: {
180: PetscInt mmat,nmat,mis,m;
181: PetscErrorCode (*r)(Mat,const MatOrderingType,IS*,IS*);
182: PetscTruth flg,isseqdense,ismpidense;
188: if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
189: if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
191: PetscTypeCompare((PetscObject)mat,MATSEQDENSE,&isseqdense);
192: PetscTypeCompare((PetscObject)mat,MATMPIDENSE,&ismpidense);
193: if (isseqdense || ismpidense) {
194: MatGetLocalSize(mat,&m,PETSC_NULL);
195: /*
196: Dense matrices only give natural ordering
197: */
198: ISCreateStride(PETSC_COMM_SELF,0,m,1,cperm);
199: ISCreateStride(PETSC_COMM_SELF,0,m,1,rperm);
200: ISSetIdentity(*cperm);
201: ISSetIdentity(*rperm);
202: ISSetPermutation(*rperm);
203: ISSetPermutation(*cperm);
204: return(0);
205: }
207: if (!mat->rmap.N) { /* matrix has zero rows */
208: ISCreateStride(PETSC_COMM_SELF,0,0,1,cperm);
209: ISCreateStride(PETSC_COMM_SELF,0,0,1,rperm);
210: ISSetIdentity(*cperm);
211: ISSetIdentity(*rperm);
212: ISSetPermutation(*rperm);
213: ISSetPermutation(*cperm);
214: return(0);
215: }
217: if (!MatOrderingRegisterAllCalled) {
218: MatOrderingRegisterAll(PETSC_NULL);
219: }
222: PetscFListFind(MatOrderingList,mat->comm,type,(void (**)(void)) &r);
223: if (!r) {SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Unknown or unregistered type: %s",type);}
225: (*r)(mat,type,rperm,cperm);
226: ISSetPermutation(*rperm);
227: ISSetPermutation(*cperm);
229: /*
230: Adjust for inode (reduced matrix ordering) only if row permutation
231: is smaller then matrix size
232: */
233: MatGetLocalSize(mat,&mmat,&nmat);
234: ISGetLocalSize(*rperm,&mis);
235: if (mmat > mis) {
236: MatInodeAdjustForInodes(mat,rperm,cperm);
237: }
241: PetscOptionsHasName(PETSC_NULL,"-mat_view_ordering_draw",&flg);
242: if (flg) {
243: Mat tmat;
244: PetscOptionsHasName(PETSC_NULL,"-mat_view_contour",&flg);
245: if (flg) {
246: PetscViewerPushFormat(PETSC_VIEWER_DRAW_(mat->comm),PETSC_VIEWER_DRAW_CONTOUR);
247: }
248: MatPermute(mat,*rperm,*cperm,&tmat);
249: MatView(tmat,PETSC_VIEWER_DRAW_(mat->comm));
250: if (flg) {
251: PetscViewerPopFormat(PETSC_VIEWER_DRAW_(mat->comm));
252: }
253: MatDestroy(tmat);
254: }
256: return(0);
257: }
261: PetscErrorCode MatGetOrderingList(PetscFList *list)
262: {
264: *list = MatOrderingList;
265: return(0);
266: }