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:
21: 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 PetscErrorCodeMatCheckCompressedRow(Mat A,PetscInt nrows,Mat_CompressedRow *compressedrow,PetscInt *ai,PetscInt mbs,PetscReal ratio) 32: {
34: PetscInt *cpi=NULL,*ridx=NULL,nz,i,row;
37: /* in case this is being reused, delete old space */
38: PetscFree2(compressedrow->i,compressedrow->rindex);
40: compressedrow->i = NULL;
41: compressedrow->rindex = NULL;
44: /* compute number of zero rows */
45: nrows = mbs - nrows;
47: /* if a large number of zero rows is found, use compressedrow data structure */
48: if (nrows < ratio*mbs) {
49: compressedrow->use = PETSC_FALSE;
51: PetscInfo3(A,"Found the ratio (num_zerorows %d)/(num_localrows %d) < %g. Do not use CompressedRow routines.\n",nrows,mbs,(double)ratio);
52: } else {
53: compressedrow->use = PETSC_TRUE;
55: PetscInfo3(A,"Found the ratio (num_zerorows %d)/(num_localrows %d) > %g. Use CompressedRow routines.\n",nrows,mbs,(double)ratio);
57: /* set compressed row format */
58: nrows = mbs - nrows; /* num of non-zero rows */
59: PetscMalloc2(nrows+1,&cpi,nrows,&ridx);
60: row = 0;
61: cpi[0] = 0;
62: for (i=0; i<mbs; i++) {
63: nz = ai[i+1] - ai[i];
64: if (nz == 0) continue;
65: cpi[row+1] = ai[i+1]; /* compressed row pointer */
66: ridx[row++] = i; /* compressed row local index */
67: }
68: compressedrow->nrows = nrows;
69: compressedrow->i = cpi;
70: compressedrow->rindex = ridx;
71: }
72: return(0);
73: }