Actual source code: sorder.c

petsc-3.12.5 2020-03-29
Report Typos and Errors

  2: /*
  3:      Provides the code that allows PETSc users to register their own
  4:   sequential matrix Ordering routines.
  5: */
  6:  #include <petsc/private/matimpl.h>
  7:  #include <petscmat.h>

  9: PetscFunctionList MatOrderingList              = 0;
 10: PetscBool         MatOrderingRegisterAllCalled = PETSC_FALSE;

 12: extern PetscErrorCode MatGetOrdering_Flow_SeqAIJ(Mat,MatOrderingType,IS*,IS*);

 14: PetscErrorCode MatGetOrdering_Flow(Mat mat,MatOrderingType type,IS *irow,IS *icol)
 15: {
 17:   SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot do default flow ordering for matrix type");
 18: #if !defined(PETSC_USE_DEBUG)
 19:   return(0);
 20: #endif
 21: }

 23: PETSC_INTERN PetscErrorCode MatGetOrdering_Natural(Mat mat,MatOrderingType type,IS *irow,IS *icol)
 24: {
 26:   PetscInt       n,i,*ii;
 27:   PetscBool      done;
 28:   MPI_Comm       comm;

 31:   PetscObjectGetComm((PetscObject)mat,&comm);
 32:   MatGetRowIJ(mat,0,PETSC_FALSE,PETSC_TRUE,&n,NULL,NULL,&done);
 33:   MatRestoreRowIJ(mat,0,PETSC_FALSE,PETSC_TRUE,NULL,NULL,NULL,&done);
 34:   if (done) { /* matrix may be "compressed" in symbolic factorization, due to i-nodes or block storage */
 35:     /*
 36:       We actually create general index sets because this avoids mallocs to
 37:       to obtain the indices in the MatSolve() routines.
 38:       ISCreateStride(PETSC_COMM_SELF,n,0,1,irow);
 39:       ISCreateStride(PETSC_COMM_SELF,n,0,1,icol);
 40:     */
 41:     PetscMalloc1(n,&ii);
 42:     for (i=0; i<n; i++) ii[i] = i;
 43:     ISCreateGeneral(PETSC_COMM_SELF,n,ii,PETSC_COPY_VALUES,irow);
 44:     ISCreateGeneral(PETSC_COMM_SELF,n,ii,PETSC_OWN_POINTER,icol);
 45:   } else {
 46:     PetscInt start,end;

 48:     MatGetOwnershipRange(mat,&start,&end);
 49:     ISCreateStride(comm,end-start,start,1,irow);
 50:     ISCreateStride(comm,end-start,start,1,icol);
 51:   }
 52:   ISSetIdentity(*irow);
 53:   ISSetIdentity(*icol);
 54:   return(0);
 55: }

 57: /*
 58:      Orders the rows (and columns) by the lengths of the rows.
 59:    This produces a symmetric Ordering but does not require a
 60:    matrix with symmetric non-zero structure.
 61: */
 62: PETSC_INTERN PetscErrorCode MatGetOrdering_RowLength(Mat mat,MatOrderingType type,IS *irow,IS *icol)
 63: {
 65:   PetscInt       n,*permr,*lens,i;
 66:   const PetscInt *ia,*ja;
 67:   PetscBool      done;

 70:   MatGetRowIJ(mat,0,PETSC_FALSE,PETSC_TRUE,&n,&ia,&ja,&done);
 71:   if (!done) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot get rows for matrix");

 73:   PetscMalloc2(n,&lens,n,&permr);
 74:   for (i=0; i<n; i++) {
 75:     lens[i]  = ia[i+1] - ia[i];
 76:     permr[i] = i;
 77:   }
 78:   MatRestoreRowIJ(mat,0,PETSC_FALSE,PETSC_TRUE,NULL,&ia,&ja,&done);

 80:   PetscSortIntWithPermutation(n,lens,permr);

 82:   ISCreateGeneral(PETSC_COMM_SELF,n,permr,PETSC_COPY_VALUES,irow);
 83:   ISCreateGeneral(PETSC_COMM_SELF,n,permr,PETSC_COPY_VALUES,icol);
 84:   PetscFree2(lens,permr);
 85:   return(0);
 86: }

 88: /*@C
 89:    MatOrderingRegister - Adds a new sparse matrix ordering to the matrix package.

 91:    Not Collective

 93:    Input Parameters:
 94: +  sname - name of ordering (for example MATORDERINGND)
 95: -  function - function pointer that creates the ordering

 97:    Level: developer

 99:    Sample usage:
100: .vb
101:    MatOrderingRegister("my_order", MyOrder);
102: .ve

104:    Then, your partitioner can be chosen with the procedural interface via
105: $     MatOrderingSetType(part,"my_order)
106:    or at runtime via the option
107: $     -pc_factor_mat_ordering_type my_order

109: .seealso: MatOrderingRegisterDestroy(), MatOrderingRegisterAll()
110: @*/
111: PetscErrorCode  MatOrderingRegister(const char sname[],PetscErrorCode (*function)(Mat,MatOrderingType,IS*,IS*))
112: {

116:   MatInitializePackage();
117:   PetscFunctionListAdd(&MatOrderingList,sname,function);
118:   return(0);
119: }

