xref: /petsc/src/mat/impls/sbaij/seq/sbaij.c (revision 71dad5bbabebfec99fe578912fe04cf957d6f190)
149b5e25fSSatish Balay 
249b5e25fSSatish Balay /*
3a1373b80SHong Zhang     Defines the basic matrix operations for the SBAIJ (compressed row)
449b5e25fSSatish Balay   matrix storage format.
549b5e25fSSatish Balay */
6c6db04a5SJed Brown #include <../src/mat/impls/baij/seq/baij.h>         /*I "petscmat.h" I*/
7c6db04a5SJed Brown #include <../src/mat/impls/sbaij/seq/sbaij.h>
8c6db04a5SJed Brown #include <petscblaslapack.h>
949b5e25fSSatish Balay 
10c6db04a5SJed Brown #include <../src/mat/impls/sbaij/seq/relax.h>
1170dcbbb9SBarry Smith #define USESHORT
12c6db04a5SJed Brown #include <../src/mat/impls/sbaij/seq/relax.h>
1370dcbbb9SBarry Smith 
14ace3abfcSBarry Smith extern PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat,PetscBool );
15b5b17502SBarry Smith 
1649b5e25fSSatish Balay /*
1749b5e25fSSatish Balay      Checks for missing diagonals
1849b5e25fSSatish Balay */
194a2ae208SSatish Balay #undef __FUNCT__
204a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqSBAIJ"
21ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqSBAIJ(Mat A,PetscBool  *missing,PetscInt *dd)
2249b5e25fSSatish Balay {
23045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
246849ba73SBarry Smith   PetscErrorCode ierr;
2513f74950SBarry Smith   PetscInt       *diag,*jj = a->j,i;
2649b5e25fSSatish Balay 
2749b5e25fSSatish Balay   PetscFunctionBegin;
28045c9aa0SHong Zhang   ierr = MatMarkDiagonal_SeqSBAIJ(A);CHKERRQ(ierr);
2949b5e25fSSatish Balay   diag = a->diag;
302af78befSBarry Smith   *missing = PETSC_FALSE;
3149b5e25fSSatish Balay   for (i=0; i<a->mbs; i++) {
322af78befSBarry Smith     if (jj[diag[i]] != i) {
332af78befSBarry Smith       *missing    = PETSC_TRUE;
342af78befSBarry Smith       if (dd) *dd = i;
352af78befSBarry Smith       break;
362af78befSBarry Smith     }
3749b5e25fSSatish Balay   }
3849b5e25fSSatish Balay   PetscFunctionReturn(0);
3949b5e25fSSatish Balay }
4049b5e25fSSatish Balay 
414a2ae208SSatish Balay #undef __FUNCT__
424a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqSBAIJ"
43dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqSBAIJ(Mat A)
4449b5e25fSSatish Balay {
45045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
466849ba73SBarry Smith   PetscErrorCode ierr;
4709f38230SBarry Smith   PetscInt       i;
4849b5e25fSSatish Balay 
4949b5e25fSSatish Balay   PetscFunctionBegin;
5009f38230SBarry Smith   if (!a->diag) {
5109f38230SBarry Smith     ierr = PetscMalloc(a->mbs*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
52c760cd28SBarry Smith     ierr = PetscLogObjectMemory(A,a->mbs*sizeof(PetscInt));CHKERRQ(ierr);
53c760cd28SBarry Smith     a->free_diag = PETSC_TRUE;
5409f38230SBarry Smith   }
5509f38230SBarry Smith   for (i=0; i<a->mbs; i++) a->diag[i] = a->i[i];
5649b5e25fSSatish Balay   PetscFunctionReturn(0);
5749b5e25fSSatish Balay }
5849b5e25fSSatish Balay 
594a2ae208SSatish Balay #undef __FUNCT__
604a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqSBAIJ"
61ace3abfcSBarry Smith static PetscErrorCode MatGetRowIJ_SeqSBAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
6249b5e25fSSatish Balay {
63a6ece127SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
64d0f46423SBarry Smith   PetscInt     i,j,n = a->mbs,nz = a->i[n],bs = A->rmap->bs;
658f7157efSSatish Balay   PetscErrorCode ierr;
6649b5e25fSSatish Balay 
6749b5e25fSSatish Balay   PetscFunctionBegin;
68d3e5a4abSHong Zhang   *nn = n;
69a1373b80SHong Zhang   if (!ia) PetscFunctionReturn(0);
708f7157efSSatish Balay   if (!blockcompressed) {
718f7157efSSatish Balay     /* malloc & create the natural set of indices */
72f1d0d59dSSatish Balay     ierr = PetscMalloc2((n+1)*bs,PetscInt,ia,nz*bs,PetscInt,ja);CHKERRQ(ierr);
738f7157efSSatish Balay     for (i=0; i<n+1; i++) {
748f7157efSSatish Balay       for (j=0; j<bs; j++) {
758f7157efSSatish Balay         *ia[i*bs+j] = a->i[i]*bs+j+oshift;
768f7157efSSatish Balay       }
778f7157efSSatish Balay     }
788f7157efSSatish Balay     for (i=0; i<nz; i++) {
798f7157efSSatish Balay       for (j=0; j<bs; j++) {
808f7157efSSatish Balay         *ja[i*bs+j] = a->j[i]*bs+j+oshift;
818f7157efSSatish Balay       }
828f7157efSSatish Balay     }
838f7157efSSatish Balay   } else { /* blockcompressed */
84a6ece127SHong Zhang     if (oshift == 1) {
85a6ece127SHong Zhang       /* temporarily add 1 to i and j indices */
866c6c5352SBarry Smith       for (i=0; i<nz; i++) a->j[i]++;
87a1373b80SHong Zhang       for (i=0; i<n+1; i++) a->i[i]++;
888f7157efSSatish Balay     }
89a1373b80SHong Zhang     *ia = a->i; *ja = a->j;
90a6ece127SHong Zhang   }
918f7157efSSatish Balay 
9249b5e25fSSatish Balay   PetscFunctionReturn(0);
9349b5e25fSSatish Balay }
9449b5e25fSSatish Balay 
954a2ae208SSatish Balay #undef __FUNCT__
964a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqSBAIJ"
97ace3abfcSBarry Smith static PetscErrorCode MatRestoreRowIJ_SeqSBAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
9849b5e25fSSatish Balay {
99b7aaefc3SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
1008f7157efSSatish Balay   PetscInt     i,n = a->mbs,nz = a->i[n];
1018f7157efSSatish Balay   PetscErrorCode ierr;
102a6ece127SHong Zhang 
10349b5e25fSSatish Balay   PetscFunctionBegin;
10449b5e25fSSatish Balay   if (!ia) PetscFunctionReturn(0);
105a6ece127SHong Zhang 
1068f7157efSSatish Balay   if (!blockcompressed) {
1078f7157efSSatish Balay     ierr = PetscFree2(*ia,*ja);CHKERRQ(ierr);
1088f7157efSSatish Balay   } else if (oshift == 1) { /* blockcompressed */
1096c6c5352SBarry Smith     for (i=0; i<nz; i++) a->j[i]--;
110a6ece127SHong Zhang     for (i=0; i<n+1; i++) a->i[i]--;
111a6ece127SHong Zhang   }
1128f7157efSSatish Balay 
113a6ece127SHong Zhang   PetscFunctionReturn(0);
11449b5e25fSSatish Balay }
11549b5e25fSSatish Balay 
1164a2ae208SSatish Balay #undef __FUNCT__
1174a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqSBAIJ"
118dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqSBAIJ(Mat A)
11949b5e25fSSatish Balay {
12049b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
121dfbe8321SBarry Smith   PetscErrorCode ierr;
12249b5e25fSSatish Balay 
12349b5e25fSSatish Balay   PetscFunctionBegin;
124a9f03627SSatish Balay #if defined(PETSC_USE_LOG)
125d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, NZ=%D",A->rmap->N,a->nz);
126a9f03627SSatish Balay #endif
127e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
1287f53bb6cSHong Zhang   if (a->free_diag){ierr = PetscFree(a->diag);CHKERRQ(ierr);}
1296bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
1306bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
1316bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
132c31cb41cSBarry Smith   ierr = PetscFree(a->idiag);CHKERRQ(ierr);
133c31cb41cSBarry Smith   ierr = PetscFree(a->inode.size);CHKERRQ(ierr);
134c31cb41cSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
135c760cd28SBarry Smith   if (a->free_imax_ilen) {ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);}
13605b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
13741f059aeSBarry Smith   ierr = PetscFree(a->sor_work);CHKERRQ(ierr);
13805b42c5fSBarry Smith   ierr = PetscFree(a->solves_work);CHKERRQ(ierr);
13905b42c5fSBarry Smith   ierr = PetscFree(a->mult_work);CHKERRQ(ierr);
14005b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
14105b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
1424da8f245SBarry Smith   if (a->free_jshort) {ierr = PetscFree(a->jshort);CHKERRQ(ierr);}
1431a3463dfSHong Zhang   ierr = PetscFree(a->inew);CHKERRQ(ierr);
1446bf464f9SBarry Smith   ierr = MatDestroy(&a->parent);CHKERRQ(ierr);
145bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
146901853e0SKris Buschelman 
147dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
148901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
149901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
150901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
151901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqaij_C","",PETSC_NULL);CHKERRQ(ierr);
152901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
153901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
15449b5e25fSSatish Balay   PetscFunctionReturn(0);
15549b5e25fSSatish Balay }
15649b5e25fSSatish Balay 
1574a2ae208SSatish Balay #undef __FUNCT__
1584a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqSBAIJ"
159ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqSBAIJ(Mat A,MatOption op,PetscBool  flg)
16049b5e25fSSatish Balay {
161045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
16263ba0a88SBarry Smith   PetscErrorCode ierr;
16349b5e25fSSatish Balay 
16449b5e25fSSatish Balay   PetscFunctionBegin;
1654d9d31abSKris Buschelman   switch (op) {
1664d9d31abSKris Buschelman   case MAT_ROW_ORIENTED:
1674e0d8c25SBarry Smith     a->roworiented = flg;
1684d9d31abSKris Buschelman     break;
169a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
170a9817697SBarry Smith     a->keepnonzeropattern = flg;
1714d9d31abSKris Buschelman     break;
172512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
173512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1744d9d31abSKris Buschelman     break;
1754d9d31abSKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
1764e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1774d9d31abSKris Buschelman     break;
1784d9d31abSKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1794e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1804d9d31abSKris Buschelman     break;
18128b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
18228b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
18328b2fa4aSMatthew Knepley     break;
1844e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1854d9d31abSKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1864d9d31abSKris Buschelman   case MAT_USE_HASH_TABLE:
187290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1884d9d31abSKris Buschelman     break;
1899a4540c5SBarry Smith   case MAT_HERMITIAN:
190e32f2f54SBarry Smith     if (!A->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call MatAssemblyEnd() first");
1910bc54ff2SBarry Smith     if (A->cmap->n < 65536 && A->cmap->bs == 1) {
192eeffb40dSHong Zhang       A->ops->mult = MatMult_SeqSBAIJ_1_Hermitian_ushort;
1930bc54ff2SBarry Smith     } else if (A->cmap->bs == 1) {
194eeffb40dSHong Zhang       A->ops->mult = MatMult_SeqSBAIJ_1_Hermitian;
195e32f2f54SBarry Smith     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for Hermitian with block size greater than 1");
196eeffb40dSHong Zhang     break;
1973d472b54SHong Zhang   case MAT_SPD:
1983d472b54SHong Zhang     A->spd_set                         = PETSC_TRUE;
1993d472b54SHong Zhang     A->spd                             = flg;
2003d472b54SHong Zhang     if (flg) {
2013d472b54SHong Zhang       A->symmetric                     = PETSC_TRUE;
2023d472b54SHong Zhang       A->structurally_symmetric        = PETSC_TRUE;
2033d472b54SHong Zhang       A->symmetric_set                 = PETSC_TRUE;
2043d472b54SHong Zhang       A->structurally_symmetric_set    = PETSC_TRUE;
2053d472b54SHong Zhang     }
2063d472b54SHong Zhang     break;
20777e54ba9SKris Buschelman   case MAT_SYMMETRIC:
20877e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
2099a4540c5SBarry Smith   case MAT_SYMMETRY_ETERNAL:
210e32f2f54SBarry Smith     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix must be symmetric");
211290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s not relevent\n",MatOptions[op]);CHKERRQ(ierr);
212290bbb0aSBarry Smith     break;
213941593c8SHong Zhang   case MAT_IGNORE_LOWER_TRIANGULAR:
2144e0d8c25SBarry Smith     a->ignore_ltriangular = flg;
215941593c8SHong Zhang     break;
216941593c8SHong Zhang   case MAT_ERROR_LOWER_TRIANGULAR:
2174e0d8c25SBarry Smith     a->ignore_ltriangular = flg;
21877e54ba9SKris Buschelman     break;
219f5edf698SHong Zhang   case MAT_GETROW_UPPERTRIANGULAR:
2204e0d8c25SBarry Smith     a->getrow_utriangular = flg;
221f5edf698SHong Zhang     break;
2224d9d31abSKris Buschelman   default:
223e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
22449b5e25fSSatish Balay   }
22549b5e25fSSatish Balay   PetscFunctionReturn(0);
22649b5e25fSSatish Balay }
22749b5e25fSSatish Balay 
2284a2ae208SSatish Balay #undef __FUNCT__
2294a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqSBAIJ"
23013f74950SBarry Smith PetscErrorCode MatGetRow_SeqSBAIJ(Mat A,PetscInt row,PetscInt *ncols,PetscInt **cols,PetscScalar **v)
23149b5e25fSSatish Balay {
23249b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
2336849ba73SBarry Smith   PetscErrorCode ierr;
23413f74950SBarry Smith   PetscInt       itmp,i,j,k,M,*ai,*aj,bs,bn,bp,*cols_i,bs2;
23549b5e25fSSatish Balay   MatScalar      *aa,*aa_i;
23687828ca2SBarry Smith   PetscScalar    *v_i;
23749b5e25fSSatish Balay 
23849b5e25fSSatish Balay   PetscFunctionBegin;
239e32f2f54SBarry 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()");
240f5edf698SHong Zhang   /* Get the upper triangular part of the row */
241d0f46423SBarry Smith   bs  = A->rmap->bs;
24249b5e25fSSatish Balay   ai  = a->i;
24349b5e25fSSatish Balay   aj  = a->j;
24449b5e25fSSatish Balay   aa  = a->a;
24549b5e25fSSatish Balay   bs2 = a->bs2;
24649b5e25fSSatish Balay 
247e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Row %D out of range", row);
24849b5e25fSSatish Balay 
24949b5e25fSSatish Balay   bn  = row/bs;   /* Block number */
25049b5e25fSSatish Balay   bp  = row % bs; /* Block position */
25149b5e25fSSatish Balay   M   = ai[bn+1] - ai[bn];
25249b5e25fSSatish Balay   *ncols = bs*M;
25349b5e25fSSatish Balay 
25449b5e25fSSatish Balay   if (v) {
25549b5e25fSSatish Balay     *v = 0;
25649b5e25fSSatish Balay     if (*ncols) {
25787828ca2SBarry Smith       ierr = PetscMalloc((*ncols+row)*sizeof(PetscScalar),v);CHKERRQ(ierr);
25849b5e25fSSatish Balay       for (i=0; i<M; i++) { /* for each block in the block row */
25949b5e25fSSatish Balay         v_i  = *v + i*bs;
26049b5e25fSSatish Balay         aa_i = aa + bs2*(ai[bn] + i);
26149b5e25fSSatish Balay         for (j=bp,k=0; j<bs2; j+=bs,k++) {v_i[k] = aa_i[j];}
26249b5e25fSSatish Balay       }
26349b5e25fSSatish Balay     }
26449b5e25fSSatish Balay   }
26549b5e25fSSatish Balay 
26649b5e25fSSatish Balay   if (cols) {
26749b5e25fSSatish Balay     *cols = 0;
26849b5e25fSSatish Balay     if (*ncols) {
26913f74950SBarry Smith       ierr = PetscMalloc((*ncols+row)*sizeof(PetscInt),cols);CHKERRQ(ierr);
27049b5e25fSSatish Balay       for (i=0; i<M; i++) { /* for each block in the block row */
27149b5e25fSSatish Balay         cols_i = *cols + i*bs;
27249b5e25fSSatish Balay         itmp  = bs*aj[ai[bn] + i];
27349b5e25fSSatish Balay         for (j=0; j<bs; j++) {cols_i[j] = itmp++;}
27449b5e25fSSatish Balay       }
27549b5e25fSSatish Balay     }
27649b5e25fSSatish Balay   }
27749b5e25fSSatish Balay 
27849b5e25fSSatish Balay   /*search column A(0:row-1,row) (=A(row,0:row-1)). Could be expensive! */
2795ddb2528SHong Zhang   /* this segment is currently removed, so only entries in the upper triangle are obtained */
2805ddb2528SHong Zhang #ifdef column_search
28149b5e25fSSatish Balay   v_i    = *v    + M*bs;
28249b5e25fSSatish Balay   cols_i = *cols + M*bs;
28349b5e25fSSatish Balay   for (i=0; i<bn; i++){ /* for each block row */
28449b5e25fSSatish Balay     M = ai[i+1] - ai[i];
28549b5e25fSSatish Balay     for (j=0; j<M; j++){
28649b5e25fSSatish Balay       itmp = aj[ai[i] + j];    /* block column value */
28749b5e25fSSatish Balay       if (itmp == bn){
28849b5e25fSSatish Balay         aa_i   = aa    + bs2*(ai[i] + j) + bs*bp;
28949b5e25fSSatish Balay         for (k=0; k<bs; k++) {
29049b5e25fSSatish Balay           *cols_i++ = i*bs+k;
29149b5e25fSSatish Balay           *v_i++    = aa_i[k];
29249b5e25fSSatish Balay         }
29349b5e25fSSatish Balay         *ncols += bs;
29449b5e25fSSatish Balay         break;
29549b5e25fSSatish Balay       }
29649b5e25fSSatish Balay     }
29749b5e25fSSatish Balay   }
2985ddb2528SHong Zhang #endif
29949b5e25fSSatish Balay   PetscFunctionReturn(0);
30049b5e25fSSatish Balay }
30149b5e25fSSatish Balay 
3024a2ae208SSatish Balay #undef __FUNCT__
3034a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqSBAIJ"
30413f74950SBarry Smith PetscErrorCode MatRestoreRow_SeqSBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
30549b5e25fSSatish Balay {
306dfbe8321SBarry Smith   PetscErrorCode ierr;
30749b5e25fSSatish Balay 
30849b5e25fSSatish Balay   PetscFunctionBegin;
30905b42c5fSBarry Smith   if (idx) {ierr = PetscFree(*idx);CHKERRQ(ierr);}
31005b42c5fSBarry Smith   if (v)   {ierr = PetscFree(*v);CHKERRQ(ierr);}
31149b5e25fSSatish Balay   PetscFunctionReturn(0);
31249b5e25fSSatish Balay }
31349b5e25fSSatish Balay 
3144a2ae208SSatish Balay #undef __FUNCT__
315f5edf698SHong Zhang #define __FUNCT__ "MatGetRowUpperTriangular_SeqSBAIJ"
316f5edf698SHong Zhang PetscErrorCode MatGetRowUpperTriangular_SeqSBAIJ(Mat A)
317f5edf698SHong Zhang {
318f5edf698SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
319f5edf698SHong Zhang 
320f5edf698SHong Zhang   PetscFunctionBegin;
321f5edf698SHong Zhang   a->getrow_utriangular = PETSC_TRUE;
322f5edf698SHong Zhang   PetscFunctionReturn(0);
323f5edf698SHong Zhang }
324f5edf698SHong Zhang #undef __FUNCT__
325f5edf698SHong Zhang #define __FUNCT__ "MatRestoreRowUpperTriangular_SeqSBAIJ"
326f5edf698SHong Zhang PetscErrorCode MatRestoreRowUpperTriangular_SeqSBAIJ(Mat A)
327f5edf698SHong Zhang {
328f5edf698SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
329f5edf698SHong Zhang 
330f5edf698SHong Zhang   PetscFunctionBegin;
331f5edf698SHong Zhang   a->getrow_utriangular = PETSC_FALSE;
332f5edf698SHong Zhang   PetscFunctionReturn(0);
333f5edf698SHong Zhang }
334f5edf698SHong Zhang 
335f5edf698SHong Zhang #undef __FUNCT__
3364a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqSBAIJ"
337fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqSBAIJ(Mat A,MatReuse reuse,Mat *B)
33849b5e25fSSatish Balay {
339dfbe8321SBarry Smith   PetscErrorCode ierr;
34049b5e25fSSatish Balay   PetscFunctionBegin;
341815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
342999d9058SBarry Smith     ierr = MatDuplicate(A,MAT_COPY_VALUES,B);CHKERRQ(ierr);
343fc4dec0aSBarry Smith   }
3448115998fSBarry Smith   PetscFunctionReturn(0);
34549b5e25fSSatish Balay }
34649b5e25fSSatish Balay 
3474a2ae208SSatish Balay #undef __FUNCT__
3484a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_ASCII"
3496849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_ASCII(Mat A,PetscViewer viewer)
35049b5e25fSSatish Balay {
35149b5e25fSSatish Balay   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
352dfbe8321SBarry Smith   PetscErrorCode    ierr;
353d0f46423SBarry Smith   PetscInt          i,j,bs = A->rmap->bs,k,l,bs2=a->bs2;
354f3ef73ceSBarry Smith   PetscViewerFormat format;
35549b5e25fSSatish Balay 
35649b5e25fSSatish Balay   PetscFunctionBegin;
357b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
358456192e2SBarry Smith   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
35977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);CHKERRQ(ierr);
360fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
361d2507d54SMatthew Knepley     Mat aij;
362d5f3da31SBarry Smith     if (A->factortype && bs>1){
36370d5e725SHong Zhang       ierr = PetscPrintf(PETSC_COMM_SELF,"Warning: matrix is factored with bs>1. MatView() with PETSC_VIEWER_ASCII_MATLAB is not supported and ignored!\n");CHKERRQ(ierr);
36470d5e725SHong Zhang       PetscFunctionReturn(0);
36570d5e725SHong Zhang     }
366c9f458caSMatthew Knepley     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&aij);CHKERRQ(ierr);
367c9f458caSMatthew Knepley     ierr = MatView(aij,viewer);CHKERRQ(ierr);
3686bf464f9SBarry Smith     ierr = MatDestroy(&aij);CHKERRQ(ierr);
369fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
370d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
37149b5e25fSSatish Balay     for (i=0; i<a->mbs; i++) {
37249b5e25fSSatish Balay       for (j=0; j<bs; j++) {
37377431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
37449b5e25fSSatish Balay         for (k=a->i[i]; k<a->i[i+1]; k++) {
37549b5e25fSSatish Balay           for (l=0; l<bs; l++) {
37649b5e25fSSatish Balay #if defined(PETSC_USE_COMPLEX)
37749b5e25fSSatish Balay             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
378a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",bs*a->j[k]+l,
37949b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
38049b5e25fSSatish Balay             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
381a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",bs*a->j[k]+l,
38249b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),-PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
38349b5e25fSSatish Balay             } else if (PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
384a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
38549b5e25fSSatish Balay             }
38649b5e25fSSatish Balay #else
38749b5e25fSSatish Balay             if (a->a[bs2*k + l*bs + j] != 0.0) {
388a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
38949b5e25fSSatish Balay             }
39049b5e25fSSatish Balay #endif
39149b5e25fSSatish Balay           }
39249b5e25fSSatish Balay         }
393b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
39449b5e25fSSatish Balay       }
39549b5e25fSSatish Balay     }
396d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
397c1490034SHong Zhang   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
398c1490034SHong Zhang      PetscFunctionReturn(0);
39949b5e25fSSatish Balay   } else {
400d5f3da31SBarry Smith     if (A->factortype && bs>1){
4018608aa04SHong Zhang       ierr = PetscPrintf(PETSC_COMM_SELF,"Warning: matrix is factored. MatView_SeqSBAIJ_ASCII() may not display complete or logically correct entries!\n");CHKERRQ(ierr);
4028608aa04SHong Zhang     }
403d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4047566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
40549b5e25fSSatish Balay     for (i=0; i<a->mbs; i++) {
40649b5e25fSSatish Balay       for (j=0; j<bs; j++) {
40777431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
40849b5e25fSSatish Balay         for (k=a->i[i]; k<a->i[i+1]; k++) {
40949b5e25fSSatish Balay           for (l=0; l<bs; l++) {
41049b5e25fSSatish Balay #if defined(PETSC_USE_COMPLEX)
41149b5e25fSSatish Balay             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0) {
412a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",bs*a->j[k]+l,
41349b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
41449b5e25fSSatish Balay             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0) {
415a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",bs*a->j[k]+l,
41649b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),-PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
41749b5e25fSSatish Balay             } else {
418a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
41949b5e25fSSatish Balay             }
42049b5e25fSSatish Balay #else
421e9f7bc9eSHong Zhang             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
42249b5e25fSSatish Balay #endif
42349b5e25fSSatish Balay           }
42449b5e25fSSatish Balay         }
425b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
42649b5e25fSSatish Balay       }
42749b5e25fSSatish Balay     }
428d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
42949b5e25fSSatish Balay   }
430b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
43149b5e25fSSatish Balay   PetscFunctionReturn(0);
43249b5e25fSSatish Balay }
43349b5e25fSSatish Balay 
4344a2ae208SSatish Balay #undef __FUNCT__
4354a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_Draw_Zoom"
4366849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
43749b5e25fSSatish Balay {
43849b5e25fSSatish Balay   Mat            A = (Mat) Aa;
43949b5e25fSSatish Balay   Mat_SeqSBAIJ   *a=(Mat_SeqSBAIJ*)A->data;
4406849ba73SBarry Smith   PetscErrorCode ierr;
441d0f46423SBarry Smith   PetscInt       row,i,j,k,l,mbs=a->mbs,color,bs=A->rmap->bs,bs2=a->bs2;
44213f74950SBarry Smith   PetscMPIInt    rank;
44349b5e25fSSatish Balay   PetscReal      xl,yl,xr,yr,x_l,x_r,y_l,y_r;
44449b5e25fSSatish Balay   MatScalar      *aa;
44549b5e25fSSatish Balay   MPI_Comm       comm;
446b0a32e0cSBarry Smith   PetscViewer    viewer;
44749b5e25fSSatish Balay 
44849b5e25fSSatish Balay   PetscFunctionBegin;
44949b5e25fSSatish Balay   /*
45049b5e25fSSatish Balay     This is nasty. If this is called from an originally parallel matrix
45149b5e25fSSatish Balay     then all processes call this,but only the first has the matrix so the
45249b5e25fSSatish Balay     rest should return immediately.
45349b5e25fSSatish Balay   */
45449b5e25fSSatish Balay   ierr = PetscObjectGetComm((PetscObject)draw,&comm);CHKERRQ(ierr);
45549b5e25fSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
45649b5e25fSSatish Balay   if (rank) PetscFunctionReturn(0);
45749b5e25fSSatish Balay 
45849b5e25fSSatish Balay   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
45949b5e25fSSatish Balay 
460b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
461b0a32e0cSBarry Smith   PetscDrawString(draw, .3*(xl+xr), .3*(yl+yr), PETSC_DRAW_BLACK, "symmetric");
46249b5e25fSSatish Balay 
46349b5e25fSSatish Balay   /* loop over matrix elements drawing boxes */
464b0a32e0cSBarry Smith   color = PETSC_DRAW_BLUE;
46549b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
46649b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
467d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
46849b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
46949b5e25fSSatish Balay       aa = a->a + j*bs2;
47049b5e25fSSatish Balay       for (k=0; k<bs; k++) {
47149b5e25fSSatish Balay         for (l=0; l<bs; l++) {
47249b5e25fSSatish Balay           if (PetscRealPart(*aa++) >=  0.) continue;
473b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
47449b5e25fSSatish Balay         }
47549b5e25fSSatish Balay       }
47649b5e25fSSatish Balay     }
47749b5e25fSSatish Balay   }
478b0a32e0cSBarry Smith   color = PETSC_DRAW_CYAN;
47949b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
48049b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
481d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
48249b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
48349b5e25fSSatish Balay       aa = a->a + j*bs2;
48449b5e25fSSatish Balay       for (k=0; k<bs; k++) {
48549b5e25fSSatish Balay         for (l=0; l<bs; l++) {
48649b5e25fSSatish Balay           if (PetscRealPart(*aa++) != 0.) continue;
487b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
48849b5e25fSSatish Balay         }
48949b5e25fSSatish Balay       }
49049b5e25fSSatish Balay     }
49149b5e25fSSatish Balay   }
49249b5e25fSSatish Balay 
493b0a32e0cSBarry Smith   color = PETSC_DRAW_RED;
49449b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
49549b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
496d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
49749b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
49849b5e25fSSatish Balay       aa = a->a + j*bs2;
49949b5e25fSSatish Balay       for (k=0; k<bs; k++) {
50049b5e25fSSatish Balay         for (l=0; l<bs; l++) {
50149b5e25fSSatish Balay           if (PetscRealPart(*aa++) <= 0.) continue;
502b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
50349b5e25fSSatish Balay         }
50449b5e25fSSatish Balay       }
50549b5e25fSSatish Balay     }
50649b5e25fSSatish Balay   }
50749b5e25fSSatish Balay   PetscFunctionReturn(0);
50849b5e25fSSatish Balay }
50949b5e25fSSatish Balay 
5104a2ae208SSatish Balay #undef __FUNCT__
5114a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_Draw"
5126849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_Draw(Mat A,PetscViewer viewer)
51349b5e25fSSatish Balay {
514dfbe8321SBarry Smith   PetscErrorCode ierr;
51549b5e25fSSatish Balay   PetscReal      xl,yl,xr,yr,w,h;
516b0a32e0cSBarry Smith   PetscDraw      draw;
517ace3abfcSBarry Smith   PetscBool      isnull;
51849b5e25fSSatish Balay 
51949b5e25fSSatish Balay   PetscFunctionBegin;
520b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
521b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
52249b5e25fSSatish Balay 
52349b5e25fSSatish Balay   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
524d0f46423SBarry Smith   xr  = A->rmap->N; yr = A->rmap->N; h = yr/10.0; w = xr/10.0;
52549b5e25fSSatish Balay   xr += w;    yr += h;  xl = -w;     yl = -h;
526b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
527b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqSBAIJ_Draw_Zoom,A);CHKERRQ(ierr);
52849b5e25fSSatish Balay   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
52949b5e25fSSatish Balay   PetscFunctionReturn(0);
53049b5e25fSSatish Balay }
53149b5e25fSSatish Balay 
5324a2ae208SSatish Balay #undef __FUNCT__
5334a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ"
534dfbe8321SBarry Smith PetscErrorCode MatView_SeqSBAIJ(Mat A,PetscViewer viewer)
53549b5e25fSSatish Balay {
536dfbe8321SBarry Smith   PetscErrorCode ierr;
537ace3abfcSBarry Smith   PetscBool      iascii,isdraw;
53808917f38SBarry Smith   FILE           *file = 0;
53949b5e25fSSatish Balay 
54049b5e25fSSatish Balay   PetscFunctionBegin;
5412692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
5422692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
54332077d6dSBarry Smith   if (iascii){
54449b5e25fSSatish Balay     ierr = MatView_SeqSBAIJ_ASCII(A,viewer);CHKERRQ(ierr);
54549b5e25fSSatish Balay   } else if (isdraw) {
54649b5e25fSSatish Balay     ierr = MatView_SeqSBAIJ_Draw(A,viewer);CHKERRQ(ierr);
54749b5e25fSSatish Balay   } else {
548a5e6ed63SBarry Smith     Mat B;
549ceb03754SKris Buschelman     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&B);CHKERRQ(ierr);
550a5e6ed63SBarry Smith     ierr = MatView(B,viewer);CHKERRQ(ierr);
5516bf464f9SBarry Smith     ierr = MatDestroy(&B);CHKERRQ(ierr);
55208917f38SBarry Smith     ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
55308917f38SBarry Smith     if (file) {
55408917f38SBarry Smith       fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
55508917f38SBarry Smith     }
55649b5e25fSSatish Balay   }
55749b5e25fSSatish Balay   PetscFunctionReturn(0);
55849b5e25fSSatish Balay }
55949b5e25fSSatish Balay 
56049b5e25fSSatish Balay 
5614a2ae208SSatish Balay #undef __FUNCT__
5624a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqSBAIJ"
56313f74950SBarry Smith PetscErrorCode MatGetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
56449b5e25fSSatish Balay {
565045c9aa0SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
56613f74950SBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
56713f74950SBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
568d0f46423SBarry Smith   PetscInt     brow,bcol,ridx,cidx,bs=A->rmap->bs,bs2=a->bs2;
56997e567efSBarry Smith   MatScalar    *ap,*aa = a->a;
57049b5e25fSSatish Balay 
57149b5e25fSSatish Balay   PetscFunctionBegin;
57249b5e25fSSatish Balay   for (k=0; k<m; k++) { /* loop over rows */
57349b5e25fSSatish Balay     row  = im[k]; brow = row/bs;
574e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
575e32f2f54SBarry 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);
57649b5e25fSSatish Balay     rp   = aj + ai[brow] ; ap = aa + bs2*ai[brow] ;
57749b5e25fSSatish Balay     nrow = ailen[brow];
57849b5e25fSSatish Balay     for (l=0; l<n; l++) { /* loop over columns */
579e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
580e32f2f54SBarry 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);
58149b5e25fSSatish Balay       col  = in[l] ;
58249b5e25fSSatish Balay       bcol = col/bs;
58349b5e25fSSatish Balay       cidx = col%bs;
58449b5e25fSSatish Balay       ridx = row%bs;
58549b5e25fSSatish Balay       high = nrow;
58649b5e25fSSatish Balay       low  = 0; /* assume unsorted */
58749b5e25fSSatish Balay       while (high-low > 5) {
58849b5e25fSSatish Balay         t = (low+high)/2;
58949b5e25fSSatish Balay         if (rp[t] > bcol) high = t;
59049b5e25fSSatish Balay         else             low  = t;
59149b5e25fSSatish Balay       }
59249b5e25fSSatish Balay       for (i=low; i<high; i++) {
59349b5e25fSSatish Balay         if (rp[i] > bcol) break;
59449b5e25fSSatish Balay         if (rp[i] == bcol) {
59549b5e25fSSatish Balay           *v++ = ap[bs2*i+bs*cidx+ridx];
59649b5e25fSSatish Balay           goto finished;
59749b5e25fSSatish Balay         }
59849b5e25fSSatish Balay       }
59997e567efSBarry Smith       *v++ = 0.0;
60049b5e25fSSatish Balay       finished:;
60149b5e25fSSatish Balay     }
60249b5e25fSSatish Balay   }
60349b5e25fSSatish Balay   PetscFunctionReturn(0);
60449b5e25fSSatish Balay }
60549b5e25fSSatish Balay 
60649b5e25fSSatish Balay 
6074a2ae208SSatish Balay #undef __FUNCT__
6084a2ae208SSatish Balay #define __FUNCT__ "MatSetValuesBlocked_SeqSBAIJ"
60913f74950SBarry Smith PetscErrorCode MatSetValuesBlocked_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
61049b5e25fSSatish Balay {
6110880e062SHong Zhang   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
6126849ba73SBarry Smith   PetscErrorCode    ierr;
613e2ee6c50SBarry Smith   PetscInt          *rp,k,low,high,t,ii,jj,row,nrow,i,col,l,rmax,N,lastcol = -1;
61413f74950SBarry Smith   PetscInt          *imax=a->imax,*ai=a->i,*ailen=a->ilen;
615d0f46423SBarry Smith   PetscInt          *aj=a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs,stepval;
616ace3abfcSBarry Smith   PetscBool         roworiented=a->roworiented;
617dd6ea824SBarry Smith   const PetscScalar *value = v;
618f15d580aSBarry Smith   MatScalar         *ap,*aa = a->a,*bap;
6190880e062SHong Zhang 
62049b5e25fSSatish Balay   PetscFunctionBegin;
6210880e062SHong Zhang   if (roworiented) {
6220880e062SHong Zhang     stepval = (n-1)*bs;
6230880e062SHong Zhang   } else {
6240880e062SHong Zhang     stepval = (m-1)*bs;
6250880e062SHong Zhang   }
6260880e062SHong Zhang   for (k=0; k<m; k++) { /* loop over added rows */
6270880e062SHong Zhang     row  = im[k];
6280880e062SHong Zhang     if (row < 0) continue;
6292515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
630e32f2f54SBarry Smith     if (row >= a->mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,a->mbs-1);
6310880e062SHong Zhang #endif
6320880e062SHong Zhang     rp   = aj + ai[row];
6330880e062SHong Zhang     ap   = aa + bs2*ai[row];
6340880e062SHong Zhang     rmax = imax[row];
6350880e062SHong Zhang     nrow = ailen[row];
6360880e062SHong Zhang     low  = 0;
637818f2c47SBarry Smith     high = nrow;
6380880e062SHong Zhang     for (l=0; l<n; l++) { /* loop over added columns */
6390880e062SHong Zhang       if (in[l] < 0) continue;
6400880e062SHong Zhang       col = in[l];
6412515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
642e32f2f54SBarry Smith       if (col >= a->nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",col,a->nbs-1);
643b1823623SSatish Balay #endif
644b98bf0e1SJed Brown       if (col < row) {
645b98bf0e1SJed Brown         if (a->ignore_ltriangular) {
646b98bf0e1SJed Brown           continue; /* ignore lower triangular block */
647b98bf0e1SJed Brown         } else {
648e32f2f54SBarry 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)");
649b98bf0e1SJed Brown         }
650b98bf0e1SJed Brown       }
6510880e062SHong Zhang       if (roworiented) {
6520880e062SHong Zhang         value = v + k*(stepval+bs)*bs + l*bs;
6530880e062SHong Zhang       } else {
6540880e062SHong Zhang         value = v + l*(stepval+bs)*bs + k*bs;
6550880e062SHong Zhang       }
6567cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
657e2ee6c50SBarry Smith       lastcol = col;
6580880e062SHong Zhang       while (high-low > 7) {
6590880e062SHong Zhang         t = (low+high)/2;
6600880e062SHong Zhang         if (rp[t] > col) high = t;
6610880e062SHong Zhang         else             low  = t;
6620880e062SHong Zhang       }
6630880e062SHong Zhang       for (i=low; i<high; i++) {
6640880e062SHong Zhang         if (rp[i] > col) break;
6650880e062SHong Zhang         if (rp[i] == col) {
6660880e062SHong Zhang           bap  = ap +  bs2*i;
6670880e062SHong Zhang           if (roworiented) {
6680880e062SHong Zhang             if (is == ADD_VALUES) {
6690880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6700880e062SHong Zhang                 for (jj=ii; jj<bs2; jj+=bs) {
6710880e062SHong Zhang                   bap[jj] += *value++;
6720880e062SHong Zhang                 }
6730880e062SHong Zhang               }
6740880e062SHong Zhang             } else {
6750880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6760880e062SHong Zhang                 for (jj=ii; jj<bs2; jj+=bs) {
6770880e062SHong Zhang                   bap[jj] = *value++;
6780880e062SHong Zhang                 }
6790880e062SHong Zhang                }
6800880e062SHong Zhang             }
6810880e062SHong Zhang           } else {
6820880e062SHong Zhang             if (is == ADD_VALUES) {
6830880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6840880e062SHong Zhang                 for (jj=0; jj<bs; jj++) {
6850880e062SHong Zhang                   *bap++ += *value++;
6860880e062SHong Zhang                 }
6870880e062SHong Zhang               }
6880880e062SHong Zhang             } else {
6890880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6900880e062SHong Zhang                 for (jj=0; jj<bs; jj++) {
6910880e062SHong Zhang                   *bap++  = *value++;
6920880e062SHong Zhang                 }
6930880e062SHong Zhang               }
6940880e062SHong Zhang             }
6950880e062SHong Zhang           }
6960880e062SHong Zhang           goto noinsert2;
6970880e062SHong Zhang         }
6980880e062SHong Zhang       }
6990880e062SHong Zhang       if (nonew == 1) goto noinsert2;
700e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
701fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
702c03d1d03SSatish Balay       N = nrow++ - 1; high++;
7030880e062SHong Zhang       /* shift up all the later entries in this row */
7040880e062SHong Zhang       for (ii=N; ii>=i; ii--) {
7050880e062SHong Zhang         rp[ii+1] = rp[ii];
7060880e062SHong Zhang         ierr = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
7070880e062SHong Zhang       }
7080880e062SHong Zhang       if (N >= i) {
7090880e062SHong Zhang         ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
7100880e062SHong Zhang       }
7110880e062SHong Zhang       rp[i] = col;
7120880e062SHong Zhang       bap   = ap +  bs2*i;
7130880e062SHong Zhang       if (roworiented) {
7140880e062SHong Zhang         for (ii=0; ii<bs; ii++,value+=stepval) {
7150880e062SHong Zhang           for (jj=ii; jj<bs2; jj+=bs) {
7160880e062SHong Zhang             bap[jj] = *value++;
7170880e062SHong Zhang           }
7180880e062SHong Zhang         }
7190880e062SHong Zhang       } else {
7200880e062SHong Zhang         for (ii=0; ii<bs; ii++,value+=stepval) {
7210880e062SHong Zhang           for (jj=0; jj<bs; jj++) {
7220880e062SHong Zhang             *bap++  = *value++;
7230880e062SHong Zhang           }
7240880e062SHong Zhang         }
7250880e062SHong Zhang        }
7260880e062SHong Zhang     noinsert2:;
7270880e062SHong Zhang       low = i;
7280880e062SHong Zhang     }
7290880e062SHong Zhang     ailen[row] = nrow;
7300880e062SHong Zhang   }
7310880e062SHong Zhang    PetscFunctionReturn(0);
73249b5e25fSSatish Balay }
73349b5e25fSSatish Balay 
73464831d72SBarry Smith /*
73564831d72SBarry Smith     This is not yet used
73664831d72SBarry Smith */
7374a2ae208SSatish Balay #undef __FUNCT__
7384108e4d5SBarry Smith #define __FUNCT__ "MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode"
7394108e4d5SBarry Smith PetscErrorCode MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode(Mat A)
7400def2e27SBarry Smith {
7410def2e27SBarry Smith   Mat_SeqSBAIJ    *a = (Mat_SeqSBAIJ*)A->data;
7420def2e27SBarry Smith   PetscErrorCode  ierr;
7430def2e27SBarry Smith   const PetscInt  *ai = a->i, *aj = a->j,*cols;
7440def2e27SBarry Smith   PetscInt        i = 0,j,blk_size,m = A->rmap->n,node_count = 0,nzx,nzy,*ns,row,nz,cnt,cnt2,*counts;
745ace3abfcSBarry Smith   PetscBool       flag;
7460def2e27SBarry Smith 
7470def2e27SBarry Smith   PetscFunctionBegin;
7480def2e27SBarry Smith   ierr = PetscMalloc(m*sizeof(PetscInt),&ns);CHKERRQ(ierr);
7490def2e27SBarry Smith   while (i < m){
7500def2e27SBarry Smith     nzx = ai[i+1] - ai[i];       /* Number of nonzeros */
7510def2e27SBarry Smith     /* Limits the number of elements in a node to 'a->inode.limit' */
7520def2e27SBarry Smith     for (j=i+1,blk_size=1; j<m && blk_size <a->inode.limit; ++j,++blk_size) {
7530def2e27SBarry Smith       nzy  = ai[j+1] - ai[j];
7540def2e27SBarry Smith       if (nzy != (nzx - j + i)) break;
7550def2e27SBarry Smith       ierr = PetscMemcmp(aj + ai[i] + j - i,aj + ai[j],nzy*sizeof(PetscInt),&flag);CHKERRQ(ierr);
7560def2e27SBarry Smith       if (!flag) break;
7570def2e27SBarry Smith     }
7580def2e27SBarry Smith     ns[node_count++] = blk_size;
7590def2e27SBarry Smith     i = j;
7600def2e27SBarry Smith   }
7610def2e27SBarry Smith   if (!a->inode.size && m && node_count > .9*m) {
7620def2e27SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
7630def2e27SBarry Smith     ierr = PetscInfo2(A,"Found %D nodes out of %D rows. Not using Inode routines\n",node_count,m);CHKERRQ(ierr);
7640def2e27SBarry Smith   } else {
7650def2e27SBarry Smith     a->inode.node_count = node_count;
7660def2e27SBarry Smith     ierr = PetscMalloc(node_count*sizeof(PetscInt),&a->inode.size);CHKERRQ(ierr);
767c760cd28SBarry Smith     ierr = PetscLogObjectMemory(A,node_count*sizeof(PetscInt));CHKERRQ(ierr);
7680def2e27SBarry Smith     ierr = PetscMemcpy(a->inode.size,ns,node_count*sizeof(PetscInt));
7690def2e27SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
7700def2e27SBarry Smith     ierr = PetscInfo3(A,"Found %D nodes of %D. Limit used: %D. Using Inode routines\n",node_count,m,a->inode.limit);CHKERRQ(ierr);
7710def2e27SBarry Smith 
7720def2e27SBarry Smith     /* count collections of adjacent columns in each inode */
7730def2e27SBarry Smith     row = 0;
7740def2e27SBarry Smith     cnt = 0;
7750def2e27SBarry Smith     for (i=0; i<node_count; i++) {
7760def2e27SBarry Smith       cols = aj + ai[row] + a->inode.size[i];
7770def2e27SBarry Smith       nz   = ai[row+1] - ai[row] - a->inode.size[i];
7780def2e27SBarry Smith       for (j=1; j<nz; j++) {
7790def2e27SBarry Smith         if (cols[j] != cols[j-1]+1) {
7800def2e27SBarry Smith           cnt++;
7810def2e27SBarry Smith         }
7820def2e27SBarry Smith       }
7830def2e27SBarry Smith       cnt++;
7840def2e27SBarry Smith       row += a->inode.size[i];
7850def2e27SBarry Smith     }
7860def2e27SBarry Smith     ierr = PetscMalloc(2*cnt*sizeof(PetscInt),&counts);CHKERRQ(ierr);
7870def2e27SBarry Smith     cnt = 0;
7880def2e27SBarry Smith     row = 0;
7890def2e27SBarry Smith     for (i=0; i<node_count; i++) {
7900def2e27SBarry Smith       cols          = aj + ai[row] + a->inode.size[i];
7910def2e27SBarry Smith 	  CHKMEMQ;
7920def2e27SBarry Smith       counts[2*cnt] = cols[0];
7930def2e27SBarry Smith 	  CHKMEMQ;
7940def2e27SBarry Smith       nz            = ai[row+1] - ai[row] - a->inode.size[i];
7950def2e27SBarry Smith       cnt2          = 1;
7960def2e27SBarry Smith       for (j=1; j<nz; j++) {
7970def2e27SBarry Smith         if (cols[j] != cols[j-1]+1) {
7980def2e27SBarry Smith 	  CHKMEMQ;
7990def2e27SBarry Smith           counts[2*(cnt++)+1] = cnt2;
8000def2e27SBarry Smith           counts[2*cnt]       = cols[j];
8010def2e27SBarry Smith 	  CHKMEMQ;
8020def2e27SBarry Smith           cnt2                = 1;
8030def2e27SBarry Smith         } else cnt2++;
8040def2e27SBarry Smith       }
8050def2e27SBarry Smith 	  CHKMEMQ;
8060def2e27SBarry Smith       counts[2*(cnt++)+1] = cnt2;
8070def2e27SBarry Smith 	  CHKMEMQ;
8080def2e27SBarry Smith       row += a->inode.size[i];
8090def2e27SBarry Smith     }
8100def2e27SBarry Smith     ierr = PetscIntView(2*cnt,counts,0);
8110def2e27SBarry Smith   }
81238702af4SBarry Smith   PetscFunctionReturn(0);
81338702af4SBarry Smith }
81438702af4SBarry Smith 
81538702af4SBarry Smith #undef __FUNCT__
8164a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqSBAIJ"
817dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqSBAIJ(Mat A,MatAssemblyType mode)
81849b5e25fSSatish Balay {
81949b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
8206849ba73SBarry Smith   PetscErrorCode ierr;
82113f74950SBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
822d0f46423SBarry Smith   PetscInt       m = A->rmap->N,*ip,N,*ailen = a->ilen;
82313f74950SBarry Smith   PetscInt       mbs = a->mbs,bs2 = a->bs2,rmax = 0;
82449b5e25fSSatish Balay   MatScalar      *aa = a->a,*ap;
82549b5e25fSSatish Balay 
82649b5e25fSSatish Balay   PetscFunctionBegin;
82749b5e25fSSatish Balay   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
82849b5e25fSSatish Balay 
82949b5e25fSSatish Balay   if (m) rmax = ailen[0];
83049b5e25fSSatish Balay   for (i=1; i<mbs; i++) {
83149b5e25fSSatish Balay     /* move each row back by the amount of empty slots (fshift) before it*/
83249b5e25fSSatish Balay     fshift += imax[i-1] - ailen[i-1];
83349b5e25fSSatish Balay      rmax   = PetscMax(rmax,ailen[i]);
83449b5e25fSSatish Balay      if (fshift) {
83549b5e25fSSatish Balay        ip = aj + ai[i]; ap = aa + bs2*ai[i];
83649b5e25fSSatish Balay        N = ailen[i];
83749b5e25fSSatish Balay        for (j=0; j<N; j++) {
83849b5e25fSSatish Balay          ip[j-fshift] = ip[j];
83949b5e25fSSatish Balay          ierr = PetscMemcpy(ap+(j-fshift)*bs2,ap+j*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
84049b5e25fSSatish Balay        }
84149b5e25fSSatish Balay      }
84249b5e25fSSatish Balay      ai[i] = ai[i-1] + ailen[i-1];
84349b5e25fSSatish Balay   }
84449b5e25fSSatish Balay   if (mbs) {
84549b5e25fSSatish Balay     fshift += imax[mbs-1] - ailen[mbs-1];
84649b5e25fSSatish Balay      ai[mbs] = ai[mbs-1] + ailen[mbs-1];
84749b5e25fSSatish Balay   }
84849b5e25fSSatish Balay   /* reset ilen and imax for each row */
84949b5e25fSSatish Balay   for (i=0; i<mbs; i++) {
85049b5e25fSSatish Balay     ailen[i] = imax[i] = ai[i+1] - ai[i];
85149b5e25fSSatish Balay   }
8526c6c5352SBarry Smith   a->nz = ai[mbs];
85349b5e25fSSatish Balay 
854b424e231SHong Zhang   /* diagonals may have moved, reset it */
855b424e231SHong Zhang   if (a->diag) {
8562ed38d0bSJed Brown     ierr = PetscMemcpy(a->diag,ai,mbs*sizeof(PetscInt));CHKERRQ(ierr);
85749b5e25fSSatish Balay   }
85828b2fa4aSMatthew Knepley   if (fshift && a->nounused == -1) {
859e32f2f54SBarry 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);
86028b2fa4aSMatthew Knepley   }
861d0f46423SBarry 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);
862ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues is %D\n",a->reallocs);CHKERRQ(ierr);
863ae15b995SBarry Smith   ierr = PetscInfo1(A,"Most nonzeros blocks in any row is %D\n",rmax);CHKERRQ(ierr);
8648e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
86549b5e25fSSatish Balay   a->reallocs          = 0;
86649b5e25fSSatish Balay   A->info.nz_unneeded  = (PetscReal)fshift*bs2;
867061b2667SBarry Smith   a->idiagvalid = PETSC_FALSE;
86838702af4SBarry Smith 
86938702af4SBarry Smith   if (A->cmap->n < 65536 && A->cmap->bs == 1) {
87038702af4SBarry Smith     if (!a->jshort) {
87138702af4SBarry Smith       ierr = PetscMalloc(a->i[A->rmap->n]*sizeof(unsigned short),&a->jshort);CHKERRQ(ierr);
872c760cd28SBarry Smith       ierr = PetscLogObjectMemory(A,a->i[A->rmap->n]*sizeof(unsigned short));CHKERRQ(ierr);
87338702af4SBarry Smith       for (i=0; i<a->i[A->rmap->n]; i++) a->jshort[i] = a->j[i];
87438702af4SBarry Smith       A->ops->mult  = MatMult_SeqSBAIJ_1_ushort;
87541f059aeSBarry Smith       A->ops->sor = MatSOR_SeqSBAIJ_ushort;
8764da8f245SBarry Smith       a->free_jshort = PETSC_TRUE;
87738702af4SBarry Smith     }
87838702af4SBarry Smith   }
87949b5e25fSSatish Balay   PetscFunctionReturn(0);
88049b5e25fSSatish Balay }
88149b5e25fSSatish Balay 
88249b5e25fSSatish Balay /*
88349b5e25fSSatish Balay    This function returns an array of flags which indicate the locations of contiguous
88449b5e25fSSatish Balay    blocks that should be zeroed. for eg: if bs = 3  and is = [0,1,2,3,5,6,7,8,9]
88549b5e25fSSatish Balay    then the resulting sizes = [3,1,1,3,1] correspondig to sets [(0,1,2),(3),(5),(6,7,8),(9)]
88649b5e25fSSatish Balay    Assume: sizes should be long enough to hold all the values.
88749b5e25fSSatish Balay */
8884a2ae208SSatish Balay #undef __FUNCT__
8894a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqSBAIJ_Check_Blocks"
89013f74950SBarry Smith PetscErrorCode MatZeroRows_SeqSBAIJ_Check_Blocks(PetscInt idx[],PetscInt n,PetscInt bs,PetscInt sizes[], PetscInt *bs_max)
89149b5e25fSSatish Balay {
89213f74950SBarry Smith   PetscInt   i,j,k,row;
893ace3abfcSBarry Smith   PetscBool  flg;
89449b5e25fSSatish Balay 
89549b5e25fSSatish Balay   PetscFunctionBegin;
89649b5e25fSSatish Balay    for (i=0,j=0; i<n; j++) {
89749b5e25fSSatish Balay      row = idx[i];
89849b5e25fSSatish Balay      if (row%bs!=0) { /* Not the begining of a block */
89949b5e25fSSatish Balay        sizes[j] = 1;
90049b5e25fSSatish Balay        i++;
90149b5e25fSSatish Balay      } else if (i+bs > n) { /* Beginning of a block, but complete block doesn't exist (at idx end) */
90249b5e25fSSatish Balay        sizes[j] = 1;         /* Also makes sure atleast 'bs' values exist for next else */
90349b5e25fSSatish Balay        i++;
90449b5e25fSSatish Balay      } else { /* Begining of the block, so check if the complete block exists */
90549b5e25fSSatish Balay        flg = PETSC_TRUE;
90649b5e25fSSatish Balay        for (k=1; k<bs; k++) {
90749b5e25fSSatish Balay          if (row+k != idx[i+k]) { /* break in the block */
90849b5e25fSSatish Balay            flg = PETSC_FALSE;
90949b5e25fSSatish Balay            break;
91049b5e25fSSatish Balay          }
91149b5e25fSSatish Balay        }
912abc0a331SBarry Smith        if (flg) { /* No break in the bs */
91349b5e25fSSatish Balay          sizes[j] = bs;
91449b5e25fSSatish Balay          i+= bs;
91549b5e25fSSatish Balay        } else {
91649b5e25fSSatish Balay          sizes[j] = 1;
91749b5e25fSSatish Balay          i++;
91849b5e25fSSatish Balay        }
91949b5e25fSSatish Balay      }
92049b5e25fSSatish Balay    }
92149b5e25fSSatish Balay    *bs_max = j;
92249b5e25fSSatish Balay    PetscFunctionReturn(0);
92349b5e25fSSatish Balay }
92449b5e25fSSatish Balay 
92549b5e25fSSatish Balay 
92649b5e25fSSatish Balay /* Only add/insert a(i,j) with i<=j (blocks).
92749b5e25fSSatish Balay    Any a(i,j) with i>j input by user is ingored.
92849b5e25fSSatish Balay */
92949b5e25fSSatish Balay 
9304a2ae208SSatish Balay #undef __FUNCT__
9314a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqSBAIJ"
93213f74950SBarry Smith PetscErrorCode MatSetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
93349b5e25fSSatish Balay {
93449b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
9356849ba73SBarry Smith   PetscErrorCode ierr;
936e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,lastcol = -1;
93713f74950SBarry Smith   PetscInt       *imax=a->imax,*ai=a->i,*ailen=a->ilen,roworiented=a->roworiented;
938d0f46423SBarry Smith   PetscInt       *aj=a->j,nonew=a->nonew,bs=A->rmap->bs,brow,bcol;
93913f74950SBarry Smith   PetscInt       ridx,cidx,bs2=a->bs2;
94049b5e25fSSatish Balay   MatScalar      *ap,value,*aa=a->a,*bap;
94149b5e25fSSatish Balay 
94249b5e25fSSatish Balay   PetscFunctionBegin;
94371fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
94449b5e25fSSatish Balay   for (k=0; k<m; k++) { /* loop over added rows */
94549b5e25fSSatish Balay     row  = im[k];       /* row number */
94649b5e25fSSatish Balay     brow = row/bs;      /* block row number */
94749b5e25fSSatish Balay     if (row < 0) continue;
9482515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
949e32f2f54SBarry 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);
95049b5e25fSSatish Balay #endif
95149b5e25fSSatish Balay     rp   = aj + ai[brow]; /*ptr to beginning of column value of the row block*/
95249b5e25fSSatish Balay     ap   = aa + bs2*ai[brow]; /*ptr to beginning of element value of the row block*/
95349b5e25fSSatish Balay     rmax = imax[brow];  /* maximum space allocated for this row */
95449b5e25fSSatish Balay     nrow = ailen[brow]; /* actual length of this row */
95549b5e25fSSatish Balay     low  = 0;
95649b5e25fSSatish Balay 
95749b5e25fSSatish Balay     for (l=0; l<n; l++) { /* loop over added columns */
95849b5e25fSSatish Balay       if (in[l] < 0) continue;
9592515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
960e32f2f54SBarry 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);
96149b5e25fSSatish Balay #endif
96249b5e25fSSatish Balay       col = in[l];
96349b5e25fSSatish Balay       bcol = col/bs;              /* block col number */
96449b5e25fSSatish Balay 
965941593c8SHong Zhang       if (brow > bcol) {
966941593c8SHong Zhang         if (a->ignore_ltriangular){
967941593c8SHong Zhang           continue; /* ignore lower triangular values */
968941593c8SHong Zhang         } else {
969e32f2f54SBarry 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)");
970941593c8SHong Zhang         }
971941593c8SHong Zhang       }
972f4989cb3SHong Zhang 
97349b5e25fSSatish Balay       ridx = row % bs; cidx = col % bs; /*row and col index inside the block */
9748549e402SHong Zhang       if ((brow==bcol && ridx<=cidx) || (brow<bcol)){
97549b5e25fSSatish Balay         /* element value a(k,l) */
97649b5e25fSSatish Balay         if (roworiented) {
97749b5e25fSSatish Balay           value = v[l + k*n];
97849b5e25fSSatish Balay         } else {
97949b5e25fSSatish Balay           value = v[k + l*m];
98049b5e25fSSatish Balay         }
98149b5e25fSSatish Balay 
98249b5e25fSSatish Balay         /* move pointer bap to a(k,l) quickly and add/insert value */
9837cd84e04SBarry Smith         if (col <= lastcol) low = 0; high = nrow;
984e2ee6c50SBarry Smith         lastcol = col;
98549b5e25fSSatish Balay         while (high-low > 7) {
98649b5e25fSSatish Balay           t = (low+high)/2;
98749b5e25fSSatish Balay           if (rp[t] > bcol) high = t;
98849b5e25fSSatish Balay           else              low  = t;
98949b5e25fSSatish Balay         }
99049b5e25fSSatish Balay         for (i=low; i<high; i++) {
99149b5e25fSSatish Balay           if (rp[i] > bcol) break;
99249b5e25fSSatish Balay           if (rp[i] == bcol) {
99349b5e25fSSatish Balay             bap  = ap +  bs2*i + bs*cidx + ridx;
99449b5e25fSSatish Balay             if (is == ADD_VALUES) *bap += value;
99549b5e25fSSatish Balay             else                  *bap  = value;
9968549e402SHong Zhang             /* for diag block, add/insert its symmetric element a(cidx,ridx) */
9978549e402SHong Zhang             if (brow == bcol && ridx < cidx){
9988549e402SHong Zhang               bap  = ap +  bs2*i + bs*ridx + cidx;
9998549e402SHong Zhang               if (is == ADD_VALUES) *bap += value;
10008549e402SHong Zhang               else                  *bap  = value;
10018549e402SHong Zhang             }
100249b5e25fSSatish Balay             goto noinsert1;
100349b5e25fSSatish Balay           }
100449b5e25fSSatish Balay         }
100549b5e25fSSatish Balay 
100649b5e25fSSatish Balay         if (nonew == 1) goto noinsert1;
1007e32f2f54SBarry Smith         if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
1008fef13f97SBarry Smith         MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
100949b5e25fSSatish Balay 
1010c03d1d03SSatish Balay         N = nrow++ - 1; high++;
101149b5e25fSSatish Balay         /* shift up all the later entries in this row */
101249b5e25fSSatish Balay         for (ii=N; ii>=i; ii--) {
101349b5e25fSSatish Balay           rp[ii+1] = rp[ii];
101449b5e25fSSatish Balay           ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
101549b5e25fSSatish Balay         }
101649b5e25fSSatish Balay         if (N>=i) {
101749b5e25fSSatish Balay           ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
101849b5e25fSSatish Balay         }
101949b5e25fSSatish Balay         rp[i]                      = bcol;
102049b5e25fSSatish Balay         ap[bs2*i + bs*cidx + ridx] = value;
102149b5e25fSSatish Balay       noinsert1:;
102249b5e25fSSatish Balay         low = i;
10238549e402SHong Zhang       }
102449b5e25fSSatish Balay     }   /* end of loop over added columns */
102549b5e25fSSatish Balay     ailen[brow] = nrow;
102649b5e25fSSatish Balay   }   /* end of loop over added rows */
102749b5e25fSSatish Balay   PetscFunctionReturn(0);
102849b5e25fSSatish Balay }
102949b5e25fSSatish Balay 
10304a2ae208SSatish Balay #undef __FUNCT__
10314d101231SSatish Balay #define __FUNCT__ "MatICCFactor_SeqSBAIJ"
10320481f469SBarry Smith PetscErrorCode MatICCFactor_SeqSBAIJ(Mat inA,IS row,const MatFactorInfo *info)
103349b5e25fSSatish Balay {
10344ccecd49SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)inA->data;
103549b5e25fSSatish Balay   Mat            outA;
1036dfbe8321SBarry Smith   PetscErrorCode ierr;
1037ace3abfcSBarry Smith   PetscBool      row_identity;
103849b5e25fSSatish Balay 
103949b5e25fSSatish Balay   PetscFunctionBegin;
1040e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 is supported for in-place icc");
1041c84f5b01SHong Zhang   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1042e32f2f54SBarry Smith   if (!row_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix reordering is not supported");
1043e32f2f54SBarry 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()! */
1044c84f5b01SHong Zhang 
104549b5e25fSSatish Balay   outA            = inA;
1046d5f3da31SBarry Smith   inA->factortype = MAT_FACTOR_ICC;
104749b5e25fSSatish Balay 
10481a3463dfSHong Zhang   ierr = MatMarkDiagonal_SeqSBAIJ(inA);CHKERRQ(ierr);
1049d595f711SHong Zhang   ierr = MatSeqSBAIJSetNumericFactorization_inplace(inA,row_identity);CHKERRQ(ierr);
105049b5e25fSSatish Balay 
1051c3122656SLisandro Dalcin   ierr   = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
10526bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
1053c84f5b01SHong Zhang   a->row = row;
1054c3122656SLisandro Dalcin   ierr   = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
10556bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
1056c84f5b01SHong Zhang   a->col = row;
1057c84f5b01SHong Zhang 
1058c84f5b01SHong Zhang   /* Create the invert permutation so that it can be used in MatCholeskyFactorNumeric() */
1059c84f5b01SHong Zhang   if (a->icol) {ierr = ISInvertPermutation(row,PETSC_DECIDE, &a->icol);CHKERRQ(ierr);}
106052e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
106149b5e25fSSatish Balay 
106249b5e25fSSatish Balay   if (!a->solve_work) {
1063d0f46423SBarry Smith     ierr = PetscMalloc((inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
1064d0f46423SBarry Smith     ierr = PetscLogObjectMemory(inA,(inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar));CHKERRQ(ierr);
106549b5e25fSSatish Balay   }
106649b5e25fSSatish Balay 
1067719d5645SBarry Smith   ierr = MatCholeskyFactorNumeric(outA,inA,info);CHKERRQ(ierr);
106849b5e25fSSatish Balay   PetscFunctionReturn(0);
106949b5e25fSSatish Balay }
1070950f1e5bSHong Zhang 
107149b5e25fSSatish Balay EXTERN_C_BEGIN
10724a2ae208SSatish Balay #undef __FUNCT__
10734a2ae208SSatish Balay #define __FUNCT__ "MatSeqSBAIJSetColumnIndices_SeqSBAIJ"
10747087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetColumnIndices_SeqSBAIJ(Mat mat,PetscInt *indices)
107549b5e25fSSatish Balay {
1076045c9aa0SHong Zhang   Mat_SeqSBAIJ *baij = (Mat_SeqSBAIJ *)mat->data;
107713f74950SBarry Smith   PetscInt     i,nz,n;
107849b5e25fSSatish Balay 
107949b5e25fSSatish Balay   PetscFunctionBegin;
10806c6c5352SBarry Smith   nz = baij->maxnz;
1081d0f46423SBarry Smith   n  = mat->cmap->n;
108249b5e25fSSatish Balay   for (i=0; i<nz; i++) {
108349b5e25fSSatish Balay     baij->j[i] = indices[i];
108449b5e25fSSatish Balay   }
10856c6c5352SBarry Smith    baij->nz = nz;
108649b5e25fSSatish Balay    for (i=0; i<n; i++) {
108749b5e25fSSatish Balay      baij->ilen[i] = baij->imax[i];
108849b5e25fSSatish Balay    }
108949b5e25fSSatish Balay    PetscFunctionReturn(0);
109049b5e25fSSatish Balay }
109149b5e25fSSatish Balay EXTERN_C_END
109249b5e25fSSatish Balay 
10934a2ae208SSatish Balay #undef __FUNCT__
10944a2ae208SSatish Balay #define __FUNCT__ "MatSeqSBAIJSetColumnIndices"
109549b5e25fSSatish Balay /*@
109619585528SSatish Balay   MatSeqSBAIJSetColumnIndices - Set the column indices for all the rows
109749b5e25fSSatish Balay   in the matrix.
109849b5e25fSSatish Balay 
109949b5e25fSSatish Balay   Input Parameters:
110019585528SSatish Balay   +  mat     - the SeqSBAIJ matrix
110149b5e25fSSatish Balay   -  indices - the column indices
110249b5e25fSSatish Balay 
110349b5e25fSSatish Balay   Level: advanced
110449b5e25fSSatish Balay 
110549b5e25fSSatish Balay   Notes:
110649b5e25fSSatish Balay   This can be called if you have precomputed the nonzero structure of the
110749b5e25fSSatish Balay   matrix and want to provide it to the matrix object to improve the performance
110849b5e25fSSatish Balay   of the MatSetValues() operation.
110949b5e25fSSatish Balay 
111049b5e25fSSatish Balay   You MUST have set the correct numbers of nonzeros per row in the call to
1111d1be2dadSMatthew Knepley   MatCreateSeqSBAIJ(), and the columns indices MUST be sorted.
111249b5e25fSSatish Balay 
1113ab9f2c04SSatish Balay   MUST be called before any calls to MatSetValues()
111449b5e25fSSatish Balay 
1115ab9f2c04SSatish Balay   .seealso: MatCreateSeqSBAIJ
111649b5e25fSSatish Balay @*/
11177087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetColumnIndices(Mat mat,PetscInt *indices)
111849b5e25fSSatish Balay {
11194ac538c5SBarry Smith   PetscErrorCode ierr;
112049b5e25fSSatish Balay 
112149b5e25fSSatish Balay   PetscFunctionBegin;
11220700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
11234482741eSBarry Smith   PetscValidPointer(indices,2);
11244ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqSBAIJSetColumnIndices_C",(Mat,PetscInt *),(mat,indices));CHKERRQ(ierr);
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);
11956bf464f9SBarry Smith       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"
1235ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqSBAIJ(Mat A,PetscReal tol,PetscBool  *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"
1244ace3abfcSBarry Smith PetscErrorCode MatIsStructurallySymmetric_SeqSBAIJ(Mat A,PetscBool  *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"
1253ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqSBAIJ(Mat A,PetscReal tol,PetscBool  *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 
12863bededecSBarry Smith #undef __FUNCT__
12873bededecSBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqSBAIJ"
12883bededecSBarry Smith PetscErrorCode MatZeroRowsColumns_SeqSBAIJ(Mat A,PetscInt is_n,const PetscInt is_idx[],PetscScalar diag,Vec x, Vec b)
12893bededecSBarry Smith {
12903bededecSBarry Smith   Mat_SeqSBAIJ      *baij=(Mat_SeqSBAIJ*)A->data;
12913bededecSBarry Smith   PetscErrorCode    ierr;
12923bededecSBarry Smith   PetscInt          i,j,k,count;
12933bededecSBarry Smith   PetscInt          bs=A->rmap->bs,bs2=baij->bs2,row,col;
12943bededecSBarry Smith   PetscScalar       zero = 0.0;
12953bededecSBarry Smith   MatScalar         *aa;
12963bededecSBarry Smith   const PetscScalar *xx;
12973bededecSBarry Smith   PetscScalar       *bb;
129856777dd2SBarry Smith   PetscBool         *zeroed,vecs = PETSC_FALSE;
12993bededecSBarry Smith 
13003bededecSBarry Smith   PetscFunctionBegin;
13013bededecSBarry Smith   /* fix right hand side if needed */
13023bededecSBarry Smith   if (x && b) {
13033bededecSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
13043bededecSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
130556777dd2SBarry Smith     vecs = PETSC_TRUE;
13063bededecSBarry Smith   }
13073bededecSBarry Smith   A->same_nonzero = PETSC_TRUE;
13083bededecSBarry Smith 
13093bededecSBarry Smith   /* zero the columns */
13103bededecSBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
13113bededecSBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
13123bededecSBarry Smith   for (i=0; i<is_n; i++) {
13133bededecSBarry Smith     if (is_idx[i] < 0 || is_idx[i] >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range",is_idx[i]);
13143bededecSBarry Smith     zeroed[is_idx[i]] = PETSC_TRUE;
13153bededecSBarry Smith   }
131656777dd2SBarry Smith   if (vecs) {
131756777dd2SBarry Smith     for (i=0; i<A->rmap->N; i++) {
131856777dd2SBarry Smith       row = i/bs;
131956777dd2SBarry Smith       for (j=baij->i[row]; j<baij->i[row+1]; j++) {
132056777dd2SBarry Smith 	for (k=0; k<bs; k++) {
132156777dd2SBarry Smith 	  col = bs*baij->j[j] + k;
132256777dd2SBarry Smith           if (col <= i) continue;
132356777dd2SBarry Smith 	  aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
132456777dd2SBarry Smith 	  if (!zeroed[i] && zeroed[col]) {
132556777dd2SBarry Smith 	    bb[i] -= aa[0]*xx[col];
132656777dd2SBarry Smith 	  }
132756777dd2SBarry Smith 	  if (zeroed[i] && !zeroed[col]) {
132856777dd2SBarry Smith 	    bb[col] -= aa[0]*xx[i];
132956777dd2SBarry Smith 	  }
133056777dd2SBarry Smith 	}
133156777dd2SBarry Smith       }
133256777dd2SBarry Smith     }
133356777dd2SBarry Smith     for (i=0; i<is_n; i++) {
133456777dd2SBarry Smith       bb[is_idx[i]] = diag*xx[is_idx[i]];
133556777dd2SBarry Smith     }
133656777dd2SBarry Smith   }
133756777dd2SBarry Smith 
13383bededecSBarry Smith   for (i=0; i<A->rmap->N; i++) {
13393bededecSBarry Smith     if (!zeroed[i]) {
13403bededecSBarry Smith       row = i/bs;
13413bededecSBarry Smith       for (j=baij->i[row]; j<baij->i[row+1]; j++) {
13423bededecSBarry Smith         for (k=0; k<bs; k++) {
13433bededecSBarry Smith           col = bs*baij->j[j] + k;
13443bededecSBarry Smith 	  if (zeroed[col]) {
13453bededecSBarry Smith 	    aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
13463bededecSBarry Smith             aa[0] = 0.0;
13473bededecSBarry Smith           }
13483bededecSBarry Smith         }
13493bededecSBarry Smith       }
13503bededecSBarry Smith     }
13513bededecSBarry Smith   }
13523bededecSBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
135356777dd2SBarry Smith   if (vecs) {
135456777dd2SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
135556777dd2SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
135656777dd2SBarry Smith   }
13573bededecSBarry Smith 
13583bededecSBarry Smith   /* zero the rows */
13593bededecSBarry Smith   for (i=0; i<is_n; i++) {
13603bededecSBarry Smith     row   = is_idx[i];
13613bededecSBarry Smith     count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
13623bededecSBarry Smith     aa    = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
13633bededecSBarry Smith     for (k=0; k<count; k++) {
13643bededecSBarry Smith       aa[0] =  zero;
13653bededecSBarry Smith       aa    += bs;
13663bededecSBarry Smith     }
13673bededecSBarry Smith     if (diag != 0.0) {
13683bededecSBarry Smith       ierr = (*A->ops->setvalues)(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr);
13693bededecSBarry Smith     }
13703bededecSBarry Smith   }
13713bededecSBarry Smith   ierr = MatAssemblyEnd_SeqSBAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13723bededecSBarry Smith   PetscFunctionReturn(0);
13733bededecSBarry Smith }
13743bededecSBarry Smith 
137549b5e25fSSatish Balay /* -------------------------------------------------------------------*/
137649b5e25fSSatish Balay static struct _MatOps MatOps_Values = {MatSetValues_SeqSBAIJ,
137749b5e25fSSatish Balay        MatGetRow_SeqSBAIJ,
137849b5e25fSSatish Balay        MatRestoreRow_SeqSBAIJ,
137949b5e25fSSatish Balay        MatMult_SeqSBAIJ_N,
138097304618SKris Buschelman /* 4*/ MatMultAdd_SeqSBAIJ_N,
1381431c96f7SBarry Smith        MatMult_SeqSBAIJ_N,       /* transpose versions are same as non-transpose versions */
1382e005ede5SBarry Smith        MatMultAdd_SeqSBAIJ_N,
1383db4efbfdSBarry Smith        0,
138449b5e25fSSatish Balay        0,
138549b5e25fSSatish Balay        0,
138697304618SKris Buschelman /*10*/ 0,
138749b5e25fSSatish Balay        0,
1388c078aec8SLisandro Dalcin        MatCholeskyFactor_SeqSBAIJ,
138941f059aeSBarry Smith        MatSOR_SeqSBAIJ,
139049b5e25fSSatish Balay        MatTranspose_SeqSBAIJ,
139197304618SKris Buschelman /*15*/ MatGetInfo_SeqSBAIJ,
139249b5e25fSSatish Balay        MatEqual_SeqSBAIJ,
139349b5e25fSSatish Balay        MatGetDiagonal_SeqSBAIJ,
139449b5e25fSSatish Balay        MatDiagonalScale_SeqSBAIJ,
139549b5e25fSSatish Balay        MatNorm_SeqSBAIJ,
139697304618SKris Buschelman /*20*/ 0,
139749b5e25fSSatish Balay        MatAssemblyEnd_SeqSBAIJ,
139849b5e25fSSatish Balay        MatSetOption_SeqSBAIJ,
139949b5e25fSSatish Balay        MatZeroEntries_SeqSBAIJ,
1400d519adbfSMatthew Knepley /*24*/ 0,
140149b5e25fSSatish Balay        0,
140249b5e25fSSatish Balay        0,
1403db4efbfdSBarry Smith        0,
1404db4efbfdSBarry Smith        0,
1405d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqSBAIJ,
1406c464158bSHong Zhang        0,
1407db4efbfdSBarry Smith        0,
1408a6ece127SHong Zhang        MatGetArray_SeqSBAIJ,
1409a6ece127SHong Zhang        MatRestoreArray_SeqSBAIJ,
1410d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqSBAIJ,
1411719d5645SBarry Smith        0,
1412719d5645SBarry Smith        0,
141349b5e25fSSatish Balay        0,
1414c84f5b01SHong Zhang        MatICCFactor_SeqSBAIJ,
1415d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqSBAIJ,
141649b5e25fSSatish Balay        MatGetSubMatrices_SeqSBAIJ,
141749b5e25fSSatish Balay        MatIncreaseOverlap_SeqSBAIJ,
141849b5e25fSSatish Balay        MatGetValues_SeqSBAIJ,
14193c896bc6SHong Zhang        MatCopy_SeqSBAIJ,
1420d519adbfSMatthew Knepley /*44*/ 0,
142149b5e25fSSatish Balay        MatScale_SeqSBAIJ,
142249b5e25fSSatish Balay        0,
142349b5e25fSSatish Balay        0,
14243bededecSBarry Smith        MatZeroRowsColumns_SeqSBAIJ,
14256363de48SJed Brown /*49*/ MatSetBlockSize_SeqSBAIJ,
142649b5e25fSSatish Balay        MatGetRowIJ_SeqSBAIJ,
142749b5e25fSSatish Balay        MatRestoreRowIJ_SeqSBAIJ,
142849b5e25fSSatish Balay        0,
142949b5e25fSSatish Balay        0,
1430d519adbfSMatthew Knepley /*54*/ 0,
143149b5e25fSSatish Balay        0,
143249b5e25fSSatish Balay        0,
143349b5e25fSSatish Balay        0,
143449b5e25fSSatish Balay        MatSetValuesBlocked_SeqSBAIJ,
1435d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_SeqSBAIJ,
143649b5e25fSSatish Balay        0,
143749b5e25fSSatish Balay        0,
1438357abbc8SBarry Smith        0,
1439d959ec07SHong Zhang        0,
1440d519adbfSMatthew Knepley /*64*/ 0,
1441d959ec07SHong Zhang        0,
1442d959ec07SHong Zhang        0,
1443d959ec07SHong Zhang        0,
1444d959ec07SHong Zhang        0,
1445d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqSBAIJ,
14463e0d88b5SBarry Smith        0,
14473e0d88b5SBarry Smith        0,
14483e0d88b5SBarry Smith        0,
14493e0d88b5SBarry Smith        0,
1450d519adbfSMatthew Knepley /*74*/ 0,
14513e0d88b5SBarry Smith        0,
14523e0d88b5SBarry Smith        0,
14533e0d88b5SBarry Smith        0,
14543e0d88b5SBarry Smith        0,
1455d519adbfSMatthew Knepley /*79*/ 0,
14563e0d88b5SBarry Smith        0,
14573e0d88b5SBarry Smith        0,
145897304618SKris Buschelman        MatGetInertia_SeqSBAIJ,
14595bba2384SShri Abhyankar        MatLoad_SeqSBAIJ,
1460d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqSBAIJ,
1461865e5f61SKris Buschelman        MatIsHermitian_SeqSBAIJ,
1462efcf0fc3SBarry Smith        MatIsStructurallySymmetric_SeqSBAIJ,
1463865e5f61SKris Buschelman        0,
1464865e5f61SKris Buschelman        0,
1465d519adbfSMatthew Knepley /*89*/ 0,
1466865e5f61SKris Buschelman        0,
1467865e5f61SKris Buschelman        0,
1468865e5f61SKris Buschelman        0,
1469865e5f61SKris Buschelman        0,
1470d519adbfSMatthew Knepley /*94*/ 0,
1471865e5f61SKris Buschelman        0,
1472865e5f61SKris Buschelman        0,
147399cafbc1SBarry Smith        0,
147499cafbc1SBarry Smith        0,
1475d519adbfSMatthew Knepley /*99*/ 0,
147699cafbc1SBarry Smith        0,
147799cafbc1SBarry Smith        0,
147899cafbc1SBarry Smith        0,
147999cafbc1SBarry Smith        0,
1480d519adbfSMatthew Knepley /*104*/0,
148199cafbc1SBarry Smith        MatRealPart_SeqSBAIJ,
1482f5edf698SHong Zhang        MatImaginaryPart_SeqSBAIJ,
1483f5edf698SHong Zhang        MatGetRowUpperTriangular_SeqSBAIJ,
14842af78befSBarry Smith        MatRestoreRowUpperTriangular_SeqSBAIJ,
1485d519adbfSMatthew Knepley /*109*/0,
14862af78befSBarry Smith        0,
14872af78befSBarry Smith        0,
14882af78befSBarry Smith        0,
1489547795f9SHong Zhang        MatMissingDiagonal_SeqSBAIJ,
1490547795f9SHong Zhang /*114*/0,
1491547795f9SHong Zhang        0,
1492547795f9SHong Zhang        0,
1493547795f9SHong Zhang        0,
1494547795f9SHong Zhang        0,
1495547795f9SHong Zhang /*119*/0,
1496547795f9SHong Zhang        0,
14972f480046SShri Abhyankar        0,
14985bba2384SShri Abhyankar        0
149999cafbc1SBarry Smith };
1500be1d678aSKris Buschelman 
150149b5e25fSSatish Balay EXTERN_C_BEGIN
15024a2ae208SSatish Balay #undef __FUNCT__
15034a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqSBAIJ"
15047087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqSBAIJ(Mat mat)
150549b5e25fSSatish Balay {
15064afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ *)mat->data;
1507d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
1508dfbe8321SBarry Smith   PetscErrorCode ierr;
150949b5e25fSSatish Balay 
151049b5e25fSSatish Balay   PetscFunctionBegin;
1511e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
151249b5e25fSSatish Balay 
151349b5e25fSSatish Balay   /* allocate space for values if not already there */
151449b5e25fSSatish Balay   if (!aij->saved_values) {
151587828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
151649b5e25fSSatish Balay   }
151749b5e25fSSatish Balay 
151849b5e25fSSatish Balay   /* copy values over */
151987828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
152049b5e25fSSatish Balay   PetscFunctionReturn(0);
152149b5e25fSSatish Balay }
152249b5e25fSSatish Balay EXTERN_C_END
152349b5e25fSSatish Balay 
152449b5e25fSSatish Balay EXTERN_C_BEGIN
15254a2ae208SSatish Balay #undef __FUNCT__
15264a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqSBAIJ"
15277087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqSBAIJ(Mat mat)
152849b5e25fSSatish Balay {
15294afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ *)mat->data;
15306849ba73SBarry Smith   PetscErrorCode ierr;
1531d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
153249b5e25fSSatish Balay 
153349b5e25fSSatish Balay   PetscFunctionBegin;
1534e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
1535e7e72b3dSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
153649b5e25fSSatish Balay 
153749b5e25fSSatish Balay   /* copy values over */
153887828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
153949b5e25fSSatish Balay   PetscFunctionReturn(0);
154049b5e25fSSatish Balay }
154149b5e25fSSatish Balay EXTERN_C_END
154249b5e25fSSatish Balay 
15438549e402SHong Zhang EXTERN_C_BEGIN
15444a2ae208SSatish Balay #undef __FUNCT__
1545a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation_SeqSBAIJ"
15467087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetPreallocation_SeqSBAIJ(Mat B,PetscInt bs,PetscInt nz,PetscInt *nnz)
154749b5e25fSSatish Balay {
1548c464158bSHong Zhang   Mat_SeqSBAIJ   *b = (Mat_SeqSBAIJ*)B->data;
15496849ba73SBarry Smith   PetscErrorCode ierr;
1550db4efbfdSBarry Smith   PetscInt       i,mbs,bs2, newbs = PetscAbs(bs);
1551ace3abfcSBarry Smith   PetscBool      skipallocation = PETSC_FALSE,flg = PETSC_FALSE;
155249b5e25fSSatish Balay 
155349b5e25fSSatish Balay   PetscFunctionBegin;
1554273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
1555db4efbfdSBarry Smith   if (bs < 0) {
1556db4efbfdSBarry Smith     ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Options for MPISBAIJ matrix","Mat");CHKERRQ(ierr);
1557db4efbfdSBarry Smith       ierr = PetscOptionsInt("-mat_block_size","Set the blocksize used to store the matrix","MatSeqSBAIJSetPreallocation",newbs,&newbs,PETSC_NULL);CHKERRQ(ierr);
1558db4efbfdSBarry Smith     ierr = PetscOptionsEnd();CHKERRQ(ierr);
1559db4efbfdSBarry Smith     bs   = PetscAbs(bs);
1560db4efbfdSBarry Smith   }
1561e7e72b3dSBarry Smith   if (nnz && newbs != bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot change blocksize from command line if setting nnz");
1562db4efbfdSBarry Smith   bs = newbs;
1563db4efbfdSBarry Smith 
156426283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
156526283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
156626283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
156726283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
1568899cda47SBarry Smith 
1569d0f46423SBarry Smith   mbs  = B->rmap->N/bs;
157049b5e25fSSatish Balay   bs2  = bs*bs;
157149b5e25fSSatish Balay 
1572e7e72b3dSBarry Smith   if (mbs*bs != B->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number rows, cols must be divisible by blocksize");
157349b5e25fSSatish Balay 
1574ab93d7beSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
1575ab93d7beSBarry Smith     skipallocation = PETSC_TRUE;
1576ab93d7beSBarry Smith     nz             = 0;
1577ab93d7beSBarry Smith   }
1578ab93d7beSBarry Smith 
1579435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 3;
1580e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
158149b5e25fSSatish Balay   if (nnz) {
158249b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
1583e32f2f54SBarry 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]);
1584e32f2f54SBarry 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);
158549b5e25fSSatish Balay     }
158649b5e25fSSatish Balay   }
158749b5e25fSSatish Balay 
1588db4efbfdSBarry Smith   B->ops->mult             = MatMult_SeqSBAIJ_N;
1589db4efbfdSBarry Smith   B->ops->multadd          = MatMultAdd_SeqSBAIJ_N;
1590db4efbfdSBarry Smith   B->ops->multtranspose    = MatMult_SeqSBAIJ_N;
1591db4efbfdSBarry Smith   B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_N;
1592acfcf0e5SJed Brown   ierr  = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,PETSC_NULL);CHKERRQ(ierr);
159349b5e25fSSatish Balay   if (!flg) {
159449b5e25fSSatish Balay     switch (bs) {
159549b5e25fSSatish Balay     case 1:
159649b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_1;
159749b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_1;
1598431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_1;
1599431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_1;
160049b5e25fSSatish Balay       break;
160149b5e25fSSatish Balay     case 2:
160249b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_2;
160349b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_2;
1604431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_2;
1605431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_2;
160649b5e25fSSatish Balay       break;
160749b5e25fSSatish Balay     case 3:
160849b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_3;
160949b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_3;
1610431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_3;
1611431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_3;
161249b5e25fSSatish Balay       break;
161349b5e25fSSatish Balay     case 4:
161449b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_4;
161549b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_4;
1616431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_4;
1617431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_4;
161849b5e25fSSatish Balay       break;
161949b5e25fSSatish Balay     case 5:
162049b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_5;
162149b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_5;
1622431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_5;
1623431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_5;
162449b5e25fSSatish Balay       break;
162549b5e25fSSatish Balay     case 6:
162649b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_6;
162749b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_6;
1628431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_6;
1629431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_6;
163049b5e25fSSatish Balay       break;
163149b5e25fSSatish Balay     case 7:
1632de53e5efSHong Zhang       B->ops->mult             = MatMult_SeqSBAIJ_7;
163349b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_7;
1634431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_7;
1635431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_7;
163649b5e25fSSatish Balay       break;
163749b5e25fSSatish Balay     }
163849b5e25fSSatish Balay   }
163949b5e25fSSatish Balay 
164049b5e25fSSatish Balay   b->mbs = mbs;
16414afc71dfSHong Zhang   b->nbs = mbs;
1642ab93d7beSBarry Smith   if (!skipallocation) {
16432ee49352SLisandro Dalcin     if (!b->imax) {
1644ab93d7beSBarry Smith       ierr = PetscMalloc2(mbs,PetscInt,&b->imax,mbs,PetscInt,&b->ilen);CHKERRQ(ierr);
1645c760cd28SBarry Smith       b->free_imax_ilen = PETSC_TRUE;
16462ee49352SLisandro Dalcin       ierr = PetscLogObjectMemory(B,2*mbs*sizeof(PetscInt));CHKERRQ(ierr);
16472ee49352SLisandro Dalcin     }
164849b5e25fSSatish Balay     if (!nnz) {
1649435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
165049b5e25fSSatish Balay       else if (nz <= 0)        nz = 1;
165149b5e25fSSatish Balay       for (i=0; i<mbs; i++) {
16528cef66ccSHong Zhang         b->imax[i] = nz;
165349b5e25fSSatish Balay       }
1654153ea458SHong Zhang       nz = nz*mbs; /* total nz */
165549b5e25fSSatish Balay     } else {
165649b5e25fSSatish Balay       nz = 0;
16578cef66ccSHong Zhang       for (i=0; i<mbs; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
165849b5e25fSSatish Balay     }
16592ee49352SLisandro Dalcin     /* b->ilen will count nonzeros in each block row so far. */
16602ee49352SLisandro Dalcin     for (i=0; i<mbs; i++) { b->ilen[i] = 0;}
16616c6c5352SBarry Smith     /* nz=(nz+mbs)/2; */ /* total diagonal and superdiagonal nonzero blocks */
166249b5e25fSSatish Balay 
166349b5e25fSSatish Balay     /* allocate the matrix space */
16642ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
1665d0f46423SBarry Smith     ierr = PetscMalloc3(bs2*nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->N+1,PetscInt,&b->i);CHKERRQ(ierr);
1666d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->N+1)*sizeof(PetscInt)+nz*(bs2*sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
16676c6c5352SBarry Smith     ierr = PetscMemzero(b->a,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
166813f74950SBarry Smith     ierr = PetscMemzero(b->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
166949b5e25fSSatish Balay     b->singlemalloc = PETSC_TRUE;
167049b5e25fSSatish Balay 
167149b5e25fSSatish Balay     /* pointer to beginning of each row */
1672e60cf9a0SBarry Smith     b->i[0] = 0;
167349b5e25fSSatish Balay     for (i=1; i<mbs+1; i++) {
167449b5e25fSSatish Balay       b->i[i] = b->i[i-1] + b->imax[i-1];
167549b5e25fSSatish Balay     }
1676e6b907acSBarry Smith     b->free_a     = PETSC_TRUE;
1677e6b907acSBarry Smith     b->free_ij    = PETSC_TRUE;
1678e811da20SHong Zhang   } else {
1679e6b907acSBarry Smith     b->free_a     = PETSC_FALSE;
1680e6b907acSBarry Smith     b->free_ij    = PETSC_FALSE;
1681ab93d7beSBarry Smith   }
168249b5e25fSSatish Balay 
1683d0f46423SBarry Smith   B->rmap->bs     = bs;
168449b5e25fSSatish Balay   b->bs2          = bs2;
16856c6c5352SBarry Smith   b->nz           = 0;
1686b32cb4a7SJed Brown   b->maxnz        = nz;
1687153ea458SHong Zhang 
168816cdd363SHong Zhang   b->inew             = 0;
168916cdd363SHong Zhang   b->jnew             = 0;
169016cdd363SHong Zhang   b->anew             = 0;
169116cdd363SHong Zhang   b->a2anew           = 0;
16921a3463dfSHong Zhang   b->permute          = PETSC_FALSE;
1693c464158bSHong Zhang   PetscFunctionReturn(0);
1694c464158bSHong Zhang }
1695a23d5eceSKris Buschelman EXTERN_C_END
1696153ea458SHong Zhang 
1697db4efbfdSBarry Smith /*
1698db4efbfdSBarry Smith    This is used to set the numeric factorization for both Cholesky and ICC symbolic factorization
1699db4efbfdSBarry Smith */
17008b1456e3SHong Zhang #undef __FUNCT__
1701d595f711SHong Zhang #define __FUNCT__ "MatSeqSBAIJSetNumericFactorization_inplace"
1702ace3abfcSBarry Smith PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat B,PetscBool  natural)
1703db4efbfdSBarry Smith {
1704db4efbfdSBarry Smith   PetscErrorCode ierr;
1705ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
1706db4efbfdSBarry Smith   PetscInt       bs = B->rmap->bs;
1707db4efbfdSBarry Smith 
1708db4efbfdSBarry Smith   PetscFunctionBegin;
1709acfcf0e5SJed Brown   ierr    = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,PETSC_NULL);CHKERRQ(ierr);
1710db4efbfdSBarry Smith   if (flg) bs = 8;
1711db4efbfdSBarry Smith 
1712db4efbfdSBarry Smith   if (!natural) {
1713db4efbfdSBarry Smith     switch (bs) {
1714db4efbfdSBarry Smith     case 1:
1715d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_inplace;
1716db4efbfdSBarry Smith       break;
1717db4efbfdSBarry Smith     case 2:
1718db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2;
1719db4efbfdSBarry Smith       break;
1720db4efbfdSBarry Smith     case 3:
1721db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3;
1722db4efbfdSBarry Smith       break;
1723db4efbfdSBarry Smith     case 4:
1724db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4;
1725db4efbfdSBarry Smith       break;
1726db4efbfdSBarry Smith     case 5:
1727db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5;
1728db4efbfdSBarry Smith       break;
1729db4efbfdSBarry Smith     case 6:
1730db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6;
1731db4efbfdSBarry Smith       break;
1732db4efbfdSBarry Smith     case 7:
1733db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7;
1734db4efbfdSBarry Smith       break;
1735db4efbfdSBarry Smith     default:
1736db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N;
1737db4efbfdSBarry Smith       break;
1738db4efbfdSBarry Smith     }
1739db4efbfdSBarry Smith   } else {
1740db4efbfdSBarry Smith     switch (bs) {
1741db4efbfdSBarry Smith     case 1:
1742d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering_inplace;
1743db4efbfdSBarry Smith       break;
1744db4efbfdSBarry Smith     case 2:
1745db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering;
1746db4efbfdSBarry Smith       break;
1747db4efbfdSBarry Smith     case 3:
1748db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3_NaturalOrdering;
1749db4efbfdSBarry Smith       break;
1750db4efbfdSBarry Smith     case 4:
1751db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4_NaturalOrdering;
1752db4efbfdSBarry Smith       break;
1753db4efbfdSBarry Smith     case 5:
1754db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5_NaturalOrdering;
1755db4efbfdSBarry Smith       break;
1756db4efbfdSBarry Smith     case 6:
1757db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6_NaturalOrdering;
1758db4efbfdSBarry Smith       break;
1759db4efbfdSBarry Smith     case 7:
1760db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7_NaturalOrdering;
1761db4efbfdSBarry Smith       break;
1762db4efbfdSBarry Smith     default:
1763db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering;
1764db4efbfdSBarry Smith       break;
1765db4efbfdSBarry Smith     }
1766db4efbfdSBarry Smith   }
1767db4efbfdSBarry Smith   PetscFunctionReturn(0);
1768db4efbfdSBarry Smith }
1769db4efbfdSBarry Smith 
1770d769727bSBarry Smith EXTERN_C_BEGIN
17717087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqSBAIJ_SeqAIJ(Mat, MatType,MatReuse,Mat*);
17727087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqSBAIJ_SeqBAIJ(Mat, MatType,MatReuse,Mat*);
1773d769727bSBarry Smith EXTERN_C_END
1774d769727bSBarry Smith 
1775e631078cSBarry Smith 
1776e631078cSBarry Smith EXTERN_C_BEGIN
17775c9eb25fSBarry Smith #undef __FUNCT__
17785c9eb25fSBarry Smith #define __FUNCT__ "MatGetFactor_seqsbaij_petsc"
17795c9eb25fSBarry Smith PetscErrorCode MatGetFactor_seqsbaij_petsc(Mat A,MatFactorType ftype,Mat *B)
17805c9eb25fSBarry Smith {
1781d0f46423SBarry Smith   PetscInt           n = A->rmap->n;
17825c9eb25fSBarry Smith   PetscErrorCode     ierr;
17835c9eb25fSBarry Smith 
17845c9eb25fSBarry Smith   PetscFunctionBegin;
17855c9eb25fSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
17865c9eb25fSBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
17875c9eb25fSBarry Smith   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
17885c9eb25fSBarry Smith     ierr = MatSetType(*B,MATSEQSBAIJ);CHKERRQ(ierr);
17895c9eb25fSBarry Smith     ierr = MatSeqSBAIJSetPreallocation(*B,1,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr);
17907b056e98SHong Zhang     (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqSBAIJ;
1791c6d0d4f0SHong Zhang     (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqSBAIJ;
1792e32f2f54SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported");
1793d5f3da31SBarry Smith   (*B)->factortype = ftype;
17945c9eb25fSBarry Smith   PetscFunctionReturn(0);
17955c9eb25fSBarry Smith }
1796e631078cSBarry Smith EXTERN_C_END
17975c9eb25fSBarry Smith 
17985c9eb25fSBarry Smith EXTERN_C_BEGIN
1799db4efbfdSBarry Smith #undef __FUNCT__
1800db4efbfdSBarry Smith #define __FUNCT__ "MatGetFactorAvailable_seqsbaij_petsc"
1801ace3abfcSBarry Smith PetscErrorCode MatGetFactorAvailable_seqsbaij_petsc(Mat A,MatFactorType ftype,PetscBool  *flg)
1802db4efbfdSBarry Smith {
1803db4efbfdSBarry Smith   PetscFunctionBegin;
1804db4efbfdSBarry Smith   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
1805db4efbfdSBarry Smith     *flg = PETSC_TRUE;
1806db4efbfdSBarry Smith   } else {
1807db4efbfdSBarry Smith     *flg = PETSC_FALSE;
1808db4efbfdSBarry Smith   }
1809db4efbfdSBarry Smith   PetscFunctionReturn(0);
1810db4efbfdSBarry Smith }
1811db4efbfdSBarry Smith EXTERN_C_END
1812db4efbfdSBarry Smith 
1813db4efbfdSBarry Smith EXTERN_C_BEGIN
1814611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
1815bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_sbaij_mumps(Mat,MatFactorType,Mat*);
1816611f576cSBarry Smith #endif
1817611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
18185c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_seqsbaij_spooles(Mat,MatFactorType,Mat*);
1819611f576cSBarry Smith #endif
1820b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
1821b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqsbaij_pastix(Mat,MatFactorType,Mat*);
1822b5e56a35SBarry Smith #endif
182320db9a53SJed Brown #if defined(PETSC_HAVE_CHOLMOD)
182420db9a53SJed Brown extern PetscErrorCode MatGetFactor_seqsbaij_cholmod(Mat,MatFactorType,Mat*);
182520db9a53SJed Brown #endif
18265c9eb25fSBarry Smith EXTERN_C_END
18275c9eb25fSBarry Smith 
18280bad9183SKris Buschelman /*MC
1829fafad747SKris Buschelman   MATSEQSBAIJ - MATSEQSBAIJ = "seqsbaij" - A matrix type to be used for sequential symmetric block sparse matrices,
18300bad9183SKris Buschelman   based on block compressed sparse row format.  Only the upper triangular portion of the matrix is stored.
18310bad9183SKris Buschelman 
1832828413b8SBarry Smith   For complex numbers by default this matrix is symmetric, NOT Hermitian symmetric. To make it Hermitian symmetric you
1833*71dad5bbSBarry Smith   can call MatSetOption(Mat, MAT_HERMITIAN); after MatAssemblyEnd()
1834828413b8SBarry Smith 
18350bad9183SKris Buschelman   Options Database Keys:
18360bad9183SKris Buschelman   . -mat_type seqsbaij - sets the matrix type to "seqsbaij" during a call to MatSetFromOptions()
18370bad9183SKris Buschelman 
1838*71dad5bbSBarry Smith   Notes: By default if you insert values into the lower triangular part of the matrix they are simply ignored (since they are not
1839*71dad5bbSBarry Smith      stored and it is assumed they symmetric to the upper triangular). If you call MatSetOption(Mat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_FALSE) or use
1840*71dad5bbSBarry Smith      the options database -mat_ignore_lower_triangular false it will generate an error if you try to set a value in the lower triangular portion.
1841*71dad5bbSBarry Smith 
1842*71dad5bbSBarry Smith 
18430bad9183SKris Buschelman   Level: beginner
18440bad9183SKris Buschelman 
18450bad9183SKris Buschelman   .seealso: MatCreateSeqSBAIJ
18460bad9183SKris Buschelman M*/
18470bad9183SKris Buschelman 
1848a23d5eceSKris Buschelman EXTERN_C_BEGIN
1849a23d5eceSKris Buschelman #undef __FUNCT__
1850a23d5eceSKris Buschelman #define __FUNCT__ "MatCreate_SeqSBAIJ"
18517087cfbeSBarry Smith PetscErrorCode  MatCreate_SeqSBAIJ(Mat B)
1852a23d5eceSKris Buschelman {
1853a23d5eceSKris Buschelman   Mat_SeqSBAIJ   *b;
1854dfbe8321SBarry Smith   PetscErrorCode ierr;
185513f74950SBarry Smith   PetscMPIInt    size;
1856ace3abfcSBarry Smith   PetscBool      no_unroll = PETSC_FALSE,no_inode = PETSC_FALSE;
1857a23d5eceSKris Buschelman 
1858a23d5eceSKris Buschelman   PetscFunctionBegin;
18597adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
1860e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Comm must be of size 1");
1861a23d5eceSKris Buschelman 
186238f2d2fdSLisandro Dalcin   ierr    = PetscNewLog(B,Mat_SeqSBAIJ,&b);CHKERRQ(ierr);
1863a23d5eceSKris Buschelman   B->data = (void*)b;
1864a23d5eceSKris Buschelman   ierr    = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
1865a23d5eceSKris Buschelman   B->ops->destroy     = MatDestroy_SeqSBAIJ;
1866a23d5eceSKris Buschelman   B->ops->view        = MatView_SeqSBAIJ;
1867a23d5eceSKris Buschelman   b->row              = 0;
1868a23d5eceSKris Buschelman   b->icol             = 0;
1869a23d5eceSKris Buschelman   b->reallocs         = 0;
1870a23d5eceSKris Buschelman   b->saved_values     = 0;
18710def2e27SBarry Smith   b->inode.limit      = 5;
18720def2e27SBarry Smith   b->inode.max_limit  = 5;
1873a23d5eceSKris Buschelman 
1874a23d5eceSKris Buschelman   b->roworiented      = PETSC_TRUE;
1875a23d5eceSKris Buschelman   b->nonew            = 0;
1876a23d5eceSKris Buschelman   b->diag             = 0;
1877a23d5eceSKris Buschelman   b->solve_work       = 0;
1878a23d5eceSKris Buschelman   b->mult_work        = 0;
1879a23d5eceSKris Buschelman   B->spptr            = 0;
1880f2cbd3d5SJed Brown   B->info.nz_unneeded = (PetscReal)b->maxnz*b->bs2;
1881a9817697SBarry Smith   b->keepnonzeropattern   = PETSC_FALSE;
1882a23d5eceSKris Buschelman   b->xtoy             = 0;
1883a23d5eceSKris Buschelman   b->XtoY             = 0;
1884a23d5eceSKris Buschelman 
1885a23d5eceSKris Buschelman   b->inew             = 0;
1886a23d5eceSKris Buschelman   b->jnew             = 0;
1887a23d5eceSKris Buschelman   b->anew             = 0;
1888a23d5eceSKris Buschelman   b->a2anew           = 0;
1889a23d5eceSKris Buschelman   b->permute          = PETSC_FALSE;
1890a23d5eceSKris Buschelman 
1891*71dad5bbSBarry Smith   b->ignore_ltriangular = PETSC_TRUE;
1892acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_ignore_lower_triangular",&b->ignore_ltriangular,PETSC_NULL);CHKERRQ(ierr);
1893941593c8SHong Zhang 
1894f5edf698SHong Zhang   b->getrow_utriangular = PETSC_FALSE;
1895acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_getrow_uppertriangular",&b->getrow_utriangular,PETSC_NULL);CHKERRQ(ierr);
1896f5edf698SHong Zhang 
1897b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
1898ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C",
1899b5e56a35SBarry Smith 					   "MatGetFactor_seqsbaij_pastix",
1900b5e56a35SBarry Smith 					   MatGetFactor_seqsbaij_pastix);CHKERRQ(ierr);
1901b5e56a35SBarry Smith #endif
1902611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
1903ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C",
19045c9eb25fSBarry Smith                                      "MatGetFactor_seqsbaij_spooles",
19055c9eb25fSBarry Smith                                      MatGetFactor_seqsbaij_spooles);CHKERRQ(ierr);
1906611f576cSBarry Smith #endif
1907611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
1908ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C",
1909bccb9932SShri Abhyankar                                      "MatGetFactor_sbaij_mumps",
1910bccb9932SShri Abhyankar                                      MatGetFactor_sbaij_mumps);CHKERRQ(ierr);
1911611f576cSBarry Smith #endif
191220db9a53SJed Brown #if defined(PETSC_HAVE_CHOLMOD)
191320db9a53SJed Brown   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C",
191420db9a53SJed Brown                                      "MatGetFactor_seqsbaij_cholmod",
191520db9a53SJed Brown                                      MatGetFactor_seqsbaij_cholmod);CHKERRQ(ierr);
191620db9a53SJed Brown #endif
1917ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C",
1918db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqsbaij_petsc",
1919db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqsbaij_petsc);CHKERRQ(ierr);
1920ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C",
19215c9eb25fSBarry Smith                                      "MatGetFactor_seqsbaij_petsc",
19225c9eb25fSBarry Smith                                      MatGetFactor_seqsbaij_petsc);CHKERRQ(ierr);
1923a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
1924a23d5eceSKris Buschelman                                      "MatStoreValues_SeqSBAIJ",
1925a23d5eceSKris Buschelman                                      MatStoreValues_SeqSBAIJ);CHKERRQ(ierr);
1926a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
1927a23d5eceSKris Buschelman                                      "MatRetrieveValues_SeqSBAIJ",
1928a23d5eceSKris Buschelman                                      (void*)MatRetrieveValues_SeqSBAIJ);CHKERRQ(ierr);
1929a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqSBAIJSetColumnIndices_C",
1930a23d5eceSKris Buschelman                                      "MatSeqSBAIJSetColumnIndices_SeqSBAIJ",
1931a23d5eceSKris Buschelman                                      MatSeqSBAIJSetColumnIndices_SeqSBAIJ);CHKERRQ(ierr);
19324e5e7fe4SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqsbaij_seqaij_C",
19334e5e7fe4SHong Zhang                                      "MatConvert_SeqSBAIJ_SeqAIJ",
19344e5e7fe4SHong Zhang                                       MatConvert_SeqSBAIJ_SeqAIJ);CHKERRQ(ierr);
1935a0e1a404SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqsbaij_seqbaij_C",
1936a0e1a404SHong Zhang                                      "MatConvert_SeqSBAIJ_SeqBAIJ",
1937a0e1a404SHong Zhang                                       MatConvert_SeqSBAIJ_SeqBAIJ);CHKERRQ(ierr);
1938a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqSBAIJSetPreallocation_C",
1939a23d5eceSKris Buschelman                                      "MatSeqSBAIJSetPreallocation_SeqSBAIJ",
1940a23d5eceSKris Buschelman                                      MatSeqSBAIJSetPreallocation_SeqSBAIJ);CHKERRQ(ierr);
194123ce1328SBarry Smith 
194223ce1328SBarry Smith   B->symmetric                  = PETSC_TRUE;
194323ce1328SBarry Smith   B->structurally_symmetric     = PETSC_TRUE;
194423ce1328SBarry Smith   B->symmetric_set              = PETSC_TRUE;
194523ce1328SBarry Smith   B->structurally_symmetric_set = PETSC_TRUE;
194617667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQSBAIJ);CHKERRQ(ierr);
19470def2e27SBarry Smith 
19480def2e27SBarry Smith   ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Options for SEQSBAIJ matrix","Mat");CHKERRQ(ierr);
1949acfcf0e5SJed Brown     ierr = PetscOptionsBool("-mat_no_unroll","Do not optimize for inodes (slower)",PETSC_NULL,no_unroll,&no_unroll,PETSC_NULL);CHKERRQ(ierr);
19500def2e27SBarry Smith     if (no_unroll) {ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_unroll\n");CHKERRQ(ierr);}
1951acfcf0e5SJed Brown     ierr = PetscOptionsBool("-mat_no_inode","Do not optimize for inodes (slower)",PETSC_NULL,no_inode,&no_inode,PETSC_NULL);CHKERRQ(ierr);
19520def2e27SBarry Smith     if (no_inode) {ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_inode\n");CHKERRQ(ierr);}
19530def2e27SBarry 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);
19540def2e27SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1955ace3abfcSBarry Smith   b->inode.use = (PetscBool)(!(no_unroll || no_inode));
19560def2e27SBarry Smith   if (b->inode.limit > b->inode.max_limit) b->inode.limit = b->inode.max_limit;
19570def2e27SBarry Smith 
1958a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1959a23d5eceSKris Buschelman }
1960a23d5eceSKris Buschelman EXTERN_C_END
1961a23d5eceSKris Buschelman 
1962a23d5eceSKris Buschelman #undef __FUNCT__
1963a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation"
1964a23d5eceSKris Buschelman /*@C
1965a23d5eceSKris Buschelman    MatSeqSBAIJSetPreallocation - Creates a sparse symmetric matrix in block AIJ (block
1966a23d5eceSKris Buschelman    compressed row) format.  For good matrix assembly performance the
1967a23d5eceSKris Buschelman    user should preallocate the matrix storage by setting the parameter nz
1968a23d5eceSKris Buschelman    (or the array nnz).  By setting these parameters accurately, performance
1969a23d5eceSKris Buschelman    during matrix assembly can be increased by more than a factor of 50.
1970a23d5eceSKris Buschelman 
1971a23d5eceSKris Buschelman    Collective on Mat
1972a23d5eceSKris Buschelman 
1973a23d5eceSKris Buschelman    Input Parameters:
1974a23d5eceSKris Buschelman +  A - the symmetric matrix
1975a23d5eceSKris Buschelman .  bs - size of block
1976a23d5eceSKris Buschelman .  nz - number of block nonzeros per block row (same for all rows)
1977a23d5eceSKris Buschelman -  nnz - array containing the number of block nonzeros in the upper triangular plus
1978a23d5eceSKris Buschelman          diagonal portion of each block (possibly different for each block row) or PETSC_NULL
1979a23d5eceSKris Buschelman 
1980a23d5eceSKris Buschelman    Options Database Keys:
1981a23d5eceSKris Buschelman .   -mat_no_unroll - uses code that does not unroll the loops in the
1982a23d5eceSKris Buschelman                      block calculations (much slower)
1983db4efbfdSBarry Smith .    -mat_block_size - size of the blocks to use (only works if a negative bs is passed in
1984a23d5eceSKris Buschelman 
1985a23d5eceSKris Buschelman    Level: intermediate
1986a23d5eceSKris Buschelman 
1987a23d5eceSKris Buschelman    Notes:
1988a23d5eceSKris Buschelman    Specify the preallocated storage with either nz or nnz (not both).
1989a23d5eceSKris Buschelman    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
19900598bfebSBarry Smith    allocation.  See the <a href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</a> for details.
1991a23d5eceSKris Buschelman 
1992aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
1993aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
1994aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
1995aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
1996aa95bbe8SBarry Smith 
199749a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
199849a6f317SBarry Smith 
199949a6f317SBarry Smith 
2000a23d5eceSKris Buschelman .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPISBAIJ()
2001a23d5eceSKris Buschelman @*/
20027087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt nz,const PetscInt nnz[])
200313f74950SBarry Smith {
20044ac538c5SBarry Smith   PetscErrorCode ierr;
2005a23d5eceSKris Buschelman 
2006a23d5eceSKris Buschelman   PetscFunctionBegin;
20074ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqSBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[]),(B,bs,nz,nnz));CHKERRQ(ierr);
2008a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2009a23d5eceSKris Buschelman }
201049b5e25fSSatish Balay 
20114a2ae208SSatish Balay #undef __FUNCT__
20124a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqSBAIJ"
2013c464158bSHong Zhang /*@C
2014c464158bSHong Zhang    MatCreateSeqSBAIJ - Creates a sparse symmetric matrix in block AIJ (block
2015c464158bSHong Zhang    compressed row) format.  For good matrix assembly performance the
2016c464158bSHong Zhang    user should preallocate the matrix storage by setting the parameter nz
2017c464158bSHong Zhang    (or the array nnz).  By setting these parameters accurately, performance
2018c464158bSHong Zhang    during matrix assembly can be increased by more than a factor of 50.
201949b5e25fSSatish Balay 
2020c464158bSHong Zhang    Collective on MPI_Comm
2021c464158bSHong Zhang 
2022c464158bSHong Zhang    Input Parameters:
2023c464158bSHong Zhang +  comm - MPI communicator, set to PETSC_COMM_SELF
2024c464158bSHong Zhang .  bs - size of block
2025c464158bSHong Zhang .  m - number of rows, or number of columns
2026c464158bSHong Zhang .  nz - number of block nonzeros per block row (same for all rows)
2027744e8345SSatish Balay -  nnz - array containing the number of block nonzeros in the upper triangular plus
2028744e8345SSatish Balay          diagonal portion of each block (possibly different for each block row) or PETSC_NULL
2029c464158bSHong Zhang 
2030c464158bSHong Zhang    Output Parameter:
2031c464158bSHong Zhang .  A - the symmetric matrix
2032c464158bSHong Zhang 
2033c464158bSHong Zhang    Options Database Keys:
2034c464158bSHong Zhang .   -mat_no_unroll - uses code that does not unroll the loops in the
2035c464158bSHong Zhang                      block calculations (much slower)
2036c464158bSHong Zhang .    -mat_block_size - size of the blocks to use
2037c464158bSHong Zhang 
2038c464158bSHong Zhang    Level: intermediate
2039c464158bSHong Zhang 
2040175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2041ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
2042175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2043175b88e8SBarry Smith 
2044c464158bSHong Zhang    Notes:
20456d6d819aSHong Zhang    The number of rows and columns must be divisible by blocksize.
20466d6d819aSHong Zhang    This matrix type does not support complex Hermitian operation.
2047c464158bSHong Zhang 
2048c464158bSHong Zhang    Specify the preallocated storage with either nz or nnz (not both).
2049c464158bSHong Zhang    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
20500598bfebSBarry Smith    allocation.  See the <a href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</a> for details.
2051c464158bSHong Zhang 
205249a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
205349a6f317SBarry Smith 
2054c464158bSHong Zhang .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPISBAIJ()
2055c464158bSHong Zhang @*/
20567087cfbeSBarry Smith PetscErrorCode  MatCreateSeqSBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
2057c464158bSHong Zhang {
2058dfbe8321SBarry Smith   PetscErrorCode ierr;
2059c464158bSHong Zhang 
2060c464158bSHong Zhang   PetscFunctionBegin;
2061f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2062f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2063c464158bSHong Zhang   ierr = MatSetType(*A,MATSEQSBAIJ);CHKERRQ(ierr);
2064ab93d7beSBarry Smith   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*A,bs,nz,(PetscInt*)nnz);CHKERRQ(ierr);
206549b5e25fSSatish Balay   PetscFunctionReturn(0);
206649b5e25fSSatish Balay }
206749b5e25fSSatish Balay 
20684a2ae208SSatish Balay #undef __FUNCT__
20694a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqSBAIJ"
2070dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqSBAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
207149b5e25fSSatish Balay {
207249b5e25fSSatish Balay   Mat            C;
207349b5e25fSSatish Balay   Mat_SeqSBAIJ   *c,*a = (Mat_SeqSBAIJ*)A->data;
20746849ba73SBarry Smith   PetscErrorCode ierr;
2075b40805acSSatish Balay   PetscInt       i,mbs = a->mbs,nz = a->nz,bs2 =a->bs2;
207649b5e25fSSatish Balay 
207749b5e25fSSatish Balay   PetscFunctionBegin;
2078e32f2f54SBarry Smith   if (a->i[mbs] != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupt matrix");
207949b5e25fSSatish Balay 
208049b5e25fSSatish Balay   *B = 0;
20817adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2082d0f46423SBarry Smith   ierr = MatSetSizes(C,A->rmap->N,A->cmap->n,A->rmap->N,A->cmap->n);CHKERRQ(ierr);
20838e9a0fb8SHong Zhang   ierr = MatSetType(C,MATSEQSBAIJ);CHKERRQ(ierr);
20841d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
2085692f9cbeSHong Zhang   c    = (Mat_SeqSBAIJ*)C->data;
2086692f9cbeSHong Zhang 
2087273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2088d5f3da31SBarry Smith   C->factortype         = A->factortype;
208949b5e25fSSatish Balay   c->row                = 0;
209049b5e25fSSatish Balay   c->icol               = 0;
209149b5e25fSSatish Balay   c->saved_values       = 0;
2092a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
209349b5e25fSSatish Balay   C->assembled          = PETSC_TRUE;
209449b5e25fSSatish Balay 
209526283091SBarry Smith   ierr = PetscLayoutCopy(A->rmap,&C->rmap);CHKERRQ(ierr);
209626283091SBarry Smith   ierr = PetscLayoutCopy(A->cmap,&C->cmap);CHKERRQ(ierr);
209749b5e25fSSatish Balay   c->bs2  = a->bs2;
209849b5e25fSSatish Balay   c->mbs  = a->mbs;
209949b5e25fSSatish Balay   c->nbs  = a->nbs;
210049b5e25fSSatish Balay 
2101c760cd28SBarry Smith   if  (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2102c760cd28SBarry Smith     c->imax           = a->imax;
2103c760cd28SBarry Smith     c->ilen           = a->ilen;
2104c760cd28SBarry Smith     c->free_imax_ilen = PETSC_FALSE;
2105c760cd28SBarry Smith   } else {
21068777fc3fSSatish Balay     ierr = PetscMalloc2((mbs+1),PetscInt,&c->imax,(mbs+1),PetscInt,&c->ilen);CHKERRQ(ierr);
2107c760cd28SBarry Smith     ierr = PetscLogObjectMemory(C,2*(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
210849b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
210949b5e25fSSatish Balay       c->imax[i] = a->imax[i];
211049b5e25fSSatish Balay       c->ilen[i] = a->ilen[i];
211149b5e25fSSatish Balay     }
2112c760cd28SBarry Smith     c->free_imax_ilen = PETSC_TRUE;
2113c760cd28SBarry Smith   }
211449b5e25fSSatish Balay 
211549b5e25fSSatish Balay   /* allocate the matrix space */
21164da8f245SBarry Smith   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
21174da8f245SBarry Smith     ierr = PetscMalloc(bs2*nz*sizeof(MatScalar),&c->a);CHKERRQ(ierr);
21184da8f245SBarry Smith     ierr = PetscLogObjectMemory(C,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
21194da8f245SBarry Smith     c->singlemalloc = PETSC_FALSE;
21204da8f245SBarry Smith     c->free_ij      = PETSC_FALSE;
21214da8f245SBarry Smith     c->parent       = A;
21224da8f245SBarry Smith     ierr            = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
21234da8f245SBarry Smith     ierr            = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
21244da8f245SBarry Smith     ierr            = MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
21254da8f245SBarry Smith   } else {
2126b40805acSSatish Balay     ierr = PetscMalloc3(bs2*nz,MatScalar,&c->a,nz,PetscInt,&c->j,mbs+1,PetscInt,&c->i);CHKERRQ(ierr);
212713f74950SBarry Smith     ierr = PetscMemcpy(c->i,a->i,(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
2128b40805acSSatish Balay     ierr = PetscLogObjectMemory(C,(mbs+1)*sizeof(PetscInt) + nz*(bs2*sizeof(MatScalar) + sizeof(PetscInt)));CHKERRQ(ierr);
21294da8f245SBarry Smith     c->singlemalloc = PETSC_TRUE;
21304da8f245SBarry Smith     c->free_ij      = PETSC_TRUE;
21314da8f245SBarry Smith   }
213249b5e25fSSatish Balay   if (mbs > 0) {
21334da8f245SBarry Smith     if (cpvalues != MAT_SHARE_NONZERO_PATTERN) {
213413f74950SBarry Smith       ierr = PetscMemcpy(c->j,a->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
21354da8f245SBarry Smith     }
213649b5e25fSSatish Balay     if (cpvalues == MAT_COPY_VALUES) {
213749b5e25fSSatish Balay       ierr = PetscMemcpy(c->a,a->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
213849b5e25fSSatish Balay     } else {
213949b5e25fSSatish Balay       ierr = PetscMemzero(c->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
214049b5e25fSSatish Balay     }
2141a1c3900fSBarry Smith     if (a->jshort) {
21424da8f245SBarry Smith       if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
21434da8f245SBarry Smith         c->jshort      = a->jshort;
21444da8f245SBarry Smith         c->free_jshort = PETSC_FALSE;
21454da8f245SBarry Smith       } else {
2146a1c3900fSBarry Smith         ierr = PetscMalloc(nz*sizeof(unsigned short),&c->jshort);CHKERRQ(ierr);
2147c760cd28SBarry Smith         ierr = PetscLogObjectMemory(C,nz*sizeof(unsigned short));CHKERRQ(ierr);
2148a1c3900fSBarry Smith         ierr = PetscMemcpy(c->jshort,a->jshort,nz*sizeof(unsigned short));CHKERRQ(ierr);
21494da8f245SBarry Smith         c->free_jshort = PETSC_TRUE;
21504da8f245SBarry Smith       }
2151a1c3900fSBarry Smith     }
215249b5e25fSSatish Balay   }
215349b5e25fSSatish Balay 
215449b5e25fSSatish Balay   c->roworiented = a->roworiented;
215549b5e25fSSatish Balay   c->nonew       = a->nonew;
215649b5e25fSSatish Balay 
215749b5e25fSSatish Balay   if (a->diag) {
2158c760cd28SBarry Smith     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2159c760cd28SBarry Smith       c->diag      = a->diag;
2160c760cd28SBarry Smith       c->free_diag = PETSC_FALSE;
2161c760cd28SBarry Smith     } else {
21622ed38d0bSJed Brown       ierr = PetscMalloc(mbs*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
21632ed38d0bSJed Brown       ierr = PetscLogObjectMemory(C,mbs*sizeof(PetscInt));CHKERRQ(ierr);
216449b5e25fSSatish Balay       for (i=0; i<mbs; i++) {
216549b5e25fSSatish Balay 	c->diag[i] = a->diag[i];
216649b5e25fSSatish Balay       }
2167c760cd28SBarry Smith       c->free_diag = PETSC_TRUE;
2168c760cd28SBarry Smith     }
216949b5e25fSSatish Balay   } else c->diag  = 0;
21706c6c5352SBarry Smith   c->nz           = a->nz;
2171f2cbd3d5SJed Brown   c->maxnz        = a->nz; /* Since we allocate exactly the right amount */
217249b5e25fSSatish Balay   c->solve_work   = 0;
217349b5e25fSSatish Balay   c->mult_work    = 0;
2174e6b907acSBarry Smith   c->free_a       = PETSC_TRUE;
217549b5e25fSSatish Balay   *B = C;
21767adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
217749b5e25fSSatish Balay   PetscFunctionReturn(0);
217849b5e25fSSatish Balay }
217949b5e25fSSatish Balay 
21804a2ae208SSatish Balay #undef __FUNCT__
21815bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_SeqSBAIJ"
2182112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqSBAIJ(Mat newmat,PetscViewer viewer)
21832f480046SShri Abhyankar {
21842f480046SShri Abhyankar   Mat_SeqSBAIJ   *a;
21852f480046SShri Abhyankar   PetscErrorCode ierr;
21862f480046SShri Abhyankar   int            fd;
21872f480046SShri Abhyankar   PetscMPIInt    size;
21882f480046SShri Abhyankar   PetscInt       i,nz,header[4],*rowlengths=0,M,N,bs=1;
21892f480046SShri Abhyankar   PetscInt       *mask,mbs,*jj,j,rowcount,nzcount,k,*s_browlengths,maskcount;
21902f480046SShri Abhyankar   PetscInt       kmax,jcount,block,idx,point,nzcountb,extra_rows,rows,cols;
21912f480046SShri Abhyankar   PetscInt       *masked,nmask,tmp,bs2,ishift;
21922f480046SShri Abhyankar   PetscScalar    *aa;
21932f480046SShri Abhyankar   MPI_Comm       comm = ((PetscObject)viewer)->comm;
21942f480046SShri Abhyankar 
21952f480046SShri Abhyankar   PetscFunctionBegin;
21962f480046SShri Abhyankar   ierr = PetscOptionsGetInt(PETSC_NULL,"-matload_block_size",&bs,PETSC_NULL);CHKERRQ(ierr);
21972f480046SShri Abhyankar   bs2  = bs*bs;
21982f480046SShri Abhyankar 
21992f480046SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
22002f480046SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
22012f480046SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
22022f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
22032f480046SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not Mat object");
22042f480046SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
22052f480046SShri Abhyankar 
22062f480046SShri Abhyankar   if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as SeqSBAIJ");
22072f480046SShri Abhyankar 
22082f480046SShri Abhyankar   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");
22092f480046SShri Abhyankar 
22102f480046SShri Abhyankar   /*
22112f480046SShri Abhyankar      This code adds extra rows to make sure the number of rows is
22122f480046SShri Abhyankar     divisible by the blocksize
22132f480046SShri Abhyankar   */
22142f480046SShri Abhyankar   mbs        = M/bs;
22152f480046SShri Abhyankar   extra_rows = bs - M + bs*(mbs);
22162f480046SShri Abhyankar   if (extra_rows == bs) extra_rows = 0;
22172f480046SShri Abhyankar   else                  mbs++;
22182f480046SShri Abhyankar   if (extra_rows) {
22192f480046SShri Abhyankar     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
22202f480046SShri Abhyankar   }
22212f480046SShri Abhyankar 
22222f480046SShri Abhyankar   /* Set global sizes if not already set */
22232f480046SShri Abhyankar   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) {
22242f480046SShri Abhyankar     ierr = MatSetSizes(newmat,PETSC_DECIDE,PETSC_DECIDE,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
22252f480046SShri Abhyankar   } else { /* Check if the matrix global sizes are correct */
22262f480046SShri Abhyankar     ierr = MatGetSize(newmat,&rows,&cols);CHKERRQ(ierr);
22272f480046SShri Abhyankar     if (M != rows ||  N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix in file of different length (%d, %d) than the input matrix (%d, %d)",M,N,rows,cols);
22282f480046SShri Abhyankar   }
22292f480046SShri Abhyankar 
22302f480046SShri Abhyankar   /* read in row lengths */
22312f480046SShri Abhyankar   ierr = PetscMalloc((M+extra_rows)*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
22322f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
22332f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
22342f480046SShri Abhyankar 
22352f480046SShri Abhyankar   /* read in column indices */
22362f480046SShri Abhyankar   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscInt),&jj);CHKERRQ(ierr);
22372f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,jj,nz,PETSC_INT);CHKERRQ(ierr);
22382f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) jj[nz+i] = M+i;
22392f480046SShri Abhyankar 
22402f480046SShri Abhyankar   /* loop over row lengths determining block row lengths */
22412f480046SShri Abhyankar   ierr     = PetscMalloc(mbs*sizeof(PetscInt),&s_browlengths);CHKERRQ(ierr);
22422f480046SShri Abhyankar   ierr     = PetscMemzero(s_browlengths,mbs*sizeof(PetscInt));CHKERRQ(ierr);
22432f480046SShri Abhyankar   ierr     = PetscMalloc2(mbs,PetscInt,&mask,mbs,PetscInt,&masked);CHKERRQ(ierr);
22442f480046SShri Abhyankar   ierr     = PetscMemzero(mask,mbs*sizeof(PetscInt));CHKERRQ(ierr);
22452f480046SShri Abhyankar   rowcount = 0;
22462f480046SShri Abhyankar   nzcount  = 0;
22472f480046SShri Abhyankar   for (i=0; i<mbs; i++) {
22482f480046SShri Abhyankar     nmask = 0;
22492f480046SShri Abhyankar     for (j=0; j<bs; j++) {
22502f480046SShri Abhyankar       kmax = rowlengths[rowcount];
22512f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
22522f480046SShri Abhyankar         tmp = jj[nzcount++]/bs;   /* block col. index */
22532f480046SShri Abhyankar         if (!mask[tmp] && tmp >= i) {masked[nmask++] = tmp; mask[tmp] = 1;}
22542f480046SShri Abhyankar       }
22552f480046SShri Abhyankar       rowcount++;
22562f480046SShri Abhyankar     }
22572f480046SShri Abhyankar     s_browlengths[i] += nmask;
22582f480046SShri Abhyankar 
22592f480046SShri Abhyankar     /* zero out the mask elements we set */
22602f480046SShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
22612f480046SShri Abhyankar   }
22622f480046SShri Abhyankar 
22632f480046SShri Abhyankar   /* Do preallocation */
22642f480046SShri Abhyankar   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(newmat,bs,0,s_browlengths);CHKERRQ(ierr);
22652f480046SShri Abhyankar   a = (Mat_SeqSBAIJ*)newmat->data;
22662f480046SShri Abhyankar 
22672f480046SShri Abhyankar   /* set matrix "i" values */
22682f480046SShri Abhyankar   a->i[0] = 0;
22692f480046SShri Abhyankar   for (i=1; i<= mbs; i++) {
22702f480046SShri Abhyankar     a->i[i]      = a->i[i-1] + s_browlengths[i-1];
22712f480046SShri Abhyankar     a->ilen[i-1] = s_browlengths[i-1];
22722f480046SShri Abhyankar   }
22732f480046SShri Abhyankar   a->nz = a->i[mbs];
22742f480046SShri Abhyankar 
22752f480046SShri Abhyankar   /* read in nonzero values */
22762f480046SShri Abhyankar   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscScalar),&aa);CHKERRQ(ierr);
22772f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,aa,nz,PETSC_SCALAR);CHKERRQ(ierr);
22782f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) aa[nz+i] = 1.0;
22792f480046SShri Abhyankar 
22802f480046SShri Abhyankar   /* set "a" and "j" values into matrix */
22812f480046SShri Abhyankar   nzcount = 0; jcount = 0;
22822f480046SShri Abhyankar   for (i=0; i<mbs; i++) {
22832f480046SShri Abhyankar     nzcountb = nzcount;
22842f480046SShri Abhyankar     nmask    = 0;
22852f480046SShri Abhyankar     for (j=0; j<bs; j++) {
22862f480046SShri Abhyankar       kmax = rowlengths[i*bs+j];
22872f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
22882f480046SShri Abhyankar         tmp = jj[nzcount++]/bs; /* block col. index */
22892f480046SShri Abhyankar         if (!mask[tmp] && tmp >= i) { masked[nmask++] = tmp; mask[tmp] = 1;}
22902f480046SShri Abhyankar       }
22912f480046SShri Abhyankar     }
22922f480046SShri Abhyankar     /* sort the masked values */
22932f480046SShri Abhyankar     ierr = PetscSortInt(nmask,masked);CHKERRQ(ierr);
22942f480046SShri Abhyankar 
22952f480046SShri Abhyankar     /* set "j" values into matrix */
22962f480046SShri Abhyankar     maskcount = 1;
22972f480046SShri Abhyankar     for (j=0; j<nmask; j++) {
22982f480046SShri Abhyankar       a->j[jcount++]  = masked[j];
22992f480046SShri Abhyankar       mask[masked[j]] = maskcount++;
23002f480046SShri Abhyankar     }
23012f480046SShri Abhyankar 
23022f480046SShri Abhyankar     /* set "a" values into matrix */
23032f480046SShri Abhyankar     ishift = bs2*a->i[i];
23042f480046SShri Abhyankar     for (j=0; j<bs; j++) {
23052f480046SShri Abhyankar       kmax = rowlengths[i*bs+j];
23062f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
23072f480046SShri Abhyankar         tmp       = jj[nzcountb]/bs ; /* block col. index */
23082f480046SShri Abhyankar         if (tmp >= i){
23092f480046SShri Abhyankar           block     = mask[tmp] - 1;
23102f480046SShri Abhyankar           point     = jj[nzcountb] - bs*tmp;
23112f480046SShri Abhyankar           idx       = ishift + bs2*block + j + bs*point;
23122f480046SShri Abhyankar           a->a[idx] = aa[nzcountb];
23132f480046SShri Abhyankar         }
23142f480046SShri Abhyankar         nzcountb++;
23152f480046SShri Abhyankar       }
23162f480046SShri Abhyankar     }
23172f480046SShri Abhyankar     /* zero out the mask elements we set */
23182f480046SShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
23192f480046SShri Abhyankar   }
23202f480046SShri Abhyankar   if (jcount != a->nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Bad binary matrix");
23212f480046SShri Abhyankar 
23222f480046SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
23232f480046SShri Abhyankar   ierr = PetscFree(s_browlengths);CHKERRQ(ierr);
23242f480046SShri Abhyankar   ierr = PetscFree(aa);CHKERRQ(ierr);
23252f480046SShri Abhyankar   ierr = PetscFree(jj);CHKERRQ(ierr);
23262f480046SShri Abhyankar   ierr = PetscFree2(mask,masked);CHKERRQ(ierr);
23272f480046SShri Abhyankar 
23282f480046SShri Abhyankar   ierr = MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
23292f480046SShri Abhyankar   ierr = MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
23302f480046SShri Abhyankar   ierr = MatView_Private(newmat);CHKERRQ(ierr);
23312f480046SShri Abhyankar   PetscFunctionReturn(0);
23322f480046SShri Abhyankar }
23332f480046SShri Abhyankar 
23342f480046SShri Abhyankar #undef __FUNCT__
2335c75a6043SHong Zhang #define __FUNCT__ "MatCreateSeqSBAIJWithArrays"
2336c75a6043SHong Zhang /*@
2337c75a6043SHong Zhang      MatCreateSeqSBAIJWithArrays - Creates an sequential SBAIJ matrix using matrix elements
2338c75a6043SHong Zhang               (upper triangular entries in CSR format) provided by the user.
2339c75a6043SHong Zhang 
2340c75a6043SHong Zhang      Collective on MPI_Comm
2341c75a6043SHong Zhang 
2342c75a6043SHong Zhang    Input Parameters:
2343c75a6043SHong Zhang +  comm - must be an MPI communicator of size 1
2344c75a6043SHong Zhang .  bs - size of block
2345c75a6043SHong Zhang .  m - number of rows
2346c75a6043SHong Zhang .  n - number of columns
2347c75a6043SHong Zhang .  i - row indices
2348c75a6043SHong Zhang .  j - column indices
2349c75a6043SHong Zhang -  a - matrix values
2350c75a6043SHong Zhang 
2351c75a6043SHong Zhang    Output Parameter:
2352c75a6043SHong Zhang .  mat - the matrix
2353c75a6043SHong Zhang 
2354dfb205c3SBarry Smith    Level: advanced
2355c75a6043SHong Zhang 
2356c75a6043SHong Zhang    Notes:
2357c75a6043SHong Zhang        The i, j, and a arrays are not copied by this routine, the user must free these arrays
2358c75a6043SHong Zhang     once the matrix is destroyed
2359c75a6043SHong Zhang 
2360c75a6043SHong Zhang        You cannot set new nonzero locations into this matrix, that will generate an error.
2361c75a6043SHong Zhang 
2362c75a6043SHong Zhang        The i and j indices are 0 based
2363c75a6043SHong Zhang 
2364dfb205c3SBarry Smith        When block size is greater than 1 the matrix values must be stored using the SBAIJ storage format (see the SBAIJ code to determine this). For block size of 1
2365dfb205c3SBarry Smith        it is the regular CSR format excluding the lower triangular elements.
2366dfb205c3SBarry Smith 
2367c75a6043SHong Zhang .seealso: MatCreate(), MatCreateMPISBAIJ(), MatCreateSeqSBAIJ()
2368c75a6043SHong Zhang 
2369c75a6043SHong Zhang @*/
23707087cfbeSBarry Smith PetscErrorCode  MatCreateSeqSBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
2371c75a6043SHong Zhang {
2372c75a6043SHong Zhang   PetscErrorCode ierr;
2373c75a6043SHong Zhang   PetscInt       ii;
2374c75a6043SHong Zhang   Mat_SeqSBAIJ   *sbaij;
2375c75a6043SHong Zhang 
2376c75a6043SHong Zhang   PetscFunctionBegin;
2377e32f2f54SBarry Smith   if (bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"block size %D > 1 is not supported yet",bs);
2378e32f2f54SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
2379c75a6043SHong Zhang 
2380c75a6043SHong Zhang   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
2381c75a6043SHong Zhang   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
2382c75a6043SHong Zhang   ierr = MatSetType(*mat,MATSEQSBAIJ);CHKERRQ(ierr);
2383c75a6043SHong Zhang   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*mat,bs,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
2384c75a6043SHong Zhang   sbaij = (Mat_SeqSBAIJ*)(*mat)->data;
2385c75a6043SHong Zhang   ierr = PetscMalloc2(m,PetscInt,&sbaij->imax,m,PetscInt,&sbaij->ilen);CHKERRQ(ierr);
2386c760cd28SBarry Smith   ierr = PetscLogObjectMemory(*mat,2*m*sizeof(PetscInt));CHKERRQ(ierr);
2387c75a6043SHong Zhang 
2388c75a6043SHong Zhang   sbaij->i = i;
2389c75a6043SHong Zhang   sbaij->j = j;
2390c75a6043SHong Zhang   sbaij->a = a;
2391c75a6043SHong Zhang   sbaij->singlemalloc = PETSC_FALSE;
2392c75a6043SHong Zhang   sbaij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
2393e6b907acSBarry Smith   sbaij->free_a       = PETSC_FALSE;
2394e6b907acSBarry Smith   sbaij->free_ij      = PETSC_FALSE;
2395c75a6043SHong Zhang 
2396c75a6043SHong Zhang   for (ii=0; ii<m; ii++) {
2397c75a6043SHong Zhang     sbaij->ilen[ii] = sbaij->imax[ii] = i[ii+1] - i[ii];
2398c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2399e32f2f54SBarry 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]);
2400c75a6043SHong Zhang #endif
2401c75a6043SHong Zhang   }
2402c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2403c75a6043SHong Zhang   for (ii=0; ii<sbaij->i[m]; ii++) {
2404e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
2405e32f2f54SBarry 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]);
2406c75a6043SHong Zhang   }
2407c75a6043SHong Zhang #endif
2408c75a6043SHong Zhang 
2409c75a6043SHong Zhang   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2410c75a6043SHong Zhang   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2411c75a6043SHong Zhang   PetscFunctionReturn(0);
2412c75a6043SHong Zhang }
2413d06b337dSHong Zhang 
2414d06b337dSHong Zhang 
2415d06b337dSHong Zhang 
241649b5e25fSSatish Balay 
241749b5e25fSSatish Balay 
2418