Actual source code: compressedrow.c

  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: {
 28:   PetscInt *cpi = NULL, *ridx = NULL, nz, i, row;

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

 33:   /* compute number of zero rows */
 34:   nrows = mbs - nrows;

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

 40:     PetscInfo(A, "Found the ratio (num_zerorows %" PetscInt_FMT ")/(num_localrows %" PetscInt_FMT ") < %g. Do not use CompressedRow routines.\n", nrows, mbs, (double)ratio);
 41:   } else {
 42:     compressedrow->use = PETSC_TRUE;

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

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