Actual source code: compressedrow.c

petsc-3.12.5 2020-03-29
Report Typos and Errors
  1:  #include <petsc/private/matimpl.h>

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

  9:    Collective

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

 19:    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
 20:                    the values are not therefore obtained by directly taking the values from the matrix object.
 21:                    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
 22:                    by some preconditioners and hence is labeled as PETSC_EXTERN 

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

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

 35:   compressedrow->i      = NULL;
 36:   compressedrow->rindex = NULL;


 39:   /* compute number of zero rows */
 40:   nrows = mbs - nrows;

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

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

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

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