Actual source code: compressedrow.c

petsc-3.8.4 2018-03-24
Report Typos and Errors

  2:  #include <petsc/private/matimpl.h>

  4: /*@C
  5:    MatCheckCompressedRow - Determines whether the compressed row matrix format should be used.
  6:       If the format is to be used, this routine creates Mat_CompressedRow struct.
  7:       Compressed row format provides high performance routines by taking advantage of zero rows.
  8:       Supported types are MATAIJ, MATBAIJ and MATSBAIJ.

 10:    Collective

 12:    Input Parameters:
 13: +  A             - the matrix
 14: .  nrows         - number of rows with nonzero entries
 15: .  compressedrow - pointer to the struct Mat_CompressedRow
 16: .  ai            - row pointer used by seqaij and seqbaij
 17: .  mbs           - number of (block) rows represented by ai
 18: -  ratio         - ratio of (num of zero rows)/m, used to determine if the compressed row format should be used

 20:    Notes: By default PETSc will not check for compressed rows on sequential matrices. Call MatSetOption(Mat,MAT_CHECK_COMPRESSED_ROW,PETSC_TRUE); before
 21:           MatAssemblyBegin() to have it check.

 23:    Developer Note: The reason this takes the compressedrow, ai and mbs arguments is because it is called by both the SeqAIJ and SEQBAIJ matrices and
 24:                    the values are not therefore obtained by directly taking the values from the matrix object.
 25:                    This is not a general public routine and hence is not listed in petscmat.h (it exposes a private data structure) but it is used
 26:                    by some preconditioners and hence is labeled as PETSC_EXTERN 

 28:    Level: developer
 29: @*/
 30: PETSC_EXTERN PetscErrorCode MatCheckCompressedRow(Mat A,PetscInt nrows,Mat_CompressedRow *compressedrow,PetscInt *ai,PetscInt mbs,PetscReal ratio)
 31: {
 33:   PetscInt       *cpi=NULL,*ridx=NULL,nz,i,row;

 36:   /* in case this is being reused, delete old space */
 37:   PetscFree2(compressedrow->i,compressedrow->rindex);

 39:   compressedrow->i      = NULL;
 40:   compressedrow->rindex = NULL;


 43:   /* compute number of zero rows */
 44:   nrows = mbs - nrows;

 46:   /* if a large number of zero rows is found, use compressedrow data structure */
 47:   if (nrows < ratio*mbs) {
 48:     compressedrow->use = PETSC_FALSE;

 50:     PetscInfo3(A,"Found the ratio (num_zerorows %d)/(num_localrows %d) < %g. Do not use CompressedRow routines.\n",nrows,mbs,(double)ratio);
 51:   } else {
 52:     compressedrow->use = PETSC_TRUE;

 54:     PetscInfo3(A,"Found the ratio (num_zerorows %d)/(num_localrows %d) > %g. Use CompressedRow routines.\n",nrows,mbs,(double)ratio);

 56:     /* set compressed row format */
 57:     nrows  = mbs - nrows; /* num of non-zero rows */
 58:     PetscMalloc2(nrows+1,&cpi,nrows,&ridx);
 59:     row    = 0;
 60:     cpi[0] = 0;
 61:     for (i=0; i<mbs; i++) {
 62:       nz = ai[i+1] - ai[i];
 63:       if (nz == 0) continue;
 64:       cpi[row+1]  = ai[i+1];    /* compressed row pointer */
 65:       ridx[row++] = i;          /* compressed row local index */
 66:     }
 67:     compressedrow->nrows  = nrows;
 68:     compressedrow->i      = cpi;
 69:     compressedrow->rindex = ridx;
 70:   }
 71:   return(0);
 72: }