Actual source code: compressedrow.c
1: #define PETSCMAT_DLL
3: #include private/matimpl.h
7: /*@C
8: Mat_CheckCompressedRow - Determines whether the compressed row matrix format should be used.
9: If the format is to be used, this routine creates Mat_CompressedRow struct.
10: Compressed row format provides high performance routines by taking advantage of zero rows.
11: Supported types are MATAIJ, MATBAIJ and MATSBAIJ.
13: Collective
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: . mbs - number of (block) rows represented by ai
20: - ratio - ratio of (num of zero rows)/m, used to determine if the compressed row format should be used
22: Level: developer
23: @*/
24: PetscErrorCode Mat_CheckCompressedRow(Mat A,Mat_CompressedRow *compressedrow,PetscInt *ai,PetscInt mbs,PetscReal ratio)
25: {
27: PetscInt nrows,*cpi=PETSC_NULL,*ridx=PETSC_NULL,nz,i,row;
30: if (!compressedrow->use) return(0);
31: if (compressedrow->checked){
32: if (!A->same_nonzero){
33: PetscFree2(compressedrow->i,compressedrow->rindex);
34: compressedrow->i = PETSC_NULL;
35: compressedrow->rindex = PETSC_NULL;
36: PetscInfo(A,"Mat structure might be changed. Free memory and recheck.\n");
37: } else if (!compressedrow->i) {
38: /* Don't know why this occures. For safe, recheck. */
39: PetscInfo(A,"compressedrow.checked, but compressedrow.i==null. Recheck.\n");
40: } else { /* use compressedrow, checked, A->same_nonzero = PETSC_TRUE. Skip check */
41: PetscInfo7(A,"Skip check. m: %d, n: %d,M: %d, N: %d,nrows: %d, ii: %p, type: %s\n",A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N,compressedrow->nrows,compressedrow->i,((PetscObject)A)->type_name);
42: return(0);
43: }
44: }
45: compressedrow->checked = PETSC_TRUE;
47: /* compute number of zero rows */
48: nrows = 0;
49: for (i=0; i<mbs; i++){ /* for each row */
50: nz = ai[i+1] - ai[i]; /* number of nonzeros */
51: if (nz == 0) nrows++;
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: PetscInfo3(A,"Found the ratio (num_zerorows %d)/(num_localrows %d) < %G. Do not use CompressedRow routines.\n",nrows,mbs,ratio);
57: } else {
58: compressedrow->use = PETSC_TRUE;
59: PetscInfo3(A,"Found the ratio (num_zerorows %d)/(num_localrows %d) > %G. Use CompressedRow routines.\n",nrows,mbs,ratio);
61: /* set compressed row format */
62: nrows = mbs - nrows; /* num of non-zero rows */
63: PetscMalloc2(nrows+1,PetscInt,&cpi,nrows,PetscInt,&ridx);
64: row = 0;
65: cpi[0] = 0;
66: for (i=0; i<mbs; i++){
67: nz = ai[i+1] - ai[i];
68: if (nz == 0) continue;
69: cpi[row+1] = ai[i+1]; /* compressed row pointer */
70: ridx[row++] = i; /* compressed row local index */
71: }
72: compressedrow->nrows = nrows;
73: compressedrow->i = cpi;
74: compressedrow->rindex = ridx;
75: }
76:
77: return(0);
78: }