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