1 2 #include <petsc-private/matimpl.h> /*I "petscmat.h" I*/ 3 4 #undef __FUNCT__ 5 #define __FUNCT__ "MatCheckCompressedRow" 6 /*@C 7 MatCheckCompressedRow - 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 Notes: By default PETSc will not check for compressed rows on sequential matrices. Call MatSetOption(Mat,MAT_CHECK_COMPRESSED_ROW,PETSC_TRUE); before 22 MatAssemblyBegin() to have it check. 23 24 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 25 the values are not therefore obtained by directly taking the values from the matrix object. 26 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 27 by some preconditioners and hence is labeled as PETSC_EXTERN 28 29 Level: developer 30 @*/ 31 PETSC_EXTERN PetscErrorCode MatCheckCompressedRow(Mat A,Mat_CompressedRow *compressedrow,PetscInt *ai,PetscInt mbs,PetscReal ratio) 32 { 33 PetscErrorCode ierr; 34 PetscInt nrows,*cpi=NULL,*ridx=NULL,nz,i,row; 35 36 PetscFunctionBegin; 37 if (!compressedrow->check) PetscFunctionReturn(0); 38 39 /* in case this is being reused, delete old space */ 40 ierr = PetscFree2(compressedrow->i,compressedrow->rindex);CHKERRQ(ierr); 41 42 compressedrow->i = NULL; 43 compressedrow->rindex = NULL; 44 45 46 /* compute number of zero rows */ 47 nrows = 0; 48 for (i=0; i<mbs; i++) { /* for each row */ 49 nz = ai[i+1] - ai[i]; /* number of nonzeros */ 50 if (nz == 0) nrows++; 51 } 52 53 /* if a large number of zero rows is found, use compressedrow data structure */ 54 if (nrows < ratio*mbs) { 55 compressedrow->use = PETSC_FALSE; 56 57 ierr = PetscInfo3(A,"Found the ratio (num_zerorows %d)/(num_localrows %d) < %G. Do not use CompressedRow routines.\n",nrows,mbs,ratio);CHKERRQ(ierr); 58 } else { 59 compressedrow->use = PETSC_TRUE; 60 61 ierr = PetscInfo3(A,"Found the ratio (num_zerorows %d)/(num_localrows %d) > %G. Use CompressedRow routines.\n",nrows,mbs,ratio);CHKERRQ(ierr); 62 63 /* set compressed row format */ 64 nrows = mbs - nrows; /* num of non-zero rows */ 65 ierr = PetscMalloc2(nrows+1,PetscInt,&cpi,nrows,PetscInt,&ridx);CHKERRQ(ierr); 66 row = 0; 67 cpi[0] = 0; 68 for (i=0; i<mbs; i++) { 69 nz = ai[i+1] - ai[i]; 70 if (nz == 0) continue; 71 cpi[row+1] = ai[i+1]; /* compressed row pointer */ 72 ridx[row++] = i; /* compressed row local index */ 73 } 74 compressedrow->nrows = nrows; 75 compressedrow->i = cpi; 76 compressedrow->rindex = ridx; 77 } 78 PetscFunctionReturn(0); 79 } 80