xref: /petsc/src/mat/utils/compressedrow.c (revision feff33ee0b5b037fa8f9f294dede656a2f85cc47)
1 
2 #include <petsc/private/matimpl.h>  /*I   "petscmat.h"  I*/
3 
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.
9 
10    Collective
11 
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
19 
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.
22 
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
27 
28    Level: developer
29 @*/
30 PETSC_EXTERN PetscErrorCode MatCheckCompressedRow(Mat A,PetscInt nrows,Mat_CompressedRow *compressedrow,PetscInt *ai,PetscInt mbs,PetscReal ratio)
31 {
32   PetscErrorCode ierr;
33   PetscInt       *cpi=NULL,*ridx=NULL,nz,i,row;
34 
35   PetscFunctionBegin;
36   /* in case this is being reused, delete old space */
37   ierr = PetscFree2(compressedrow->i,compressedrow->rindex);CHKERRQ(ierr);
38 
39   compressedrow->i      = NULL;
40   compressedrow->rindex = NULL;
41 
42 
43   /* compute number of zero rows */
44   nrows = mbs - nrows;
45 
46   /* if a large number of zero rows is found, use compressedrow data structure */
47   if (nrows < ratio*mbs) {
48     compressedrow->use = PETSC_FALSE;
49 
50     ierr = PetscInfo3(A,"Found the ratio (num_zerorows %d)/(num_localrows %d) < %g. Do not use CompressedRow routines.\n",nrows,mbs,(double)ratio);CHKERRQ(ierr);
51   } else {
52     compressedrow->use = PETSC_TRUE;
53 
54     ierr = PetscInfo3(A,"Found the ratio (num_zerorows %d)/(num_localrows %d) > %g. Use CompressedRow routines.\n",nrows,mbs,(double)ratio);CHKERRQ(ierr);
55 
56     /* set compressed row format */
57     nrows  = mbs - nrows; /* num of non-zero rows */
58     ierr   = PetscMalloc2(nrows+1,&cpi,nrows,&ridx);CHKERRQ(ierr);
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   PetscFunctionReturn(0);
72 }
73