EXTERN_C_END EXTERN_C_BEGIN /* ----------------------------------------------------------------------------*/ /* MatGetColoring_LF_Minpack - */ #undef __FUNCT__ #define __FUNCT__ "MatGetColoring_LF_Minpack" PetscErrorCode MatGetColoring_LF_Minpack(Mat mat,MatColoringType name,ISColoring *iscoloring) { PetscErrorCode ierr; PetscInt *list,*work,*seq,*coloring,n; const PetscInt *ria,*rja,*cia,*cja; PetscInt n1, none,ncolors,i; PetscBool done; Mat mat_seq = mat; PetscMPIInt size; MPI_Comm comm; ISColoring iscoloring_seq; PetscInt bs = 1,rstart,rend,N_loc,nc; ISColoringValue *colors_loc; PetscBool flg1,flg2; PetscFunctionBegin; /* this is ugly way to get blocksize but cannot call MatGetBlockSize() because AIJ can have bs > 1 */ ierr = PetscObjectTypeCompare((PetscObject)mat,MATSEQBAIJ,&flg1);CHKERRQ(ierr); ierr = PetscObjectTypeCompare((PetscObject)mat,MATMPIBAIJ,&flg2);CHKERRQ(ierr); if (flg1 || flg2) { ierr = MatGetBlockSize(mat,&bs);CHKERRQ(ierr); } ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); if (size > 1){ /* create a sequential iscoloring on all processors */ ierr = MatGetSeqNonzeroStructure(mat,&mat_seq);CHKERRQ(ierr); } ierr = MatGetRowIJ(mat_seq,1,PETSC_FALSE,PETSC_TRUE,&n,&ria,&rja,&done);CHKERRQ(ierr); ierr = MatGetColumnIJ(mat_seq,1,PETSC_FALSE,PETSC_TRUE,&n,&cia,&cja,&done);CHKERRQ(ierr); if (!done) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Ordering requires IJ"); ierr = MatFDColoringDegreeSequence_Minpack(n,cja,cia,rja,ria,&seq);CHKERRQ(ierr); ierr = PetscMalloc2(n,PetscInt,&list,4*n,PetscInt,&work);CHKERRQ(ierr); n1 = n - 1; none = -1; MINPACKnumsrt(&n,&n1,seq,&none,list,work+2*n,work+n); ierr = PetscMalloc(n*sizeof(PetscInt),&coloring);CHKERRQ(ierr); MINPACKseq(&n,cja,cia,rja,ria,list,coloring,&ncolors,work); ierr = PetscFree2(list,work);CHKERRQ(ierr); ierr = PetscFree(seq);CHKERRQ(ierr); ierr = MatRestoreRowIJ(mat_seq,1,PETSC_FALSE,PETSC_TRUE,&n,&ria,&rja,&done);CHKERRQ(ierr); ierr = MatRestoreColumnIJ(mat_seq,1,PETSC_FALSE,PETSC_TRUE,&n,&cia,&cja,&done);CHKERRQ(ierr); /* shift coloring numbers to start at zero and shorten */ if (ncolors > IS_COLORING_MAX-1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Maximum color size exceeded"); { ISColoringValue *s = (ISColoringValue*) coloring; for (i=0; i<n; i++) { s[i] = (ISColoringValue) (coloring[i]-1); } ierr = MatColoringPatch(mat_seq,ncolors,n,s,iscoloring);CHKERRQ(ierr); } if (size > 1) { ierr = MatDestroySeqNonzeroStructure(&mat_seq);CHKERRQ(ierr); /* convert iscoloring_seq to a parallel iscoloring */ iscoloring_seq = *iscoloring; rstart = mat->rmap->rstart/bs; rend = mat->rmap->rend/bs; N_loc = rend - rstart; /* number of local nodes */ /* get local colors for each local node */ ierr = PetscMalloc((N_loc+1)*sizeof(ISColoringValue),&colors_loc);CHKERRQ(ierr); for (i=rstart; i<rend; i++){ colors_loc[i-rstart] = iscoloring_seq->colors[i]; } /* create a parallel iscoloring */ nc=iscoloring_seq->n; ierr = ISColoringCreate(comm,nc,N_loc,colors_loc,iscoloring);CHKERRQ(ierr); ierr = ISColoringDestroy(&iscoloring_seq);CHKERRQ(ierr); } PetscFunctionReturn(0); }
PetscErrorCode MINPACKido(PetscInt *m,PetscInt * n,const PetscInt * indrow,const PetscInt * jpntr,const PetscInt * indcol,const PetscInt * ipntr,PetscInt * ndeg, PetscInt *list,PetscInt *maxclq, PetscInt *iwa1, PetscInt *iwa2, PetscInt *iwa3, PetscInt *iwa4) { /* System generated locals */ PetscInt i__1, i__2, i__3, i__4; /* Local variables */ PetscInt jcol = 0, ncomp = 0, ic, ip, jp, ir, maxinc, numinc, numord, maxlst, numwgt, numlst; /* Given the sparsity pattern of an m by n matrix A, this */ /* subroutine determines an incidence-degree ordering of the */ /* columns of A. */ /* The incidence-degree ordering is defined for the loopless */ /* graph G with vertices a(j), j = 1,2,...,n where a(j) is the */ /* j-th column of A and with edge (a(i),a(j)) if and only if */ /* columns i and j have a non-zero in the same row position. */ /* The incidence-degree ordering is determined recursively by */ /* letting list(k), k = 1,...,n be a column with maximal */ /* incidence to the subgraph spanned by the ordered columns. */ /* Among all the columns of maximal incidence, ido chooses a */ /* column of maximal degree. */ /* The subroutine statement is */ /* subroutine ido(m,n,indrow,jpntr,indcol,ipntr,ndeg,list, */ /* maxclq,iwa1,iwa2,iwa3,iwa4) */ /* where */ /* m is a positive integer input variable set to the number */ /* of rows of A. */ /* n is a positive integer input variable set to the number */ /* of columns of A. */ /* indrow is an integer input array which contains the row */ /* indices for the non-zeroes in the matrix A. */ /* jpntr is an integer input array of length n + 1 which */ /* specifies the locations of the row indices in indrow. */ /* The row indices for column j are */ /* indrow(k), k = jpntr(j),...,jpntr(j+1)-1. */ /* Note that jpntr(n+1)-1 is then the number of non-zero */ /* elements of the matrix A. */ /* indcol is an integer input array which contains the */ /* column indices for the non-zeroes in the matrix A. */ /* ipntr is an integer input array of length m + 1 which */ /* specifies the locations of the column indices in indcol. */ /* The column indices for row i are */ /* indcol(k), k = ipntr(i),...,ipntr(i+1)-1. */ /* Note that ipntr(m+1)-1 is then the number of non-zero */ /* elements of the matrix A. */ /* ndeg is an integer input array of length n which specifies */ /* the degree sequence. The degree of the j-th column */ /* of A is ndeg(j). */ /* list is an integer output array of length n which specifies */ /* the incidence-degree ordering of the columns of A. The j-th */ /* column in this order is list(j). */ /* maxclq is an integer output variable set to the size */ /* of the largest clique found during the ordering. */ /* iwa1,iwa2,iwa3, and iwa4 are integer work arrays of length n. */ /* Subprograms called */ /* MINPACK-supplied ... numsrt */ /* FORTRAN-supplied ... max */ /* Argonne National Laboratory. MINPACK Project. August 1984. */ /* Thomas F. Coleman, Burton S. Garbow, Jorge J. More' */ /* Sort the degree sequence. */ PetscFunctionBegin; /* Parameter adjustments */ --iwa4; --iwa3; --iwa2; --list; --ndeg; --ipntr; --indcol; --jpntr; --indrow; /* Function Body */ i__1 = *n - 1; MINPACKnumsrt(n, &i__1, &ndeg[1], &c_n1, &iwa4[1], &iwa2[1], &iwa3[1]); /* Initialization block. */ /* Create a doubly-linked list to access the incidences of the */ /* columns. The pointers for the linked list are as follows. */ /* Each un-ordered column ic is in a list (the incidence list) */ /* of columns with the same incidence. */ /* iwa1(numinc) is the first column in the numinc list */ /* unless iwa1(numinc) = 0. In this case there are */ /* no columns in the numinc list. */ /* iwa2(ic) is the column before ic in the incidence list */ /* unless iwa2(ic) = 0. In this case ic is the first */ /* column in this incidence list. */ /* iwa3(ic) is the column after ic in the incidence list */ /* unless iwa3(ic) = 0. In this case ic is the last */ /* column in this incidence list. */ /* If ic is an un-ordered column, then list(ic) is the */ /* incidence of ic to the graph induced by the ordered */ /* columns. If jcol is an ordered column, then list(jcol) */ /* is the incidence-degree order of column jcol. */ maxinc = 0; for (jp = *n; jp >= 1; --jp) { ic = iwa4[jp]; iwa1[*n - jp] = 0; iwa2[ic] = 0; iwa3[ic] = iwa1[0]; if (iwa1[0] > 0) iwa2[iwa1[0]] = ic; iwa1[0] = ic; iwa4[jp] = 0; list[jp] = 0; } /* Determine the maximal search length for the list */ /* of columns of maximal incidence. */ maxlst = 0; i__1 = *m; for (ir = 1; ir <= i__1; ++ir) { /* Computing 2nd power */ i__2 = ipntr[ir + 1] - ipntr[ir]; maxlst += i__2 * i__2; } maxlst /= *n; *maxclq = 0; numord = 1; /* Beginning of iteration loop. */ L30: /* Choose a column jcol of maximal degree among the */ /* columns of maximal incidence maxinc. */ L40: jp = iwa1[maxinc]; if (jp > 0) goto L50; --maxinc; goto L40; L50: numwgt = -1; i__1 = maxlst; for (numlst = 1; numlst <= i__1; ++numlst) { if (ndeg[jp] > numwgt) { numwgt = ndeg[jp]; jcol = jp; } jp = iwa3[jp]; if (jp <= 0) goto L70; } L70: list[jcol] = numord; /* Update the size of the largest clique */ /* found during the ordering. */ if (!maxinc) ncomp = 0; ++ncomp; if (maxinc + 1 == ncomp) *maxclq = PetscMax(*maxclq,ncomp); /* Termination test. */ ++numord; if (numord > *n) goto L100; /* Delete column jcol from the maxinc list. */ if (!iwa2[jcol]) iwa1[maxinc] = iwa3[jcol]; else iwa3[iwa2[jcol]] = iwa3[jcol]; if (iwa3[jcol] > 0) iwa2[iwa3[jcol]] = iwa2[jcol]; /* Find all columns adjacent to column jcol. */ iwa4[jcol] = *n; /* Determine all positions (ir,jcol) which correspond */ /* to non-zeroes in the matrix. */ i__1 = jpntr[jcol + 1] - 1; for (jp = jpntr[jcol]; jp <= i__1; ++jp) { ir = indrow[jp]; /* For each row ir, determine all positions (ir,ic) */ /* which correspond to non-zeroes in the matrix. */ i__2 = ipntr[ir + 1] - 1; for (ip = ipntr[ir]; ip <= i__2; ++ip) { ic = indcol[ip]; /* Array iwa4 marks columns which are adjacent to */ /* column jcol. */ if (iwa4[ic] < numord) { iwa4[ic] = numord; /* Update the pointers to the current incidence lists. */ numinc = list[ic]; ++list[ic]; /* Computing MAX */ i__3 = maxinc, i__4 = list[ic]; maxinc = PetscMax(i__3,i__4); /* Delete column ic from the numinc list. */ if (!iwa2[ic]) iwa1[numinc] = iwa3[ic]; else iwa3[iwa2[ic]] = iwa3[ic]; if (iwa3[ic] > 0) iwa2[iwa3[ic]] = iwa2[ic]; /* Add column ic to the numinc+1 list. */ iwa2[ic] = 0; iwa3[ic] = iwa1[numinc + 1]; if (iwa1[numinc + 1] > 0) iwa2[iwa1[numinc + 1]] = ic; iwa1[numinc + 1] = ic; } } } /* End of iteration loop. */ goto L30; L100: /* Invert the array list. */ i__1 = *n; for (jcol = 1; jcol <= i__1; ++jcol) iwa2[list[jcol]] = jcol; i__1 = *n; for (jp = 1; jp <= i__1; ++jp) list[jp] = iwa2[jp]; PetscFunctionReturn(0); }
PetscErrorCode MINPACKdsm(PetscInt *m,PetscInt *n,PetscInt *npairs,PetscInt *indrow,PetscInt *indcol,PetscInt *ngrp,PetscInt *maxgrp, PetscInt *mingrp,PetscInt *info,PetscInt *ipntr,PetscInt *jpntr,PetscInt *iwa,PetscInt *liwa) { /* System generated locals */ PetscInt i__1,i__2,i__3; /* Local variables */ PetscInt i,j,maxclq,numgrp; /* Given the sparsity pattern of an m by n matrix A, this */ /* subroutine determines a partition of the columns of A */ /* consistent with the direct determination of A. */ /* The sparsity pattern of the matrix A is specified by */ /* the arrays indrow and indcol. On input the indices */ /* for the non-zero elements of A are */ /* indrow(k),indcol(k), k = 1,2,...,npairs. */ /* The (indrow,indcol) pairs may be specified in any order. */ /* Duplicate input pairs are permitted, but the subroutine */ /* eliminates them. */ /* The subroutine partitions the columns of A into groups */ /* such that columns in the same group do not have a */ /* non-zero in the same row position. A partition of the */ /* columns of A with this property is consistent with the */ /* direct determination of A. */ /* The subroutine statement is */ /* subroutine dsm(m,n,npairs,indrow,indcol,ngrp,maxgrp,mingrp, */ /* info,ipntr,jpntr,iwa,liwa) */ /* where */ /* m is a positive integer input variable set to the number */ /* of rows of A. */ /* n is a positive integer input variable set to the number */ /* of columns of A. */ /* npairs is a positive integer input variable set to the */ /* number of (indrow,indcol) pairs used to describe the */ /* sparsity pattern of A. */ /* indrow is an integer array of length npairs. On input indrow */ /* must contain the row indices of the non-zero elements of A. */ /* On output indrow is permuted so that the corresponding */ /* column indices are in non-decreasing order. The column */ /* indices can be recovered from the array jpntr. */ /* indcol is an integer array of length npairs. On input indcol */ /* must contain the column indices of the non-zero elements of */ /* A. On output indcol is permuted so that the corresponding */ /* row indices are in non-decreasing order. The row indices */ /* can be recovered from the array ipntr. */ /* ngrp is an integer output array of length n which specifies */ /* the partition of the columns of A. Column jcol belongs */ /* to group ngrp(jcol). */ /* maxgrp is an integer output variable which specifies the */ /* number of groups in the partition of the columns of A. */ /* mingrp is an integer output variable which specifies a lower */ /* bound for the number of groups in any consistent partition */ /* of the columns of A. */ /* info is an integer output variable set as follows. For */ /* normal termination info = 1. If m, n, or npairs is not */ /* positive or liwa is less than max(m,6*n), then info = 0. */ /* If the k-th element of indrow is not an integer between */ /* 1 and m or the k-th element of indcol is not an integer */ /* between 1 and n, then info = -k. */ /* ipntr is an integer output array of length m + 1 which */ /* specifies the locations of the column indices in indcol. */ /* The column indices for row i are */ /* indcol(k), k = ipntr(i),...,ipntr(i+1)-1. */ /* Note that ipntr(m+1)-1 is then the number of non-zero */ /* elements of the matrix A. */ /* jpntr is an integer output array of length n + 1 which */ /* specifies the locations of the row indices in indrow. */ /* The row indices for column j are */ /* indrow(k), k = jpntr(j),...,jpntr(j+1)-1. */ /* Note that jpntr(n+1)-1 is then the number of non-zero */ /* elements of the matrix A. */ /* iwa is an integer work array of length liwa. */ /* liwa is a positive integer input variable not less than */ /* max(m,6*n). */ /* Subprograms called */ /* MINPACK-supplied ... degr,ido,numsrt,seq,setr,slo,srtdat */ /* FORTRAN-supplied ... max */ /* Argonne National Laboratory. MINPACK Project. December 1984. */ /* Thomas F. Coleman, Burton S. Garbow, Jorge J. More' */ PetscFunctionBegin; /* Parameter adjustments */ --iwa; --jpntr; --ipntr; --ngrp; --indcol; --indrow; *info = 0; /* Determine a lower bound for the number of groups. */ *mingrp = 0; i__1 = *m; for (i = 1; i <= i__1; ++i) { /* Computing MAX */ i__2 = *mingrp,i__3 = ipntr[i + 1] - ipntr[i]; *mingrp = PetscMax(i__2,i__3); } /* Determine the degree sequence for the intersection */ /* graph of the columns of A. */ MINPACKdegr(n,&indrow[1],&jpntr[1],&indcol[1],&ipntr[1],&iwa[*n * 5 + 1],& iwa[*n + 1]); /* Color the intersection graph of the columns of A */ /* with the smallest-last (SL) ordering. */ MINPACKslo(n,&indrow[1],&jpntr[1],&indcol[1],&ipntr[1],&iwa[*n * 5 + 1],& iwa[(*n << 2) + 1],&maxclq,&iwa[1],&iwa[*n + 1],&iwa[(*n << 1) + 1],&iwa[*n * 3 + 1]); MINPACKseq(n,&indrow[1],&jpntr[1],&indcol[1],&ipntr[1],&iwa[(*n << 2) + 1], &ngrp[1],maxgrp,&iwa[*n + 1]); *mingrp = PetscMax(*mingrp,maxclq); /* Exit if the smallest-last ordering is optimal. */ if (*maxgrp == *mingrp) { PetscFunctionReturn(0); } /* Color the intersection graph of the columns of A */ /* with the incidence-degree (ID) ordering. */ MINPACKido(m,n,&indrow[1],&jpntr[1],&indcol[1],&ipntr[1],&iwa[*n * 5 + 1], &iwa[(*n << 2) + 1],&maxclq,&iwa[1],&iwa[*n + 1],&iwa[(*n << 1) + 1],&iwa[*n * 3 + 1]); MINPACKseq(n,&indrow[1],&jpntr[1],&indcol[1],&ipntr[1],&iwa[(*n << 2) + 1], &iwa[1],&numgrp,&iwa[*n + 1]); *mingrp = PetscMax(*mingrp,maxclq); /* Retain the better of the two orderings so far. */ if (numgrp < *maxgrp) { *maxgrp = numgrp; i__1 = *n; for (j = 1; j <= i__1; ++j) { ngrp[j] = iwa[j]; } /* Exit if the incidence-degree ordering is optimal. */ if (*maxgrp == *mingrp) { PetscFunctionReturn(0); } } /* Color the intersection graph of the columns of A */ /* with the largest-first (LF) ordering. */ i__1 = *n - 1; MINPACKnumsrt(n,&i__1,&iwa[*n * 5 + 1],&c_n1,&iwa[(*n << 2) + 1],&iwa[(*n << 1) + 1],&iwa[*n + 1]); MINPACKseq(n,&indrow[1],&jpntr[1],&indcol[1],&ipntr[1],&iwa[(*n << 2) + 1], &iwa[1],&numgrp,&iwa[*n + 1]); /* Retain the best of the three orderings and exit. */ if (numgrp < *maxgrp) { *maxgrp = numgrp; i__1 = *n; for (j = 1; j <= i__1; ++j) { ngrp[j] = iwa[j]; } } PetscFunctionReturn(0); }