xref: /petsc/src/mat/utils/compressedrow.c (revision ab90151441bebc4d0600d71c4d1b89a49c47f278)
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   if (!compressedrow->use) PetscFunctionReturn(0);
30   if (compressedrow->checked && !A->same_nonzero){
31     ierr = PetscFree(compressedrow->i);CHKERRQ(ierr);
32     compressedrow->rindex = PETSC_NULL;
33     PetscLogInfo(A,"Mat_CheckCompressedRow: Mat structure might be changed. Free memory and recheck.\n");
34   }
35   compressedrow->checked = PETSC_TRUE;
36 
37   /* compute number of zero rows */
38   nrows = 0;
39   for (i=0; i<m; i++){                /* for each row */
40     nz = ai[i+1] - ai[i];       /* number of nonzeros */
41     if (nz == 0) nrows++;
42   }
43   /* if enough zero rows are found, use compressedrow data structure */
44   if (nrows < ratio*m) {
45     compressedrow->use = PETSC_FALSE;
46     PetscLogInfo(A,"Mat_CheckCompressedRow: Found the ratio (num_zerorows %d)/(num_localrows %d) < %g. Do not use CompressedRow routines.\n",nrows,m,ratio);
47   } else {
48     compressedrow->use = PETSC_TRUE;
49     PetscLogInfo(A,"Mat_CheckCompressedRow: Found the ratio (num_zerorows %d)/(num_localrows %d) > %g. Use CompressedRow routines.\n",nrows,m,ratio);
50 
51     /* set compressed row format */
52     nrows = m - nrows; /* num of non-zero rows */
53     ierr = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&cpi);CHKERRQ(ierr);
54     ridx = cpi + nrows + 1;
55     row    = 0;
56     cpi[0] = 0;
57     for (i=0; i<m; i++){
58       nz = ai[i+1] - ai[i];
59       if (nz == 0) continue;
60       cpi[row+1]  = ai[i+1];    /* compressed row pointer */
61       ridx[row++] = i;          /* compressed row local index */
62     }
63     compressedrow->nrows  = nrows;
64     compressedrow->i      = cpi;
65     compressedrow->rindex = ridx;
66   }
67   PetscFunctionReturn(0);
68 }
69