xref: /petsc/src/mat/impls/sbaij/seq/sbaij.c (revision f9426fe092dba0ba2fdf65dfec8d938c4b10a31c)
149b5e25fSSatish Balay 
249b5e25fSSatish Balay /*
3a1373b80SHong Zhang     Defines the basic matrix operations for the SBAIJ (compressed row)
449b5e25fSSatish Balay   matrix storage format.
549b5e25fSSatish Balay */
6c6db04a5SJed Brown #include <../src/mat/impls/baij/seq/baij.h>         /*I "petscmat.h" I*/
7c6db04a5SJed Brown #include <../src/mat/impls/sbaij/seq/sbaij.h>
8c6db04a5SJed Brown #include <petscblaslapack.h>
949b5e25fSSatish Balay 
10c6db04a5SJed Brown #include <../src/mat/impls/sbaij/seq/relax.h>
1170dcbbb9SBarry Smith #define USESHORT
12c6db04a5SJed Brown #include <../src/mat/impls/sbaij/seq/relax.h>
1370dcbbb9SBarry Smith 
14ace3abfcSBarry Smith extern PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat,PetscBool);
15b5b17502SBarry Smith 
1649b5e25fSSatish Balay /*
1749b5e25fSSatish Balay      Checks for missing diagonals
1849b5e25fSSatish Balay */
194a2ae208SSatish Balay #undef __FUNCT__
204a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqSBAIJ"
21ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqSBAIJ(Mat A,PetscBool  *missing,PetscInt *dd)
2249b5e25fSSatish Balay {
23045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
246849ba73SBarry Smith   PetscErrorCode ierr;
2513f74950SBarry Smith   PetscInt       *diag,*jj = a->j,i;
2649b5e25fSSatish Balay 
2749b5e25fSSatish Balay   PetscFunctionBegin;
28045c9aa0SHong Zhang   ierr     = MatMarkDiagonal_SeqSBAIJ(A);CHKERRQ(ierr);
292af78befSBarry Smith   *missing = PETSC_FALSE;
30358d2f5dSShri Abhyankar   if (A->rmap->n > 0 && !jj) {
31358d2f5dSShri Abhyankar     *missing = PETSC_TRUE;
32358d2f5dSShri Abhyankar     if (dd) *dd = 0;
33358d2f5dSShri Abhyankar     PetscInfo(A,"Matrix has no entries therefore is missing diagonal");
34358d2f5dSShri Abhyankar   } else {
35358d2f5dSShri Abhyankar     diag = a->diag;
3649b5e25fSSatish Balay     for (i=0; i<a->mbs; i++) {
372af78befSBarry Smith       if (jj[diag[i]] != i) {
382af78befSBarry Smith         *missing = PETSC_TRUE;
392af78befSBarry Smith         if (dd) *dd = i;
402af78befSBarry Smith         break;
412af78befSBarry Smith       }
4249b5e25fSSatish Balay     }
43358d2f5dSShri Abhyankar   }
4449b5e25fSSatish Balay   PetscFunctionReturn(0);
4549b5e25fSSatish Balay }
4649b5e25fSSatish Balay 
474a2ae208SSatish Balay #undef __FUNCT__
484a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqSBAIJ"
49dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqSBAIJ(Mat A)
5049b5e25fSSatish Balay {
51045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
526849ba73SBarry Smith   PetscErrorCode ierr;
5309f38230SBarry Smith   PetscInt       i;
5449b5e25fSSatish Balay 
5549b5e25fSSatish Balay   PetscFunctionBegin;
5609f38230SBarry Smith   if (!a->diag) {
5709f38230SBarry Smith     ierr         = PetscMalloc(a->mbs*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
583bb1ff40SBarry Smith     ierr         = PetscLogObjectMemory((PetscObject)A,a->mbs*sizeof(PetscInt));CHKERRQ(ierr);
59c760cd28SBarry Smith     a->free_diag = PETSC_TRUE;
6009f38230SBarry Smith   }
6109f38230SBarry Smith   for (i=0; i<a->mbs; i++) a->diag[i] = a->i[i];
6249b5e25fSSatish Balay   PetscFunctionReturn(0);
6349b5e25fSSatish Balay }
6449b5e25fSSatish Balay 
654a2ae208SSatish Balay #undef __FUNCT__
664a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqSBAIJ"
671a83f524SJed Brown static PetscErrorCode MatGetRowIJ_SeqSBAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool blockcompressed,PetscInt *nn,const PetscInt *inia[],const PetscInt *inja[],PetscBool  *done)
6849b5e25fSSatish Balay {
69a6ece127SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
70d0f46423SBarry Smith   PetscInt       i,j,n = a->mbs,nz = a->i[n],bs = A->rmap->bs;
711a83f524SJed Brown   PetscInt       **ia = (PetscInt**)inia,**ja = (PetscInt**)inja;
728f7157efSSatish Balay   PetscErrorCode ierr;
7349b5e25fSSatish Balay 
7449b5e25fSSatish Balay   PetscFunctionBegin;
75d3e5a4abSHong Zhang   *nn = n;
76a1373b80SHong Zhang   if (!ia) PetscFunctionReturn(0);
778f7157efSSatish Balay   if (!blockcompressed) {
788f7157efSSatish Balay     /* malloc & create the natural set of indices */
79f1d0d59dSSatish Balay     ierr = PetscMalloc2((n+1)*bs,PetscInt,ia,nz*bs,PetscInt,ja);CHKERRQ(ierr);
808f7157efSSatish Balay     for (i=0; i<n+1; i++) {
818f7157efSSatish Balay       for (j=0; j<bs; j++) {
828f7157efSSatish Balay         *ia[i*bs+j] = a->i[i]*bs+j+oshift;
838f7157efSSatish Balay       }
848f7157efSSatish Balay     }
858f7157efSSatish Balay     for (i=0; i<nz; i++) {
868f7157efSSatish Balay       for (j=0; j<bs; j++) {
878f7157efSSatish Balay         *ja[i*bs+j] = a->j[i]*bs+j+oshift;
888f7157efSSatish Balay       }
898f7157efSSatish Balay     }
908f7157efSSatish Balay   } else { /* blockcompressed */
91a6ece127SHong Zhang     if (oshift == 1) {
92a6ece127SHong Zhang       /* temporarily add 1 to i and j indices */
936c6c5352SBarry Smith       for (i=0; i<nz; i++) a->j[i]++;
94a1373b80SHong Zhang       for (i=0; i<n+1; i++) a->i[i]++;
958f7157efSSatish Balay     }
96a1373b80SHong Zhang     *ia = a->i; *ja = a->j;
97a6ece127SHong Zhang   }
9849b5e25fSSatish Balay   PetscFunctionReturn(0);
9949b5e25fSSatish Balay }
10049b5e25fSSatish Balay 
1014a2ae208SSatish Balay #undef __FUNCT__
1024a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqSBAIJ"
1031a83f524SJed Brown static PetscErrorCode MatRestoreRowIJ_SeqSBAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool blockcompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
10449b5e25fSSatish Balay {
105b7aaefc3SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
1068f7157efSSatish Balay   PetscInt       i,n = a->mbs,nz = a->i[n];
1078f7157efSSatish Balay   PetscErrorCode ierr;
108a6ece127SHong Zhang 
10949b5e25fSSatish Balay   PetscFunctionBegin;
11049b5e25fSSatish Balay   if (!ia) PetscFunctionReturn(0);
111a6ece127SHong Zhang 
1128f7157efSSatish Balay   if (!blockcompressed) {
1138f7157efSSatish Balay     ierr = PetscFree2(*ia,*ja);CHKERRQ(ierr);
1148f7157efSSatish Balay   } else if (oshift == 1) { /* blockcompressed */
1156c6c5352SBarry Smith     for (i=0; i<nz; i++) a->j[i]--;
116a6ece127SHong Zhang     for (i=0; i<n+1; i++) a->i[i]--;
117a6ece127SHong Zhang   }
118a6ece127SHong Zhang   PetscFunctionReturn(0);
11949b5e25fSSatish Balay }
12049b5e25fSSatish Balay 
1214a2ae208SSatish Balay #undef __FUNCT__
1224a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqSBAIJ"
123dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqSBAIJ(Mat A)
12449b5e25fSSatish Balay {
12549b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
126dfbe8321SBarry Smith   PetscErrorCode ierr;
12749b5e25fSSatish Balay 
12849b5e25fSSatish Balay   PetscFunctionBegin;
129a9f03627SSatish Balay #if defined(PETSC_USE_LOG)
130d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, NZ=%D",A->rmap->N,a->nz);
131a9f03627SSatish Balay #endif
132e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
1337f53bb6cSHong Zhang   if (a->free_diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
1346bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
1356bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
1366bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
137c31cb41cSBarry Smith   ierr = PetscFree(a->idiag);CHKERRQ(ierr);
138c31cb41cSBarry Smith   ierr = PetscFree(a->inode.size);CHKERRQ(ierr);
139c760cd28SBarry Smith   if (a->free_imax_ilen) {ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);}
14005b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
14141f059aeSBarry Smith   ierr = PetscFree(a->sor_work);CHKERRQ(ierr);
14205b42c5fSBarry Smith   ierr = PetscFree(a->solves_work);CHKERRQ(ierr);
14305b42c5fSBarry Smith   ierr = PetscFree(a->mult_work);CHKERRQ(ierr);
14405b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
14505b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
1464da8f245SBarry Smith   if (a->free_jshort) {ierr = PetscFree(a->jshort);CHKERRQ(ierr);}
1471a3463dfSHong Zhang   ierr = PetscFree(a->inew);CHKERRQ(ierr);
1486bf464f9SBarry Smith   ierr = MatDestroy(&a->parent);CHKERRQ(ierr);
149bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
150901853e0SKris Buschelman 
151dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
152bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);CHKERRQ(ierr);
153bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);CHKERRQ(ierr);
154bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetColumnIndices_C",NULL);CHKERRQ(ierr);
155bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqaij_C",NULL);CHKERRQ(ierr);
156bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqbaij_C",NULL);CHKERRQ(ierr);
157bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetPreallocation_C",NULL);CHKERRQ(ierr);
158bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqsbstrm_C",NULL);CHKERRQ(ierr);
15949b5e25fSSatish Balay   PetscFunctionReturn(0);
16049b5e25fSSatish Balay }
16149b5e25fSSatish Balay 
1624a2ae208SSatish Balay #undef __FUNCT__
1634a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqSBAIJ"
164ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqSBAIJ(Mat A,MatOption op,PetscBool flg)
16549b5e25fSSatish Balay {
166045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
16763ba0a88SBarry Smith   PetscErrorCode ierr;
16849b5e25fSSatish Balay 
16949b5e25fSSatish Balay   PetscFunctionBegin;
1704d9d31abSKris Buschelman   switch (op) {
1714d9d31abSKris Buschelman   case MAT_ROW_ORIENTED:
1724e0d8c25SBarry Smith     a->roworiented = flg;
1734d9d31abSKris Buschelman     break;
174a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
175a9817697SBarry Smith     a->keepnonzeropattern = flg;
1764d9d31abSKris Buschelman     break;
177512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
178512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1794d9d31abSKris Buschelman     break;
1804d9d31abSKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
1814e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1824d9d31abSKris Buschelman     break;
1834d9d31abSKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1844e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1854d9d31abSKris Buschelman     break;
18628b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
18728b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
18828b2fa4aSMatthew Knepley     break;
1894e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1904d9d31abSKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1914d9d31abSKris Buschelman   case MAT_USE_HASH_TABLE:
192290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1934d9d31abSKris Buschelman     break;
1949a4540c5SBarry Smith   case MAT_HERMITIAN:
195e32f2f54SBarry Smith     if (!A->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call MatAssemblyEnd() first");
1960bc54ff2SBarry Smith     if (A->cmap->n < 65536 && A->cmap->bs == 1) {
197eeffb40dSHong Zhang       A->ops->mult = MatMult_SeqSBAIJ_1_Hermitian_ushort;
1980bc54ff2SBarry Smith     } else if (A->cmap->bs == 1) {
199eeffb40dSHong Zhang       A->ops->mult = MatMult_SeqSBAIJ_1_Hermitian;
200e32f2f54SBarry Smith     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for Hermitian with block size greater than 1");
201eeffb40dSHong Zhang     break;
2023d472b54SHong Zhang   case MAT_SPD:
2035021d80fSJed Brown     /* These options are handled directly by MatSetOption() */
2043d472b54SHong Zhang     break;
20577e54ba9SKris Buschelman   case MAT_SYMMETRIC:
20677e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
2079a4540c5SBarry Smith   case MAT_SYMMETRY_ETERNAL:
208e32f2f54SBarry Smith     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix must be symmetric");
209290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s not relevent\n",MatOptions[op]);CHKERRQ(ierr);
210290bbb0aSBarry Smith     break;
211941593c8SHong Zhang   case MAT_IGNORE_LOWER_TRIANGULAR:
2124e0d8c25SBarry Smith     a->ignore_ltriangular = flg;
213941593c8SHong Zhang     break;
214941593c8SHong Zhang   case MAT_ERROR_LOWER_TRIANGULAR:
2154e0d8c25SBarry Smith     a->ignore_ltriangular = flg;
21677e54ba9SKris Buschelman     break;
217f5edf698SHong Zhang   case MAT_GETROW_UPPERTRIANGULAR:
2184e0d8c25SBarry Smith     a->getrow_utriangular = flg;
219f5edf698SHong Zhang     break;
2204d9d31abSKris Buschelman   default:
221e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
22249b5e25fSSatish Balay   }
22349b5e25fSSatish Balay   PetscFunctionReturn(0);
22449b5e25fSSatish Balay }
22549b5e25fSSatish Balay 
2264a2ae208SSatish Balay #undef __FUNCT__
2274a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqSBAIJ"
22813f74950SBarry Smith PetscErrorCode MatGetRow_SeqSBAIJ(Mat A,PetscInt row,PetscInt *ncols,PetscInt **cols,PetscScalar **v)
22949b5e25fSSatish Balay {
23049b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
2316849ba73SBarry Smith   PetscErrorCode ierr;
23213f74950SBarry Smith   PetscInt       itmp,i,j,k,M,*ai,*aj,bs,bn,bp,*cols_i,bs2;
23349b5e25fSSatish Balay   MatScalar      *aa,*aa_i;
23487828ca2SBarry Smith   PetscScalar    *v_i;
23549b5e25fSSatish Balay 
23649b5e25fSSatish Balay   PetscFunctionBegin;
237e32f2f54SBarry Smith   if (A && !a->getrow_utriangular) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatGetRow is not supported for SBAIJ matrix format. Getting the upper triangular part of row, run with -mat_getrow_uppertriangular, call MatSetOption(mat,MAT_GETROW_UPPERTRIANGULAR,PETSC_TRUE) or MatGetRowUpperTriangular()");
238f5edf698SHong Zhang   /* Get the upper triangular part of the row */
239d0f46423SBarry Smith   bs  = A->rmap->bs;
24049b5e25fSSatish Balay   ai  = a->i;
24149b5e25fSSatish Balay   aj  = a->j;
24249b5e25fSSatish Balay   aa  = a->a;
24349b5e25fSSatish Balay   bs2 = a->bs2;
24449b5e25fSSatish Balay 
245e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Row %D out of range", row);
24649b5e25fSSatish Balay 
24749b5e25fSSatish Balay   bn     = row/bs; /* Block number */
24849b5e25fSSatish Balay   bp     = row % bs; /* Block position */
24949b5e25fSSatish Balay   M      = ai[bn+1] - ai[bn];
25049b5e25fSSatish Balay   *ncols = bs*M;
25149b5e25fSSatish Balay 
25249b5e25fSSatish Balay   if (v) {
25349b5e25fSSatish Balay     *v = 0;
25449b5e25fSSatish Balay     if (*ncols) {
25587828ca2SBarry Smith       ierr = PetscMalloc((*ncols+row)*sizeof(PetscScalar),v);CHKERRQ(ierr);
25649b5e25fSSatish Balay       for (i=0; i<M; i++) { /* for each block in the block row */
25749b5e25fSSatish Balay         v_i  = *v + i*bs;
25849b5e25fSSatish Balay         aa_i = aa + bs2*(ai[bn] + i);
25926fbe8dcSKarl Rupp         for (j=bp,k=0; j<bs2; j+=bs,k++) v_i[k] = aa_i[j];
26049b5e25fSSatish Balay       }
26149b5e25fSSatish Balay     }
26249b5e25fSSatish Balay   }
26349b5e25fSSatish Balay 
26449b5e25fSSatish Balay   if (cols) {
26549b5e25fSSatish Balay     *cols = 0;
26649b5e25fSSatish Balay     if (*ncols) {
26713f74950SBarry Smith       ierr = PetscMalloc((*ncols+row)*sizeof(PetscInt),cols);CHKERRQ(ierr);
26849b5e25fSSatish Balay       for (i=0; i<M; i++) { /* for each block in the block row */
26949b5e25fSSatish Balay         cols_i = *cols + i*bs;
27049b5e25fSSatish Balay         itmp   = bs*aj[ai[bn] + i];
27126fbe8dcSKarl Rupp         for (j=0; j<bs; j++) cols_i[j] = itmp++;
27249b5e25fSSatish Balay       }
27349b5e25fSSatish Balay     }
27449b5e25fSSatish Balay   }
27549b5e25fSSatish Balay 
27649b5e25fSSatish Balay   /*search column A(0:row-1,row) (=A(row,0:row-1)). Could be expensive! */
2775ddb2528SHong Zhang   /* this segment is currently removed, so only entries in the upper triangle are obtained */
278519f805aSKarl Rupp #if defined(column_search)
27949b5e25fSSatish Balay   v_i    = *v    + M*bs;
28049b5e25fSSatish Balay   cols_i = *cols + M*bs;
28149b5e25fSSatish Balay   for (i=0; i<bn; i++) { /* for each block row */
28249b5e25fSSatish Balay     M = ai[i+1] - ai[i];
28349b5e25fSSatish Balay     for (j=0; j<M; j++) {
28449b5e25fSSatish Balay       itmp = aj[ai[i] + j];    /* block column value */
28549b5e25fSSatish Balay       if (itmp == bn) {
28649b5e25fSSatish Balay         aa_i = aa + bs2*(ai[i] + j) + bs*bp;
28749b5e25fSSatish Balay         for (k=0; k<bs; k++) {
28849b5e25fSSatish Balay           *cols_i++ = i*bs+k;
28949b5e25fSSatish Balay           *v_i++    = aa_i[k];
29049b5e25fSSatish Balay         }
29149b5e25fSSatish Balay         *ncols += bs;
29249b5e25fSSatish Balay         break;
29349b5e25fSSatish Balay       }
29449b5e25fSSatish Balay     }
29549b5e25fSSatish Balay   }
2965ddb2528SHong Zhang #endif
29749b5e25fSSatish Balay   PetscFunctionReturn(0);
29849b5e25fSSatish Balay }
29949b5e25fSSatish Balay 
3004a2ae208SSatish Balay #undef __FUNCT__
3014a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqSBAIJ"
30213f74950SBarry Smith PetscErrorCode MatRestoreRow_SeqSBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
30349b5e25fSSatish Balay {
304dfbe8321SBarry Smith   PetscErrorCode ierr;
30549b5e25fSSatish Balay 
30649b5e25fSSatish Balay   PetscFunctionBegin;
30705b42c5fSBarry Smith   if (idx) {ierr = PetscFree(*idx);CHKERRQ(ierr);}
30805b42c5fSBarry Smith   if (v)   {ierr = PetscFree(*v);CHKERRQ(ierr);}
30949b5e25fSSatish Balay   PetscFunctionReturn(0);
31049b5e25fSSatish Balay }
31149b5e25fSSatish Balay 
3124a2ae208SSatish Balay #undef __FUNCT__
313f5edf698SHong Zhang #define __FUNCT__ "MatGetRowUpperTriangular_SeqSBAIJ"
314f5edf698SHong Zhang PetscErrorCode MatGetRowUpperTriangular_SeqSBAIJ(Mat A)
315f5edf698SHong Zhang {
316f5edf698SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
317f5edf698SHong Zhang 
318f5edf698SHong Zhang   PetscFunctionBegin;
319f5edf698SHong Zhang   a->getrow_utriangular = PETSC_TRUE;
320f5edf698SHong Zhang   PetscFunctionReturn(0);
321f5edf698SHong Zhang }
322f5edf698SHong Zhang #undef __FUNCT__
323f5edf698SHong Zhang #define __FUNCT__ "MatRestoreRowUpperTriangular_SeqSBAIJ"
324f5edf698SHong Zhang PetscErrorCode MatRestoreRowUpperTriangular_SeqSBAIJ(Mat A)
325f5edf698SHong Zhang {
326f5edf698SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
327f5edf698SHong Zhang 
328f5edf698SHong Zhang   PetscFunctionBegin;
329f5edf698SHong Zhang   a->getrow_utriangular = PETSC_FALSE;
330f5edf698SHong Zhang   PetscFunctionReturn(0);
331f5edf698SHong Zhang }
332f5edf698SHong Zhang 
333f5edf698SHong Zhang #undef __FUNCT__
3344a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqSBAIJ"
335fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqSBAIJ(Mat A,MatReuse reuse,Mat *B)
33649b5e25fSSatish Balay {
337dfbe8321SBarry Smith   PetscErrorCode ierr;
3385fd66863SKarl Rupp 
33949b5e25fSSatish Balay   PetscFunctionBegin;
340815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
341999d9058SBarry Smith     ierr = MatDuplicate(A,MAT_COPY_VALUES,B);CHKERRQ(ierr);
342fc4dec0aSBarry Smith   }
3438115998fSBarry Smith   PetscFunctionReturn(0);
34449b5e25fSSatish Balay }
34549b5e25fSSatish Balay 
3464a2ae208SSatish Balay #undef __FUNCT__
3474a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_ASCII"
3486849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_ASCII(Mat A,PetscViewer viewer)
34949b5e25fSSatish Balay {
35049b5e25fSSatish Balay   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
351dfbe8321SBarry Smith   PetscErrorCode    ierr;
352d0f46423SBarry Smith   PetscInt          i,j,bs = A->rmap->bs,k,l,bs2=a->bs2;
353f3ef73ceSBarry Smith   PetscViewerFormat format;
354121deb67SSatish Balay   PetscInt          *diag;
35549b5e25fSSatish Balay 
35649b5e25fSSatish Balay   PetscFunctionBegin;
357b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
358456192e2SBarry Smith   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
35977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);CHKERRQ(ierr);
360fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
361d2507d54SMatthew Knepley     Mat aij;
362d5f3da31SBarry Smith     if (A->factortype && bs>1) {
36370d5e725SHong Zhang       ierr = PetscPrintf(PETSC_COMM_SELF,"Warning: matrix is factored with bs>1. MatView() with PETSC_VIEWER_ASCII_MATLAB is not supported and ignored!\n");CHKERRQ(ierr);
36470d5e725SHong Zhang       PetscFunctionReturn(0);
36570d5e725SHong Zhang     }
366c9f458caSMatthew Knepley     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&aij);CHKERRQ(ierr);
367c9f458caSMatthew Knepley     ierr = MatView(aij,viewer);CHKERRQ(ierr);
3686bf464f9SBarry Smith     ierr = MatDestroy(&aij);CHKERRQ(ierr);
369fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
370d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
37149b5e25fSSatish Balay     for (i=0; i<a->mbs; i++) {
37249b5e25fSSatish Balay       for (j=0; j<bs; j++) {
37377431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
37449b5e25fSSatish Balay         for (k=a->i[i]; k<a->i[i+1]; k++) {
37549b5e25fSSatish Balay           for (l=0; l<bs; l++) {
37649b5e25fSSatish Balay #if defined(PETSC_USE_COMPLEX)
37749b5e25fSSatish Balay             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
378a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",bs*a->j[k]+l,
37949b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
38049b5e25fSSatish Balay             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
381a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",bs*a->j[k]+l,
38249b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),-PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
38349b5e25fSSatish Balay             } else if (PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
384a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
38549b5e25fSSatish Balay             }
38649b5e25fSSatish Balay #else
38749b5e25fSSatish Balay             if (a->a[bs2*k + l*bs + j] != 0.0) {
388a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
38949b5e25fSSatish Balay             }
39049b5e25fSSatish Balay #endif
39149b5e25fSSatish Balay           }
39249b5e25fSSatish Balay         }
393b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
39449b5e25fSSatish Balay       }
39549b5e25fSSatish Balay     }
396d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
397c1490034SHong Zhang   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
398c1490034SHong Zhang     PetscFunctionReturn(0);
39949b5e25fSSatish Balay   } else {
400d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
401dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer);CHKERRQ(ierr);
4022c990fa1SHong Zhang     if (A->factortype) { /* for factored matrix */
4032c990fa1SHong Zhang       if (bs>1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"matrix is factored with bs>1. Not implemented yet");
4042c990fa1SHong Zhang 
405121deb67SSatish Balay       diag=a->diag;
406121deb67SSatish Balay       for (i=0; i<a->mbs; i++) { /* for row block i */
4072c990fa1SHong Zhang         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
4082c990fa1SHong Zhang         /* diagonal entry */
4092c990fa1SHong Zhang #if defined(PETSC_USE_COMPLEX)
4102c990fa1SHong Zhang         if (PetscImaginaryPart(a->a[diag[i]]) > 0.0) {
411ca0704adSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",a->j[diag[i]],PetscRealPart(1.0/a->a[diag[i]]),PetscImaginaryPart(1.0/a->a[diag[i]]));CHKERRQ(ierr);
4122c990fa1SHong Zhang         } else if (PetscImaginaryPart(a->a[diag[i]]) < 0.0) {
413ca0704adSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",a->j[diag[i]],PetscRealPart(1.0/a->a[diag[i]]),-PetscImaginaryPart(1.0/a->a[diag[i]]));CHKERRQ(ierr);
4142c990fa1SHong Zhang         } else {
4152c990fa1SHong Zhang           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[diag[i]],PetscRealPart(1.0/a->a[diag[i]]));CHKERRQ(ierr);
4162c990fa1SHong Zhang         }
4172c990fa1SHong Zhang #else
4182c990fa1SHong Zhang         ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[diag[i]],1.0/a->a[diag[i]]);CHKERRQ(ierr);
4192c990fa1SHong Zhang #endif
4202c990fa1SHong Zhang         /* off-diagonal entries */
4212c990fa1SHong Zhang         for (k=a->i[i]; k<a->i[i+1]-1; k++) {
4222c990fa1SHong Zhang #if defined(PETSC_USE_COMPLEX)
423ca0704adSBarry Smith           if (PetscImaginaryPart(a->a[k]) > 0.0) {
424ca0704adSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",bs*a->j[k],PetscRealPart(a->a[k]),PetscImaginaryPart(a->a[k]));CHKERRQ(ierr);
425ca0704adSBarry Smith           } else if (PetscImaginaryPart(a->a[k]) < 0.0) {
426ca0704adSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",bs*a->j[k],PetscRealPart(a->a[k]),-PetscImaginaryPart(a->a[k]));CHKERRQ(ierr);
4272c990fa1SHong Zhang           } else {
428ca0704adSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k],PetscRealPart(a->a[k]));CHKERRQ(ierr);
4292c990fa1SHong Zhang           }
4302c990fa1SHong Zhang #else
4312c990fa1SHong Zhang           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[k],a->a[k]);CHKERRQ(ierr);
4322c990fa1SHong Zhang #endif
4332c990fa1SHong Zhang         }
4342c990fa1SHong Zhang         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
4352c990fa1SHong Zhang       }
4362c990fa1SHong Zhang 
4372c990fa1SHong Zhang     } else { /* for non-factored matrix */
4380c74a584SJed Brown       for (i=0; i<a->mbs; i++) { /* for row block i */
4390c74a584SJed Brown         for (j=0; j<bs; j++) {   /* for row bs*i + j */
44077431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
4410c74a584SJed Brown           for (k=a->i[i]; k<a->i[i+1]; k++) { /* for column block */
4420c74a584SJed Brown             for (l=0; l<bs; l++) {            /* for column */
44349b5e25fSSatish Balay #if defined(PETSC_USE_COMPLEX)
44449b5e25fSSatish Balay               if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0) {
445a83599f4SBarry Smith                 ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",bs*a->j[k]+l,
44649b5e25fSSatish Balay                                               PetscRealPart(a->a[bs2*k + l*bs + j]),PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
44749b5e25fSSatish Balay               } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0) {
448a83599f4SBarry Smith                 ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",bs*a->j[k]+l,
44949b5e25fSSatish Balay                                               PetscRealPart(a->a[bs2*k + l*bs + j]),-PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
45049b5e25fSSatish Balay               } else {
451a83599f4SBarry Smith                 ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
45249b5e25fSSatish Balay               }
45349b5e25fSSatish Balay #else
454e9f7bc9eSHong Zhang               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
45549b5e25fSSatish Balay #endif
45649b5e25fSSatish Balay             }
45749b5e25fSSatish Balay           }
458b0a32e0cSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
45949b5e25fSSatish Balay         }
46049b5e25fSSatish Balay       }
4612c990fa1SHong Zhang     }
462d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
46349b5e25fSSatish Balay   }
464b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
46549b5e25fSSatish Balay   PetscFunctionReturn(0);
46649b5e25fSSatish Balay }
46749b5e25fSSatish Balay 
4689804daf3SBarry Smith #include <petscdraw.h>
4694a2ae208SSatish Balay #undef __FUNCT__
4704a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_Draw_Zoom"
4716849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
47249b5e25fSSatish Balay {
47349b5e25fSSatish Balay   Mat            A = (Mat) Aa;
47449b5e25fSSatish Balay   Mat_SeqSBAIJ   *a=(Mat_SeqSBAIJ*)A->data;
4756849ba73SBarry Smith   PetscErrorCode ierr;
476d0f46423SBarry Smith   PetscInt       row,i,j,k,l,mbs=a->mbs,color,bs=A->rmap->bs,bs2=a->bs2;
47713f74950SBarry Smith   PetscMPIInt    rank;
47849b5e25fSSatish Balay   PetscReal      xl,yl,xr,yr,x_l,x_r,y_l,y_r;
47949b5e25fSSatish Balay   MatScalar      *aa;
48049b5e25fSSatish Balay   MPI_Comm       comm;
481b0a32e0cSBarry Smith   PetscViewer    viewer;
48249b5e25fSSatish Balay 
48349b5e25fSSatish Balay   PetscFunctionBegin;
48449b5e25fSSatish Balay   /*
48549b5e25fSSatish Balay     This is nasty. If this is called from an originally parallel matrix
48649b5e25fSSatish Balay     then all processes call this,but only the first has the matrix so the
48749b5e25fSSatish Balay     rest should return immediately.
48849b5e25fSSatish Balay   */
48949b5e25fSSatish Balay   ierr = PetscObjectGetComm((PetscObject)draw,&comm);CHKERRQ(ierr);
49049b5e25fSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
49149b5e25fSSatish Balay   if (rank) PetscFunctionReturn(0);
49249b5e25fSSatish Balay 
49349b5e25fSSatish Balay   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
49449b5e25fSSatish Balay 
495b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
496b0a32e0cSBarry Smith   PetscDrawString(draw, .3*(xl+xr), .3*(yl+yr), PETSC_DRAW_BLACK, "symmetric");
49749b5e25fSSatish Balay 
49849b5e25fSSatish Balay   /* loop over matrix elements drawing boxes */
499b0a32e0cSBarry Smith   color = PETSC_DRAW_BLUE;
50049b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
50149b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
502d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
50349b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
50449b5e25fSSatish Balay       aa  = a->a + j*bs2;
50549b5e25fSSatish Balay       for (k=0; k<bs; k++) {
50649b5e25fSSatish Balay         for (l=0; l<bs; l++) {
50749b5e25fSSatish Balay           if (PetscRealPart(*aa++) >=  0.) continue;
508b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
50949b5e25fSSatish Balay         }
51049b5e25fSSatish Balay       }
51149b5e25fSSatish Balay     }
51249b5e25fSSatish Balay   }
513b0a32e0cSBarry Smith   color = PETSC_DRAW_CYAN;
51449b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
51549b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
516d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
51749b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
51849b5e25fSSatish Balay       aa = a->a + j*bs2;
51949b5e25fSSatish Balay       for (k=0; k<bs; k++) {
52049b5e25fSSatish Balay         for (l=0; l<bs; l++) {
52149b5e25fSSatish Balay           if (PetscRealPart(*aa++) != 0.) continue;
522b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
52349b5e25fSSatish Balay         }
52449b5e25fSSatish Balay       }
52549b5e25fSSatish Balay     }
52649b5e25fSSatish Balay   }
52749b5e25fSSatish Balay 
528b0a32e0cSBarry Smith   color = PETSC_DRAW_RED;
52949b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
53049b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
531d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
53249b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
53349b5e25fSSatish Balay       aa = a->a + j*bs2;
53449b5e25fSSatish Balay       for (k=0; k<bs; k++) {
53549b5e25fSSatish Balay         for (l=0; l<bs; l++) {
53649b5e25fSSatish Balay           if (PetscRealPart(*aa++) <= 0.) continue;
537b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
53849b5e25fSSatish Balay         }
53949b5e25fSSatish Balay       }
54049b5e25fSSatish Balay     }
54149b5e25fSSatish Balay   }
54249b5e25fSSatish Balay   PetscFunctionReturn(0);
54349b5e25fSSatish Balay }
54449b5e25fSSatish Balay 
5454a2ae208SSatish Balay #undef __FUNCT__
5464a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_Draw"
5476849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_Draw(Mat A,PetscViewer viewer)
54849b5e25fSSatish Balay {
549dfbe8321SBarry Smith   PetscErrorCode ierr;
55049b5e25fSSatish Balay   PetscReal      xl,yl,xr,yr,w,h;
551b0a32e0cSBarry Smith   PetscDraw      draw;
552ace3abfcSBarry Smith   PetscBool      isnull;
55349b5e25fSSatish Balay 
55449b5e25fSSatish Balay   PetscFunctionBegin;
555b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
556b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
55749b5e25fSSatish Balay 
55849b5e25fSSatish Balay   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
559d0f46423SBarry Smith   xr   = A->rmap->N; yr = A->rmap->N; h = yr/10.0; w = xr/10.0;
56049b5e25fSSatish Balay   xr  += w;    yr += h;  xl = -w;     yl = -h;
561b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
562b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqSBAIJ_Draw_Zoom,A);CHKERRQ(ierr);
5630298fd71SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr);
56449b5e25fSSatish Balay   PetscFunctionReturn(0);
56549b5e25fSSatish Balay }
56649b5e25fSSatish Balay 
5674a2ae208SSatish Balay #undef __FUNCT__
5684a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ"
569dfbe8321SBarry Smith PetscErrorCode MatView_SeqSBAIJ(Mat A,PetscViewer viewer)
57049b5e25fSSatish Balay {
571dfbe8321SBarry Smith   PetscErrorCode ierr;
572ace3abfcSBarry Smith   PetscBool      iascii,isdraw;
57308917f38SBarry Smith   FILE           *file = 0;
57449b5e25fSSatish Balay 
57549b5e25fSSatish Balay   PetscFunctionBegin;
576251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
577251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
57832077d6dSBarry Smith   if (iascii) {
57949b5e25fSSatish Balay     ierr = MatView_SeqSBAIJ_ASCII(A,viewer);CHKERRQ(ierr);
58049b5e25fSSatish Balay   } else if (isdraw) {
58149b5e25fSSatish Balay     ierr = MatView_SeqSBAIJ_Draw(A,viewer);CHKERRQ(ierr);
58249b5e25fSSatish Balay   } else {
583a5e6ed63SBarry Smith     Mat B;
584ceb03754SKris Buschelman     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&B);CHKERRQ(ierr);
585a5e6ed63SBarry Smith     ierr = MatView(B,viewer);CHKERRQ(ierr);
5866bf464f9SBarry Smith     ierr = MatDestroy(&B);CHKERRQ(ierr);
58708917f38SBarry Smith     ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
58808917f38SBarry Smith     if (file) {
58908917f38SBarry Smith       fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
59008917f38SBarry Smith     }
59149b5e25fSSatish Balay   }
59249b5e25fSSatish Balay   PetscFunctionReturn(0);
59349b5e25fSSatish Balay }
59449b5e25fSSatish Balay 
59549b5e25fSSatish Balay 
5964a2ae208SSatish Balay #undef __FUNCT__
5974a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqSBAIJ"
59813f74950SBarry Smith PetscErrorCode MatGetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
59949b5e25fSSatish Balay {
600045c9aa0SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
60113f74950SBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
60213f74950SBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
603d0f46423SBarry Smith   PetscInt     brow,bcol,ridx,cidx,bs=A->rmap->bs,bs2=a->bs2;
60497e567efSBarry Smith   MatScalar    *ap,*aa = a->a;
60549b5e25fSSatish Balay 
60649b5e25fSSatish Balay   PetscFunctionBegin;
60749b5e25fSSatish Balay   for (k=0; k<m; k++) { /* loop over rows */
60849b5e25fSSatish Balay     row = im[k]; brow = row/bs;
609e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
610e32f2f54SBarry Smith     if (row >= A->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->N-1);
61149b5e25fSSatish Balay     rp   = aj + ai[brow]; ap = aa + bs2*ai[brow];
61249b5e25fSSatish Balay     nrow = ailen[brow];
61349b5e25fSSatish Balay     for (l=0; l<n; l++) { /* loop over columns */
614e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
615e32f2f54SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
61649b5e25fSSatish Balay       col  = in[l];
61749b5e25fSSatish Balay       bcol = col/bs;
61849b5e25fSSatish Balay       cidx = col%bs;
61949b5e25fSSatish Balay       ridx = row%bs;
62049b5e25fSSatish Balay       high = nrow;
62149b5e25fSSatish Balay       low  = 0; /* assume unsorted */
62249b5e25fSSatish Balay       while (high-low > 5) {
62349b5e25fSSatish Balay         t = (low+high)/2;
62449b5e25fSSatish Balay         if (rp[t] > bcol) high = t;
62549b5e25fSSatish Balay         else              low  = t;
62649b5e25fSSatish Balay       }
62749b5e25fSSatish Balay       for (i=low; i<high; i++) {
62849b5e25fSSatish Balay         if (rp[i] > bcol) break;
62949b5e25fSSatish Balay         if (rp[i] == bcol) {
63049b5e25fSSatish Balay           *v++ = ap[bs2*i+bs*cidx+ridx];
63149b5e25fSSatish Balay           goto finished;
63249b5e25fSSatish Balay         }
63349b5e25fSSatish Balay       }
63497e567efSBarry Smith       *v++ = 0.0;
63549b5e25fSSatish Balay finished:;
63649b5e25fSSatish Balay     }
63749b5e25fSSatish Balay   }
63849b5e25fSSatish Balay   PetscFunctionReturn(0);
63949b5e25fSSatish Balay }
64049b5e25fSSatish Balay 
64149b5e25fSSatish Balay 
6424a2ae208SSatish Balay #undef __FUNCT__
6434a2ae208SSatish Balay #define __FUNCT__ "MatSetValuesBlocked_SeqSBAIJ"
64413f74950SBarry Smith PetscErrorCode MatSetValuesBlocked_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
64549b5e25fSSatish Balay {
6460880e062SHong Zhang   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
6476849ba73SBarry Smith   PetscErrorCode    ierr;
648e2ee6c50SBarry Smith   PetscInt          *rp,k,low,high,t,ii,jj,row,nrow,i,col,l,rmax,N,lastcol = -1;
64913f74950SBarry Smith   PetscInt          *imax      =a->imax,*ai=a->i,*ailen=a->ilen;
650d0f46423SBarry Smith   PetscInt          *aj        =a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs,stepval;
651ace3abfcSBarry Smith   PetscBool         roworiented=a->roworiented;
652dd6ea824SBarry Smith   const PetscScalar *value     = v;
653f15d580aSBarry Smith   MatScalar         *ap,*aa = a->a,*bap;
6540880e062SHong Zhang 
65549b5e25fSSatish Balay   PetscFunctionBegin;
65626fbe8dcSKarl Rupp   if (roworiented) stepval = (n-1)*bs;
65726fbe8dcSKarl Rupp   else stepval = (m-1)*bs;
65826fbe8dcSKarl Rupp 
6590880e062SHong Zhang   for (k=0; k<m; k++) { /* loop over added rows */
6600880e062SHong Zhang     row = im[k];
6610880e062SHong Zhang     if (row < 0) continue;
6622515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
663e32f2f54SBarry Smith     if (row >= a->mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,a->mbs-1);
6640880e062SHong Zhang #endif
6650880e062SHong Zhang     rp   = aj + ai[row];
6660880e062SHong Zhang     ap   = aa + bs2*ai[row];
6670880e062SHong Zhang     rmax = imax[row];
6680880e062SHong Zhang     nrow = ailen[row];
6690880e062SHong Zhang     low  = 0;
670818f2c47SBarry Smith     high = nrow;
6710880e062SHong Zhang     for (l=0; l<n; l++) { /* loop over added columns */
6720880e062SHong Zhang       if (in[l] < 0) continue;
6730880e062SHong Zhang       col = in[l];
6742515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
675e32f2f54SBarry Smith       if (col >= a->nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",col,a->nbs-1);
676b1823623SSatish Balay #endif
677b98bf0e1SJed Brown       if (col < row) {
67826fbe8dcSKarl Rupp         if (a->ignore_ltriangular) continue; /* ignore lower triangular block */
67926fbe8dcSKarl Rupp         else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Lower triangular value cannot be set for sbaij format. Ignoring these values, run with -mat_ignore_lower_triangular or call MatSetOption(mat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE)");
680b98bf0e1SJed Brown       }
68126fbe8dcSKarl Rupp       if (roworiented) value = v + k*(stepval+bs)*bs + l*bs;
68226fbe8dcSKarl Rupp       else value = v + l*(stepval+bs)*bs + k*bs;
68326fbe8dcSKarl Rupp 
68426fbe8dcSKarl Rupp       if (col <= lastcol) low = 0;
68526fbe8dcSKarl Rupp       else high = nrow;
68626fbe8dcSKarl Rupp 
687e2ee6c50SBarry Smith       lastcol = col;
6880880e062SHong Zhang       while (high-low > 7) {
6890880e062SHong Zhang         t = (low+high)/2;
6900880e062SHong Zhang         if (rp[t] > col) high = t;
6910880e062SHong Zhang         else             low  = t;
6920880e062SHong Zhang       }
6930880e062SHong Zhang       for (i=low; i<high; i++) {
6940880e062SHong Zhang         if (rp[i] > col) break;
6950880e062SHong Zhang         if (rp[i] == col) {
6960880e062SHong Zhang           bap = ap +  bs2*i;
6970880e062SHong Zhang           if (roworiented) {
6980880e062SHong Zhang             if (is == ADD_VALUES) {
6990880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
7000880e062SHong Zhang                 for (jj=ii; jj<bs2; jj+=bs) {
7010880e062SHong Zhang                   bap[jj] += *value++;
7020880e062SHong Zhang                 }
7030880e062SHong Zhang               }
7040880e062SHong Zhang             } else {
7050880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
7060880e062SHong Zhang                 for (jj=ii; jj<bs2; jj+=bs) {
7070880e062SHong Zhang                   bap[jj] = *value++;
7080880e062SHong Zhang                 }
7090880e062SHong Zhang                }
7100880e062SHong Zhang             }
7110880e062SHong Zhang           } else {
7120880e062SHong Zhang             if (is == ADD_VALUES) {
7130880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
7140880e062SHong Zhang                 for (jj=0; jj<bs; jj++) {
7150880e062SHong Zhang                   *bap++ += *value++;
7160880e062SHong Zhang                 }
7170880e062SHong Zhang               }
7180880e062SHong Zhang             } else {
7190880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
7200880e062SHong Zhang                 for (jj=0; jj<bs; jj++) {
7210880e062SHong Zhang                   *bap++  = *value++;
7220880e062SHong Zhang                 }
7230880e062SHong Zhang               }
7240880e062SHong Zhang             }
7250880e062SHong Zhang           }
7260880e062SHong Zhang           goto noinsert2;
7270880e062SHong Zhang         }
7280880e062SHong Zhang       }
7290880e062SHong Zhang       if (nonew == 1) goto noinsert2;
730e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
731fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
732c03d1d03SSatish Balay       N = nrow++ - 1; high++;
7330880e062SHong Zhang       /* shift up all the later entries in this row */
7340880e062SHong Zhang       for (ii=N; ii>=i; ii--) {
7350880e062SHong Zhang         rp[ii+1] = rp[ii];
7360880e062SHong Zhang         ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
7370880e062SHong Zhang       }
7380880e062SHong Zhang       if (N >= i) {
7390880e062SHong Zhang         ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
7400880e062SHong Zhang       }
7410880e062SHong Zhang       rp[i] = col;
7420880e062SHong Zhang       bap   = ap +  bs2*i;
7430880e062SHong Zhang       if (roworiented) {
7440880e062SHong Zhang         for (ii=0; ii<bs; ii++,value+=stepval) {
7450880e062SHong Zhang           for (jj=ii; jj<bs2; jj+=bs) {
7460880e062SHong Zhang             bap[jj] = *value++;
7470880e062SHong Zhang           }
7480880e062SHong Zhang         }
7490880e062SHong Zhang       } else {
7500880e062SHong Zhang         for (ii=0; ii<bs; ii++,value+=stepval) {
7510880e062SHong Zhang           for (jj=0; jj<bs; jj++) {
7520880e062SHong Zhang             *bap++ = *value++;
7530880e062SHong Zhang           }
7540880e062SHong Zhang         }
7550880e062SHong Zhang        }
7560880e062SHong Zhang     noinsert2:;
7570880e062SHong Zhang       low = i;
7580880e062SHong Zhang     }
7590880e062SHong Zhang     ailen[row] = nrow;
7600880e062SHong Zhang   }
7610880e062SHong Zhang   PetscFunctionReturn(0);
76249b5e25fSSatish Balay }
76349b5e25fSSatish Balay 
76464831d72SBarry Smith /*
76564831d72SBarry Smith     This is not yet used
76664831d72SBarry Smith */
7674a2ae208SSatish Balay #undef __FUNCT__
7684108e4d5SBarry Smith #define __FUNCT__ "MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode"
7694108e4d5SBarry Smith PetscErrorCode MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode(Mat A)
7700def2e27SBarry Smith {
7710def2e27SBarry Smith   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
7720def2e27SBarry Smith   PetscErrorCode ierr;
7730def2e27SBarry Smith   const PetscInt *ai = a->i, *aj = a->j,*cols;
7740def2e27SBarry Smith   PetscInt       i   = 0,j,blk_size,m = A->rmap->n,node_count = 0,nzx,nzy,*ns,row,nz,cnt,cnt2,*counts;
775ace3abfcSBarry Smith   PetscBool      flag;
7760def2e27SBarry Smith 
7770def2e27SBarry Smith   PetscFunctionBegin;
7780def2e27SBarry Smith   ierr = PetscMalloc(m*sizeof(PetscInt),&ns);CHKERRQ(ierr);
7790def2e27SBarry Smith   while (i < m) {
7800def2e27SBarry Smith     nzx = ai[i+1] - ai[i];       /* Number of nonzeros */
7810def2e27SBarry Smith     /* Limits the number of elements in a node to 'a->inode.limit' */
7820def2e27SBarry Smith     for (j=i+1,blk_size=1; j<m && blk_size <a->inode.limit; ++j,++blk_size) {
7830def2e27SBarry Smith       nzy = ai[j+1] - ai[j];
7840def2e27SBarry Smith       if (nzy != (nzx - j + i)) break;
7850def2e27SBarry Smith       ierr = PetscMemcmp(aj + ai[i] + j - i,aj + ai[j],nzy*sizeof(PetscInt),&flag);CHKERRQ(ierr);
7860def2e27SBarry Smith       if (!flag) break;
7870def2e27SBarry Smith     }
7880def2e27SBarry Smith     ns[node_count++] = blk_size;
78926fbe8dcSKarl Rupp 
7900def2e27SBarry Smith     i = j;
7910def2e27SBarry Smith   }
7920def2e27SBarry Smith   if (!a->inode.size && m && node_count > .9*m) {
7930def2e27SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
7940def2e27SBarry Smith     ierr = PetscInfo2(A,"Found %D nodes out of %D rows. Not using Inode routines\n",node_count,m);CHKERRQ(ierr);
7950def2e27SBarry Smith   } else {
7960def2e27SBarry Smith     a->inode.node_count = node_count;
79726fbe8dcSKarl Rupp 
7980def2e27SBarry Smith     ierr = PetscMalloc(node_count*sizeof(PetscInt),&a->inode.size);CHKERRQ(ierr);
7993bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A,node_count*sizeof(PetscInt));CHKERRQ(ierr);
80022d28d08SBarry Smith     ierr = PetscMemcpy(a->inode.size,ns,node_count*sizeof(PetscInt));CHKERRQ(ierr);
8010def2e27SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
8020def2e27SBarry Smith     ierr = PetscInfo3(A,"Found %D nodes of %D. Limit used: %D. Using Inode routines\n",node_count,m,a->inode.limit);CHKERRQ(ierr);
8030def2e27SBarry Smith 
8040def2e27SBarry Smith     /* count collections of adjacent columns in each inode */
8050def2e27SBarry Smith     row = 0;
8060def2e27SBarry Smith     cnt = 0;
8070def2e27SBarry Smith     for (i=0; i<node_count; i++) {
8080def2e27SBarry Smith       cols = aj + ai[row] + a->inode.size[i];
8090def2e27SBarry Smith       nz   = ai[row+1] - ai[row] - a->inode.size[i];
8100def2e27SBarry Smith       for (j=1; j<nz; j++) {
81126fbe8dcSKarl Rupp         if (cols[j] != cols[j-1]+1) cnt++;
8120def2e27SBarry Smith       }
8130def2e27SBarry Smith       cnt++;
8140def2e27SBarry Smith       row += a->inode.size[i];
8150def2e27SBarry Smith     }
8160def2e27SBarry Smith     ierr = PetscMalloc(2*cnt*sizeof(PetscInt),&counts);CHKERRQ(ierr);
8170def2e27SBarry Smith     cnt  = 0;
8180def2e27SBarry Smith     row  = 0;
8190def2e27SBarry Smith     for (i=0; i<node_count; i++) {
8200def2e27SBarry Smith       cols = aj + ai[row] + a->inode.size[i];
8210def2e27SBarry Smith       counts[2*cnt] = cols[0];
8220def2e27SBarry Smith       nz   = ai[row+1] - ai[row] - a->inode.size[i];
8230def2e27SBarry Smith       cnt2 = 1;
8240def2e27SBarry Smith       for (j=1; j<nz; j++) {
8250def2e27SBarry Smith         if (cols[j] != cols[j-1]+1) {
8260def2e27SBarry Smith           counts[2*(cnt++)+1] = cnt2;
8270def2e27SBarry Smith           counts[2*cnt]       = cols[j];
8280def2e27SBarry Smith           cnt2 = 1;
8290def2e27SBarry Smith         } else cnt2++;
8300def2e27SBarry Smith       }
8310def2e27SBarry Smith       counts[2*(cnt++)+1] = cnt2;
8320def2e27SBarry Smith       row += a->inode.size[i];
8330def2e27SBarry Smith     }
83422d28d08SBarry Smith     ierr = PetscIntView(2*cnt,counts,0);CHKERRQ(ierr);
8350def2e27SBarry Smith   }
83638702af4SBarry Smith   PetscFunctionReturn(0);
83738702af4SBarry Smith }
83838702af4SBarry Smith 
83938702af4SBarry Smith #undef __FUNCT__
8404a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqSBAIJ"
841dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqSBAIJ(Mat A,MatAssemblyType mode)
84249b5e25fSSatish Balay {
84349b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
8446849ba73SBarry Smith   PetscErrorCode ierr;
84513f74950SBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
846d0f46423SBarry Smith   PetscInt       m      = A->rmap->N,*ip,N,*ailen = a->ilen;
84713f74950SBarry Smith   PetscInt       mbs    = a->mbs,bs2 = a->bs2,rmax = 0;
84849b5e25fSSatish Balay   MatScalar      *aa    = a->a,*ap;
84949b5e25fSSatish Balay 
85049b5e25fSSatish Balay   PetscFunctionBegin;
85149b5e25fSSatish Balay   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
85249b5e25fSSatish Balay 
85349b5e25fSSatish Balay   if (m) rmax = ailen[0];
85449b5e25fSSatish Balay   for (i=1; i<mbs; i++) {
85549b5e25fSSatish Balay     /* move each row back by the amount of empty slots (fshift) before it*/
85649b5e25fSSatish Balay     fshift += imax[i-1] - ailen[i-1];
85749b5e25fSSatish Balay     rmax    = PetscMax(rmax,ailen[i]);
85849b5e25fSSatish Balay     if (fshift) {
85949b5e25fSSatish Balay       ip = aj + ai[i]; ap = aa + bs2*ai[i];
86049b5e25fSSatish Balay       N  = ailen[i];
86149b5e25fSSatish Balay       for (j=0; j<N; j++) {
86249b5e25fSSatish Balay         ip[j-fshift] = ip[j];
86349b5e25fSSatish Balay         ierr         = PetscMemcpy(ap+(j-fshift)*bs2,ap+j*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
86449b5e25fSSatish Balay       }
86549b5e25fSSatish Balay     }
86649b5e25fSSatish Balay     ai[i] = ai[i-1] + ailen[i-1];
86749b5e25fSSatish Balay   }
86849b5e25fSSatish Balay   if (mbs) {
86949b5e25fSSatish Balay     fshift += imax[mbs-1] - ailen[mbs-1];
87049b5e25fSSatish Balay     ai[mbs] = ai[mbs-1] + ailen[mbs-1];
87149b5e25fSSatish Balay   }
87249b5e25fSSatish Balay   /* reset ilen and imax for each row */
87349b5e25fSSatish Balay   for (i=0; i<mbs; i++) {
87449b5e25fSSatish Balay     ailen[i] = imax[i] = ai[i+1] - ai[i];
87549b5e25fSSatish Balay   }
8766c6c5352SBarry Smith   a->nz = ai[mbs];
87749b5e25fSSatish Balay 
878b424e231SHong Zhang   /* diagonals may have moved, reset it */
879b424e231SHong Zhang   if (a->diag) {
8802ed38d0bSJed Brown     ierr = PetscMemcpy(a->diag,ai,mbs*sizeof(PetscInt));CHKERRQ(ierr);
88149b5e25fSSatish Balay   }
88226fbe8dcSKarl Rupp   if (fshift && a->nounused == -1) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D block size %D, %D unneeded", m, A->cmap->n, A->rmap->bs, fshift*bs2);
88326fbe8dcSKarl Rupp 
884d0f46423SBarry Smith   ierr = PetscInfo5(A,"Matrix size: %D X %D, block size %D; storage space: %D unneeded, %D used\n",m,A->rmap->N,A->rmap->bs,fshift*bs2,a->nz*bs2);CHKERRQ(ierr);
885ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues is %D\n",a->reallocs);CHKERRQ(ierr);
886ae15b995SBarry Smith   ierr = PetscInfo1(A,"Most nonzeros blocks in any row is %D\n",rmax);CHKERRQ(ierr);
88726fbe8dcSKarl Rupp 
8888e58a170SBarry Smith   A->info.mallocs    += a->reallocs;
88949b5e25fSSatish Balay   a->reallocs         = 0;
89049b5e25fSSatish Balay   A->info.nz_unneeded = (PetscReal)fshift*bs2;
891061b2667SBarry Smith   a->idiagvalid       = PETSC_FALSE;
89238702af4SBarry Smith 
89338702af4SBarry Smith   if (A->cmap->n < 65536 && A->cmap->bs == 1) {
89444e1c64aSLisandro Dalcin     if (a->jshort && a->free_jshort) {
89517803ae8SHong Zhang       /* when matrix data structure is changed, previous jshort must be replaced */
89617803ae8SHong Zhang       ierr = PetscFree(a->jshort);CHKERRQ(ierr);
89717803ae8SHong Zhang     }
89838702af4SBarry Smith     ierr = PetscMalloc(a->i[A->rmap->n]*sizeof(unsigned short),&a->jshort);CHKERRQ(ierr);
8993bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A,a->i[A->rmap->n]*sizeof(unsigned short));CHKERRQ(ierr);
90038702af4SBarry Smith     for (i=0; i<a->i[A->rmap->n]; i++) a->jshort[i] = a->j[i];
90138702af4SBarry Smith     A->ops->mult   = MatMult_SeqSBAIJ_1_ushort;
90241f059aeSBarry Smith     A->ops->sor    = MatSOR_SeqSBAIJ_ushort;
9034da8f245SBarry Smith     a->free_jshort = PETSC_TRUE;
90438702af4SBarry Smith   }
90549b5e25fSSatish Balay   PetscFunctionReturn(0);
90649b5e25fSSatish Balay }
90749b5e25fSSatish Balay 
90849b5e25fSSatish Balay /*
90949b5e25fSSatish Balay    This function returns an array of flags which indicate the locations of contiguous
91049b5e25fSSatish Balay    blocks that should be zeroed. for eg: if bs = 3  and is = [0,1,2,3,5,6,7,8,9]
91149b5e25fSSatish Balay    then the resulting sizes = [3,1,1,3,1] correspondig to sets [(0,1,2),(3),(5),(6,7,8),(9)]
91249b5e25fSSatish Balay    Assume: sizes should be long enough to hold all the values.
91349b5e25fSSatish Balay */
9144a2ae208SSatish Balay #undef __FUNCT__
9154a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqSBAIJ_Check_Blocks"
91613f74950SBarry Smith PetscErrorCode MatZeroRows_SeqSBAIJ_Check_Blocks(PetscInt idx[],PetscInt n,PetscInt bs,PetscInt sizes[], PetscInt *bs_max)
91749b5e25fSSatish Balay {
91813f74950SBarry Smith   PetscInt  i,j,k,row;
919ace3abfcSBarry Smith   PetscBool flg;
92049b5e25fSSatish Balay 
92149b5e25fSSatish Balay   PetscFunctionBegin;
92249b5e25fSSatish Balay   for (i=0,j=0; i<n; j++) {
92349b5e25fSSatish Balay     row = idx[i];
92449b5e25fSSatish Balay     if (row%bs!=0) { /* Not the begining of a block */
92549b5e25fSSatish Balay       sizes[j] = 1;
92649b5e25fSSatish Balay       i++;
92749b5e25fSSatish Balay     } else if (i+bs > n) { /* Beginning of a block, but complete block doesn't exist (at idx end) */
92849b5e25fSSatish Balay       sizes[j] = 1;         /* Also makes sure atleast 'bs' values exist for next else */
92949b5e25fSSatish Balay       i++;
93049b5e25fSSatish Balay     } else { /* Begining of the block, so check if the complete block exists */
93149b5e25fSSatish Balay       flg = PETSC_TRUE;
93249b5e25fSSatish Balay       for (k=1; k<bs; k++) {
93349b5e25fSSatish Balay         if (row+k != idx[i+k]) { /* break in the block */
93449b5e25fSSatish Balay           flg = PETSC_FALSE;
93549b5e25fSSatish Balay           break;
93649b5e25fSSatish Balay         }
93749b5e25fSSatish Balay       }
938abc0a331SBarry Smith       if (flg) { /* No break in the bs */
93949b5e25fSSatish Balay         sizes[j] = bs;
94049b5e25fSSatish Balay         i       += bs;
94149b5e25fSSatish Balay       } else {
94249b5e25fSSatish Balay         sizes[j] = 1;
94349b5e25fSSatish Balay         i++;
94449b5e25fSSatish Balay       }
94549b5e25fSSatish Balay     }
94649b5e25fSSatish Balay   }
94749b5e25fSSatish Balay   *bs_max = j;
94849b5e25fSSatish Balay   PetscFunctionReturn(0);
94949b5e25fSSatish Balay }
95049b5e25fSSatish Balay 
95149b5e25fSSatish Balay 
95249b5e25fSSatish Balay /* Only add/insert a(i,j) with i<=j (blocks).
95349b5e25fSSatish Balay    Any a(i,j) with i>j input by user is ingored.
95449b5e25fSSatish Balay */
95549b5e25fSSatish Balay 
9564a2ae208SSatish Balay #undef __FUNCT__
9574a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqSBAIJ"
95813f74950SBarry Smith PetscErrorCode MatSetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
95949b5e25fSSatish Balay {
96049b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
9616849ba73SBarry Smith   PetscErrorCode ierr;
962e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,lastcol = -1;
96313f74950SBarry Smith   PetscInt       *imax=a->imax,*ai=a->i,*ailen=a->ilen,roworiented=a->roworiented;
964d0f46423SBarry Smith   PetscInt       *aj  =a->j,nonew=a->nonew,bs=A->rmap->bs,brow,bcol;
96513f74950SBarry Smith   PetscInt       ridx,cidx,bs2=a->bs2;
96649b5e25fSSatish Balay   MatScalar      *ap,value,*aa=a->a,*bap;
96749b5e25fSSatish Balay 
96849b5e25fSSatish Balay   PetscFunctionBegin;
96971fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
97049b5e25fSSatish Balay   for (k=0; k<m; k++) { /* loop over added rows */
97149b5e25fSSatish Balay     row  = im[k];       /* row number */
97249b5e25fSSatish Balay     brow = row/bs;      /* block row number */
97349b5e25fSSatish Balay     if (row < 0) continue;
9742515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
975e32f2f54SBarry Smith     if (row >= A->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->N-1);
97649b5e25fSSatish Balay #endif
97749b5e25fSSatish Balay     rp   = aj + ai[brow]; /*ptr to beginning of column value of the row block*/
97849b5e25fSSatish Balay     ap   = aa + bs2*ai[brow]; /*ptr to beginning of element value of the row block*/
97949b5e25fSSatish Balay     rmax = imax[brow];  /* maximum space allocated for this row */
98049b5e25fSSatish Balay     nrow = ailen[brow]; /* actual length of this row */
98149b5e25fSSatish Balay     low  = 0;
98249b5e25fSSatish Balay 
98349b5e25fSSatish Balay     for (l=0; l<n; l++) { /* loop over added columns */
98449b5e25fSSatish Balay       if (in[l] < 0) continue;
9852515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
986e32f2f54SBarry Smith       if (in[l] >= A->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->rmap->N-1);
98749b5e25fSSatish Balay #endif
98849b5e25fSSatish Balay       col  = in[l];
98949b5e25fSSatish Balay       bcol = col/bs;              /* block col number */
99049b5e25fSSatish Balay 
991941593c8SHong Zhang       if (brow > bcol) {
99226fbe8dcSKarl Rupp         if (a->ignore_ltriangular) continue; /* ignore lower triangular values */
99326fbe8dcSKarl Rupp         else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Lower triangular value cannot be set for sbaij format. Ignoring these values, run with -mat_ignore_lower_triangular or call MatSetOption(mat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE)");
994941593c8SHong Zhang       }
995f4989cb3SHong Zhang 
99649b5e25fSSatish Balay       ridx = row % bs; cidx = col % bs; /*row and col index inside the block */
9978549e402SHong Zhang       if ((brow==bcol && ridx<=cidx) || (brow<bcol)) {
99849b5e25fSSatish Balay         /* element value a(k,l) */
99926fbe8dcSKarl Rupp         if (roworiented) value = v[l + k*n];
100026fbe8dcSKarl Rupp         else value = v[k + l*m];
100149b5e25fSSatish Balay 
100249b5e25fSSatish Balay         /* move pointer bap to a(k,l) quickly and add/insert value */
100326fbe8dcSKarl Rupp         if (col <= lastcol) low = 0;
100426fbe8dcSKarl Rupp         high = nrow;
1005e2ee6c50SBarry Smith         lastcol = col;
100649b5e25fSSatish Balay         while (high-low > 7) {
100749b5e25fSSatish Balay           t = (low+high)/2;
100849b5e25fSSatish Balay           if (rp[t] > bcol) high = t;
100949b5e25fSSatish Balay           else              low  = t;
101049b5e25fSSatish Balay         }
101149b5e25fSSatish Balay         for (i=low; i<high; i++) {
101249b5e25fSSatish Balay           if (rp[i] > bcol) break;
101349b5e25fSSatish Balay           if (rp[i] == bcol) {
101449b5e25fSSatish Balay             bap = ap +  bs2*i + bs*cidx + ridx;
101549b5e25fSSatish Balay             if (is == ADD_VALUES) *bap += value;
101649b5e25fSSatish Balay             else                  *bap  = value;
10178549e402SHong Zhang             /* for diag block, add/insert its symmetric element a(cidx,ridx) */
10188549e402SHong Zhang             if (brow == bcol && ridx < cidx) {
10198549e402SHong Zhang               bap = ap +  bs2*i + bs*ridx + cidx;
10208549e402SHong Zhang               if (is == ADD_VALUES) *bap += value;
10218549e402SHong Zhang               else                  *bap  = value;
10228549e402SHong Zhang             }
102349b5e25fSSatish Balay             goto noinsert1;
102449b5e25fSSatish Balay           }
102549b5e25fSSatish Balay         }
102649b5e25fSSatish Balay 
102749b5e25fSSatish Balay         if (nonew == 1) goto noinsert1;
1028e32f2f54SBarry Smith         if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
1029fef13f97SBarry Smith         MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
103049b5e25fSSatish Balay 
1031c03d1d03SSatish Balay         N = nrow++ - 1; high++;
103249b5e25fSSatish Balay         /* shift up all the later entries in this row */
103349b5e25fSSatish Balay         for (ii=N; ii>=i; ii--) {
103449b5e25fSSatish Balay           rp[ii+1] = rp[ii];
103549b5e25fSSatish Balay           ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
103649b5e25fSSatish Balay         }
103749b5e25fSSatish Balay         if (N>=i) {
103849b5e25fSSatish Balay           ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
103949b5e25fSSatish Balay         }
104049b5e25fSSatish Balay         rp[i]                      = bcol;
104149b5e25fSSatish Balay         ap[bs2*i + bs*cidx + ridx] = value;
104249b5e25fSSatish Balay noinsert1:;
104349b5e25fSSatish Balay         low = i;
10448549e402SHong Zhang       }
104549b5e25fSSatish Balay     }   /* end of loop over added columns */
104649b5e25fSSatish Balay     ailen[brow] = nrow;
104749b5e25fSSatish Balay   }   /* end of loop over added rows */
104849b5e25fSSatish Balay   PetscFunctionReturn(0);
104949b5e25fSSatish Balay }
105049b5e25fSSatish Balay 
10514a2ae208SSatish Balay #undef __FUNCT__
10524d101231SSatish Balay #define __FUNCT__ "MatICCFactor_SeqSBAIJ"
10530481f469SBarry Smith PetscErrorCode MatICCFactor_SeqSBAIJ(Mat inA,IS row,const MatFactorInfo *info)
105449b5e25fSSatish Balay {
10554ccecd49SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)inA->data;
105649b5e25fSSatish Balay   Mat            outA;
1057dfbe8321SBarry Smith   PetscErrorCode ierr;
1058ace3abfcSBarry Smith   PetscBool      row_identity;
105949b5e25fSSatish Balay 
106049b5e25fSSatish Balay   PetscFunctionBegin;
1061e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 is supported for in-place icc");
1062c84f5b01SHong Zhang   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1063e32f2f54SBarry Smith   if (!row_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix reordering is not supported");
1064e32f2f54SBarry Smith   if (inA->rmap->bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix block size %D is not supported",inA->rmap->bs); /* Need to replace MatCholeskyFactorSymbolic_SeqSBAIJ_MSR()! */
1065c84f5b01SHong Zhang 
106649b5e25fSSatish Balay   outA            = inA;
1067d5f3da31SBarry Smith   inA->factortype = MAT_FACTOR_ICC;
106849b5e25fSSatish Balay 
10691a3463dfSHong Zhang   ierr = MatMarkDiagonal_SeqSBAIJ(inA);CHKERRQ(ierr);
1070d595f711SHong Zhang   ierr = MatSeqSBAIJSetNumericFactorization_inplace(inA,row_identity);CHKERRQ(ierr);
107149b5e25fSSatish Balay 
1072c3122656SLisandro Dalcin   ierr   = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
10736bf464f9SBarry Smith   ierr   = ISDestroy(&a->row);CHKERRQ(ierr);
1074c84f5b01SHong Zhang   a->row = row;
1075c3122656SLisandro Dalcin   ierr   = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
10766bf464f9SBarry Smith   ierr   = ISDestroy(&a->col);CHKERRQ(ierr);
1077c84f5b01SHong Zhang   a->col = row;
1078c84f5b01SHong Zhang 
1079c84f5b01SHong Zhang   /* Create the invert permutation so that it can be used in MatCholeskyFactorNumeric() */
1080c84f5b01SHong Zhang   if (a->icol) {ierr = ISInvertPermutation(row,PETSC_DECIDE, &a->icol);CHKERRQ(ierr);}
10813bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr);
108249b5e25fSSatish Balay 
108349b5e25fSSatish Balay   if (!a->solve_work) {
1084d0f46423SBarry Smith     ierr = PetscMalloc((inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
10853bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)inA,(inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar));CHKERRQ(ierr);
108649b5e25fSSatish Balay   }
108749b5e25fSSatish Balay 
1088719d5645SBarry Smith   ierr = MatCholeskyFactorNumeric(outA,inA,info);CHKERRQ(ierr);
108949b5e25fSSatish Balay   PetscFunctionReturn(0);
109049b5e25fSSatish Balay }
1091950f1e5bSHong Zhang 
10924a2ae208SSatish Balay #undef __FUNCT__
10934a2ae208SSatish Balay #define __FUNCT__ "MatSeqSBAIJSetColumnIndices_SeqSBAIJ"
10947087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetColumnIndices_SeqSBAIJ(Mat mat,PetscInt *indices)
109549b5e25fSSatish Balay {
1096045c9aa0SHong Zhang   Mat_SeqSBAIJ   *baij = (Mat_SeqSBAIJ*)mat->data;
109713f74950SBarry Smith   PetscInt       i,nz,n;
10987827cd58SJed Brown   PetscErrorCode ierr;
109949b5e25fSSatish Balay 
110049b5e25fSSatish Balay   PetscFunctionBegin;
11016c6c5352SBarry Smith   nz = baij->maxnz;
1102d0f46423SBarry Smith   n  = mat->cmap->n;
110326fbe8dcSKarl Rupp   for (i=0; i<nz; i++) baij->j[i] = indices[i];
110426fbe8dcSKarl Rupp 
11056c6c5352SBarry Smith   baij->nz = nz;
110626fbe8dcSKarl Rupp   for (i=0; i<n; i++) baij->ilen[i] = baij->imax[i];
110726fbe8dcSKarl Rupp 
11087827cd58SJed Brown   ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
110949b5e25fSSatish Balay   PetscFunctionReturn(0);
111049b5e25fSSatish Balay }
111149b5e25fSSatish Balay 
11124a2ae208SSatish Balay #undef __FUNCT__
11134a2ae208SSatish Balay #define __FUNCT__ "MatSeqSBAIJSetColumnIndices"
111449b5e25fSSatish Balay /*@
111519585528SSatish Balay   MatSeqSBAIJSetColumnIndices - Set the column indices for all the rows
111649b5e25fSSatish Balay   in the matrix.
111749b5e25fSSatish Balay 
111849b5e25fSSatish Balay   Input Parameters:
111919585528SSatish Balay   +  mat     - the SeqSBAIJ matrix
112049b5e25fSSatish Balay   -  indices - the column indices
112149b5e25fSSatish Balay 
112249b5e25fSSatish Balay   Level: advanced
112349b5e25fSSatish Balay 
112449b5e25fSSatish Balay   Notes:
112549b5e25fSSatish Balay   This can be called if you have precomputed the nonzero structure of the
112649b5e25fSSatish Balay   matrix and want to provide it to the matrix object to improve the performance
112749b5e25fSSatish Balay   of the MatSetValues() operation.
112849b5e25fSSatish Balay 
112949b5e25fSSatish Balay   You MUST have set the correct numbers of nonzeros per row in the call to
1130d1be2dadSMatthew Knepley   MatCreateSeqSBAIJ(), and the columns indices MUST be sorted.
113149b5e25fSSatish Balay 
1132ab9f2c04SSatish Balay   MUST be called before any calls to MatSetValues()
113349b5e25fSSatish Balay 
1134ab9f2c04SSatish Balay   .seealso: MatCreateSeqSBAIJ
113549b5e25fSSatish Balay @*/
11367087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetColumnIndices(Mat mat,PetscInt *indices)
113749b5e25fSSatish Balay {
11384ac538c5SBarry Smith   PetscErrorCode ierr;
113949b5e25fSSatish Balay 
114049b5e25fSSatish Balay   PetscFunctionBegin;
11410700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
11424482741eSBarry Smith   PetscValidPointer(indices,2);
11434ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqSBAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr);
114449b5e25fSSatish Balay   PetscFunctionReturn(0);
114549b5e25fSSatish Balay }
114649b5e25fSSatish Balay 
11474a2ae208SSatish Balay #undef __FUNCT__
11483c896bc6SHong Zhang #define __FUNCT__ "MatCopy_SeqSBAIJ"
11493c896bc6SHong Zhang PetscErrorCode MatCopy_SeqSBAIJ(Mat A,Mat B,MatStructure str)
11503c896bc6SHong Zhang {
11513c896bc6SHong Zhang   PetscErrorCode ierr;
11523c896bc6SHong Zhang 
11533c896bc6SHong Zhang   PetscFunctionBegin;
11543c896bc6SHong Zhang   /* If the two matrices have the same copy implementation, use fast copy. */
11553c896bc6SHong Zhang   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
11563c896bc6SHong Zhang     Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
11573c896bc6SHong Zhang     Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ*)B->data;
11583c896bc6SHong Zhang 
1159e7e72b3dSBarry Smith     if (a->i[A->rmap->N] != b->i[B->rmap->N]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
1160d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->N])*sizeof(PetscScalar));CHKERRQ(ierr);
11613c896bc6SHong Zhang   } else {
1162f5edf698SHong Zhang     ierr = MatGetRowUpperTriangular(A);CHKERRQ(ierr);
11633c896bc6SHong Zhang     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1164f5edf698SHong Zhang     ierr = MatRestoreRowUpperTriangular(A);CHKERRQ(ierr);
11653c896bc6SHong Zhang   }
11663c896bc6SHong Zhang   PetscFunctionReturn(0);
11673c896bc6SHong Zhang }
11683c896bc6SHong Zhang 
11693c896bc6SHong Zhang #undef __FUNCT__
11704994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqSBAIJ"
11714994cf47SJed Brown PetscErrorCode MatSetUp_SeqSBAIJ(Mat A)
1172273d9f13SBarry Smith {
1173dfbe8321SBarry Smith   PetscErrorCode ierr;
1174273d9f13SBarry Smith 
1175273d9f13SBarry Smith   PetscFunctionBegin;
1176535b19f3SBarry Smith   ierr =  MatSeqSBAIJSetPreallocation_SeqSBAIJ(A,A->rmap->bs,PETSC_DEFAULT,0);CHKERRQ(ierr);
1177273d9f13SBarry Smith   PetscFunctionReturn(0);
1178273d9f13SBarry Smith }
1179273d9f13SBarry Smith 
1180a6ece127SHong Zhang #undef __FUNCT__
11818c778c55SBarry Smith #define __FUNCT__ "MatSeqSBAIJGetArray_SeqSBAIJ"
11828c778c55SBarry Smith PetscErrorCode MatSeqSBAIJGetArray_SeqSBAIJ(Mat A,PetscScalar *array[])
1183a6ece127SHong Zhang {
1184a6ece127SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
11855fd66863SKarl Rupp 
1186a6ece127SHong Zhang   PetscFunctionBegin;
1187a6ece127SHong Zhang   *array = a->a;
1188a6ece127SHong Zhang   PetscFunctionReturn(0);
1189a6ece127SHong Zhang }
1190a6ece127SHong Zhang 
1191a6ece127SHong Zhang #undef __FUNCT__
11928c778c55SBarry Smith #define __FUNCT__ "MatSeqSBAIJRestoreArray_SeqSBAIJ"
11938c778c55SBarry Smith PetscErrorCode MatSeqSBAIJRestoreArray_SeqSBAIJ(Mat A,PetscScalar *array[])
1194a6ece127SHong Zhang {
1195a6ece127SHong Zhang   PetscFunctionBegin;
1196a6ece127SHong Zhang   PetscFunctionReturn(0);
1197a6ece127SHong Zhang }
1198a6ece127SHong Zhang 
119942ee4b1aSHong Zhang #undef __FUNCT__
120042ee4b1aSHong Zhang #define __FUNCT__ "MatAXPY_SeqSBAIJ"
1201f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqSBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
120242ee4b1aSHong Zhang {
120342ee4b1aSHong Zhang   Mat_SeqSBAIJ   *x=(Mat_SeqSBAIJ*)X->data, *y=(Mat_SeqSBAIJ*)Y->data;
1204dfbe8321SBarry Smith   PetscErrorCode ierr;
1205e838b9e7SJed Brown   PetscInt       i,bs=Y->rmap->bs,bs2=bs*bs,j;
1206e838b9e7SJed Brown   PetscBLASInt   one = 1;
120742ee4b1aSHong Zhang 
120842ee4b1aSHong Zhang   PetscFunctionBegin;
120942ee4b1aSHong Zhang   if (str == SAME_NONZERO_PATTERN) {
1210f4df32b1SMatthew Knepley     PetscScalar  alpha = a;
1211c5df96a5SBarry Smith     PetscBLASInt bnz;
1212c5df96a5SBarry Smith     ierr = PetscBLASIntCast(x->nz*bs2,&bnz);CHKERRQ(ierr);
12138b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
1214c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
1215c4319e64SHong Zhang     if (y->xtoy && y->XtoY != X) {
1216c4319e64SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
12176bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
1218c537a176SHong Zhang     }
1219c4319e64SHong Zhang     if (!y->xtoy) { /* get xtoy */
12200298fd71SBarry Smith       ierr    = MatAXPYGetxtoy_Private(x->mbs,x->i,x->j,NULL, y->i,y->j,NULL, &y->xtoy);CHKERRQ(ierr);
1221c4319e64SHong Zhang       y->XtoY = X;
1222c537a176SHong Zhang     }
12236c6c5352SBarry Smith     for (i=0; i<x->nz; i++) {
1224c4319e64SHong Zhang       j = 0;
1225c4319e64SHong Zhang       while (j < bs2) {
1226f4df32b1SMatthew Knepley         y->a[bs2*y->xtoy[i]+j] += a*(x->a[bs2*i+j]);
1227c4319e64SHong Zhang         j++;
1228c537a176SHong Zhang       }
1229c4319e64SHong Zhang     }
12301e2582c4SBarry Smith     ierr = PetscInfo3(Y,"ratio of nnz_s(X)/nnz_s(Y): %D/%D = %G\n",bs2*x->nz,bs2*y->nz,(PetscReal)(bs2*x->nz)/(bs2*y->nz));CHKERRQ(ierr);
123142ee4b1aSHong Zhang   } else {
1232f5edf698SHong Zhang     ierr = MatGetRowUpperTriangular(X);CHKERRQ(ierr);
1233f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
1234f5edf698SHong Zhang     ierr = MatRestoreRowUpperTriangular(X);CHKERRQ(ierr);
123542ee4b1aSHong Zhang   }
123642ee4b1aSHong Zhang   PetscFunctionReturn(0);
123742ee4b1aSHong Zhang }
123842ee4b1aSHong Zhang 
1239efcf0fc3SBarry Smith #undef __FUNCT__
1240efcf0fc3SBarry Smith #define __FUNCT__ "MatIsSymmetric_SeqSBAIJ"
1241ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqSBAIJ(Mat A,PetscReal tol,PetscBool  *flg)
1242efcf0fc3SBarry Smith {
1243efcf0fc3SBarry Smith   PetscFunctionBegin;
1244efcf0fc3SBarry Smith   *flg = PETSC_TRUE;
1245efcf0fc3SBarry Smith   PetscFunctionReturn(0);
1246efcf0fc3SBarry Smith }
1247efcf0fc3SBarry Smith 
1248efcf0fc3SBarry Smith #undef __FUNCT__
1249efcf0fc3SBarry Smith #define __FUNCT__ "MatIsStructurallySymmetric_SeqSBAIJ"
1250ace3abfcSBarry Smith PetscErrorCode MatIsStructurallySymmetric_SeqSBAIJ(Mat A,PetscBool  *flg)
1251efcf0fc3SBarry Smith {
1252efcf0fc3SBarry Smith   PetscFunctionBegin;
1253efcf0fc3SBarry Smith   *flg = PETSC_TRUE;
1254efcf0fc3SBarry Smith   PetscFunctionReturn(0);
1255efcf0fc3SBarry Smith }
1256efcf0fc3SBarry Smith 
1257efcf0fc3SBarry Smith #undef __FUNCT__
1258efcf0fc3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqSBAIJ"
1259ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqSBAIJ(Mat A,PetscReal tol,PetscBool  *flg)
1260efcf0fc3SBarry Smith {
1261efcf0fc3SBarry Smith   PetscFunctionBegin;
1262efcf0fc3SBarry Smith   *flg = PETSC_FALSE;
1263efcf0fc3SBarry Smith   PetscFunctionReturn(0);
1264efcf0fc3SBarry Smith }
1265efcf0fc3SBarry Smith 
126699cafbc1SBarry Smith #undef __FUNCT__
126799cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqSBAIJ"
126899cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqSBAIJ(Mat A)
126999cafbc1SBarry Smith {
127099cafbc1SBarry Smith   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
127199cafbc1SBarry Smith   PetscInt     i,nz = a->bs2*a->i[a->mbs];
1272dd6ea824SBarry Smith   MatScalar    *aa = a->a;
127399cafbc1SBarry Smith 
127499cafbc1SBarry Smith   PetscFunctionBegin;
127599cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
127699cafbc1SBarry Smith   PetscFunctionReturn(0);
127799cafbc1SBarry Smith }
127899cafbc1SBarry Smith 
127999cafbc1SBarry Smith #undef __FUNCT__
128099cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqSBAIJ"
128199cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqSBAIJ(Mat A)
128299cafbc1SBarry Smith {
128399cafbc1SBarry Smith   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
128499cafbc1SBarry Smith   PetscInt     i,nz = a->bs2*a->i[a->mbs];
1285dd6ea824SBarry Smith   MatScalar    *aa = a->a;
128699cafbc1SBarry Smith 
128799cafbc1SBarry Smith   PetscFunctionBegin;
128899cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
128999cafbc1SBarry Smith   PetscFunctionReturn(0);
129099cafbc1SBarry Smith }
129199cafbc1SBarry Smith 
12923bededecSBarry Smith #undef __FUNCT__
12933bededecSBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqSBAIJ"
12943bededecSBarry Smith PetscErrorCode MatZeroRowsColumns_SeqSBAIJ(Mat A,PetscInt is_n,const PetscInt is_idx[],PetscScalar diag,Vec x, Vec b)
12953bededecSBarry Smith {
12963bededecSBarry Smith   Mat_SeqSBAIJ      *baij=(Mat_SeqSBAIJ*)A->data;
12973bededecSBarry Smith   PetscErrorCode    ierr;
12983bededecSBarry Smith   PetscInt          i,j,k,count;
12993bededecSBarry Smith   PetscInt          bs   =A->rmap->bs,bs2=baij->bs2,row,col;
13003bededecSBarry Smith   PetscScalar       zero = 0.0;
13013bededecSBarry Smith   MatScalar         *aa;
13023bededecSBarry Smith   const PetscScalar *xx;
13033bededecSBarry Smith   PetscScalar       *bb;
130456777dd2SBarry Smith   PetscBool         *zeroed,vecs = PETSC_FALSE;
13053bededecSBarry Smith 
13063bededecSBarry Smith   PetscFunctionBegin;
13073bededecSBarry Smith   /* fix right hand side if needed */
13083bededecSBarry Smith   if (x && b) {
13093bededecSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
13103bededecSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
131156777dd2SBarry Smith     vecs = PETSC_TRUE;
13123bededecSBarry Smith   }
13133bededecSBarry Smith   A->same_nonzero = PETSC_TRUE;
13143bededecSBarry Smith 
13153bededecSBarry Smith   /* zero the columns */
13163bededecSBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
13173bededecSBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
13183bededecSBarry Smith   for (i=0; i<is_n; i++) {
13193bededecSBarry Smith     if (is_idx[i] < 0 || is_idx[i] >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range",is_idx[i]);
13203bededecSBarry Smith     zeroed[is_idx[i]] = PETSC_TRUE;
13213bededecSBarry Smith   }
132256777dd2SBarry Smith   if (vecs) {
132356777dd2SBarry Smith     for (i=0; i<A->rmap->N; i++) {
132456777dd2SBarry Smith       row = i/bs;
132556777dd2SBarry Smith       for (j=baij->i[row]; j<baij->i[row+1]; j++) {
132656777dd2SBarry Smith         for (k=0; k<bs; k++) {
132756777dd2SBarry Smith           col = bs*baij->j[j] + k;
132856777dd2SBarry Smith           if (col <= i) continue;
132956777dd2SBarry Smith           aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
133026fbe8dcSKarl Rupp           if (!zeroed[i] && zeroed[col]) bb[i]   -= aa[0]*xx[col];
133126fbe8dcSKarl Rupp           if (zeroed[i] && !zeroed[col]) bb[col] -= aa[0]*xx[i];
133256777dd2SBarry Smith         }
133356777dd2SBarry Smith       }
133456777dd2SBarry Smith     }
133526fbe8dcSKarl Rupp     for (i=0; i<is_n; i++) bb[is_idx[i]] = diag*xx[is_idx[i]];
133656777dd2SBarry Smith   }
133756777dd2SBarry Smith 
13383bededecSBarry Smith   for (i=0; i<A->rmap->N; i++) {
13393bededecSBarry Smith     if (!zeroed[i]) {
13403bededecSBarry Smith       row = i/bs;
13413bededecSBarry Smith       for (j=baij->i[row]; j<baij->i[row+1]; j++) {
13423bededecSBarry Smith         for (k=0; k<bs; k++) {
13433bededecSBarry Smith           col = bs*baij->j[j] + k;
13443bededecSBarry Smith           if (zeroed[col]) {
13453bededecSBarry Smith             aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
13463bededecSBarry Smith             aa[0] = 0.0;
13473bededecSBarry Smith           }
13483bededecSBarry Smith         }
13493bededecSBarry Smith       }
13503bededecSBarry Smith     }
13513bededecSBarry Smith   }
13523bededecSBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
135356777dd2SBarry Smith   if (vecs) {
135456777dd2SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
135556777dd2SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
135656777dd2SBarry Smith   }
13573bededecSBarry Smith 
13583bededecSBarry Smith   /* zero the rows */
13593bededecSBarry Smith   for (i=0; i<is_n; i++) {
13603bededecSBarry Smith     row   = is_idx[i];
13613bededecSBarry Smith     count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
13623bededecSBarry Smith     aa    = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
13633bededecSBarry Smith     for (k=0; k<count; k++) {
13643bededecSBarry Smith       aa[0] =  zero;
13653bededecSBarry Smith       aa   += bs;
13663bededecSBarry Smith     }
13673bededecSBarry Smith     if (diag != 0.0) {
13683bededecSBarry Smith       ierr = (*A->ops->setvalues)(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr);
13693bededecSBarry Smith     }
13703bededecSBarry Smith   }
13713bededecSBarry Smith   ierr = MatAssemblyEnd_SeqSBAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13723bededecSBarry Smith   PetscFunctionReturn(0);
13733bededecSBarry Smith }
13743bededecSBarry Smith 
137549b5e25fSSatish Balay /* -------------------------------------------------------------------*/
13763964eb88SJed Brown static struct _MatOps MatOps_Values = {MatSetValues_SeqSBAIJ,
137749b5e25fSSatish Balay                                        MatGetRow_SeqSBAIJ,
137849b5e25fSSatish Balay                                        MatRestoreRow_SeqSBAIJ,
137949b5e25fSSatish Balay                                        MatMult_SeqSBAIJ_N,
138097304618SKris Buschelman                                /*  4*/ MatMultAdd_SeqSBAIJ_N,
1381431c96f7SBarry Smith                                        MatMult_SeqSBAIJ_N,       /* transpose versions are same as non-transpose versions */
1382e005ede5SBarry Smith                                        MatMultAdd_SeqSBAIJ_N,
1383db4efbfdSBarry Smith                                        0,
138449b5e25fSSatish Balay                                        0,
138549b5e25fSSatish Balay                                        0,
138697304618SKris Buschelman                                /* 10*/ 0,
138749b5e25fSSatish Balay                                        0,
1388c078aec8SLisandro Dalcin                                        MatCholeskyFactor_SeqSBAIJ,
138941f059aeSBarry Smith                                        MatSOR_SeqSBAIJ,
139049b5e25fSSatish Balay                                        MatTranspose_SeqSBAIJ,
139197304618SKris Buschelman                                /* 15*/ MatGetInfo_SeqSBAIJ,
139249b5e25fSSatish Balay                                        MatEqual_SeqSBAIJ,
139349b5e25fSSatish Balay                                        MatGetDiagonal_SeqSBAIJ,
139449b5e25fSSatish Balay                                        MatDiagonalScale_SeqSBAIJ,
139549b5e25fSSatish Balay                                        MatNorm_SeqSBAIJ,
139697304618SKris Buschelman                                /* 20*/ 0,
139749b5e25fSSatish Balay                                        MatAssemblyEnd_SeqSBAIJ,
139849b5e25fSSatish Balay                                        MatSetOption_SeqSBAIJ,
139949b5e25fSSatish Balay                                        MatZeroEntries_SeqSBAIJ,
1400d519adbfSMatthew Knepley                                /* 24*/ 0,
140149b5e25fSSatish Balay                                        0,
140249b5e25fSSatish Balay                                        0,
1403db4efbfdSBarry Smith                                        0,
1404db4efbfdSBarry Smith                                        0,
14054994cf47SJed Brown                                /* 29*/ MatSetUp_SeqSBAIJ,
1406c464158bSHong Zhang                                        0,
1407db4efbfdSBarry Smith                                        0,
14088c778c55SBarry Smith                                        0,
14098c778c55SBarry Smith                                        0,
1410d519adbfSMatthew Knepley                                /* 34*/ MatDuplicate_SeqSBAIJ,
1411719d5645SBarry Smith                                        0,
1412719d5645SBarry Smith                                        0,
141349b5e25fSSatish Balay                                        0,
1414c84f5b01SHong Zhang                                        MatICCFactor_SeqSBAIJ,
1415d519adbfSMatthew Knepley                                /* 39*/ MatAXPY_SeqSBAIJ,
141649b5e25fSSatish Balay                                        MatGetSubMatrices_SeqSBAIJ,
141749b5e25fSSatish Balay                                        MatIncreaseOverlap_SeqSBAIJ,
141849b5e25fSSatish Balay                                        MatGetValues_SeqSBAIJ,
14193c896bc6SHong Zhang                                        MatCopy_SeqSBAIJ,
1420d519adbfSMatthew Knepley                                /* 44*/ 0,
142149b5e25fSSatish Balay                                        MatScale_SeqSBAIJ,
142249b5e25fSSatish Balay                                        0,
142349b5e25fSSatish Balay                                        0,
14243bededecSBarry Smith                                        MatZeroRowsColumns_SeqSBAIJ,
1425f73d5cc4SBarry Smith                                /* 49*/ 0,
142649b5e25fSSatish Balay                                        MatGetRowIJ_SeqSBAIJ,
142749b5e25fSSatish Balay                                        MatRestoreRowIJ_SeqSBAIJ,
142849b5e25fSSatish Balay                                        0,
142949b5e25fSSatish Balay                                        0,
1430d519adbfSMatthew Knepley                                /* 54*/ 0,
143149b5e25fSSatish Balay                                        0,
143249b5e25fSSatish Balay                                        0,
143349b5e25fSSatish Balay                                        0,
143449b5e25fSSatish Balay                                        MatSetValuesBlocked_SeqSBAIJ,
1435d519adbfSMatthew Knepley                                /* 59*/ MatGetSubMatrix_SeqSBAIJ,
143649b5e25fSSatish Balay                                        0,
143749b5e25fSSatish Balay                                        0,
1438357abbc8SBarry Smith                                        0,
1439d959ec07SHong Zhang                                        0,
1440d519adbfSMatthew Knepley                                /* 64*/ 0,
1441d959ec07SHong Zhang                                        0,
1442d959ec07SHong Zhang                                        0,
1443d959ec07SHong Zhang                                        0,
1444d959ec07SHong Zhang                                        0,
1445d519adbfSMatthew Knepley                                /* 69*/ MatGetRowMaxAbs_SeqSBAIJ,
14463e0d88b5SBarry Smith                                        0,
14473e0d88b5SBarry Smith                                        0,
14483e0d88b5SBarry Smith                                        0,
14493e0d88b5SBarry Smith                                        0,
1450d519adbfSMatthew Knepley                                /* 74*/ 0,
14513e0d88b5SBarry Smith                                        0,
14523e0d88b5SBarry Smith                                        0,
14533e0d88b5SBarry Smith                                        0,
14543e0d88b5SBarry Smith                                        0,
1455d519adbfSMatthew Knepley                                /* 79*/ 0,
14563e0d88b5SBarry Smith                                        0,
14573e0d88b5SBarry Smith                                        0,
145897304618SKris Buschelman                                        MatGetInertia_SeqSBAIJ,
14595bba2384SShri Abhyankar                                        MatLoad_SeqSBAIJ,
1460d519adbfSMatthew Knepley                                /* 84*/ MatIsSymmetric_SeqSBAIJ,
1461865e5f61SKris Buschelman                                        MatIsHermitian_SeqSBAIJ,
1462efcf0fc3SBarry Smith                                        MatIsStructurallySymmetric_SeqSBAIJ,
1463865e5f61SKris Buschelman                                        0,
1464865e5f61SKris Buschelman                                        0,
1465d519adbfSMatthew Knepley                                /* 89*/ 0,
1466865e5f61SKris Buschelman                                        0,
1467865e5f61SKris Buschelman                                        0,
1468865e5f61SKris Buschelman                                        0,
1469865e5f61SKris Buschelman                                        0,
1470d519adbfSMatthew Knepley                                /* 94*/ 0,
1471865e5f61SKris Buschelman                                        0,
1472865e5f61SKris Buschelman                                        0,
147399cafbc1SBarry Smith                                        0,
147499cafbc1SBarry Smith                                        0,
1475d519adbfSMatthew Knepley                                /* 99*/ 0,
147699cafbc1SBarry Smith                                        0,
147799cafbc1SBarry Smith                                        0,
147899cafbc1SBarry Smith                                        0,
147999cafbc1SBarry Smith                                        0,
1480d519adbfSMatthew Knepley                                /*104*/ 0,
148199cafbc1SBarry Smith                                        MatRealPart_SeqSBAIJ,
1482f5edf698SHong Zhang                                        MatImaginaryPart_SeqSBAIJ,
1483f5edf698SHong Zhang                                        MatGetRowUpperTriangular_SeqSBAIJ,
14842af78befSBarry Smith                                        MatRestoreRowUpperTriangular_SeqSBAIJ,
1485d519adbfSMatthew Knepley                                /*109*/ 0,
14862af78befSBarry Smith                                        0,
14872af78befSBarry Smith                                        0,
14882af78befSBarry Smith                                        0,
1489547795f9SHong Zhang                                        MatMissingDiagonal_SeqSBAIJ,
1490547795f9SHong Zhang                                /*114*/ 0,
1491547795f9SHong Zhang                                        0,
1492547795f9SHong Zhang                                        0,
1493547795f9SHong Zhang                                        0,
1494547795f9SHong Zhang                                        0,
1495547795f9SHong Zhang                                /*119*/ 0,
1496547795f9SHong Zhang                                        0,
14972f480046SShri Abhyankar                                        0,
14983964eb88SJed Brown                                        0,
14993964eb88SJed Brown                                        0,
15003964eb88SJed Brown                                /*124*/ 0,
15013964eb88SJed Brown                                        0,
15023964eb88SJed Brown                                        0,
15033964eb88SJed Brown                                        0,
15043964eb88SJed Brown                                        0,
15053964eb88SJed Brown                                /*129*/ 0,
15063964eb88SJed Brown                                        0,
15073964eb88SJed Brown                                        0,
15083964eb88SJed Brown                                        0,
15093964eb88SJed Brown                                        0,
15103964eb88SJed Brown                                /*134*/ 0,
15113964eb88SJed Brown                                        0,
15123964eb88SJed Brown                                        0,
15133964eb88SJed Brown                                        0,
15143964eb88SJed Brown                                        0,
15153964eb88SJed Brown                                /*139*/ 0,
1516*f9426fe0SMark Adams                                        0,
15175bba2384SShri Abhyankar                                        0
151899cafbc1SBarry Smith };
1519be1d678aSKris Buschelman 
15204a2ae208SSatish Balay #undef __FUNCT__
15214a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqSBAIJ"
15227087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqSBAIJ(Mat mat)
152349b5e25fSSatish Balay {
15244afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ*)mat->data;
1525d0f46423SBarry Smith   PetscInt       nz   = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
1526dfbe8321SBarry Smith   PetscErrorCode ierr;
152749b5e25fSSatish Balay 
152849b5e25fSSatish Balay   PetscFunctionBegin;
1529e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
153049b5e25fSSatish Balay 
153149b5e25fSSatish Balay   /* allocate space for values if not already there */
153249b5e25fSSatish Balay   if (!aij->saved_values) {
153387828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
153449b5e25fSSatish Balay   }
153549b5e25fSSatish Balay 
153649b5e25fSSatish Balay   /* copy values over */
153787828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
153849b5e25fSSatish Balay   PetscFunctionReturn(0);
153949b5e25fSSatish Balay }
154049b5e25fSSatish Balay 
15414a2ae208SSatish Balay #undef __FUNCT__
15424a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqSBAIJ"
15437087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqSBAIJ(Mat mat)
154449b5e25fSSatish Balay {
15454afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ*)mat->data;
15466849ba73SBarry Smith   PetscErrorCode ierr;
1547d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
154849b5e25fSSatish Balay 
154949b5e25fSSatish Balay   PetscFunctionBegin;
1550e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
1551e7e72b3dSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
155249b5e25fSSatish Balay 
155349b5e25fSSatish Balay   /* copy values over */
155487828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
155549b5e25fSSatish Balay   PetscFunctionReturn(0);
155649b5e25fSSatish Balay }
155749b5e25fSSatish Balay 
15584a2ae208SSatish Balay #undef __FUNCT__
1559a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation_SeqSBAIJ"
15607087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetPreallocation_SeqSBAIJ(Mat B,PetscInt bs,PetscInt nz,PetscInt *nnz)
156149b5e25fSSatish Balay {
1562c464158bSHong Zhang   Mat_SeqSBAIJ   *b = (Mat_SeqSBAIJ*)B->data;
15636849ba73SBarry Smith   PetscErrorCode ierr;
1564535b19f3SBarry Smith   PetscInt       i,mbs,bs2;
15652576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,flg = PETSC_FALSE,realalloc = PETSC_FALSE;
156649b5e25fSSatish Balay 
156749b5e25fSSatish Balay   PetscFunctionBegin;
15682576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
1569273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
1570db4efbfdSBarry Smith 
157126283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
157226283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
157326283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
157426283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
1575e02043d6SBarry Smith   ierr = PetscLayoutGetBlockSize(B->rmap,&bs);CHKERRQ(ierr);
1576899cda47SBarry Smith 
1577d0f46423SBarry Smith   mbs = B->rmap->N/bs;
157849b5e25fSSatish Balay   bs2 = bs*bs;
157949b5e25fSSatish Balay 
1580e7e72b3dSBarry Smith   if (mbs*bs != B->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number rows, cols must be divisible by blocksize");
158149b5e25fSSatish Balay 
1582ab93d7beSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
1583ab93d7beSBarry Smith     skipallocation = PETSC_TRUE;
1584ab93d7beSBarry Smith     nz             = 0;
1585ab93d7beSBarry Smith   }
1586ab93d7beSBarry Smith 
1587435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 3;
1588e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
158949b5e25fSSatish Balay   if (nnz) {
159049b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
1591e32f2f54SBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %D value %D",i,nnz[i]);
1592e32f2f54SBarry Smith       if (nnz[i] > mbs) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than block row length: local row %D value %D rowlength %D",i,nnz[i],mbs);
159349b5e25fSSatish Balay     }
159449b5e25fSSatish Balay   }
159549b5e25fSSatish Balay 
1596db4efbfdSBarry Smith   B->ops->mult             = MatMult_SeqSBAIJ_N;
1597db4efbfdSBarry Smith   B->ops->multadd          = MatMultAdd_SeqSBAIJ_N;
1598db4efbfdSBarry Smith   B->ops->multtranspose    = MatMult_SeqSBAIJ_N;
1599db4efbfdSBarry Smith   B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_N;
160026fbe8dcSKarl Rupp 
16010298fd71SBarry Smith   ierr  = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,NULL);CHKERRQ(ierr);
160249b5e25fSSatish Balay   if (!flg) {
160349b5e25fSSatish Balay     switch (bs) {
160449b5e25fSSatish Balay     case 1:
160549b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_1;
160649b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_1;
1607431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_1;
1608431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_1;
160949b5e25fSSatish Balay       break;
161049b5e25fSSatish Balay     case 2:
161149b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_2;
161249b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_2;
1613431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_2;
1614431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_2;
161549b5e25fSSatish Balay       break;
161649b5e25fSSatish Balay     case 3:
161749b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_3;
161849b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_3;
1619431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_3;
1620431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_3;
162149b5e25fSSatish Balay       break;
162249b5e25fSSatish Balay     case 4:
162349b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_4;
162449b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_4;
1625431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_4;
1626431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_4;
162749b5e25fSSatish Balay       break;
162849b5e25fSSatish Balay     case 5:
162949b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_5;
163049b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_5;
1631431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_5;
1632431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_5;
163349b5e25fSSatish Balay       break;
163449b5e25fSSatish Balay     case 6:
163549b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_6;
163649b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_6;
1637431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_6;
1638431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_6;
163949b5e25fSSatish Balay       break;
164049b5e25fSSatish Balay     case 7:
1641de53e5efSHong Zhang       B->ops->mult             = MatMult_SeqSBAIJ_7;
164249b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_7;
1643431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_7;
1644431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_7;
164549b5e25fSSatish Balay       break;
164649b5e25fSSatish Balay     }
164749b5e25fSSatish Balay   }
164849b5e25fSSatish Balay 
164949b5e25fSSatish Balay   b->mbs = mbs;
16504afc71dfSHong Zhang   b->nbs = mbs;
1651ab93d7beSBarry Smith   if (!skipallocation) {
16522ee49352SLisandro Dalcin     if (!b->imax) {
1653ab93d7beSBarry Smith       ierr = PetscMalloc2(mbs,PetscInt,&b->imax,mbs,PetscInt,&b->ilen);CHKERRQ(ierr);
165426fbe8dcSKarl Rupp 
1655c760cd28SBarry Smith       b->free_imax_ilen = PETSC_TRUE;
165626fbe8dcSKarl Rupp 
16573bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,2*mbs*sizeof(PetscInt));CHKERRQ(ierr);
16582ee49352SLisandro Dalcin     }
165949b5e25fSSatish Balay     if (!nnz) {
1660435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
166149b5e25fSSatish Balay       else if (nz <= 0) nz = 1;
166226fbe8dcSKarl Rupp       for (i=0; i<mbs; i++) b->imax[i] = nz;
1663153ea458SHong Zhang       nz = nz*mbs; /* total nz */
166449b5e25fSSatish Balay     } else {
166549b5e25fSSatish Balay       nz = 0;
16668cef66ccSHong Zhang       for (i=0; i<mbs; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
166749b5e25fSSatish Balay     }
16682ee49352SLisandro Dalcin     /* b->ilen will count nonzeros in each block row so far. */
166926fbe8dcSKarl Rupp     for (i=0; i<mbs; i++) b->ilen[i] = 0;
16706c6c5352SBarry Smith     /* nz=(nz+mbs)/2; */ /* total diagonal and superdiagonal nonzero blocks */
167149b5e25fSSatish Balay 
167249b5e25fSSatish Balay     /* allocate the matrix space */
16732ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
1674d0f46423SBarry Smith     ierr = PetscMalloc3(bs2*nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->N+1,PetscInt,&b->i);CHKERRQ(ierr);
16753bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)B,(B->rmap->N+1)*sizeof(PetscInt)+nz*(bs2*sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
16766c6c5352SBarry Smith     ierr = PetscMemzero(b->a,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
167713f74950SBarry Smith     ierr = PetscMemzero(b->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
167826fbe8dcSKarl Rupp 
167949b5e25fSSatish Balay     b->singlemalloc = PETSC_TRUE;
168049b5e25fSSatish Balay 
168149b5e25fSSatish Balay     /* pointer to beginning of each row */
1682e60cf9a0SBarry Smith     b->i[0] = 0;
168326fbe8dcSKarl Rupp     for (i=1; i<mbs+1; i++) b->i[i] = b->i[i-1] + b->imax[i-1];
168426fbe8dcSKarl Rupp 
1685e6b907acSBarry Smith     b->free_a  = PETSC_TRUE;
1686e6b907acSBarry Smith     b->free_ij = PETSC_TRUE;
1687e811da20SHong Zhang   } else {
1688e6b907acSBarry Smith     b->free_a  = PETSC_FALSE;
1689e6b907acSBarry Smith     b->free_ij = PETSC_FALSE;
1690ab93d7beSBarry Smith   }
169149b5e25fSSatish Balay 
1692d0f46423SBarry Smith   B->rmap->bs = bs;
169349b5e25fSSatish Balay   b->bs2      = bs2;
16946c6c5352SBarry Smith   b->nz       = 0;
1695b32cb4a7SJed Brown   b->maxnz    = nz;
1696153ea458SHong Zhang 
169716cdd363SHong Zhang   b->inew    = 0;
169816cdd363SHong Zhang   b->jnew    = 0;
169916cdd363SHong Zhang   b->anew    = 0;
170016cdd363SHong Zhang   b->a2anew  = 0;
17011a3463dfSHong Zhang   b->permute = PETSC_FALSE;
17022576faa2SJed Brown   if (realalloc) {ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);}
1703c464158bSHong Zhang   PetscFunctionReturn(0);
1704c464158bSHong Zhang }
1705153ea458SHong Zhang 
1706db4efbfdSBarry Smith /*
1707db4efbfdSBarry Smith    This is used to set the numeric factorization for both Cholesky and ICC symbolic factorization
1708db4efbfdSBarry Smith */
17098b1456e3SHong Zhang #undef __FUNCT__
1710d595f711SHong Zhang #define __FUNCT__ "MatSeqSBAIJSetNumericFactorization_inplace"
1711ace3abfcSBarry Smith PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat B,PetscBool natural)
1712db4efbfdSBarry Smith {
1713db4efbfdSBarry Smith   PetscErrorCode ierr;
1714ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
1715db4efbfdSBarry Smith   PetscInt       bs  = B->rmap->bs;
1716db4efbfdSBarry Smith 
1717db4efbfdSBarry Smith   PetscFunctionBegin;
17180298fd71SBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,NULL);CHKERRQ(ierr);
1719db4efbfdSBarry Smith   if (flg) bs = 8;
1720db4efbfdSBarry Smith 
1721db4efbfdSBarry Smith   if (!natural) {
1722db4efbfdSBarry Smith     switch (bs) {
1723db4efbfdSBarry Smith     case 1:
1724d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_inplace;
1725db4efbfdSBarry Smith       break;
1726db4efbfdSBarry Smith     case 2:
1727db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2;
1728db4efbfdSBarry Smith       break;
1729db4efbfdSBarry Smith     case 3:
1730db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3;
1731db4efbfdSBarry Smith       break;
1732db4efbfdSBarry Smith     case 4:
1733db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4;
1734db4efbfdSBarry Smith       break;
1735db4efbfdSBarry Smith     case 5:
1736db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5;
1737db4efbfdSBarry Smith       break;
1738db4efbfdSBarry Smith     case 6:
1739db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6;
1740db4efbfdSBarry Smith       break;
1741db4efbfdSBarry Smith     case 7:
1742db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7;
1743db4efbfdSBarry Smith       break;
1744db4efbfdSBarry Smith     default:
1745db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N;
1746db4efbfdSBarry Smith       break;
1747db4efbfdSBarry Smith     }
1748db4efbfdSBarry Smith   } else {
1749db4efbfdSBarry Smith     switch (bs) {
1750db4efbfdSBarry Smith     case 1:
1751d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering_inplace;
1752db4efbfdSBarry Smith       break;
1753db4efbfdSBarry Smith     case 2:
1754db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering;
1755db4efbfdSBarry Smith       break;
1756db4efbfdSBarry Smith     case 3:
1757db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3_NaturalOrdering;
1758db4efbfdSBarry Smith       break;
1759db4efbfdSBarry Smith     case 4:
1760db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4_NaturalOrdering;
1761db4efbfdSBarry Smith       break;
1762db4efbfdSBarry Smith     case 5:
1763db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5_NaturalOrdering;
1764db4efbfdSBarry Smith       break;
1765db4efbfdSBarry Smith     case 6:
1766db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6_NaturalOrdering;
1767db4efbfdSBarry Smith       break;
1768db4efbfdSBarry Smith     case 7:
1769db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7_NaturalOrdering;
1770db4efbfdSBarry Smith       break;
1771db4efbfdSBarry Smith     default:
1772db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering;
1773db4efbfdSBarry Smith       break;
1774db4efbfdSBarry Smith     }
1775db4efbfdSBarry Smith   }
1776db4efbfdSBarry Smith   PetscFunctionReturn(0);
1777db4efbfdSBarry Smith }
1778db4efbfdSBarry Smith 
17798cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqAIJ(Mat, MatType,MatReuse,Mat*);
17808cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqBAIJ(Mat, MatType,MatReuse,Mat*);
1781d769727bSBarry Smith 
17825c9eb25fSBarry Smith #undef __FUNCT__
17835c9eb25fSBarry Smith #define __FUNCT__ "MatGetFactor_seqsbaij_petsc"
17848cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqsbaij_petsc(Mat A,MatFactorType ftype,Mat *B)
17855c9eb25fSBarry Smith {
1786d0f46423SBarry Smith   PetscInt       n = A->rmap->n;
17875c9eb25fSBarry Smith   PetscErrorCode ierr;
17885c9eb25fSBarry Smith 
17895c9eb25fSBarry Smith   PetscFunctionBegin;
17900e92d65fSHong Zhang #if defined(PETSC_USE_COMPLEX)
17910e92d65fSHong Zhang   if (A->hermitian) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Hermitian Factor is not supported");
17920e92d65fSHong Zhang #endif
1793ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
17945c9eb25fSBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
17955c9eb25fSBarry Smith   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
17965c9eb25fSBarry Smith     ierr = MatSetType(*B,MATSEQSBAIJ);CHKERRQ(ierr);
17970298fd71SBarry Smith     ierr = MatSeqSBAIJSetPreallocation(*B,A->rmap->bs,MAT_SKIP_ALLOCATION,NULL);CHKERRQ(ierr);
179826fbe8dcSKarl Rupp 
17997b056e98SHong Zhang     (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqSBAIJ;
1800c6d0d4f0SHong Zhang     (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqSBAIJ;
1801e32f2f54SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported");
1802d5f3da31SBarry Smith   (*B)->factortype = ftype;
18035c9eb25fSBarry Smith   PetscFunctionReturn(0);
18045c9eb25fSBarry Smith }
18055c9eb25fSBarry Smith 
1806db4efbfdSBarry Smith #undef __FUNCT__
1807db4efbfdSBarry Smith #define __FUNCT__ "MatGetFactorAvailable_seqsbaij_petsc"
1808ace3abfcSBarry Smith PetscErrorCode MatGetFactorAvailable_seqsbaij_petsc(Mat A,MatFactorType ftype,PetscBool  *flg)
1809db4efbfdSBarry Smith {
1810db4efbfdSBarry Smith   PetscFunctionBegin;
1811db4efbfdSBarry Smith   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
1812db4efbfdSBarry Smith     *flg = PETSC_TRUE;
1813db4efbfdSBarry Smith   } else {
1814db4efbfdSBarry Smith     *flg = PETSC_FALSE;
1815db4efbfdSBarry Smith   }
1816db4efbfdSBarry Smith   PetscFunctionReturn(0);
1817db4efbfdSBarry Smith }
1818db4efbfdSBarry Smith 
1819611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
18208cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_sbaij_mumps(Mat,MatFactorType,Mat*);
1821611f576cSBarry Smith #endif
1822b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
18238cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqsbaij_pastix(Mat,MatFactorType,Mat*);
1824b5e56a35SBarry Smith #endif
182520db9a53SJed Brown #if defined(PETSC_HAVE_CHOLMOD)
18268cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqsbaij_cholmod(Mat,MatFactorType,Mat*);
182720db9a53SJed Brown #endif
18288cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqsbaij_sbstrm(Mat,MatFactorType,Mat*);
18295c9eb25fSBarry Smith 
18300bad9183SKris Buschelman /*MC
1831fafad747SKris Buschelman   MATSEQSBAIJ - MATSEQSBAIJ = "seqsbaij" - A matrix type to be used for sequential symmetric block sparse matrices,
18320bad9183SKris Buschelman   based on block compressed sparse row format.  Only the upper triangular portion of the matrix is stored.
18330bad9183SKris Buschelman 
1834828413b8SBarry Smith   For complex numbers by default this matrix is symmetric, NOT Hermitian symmetric. To make it Hermitian symmetric you
183571dad5bbSBarry Smith   can call MatSetOption(Mat, MAT_HERMITIAN); after MatAssemblyEnd()
1836828413b8SBarry Smith 
18370bad9183SKris Buschelman   Options Database Keys:
18380bad9183SKris Buschelman   . -mat_type seqsbaij - sets the matrix type to "seqsbaij" during a call to MatSetFromOptions()
18390bad9183SKris Buschelman 
184071dad5bbSBarry Smith   Notes: By default if you insert values into the lower triangular part of the matrix they are simply ignored (since they are not
184171dad5bbSBarry Smith      stored and it is assumed they symmetric to the upper triangular). If you call MatSetOption(Mat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_FALSE) or use
184271dad5bbSBarry Smith      the options database -mat_ignore_lower_triangular false it will generate an error if you try to set a value in the lower triangular portion.
184371dad5bbSBarry Smith 
184471dad5bbSBarry Smith 
18450bad9183SKris Buschelman   Level: beginner
18460bad9183SKris Buschelman 
18470bad9183SKris Buschelman   .seealso: MatCreateSeqSBAIJ
18480bad9183SKris Buschelman M*/
18490bad9183SKris Buschelman 
18508cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqSBSTRM(Mat, MatType,MatReuse,Mat*);
1851aa5a9175SDahai Guo 
1852a23d5eceSKris Buschelman #undef __FUNCT__
1853a23d5eceSKris Buschelman #define __FUNCT__ "MatCreate_SeqSBAIJ"
18548cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqSBAIJ(Mat B)
1855a23d5eceSKris Buschelman {
1856a23d5eceSKris Buschelman   Mat_SeqSBAIJ   *b;
1857dfbe8321SBarry Smith   PetscErrorCode ierr;
185813f74950SBarry Smith   PetscMPIInt    size;
1859ace3abfcSBarry Smith   PetscBool      no_unroll = PETSC_FALSE,no_inode = PETSC_FALSE;
1860a23d5eceSKris Buschelman 
1861a23d5eceSKris Buschelman   PetscFunctionBegin;
1862ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
1863e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Comm must be of size 1");
1864a23d5eceSKris Buschelman 
186538f2d2fdSLisandro Dalcin   ierr    = PetscNewLog(B,Mat_SeqSBAIJ,&b);CHKERRQ(ierr);
1866a23d5eceSKris Buschelman   B->data = (void*)b;
1867a23d5eceSKris Buschelman   ierr    = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
186826fbe8dcSKarl Rupp 
1869a23d5eceSKris Buschelman   B->ops->destroy    = MatDestroy_SeqSBAIJ;
1870a23d5eceSKris Buschelman   B->ops->view       = MatView_SeqSBAIJ;
1871a23d5eceSKris Buschelman   b->row             = 0;
1872a23d5eceSKris Buschelman   b->icol            = 0;
1873a23d5eceSKris Buschelman   b->reallocs        = 0;
1874a23d5eceSKris Buschelman   b->saved_values    = 0;
18750def2e27SBarry Smith   b->inode.limit     = 5;
18760def2e27SBarry Smith   b->inode.max_limit = 5;
1877a23d5eceSKris Buschelman 
1878a23d5eceSKris Buschelman   b->roworiented        = PETSC_TRUE;
1879a23d5eceSKris Buschelman   b->nonew              = 0;
1880a23d5eceSKris Buschelman   b->diag               = 0;
1881a23d5eceSKris Buschelman   b->solve_work         = 0;
1882a23d5eceSKris Buschelman   b->mult_work          = 0;
1883a23d5eceSKris Buschelman   B->spptr              = 0;
1884f2cbd3d5SJed Brown   B->info.nz_unneeded   = (PetscReal)b->maxnz*b->bs2;
1885a9817697SBarry Smith   b->keepnonzeropattern = PETSC_FALSE;
1886a23d5eceSKris Buschelman   b->xtoy               = 0;
1887a23d5eceSKris Buschelman   b->XtoY               = 0;
1888a23d5eceSKris Buschelman 
1889a23d5eceSKris Buschelman   b->inew    = 0;
1890a23d5eceSKris Buschelman   b->jnew    = 0;
1891a23d5eceSKris Buschelman   b->anew    = 0;
1892a23d5eceSKris Buschelman   b->a2anew  = 0;
1893a23d5eceSKris Buschelman   b->permute = PETSC_FALSE;
1894a23d5eceSKris Buschelman 
189571dad5bbSBarry Smith   b->ignore_ltriangular = PETSC_TRUE;
189626fbe8dcSKarl Rupp 
18970298fd71SBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_ignore_lower_triangular",&b->ignore_ltriangular,NULL);CHKERRQ(ierr);
1898941593c8SHong Zhang 
1899f5edf698SHong Zhang   b->getrow_utriangular = PETSC_FALSE;
190026fbe8dcSKarl Rupp 
19010298fd71SBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_getrow_uppertriangular",&b->getrow_utriangular,NULL);CHKERRQ(ierr);
1902f5edf698SHong Zhang 
1903b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
1904bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_pastix_C",MatGetFactor_seqsbaij_pastix);CHKERRQ(ierr);
1905b5e56a35SBarry Smith #endif
1906611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
1907bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mumps_C",MatGetFactor_sbaij_mumps);CHKERRQ(ierr);
1908611f576cSBarry Smith #endif
190920db9a53SJed Brown #if defined(PETSC_HAVE_CHOLMOD)
1910bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_cholmod_C",MatGetFactor_seqsbaij_cholmod);CHKERRQ(ierr);
191120db9a53SJed Brown #endif
1912bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactorAvailable_petsc_C",MatGetFactorAvailable_seqsbaij_petsc);CHKERRQ(ierr);
1913bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_petsc_C",MatGetFactor_seqsbaij_petsc);CHKERRQ(ierr);
1914bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_sbstrm_C",MatGetFactor_seqsbaij_sbstrm);CHKERRQ(ierr);
1915bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqSBAIJ);CHKERRQ(ierr);
1916bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqSBAIJ);CHKERRQ(ierr);
1917bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqSBAIJSetColumnIndices_C",MatSeqSBAIJSetColumnIndices_SeqSBAIJ);CHKERRQ(ierr);
1918bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_seqaij_C",MatConvert_SeqSBAIJ_SeqAIJ);CHKERRQ(ierr);
1919bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_seqbaij_C",MatConvert_SeqSBAIJ_SeqBAIJ);CHKERRQ(ierr);
1920bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqSBAIJSetPreallocation_C",MatSeqSBAIJSetPreallocation_SeqSBAIJ);CHKERRQ(ierr);
1921bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_seqsbstrm_C",MatConvert_SeqSBAIJ_SeqSBSTRM);CHKERRQ(ierr);
192223ce1328SBarry Smith 
192323ce1328SBarry Smith   B->symmetric                  = PETSC_TRUE;
192423ce1328SBarry Smith   B->structurally_symmetric     = PETSC_TRUE;
192523ce1328SBarry Smith   B->symmetric_set              = PETSC_TRUE;
192623ce1328SBarry Smith   B->structurally_symmetric_set = PETSC_TRUE;
192726fbe8dcSKarl Rupp 
192817667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQSBAIJ);CHKERRQ(ierr);
19290def2e27SBarry Smith 
1930ce94432eSBarry Smith   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)B),((PetscObject)B)->prefix,"Options for SEQSBAIJ matrix","Mat");CHKERRQ(ierr);
19310298fd71SBarry Smith   ierr = PetscOptionsBool("-mat_no_unroll","Do not optimize for inodes (slower)",NULL,no_unroll,&no_unroll,NULL);CHKERRQ(ierr);
193226fbe8dcSKarl Rupp   if (no_unroll) {
193326fbe8dcSKarl Rupp     ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_unroll\n");CHKERRQ(ierr);
193426fbe8dcSKarl Rupp   }
19350298fd71SBarry Smith   ierr = PetscOptionsBool("-mat_no_inode","Do not optimize for inodes (slower)",NULL,no_inode,&no_inode,NULL);CHKERRQ(ierr);
193626fbe8dcSKarl Rupp   if (no_inode) {
193726fbe8dcSKarl Rupp     ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_inode\n");CHKERRQ(ierr);
193826fbe8dcSKarl Rupp   }
19390298fd71SBarry Smith   ierr = PetscOptionsInt("-mat_inode_limit","Do not use inodes larger then this value",NULL,b->inode.limit,&b->inode.limit,NULL);CHKERRQ(ierr);
19400def2e27SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1941ace3abfcSBarry Smith   b->inode.use = (PetscBool)(!(no_unroll || no_inode));
19420def2e27SBarry Smith   if (b->inode.limit > b->inode.max_limit) b->inode.limit = b->inode.max_limit;
1943a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1944a23d5eceSKris Buschelman }
1945a23d5eceSKris Buschelman 
1946a23d5eceSKris Buschelman #undef __FUNCT__
1947a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation"
1948a23d5eceSKris Buschelman /*@C
1949a23d5eceSKris Buschelman    MatSeqSBAIJSetPreallocation - Creates a sparse symmetric matrix in block AIJ (block
1950a23d5eceSKris Buschelman    compressed row) format.  For good matrix assembly performance the
1951a23d5eceSKris Buschelman    user should preallocate the matrix storage by setting the parameter nz
1952a23d5eceSKris Buschelman    (or the array nnz).  By setting these parameters accurately, performance
1953a23d5eceSKris Buschelman    during matrix assembly can be increased by more than a factor of 50.
1954a23d5eceSKris Buschelman 
1955a23d5eceSKris Buschelman    Collective on Mat
1956a23d5eceSKris Buschelman 
1957a23d5eceSKris Buschelman    Input Parameters:
1958a23d5eceSKris Buschelman +  A - the symmetric matrix
1959a23d5eceSKris Buschelman .  bs - size of block
1960a23d5eceSKris Buschelman .  nz - number of block nonzeros per block row (same for all rows)
1961a23d5eceSKris Buschelman -  nnz - array containing the number of block nonzeros in the upper triangular plus
19620298fd71SBarry Smith          diagonal portion of each block (possibly different for each block row) or NULL
1963a23d5eceSKris Buschelman 
1964a23d5eceSKris Buschelman    Options Database Keys:
1965a23d5eceSKris Buschelman .   -mat_no_unroll - uses code that does not unroll the loops in the
1966a23d5eceSKris Buschelman                      block calculations (much slower)
1967db4efbfdSBarry Smith .    -mat_block_size - size of the blocks to use (only works if a negative bs is passed in
1968a23d5eceSKris Buschelman 
1969a23d5eceSKris Buschelman    Level: intermediate
1970a23d5eceSKris Buschelman 
1971a23d5eceSKris Buschelman    Notes:
1972a23d5eceSKris Buschelman    Specify the preallocated storage with either nz or nnz (not both).
19730298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
19740598bfebSBarry Smith    allocation.  See the <a href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</a> for details.
1975a23d5eceSKris Buschelman 
1976aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
1977aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
1978aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
1979aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
1980aa95bbe8SBarry Smith 
198149a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
198249a6f317SBarry Smith 
198349a6f317SBarry Smith 
198469b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateSBAIJ()
1985a23d5eceSKris Buschelman @*/
19867087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt nz,const PetscInt nnz[])
198713f74950SBarry Smith {
19884ac538c5SBarry Smith   PetscErrorCode ierr;
1989a23d5eceSKris Buschelman 
1990a23d5eceSKris Buschelman   PetscFunctionBegin;
19916ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
19926ba663aaSJed Brown   PetscValidType(B,1);
19936ba663aaSJed Brown   PetscValidLogicalCollectiveInt(B,bs,2);
19944ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqSBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[]),(B,bs,nz,nnz));CHKERRQ(ierr);
1995a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1996a23d5eceSKris Buschelman }
199749b5e25fSSatish Balay 
19984a2ae208SSatish Balay #undef __FUNCT__
19994a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqSBAIJ"
2000c464158bSHong Zhang /*@C
2001c464158bSHong Zhang    MatCreateSeqSBAIJ - Creates a sparse symmetric matrix in block AIJ (block
2002c464158bSHong Zhang    compressed row) format.  For good matrix assembly performance the
2003c464158bSHong Zhang    user should preallocate the matrix storage by setting the parameter nz
2004c464158bSHong Zhang    (or the array nnz).  By setting these parameters accurately, performance
2005c464158bSHong Zhang    during matrix assembly can be increased by more than a factor of 50.
200649b5e25fSSatish Balay 
2007c464158bSHong Zhang    Collective on MPI_Comm
2008c464158bSHong Zhang 
2009c464158bSHong Zhang    Input Parameters:
2010c464158bSHong Zhang +  comm - MPI communicator, set to PETSC_COMM_SELF
2011c464158bSHong Zhang .  bs - size of block
2012c464158bSHong Zhang .  m - number of rows, or number of columns
2013c464158bSHong Zhang .  nz - number of block nonzeros per block row (same for all rows)
2014744e8345SSatish Balay -  nnz - array containing the number of block nonzeros in the upper triangular plus
20150298fd71SBarry Smith          diagonal portion of each block (possibly different for each block row) or NULL
2016c464158bSHong Zhang 
2017c464158bSHong Zhang    Output Parameter:
2018c464158bSHong Zhang .  A - the symmetric matrix
2019c464158bSHong Zhang 
2020c464158bSHong Zhang    Options Database Keys:
2021c464158bSHong Zhang .   -mat_no_unroll - uses code that does not unroll the loops in the
2022c464158bSHong Zhang                      block calculations (much slower)
2023c464158bSHong Zhang .    -mat_block_size - size of the blocks to use
2024c464158bSHong Zhang 
2025c464158bSHong Zhang    Level: intermediate
2026c464158bSHong Zhang 
2027175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2028ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
2029175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2030175b88e8SBarry Smith 
2031c464158bSHong Zhang    Notes:
20326d6d819aSHong Zhang    The number of rows and columns must be divisible by blocksize.
20336d6d819aSHong Zhang    This matrix type does not support complex Hermitian operation.
2034c464158bSHong Zhang 
2035c464158bSHong Zhang    Specify the preallocated storage with either nz or nnz (not both).
20360298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
20370598bfebSBarry Smith    allocation.  See the <a href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</a> for details.
2038c464158bSHong Zhang 
203949a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
204049a6f317SBarry Smith 
204169b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateSBAIJ()
2042c464158bSHong Zhang @*/
20437087cfbeSBarry Smith PetscErrorCode  MatCreateSeqSBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
2044c464158bSHong Zhang {
2045dfbe8321SBarry Smith   PetscErrorCode ierr;
2046c464158bSHong Zhang 
2047c464158bSHong Zhang   PetscFunctionBegin;
2048f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2049f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2050c464158bSHong Zhang   ierr = MatSetType(*A,MATSEQSBAIJ);CHKERRQ(ierr);
2051ab93d7beSBarry Smith   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*A,bs,nz,(PetscInt*)nnz);CHKERRQ(ierr);
205249b5e25fSSatish Balay   PetscFunctionReturn(0);
205349b5e25fSSatish Balay }
205449b5e25fSSatish Balay 
20554a2ae208SSatish Balay #undef __FUNCT__
20564a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqSBAIJ"
2057dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqSBAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
205849b5e25fSSatish Balay {
205949b5e25fSSatish Balay   Mat            C;
206049b5e25fSSatish Balay   Mat_SeqSBAIJ   *c,*a = (Mat_SeqSBAIJ*)A->data;
20616849ba73SBarry Smith   PetscErrorCode ierr;
2062b40805acSSatish Balay   PetscInt       i,mbs = a->mbs,nz = a->nz,bs2 =a->bs2;
206349b5e25fSSatish Balay 
206449b5e25fSSatish Balay   PetscFunctionBegin;
2065e32f2f54SBarry Smith   if (a->i[mbs] != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupt matrix");
206649b5e25fSSatish Balay 
206749b5e25fSSatish Balay   *B   = 0;
2068ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2069d0f46423SBarry Smith   ierr = MatSetSizes(C,A->rmap->N,A->cmap->n,A->rmap->N,A->cmap->n);CHKERRQ(ierr);
20708e9a0fb8SHong Zhang   ierr = MatSetType(C,MATSEQSBAIJ);CHKERRQ(ierr);
20711d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
2072692f9cbeSHong Zhang   c    = (Mat_SeqSBAIJ*)C->data;
2073692f9cbeSHong Zhang 
2074273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2075d5f3da31SBarry Smith   C->factortype         = A->factortype;
207649b5e25fSSatish Balay   c->row                = 0;
207749b5e25fSSatish Balay   c->icol               = 0;
207849b5e25fSSatish Balay   c->saved_values       = 0;
2079a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
208049b5e25fSSatish Balay   C->assembled          = PETSC_TRUE;
208149b5e25fSSatish Balay 
20821e1e43feSBarry Smith   ierr   = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
20831e1e43feSBarry Smith   ierr   = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
208449b5e25fSSatish Balay   c->bs2 = a->bs2;
208549b5e25fSSatish Balay   c->mbs = a->mbs;
208649b5e25fSSatish Balay   c->nbs = a->nbs;
208749b5e25fSSatish Balay 
2088c760cd28SBarry Smith   if  (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2089c760cd28SBarry Smith     c->imax           = a->imax;
2090c760cd28SBarry Smith     c->ilen           = a->ilen;
2091c760cd28SBarry Smith     c->free_imax_ilen = PETSC_FALSE;
2092c760cd28SBarry Smith   } else {
20938777fc3fSSatish Balay     ierr = PetscMalloc2((mbs+1),PetscInt,&c->imax,(mbs+1),PetscInt,&c->ilen);CHKERRQ(ierr);
20943bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C,2*(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
209549b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
209649b5e25fSSatish Balay       c->imax[i] = a->imax[i];
209749b5e25fSSatish Balay       c->ilen[i] = a->ilen[i];
209849b5e25fSSatish Balay     }
2099c760cd28SBarry Smith     c->free_imax_ilen = PETSC_TRUE;
2100c760cd28SBarry Smith   }
210149b5e25fSSatish Balay 
210249b5e25fSSatish Balay   /* allocate the matrix space */
21034da8f245SBarry Smith   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
21044da8f245SBarry Smith     ierr            = PetscMalloc(bs2*nz*sizeof(MatScalar),&c->a);CHKERRQ(ierr);
21053bb1ff40SBarry Smith     ierr            = PetscLogObjectMemory((PetscObject)C,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
210644e1c64aSLisandro Dalcin     c->i            = a->i;
210744e1c64aSLisandro Dalcin     c->j            = a->j;
21084da8f245SBarry Smith     c->singlemalloc = PETSC_FALSE;
210944e1c64aSLisandro Dalcin     c->free_a       = PETSC_TRUE;
21104da8f245SBarry Smith     c->free_ij      = PETSC_FALSE;
21114da8f245SBarry Smith     c->parent       = A;
21124da8f245SBarry Smith     ierr            = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
21134da8f245SBarry Smith     ierr            = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
21144da8f245SBarry Smith     ierr            = MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
21154da8f245SBarry Smith   } else {
2116b40805acSSatish Balay     ierr            = PetscMalloc3(bs2*nz,MatScalar,&c->a,nz,PetscInt,&c->j,mbs+1,PetscInt,&c->i);CHKERRQ(ierr);
211713f74950SBarry Smith     ierr            = PetscMemcpy(c->i,a->i,(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
21183bb1ff40SBarry Smith     ierr            = PetscLogObjectMemory((PetscObject)C,(mbs+1)*sizeof(PetscInt) + nz*(bs2*sizeof(MatScalar) + sizeof(PetscInt)));CHKERRQ(ierr);
21194da8f245SBarry Smith     c->singlemalloc = PETSC_TRUE;
212044e1c64aSLisandro Dalcin     c->free_a       = PETSC_TRUE;
21214da8f245SBarry Smith     c->free_ij      = PETSC_TRUE;
21224da8f245SBarry Smith   }
212349b5e25fSSatish Balay   if (mbs > 0) {
21244da8f245SBarry Smith     if (cpvalues != MAT_SHARE_NONZERO_PATTERN) {
212513f74950SBarry Smith       ierr = PetscMemcpy(c->j,a->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
21264da8f245SBarry Smith     }
212749b5e25fSSatish Balay     if (cpvalues == MAT_COPY_VALUES) {
212849b5e25fSSatish Balay       ierr = PetscMemcpy(c->a,a->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
212949b5e25fSSatish Balay     } else {
213049b5e25fSSatish Balay       ierr = PetscMemzero(c->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
213149b5e25fSSatish Balay     }
2132a1c3900fSBarry Smith     if (a->jshort) {
213344e1c64aSLisandro Dalcin       /* cannot share jshort, it is reallocated in MatAssemblyEnd_SeqSBAIJ() */
213444e1c64aSLisandro Dalcin       /* if the parent matrix is reassembled, this child matrix will never notice */
2135a1c3900fSBarry Smith       ierr = PetscMalloc(nz*sizeof(unsigned short),&c->jshort);CHKERRQ(ierr);
21363bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)C,nz*sizeof(unsigned short));CHKERRQ(ierr);
2137a1c3900fSBarry Smith       ierr = PetscMemcpy(c->jshort,a->jshort,nz*sizeof(unsigned short));CHKERRQ(ierr);
213826fbe8dcSKarl Rupp 
21394da8f245SBarry Smith       c->free_jshort = PETSC_TRUE;
21404da8f245SBarry Smith     }
2141a1c3900fSBarry Smith   }
214249b5e25fSSatish Balay 
214349b5e25fSSatish Balay   c->roworiented = a->roworiented;
214449b5e25fSSatish Balay   c->nonew       = a->nonew;
214549b5e25fSSatish Balay 
214649b5e25fSSatish Balay   if (a->diag) {
2147c760cd28SBarry Smith     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2148c760cd28SBarry Smith       c->diag      = a->diag;
2149c760cd28SBarry Smith       c->free_diag = PETSC_FALSE;
2150c760cd28SBarry Smith     } else {
21512ed38d0bSJed Brown       ierr = PetscMalloc(mbs*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
21523bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)C,mbs*sizeof(PetscInt));CHKERRQ(ierr);
215326fbe8dcSKarl Rupp       for (i=0; i<mbs; i++) c->diag[i] = a->diag[i];
2154c760cd28SBarry Smith       c->free_diag = PETSC_TRUE;
2155c760cd28SBarry Smith     }
215644e1c64aSLisandro Dalcin   }
21576c6c5352SBarry Smith   c->nz         = a->nz;
2158f2cbd3d5SJed Brown   c->maxnz      = a->nz; /* Since we allocate exactly the right amount */
215949b5e25fSSatish Balay   c->solve_work = 0;
216049b5e25fSSatish Balay   c->mult_work  = 0;
216126fbe8dcSKarl Rupp 
216249b5e25fSSatish Balay   *B   = C;
2163140e18c1SBarry Smith   ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
216449b5e25fSSatish Balay   PetscFunctionReturn(0);
216549b5e25fSSatish Balay }
216649b5e25fSSatish Balay 
21674a2ae208SSatish Balay #undef __FUNCT__
21685bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_SeqSBAIJ"
2169112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqSBAIJ(Mat newmat,PetscViewer viewer)
21702f480046SShri Abhyankar {
21712f480046SShri Abhyankar   Mat_SeqSBAIJ   *a;
21722f480046SShri Abhyankar   PetscErrorCode ierr;
21732f480046SShri Abhyankar   int            fd;
21742f480046SShri Abhyankar   PetscMPIInt    size;
21752f480046SShri Abhyankar   PetscInt       i,nz,header[4],*rowlengths=0,M,N,bs=1;
21762f480046SShri Abhyankar   PetscInt       *mask,mbs,*jj,j,rowcount,nzcount,k,*s_browlengths,maskcount;
21772f480046SShri Abhyankar   PetscInt       kmax,jcount,block,idx,point,nzcountb,extra_rows,rows,cols;
21782f480046SShri Abhyankar   PetscInt       *masked,nmask,tmp,bs2,ishift;
21792f480046SShri Abhyankar   PetscScalar    *aa;
2180ce94432eSBarry Smith   MPI_Comm       comm;
21812f480046SShri Abhyankar 
21822f480046SShri Abhyankar   PetscFunctionBegin;
2183ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
21840298fd71SBarry Smith   ierr = PetscOptionsGetInt(((PetscObject)newmat)->prefix,"-matload_block_size",&bs,NULL);CHKERRQ(ierr);
21852f480046SShri Abhyankar   bs2  = bs*bs;
21862f480046SShri Abhyankar 
21872f480046SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
21882f480046SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
21892f480046SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
21902f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
21912f480046SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not Mat object");
21922f480046SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
21932f480046SShri Abhyankar 
21942f480046SShri Abhyankar   if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as SeqSBAIJ");
21952f480046SShri Abhyankar 
21962f480046SShri Abhyankar   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");
21972f480046SShri Abhyankar 
21982f480046SShri Abhyankar   /*
21992f480046SShri Abhyankar      This code adds extra rows to make sure the number of rows is
22002f480046SShri Abhyankar     divisible by the blocksize
22012f480046SShri Abhyankar   */
22022f480046SShri Abhyankar   mbs        = M/bs;
22032f480046SShri Abhyankar   extra_rows = bs - M + bs*(mbs);
22042f480046SShri Abhyankar   if (extra_rows == bs) extra_rows = 0;
22052f480046SShri Abhyankar   else                  mbs++;
22062f480046SShri Abhyankar   if (extra_rows) {
22072f480046SShri Abhyankar     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
22082f480046SShri Abhyankar   }
22092f480046SShri Abhyankar 
22102f480046SShri Abhyankar   /* Set global sizes if not already set */
22112f480046SShri Abhyankar   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) {
22122f480046SShri Abhyankar     ierr = MatSetSizes(newmat,PETSC_DECIDE,PETSC_DECIDE,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
22132f480046SShri Abhyankar   } else { /* Check if the matrix global sizes are correct */
22142f480046SShri Abhyankar     ierr = MatGetSize(newmat,&rows,&cols);CHKERRQ(ierr);
22152f480046SShri Abhyankar     if (M != rows ||  N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix in file of different length (%d, %d) than the input matrix (%d, %d)",M,N,rows,cols);
22162f480046SShri Abhyankar   }
22172f480046SShri Abhyankar 
22182f480046SShri Abhyankar   /* read in row lengths */
22192f480046SShri Abhyankar   ierr = PetscMalloc((M+extra_rows)*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
22202f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
22212f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
22222f480046SShri Abhyankar 
22232f480046SShri Abhyankar   /* read in column indices */
22242f480046SShri Abhyankar   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscInt),&jj);CHKERRQ(ierr);
22252f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,jj,nz,PETSC_INT);CHKERRQ(ierr);
22262f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) jj[nz+i] = M+i;
22272f480046SShri Abhyankar 
22282f480046SShri Abhyankar   /* loop over row lengths determining block row lengths */
22292f480046SShri Abhyankar   ierr     = PetscMalloc(mbs*sizeof(PetscInt),&s_browlengths);CHKERRQ(ierr);
22302f480046SShri Abhyankar   ierr     = PetscMemzero(s_browlengths,mbs*sizeof(PetscInt));CHKERRQ(ierr);
22312f480046SShri Abhyankar   ierr     = PetscMalloc2(mbs,PetscInt,&mask,mbs,PetscInt,&masked);CHKERRQ(ierr);
22322f480046SShri Abhyankar   ierr     = PetscMemzero(mask,mbs*sizeof(PetscInt));CHKERRQ(ierr);
22332f480046SShri Abhyankar   rowcount = 0;
22342f480046SShri Abhyankar   nzcount  = 0;
22352f480046SShri Abhyankar   for (i=0; i<mbs; i++) {
22362f480046SShri Abhyankar     nmask = 0;
22372f480046SShri Abhyankar     for (j=0; j<bs; j++) {
22382f480046SShri Abhyankar       kmax = rowlengths[rowcount];
22392f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
22402f480046SShri Abhyankar         tmp = jj[nzcount++]/bs;   /* block col. index */
22412f480046SShri Abhyankar         if (!mask[tmp] && tmp >= i) {masked[nmask++] = tmp; mask[tmp] = 1;}
22422f480046SShri Abhyankar       }
22432f480046SShri Abhyankar       rowcount++;
22442f480046SShri Abhyankar     }
22452f480046SShri Abhyankar     s_browlengths[i] += nmask;
22462f480046SShri Abhyankar 
22472f480046SShri Abhyankar     /* zero out the mask elements we set */
22482f480046SShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
22492f480046SShri Abhyankar   }
22502f480046SShri Abhyankar 
22512f480046SShri Abhyankar   /* Do preallocation */
22522f480046SShri Abhyankar   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(newmat,bs,0,s_browlengths);CHKERRQ(ierr);
22532f480046SShri Abhyankar   a    = (Mat_SeqSBAIJ*)newmat->data;
22542f480046SShri Abhyankar 
22552f480046SShri Abhyankar   /* set matrix "i" values */
22562f480046SShri Abhyankar   a->i[0] = 0;
22572f480046SShri Abhyankar   for (i=1; i<= mbs; i++) {
22582f480046SShri Abhyankar     a->i[i]      = a->i[i-1] + s_browlengths[i-1];
22592f480046SShri Abhyankar     a->ilen[i-1] = s_browlengths[i-1];
22602f480046SShri Abhyankar   }
22612f480046SShri Abhyankar   a->nz = a->i[mbs];
22622f480046SShri Abhyankar 
22632f480046SShri Abhyankar   /* read in nonzero values */
22642f480046SShri Abhyankar   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscScalar),&aa);CHKERRQ(ierr);
22652f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,aa,nz,PETSC_SCALAR);CHKERRQ(ierr);
22662f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) aa[nz+i] = 1.0;
22672f480046SShri Abhyankar 
22682f480046SShri Abhyankar   /* set "a" and "j" values into matrix */
22692f480046SShri Abhyankar   nzcount = 0; jcount = 0;
22702f480046SShri Abhyankar   for (i=0; i<mbs; i++) {
22712f480046SShri Abhyankar     nzcountb = nzcount;
22722f480046SShri Abhyankar     nmask    = 0;
22732f480046SShri Abhyankar     for (j=0; j<bs; j++) {
22742f480046SShri Abhyankar       kmax = rowlengths[i*bs+j];
22752f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
22762f480046SShri Abhyankar         tmp = jj[nzcount++]/bs; /* block col. index */
22772f480046SShri Abhyankar         if (!mask[tmp] && tmp >= i) { masked[nmask++] = tmp; mask[tmp] = 1;}
22782f480046SShri Abhyankar       }
22792f480046SShri Abhyankar     }
22802f480046SShri Abhyankar     /* sort the masked values */
22812f480046SShri Abhyankar     ierr = PetscSortInt(nmask,masked);CHKERRQ(ierr);
22822f480046SShri Abhyankar 
22832f480046SShri Abhyankar     /* set "j" values into matrix */
22842f480046SShri Abhyankar     maskcount = 1;
22852f480046SShri Abhyankar     for (j=0; j<nmask; j++) {
22862f480046SShri Abhyankar       a->j[jcount++]  = masked[j];
22872f480046SShri Abhyankar       mask[masked[j]] = maskcount++;
22882f480046SShri Abhyankar     }
22892f480046SShri Abhyankar 
22902f480046SShri Abhyankar     /* set "a" values into matrix */
22912f480046SShri Abhyankar     ishift = bs2*a->i[i];
22922f480046SShri Abhyankar     for (j=0; j<bs; j++) {
22932f480046SShri Abhyankar       kmax = rowlengths[i*bs+j];
22942f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
22952f480046SShri Abhyankar         tmp = jj[nzcountb]/bs;        /* block col. index */
22962f480046SShri Abhyankar         if (tmp >= i) {
22972f480046SShri Abhyankar           block     = mask[tmp] - 1;
22982f480046SShri Abhyankar           point     = jj[nzcountb] - bs*tmp;
22992f480046SShri Abhyankar           idx       = ishift + bs2*block + j + bs*point;
23002f480046SShri Abhyankar           a->a[idx] = aa[nzcountb];
23012f480046SShri Abhyankar         }
23022f480046SShri Abhyankar         nzcountb++;
23032f480046SShri Abhyankar       }
23042f480046SShri Abhyankar     }
23052f480046SShri Abhyankar     /* zero out the mask elements we set */
23062f480046SShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
23072f480046SShri Abhyankar   }
23082f480046SShri Abhyankar   if (jcount != a->nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Bad binary matrix");
23092f480046SShri Abhyankar 
23102f480046SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
23112f480046SShri Abhyankar   ierr = PetscFree(s_browlengths);CHKERRQ(ierr);
23122f480046SShri Abhyankar   ierr = PetscFree(aa);CHKERRQ(ierr);
23132f480046SShri Abhyankar   ierr = PetscFree(jj);CHKERRQ(ierr);
23142f480046SShri Abhyankar   ierr = PetscFree2(mask,masked);CHKERRQ(ierr);
23152f480046SShri Abhyankar 
23162f480046SShri Abhyankar   ierr = MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
23172f480046SShri Abhyankar   ierr = MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
23182f480046SShri Abhyankar   PetscFunctionReturn(0);
23192f480046SShri Abhyankar }
23202f480046SShri Abhyankar 
23212f480046SShri Abhyankar #undef __FUNCT__
2322c75a6043SHong Zhang #define __FUNCT__ "MatCreateSeqSBAIJWithArrays"
2323c75a6043SHong Zhang /*@
2324c75a6043SHong Zhang      MatCreateSeqSBAIJWithArrays - Creates an sequential SBAIJ matrix using matrix elements
2325c75a6043SHong Zhang               (upper triangular entries in CSR format) provided by the user.
2326c75a6043SHong Zhang 
2327c75a6043SHong Zhang      Collective on MPI_Comm
2328c75a6043SHong Zhang 
2329c75a6043SHong Zhang    Input Parameters:
2330c75a6043SHong Zhang +  comm - must be an MPI communicator of size 1
2331c75a6043SHong Zhang .  bs - size of block
2332c75a6043SHong Zhang .  m - number of rows
2333c75a6043SHong Zhang .  n - number of columns
2334c75a6043SHong Zhang .  i - row indices
2335c75a6043SHong Zhang .  j - column indices
2336c75a6043SHong Zhang -  a - matrix values
2337c75a6043SHong Zhang 
2338c75a6043SHong Zhang    Output Parameter:
2339c75a6043SHong Zhang .  mat - the matrix
2340c75a6043SHong Zhang 
2341dfb205c3SBarry Smith    Level: advanced
2342c75a6043SHong Zhang 
2343c75a6043SHong Zhang    Notes:
2344c75a6043SHong Zhang        The i, j, and a arrays are not copied by this routine, the user must free these arrays
2345c75a6043SHong Zhang     once the matrix is destroyed
2346c75a6043SHong Zhang 
2347c75a6043SHong Zhang        You cannot set new nonzero locations into this matrix, that will generate an error.
2348c75a6043SHong Zhang 
2349c75a6043SHong Zhang        The i and j indices are 0 based
2350c75a6043SHong Zhang 
2351dfb205c3SBarry Smith        When block size is greater than 1 the matrix values must be stored using the SBAIJ storage format (see the SBAIJ code to determine this). For block size of 1
2352dfb205c3SBarry Smith        it is the regular CSR format excluding the lower triangular elements.
2353dfb205c3SBarry Smith 
235469b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSBAIJ(), MatCreateSeqSBAIJ()
2355c75a6043SHong Zhang 
2356c75a6043SHong Zhang @*/
23577087cfbeSBarry Smith PetscErrorCode  MatCreateSeqSBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
2358c75a6043SHong Zhang {
2359c75a6043SHong Zhang   PetscErrorCode ierr;
2360c75a6043SHong Zhang   PetscInt       ii;
2361c75a6043SHong Zhang   Mat_SeqSBAIJ   *sbaij;
2362c75a6043SHong Zhang 
2363c75a6043SHong Zhang   PetscFunctionBegin;
2364e32f2f54SBarry Smith   if (bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"block size %D > 1 is not supported yet",bs);
2365e32f2f54SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
2366c75a6043SHong Zhang 
2367c75a6043SHong Zhang   ierr  = MatCreate(comm,mat);CHKERRQ(ierr);
2368c75a6043SHong Zhang   ierr  = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
2369c75a6043SHong Zhang   ierr  = MatSetType(*mat,MATSEQSBAIJ);CHKERRQ(ierr);
2370c75a6043SHong Zhang   ierr  = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*mat,bs,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
2371c75a6043SHong Zhang   sbaij = (Mat_SeqSBAIJ*)(*mat)->data;
2372c75a6043SHong Zhang   ierr  = PetscMalloc2(m,PetscInt,&sbaij->imax,m,PetscInt,&sbaij->ilen);CHKERRQ(ierr);
23733bb1ff40SBarry Smith   ierr  = PetscLogObjectMemory((PetscObject)*mat,2*m*sizeof(PetscInt));CHKERRQ(ierr);
2374c75a6043SHong Zhang 
2375c75a6043SHong Zhang   sbaij->i = i;
2376c75a6043SHong Zhang   sbaij->j = j;
2377c75a6043SHong Zhang   sbaij->a = a;
237826fbe8dcSKarl Rupp 
2379c75a6043SHong Zhang   sbaij->singlemalloc = PETSC_FALSE;
2380c75a6043SHong Zhang   sbaij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
2381e6b907acSBarry Smith   sbaij->free_a       = PETSC_FALSE;
2382e6b907acSBarry Smith   sbaij->free_ij      = PETSC_FALSE;
2383c75a6043SHong Zhang 
2384c75a6043SHong Zhang   for (ii=0; ii<m; ii++) {
2385c75a6043SHong Zhang     sbaij->ilen[ii] = sbaij->imax[ii] = i[ii+1] - i[ii];
2386c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2387e32f2f54SBarry Smith     if (i[ii+1] - i[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]);
2388c75a6043SHong Zhang #endif
2389c75a6043SHong Zhang   }
2390c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2391c75a6043SHong Zhang   for (ii=0; ii<sbaij->i[m]; ii++) {
2392e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
2393e32f2f54SBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
2394c75a6043SHong Zhang   }
2395c75a6043SHong Zhang #endif
2396c75a6043SHong Zhang 
2397c75a6043SHong Zhang   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2398c75a6043SHong Zhang   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2399c75a6043SHong Zhang   PetscFunctionReturn(0);
2400c75a6043SHong Zhang }
2401d06b337dSHong Zhang 
2402d06b337dSHong Zhang 
2403d06b337dSHong Zhang 
240449b5e25fSSatish Balay 
240549b5e25fSSatish Balay 
2406