xref: /petsc/src/mat/utils/compressedrow.c (revision d71ae5a4db6382e7f06317b8d368875286fe9008)
1af0996ceSBarry Smith #include <petsc/private/matimpl.h> /*I   "petscmat.h"  I*/
226e093fcSHong Zhang 
373e7a558SHong Zhang /*@C
4cd6b891eSBarry Smith    MatCheckCompressedRow - Determines whether the compressed row matrix format should be used.
5f38b99b6SHong Zhang       If the format is to be used, this routine creates Mat_CompressedRow struct.
6f38b99b6SHong Zhang       Compressed row format provides high performance routines by taking advantage of zero rows.
7f38b99b6SHong Zhang       Supported types are MATAIJ, MATBAIJ and MATSBAIJ.
873e7a558SHong Zhang 
973e7a558SHong Zhang    Collective
1073e7a558SHong Zhang 
1173e7a558SHong Zhang    Input Parameters:
1273e7a558SHong Zhang +  A             - the matrix
1311e456e1SBarry Smith .  nrows         - number of rows with nonzero entries
1473e7a558SHong Zhang .  compressedrow - pointer to the struct Mat_CompressedRow
1573e7a558SHong Zhang .  ai            - row pointer used by seqaij and seqbaij
16317fbc4cSHong Zhang .  mbs           - number of (block) rows represented by ai
1773e7a558SHong Zhang -  ratio         - ratio of (num of zero rows)/m, used to determine if the compressed row format should be used
1873e7a558SHong Zhang 
19340b11eeSBarry Smith    Developer Note: The reason this takes the compressedrow, ai and mbs arguments is because it is called by both the SeqAIJ and SEQBAIJ matrices and
20340b11eeSBarry Smith                    the values are not therefore obtained by directly taking the values from the matrix object.
218aea9937SBarry Smith                    This is not a general public routine and hence is not listed in petscmat.h (it exposes a private data structure) but it is used
228aea9937SBarry Smith                    by some preconditioners and hence is labeled as PETSC_EXTERN
238aea9937SBarry Smith 
2473e7a558SHong Zhang    Level: developer
2573e7a558SHong Zhang @*/
26*d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MatCheckCompressedRow(Mat A, PetscInt nrows, Mat_CompressedRow *compressedrow, PetscInt *ai, PetscInt mbs, PetscReal ratio)
27*d71ae5a4SJacob Faibussowitsch {
2811e456e1SBarry Smith   PetscInt *cpi = NULL, *ridx = NULL, nz, i, row;
2973e7a558SHong Zhang 
3073e7a558SHong Zhang   PetscFunctionBegin;
31cd6b891eSBarry Smith   /* in case this is being reused, delete old space */
329566063dSJacob Faibussowitsch   PetscCall(PetscFree2(compressedrow->i, compressedrow->rindex));
338865f1eaSKarl Rupp 
3473e7a558SHong Zhang   /* compute number of zero rows */
3511e456e1SBarry Smith   nrows = mbs - nrows;
36baa4e9faSMark F. Adams 
37317fbc4cSHong Zhang   /* if a large number of zero rows is found, use compressedrow data structure */
38317fbc4cSHong Zhang   if (nrows < ratio * mbs) {
3973e7a558SHong Zhang     compressedrow->use = PETSC_FALSE;
408865f1eaSKarl Rupp 
4163a3b9bcSJacob Faibussowitsch     PetscCall(PetscInfo(A, "Found the ratio (num_zerorows %" PetscInt_FMT ")/(num_localrows %" PetscInt_FMT ") < %g. Do not use CompressedRow routines.\n", nrows, mbs, (double)ratio));
4273e7a558SHong Zhang   } else {
4373e7a558SHong Zhang     compressedrow->use = PETSC_TRUE;
448865f1eaSKarl Rupp 
4563a3b9bcSJacob Faibussowitsch     PetscCall(PetscInfo(A, "Found the ratio (num_zerorows %" PetscInt_FMT ")/(num_localrows %" PetscInt_FMT ") > %g. Use CompressedRow routines.\n", nrows, mbs, (double)ratio));
4673e7a558SHong Zhang 
4773e7a558SHong Zhang     /* set compressed row format */
48317fbc4cSHong Zhang     nrows = mbs - nrows; /* num of non-zero rows */
499566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(nrows + 1, &cpi, nrows, &ridx));
5073e7a558SHong Zhang     row    = 0;
5173e7a558SHong Zhang     cpi[0] = 0;
52317fbc4cSHong Zhang     for (i = 0; i < mbs; i++) {
5373e7a558SHong Zhang       nz = ai[i + 1] - ai[i];
5473e7a558SHong Zhang       if (nz == 0) continue;
5573e7a558SHong Zhang       cpi[row + 1] = ai[i + 1]; /* compressed row pointer */
567b2bb3b9SHong Zhang       ridx[row++]  = i;         /* compressed row local index */
5773e7a558SHong Zhang     }
5873e7a558SHong Zhang     compressedrow->nrows  = nrows;
5973e7a558SHong Zhang     compressedrow->i      = cpi;
607b2bb3b9SHong Zhang     compressedrow->rindex = ridx;
6173e7a558SHong Zhang   }
6273e7a558SHong Zhang   PetscFunctionReturn(0);
6373e7a558SHong Zhang }
64