xref: /petsc/src/mat/utils/compressedrow.c (revision af0996ce37bc06907c37d8d91773840993d61e62)
1 
2 #include <petsc/private/matimpl.h>  /*I   "petscmat.h"  I*/
3 
4 #undef __FUNCT__
5 #define __FUNCT__ "MatCheckCompressedRow"
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.
11 
12    Collective
13 
14    Input Parameters:
15 +  A             - the matrix
16 .  nrows         - number of rows with nonzero entries
17 .  compressedrow - pointer to the struct Mat_CompressedRow
18 .  ai            - row pointer used by seqaij and seqbaij
19 .  mbs           - number of (block) rows represented by ai
20 -  ratio         - ratio of (num of zero rows)/m, used to determine if the compressed row format should be used
21 
22    Notes: By default PETSc will not check for compressed rows on sequential matrices. Call MatSetOption(Mat,MAT_CHECK_COMPRESSED_ROW,PETSC_TRUE); before
23           MatAssemblyBegin() to have it check.
24 
25    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
26                    the values are not therefore obtained by directly taking the values from the matrix object.
27                    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
28                    by some preconditioners and hence is labeled as PETSC_EXTERN
29 
30    Level: developer
31 @*/
32 PETSC_EXTERN PetscErrorCode MatCheckCompressedRow(Mat A,PetscInt nrows,Mat_CompressedRow *compressedrow,PetscInt *ai,PetscInt mbs,PetscReal ratio)
33 {
34   PetscErrorCode ierr;
35   PetscInt       *cpi=NULL,*ridx=NULL,nz,i,row;
36 
37   PetscFunctionBegin;
38   /* in case this is being reused, delete old space */
39   ierr = PetscFree2(compressedrow->i,compressedrow->rindex);CHKERRQ(ierr);
40 
41   compressedrow->i      = NULL;
42   compressedrow->rindex = NULL;
43 
44 
45   /* compute number of zero rows */
46   nrows = mbs - nrows;
47 
48   /* if a large number of zero rows is found, use compressedrow data structure */
49   if (nrows < ratio*mbs) {
50     compressedrow->use = PETSC_FALSE;
51 
52     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);
53   } else {
54     compressedrow->use = PETSC_TRUE;
55 
56     ierr = PetscInfo3(A,"Found the ratio (num_zerorows %d)/(num_localrows %d) > %g. Use CompressedRow routines.\n",nrows,mbs,(double)ratio);CHKERRQ(ierr);
57 
58     /* set compressed row format */
59     nrows  = mbs - nrows; /* num of non-zero rows */
60     ierr   = PetscMalloc2(nrows+1,&cpi,nrows,&ridx);CHKERRQ(ierr);
61     row    = 0;
62     cpi[0] = 0;
63     for (i=0; i<mbs; i++) {
64       nz = ai[i+1] - ai[i];
65       if (nz == 0) continue;
66       cpi[row+1]  = ai[i+1];    /* compressed row pointer */
67       ridx[row++] = i;          /* compressed row local index */
68     }
69     compressedrow->nrows  = nrows;
70     compressedrow->i      = cpi;
71     compressedrow->rindex = ridx;
72   }
73   PetscFunctionReturn(0);
74 }
75