xref: /petsc/src/mat/utils/compressedrow.c (revision 7b2bb3b9377a3c586e39c07aea434072d6d6ba33)
1 
2 #include "src/mat/matimpl.h"  /*I   "petscmat.h"  I*/
3 
4 #undef __FUNCT__
5 #define __FUNCT__ "Mat_CheckCompressedRow"
6 /*@C
7    Mat_CheckCompressedRow - Determines whether the compressed row matrix format should be used. If
8                             the format is to be used, this routine creates Mat_CompressedRow struct.
9 
10                             Compressed row format provides high performance routines by
11                             taking advantage of zero rows.
12 
13    Collective
14 
15    Input Parameters:
16 +  A             - the matrix
17 .  compressedrow - pointer to the struct Mat_CompressedRow
18 .  ai            - row pointer used by seqaij and seqbaij
19 -  ratio         - ratio of (num of zero rows)/m, used to determine if the compressed row format should be used
20 
21    Level: developer
22 @*/
23 PetscErrorCode Mat_CheckCompressedRow(Mat A,Mat_CompressedRow *compressedrow,PetscInt *ai,PetscReal ratio)
24 {
25   PetscErrorCode ierr;
26   PetscInt       nrows,*cpi=PETSC_NULL,*ridx=PETSC_NULL,nz,i,row,m=A->m/A->bs;
27 
28   PetscFunctionBegin;
29   compressedrow->checked = PETSC_TRUE;
30 
31   /* compute number of zero rows */
32   nrows = 0;
33   for (i=0; i<m; i++){                /* for each row */
34     nz = ai[i+1] - ai[i];       /* number of nonzeros */
35     if (nz == 0) nrows++;
36   }
37   /* if enough zero rows are found, use compressedrow data structure */
38   if (nrows < ratio*m) {
39     compressedrow->use = PETSC_FALSE;
40     PetscLogInfo(A,"Mat_CheckCompressedRow: Found the ratio (num_zerorows %d)/(num_localrows %d) < %g. Do not use CompressedRow routines.\n",nrows,m,ratio);
41   } else {
42     compressedrow->use = PETSC_TRUE;
43     PetscLogInfo(A,"Mat_CheckCompressedRow: Found the ratio (num_zerorows %d)/(num_localrows %d) > %g. Use CompressedRow routines.\n",nrows,m,ratio);
44 
45     /* set compressed row format */
46     nrows = m - nrows; /* num of non-zero rows */
47     ierr = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&cpi);CHKERRQ(ierr);
48     ridx = cpi + nrows + 1;
49     row    = 0;
50     cpi[0] = 0;
51     for (i=0; i<m; 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   PetscFunctionReturn(0);
62 }
63