xref: /petsc/src/mat/impls/sbaij/seq/sbaij.c (revision 3d472b5489a249cae8c8a56ebcb8e864414a0bfa)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
249b5e25fSSatish Balay 
349b5e25fSSatish Balay /*
4a1373b80SHong Zhang     Defines the basic matrix operations for the SBAIJ (compressed row)
549b5e25fSSatish Balay   matrix storage format.
649b5e25fSSatish Balay */
77c4f633dSBarry Smith #include "../src/mat/impls/baij/seq/baij.h"         /*I "petscmat.h" I*/
87c4f633dSBarry Smith #include "../src/mat/impls/sbaij/seq/sbaij.h"
9f3da1532SBarry Smith #include "petscblaslapack.h"
1049b5e25fSSatish Balay 
11b5b17502SBarry Smith #include "../src/mat/impls/sbaij/seq/relax.h"
1270dcbbb9SBarry Smith #define USESHORT
13b5b17502SBarry Smith #include "../src/mat/impls/sbaij/seq/relax.h"
1470dcbbb9SBarry Smith 
15d595f711SHong Zhang extern PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat,PetscTruth);
16b5b17502SBarry Smith 
1749b5e25fSSatish Balay /*
1849b5e25fSSatish Balay      Checks for missing diagonals
1949b5e25fSSatish Balay */
204a2ae208SSatish Balay #undef __FUNCT__
214a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqSBAIJ"
222af78befSBarry Smith PetscErrorCode MatMissingDiagonal_SeqSBAIJ(Mat A,PetscTruth *missing,PetscInt *dd)
2349b5e25fSSatish Balay {
24045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
256849ba73SBarry Smith   PetscErrorCode ierr;
2613f74950SBarry Smith   PetscInt       *diag,*jj = a->j,i;
2749b5e25fSSatish Balay 
2849b5e25fSSatish Balay   PetscFunctionBegin;
29045c9aa0SHong Zhang   ierr = MatMarkDiagonal_SeqSBAIJ(A);CHKERRQ(ierr);
3049b5e25fSSatish Balay   diag = a->diag;
312af78befSBarry Smith   *missing = PETSC_FALSE;
3249b5e25fSSatish Balay   for (i=0; i<a->mbs; i++) {
332af78befSBarry Smith     if (jj[diag[i]] != i) {
342af78befSBarry Smith       *missing    = PETSC_TRUE;
352af78befSBarry Smith       if (dd) *dd = i;
362af78befSBarry Smith       break;
372af78befSBarry Smith     }
3849b5e25fSSatish Balay   }
3949b5e25fSSatish Balay   PetscFunctionReturn(0);
4049b5e25fSSatish Balay }
4149b5e25fSSatish Balay 
424a2ae208SSatish Balay #undef __FUNCT__
434a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqSBAIJ"
44dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqSBAIJ(Mat A)
4549b5e25fSSatish Balay {
46045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
476849ba73SBarry Smith   PetscErrorCode ierr;
4809f38230SBarry Smith   PetscInt       i;
4949b5e25fSSatish Balay 
5049b5e25fSSatish Balay   PetscFunctionBegin;
5109f38230SBarry Smith   if (!a->diag) {
5209f38230SBarry Smith     ierr = PetscMalloc(a->mbs*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
53c760cd28SBarry Smith     ierr = PetscLogObjectMemory(A,a->mbs*sizeof(PetscInt));CHKERRQ(ierr);
54c760cd28SBarry Smith     a->free_diag = PETSC_TRUE;
5509f38230SBarry Smith   }
5609f38230SBarry Smith   for (i=0; i<a->mbs; i++) a->diag[i] = a->i[i];
5749b5e25fSSatish Balay   PetscFunctionReturn(0);
5849b5e25fSSatish Balay }
5949b5e25fSSatish Balay 
604a2ae208SSatish Balay #undef __FUNCT__
614a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqSBAIJ"
628f7157efSSatish Balay static PetscErrorCode MatGetRowIJ_SeqSBAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
6349b5e25fSSatish Balay {
64a6ece127SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
65d0f46423SBarry Smith   PetscInt     i,j,n = a->mbs,nz = a->i[n],bs = A->rmap->bs;
668f7157efSSatish Balay   PetscErrorCode ierr;
6749b5e25fSSatish Balay 
6849b5e25fSSatish Balay   PetscFunctionBegin;
69d3e5a4abSHong Zhang   *nn = n;
70a1373b80SHong Zhang   if (!ia) PetscFunctionReturn(0);
718f7157efSSatish Balay   if (!blockcompressed) {
728f7157efSSatish Balay     /* malloc & create the natural set of indices */
73f1d0d59dSSatish Balay     ierr = PetscMalloc2((n+1)*bs,PetscInt,ia,nz*bs,PetscInt,ja);CHKERRQ(ierr);
748f7157efSSatish Balay     for (i=0; i<n+1; i++) {
758f7157efSSatish Balay       for (j=0; j<bs; j++) {
768f7157efSSatish Balay         *ia[i*bs+j] = a->i[i]*bs+j+oshift;
778f7157efSSatish Balay       }
788f7157efSSatish Balay     }
798f7157efSSatish Balay     for (i=0; i<nz; i++) {
808f7157efSSatish Balay       for (j=0; j<bs; j++) {
818f7157efSSatish Balay         *ja[i*bs+j] = a->j[i]*bs+j+oshift;
828f7157efSSatish Balay       }
838f7157efSSatish Balay     }
848f7157efSSatish Balay   } else { /* blockcompressed */
85a6ece127SHong Zhang     if (oshift == 1) {
86a6ece127SHong Zhang       /* temporarily add 1 to i and j indices */
876c6c5352SBarry Smith       for (i=0; i<nz; i++) a->j[i]++;
88a1373b80SHong Zhang       for (i=0; i<n+1; i++) a->i[i]++;
898f7157efSSatish Balay     }
90a1373b80SHong Zhang     *ia = a->i; *ja = a->j;
91a6ece127SHong Zhang   }
928f7157efSSatish Balay 
9349b5e25fSSatish Balay   PetscFunctionReturn(0);
9449b5e25fSSatish Balay }
9549b5e25fSSatish Balay 
964a2ae208SSatish Balay #undef __FUNCT__
974a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqSBAIJ"
988f7157efSSatish Balay static PetscErrorCode MatRestoreRowIJ_SeqSBAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
9949b5e25fSSatish Balay {
100b7aaefc3SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
1018f7157efSSatish Balay   PetscInt     i,n = a->mbs,nz = a->i[n];
1028f7157efSSatish Balay   PetscErrorCode ierr;
103a6ece127SHong Zhang 
10449b5e25fSSatish Balay   PetscFunctionBegin;
10549b5e25fSSatish Balay   if (!ia) PetscFunctionReturn(0);
106a6ece127SHong Zhang 
1078f7157efSSatish Balay   if (!blockcompressed) {
1088f7157efSSatish Balay     ierr = PetscFree2(*ia,*ja);CHKERRQ(ierr);
1098f7157efSSatish Balay   } else if (oshift == 1) { /* blockcompressed */
1106c6c5352SBarry Smith     for (i=0; i<nz; i++) a->j[i]--;
111a6ece127SHong Zhang     for (i=0; i<n+1; i++) a->i[i]--;
112a6ece127SHong Zhang   }
1138f7157efSSatish Balay 
114a6ece127SHong Zhang   PetscFunctionReturn(0);
11549b5e25fSSatish Balay }
11649b5e25fSSatish Balay 
1174a2ae208SSatish Balay #undef __FUNCT__
1184a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqSBAIJ"
119dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqSBAIJ(Mat A)
12049b5e25fSSatish Balay {
12149b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
122dfbe8321SBarry Smith   PetscErrorCode ierr;
12349b5e25fSSatish Balay 
12449b5e25fSSatish Balay   PetscFunctionBegin;
125a9f03627SSatish Balay #if defined(PETSC_USE_LOG)
126d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, NZ=%D",A->rmap->N,a->nz);
127a9f03627SSatish Balay #endif
128e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
1297f53bb6cSHong Zhang   if (a->free_diag){ierr = PetscFree(a->diag);CHKERRQ(ierr);}
1309bfd6278SHong Zhang   if (a->row) {ierr = ISDestroy(a->row);CHKERRQ(ierr);}
1319bfd6278SHong Zhang   if (a->col){ierr = ISDestroy(a->col);CHKERRQ(ierr);}
1329bfd6278SHong Zhang   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
133061b2667SBarry Smith   if (a->idiag) {ierr = PetscFree(a->idiag);CHKERRQ(ierr);}
1340def2e27SBarry Smith   if (a->inode.size) {ierr = PetscFree(a->inode.size);CHKERRQ(ierr);}
135c760cd28SBarry Smith   if (a->free_diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
136c760cd28SBarry Smith   if (a->free_imax_ilen) {ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);}
13705b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
13841f059aeSBarry Smith   ierr = PetscFree(a->sor_work);CHKERRQ(ierr);
13905b42c5fSBarry Smith   ierr = PetscFree(a->solves_work);CHKERRQ(ierr);
14005b42c5fSBarry Smith   ierr = PetscFree(a->mult_work);CHKERRQ(ierr);
14105b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
14205b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
1434da8f245SBarry Smith   if (a->free_jshort) {ierr = PetscFree(a->jshort);CHKERRQ(ierr);}
1441a3463dfSHong Zhang   ierr = PetscFree(a->inew);CHKERRQ(ierr);
1454da8f245SBarry Smith   if (a->parent) {ierr = MatDestroy(a->parent);CHKERRQ(ierr);}
14649b5e25fSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
147901853e0SKris Buschelman 
148dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
149901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
150901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
151901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
152901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqaij_C","",PETSC_NULL);CHKERRQ(ierr);
153901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
154901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
15549b5e25fSSatish Balay   PetscFunctionReturn(0);
15649b5e25fSSatish Balay }
15749b5e25fSSatish Balay 
1584a2ae208SSatish Balay #undef __FUNCT__
1594a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqSBAIJ"
1604e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqSBAIJ(Mat A,MatOption op,PetscTruth flg)
16149b5e25fSSatish Balay {
162045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
16363ba0a88SBarry Smith   PetscErrorCode ierr;
16449b5e25fSSatish Balay 
16549b5e25fSSatish Balay   PetscFunctionBegin;
1664d9d31abSKris Buschelman   switch (op) {
1674d9d31abSKris Buschelman   case MAT_ROW_ORIENTED:
1684e0d8c25SBarry Smith     a->roworiented = flg;
1694d9d31abSKris Buschelman     break;
170a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
171a9817697SBarry Smith     a->keepnonzeropattern = flg;
1724d9d31abSKris Buschelman     break;
173512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
174512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1754d9d31abSKris Buschelman     break;
1764d9d31abSKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
1774e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1784d9d31abSKris Buschelman     break;
1794d9d31abSKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1804e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1814d9d31abSKris Buschelman     break;
18228b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
18328b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
18428b2fa4aSMatthew Knepley     break;
1854e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1864d9d31abSKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1874d9d31abSKris Buschelman   case MAT_USE_HASH_TABLE:
188290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1894d9d31abSKris Buschelman     break;
1909a4540c5SBarry Smith   case MAT_HERMITIAN:
191e32f2f54SBarry Smith     if (!A->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call MatAssemblyEnd() first");
1920bc54ff2SBarry Smith     if (A->cmap->n < 65536 && A->cmap->bs == 1) {
193eeffb40dSHong Zhang       A->ops->mult = MatMult_SeqSBAIJ_1_Hermitian_ushort;
1940bc54ff2SBarry Smith     } else if (A->cmap->bs == 1) {
195eeffb40dSHong Zhang       A->ops->mult = MatMult_SeqSBAIJ_1_Hermitian;
196e32f2f54SBarry Smith     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for Hermitian with block size greater than 1");
197eeffb40dSHong Zhang     break;
198*3d472b54SHong Zhang   case MAT_SPD:
199*3d472b54SHong Zhang     A->spd_set                         = PETSC_TRUE;
200*3d472b54SHong Zhang     A->spd                             = flg;
201*3d472b54SHong Zhang     if (flg) {
202*3d472b54SHong Zhang       A->symmetric                     = PETSC_TRUE;
203*3d472b54SHong Zhang       A->structurally_symmetric        = PETSC_TRUE;
204*3d472b54SHong Zhang       A->symmetric_set                 = PETSC_TRUE;
205*3d472b54SHong Zhang       A->structurally_symmetric_set    = PETSC_TRUE;
206*3d472b54SHong Zhang     }
207*3d472b54SHong Zhang     break;
20877e54ba9SKris Buschelman   case MAT_SYMMETRIC:
20977e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
2109a4540c5SBarry Smith   case MAT_SYMMETRY_ETERNAL:
211e32f2f54SBarry Smith     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix must be symmetric");
212290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s not relevent\n",MatOptions[op]);CHKERRQ(ierr);
213290bbb0aSBarry Smith     break;
214941593c8SHong Zhang   case MAT_IGNORE_LOWER_TRIANGULAR:
2154e0d8c25SBarry Smith     a->ignore_ltriangular = flg;
216941593c8SHong Zhang     break;
217941593c8SHong Zhang   case MAT_ERROR_LOWER_TRIANGULAR:
2184e0d8c25SBarry Smith     a->ignore_ltriangular = flg;
21977e54ba9SKris Buschelman     break;
220f5edf698SHong Zhang   case MAT_GETROW_UPPERTRIANGULAR:
2214e0d8c25SBarry Smith     a->getrow_utriangular = flg;
222f5edf698SHong Zhang     break;
2234d9d31abSKris Buschelman   default:
224e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
22549b5e25fSSatish Balay   }
22649b5e25fSSatish Balay   PetscFunctionReturn(0);
22749b5e25fSSatish Balay }
22849b5e25fSSatish Balay 
2294a2ae208SSatish Balay #undef __FUNCT__
2304a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqSBAIJ"
23113f74950SBarry Smith PetscErrorCode MatGetRow_SeqSBAIJ(Mat A,PetscInt row,PetscInt *ncols,PetscInt **cols,PetscScalar **v)
23249b5e25fSSatish Balay {
23349b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
2346849ba73SBarry Smith   PetscErrorCode ierr;
23513f74950SBarry Smith   PetscInt       itmp,i,j,k,M,*ai,*aj,bs,bn,bp,*cols_i,bs2;
23649b5e25fSSatish Balay   MatScalar      *aa,*aa_i;
23787828ca2SBarry Smith   PetscScalar    *v_i;
23849b5e25fSSatish Balay 
23949b5e25fSSatish Balay   PetscFunctionBegin;
240e32f2f54SBarry 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()");
241f5edf698SHong Zhang   /* Get the upper triangular part of the row */
242d0f46423SBarry Smith   bs  = A->rmap->bs;
24349b5e25fSSatish Balay   ai  = a->i;
24449b5e25fSSatish Balay   aj  = a->j;
24549b5e25fSSatish Balay   aa  = a->a;
24649b5e25fSSatish Balay   bs2 = a->bs2;
24749b5e25fSSatish Balay 
248e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Row %D out of range", row);
24949b5e25fSSatish Balay 
25049b5e25fSSatish Balay   bn  = row/bs;   /* Block number */
25149b5e25fSSatish Balay   bp  = row % bs; /* Block position */
25249b5e25fSSatish Balay   M   = ai[bn+1] - ai[bn];
25349b5e25fSSatish Balay   *ncols = bs*M;
25449b5e25fSSatish Balay 
25549b5e25fSSatish Balay   if (v) {
25649b5e25fSSatish Balay     *v = 0;
25749b5e25fSSatish Balay     if (*ncols) {
25887828ca2SBarry Smith       ierr = PetscMalloc((*ncols+row)*sizeof(PetscScalar),v);CHKERRQ(ierr);
25949b5e25fSSatish Balay       for (i=0; i<M; i++) { /* for each block in the block row */
26049b5e25fSSatish Balay         v_i  = *v + i*bs;
26149b5e25fSSatish Balay         aa_i = aa + bs2*(ai[bn] + i);
26249b5e25fSSatish Balay         for (j=bp,k=0; j<bs2; j+=bs,k++) {v_i[k] = aa_i[j];}
26349b5e25fSSatish Balay       }
26449b5e25fSSatish Balay     }
26549b5e25fSSatish Balay   }
26649b5e25fSSatish Balay 
26749b5e25fSSatish Balay   if (cols) {
26849b5e25fSSatish Balay     *cols = 0;
26949b5e25fSSatish Balay     if (*ncols) {
27013f74950SBarry Smith       ierr = PetscMalloc((*ncols+row)*sizeof(PetscInt),cols);CHKERRQ(ierr);
27149b5e25fSSatish Balay       for (i=0; i<M; i++) { /* for each block in the block row */
27249b5e25fSSatish Balay         cols_i = *cols + i*bs;
27349b5e25fSSatish Balay         itmp  = bs*aj[ai[bn] + i];
27449b5e25fSSatish Balay         for (j=0; j<bs; j++) {cols_i[j] = itmp++;}
27549b5e25fSSatish Balay       }
27649b5e25fSSatish Balay     }
27749b5e25fSSatish Balay   }
27849b5e25fSSatish Balay 
27949b5e25fSSatish Balay   /*search column A(0:row-1,row) (=A(row,0:row-1)). Could be expensive! */
2805ddb2528SHong Zhang   /* this segment is currently removed, so only entries in the upper triangle are obtained */
2815ddb2528SHong Zhang #ifdef column_search
28249b5e25fSSatish Balay   v_i    = *v    + M*bs;
28349b5e25fSSatish Balay   cols_i = *cols + M*bs;
28449b5e25fSSatish Balay   for (i=0; i<bn; i++){ /* for each block row */
28549b5e25fSSatish Balay     M = ai[i+1] - ai[i];
28649b5e25fSSatish Balay     for (j=0; j<M; j++){
28749b5e25fSSatish Balay       itmp = aj[ai[i] + j];    /* block column value */
28849b5e25fSSatish Balay       if (itmp == bn){
28949b5e25fSSatish Balay         aa_i   = aa    + bs2*(ai[i] + j) + bs*bp;
29049b5e25fSSatish Balay         for (k=0; k<bs; k++) {
29149b5e25fSSatish Balay           *cols_i++ = i*bs+k;
29249b5e25fSSatish Balay           *v_i++    = aa_i[k];
29349b5e25fSSatish Balay         }
29449b5e25fSSatish Balay         *ncols += bs;
29549b5e25fSSatish Balay         break;
29649b5e25fSSatish Balay       }
29749b5e25fSSatish Balay     }
29849b5e25fSSatish Balay   }
2995ddb2528SHong Zhang #endif
30049b5e25fSSatish Balay   PetscFunctionReturn(0);
30149b5e25fSSatish Balay }
30249b5e25fSSatish Balay 
3034a2ae208SSatish Balay #undef __FUNCT__
3044a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqSBAIJ"
30513f74950SBarry Smith PetscErrorCode MatRestoreRow_SeqSBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
30649b5e25fSSatish Balay {
307dfbe8321SBarry Smith   PetscErrorCode ierr;
30849b5e25fSSatish Balay 
30949b5e25fSSatish Balay   PetscFunctionBegin;
31005b42c5fSBarry Smith   if (idx) {ierr = PetscFree(*idx);CHKERRQ(ierr);}
31105b42c5fSBarry Smith   if (v)   {ierr = PetscFree(*v);CHKERRQ(ierr);}
31249b5e25fSSatish Balay   PetscFunctionReturn(0);
31349b5e25fSSatish Balay }
31449b5e25fSSatish Balay 
3154a2ae208SSatish Balay #undef __FUNCT__
316f5edf698SHong Zhang #define __FUNCT__ "MatGetRowUpperTriangular_SeqSBAIJ"
317f5edf698SHong Zhang PetscErrorCode MatGetRowUpperTriangular_SeqSBAIJ(Mat A)
318f5edf698SHong Zhang {
319f5edf698SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
320f5edf698SHong Zhang 
321f5edf698SHong Zhang   PetscFunctionBegin;
322f5edf698SHong Zhang   a->getrow_utriangular = PETSC_TRUE;
323f5edf698SHong Zhang   PetscFunctionReturn(0);
324f5edf698SHong Zhang }
325f5edf698SHong Zhang #undef __FUNCT__
326f5edf698SHong Zhang #define __FUNCT__ "MatRestoreRowUpperTriangular_SeqSBAIJ"
327f5edf698SHong Zhang PetscErrorCode MatRestoreRowUpperTriangular_SeqSBAIJ(Mat A)
328f5edf698SHong Zhang {
329f5edf698SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
330f5edf698SHong Zhang 
331f5edf698SHong Zhang   PetscFunctionBegin;
332f5edf698SHong Zhang   a->getrow_utriangular = PETSC_FALSE;
333f5edf698SHong Zhang   PetscFunctionReturn(0);
334f5edf698SHong Zhang }
335f5edf698SHong Zhang 
336f5edf698SHong Zhang #undef __FUNCT__
3374a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqSBAIJ"
338fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqSBAIJ(Mat A,MatReuse reuse,Mat *B)
33949b5e25fSSatish Balay {
340dfbe8321SBarry Smith   PetscErrorCode ierr;
34149b5e25fSSatish Balay   PetscFunctionBegin;
342815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
343999d9058SBarry Smith     ierr = MatDuplicate(A,MAT_COPY_VALUES,B);CHKERRQ(ierr);
344fc4dec0aSBarry Smith   }
3458115998fSBarry Smith   PetscFunctionReturn(0);
34649b5e25fSSatish Balay }
34749b5e25fSSatish Balay 
3484a2ae208SSatish Balay #undef __FUNCT__
3494a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_ASCII"
3506849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_ASCII(Mat A,PetscViewer viewer)
35149b5e25fSSatish Balay {
35249b5e25fSSatish Balay   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
353dfbe8321SBarry Smith   PetscErrorCode    ierr;
354d0f46423SBarry Smith   PetscInt          i,j,bs = A->rmap->bs,k,l,bs2=a->bs2;
3552dcb1b2aSMatthew Knepley   const char        *name;
356f3ef73ceSBarry Smith   PetscViewerFormat format;
35749b5e25fSSatish Balay 
35849b5e25fSSatish Balay   PetscFunctionBegin;
35980fe4e49SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
360b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
361456192e2SBarry Smith   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
36277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);CHKERRQ(ierr);
363fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
364d2507d54SMatthew Knepley     Mat aij;
365d2507d54SMatthew Knepley 
366d5f3da31SBarry Smith     if (A->factortype && bs>1){
36770d5e725SHong 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);
36870d5e725SHong Zhang       PetscFunctionReturn(0);
36970d5e725SHong Zhang     }
370c9f458caSMatthew Knepley     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&aij);CHKERRQ(ierr);
371c9f458caSMatthew Knepley     ierr = MatView(aij,viewer);CHKERRQ(ierr);
372c9f458caSMatthew Knepley     ierr = MatDestroy(aij);CHKERRQ(ierr);
373fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
374b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
37549b5e25fSSatish Balay     for (i=0; i<a->mbs; i++) {
37649b5e25fSSatish Balay       for (j=0; j<bs; j++) {
37777431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
37849b5e25fSSatish Balay         for (k=a->i[i]; k<a->i[i+1]; k++) {
37949b5e25fSSatish Balay           for (l=0; l<bs; l++) {
38049b5e25fSSatish Balay #if defined(PETSC_USE_COMPLEX)
38149b5e25fSSatish Balay             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
382a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",bs*a->j[k]+l,
38349b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
38449b5e25fSSatish Balay             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
385a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",bs*a->j[k]+l,
38649b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),-PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
38749b5e25fSSatish Balay             } else if (PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
388a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
38949b5e25fSSatish Balay             }
39049b5e25fSSatish Balay #else
39149b5e25fSSatish Balay             if (a->a[bs2*k + l*bs + j] != 0.0) {
392a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
39349b5e25fSSatish Balay             }
39449b5e25fSSatish Balay #endif
39549b5e25fSSatish Balay           }
39649b5e25fSSatish Balay         }
397b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
39849b5e25fSSatish Balay       }
39949b5e25fSSatish Balay     }
400b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
401c1490034SHong Zhang   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
402c1490034SHong Zhang      PetscFunctionReturn(0);
40349b5e25fSSatish Balay   } else {
404d5f3da31SBarry Smith     if (A->factortype && bs>1){
4058608aa04SHong Zhang       ierr = PetscPrintf(PETSC_COMM_SELF,"Warning: matrix is factored. MatView_SeqSBAIJ_ASCII() may not display complete or logically correct entries!\n");CHKERRQ(ierr);
4068608aa04SHong Zhang     }
407b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
40849b5e25fSSatish Balay     for (i=0; i<a->mbs; i++) {
40949b5e25fSSatish Balay       for (j=0; j<bs; j++) {
41077431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
41149b5e25fSSatish Balay         for (k=a->i[i]; k<a->i[i+1]; k++) {
41249b5e25fSSatish Balay           for (l=0; l<bs; l++) {
41349b5e25fSSatish Balay #if defined(PETSC_USE_COMPLEX)
41449b5e25fSSatish Balay             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0) {
415a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",bs*a->j[k]+l,
41649b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
41749b5e25fSSatish Balay             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0) {
418a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",bs*a->j[k]+l,
41949b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),-PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
42049b5e25fSSatish Balay             } else {
421a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
42249b5e25fSSatish Balay             }
42349b5e25fSSatish Balay #else
424e9f7bc9eSHong Zhang             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
42549b5e25fSSatish Balay #endif
42649b5e25fSSatish Balay           }
42749b5e25fSSatish Balay         }
428b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
42949b5e25fSSatish Balay       }
43049b5e25fSSatish Balay     }
431b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
43249b5e25fSSatish Balay   }
433b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
43449b5e25fSSatish Balay   PetscFunctionReturn(0);
43549b5e25fSSatish Balay }
43649b5e25fSSatish Balay 
4374a2ae208SSatish Balay #undef __FUNCT__
4384a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_Draw_Zoom"
4396849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
44049b5e25fSSatish Balay {
44149b5e25fSSatish Balay   Mat            A = (Mat) Aa;
44249b5e25fSSatish Balay   Mat_SeqSBAIJ   *a=(Mat_SeqSBAIJ*)A->data;
4436849ba73SBarry Smith   PetscErrorCode ierr;
444d0f46423SBarry Smith   PetscInt       row,i,j,k,l,mbs=a->mbs,color,bs=A->rmap->bs,bs2=a->bs2;
44513f74950SBarry Smith   PetscMPIInt    rank;
44649b5e25fSSatish Balay   PetscReal      xl,yl,xr,yr,x_l,x_r,y_l,y_r;
44749b5e25fSSatish Balay   MatScalar      *aa;
44849b5e25fSSatish Balay   MPI_Comm       comm;
449b0a32e0cSBarry Smith   PetscViewer    viewer;
45049b5e25fSSatish Balay 
45149b5e25fSSatish Balay   PetscFunctionBegin;
45249b5e25fSSatish Balay   /*
45349b5e25fSSatish Balay     This is nasty. If this is called from an originally parallel matrix
45449b5e25fSSatish Balay     then all processes call this,but only the first has the matrix so the
45549b5e25fSSatish Balay     rest should return immediately.
45649b5e25fSSatish Balay   */
45749b5e25fSSatish Balay   ierr = PetscObjectGetComm((PetscObject)draw,&comm);CHKERRQ(ierr);
45849b5e25fSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
45949b5e25fSSatish Balay   if (rank) PetscFunctionReturn(0);
46049b5e25fSSatish Balay 
46149b5e25fSSatish Balay   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
46249b5e25fSSatish Balay 
463b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
464b0a32e0cSBarry Smith   PetscDrawString(draw, .3*(xl+xr), .3*(yl+yr), PETSC_DRAW_BLACK, "symmetric");
46549b5e25fSSatish Balay 
46649b5e25fSSatish Balay   /* loop over matrix elements drawing boxes */
467b0a32e0cSBarry Smith   color = PETSC_DRAW_BLUE;
46849b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
46949b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
470d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
47149b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
47249b5e25fSSatish Balay       aa = a->a + j*bs2;
47349b5e25fSSatish Balay       for (k=0; k<bs; k++) {
47449b5e25fSSatish Balay         for (l=0; l<bs; l++) {
47549b5e25fSSatish Balay           if (PetscRealPart(*aa++) >=  0.) continue;
476b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
47749b5e25fSSatish Balay         }
47849b5e25fSSatish Balay       }
47949b5e25fSSatish Balay     }
48049b5e25fSSatish Balay   }
481b0a32e0cSBarry Smith   color = PETSC_DRAW_CYAN;
48249b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
48349b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
484d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
48549b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
48649b5e25fSSatish Balay       aa = a->a + j*bs2;
48749b5e25fSSatish Balay       for (k=0; k<bs; k++) {
48849b5e25fSSatish Balay         for (l=0; l<bs; l++) {
48949b5e25fSSatish Balay           if (PetscRealPart(*aa++) != 0.) continue;
490b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
49149b5e25fSSatish Balay         }
49249b5e25fSSatish Balay       }
49349b5e25fSSatish Balay     }
49449b5e25fSSatish Balay   }
49549b5e25fSSatish Balay 
496b0a32e0cSBarry Smith   color = PETSC_DRAW_RED;
49749b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
49849b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
499d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
50049b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
50149b5e25fSSatish Balay       aa = a->a + j*bs2;
50249b5e25fSSatish Balay       for (k=0; k<bs; k++) {
50349b5e25fSSatish Balay         for (l=0; l<bs; l++) {
50449b5e25fSSatish Balay           if (PetscRealPart(*aa++) <= 0.) continue;
505b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
50649b5e25fSSatish Balay         }
50749b5e25fSSatish Balay       }
50849b5e25fSSatish Balay     }
50949b5e25fSSatish Balay   }
51049b5e25fSSatish Balay   PetscFunctionReturn(0);
51149b5e25fSSatish Balay }
51249b5e25fSSatish Balay 
5134a2ae208SSatish Balay #undef __FUNCT__
5144a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_Draw"
5156849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_Draw(Mat A,PetscViewer viewer)
51649b5e25fSSatish Balay {
517dfbe8321SBarry Smith   PetscErrorCode ierr;
51849b5e25fSSatish Balay   PetscReal      xl,yl,xr,yr,w,h;
519b0a32e0cSBarry Smith   PetscDraw      draw;
52049b5e25fSSatish Balay   PetscTruth     isnull;
52149b5e25fSSatish Balay 
52249b5e25fSSatish Balay   PetscFunctionBegin;
523b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
524b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
52549b5e25fSSatish Balay 
52649b5e25fSSatish Balay   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
527d0f46423SBarry Smith   xr  = A->rmap->N; yr = A->rmap->N; h = yr/10.0; w = xr/10.0;
52849b5e25fSSatish Balay   xr += w;    yr += h;  xl = -w;     yl = -h;
529b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
530b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqSBAIJ_Draw_Zoom,A);CHKERRQ(ierr);
53149b5e25fSSatish Balay   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
53249b5e25fSSatish Balay   PetscFunctionReturn(0);
53349b5e25fSSatish Balay }
53449b5e25fSSatish Balay 
5354a2ae208SSatish Balay #undef __FUNCT__
5364a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ"
537dfbe8321SBarry Smith PetscErrorCode MatView_SeqSBAIJ(Mat A,PetscViewer viewer)
53849b5e25fSSatish Balay {
539dfbe8321SBarry Smith   PetscErrorCode ierr;
54032077d6dSBarry Smith   PetscTruth     iascii,isdraw;
54108917f38SBarry Smith   FILE           *file = 0;
54249b5e25fSSatish Balay 
54349b5e25fSSatish Balay   PetscFunctionBegin;
5442692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
5452692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
54632077d6dSBarry Smith   if (iascii){
54749b5e25fSSatish Balay     ierr = MatView_SeqSBAIJ_ASCII(A,viewer);CHKERRQ(ierr);
54849b5e25fSSatish Balay   } else if (isdraw) {
54949b5e25fSSatish Balay     ierr = MatView_SeqSBAIJ_Draw(A,viewer);CHKERRQ(ierr);
55049b5e25fSSatish Balay   } else {
551a5e6ed63SBarry Smith     Mat B;
552ceb03754SKris Buschelman     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&B);CHKERRQ(ierr);
553a5e6ed63SBarry Smith     ierr = MatView(B,viewer);CHKERRQ(ierr);
554a5e6ed63SBarry Smith     ierr = MatDestroy(B);CHKERRQ(ierr);
55508917f38SBarry Smith     ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
55608917f38SBarry Smith     if (file) {
55708917f38SBarry Smith       fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
55808917f38SBarry Smith     }
55949b5e25fSSatish Balay   }
56049b5e25fSSatish Balay   PetscFunctionReturn(0);
56149b5e25fSSatish Balay }
56249b5e25fSSatish Balay 
56349b5e25fSSatish Balay 
5644a2ae208SSatish Balay #undef __FUNCT__
5654a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqSBAIJ"
56613f74950SBarry Smith PetscErrorCode MatGetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
56749b5e25fSSatish Balay {
568045c9aa0SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
56913f74950SBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
57013f74950SBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
571d0f46423SBarry Smith   PetscInt     brow,bcol,ridx,cidx,bs=A->rmap->bs,bs2=a->bs2;
57297e567efSBarry Smith   MatScalar    *ap,*aa = a->a;
57349b5e25fSSatish Balay 
57449b5e25fSSatish Balay   PetscFunctionBegin;
57549b5e25fSSatish Balay   for (k=0; k<m; k++) { /* loop over rows */
57649b5e25fSSatish Balay     row  = im[k]; brow = row/bs;
577e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
578e32f2f54SBarry 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);
57949b5e25fSSatish Balay     rp   = aj + ai[brow] ; ap = aa + bs2*ai[brow] ;
58049b5e25fSSatish Balay     nrow = ailen[brow];
58149b5e25fSSatish Balay     for (l=0; l<n; l++) { /* loop over columns */
582e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
583e32f2f54SBarry 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);
58449b5e25fSSatish Balay       col  = in[l] ;
58549b5e25fSSatish Balay       bcol = col/bs;
58649b5e25fSSatish Balay       cidx = col%bs;
58749b5e25fSSatish Balay       ridx = row%bs;
58849b5e25fSSatish Balay       high = nrow;
58949b5e25fSSatish Balay       low  = 0; /* assume unsorted */
59049b5e25fSSatish Balay       while (high-low > 5) {
59149b5e25fSSatish Balay         t = (low+high)/2;
59249b5e25fSSatish Balay         if (rp[t] > bcol) high = t;
59349b5e25fSSatish Balay         else             low  = t;
59449b5e25fSSatish Balay       }
59549b5e25fSSatish Balay       for (i=low; i<high; i++) {
59649b5e25fSSatish Balay         if (rp[i] > bcol) break;
59749b5e25fSSatish Balay         if (rp[i] == bcol) {
59849b5e25fSSatish Balay           *v++ = ap[bs2*i+bs*cidx+ridx];
59949b5e25fSSatish Balay           goto finished;
60049b5e25fSSatish Balay         }
60149b5e25fSSatish Balay       }
60297e567efSBarry Smith       *v++ = 0.0;
60349b5e25fSSatish Balay       finished:;
60449b5e25fSSatish Balay     }
60549b5e25fSSatish Balay   }
60649b5e25fSSatish Balay   PetscFunctionReturn(0);
60749b5e25fSSatish Balay }
60849b5e25fSSatish Balay 
60949b5e25fSSatish Balay 
6104a2ae208SSatish Balay #undef __FUNCT__
6114a2ae208SSatish Balay #define __FUNCT__ "MatSetValuesBlocked_SeqSBAIJ"
61213f74950SBarry Smith PetscErrorCode MatSetValuesBlocked_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
61349b5e25fSSatish Balay {
6140880e062SHong Zhang   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
6156849ba73SBarry Smith   PetscErrorCode    ierr;
616e2ee6c50SBarry Smith   PetscInt          *rp,k,low,high,t,ii,jj,row,nrow,i,col,l,rmax,N,lastcol = -1;
61713f74950SBarry Smith   PetscInt          *imax=a->imax,*ai=a->i,*ailen=a->ilen;
618d0f46423SBarry Smith   PetscInt          *aj=a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs,stepval;
6190880e062SHong Zhang   PetscTruth        roworiented=a->roworiented;
620dd6ea824SBarry Smith   const PetscScalar *value = v;
621f15d580aSBarry Smith   MatScalar         *ap,*aa = a->a,*bap;
6220880e062SHong Zhang 
62349b5e25fSSatish Balay   PetscFunctionBegin;
6240880e062SHong Zhang   if (roworiented) {
6250880e062SHong Zhang     stepval = (n-1)*bs;
6260880e062SHong Zhang   } else {
6270880e062SHong Zhang     stepval = (m-1)*bs;
6280880e062SHong Zhang   }
6290880e062SHong Zhang   for (k=0; k<m; k++) { /* loop over added rows */
6300880e062SHong Zhang     row  = im[k];
6310880e062SHong Zhang     if (row < 0) continue;
6322515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
633e32f2f54SBarry Smith     if (row >= a->mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,a->mbs-1);
6340880e062SHong Zhang #endif
6350880e062SHong Zhang     rp   = aj + ai[row];
6360880e062SHong Zhang     ap   = aa + bs2*ai[row];
6370880e062SHong Zhang     rmax = imax[row];
6380880e062SHong Zhang     nrow = ailen[row];
6390880e062SHong Zhang     low  = 0;
640818f2c47SBarry Smith     high = nrow;
6410880e062SHong Zhang     for (l=0; l<n; l++) { /* loop over added columns */
6420880e062SHong Zhang       if (in[l] < 0) continue;
6430880e062SHong Zhang       col = in[l];
6442515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
645e32f2f54SBarry Smith       if (col >= a->nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",col,a->nbs-1);
646b1823623SSatish Balay #endif
647b98bf0e1SJed Brown       if (col < row) {
648b98bf0e1SJed Brown         if (a->ignore_ltriangular) {
649b98bf0e1SJed Brown           continue; /* ignore lower triangular block */
650b98bf0e1SJed Brown         } else {
651e32f2f54SBarry Smith           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)");
652b98bf0e1SJed Brown         }
653b98bf0e1SJed Brown       }
6540880e062SHong Zhang       if (roworiented) {
6550880e062SHong Zhang         value = v + k*(stepval+bs)*bs + l*bs;
6560880e062SHong Zhang       } else {
6570880e062SHong Zhang         value = v + l*(stepval+bs)*bs + k*bs;
6580880e062SHong Zhang       }
6597cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
660e2ee6c50SBarry Smith       lastcol = col;
6610880e062SHong Zhang       while (high-low > 7) {
6620880e062SHong Zhang         t = (low+high)/2;
6630880e062SHong Zhang         if (rp[t] > col) high = t;
6640880e062SHong Zhang         else             low  = t;
6650880e062SHong Zhang       }
6660880e062SHong Zhang       for (i=low; i<high; i++) {
6670880e062SHong Zhang         if (rp[i] > col) break;
6680880e062SHong Zhang         if (rp[i] == col) {
6690880e062SHong Zhang           bap  = ap +  bs2*i;
6700880e062SHong Zhang           if (roworiented) {
6710880e062SHong Zhang             if (is == ADD_VALUES) {
6720880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6730880e062SHong Zhang                 for (jj=ii; jj<bs2; jj+=bs) {
6740880e062SHong Zhang                   bap[jj] += *value++;
6750880e062SHong Zhang                 }
6760880e062SHong Zhang               }
6770880e062SHong Zhang             } else {
6780880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6790880e062SHong Zhang                 for (jj=ii; jj<bs2; jj+=bs) {
6800880e062SHong Zhang                   bap[jj] = *value++;
6810880e062SHong Zhang                 }
6820880e062SHong Zhang                }
6830880e062SHong Zhang             }
6840880e062SHong Zhang           } else {
6850880e062SHong Zhang             if (is == ADD_VALUES) {
6860880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6870880e062SHong Zhang                 for (jj=0; jj<bs; jj++) {
6880880e062SHong Zhang                   *bap++ += *value++;
6890880e062SHong Zhang                 }
6900880e062SHong Zhang               }
6910880e062SHong Zhang             } else {
6920880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6930880e062SHong Zhang                 for (jj=0; jj<bs; jj++) {
6940880e062SHong Zhang                   *bap++  = *value++;
6950880e062SHong Zhang                 }
6960880e062SHong Zhang               }
6970880e062SHong Zhang             }
6980880e062SHong Zhang           }
6990880e062SHong Zhang           goto noinsert2;
7000880e062SHong Zhang         }
7010880e062SHong Zhang       }
7020880e062SHong Zhang       if (nonew == 1) goto noinsert2;
703e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
704fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
705c03d1d03SSatish Balay       N = nrow++ - 1; high++;
7060880e062SHong Zhang       /* shift up all the later entries in this row */
7070880e062SHong Zhang       for (ii=N; ii>=i; ii--) {
7080880e062SHong Zhang         rp[ii+1] = rp[ii];
7090880e062SHong Zhang         ierr = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
7100880e062SHong Zhang       }
7110880e062SHong Zhang       if (N >= i) {
7120880e062SHong Zhang         ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
7130880e062SHong Zhang       }
7140880e062SHong Zhang       rp[i] = col;
7150880e062SHong Zhang       bap   = ap +  bs2*i;
7160880e062SHong Zhang       if (roworiented) {
7170880e062SHong Zhang         for (ii=0; ii<bs; ii++,value+=stepval) {
7180880e062SHong Zhang           for (jj=ii; jj<bs2; jj+=bs) {
7190880e062SHong Zhang             bap[jj] = *value++;
7200880e062SHong Zhang           }
7210880e062SHong Zhang         }
7220880e062SHong Zhang       } else {
7230880e062SHong Zhang         for (ii=0; ii<bs; ii++,value+=stepval) {
7240880e062SHong Zhang           for (jj=0; jj<bs; jj++) {
7250880e062SHong Zhang             *bap++  = *value++;
7260880e062SHong Zhang           }
7270880e062SHong Zhang         }
7280880e062SHong Zhang        }
7290880e062SHong Zhang     noinsert2:;
7300880e062SHong Zhang       low = i;
7310880e062SHong Zhang     }
7320880e062SHong Zhang     ailen[row] = nrow;
7330880e062SHong Zhang   }
7340880e062SHong Zhang    PetscFunctionReturn(0);
73549b5e25fSSatish Balay }
73649b5e25fSSatish Balay 
73764831d72SBarry Smith /*
73864831d72SBarry Smith     This is not yet used
73964831d72SBarry Smith */
7404a2ae208SSatish Balay #undef __FUNCT__
7414108e4d5SBarry Smith #define __FUNCT__ "MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode"
7424108e4d5SBarry Smith PetscErrorCode MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode(Mat A)
7430def2e27SBarry Smith {
7440def2e27SBarry Smith   Mat_SeqSBAIJ    *a = (Mat_SeqSBAIJ*)A->data;
7450def2e27SBarry Smith   PetscErrorCode  ierr;
7460def2e27SBarry Smith   const PetscInt  *ai = a->i, *aj = a->j,*cols;
7470def2e27SBarry Smith   PetscInt        i = 0,j,blk_size,m = A->rmap->n,node_count = 0,nzx,nzy,*ns,row,nz,cnt,cnt2,*counts;
7480def2e27SBarry Smith   PetscTruth      flag;
7490def2e27SBarry Smith 
7500def2e27SBarry Smith   PetscFunctionBegin;
7510def2e27SBarry Smith   ierr = PetscMalloc(m*sizeof(PetscInt),&ns);CHKERRQ(ierr);
7520def2e27SBarry Smith   while (i < m){
7530def2e27SBarry Smith     nzx = ai[i+1] - ai[i];       /* Number of nonzeros */
7540def2e27SBarry Smith     /* Limits the number of elements in a node to 'a->inode.limit' */
7550def2e27SBarry Smith     for (j=i+1,blk_size=1; j<m && blk_size <a->inode.limit; ++j,++blk_size) {
7560def2e27SBarry Smith       nzy  = ai[j+1] - ai[j];
7570def2e27SBarry Smith       if (nzy != (nzx - j + i)) break;
7580def2e27SBarry Smith       ierr = PetscMemcmp(aj + ai[i] + j - i,aj + ai[j],nzy*sizeof(PetscInt),&flag);CHKERRQ(ierr);
7590def2e27SBarry Smith       if (!flag) break;
7600def2e27SBarry Smith     }
7610def2e27SBarry Smith     ns[node_count++] = blk_size;
7620def2e27SBarry Smith     i = j;
7630def2e27SBarry Smith   }
7640def2e27SBarry Smith   if (!a->inode.size && m && node_count > .9*m) {
7650def2e27SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
7660def2e27SBarry Smith     ierr = PetscInfo2(A,"Found %D nodes out of %D rows. Not using Inode routines\n",node_count,m);CHKERRQ(ierr);
7670def2e27SBarry Smith   } else {
7680def2e27SBarry Smith     a->inode.node_count = node_count;
7690def2e27SBarry Smith     ierr = PetscMalloc(node_count*sizeof(PetscInt),&a->inode.size);CHKERRQ(ierr);
770c760cd28SBarry Smith     ierr = PetscLogObjectMemory(A,node_count*sizeof(PetscInt));CHKERRQ(ierr);
7710def2e27SBarry Smith     ierr = PetscMemcpy(a->inode.size,ns,node_count*sizeof(PetscInt));
7720def2e27SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
7730def2e27SBarry Smith     ierr = PetscInfo3(A,"Found %D nodes of %D. Limit used: %D. Using Inode routines\n",node_count,m,a->inode.limit);CHKERRQ(ierr);
7740def2e27SBarry Smith 
7750def2e27SBarry Smith     /* count collections of adjacent columns in each inode */
7760def2e27SBarry Smith     row = 0;
7770def2e27SBarry Smith     cnt = 0;
7780def2e27SBarry Smith     for (i=0; i<node_count; i++) {
7790def2e27SBarry Smith       cols = aj + ai[row] + a->inode.size[i];
7800def2e27SBarry Smith       nz   = ai[row+1] - ai[row] - a->inode.size[i];
7810def2e27SBarry Smith       for (j=1; j<nz; j++) {
7820def2e27SBarry Smith         if (cols[j] != cols[j-1]+1) {
7830def2e27SBarry Smith           cnt++;
7840def2e27SBarry Smith         }
7850def2e27SBarry Smith       }
7860def2e27SBarry Smith       cnt++;
7870def2e27SBarry Smith       row += a->inode.size[i];
7880def2e27SBarry Smith     }
7890def2e27SBarry Smith     ierr = PetscMalloc(2*cnt*sizeof(PetscInt),&counts);CHKERRQ(ierr);
7900def2e27SBarry Smith     cnt = 0;
7910def2e27SBarry Smith     row = 0;
7920def2e27SBarry Smith     for (i=0; i<node_count; i++) {
7930def2e27SBarry Smith       cols          = aj + ai[row] + a->inode.size[i];
7940def2e27SBarry Smith 	  CHKMEMQ;
7950def2e27SBarry Smith       counts[2*cnt] = cols[0];
7960def2e27SBarry Smith 	  CHKMEMQ;
7970def2e27SBarry Smith       nz            = ai[row+1] - ai[row] - a->inode.size[i];
7980def2e27SBarry Smith       cnt2          = 1;
7990def2e27SBarry Smith       for (j=1; j<nz; j++) {
8000def2e27SBarry Smith         if (cols[j] != cols[j-1]+1) {
8010def2e27SBarry Smith 	  CHKMEMQ;
8020def2e27SBarry Smith           counts[2*(cnt++)+1] = cnt2;
8030def2e27SBarry Smith           counts[2*cnt]       = cols[j];
8040def2e27SBarry Smith 	  CHKMEMQ;
8050def2e27SBarry Smith           cnt2                = 1;
8060def2e27SBarry Smith         } else cnt2++;
8070def2e27SBarry Smith       }
8080def2e27SBarry Smith 	  CHKMEMQ;
8090def2e27SBarry Smith       counts[2*(cnt++)+1] = cnt2;
8100def2e27SBarry Smith 	  CHKMEMQ;
8110def2e27SBarry Smith       row += a->inode.size[i];
8120def2e27SBarry Smith     }
8130def2e27SBarry Smith     ierr = PetscIntView(2*cnt,counts,0);
8140def2e27SBarry Smith   }
81538702af4SBarry Smith   PetscFunctionReturn(0);
81638702af4SBarry Smith }
81738702af4SBarry Smith 
81838702af4SBarry Smith #undef __FUNCT__
8194a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqSBAIJ"
820dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqSBAIJ(Mat A,MatAssemblyType mode)
82149b5e25fSSatish Balay {
82249b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
8236849ba73SBarry Smith   PetscErrorCode ierr;
82413f74950SBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
825d0f46423SBarry Smith   PetscInt       m = A->rmap->N,*ip,N,*ailen = a->ilen;
82613f74950SBarry Smith   PetscInt       mbs = a->mbs,bs2 = a->bs2,rmax = 0;
82749b5e25fSSatish Balay   MatScalar      *aa = a->a,*ap;
82849b5e25fSSatish Balay 
82949b5e25fSSatish Balay   PetscFunctionBegin;
83049b5e25fSSatish Balay   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
83149b5e25fSSatish Balay 
83249b5e25fSSatish Balay   if (m) rmax = ailen[0];
83349b5e25fSSatish Balay   for (i=1; i<mbs; i++) {
83449b5e25fSSatish Balay     /* move each row back by the amount of empty slots (fshift) before it*/
83549b5e25fSSatish Balay     fshift += imax[i-1] - ailen[i-1];
83649b5e25fSSatish Balay      rmax   = PetscMax(rmax,ailen[i]);
83749b5e25fSSatish Balay      if (fshift) {
83849b5e25fSSatish Balay        ip = aj + ai[i]; ap = aa + bs2*ai[i];
83949b5e25fSSatish Balay        N = ailen[i];
84049b5e25fSSatish Balay        for (j=0; j<N; j++) {
84149b5e25fSSatish Balay          ip[j-fshift] = ip[j];
84249b5e25fSSatish Balay          ierr = PetscMemcpy(ap+(j-fshift)*bs2,ap+j*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
84349b5e25fSSatish Balay        }
84449b5e25fSSatish Balay      }
84549b5e25fSSatish Balay      ai[i] = ai[i-1] + ailen[i-1];
84649b5e25fSSatish Balay   }
84749b5e25fSSatish Balay   if (mbs) {
84849b5e25fSSatish Balay     fshift += imax[mbs-1] - ailen[mbs-1];
84949b5e25fSSatish Balay      ai[mbs] = ai[mbs-1] + ailen[mbs-1];
85049b5e25fSSatish Balay   }
85149b5e25fSSatish Balay   /* reset ilen and imax for each row */
85249b5e25fSSatish Balay   for (i=0; i<mbs; i++) {
85349b5e25fSSatish Balay     ailen[i] = imax[i] = ai[i+1] - ai[i];
85449b5e25fSSatish Balay   }
8556c6c5352SBarry Smith   a->nz = ai[mbs];
85649b5e25fSSatish Balay 
857b424e231SHong Zhang   /* diagonals may have moved, reset it */
858b424e231SHong Zhang   if (a->diag) {
8592ed38d0bSJed Brown     ierr = PetscMemcpy(a->diag,ai,mbs*sizeof(PetscInt));CHKERRQ(ierr);
86049b5e25fSSatish Balay   }
86128b2fa4aSMatthew Knepley   if (fshift && a->nounused == -1) {
862e32f2f54SBarry Smith     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);
86328b2fa4aSMatthew Knepley   }
864d0f46423SBarry 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);
865ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues is %D\n",a->reallocs);CHKERRQ(ierr);
866ae15b995SBarry Smith   ierr = PetscInfo1(A,"Most nonzeros blocks in any row is %D\n",rmax);CHKERRQ(ierr);
8678e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
86849b5e25fSSatish Balay   a->reallocs          = 0;
86949b5e25fSSatish Balay   A->info.nz_unneeded  = (PetscReal)fshift*bs2;
870061b2667SBarry Smith   a->idiagvalid = PETSC_FALSE;
87138702af4SBarry Smith 
87238702af4SBarry Smith   if (A->cmap->n < 65536 && A->cmap->bs == 1) {
87338702af4SBarry Smith     if (!a->jshort) {
87438702af4SBarry Smith       ierr = PetscMalloc(a->i[A->rmap->n]*sizeof(unsigned short),&a->jshort);CHKERRQ(ierr);
875c760cd28SBarry Smith       ierr = PetscLogObjectMemory(A,a->i[A->rmap->n]*sizeof(unsigned short));CHKERRQ(ierr);
87638702af4SBarry Smith       for (i=0; i<a->i[A->rmap->n]; i++) a->jshort[i] = a->j[i];
87738702af4SBarry Smith       A->ops->mult  = MatMult_SeqSBAIJ_1_ushort;
87841f059aeSBarry Smith       A->ops->sor = MatSOR_SeqSBAIJ_ushort;
8794da8f245SBarry Smith       a->free_jshort = PETSC_TRUE;
88038702af4SBarry Smith     }
88138702af4SBarry Smith   }
88249b5e25fSSatish Balay   PetscFunctionReturn(0);
88349b5e25fSSatish Balay }
88449b5e25fSSatish Balay 
88549b5e25fSSatish Balay /*
88649b5e25fSSatish Balay    This function returns an array of flags which indicate the locations of contiguous
88749b5e25fSSatish Balay    blocks that should be zeroed. for eg: if bs = 3  and is = [0,1,2,3,5,6,7,8,9]
88849b5e25fSSatish Balay    then the resulting sizes = [3,1,1,3,1] correspondig to sets [(0,1,2),(3),(5),(6,7,8),(9)]
88949b5e25fSSatish Balay    Assume: sizes should be long enough to hold all the values.
89049b5e25fSSatish Balay */
8914a2ae208SSatish Balay #undef __FUNCT__
8924a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqSBAIJ_Check_Blocks"
89313f74950SBarry Smith PetscErrorCode MatZeroRows_SeqSBAIJ_Check_Blocks(PetscInt idx[],PetscInt n,PetscInt bs,PetscInt sizes[], PetscInt *bs_max)
89449b5e25fSSatish Balay {
89513f74950SBarry Smith   PetscInt   i,j,k,row;
89649b5e25fSSatish Balay   PetscTruth flg;
89749b5e25fSSatish Balay 
89849b5e25fSSatish Balay   PetscFunctionBegin;
89949b5e25fSSatish Balay    for (i=0,j=0; i<n; j++) {
90049b5e25fSSatish Balay      row = idx[i];
90149b5e25fSSatish Balay      if (row%bs!=0) { /* Not the begining of a block */
90249b5e25fSSatish Balay        sizes[j] = 1;
90349b5e25fSSatish Balay        i++;
90449b5e25fSSatish Balay      } else if (i+bs > n) { /* Beginning of a block, but complete block doesn't exist (at idx end) */
90549b5e25fSSatish Balay        sizes[j] = 1;         /* Also makes sure atleast 'bs' values exist for next else */
90649b5e25fSSatish Balay        i++;
90749b5e25fSSatish Balay      } else { /* Begining of the block, so check if the complete block exists */
90849b5e25fSSatish Balay        flg = PETSC_TRUE;
90949b5e25fSSatish Balay        for (k=1; k<bs; k++) {
91049b5e25fSSatish Balay          if (row+k != idx[i+k]) { /* break in the block */
91149b5e25fSSatish Balay            flg = PETSC_FALSE;
91249b5e25fSSatish Balay            break;
91349b5e25fSSatish Balay          }
91449b5e25fSSatish Balay        }
915abc0a331SBarry Smith        if (flg) { /* No break in the bs */
91649b5e25fSSatish Balay          sizes[j] = bs;
91749b5e25fSSatish Balay          i+= bs;
91849b5e25fSSatish Balay        } else {
91949b5e25fSSatish Balay          sizes[j] = 1;
92049b5e25fSSatish Balay          i++;
92149b5e25fSSatish Balay        }
92249b5e25fSSatish Balay      }
92349b5e25fSSatish Balay    }
92449b5e25fSSatish Balay    *bs_max = j;
92549b5e25fSSatish Balay    PetscFunctionReturn(0);
92649b5e25fSSatish Balay }
92749b5e25fSSatish Balay 
92849b5e25fSSatish Balay 
92949b5e25fSSatish Balay /* Only add/insert a(i,j) with i<=j (blocks).
93049b5e25fSSatish Balay    Any a(i,j) with i>j input by user is ingored.
93149b5e25fSSatish Balay */
93249b5e25fSSatish Balay 
9334a2ae208SSatish Balay #undef __FUNCT__
9344a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqSBAIJ"
93513f74950SBarry Smith PetscErrorCode MatSetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
93649b5e25fSSatish Balay {
93749b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
9386849ba73SBarry Smith   PetscErrorCode ierr;
939e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,lastcol = -1;
94013f74950SBarry Smith   PetscInt       *imax=a->imax,*ai=a->i,*ailen=a->ilen,roworiented=a->roworiented;
941d0f46423SBarry Smith   PetscInt       *aj=a->j,nonew=a->nonew,bs=A->rmap->bs,brow,bcol;
94213f74950SBarry Smith   PetscInt       ridx,cidx,bs2=a->bs2;
94349b5e25fSSatish Balay   MatScalar      *ap,value,*aa=a->a,*bap;
94449b5e25fSSatish Balay 
94549b5e25fSSatish Balay   PetscFunctionBegin;
94671fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
94749b5e25fSSatish Balay   for (k=0; k<m; k++) { /* loop over added rows */
94849b5e25fSSatish Balay     row  = im[k];       /* row number */
94949b5e25fSSatish Balay     brow = row/bs;      /* block row number */
95049b5e25fSSatish Balay     if (row < 0) continue;
9512515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
952e32f2f54SBarry 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);
95349b5e25fSSatish Balay #endif
95449b5e25fSSatish Balay     rp   = aj + ai[brow]; /*ptr to beginning of column value of the row block*/
95549b5e25fSSatish Balay     ap   = aa + bs2*ai[brow]; /*ptr to beginning of element value of the row block*/
95649b5e25fSSatish Balay     rmax = imax[brow];  /* maximum space allocated for this row */
95749b5e25fSSatish Balay     nrow = ailen[brow]; /* actual length of this row */
95849b5e25fSSatish Balay     low  = 0;
95949b5e25fSSatish Balay 
96049b5e25fSSatish Balay     for (l=0; l<n; l++) { /* loop over added columns */
96149b5e25fSSatish Balay       if (in[l] < 0) continue;
9622515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
963e32f2f54SBarry 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);
96449b5e25fSSatish Balay #endif
96549b5e25fSSatish Balay       col = in[l];
96649b5e25fSSatish Balay       bcol = col/bs;              /* block col number */
96749b5e25fSSatish Balay 
968941593c8SHong Zhang       if (brow > bcol) {
969941593c8SHong Zhang         if (a->ignore_ltriangular){
970941593c8SHong Zhang           continue; /* ignore lower triangular values */
971941593c8SHong Zhang         } else {
972e32f2f54SBarry Smith           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)");
973941593c8SHong Zhang         }
974941593c8SHong Zhang       }
975f4989cb3SHong Zhang 
97649b5e25fSSatish Balay       ridx = row % bs; cidx = col % bs; /*row and col index inside the block */
9778549e402SHong Zhang       if ((brow==bcol && ridx<=cidx) || (brow<bcol)){
97849b5e25fSSatish Balay         /* element value a(k,l) */
97949b5e25fSSatish Balay         if (roworiented) {
98049b5e25fSSatish Balay           value = v[l + k*n];
98149b5e25fSSatish Balay         } else {
98249b5e25fSSatish Balay           value = v[k + l*m];
98349b5e25fSSatish Balay         }
98449b5e25fSSatish Balay 
98549b5e25fSSatish Balay         /* move pointer bap to a(k,l) quickly and add/insert value */
9867cd84e04SBarry Smith         if (col <= lastcol) low = 0; high = nrow;
987e2ee6c50SBarry Smith         lastcol = col;
98849b5e25fSSatish Balay         while (high-low > 7) {
98949b5e25fSSatish Balay           t = (low+high)/2;
99049b5e25fSSatish Balay           if (rp[t] > bcol) high = t;
99149b5e25fSSatish Balay           else              low  = t;
99249b5e25fSSatish Balay         }
99349b5e25fSSatish Balay         for (i=low; i<high; i++) {
99449b5e25fSSatish Balay           if (rp[i] > bcol) break;
99549b5e25fSSatish Balay           if (rp[i] == bcol) {
99649b5e25fSSatish Balay             bap  = ap +  bs2*i + bs*cidx + ridx;
99749b5e25fSSatish Balay             if (is == ADD_VALUES) *bap += value;
99849b5e25fSSatish Balay             else                  *bap  = value;
9998549e402SHong Zhang             /* for diag block, add/insert its symmetric element a(cidx,ridx) */
10008549e402SHong Zhang             if (brow == bcol && ridx < cidx){
10018549e402SHong Zhang               bap  = ap +  bs2*i + bs*ridx + cidx;
10028549e402SHong Zhang               if (is == ADD_VALUES) *bap += value;
10038549e402SHong Zhang               else                  *bap  = value;
10048549e402SHong Zhang             }
100549b5e25fSSatish Balay             goto noinsert1;
100649b5e25fSSatish Balay           }
100749b5e25fSSatish Balay         }
100849b5e25fSSatish Balay 
100949b5e25fSSatish Balay         if (nonew == 1) goto noinsert1;
1010e32f2f54SBarry Smith         if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
1011fef13f97SBarry Smith         MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
101249b5e25fSSatish Balay 
1013c03d1d03SSatish Balay         N = nrow++ - 1; high++;
101449b5e25fSSatish Balay         /* shift up all the later entries in this row */
101549b5e25fSSatish Balay         for (ii=N; ii>=i; ii--) {
101649b5e25fSSatish Balay           rp[ii+1] = rp[ii];
101749b5e25fSSatish Balay           ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
101849b5e25fSSatish Balay         }
101949b5e25fSSatish Balay         if (N>=i) {
102049b5e25fSSatish Balay           ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
102149b5e25fSSatish Balay         }
102249b5e25fSSatish Balay         rp[i]                      = bcol;
102349b5e25fSSatish Balay         ap[bs2*i + bs*cidx + ridx] = value;
102449b5e25fSSatish Balay       noinsert1:;
102549b5e25fSSatish Balay         low = i;
10268549e402SHong Zhang       }
102749b5e25fSSatish Balay     }   /* end of loop over added columns */
102849b5e25fSSatish Balay     ailen[brow] = nrow;
102949b5e25fSSatish Balay   }   /* end of loop over added rows */
103049b5e25fSSatish Balay   PetscFunctionReturn(0);
103149b5e25fSSatish Balay }
103249b5e25fSSatish Balay 
10334a2ae208SSatish Balay #undef __FUNCT__
10344d101231SSatish Balay #define __FUNCT__ "MatICCFactor_SeqSBAIJ"
10350481f469SBarry Smith PetscErrorCode MatICCFactor_SeqSBAIJ(Mat inA,IS row,const MatFactorInfo *info)
103649b5e25fSSatish Balay {
10374ccecd49SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)inA->data;
103849b5e25fSSatish Balay   Mat            outA;
1039dfbe8321SBarry Smith   PetscErrorCode ierr;
1040c84f5b01SHong Zhang   PetscTruth     row_identity;
104149b5e25fSSatish Balay 
104249b5e25fSSatish Balay   PetscFunctionBegin;
1043e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 is supported for in-place icc");
1044c84f5b01SHong Zhang   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1045e32f2f54SBarry Smith   if (!row_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix reordering is not supported");
1046e32f2f54SBarry 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()! */
1047c84f5b01SHong Zhang 
104849b5e25fSSatish Balay   outA            = inA;
1049d5f3da31SBarry Smith   inA->factortype = MAT_FACTOR_ICC;
105049b5e25fSSatish Balay 
10511a3463dfSHong Zhang   ierr = MatMarkDiagonal_SeqSBAIJ(inA);CHKERRQ(ierr);
1052d595f711SHong Zhang   ierr = MatSeqSBAIJSetNumericFactorization_inplace(inA,row_identity);CHKERRQ(ierr);
105349b5e25fSSatish Balay 
1054c3122656SLisandro Dalcin   ierr   = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1055c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr); }
1056c84f5b01SHong Zhang   a->row = row;
1057c3122656SLisandro Dalcin   ierr   = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1058c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr); }
1059c84f5b01SHong Zhang   a->col = row;
1060c84f5b01SHong Zhang 
1061c84f5b01SHong Zhang   /* Create the invert permutation so that it can be used in MatCholeskyFactorNumeric() */
1062c84f5b01SHong Zhang   if (a->icol) {ierr = ISInvertPermutation(row,PETSC_DECIDE, &a->icol);CHKERRQ(ierr);}
106352e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
106449b5e25fSSatish Balay 
106549b5e25fSSatish Balay   if (!a->solve_work) {
1066d0f46423SBarry Smith     ierr = PetscMalloc((inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
1067d0f46423SBarry Smith     ierr = PetscLogObjectMemory(inA,(inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar));CHKERRQ(ierr);
106849b5e25fSSatish Balay   }
106949b5e25fSSatish Balay 
1070719d5645SBarry Smith   ierr = MatCholeskyFactorNumeric(outA,inA,info);CHKERRQ(ierr);
107149b5e25fSSatish Balay   PetscFunctionReturn(0);
107249b5e25fSSatish Balay }
1073950f1e5bSHong Zhang 
107449b5e25fSSatish Balay EXTERN_C_BEGIN
10754a2ae208SSatish Balay #undef __FUNCT__
10764a2ae208SSatish Balay #define __FUNCT__ "MatSeqSBAIJSetColumnIndices_SeqSBAIJ"
1077be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqSBAIJSetColumnIndices_SeqSBAIJ(Mat mat,PetscInt *indices)
107849b5e25fSSatish Balay {
1079045c9aa0SHong Zhang   Mat_SeqSBAIJ *baij = (Mat_SeqSBAIJ *)mat->data;
108013f74950SBarry Smith   PetscInt     i,nz,n;
108149b5e25fSSatish Balay 
108249b5e25fSSatish Balay   PetscFunctionBegin;
10836c6c5352SBarry Smith   nz = baij->maxnz;
1084d0f46423SBarry Smith   n  = mat->cmap->n;
108549b5e25fSSatish Balay   for (i=0; i<nz; i++) {
108649b5e25fSSatish Balay     baij->j[i] = indices[i];
108749b5e25fSSatish Balay   }
10886c6c5352SBarry Smith    baij->nz = nz;
108949b5e25fSSatish Balay    for (i=0; i<n; i++) {
109049b5e25fSSatish Balay      baij->ilen[i] = baij->imax[i];
109149b5e25fSSatish Balay    }
109249b5e25fSSatish Balay    PetscFunctionReturn(0);
109349b5e25fSSatish Balay }
109449b5e25fSSatish Balay EXTERN_C_END
109549b5e25fSSatish Balay 
10964a2ae208SSatish Balay #undef __FUNCT__
10974a2ae208SSatish Balay #define __FUNCT__ "MatSeqSBAIJSetColumnIndices"
109849b5e25fSSatish Balay /*@
109919585528SSatish Balay   MatSeqSBAIJSetColumnIndices - Set the column indices for all the rows
110049b5e25fSSatish Balay   in the matrix.
110149b5e25fSSatish Balay 
110249b5e25fSSatish Balay   Input Parameters:
110319585528SSatish Balay   +  mat     - the SeqSBAIJ matrix
110449b5e25fSSatish Balay   -  indices - the column indices
110549b5e25fSSatish Balay 
110649b5e25fSSatish Balay   Level: advanced
110749b5e25fSSatish Balay 
110849b5e25fSSatish Balay   Notes:
110949b5e25fSSatish Balay   This can be called if you have precomputed the nonzero structure of the
111049b5e25fSSatish Balay   matrix and want to provide it to the matrix object to improve the performance
111149b5e25fSSatish Balay   of the MatSetValues() operation.
111249b5e25fSSatish Balay 
111349b5e25fSSatish Balay   You MUST have set the correct numbers of nonzeros per row in the call to
1114d1be2dadSMatthew Knepley   MatCreateSeqSBAIJ(), and the columns indices MUST be sorted.
111549b5e25fSSatish Balay 
1116ab9f2c04SSatish Balay   MUST be called before any calls to MatSetValues()
111749b5e25fSSatish Balay 
1118ab9f2c04SSatish Balay   .seealso: MatCreateSeqSBAIJ
111949b5e25fSSatish Balay @*/
1120be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqSBAIJSetColumnIndices(Mat mat,PetscInt *indices)
112149b5e25fSSatish Balay {
112213f74950SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
112349b5e25fSSatish Balay 
112449b5e25fSSatish Balay   PetscFunctionBegin;
11250700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
11264482741eSBarry Smith   PetscValidPointer(indices,2);
1127c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqSBAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
112849b5e25fSSatish Balay   if (f) {
112949b5e25fSSatish Balay     ierr = (*f)(mat,indices);CHKERRQ(ierr);
113049b5e25fSSatish Balay   } else {
1131e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
113249b5e25fSSatish Balay   }
113349b5e25fSSatish Balay   PetscFunctionReturn(0);
113449b5e25fSSatish Balay }
113549b5e25fSSatish Balay 
11364a2ae208SSatish Balay #undef __FUNCT__
11373c896bc6SHong Zhang #define __FUNCT__ "MatCopy_SeqSBAIJ"
11383c896bc6SHong Zhang PetscErrorCode MatCopy_SeqSBAIJ(Mat A,Mat B,MatStructure str)
11393c896bc6SHong Zhang {
11403c896bc6SHong Zhang   PetscErrorCode ierr;
11413c896bc6SHong Zhang 
11423c896bc6SHong Zhang   PetscFunctionBegin;
11433c896bc6SHong Zhang   /* If the two matrices have the same copy implementation, use fast copy. */
11443c896bc6SHong Zhang   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
11453c896bc6SHong Zhang     Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
11463c896bc6SHong Zhang     Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ*)B->data;
11473c896bc6SHong Zhang 
1148e7e72b3dSBarry 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");
1149d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->N])*sizeof(PetscScalar));CHKERRQ(ierr);
11503c896bc6SHong Zhang   } else {
1151f5edf698SHong Zhang     ierr = MatGetRowUpperTriangular(A);CHKERRQ(ierr);
11523c896bc6SHong Zhang     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1153f5edf698SHong Zhang     ierr = MatRestoreRowUpperTriangular(A);CHKERRQ(ierr);
11543c896bc6SHong Zhang   }
11553c896bc6SHong Zhang   PetscFunctionReturn(0);
11563c896bc6SHong Zhang }
11573c896bc6SHong Zhang 
11583c896bc6SHong Zhang #undef __FUNCT__
11594a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqSBAIJ"
1160dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqSBAIJ(Mat A)
1161273d9f13SBarry Smith {
1162dfbe8321SBarry Smith   PetscErrorCode ierr;
1163273d9f13SBarry Smith 
1164273d9f13SBarry Smith   PetscFunctionBegin;
1165db4efbfdSBarry Smith   ierr =  MatSeqSBAIJSetPreallocation_SeqSBAIJ(A,-PetscMax(A->rmap->bs,1),PETSC_DEFAULT,0);CHKERRQ(ierr);
1166273d9f13SBarry Smith   PetscFunctionReturn(0);
1167273d9f13SBarry Smith }
1168273d9f13SBarry Smith 
1169a6ece127SHong Zhang #undef __FUNCT__
1170a6ece127SHong Zhang #define __FUNCT__ "MatGetArray_SeqSBAIJ"
1171dfbe8321SBarry Smith PetscErrorCode MatGetArray_SeqSBAIJ(Mat A,PetscScalar *array[])
1172a6ece127SHong Zhang {
1173a6ece127SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
1174a6ece127SHong Zhang   PetscFunctionBegin;
1175a6ece127SHong Zhang   *array = a->a;
1176a6ece127SHong Zhang   PetscFunctionReturn(0);
1177a6ece127SHong Zhang }
1178a6ece127SHong Zhang 
1179a6ece127SHong Zhang #undef __FUNCT__
1180a6ece127SHong Zhang #define __FUNCT__ "MatRestoreArray_SeqSBAIJ"
1181dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqSBAIJ(Mat A,PetscScalar *array[])
1182a6ece127SHong Zhang {
1183a6ece127SHong Zhang   PetscFunctionBegin;
1184a6ece127SHong Zhang   PetscFunctionReturn(0);
1185a6ece127SHong Zhang  }
1186a6ece127SHong Zhang 
118742ee4b1aSHong Zhang #undef __FUNCT__
118842ee4b1aSHong Zhang #define __FUNCT__ "MatAXPY_SeqSBAIJ"
1189f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqSBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
119042ee4b1aSHong Zhang {
119142ee4b1aSHong Zhang   Mat_SeqSBAIJ   *x=(Mat_SeqSBAIJ *)X->data, *y=(Mat_SeqSBAIJ *)Y->data;
1192dfbe8321SBarry Smith   PetscErrorCode ierr;
1193d0f46423SBarry Smith   PetscInt       i,bs=Y->rmap->bs,bs2,j;
11940805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(x->nz);
119542ee4b1aSHong Zhang 
119642ee4b1aSHong Zhang   PetscFunctionBegin;
119742ee4b1aSHong Zhang   if (str == SAME_NONZERO_PATTERN) {
1198f4df32b1SMatthew Knepley     PetscScalar alpha = a;
1199f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
1200c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
1201c4319e64SHong Zhang     if (y->xtoy && y->XtoY != X) {
1202c4319e64SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
1203c4319e64SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
1204c537a176SHong Zhang     }
1205c4319e64SHong Zhang     if (!y->xtoy) { /* get xtoy */
1206c4319e64SHong Zhang       ierr = MatAXPYGetxtoy_Private(x->mbs,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
1207c4319e64SHong Zhang       y->XtoY = X;
1208c537a176SHong Zhang     }
1209c4319e64SHong Zhang     bs2 = bs*bs;
12106c6c5352SBarry Smith     for (i=0; i<x->nz; i++) {
1211c4319e64SHong Zhang       j = 0;
1212c4319e64SHong Zhang       while (j < bs2){
1213f4df32b1SMatthew Knepley         y->a[bs2*y->xtoy[i]+j] += a*(x->a[bs2*i+j]);
1214c4319e64SHong Zhang         j++;
1215c537a176SHong Zhang       }
1216c4319e64SHong Zhang     }
12171e2582c4SBarry 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);
121842ee4b1aSHong Zhang   } else {
1219f5edf698SHong Zhang     ierr = MatGetRowUpperTriangular(X);CHKERRQ(ierr);
1220f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
1221f5edf698SHong Zhang     ierr = MatRestoreRowUpperTriangular(X);CHKERRQ(ierr);
122242ee4b1aSHong Zhang   }
122342ee4b1aSHong Zhang   PetscFunctionReturn(0);
122442ee4b1aSHong Zhang }
122542ee4b1aSHong Zhang 
1226efcf0fc3SBarry Smith #undef __FUNCT__
12276363de48SJed Brown #define __FUNCT__ "MatSetBlockSize_SeqSBAIJ"
12286363de48SJed Brown PetscErrorCode MatSetBlockSize_SeqSBAIJ(Mat A,PetscInt bs)
12296363de48SJed Brown {
12306363de48SJed Brown   PetscInt rbs,cbs;
12316363de48SJed Brown   PetscErrorCode ierr;
12326363de48SJed Brown 
12336363de48SJed Brown   PetscFunctionBegin;
12346363de48SJed Brown   ierr = PetscLayoutGetBlockSize(A->rmap,&rbs);CHKERRQ(ierr);
12356363de48SJed Brown   ierr = PetscLayoutGetBlockSize(A->cmap,&cbs);CHKERRQ(ierr);
1236e32f2f54SBarry Smith   if (rbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with SBAIJ %d",bs,rbs);
1237e32f2f54SBarry Smith   if (cbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with SBAIJ %d",bs,cbs);
12386363de48SJed Brown   PetscFunctionReturn(0);
12396363de48SJed Brown }
12406363de48SJed Brown 
12416363de48SJed Brown #undef __FUNCT__
1242efcf0fc3SBarry Smith #define __FUNCT__ "MatIsSymmetric_SeqSBAIJ"
1243dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqSBAIJ(Mat A,PetscReal tol,PetscTruth *flg)
1244efcf0fc3SBarry Smith {
1245efcf0fc3SBarry Smith   PetscFunctionBegin;
1246efcf0fc3SBarry Smith   *flg = PETSC_TRUE;
1247efcf0fc3SBarry Smith   PetscFunctionReturn(0);
1248efcf0fc3SBarry Smith }
1249efcf0fc3SBarry Smith 
1250efcf0fc3SBarry Smith #undef __FUNCT__
1251efcf0fc3SBarry Smith #define __FUNCT__ "MatIsStructurallySymmetric_SeqSBAIJ"
1252dfbe8321SBarry Smith PetscErrorCode MatIsStructurallySymmetric_SeqSBAIJ(Mat A,PetscTruth *flg)
1253efcf0fc3SBarry Smith {
1254efcf0fc3SBarry Smith    PetscFunctionBegin;
1255efcf0fc3SBarry Smith    *flg = PETSC_TRUE;
1256efcf0fc3SBarry Smith    PetscFunctionReturn(0);
1257efcf0fc3SBarry Smith }
1258efcf0fc3SBarry Smith 
1259efcf0fc3SBarry Smith #undef __FUNCT__
1260efcf0fc3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqSBAIJ"
1261ab5e4463SMatthew Knepley PetscErrorCode MatIsHermitian_SeqSBAIJ(Mat A,PetscReal tol,PetscTruth *flg)
1262efcf0fc3SBarry Smith  {
1263efcf0fc3SBarry Smith    PetscFunctionBegin;
1264efcf0fc3SBarry Smith    *flg = PETSC_FALSE;
1265efcf0fc3SBarry Smith    PetscFunctionReturn(0);
1266efcf0fc3SBarry Smith  }
1267efcf0fc3SBarry Smith 
126899cafbc1SBarry Smith #undef __FUNCT__
126999cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqSBAIJ"
127099cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqSBAIJ(Mat A)
127199cafbc1SBarry Smith {
127299cafbc1SBarry Smith   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
127399cafbc1SBarry Smith   PetscInt       i,nz = a->bs2*a->i[a->mbs];
1274dd6ea824SBarry Smith   MatScalar      *aa = a->a;
127599cafbc1SBarry Smith 
127699cafbc1SBarry Smith   PetscFunctionBegin;
127799cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
127899cafbc1SBarry Smith   PetscFunctionReturn(0);
127999cafbc1SBarry Smith }
128099cafbc1SBarry Smith 
128199cafbc1SBarry Smith #undef __FUNCT__
128299cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqSBAIJ"
128399cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqSBAIJ(Mat A)
128499cafbc1SBarry Smith {
128599cafbc1SBarry Smith   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
128699cafbc1SBarry Smith   PetscInt       i,nz = a->bs2*a->i[a->mbs];
1287dd6ea824SBarry Smith   MatScalar      *aa = a->a;
128899cafbc1SBarry Smith 
128999cafbc1SBarry Smith   PetscFunctionBegin;
129099cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
129199cafbc1SBarry Smith   PetscFunctionReturn(0);
129299cafbc1SBarry Smith }
129399cafbc1SBarry Smith 
129449b5e25fSSatish Balay /* -------------------------------------------------------------------*/
129549b5e25fSSatish Balay static struct _MatOps MatOps_Values = {MatSetValues_SeqSBAIJ,
129649b5e25fSSatish Balay        MatGetRow_SeqSBAIJ,
129749b5e25fSSatish Balay        MatRestoreRow_SeqSBAIJ,
129849b5e25fSSatish Balay        MatMult_SeqSBAIJ_N,
129997304618SKris Buschelman /* 4*/ MatMultAdd_SeqSBAIJ_N,
1300431c96f7SBarry Smith        MatMult_SeqSBAIJ_N,       /* transpose versions are same as non-transpose versions */
1301e005ede5SBarry Smith        MatMultAdd_SeqSBAIJ_N,
1302db4efbfdSBarry Smith        0,
130349b5e25fSSatish Balay        0,
130449b5e25fSSatish Balay        0,
130597304618SKris Buschelman /*10*/ 0,
130649b5e25fSSatish Balay        0,
1307c078aec8SLisandro Dalcin        MatCholeskyFactor_SeqSBAIJ,
130841f059aeSBarry Smith        MatSOR_SeqSBAIJ,
130949b5e25fSSatish Balay        MatTranspose_SeqSBAIJ,
131097304618SKris Buschelman /*15*/ MatGetInfo_SeqSBAIJ,
131149b5e25fSSatish Balay        MatEqual_SeqSBAIJ,
131249b5e25fSSatish Balay        MatGetDiagonal_SeqSBAIJ,
131349b5e25fSSatish Balay        MatDiagonalScale_SeqSBAIJ,
131449b5e25fSSatish Balay        MatNorm_SeqSBAIJ,
131597304618SKris Buschelman /*20*/ 0,
131649b5e25fSSatish Balay        MatAssemblyEnd_SeqSBAIJ,
131749b5e25fSSatish Balay        MatSetOption_SeqSBAIJ,
131849b5e25fSSatish Balay        MatZeroEntries_SeqSBAIJ,
1319d519adbfSMatthew Knepley /*24*/ 0,
132049b5e25fSSatish Balay        0,
132149b5e25fSSatish Balay        0,
1322db4efbfdSBarry Smith        0,
1323db4efbfdSBarry Smith        0,
1324d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqSBAIJ,
1325c464158bSHong Zhang        0,
1326db4efbfdSBarry Smith        0,
1327a6ece127SHong Zhang        MatGetArray_SeqSBAIJ,
1328a6ece127SHong Zhang        MatRestoreArray_SeqSBAIJ,
1329d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqSBAIJ,
1330719d5645SBarry Smith        0,
1331719d5645SBarry Smith        0,
133249b5e25fSSatish Balay        0,
1333c84f5b01SHong Zhang        MatICCFactor_SeqSBAIJ,
1334d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqSBAIJ,
133549b5e25fSSatish Balay        MatGetSubMatrices_SeqSBAIJ,
133649b5e25fSSatish Balay        MatIncreaseOverlap_SeqSBAIJ,
133749b5e25fSSatish Balay        MatGetValues_SeqSBAIJ,
13383c896bc6SHong Zhang        MatCopy_SeqSBAIJ,
1339d519adbfSMatthew Knepley /*44*/ 0,
134049b5e25fSSatish Balay        MatScale_SeqSBAIJ,
134149b5e25fSSatish Balay        0,
134249b5e25fSSatish Balay        0,
134349b5e25fSSatish Balay        0,
13446363de48SJed Brown /*49*/ MatSetBlockSize_SeqSBAIJ,
134549b5e25fSSatish Balay        MatGetRowIJ_SeqSBAIJ,
134649b5e25fSSatish Balay        MatRestoreRowIJ_SeqSBAIJ,
134749b5e25fSSatish Balay        0,
134849b5e25fSSatish Balay        0,
1349d519adbfSMatthew Knepley /*54*/ 0,
135049b5e25fSSatish Balay        0,
135149b5e25fSSatish Balay        0,
135249b5e25fSSatish Balay        0,
135349b5e25fSSatish Balay        MatSetValuesBlocked_SeqSBAIJ,
1354d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_SeqSBAIJ,
135549b5e25fSSatish Balay        0,
135649b5e25fSSatish Balay        0,
1357357abbc8SBarry Smith        0,
1358d959ec07SHong Zhang        0,
1359d519adbfSMatthew Knepley /*64*/ 0,
1360d959ec07SHong Zhang        0,
1361d959ec07SHong Zhang        0,
1362d959ec07SHong Zhang        0,
1363d959ec07SHong Zhang        0,
1364d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqSBAIJ,
13653e0d88b5SBarry Smith        0,
13663e0d88b5SBarry Smith        0,
13673e0d88b5SBarry Smith        0,
13683e0d88b5SBarry Smith        0,
1369d519adbfSMatthew Knepley /*74*/ 0,
13703e0d88b5SBarry Smith        0,
13713e0d88b5SBarry Smith        0,
13723e0d88b5SBarry Smith        0,
13733e0d88b5SBarry Smith        0,
1374d519adbfSMatthew Knepley /*79*/ 0,
13753e0d88b5SBarry Smith        0,
13763e0d88b5SBarry Smith        0,
137797304618SKris Buschelman        MatGetInertia_SeqSBAIJ,
13785bba2384SShri Abhyankar        MatLoad_SeqSBAIJ,
1379d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqSBAIJ,
1380865e5f61SKris Buschelman        MatIsHermitian_SeqSBAIJ,
1381efcf0fc3SBarry Smith        MatIsStructurallySymmetric_SeqSBAIJ,
1382865e5f61SKris Buschelman        0,
1383865e5f61SKris Buschelman        0,
1384d519adbfSMatthew Knepley /*89*/ 0,
1385865e5f61SKris Buschelman        0,
1386865e5f61SKris Buschelman        0,
1387865e5f61SKris Buschelman        0,
1388865e5f61SKris Buschelman        0,
1389d519adbfSMatthew Knepley /*94*/ 0,
1390865e5f61SKris Buschelman        0,
1391865e5f61SKris Buschelman        0,
139299cafbc1SBarry Smith        0,
139399cafbc1SBarry Smith        0,
1394d519adbfSMatthew Knepley /*99*/ 0,
139599cafbc1SBarry Smith        0,
139699cafbc1SBarry Smith        0,
139799cafbc1SBarry Smith        0,
139899cafbc1SBarry Smith        0,
1399d519adbfSMatthew Knepley /*104*/0,
140099cafbc1SBarry Smith        MatRealPart_SeqSBAIJ,
1401f5edf698SHong Zhang        MatImaginaryPart_SeqSBAIJ,
1402f5edf698SHong Zhang        MatGetRowUpperTriangular_SeqSBAIJ,
14032af78befSBarry Smith        MatRestoreRowUpperTriangular_SeqSBAIJ,
1404d519adbfSMatthew Knepley /*109*/0,
14052af78befSBarry Smith        0,
14062af78befSBarry Smith        0,
14072af78befSBarry Smith        0,
1408547795f9SHong Zhang        MatMissingDiagonal_SeqSBAIJ,
1409547795f9SHong Zhang /*114*/0,
1410547795f9SHong Zhang        0,
1411547795f9SHong Zhang        0,
1412547795f9SHong Zhang        0,
1413547795f9SHong Zhang        0,
1414547795f9SHong Zhang /*119*/0,
1415547795f9SHong Zhang        0,
14162f480046SShri Abhyankar        0,
14175bba2384SShri Abhyankar        0
141899cafbc1SBarry Smith };
1419be1d678aSKris Buschelman 
142049b5e25fSSatish Balay EXTERN_C_BEGIN
14214a2ae208SSatish Balay #undef __FUNCT__
14224a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqSBAIJ"
1423be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqSBAIJ(Mat mat)
142449b5e25fSSatish Balay {
14254afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ *)mat->data;
1426d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
1427dfbe8321SBarry Smith   PetscErrorCode ierr;
142849b5e25fSSatish Balay 
142949b5e25fSSatish Balay   PetscFunctionBegin;
1430e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
143149b5e25fSSatish Balay 
143249b5e25fSSatish Balay   /* allocate space for values if not already there */
143349b5e25fSSatish Balay   if (!aij->saved_values) {
143487828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
143549b5e25fSSatish Balay   }
143649b5e25fSSatish Balay 
143749b5e25fSSatish Balay   /* copy values over */
143887828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
143949b5e25fSSatish Balay   PetscFunctionReturn(0);
144049b5e25fSSatish Balay }
144149b5e25fSSatish Balay EXTERN_C_END
144249b5e25fSSatish Balay 
144349b5e25fSSatish Balay EXTERN_C_BEGIN
14444a2ae208SSatish Balay #undef __FUNCT__
14454a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqSBAIJ"
1446be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqSBAIJ(Mat mat)
144749b5e25fSSatish Balay {
14484afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ *)mat->data;
14496849ba73SBarry Smith   PetscErrorCode ierr;
1450d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
145149b5e25fSSatish Balay 
145249b5e25fSSatish Balay   PetscFunctionBegin;
1453e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
1454e7e72b3dSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
145549b5e25fSSatish Balay 
145649b5e25fSSatish Balay   /* copy values over */
145787828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
145849b5e25fSSatish Balay   PetscFunctionReturn(0);
145949b5e25fSSatish Balay }
146049b5e25fSSatish Balay EXTERN_C_END
146149b5e25fSSatish Balay 
14628549e402SHong Zhang EXTERN_C_BEGIN
14634a2ae208SSatish Balay #undef __FUNCT__
1464a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation_SeqSBAIJ"
1465be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqSBAIJSetPreallocation_SeqSBAIJ(Mat B,PetscInt bs,PetscInt nz,PetscInt *nnz)
146649b5e25fSSatish Balay {
1467c464158bSHong Zhang   Mat_SeqSBAIJ   *b = (Mat_SeqSBAIJ*)B->data;
14686849ba73SBarry Smith   PetscErrorCode ierr;
1469db4efbfdSBarry Smith   PetscInt       i,mbs,bs2, newbs = PetscAbs(bs);
147090d69ab7SBarry Smith   PetscTruth     skipallocation = PETSC_FALSE,flg = PETSC_FALSE;
147149b5e25fSSatish Balay 
147249b5e25fSSatish Balay   PetscFunctionBegin;
1473273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
1474db4efbfdSBarry Smith   if (bs < 0) {
1475db4efbfdSBarry Smith     ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Options for MPISBAIJ matrix","Mat");CHKERRQ(ierr);
1476db4efbfdSBarry Smith       ierr = PetscOptionsInt("-mat_block_size","Set the blocksize used to store the matrix","MatSeqSBAIJSetPreallocation",newbs,&newbs,PETSC_NULL);CHKERRQ(ierr);
1477db4efbfdSBarry Smith     ierr = PetscOptionsEnd();CHKERRQ(ierr);
1478db4efbfdSBarry Smith     bs   = PetscAbs(bs);
1479db4efbfdSBarry Smith   }
1480e7e72b3dSBarry Smith   if (nnz && newbs != bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot change blocksize from command line if setting nnz");
1481db4efbfdSBarry Smith   bs = newbs;
1482db4efbfdSBarry Smith 
148326283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
148426283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
148526283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
148626283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
1487899cda47SBarry Smith 
1488d0f46423SBarry Smith   mbs  = B->rmap->N/bs;
148949b5e25fSSatish Balay   bs2  = bs*bs;
149049b5e25fSSatish Balay 
1491e7e72b3dSBarry Smith   if (mbs*bs != B->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number rows, cols must be divisible by blocksize");
149249b5e25fSSatish Balay 
1493ab93d7beSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
1494ab93d7beSBarry Smith     skipallocation = PETSC_TRUE;
1495ab93d7beSBarry Smith     nz             = 0;
1496ab93d7beSBarry Smith   }
1497ab93d7beSBarry Smith 
1498435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 3;
1499e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
150049b5e25fSSatish Balay   if (nnz) {
150149b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
1502e32f2f54SBarry 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]);
1503e32f2f54SBarry 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);
150449b5e25fSSatish Balay     }
150549b5e25fSSatish Balay   }
150649b5e25fSSatish Balay 
1507db4efbfdSBarry Smith   B->ops->mult             = MatMult_SeqSBAIJ_N;
1508db4efbfdSBarry Smith   B->ops->multadd          = MatMultAdd_SeqSBAIJ_N;
1509db4efbfdSBarry Smith   B->ops->multtranspose    = MatMult_SeqSBAIJ_N;
1510db4efbfdSBarry Smith   B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_N;
151190d69ab7SBarry Smith   ierr  = PetscOptionsGetTruth(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,PETSC_NULL);CHKERRQ(ierr);
151249b5e25fSSatish Balay   if (!flg) {
151349b5e25fSSatish Balay     switch (bs) {
151449b5e25fSSatish Balay     case 1:
151549b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_1;
151649b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_1;
1517431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_1;
1518431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_1;
151949b5e25fSSatish Balay       break;
152049b5e25fSSatish Balay     case 2:
152149b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_2;
152249b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_2;
1523431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_2;
1524431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_2;
152549b5e25fSSatish Balay       break;
152649b5e25fSSatish Balay     case 3:
152749b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_3;
152849b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_3;
1529431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_3;
1530431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_3;
153149b5e25fSSatish Balay       break;
153249b5e25fSSatish Balay     case 4:
153349b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_4;
153449b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_4;
1535431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_4;
1536431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_4;
153749b5e25fSSatish Balay       break;
153849b5e25fSSatish Balay     case 5:
153949b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_5;
154049b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_5;
1541431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_5;
1542431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_5;
154349b5e25fSSatish Balay       break;
154449b5e25fSSatish Balay     case 6:
154549b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_6;
154649b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_6;
1547431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_6;
1548431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_6;
154949b5e25fSSatish Balay       break;
155049b5e25fSSatish Balay     case 7:
1551de53e5efSHong Zhang       B->ops->mult             = MatMult_SeqSBAIJ_7;
155249b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_7;
1553431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_7;
1554431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_7;
155549b5e25fSSatish Balay       break;
155649b5e25fSSatish Balay     }
155749b5e25fSSatish Balay   }
155849b5e25fSSatish Balay 
155949b5e25fSSatish Balay   b->mbs = mbs;
15604afc71dfSHong Zhang   b->nbs = mbs;
1561ab93d7beSBarry Smith   if (!skipallocation) {
15622ee49352SLisandro Dalcin     if (!b->imax) {
1563ab93d7beSBarry Smith       ierr = PetscMalloc2(mbs,PetscInt,&b->imax,mbs,PetscInt,&b->ilen);CHKERRQ(ierr);
1564c760cd28SBarry Smith       b->free_imax_ilen = PETSC_TRUE;
15652ee49352SLisandro Dalcin       ierr = PetscLogObjectMemory(B,2*mbs*sizeof(PetscInt));CHKERRQ(ierr);
15662ee49352SLisandro Dalcin     }
156749b5e25fSSatish Balay     if (!nnz) {
1568435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
156949b5e25fSSatish Balay       else if (nz <= 0)        nz = 1;
157049b5e25fSSatish Balay       for (i=0; i<mbs; i++) {
15718cef66ccSHong Zhang         b->imax[i] = nz;
157249b5e25fSSatish Balay       }
1573153ea458SHong Zhang       nz = nz*mbs; /* total nz */
157449b5e25fSSatish Balay     } else {
157549b5e25fSSatish Balay       nz = 0;
15768cef66ccSHong Zhang       for (i=0; i<mbs; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
157749b5e25fSSatish Balay     }
15782ee49352SLisandro Dalcin     /* b->ilen will count nonzeros in each block row so far. */
15792ee49352SLisandro Dalcin     for (i=0; i<mbs; i++) { b->ilen[i] = 0;}
15806c6c5352SBarry Smith     /* nz=(nz+mbs)/2; */ /* total diagonal and superdiagonal nonzero blocks */
158149b5e25fSSatish Balay 
158249b5e25fSSatish Balay     /* allocate the matrix space */
15832ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
1584d0f46423SBarry Smith     ierr = PetscMalloc3(bs2*nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->N+1,PetscInt,&b->i);CHKERRQ(ierr);
1585d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->N+1)*sizeof(PetscInt)+nz*(bs2*sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
15866c6c5352SBarry Smith     ierr = PetscMemzero(b->a,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
158713f74950SBarry Smith     ierr = PetscMemzero(b->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
158849b5e25fSSatish Balay     b->singlemalloc = PETSC_TRUE;
158949b5e25fSSatish Balay 
159049b5e25fSSatish Balay     /* pointer to beginning of each row */
1591e60cf9a0SBarry Smith     b->i[0] = 0;
159249b5e25fSSatish Balay     for (i=1; i<mbs+1; i++) {
159349b5e25fSSatish Balay       b->i[i] = b->i[i-1] + b->imax[i-1];
159449b5e25fSSatish Balay     }
1595e6b907acSBarry Smith     b->free_a     = PETSC_TRUE;
1596e6b907acSBarry Smith     b->free_ij    = PETSC_TRUE;
1597e811da20SHong Zhang   } else {
1598e6b907acSBarry Smith     b->free_a     = PETSC_FALSE;
1599e6b907acSBarry Smith     b->free_ij    = PETSC_FALSE;
1600ab93d7beSBarry Smith   }
160149b5e25fSSatish Balay 
1602d0f46423SBarry Smith   B->rmap->bs               = bs;
160349b5e25fSSatish Balay   b->bs2              = bs2;
16046c6c5352SBarry Smith   b->nz             = 0;
16056c6c5352SBarry Smith   b->maxnz          = nz*bs2;
1606153ea458SHong Zhang 
160716cdd363SHong Zhang   b->inew             = 0;
160816cdd363SHong Zhang   b->jnew             = 0;
160916cdd363SHong Zhang   b->anew             = 0;
161016cdd363SHong Zhang   b->a2anew           = 0;
16111a3463dfSHong Zhang   b->permute          = PETSC_FALSE;
1612c464158bSHong Zhang   PetscFunctionReturn(0);
1613c464158bSHong Zhang }
1614a23d5eceSKris Buschelman EXTERN_C_END
1615153ea458SHong Zhang 
1616db4efbfdSBarry Smith /*
1617db4efbfdSBarry Smith    This is used to set the numeric factorization for both Cholesky and ICC symbolic factorization
1618db4efbfdSBarry Smith */
16198b1456e3SHong Zhang #undef __FUNCT__
1620d595f711SHong Zhang #define __FUNCT__ "MatSeqSBAIJSetNumericFactorization_inplace"
1621d595f711SHong Zhang PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat B,PetscTruth natural)
1622db4efbfdSBarry Smith {
1623db4efbfdSBarry Smith   PetscErrorCode ierr;
162490d69ab7SBarry Smith   PetscTruth     flg = PETSC_FALSE;
1625db4efbfdSBarry Smith   PetscInt       bs = B->rmap->bs;
1626db4efbfdSBarry Smith 
1627db4efbfdSBarry Smith   PetscFunctionBegin;
162890d69ab7SBarry Smith   ierr    = PetscOptionsGetTruth(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,PETSC_NULL);CHKERRQ(ierr);
1629db4efbfdSBarry Smith   if (flg) bs = 8;
1630db4efbfdSBarry Smith 
1631db4efbfdSBarry Smith   if (!natural) {
1632db4efbfdSBarry Smith     switch (bs) {
1633db4efbfdSBarry Smith     case 1:
1634d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_inplace;
1635db4efbfdSBarry Smith       break;
1636db4efbfdSBarry Smith     case 2:
1637db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2;
1638db4efbfdSBarry Smith       break;
1639db4efbfdSBarry Smith     case 3:
1640db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3;
1641db4efbfdSBarry Smith       break;
1642db4efbfdSBarry Smith     case 4:
1643db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4;
1644db4efbfdSBarry Smith       break;
1645db4efbfdSBarry Smith     case 5:
1646db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5;
1647db4efbfdSBarry Smith       break;
1648db4efbfdSBarry Smith     case 6:
1649db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6;
1650db4efbfdSBarry Smith       break;
1651db4efbfdSBarry Smith     case 7:
1652db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7;
1653db4efbfdSBarry Smith       break;
1654db4efbfdSBarry Smith     default:
1655db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N;
1656db4efbfdSBarry Smith       break;
1657db4efbfdSBarry Smith     }
1658db4efbfdSBarry Smith   } else {
1659db4efbfdSBarry Smith     switch (bs) {
1660db4efbfdSBarry Smith     case 1:
1661d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering_inplace;
1662db4efbfdSBarry Smith       break;
1663db4efbfdSBarry Smith     case 2:
1664db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering;
1665db4efbfdSBarry Smith       break;
1666db4efbfdSBarry Smith     case 3:
1667db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3_NaturalOrdering;
1668db4efbfdSBarry Smith       break;
1669db4efbfdSBarry Smith     case 4:
1670db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4_NaturalOrdering;
1671db4efbfdSBarry Smith       break;
1672db4efbfdSBarry Smith     case 5:
1673db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5_NaturalOrdering;
1674db4efbfdSBarry Smith       break;
1675db4efbfdSBarry Smith     case 6:
1676db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6_NaturalOrdering;
1677db4efbfdSBarry Smith       break;
1678db4efbfdSBarry Smith     case 7:
1679db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7_NaturalOrdering;
1680db4efbfdSBarry Smith       break;
1681db4efbfdSBarry Smith     default:
1682db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering;
1683db4efbfdSBarry Smith       break;
1684db4efbfdSBarry Smith     }
1685db4efbfdSBarry Smith   }
1686db4efbfdSBarry Smith   PetscFunctionReturn(0);
1687db4efbfdSBarry Smith }
1688db4efbfdSBarry Smith 
1689d769727bSBarry Smith EXTERN_C_BEGIN
1690f69a0ea3SMatthew Knepley EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqSBAIJ_SeqAIJ(Mat, MatType,MatReuse,Mat*);
1691f69a0ea3SMatthew Knepley EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqSBAIJ_SeqBAIJ(Mat, MatType,MatReuse,Mat*);
1692d769727bSBarry Smith EXTERN_C_END
1693d769727bSBarry Smith 
1694e631078cSBarry Smith 
1695e631078cSBarry Smith EXTERN_C_BEGIN
16965c9eb25fSBarry Smith #undef __FUNCT__
16975c9eb25fSBarry Smith #define __FUNCT__ "MatGetFactor_seqsbaij_petsc"
16985c9eb25fSBarry Smith PetscErrorCode MatGetFactor_seqsbaij_petsc(Mat A,MatFactorType ftype,Mat *B)
16995c9eb25fSBarry Smith {
1700d0f46423SBarry Smith   PetscInt           n = A->rmap->n;
17015c9eb25fSBarry Smith   PetscErrorCode     ierr;
17025c9eb25fSBarry Smith 
17035c9eb25fSBarry Smith   PetscFunctionBegin;
17045c9eb25fSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
17055c9eb25fSBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
17065c9eb25fSBarry Smith   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
17075c9eb25fSBarry Smith     ierr = MatSetType(*B,MATSEQSBAIJ);CHKERRQ(ierr);
17085c9eb25fSBarry Smith     ierr = MatSeqSBAIJSetPreallocation(*B,1,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr);
17097b056e98SHong Zhang     (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqSBAIJ;
1710c6d0d4f0SHong Zhang     (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqSBAIJ;
1711e32f2f54SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported");
1712d5f3da31SBarry Smith   (*B)->factortype = ftype;
17135c9eb25fSBarry Smith   PetscFunctionReturn(0);
17145c9eb25fSBarry Smith }
1715e631078cSBarry Smith EXTERN_C_END
17165c9eb25fSBarry Smith 
17175c9eb25fSBarry Smith EXTERN_C_BEGIN
1718db4efbfdSBarry Smith #undef __FUNCT__
1719db4efbfdSBarry Smith #define __FUNCT__ "MatGetFactorAvailable_seqsbaij_petsc"
1720db4efbfdSBarry Smith PetscErrorCode MatGetFactorAvailable_seqsbaij_petsc(Mat A,MatFactorType ftype,PetscTruth *flg)
1721db4efbfdSBarry Smith {
1722db4efbfdSBarry Smith   PetscFunctionBegin;
1723db4efbfdSBarry Smith   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
1724db4efbfdSBarry Smith     *flg = PETSC_TRUE;
1725db4efbfdSBarry Smith   } else {
1726db4efbfdSBarry Smith     *flg = PETSC_FALSE;
1727db4efbfdSBarry Smith   }
1728db4efbfdSBarry Smith   PetscFunctionReturn(0);
1729db4efbfdSBarry Smith }
1730db4efbfdSBarry Smith EXTERN_C_END
1731db4efbfdSBarry Smith 
1732db4efbfdSBarry Smith EXTERN_C_BEGIN
1733611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
1734bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_sbaij_mumps(Mat,MatFactorType,Mat*);
1735611f576cSBarry Smith #endif
1736611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
17375c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_seqsbaij_spooles(Mat,MatFactorType,Mat*);
1738611f576cSBarry Smith #endif
1739b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
1740b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqsbaij_pastix(Mat,MatFactorType,Mat*);
1741b5e56a35SBarry Smith #endif
174220db9a53SJed Brown #if defined(PETSC_HAVE_CHOLMOD)
174320db9a53SJed Brown extern PetscErrorCode MatGetFactor_seqsbaij_cholmod(Mat,MatFactorType,Mat*);
174420db9a53SJed Brown #endif
17455c9eb25fSBarry Smith EXTERN_C_END
17465c9eb25fSBarry Smith 
17470bad9183SKris Buschelman /*MC
1748fafad747SKris Buschelman   MATSEQSBAIJ - MATSEQSBAIJ = "seqsbaij" - A matrix type to be used for sequential symmetric block sparse matrices,
17490bad9183SKris Buschelman   based on block compressed sparse row format.  Only the upper triangular portion of the matrix is stored.
17500bad9183SKris Buschelman 
17510bad9183SKris Buschelman   Options Database Keys:
17520bad9183SKris Buschelman   . -mat_type seqsbaij - sets the matrix type to "seqsbaij" during a call to MatSetFromOptions()
17530bad9183SKris Buschelman 
17540bad9183SKris Buschelman   Level: beginner
17550bad9183SKris Buschelman 
17560bad9183SKris Buschelman   .seealso: MatCreateSeqSBAIJ
17570bad9183SKris Buschelman M*/
17580bad9183SKris Buschelman 
1759a23d5eceSKris Buschelman EXTERN_C_BEGIN
1760a23d5eceSKris Buschelman #undef __FUNCT__
1761a23d5eceSKris Buschelman #define __FUNCT__ "MatCreate_SeqSBAIJ"
1762be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqSBAIJ(Mat B)
1763a23d5eceSKris Buschelman {
1764a23d5eceSKris Buschelman   Mat_SeqSBAIJ   *b;
1765dfbe8321SBarry Smith   PetscErrorCode ierr;
176613f74950SBarry Smith   PetscMPIInt    size;
17670def2e27SBarry Smith   PetscTruth     no_unroll = PETSC_FALSE,no_inode = PETSC_FALSE;
1768a23d5eceSKris Buschelman 
1769a23d5eceSKris Buschelman   PetscFunctionBegin;
17707adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
1771e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Comm must be of size 1");
1772a23d5eceSKris Buschelman 
177338f2d2fdSLisandro Dalcin   ierr    = PetscNewLog(B,Mat_SeqSBAIJ,&b);CHKERRQ(ierr);
1774a23d5eceSKris Buschelman   B->data = (void*)b;
1775a23d5eceSKris Buschelman   ierr    = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
1776a23d5eceSKris Buschelman   B->ops->destroy     = MatDestroy_SeqSBAIJ;
1777a23d5eceSKris Buschelman   B->ops->view        = MatView_SeqSBAIJ;
1778a23d5eceSKris Buschelman   B->mapping          = 0;
1779a23d5eceSKris Buschelman   b->row              = 0;
1780a23d5eceSKris Buschelman   b->icol             = 0;
1781a23d5eceSKris Buschelman   b->reallocs         = 0;
1782a23d5eceSKris Buschelman   b->saved_values     = 0;
17830def2e27SBarry Smith   b->inode.limit      = 5;
17840def2e27SBarry Smith   b->inode.max_limit  = 5;
1785a23d5eceSKris Buschelman 
1786a23d5eceSKris Buschelman   b->roworiented      = PETSC_TRUE;
1787a23d5eceSKris Buschelman   b->nonew            = 0;
1788a23d5eceSKris Buschelman   b->diag             = 0;
1789a23d5eceSKris Buschelman   b->solve_work       = 0;
1790a23d5eceSKris Buschelman   b->mult_work        = 0;
1791a23d5eceSKris Buschelman   B->spptr            = 0;
17928e9a0fb8SHong Zhang   B->info.nz_unneeded = (PetscReal)b->maxnz;
1793a9817697SBarry Smith   b->keepnonzeropattern   = PETSC_FALSE;
1794a23d5eceSKris Buschelman   b->xtoy             = 0;
1795a23d5eceSKris Buschelman   b->XtoY             = 0;
1796a23d5eceSKris Buschelman 
1797a23d5eceSKris Buschelman   b->inew             = 0;
1798a23d5eceSKris Buschelman   b->jnew             = 0;
1799a23d5eceSKris Buschelman   b->anew             = 0;
1800a23d5eceSKris Buschelman   b->a2anew           = 0;
1801a23d5eceSKris Buschelman   b->permute          = PETSC_FALSE;
1802a23d5eceSKris Buschelman 
1803941593c8SHong Zhang   b->ignore_ltriangular = PETSC_FALSE;
180490d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)B)->prefix,"-mat_ignore_lower_triangular",&b->ignore_ltriangular,PETSC_NULL);CHKERRQ(ierr);
1805941593c8SHong Zhang 
1806f5edf698SHong Zhang   b->getrow_utriangular = PETSC_FALSE;
180790d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)B)->prefix,"-mat_getrow_uppertriangular",&b->getrow_utriangular,PETSC_NULL);CHKERRQ(ierr);
1808f5edf698SHong Zhang 
1809b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
1810ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C",
1811b5e56a35SBarry Smith 					   "MatGetFactor_seqsbaij_pastix",
1812b5e56a35SBarry Smith 					   MatGetFactor_seqsbaij_pastix);CHKERRQ(ierr);
1813b5e56a35SBarry Smith #endif
1814611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
1815ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C",
18165c9eb25fSBarry Smith                                      "MatGetFactor_seqsbaij_spooles",
18175c9eb25fSBarry Smith                                      MatGetFactor_seqsbaij_spooles);CHKERRQ(ierr);
1818611f576cSBarry Smith #endif
1819611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
1820ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C",
1821bccb9932SShri Abhyankar                                      "MatGetFactor_sbaij_mumps",
1822bccb9932SShri Abhyankar                                      MatGetFactor_sbaij_mumps);CHKERRQ(ierr);
1823611f576cSBarry Smith #endif
182420db9a53SJed Brown #if defined(PETSC_HAVE_CHOLMOD)
182520db9a53SJed Brown   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C",
182620db9a53SJed Brown                                      "MatGetFactor_seqsbaij_cholmod",
182720db9a53SJed Brown                                      MatGetFactor_seqsbaij_cholmod);CHKERRQ(ierr);
182820db9a53SJed Brown #endif
1829ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C",
1830db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqsbaij_petsc",
1831db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqsbaij_petsc);CHKERRQ(ierr);
1832ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C",
18335c9eb25fSBarry Smith                                      "MatGetFactor_seqsbaij_petsc",
18345c9eb25fSBarry Smith                                      MatGetFactor_seqsbaij_petsc);CHKERRQ(ierr);
1835a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
1836a23d5eceSKris Buschelman                                      "MatStoreValues_SeqSBAIJ",
1837a23d5eceSKris Buschelman                                      MatStoreValues_SeqSBAIJ);CHKERRQ(ierr);
1838a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
1839a23d5eceSKris Buschelman                                      "MatRetrieveValues_SeqSBAIJ",
1840a23d5eceSKris Buschelman                                      (void*)MatRetrieveValues_SeqSBAIJ);CHKERRQ(ierr);
1841a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqSBAIJSetColumnIndices_C",
1842a23d5eceSKris Buschelman                                      "MatSeqSBAIJSetColumnIndices_SeqSBAIJ",
1843a23d5eceSKris Buschelman                                      MatSeqSBAIJSetColumnIndices_SeqSBAIJ);CHKERRQ(ierr);
18444e5e7fe4SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqsbaij_seqaij_C",
18454e5e7fe4SHong Zhang                                      "MatConvert_SeqSBAIJ_SeqAIJ",
18464e5e7fe4SHong Zhang                                       MatConvert_SeqSBAIJ_SeqAIJ);CHKERRQ(ierr);
1847a0e1a404SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqsbaij_seqbaij_C",
1848a0e1a404SHong Zhang                                      "MatConvert_SeqSBAIJ_SeqBAIJ",
1849a0e1a404SHong Zhang                                       MatConvert_SeqSBAIJ_SeqBAIJ);CHKERRQ(ierr);
1850a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqSBAIJSetPreallocation_C",
1851a23d5eceSKris Buschelman                                      "MatSeqSBAIJSetPreallocation_SeqSBAIJ",
1852a23d5eceSKris Buschelman                                      MatSeqSBAIJSetPreallocation_SeqSBAIJ);CHKERRQ(ierr);
185323ce1328SBarry Smith 
185423ce1328SBarry Smith   B->symmetric                  = PETSC_TRUE;
185523ce1328SBarry Smith   B->structurally_symmetric     = PETSC_TRUE;
185623ce1328SBarry Smith   B->symmetric_set              = PETSC_TRUE;
185723ce1328SBarry Smith   B->structurally_symmetric_set = PETSC_TRUE;
185817667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQSBAIJ);CHKERRQ(ierr);
18590def2e27SBarry Smith 
18600def2e27SBarry Smith   ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Options for SEQSBAIJ matrix","Mat");CHKERRQ(ierr);
18610def2e27SBarry Smith     ierr = PetscOptionsTruth("-mat_no_unroll","Do not optimize for inodes (slower)",PETSC_NULL,no_unroll,&no_unroll,PETSC_NULL);CHKERRQ(ierr);
18620def2e27SBarry Smith     if (no_unroll) {ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_unroll\n");CHKERRQ(ierr);}
18630def2e27SBarry Smith     ierr = PetscOptionsTruth("-mat_no_inode","Do not optimize for inodes (slower)",PETSC_NULL,no_inode,&no_inode,PETSC_NULL);CHKERRQ(ierr);
18640def2e27SBarry Smith     if (no_inode) {ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_inode\n");CHKERRQ(ierr);}
18650def2e27SBarry Smith     ierr = PetscOptionsInt("-mat_inode_limit","Do not use inodes larger then this value",PETSC_NULL,b->inode.limit,&b->inode.limit,PETSC_NULL);CHKERRQ(ierr);
18660def2e27SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
18670def2e27SBarry Smith   b->inode.use = (PetscTruth)(!(no_unroll || no_inode));
18680def2e27SBarry Smith   if (b->inode.limit > b->inode.max_limit) b->inode.limit = b->inode.max_limit;
18690def2e27SBarry Smith 
1870a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1871a23d5eceSKris Buschelman }
1872a23d5eceSKris Buschelman EXTERN_C_END
1873a23d5eceSKris Buschelman 
1874a23d5eceSKris Buschelman #undef __FUNCT__
1875a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation"
1876a23d5eceSKris Buschelman /*@C
1877a23d5eceSKris Buschelman    MatSeqSBAIJSetPreallocation - Creates a sparse symmetric matrix in block AIJ (block
1878a23d5eceSKris Buschelman    compressed row) format.  For good matrix assembly performance the
1879a23d5eceSKris Buschelman    user should preallocate the matrix storage by setting the parameter nz
1880a23d5eceSKris Buschelman    (or the array nnz).  By setting these parameters accurately, performance
1881a23d5eceSKris Buschelman    during matrix assembly can be increased by more than a factor of 50.
1882a23d5eceSKris Buschelman 
1883a23d5eceSKris Buschelman    Collective on Mat
1884a23d5eceSKris Buschelman 
1885a23d5eceSKris Buschelman    Input Parameters:
1886a23d5eceSKris Buschelman +  A - the symmetric matrix
1887a23d5eceSKris Buschelman .  bs - size of block
1888a23d5eceSKris Buschelman .  nz - number of block nonzeros per block row (same for all rows)
1889a23d5eceSKris Buschelman -  nnz - array containing the number of block nonzeros in the upper triangular plus
1890a23d5eceSKris Buschelman          diagonal portion of each block (possibly different for each block row) or PETSC_NULL
1891a23d5eceSKris Buschelman 
1892a23d5eceSKris Buschelman    Options Database Keys:
1893a23d5eceSKris Buschelman .   -mat_no_unroll - uses code that does not unroll the loops in the
1894a23d5eceSKris Buschelman                      block calculations (much slower)
1895db4efbfdSBarry Smith .    -mat_block_size - size of the blocks to use (only works if a negative bs is passed in
1896a23d5eceSKris Buschelman 
1897a23d5eceSKris Buschelman    Level: intermediate
1898a23d5eceSKris Buschelman 
1899a23d5eceSKris Buschelman    Notes:
1900a23d5eceSKris Buschelman    Specify the preallocated storage with either nz or nnz (not both).
1901a23d5eceSKris Buschelman    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
1902a23d5eceSKris Buschelman    allocation.  For additional details, see the users manual chapter on
1903a23d5eceSKris Buschelman    matrices.
1904a23d5eceSKris Buschelman 
1905aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
1906aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
1907aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
1908aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
1909aa95bbe8SBarry Smith 
191049a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
191149a6f317SBarry Smith 
191249a6f317SBarry Smith 
1913a23d5eceSKris Buschelman .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPISBAIJ()
1914a23d5eceSKris Buschelman @*/
1915be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqSBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt nz,const PetscInt nnz[])
191613f74950SBarry Smith {
191713f74950SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,PetscInt,const PetscInt[]);
1918a23d5eceSKris Buschelman 
1919a23d5eceSKris Buschelman   PetscFunctionBegin;
1920a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqSBAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
1921a23d5eceSKris Buschelman   if (f) {
1922a23d5eceSKris Buschelman     ierr = (*f)(B,bs,nz,nnz);CHKERRQ(ierr);
1923a23d5eceSKris Buschelman   }
1924a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1925a23d5eceSKris Buschelman }
192649b5e25fSSatish Balay 
19274a2ae208SSatish Balay #undef __FUNCT__
19284a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqSBAIJ"
1929c464158bSHong Zhang /*@C
1930c464158bSHong Zhang    MatCreateSeqSBAIJ - Creates a sparse symmetric matrix in block AIJ (block
1931c464158bSHong Zhang    compressed row) format.  For good matrix assembly performance the
1932c464158bSHong Zhang    user should preallocate the matrix storage by setting the parameter nz
1933c464158bSHong Zhang    (or the array nnz).  By setting these parameters accurately, performance
1934c464158bSHong Zhang    during matrix assembly can be increased by more than a factor of 50.
193549b5e25fSSatish Balay 
1936c464158bSHong Zhang    Collective on MPI_Comm
1937c464158bSHong Zhang 
1938c464158bSHong Zhang    Input Parameters:
1939c464158bSHong Zhang +  comm - MPI communicator, set to PETSC_COMM_SELF
1940c464158bSHong Zhang .  bs - size of block
1941c464158bSHong Zhang .  m - number of rows, or number of columns
1942c464158bSHong Zhang .  nz - number of block nonzeros per block row (same for all rows)
1943744e8345SSatish Balay -  nnz - array containing the number of block nonzeros in the upper triangular plus
1944744e8345SSatish Balay          diagonal portion of each block (possibly different for each block row) or PETSC_NULL
1945c464158bSHong Zhang 
1946c464158bSHong Zhang    Output Parameter:
1947c464158bSHong Zhang .  A - the symmetric matrix
1948c464158bSHong Zhang 
1949c464158bSHong Zhang    Options Database Keys:
1950c464158bSHong Zhang .   -mat_no_unroll - uses code that does not unroll the loops in the
1951c464158bSHong Zhang                      block calculations (much slower)
1952c464158bSHong Zhang .    -mat_block_size - size of the blocks to use
1953c464158bSHong Zhang 
1954c464158bSHong Zhang    Level: intermediate
1955c464158bSHong Zhang 
1956175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
1957ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
1958175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
1959175b88e8SBarry Smith 
1960c464158bSHong Zhang    Notes:
19616d6d819aSHong Zhang    The number of rows and columns must be divisible by blocksize.
19626d6d819aSHong Zhang    This matrix type does not support complex Hermitian operation.
1963c464158bSHong Zhang 
1964c464158bSHong Zhang    Specify the preallocated storage with either nz or nnz (not both).
1965c464158bSHong Zhang    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
1966c464158bSHong Zhang    allocation.  For additional details, see the users manual chapter on
1967c464158bSHong Zhang    matrices.
1968c464158bSHong Zhang 
196949a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
197049a6f317SBarry Smith 
1971c464158bSHong Zhang .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPISBAIJ()
1972c464158bSHong Zhang @*/
1973be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqSBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
1974c464158bSHong Zhang {
1975dfbe8321SBarry Smith   PetscErrorCode ierr;
1976c464158bSHong Zhang 
1977c464158bSHong Zhang   PetscFunctionBegin;
1978f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
1979f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
1980c464158bSHong Zhang   ierr = MatSetType(*A,MATSEQSBAIJ);CHKERRQ(ierr);
1981ab93d7beSBarry Smith   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*A,bs,nz,(PetscInt*)nnz);CHKERRQ(ierr);
198249b5e25fSSatish Balay   PetscFunctionReturn(0);
198349b5e25fSSatish Balay }
198449b5e25fSSatish Balay 
19854a2ae208SSatish Balay #undef __FUNCT__
19864a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqSBAIJ"
1987dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqSBAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
198849b5e25fSSatish Balay {
198949b5e25fSSatish Balay   Mat            C;
199049b5e25fSSatish Balay   Mat_SeqSBAIJ   *c,*a = (Mat_SeqSBAIJ*)A->data;
19916849ba73SBarry Smith   PetscErrorCode ierr;
1992b40805acSSatish Balay   PetscInt       i,mbs = a->mbs,nz = a->nz,bs2 =a->bs2;
199349b5e25fSSatish Balay 
199449b5e25fSSatish Balay   PetscFunctionBegin;
1995e32f2f54SBarry Smith   if (a->i[mbs] != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupt matrix");
199649b5e25fSSatish Balay 
199749b5e25fSSatish Balay   *B = 0;
19987adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1999d0f46423SBarry Smith   ierr = MatSetSizes(C,A->rmap->N,A->cmap->n,A->rmap->N,A->cmap->n);CHKERRQ(ierr);
20008e9a0fb8SHong Zhang   ierr = MatSetType(C,MATSEQSBAIJ);CHKERRQ(ierr);
20011d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
2002692f9cbeSHong Zhang   c    = (Mat_SeqSBAIJ*)C->data;
2003692f9cbeSHong Zhang 
2004273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2005d5f3da31SBarry Smith   C->factortype         = A->factortype;
200649b5e25fSSatish Balay   c->row                = 0;
200749b5e25fSSatish Balay   c->icol               = 0;
200849b5e25fSSatish Balay   c->saved_values       = 0;
2009a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
201049b5e25fSSatish Balay   C->assembled          = PETSC_TRUE;
201149b5e25fSSatish Balay 
201226283091SBarry Smith   ierr = PetscLayoutCopy(A->rmap,&C->rmap);CHKERRQ(ierr);
201326283091SBarry Smith   ierr = PetscLayoutCopy(A->cmap,&C->cmap);CHKERRQ(ierr);
201449b5e25fSSatish Balay   c->bs2  = a->bs2;
201549b5e25fSSatish Balay   c->mbs  = a->mbs;
201649b5e25fSSatish Balay   c->nbs  = a->nbs;
201749b5e25fSSatish Balay 
2018c760cd28SBarry Smith   if  (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2019c760cd28SBarry Smith     c->imax           = a->imax;
2020c760cd28SBarry Smith     c->ilen           = a->ilen;
2021c760cd28SBarry Smith     c->free_imax_ilen = PETSC_FALSE;
2022c760cd28SBarry Smith   } else {
20238777fc3fSSatish Balay     ierr = PetscMalloc2((mbs+1),PetscInt,&c->imax,(mbs+1),PetscInt,&c->ilen);CHKERRQ(ierr);
2024c760cd28SBarry Smith     ierr = PetscLogObjectMemory(C,2*(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
202549b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
202649b5e25fSSatish Balay       c->imax[i] = a->imax[i];
202749b5e25fSSatish Balay       c->ilen[i] = a->ilen[i];
202849b5e25fSSatish Balay     }
2029c760cd28SBarry Smith     c->free_imax_ilen = PETSC_TRUE;
2030c760cd28SBarry Smith   }
203149b5e25fSSatish Balay 
203249b5e25fSSatish Balay   /* allocate the matrix space */
20334da8f245SBarry Smith   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
20344da8f245SBarry Smith     ierr = PetscMalloc(bs2*nz*sizeof(MatScalar),&c->a);CHKERRQ(ierr);
20354da8f245SBarry Smith     ierr = PetscLogObjectMemory(C,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
20364da8f245SBarry Smith     c->singlemalloc = PETSC_FALSE;
20374da8f245SBarry Smith     c->free_ij      = PETSC_FALSE;
20384da8f245SBarry Smith     c->parent       = A;
20394da8f245SBarry Smith     ierr            = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
20404da8f245SBarry Smith     ierr            = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
20414da8f245SBarry Smith     ierr            = MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
20424da8f245SBarry Smith   } else {
2043b40805acSSatish Balay     ierr = PetscMalloc3(bs2*nz,MatScalar,&c->a,nz,PetscInt,&c->j,mbs+1,PetscInt,&c->i);CHKERRQ(ierr);
204413f74950SBarry Smith     ierr = PetscMemcpy(c->i,a->i,(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
2045b40805acSSatish Balay     ierr = PetscLogObjectMemory(C,(mbs+1)*sizeof(PetscInt) + nz*(bs2*sizeof(MatScalar) + sizeof(PetscInt)));CHKERRQ(ierr);
20464da8f245SBarry Smith     c->singlemalloc = PETSC_TRUE;
20474da8f245SBarry Smith     c->free_ij      = PETSC_TRUE;
20484da8f245SBarry Smith   }
204949b5e25fSSatish Balay   if (mbs > 0) {
20504da8f245SBarry Smith     if (cpvalues != MAT_SHARE_NONZERO_PATTERN) {
205113f74950SBarry Smith       ierr = PetscMemcpy(c->j,a->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
20524da8f245SBarry Smith     }
205349b5e25fSSatish Balay     if (cpvalues == MAT_COPY_VALUES) {
205449b5e25fSSatish Balay       ierr = PetscMemcpy(c->a,a->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
205549b5e25fSSatish Balay     } else {
205649b5e25fSSatish Balay       ierr = PetscMemzero(c->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
205749b5e25fSSatish Balay     }
2058a1c3900fSBarry Smith     if (a->jshort) {
20594da8f245SBarry Smith       if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
20604da8f245SBarry Smith         c->jshort      = a->jshort;
20614da8f245SBarry Smith         c->free_jshort = PETSC_FALSE;
20624da8f245SBarry Smith       } else {
2063a1c3900fSBarry Smith         ierr = PetscMalloc(nz*sizeof(unsigned short),&c->jshort);CHKERRQ(ierr);
2064c760cd28SBarry Smith         ierr = PetscLogObjectMemory(C,nz*sizeof(unsigned short));CHKERRQ(ierr);
2065a1c3900fSBarry Smith         ierr = PetscMemcpy(c->jshort,a->jshort,nz*sizeof(unsigned short));CHKERRQ(ierr);
20664da8f245SBarry Smith         c->free_jshort = PETSC_TRUE;
20674da8f245SBarry Smith       }
2068a1c3900fSBarry Smith     }
206949b5e25fSSatish Balay   }
207049b5e25fSSatish Balay 
207149b5e25fSSatish Balay   c->roworiented = a->roworiented;
207249b5e25fSSatish Balay   c->nonew       = a->nonew;
207349b5e25fSSatish Balay 
207449b5e25fSSatish Balay   if (a->diag) {
2075c760cd28SBarry Smith     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2076c760cd28SBarry Smith       c->diag      = a->diag;
2077c760cd28SBarry Smith       c->free_diag = PETSC_FALSE;
2078c760cd28SBarry Smith     } else {
20792ed38d0bSJed Brown       ierr = PetscMalloc(mbs*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
20802ed38d0bSJed Brown       ierr = PetscLogObjectMemory(C,mbs*sizeof(PetscInt));CHKERRQ(ierr);
208149b5e25fSSatish Balay       for (i=0; i<mbs; i++) {
208249b5e25fSSatish Balay 	c->diag[i] = a->diag[i];
208349b5e25fSSatish Balay       }
2084c760cd28SBarry Smith       c->free_diag = PETSC_TRUE;
2085c760cd28SBarry Smith     }
208649b5e25fSSatish Balay   } else c->diag  = 0;
20876c6c5352SBarry Smith   c->nz           = a->nz;
20888e9a0fb8SHong Zhang   c->maxnz        = bs2*a->nz; /* Since we allocate exactly the right amount */
208949b5e25fSSatish Balay   c->solve_work   = 0;
209049b5e25fSSatish Balay   c->mult_work    = 0;
2091e6b907acSBarry Smith   c->free_a       = PETSC_TRUE;
209249b5e25fSSatish Balay   *B = C;
20937adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
209449b5e25fSSatish Balay   PetscFunctionReturn(0);
209549b5e25fSSatish Balay }
209649b5e25fSSatish Balay 
20974a2ae208SSatish Balay #undef __FUNCT__
20985bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_SeqSBAIJ"
20995bba2384SShri Abhyankar PetscErrorCode MatLoad_SeqSBAIJ(PetscViewer viewer, Mat newmat)
21002f480046SShri Abhyankar {
21012f480046SShri Abhyankar   Mat_SeqSBAIJ   *a;
21022f480046SShri Abhyankar   PetscErrorCode ierr;
21032f480046SShri Abhyankar   int            fd;
21042f480046SShri Abhyankar   PetscMPIInt    size;
21052f480046SShri Abhyankar   PetscInt       i,nz,header[4],*rowlengths=0,M,N,bs=1;
21062f480046SShri Abhyankar   PetscInt       *mask,mbs,*jj,j,rowcount,nzcount,k,*s_browlengths,maskcount;
21072f480046SShri Abhyankar   PetscInt       kmax,jcount,block,idx,point,nzcountb,extra_rows,rows,cols;
21082f480046SShri Abhyankar   PetscInt       *masked,nmask,tmp,bs2,ishift;
21092f480046SShri Abhyankar   PetscScalar    *aa;
21102f480046SShri Abhyankar   MPI_Comm       comm = ((PetscObject)viewer)->comm;
21112f480046SShri Abhyankar 
21122f480046SShri Abhyankar   PetscFunctionBegin;
21132f480046SShri Abhyankar   ierr = PetscOptionsGetInt(PETSC_NULL,"-matload_block_size",&bs,PETSC_NULL);CHKERRQ(ierr);
21142f480046SShri Abhyankar   bs2  = bs*bs;
21152f480046SShri Abhyankar 
21162f480046SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
21172f480046SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
21182f480046SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
21192f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
21202f480046SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not Mat object");
21212f480046SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
21222f480046SShri Abhyankar 
21232f480046SShri Abhyankar   if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as SeqSBAIJ");
21242f480046SShri Abhyankar 
21252f480046SShri Abhyankar   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");
21262f480046SShri Abhyankar 
21272f480046SShri Abhyankar   /*
21282f480046SShri Abhyankar      This code adds extra rows to make sure the number of rows is
21292f480046SShri Abhyankar     divisible by the blocksize
21302f480046SShri Abhyankar   */
21312f480046SShri Abhyankar   mbs        = M/bs;
21322f480046SShri Abhyankar   extra_rows = bs - M + bs*(mbs);
21332f480046SShri Abhyankar   if (extra_rows == bs) extra_rows = 0;
21342f480046SShri Abhyankar   else                  mbs++;
21352f480046SShri Abhyankar   if (extra_rows) {
21362f480046SShri Abhyankar     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
21372f480046SShri Abhyankar   }
21382f480046SShri Abhyankar 
21392f480046SShri Abhyankar   /* Set global sizes if not already set */
21402f480046SShri Abhyankar   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) {
21412f480046SShri Abhyankar     ierr = MatSetSizes(newmat,PETSC_DECIDE,PETSC_DECIDE,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
21422f480046SShri Abhyankar   } else { /* Check if the matrix global sizes are correct */
21432f480046SShri Abhyankar     ierr = MatGetSize(newmat,&rows,&cols);CHKERRQ(ierr);
21442f480046SShri 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);
21452f480046SShri Abhyankar   }
21462f480046SShri Abhyankar 
21472f480046SShri Abhyankar   /* read in row lengths */
21482f480046SShri Abhyankar   ierr = PetscMalloc((M+extra_rows)*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
21492f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
21502f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
21512f480046SShri Abhyankar 
21522f480046SShri Abhyankar   /* read in column indices */
21532f480046SShri Abhyankar   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscInt),&jj);CHKERRQ(ierr);
21542f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,jj,nz,PETSC_INT);CHKERRQ(ierr);
21552f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) jj[nz+i] = M+i;
21562f480046SShri Abhyankar 
21572f480046SShri Abhyankar   /* loop over row lengths determining block row lengths */
21582f480046SShri Abhyankar   ierr     = PetscMalloc(mbs*sizeof(PetscInt),&s_browlengths);CHKERRQ(ierr);
21592f480046SShri Abhyankar   ierr     = PetscMemzero(s_browlengths,mbs*sizeof(PetscInt));CHKERRQ(ierr);
21602f480046SShri Abhyankar   ierr     = PetscMalloc2(mbs,PetscInt,&mask,mbs,PetscInt,&masked);CHKERRQ(ierr);
21612f480046SShri Abhyankar   ierr     = PetscMemzero(mask,mbs*sizeof(PetscInt));CHKERRQ(ierr);
21622f480046SShri Abhyankar   rowcount = 0;
21632f480046SShri Abhyankar   nzcount  = 0;
21642f480046SShri Abhyankar   for (i=0; i<mbs; i++) {
21652f480046SShri Abhyankar     nmask = 0;
21662f480046SShri Abhyankar     for (j=0; j<bs; j++) {
21672f480046SShri Abhyankar       kmax = rowlengths[rowcount];
21682f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
21692f480046SShri Abhyankar         tmp = jj[nzcount++]/bs;   /* block col. index */
21702f480046SShri Abhyankar         if (!mask[tmp] && tmp >= i) {masked[nmask++] = tmp; mask[tmp] = 1;}
21712f480046SShri Abhyankar       }
21722f480046SShri Abhyankar       rowcount++;
21732f480046SShri Abhyankar     }
21742f480046SShri Abhyankar     s_browlengths[i] += nmask;
21752f480046SShri Abhyankar 
21762f480046SShri Abhyankar     /* zero out the mask elements we set */
21772f480046SShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
21782f480046SShri Abhyankar   }
21792f480046SShri Abhyankar 
21802f480046SShri Abhyankar   /* Do preallocation */
21812f480046SShri Abhyankar   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(newmat,bs,0,s_browlengths);CHKERRQ(ierr);
21822f480046SShri Abhyankar   a = (Mat_SeqSBAIJ*)newmat->data;
21832f480046SShri Abhyankar 
21842f480046SShri Abhyankar   /* set matrix "i" values */
21852f480046SShri Abhyankar   a->i[0] = 0;
21862f480046SShri Abhyankar   for (i=1; i<= mbs; i++) {
21872f480046SShri Abhyankar     a->i[i]      = a->i[i-1] + s_browlengths[i-1];
21882f480046SShri Abhyankar     a->ilen[i-1] = s_browlengths[i-1];
21892f480046SShri Abhyankar   }
21902f480046SShri Abhyankar   a->nz = a->i[mbs];
21912f480046SShri Abhyankar 
21922f480046SShri Abhyankar   /* read in nonzero values */
21932f480046SShri Abhyankar   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscScalar),&aa);CHKERRQ(ierr);
21942f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,aa,nz,PETSC_SCALAR);CHKERRQ(ierr);
21952f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) aa[nz+i] = 1.0;
21962f480046SShri Abhyankar 
21972f480046SShri Abhyankar   /* set "a" and "j" values into matrix */
21982f480046SShri Abhyankar   nzcount = 0; jcount = 0;
21992f480046SShri Abhyankar   for (i=0; i<mbs; i++) {
22002f480046SShri Abhyankar     nzcountb = nzcount;
22012f480046SShri Abhyankar     nmask    = 0;
22022f480046SShri Abhyankar     for (j=0; j<bs; j++) {
22032f480046SShri Abhyankar       kmax = rowlengths[i*bs+j];
22042f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
22052f480046SShri Abhyankar         tmp = jj[nzcount++]/bs; /* block col. index */
22062f480046SShri Abhyankar         if (!mask[tmp] && tmp >= i) { masked[nmask++] = tmp; mask[tmp] = 1;}
22072f480046SShri Abhyankar       }
22082f480046SShri Abhyankar     }
22092f480046SShri Abhyankar     /* sort the masked values */
22102f480046SShri Abhyankar     ierr = PetscSortInt(nmask,masked);CHKERRQ(ierr);
22112f480046SShri Abhyankar 
22122f480046SShri Abhyankar     /* set "j" values into matrix */
22132f480046SShri Abhyankar     maskcount = 1;
22142f480046SShri Abhyankar     for (j=0; j<nmask; j++) {
22152f480046SShri Abhyankar       a->j[jcount++]  = masked[j];
22162f480046SShri Abhyankar       mask[masked[j]] = maskcount++;
22172f480046SShri Abhyankar     }
22182f480046SShri Abhyankar 
22192f480046SShri Abhyankar     /* set "a" values into matrix */
22202f480046SShri Abhyankar     ishift = bs2*a->i[i];
22212f480046SShri Abhyankar     for (j=0; j<bs; j++) {
22222f480046SShri Abhyankar       kmax = rowlengths[i*bs+j];
22232f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
22242f480046SShri Abhyankar         tmp       = jj[nzcountb]/bs ; /* block col. index */
22252f480046SShri Abhyankar         if (tmp >= i){
22262f480046SShri Abhyankar           block     = mask[tmp] - 1;
22272f480046SShri Abhyankar           point     = jj[nzcountb] - bs*tmp;
22282f480046SShri Abhyankar           idx       = ishift + bs2*block + j + bs*point;
22292f480046SShri Abhyankar           a->a[idx] = aa[nzcountb];
22302f480046SShri Abhyankar         }
22312f480046SShri Abhyankar         nzcountb++;
22322f480046SShri Abhyankar       }
22332f480046SShri Abhyankar     }
22342f480046SShri Abhyankar     /* zero out the mask elements we set */
22352f480046SShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
22362f480046SShri Abhyankar   }
22372f480046SShri Abhyankar   if (jcount != a->nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Bad binary matrix");
22382f480046SShri Abhyankar 
22392f480046SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
22402f480046SShri Abhyankar   ierr = PetscFree(s_browlengths);CHKERRQ(ierr);
22412f480046SShri Abhyankar   ierr = PetscFree(aa);CHKERRQ(ierr);
22422f480046SShri Abhyankar   ierr = PetscFree(jj);CHKERRQ(ierr);
22432f480046SShri Abhyankar   ierr = PetscFree2(mask,masked);CHKERRQ(ierr);
22442f480046SShri Abhyankar 
22452f480046SShri Abhyankar   ierr = MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22462f480046SShri Abhyankar   ierr = MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22472f480046SShri Abhyankar   ierr = MatView_Private(newmat);CHKERRQ(ierr);
22482f480046SShri Abhyankar   PetscFunctionReturn(0);
22492f480046SShri Abhyankar }
22502f480046SShri Abhyankar 
22512f480046SShri Abhyankar #undef __FUNCT__
2252c75a6043SHong Zhang #define __FUNCT__ "MatCreateSeqSBAIJWithArrays"
2253c75a6043SHong Zhang /*@
2254c75a6043SHong Zhang      MatCreateSeqSBAIJWithArrays - Creates an sequential SBAIJ matrix using matrix elements
2255c75a6043SHong Zhang               (upper triangular entries in CSR format) provided by the user.
2256c75a6043SHong Zhang 
2257c75a6043SHong Zhang      Collective on MPI_Comm
2258c75a6043SHong Zhang 
2259c75a6043SHong Zhang    Input Parameters:
2260c75a6043SHong Zhang +  comm - must be an MPI communicator of size 1
2261c75a6043SHong Zhang .  bs - size of block
2262c75a6043SHong Zhang .  m - number of rows
2263c75a6043SHong Zhang .  n - number of columns
2264c75a6043SHong Zhang .  i - row indices
2265c75a6043SHong Zhang .  j - column indices
2266c75a6043SHong Zhang -  a - matrix values
2267c75a6043SHong Zhang 
2268c75a6043SHong Zhang    Output Parameter:
2269c75a6043SHong Zhang .  mat - the matrix
2270c75a6043SHong Zhang 
2271c75a6043SHong Zhang    Level: intermediate
2272c75a6043SHong Zhang 
2273c75a6043SHong Zhang    Notes:
2274c75a6043SHong Zhang        The i, j, and a arrays are not copied by this routine, the user must free these arrays
2275c75a6043SHong Zhang     once the matrix is destroyed
2276c75a6043SHong Zhang 
2277c75a6043SHong Zhang        You cannot set new nonzero locations into this matrix, that will generate an error.
2278c75a6043SHong Zhang 
2279c75a6043SHong Zhang        The i and j indices are 0 based
2280c75a6043SHong Zhang 
2281c75a6043SHong Zhang .seealso: MatCreate(), MatCreateMPISBAIJ(), MatCreateSeqSBAIJ()
2282c75a6043SHong Zhang 
2283c75a6043SHong Zhang @*/
2284c75a6043SHong Zhang PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqSBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
2285c75a6043SHong Zhang {
2286c75a6043SHong Zhang   PetscErrorCode ierr;
2287c75a6043SHong Zhang   PetscInt       ii;
2288c75a6043SHong Zhang   Mat_SeqSBAIJ   *sbaij;
2289c75a6043SHong Zhang 
2290c75a6043SHong Zhang   PetscFunctionBegin;
2291e32f2f54SBarry Smith   if (bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"block size %D > 1 is not supported yet",bs);
2292e32f2f54SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
2293c75a6043SHong Zhang 
2294c75a6043SHong Zhang   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
2295c75a6043SHong Zhang   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
2296c75a6043SHong Zhang   ierr = MatSetType(*mat,MATSEQSBAIJ);CHKERRQ(ierr);
2297c75a6043SHong Zhang   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*mat,bs,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
2298c75a6043SHong Zhang   sbaij = (Mat_SeqSBAIJ*)(*mat)->data;
2299c75a6043SHong Zhang   ierr = PetscMalloc2(m,PetscInt,&sbaij->imax,m,PetscInt,&sbaij->ilen);CHKERRQ(ierr);
2300c760cd28SBarry Smith   ierr = PetscLogObjectMemory(*mat,2*m*sizeof(PetscInt));CHKERRQ(ierr);
2301c75a6043SHong Zhang 
2302c75a6043SHong Zhang   sbaij->i = i;
2303c75a6043SHong Zhang   sbaij->j = j;
2304c75a6043SHong Zhang   sbaij->a = a;
2305c75a6043SHong Zhang   sbaij->singlemalloc = PETSC_FALSE;
2306c75a6043SHong Zhang   sbaij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
2307e6b907acSBarry Smith   sbaij->free_a       = PETSC_FALSE;
2308e6b907acSBarry Smith   sbaij->free_ij      = PETSC_FALSE;
2309c75a6043SHong Zhang 
2310c75a6043SHong Zhang   for (ii=0; ii<m; ii++) {
2311c75a6043SHong Zhang     sbaij->ilen[ii] = sbaij->imax[ii] = i[ii+1] - i[ii];
2312c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2313e32f2f54SBarry 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]);
2314c75a6043SHong Zhang #endif
2315c75a6043SHong Zhang   }
2316c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2317c75a6043SHong Zhang   for (ii=0; ii<sbaij->i[m]; ii++) {
2318e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
2319e32f2f54SBarry 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]);
2320c75a6043SHong Zhang   }
2321c75a6043SHong Zhang #endif
2322c75a6043SHong Zhang 
2323c75a6043SHong Zhang   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2324c75a6043SHong Zhang   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2325c75a6043SHong Zhang   PetscFunctionReturn(0);
2326c75a6043SHong Zhang }
2327d06b337dSHong Zhang 
2328d06b337dSHong Zhang 
2329d06b337dSHong Zhang 
233049b5e25fSSatish Balay 
233149b5e25fSSatish Balay 
2332