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