xref: /petsc/src/mat/utils/compressedrow.c (revision 52e6d16ba989af9362d0fcebb12e73dd7c9666ed)
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.
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 .  compressedrow - pointer to the struct Mat_CompressedRow
17 .  ai            - row pointer used by seqaij and seqbaij
18 .  mbs           - number of (block) rows represented by ai
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,PetscInt mbs,PetscReal ratio)
24 {
25   PetscErrorCode ierr;
26   PetscInt       nrows,*cpi=PETSC_NULL,*ridx=PETSC_NULL,nz,i,row;
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, A->same_nonzero = PETSC_TRUE. Skip check */
39       PetscLogInfo(A,"Mat_CheckCompressedRow: Skip check. m: %d, n: %d,M: %d, N: %d,nrows: %d, ii: %p, type: %s\n",A->m,A->n,A->M,A->N,compressedrow->nrows,compressedrow->i,A->type_name);
40       PetscFunctionReturn(0);
41     }
42   }
43   compressedrow->checked = PETSC_TRUE;
44 
45   /* compute number of zero rows */
46   nrows = 0;
47   for (i=0; i<mbs; i++){        /* for each row */
48     nz = ai[i+1] - ai[i];       /* number of nonzeros */
49     if (nz == 0) nrows++;
50   }
51   /* if a large number of zero rows is found, use compressedrow data structure */
52   if (nrows < ratio*mbs) {
53     compressedrow->use = PETSC_FALSE;
54     PetscLogInfo(A,"Mat_CheckCompressedRow: Found the ratio (num_zerorows %d)/(num_localrows %d) < %g. Do not use CompressedRow routines.\n",nrows,mbs,ratio);
55   } else {
56     compressedrow->use = PETSC_TRUE;
57     PetscLogInfo(A,"Mat_CheckCompressedRow: Found the ratio (num_zerorows %d)/(num_localrows %d) > %g. Use CompressedRow routines.\n",nrows,mbs,ratio);
58 
59     /* set compressed row format */
60     nrows = mbs - nrows; /* num of non-zero rows */
61     ierr = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&cpi);CHKERRQ(ierr);
62     ridx = cpi + nrows + 1;
63     row    = 0;
64     cpi[0] = 0;
65     for (i=0; i<mbs; i++){
66       nz = ai[i+1] - ai[i];
67       if (nz == 0) continue;
68       cpi[row+1]  = ai[i+1];    /* compressed row pointer */
69       ridx[row++] = i;          /* compressed row local index */
70     }
71     compressedrow->nrows  = nrows;
72     compressedrow->i      = cpi;
73     compressedrow->rindex = ridx;
74   }
75 
76   PetscFunctionReturn(0);
77 }
78