Actual source code: compressedrow.c

petsc-3.4.5 2014-06-29
  2: #include <petsc-private/matimpl.h>  /*I   "petscmat.h"  I*/

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

 12:    Collective

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

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

 24:    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
 25:                    the values are not therefore obtained by directly taking the values from the matrix object.
 26:                    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
 27:                    by some preconditioners and hence is labeled as PETSC_EXTERN 

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

 37:   if (!compressedrow->check) return(0);

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

 42:   compressedrow->i      = NULL;
 43:   compressedrow->rindex = NULL;


 46:   /* compute number of zero rows */
 47:   nrows = 0;
 48:   for (i=0; i<mbs; i++) {        /* for each row */
 49:     nz = ai[i+1] - ai[i];       /* number of nonzeros */
 50:     if (nz == 0) nrows++;
 51:   }

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

 57:     PetscInfo3(A,"Found the ratio (num_zerorows %d)/(num_localrows %d) < %G. Do not use CompressedRow routines.\n",nrows,mbs,ratio);
 58:   } else {
 59:     compressedrow->use = PETSC_TRUE;

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

 63:     /* set compressed row format */
 64:     nrows  = mbs - nrows; /* num of non-zero rows */
 65:     PetscMalloc2(nrows+1,PetscInt,&cpi,nrows,PetscInt,&ridx);
 66:     row    = 0;
 67:     cpi[0] = 0;
 68:     for (i=0; i<mbs; i++) {
 69:       nz = ai[i+1] - ai[i];
 70:       if (nz == 0) continue;
 71:       cpi[row+1]  = ai[i+1];    /* compressed row pointer */
 72:       ridx[row++] = i;          /* compressed row local index */
 73:     }
 74:     compressedrow->nrows  = nrows;
 75:     compressedrow->i      = cpi;
 76:     compressedrow->rindex = ridx;
 77:   }
 78:   return(0);
 79: }