xref: /petsc/src/mat/impls/sbaij/seq/sbaij.c (revision 8e58a17063def9c11d307e97b7d4ceafd1fd34ac)
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);
1649b5e25fSSatish Balay #define CHUNKSIZE  10
1749b5e25fSSatish Balay 
18b5b17502SBarry Smith 
1949b5e25fSSatish Balay /*
2049b5e25fSSatish Balay      Checks for missing diagonals
2149b5e25fSSatish Balay */
224a2ae208SSatish Balay #undef __FUNCT__
234a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqSBAIJ"
242af78befSBarry Smith PetscErrorCode MatMissingDiagonal_SeqSBAIJ(Mat A,PetscTruth *missing,PetscInt *dd)
2549b5e25fSSatish Balay {
26045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
276849ba73SBarry Smith   PetscErrorCode ierr;
2813f74950SBarry Smith   PetscInt       *diag,*jj = a->j,i;
2949b5e25fSSatish Balay 
3049b5e25fSSatish Balay   PetscFunctionBegin;
31045c9aa0SHong Zhang   ierr = MatMarkDiagonal_SeqSBAIJ(A);CHKERRQ(ierr);
3249b5e25fSSatish Balay   diag = a->diag;
332af78befSBarry Smith   *missing = PETSC_FALSE;
3449b5e25fSSatish Balay   for (i=0; i<a->mbs; i++) {
352af78befSBarry Smith     if (jj[diag[i]] != i) {
362af78befSBarry Smith       *missing    = PETSC_TRUE;
372af78befSBarry Smith       if (dd) *dd = i;
382af78befSBarry Smith       break;
392af78befSBarry Smith     }
4049b5e25fSSatish Balay   }
4149b5e25fSSatish Balay   PetscFunctionReturn(0);
4249b5e25fSSatish Balay }
4349b5e25fSSatish Balay 
444a2ae208SSatish Balay #undef __FUNCT__
454a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqSBAIJ"
46dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqSBAIJ(Mat A)
4749b5e25fSSatish Balay {
48045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
496849ba73SBarry Smith   PetscErrorCode ierr;
5009f38230SBarry Smith   PetscInt       i;
5149b5e25fSSatish Balay 
5249b5e25fSSatish Balay   PetscFunctionBegin;
5309f38230SBarry Smith   if (!a->diag) {
5409f38230SBarry Smith     ierr = PetscMalloc(a->mbs*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
55c760cd28SBarry Smith     ierr = PetscLogObjectMemory(A,a->mbs*sizeof(PetscInt));CHKERRQ(ierr);
56c760cd28SBarry Smith     a->free_diag = PETSC_TRUE;
5709f38230SBarry Smith   }
5809f38230SBarry Smith   for (i=0; i<a->mbs; i++) a->diag[i] = a->i[i];
5949b5e25fSSatish Balay   PetscFunctionReturn(0);
6049b5e25fSSatish Balay }
6149b5e25fSSatish Balay 
624a2ae208SSatish Balay #undef __FUNCT__
634a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqSBAIJ"
648f7157efSSatish Balay static PetscErrorCode MatGetRowIJ_SeqSBAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
6549b5e25fSSatish Balay {
66a6ece127SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
67d0f46423SBarry Smith   PetscInt     i,j,n = a->mbs,nz = a->i[n],bs = A->rmap->bs;
688f7157efSSatish Balay   PetscErrorCode ierr;
6949b5e25fSSatish Balay 
7049b5e25fSSatish Balay   PetscFunctionBegin;
71d3e5a4abSHong Zhang   *nn = n;
72a1373b80SHong Zhang   if (!ia) PetscFunctionReturn(0);
738f7157efSSatish Balay   if (!blockcompressed) {
748f7157efSSatish Balay     /* malloc & create the natural set of indices */
75f1d0d59dSSatish Balay     ierr = PetscMalloc2((n+1)*bs,PetscInt,ia,nz*bs,PetscInt,ja);CHKERRQ(ierr);
768f7157efSSatish Balay     for (i=0; i<n+1; i++) {
778f7157efSSatish Balay       for (j=0; j<bs; j++) {
788f7157efSSatish Balay         *ia[i*bs+j] = a->i[i]*bs+j+oshift;
798f7157efSSatish Balay       }
808f7157efSSatish Balay     }
818f7157efSSatish Balay     for (i=0; i<nz; i++) {
828f7157efSSatish Balay       for (j=0; j<bs; j++) {
838f7157efSSatish Balay         *ja[i*bs+j] = a->j[i]*bs+j+oshift;
848f7157efSSatish Balay       }
858f7157efSSatish Balay     }
868f7157efSSatish Balay   } else { /* blockcompressed */
87a6ece127SHong Zhang     if (oshift == 1) {
88a6ece127SHong Zhang       /* temporarily add 1 to i and j indices */
896c6c5352SBarry Smith       for (i=0; i<nz; i++) a->j[i]++;
90a1373b80SHong Zhang       for (i=0; i<n+1; i++) a->i[i]++;
918f7157efSSatish Balay     }
92a1373b80SHong Zhang     *ia = a->i; *ja = a->j;
93a6ece127SHong Zhang   }
948f7157efSSatish Balay 
9549b5e25fSSatish Balay   PetscFunctionReturn(0);
9649b5e25fSSatish Balay }
9749b5e25fSSatish Balay 
984a2ae208SSatish Balay #undef __FUNCT__
994a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqSBAIJ"
1008f7157efSSatish Balay static PetscErrorCode MatRestoreRowIJ_SeqSBAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
10149b5e25fSSatish Balay {
102b7aaefc3SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
1038f7157efSSatish Balay   PetscInt     i,n = a->mbs,nz = a->i[n];
1048f7157efSSatish Balay   PetscErrorCode ierr;
105a6ece127SHong Zhang 
10649b5e25fSSatish Balay   PetscFunctionBegin;
10749b5e25fSSatish Balay   if (!ia) PetscFunctionReturn(0);
108a6ece127SHong Zhang 
1098f7157efSSatish Balay   if (!blockcompressed) {
1108f7157efSSatish Balay     ierr = PetscFree2(*ia,*ja);CHKERRQ(ierr);
1118f7157efSSatish Balay   } else if (oshift == 1) { /* blockcompressed */
1126c6c5352SBarry Smith     for (i=0; i<nz; i++) a->j[i]--;
113a6ece127SHong Zhang     for (i=0; i<n+1; i++) a->i[i]--;
114a6ece127SHong Zhang   }
1158f7157efSSatish Balay 
116a6ece127SHong Zhang   PetscFunctionReturn(0);
11749b5e25fSSatish Balay }
11849b5e25fSSatish Balay 
1194a2ae208SSatish Balay #undef __FUNCT__
1204a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqSBAIJ"
121dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqSBAIJ(Mat A)
12249b5e25fSSatish Balay {
12349b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
124dfbe8321SBarry Smith   PetscErrorCode ierr;
12549b5e25fSSatish Balay 
12649b5e25fSSatish Balay   PetscFunctionBegin;
127a9f03627SSatish Balay #if defined(PETSC_USE_LOG)
128d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, NZ=%D",A->rmap->N,a->nz);
129a9f03627SSatish Balay #endif
130e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
1317f53bb6cSHong Zhang   if (a->free_diag){ierr = PetscFree(a->diag);CHKERRQ(ierr);}
1329bfd6278SHong Zhang   if (a->row) {ierr = ISDestroy(a->row);CHKERRQ(ierr);}
1339bfd6278SHong Zhang   if (a->col){ierr = ISDestroy(a->col);CHKERRQ(ierr);}
1349bfd6278SHong Zhang   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
135061b2667SBarry Smith   if (a->idiag) {ierr = PetscFree(a->idiag);CHKERRQ(ierr);}
1360def2e27SBarry Smith   if (a->inode.size) {ierr = PetscFree(a->inode.size);CHKERRQ(ierr);}
137c760cd28SBarry Smith   if (a->free_diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
138c760cd28SBarry Smith   if (a->free_imax_ilen) {ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);}
13905b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
14041f059aeSBarry Smith   ierr = PetscFree(a->sor_work);CHKERRQ(ierr);
14105b42c5fSBarry Smith   ierr = PetscFree(a->solves_work);CHKERRQ(ierr);
14205b42c5fSBarry Smith   ierr = PetscFree(a->mult_work);CHKERRQ(ierr);
14305b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
14405b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
1454da8f245SBarry Smith   if (a->free_jshort) {ierr = PetscFree(a->jshort);CHKERRQ(ierr);}
1461a3463dfSHong Zhang   ierr = PetscFree(a->inew);CHKERRQ(ierr);
1474da8f245SBarry Smith   if (a->parent) {ierr = MatDestroy(a->parent);CHKERRQ(ierr);}
14849b5e25fSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
149901853e0SKris Buschelman 
150dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
151901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
152901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
153901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
154901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqaij_C","",PETSC_NULL);CHKERRQ(ierr);
155901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
156901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
15749b5e25fSSatish Balay   PetscFunctionReturn(0);
15849b5e25fSSatish Balay }
15949b5e25fSSatish Balay 
1604a2ae208SSatish Balay #undef __FUNCT__
1614a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqSBAIJ"
1624e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqSBAIJ(Mat A,MatOption op,PetscTruth flg)
16349b5e25fSSatish Balay {
164045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
16563ba0a88SBarry Smith   PetscErrorCode ierr;
16649b5e25fSSatish Balay 
16749b5e25fSSatish Balay   PetscFunctionBegin;
1684d9d31abSKris Buschelman   switch (op) {
1694d9d31abSKris Buschelman   case MAT_ROW_ORIENTED:
1704e0d8c25SBarry Smith     a->roworiented = flg;
1714d9d31abSKris Buschelman     break;
172a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
173a9817697SBarry Smith     a->keepnonzeropattern = flg;
1744d9d31abSKris Buschelman     break;
175512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
176512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1774d9d31abSKris Buschelman     break;
1784d9d31abSKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
1794e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1804d9d31abSKris Buschelman     break;
1814d9d31abSKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1824e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1834d9d31abSKris Buschelman     break;
18428b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
18528b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
18628b2fa4aSMatthew Knepley     break;
1874e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1884d9d31abSKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1894d9d31abSKris Buschelman   case MAT_USE_HASH_TABLE:
190290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1914d9d31abSKris Buschelman     break;
1929a4540c5SBarry Smith   case MAT_HERMITIAN:
193e32f2f54SBarry Smith     if (!A->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call MatAssemblyEnd() first");
1940bc54ff2SBarry Smith     if (A->cmap->n < 65536 && A->cmap->bs == 1) {
195eeffb40dSHong Zhang       A->ops->mult = MatMult_SeqSBAIJ_1_Hermitian_ushort;
1960bc54ff2SBarry Smith     } else if (A->cmap->bs == 1) {
197eeffb40dSHong Zhang       A->ops->mult = MatMult_SeqSBAIJ_1_Hermitian;
198e32f2f54SBarry Smith     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for Hermitian with block size greater than 1");
199eeffb40dSHong Zhang     break;
20077e54ba9SKris Buschelman   case MAT_SYMMETRIC:
20177e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
2029a4540c5SBarry Smith   case MAT_SYMMETRY_ETERNAL:
203e32f2f54SBarry Smith     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix must be symmetric");
204290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s not relevent\n",MatOptions[op]);CHKERRQ(ierr);
205290bbb0aSBarry Smith     break;
206941593c8SHong Zhang   case MAT_IGNORE_LOWER_TRIANGULAR:
2074e0d8c25SBarry Smith     a->ignore_ltriangular = flg;
208941593c8SHong Zhang     break;
209941593c8SHong Zhang   case MAT_ERROR_LOWER_TRIANGULAR:
2104e0d8c25SBarry Smith     a->ignore_ltriangular = flg;
21177e54ba9SKris Buschelman     break;
212f5edf698SHong Zhang   case MAT_GETROW_UPPERTRIANGULAR:
2134e0d8c25SBarry Smith     a->getrow_utriangular = flg;
214f5edf698SHong Zhang     break;
2154d9d31abSKris Buschelman   default:
216e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
21749b5e25fSSatish Balay   }
21849b5e25fSSatish Balay   PetscFunctionReturn(0);
21949b5e25fSSatish Balay }
22049b5e25fSSatish Balay 
2214a2ae208SSatish Balay #undef __FUNCT__
2224a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqSBAIJ"
22313f74950SBarry Smith PetscErrorCode MatGetRow_SeqSBAIJ(Mat A,PetscInt row,PetscInt *ncols,PetscInt **cols,PetscScalar **v)
22449b5e25fSSatish Balay {
22549b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
2266849ba73SBarry Smith   PetscErrorCode ierr;
22713f74950SBarry Smith   PetscInt       itmp,i,j,k,M,*ai,*aj,bs,bn,bp,*cols_i,bs2;
22849b5e25fSSatish Balay   MatScalar      *aa,*aa_i;
22987828ca2SBarry Smith   PetscScalar    *v_i;
23049b5e25fSSatish Balay 
23149b5e25fSSatish Balay   PetscFunctionBegin;
232e32f2f54SBarry 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()");
233f5edf698SHong Zhang   /* Get the upper triangular part of the row */
234d0f46423SBarry Smith   bs  = A->rmap->bs;
23549b5e25fSSatish Balay   ai  = a->i;
23649b5e25fSSatish Balay   aj  = a->j;
23749b5e25fSSatish Balay   aa  = a->a;
23849b5e25fSSatish Balay   bs2 = a->bs2;
23949b5e25fSSatish Balay 
240e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Row %D out of range", row);
24149b5e25fSSatish Balay 
24249b5e25fSSatish Balay   bn  = row/bs;   /* Block number */
24349b5e25fSSatish Balay   bp  = row % bs; /* Block position */
24449b5e25fSSatish Balay   M   = ai[bn+1] - ai[bn];
24549b5e25fSSatish Balay   *ncols = bs*M;
24649b5e25fSSatish Balay 
24749b5e25fSSatish Balay   if (v) {
24849b5e25fSSatish Balay     *v = 0;
24949b5e25fSSatish Balay     if (*ncols) {
25087828ca2SBarry Smith       ierr = PetscMalloc((*ncols+row)*sizeof(PetscScalar),v);CHKERRQ(ierr);
25149b5e25fSSatish Balay       for (i=0; i<M; i++) { /* for each block in the block row */
25249b5e25fSSatish Balay         v_i  = *v + i*bs;
25349b5e25fSSatish Balay         aa_i = aa + bs2*(ai[bn] + i);
25449b5e25fSSatish Balay         for (j=bp,k=0; j<bs2; j+=bs,k++) {v_i[k] = aa_i[j];}
25549b5e25fSSatish Balay       }
25649b5e25fSSatish Balay     }
25749b5e25fSSatish Balay   }
25849b5e25fSSatish Balay 
25949b5e25fSSatish Balay   if (cols) {
26049b5e25fSSatish Balay     *cols = 0;
26149b5e25fSSatish Balay     if (*ncols) {
26213f74950SBarry Smith       ierr = PetscMalloc((*ncols+row)*sizeof(PetscInt),cols);CHKERRQ(ierr);
26349b5e25fSSatish Balay       for (i=0; i<M; i++) { /* for each block in the block row */
26449b5e25fSSatish Balay         cols_i = *cols + i*bs;
26549b5e25fSSatish Balay         itmp  = bs*aj[ai[bn] + i];
26649b5e25fSSatish Balay         for (j=0; j<bs; j++) {cols_i[j] = itmp++;}
26749b5e25fSSatish Balay       }
26849b5e25fSSatish Balay     }
26949b5e25fSSatish Balay   }
27049b5e25fSSatish Balay 
27149b5e25fSSatish Balay   /*search column A(0:row-1,row) (=A(row,0:row-1)). Could be expensive! */
2725ddb2528SHong Zhang   /* this segment is currently removed, so only entries in the upper triangle are obtained */
2735ddb2528SHong Zhang #ifdef column_search
27449b5e25fSSatish Balay   v_i    = *v    + M*bs;
27549b5e25fSSatish Balay   cols_i = *cols + M*bs;
27649b5e25fSSatish Balay   for (i=0; i<bn; i++){ /* for each block row */
27749b5e25fSSatish Balay     M = ai[i+1] - ai[i];
27849b5e25fSSatish Balay     for (j=0; j<M; j++){
27949b5e25fSSatish Balay       itmp = aj[ai[i] + j];    /* block column value */
28049b5e25fSSatish Balay       if (itmp == bn){
28149b5e25fSSatish Balay         aa_i   = aa    + bs2*(ai[i] + j) + bs*bp;
28249b5e25fSSatish Balay         for (k=0; k<bs; k++) {
28349b5e25fSSatish Balay           *cols_i++ = i*bs+k;
28449b5e25fSSatish Balay           *v_i++    = aa_i[k];
28549b5e25fSSatish Balay         }
28649b5e25fSSatish Balay         *ncols += bs;
28749b5e25fSSatish Balay         break;
28849b5e25fSSatish Balay       }
28949b5e25fSSatish Balay     }
29049b5e25fSSatish Balay   }
2915ddb2528SHong Zhang #endif
29249b5e25fSSatish Balay   PetscFunctionReturn(0);
29349b5e25fSSatish Balay }
29449b5e25fSSatish Balay 
2954a2ae208SSatish Balay #undef __FUNCT__
2964a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqSBAIJ"
29713f74950SBarry Smith PetscErrorCode MatRestoreRow_SeqSBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
29849b5e25fSSatish Balay {
299dfbe8321SBarry Smith   PetscErrorCode ierr;
30049b5e25fSSatish Balay 
30149b5e25fSSatish Balay   PetscFunctionBegin;
30205b42c5fSBarry Smith   if (idx) {ierr = PetscFree(*idx);CHKERRQ(ierr);}
30305b42c5fSBarry Smith   if (v)   {ierr = PetscFree(*v);CHKERRQ(ierr);}
30449b5e25fSSatish Balay   PetscFunctionReturn(0);
30549b5e25fSSatish Balay }
30649b5e25fSSatish Balay 
3074a2ae208SSatish Balay #undef __FUNCT__
308f5edf698SHong Zhang #define __FUNCT__ "MatGetRowUpperTriangular_SeqSBAIJ"
309f5edf698SHong Zhang PetscErrorCode MatGetRowUpperTriangular_SeqSBAIJ(Mat A)
310f5edf698SHong Zhang {
311f5edf698SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
312f5edf698SHong Zhang 
313f5edf698SHong Zhang   PetscFunctionBegin;
314f5edf698SHong Zhang   a->getrow_utriangular = PETSC_TRUE;
315f5edf698SHong Zhang   PetscFunctionReturn(0);
316f5edf698SHong Zhang }
317f5edf698SHong Zhang #undef __FUNCT__
318f5edf698SHong Zhang #define __FUNCT__ "MatRestoreRowUpperTriangular_SeqSBAIJ"
319f5edf698SHong Zhang PetscErrorCode MatRestoreRowUpperTriangular_SeqSBAIJ(Mat A)
320f5edf698SHong Zhang {
321f5edf698SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
322f5edf698SHong Zhang 
323f5edf698SHong Zhang   PetscFunctionBegin;
324f5edf698SHong Zhang   a->getrow_utriangular = PETSC_FALSE;
325f5edf698SHong Zhang   PetscFunctionReturn(0);
326f5edf698SHong Zhang }
327f5edf698SHong Zhang 
328f5edf698SHong Zhang #undef __FUNCT__
3294a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqSBAIJ"
330fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqSBAIJ(Mat A,MatReuse reuse,Mat *B)
33149b5e25fSSatish Balay {
332dfbe8321SBarry Smith   PetscErrorCode ierr;
33349b5e25fSSatish Balay   PetscFunctionBegin;
334815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
335999d9058SBarry Smith     ierr = MatDuplicate(A,MAT_COPY_VALUES,B);CHKERRQ(ierr);
336fc4dec0aSBarry Smith   }
3378115998fSBarry Smith   PetscFunctionReturn(0);
33849b5e25fSSatish Balay }
33949b5e25fSSatish Balay 
3404a2ae208SSatish Balay #undef __FUNCT__
3414a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_ASCII"
3426849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_ASCII(Mat A,PetscViewer viewer)
34349b5e25fSSatish Balay {
34449b5e25fSSatish Balay   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
345dfbe8321SBarry Smith   PetscErrorCode    ierr;
346d0f46423SBarry Smith   PetscInt          i,j,bs = A->rmap->bs,k,l,bs2=a->bs2;
3472dcb1b2aSMatthew Knepley   const char        *name;
348f3ef73ceSBarry Smith   PetscViewerFormat format;
34949b5e25fSSatish Balay 
35049b5e25fSSatish Balay   PetscFunctionBegin;
35180fe4e49SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
352b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
353456192e2SBarry Smith   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
35477431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);CHKERRQ(ierr);
355fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
356d2507d54SMatthew Knepley     Mat aij;
357d2507d54SMatthew Knepley 
358d5f3da31SBarry Smith     if (A->factortype && bs>1){
35970d5e725SHong 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);
36070d5e725SHong Zhang       PetscFunctionReturn(0);
36170d5e725SHong Zhang     }
362c9f458caSMatthew Knepley     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&aij);CHKERRQ(ierr);
363c9f458caSMatthew Knepley     ierr = MatView(aij,viewer);CHKERRQ(ierr);
364c9f458caSMatthew Knepley     ierr = MatDestroy(aij);CHKERRQ(ierr);
365fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
366b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
36749b5e25fSSatish Balay     for (i=0; i<a->mbs; i++) {
36849b5e25fSSatish Balay       for (j=0; j<bs; j++) {
36977431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
37049b5e25fSSatish Balay         for (k=a->i[i]; k<a->i[i+1]; k++) {
37149b5e25fSSatish Balay           for (l=0; l<bs; l++) {
37249b5e25fSSatish Balay #if defined(PETSC_USE_COMPLEX)
37349b5e25fSSatish Balay             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
374a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",bs*a->j[k]+l,
37549b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
37649b5e25fSSatish Balay             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
377a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",bs*a->j[k]+l,
37849b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),-PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
37949b5e25fSSatish Balay             } else if (PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
380a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
38149b5e25fSSatish Balay             }
38249b5e25fSSatish Balay #else
38349b5e25fSSatish Balay             if (a->a[bs2*k + l*bs + j] != 0.0) {
384a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
38549b5e25fSSatish Balay             }
38649b5e25fSSatish Balay #endif
38749b5e25fSSatish Balay           }
38849b5e25fSSatish Balay         }
389b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
39049b5e25fSSatish Balay       }
39149b5e25fSSatish Balay     }
392b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
393c1490034SHong Zhang   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
394c1490034SHong Zhang      PetscFunctionReturn(0);
39549b5e25fSSatish Balay   } else {
396d5f3da31SBarry Smith     if (A->factortype && bs>1){
3978608aa04SHong Zhang       ierr = PetscPrintf(PETSC_COMM_SELF,"Warning: matrix is factored. MatView_SeqSBAIJ_ASCII() may not display complete or logically correct entries!\n");CHKERRQ(ierr);
3988608aa04SHong Zhang     }
399b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
40049b5e25fSSatish Balay     for (i=0; i<a->mbs; i++) {
40149b5e25fSSatish Balay       for (j=0; j<bs; j++) {
40277431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
40349b5e25fSSatish Balay         for (k=a->i[i]; k<a->i[i+1]; k++) {
40449b5e25fSSatish Balay           for (l=0; l<bs; l++) {
40549b5e25fSSatish Balay #if defined(PETSC_USE_COMPLEX)
40649b5e25fSSatish Balay             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0) {
407a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",bs*a->j[k]+l,
40849b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
40949b5e25fSSatish Balay             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0) {
410a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",bs*a->j[k]+l,
41149b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),-PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
41249b5e25fSSatish Balay             } else {
413a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
41449b5e25fSSatish Balay             }
41549b5e25fSSatish Balay #else
416e9f7bc9eSHong Zhang             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
41749b5e25fSSatish Balay #endif
41849b5e25fSSatish Balay           }
41949b5e25fSSatish Balay         }
420b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
42149b5e25fSSatish Balay       }
42249b5e25fSSatish Balay     }
423b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
42449b5e25fSSatish Balay   }
425b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
42649b5e25fSSatish Balay   PetscFunctionReturn(0);
42749b5e25fSSatish Balay }
42849b5e25fSSatish Balay 
4294a2ae208SSatish Balay #undef __FUNCT__
4304a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_Draw_Zoom"
4316849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
43249b5e25fSSatish Balay {
43349b5e25fSSatish Balay   Mat            A = (Mat) Aa;
43449b5e25fSSatish Balay   Mat_SeqSBAIJ   *a=(Mat_SeqSBAIJ*)A->data;
4356849ba73SBarry Smith   PetscErrorCode ierr;
436d0f46423SBarry Smith   PetscInt       row,i,j,k,l,mbs=a->mbs,color,bs=A->rmap->bs,bs2=a->bs2;
43713f74950SBarry Smith   PetscMPIInt    rank;
43849b5e25fSSatish Balay   PetscReal      xl,yl,xr,yr,x_l,x_r,y_l,y_r;
43949b5e25fSSatish Balay   MatScalar      *aa;
44049b5e25fSSatish Balay   MPI_Comm       comm;
441b0a32e0cSBarry Smith   PetscViewer    viewer;
44249b5e25fSSatish Balay 
44349b5e25fSSatish Balay   PetscFunctionBegin;
44449b5e25fSSatish Balay   /*
44549b5e25fSSatish Balay     This is nasty. If this is called from an originally parallel matrix
44649b5e25fSSatish Balay     then all processes call this,but only the first has the matrix so the
44749b5e25fSSatish Balay     rest should return immediately.
44849b5e25fSSatish Balay   */
44949b5e25fSSatish Balay   ierr = PetscObjectGetComm((PetscObject)draw,&comm);CHKERRQ(ierr);
45049b5e25fSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
45149b5e25fSSatish Balay   if (rank) PetscFunctionReturn(0);
45249b5e25fSSatish Balay 
45349b5e25fSSatish Balay   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
45449b5e25fSSatish Balay 
455b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
456b0a32e0cSBarry Smith   PetscDrawString(draw, .3*(xl+xr), .3*(yl+yr), PETSC_DRAW_BLACK, "symmetric");
45749b5e25fSSatish Balay 
45849b5e25fSSatish Balay   /* loop over matrix elements drawing boxes */
459b0a32e0cSBarry Smith   color = PETSC_DRAW_BLUE;
46049b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
46149b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
462d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
46349b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
46449b5e25fSSatish Balay       aa = a->a + j*bs2;
46549b5e25fSSatish Balay       for (k=0; k<bs; k++) {
46649b5e25fSSatish Balay         for (l=0; l<bs; l++) {
46749b5e25fSSatish Balay           if (PetscRealPart(*aa++) >=  0.) continue;
468b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
46949b5e25fSSatish Balay         }
47049b5e25fSSatish Balay       }
47149b5e25fSSatish Balay     }
47249b5e25fSSatish Balay   }
473b0a32e0cSBarry Smith   color = PETSC_DRAW_CYAN;
47449b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
47549b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
476d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
47749b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
47849b5e25fSSatish Balay       aa = a->a + j*bs2;
47949b5e25fSSatish Balay       for (k=0; k<bs; k++) {
48049b5e25fSSatish Balay         for (l=0; l<bs; l++) {
48149b5e25fSSatish Balay           if (PetscRealPart(*aa++) != 0.) continue;
482b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
48349b5e25fSSatish Balay         }
48449b5e25fSSatish Balay       }
48549b5e25fSSatish Balay     }
48649b5e25fSSatish Balay   }
48749b5e25fSSatish Balay 
488b0a32e0cSBarry Smith   color = PETSC_DRAW_RED;
48949b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
49049b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
491d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
49249b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
49349b5e25fSSatish Balay       aa = a->a + j*bs2;
49449b5e25fSSatish Balay       for (k=0; k<bs; k++) {
49549b5e25fSSatish Balay         for (l=0; l<bs; l++) {
49649b5e25fSSatish Balay           if (PetscRealPart(*aa++) <= 0.) continue;
497b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
49849b5e25fSSatish Balay         }
49949b5e25fSSatish Balay       }
50049b5e25fSSatish Balay     }
50149b5e25fSSatish Balay   }
50249b5e25fSSatish Balay   PetscFunctionReturn(0);
50349b5e25fSSatish Balay }
50449b5e25fSSatish Balay 
5054a2ae208SSatish Balay #undef __FUNCT__
5064a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_Draw"
5076849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_Draw(Mat A,PetscViewer viewer)
50849b5e25fSSatish Balay {
509dfbe8321SBarry Smith   PetscErrorCode ierr;
51049b5e25fSSatish Balay   PetscReal      xl,yl,xr,yr,w,h;
511b0a32e0cSBarry Smith   PetscDraw      draw;
51249b5e25fSSatish Balay   PetscTruth     isnull;
51349b5e25fSSatish Balay 
51449b5e25fSSatish Balay   PetscFunctionBegin;
515b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
516b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
51749b5e25fSSatish Balay 
51849b5e25fSSatish Balay   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
519d0f46423SBarry Smith   xr  = A->rmap->N; yr = A->rmap->N; h = yr/10.0; w = xr/10.0;
52049b5e25fSSatish Balay   xr += w;    yr += h;  xl = -w;     yl = -h;
521b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
522b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqSBAIJ_Draw_Zoom,A);CHKERRQ(ierr);
52349b5e25fSSatish Balay   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
52449b5e25fSSatish Balay   PetscFunctionReturn(0);
52549b5e25fSSatish Balay }
52649b5e25fSSatish Balay 
5274a2ae208SSatish Balay #undef __FUNCT__
5284a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ"
529dfbe8321SBarry Smith PetscErrorCode MatView_SeqSBAIJ(Mat A,PetscViewer viewer)
53049b5e25fSSatish Balay {
531dfbe8321SBarry Smith   PetscErrorCode ierr;
53232077d6dSBarry Smith   PetscTruth     iascii,isdraw;
53308917f38SBarry Smith   FILE           *file = 0;
53449b5e25fSSatish Balay 
53549b5e25fSSatish Balay   PetscFunctionBegin;
5362692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
5372692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
53832077d6dSBarry Smith   if (iascii){
53949b5e25fSSatish Balay     ierr = MatView_SeqSBAIJ_ASCII(A,viewer);CHKERRQ(ierr);
54049b5e25fSSatish Balay   } else if (isdraw) {
54149b5e25fSSatish Balay     ierr = MatView_SeqSBAIJ_Draw(A,viewer);CHKERRQ(ierr);
54249b5e25fSSatish Balay   } else {
543a5e6ed63SBarry Smith     Mat B;
544ceb03754SKris Buschelman     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&B);CHKERRQ(ierr);
545a5e6ed63SBarry Smith     ierr = MatView(B,viewer);CHKERRQ(ierr);
546a5e6ed63SBarry Smith     ierr = MatDestroy(B);CHKERRQ(ierr);
54708917f38SBarry Smith     ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
54808917f38SBarry Smith     if (file) {
54908917f38SBarry Smith       fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
55008917f38SBarry Smith     }
55149b5e25fSSatish Balay   }
55249b5e25fSSatish Balay   PetscFunctionReturn(0);
55349b5e25fSSatish Balay }
55449b5e25fSSatish Balay 
55549b5e25fSSatish Balay 
5564a2ae208SSatish Balay #undef __FUNCT__
5574a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqSBAIJ"
55813f74950SBarry Smith PetscErrorCode MatGetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
55949b5e25fSSatish Balay {
560045c9aa0SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
56113f74950SBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
56213f74950SBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
563d0f46423SBarry Smith   PetscInt     brow,bcol,ridx,cidx,bs=A->rmap->bs,bs2=a->bs2;
56497e567efSBarry Smith   MatScalar    *ap,*aa = a->a;
56549b5e25fSSatish Balay 
56649b5e25fSSatish Balay   PetscFunctionBegin;
56749b5e25fSSatish Balay   for (k=0; k<m; k++) { /* loop over rows */
56849b5e25fSSatish Balay     row  = im[k]; brow = row/bs;
569e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
570e32f2f54SBarry 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);
57149b5e25fSSatish Balay     rp   = aj + ai[brow] ; ap = aa + bs2*ai[brow] ;
57249b5e25fSSatish Balay     nrow = ailen[brow];
57349b5e25fSSatish Balay     for (l=0; l<n; l++) { /* loop over columns */
574e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
575e32f2f54SBarry 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);
57649b5e25fSSatish Balay       col  = in[l] ;
57749b5e25fSSatish Balay       bcol = col/bs;
57849b5e25fSSatish Balay       cidx = col%bs;
57949b5e25fSSatish Balay       ridx = row%bs;
58049b5e25fSSatish Balay       high = nrow;
58149b5e25fSSatish Balay       low  = 0; /* assume unsorted */
58249b5e25fSSatish Balay       while (high-low > 5) {
58349b5e25fSSatish Balay         t = (low+high)/2;
58449b5e25fSSatish Balay         if (rp[t] > bcol) high = t;
58549b5e25fSSatish Balay         else             low  = t;
58649b5e25fSSatish Balay       }
58749b5e25fSSatish Balay       for (i=low; i<high; i++) {
58849b5e25fSSatish Balay         if (rp[i] > bcol) break;
58949b5e25fSSatish Balay         if (rp[i] == bcol) {
59049b5e25fSSatish Balay           *v++ = ap[bs2*i+bs*cidx+ridx];
59149b5e25fSSatish Balay           goto finished;
59249b5e25fSSatish Balay         }
59349b5e25fSSatish Balay       }
59497e567efSBarry Smith       *v++ = 0.0;
59549b5e25fSSatish Balay       finished:;
59649b5e25fSSatish Balay     }
59749b5e25fSSatish Balay   }
59849b5e25fSSatish Balay   PetscFunctionReturn(0);
59949b5e25fSSatish Balay }
60049b5e25fSSatish Balay 
60149b5e25fSSatish Balay 
6024a2ae208SSatish Balay #undef __FUNCT__
6034a2ae208SSatish Balay #define __FUNCT__ "MatSetValuesBlocked_SeqSBAIJ"
60413f74950SBarry Smith PetscErrorCode MatSetValuesBlocked_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
60549b5e25fSSatish Balay {
6060880e062SHong Zhang   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
6076849ba73SBarry Smith   PetscErrorCode    ierr;
608e2ee6c50SBarry Smith   PetscInt          *rp,k,low,high,t,ii,jj,row,nrow,i,col,l,rmax,N,lastcol = -1;
60913f74950SBarry Smith   PetscInt          *imax=a->imax,*ai=a->i,*ailen=a->ilen;
610d0f46423SBarry Smith   PetscInt          *aj=a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs,stepval;
6110880e062SHong Zhang   PetscTruth        roworiented=a->roworiented;
612dd6ea824SBarry Smith   const PetscScalar *value = v;
613f15d580aSBarry Smith   MatScalar         *ap,*aa = a->a,*bap;
6140880e062SHong Zhang 
61549b5e25fSSatish Balay   PetscFunctionBegin;
6160880e062SHong Zhang   if (roworiented) {
6170880e062SHong Zhang     stepval = (n-1)*bs;
6180880e062SHong Zhang   } else {
6190880e062SHong Zhang     stepval = (m-1)*bs;
6200880e062SHong Zhang   }
6210880e062SHong Zhang   for (k=0; k<m; k++) { /* loop over added rows */
6220880e062SHong Zhang     row  = im[k];
6230880e062SHong Zhang     if (row < 0) continue;
6242515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
625e32f2f54SBarry Smith     if (row >= a->mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,a->mbs-1);
6260880e062SHong Zhang #endif
6270880e062SHong Zhang     rp   = aj + ai[row];
6280880e062SHong Zhang     ap   = aa + bs2*ai[row];
6290880e062SHong Zhang     rmax = imax[row];
6300880e062SHong Zhang     nrow = ailen[row];
6310880e062SHong Zhang     low  = 0;
632818f2c47SBarry Smith     high = nrow;
6330880e062SHong Zhang     for (l=0; l<n; l++) { /* loop over added columns */
6340880e062SHong Zhang       if (in[l] < 0) continue;
6350880e062SHong Zhang       col = in[l];
6362515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
637e32f2f54SBarry Smith       if (col >= a->nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",col,a->nbs-1);
638b1823623SSatish Balay #endif
639b98bf0e1SJed Brown       if (col < row) {
640b98bf0e1SJed Brown         if (a->ignore_ltriangular) {
641b98bf0e1SJed Brown           continue; /* ignore lower triangular block */
642b98bf0e1SJed Brown         } else {
643e32f2f54SBarry 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)");
644b98bf0e1SJed Brown         }
645b98bf0e1SJed Brown       }
6460880e062SHong Zhang       if (roworiented) {
6470880e062SHong Zhang         value = v + k*(stepval+bs)*bs + l*bs;
6480880e062SHong Zhang       } else {
6490880e062SHong Zhang         value = v + l*(stepval+bs)*bs + k*bs;
6500880e062SHong Zhang       }
6517cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
652e2ee6c50SBarry Smith       lastcol = col;
6530880e062SHong Zhang       while (high-low > 7) {
6540880e062SHong Zhang         t = (low+high)/2;
6550880e062SHong Zhang         if (rp[t] > col) high = t;
6560880e062SHong Zhang         else             low  = t;
6570880e062SHong Zhang       }
6580880e062SHong Zhang       for (i=low; i<high; i++) {
6590880e062SHong Zhang         if (rp[i] > col) break;
6600880e062SHong Zhang         if (rp[i] == col) {
6610880e062SHong Zhang           bap  = ap +  bs2*i;
6620880e062SHong Zhang           if (roworiented) {
6630880e062SHong Zhang             if (is == ADD_VALUES) {
6640880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6650880e062SHong Zhang                 for (jj=ii; jj<bs2; jj+=bs) {
6660880e062SHong Zhang                   bap[jj] += *value++;
6670880e062SHong Zhang                 }
6680880e062SHong Zhang               }
6690880e062SHong Zhang             } else {
6700880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6710880e062SHong Zhang                 for (jj=ii; jj<bs2; jj+=bs) {
6720880e062SHong Zhang                   bap[jj] = *value++;
6730880e062SHong Zhang                 }
6740880e062SHong Zhang                }
6750880e062SHong Zhang             }
6760880e062SHong Zhang           } else {
6770880e062SHong Zhang             if (is == ADD_VALUES) {
6780880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6790880e062SHong Zhang                 for (jj=0; jj<bs; jj++) {
6800880e062SHong Zhang                   *bap++ += *value++;
6810880e062SHong Zhang                 }
6820880e062SHong Zhang               }
6830880e062SHong Zhang             } else {
6840880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6850880e062SHong Zhang                 for (jj=0; jj<bs; jj++) {
6860880e062SHong Zhang                   *bap++  = *value++;
6870880e062SHong Zhang                 }
6880880e062SHong Zhang               }
6890880e062SHong Zhang             }
6900880e062SHong Zhang           }
6910880e062SHong Zhang           goto noinsert2;
6920880e062SHong Zhang         }
6930880e062SHong Zhang       }
6940880e062SHong Zhang       if (nonew == 1) goto noinsert2;
695e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
696421e10b8SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
697c03d1d03SSatish Balay       N = nrow++ - 1; high++;
6980880e062SHong Zhang       /* shift up all the later entries in this row */
6990880e062SHong Zhang       for (ii=N; ii>=i; ii--) {
7000880e062SHong Zhang         rp[ii+1] = rp[ii];
7010880e062SHong Zhang         ierr = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
7020880e062SHong Zhang       }
7030880e062SHong Zhang       if (N >= i) {
7040880e062SHong Zhang         ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
7050880e062SHong Zhang       }
7060880e062SHong Zhang       rp[i] = col;
7070880e062SHong Zhang       bap   = ap +  bs2*i;
7080880e062SHong Zhang       if (roworiented) {
7090880e062SHong Zhang         for (ii=0; ii<bs; ii++,value+=stepval) {
7100880e062SHong Zhang           for (jj=ii; jj<bs2; jj+=bs) {
7110880e062SHong Zhang             bap[jj] = *value++;
7120880e062SHong Zhang           }
7130880e062SHong Zhang         }
7140880e062SHong Zhang       } else {
7150880e062SHong Zhang         for (ii=0; ii<bs; ii++,value+=stepval) {
7160880e062SHong Zhang           for (jj=0; jj<bs; jj++) {
7170880e062SHong Zhang             *bap++  = *value++;
7180880e062SHong Zhang           }
7190880e062SHong Zhang         }
7200880e062SHong Zhang        }
7210880e062SHong Zhang     noinsert2:;
7220880e062SHong Zhang       low = i;
7230880e062SHong Zhang     }
7240880e062SHong Zhang     ailen[row] = nrow;
7250880e062SHong Zhang   }
7260880e062SHong Zhang    PetscFunctionReturn(0);
72749b5e25fSSatish Balay }
72849b5e25fSSatish Balay 
72964831d72SBarry Smith /*
73064831d72SBarry Smith     This is not yet used
73164831d72SBarry Smith */
7324a2ae208SSatish Balay #undef __FUNCT__
7334108e4d5SBarry Smith #define __FUNCT__ "MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode"
7344108e4d5SBarry Smith PetscErrorCode MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode(Mat A)
7350def2e27SBarry Smith {
7360def2e27SBarry Smith   Mat_SeqSBAIJ    *a = (Mat_SeqSBAIJ*)A->data;
7370def2e27SBarry Smith   PetscErrorCode  ierr;
7380def2e27SBarry Smith   const PetscInt  *ai = a->i, *aj = a->j,*cols;
7390def2e27SBarry Smith   PetscInt        i = 0,j,blk_size,m = A->rmap->n,node_count = 0,nzx,nzy,*ns,row,nz,cnt,cnt2,*counts;
7400def2e27SBarry Smith   PetscTruth      flag;
7410def2e27SBarry Smith 
7420def2e27SBarry Smith   PetscFunctionBegin;
7430def2e27SBarry Smith   ierr = PetscMalloc(m*sizeof(PetscInt),&ns);CHKERRQ(ierr);
7440def2e27SBarry Smith   while (i < m){
7450def2e27SBarry Smith     nzx = ai[i+1] - ai[i];       /* Number of nonzeros */
7460def2e27SBarry Smith     /* Limits the number of elements in a node to 'a->inode.limit' */
7470def2e27SBarry Smith     for (j=i+1,blk_size=1; j<m && blk_size <a->inode.limit; ++j,++blk_size) {
7480def2e27SBarry Smith       nzy  = ai[j+1] - ai[j];
7490def2e27SBarry Smith       if (nzy != (nzx - j + i)) break;
7500def2e27SBarry Smith       ierr = PetscMemcmp(aj + ai[i] + j - i,aj + ai[j],nzy*sizeof(PetscInt),&flag);CHKERRQ(ierr);
7510def2e27SBarry Smith       if (!flag) break;
7520def2e27SBarry Smith     }
7530def2e27SBarry Smith     ns[node_count++] = blk_size;
7540def2e27SBarry Smith     i = j;
7550def2e27SBarry Smith   }
7560def2e27SBarry Smith   if (!a->inode.size && m && node_count > .9*m) {
7570def2e27SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
7580def2e27SBarry Smith     ierr = PetscInfo2(A,"Found %D nodes out of %D rows. Not using Inode routines\n",node_count,m);CHKERRQ(ierr);
7590def2e27SBarry Smith   } else {
7600def2e27SBarry Smith     a->inode.node_count = node_count;
7610def2e27SBarry Smith     ierr = PetscMalloc(node_count*sizeof(PetscInt),&a->inode.size);CHKERRQ(ierr);
762c760cd28SBarry Smith     ierr = PetscLogObjectMemory(A,node_count*sizeof(PetscInt));CHKERRQ(ierr);
7630def2e27SBarry Smith     ierr = PetscMemcpy(a->inode.size,ns,node_count*sizeof(PetscInt));
7640def2e27SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
7650def2e27SBarry Smith     ierr = PetscInfo3(A,"Found %D nodes of %D. Limit used: %D. Using Inode routines\n",node_count,m,a->inode.limit);CHKERRQ(ierr);
7660def2e27SBarry Smith 
7670def2e27SBarry Smith     /* count collections of adjacent columns in each inode */
7680def2e27SBarry Smith     row = 0;
7690def2e27SBarry Smith     cnt = 0;
7700def2e27SBarry Smith     for (i=0; i<node_count; i++) {
7710def2e27SBarry Smith       cols = aj + ai[row] + a->inode.size[i];
7720def2e27SBarry Smith       nz   = ai[row+1] - ai[row] - a->inode.size[i];
7730def2e27SBarry Smith       for (j=1; j<nz; j++) {
7740def2e27SBarry Smith         if (cols[j] != cols[j-1]+1) {
7750def2e27SBarry Smith           cnt++;
7760def2e27SBarry Smith         }
7770def2e27SBarry Smith       }
7780def2e27SBarry Smith       cnt++;
7790def2e27SBarry Smith       row += a->inode.size[i];
7800def2e27SBarry Smith     }
7810def2e27SBarry Smith     ierr = PetscMalloc(2*cnt*sizeof(PetscInt),&counts);CHKERRQ(ierr);
7820def2e27SBarry Smith     cnt = 0;
7830def2e27SBarry Smith     row = 0;
7840def2e27SBarry Smith     for (i=0; i<node_count; i++) {
7850def2e27SBarry Smith       cols          = aj + ai[row] + a->inode.size[i];
7860def2e27SBarry Smith 	  CHKMEMQ;
7870def2e27SBarry Smith       counts[2*cnt] = cols[0];
7880def2e27SBarry Smith 	  CHKMEMQ;
7890def2e27SBarry Smith       nz            = ai[row+1] - ai[row] - a->inode.size[i];
7900def2e27SBarry Smith       cnt2          = 1;
7910def2e27SBarry Smith       for (j=1; j<nz; j++) {
7920def2e27SBarry Smith         if (cols[j] != cols[j-1]+1) {
7930def2e27SBarry Smith 	  CHKMEMQ;
7940def2e27SBarry Smith           counts[2*(cnt++)+1] = cnt2;
7950def2e27SBarry Smith           counts[2*cnt]       = cols[j];
7960def2e27SBarry Smith 	  CHKMEMQ;
7970def2e27SBarry Smith           cnt2                = 1;
7980def2e27SBarry Smith         } else cnt2++;
7990def2e27SBarry Smith       }
8000def2e27SBarry Smith 	  CHKMEMQ;
8010def2e27SBarry Smith       counts[2*(cnt++)+1] = cnt2;
8020def2e27SBarry Smith 	  CHKMEMQ;
8030def2e27SBarry Smith       row += a->inode.size[i];
8040def2e27SBarry Smith     }
8050def2e27SBarry Smith     ierr = PetscIntView(2*cnt,counts,0);
8060def2e27SBarry Smith   }
80738702af4SBarry Smith   PetscFunctionReturn(0);
80838702af4SBarry Smith }
80938702af4SBarry Smith 
81038702af4SBarry Smith #undef __FUNCT__
8114a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqSBAIJ"
812dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqSBAIJ(Mat A,MatAssemblyType mode)
81349b5e25fSSatish Balay {
81449b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
8156849ba73SBarry Smith   PetscErrorCode ierr;
81613f74950SBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
817d0f46423SBarry Smith   PetscInt       m = A->rmap->N,*ip,N,*ailen = a->ilen;
81813f74950SBarry Smith   PetscInt       mbs = a->mbs,bs2 = a->bs2,rmax = 0;
81949b5e25fSSatish Balay   MatScalar      *aa = a->a,*ap;
82049b5e25fSSatish Balay 
82149b5e25fSSatish Balay   PetscFunctionBegin;
82249b5e25fSSatish Balay   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
82349b5e25fSSatish Balay 
82449b5e25fSSatish Balay   if (m) rmax = ailen[0];
82549b5e25fSSatish Balay   for (i=1; i<mbs; i++) {
82649b5e25fSSatish Balay     /* move each row back by the amount of empty slots (fshift) before it*/
82749b5e25fSSatish Balay     fshift += imax[i-1] - ailen[i-1];
82849b5e25fSSatish Balay      rmax   = PetscMax(rmax,ailen[i]);
82949b5e25fSSatish Balay      if (fshift) {
83049b5e25fSSatish Balay        ip = aj + ai[i]; ap = aa + bs2*ai[i];
83149b5e25fSSatish Balay        N = ailen[i];
83249b5e25fSSatish Balay        for (j=0; j<N; j++) {
83349b5e25fSSatish Balay          ip[j-fshift] = ip[j];
83449b5e25fSSatish Balay          ierr = PetscMemcpy(ap+(j-fshift)*bs2,ap+j*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
83549b5e25fSSatish Balay        }
83649b5e25fSSatish Balay      }
83749b5e25fSSatish Balay      ai[i] = ai[i-1] + ailen[i-1];
83849b5e25fSSatish Balay   }
83949b5e25fSSatish Balay   if (mbs) {
84049b5e25fSSatish Balay     fshift += imax[mbs-1] - ailen[mbs-1];
84149b5e25fSSatish Balay      ai[mbs] = ai[mbs-1] + ailen[mbs-1];
84249b5e25fSSatish Balay   }
84349b5e25fSSatish Balay   /* reset ilen and imax for each row */
84449b5e25fSSatish Balay   for (i=0; i<mbs; i++) {
84549b5e25fSSatish Balay     ailen[i] = imax[i] = ai[i+1] - ai[i];
84649b5e25fSSatish Balay   }
8476c6c5352SBarry Smith   a->nz = ai[mbs];
84849b5e25fSSatish Balay 
849b424e231SHong Zhang   /* diagonals may have moved, reset it */
850b424e231SHong Zhang   if (a->diag) {
8512ed38d0bSJed Brown     ierr = PetscMemcpy(a->diag,ai,mbs*sizeof(PetscInt));CHKERRQ(ierr);
85249b5e25fSSatish Balay   }
85328b2fa4aSMatthew Knepley   if (fshift && a->nounused == -1) {
854e32f2f54SBarry 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);
85528b2fa4aSMatthew Knepley   }
856d0f46423SBarry 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);
857ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues is %D\n",a->reallocs);CHKERRQ(ierr);
858ae15b995SBarry Smith   ierr = PetscInfo1(A,"Most nonzeros blocks in any row is %D\n",rmax);CHKERRQ(ierr);
859*8e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
86049b5e25fSSatish Balay   a->reallocs          = 0;
86149b5e25fSSatish Balay   A->info.nz_unneeded  = (PetscReal)fshift*bs2;
862061b2667SBarry Smith   a->idiagvalid = PETSC_FALSE;
86338702af4SBarry Smith 
86438702af4SBarry Smith   if (A->cmap->n < 65536 && A->cmap->bs == 1) {
86538702af4SBarry Smith     if (!a->jshort) {
86638702af4SBarry Smith       ierr = PetscMalloc(a->i[A->rmap->n]*sizeof(unsigned short),&a->jshort);CHKERRQ(ierr);
867c760cd28SBarry Smith       ierr = PetscLogObjectMemory(A,a->i[A->rmap->n]*sizeof(unsigned short));CHKERRQ(ierr);
86838702af4SBarry Smith       for (i=0; i<a->i[A->rmap->n]; i++) a->jshort[i] = a->j[i];
86938702af4SBarry Smith       A->ops->mult  = MatMult_SeqSBAIJ_1_ushort;
87041f059aeSBarry Smith       A->ops->sor = MatSOR_SeqSBAIJ_ushort;
8714da8f245SBarry Smith       a->free_jshort = PETSC_TRUE;
87238702af4SBarry Smith     }
87338702af4SBarry Smith   }
87449b5e25fSSatish Balay   PetscFunctionReturn(0);
87549b5e25fSSatish Balay }
87649b5e25fSSatish Balay 
87749b5e25fSSatish Balay /*
87849b5e25fSSatish Balay    This function returns an array of flags which indicate the locations of contiguous
87949b5e25fSSatish Balay    blocks that should be zeroed. for eg: if bs = 3  and is = [0,1,2,3,5,6,7,8,9]
88049b5e25fSSatish Balay    then the resulting sizes = [3,1,1,3,1] correspondig to sets [(0,1,2),(3),(5),(6,7,8),(9)]
88149b5e25fSSatish Balay    Assume: sizes should be long enough to hold all the values.
88249b5e25fSSatish Balay */
8834a2ae208SSatish Balay #undef __FUNCT__
8844a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqSBAIJ_Check_Blocks"
88513f74950SBarry Smith PetscErrorCode MatZeroRows_SeqSBAIJ_Check_Blocks(PetscInt idx[],PetscInt n,PetscInt bs,PetscInt sizes[], PetscInt *bs_max)
88649b5e25fSSatish Balay {
88713f74950SBarry Smith   PetscInt   i,j,k,row;
88849b5e25fSSatish Balay   PetscTruth flg;
88949b5e25fSSatish Balay 
89049b5e25fSSatish Balay   PetscFunctionBegin;
89149b5e25fSSatish Balay    for (i=0,j=0; i<n; j++) {
89249b5e25fSSatish Balay      row = idx[i];
89349b5e25fSSatish Balay      if (row%bs!=0) { /* Not the begining of a block */
89449b5e25fSSatish Balay        sizes[j] = 1;
89549b5e25fSSatish Balay        i++;
89649b5e25fSSatish Balay      } else if (i+bs > n) { /* Beginning of a block, but complete block doesn't exist (at idx end) */
89749b5e25fSSatish Balay        sizes[j] = 1;         /* Also makes sure atleast 'bs' values exist for next else */
89849b5e25fSSatish Balay        i++;
89949b5e25fSSatish Balay      } else { /* Begining of the block, so check if the complete block exists */
90049b5e25fSSatish Balay        flg = PETSC_TRUE;
90149b5e25fSSatish Balay        for (k=1; k<bs; k++) {
90249b5e25fSSatish Balay          if (row+k != idx[i+k]) { /* break in the block */
90349b5e25fSSatish Balay            flg = PETSC_FALSE;
90449b5e25fSSatish Balay            break;
90549b5e25fSSatish Balay          }
90649b5e25fSSatish Balay        }
907abc0a331SBarry Smith        if (flg) { /* No break in the bs */
90849b5e25fSSatish Balay          sizes[j] = bs;
90949b5e25fSSatish Balay          i+= bs;
91049b5e25fSSatish Balay        } else {
91149b5e25fSSatish Balay          sizes[j] = 1;
91249b5e25fSSatish Balay          i++;
91349b5e25fSSatish Balay        }
91449b5e25fSSatish Balay      }
91549b5e25fSSatish Balay    }
91649b5e25fSSatish Balay    *bs_max = j;
91749b5e25fSSatish Balay    PetscFunctionReturn(0);
91849b5e25fSSatish Balay }
91949b5e25fSSatish Balay 
92049b5e25fSSatish Balay 
92149b5e25fSSatish Balay /* Only add/insert a(i,j) with i<=j (blocks).
92249b5e25fSSatish Balay    Any a(i,j) with i>j input by user is ingored.
92349b5e25fSSatish Balay */
92449b5e25fSSatish Balay 
9254a2ae208SSatish Balay #undef __FUNCT__
9264a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqSBAIJ"
92713f74950SBarry Smith PetscErrorCode MatSetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
92849b5e25fSSatish Balay {
92949b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
9306849ba73SBarry Smith   PetscErrorCode ierr;
931e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,lastcol = -1;
93213f74950SBarry Smith   PetscInt       *imax=a->imax,*ai=a->i,*ailen=a->ilen,roworiented=a->roworiented;
933d0f46423SBarry Smith   PetscInt       *aj=a->j,nonew=a->nonew,bs=A->rmap->bs,brow,bcol;
93413f74950SBarry Smith   PetscInt       ridx,cidx,bs2=a->bs2;
93549b5e25fSSatish Balay   MatScalar      *ap,value,*aa=a->a,*bap;
93649b5e25fSSatish Balay 
93749b5e25fSSatish Balay   PetscFunctionBegin;
93871fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
93949b5e25fSSatish Balay   for (k=0; k<m; k++) { /* loop over added rows */
94049b5e25fSSatish Balay     row  = im[k];       /* row number */
94149b5e25fSSatish Balay     brow = row/bs;      /* block row number */
94249b5e25fSSatish Balay     if (row < 0) continue;
9432515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
944e32f2f54SBarry 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);
94549b5e25fSSatish Balay #endif
94649b5e25fSSatish Balay     rp   = aj + ai[brow]; /*ptr to beginning of column value of the row block*/
94749b5e25fSSatish Balay     ap   = aa + bs2*ai[brow]; /*ptr to beginning of element value of the row block*/
94849b5e25fSSatish Balay     rmax = imax[brow];  /* maximum space allocated for this row */
94949b5e25fSSatish Balay     nrow = ailen[brow]; /* actual length of this row */
95049b5e25fSSatish Balay     low  = 0;
95149b5e25fSSatish Balay 
95249b5e25fSSatish Balay     for (l=0; l<n; l++) { /* loop over added columns */
95349b5e25fSSatish Balay       if (in[l] < 0) continue;
9542515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
955e32f2f54SBarry 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);
95649b5e25fSSatish Balay #endif
95749b5e25fSSatish Balay       col = in[l];
95849b5e25fSSatish Balay       bcol = col/bs;              /* block col number */
95949b5e25fSSatish Balay 
960941593c8SHong Zhang       if (brow > bcol) {
961941593c8SHong Zhang         if (a->ignore_ltriangular){
962941593c8SHong Zhang           continue; /* ignore lower triangular values */
963941593c8SHong Zhang         } else {
964e32f2f54SBarry 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)");
965941593c8SHong Zhang         }
966941593c8SHong Zhang       }
967f4989cb3SHong Zhang 
96849b5e25fSSatish Balay       ridx = row % bs; cidx = col % bs; /*row and col index inside the block */
9698549e402SHong Zhang       if ((brow==bcol && ridx<=cidx) || (brow<bcol)){
97049b5e25fSSatish Balay         /* element value a(k,l) */
97149b5e25fSSatish Balay         if (roworiented) {
97249b5e25fSSatish Balay           value = v[l + k*n];
97349b5e25fSSatish Balay         } else {
97449b5e25fSSatish Balay           value = v[k + l*m];
97549b5e25fSSatish Balay         }
97649b5e25fSSatish Balay 
97749b5e25fSSatish Balay         /* move pointer bap to a(k,l) quickly and add/insert value */
9787cd84e04SBarry Smith         if (col <= lastcol) low = 0; high = nrow;
979e2ee6c50SBarry Smith         lastcol = col;
98049b5e25fSSatish Balay         while (high-low > 7) {
98149b5e25fSSatish Balay           t = (low+high)/2;
98249b5e25fSSatish Balay           if (rp[t] > bcol) high = t;
98349b5e25fSSatish Balay           else              low  = t;
98449b5e25fSSatish Balay         }
98549b5e25fSSatish Balay         for (i=low; i<high; i++) {
98649b5e25fSSatish Balay           if (rp[i] > bcol) break;
98749b5e25fSSatish Balay           if (rp[i] == bcol) {
98849b5e25fSSatish Balay             bap  = ap +  bs2*i + bs*cidx + ridx;
98949b5e25fSSatish Balay             if (is == ADD_VALUES) *bap += value;
99049b5e25fSSatish Balay             else                  *bap  = value;
9918549e402SHong Zhang             /* for diag block, add/insert its symmetric element a(cidx,ridx) */
9928549e402SHong Zhang             if (brow == bcol && ridx < cidx){
9938549e402SHong Zhang               bap  = ap +  bs2*i + bs*ridx + cidx;
9948549e402SHong Zhang               if (is == ADD_VALUES) *bap += value;
9958549e402SHong Zhang               else                  *bap  = value;
9968549e402SHong Zhang             }
99749b5e25fSSatish Balay             goto noinsert1;
99849b5e25fSSatish Balay           }
99949b5e25fSSatish Balay         }
100049b5e25fSSatish Balay 
100149b5e25fSSatish Balay         if (nonew == 1) goto noinsert1;
1002e32f2f54SBarry Smith         if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
1003421e10b8SBarry Smith         MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
100449b5e25fSSatish Balay 
1005c03d1d03SSatish Balay         N = nrow++ - 1; high++;
100649b5e25fSSatish Balay         /* shift up all the later entries in this row */
100749b5e25fSSatish Balay         for (ii=N; ii>=i; ii--) {
100849b5e25fSSatish Balay           rp[ii+1] = rp[ii];
100949b5e25fSSatish Balay           ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
101049b5e25fSSatish Balay         }
101149b5e25fSSatish Balay         if (N>=i) {
101249b5e25fSSatish Balay           ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
101349b5e25fSSatish Balay         }
101449b5e25fSSatish Balay         rp[i]                      = bcol;
101549b5e25fSSatish Balay         ap[bs2*i + bs*cidx + ridx] = value;
101649b5e25fSSatish Balay       noinsert1:;
101749b5e25fSSatish Balay         low = i;
10188549e402SHong Zhang       }
101949b5e25fSSatish Balay     }   /* end of loop over added columns */
102049b5e25fSSatish Balay     ailen[brow] = nrow;
102149b5e25fSSatish Balay   }   /* end of loop over added rows */
102249b5e25fSSatish Balay   PetscFunctionReturn(0);
102349b5e25fSSatish Balay }
102449b5e25fSSatish Balay 
10254a2ae208SSatish Balay #undef __FUNCT__
10264d101231SSatish Balay #define __FUNCT__ "MatICCFactor_SeqSBAIJ"
10270481f469SBarry Smith PetscErrorCode MatICCFactor_SeqSBAIJ(Mat inA,IS row,const MatFactorInfo *info)
102849b5e25fSSatish Balay {
10294ccecd49SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)inA->data;
103049b5e25fSSatish Balay   Mat            outA;
1031dfbe8321SBarry Smith   PetscErrorCode ierr;
1032c84f5b01SHong Zhang   PetscTruth     row_identity;
103349b5e25fSSatish Balay 
103449b5e25fSSatish Balay   PetscFunctionBegin;
1035e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 is supported for in-place icc");
1036c84f5b01SHong Zhang   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1037e32f2f54SBarry Smith   if (!row_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix reordering is not supported");
1038e32f2f54SBarry 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()! */
1039c84f5b01SHong Zhang 
104049b5e25fSSatish Balay   outA            = inA;
1041d5f3da31SBarry Smith   inA->factortype = MAT_FACTOR_ICC;
104249b5e25fSSatish Balay 
10431a3463dfSHong Zhang   ierr = MatMarkDiagonal_SeqSBAIJ(inA);CHKERRQ(ierr);
1044d595f711SHong Zhang   ierr = MatSeqSBAIJSetNumericFactorization_inplace(inA,row_identity);CHKERRQ(ierr);
104549b5e25fSSatish Balay 
1046c3122656SLisandro Dalcin   ierr   = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1047c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr); }
1048c84f5b01SHong Zhang   a->row = row;
1049c3122656SLisandro Dalcin   ierr   = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1050c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr); }
1051c84f5b01SHong Zhang   a->col = row;
1052c84f5b01SHong Zhang 
1053c84f5b01SHong Zhang   /* Create the invert permutation so that it can be used in MatCholeskyFactorNumeric() */
1054c84f5b01SHong Zhang   if (a->icol) {ierr = ISInvertPermutation(row,PETSC_DECIDE, &a->icol);CHKERRQ(ierr);}
105552e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
105649b5e25fSSatish Balay 
105749b5e25fSSatish Balay   if (!a->solve_work) {
1058d0f46423SBarry Smith     ierr = PetscMalloc((inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
1059d0f46423SBarry Smith     ierr = PetscLogObjectMemory(inA,(inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar));CHKERRQ(ierr);
106049b5e25fSSatish Balay   }
106149b5e25fSSatish Balay 
1062719d5645SBarry Smith   ierr = MatCholeskyFactorNumeric(outA,inA,info);CHKERRQ(ierr);
106349b5e25fSSatish Balay   PetscFunctionReturn(0);
106449b5e25fSSatish Balay }
1065950f1e5bSHong Zhang 
106649b5e25fSSatish Balay EXTERN_C_BEGIN
10674a2ae208SSatish Balay #undef __FUNCT__
10684a2ae208SSatish Balay #define __FUNCT__ "MatSeqSBAIJSetColumnIndices_SeqSBAIJ"
1069be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqSBAIJSetColumnIndices_SeqSBAIJ(Mat mat,PetscInt *indices)
107049b5e25fSSatish Balay {
1071045c9aa0SHong Zhang   Mat_SeqSBAIJ *baij = (Mat_SeqSBAIJ *)mat->data;
107213f74950SBarry Smith   PetscInt     i,nz,n;
107349b5e25fSSatish Balay 
107449b5e25fSSatish Balay   PetscFunctionBegin;
10756c6c5352SBarry Smith   nz = baij->maxnz;
1076d0f46423SBarry Smith   n  = mat->cmap->n;
107749b5e25fSSatish Balay   for (i=0; i<nz; i++) {
107849b5e25fSSatish Balay     baij->j[i] = indices[i];
107949b5e25fSSatish Balay   }
10806c6c5352SBarry Smith    baij->nz = nz;
108149b5e25fSSatish Balay    for (i=0; i<n; i++) {
108249b5e25fSSatish Balay      baij->ilen[i] = baij->imax[i];
108349b5e25fSSatish Balay    }
108449b5e25fSSatish Balay    PetscFunctionReturn(0);
108549b5e25fSSatish Balay }
108649b5e25fSSatish Balay EXTERN_C_END
108749b5e25fSSatish Balay 
10884a2ae208SSatish Balay #undef __FUNCT__
10894a2ae208SSatish Balay #define __FUNCT__ "MatSeqSBAIJSetColumnIndices"
109049b5e25fSSatish Balay /*@
109119585528SSatish Balay   MatSeqSBAIJSetColumnIndices - Set the column indices for all the rows
109249b5e25fSSatish Balay   in the matrix.
109349b5e25fSSatish Balay 
109449b5e25fSSatish Balay   Input Parameters:
109519585528SSatish Balay   +  mat     - the SeqSBAIJ matrix
109649b5e25fSSatish Balay   -  indices - the column indices
109749b5e25fSSatish Balay 
109849b5e25fSSatish Balay   Level: advanced
109949b5e25fSSatish Balay 
110049b5e25fSSatish Balay   Notes:
110149b5e25fSSatish Balay   This can be called if you have precomputed the nonzero structure of the
110249b5e25fSSatish Balay   matrix and want to provide it to the matrix object to improve the performance
110349b5e25fSSatish Balay   of the MatSetValues() operation.
110449b5e25fSSatish Balay 
110549b5e25fSSatish Balay   You MUST have set the correct numbers of nonzeros per row in the call to
1106d1be2dadSMatthew Knepley   MatCreateSeqSBAIJ(), and the columns indices MUST be sorted.
110749b5e25fSSatish Balay 
1108ab9f2c04SSatish Balay   MUST be called before any calls to MatSetValues()
110949b5e25fSSatish Balay 
1110ab9f2c04SSatish Balay   .seealso: MatCreateSeqSBAIJ
111149b5e25fSSatish Balay @*/
1112be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqSBAIJSetColumnIndices(Mat mat,PetscInt *indices)
111349b5e25fSSatish Balay {
111413f74950SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
111549b5e25fSSatish Balay 
111649b5e25fSSatish Balay   PetscFunctionBegin;
11170700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
11184482741eSBarry Smith   PetscValidPointer(indices,2);
1119c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqSBAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
112049b5e25fSSatish Balay   if (f) {
112149b5e25fSSatish Balay     ierr = (*f)(mat,indices);CHKERRQ(ierr);
112249b5e25fSSatish Balay   } else {
1123e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
112449b5e25fSSatish Balay   }
112549b5e25fSSatish Balay   PetscFunctionReturn(0);
112649b5e25fSSatish Balay }
112749b5e25fSSatish Balay 
11284a2ae208SSatish Balay #undef __FUNCT__
11293c896bc6SHong Zhang #define __FUNCT__ "MatCopy_SeqSBAIJ"
11303c896bc6SHong Zhang PetscErrorCode MatCopy_SeqSBAIJ(Mat A,Mat B,MatStructure str)
11313c896bc6SHong Zhang {
11323c896bc6SHong Zhang   PetscErrorCode ierr;
11333c896bc6SHong Zhang 
11343c896bc6SHong Zhang   PetscFunctionBegin;
11353c896bc6SHong Zhang   /* If the two matrices have the same copy implementation, use fast copy. */
11363c896bc6SHong Zhang   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
11373c896bc6SHong Zhang     Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
11383c896bc6SHong Zhang     Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ*)B->data;
11393c896bc6SHong Zhang 
1140e7e72b3dSBarry 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");
1141d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->N])*sizeof(PetscScalar));CHKERRQ(ierr);
11423c896bc6SHong Zhang   } else {
1143f5edf698SHong Zhang     ierr = MatGetRowUpperTriangular(A);CHKERRQ(ierr);
11443c896bc6SHong Zhang     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1145f5edf698SHong Zhang     ierr = MatRestoreRowUpperTriangular(A);CHKERRQ(ierr);
11463c896bc6SHong Zhang   }
11473c896bc6SHong Zhang   PetscFunctionReturn(0);
11483c896bc6SHong Zhang }
11493c896bc6SHong Zhang 
11503c896bc6SHong Zhang #undef __FUNCT__
11514a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqSBAIJ"
1152dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqSBAIJ(Mat A)
1153273d9f13SBarry Smith {
1154dfbe8321SBarry Smith   PetscErrorCode ierr;
1155273d9f13SBarry Smith 
1156273d9f13SBarry Smith   PetscFunctionBegin;
1157db4efbfdSBarry Smith   ierr =  MatSeqSBAIJSetPreallocation_SeqSBAIJ(A,-PetscMax(A->rmap->bs,1),PETSC_DEFAULT,0);CHKERRQ(ierr);
1158273d9f13SBarry Smith   PetscFunctionReturn(0);
1159273d9f13SBarry Smith }
1160273d9f13SBarry Smith 
1161a6ece127SHong Zhang #undef __FUNCT__
1162a6ece127SHong Zhang #define __FUNCT__ "MatGetArray_SeqSBAIJ"
1163dfbe8321SBarry Smith PetscErrorCode MatGetArray_SeqSBAIJ(Mat A,PetscScalar *array[])
1164a6ece127SHong Zhang {
1165a6ece127SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
1166a6ece127SHong Zhang   PetscFunctionBegin;
1167a6ece127SHong Zhang   *array = a->a;
1168a6ece127SHong Zhang   PetscFunctionReturn(0);
1169a6ece127SHong Zhang }
1170a6ece127SHong Zhang 
1171a6ece127SHong Zhang #undef __FUNCT__
1172a6ece127SHong Zhang #define __FUNCT__ "MatRestoreArray_SeqSBAIJ"
1173dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqSBAIJ(Mat A,PetscScalar *array[])
1174a6ece127SHong Zhang {
1175a6ece127SHong Zhang   PetscFunctionBegin;
1176a6ece127SHong Zhang   PetscFunctionReturn(0);
1177a6ece127SHong Zhang  }
1178a6ece127SHong Zhang 
117942ee4b1aSHong Zhang #undef __FUNCT__
118042ee4b1aSHong Zhang #define __FUNCT__ "MatAXPY_SeqSBAIJ"
1181f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqSBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
118242ee4b1aSHong Zhang {
118342ee4b1aSHong Zhang   Mat_SeqSBAIJ   *x=(Mat_SeqSBAIJ *)X->data, *y=(Mat_SeqSBAIJ *)Y->data;
1184dfbe8321SBarry Smith   PetscErrorCode ierr;
1185d0f46423SBarry Smith   PetscInt       i,bs=Y->rmap->bs,bs2,j;
11860805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(x->nz);
118742ee4b1aSHong Zhang 
118842ee4b1aSHong Zhang   PetscFunctionBegin;
118942ee4b1aSHong Zhang   if (str == SAME_NONZERO_PATTERN) {
1190f4df32b1SMatthew Knepley     PetscScalar alpha = a;
1191f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
1192c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
1193c4319e64SHong Zhang     if (y->xtoy && y->XtoY != X) {
1194c4319e64SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
1195c4319e64SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
1196c537a176SHong Zhang     }
1197c4319e64SHong Zhang     if (!y->xtoy) { /* get xtoy */
1198c4319e64SHong Zhang       ierr = MatAXPYGetxtoy_Private(x->mbs,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
1199c4319e64SHong Zhang       y->XtoY = X;
1200c537a176SHong Zhang     }
1201c4319e64SHong Zhang     bs2 = bs*bs;
12026c6c5352SBarry Smith     for (i=0; i<x->nz; i++) {
1203c4319e64SHong Zhang       j = 0;
1204c4319e64SHong Zhang       while (j < bs2){
1205f4df32b1SMatthew Knepley         y->a[bs2*y->xtoy[i]+j] += a*(x->a[bs2*i+j]);
1206c4319e64SHong Zhang         j++;
1207c537a176SHong Zhang       }
1208c4319e64SHong Zhang     }
12091e2582c4SBarry 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);
121042ee4b1aSHong Zhang   } else {
1211f5edf698SHong Zhang     ierr = MatGetRowUpperTriangular(X);CHKERRQ(ierr);
1212f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
1213f5edf698SHong Zhang     ierr = MatRestoreRowUpperTriangular(X);CHKERRQ(ierr);
121442ee4b1aSHong Zhang   }
121542ee4b1aSHong Zhang   PetscFunctionReturn(0);
121642ee4b1aSHong Zhang }
121742ee4b1aSHong Zhang 
1218efcf0fc3SBarry Smith #undef __FUNCT__
12196363de48SJed Brown #define __FUNCT__ "MatSetBlockSize_SeqSBAIJ"
12206363de48SJed Brown PetscErrorCode MatSetBlockSize_SeqSBAIJ(Mat A,PetscInt bs)
12216363de48SJed Brown {
12226363de48SJed Brown   PetscInt rbs,cbs;
12236363de48SJed Brown   PetscErrorCode ierr;
12246363de48SJed Brown 
12256363de48SJed Brown   PetscFunctionBegin;
12266363de48SJed Brown   ierr = PetscLayoutGetBlockSize(A->rmap,&rbs);CHKERRQ(ierr);
12276363de48SJed Brown   ierr = PetscLayoutGetBlockSize(A->cmap,&cbs);CHKERRQ(ierr);
1228e32f2f54SBarry Smith   if (rbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with SBAIJ %d",bs,rbs);
1229e32f2f54SBarry Smith   if (cbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with SBAIJ %d",bs,cbs);
12306363de48SJed Brown   PetscFunctionReturn(0);
12316363de48SJed Brown }
12326363de48SJed Brown 
12336363de48SJed Brown #undef __FUNCT__
1234efcf0fc3SBarry Smith #define __FUNCT__ "MatIsSymmetric_SeqSBAIJ"
1235dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqSBAIJ(Mat A,PetscReal tol,PetscTruth *flg)
1236efcf0fc3SBarry Smith {
1237efcf0fc3SBarry Smith   PetscFunctionBegin;
1238efcf0fc3SBarry Smith   *flg = PETSC_TRUE;
1239efcf0fc3SBarry Smith   PetscFunctionReturn(0);
1240efcf0fc3SBarry Smith }
1241efcf0fc3SBarry Smith 
1242efcf0fc3SBarry Smith #undef __FUNCT__
1243efcf0fc3SBarry Smith #define __FUNCT__ "MatIsStructurallySymmetric_SeqSBAIJ"
1244dfbe8321SBarry Smith PetscErrorCode MatIsStructurallySymmetric_SeqSBAIJ(Mat A,PetscTruth *flg)
1245efcf0fc3SBarry Smith {
1246efcf0fc3SBarry Smith    PetscFunctionBegin;
1247efcf0fc3SBarry Smith    *flg = PETSC_TRUE;
1248efcf0fc3SBarry Smith    PetscFunctionReturn(0);
1249efcf0fc3SBarry Smith }
1250efcf0fc3SBarry Smith 
1251efcf0fc3SBarry Smith #undef __FUNCT__
1252efcf0fc3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqSBAIJ"
1253ab5e4463SMatthew Knepley PetscErrorCode MatIsHermitian_SeqSBAIJ(Mat A,PetscReal tol,PetscTruth *flg)
1254efcf0fc3SBarry Smith  {
1255efcf0fc3SBarry Smith    PetscFunctionBegin;
1256efcf0fc3SBarry Smith    *flg = PETSC_FALSE;
1257efcf0fc3SBarry Smith    PetscFunctionReturn(0);
1258efcf0fc3SBarry Smith  }
1259efcf0fc3SBarry Smith 
126099cafbc1SBarry Smith #undef __FUNCT__
126199cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqSBAIJ"
126299cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqSBAIJ(Mat A)
126399cafbc1SBarry Smith {
126499cafbc1SBarry Smith   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
126599cafbc1SBarry Smith   PetscInt       i,nz = a->bs2*a->i[a->mbs];
1266dd6ea824SBarry Smith   MatScalar      *aa = a->a;
126799cafbc1SBarry Smith 
126899cafbc1SBarry Smith   PetscFunctionBegin;
126999cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
127099cafbc1SBarry Smith   PetscFunctionReturn(0);
127199cafbc1SBarry Smith }
127299cafbc1SBarry Smith 
127399cafbc1SBarry Smith #undef __FUNCT__
127499cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqSBAIJ"
127599cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqSBAIJ(Mat A)
127699cafbc1SBarry Smith {
127799cafbc1SBarry Smith   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
127899cafbc1SBarry Smith   PetscInt       i,nz = a->bs2*a->i[a->mbs];
1279dd6ea824SBarry Smith   MatScalar      *aa = a->a;
128099cafbc1SBarry Smith 
128199cafbc1SBarry Smith   PetscFunctionBegin;
128299cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
128399cafbc1SBarry Smith   PetscFunctionReturn(0);
128499cafbc1SBarry Smith }
128599cafbc1SBarry Smith 
128649b5e25fSSatish Balay /* -------------------------------------------------------------------*/
128749b5e25fSSatish Balay static struct _MatOps MatOps_Values = {MatSetValues_SeqSBAIJ,
128849b5e25fSSatish Balay        MatGetRow_SeqSBAIJ,
128949b5e25fSSatish Balay        MatRestoreRow_SeqSBAIJ,
129049b5e25fSSatish Balay        MatMult_SeqSBAIJ_N,
129197304618SKris Buschelman /* 4*/ MatMultAdd_SeqSBAIJ_N,
1292431c96f7SBarry Smith        MatMult_SeqSBAIJ_N,       /* transpose versions are same as non-transpose versions */
1293e005ede5SBarry Smith        MatMultAdd_SeqSBAIJ_N,
1294db4efbfdSBarry Smith        0,
129549b5e25fSSatish Balay        0,
129649b5e25fSSatish Balay        0,
129797304618SKris Buschelman /*10*/ 0,
129849b5e25fSSatish Balay        0,
1299c078aec8SLisandro Dalcin        MatCholeskyFactor_SeqSBAIJ,
130041f059aeSBarry Smith        MatSOR_SeqSBAIJ,
130149b5e25fSSatish Balay        MatTranspose_SeqSBAIJ,
130297304618SKris Buschelman /*15*/ MatGetInfo_SeqSBAIJ,
130349b5e25fSSatish Balay        MatEqual_SeqSBAIJ,
130449b5e25fSSatish Balay        MatGetDiagonal_SeqSBAIJ,
130549b5e25fSSatish Balay        MatDiagonalScale_SeqSBAIJ,
130649b5e25fSSatish Balay        MatNorm_SeqSBAIJ,
130797304618SKris Buschelman /*20*/ 0,
130849b5e25fSSatish Balay        MatAssemblyEnd_SeqSBAIJ,
130949b5e25fSSatish Balay        MatSetOption_SeqSBAIJ,
131049b5e25fSSatish Balay        MatZeroEntries_SeqSBAIJ,
1311d519adbfSMatthew Knepley /*24*/ 0,
131249b5e25fSSatish Balay        0,
131349b5e25fSSatish Balay        0,
1314db4efbfdSBarry Smith        0,
1315db4efbfdSBarry Smith        0,
1316d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqSBAIJ,
1317c464158bSHong Zhang        0,
1318db4efbfdSBarry Smith        0,
1319a6ece127SHong Zhang        MatGetArray_SeqSBAIJ,
1320a6ece127SHong Zhang        MatRestoreArray_SeqSBAIJ,
1321d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqSBAIJ,
1322719d5645SBarry Smith        0,
1323719d5645SBarry Smith        0,
132449b5e25fSSatish Balay        0,
1325c84f5b01SHong Zhang        MatICCFactor_SeqSBAIJ,
1326d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqSBAIJ,
132749b5e25fSSatish Balay        MatGetSubMatrices_SeqSBAIJ,
132849b5e25fSSatish Balay        MatIncreaseOverlap_SeqSBAIJ,
132949b5e25fSSatish Balay        MatGetValues_SeqSBAIJ,
13303c896bc6SHong Zhang        MatCopy_SeqSBAIJ,
1331d519adbfSMatthew Knepley /*44*/ 0,
133249b5e25fSSatish Balay        MatScale_SeqSBAIJ,
133349b5e25fSSatish Balay        0,
133449b5e25fSSatish Balay        0,
133549b5e25fSSatish Balay        0,
13366363de48SJed Brown /*49*/ MatSetBlockSize_SeqSBAIJ,
133749b5e25fSSatish Balay        MatGetRowIJ_SeqSBAIJ,
133849b5e25fSSatish Balay        MatRestoreRowIJ_SeqSBAIJ,
133949b5e25fSSatish Balay        0,
134049b5e25fSSatish Balay        0,
1341d519adbfSMatthew Knepley /*54*/ 0,
134249b5e25fSSatish Balay        0,
134349b5e25fSSatish Balay        0,
134449b5e25fSSatish Balay        0,
134549b5e25fSSatish Balay        MatSetValuesBlocked_SeqSBAIJ,
1346d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_SeqSBAIJ,
134749b5e25fSSatish Balay        0,
134849b5e25fSSatish Balay        0,
1349357abbc8SBarry Smith        0,
1350d959ec07SHong Zhang        0,
1351d519adbfSMatthew Knepley /*64*/ 0,
1352d959ec07SHong Zhang        0,
1353d959ec07SHong Zhang        0,
1354d959ec07SHong Zhang        0,
1355d959ec07SHong Zhang        0,
1356d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqSBAIJ,
13573e0d88b5SBarry Smith        0,
13583e0d88b5SBarry Smith        0,
13593e0d88b5SBarry Smith        0,
13603e0d88b5SBarry Smith        0,
1361d519adbfSMatthew Knepley /*74*/ 0,
13623e0d88b5SBarry Smith        0,
13633e0d88b5SBarry Smith        0,
13643e0d88b5SBarry Smith        0,
13653e0d88b5SBarry Smith        0,
1366d519adbfSMatthew Knepley /*79*/ 0,
13673e0d88b5SBarry Smith        0,
13683e0d88b5SBarry Smith        0,
136997304618SKris Buschelman        MatGetInertia_SeqSBAIJ,
1370865e5f61SKris Buschelman        MatLoad_SeqSBAIJ,
1371d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqSBAIJ,
1372865e5f61SKris Buschelman        MatIsHermitian_SeqSBAIJ,
1373efcf0fc3SBarry Smith        MatIsStructurallySymmetric_SeqSBAIJ,
1374865e5f61SKris Buschelman        0,
1375865e5f61SKris Buschelman        0,
1376d519adbfSMatthew Knepley /*89*/ 0,
1377865e5f61SKris Buschelman        0,
1378865e5f61SKris Buschelman        0,
1379865e5f61SKris Buschelman        0,
1380865e5f61SKris Buschelman        0,
1381d519adbfSMatthew Knepley /*94*/ 0,
1382865e5f61SKris Buschelman        0,
1383865e5f61SKris Buschelman        0,
138499cafbc1SBarry Smith        0,
138599cafbc1SBarry Smith        0,
1386d519adbfSMatthew Knepley /*99*/ 0,
138799cafbc1SBarry Smith        0,
138899cafbc1SBarry Smith        0,
138999cafbc1SBarry Smith        0,
139099cafbc1SBarry Smith        0,
1391d519adbfSMatthew Knepley /*104*/0,
139299cafbc1SBarry Smith        MatRealPart_SeqSBAIJ,
1393f5edf698SHong Zhang        MatImaginaryPart_SeqSBAIJ,
1394f5edf698SHong Zhang        MatGetRowUpperTriangular_SeqSBAIJ,
13952af78befSBarry Smith        MatRestoreRowUpperTriangular_SeqSBAIJ,
1396d519adbfSMatthew Knepley /*109*/0,
13972af78befSBarry Smith        0,
13982af78befSBarry Smith        0,
13992af78befSBarry Smith        0,
1400547795f9SHong Zhang        MatMissingDiagonal_SeqSBAIJ,
1401547795f9SHong Zhang /*114*/0,
1402547795f9SHong Zhang        0,
1403547795f9SHong Zhang        0,
1404547795f9SHong Zhang        0,
1405547795f9SHong Zhang        0,
1406547795f9SHong Zhang /*119*/0,
1407547795f9SHong Zhang        0,
1408547795f9SHong Zhang        0
140999cafbc1SBarry Smith };
1410be1d678aSKris Buschelman 
141149b5e25fSSatish Balay EXTERN_C_BEGIN
14124a2ae208SSatish Balay #undef __FUNCT__
14134a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqSBAIJ"
1414be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqSBAIJ(Mat mat)
141549b5e25fSSatish Balay {
14164afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ *)mat->data;
1417d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
1418dfbe8321SBarry Smith   PetscErrorCode ierr;
141949b5e25fSSatish Balay 
142049b5e25fSSatish Balay   PetscFunctionBegin;
1421e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
142249b5e25fSSatish Balay 
142349b5e25fSSatish Balay   /* allocate space for values if not already there */
142449b5e25fSSatish Balay   if (!aij->saved_values) {
142587828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
142649b5e25fSSatish Balay   }
142749b5e25fSSatish Balay 
142849b5e25fSSatish Balay   /* copy values over */
142987828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
143049b5e25fSSatish Balay   PetscFunctionReturn(0);
143149b5e25fSSatish Balay }
143249b5e25fSSatish Balay EXTERN_C_END
143349b5e25fSSatish Balay 
143449b5e25fSSatish Balay EXTERN_C_BEGIN
14354a2ae208SSatish Balay #undef __FUNCT__
14364a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqSBAIJ"
1437be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqSBAIJ(Mat mat)
143849b5e25fSSatish Balay {
14394afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ *)mat->data;
14406849ba73SBarry Smith   PetscErrorCode ierr;
1441d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
144249b5e25fSSatish Balay 
144349b5e25fSSatish Balay   PetscFunctionBegin;
1444e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
1445e7e72b3dSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
144649b5e25fSSatish Balay 
144749b5e25fSSatish Balay   /* copy values over */
144887828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
144949b5e25fSSatish Balay   PetscFunctionReturn(0);
145049b5e25fSSatish Balay }
145149b5e25fSSatish Balay EXTERN_C_END
145249b5e25fSSatish Balay 
14538549e402SHong Zhang EXTERN_C_BEGIN
14544a2ae208SSatish Balay #undef __FUNCT__
1455a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation_SeqSBAIJ"
1456be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqSBAIJSetPreallocation_SeqSBAIJ(Mat B,PetscInt bs,PetscInt nz,PetscInt *nnz)
145749b5e25fSSatish Balay {
1458c464158bSHong Zhang   Mat_SeqSBAIJ   *b = (Mat_SeqSBAIJ*)B->data;
14596849ba73SBarry Smith   PetscErrorCode ierr;
1460db4efbfdSBarry Smith   PetscInt       i,mbs,bs2, newbs = PetscAbs(bs);
146190d69ab7SBarry Smith   PetscTruth     skipallocation = PETSC_FALSE,flg = PETSC_FALSE;
146249b5e25fSSatish Balay 
146349b5e25fSSatish Balay   PetscFunctionBegin;
1464273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
1465db4efbfdSBarry Smith   if (bs < 0) {
1466db4efbfdSBarry Smith     ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Options for MPISBAIJ matrix","Mat");CHKERRQ(ierr);
1467db4efbfdSBarry Smith       ierr = PetscOptionsInt("-mat_block_size","Set the blocksize used to store the matrix","MatSeqSBAIJSetPreallocation",newbs,&newbs,PETSC_NULL);CHKERRQ(ierr);
1468db4efbfdSBarry Smith     ierr = PetscOptionsEnd();CHKERRQ(ierr);
1469db4efbfdSBarry Smith     bs   = PetscAbs(bs);
1470db4efbfdSBarry Smith   }
1471e7e72b3dSBarry Smith   if (nnz && newbs != bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot change blocksize from command line if setting nnz");
1472db4efbfdSBarry Smith   bs = newbs;
1473db4efbfdSBarry Smith 
147426283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
147526283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
147626283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
147726283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
1478899cda47SBarry Smith 
1479d0f46423SBarry Smith   mbs  = B->rmap->N/bs;
148049b5e25fSSatish Balay   bs2  = bs*bs;
148149b5e25fSSatish Balay 
1482e7e72b3dSBarry Smith   if (mbs*bs != B->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number rows, cols must be divisible by blocksize");
148349b5e25fSSatish Balay 
1484ab93d7beSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
1485ab93d7beSBarry Smith     skipallocation = PETSC_TRUE;
1486ab93d7beSBarry Smith     nz             = 0;
1487ab93d7beSBarry Smith   }
1488ab93d7beSBarry Smith 
1489435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 3;
1490e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
149149b5e25fSSatish Balay   if (nnz) {
149249b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
1493e32f2f54SBarry 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]);
1494e32f2f54SBarry 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);
149549b5e25fSSatish Balay     }
149649b5e25fSSatish Balay   }
149749b5e25fSSatish Balay 
1498db4efbfdSBarry Smith   B->ops->mult             = MatMult_SeqSBAIJ_N;
1499db4efbfdSBarry Smith   B->ops->multadd          = MatMultAdd_SeqSBAIJ_N;
1500db4efbfdSBarry Smith   B->ops->multtranspose    = MatMult_SeqSBAIJ_N;
1501db4efbfdSBarry Smith   B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_N;
150290d69ab7SBarry Smith   ierr  = PetscOptionsGetTruth(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,PETSC_NULL);CHKERRQ(ierr);
150349b5e25fSSatish Balay   if (!flg) {
150449b5e25fSSatish Balay     switch (bs) {
150549b5e25fSSatish Balay     case 1:
150649b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_1;
150749b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_1;
1508431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_1;
1509431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_1;
151049b5e25fSSatish Balay       break;
151149b5e25fSSatish Balay     case 2:
151249b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_2;
151349b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_2;
1514431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_2;
1515431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_2;
151649b5e25fSSatish Balay       break;
151749b5e25fSSatish Balay     case 3:
151849b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_3;
151949b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_3;
1520431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_3;
1521431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_3;
152249b5e25fSSatish Balay       break;
152349b5e25fSSatish Balay     case 4:
152449b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_4;
152549b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_4;
1526431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_4;
1527431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_4;
152849b5e25fSSatish Balay       break;
152949b5e25fSSatish Balay     case 5:
153049b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_5;
153149b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_5;
1532431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_5;
1533431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_5;
153449b5e25fSSatish Balay       break;
153549b5e25fSSatish Balay     case 6:
153649b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_6;
153749b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_6;
1538431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_6;
1539431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_6;
154049b5e25fSSatish Balay       break;
154149b5e25fSSatish Balay     case 7:
1542de53e5efSHong Zhang       B->ops->mult             = MatMult_SeqSBAIJ_7;
154349b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_7;
1544431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_7;
1545431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_7;
154649b5e25fSSatish Balay       break;
154749b5e25fSSatish Balay     }
154849b5e25fSSatish Balay   }
154949b5e25fSSatish Balay 
155049b5e25fSSatish Balay   b->mbs = mbs;
15514afc71dfSHong Zhang   b->nbs = mbs;
1552ab93d7beSBarry Smith   if (!skipallocation) {
15532ee49352SLisandro Dalcin     if (!b->imax) {
1554ab93d7beSBarry Smith       ierr = PetscMalloc2(mbs,PetscInt,&b->imax,mbs,PetscInt,&b->ilen);CHKERRQ(ierr);
1555c760cd28SBarry Smith       b->free_imax_ilen = PETSC_TRUE;
15562ee49352SLisandro Dalcin       ierr = PetscLogObjectMemory(B,2*mbs*sizeof(PetscInt));CHKERRQ(ierr);
15572ee49352SLisandro Dalcin     }
155849b5e25fSSatish Balay     if (!nnz) {
1559435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
156049b5e25fSSatish Balay       else if (nz <= 0)        nz = 1;
156149b5e25fSSatish Balay       for (i=0; i<mbs; i++) {
15628cef66ccSHong Zhang         b->imax[i] = nz;
156349b5e25fSSatish Balay       }
1564153ea458SHong Zhang       nz = nz*mbs; /* total nz */
156549b5e25fSSatish Balay     } else {
156649b5e25fSSatish Balay       nz = 0;
15678cef66ccSHong Zhang       for (i=0; i<mbs; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
156849b5e25fSSatish Balay     }
15692ee49352SLisandro Dalcin     /* b->ilen will count nonzeros in each block row so far. */
15702ee49352SLisandro Dalcin     for (i=0; i<mbs; i++) { b->ilen[i] = 0;}
15716c6c5352SBarry Smith     /* nz=(nz+mbs)/2; */ /* total diagonal and superdiagonal nonzero blocks */
157249b5e25fSSatish Balay 
157349b5e25fSSatish Balay     /* allocate the matrix space */
15742ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
1575d0f46423SBarry Smith     ierr = PetscMalloc3(bs2*nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->N+1,PetscInt,&b->i);CHKERRQ(ierr);
1576d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->N+1)*sizeof(PetscInt)+nz*(bs2*sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
15776c6c5352SBarry Smith     ierr = PetscMemzero(b->a,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
157813f74950SBarry Smith     ierr = PetscMemzero(b->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
157949b5e25fSSatish Balay     b->singlemalloc = PETSC_TRUE;
158049b5e25fSSatish Balay 
158149b5e25fSSatish Balay     /* pointer to beginning of each row */
1582e60cf9a0SBarry Smith     b->i[0] = 0;
158349b5e25fSSatish Balay     for (i=1; i<mbs+1; i++) {
158449b5e25fSSatish Balay       b->i[i] = b->i[i-1] + b->imax[i-1];
158549b5e25fSSatish Balay     }
1586e6b907acSBarry Smith     b->free_a     = PETSC_TRUE;
1587e6b907acSBarry Smith     b->free_ij    = PETSC_TRUE;
1588e811da20SHong Zhang   } else {
1589e6b907acSBarry Smith     b->free_a     = PETSC_FALSE;
1590e6b907acSBarry Smith     b->free_ij    = PETSC_FALSE;
1591ab93d7beSBarry Smith   }
159249b5e25fSSatish Balay 
1593d0f46423SBarry Smith   B->rmap->bs               = bs;
159449b5e25fSSatish Balay   b->bs2              = bs2;
15956c6c5352SBarry Smith   b->nz             = 0;
15966c6c5352SBarry Smith   b->maxnz          = nz*bs2;
1597153ea458SHong Zhang 
159816cdd363SHong Zhang   b->inew             = 0;
159916cdd363SHong Zhang   b->jnew             = 0;
160016cdd363SHong Zhang   b->anew             = 0;
160116cdd363SHong Zhang   b->a2anew           = 0;
16021a3463dfSHong Zhang   b->permute          = PETSC_FALSE;
1603c464158bSHong Zhang   PetscFunctionReturn(0);
1604c464158bSHong Zhang }
1605a23d5eceSKris Buschelman EXTERN_C_END
1606153ea458SHong Zhang 
1607db4efbfdSBarry Smith /*
1608db4efbfdSBarry Smith    This is used to set the numeric factorization for both Cholesky and ICC symbolic factorization
1609db4efbfdSBarry Smith */
16108b1456e3SHong Zhang #undef __FUNCT__
1611d595f711SHong Zhang #define __FUNCT__ "MatSeqSBAIJSetNumericFactorization_inplace"
1612d595f711SHong Zhang PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat B,PetscTruth natural)
1613db4efbfdSBarry Smith {
1614db4efbfdSBarry Smith   PetscErrorCode ierr;
161590d69ab7SBarry Smith   PetscTruth     flg = PETSC_FALSE;
1616db4efbfdSBarry Smith   PetscInt       bs = B->rmap->bs;
1617db4efbfdSBarry Smith 
1618db4efbfdSBarry Smith   PetscFunctionBegin;
161990d69ab7SBarry Smith   ierr    = PetscOptionsGetTruth(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,PETSC_NULL);CHKERRQ(ierr);
1620db4efbfdSBarry Smith   if (flg) bs = 8;
1621db4efbfdSBarry Smith 
1622db4efbfdSBarry Smith   if (!natural) {
1623db4efbfdSBarry Smith     switch (bs) {
1624db4efbfdSBarry Smith     case 1:
1625d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_inplace;
1626db4efbfdSBarry Smith       break;
1627db4efbfdSBarry Smith     case 2:
1628db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2;
1629db4efbfdSBarry Smith       break;
1630db4efbfdSBarry Smith     case 3:
1631db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3;
1632db4efbfdSBarry Smith       break;
1633db4efbfdSBarry Smith     case 4:
1634db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4;
1635db4efbfdSBarry Smith       break;
1636db4efbfdSBarry Smith     case 5:
1637db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5;
1638db4efbfdSBarry Smith       break;
1639db4efbfdSBarry Smith     case 6:
1640db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6;
1641db4efbfdSBarry Smith       break;
1642db4efbfdSBarry Smith     case 7:
1643db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7;
1644db4efbfdSBarry Smith       break;
1645db4efbfdSBarry Smith     default:
1646db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N;
1647db4efbfdSBarry Smith       break;
1648db4efbfdSBarry Smith     }
1649db4efbfdSBarry Smith   } else {
1650db4efbfdSBarry Smith     switch (bs) {
1651db4efbfdSBarry Smith     case 1:
1652d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering_inplace;
1653db4efbfdSBarry Smith       break;
1654db4efbfdSBarry Smith     case 2:
1655db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering;
1656db4efbfdSBarry Smith       break;
1657db4efbfdSBarry Smith     case 3:
1658db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3_NaturalOrdering;
1659db4efbfdSBarry Smith       break;
1660db4efbfdSBarry Smith     case 4:
1661db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4_NaturalOrdering;
1662db4efbfdSBarry Smith       break;
1663db4efbfdSBarry Smith     case 5:
1664db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5_NaturalOrdering;
1665db4efbfdSBarry Smith       break;
1666db4efbfdSBarry Smith     case 6:
1667db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6_NaturalOrdering;
1668db4efbfdSBarry Smith       break;
1669db4efbfdSBarry Smith     case 7:
1670db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7_NaturalOrdering;
1671db4efbfdSBarry Smith       break;
1672db4efbfdSBarry Smith     default:
1673db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering;
1674db4efbfdSBarry Smith       break;
1675db4efbfdSBarry Smith     }
1676db4efbfdSBarry Smith   }
1677db4efbfdSBarry Smith   PetscFunctionReturn(0);
1678db4efbfdSBarry Smith }
1679db4efbfdSBarry Smith 
1680d769727bSBarry Smith EXTERN_C_BEGIN
1681f69a0ea3SMatthew Knepley EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqSBAIJ_SeqAIJ(Mat, MatType,MatReuse,Mat*);
1682f69a0ea3SMatthew Knepley EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqSBAIJ_SeqBAIJ(Mat, MatType,MatReuse,Mat*);
1683d769727bSBarry Smith EXTERN_C_END
1684d769727bSBarry Smith 
1685e631078cSBarry Smith 
1686e631078cSBarry Smith EXTERN_C_BEGIN
16875c9eb25fSBarry Smith #undef __FUNCT__
16885c9eb25fSBarry Smith #define __FUNCT__ "MatGetFactor_seqsbaij_petsc"
16895c9eb25fSBarry Smith PetscErrorCode MatGetFactor_seqsbaij_petsc(Mat A,MatFactorType ftype,Mat *B)
16905c9eb25fSBarry Smith {
1691d0f46423SBarry Smith   PetscInt           n = A->rmap->n;
16925c9eb25fSBarry Smith   PetscErrorCode     ierr;
16935c9eb25fSBarry Smith 
16945c9eb25fSBarry Smith   PetscFunctionBegin;
16955c9eb25fSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
16965c9eb25fSBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
16975c9eb25fSBarry Smith   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
16985c9eb25fSBarry Smith     ierr = MatSetType(*B,MATSEQSBAIJ);CHKERRQ(ierr);
16995c9eb25fSBarry Smith     ierr = MatSeqSBAIJSetPreallocation(*B,1,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr);
17007b056e98SHong Zhang     (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqSBAIJ;
1701c6d0d4f0SHong Zhang     (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqSBAIJ;
1702e32f2f54SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported");
1703d5f3da31SBarry Smith   (*B)->factortype = ftype;
17045c9eb25fSBarry Smith   PetscFunctionReturn(0);
17055c9eb25fSBarry Smith }
1706e631078cSBarry Smith EXTERN_C_END
17075c9eb25fSBarry Smith 
17085c9eb25fSBarry Smith EXTERN_C_BEGIN
1709db4efbfdSBarry Smith #undef __FUNCT__
1710db4efbfdSBarry Smith #define __FUNCT__ "MatGetFactorAvailable_seqsbaij_petsc"
1711db4efbfdSBarry Smith PetscErrorCode MatGetFactorAvailable_seqsbaij_petsc(Mat A,MatFactorType ftype,PetscTruth *flg)
1712db4efbfdSBarry Smith {
1713db4efbfdSBarry Smith   PetscFunctionBegin;
1714db4efbfdSBarry Smith   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
1715db4efbfdSBarry Smith     *flg = PETSC_TRUE;
1716db4efbfdSBarry Smith   } else {
1717db4efbfdSBarry Smith     *flg = PETSC_FALSE;
1718db4efbfdSBarry Smith   }
1719db4efbfdSBarry Smith   PetscFunctionReturn(0);
1720db4efbfdSBarry Smith }
1721db4efbfdSBarry Smith EXTERN_C_END
1722db4efbfdSBarry Smith 
1723db4efbfdSBarry Smith EXTERN_C_BEGIN
1724611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
1725bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_sbaij_mumps(Mat,MatFactorType,Mat*);
1726611f576cSBarry Smith #endif
1727611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
17285c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_seqsbaij_spooles(Mat,MatFactorType,Mat*);
1729611f576cSBarry Smith #endif
1730b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
1731b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqsbaij_pastix(Mat,MatFactorType,Mat*);
1732b5e56a35SBarry Smith #endif
17335c9eb25fSBarry Smith EXTERN_C_END
17345c9eb25fSBarry Smith 
17350bad9183SKris Buschelman /*MC
1736fafad747SKris Buschelman   MATSEQSBAIJ - MATSEQSBAIJ = "seqsbaij" - A matrix type to be used for sequential symmetric block sparse matrices,
17370bad9183SKris Buschelman   based on block compressed sparse row format.  Only the upper triangular portion of the matrix is stored.
17380bad9183SKris Buschelman 
17390bad9183SKris Buschelman   Options Database Keys:
17400bad9183SKris Buschelman   . -mat_type seqsbaij - sets the matrix type to "seqsbaij" during a call to MatSetFromOptions()
17410bad9183SKris Buschelman 
17420bad9183SKris Buschelman   Level: beginner
17430bad9183SKris Buschelman 
17440bad9183SKris Buschelman   .seealso: MatCreateSeqSBAIJ
17450bad9183SKris Buschelman M*/
17460bad9183SKris Buschelman 
1747a23d5eceSKris Buschelman EXTERN_C_BEGIN
1748a23d5eceSKris Buschelman #undef __FUNCT__
1749a23d5eceSKris Buschelman #define __FUNCT__ "MatCreate_SeqSBAIJ"
1750be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqSBAIJ(Mat B)
1751a23d5eceSKris Buschelman {
1752a23d5eceSKris Buschelman   Mat_SeqSBAIJ   *b;
1753dfbe8321SBarry Smith   PetscErrorCode ierr;
175413f74950SBarry Smith   PetscMPIInt    size;
17550def2e27SBarry Smith   PetscTruth     no_unroll = PETSC_FALSE,no_inode = PETSC_FALSE;
1756a23d5eceSKris Buschelman 
1757a23d5eceSKris Buschelman   PetscFunctionBegin;
17587adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
1759e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Comm must be of size 1");
1760a23d5eceSKris Buschelman 
176138f2d2fdSLisandro Dalcin   ierr    = PetscNewLog(B,Mat_SeqSBAIJ,&b);CHKERRQ(ierr);
1762a23d5eceSKris Buschelman   B->data = (void*)b;
1763a23d5eceSKris Buschelman   ierr    = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
1764a23d5eceSKris Buschelman   B->ops->destroy     = MatDestroy_SeqSBAIJ;
1765a23d5eceSKris Buschelman   B->ops->view        = MatView_SeqSBAIJ;
1766a23d5eceSKris Buschelman   B->mapping          = 0;
1767a23d5eceSKris Buschelman   b->row              = 0;
1768a23d5eceSKris Buschelman   b->icol             = 0;
1769a23d5eceSKris Buschelman   b->reallocs         = 0;
1770a23d5eceSKris Buschelman   b->saved_values     = 0;
17710def2e27SBarry Smith   b->inode.limit      = 5;
17720def2e27SBarry Smith   b->inode.max_limit  = 5;
1773a23d5eceSKris Buschelman 
1774a23d5eceSKris Buschelman   b->roworiented      = PETSC_TRUE;
1775a23d5eceSKris Buschelman   b->nonew            = 0;
1776a23d5eceSKris Buschelman   b->diag             = 0;
1777a23d5eceSKris Buschelman   b->solve_work       = 0;
1778a23d5eceSKris Buschelman   b->mult_work        = 0;
1779a23d5eceSKris Buschelman   B->spptr            = 0;
17808e9a0fb8SHong Zhang   B->info.nz_unneeded = (PetscReal)b->maxnz;
1781a9817697SBarry Smith   b->keepnonzeropattern   = PETSC_FALSE;
1782a23d5eceSKris Buschelman   b->xtoy             = 0;
1783a23d5eceSKris Buschelman   b->XtoY             = 0;
1784a23d5eceSKris Buschelman 
1785a23d5eceSKris Buschelman   b->inew             = 0;
1786a23d5eceSKris Buschelman   b->jnew             = 0;
1787a23d5eceSKris Buschelman   b->anew             = 0;
1788a23d5eceSKris Buschelman   b->a2anew           = 0;
1789a23d5eceSKris Buschelman   b->permute          = PETSC_FALSE;
1790a23d5eceSKris Buschelman 
1791941593c8SHong Zhang   b->ignore_ltriangular = PETSC_FALSE;
179290d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)B)->prefix,"-mat_ignore_lower_triangular",&b->ignore_ltriangular,PETSC_NULL);CHKERRQ(ierr);
1793941593c8SHong Zhang 
1794f5edf698SHong Zhang   b->getrow_utriangular = PETSC_FALSE;
179590d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)B)->prefix,"-mat_getrow_uppertriangular",&b->getrow_utriangular,PETSC_NULL);CHKERRQ(ierr);
1796f5edf698SHong Zhang 
1797b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
1798ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C",
1799b5e56a35SBarry Smith 					   "MatGetFactor_seqsbaij_pastix",
1800b5e56a35SBarry Smith 					   MatGetFactor_seqsbaij_pastix);CHKERRQ(ierr);
1801b5e56a35SBarry Smith #endif
1802611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
1803ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C",
18045c9eb25fSBarry Smith                                      "MatGetFactor_seqsbaij_spooles",
18055c9eb25fSBarry Smith                                      MatGetFactor_seqsbaij_spooles);CHKERRQ(ierr);
1806611f576cSBarry Smith #endif
1807611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
1808ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C",
1809bccb9932SShri Abhyankar                                      "MatGetFactor_sbaij_mumps",
1810bccb9932SShri Abhyankar                                      MatGetFactor_sbaij_mumps);CHKERRQ(ierr);
1811611f576cSBarry Smith #endif
1812ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C",
1813db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqsbaij_petsc",
1814db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqsbaij_petsc);CHKERRQ(ierr);
1815ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C",
18165c9eb25fSBarry Smith                                      "MatGetFactor_seqsbaij_petsc",
18175c9eb25fSBarry Smith                                      MatGetFactor_seqsbaij_petsc);CHKERRQ(ierr);
1818a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
1819a23d5eceSKris Buschelman                                      "MatStoreValues_SeqSBAIJ",
1820a23d5eceSKris Buschelman                                      MatStoreValues_SeqSBAIJ);CHKERRQ(ierr);
1821a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
1822a23d5eceSKris Buschelman                                      "MatRetrieveValues_SeqSBAIJ",
1823a23d5eceSKris Buschelman                                      (void*)MatRetrieveValues_SeqSBAIJ);CHKERRQ(ierr);
1824a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqSBAIJSetColumnIndices_C",
1825a23d5eceSKris Buschelman                                      "MatSeqSBAIJSetColumnIndices_SeqSBAIJ",
1826a23d5eceSKris Buschelman                                      MatSeqSBAIJSetColumnIndices_SeqSBAIJ);CHKERRQ(ierr);
18274e5e7fe4SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqsbaij_seqaij_C",
18284e5e7fe4SHong Zhang                                      "MatConvert_SeqSBAIJ_SeqAIJ",
18294e5e7fe4SHong Zhang                                       MatConvert_SeqSBAIJ_SeqAIJ);CHKERRQ(ierr);
1830a0e1a404SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqsbaij_seqbaij_C",
1831a0e1a404SHong Zhang                                      "MatConvert_SeqSBAIJ_SeqBAIJ",
1832a0e1a404SHong Zhang                                       MatConvert_SeqSBAIJ_SeqBAIJ);CHKERRQ(ierr);
1833a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqSBAIJSetPreallocation_C",
1834a23d5eceSKris Buschelman                                      "MatSeqSBAIJSetPreallocation_SeqSBAIJ",
1835a23d5eceSKris Buschelman                                      MatSeqSBAIJSetPreallocation_SeqSBAIJ);CHKERRQ(ierr);
183623ce1328SBarry Smith 
183723ce1328SBarry Smith   B->symmetric                  = PETSC_TRUE;
183823ce1328SBarry Smith   B->structurally_symmetric     = PETSC_TRUE;
183923ce1328SBarry Smith   B->symmetric_set              = PETSC_TRUE;
184023ce1328SBarry Smith   B->structurally_symmetric_set = PETSC_TRUE;
184117667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQSBAIJ);CHKERRQ(ierr);
18420def2e27SBarry Smith 
18430def2e27SBarry Smith   ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Options for SEQSBAIJ matrix","Mat");CHKERRQ(ierr);
18440def2e27SBarry Smith     ierr = PetscOptionsTruth("-mat_no_unroll","Do not optimize for inodes (slower)",PETSC_NULL,no_unroll,&no_unroll,PETSC_NULL);CHKERRQ(ierr);
18450def2e27SBarry Smith     if (no_unroll) {ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_unroll\n");CHKERRQ(ierr);}
18460def2e27SBarry Smith     ierr = PetscOptionsTruth("-mat_no_inode","Do not optimize for inodes (slower)",PETSC_NULL,no_inode,&no_inode,PETSC_NULL);CHKERRQ(ierr);
18470def2e27SBarry Smith     if (no_inode) {ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_inode\n");CHKERRQ(ierr);}
18480def2e27SBarry 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);
18490def2e27SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
18500def2e27SBarry Smith   b->inode.use = (PetscTruth)(!(no_unroll || no_inode));
18510def2e27SBarry Smith   if (b->inode.limit > b->inode.max_limit) b->inode.limit = b->inode.max_limit;
18520def2e27SBarry Smith 
1853a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1854a23d5eceSKris Buschelman }
1855a23d5eceSKris Buschelman EXTERN_C_END
1856a23d5eceSKris Buschelman 
1857a23d5eceSKris Buschelman #undef __FUNCT__
1858a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation"
1859a23d5eceSKris Buschelman /*@C
1860a23d5eceSKris Buschelman    MatSeqSBAIJSetPreallocation - Creates a sparse symmetric matrix in block AIJ (block
1861a23d5eceSKris Buschelman    compressed row) format.  For good matrix assembly performance the
1862a23d5eceSKris Buschelman    user should preallocate the matrix storage by setting the parameter nz
1863a23d5eceSKris Buschelman    (or the array nnz).  By setting these parameters accurately, performance
1864a23d5eceSKris Buschelman    during matrix assembly can be increased by more than a factor of 50.
1865a23d5eceSKris Buschelman 
1866a23d5eceSKris Buschelman    Collective on Mat
1867a23d5eceSKris Buschelman 
1868a23d5eceSKris Buschelman    Input Parameters:
1869a23d5eceSKris Buschelman +  A - the symmetric matrix
1870a23d5eceSKris Buschelman .  bs - size of block
1871a23d5eceSKris Buschelman .  nz - number of block nonzeros per block row (same for all rows)
1872a23d5eceSKris Buschelman -  nnz - array containing the number of block nonzeros in the upper triangular plus
1873a23d5eceSKris Buschelman          diagonal portion of each block (possibly different for each block row) or PETSC_NULL
1874a23d5eceSKris Buschelman 
1875a23d5eceSKris Buschelman    Options Database Keys:
1876a23d5eceSKris Buschelman .   -mat_no_unroll - uses code that does not unroll the loops in the
1877a23d5eceSKris Buschelman                      block calculations (much slower)
1878db4efbfdSBarry Smith .    -mat_block_size - size of the blocks to use (only works if a negative bs is passed in
1879a23d5eceSKris Buschelman 
1880a23d5eceSKris Buschelman    Level: intermediate
1881a23d5eceSKris Buschelman 
1882a23d5eceSKris Buschelman    Notes:
1883a23d5eceSKris Buschelman    Specify the preallocated storage with either nz or nnz (not both).
1884a23d5eceSKris Buschelman    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
1885a23d5eceSKris Buschelman    allocation.  For additional details, see the users manual chapter on
1886a23d5eceSKris Buschelman    matrices.
1887a23d5eceSKris Buschelman 
1888aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
1889aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
1890aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
1891aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
1892aa95bbe8SBarry Smith 
189349a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
189449a6f317SBarry Smith 
189549a6f317SBarry Smith 
1896a23d5eceSKris Buschelman .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPISBAIJ()
1897a23d5eceSKris Buschelman @*/
1898be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqSBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt nz,const PetscInt nnz[])
189913f74950SBarry Smith {
190013f74950SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,PetscInt,const PetscInt[]);
1901a23d5eceSKris Buschelman 
1902a23d5eceSKris Buschelman   PetscFunctionBegin;
1903a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqSBAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
1904a23d5eceSKris Buschelman   if (f) {
1905a23d5eceSKris Buschelman     ierr = (*f)(B,bs,nz,nnz);CHKERRQ(ierr);
1906a23d5eceSKris Buschelman   }
1907a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1908a23d5eceSKris Buschelman }
190949b5e25fSSatish Balay 
19104a2ae208SSatish Balay #undef __FUNCT__
19114a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqSBAIJ"
1912c464158bSHong Zhang /*@C
1913c464158bSHong Zhang    MatCreateSeqSBAIJ - Creates a sparse symmetric matrix in block AIJ (block
1914c464158bSHong Zhang    compressed row) format.  For good matrix assembly performance the
1915c464158bSHong Zhang    user should preallocate the matrix storage by setting the parameter nz
1916c464158bSHong Zhang    (or the array nnz).  By setting these parameters accurately, performance
1917c464158bSHong Zhang    during matrix assembly can be increased by more than a factor of 50.
191849b5e25fSSatish Balay 
1919c464158bSHong Zhang    Collective on MPI_Comm
1920c464158bSHong Zhang 
1921c464158bSHong Zhang    Input Parameters:
1922c464158bSHong Zhang +  comm - MPI communicator, set to PETSC_COMM_SELF
1923c464158bSHong Zhang .  bs - size of block
1924c464158bSHong Zhang .  m - number of rows, or number of columns
1925c464158bSHong Zhang .  nz - number of block nonzeros per block row (same for all rows)
1926744e8345SSatish Balay -  nnz - array containing the number of block nonzeros in the upper triangular plus
1927744e8345SSatish Balay          diagonal portion of each block (possibly different for each block row) or PETSC_NULL
1928c464158bSHong Zhang 
1929c464158bSHong Zhang    Output Parameter:
1930c464158bSHong Zhang .  A - the symmetric matrix
1931c464158bSHong Zhang 
1932c464158bSHong Zhang    Options Database Keys:
1933c464158bSHong Zhang .   -mat_no_unroll - uses code that does not unroll the loops in the
1934c464158bSHong Zhang                      block calculations (much slower)
1935c464158bSHong Zhang .    -mat_block_size - size of the blocks to use
1936c464158bSHong Zhang 
1937c464158bSHong Zhang    Level: intermediate
1938c464158bSHong Zhang 
1939175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
1940ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
1941175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
1942175b88e8SBarry Smith 
1943c464158bSHong Zhang    Notes:
19446d6d819aSHong Zhang    The number of rows and columns must be divisible by blocksize.
19456d6d819aSHong Zhang    This matrix type does not support complex Hermitian operation.
1946c464158bSHong Zhang 
1947c464158bSHong Zhang    Specify the preallocated storage with either nz or nnz (not both).
1948c464158bSHong Zhang    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
1949c464158bSHong Zhang    allocation.  For additional details, see the users manual chapter on
1950c464158bSHong Zhang    matrices.
1951c464158bSHong Zhang 
195249a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
195349a6f317SBarry Smith 
1954c464158bSHong Zhang .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPISBAIJ()
1955c464158bSHong Zhang @*/
1956be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqSBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
1957c464158bSHong Zhang {
1958dfbe8321SBarry Smith   PetscErrorCode ierr;
1959c464158bSHong Zhang 
1960c464158bSHong Zhang   PetscFunctionBegin;
1961f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
1962f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
1963c464158bSHong Zhang   ierr = MatSetType(*A,MATSEQSBAIJ);CHKERRQ(ierr);
1964ab93d7beSBarry Smith   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*A,bs,nz,(PetscInt*)nnz);CHKERRQ(ierr);
196549b5e25fSSatish Balay   PetscFunctionReturn(0);
196649b5e25fSSatish Balay }
196749b5e25fSSatish Balay 
19684a2ae208SSatish Balay #undef __FUNCT__
19694a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqSBAIJ"
1970dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqSBAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
197149b5e25fSSatish Balay {
197249b5e25fSSatish Balay   Mat            C;
197349b5e25fSSatish Balay   Mat_SeqSBAIJ   *c,*a = (Mat_SeqSBAIJ*)A->data;
19746849ba73SBarry Smith   PetscErrorCode ierr;
1975b40805acSSatish Balay   PetscInt       i,mbs = a->mbs,nz = a->nz,bs2 =a->bs2;
197649b5e25fSSatish Balay 
197749b5e25fSSatish Balay   PetscFunctionBegin;
1978e32f2f54SBarry Smith   if (a->i[mbs] != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupt matrix");
197949b5e25fSSatish Balay 
198049b5e25fSSatish Balay   *B = 0;
19817adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1982d0f46423SBarry Smith   ierr = MatSetSizes(C,A->rmap->N,A->cmap->n,A->rmap->N,A->cmap->n);CHKERRQ(ierr);
19838e9a0fb8SHong Zhang   ierr = MatSetType(C,MATSEQSBAIJ);CHKERRQ(ierr);
19841d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
1985692f9cbeSHong Zhang   c    = (Mat_SeqSBAIJ*)C->data;
1986692f9cbeSHong Zhang 
1987273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
1988d5f3da31SBarry Smith   C->factortype         = A->factortype;
198949b5e25fSSatish Balay   c->row                = 0;
199049b5e25fSSatish Balay   c->icol               = 0;
199149b5e25fSSatish Balay   c->saved_values       = 0;
1992a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
199349b5e25fSSatish Balay   C->assembled          = PETSC_TRUE;
199449b5e25fSSatish Balay 
199526283091SBarry Smith   ierr = PetscLayoutCopy(A->rmap,&C->rmap);CHKERRQ(ierr);
199626283091SBarry Smith   ierr = PetscLayoutCopy(A->cmap,&C->cmap);CHKERRQ(ierr);
199749b5e25fSSatish Balay   c->bs2  = a->bs2;
199849b5e25fSSatish Balay   c->mbs  = a->mbs;
199949b5e25fSSatish Balay   c->nbs  = a->nbs;
200049b5e25fSSatish Balay 
2001c760cd28SBarry Smith   if  (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2002c760cd28SBarry Smith     c->imax           = a->imax;
2003c760cd28SBarry Smith     c->ilen           = a->ilen;
2004c760cd28SBarry Smith     c->free_imax_ilen = PETSC_FALSE;
2005c760cd28SBarry Smith   } else {
20068777fc3fSSatish Balay     ierr = PetscMalloc2((mbs+1),PetscInt,&c->imax,(mbs+1),PetscInt,&c->ilen);CHKERRQ(ierr);
2007c760cd28SBarry Smith     ierr = PetscLogObjectMemory(C,2*(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
200849b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
200949b5e25fSSatish Balay       c->imax[i] = a->imax[i];
201049b5e25fSSatish Balay       c->ilen[i] = a->ilen[i];
201149b5e25fSSatish Balay     }
2012c760cd28SBarry Smith     c->free_imax_ilen = PETSC_TRUE;
2013c760cd28SBarry Smith   }
201449b5e25fSSatish Balay 
201549b5e25fSSatish Balay   /* allocate the matrix space */
20164da8f245SBarry Smith   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
20174da8f245SBarry Smith     ierr = PetscMalloc(bs2*nz*sizeof(MatScalar),&c->a);CHKERRQ(ierr);
20184da8f245SBarry Smith     ierr = PetscLogObjectMemory(C,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
20194da8f245SBarry Smith     c->singlemalloc = PETSC_FALSE;
20204da8f245SBarry Smith     c->free_ij      = PETSC_FALSE;
20214da8f245SBarry Smith     c->parent       = A;
20224da8f245SBarry Smith     ierr            = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
20234da8f245SBarry Smith     ierr            = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
20244da8f245SBarry Smith     ierr            = MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
20254da8f245SBarry Smith   } else {
2026b40805acSSatish Balay     ierr = PetscMalloc3(bs2*nz,MatScalar,&c->a,nz,PetscInt,&c->j,mbs+1,PetscInt,&c->i);CHKERRQ(ierr);
202713f74950SBarry Smith     ierr = PetscMemcpy(c->i,a->i,(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
2028b40805acSSatish Balay     ierr = PetscLogObjectMemory(C,(mbs+1)*sizeof(PetscInt) + nz*(bs2*sizeof(MatScalar) + sizeof(PetscInt)));CHKERRQ(ierr);
20294da8f245SBarry Smith     c->singlemalloc = PETSC_TRUE;
20304da8f245SBarry Smith     c->free_ij      = PETSC_TRUE;
20314da8f245SBarry Smith   }
203249b5e25fSSatish Balay   if (mbs > 0) {
20334da8f245SBarry Smith     if (cpvalues != MAT_SHARE_NONZERO_PATTERN) {
203413f74950SBarry Smith       ierr = PetscMemcpy(c->j,a->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
20354da8f245SBarry Smith     }
203649b5e25fSSatish Balay     if (cpvalues == MAT_COPY_VALUES) {
203749b5e25fSSatish Balay       ierr = PetscMemcpy(c->a,a->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
203849b5e25fSSatish Balay     } else {
203949b5e25fSSatish Balay       ierr = PetscMemzero(c->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
204049b5e25fSSatish Balay     }
2041a1c3900fSBarry Smith     if (a->jshort) {
20424da8f245SBarry Smith       if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
20434da8f245SBarry Smith         c->jshort      = a->jshort;
20444da8f245SBarry Smith         c->free_jshort = PETSC_FALSE;
20454da8f245SBarry Smith       } else {
2046a1c3900fSBarry Smith         ierr = PetscMalloc(nz*sizeof(unsigned short),&c->jshort);CHKERRQ(ierr);
2047c760cd28SBarry Smith         ierr = PetscLogObjectMemory(C,nz*sizeof(unsigned short));CHKERRQ(ierr);
2048a1c3900fSBarry Smith         ierr = PetscMemcpy(c->jshort,a->jshort,nz*sizeof(unsigned short));CHKERRQ(ierr);
20494da8f245SBarry Smith         c->free_jshort = PETSC_TRUE;
20504da8f245SBarry Smith       }
2051a1c3900fSBarry Smith     }
205249b5e25fSSatish Balay   }
205349b5e25fSSatish Balay 
205449b5e25fSSatish Balay   c->roworiented = a->roworiented;
205549b5e25fSSatish Balay   c->nonew       = a->nonew;
205649b5e25fSSatish Balay 
205749b5e25fSSatish Balay   if (a->diag) {
2058c760cd28SBarry Smith     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2059c760cd28SBarry Smith       c->diag      = a->diag;
2060c760cd28SBarry Smith       c->free_diag = PETSC_FALSE;
2061c760cd28SBarry Smith     } else {
20622ed38d0bSJed Brown       ierr = PetscMalloc(mbs*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
20632ed38d0bSJed Brown       ierr = PetscLogObjectMemory(C,mbs*sizeof(PetscInt));CHKERRQ(ierr);
206449b5e25fSSatish Balay       for (i=0; i<mbs; i++) {
206549b5e25fSSatish Balay 	c->diag[i] = a->diag[i];
206649b5e25fSSatish Balay       }
2067c760cd28SBarry Smith       c->free_diag = PETSC_TRUE;
2068c760cd28SBarry Smith     }
206949b5e25fSSatish Balay   } else c->diag  = 0;
20706c6c5352SBarry Smith   c->nz           = a->nz;
20718e9a0fb8SHong Zhang   c->maxnz        = bs2*a->nz; /* Since we allocate exactly the right amount */
207249b5e25fSSatish Balay   c->solve_work   = 0;
207349b5e25fSSatish Balay   c->mult_work    = 0;
2074e6b907acSBarry Smith   c->free_a       = PETSC_TRUE;
207549b5e25fSSatish Balay   *B = C;
20767adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
207749b5e25fSSatish Balay   PetscFunctionReturn(0);
207849b5e25fSSatish Balay }
207949b5e25fSSatish Balay 
20804a2ae208SSatish Balay #undef __FUNCT__
20814a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqSBAIJ"
2082a313700dSBarry Smith PetscErrorCode MatLoad_SeqSBAIJ(PetscViewer viewer, const MatType type,Mat *A)
208349b5e25fSSatish Balay {
208449b5e25fSSatish Balay   Mat_SeqSBAIJ   *a;
208549b5e25fSSatish Balay   Mat            B;
20866849ba73SBarry Smith   PetscErrorCode ierr;
208713f74950SBarry Smith   int            fd;
208813f74950SBarry Smith   PetscMPIInt    size;
208913f74950SBarry Smith   PetscInt       i,nz,header[4],*rowlengths=0,M,N,bs=1;
209013f74950SBarry Smith   PetscInt       *mask,mbs,*jj,j,rowcount,nzcount,k,*s_browlengths,maskcount;
209113f74950SBarry Smith   PetscInt       kmax,jcount,block,idx,point,nzcountb,extra_rows;
209213f74950SBarry Smith   PetscInt       *masked,nmask,tmp,bs2,ishift;
209387828ca2SBarry Smith   PetscScalar    *aa;
209449b5e25fSSatish Balay   MPI_Comm       comm = ((PetscObject)viewer)->comm;
209549b5e25fSSatish Balay 
209649b5e25fSSatish Balay   PetscFunctionBegin;
2097b0a32e0cSBarry Smith   ierr = PetscOptionsGetInt(PETSC_NULL,"-matload_block_size",&bs,PETSC_NULL);CHKERRQ(ierr);
209849b5e25fSSatish Balay   bs2  = bs*bs;
209949b5e25fSSatish Balay 
210049b5e25fSSatish Balay   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2101e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
2102b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
210349b5e25fSSatish Balay   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
2104e32f2f54SBarry Smith   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not Mat object");
210549b5e25fSSatish Balay   M = header[1]; N = header[2]; nz = header[3];
210649b5e25fSSatish Balay 
2107e7e72b3dSBarry Smith   if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as SeqSBAIJ");
210849b5e25fSSatish Balay 
2109e32f2f54SBarry Smith   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");
211049b5e25fSSatish Balay 
211149b5e25fSSatish Balay   /*
211249b5e25fSSatish Balay      This code adds extra rows to make sure the number of rows is
211349b5e25fSSatish Balay     divisible by the blocksize
211449b5e25fSSatish Balay   */
211549b5e25fSSatish Balay   mbs        = M/bs;
211649b5e25fSSatish Balay   extra_rows = bs - M + bs*(mbs);
211749b5e25fSSatish Balay   if (extra_rows == bs) extra_rows = 0;
211849b5e25fSSatish Balay   else                  mbs++;
211949b5e25fSSatish Balay   if (extra_rows) {
21201e2582c4SBarry Smith     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
212149b5e25fSSatish Balay   }
212249b5e25fSSatish Balay 
212349b5e25fSSatish Balay   /* read in row lengths */
212413f74950SBarry Smith   ierr = PetscMalloc((M+extra_rows)*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
212549b5e25fSSatish Balay   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
212649b5e25fSSatish Balay   for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
212749b5e25fSSatish Balay 
212849b5e25fSSatish Balay   /* read in column indices */
212913f74950SBarry Smith   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscInt),&jj);CHKERRQ(ierr);
213049b5e25fSSatish Balay   ierr = PetscBinaryRead(fd,jj,nz,PETSC_INT);CHKERRQ(ierr);
213149b5e25fSSatish Balay   for (i=0; i<extra_rows; i++) jj[nz+i] = M+i;
213249b5e25fSSatish Balay 
213349b5e25fSSatish Balay   /* loop over row lengths determining block row lengths */
213413f74950SBarry Smith   ierr     = PetscMalloc(mbs*sizeof(PetscInt),&s_browlengths);CHKERRQ(ierr);
213513f74950SBarry Smith   ierr     = PetscMemzero(s_browlengths,mbs*sizeof(PetscInt));CHKERRQ(ierr);
213674ed9c26SBarry Smith   ierr     = PetscMalloc2(mbs,PetscInt,&mask,mbs,PetscInt,&masked);CHKERRQ(ierr);
213713f74950SBarry Smith   ierr     = PetscMemzero(mask,mbs*sizeof(PetscInt));CHKERRQ(ierr);
213874ed9c26SBarry Smith   rowcount = 0;
213974ed9c26SBarry Smith   nzcount  = 0;
214049b5e25fSSatish Balay   for (i=0; i<mbs; i++) {
214149b5e25fSSatish Balay     nmask = 0;
214249b5e25fSSatish Balay     for (j=0; j<bs; j++) {
214349b5e25fSSatish Balay       kmax = rowlengths[rowcount];
214449b5e25fSSatish Balay       for (k=0; k<kmax; k++) {
21452d703238SHong Zhang         tmp = jj[nzcount++]/bs;   /* block col. index */
214603630b6eSHong Zhang         if (!mask[tmp] && tmp >= i) {masked[nmask++] = tmp; mask[tmp] = 1;}
214749b5e25fSSatish Balay       }
214849b5e25fSSatish Balay       rowcount++;
214949b5e25fSSatish Balay     }
2150574b2666SHong Zhang     s_browlengths[i] += nmask;
2151574b2666SHong Zhang 
215249b5e25fSSatish Balay     /* zero out the mask elements we set */
215349b5e25fSSatish Balay     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
215449b5e25fSSatish Balay   }
215549b5e25fSSatish Balay 
215649b5e25fSSatish Balay   /* create our matrix */
2157f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
2158f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,M+extra_rows,N+extra_rows,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
21599abb65ffSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
2160ab93d7beSBarry Smith   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(B,bs,0,s_browlengths);CHKERRQ(ierr);
216149b5e25fSSatish Balay   a = (Mat_SeqSBAIJ*)B->data;
216249b5e25fSSatish Balay 
216349b5e25fSSatish Balay   /* set matrix "i" values */
2164e60cf9a0SBarry Smith   a->i[0] = 0;
216549b5e25fSSatish Balay   for (i=1; i<= mbs; i++) {
2166574b2666SHong Zhang     a->i[i]      = a->i[i-1] + s_browlengths[i-1];
2167574b2666SHong Zhang     a->ilen[i-1] = s_browlengths[i-1];
216849b5e25fSSatish Balay   }
21696c6c5352SBarry Smith   a->nz = a->i[mbs];
217049b5e25fSSatish Balay 
217149b5e25fSSatish Balay   /* read in nonzero values */
217287828ca2SBarry Smith   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscScalar),&aa);CHKERRQ(ierr);
217349b5e25fSSatish Balay   ierr = PetscBinaryRead(fd,aa,nz,PETSC_SCALAR);CHKERRQ(ierr);
217449b5e25fSSatish Balay   for (i=0; i<extra_rows; i++) aa[nz+i] = 1.0;
217549b5e25fSSatish Balay 
217649b5e25fSSatish Balay   /* set "a" and "j" values into matrix */
217749b5e25fSSatish Balay   nzcount = 0; jcount = 0;
217849b5e25fSSatish Balay   for (i=0; i<mbs; i++) {
217949b5e25fSSatish Balay     nzcountb = nzcount;
218049b5e25fSSatish Balay     nmask    = 0;
218149b5e25fSSatish Balay     for (j=0; j<bs; j++) {
218249b5e25fSSatish Balay       kmax = rowlengths[i*bs+j];
218349b5e25fSSatish Balay       for (k=0; k<kmax; k++) {
21842d703238SHong Zhang         tmp = jj[nzcount++]/bs; /* block col. index */
218503630b6eSHong Zhang         if (!mask[tmp] && tmp >= i) { masked[nmask++] = tmp; mask[tmp] = 1;}
21862d703238SHong Zhang       }
21872d703238SHong Zhang     }
21882d703238SHong Zhang     /* sort the masked values */
21892d703238SHong Zhang     ierr = PetscSortInt(nmask,masked);CHKERRQ(ierr);
21902d703238SHong Zhang 
21912d703238SHong Zhang     /* set "j" values into matrix */
21922d703238SHong Zhang     maskcount = 1;
21932d703238SHong Zhang     for (j=0; j<nmask; j++) {
219449b5e25fSSatish Balay       a->j[jcount++]  = masked[j];
219549b5e25fSSatish Balay       mask[masked[j]] = maskcount++;
219649b5e25fSSatish Balay     }
2197574b2666SHong Zhang 
219849b5e25fSSatish Balay     /* set "a" values into matrix */
219949b5e25fSSatish Balay     ishift = bs2*a->i[i];
220049b5e25fSSatish Balay     for (j=0; j<bs; j++) {
220149b5e25fSSatish Balay       kmax = rowlengths[i*bs+j];
220249b5e25fSSatish Balay       for (k=0; k<kmax; k++) {
2203574b2666SHong Zhang         tmp       = jj[nzcountb]/bs ; /* block col. index */
2204574b2666SHong Zhang         if (tmp >= i){
220549b5e25fSSatish Balay           block     = mask[tmp] - 1;
220649b5e25fSSatish Balay           point     = jj[nzcountb] - bs*tmp;
220749b5e25fSSatish Balay           idx       = ishift + bs2*block + j + bs*point;
2208574b2666SHong Zhang           a->a[idx] = aa[nzcountb];
2209574b2666SHong Zhang         }
2210574b2666SHong Zhang         nzcountb++;
221149b5e25fSSatish Balay       }
221249b5e25fSSatish Balay     }
221349b5e25fSSatish Balay     /* zero out the mask elements we set */
221449b5e25fSSatish Balay     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
221549b5e25fSSatish Balay   }
2216e32f2f54SBarry Smith   if (jcount != a->nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Bad binary matrix");
221749b5e25fSSatish Balay 
221849b5e25fSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
2219574b2666SHong Zhang   ierr = PetscFree(s_browlengths);CHKERRQ(ierr);
222049b5e25fSSatish Balay   ierr = PetscFree(aa);CHKERRQ(ierr);
222149b5e25fSSatish Balay   ierr = PetscFree(jj);CHKERRQ(ierr);
222274ed9c26SBarry Smith   ierr = PetscFree2(mask,masked);CHKERRQ(ierr);
222349b5e25fSSatish Balay 
22249abb65ffSKris Buschelman   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22259abb65ffSKris Buschelman   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
222649b5e25fSSatish Balay   ierr = MatView_Private(B);CHKERRQ(ierr);
22279abb65ffSKris Buschelman   *A = B;
222849b5e25fSSatish Balay   PetscFunctionReturn(0);
222949b5e25fSSatish Balay }
2230574b2666SHong Zhang 
2231d06b337dSHong Zhang #undef __FUNCT__
2232c75a6043SHong Zhang #define __FUNCT__ "MatCreateSeqSBAIJWithArrays"
2233c75a6043SHong Zhang /*@
2234c75a6043SHong Zhang      MatCreateSeqSBAIJWithArrays - Creates an sequential SBAIJ matrix using matrix elements
2235c75a6043SHong Zhang               (upper triangular entries in CSR format) provided by the user.
2236c75a6043SHong Zhang 
2237c75a6043SHong Zhang      Collective on MPI_Comm
2238c75a6043SHong Zhang 
2239c75a6043SHong Zhang    Input Parameters:
2240c75a6043SHong Zhang +  comm - must be an MPI communicator of size 1
2241c75a6043SHong Zhang .  bs - size of block
2242c75a6043SHong Zhang .  m - number of rows
2243c75a6043SHong Zhang .  n - number of columns
2244c75a6043SHong Zhang .  i - row indices
2245c75a6043SHong Zhang .  j - column indices
2246c75a6043SHong Zhang -  a - matrix values
2247c75a6043SHong Zhang 
2248c75a6043SHong Zhang    Output Parameter:
2249c75a6043SHong Zhang .  mat - the matrix
2250c75a6043SHong Zhang 
2251c75a6043SHong Zhang    Level: intermediate
2252c75a6043SHong Zhang 
2253c75a6043SHong Zhang    Notes:
2254c75a6043SHong Zhang        The i, j, and a arrays are not copied by this routine, the user must free these arrays
2255c75a6043SHong Zhang     once the matrix is destroyed
2256c75a6043SHong Zhang 
2257c75a6043SHong Zhang        You cannot set new nonzero locations into this matrix, that will generate an error.
2258c75a6043SHong Zhang 
2259c75a6043SHong Zhang        The i and j indices are 0 based
2260c75a6043SHong Zhang 
2261c75a6043SHong Zhang .seealso: MatCreate(), MatCreateMPISBAIJ(), MatCreateSeqSBAIJ()
2262c75a6043SHong Zhang 
2263c75a6043SHong Zhang @*/
2264c75a6043SHong Zhang PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqSBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
2265c75a6043SHong Zhang {
2266c75a6043SHong Zhang   PetscErrorCode ierr;
2267c75a6043SHong Zhang   PetscInt       ii;
2268c75a6043SHong Zhang   Mat_SeqSBAIJ   *sbaij;
2269c75a6043SHong Zhang 
2270c75a6043SHong Zhang   PetscFunctionBegin;
2271e32f2f54SBarry Smith   if (bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"block size %D > 1 is not supported yet",bs);
2272e32f2f54SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
2273c75a6043SHong Zhang 
2274c75a6043SHong Zhang   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
2275c75a6043SHong Zhang   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
2276c75a6043SHong Zhang   ierr = MatSetType(*mat,MATSEQSBAIJ);CHKERRQ(ierr);
2277c75a6043SHong Zhang   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*mat,bs,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
2278c75a6043SHong Zhang   sbaij = (Mat_SeqSBAIJ*)(*mat)->data;
2279c75a6043SHong Zhang   ierr = PetscMalloc2(m,PetscInt,&sbaij->imax,m,PetscInt,&sbaij->ilen);CHKERRQ(ierr);
2280c760cd28SBarry Smith   ierr = PetscLogObjectMemory(*mat,2*m*sizeof(PetscInt));CHKERRQ(ierr);
2281c75a6043SHong Zhang 
2282c75a6043SHong Zhang   sbaij->i = i;
2283c75a6043SHong Zhang   sbaij->j = j;
2284c75a6043SHong Zhang   sbaij->a = a;
2285c75a6043SHong Zhang   sbaij->singlemalloc = PETSC_FALSE;
2286c75a6043SHong Zhang   sbaij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
2287e6b907acSBarry Smith   sbaij->free_a       = PETSC_FALSE;
2288e6b907acSBarry Smith   sbaij->free_ij      = PETSC_FALSE;
2289c75a6043SHong Zhang 
2290c75a6043SHong Zhang   for (ii=0; ii<m; ii++) {
2291c75a6043SHong Zhang     sbaij->ilen[ii] = sbaij->imax[ii] = i[ii+1] - i[ii];
2292c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2293e32f2f54SBarry 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]);
2294c75a6043SHong Zhang #endif
2295c75a6043SHong Zhang   }
2296c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2297c75a6043SHong Zhang   for (ii=0; ii<sbaij->i[m]; ii++) {
2298e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
2299e32f2f54SBarry 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]);
2300c75a6043SHong Zhang   }
2301c75a6043SHong Zhang #endif
2302c75a6043SHong Zhang 
2303c75a6043SHong Zhang   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2304c75a6043SHong Zhang   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2305c75a6043SHong Zhang   PetscFunctionReturn(0);
2306c75a6043SHong Zhang }
2307d06b337dSHong Zhang 
2308d06b337dSHong Zhang 
2309d06b337dSHong Zhang 
231049b5e25fSSatish Balay 
231149b5e25fSSatish Balay 
2312