121:  #include <../src/mat/impls/aij/mpi/mpiaij.h>
122: /*@C
123:    MatGetOrdering - Gets a reordering for a matrix to reduce fill or to
124:    improve numerical stability of LU factorization.

126:    Collective on Mat

128:    Input Parameters:
129: +  mat - the matrix
130: -  type - type of reordering, one of the following:
131: $      MATORDERINGNATURAL - Natural
132: $      MATORDERINGND - Nested Dissection
133: $      MATORDERING1WD - One-way Dissection
134: $      MATORDERINGRCM - Reverse Cuthill-McKee
135: $      MATORDERINGQMD - Quotient Minimum Degree

137:    Output Parameters:
138: +  rperm - row permutation indices
139: -  cperm - column permutation indices


142:    Options Database Key:
143: . -mat_view_ordering draw - plots matrix nonzero structure in new ordering

145:    Level: intermediate

147:    Notes:
148:       This DOES NOT actually reorder the matrix; it merely returns two index sets
149:    that define a reordering. This is usually not used directly, rather use the
150:    options PCFactorSetMatOrderingType()

152:    The user can define additional orderings; see MatOrderingRegister().

154:    These are generally only implemented for sequential sparse matrices.

156:    The external packages that PETSc can use for direct factorization such as SuperLU do not accept orderings provided by
157:    this call.


160:            fill, reordering, natural, Nested Dissection,
161:            One-way Dissection, Cholesky, Reverse Cuthill-McKee,
162:            Quotient Minimum Degree

164: .seealso:   MatOrderingRegister(), PCFactorSetMatOrderingType()
165: @*/
166: PetscErrorCode  MatGetOrdering(Mat mat,MatOrderingType type,IS *rperm,IS *cperm)
167: {
169:   PetscInt       mmat,nmat,mis,m;
170:   PetscErrorCode (*r)(Mat,MatOrderingType,IS*,IS*);
171:   PetscBool      flg = PETSC_FALSE,isseqdense,ismpidense,ismpiaij,ismpibaij,ismpisbaij,ismpiaijcusparse,iselemental;

177:   if (!mat->assembled) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
178:   if (mat->factortype) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");

180:   /* This code is terrible. MatGetOrdering() multiple dispatch should use matrix and this code should move to impls/aij/mpi. */
181:   PetscObjectTypeCompare((PetscObject)mat,MATMPIAIJ,&ismpiaij);
182:   if (ismpiaij) {               /* Reorder using diagonal block */
183:     Mat            Ad,Ao;
184:     const PetscInt *colmap;
185:     IS             lrowperm,lcolperm;
186:     PetscInt       i,rstart,rend,*idx;
187:     const PetscInt *lidx;

189:     MatMPIAIJGetSeqAIJ(mat,&Ad,&Ao,&colmap);
190:     MatGetOrdering(Ad,type,&lrowperm,&lcolperm);
191:     MatGetOwnershipRange(mat,&rstart,&rend);
192:     /* Remap row index set to global space */
193:     ISGetIndices(lrowperm,&lidx);
194:     PetscMalloc1(rend-rstart,&idx);
195:     for (i=0; i+rstart<rend; i++) idx[i] = rstart + lidx[i];
196:     ISRestoreIndices(lrowperm,&lidx);
197:     ISDestroy(&lrowperm);
198:     ISCreateGeneral(PetscObjectComm((PetscObject)mat),rend-rstart,idx,PETSC_OWN_POINTER,rperm);
199:     ISSetPermutation(*rperm);
200:     /* Remap column index set to global space */
201:     ISGetIndices(lcolperm,&lidx);
202:     PetscMalloc1(rend-rstart,&idx);
203:     for (i=0; i+rstart<rend; i++) idx[i] = rstart + lidx[i];
204:     ISRestoreIndices(lcolperm,&lidx);
205:     ISDestroy(&lcolperm);
206:     ISCreateGeneral(PetscObjectComm((PetscObject)mat),rend-rstart,idx,PETSC_OWN_POINTER,cperm);
207:     ISSetPermutation(*cperm);
208:     return(0);
209:   }

211:   /* this chunk of code is REALLY bad, should maybe get the ordering from the factor matrix,
212:      then those that don't support orderings will handle their cases themselves. */
213:   PetscObjectTypeCompare((PetscObject)mat,MATSEQDENSE,&isseqdense);
214:   PetscObjectTypeCompare((PetscObject)mat,MATMPIDENSE,&ismpidense);
215:   PetscObjectTypeCompare((PetscObject)mat,MATMPIAIJCUSPARSE,&ismpiaijcusparse);
216:   PetscObjectTypeCompare((PetscObject)mat,MATMPIBAIJ,&ismpibaij);
217:   PetscObjectTypeCompare((PetscObject)mat,MATMPISBAIJ,&ismpisbaij);
218:   PetscObjectTypeCompare((PetscObject)mat,MATELEMENTAL,&iselemental);
219:   if (isseqdense || ismpidense || ismpibaij || ismpisbaij || ismpiaijcusparse || iselemental) {
220:     MatGetLocalSize(mat,&m,NULL);
221:     /*
222:        These matrices only give natural ordering
223:     */
224:     ISCreateStride(PETSC_COMM_SELF,m,0,1,cperm);
225:     ISCreateStride(PETSC_COMM_SELF,m,0,1,rperm);
226:     ISSetIdentity(*cperm);
227:     ISSetIdentity(*rperm);
228:     return(0);
229:   }

231:   if (!mat->rmap->N) { /* matrix has zero rows */
232:     ISCreateStride(PETSC_COMM_SELF,0,0,1,cperm);
233:     ISCreateStride(PETSC_COMM_SELF,0,0,1,rperm);
234:     ISSetIdentity(*cperm);
235:     ISSetIdentity(*rperm);
236:     return(0);
237:   }

239:   MatGetLocalSize(mat,&mmat,&nmat);
240:   if (mmat != nmat) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Must be square matrix, rows %D columns %D",mmat,nmat);

242:   MatOrderingRegisterAll();
243:   PetscFunctionListFind(MatOrderingList,type,&r);
244:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown or unregistered type: %s",type);

246:   PetscLogEventBegin(MAT_GetOrdering,mat,0,0,0);
247:   (*r)(mat,type,rperm,cperm);
248:   ISSetPermutation(*rperm);
249:   ISSetPermutation(*cperm);
250:   /* Adjust for inode (reduced matrix ordering) only if row permutation is smaller the matrix size */
251:   ISGetLocalSize(*rperm,&mis);
252:   if (mmat > mis) {MatInodeAdjustForInodes(mat,rperm,cperm);}
253:   PetscLogEventEnd(MAT_GetOrdering,mat,0,0,0);


256:   PetscOptionsHasName(((PetscObject)mat)->options,((PetscObject)mat)->prefix,"-mat_view_ordering",&flg);
257:   if (flg) {
258:     Mat tmat;
259:     MatPermute(mat,*rperm,*cperm,&tmat);
260:     MatViewFromOptions(tmat,(PetscObject)mat,"-mat_view_ordering");
261:     MatDestroy(&tmat);
262:   }
263:   return(0);
264: }

266: PetscErrorCode MatGetOrderingList(PetscFunctionList *list)
267: {
269:   *list = MatOrderingList;
270:   return(0);
271: }