xref: /petsc/src/mat/impls/sbaij/seq/sbaij.c (revision 358d2f5dfd24f51765856617f4af4d6a23b983c0)
149b5e25fSSatish Balay 
249b5e25fSSatish Balay /*
3a1373b80SHong Zhang     Defines the basic matrix operations for the SBAIJ (compressed row)
449b5e25fSSatish Balay   matrix storage format.
549b5e25fSSatish Balay */
6c6db04a5SJed Brown #include <../src/mat/impls/baij/seq/baij.h>         /*I "petscmat.h" I*/
7c6db04a5SJed Brown #include <../src/mat/impls/sbaij/seq/sbaij.h>
8c6db04a5SJed Brown #include <petscblaslapack.h>
949b5e25fSSatish Balay 
10c6db04a5SJed Brown #include <../src/mat/impls/sbaij/seq/relax.h>
1170dcbbb9SBarry Smith #define USESHORT
12c6db04a5SJed Brown #include <../src/mat/impls/sbaij/seq/relax.h>
1370dcbbb9SBarry Smith 
14ace3abfcSBarry Smith extern PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat,PetscBool );
15b5b17502SBarry Smith 
1649b5e25fSSatish Balay /*
1749b5e25fSSatish Balay      Checks for missing diagonals
1849b5e25fSSatish Balay */
194a2ae208SSatish Balay #undef __FUNCT__
204a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqSBAIJ"
21ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqSBAIJ(Mat A,PetscBool  *missing,PetscInt *dd)
2249b5e25fSSatish Balay {
23045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
246849ba73SBarry Smith   PetscErrorCode ierr;
2513f74950SBarry Smith   PetscInt       *diag,*jj = a->j,i;
2649b5e25fSSatish Balay 
2749b5e25fSSatish Balay   PetscFunctionBegin;
28045c9aa0SHong Zhang   ierr = MatMarkDiagonal_SeqSBAIJ(A);CHKERRQ(ierr);
292af78befSBarry Smith   *missing = PETSC_FALSE;
30*358d2f5dSShri Abhyankar   if(A->rmap->n > 0 && !jj) {
31*358d2f5dSShri Abhyankar     *missing = PETSC_TRUE;
32*358d2f5dSShri Abhyankar     if (dd) *dd = 0;
33*358d2f5dSShri Abhyankar     PetscInfo(A,"Matrix has no entries therefore is missing diagonal");
34*358d2f5dSShri Abhyankar   } else {
35*358d2f5dSShri Abhyankar     diag = a->diag;
3649b5e25fSSatish Balay     for (i=0; i<a->mbs; i++) {
372af78befSBarry Smith       if (jj[diag[i]] != i) {
382af78befSBarry Smith 	*missing    = PETSC_TRUE;
392af78befSBarry Smith 	if (dd) *dd = i;
402af78befSBarry Smith 	break;
412af78befSBarry Smith       }
4249b5e25fSSatish Balay     }
43*358d2f5dSShri Abhyankar   }
4449b5e25fSSatish Balay   PetscFunctionReturn(0);
4549b5e25fSSatish Balay }
4649b5e25fSSatish Balay 
474a2ae208SSatish Balay #undef __FUNCT__
484a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqSBAIJ"
49dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqSBAIJ(Mat A)
5049b5e25fSSatish Balay {
51045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
526849ba73SBarry Smith   PetscErrorCode ierr;
5309f38230SBarry Smith   PetscInt       i;
5449b5e25fSSatish Balay 
5549b5e25fSSatish Balay   PetscFunctionBegin;
5609f38230SBarry Smith   if (!a->diag) {
5709f38230SBarry Smith     ierr = PetscMalloc(a->mbs*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
58c760cd28SBarry Smith     ierr = PetscLogObjectMemory(A,a->mbs*sizeof(PetscInt));CHKERRQ(ierr);
59c760cd28SBarry Smith     a->free_diag = PETSC_TRUE;
6009f38230SBarry Smith   }
6109f38230SBarry Smith   for (i=0; i<a->mbs; i++) a->diag[i] = a->i[i];
6249b5e25fSSatish Balay   PetscFunctionReturn(0);
6349b5e25fSSatish Balay }
6449b5e25fSSatish Balay 
654a2ae208SSatish Balay #undef __FUNCT__
664a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqSBAIJ"
67ace3abfcSBarry Smith static PetscErrorCode MatGetRowIJ_SeqSBAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
6849b5e25fSSatish Balay {
69a6ece127SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
70d0f46423SBarry Smith   PetscInt     i,j,n = a->mbs,nz = a->i[n],bs = A->rmap->bs;
718f7157efSSatish Balay   PetscErrorCode ierr;
7249b5e25fSSatish Balay 
7349b5e25fSSatish Balay   PetscFunctionBegin;
74d3e5a4abSHong Zhang   *nn = n;
75a1373b80SHong Zhang   if (!ia) PetscFunctionReturn(0);
768f7157efSSatish Balay   if (!blockcompressed) {
778f7157efSSatish Balay     /* malloc & create the natural set of indices */
78f1d0d59dSSatish Balay     ierr = PetscMalloc2((n+1)*bs,PetscInt,ia,nz*bs,PetscInt,ja);CHKERRQ(ierr);
798f7157efSSatish Balay     for (i=0; i<n+1; i++) {
808f7157efSSatish Balay       for (j=0; j<bs; j++) {
818f7157efSSatish Balay         *ia[i*bs+j] = a->i[i]*bs+j+oshift;
828f7157efSSatish Balay       }
838f7157efSSatish Balay     }
848f7157efSSatish Balay     for (i=0; i<nz; i++) {
858f7157efSSatish Balay       for (j=0; j<bs; j++) {
868f7157efSSatish Balay         *ja[i*bs+j] = a->j[i]*bs+j+oshift;
878f7157efSSatish Balay       }
888f7157efSSatish Balay     }
898f7157efSSatish Balay   } else { /* blockcompressed */
90a6ece127SHong Zhang     if (oshift == 1) {
91a6ece127SHong Zhang       /* temporarily add 1 to i and j indices */
926c6c5352SBarry Smith       for (i=0; i<nz; i++) a->j[i]++;
93a1373b80SHong Zhang       for (i=0; i<n+1; i++) a->i[i]++;
948f7157efSSatish Balay     }
95a1373b80SHong Zhang     *ia = a->i; *ja = a->j;
96a6ece127SHong Zhang   }
978f7157efSSatish Balay 
9849b5e25fSSatish Balay   PetscFunctionReturn(0);
9949b5e25fSSatish Balay }
10049b5e25fSSatish Balay 
1014a2ae208SSatish Balay #undef __FUNCT__
1024a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqSBAIJ"
103ace3abfcSBarry Smith static PetscErrorCode MatRestoreRowIJ_SeqSBAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
10449b5e25fSSatish Balay {
105b7aaefc3SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
1068f7157efSSatish Balay   PetscInt     i,n = a->mbs,nz = a->i[n];
1078f7157efSSatish Balay   PetscErrorCode ierr;
108a6ece127SHong Zhang 
10949b5e25fSSatish Balay   PetscFunctionBegin;
11049b5e25fSSatish Balay   if (!ia) PetscFunctionReturn(0);
111a6ece127SHong Zhang 
1128f7157efSSatish Balay   if (!blockcompressed) {
1138f7157efSSatish Balay     ierr = PetscFree2(*ia,*ja);CHKERRQ(ierr);
1148f7157efSSatish Balay   } else if (oshift == 1) { /* blockcompressed */
1156c6c5352SBarry Smith     for (i=0; i<nz; i++) a->j[i]--;
116a6ece127SHong Zhang     for (i=0; i<n+1; i++) a->i[i]--;
117a6ece127SHong Zhang   }
1188f7157efSSatish Balay 
119a6ece127SHong Zhang   PetscFunctionReturn(0);
12049b5e25fSSatish Balay }
12149b5e25fSSatish Balay 
1224a2ae208SSatish Balay #undef __FUNCT__
1234a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqSBAIJ"
124dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqSBAIJ(Mat A)
12549b5e25fSSatish Balay {
12649b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
127dfbe8321SBarry Smith   PetscErrorCode ierr;
12849b5e25fSSatish Balay 
12949b5e25fSSatish Balay   PetscFunctionBegin;
130a9f03627SSatish Balay #if defined(PETSC_USE_LOG)
131d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, NZ=%D",A->rmap->N,a->nz);
132a9f03627SSatish Balay #endif
133e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
1347f53bb6cSHong Zhang   if (a->free_diag){ierr = PetscFree(a->diag);CHKERRQ(ierr);}
1356bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
1366bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
1376bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
138c31cb41cSBarry Smith   ierr = PetscFree(a->idiag);CHKERRQ(ierr);
139c31cb41cSBarry Smith   ierr = PetscFree(a->inode.size);CHKERRQ(ierr);
140c31cb41cSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
141c760cd28SBarry Smith   if (a->free_imax_ilen) {ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);}
14205b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
14341f059aeSBarry Smith   ierr = PetscFree(a->sor_work);CHKERRQ(ierr);
14405b42c5fSBarry Smith   ierr = PetscFree(a->solves_work);CHKERRQ(ierr);
14505b42c5fSBarry Smith   ierr = PetscFree(a->mult_work);CHKERRQ(ierr);
14605b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
14705b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
1484da8f245SBarry Smith   if (a->free_jshort) {ierr = PetscFree(a->jshort);CHKERRQ(ierr);}
1491a3463dfSHong Zhang   ierr = PetscFree(a->inew);CHKERRQ(ierr);
1506bf464f9SBarry Smith   ierr = MatDestroy(&a->parent);CHKERRQ(ierr);
151bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
152901853e0SKris Buschelman 
153dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
154901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
155901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
156901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
157901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqaij_C","",PETSC_NULL);CHKERRQ(ierr);
158901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
159901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
160aa5a9175SDahai Guo   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqsbstrm_C","",PETSC_NULL);CHKERRQ(ierr);
16149b5e25fSSatish Balay   PetscFunctionReturn(0);
16249b5e25fSSatish Balay }
16349b5e25fSSatish Balay 
1644a2ae208SSatish Balay #undef __FUNCT__
1654a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqSBAIJ"
166ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqSBAIJ(Mat A,MatOption op,PetscBool  flg)
16749b5e25fSSatish Balay {
168045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
16963ba0a88SBarry Smith   PetscErrorCode ierr;
17049b5e25fSSatish Balay 
17149b5e25fSSatish Balay   PetscFunctionBegin;
1724d9d31abSKris Buschelman   switch (op) {
1734d9d31abSKris Buschelman   case MAT_ROW_ORIENTED:
1744e0d8c25SBarry Smith     a->roworiented = flg;
1754d9d31abSKris Buschelman     break;
176a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
177a9817697SBarry Smith     a->keepnonzeropattern = flg;
1784d9d31abSKris Buschelman     break;
179512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
180512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1814d9d31abSKris Buschelman     break;
1824d9d31abSKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
1834e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1844d9d31abSKris Buschelman     break;
1854d9d31abSKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1864e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1874d9d31abSKris Buschelman     break;
18828b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
18928b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
19028b2fa4aSMatthew Knepley     break;
1914e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1924d9d31abSKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1934d9d31abSKris Buschelman   case MAT_USE_HASH_TABLE:
194290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1954d9d31abSKris Buschelman     break;
1969a4540c5SBarry Smith   case MAT_HERMITIAN:
197e32f2f54SBarry Smith     if (!A->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call MatAssemblyEnd() first");
1980bc54ff2SBarry Smith     if (A->cmap->n < 65536 && A->cmap->bs == 1) {
199eeffb40dSHong Zhang       A->ops->mult = MatMult_SeqSBAIJ_1_Hermitian_ushort;
2000bc54ff2SBarry Smith     } else if (A->cmap->bs == 1) {
201eeffb40dSHong Zhang       A->ops->mult = MatMult_SeqSBAIJ_1_Hermitian;
202e32f2f54SBarry Smith     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for Hermitian with block size greater than 1");
203eeffb40dSHong Zhang     break;
2043d472b54SHong Zhang   case MAT_SPD:
2053d472b54SHong Zhang     A->spd_set                         = PETSC_TRUE;
2063d472b54SHong Zhang     A->spd                             = flg;
2073d472b54SHong Zhang     if (flg) {
2083d472b54SHong Zhang       A->symmetric                     = PETSC_TRUE;
2093d472b54SHong Zhang       A->structurally_symmetric        = PETSC_TRUE;
2103d472b54SHong Zhang       A->symmetric_set                 = PETSC_TRUE;
2113d472b54SHong Zhang       A->structurally_symmetric_set    = PETSC_TRUE;
2123d472b54SHong Zhang     }
2133d472b54SHong Zhang     break;
21477e54ba9SKris Buschelman   case MAT_SYMMETRIC:
21577e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
2169a4540c5SBarry Smith   case MAT_SYMMETRY_ETERNAL:
217e32f2f54SBarry Smith     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix must be symmetric");
218290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s not relevent\n",MatOptions[op]);CHKERRQ(ierr);
219290bbb0aSBarry Smith     break;
220941593c8SHong Zhang   case MAT_IGNORE_LOWER_TRIANGULAR:
2214e0d8c25SBarry Smith     a->ignore_ltriangular = flg;
222941593c8SHong Zhang     break;
223941593c8SHong Zhang   case MAT_ERROR_LOWER_TRIANGULAR:
2244e0d8c25SBarry Smith     a->ignore_ltriangular = flg;
22577e54ba9SKris Buschelman     break;
226f5edf698SHong Zhang   case MAT_GETROW_UPPERTRIANGULAR:
2274e0d8c25SBarry Smith     a->getrow_utriangular = flg;
228f5edf698SHong Zhang     break;
2294d9d31abSKris Buschelman   default:
230e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
23149b5e25fSSatish Balay   }
23249b5e25fSSatish Balay   PetscFunctionReturn(0);
23349b5e25fSSatish Balay }
23449b5e25fSSatish Balay 
2354a2ae208SSatish Balay #undef __FUNCT__
2364a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqSBAIJ"
23713f74950SBarry Smith PetscErrorCode MatGetRow_SeqSBAIJ(Mat A,PetscInt row,PetscInt *ncols,PetscInt **cols,PetscScalar **v)
23849b5e25fSSatish Balay {
23949b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
2406849ba73SBarry Smith   PetscErrorCode ierr;
24113f74950SBarry Smith   PetscInt       itmp,i,j,k,M,*ai,*aj,bs,bn,bp,*cols_i,bs2;
24249b5e25fSSatish Balay   MatScalar      *aa,*aa_i;
24387828ca2SBarry Smith   PetscScalar    *v_i;
24449b5e25fSSatish Balay 
24549b5e25fSSatish Balay   PetscFunctionBegin;
246e32f2f54SBarry 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()");
247f5edf698SHong Zhang   /* Get the upper triangular part of the row */
248d0f46423SBarry Smith   bs  = A->rmap->bs;
24949b5e25fSSatish Balay   ai  = a->i;
25049b5e25fSSatish Balay   aj  = a->j;
25149b5e25fSSatish Balay   aa  = a->a;
25249b5e25fSSatish Balay   bs2 = a->bs2;
25349b5e25fSSatish Balay 
254e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Row %D out of range", row);
25549b5e25fSSatish Balay 
25649b5e25fSSatish Balay   bn  = row/bs;   /* Block number */
25749b5e25fSSatish Balay   bp  = row % bs; /* Block position */
25849b5e25fSSatish Balay   M   = ai[bn+1] - ai[bn];
25949b5e25fSSatish Balay   *ncols = bs*M;
26049b5e25fSSatish Balay 
26149b5e25fSSatish Balay   if (v) {
26249b5e25fSSatish Balay     *v = 0;
26349b5e25fSSatish Balay     if (*ncols) {
26487828ca2SBarry Smith       ierr = PetscMalloc((*ncols+row)*sizeof(PetscScalar),v);CHKERRQ(ierr);
26549b5e25fSSatish Balay       for (i=0; i<M; i++) { /* for each block in the block row */
26649b5e25fSSatish Balay         v_i  = *v + i*bs;
26749b5e25fSSatish Balay         aa_i = aa + bs2*(ai[bn] + i);
26849b5e25fSSatish Balay         for (j=bp,k=0; j<bs2; j+=bs,k++) {v_i[k] = aa_i[j];}
26949b5e25fSSatish Balay       }
27049b5e25fSSatish Balay     }
27149b5e25fSSatish Balay   }
27249b5e25fSSatish Balay 
27349b5e25fSSatish Balay   if (cols) {
27449b5e25fSSatish Balay     *cols = 0;
27549b5e25fSSatish Balay     if (*ncols) {
27613f74950SBarry Smith       ierr = PetscMalloc((*ncols+row)*sizeof(PetscInt),cols);CHKERRQ(ierr);
27749b5e25fSSatish Balay       for (i=0; i<M; i++) { /* for each block in the block row */
27849b5e25fSSatish Balay         cols_i = *cols + i*bs;
27949b5e25fSSatish Balay         itmp  = bs*aj[ai[bn] + i];
28049b5e25fSSatish Balay         for (j=0; j<bs; j++) {cols_i[j] = itmp++;}
28149b5e25fSSatish Balay       }
28249b5e25fSSatish Balay     }
28349b5e25fSSatish Balay   }
28449b5e25fSSatish Balay 
28549b5e25fSSatish Balay   /*search column A(0:row-1,row) (=A(row,0:row-1)). Could be expensive! */
2865ddb2528SHong Zhang   /* this segment is currently removed, so only entries in the upper triangle are obtained */
2875ddb2528SHong Zhang #ifdef column_search
28849b5e25fSSatish Balay   v_i    = *v    + M*bs;
28949b5e25fSSatish Balay   cols_i = *cols + M*bs;
29049b5e25fSSatish Balay   for (i=0; i<bn; i++){ /* for each block row */
29149b5e25fSSatish Balay     M = ai[i+1] - ai[i];
29249b5e25fSSatish Balay     for (j=0; j<M; j++){
29349b5e25fSSatish Balay       itmp = aj[ai[i] + j];    /* block column value */
29449b5e25fSSatish Balay       if (itmp == bn){
29549b5e25fSSatish Balay         aa_i   = aa    + bs2*(ai[i] + j) + bs*bp;
29649b5e25fSSatish Balay         for (k=0; k<bs; k++) {
29749b5e25fSSatish Balay           *cols_i++ = i*bs+k;
29849b5e25fSSatish Balay           *v_i++    = aa_i[k];
29949b5e25fSSatish Balay         }
30049b5e25fSSatish Balay         *ncols += bs;
30149b5e25fSSatish Balay         break;
30249b5e25fSSatish Balay       }
30349b5e25fSSatish Balay     }
30449b5e25fSSatish Balay   }
3055ddb2528SHong Zhang #endif
30649b5e25fSSatish Balay   PetscFunctionReturn(0);
30749b5e25fSSatish Balay }
30849b5e25fSSatish Balay 
3094a2ae208SSatish Balay #undef __FUNCT__
3104a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqSBAIJ"
31113f74950SBarry Smith PetscErrorCode MatRestoreRow_SeqSBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
31249b5e25fSSatish Balay {
313dfbe8321SBarry Smith   PetscErrorCode ierr;
31449b5e25fSSatish Balay 
31549b5e25fSSatish Balay   PetscFunctionBegin;
31605b42c5fSBarry Smith   if (idx) {ierr = PetscFree(*idx);CHKERRQ(ierr);}
31705b42c5fSBarry Smith   if (v)   {ierr = PetscFree(*v);CHKERRQ(ierr);}
31849b5e25fSSatish Balay   PetscFunctionReturn(0);
31949b5e25fSSatish Balay }
32049b5e25fSSatish Balay 
3214a2ae208SSatish Balay #undef __FUNCT__
322f5edf698SHong Zhang #define __FUNCT__ "MatGetRowUpperTriangular_SeqSBAIJ"
323f5edf698SHong Zhang PetscErrorCode MatGetRowUpperTriangular_SeqSBAIJ(Mat A)
324f5edf698SHong Zhang {
325f5edf698SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
326f5edf698SHong Zhang 
327f5edf698SHong Zhang   PetscFunctionBegin;
328f5edf698SHong Zhang   a->getrow_utriangular = PETSC_TRUE;
329f5edf698SHong Zhang   PetscFunctionReturn(0);
330f5edf698SHong Zhang }
331f5edf698SHong Zhang #undef __FUNCT__
332f5edf698SHong Zhang #define __FUNCT__ "MatRestoreRowUpperTriangular_SeqSBAIJ"
333f5edf698SHong Zhang PetscErrorCode MatRestoreRowUpperTriangular_SeqSBAIJ(Mat A)
334f5edf698SHong Zhang {
335f5edf698SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
336f5edf698SHong Zhang 
337f5edf698SHong Zhang   PetscFunctionBegin;
338f5edf698SHong Zhang   a->getrow_utriangular = PETSC_FALSE;
339f5edf698SHong Zhang   PetscFunctionReturn(0);
340f5edf698SHong Zhang }
341f5edf698SHong Zhang 
342f5edf698SHong Zhang #undef __FUNCT__
3434a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqSBAIJ"
344fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqSBAIJ(Mat A,MatReuse reuse,Mat *B)
34549b5e25fSSatish Balay {
346dfbe8321SBarry Smith   PetscErrorCode ierr;
34749b5e25fSSatish Balay   PetscFunctionBegin;
348815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
349999d9058SBarry Smith     ierr = MatDuplicate(A,MAT_COPY_VALUES,B);CHKERRQ(ierr);
350fc4dec0aSBarry Smith   }
3518115998fSBarry Smith   PetscFunctionReturn(0);
35249b5e25fSSatish Balay }
35349b5e25fSSatish Balay 
3544a2ae208SSatish Balay #undef __FUNCT__
3554a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_ASCII"
3566849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_ASCII(Mat A,PetscViewer viewer)
35749b5e25fSSatish Balay {
35849b5e25fSSatish Balay   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
359dfbe8321SBarry Smith   PetscErrorCode    ierr;
360d0f46423SBarry Smith   PetscInt          i,j,bs = A->rmap->bs,k,l,bs2=a->bs2;
361f3ef73ceSBarry Smith   PetscViewerFormat format;
362121deb67SSatish Balay   PetscInt          *diag;
36349b5e25fSSatish Balay 
36449b5e25fSSatish Balay   PetscFunctionBegin;
365b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
366456192e2SBarry Smith   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
36777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);CHKERRQ(ierr);
368fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
369d2507d54SMatthew Knepley     Mat aij;
370d5f3da31SBarry Smith     if (A->factortype && bs>1){
37170d5e725SHong 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);
37270d5e725SHong Zhang       PetscFunctionReturn(0);
37370d5e725SHong Zhang     }
374c9f458caSMatthew Knepley     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&aij);CHKERRQ(ierr);
375c9f458caSMatthew Knepley     ierr = MatView(aij,viewer);CHKERRQ(ierr);
3766bf464f9SBarry Smith     ierr = MatDestroy(&aij);CHKERRQ(ierr);
377fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
378d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
37949b5e25fSSatish Balay     for (i=0; i<a->mbs; i++) {
38049b5e25fSSatish Balay       for (j=0; j<bs; j++) {
38177431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
38249b5e25fSSatish Balay         for (k=a->i[i]; k<a->i[i+1]; k++) {
38349b5e25fSSatish Balay           for (l=0; l<bs; l++) {
38449b5e25fSSatish Balay #if defined(PETSC_USE_COMPLEX)
38549b5e25fSSatish Balay             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
386a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",bs*a->j[k]+l,
38749b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
38849b5e25fSSatish Balay             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
389a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",bs*a->j[k]+l,
39049b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),-PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
39149b5e25fSSatish Balay             } else if (PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
392a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
39349b5e25fSSatish Balay             }
39449b5e25fSSatish Balay #else
39549b5e25fSSatish Balay             if (a->a[bs2*k + l*bs + j] != 0.0) {
396a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
39749b5e25fSSatish Balay             }
39849b5e25fSSatish Balay #endif
39949b5e25fSSatish Balay           }
40049b5e25fSSatish Balay         }
401b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
40249b5e25fSSatish Balay       }
40349b5e25fSSatish Balay     }
404d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
405c1490034SHong Zhang   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
406c1490034SHong Zhang      PetscFunctionReturn(0);
40749b5e25fSSatish Balay   } else {
408d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4097566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
4102c990fa1SHong Zhang     if (A->factortype){ /* for factored matrix */
4112c990fa1SHong Zhang       if (bs>1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"matrix is factored with bs>1. Not implemented yet");
4122c990fa1SHong Zhang 
413121deb67SSatish Balay       diag=a->diag;
414121deb67SSatish Balay       for (i=0; i<a->mbs; i++) { /* for row block i */
4152c990fa1SHong Zhang         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
4162c990fa1SHong Zhang         /* diagonal entry */
4172c990fa1SHong Zhang #if defined(PETSC_USE_COMPLEX)
4182c990fa1SHong Zhang         if (PetscImaginaryPart(a->a[diag[i]]) > 0.0) {
419ca0704adSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",a->j[diag[i]],PetscRealPart(1.0/a->a[diag[i]]),PetscImaginaryPart(1.0/a->a[diag[i]]));CHKERRQ(ierr);
4202c990fa1SHong Zhang         } else if (PetscImaginaryPart(a->a[diag[i]]) < 0.0) {
421ca0704adSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",a->j[diag[i]],PetscRealPart(1.0/a->a[diag[i]]),-PetscImaginaryPart(1.0/a->a[diag[i]]));CHKERRQ(ierr);
4222c990fa1SHong Zhang         } else {
4232c990fa1SHong Zhang           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[diag[i]],PetscRealPart(1.0/a->a[diag[i]]));CHKERRQ(ierr);
4242c990fa1SHong Zhang         }
4252c990fa1SHong Zhang #else
4262c990fa1SHong Zhang         ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[diag[i]],1.0/a->a[diag[i]]);CHKERRQ(ierr);
4272c990fa1SHong Zhang #endif
4282c990fa1SHong Zhang         /* off-diagonal entries */
4292c990fa1SHong Zhang         for (k=a->i[i]; k<a->i[i+1]-1; k++) {
4302c990fa1SHong Zhang #if defined(PETSC_USE_COMPLEX)
431ca0704adSBarry Smith           if (PetscImaginaryPart(a->a[k]) > 0.0) {
432ca0704adSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",bs*a->j[k],PetscRealPart(a->a[k]),PetscImaginaryPart(a->a[k]));CHKERRQ(ierr);
433ca0704adSBarry Smith           } else if (PetscImaginaryPart(a->a[k]) < 0.0) {
434ca0704adSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",bs*a->j[k],PetscRealPart(a->a[k]),-PetscImaginaryPart(a->a[k]));CHKERRQ(ierr);
4352c990fa1SHong Zhang           } else {
436ca0704adSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k],PetscRealPart(a->a[k]));CHKERRQ(ierr);
4372c990fa1SHong Zhang           }
4382c990fa1SHong Zhang #else
4392c990fa1SHong Zhang           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[k],a->a[k]);CHKERRQ(ierr);
4402c990fa1SHong Zhang #endif
4412c990fa1SHong Zhang         }
4422c990fa1SHong Zhang         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
4432c990fa1SHong Zhang       }
4442c990fa1SHong Zhang 
4452c990fa1SHong Zhang     } else { /* for non-factored matrix */
4460c74a584SJed Brown       for (i=0; i<a->mbs; i++) { /* for row block i */
4470c74a584SJed Brown         for (j=0; j<bs; j++) {   /* for row bs*i + j */
44877431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
4490c74a584SJed Brown           for (k=a->i[i]; k<a->i[i+1]; k++) { /* for column block */
4500c74a584SJed Brown             for (l=0; l<bs; l++) {            /* for column */
45149b5e25fSSatish Balay #if defined(PETSC_USE_COMPLEX)
45249b5e25fSSatish Balay               if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0) {
453a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",bs*a->j[k]+l,
45449b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
45549b5e25fSSatish Balay               } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0) {
456a83599f4SBarry Smith                 ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",bs*a->j[k]+l,
45749b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),-PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
45849b5e25fSSatish Balay               } else {
459a83599f4SBarry Smith                 ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
46049b5e25fSSatish Balay               }
46149b5e25fSSatish Balay #else
462e9f7bc9eSHong Zhang               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
46349b5e25fSSatish Balay #endif
46449b5e25fSSatish Balay             }
46549b5e25fSSatish Balay           }
466b0a32e0cSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
46749b5e25fSSatish Balay         }
46849b5e25fSSatish Balay       }
4692c990fa1SHong Zhang     }
470d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
47149b5e25fSSatish Balay   }
472b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
47349b5e25fSSatish Balay   PetscFunctionReturn(0);
47449b5e25fSSatish Balay }
47549b5e25fSSatish Balay 
4764a2ae208SSatish Balay #undef __FUNCT__
4774a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_Draw_Zoom"
4786849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
47949b5e25fSSatish Balay {
48049b5e25fSSatish Balay   Mat            A = (Mat) Aa;
48149b5e25fSSatish Balay   Mat_SeqSBAIJ   *a=(Mat_SeqSBAIJ*)A->data;
4826849ba73SBarry Smith   PetscErrorCode ierr;
483d0f46423SBarry Smith   PetscInt       row,i,j,k,l,mbs=a->mbs,color,bs=A->rmap->bs,bs2=a->bs2;
48413f74950SBarry Smith   PetscMPIInt    rank;
48549b5e25fSSatish Balay   PetscReal      xl,yl,xr,yr,x_l,x_r,y_l,y_r;
48649b5e25fSSatish Balay   MatScalar      *aa;
48749b5e25fSSatish Balay   MPI_Comm       comm;
488b0a32e0cSBarry Smith   PetscViewer    viewer;
48949b5e25fSSatish Balay 
49049b5e25fSSatish Balay   PetscFunctionBegin;
49149b5e25fSSatish Balay   /*
49249b5e25fSSatish Balay     This is nasty. If this is called from an originally parallel matrix
49349b5e25fSSatish Balay     then all processes call this,but only the first has the matrix so the
49449b5e25fSSatish Balay     rest should return immediately.
49549b5e25fSSatish Balay   */
49649b5e25fSSatish Balay   ierr = PetscObjectGetComm((PetscObject)draw,&comm);CHKERRQ(ierr);
49749b5e25fSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
49849b5e25fSSatish Balay   if (rank) PetscFunctionReturn(0);
49949b5e25fSSatish Balay 
50049b5e25fSSatish Balay   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
50149b5e25fSSatish Balay 
502b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
503b0a32e0cSBarry Smith   PetscDrawString(draw, .3*(xl+xr), .3*(yl+yr), PETSC_DRAW_BLACK, "symmetric");
50449b5e25fSSatish Balay 
50549b5e25fSSatish Balay   /* loop over matrix elements drawing boxes */
506b0a32e0cSBarry Smith   color = PETSC_DRAW_BLUE;
50749b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
50849b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
509d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
51049b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
51149b5e25fSSatish Balay       aa = a->a + j*bs2;
51249b5e25fSSatish Balay       for (k=0; k<bs; k++) {
51349b5e25fSSatish Balay         for (l=0; l<bs; l++) {
51449b5e25fSSatish Balay           if (PetscRealPart(*aa++) >=  0.) continue;
515b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
51649b5e25fSSatish Balay         }
51749b5e25fSSatish Balay       }
51849b5e25fSSatish Balay     }
51949b5e25fSSatish Balay   }
520b0a32e0cSBarry Smith   color = PETSC_DRAW_CYAN;
52149b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
52249b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
523d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
52449b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
52549b5e25fSSatish Balay       aa = a->a + j*bs2;
52649b5e25fSSatish Balay       for (k=0; k<bs; k++) {
52749b5e25fSSatish Balay         for (l=0; l<bs; l++) {
52849b5e25fSSatish Balay           if (PetscRealPart(*aa++) != 0.) continue;
529b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
53049b5e25fSSatish Balay         }
53149b5e25fSSatish Balay       }
53249b5e25fSSatish Balay     }
53349b5e25fSSatish Balay   }
53449b5e25fSSatish Balay 
535b0a32e0cSBarry Smith   color = PETSC_DRAW_RED;
53649b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
53749b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
538d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
53949b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
54049b5e25fSSatish Balay       aa = a->a + j*bs2;
54149b5e25fSSatish Balay       for (k=0; k<bs; k++) {
54249b5e25fSSatish Balay         for (l=0; l<bs; l++) {
54349b5e25fSSatish Balay           if (PetscRealPart(*aa++) <= 0.) continue;
544b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
54549b5e25fSSatish Balay         }
54649b5e25fSSatish Balay       }
54749b5e25fSSatish Balay     }
54849b5e25fSSatish Balay   }
54949b5e25fSSatish Balay   PetscFunctionReturn(0);
55049b5e25fSSatish Balay }
55149b5e25fSSatish Balay 
5524a2ae208SSatish Balay #undef __FUNCT__
5534a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_Draw"
5546849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_Draw(Mat A,PetscViewer viewer)
55549b5e25fSSatish Balay {
556dfbe8321SBarry Smith   PetscErrorCode ierr;
55749b5e25fSSatish Balay   PetscReal      xl,yl,xr,yr,w,h;
558b0a32e0cSBarry Smith   PetscDraw      draw;
559ace3abfcSBarry Smith   PetscBool      isnull;
56049b5e25fSSatish Balay 
56149b5e25fSSatish Balay   PetscFunctionBegin;
562b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
563b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
56449b5e25fSSatish Balay 
56549b5e25fSSatish Balay   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
566d0f46423SBarry Smith   xr  = A->rmap->N; yr = A->rmap->N; h = yr/10.0; w = xr/10.0;
56749b5e25fSSatish Balay   xr += w;    yr += h;  xl = -w;     yl = -h;
568b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
569b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqSBAIJ_Draw_Zoom,A);CHKERRQ(ierr);
57049b5e25fSSatish Balay   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
57149b5e25fSSatish Balay   PetscFunctionReturn(0);
57249b5e25fSSatish Balay }
57349b5e25fSSatish Balay 
5744a2ae208SSatish Balay #undef __FUNCT__
5754a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ"
576dfbe8321SBarry Smith PetscErrorCode MatView_SeqSBAIJ(Mat A,PetscViewer viewer)
57749b5e25fSSatish Balay {
578dfbe8321SBarry Smith   PetscErrorCode ierr;
579ace3abfcSBarry Smith   PetscBool      iascii,isdraw;
58008917f38SBarry Smith   FILE           *file = 0;
58149b5e25fSSatish Balay 
58249b5e25fSSatish Balay   PetscFunctionBegin;
5832692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
5842692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
58532077d6dSBarry Smith   if (iascii){
58649b5e25fSSatish Balay     ierr = MatView_SeqSBAIJ_ASCII(A,viewer);CHKERRQ(ierr);
58749b5e25fSSatish Balay   } else if (isdraw) {
58849b5e25fSSatish Balay     ierr = MatView_SeqSBAIJ_Draw(A,viewer);CHKERRQ(ierr);
58949b5e25fSSatish Balay   } else {
590a5e6ed63SBarry Smith     Mat B;
591ceb03754SKris Buschelman     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&B);CHKERRQ(ierr);
592a5e6ed63SBarry Smith     ierr = MatView(B,viewer);CHKERRQ(ierr);
5936bf464f9SBarry Smith     ierr = MatDestroy(&B);CHKERRQ(ierr);
59408917f38SBarry Smith     ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
59508917f38SBarry Smith     if (file) {
59608917f38SBarry Smith       fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
59708917f38SBarry Smith     }
59849b5e25fSSatish Balay   }
59949b5e25fSSatish Balay   PetscFunctionReturn(0);
60049b5e25fSSatish Balay }
60149b5e25fSSatish Balay 
60249b5e25fSSatish Balay 
6034a2ae208SSatish Balay #undef __FUNCT__
6044a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqSBAIJ"
60513f74950SBarry Smith PetscErrorCode MatGetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
60649b5e25fSSatish Balay {
607045c9aa0SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
60813f74950SBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
60913f74950SBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
610d0f46423SBarry Smith   PetscInt     brow,bcol,ridx,cidx,bs=A->rmap->bs,bs2=a->bs2;
61197e567efSBarry Smith   MatScalar    *ap,*aa = a->a;
61249b5e25fSSatish Balay 
61349b5e25fSSatish Balay   PetscFunctionBegin;
61449b5e25fSSatish Balay   for (k=0; k<m; k++) { /* loop over rows */
61549b5e25fSSatish Balay     row  = im[k]; brow = row/bs;
616e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
617e32f2f54SBarry 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);
61849b5e25fSSatish Balay     rp   = aj + ai[brow] ; ap = aa + bs2*ai[brow] ;
61949b5e25fSSatish Balay     nrow = ailen[brow];
62049b5e25fSSatish Balay     for (l=0; l<n; l++) { /* loop over columns */
621e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
622e32f2f54SBarry 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);
62349b5e25fSSatish Balay       col  = in[l] ;
62449b5e25fSSatish Balay       bcol = col/bs;
62549b5e25fSSatish Balay       cidx = col%bs;
62649b5e25fSSatish Balay       ridx = row%bs;
62749b5e25fSSatish Balay       high = nrow;
62849b5e25fSSatish Balay       low  = 0; /* assume unsorted */
62949b5e25fSSatish Balay       while (high-low > 5) {
63049b5e25fSSatish Balay         t = (low+high)/2;
63149b5e25fSSatish Balay         if (rp[t] > bcol) high = t;
63249b5e25fSSatish Balay         else             low  = t;
63349b5e25fSSatish Balay       }
63449b5e25fSSatish Balay       for (i=low; i<high; i++) {
63549b5e25fSSatish Balay         if (rp[i] > bcol) break;
63649b5e25fSSatish Balay         if (rp[i] == bcol) {
63749b5e25fSSatish Balay           *v++ = ap[bs2*i+bs*cidx+ridx];
63849b5e25fSSatish Balay           goto finished;
63949b5e25fSSatish Balay         }
64049b5e25fSSatish Balay       }
64197e567efSBarry Smith       *v++ = 0.0;
64249b5e25fSSatish Balay       finished:;
64349b5e25fSSatish Balay     }
64449b5e25fSSatish Balay   }
64549b5e25fSSatish Balay   PetscFunctionReturn(0);
64649b5e25fSSatish Balay }
64749b5e25fSSatish Balay 
64849b5e25fSSatish Balay 
6494a2ae208SSatish Balay #undef __FUNCT__
6504a2ae208SSatish Balay #define __FUNCT__ "MatSetValuesBlocked_SeqSBAIJ"
65113f74950SBarry Smith PetscErrorCode MatSetValuesBlocked_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
65249b5e25fSSatish Balay {
6530880e062SHong Zhang   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
6546849ba73SBarry Smith   PetscErrorCode    ierr;
655e2ee6c50SBarry Smith   PetscInt          *rp,k,low,high,t,ii,jj,row,nrow,i,col,l,rmax,N,lastcol = -1;
65613f74950SBarry Smith   PetscInt          *imax=a->imax,*ai=a->i,*ailen=a->ilen;
657d0f46423SBarry Smith   PetscInt          *aj=a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs,stepval;
658ace3abfcSBarry Smith   PetscBool         roworiented=a->roworiented;
659dd6ea824SBarry Smith   const PetscScalar *value = v;
660f15d580aSBarry Smith   MatScalar         *ap,*aa = a->a,*bap;
6610880e062SHong Zhang 
66249b5e25fSSatish Balay   PetscFunctionBegin;
6630880e062SHong Zhang   if (roworiented) {
6640880e062SHong Zhang     stepval = (n-1)*bs;
6650880e062SHong Zhang   } else {
6660880e062SHong Zhang     stepval = (m-1)*bs;
6670880e062SHong Zhang   }
6680880e062SHong Zhang   for (k=0; k<m; k++) { /* loop over added rows */
6690880e062SHong Zhang     row  = im[k];
6700880e062SHong Zhang     if (row < 0) continue;
6712515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
672e32f2f54SBarry Smith     if (row >= a->mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,a->mbs-1);
6730880e062SHong Zhang #endif
6740880e062SHong Zhang     rp   = aj + ai[row];
6750880e062SHong Zhang     ap   = aa + bs2*ai[row];
6760880e062SHong Zhang     rmax = imax[row];
6770880e062SHong Zhang     nrow = ailen[row];
6780880e062SHong Zhang     low  = 0;
679818f2c47SBarry Smith     high = nrow;
6800880e062SHong Zhang     for (l=0; l<n; l++) { /* loop over added columns */
6810880e062SHong Zhang       if (in[l] < 0) continue;
6820880e062SHong Zhang       col = in[l];
6832515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
684e32f2f54SBarry Smith       if (col >= a->nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",col,a->nbs-1);
685b1823623SSatish Balay #endif
686b98bf0e1SJed Brown       if (col < row) {
687b98bf0e1SJed Brown         if (a->ignore_ltriangular) {
688b98bf0e1SJed Brown           continue; /* ignore lower triangular block */
689b98bf0e1SJed Brown         } else {
690e32f2f54SBarry 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)");
691b98bf0e1SJed Brown         }
692b98bf0e1SJed Brown       }
6930880e062SHong Zhang       if (roworiented) {
6940880e062SHong Zhang         value = v + k*(stepval+bs)*bs + l*bs;
6950880e062SHong Zhang       } else {
6960880e062SHong Zhang         value = v + l*(stepval+bs)*bs + k*bs;
6970880e062SHong Zhang       }
6987cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
699e2ee6c50SBarry Smith       lastcol = col;
7000880e062SHong Zhang       while (high-low > 7) {
7010880e062SHong Zhang         t = (low+high)/2;
7020880e062SHong Zhang         if (rp[t] > col) high = t;
7030880e062SHong Zhang         else             low  = t;
7040880e062SHong Zhang       }
7050880e062SHong Zhang       for (i=low; i<high; i++) {
7060880e062SHong Zhang         if (rp[i] > col) break;
7070880e062SHong Zhang         if (rp[i] == col) {
7080880e062SHong Zhang           bap  = ap +  bs2*i;
7090880e062SHong Zhang           if (roworiented) {
7100880e062SHong Zhang             if (is == ADD_VALUES) {
7110880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
7120880e062SHong Zhang                 for (jj=ii; jj<bs2; jj+=bs) {
7130880e062SHong Zhang                   bap[jj] += *value++;
7140880e062SHong Zhang                 }
7150880e062SHong Zhang               }
7160880e062SHong Zhang             } else {
7170880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
7180880e062SHong Zhang                 for (jj=ii; jj<bs2; jj+=bs) {
7190880e062SHong Zhang                   bap[jj] = *value++;
7200880e062SHong Zhang                 }
7210880e062SHong Zhang                }
7220880e062SHong Zhang             }
7230880e062SHong Zhang           } else {
7240880e062SHong Zhang             if (is == ADD_VALUES) {
7250880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
7260880e062SHong Zhang                 for (jj=0; jj<bs; jj++) {
7270880e062SHong Zhang                   *bap++ += *value++;
7280880e062SHong Zhang                 }
7290880e062SHong Zhang               }
7300880e062SHong Zhang             } else {
7310880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
7320880e062SHong Zhang                 for (jj=0; jj<bs; jj++) {
7330880e062SHong Zhang                   *bap++  = *value++;
7340880e062SHong Zhang                 }
7350880e062SHong Zhang               }
7360880e062SHong Zhang             }
7370880e062SHong Zhang           }
7380880e062SHong Zhang           goto noinsert2;
7390880e062SHong Zhang         }
7400880e062SHong Zhang       }
7410880e062SHong Zhang       if (nonew == 1) goto noinsert2;
742e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
743fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
744c03d1d03SSatish Balay       N = nrow++ - 1; high++;
7450880e062SHong Zhang       /* shift up all the later entries in this row */
7460880e062SHong Zhang       for (ii=N; ii>=i; ii--) {
7470880e062SHong Zhang         rp[ii+1] = rp[ii];
7480880e062SHong Zhang         ierr = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
7490880e062SHong Zhang       }
7500880e062SHong Zhang       if (N >= i) {
7510880e062SHong Zhang         ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
7520880e062SHong Zhang       }
7530880e062SHong Zhang       rp[i] = col;
7540880e062SHong Zhang       bap   = ap +  bs2*i;
7550880e062SHong Zhang       if (roworiented) {
7560880e062SHong Zhang         for (ii=0; ii<bs; ii++,value+=stepval) {
7570880e062SHong Zhang           for (jj=ii; jj<bs2; jj+=bs) {
7580880e062SHong Zhang             bap[jj] = *value++;
7590880e062SHong Zhang           }
7600880e062SHong Zhang         }
7610880e062SHong Zhang       } else {
7620880e062SHong Zhang         for (ii=0; ii<bs; ii++,value+=stepval) {
7630880e062SHong Zhang           for (jj=0; jj<bs; jj++) {
7640880e062SHong Zhang             *bap++  = *value++;
7650880e062SHong Zhang           }
7660880e062SHong Zhang         }
7670880e062SHong Zhang        }
7680880e062SHong Zhang     noinsert2:;
7690880e062SHong Zhang       low = i;
7700880e062SHong Zhang     }
7710880e062SHong Zhang     ailen[row] = nrow;
7720880e062SHong Zhang   }
7730880e062SHong Zhang    PetscFunctionReturn(0);
77449b5e25fSSatish Balay }
77549b5e25fSSatish Balay 
77664831d72SBarry Smith /*
77764831d72SBarry Smith     This is not yet used
77864831d72SBarry Smith */
7794a2ae208SSatish Balay #undef __FUNCT__
7804108e4d5SBarry Smith #define __FUNCT__ "MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode"
7814108e4d5SBarry Smith PetscErrorCode MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode(Mat A)
7820def2e27SBarry Smith {
7830def2e27SBarry Smith   Mat_SeqSBAIJ    *a = (Mat_SeqSBAIJ*)A->data;
7840def2e27SBarry Smith   PetscErrorCode  ierr;
7850def2e27SBarry Smith   const PetscInt  *ai = a->i, *aj = a->j,*cols;
7860def2e27SBarry Smith   PetscInt        i = 0,j,blk_size,m = A->rmap->n,node_count = 0,nzx,nzy,*ns,row,nz,cnt,cnt2,*counts;
787ace3abfcSBarry Smith   PetscBool       flag;
7880def2e27SBarry Smith 
7890def2e27SBarry Smith   PetscFunctionBegin;
7900def2e27SBarry Smith   ierr = PetscMalloc(m*sizeof(PetscInt),&ns);CHKERRQ(ierr);
7910def2e27SBarry Smith   while (i < m){
7920def2e27SBarry Smith     nzx = ai[i+1] - ai[i];       /* Number of nonzeros */
7930def2e27SBarry Smith     /* Limits the number of elements in a node to 'a->inode.limit' */
7940def2e27SBarry Smith     for (j=i+1,blk_size=1; j<m && blk_size <a->inode.limit; ++j,++blk_size) {
7950def2e27SBarry Smith       nzy  = ai[j+1] - ai[j];
7960def2e27SBarry Smith       if (nzy != (nzx - j + i)) break;
7970def2e27SBarry Smith       ierr = PetscMemcmp(aj + ai[i] + j - i,aj + ai[j],nzy*sizeof(PetscInt),&flag);CHKERRQ(ierr);
7980def2e27SBarry Smith       if (!flag) break;
7990def2e27SBarry Smith     }
8000def2e27SBarry Smith     ns[node_count++] = blk_size;
8010def2e27SBarry Smith     i = j;
8020def2e27SBarry Smith   }
8030def2e27SBarry Smith   if (!a->inode.size && m && node_count > .9*m) {
8040def2e27SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
8050def2e27SBarry Smith     ierr = PetscInfo2(A,"Found %D nodes out of %D rows. Not using Inode routines\n",node_count,m);CHKERRQ(ierr);
8060def2e27SBarry Smith   } else {
8070def2e27SBarry Smith     a->inode.node_count = node_count;
8080def2e27SBarry Smith     ierr = PetscMalloc(node_count*sizeof(PetscInt),&a->inode.size);CHKERRQ(ierr);
809c760cd28SBarry Smith     ierr = PetscLogObjectMemory(A,node_count*sizeof(PetscInt));CHKERRQ(ierr);
8100def2e27SBarry Smith     ierr = PetscMemcpy(a->inode.size,ns,node_count*sizeof(PetscInt));
8110def2e27SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
8120def2e27SBarry Smith     ierr = PetscInfo3(A,"Found %D nodes of %D. Limit used: %D. Using Inode routines\n",node_count,m,a->inode.limit);CHKERRQ(ierr);
8130def2e27SBarry Smith 
8140def2e27SBarry Smith     /* count collections of adjacent columns in each inode */
8150def2e27SBarry Smith     row = 0;
8160def2e27SBarry Smith     cnt = 0;
8170def2e27SBarry Smith     for (i=0; i<node_count; i++) {
8180def2e27SBarry Smith       cols = aj + ai[row] + a->inode.size[i];
8190def2e27SBarry Smith       nz   = ai[row+1] - ai[row] - a->inode.size[i];
8200def2e27SBarry Smith       for (j=1; j<nz; j++) {
8210def2e27SBarry Smith         if (cols[j] != cols[j-1]+1) {
8220def2e27SBarry Smith           cnt++;
8230def2e27SBarry Smith         }
8240def2e27SBarry Smith       }
8250def2e27SBarry Smith       cnt++;
8260def2e27SBarry Smith       row += a->inode.size[i];
8270def2e27SBarry Smith     }
8280def2e27SBarry Smith     ierr = PetscMalloc(2*cnt*sizeof(PetscInt),&counts);CHKERRQ(ierr);
8290def2e27SBarry Smith     cnt = 0;
8300def2e27SBarry Smith     row = 0;
8310def2e27SBarry Smith     for (i=0; i<node_count; i++) {
8320def2e27SBarry Smith       cols          = aj + ai[row] + a->inode.size[i];
8330def2e27SBarry Smith 	  CHKMEMQ;
8340def2e27SBarry Smith       counts[2*cnt] = cols[0];
8350def2e27SBarry Smith 	  CHKMEMQ;
8360def2e27SBarry Smith       nz            = ai[row+1] - ai[row] - a->inode.size[i];
8370def2e27SBarry Smith       cnt2          = 1;
8380def2e27SBarry Smith       for (j=1; j<nz; j++) {
8390def2e27SBarry Smith         if (cols[j] != cols[j-1]+1) {
8400def2e27SBarry Smith 	  CHKMEMQ;
8410def2e27SBarry Smith           counts[2*(cnt++)+1] = cnt2;
8420def2e27SBarry Smith           counts[2*cnt]       = cols[j];
8430def2e27SBarry Smith 	  CHKMEMQ;
8440def2e27SBarry Smith           cnt2                = 1;
8450def2e27SBarry Smith         } else cnt2++;
8460def2e27SBarry Smith       }
8470def2e27SBarry Smith 	  CHKMEMQ;
8480def2e27SBarry Smith       counts[2*(cnt++)+1] = cnt2;
8490def2e27SBarry Smith 	  CHKMEMQ;
8500def2e27SBarry Smith       row += a->inode.size[i];
8510def2e27SBarry Smith     }
8520def2e27SBarry Smith     ierr = PetscIntView(2*cnt,counts,0);
8530def2e27SBarry Smith   }
85438702af4SBarry Smith   PetscFunctionReturn(0);
85538702af4SBarry Smith }
85638702af4SBarry Smith 
85738702af4SBarry Smith #undef __FUNCT__
8584a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqSBAIJ"
859dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqSBAIJ(Mat A,MatAssemblyType mode)
86049b5e25fSSatish Balay {
86149b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
8626849ba73SBarry Smith   PetscErrorCode ierr;
86313f74950SBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
864d0f46423SBarry Smith   PetscInt       m = A->rmap->N,*ip,N,*ailen = a->ilen;
86513f74950SBarry Smith   PetscInt       mbs = a->mbs,bs2 = a->bs2,rmax = 0;
86649b5e25fSSatish Balay   MatScalar      *aa = a->a,*ap;
86749b5e25fSSatish Balay 
86849b5e25fSSatish Balay   PetscFunctionBegin;
86949b5e25fSSatish Balay   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
87049b5e25fSSatish Balay 
87149b5e25fSSatish Balay   if (m) rmax = ailen[0];
87249b5e25fSSatish Balay   for (i=1; i<mbs; i++) {
87349b5e25fSSatish Balay     /* move each row back by the amount of empty slots (fshift) before it*/
87449b5e25fSSatish Balay     fshift += imax[i-1] - ailen[i-1];
87549b5e25fSSatish Balay      rmax   = PetscMax(rmax,ailen[i]);
87649b5e25fSSatish Balay      if (fshift) {
87749b5e25fSSatish Balay        ip = aj + ai[i]; ap = aa + bs2*ai[i];
87849b5e25fSSatish Balay        N = ailen[i];
87949b5e25fSSatish Balay        for (j=0; j<N; j++) {
88049b5e25fSSatish Balay          ip[j-fshift] = ip[j];
88149b5e25fSSatish Balay          ierr = PetscMemcpy(ap+(j-fshift)*bs2,ap+j*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
88249b5e25fSSatish Balay        }
88349b5e25fSSatish Balay      }
88449b5e25fSSatish Balay      ai[i] = ai[i-1] + ailen[i-1];
88549b5e25fSSatish Balay   }
88649b5e25fSSatish Balay   if (mbs) {
88749b5e25fSSatish Balay     fshift += imax[mbs-1] - ailen[mbs-1];
88849b5e25fSSatish Balay      ai[mbs] = ai[mbs-1] + ailen[mbs-1];
88949b5e25fSSatish Balay   }
89049b5e25fSSatish Balay   /* reset ilen and imax for each row */
89149b5e25fSSatish Balay   for (i=0; i<mbs; i++) {
89249b5e25fSSatish Balay     ailen[i] = imax[i] = ai[i+1] - ai[i];
89349b5e25fSSatish Balay   }
8946c6c5352SBarry Smith   a->nz = ai[mbs];
89549b5e25fSSatish Balay 
896b424e231SHong Zhang   /* diagonals may have moved, reset it */
897b424e231SHong Zhang   if (a->diag) {
8982ed38d0bSJed Brown     ierr = PetscMemcpy(a->diag,ai,mbs*sizeof(PetscInt));CHKERRQ(ierr);
89949b5e25fSSatish Balay   }
90028b2fa4aSMatthew Knepley   if (fshift && a->nounused == -1) {
901e32f2f54SBarry 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);
90228b2fa4aSMatthew Knepley   }
903d0f46423SBarry 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);
904ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues is %D\n",a->reallocs);CHKERRQ(ierr);
905ae15b995SBarry Smith   ierr = PetscInfo1(A,"Most nonzeros blocks in any row is %D\n",rmax);CHKERRQ(ierr);
9068e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
90749b5e25fSSatish Balay   a->reallocs          = 0;
90849b5e25fSSatish Balay   A->info.nz_unneeded  = (PetscReal)fshift*bs2;
909061b2667SBarry Smith   a->idiagvalid = PETSC_FALSE;
91038702af4SBarry Smith 
91138702af4SBarry Smith   if (A->cmap->n < 65536 && A->cmap->bs == 1) {
91217803ae8SHong Zhang     if (a->jshort){
91317803ae8SHong Zhang       /* when matrix data structure is changed, previous jshort must be replaced */
91417803ae8SHong Zhang       ierr = PetscFree(a->jshort);CHKERRQ(ierr);
91517803ae8SHong Zhang     }
91638702af4SBarry Smith     ierr = PetscMalloc(a->i[A->rmap->n]*sizeof(unsigned short),&a->jshort);CHKERRQ(ierr);
917c760cd28SBarry Smith     ierr = PetscLogObjectMemory(A,a->i[A->rmap->n]*sizeof(unsigned short));CHKERRQ(ierr);
91838702af4SBarry Smith     for (i=0; i<a->i[A->rmap->n]; i++) a->jshort[i] = a->j[i];
91938702af4SBarry Smith     A->ops->mult  = MatMult_SeqSBAIJ_1_ushort;
92041f059aeSBarry Smith     A->ops->sor = MatSOR_SeqSBAIJ_ushort;
9214da8f245SBarry Smith     a->free_jshort = PETSC_TRUE;
92238702af4SBarry Smith   }
92349b5e25fSSatish Balay   PetscFunctionReturn(0);
92449b5e25fSSatish Balay }
92549b5e25fSSatish Balay 
92649b5e25fSSatish Balay /*
92749b5e25fSSatish Balay    This function returns an array of flags which indicate the locations of contiguous
92849b5e25fSSatish Balay    blocks that should be zeroed. for eg: if bs = 3  and is = [0,1,2,3,5,6,7,8,9]
92949b5e25fSSatish Balay    then the resulting sizes = [3,1,1,3,1] correspondig to sets [(0,1,2),(3),(5),(6,7,8),(9)]
93049b5e25fSSatish Balay    Assume: sizes should be long enough to hold all the values.
93149b5e25fSSatish Balay */
9324a2ae208SSatish Balay #undef __FUNCT__
9334a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqSBAIJ_Check_Blocks"
93413f74950SBarry Smith PetscErrorCode MatZeroRows_SeqSBAIJ_Check_Blocks(PetscInt idx[],PetscInt n,PetscInt bs,PetscInt sizes[], PetscInt *bs_max)
93549b5e25fSSatish Balay {
93613f74950SBarry Smith   PetscInt   i,j,k,row;
937ace3abfcSBarry Smith   PetscBool  flg;
93849b5e25fSSatish Balay 
93949b5e25fSSatish Balay   PetscFunctionBegin;
94049b5e25fSSatish Balay    for (i=0,j=0; i<n; j++) {
94149b5e25fSSatish Balay      row = idx[i];
94249b5e25fSSatish Balay      if (row%bs!=0) { /* Not the begining of a block */
94349b5e25fSSatish Balay        sizes[j] = 1;
94449b5e25fSSatish Balay        i++;
94549b5e25fSSatish Balay      } else if (i+bs > n) { /* Beginning of a block, but complete block doesn't exist (at idx end) */
94649b5e25fSSatish Balay        sizes[j] = 1;         /* Also makes sure atleast 'bs' values exist for next else */
94749b5e25fSSatish Balay        i++;
94849b5e25fSSatish Balay      } else { /* Begining of the block, so check if the complete block exists */
94949b5e25fSSatish Balay        flg = PETSC_TRUE;
95049b5e25fSSatish Balay        for (k=1; k<bs; k++) {
95149b5e25fSSatish Balay          if (row+k != idx[i+k]) { /* break in the block */
95249b5e25fSSatish Balay            flg = PETSC_FALSE;
95349b5e25fSSatish Balay            break;
95449b5e25fSSatish Balay          }
95549b5e25fSSatish Balay        }
956abc0a331SBarry Smith        if (flg) { /* No break in the bs */
95749b5e25fSSatish Balay          sizes[j] = bs;
95849b5e25fSSatish Balay          i+= bs;
95949b5e25fSSatish Balay        } else {
96049b5e25fSSatish Balay          sizes[j] = 1;
96149b5e25fSSatish Balay          i++;
96249b5e25fSSatish Balay        }
96349b5e25fSSatish Balay      }
96449b5e25fSSatish Balay    }
96549b5e25fSSatish Balay    *bs_max = j;
96649b5e25fSSatish Balay    PetscFunctionReturn(0);
96749b5e25fSSatish Balay }
96849b5e25fSSatish Balay 
96949b5e25fSSatish Balay 
97049b5e25fSSatish Balay /* Only add/insert a(i,j) with i<=j (blocks).
97149b5e25fSSatish Balay    Any a(i,j) with i>j input by user is ingored.
97249b5e25fSSatish Balay */
97349b5e25fSSatish Balay 
9744a2ae208SSatish Balay #undef __FUNCT__
9754a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqSBAIJ"
97613f74950SBarry Smith PetscErrorCode MatSetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
97749b5e25fSSatish Balay {
97849b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
9796849ba73SBarry Smith   PetscErrorCode ierr;
980e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,lastcol = -1;
98113f74950SBarry Smith   PetscInt       *imax=a->imax,*ai=a->i,*ailen=a->ilen,roworiented=a->roworiented;
982d0f46423SBarry Smith   PetscInt       *aj=a->j,nonew=a->nonew,bs=A->rmap->bs,brow,bcol;
98313f74950SBarry Smith   PetscInt       ridx,cidx,bs2=a->bs2;
98449b5e25fSSatish Balay   MatScalar      *ap,value,*aa=a->a,*bap;
98549b5e25fSSatish Balay 
98649b5e25fSSatish Balay   PetscFunctionBegin;
98771fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
98849b5e25fSSatish Balay   for (k=0; k<m; k++) { /* loop over added rows */
98949b5e25fSSatish Balay     row  = im[k];       /* row number */
99049b5e25fSSatish Balay     brow = row/bs;      /* block row number */
99149b5e25fSSatish Balay     if (row < 0) continue;
9922515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
993e32f2f54SBarry 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);
99449b5e25fSSatish Balay #endif
99549b5e25fSSatish Balay     rp   = aj + ai[brow]; /*ptr to beginning of column value of the row block*/
99649b5e25fSSatish Balay     ap   = aa + bs2*ai[brow]; /*ptr to beginning of element value of the row block*/
99749b5e25fSSatish Balay     rmax = imax[brow];  /* maximum space allocated for this row */
99849b5e25fSSatish Balay     nrow = ailen[brow]; /* actual length of this row */
99949b5e25fSSatish Balay     low  = 0;
100049b5e25fSSatish Balay 
100149b5e25fSSatish Balay     for (l=0; l<n; l++) { /* loop over added columns */
100249b5e25fSSatish Balay       if (in[l] < 0) continue;
10032515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
1004e32f2f54SBarry 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);
100549b5e25fSSatish Balay #endif
100649b5e25fSSatish Balay       col = in[l];
100749b5e25fSSatish Balay       bcol = col/bs;              /* block col number */
100849b5e25fSSatish Balay 
1009941593c8SHong Zhang       if (brow > bcol) {
1010941593c8SHong Zhang         if (a->ignore_ltriangular){
1011941593c8SHong Zhang           continue; /* ignore lower triangular values */
1012941593c8SHong Zhang         } else {
1013e32f2f54SBarry 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)");
1014941593c8SHong Zhang         }
1015941593c8SHong Zhang       }
1016f4989cb3SHong Zhang 
101749b5e25fSSatish Balay       ridx = row % bs; cidx = col % bs; /*row and col index inside the block */
10188549e402SHong Zhang       if ((brow==bcol && ridx<=cidx) || (brow<bcol)){
101949b5e25fSSatish Balay         /* element value a(k,l) */
102049b5e25fSSatish Balay         if (roworiented) {
102149b5e25fSSatish Balay           value = v[l + k*n];
102249b5e25fSSatish Balay         } else {
102349b5e25fSSatish Balay           value = v[k + l*m];
102449b5e25fSSatish Balay         }
102549b5e25fSSatish Balay 
102649b5e25fSSatish Balay         /* move pointer bap to a(k,l) quickly and add/insert value */
10277cd84e04SBarry Smith         if (col <= lastcol) low = 0; high = nrow;
1028e2ee6c50SBarry Smith         lastcol = col;
102949b5e25fSSatish Balay         while (high-low > 7) {
103049b5e25fSSatish Balay           t = (low+high)/2;
103149b5e25fSSatish Balay           if (rp[t] > bcol) high = t;
103249b5e25fSSatish Balay           else              low  = t;
103349b5e25fSSatish Balay         }
103449b5e25fSSatish Balay         for (i=low; i<high; i++) {
103549b5e25fSSatish Balay           if (rp[i] > bcol) break;
103649b5e25fSSatish Balay           if (rp[i] == bcol) {
103749b5e25fSSatish Balay             bap  = ap +  bs2*i + bs*cidx + ridx;
103849b5e25fSSatish Balay             if (is == ADD_VALUES) *bap += value;
103949b5e25fSSatish Balay             else                  *bap  = value;
10408549e402SHong Zhang             /* for diag block, add/insert its symmetric element a(cidx,ridx) */
10418549e402SHong Zhang             if (brow == bcol && ridx < cidx){
10428549e402SHong Zhang               bap  = ap +  bs2*i + bs*ridx + cidx;
10438549e402SHong Zhang               if (is == ADD_VALUES) *bap += value;
10448549e402SHong Zhang               else                  *bap  = value;
10458549e402SHong Zhang             }
104649b5e25fSSatish Balay             goto noinsert1;
104749b5e25fSSatish Balay           }
104849b5e25fSSatish Balay         }
104949b5e25fSSatish Balay 
105049b5e25fSSatish Balay         if (nonew == 1) goto noinsert1;
1051e32f2f54SBarry Smith         if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
1052fef13f97SBarry Smith         MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
105349b5e25fSSatish Balay 
1054c03d1d03SSatish Balay         N = nrow++ - 1; high++;
105549b5e25fSSatish Balay         /* shift up all the later entries in this row */
105649b5e25fSSatish Balay         for (ii=N; ii>=i; ii--) {
105749b5e25fSSatish Balay           rp[ii+1] = rp[ii];
105849b5e25fSSatish Balay           ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
105949b5e25fSSatish Balay         }
106049b5e25fSSatish Balay         if (N>=i) {
106149b5e25fSSatish Balay           ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
106249b5e25fSSatish Balay         }
106349b5e25fSSatish Balay         rp[i]                      = bcol;
106449b5e25fSSatish Balay         ap[bs2*i + bs*cidx + ridx] = value;
106549b5e25fSSatish Balay       noinsert1:;
106649b5e25fSSatish Balay         low = i;
10678549e402SHong Zhang       }
106849b5e25fSSatish Balay     }   /* end of loop over added columns */
106949b5e25fSSatish Balay     ailen[brow] = nrow;
107049b5e25fSSatish Balay   }   /* end of loop over added rows */
107149b5e25fSSatish Balay   PetscFunctionReturn(0);
107249b5e25fSSatish Balay }
107349b5e25fSSatish Balay 
10744a2ae208SSatish Balay #undef __FUNCT__
10754d101231SSatish Balay #define __FUNCT__ "MatICCFactor_SeqSBAIJ"
10760481f469SBarry Smith PetscErrorCode MatICCFactor_SeqSBAIJ(Mat inA,IS row,const MatFactorInfo *info)
107749b5e25fSSatish Balay {
10784ccecd49SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)inA->data;
107949b5e25fSSatish Balay   Mat            outA;
1080dfbe8321SBarry Smith   PetscErrorCode ierr;
1081ace3abfcSBarry Smith   PetscBool      row_identity;
108249b5e25fSSatish Balay 
108349b5e25fSSatish Balay   PetscFunctionBegin;
1084e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 is supported for in-place icc");
1085c84f5b01SHong Zhang   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1086e32f2f54SBarry Smith   if (!row_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix reordering is not supported");
1087e32f2f54SBarry 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()! */
1088c84f5b01SHong Zhang 
108949b5e25fSSatish Balay   outA            = inA;
1090d5f3da31SBarry Smith   inA->factortype = MAT_FACTOR_ICC;
109149b5e25fSSatish Balay 
10921a3463dfSHong Zhang   ierr = MatMarkDiagonal_SeqSBAIJ(inA);CHKERRQ(ierr);
1093d595f711SHong Zhang   ierr = MatSeqSBAIJSetNumericFactorization_inplace(inA,row_identity);CHKERRQ(ierr);
109449b5e25fSSatish Balay 
1095c3122656SLisandro Dalcin   ierr   = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
10966bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
1097c84f5b01SHong Zhang   a->row = row;
1098c3122656SLisandro Dalcin   ierr   = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
10996bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
1100c84f5b01SHong Zhang   a->col = row;
1101c84f5b01SHong Zhang 
1102c84f5b01SHong Zhang   /* Create the invert permutation so that it can be used in MatCholeskyFactorNumeric() */
1103c84f5b01SHong Zhang   if (a->icol) {ierr = ISInvertPermutation(row,PETSC_DECIDE, &a->icol);CHKERRQ(ierr);}
110452e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
110549b5e25fSSatish Balay 
110649b5e25fSSatish Balay   if (!a->solve_work) {
1107d0f46423SBarry Smith     ierr = PetscMalloc((inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
1108d0f46423SBarry Smith     ierr = PetscLogObjectMemory(inA,(inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar));CHKERRQ(ierr);
110949b5e25fSSatish Balay   }
111049b5e25fSSatish Balay 
1111719d5645SBarry Smith   ierr = MatCholeskyFactorNumeric(outA,inA,info);CHKERRQ(ierr);
111249b5e25fSSatish Balay   PetscFunctionReturn(0);
111349b5e25fSSatish Balay }
1114950f1e5bSHong Zhang 
111549b5e25fSSatish Balay EXTERN_C_BEGIN
11164a2ae208SSatish Balay #undef __FUNCT__
11174a2ae208SSatish Balay #define __FUNCT__ "MatSeqSBAIJSetColumnIndices_SeqSBAIJ"
11187087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetColumnIndices_SeqSBAIJ(Mat mat,PetscInt *indices)
111949b5e25fSSatish Balay {
1120045c9aa0SHong Zhang   Mat_SeqSBAIJ   *baij = (Mat_SeqSBAIJ *)mat->data;
112113f74950SBarry Smith   PetscInt       i,nz,n;
11227827cd58SJed Brown   PetscErrorCode ierr;
112349b5e25fSSatish Balay 
112449b5e25fSSatish Balay   PetscFunctionBegin;
11256c6c5352SBarry Smith   nz = baij->maxnz;
1126d0f46423SBarry Smith   n  = mat->cmap->n;
112749b5e25fSSatish Balay   for (i=0; i<nz; i++) {
112849b5e25fSSatish Balay     baij->j[i] = indices[i];
112949b5e25fSSatish Balay   }
11306c6c5352SBarry Smith    baij->nz = nz;
113149b5e25fSSatish Balay    for (i=0; i<n; i++) {
113249b5e25fSSatish Balay      baij->ilen[i] = baij->imax[i];
113349b5e25fSSatish Balay    }
11347827cd58SJed Brown   ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
113549b5e25fSSatish Balay   PetscFunctionReturn(0);
113649b5e25fSSatish Balay }
113749b5e25fSSatish Balay EXTERN_C_END
113849b5e25fSSatish Balay 
11394a2ae208SSatish Balay #undef __FUNCT__
11404a2ae208SSatish Balay #define __FUNCT__ "MatSeqSBAIJSetColumnIndices"
114149b5e25fSSatish Balay /*@
114219585528SSatish Balay   MatSeqSBAIJSetColumnIndices - Set the column indices for all the rows
114349b5e25fSSatish Balay   in the matrix.
114449b5e25fSSatish Balay 
114549b5e25fSSatish Balay   Input Parameters:
114619585528SSatish Balay   +  mat     - the SeqSBAIJ matrix
114749b5e25fSSatish Balay   -  indices - the column indices
114849b5e25fSSatish Balay 
114949b5e25fSSatish Balay   Level: advanced
115049b5e25fSSatish Balay 
115149b5e25fSSatish Balay   Notes:
115249b5e25fSSatish Balay   This can be called if you have precomputed the nonzero structure of the
115349b5e25fSSatish Balay   matrix and want to provide it to the matrix object to improve the performance
115449b5e25fSSatish Balay   of the MatSetValues() operation.
115549b5e25fSSatish Balay 
115649b5e25fSSatish Balay   You MUST have set the correct numbers of nonzeros per row in the call to
1157d1be2dadSMatthew Knepley   MatCreateSeqSBAIJ(), and the columns indices MUST be sorted.
115849b5e25fSSatish Balay 
1159ab9f2c04SSatish Balay   MUST be called before any calls to MatSetValues()
116049b5e25fSSatish Balay 
1161ab9f2c04SSatish Balay   .seealso: MatCreateSeqSBAIJ
116249b5e25fSSatish Balay @*/
11637087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetColumnIndices(Mat mat,PetscInt *indices)
116449b5e25fSSatish Balay {
11654ac538c5SBarry Smith   PetscErrorCode ierr;
116649b5e25fSSatish Balay 
116749b5e25fSSatish Balay   PetscFunctionBegin;
11680700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
11694482741eSBarry Smith   PetscValidPointer(indices,2);
11704ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqSBAIJSetColumnIndices_C",(Mat,PetscInt *),(mat,indices));CHKERRQ(ierr);
117149b5e25fSSatish Balay   PetscFunctionReturn(0);
117249b5e25fSSatish Balay }
117349b5e25fSSatish Balay 
11744a2ae208SSatish Balay #undef __FUNCT__
11753c896bc6SHong Zhang #define __FUNCT__ "MatCopy_SeqSBAIJ"
11763c896bc6SHong Zhang PetscErrorCode MatCopy_SeqSBAIJ(Mat A,Mat B,MatStructure str)
11773c896bc6SHong Zhang {
11783c896bc6SHong Zhang   PetscErrorCode ierr;
11793c896bc6SHong Zhang 
11803c896bc6SHong Zhang   PetscFunctionBegin;
11813c896bc6SHong Zhang   /* If the two matrices have the same copy implementation, use fast copy. */
11823c896bc6SHong Zhang   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
11833c896bc6SHong Zhang     Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
11843c896bc6SHong Zhang     Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ*)B->data;
11853c896bc6SHong Zhang 
1186e7e72b3dSBarry 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");
1187d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->N])*sizeof(PetscScalar));CHKERRQ(ierr);
11883c896bc6SHong Zhang   } else {
1189f5edf698SHong Zhang     ierr = MatGetRowUpperTriangular(A);CHKERRQ(ierr);
11903c896bc6SHong Zhang     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1191f5edf698SHong Zhang     ierr = MatRestoreRowUpperTriangular(A);CHKERRQ(ierr);
11923c896bc6SHong Zhang   }
11933c896bc6SHong Zhang   PetscFunctionReturn(0);
11943c896bc6SHong Zhang }
11953c896bc6SHong Zhang 
11963c896bc6SHong Zhang #undef __FUNCT__
11974994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqSBAIJ"
11984994cf47SJed Brown PetscErrorCode MatSetUp_SeqSBAIJ(Mat A)
1199273d9f13SBarry Smith {
1200dfbe8321SBarry Smith   PetscErrorCode ierr;
1201273d9f13SBarry Smith 
1202273d9f13SBarry Smith   PetscFunctionBegin;
1203db4efbfdSBarry Smith   ierr =  MatSeqSBAIJSetPreallocation_SeqSBAIJ(A,-PetscMax(A->rmap->bs,1),PETSC_DEFAULT,0);CHKERRQ(ierr);
1204273d9f13SBarry Smith   PetscFunctionReturn(0);
1205273d9f13SBarry Smith }
1206273d9f13SBarry Smith 
1207a6ece127SHong Zhang #undef __FUNCT__
1208a6ece127SHong Zhang #define __FUNCT__ "MatGetArray_SeqSBAIJ"
1209dfbe8321SBarry Smith PetscErrorCode MatGetArray_SeqSBAIJ(Mat A,PetscScalar *array[])
1210a6ece127SHong Zhang {
1211a6ece127SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
1212a6ece127SHong Zhang   PetscFunctionBegin;
1213a6ece127SHong Zhang   *array = a->a;
1214a6ece127SHong Zhang   PetscFunctionReturn(0);
1215a6ece127SHong Zhang }
1216a6ece127SHong Zhang 
1217a6ece127SHong Zhang #undef __FUNCT__
1218a6ece127SHong Zhang #define __FUNCT__ "MatRestoreArray_SeqSBAIJ"
1219dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqSBAIJ(Mat A,PetscScalar *array[])
1220a6ece127SHong Zhang {
1221a6ece127SHong Zhang   PetscFunctionBegin;
1222a6ece127SHong Zhang   PetscFunctionReturn(0);
1223a6ece127SHong Zhang  }
1224a6ece127SHong Zhang 
122542ee4b1aSHong Zhang #undef __FUNCT__
122642ee4b1aSHong Zhang #define __FUNCT__ "MatAXPY_SeqSBAIJ"
1227f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqSBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
122842ee4b1aSHong Zhang {
122942ee4b1aSHong Zhang   Mat_SeqSBAIJ   *x=(Mat_SeqSBAIJ *)X->data, *y=(Mat_SeqSBAIJ *)Y->data;
1230dfbe8321SBarry Smith   PetscErrorCode ierr;
1231e838b9e7SJed Brown   PetscInt       i,bs=Y->rmap->bs,bs2=bs*bs,j;
1232e838b9e7SJed Brown   PetscBLASInt   one = 1;
123342ee4b1aSHong Zhang 
123442ee4b1aSHong Zhang   PetscFunctionBegin;
123542ee4b1aSHong Zhang   if (str == SAME_NONZERO_PATTERN) {
1236f4df32b1SMatthew Knepley     PetscScalar alpha = a;
1237666a761fSJed Brown     PetscBLASInt bnz = PetscBLASIntCast(x->nz*bs2);
1238f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
1239c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
1240c4319e64SHong Zhang     if (y->xtoy && y->XtoY != X) {
1241c4319e64SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
12426bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
1243c537a176SHong Zhang     }
1244c4319e64SHong Zhang     if (!y->xtoy) { /* get xtoy */
1245c4319e64SHong Zhang       ierr = MatAXPYGetxtoy_Private(x->mbs,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
1246c4319e64SHong Zhang       y->XtoY = X;
1247c537a176SHong Zhang     }
12486c6c5352SBarry Smith     for (i=0; i<x->nz; i++) {
1249c4319e64SHong Zhang       j = 0;
1250c4319e64SHong Zhang       while (j < bs2){
1251f4df32b1SMatthew Knepley         y->a[bs2*y->xtoy[i]+j] += a*(x->a[bs2*i+j]);
1252c4319e64SHong Zhang         j++;
1253c537a176SHong Zhang       }
1254c4319e64SHong Zhang     }
12551e2582c4SBarry 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);
125642ee4b1aSHong Zhang   } else {
1257f5edf698SHong Zhang     ierr = MatGetRowUpperTriangular(X);CHKERRQ(ierr);
1258f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
1259f5edf698SHong Zhang     ierr = MatRestoreRowUpperTriangular(X);CHKERRQ(ierr);
126042ee4b1aSHong Zhang   }
126142ee4b1aSHong Zhang   PetscFunctionReturn(0);
126242ee4b1aSHong Zhang }
126342ee4b1aSHong Zhang 
1264efcf0fc3SBarry Smith #undef __FUNCT__
12656363de48SJed Brown #define __FUNCT__ "MatSetBlockSize_SeqSBAIJ"
12666363de48SJed Brown PetscErrorCode MatSetBlockSize_SeqSBAIJ(Mat A,PetscInt bs)
12676363de48SJed Brown {
12686363de48SJed Brown   PetscInt rbs,cbs;
12696363de48SJed Brown   PetscErrorCode ierr;
12706363de48SJed Brown 
12716363de48SJed Brown   PetscFunctionBegin;
12726363de48SJed Brown   ierr = PetscLayoutGetBlockSize(A->rmap,&rbs);CHKERRQ(ierr);
12736363de48SJed Brown   ierr = PetscLayoutGetBlockSize(A->cmap,&cbs);CHKERRQ(ierr);
1274e32f2f54SBarry Smith   if (rbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with SBAIJ %d",bs,rbs);
1275e32f2f54SBarry Smith   if (cbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with SBAIJ %d",bs,cbs);
12766363de48SJed Brown   PetscFunctionReturn(0);
12776363de48SJed Brown }
12786363de48SJed Brown 
12796363de48SJed Brown #undef __FUNCT__
1280efcf0fc3SBarry Smith #define __FUNCT__ "MatIsSymmetric_SeqSBAIJ"
1281ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqSBAIJ(Mat A,PetscReal tol,PetscBool  *flg)
1282efcf0fc3SBarry Smith {
1283efcf0fc3SBarry Smith   PetscFunctionBegin;
1284efcf0fc3SBarry Smith   *flg = PETSC_TRUE;
1285efcf0fc3SBarry Smith   PetscFunctionReturn(0);
1286efcf0fc3SBarry Smith }
1287efcf0fc3SBarry Smith 
1288efcf0fc3SBarry Smith #undef __FUNCT__
1289efcf0fc3SBarry Smith #define __FUNCT__ "MatIsStructurallySymmetric_SeqSBAIJ"
1290ace3abfcSBarry Smith PetscErrorCode MatIsStructurallySymmetric_SeqSBAIJ(Mat A,PetscBool  *flg)
1291efcf0fc3SBarry Smith {
1292efcf0fc3SBarry Smith    PetscFunctionBegin;
1293efcf0fc3SBarry Smith    *flg = PETSC_TRUE;
1294efcf0fc3SBarry Smith    PetscFunctionReturn(0);
1295efcf0fc3SBarry Smith }
1296efcf0fc3SBarry Smith 
1297efcf0fc3SBarry Smith #undef __FUNCT__
1298efcf0fc3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqSBAIJ"
1299ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqSBAIJ(Mat A,PetscReal tol,PetscBool  *flg)
1300efcf0fc3SBarry Smith  {
1301efcf0fc3SBarry Smith    PetscFunctionBegin;
1302efcf0fc3SBarry Smith    *flg = PETSC_FALSE;
1303efcf0fc3SBarry Smith    PetscFunctionReturn(0);
1304efcf0fc3SBarry Smith  }
1305efcf0fc3SBarry Smith 
130699cafbc1SBarry Smith #undef __FUNCT__
130799cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqSBAIJ"
130899cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqSBAIJ(Mat A)
130999cafbc1SBarry Smith {
131099cafbc1SBarry Smith   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
131199cafbc1SBarry Smith   PetscInt       i,nz = a->bs2*a->i[a->mbs];
1312dd6ea824SBarry Smith   MatScalar      *aa = a->a;
131399cafbc1SBarry Smith 
131499cafbc1SBarry Smith   PetscFunctionBegin;
131599cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
131699cafbc1SBarry Smith   PetscFunctionReturn(0);
131799cafbc1SBarry Smith }
131899cafbc1SBarry Smith 
131999cafbc1SBarry Smith #undef __FUNCT__
132099cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqSBAIJ"
132199cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqSBAIJ(Mat A)
132299cafbc1SBarry Smith {
132399cafbc1SBarry Smith   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
132499cafbc1SBarry Smith   PetscInt       i,nz = a->bs2*a->i[a->mbs];
1325dd6ea824SBarry Smith   MatScalar      *aa = a->a;
132699cafbc1SBarry Smith 
132799cafbc1SBarry Smith   PetscFunctionBegin;
132899cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
132999cafbc1SBarry Smith   PetscFunctionReturn(0);
133099cafbc1SBarry Smith }
133199cafbc1SBarry Smith 
13323bededecSBarry Smith #undef __FUNCT__
13333bededecSBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqSBAIJ"
13343bededecSBarry Smith PetscErrorCode MatZeroRowsColumns_SeqSBAIJ(Mat A,PetscInt is_n,const PetscInt is_idx[],PetscScalar diag,Vec x, Vec b)
13353bededecSBarry Smith {
13363bededecSBarry Smith   Mat_SeqSBAIJ      *baij=(Mat_SeqSBAIJ*)A->data;
13373bededecSBarry Smith   PetscErrorCode    ierr;
13383bededecSBarry Smith   PetscInt          i,j,k,count;
13393bededecSBarry Smith   PetscInt          bs=A->rmap->bs,bs2=baij->bs2,row,col;
13403bededecSBarry Smith   PetscScalar       zero = 0.0;
13413bededecSBarry Smith   MatScalar         *aa;
13423bededecSBarry Smith   const PetscScalar *xx;
13433bededecSBarry Smith   PetscScalar       *bb;
134456777dd2SBarry Smith   PetscBool         *zeroed,vecs = PETSC_FALSE;
13453bededecSBarry Smith 
13463bededecSBarry Smith   PetscFunctionBegin;
13473bededecSBarry Smith   /* fix right hand side if needed */
13483bededecSBarry Smith   if (x && b) {
13493bededecSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
13503bededecSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
135156777dd2SBarry Smith     vecs = PETSC_TRUE;
13523bededecSBarry Smith   }
13533bededecSBarry Smith   A->same_nonzero = PETSC_TRUE;
13543bededecSBarry Smith 
13553bededecSBarry Smith   /* zero the columns */
13563bededecSBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
13573bededecSBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
13583bededecSBarry Smith   for (i=0; i<is_n; i++) {
13593bededecSBarry 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]);
13603bededecSBarry Smith     zeroed[is_idx[i]] = PETSC_TRUE;
13613bededecSBarry Smith   }
136256777dd2SBarry Smith   if (vecs) {
136356777dd2SBarry Smith     for (i=0; i<A->rmap->N; i++) {
136456777dd2SBarry Smith       row = i/bs;
136556777dd2SBarry Smith       for (j=baij->i[row]; j<baij->i[row+1]; j++) {
136656777dd2SBarry Smith 	for (k=0; k<bs; k++) {
136756777dd2SBarry Smith 	  col = bs*baij->j[j] + k;
136856777dd2SBarry Smith           if (col <= i) continue;
136956777dd2SBarry Smith 	  aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
137056777dd2SBarry Smith 	  if (!zeroed[i] && zeroed[col]) {
137156777dd2SBarry Smith 	    bb[i] -= aa[0]*xx[col];
137256777dd2SBarry Smith 	  }
137356777dd2SBarry Smith 	  if (zeroed[i] && !zeroed[col]) {
137456777dd2SBarry Smith 	    bb[col] -= aa[0]*xx[i];
137556777dd2SBarry Smith 	  }
137656777dd2SBarry Smith 	}
137756777dd2SBarry Smith       }
137856777dd2SBarry Smith     }
137956777dd2SBarry Smith     for (i=0; i<is_n; i++) {
138056777dd2SBarry Smith       bb[is_idx[i]] = diag*xx[is_idx[i]];
138156777dd2SBarry Smith     }
138256777dd2SBarry Smith   }
138356777dd2SBarry Smith 
13843bededecSBarry Smith   for (i=0; i<A->rmap->N; i++) {
13853bededecSBarry Smith     if (!zeroed[i]) {
13863bededecSBarry Smith       row = i/bs;
13873bededecSBarry Smith       for (j=baij->i[row]; j<baij->i[row+1]; j++) {
13883bededecSBarry Smith         for (k=0; k<bs; k++) {
13893bededecSBarry Smith           col = bs*baij->j[j] + k;
13903bededecSBarry Smith 	  if (zeroed[col]) {
13913bededecSBarry Smith 	    aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
13923bededecSBarry Smith             aa[0] = 0.0;
13933bededecSBarry Smith           }
13943bededecSBarry Smith         }
13953bededecSBarry Smith       }
13963bededecSBarry Smith     }
13973bededecSBarry Smith   }
13983bededecSBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
139956777dd2SBarry Smith   if (vecs) {
140056777dd2SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
140156777dd2SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
140256777dd2SBarry Smith   }
14033bededecSBarry Smith 
14043bededecSBarry Smith   /* zero the rows */
14053bededecSBarry Smith   for (i=0; i<is_n; i++) {
14063bededecSBarry Smith     row   = is_idx[i];
14073bededecSBarry Smith     count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
14083bededecSBarry Smith     aa    = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
14093bededecSBarry Smith     for (k=0; k<count; k++) {
14103bededecSBarry Smith       aa[0] =  zero;
14113bededecSBarry Smith       aa    += bs;
14123bededecSBarry Smith     }
14133bededecSBarry Smith     if (diag != 0.0) {
14143bededecSBarry Smith       ierr = (*A->ops->setvalues)(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr);
14153bededecSBarry Smith     }
14163bededecSBarry Smith   }
14173bededecSBarry Smith   ierr = MatAssemblyEnd_SeqSBAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14183bededecSBarry Smith   PetscFunctionReturn(0);
14193bededecSBarry Smith }
14203bededecSBarry Smith 
142149b5e25fSSatish Balay /* -------------------------------------------------------------------*/
142249b5e25fSSatish Balay static struct _MatOps MatOps_Values = {MatSetValues_SeqSBAIJ,
142349b5e25fSSatish Balay        MatGetRow_SeqSBAIJ,
142449b5e25fSSatish Balay        MatRestoreRow_SeqSBAIJ,
142549b5e25fSSatish Balay        MatMult_SeqSBAIJ_N,
142697304618SKris Buschelman /* 4*/ MatMultAdd_SeqSBAIJ_N,
1427431c96f7SBarry Smith        MatMult_SeqSBAIJ_N,       /* transpose versions are same as non-transpose versions */
1428e005ede5SBarry Smith        MatMultAdd_SeqSBAIJ_N,
1429db4efbfdSBarry Smith        0,
143049b5e25fSSatish Balay        0,
143149b5e25fSSatish Balay        0,
143297304618SKris Buschelman /*10*/ 0,
143349b5e25fSSatish Balay        0,
1434c078aec8SLisandro Dalcin        MatCholeskyFactor_SeqSBAIJ,
143541f059aeSBarry Smith        MatSOR_SeqSBAIJ,
143649b5e25fSSatish Balay        MatTranspose_SeqSBAIJ,
143797304618SKris Buschelman /*15*/ MatGetInfo_SeqSBAIJ,
143849b5e25fSSatish Balay        MatEqual_SeqSBAIJ,
143949b5e25fSSatish Balay        MatGetDiagonal_SeqSBAIJ,
144049b5e25fSSatish Balay        MatDiagonalScale_SeqSBAIJ,
144149b5e25fSSatish Balay        MatNorm_SeqSBAIJ,
144297304618SKris Buschelman /*20*/ 0,
144349b5e25fSSatish Balay        MatAssemblyEnd_SeqSBAIJ,
144449b5e25fSSatish Balay        MatSetOption_SeqSBAIJ,
144549b5e25fSSatish Balay        MatZeroEntries_SeqSBAIJ,
1446d519adbfSMatthew Knepley /*24*/ 0,
144749b5e25fSSatish Balay        0,
144849b5e25fSSatish Balay        0,
1449db4efbfdSBarry Smith        0,
1450db4efbfdSBarry Smith        0,
14514994cf47SJed Brown /*29*/ MatSetUp_SeqSBAIJ,
1452c464158bSHong Zhang        0,
1453db4efbfdSBarry Smith        0,
1454a6ece127SHong Zhang        MatGetArray_SeqSBAIJ,
1455a6ece127SHong Zhang        MatRestoreArray_SeqSBAIJ,
1456d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqSBAIJ,
1457719d5645SBarry Smith        0,
1458719d5645SBarry Smith        0,
145949b5e25fSSatish Balay        0,
1460c84f5b01SHong Zhang        MatICCFactor_SeqSBAIJ,
1461d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqSBAIJ,
146249b5e25fSSatish Balay        MatGetSubMatrices_SeqSBAIJ,
146349b5e25fSSatish Balay        MatIncreaseOverlap_SeqSBAIJ,
146449b5e25fSSatish Balay        MatGetValues_SeqSBAIJ,
14653c896bc6SHong Zhang        MatCopy_SeqSBAIJ,
1466d519adbfSMatthew Knepley /*44*/ 0,
146749b5e25fSSatish Balay        MatScale_SeqSBAIJ,
146849b5e25fSSatish Balay        0,
146949b5e25fSSatish Balay        0,
14703bededecSBarry Smith        MatZeroRowsColumns_SeqSBAIJ,
14716363de48SJed Brown /*49*/ MatSetBlockSize_SeqSBAIJ,
147249b5e25fSSatish Balay        MatGetRowIJ_SeqSBAIJ,
147349b5e25fSSatish Balay        MatRestoreRowIJ_SeqSBAIJ,
147449b5e25fSSatish Balay        0,
147549b5e25fSSatish Balay        0,
1476d519adbfSMatthew Knepley /*54*/ 0,
147749b5e25fSSatish Balay        0,
147849b5e25fSSatish Balay        0,
147949b5e25fSSatish Balay        0,
148049b5e25fSSatish Balay        MatSetValuesBlocked_SeqSBAIJ,
1481d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_SeqSBAIJ,
148249b5e25fSSatish Balay        0,
148349b5e25fSSatish Balay        0,
1484357abbc8SBarry Smith        0,
1485d959ec07SHong Zhang        0,
1486d519adbfSMatthew Knepley /*64*/ 0,
1487d959ec07SHong Zhang        0,
1488d959ec07SHong Zhang        0,
1489d959ec07SHong Zhang        0,
1490d959ec07SHong Zhang        0,
1491d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqSBAIJ,
14923e0d88b5SBarry Smith        0,
14933e0d88b5SBarry Smith        0,
14943e0d88b5SBarry Smith        0,
14953e0d88b5SBarry Smith        0,
1496d519adbfSMatthew Knepley /*74*/ 0,
14973e0d88b5SBarry Smith        0,
14983e0d88b5SBarry Smith        0,
14993e0d88b5SBarry Smith        0,
15003e0d88b5SBarry Smith        0,
1501d519adbfSMatthew Knepley /*79*/ 0,
15023e0d88b5SBarry Smith        0,
15033e0d88b5SBarry Smith        0,
150497304618SKris Buschelman        MatGetInertia_SeqSBAIJ,
15055bba2384SShri Abhyankar        MatLoad_SeqSBAIJ,
1506d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqSBAIJ,
1507865e5f61SKris Buschelman        MatIsHermitian_SeqSBAIJ,
1508efcf0fc3SBarry Smith        MatIsStructurallySymmetric_SeqSBAIJ,
1509865e5f61SKris Buschelman        0,
1510865e5f61SKris Buschelman        0,
1511d519adbfSMatthew Knepley /*89*/ 0,
1512865e5f61SKris Buschelman        0,
1513865e5f61SKris Buschelman        0,
1514865e5f61SKris Buschelman        0,
1515865e5f61SKris Buschelman        0,
1516d519adbfSMatthew Knepley /*94*/ 0,
1517865e5f61SKris Buschelman        0,
1518865e5f61SKris Buschelman        0,
151999cafbc1SBarry Smith        0,
152099cafbc1SBarry Smith        0,
1521d519adbfSMatthew Knepley /*99*/ 0,
152299cafbc1SBarry Smith        0,
152399cafbc1SBarry Smith        0,
152499cafbc1SBarry Smith        0,
152599cafbc1SBarry Smith        0,
1526d519adbfSMatthew Knepley /*104*/0,
152799cafbc1SBarry Smith        MatRealPart_SeqSBAIJ,
1528f5edf698SHong Zhang        MatImaginaryPart_SeqSBAIJ,
1529f5edf698SHong Zhang        MatGetRowUpperTriangular_SeqSBAIJ,
15302af78befSBarry Smith        MatRestoreRowUpperTriangular_SeqSBAIJ,
1531d519adbfSMatthew Knepley /*109*/0,
15322af78befSBarry Smith        0,
15332af78befSBarry Smith        0,
15342af78befSBarry Smith        0,
1535547795f9SHong Zhang        MatMissingDiagonal_SeqSBAIJ,
1536547795f9SHong Zhang /*114*/0,
1537547795f9SHong Zhang        0,
1538547795f9SHong Zhang        0,
1539547795f9SHong Zhang        0,
1540547795f9SHong Zhang        0,
1541547795f9SHong Zhang /*119*/0,
1542547795f9SHong Zhang        0,
15432f480046SShri Abhyankar        0,
15445bba2384SShri Abhyankar        0
154599cafbc1SBarry Smith };
1546be1d678aSKris Buschelman 
154749b5e25fSSatish Balay EXTERN_C_BEGIN
15484a2ae208SSatish Balay #undef __FUNCT__
15494a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqSBAIJ"
15507087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqSBAIJ(Mat mat)
155149b5e25fSSatish Balay {
15524afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ *)mat->data;
1553d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
1554dfbe8321SBarry Smith   PetscErrorCode ierr;
155549b5e25fSSatish Balay 
155649b5e25fSSatish Balay   PetscFunctionBegin;
1557e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
155849b5e25fSSatish Balay 
155949b5e25fSSatish Balay   /* allocate space for values if not already there */
156049b5e25fSSatish Balay   if (!aij->saved_values) {
156187828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
156249b5e25fSSatish Balay   }
156349b5e25fSSatish Balay 
156449b5e25fSSatish Balay   /* copy values over */
156587828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
156649b5e25fSSatish Balay   PetscFunctionReturn(0);
156749b5e25fSSatish Balay }
156849b5e25fSSatish Balay EXTERN_C_END
156949b5e25fSSatish Balay 
157049b5e25fSSatish Balay EXTERN_C_BEGIN
15714a2ae208SSatish Balay #undef __FUNCT__
15724a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqSBAIJ"
15737087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqSBAIJ(Mat mat)
157449b5e25fSSatish Balay {
15754afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ *)mat->data;
15766849ba73SBarry Smith   PetscErrorCode ierr;
1577d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
157849b5e25fSSatish Balay 
157949b5e25fSSatish Balay   PetscFunctionBegin;
1580e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
1581e7e72b3dSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
158249b5e25fSSatish Balay 
158349b5e25fSSatish Balay   /* copy values over */
158487828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
158549b5e25fSSatish Balay   PetscFunctionReturn(0);
158649b5e25fSSatish Balay }
158749b5e25fSSatish Balay EXTERN_C_END
158849b5e25fSSatish Balay 
15898549e402SHong Zhang EXTERN_C_BEGIN
15904a2ae208SSatish Balay #undef __FUNCT__
1591a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation_SeqSBAIJ"
15927087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetPreallocation_SeqSBAIJ(Mat B,PetscInt bs,PetscInt nz,PetscInt *nnz)
159349b5e25fSSatish Balay {
1594c464158bSHong Zhang   Mat_SeqSBAIJ   *b = (Mat_SeqSBAIJ*)B->data;
15956849ba73SBarry Smith   PetscErrorCode ierr;
1596db4efbfdSBarry Smith   PetscInt       i,mbs,bs2, newbs = PetscAbs(bs);
15972576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,flg = PETSC_FALSE,realalloc = PETSC_FALSE;
159849b5e25fSSatish Balay 
159949b5e25fSSatish Balay   PetscFunctionBegin;
16002576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
1601273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
1602db4efbfdSBarry Smith   if (bs < 0) {
1603db4efbfdSBarry Smith     ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Options for MPISBAIJ matrix","Mat");CHKERRQ(ierr);
1604db4efbfdSBarry Smith       ierr = PetscOptionsInt("-mat_block_size","Set the blocksize used to store the matrix","MatSeqSBAIJSetPreallocation",newbs,&newbs,PETSC_NULL);CHKERRQ(ierr);
1605db4efbfdSBarry Smith     ierr = PetscOptionsEnd();CHKERRQ(ierr);
1606db4efbfdSBarry Smith     bs   = PetscAbs(bs);
1607db4efbfdSBarry Smith   }
1608e7e72b3dSBarry Smith   if (nnz && newbs != bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot change blocksize from command line if setting nnz");
1609db4efbfdSBarry Smith   bs = newbs;
1610db4efbfdSBarry Smith 
161126283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
161226283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
161326283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
161426283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
1615899cda47SBarry Smith 
1616d0f46423SBarry Smith   mbs  = B->rmap->N/bs;
161749b5e25fSSatish Balay   bs2  = bs*bs;
161849b5e25fSSatish Balay 
1619e7e72b3dSBarry Smith   if (mbs*bs != B->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number rows, cols must be divisible by blocksize");
162049b5e25fSSatish Balay 
1621ab93d7beSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
1622ab93d7beSBarry Smith     skipallocation = PETSC_TRUE;
1623ab93d7beSBarry Smith     nz             = 0;
1624ab93d7beSBarry Smith   }
1625ab93d7beSBarry Smith 
1626435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 3;
1627e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
162849b5e25fSSatish Balay   if (nnz) {
162949b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
1630e32f2f54SBarry 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]);
1631e32f2f54SBarry 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);
163249b5e25fSSatish Balay     }
163349b5e25fSSatish Balay   }
163449b5e25fSSatish Balay 
1635db4efbfdSBarry Smith   B->ops->mult             = MatMult_SeqSBAIJ_N;
1636db4efbfdSBarry Smith   B->ops->multadd          = MatMultAdd_SeqSBAIJ_N;
1637db4efbfdSBarry Smith   B->ops->multtranspose    = MatMult_SeqSBAIJ_N;
1638db4efbfdSBarry Smith   B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_N;
1639acfcf0e5SJed Brown   ierr  = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,PETSC_NULL);CHKERRQ(ierr);
164049b5e25fSSatish Balay   if (!flg) {
164149b5e25fSSatish Balay     switch (bs) {
164249b5e25fSSatish Balay     case 1:
164349b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_1;
164449b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_1;
1645431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_1;
1646431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_1;
164749b5e25fSSatish Balay       break;
164849b5e25fSSatish Balay     case 2:
164949b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_2;
165049b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_2;
1651431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_2;
1652431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_2;
165349b5e25fSSatish Balay       break;
165449b5e25fSSatish Balay     case 3:
165549b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_3;
165649b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_3;
1657431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_3;
1658431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_3;
165949b5e25fSSatish Balay       break;
166049b5e25fSSatish Balay     case 4:
166149b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_4;
166249b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_4;
1663431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_4;
1664431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_4;
166549b5e25fSSatish Balay       break;
166649b5e25fSSatish Balay     case 5:
166749b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_5;
166849b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_5;
1669431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_5;
1670431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_5;
167149b5e25fSSatish Balay       break;
167249b5e25fSSatish Balay     case 6:
167349b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_6;
167449b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_6;
1675431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_6;
1676431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_6;
167749b5e25fSSatish Balay       break;
167849b5e25fSSatish Balay     case 7:
1679de53e5efSHong Zhang       B->ops->mult             = MatMult_SeqSBAIJ_7;
168049b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_7;
1681431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_7;
1682431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_7;
168349b5e25fSSatish Balay       break;
168449b5e25fSSatish Balay     }
168549b5e25fSSatish Balay   }
168649b5e25fSSatish Balay 
168749b5e25fSSatish Balay   b->mbs = mbs;
16884afc71dfSHong Zhang   b->nbs = mbs;
1689ab93d7beSBarry Smith   if (!skipallocation) {
16902ee49352SLisandro Dalcin     if (!b->imax) {
1691ab93d7beSBarry Smith       ierr = PetscMalloc2(mbs,PetscInt,&b->imax,mbs,PetscInt,&b->ilen);CHKERRQ(ierr);
1692c760cd28SBarry Smith       b->free_imax_ilen = PETSC_TRUE;
16932ee49352SLisandro Dalcin       ierr = PetscLogObjectMemory(B,2*mbs*sizeof(PetscInt));CHKERRQ(ierr);
16942ee49352SLisandro Dalcin     }
169549b5e25fSSatish Balay     if (!nnz) {
1696435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
169749b5e25fSSatish Balay       else if (nz <= 0)        nz = 1;
169849b5e25fSSatish Balay       for (i=0; i<mbs; i++) {
16998cef66ccSHong Zhang         b->imax[i] = nz;
170049b5e25fSSatish Balay       }
1701153ea458SHong Zhang       nz = nz*mbs; /* total nz */
170249b5e25fSSatish Balay     } else {
170349b5e25fSSatish Balay       nz = 0;
17048cef66ccSHong Zhang       for (i=0; i<mbs; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
170549b5e25fSSatish Balay     }
17062ee49352SLisandro Dalcin     /* b->ilen will count nonzeros in each block row so far. */
17072ee49352SLisandro Dalcin     for (i=0; i<mbs; i++) { b->ilen[i] = 0;}
17086c6c5352SBarry Smith     /* nz=(nz+mbs)/2; */ /* total diagonal and superdiagonal nonzero blocks */
170949b5e25fSSatish Balay 
171049b5e25fSSatish Balay     /* allocate the matrix space */
17112ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
1712d0f46423SBarry Smith     ierr = PetscMalloc3(bs2*nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->N+1,PetscInt,&b->i);CHKERRQ(ierr);
1713d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->N+1)*sizeof(PetscInt)+nz*(bs2*sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
17146c6c5352SBarry Smith     ierr = PetscMemzero(b->a,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
171513f74950SBarry Smith     ierr = PetscMemzero(b->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
171649b5e25fSSatish Balay     b->singlemalloc = PETSC_TRUE;
171749b5e25fSSatish Balay 
171849b5e25fSSatish Balay     /* pointer to beginning of each row */
1719e60cf9a0SBarry Smith     b->i[0] = 0;
172049b5e25fSSatish Balay     for (i=1; i<mbs+1; i++) {
172149b5e25fSSatish Balay       b->i[i] = b->i[i-1] + b->imax[i-1];
172249b5e25fSSatish Balay     }
1723e6b907acSBarry Smith     b->free_a     = PETSC_TRUE;
1724e6b907acSBarry Smith     b->free_ij    = PETSC_TRUE;
1725e811da20SHong Zhang   } else {
1726e6b907acSBarry Smith     b->free_a     = PETSC_FALSE;
1727e6b907acSBarry Smith     b->free_ij    = PETSC_FALSE;
1728ab93d7beSBarry Smith   }
172949b5e25fSSatish Balay 
1730d0f46423SBarry Smith   B->rmap->bs     = bs;
173149b5e25fSSatish Balay   b->bs2          = bs2;
17326c6c5352SBarry Smith   b->nz           = 0;
1733b32cb4a7SJed Brown   b->maxnz        = nz;
1734153ea458SHong Zhang 
173516cdd363SHong Zhang   b->inew             = 0;
173616cdd363SHong Zhang   b->jnew             = 0;
173716cdd363SHong Zhang   b->anew             = 0;
173816cdd363SHong Zhang   b->a2anew           = 0;
17391a3463dfSHong Zhang   b->permute          = PETSC_FALSE;
17402576faa2SJed Brown   if (realalloc) {ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);}
1741c464158bSHong Zhang   PetscFunctionReturn(0);
1742c464158bSHong Zhang }
1743a23d5eceSKris Buschelman EXTERN_C_END
1744153ea458SHong Zhang 
1745db4efbfdSBarry Smith /*
1746db4efbfdSBarry Smith    This is used to set the numeric factorization for both Cholesky and ICC symbolic factorization
1747db4efbfdSBarry Smith */
17488b1456e3SHong Zhang #undef __FUNCT__
1749d595f711SHong Zhang #define __FUNCT__ "MatSeqSBAIJSetNumericFactorization_inplace"
1750ace3abfcSBarry Smith PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat B,PetscBool  natural)
1751db4efbfdSBarry Smith {
1752db4efbfdSBarry Smith   PetscErrorCode ierr;
1753ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
1754db4efbfdSBarry Smith   PetscInt       bs = B->rmap->bs;
1755db4efbfdSBarry Smith 
1756db4efbfdSBarry Smith   PetscFunctionBegin;
1757acfcf0e5SJed Brown   ierr    = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,PETSC_NULL);CHKERRQ(ierr);
1758db4efbfdSBarry Smith   if (flg) bs = 8;
1759db4efbfdSBarry Smith 
1760db4efbfdSBarry Smith   if (!natural) {
1761db4efbfdSBarry Smith     switch (bs) {
1762db4efbfdSBarry Smith     case 1:
1763d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_inplace;
1764db4efbfdSBarry Smith       break;
1765db4efbfdSBarry Smith     case 2:
1766db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2;
1767db4efbfdSBarry Smith       break;
1768db4efbfdSBarry Smith     case 3:
1769db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3;
1770db4efbfdSBarry Smith       break;
1771db4efbfdSBarry Smith     case 4:
1772db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4;
1773db4efbfdSBarry Smith       break;
1774db4efbfdSBarry Smith     case 5:
1775db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5;
1776db4efbfdSBarry Smith       break;
1777db4efbfdSBarry Smith     case 6:
1778db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6;
1779db4efbfdSBarry Smith       break;
1780db4efbfdSBarry Smith     case 7:
1781db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7;
1782db4efbfdSBarry Smith       break;
1783db4efbfdSBarry Smith     default:
1784db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N;
1785db4efbfdSBarry Smith       break;
1786db4efbfdSBarry Smith     }
1787db4efbfdSBarry Smith   } else {
1788db4efbfdSBarry Smith     switch (bs) {
1789db4efbfdSBarry Smith     case 1:
1790d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering_inplace;
1791db4efbfdSBarry Smith       break;
1792db4efbfdSBarry Smith     case 2:
1793db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering;
1794db4efbfdSBarry Smith       break;
1795db4efbfdSBarry Smith     case 3:
1796db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3_NaturalOrdering;
1797db4efbfdSBarry Smith       break;
1798db4efbfdSBarry Smith     case 4:
1799db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4_NaturalOrdering;
1800db4efbfdSBarry Smith       break;
1801db4efbfdSBarry Smith     case 5:
1802db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5_NaturalOrdering;
1803db4efbfdSBarry Smith       break;
1804db4efbfdSBarry Smith     case 6:
1805db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6_NaturalOrdering;
1806db4efbfdSBarry Smith       break;
1807db4efbfdSBarry Smith     case 7:
1808db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7_NaturalOrdering;
1809db4efbfdSBarry Smith       break;
1810db4efbfdSBarry Smith     default:
1811db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering;
1812db4efbfdSBarry Smith       break;
1813db4efbfdSBarry Smith     }
1814db4efbfdSBarry Smith   }
1815db4efbfdSBarry Smith   PetscFunctionReturn(0);
1816db4efbfdSBarry Smith }
1817db4efbfdSBarry Smith 
1818d769727bSBarry Smith EXTERN_C_BEGIN
18197087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqSBAIJ_SeqAIJ(Mat, MatType,MatReuse,Mat*);
18207087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqSBAIJ_SeqBAIJ(Mat, MatType,MatReuse,Mat*);
1821d769727bSBarry Smith EXTERN_C_END
1822d769727bSBarry Smith 
1823e631078cSBarry Smith 
1824e631078cSBarry Smith EXTERN_C_BEGIN
18255c9eb25fSBarry Smith #undef __FUNCT__
18265c9eb25fSBarry Smith #define __FUNCT__ "MatGetFactor_seqsbaij_petsc"
18275c9eb25fSBarry Smith PetscErrorCode MatGetFactor_seqsbaij_petsc(Mat A,MatFactorType ftype,Mat *B)
18285c9eb25fSBarry Smith {
1829d0f46423SBarry Smith   PetscInt           n = A->rmap->n;
18305c9eb25fSBarry Smith   PetscErrorCode     ierr;
18315c9eb25fSBarry Smith 
18325c9eb25fSBarry Smith   PetscFunctionBegin;
18335c9eb25fSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
18345c9eb25fSBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
18355c9eb25fSBarry Smith   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
18365c9eb25fSBarry Smith     ierr = MatSetType(*B,MATSEQSBAIJ);CHKERRQ(ierr);
18375c9eb25fSBarry Smith     ierr = MatSeqSBAIJSetPreallocation(*B,1,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr);
18387b056e98SHong Zhang     (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqSBAIJ;
1839c6d0d4f0SHong Zhang     (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqSBAIJ;
1840e32f2f54SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported");
1841d5f3da31SBarry Smith   (*B)->factortype = ftype;
18425c9eb25fSBarry Smith   PetscFunctionReturn(0);
18435c9eb25fSBarry Smith }
1844e631078cSBarry Smith EXTERN_C_END
18455c9eb25fSBarry Smith 
18465c9eb25fSBarry Smith EXTERN_C_BEGIN
1847db4efbfdSBarry Smith #undef __FUNCT__
1848db4efbfdSBarry Smith #define __FUNCT__ "MatGetFactorAvailable_seqsbaij_petsc"
1849ace3abfcSBarry Smith PetscErrorCode MatGetFactorAvailable_seqsbaij_petsc(Mat A,MatFactorType ftype,PetscBool  *flg)
1850db4efbfdSBarry Smith {
1851db4efbfdSBarry Smith   PetscFunctionBegin;
1852db4efbfdSBarry Smith   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
1853db4efbfdSBarry Smith     *flg = PETSC_TRUE;
1854db4efbfdSBarry Smith   } else {
1855db4efbfdSBarry Smith     *flg = PETSC_FALSE;
1856db4efbfdSBarry Smith   }
1857db4efbfdSBarry Smith   PetscFunctionReturn(0);
1858db4efbfdSBarry Smith }
1859db4efbfdSBarry Smith EXTERN_C_END
1860db4efbfdSBarry Smith 
1861db4efbfdSBarry Smith EXTERN_C_BEGIN
1862611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
1863bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_sbaij_mumps(Mat,MatFactorType,Mat*);
1864611f576cSBarry Smith #endif
1865611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
18665c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_seqsbaij_spooles(Mat,MatFactorType,Mat*);
1867611f576cSBarry Smith #endif
1868b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
1869b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqsbaij_pastix(Mat,MatFactorType,Mat*);
1870b5e56a35SBarry Smith #endif
187120db9a53SJed Brown #if defined(PETSC_HAVE_CHOLMOD)
187220db9a53SJed Brown extern PetscErrorCode MatGetFactor_seqsbaij_cholmod(Mat,MatFactorType,Mat*);
187320db9a53SJed Brown #endif
18742938c13dSDahai Guo extern PetscErrorCode MatGetFactor_seqsbaij_sbstrm(Mat,MatFactorType,Mat*);
1875b1f23a54SSatish Balay EXTERN_C_END
18765c9eb25fSBarry Smith 
18770bad9183SKris Buschelman /*MC
1878fafad747SKris Buschelman   MATSEQSBAIJ - MATSEQSBAIJ = "seqsbaij" - A matrix type to be used for sequential symmetric block sparse matrices,
18790bad9183SKris Buschelman   based on block compressed sparse row format.  Only the upper triangular portion of the matrix is stored.
18800bad9183SKris Buschelman 
1881828413b8SBarry Smith   For complex numbers by default this matrix is symmetric, NOT Hermitian symmetric. To make it Hermitian symmetric you
188271dad5bbSBarry Smith   can call MatSetOption(Mat, MAT_HERMITIAN); after MatAssemblyEnd()
1883828413b8SBarry Smith 
18840bad9183SKris Buschelman   Options Database Keys:
18850bad9183SKris Buschelman   . -mat_type seqsbaij - sets the matrix type to "seqsbaij" during a call to MatSetFromOptions()
18860bad9183SKris Buschelman 
188771dad5bbSBarry Smith   Notes: By default if you insert values into the lower triangular part of the matrix they are simply ignored (since they are not
188871dad5bbSBarry 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
188971dad5bbSBarry 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.
189071dad5bbSBarry Smith 
189171dad5bbSBarry Smith 
18920bad9183SKris Buschelman   Level: beginner
18930bad9183SKris Buschelman 
18940bad9183SKris Buschelman   .seealso: MatCreateSeqSBAIJ
18950bad9183SKris Buschelman M*/
18960bad9183SKris Buschelman 
1897a23d5eceSKris Buschelman EXTERN_C_BEGIN
1898aa5a9175SDahai Guo extern PetscErrorCode  MatConvert_SeqSBAIJ_SeqSBSTRM(Mat, MatType,MatReuse,Mat*);
1899aa5a9175SDahai Guo EXTERN_C_END
1900aa5a9175SDahai Guo 
1901aa5a9175SDahai Guo 
1902aa5a9175SDahai Guo EXTERN_C_BEGIN
1903a23d5eceSKris Buschelman #undef __FUNCT__
1904a23d5eceSKris Buschelman #define __FUNCT__ "MatCreate_SeqSBAIJ"
19057087cfbeSBarry Smith PetscErrorCode  MatCreate_SeqSBAIJ(Mat B)
1906a23d5eceSKris Buschelman {
1907a23d5eceSKris Buschelman   Mat_SeqSBAIJ   *b;
1908dfbe8321SBarry Smith   PetscErrorCode ierr;
190913f74950SBarry Smith   PetscMPIInt    size;
1910ace3abfcSBarry Smith   PetscBool      no_unroll = PETSC_FALSE,no_inode = PETSC_FALSE;
1911a23d5eceSKris Buschelman 
1912a23d5eceSKris Buschelman   PetscFunctionBegin;
19137adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
1914e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Comm must be of size 1");
1915a23d5eceSKris Buschelman 
191638f2d2fdSLisandro Dalcin   ierr    = PetscNewLog(B,Mat_SeqSBAIJ,&b);CHKERRQ(ierr);
1917a23d5eceSKris Buschelman   B->data = (void*)b;
1918a23d5eceSKris Buschelman   ierr    = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
1919a23d5eceSKris Buschelman   B->ops->destroy     = MatDestroy_SeqSBAIJ;
1920a23d5eceSKris Buschelman   B->ops->view        = MatView_SeqSBAIJ;
1921a23d5eceSKris Buschelman   b->row              = 0;
1922a23d5eceSKris Buschelman   b->icol             = 0;
1923a23d5eceSKris Buschelman   b->reallocs         = 0;
1924a23d5eceSKris Buschelman   b->saved_values     = 0;
19250def2e27SBarry Smith   b->inode.limit      = 5;
19260def2e27SBarry Smith   b->inode.max_limit  = 5;
1927a23d5eceSKris Buschelman 
1928a23d5eceSKris Buschelman   b->roworiented      = PETSC_TRUE;
1929a23d5eceSKris Buschelman   b->nonew            = 0;
1930a23d5eceSKris Buschelman   b->diag             = 0;
1931a23d5eceSKris Buschelman   b->solve_work       = 0;
1932a23d5eceSKris Buschelman   b->mult_work        = 0;
1933a23d5eceSKris Buschelman   B->spptr            = 0;
1934f2cbd3d5SJed Brown   B->info.nz_unneeded = (PetscReal)b->maxnz*b->bs2;
1935a9817697SBarry Smith   b->keepnonzeropattern   = PETSC_FALSE;
1936a23d5eceSKris Buschelman   b->xtoy             = 0;
1937a23d5eceSKris Buschelman   b->XtoY             = 0;
1938a23d5eceSKris Buschelman 
1939a23d5eceSKris Buschelman   b->inew             = 0;
1940a23d5eceSKris Buschelman   b->jnew             = 0;
1941a23d5eceSKris Buschelman   b->anew             = 0;
1942a23d5eceSKris Buschelman   b->a2anew           = 0;
1943a23d5eceSKris Buschelman   b->permute          = PETSC_FALSE;
1944a23d5eceSKris Buschelman 
194571dad5bbSBarry Smith   b->ignore_ltriangular = PETSC_TRUE;
1946acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_ignore_lower_triangular",&b->ignore_ltriangular,PETSC_NULL);CHKERRQ(ierr);
1947941593c8SHong Zhang 
1948f5edf698SHong Zhang   b->getrow_utriangular = PETSC_FALSE;
1949acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_getrow_uppertriangular",&b->getrow_utriangular,PETSC_NULL);CHKERRQ(ierr);
1950f5edf698SHong Zhang 
1951b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
1952ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C",
1953b5e56a35SBarry Smith 					   "MatGetFactor_seqsbaij_pastix",
1954b5e56a35SBarry Smith 					   MatGetFactor_seqsbaij_pastix);CHKERRQ(ierr);
1955b5e56a35SBarry Smith #endif
1956611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
1957ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C",
19585c9eb25fSBarry Smith                                      "MatGetFactor_seqsbaij_spooles",
19595c9eb25fSBarry Smith                                      MatGetFactor_seqsbaij_spooles);CHKERRQ(ierr);
1960611f576cSBarry Smith #endif
1961611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
1962ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C",
1963bccb9932SShri Abhyankar                                      "MatGetFactor_sbaij_mumps",
1964bccb9932SShri Abhyankar                                      MatGetFactor_sbaij_mumps);CHKERRQ(ierr);
1965611f576cSBarry Smith #endif
196620db9a53SJed Brown #if defined(PETSC_HAVE_CHOLMOD)
196720db9a53SJed Brown   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C",
196820db9a53SJed Brown                                      "MatGetFactor_seqsbaij_cholmod",
196920db9a53SJed Brown                                      MatGetFactor_seqsbaij_cholmod);CHKERRQ(ierr);
197020db9a53SJed Brown #endif
1971ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C",
1972db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqsbaij_petsc",
1973db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqsbaij_petsc);CHKERRQ(ierr);
1974ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C",
19755c9eb25fSBarry Smith                                      "MatGetFactor_seqsbaij_petsc",
19765c9eb25fSBarry Smith                                      MatGetFactor_seqsbaij_petsc);CHKERRQ(ierr);
19773edee7c7SSatish Balay   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_sbstrm_C",
19782938c13dSDahai Guo                                      "MatGetFactor_seqsbaij_sbstrm",
19792938c13dSDahai Guo                                      MatGetFactor_seqsbaij_sbstrm);CHKERRQ(ierr);
1980a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
1981a23d5eceSKris Buschelman                                      "MatStoreValues_SeqSBAIJ",
1982a23d5eceSKris Buschelman                                      MatStoreValues_SeqSBAIJ);CHKERRQ(ierr);
1983a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
1984a23d5eceSKris Buschelman                                      "MatRetrieveValues_SeqSBAIJ",
1985a23d5eceSKris Buschelman                                      (void*)MatRetrieveValues_SeqSBAIJ);CHKERRQ(ierr);
1986a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqSBAIJSetColumnIndices_C",
1987a23d5eceSKris Buschelman                                      "MatSeqSBAIJSetColumnIndices_SeqSBAIJ",
1988a23d5eceSKris Buschelman                                      MatSeqSBAIJSetColumnIndices_SeqSBAIJ);CHKERRQ(ierr);
19894e5e7fe4SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqsbaij_seqaij_C",
19904e5e7fe4SHong Zhang                                      "MatConvert_SeqSBAIJ_SeqAIJ",
19914e5e7fe4SHong Zhang                                       MatConvert_SeqSBAIJ_SeqAIJ);CHKERRQ(ierr);
1992a0e1a404SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqsbaij_seqbaij_C",
1993a0e1a404SHong Zhang                                      "MatConvert_SeqSBAIJ_SeqBAIJ",
1994a0e1a404SHong Zhang                                       MatConvert_SeqSBAIJ_SeqBAIJ);CHKERRQ(ierr);
1995a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqSBAIJSetPreallocation_C",
1996a23d5eceSKris Buschelman                                      "MatSeqSBAIJSetPreallocation_SeqSBAIJ",
1997a23d5eceSKris Buschelman                                      MatSeqSBAIJSetPreallocation_SeqSBAIJ);CHKERRQ(ierr);
1998aa5a9175SDahai Guo   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqsbaij_seqsbstrm_C",
1999aa5a9175SDahai Guo                                      "MatConvert_SeqSBAIJ_SeqSBSTRM",
2000aa5a9175SDahai Guo                                       MatConvert_SeqSBAIJ_SeqSBSTRM);CHKERRQ(ierr);
200123ce1328SBarry Smith 
200223ce1328SBarry Smith   B->symmetric                  = PETSC_TRUE;
200323ce1328SBarry Smith   B->structurally_symmetric     = PETSC_TRUE;
200423ce1328SBarry Smith   B->symmetric_set              = PETSC_TRUE;
200523ce1328SBarry Smith   B->structurally_symmetric_set = PETSC_TRUE;
200617667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQSBAIJ);CHKERRQ(ierr);
20070def2e27SBarry Smith 
20080def2e27SBarry Smith   ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Options for SEQSBAIJ matrix","Mat");CHKERRQ(ierr);
2009acfcf0e5SJed Brown     ierr = PetscOptionsBool("-mat_no_unroll","Do not optimize for inodes (slower)",PETSC_NULL,no_unroll,&no_unroll,PETSC_NULL);CHKERRQ(ierr);
20100def2e27SBarry Smith     if (no_unroll) {ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_unroll\n");CHKERRQ(ierr);}
2011acfcf0e5SJed Brown     ierr = PetscOptionsBool("-mat_no_inode","Do not optimize for inodes (slower)",PETSC_NULL,no_inode,&no_inode,PETSC_NULL);CHKERRQ(ierr);
20120def2e27SBarry Smith     if (no_inode) {ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_inode\n");CHKERRQ(ierr);}
20130def2e27SBarry 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);
20140def2e27SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
2015ace3abfcSBarry Smith   b->inode.use = (PetscBool)(!(no_unroll || no_inode));
20160def2e27SBarry Smith   if (b->inode.limit > b->inode.max_limit) b->inode.limit = b->inode.max_limit;
20170def2e27SBarry Smith 
2018a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2019a23d5eceSKris Buschelman }
2020a23d5eceSKris Buschelman EXTERN_C_END
2021a23d5eceSKris Buschelman 
2022a23d5eceSKris Buschelman #undef __FUNCT__
2023a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation"
2024a23d5eceSKris Buschelman /*@C
2025a23d5eceSKris Buschelman    MatSeqSBAIJSetPreallocation - Creates a sparse symmetric matrix in block AIJ (block
2026a23d5eceSKris Buschelman    compressed row) format.  For good matrix assembly performance the
2027a23d5eceSKris Buschelman    user should preallocate the matrix storage by setting the parameter nz
2028a23d5eceSKris Buschelman    (or the array nnz).  By setting these parameters accurately, performance
2029a23d5eceSKris Buschelman    during matrix assembly can be increased by more than a factor of 50.
2030a23d5eceSKris Buschelman 
2031a23d5eceSKris Buschelman    Collective on Mat
2032a23d5eceSKris Buschelman 
2033a23d5eceSKris Buschelman    Input Parameters:
2034a23d5eceSKris Buschelman +  A - the symmetric matrix
2035a23d5eceSKris Buschelman .  bs - size of block
2036a23d5eceSKris Buschelman .  nz - number of block nonzeros per block row (same for all rows)
2037a23d5eceSKris Buschelman -  nnz - array containing the number of block nonzeros in the upper triangular plus
2038a23d5eceSKris Buschelman          diagonal portion of each block (possibly different for each block row) or PETSC_NULL
2039a23d5eceSKris Buschelman 
2040a23d5eceSKris Buschelman    Options Database Keys:
2041a23d5eceSKris Buschelman .   -mat_no_unroll - uses code that does not unroll the loops in the
2042a23d5eceSKris Buschelman                      block calculations (much slower)
2043db4efbfdSBarry Smith .    -mat_block_size - size of the blocks to use (only works if a negative bs is passed in
2044a23d5eceSKris Buschelman 
2045a23d5eceSKris Buschelman    Level: intermediate
2046a23d5eceSKris Buschelman 
2047a23d5eceSKris Buschelman    Notes:
2048a23d5eceSKris Buschelman    Specify the preallocated storage with either nz or nnz (not both).
2049a23d5eceSKris Buschelman    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.
2051a23d5eceSKris Buschelman 
2052aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
2053aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
2054aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
2055aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
2056aa95bbe8SBarry Smith 
205749a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
205849a6f317SBarry Smith 
205949a6f317SBarry Smith 
206069b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateSBAIJ()
2061a23d5eceSKris Buschelman @*/
20627087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt nz,const PetscInt nnz[])
206313f74950SBarry Smith {
20644ac538c5SBarry Smith   PetscErrorCode ierr;
2065a23d5eceSKris Buschelman 
2066a23d5eceSKris Buschelman   PetscFunctionBegin;
20676ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
20686ba663aaSJed Brown   PetscValidType(B,1);
20696ba663aaSJed Brown   PetscValidLogicalCollectiveInt(B,bs,2);
20704ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqSBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[]),(B,bs,nz,nnz));CHKERRQ(ierr);
2071a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2072a23d5eceSKris Buschelman }
207349b5e25fSSatish Balay 
20744a2ae208SSatish Balay #undef __FUNCT__
20754a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqSBAIJ"
2076c464158bSHong Zhang /*@C
2077c464158bSHong Zhang    MatCreateSeqSBAIJ - Creates a sparse symmetric matrix in block AIJ (block
2078c464158bSHong Zhang    compressed row) format.  For good matrix assembly performance the
2079c464158bSHong Zhang    user should preallocate the matrix storage by setting the parameter nz
2080c464158bSHong Zhang    (or the array nnz).  By setting these parameters accurately, performance
2081c464158bSHong Zhang    during matrix assembly can be increased by more than a factor of 50.
208249b5e25fSSatish Balay 
2083c464158bSHong Zhang    Collective on MPI_Comm
2084c464158bSHong Zhang 
2085c464158bSHong Zhang    Input Parameters:
2086c464158bSHong Zhang +  comm - MPI communicator, set to PETSC_COMM_SELF
2087c464158bSHong Zhang .  bs - size of block
2088c464158bSHong Zhang .  m - number of rows, or number of columns
2089c464158bSHong Zhang .  nz - number of block nonzeros per block row (same for all rows)
2090744e8345SSatish Balay -  nnz - array containing the number of block nonzeros in the upper triangular plus
2091744e8345SSatish Balay          diagonal portion of each block (possibly different for each block row) or PETSC_NULL
2092c464158bSHong Zhang 
2093c464158bSHong Zhang    Output Parameter:
2094c464158bSHong Zhang .  A - the symmetric matrix
2095c464158bSHong Zhang 
2096c464158bSHong Zhang    Options Database Keys:
2097c464158bSHong Zhang .   -mat_no_unroll - uses code that does not unroll the loops in the
2098c464158bSHong Zhang                      block calculations (much slower)
2099c464158bSHong Zhang .    -mat_block_size - size of the blocks to use
2100c464158bSHong Zhang 
2101c464158bSHong Zhang    Level: intermediate
2102c464158bSHong Zhang 
2103175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2104ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
2105175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2106175b88e8SBarry Smith 
2107c464158bSHong Zhang    Notes:
21086d6d819aSHong Zhang    The number of rows and columns must be divisible by blocksize.
21096d6d819aSHong Zhang    This matrix type does not support complex Hermitian operation.
2110c464158bSHong Zhang 
2111c464158bSHong Zhang    Specify the preallocated storage with either nz or nnz (not both).
2112c464158bSHong Zhang    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
21130598bfebSBarry Smith    allocation.  See the <a href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</a> for details.
2114c464158bSHong Zhang 
211549a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
211649a6f317SBarry Smith 
211769b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateSBAIJ()
2118c464158bSHong Zhang @*/
21197087cfbeSBarry Smith PetscErrorCode  MatCreateSeqSBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
2120c464158bSHong Zhang {
2121dfbe8321SBarry Smith   PetscErrorCode ierr;
2122c464158bSHong Zhang 
2123c464158bSHong Zhang   PetscFunctionBegin;
2124f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2125f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2126c464158bSHong Zhang   ierr = MatSetType(*A,MATSEQSBAIJ);CHKERRQ(ierr);
2127ab93d7beSBarry Smith   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*A,bs,nz,(PetscInt*)nnz);CHKERRQ(ierr);
212849b5e25fSSatish Balay   PetscFunctionReturn(0);
212949b5e25fSSatish Balay }
213049b5e25fSSatish Balay 
21314a2ae208SSatish Balay #undef __FUNCT__
21324a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqSBAIJ"
2133dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqSBAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
213449b5e25fSSatish Balay {
213549b5e25fSSatish Balay   Mat            C;
213649b5e25fSSatish Balay   Mat_SeqSBAIJ   *c,*a = (Mat_SeqSBAIJ*)A->data;
21376849ba73SBarry Smith   PetscErrorCode ierr;
2138b40805acSSatish Balay   PetscInt       i,mbs = a->mbs,nz = a->nz,bs2 =a->bs2;
213949b5e25fSSatish Balay 
214049b5e25fSSatish Balay   PetscFunctionBegin;
2141e32f2f54SBarry Smith   if (a->i[mbs] != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupt matrix");
214249b5e25fSSatish Balay 
214349b5e25fSSatish Balay   *B = 0;
21447adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2145d0f46423SBarry Smith   ierr = MatSetSizes(C,A->rmap->N,A->cmap->n,A->rmap->N,A->cmap->n);CHKERRQ(ierr);
21468e9a0fb8SHong Zhang   ierr = MatSetType(C,MATSEQSBAIJ);CHKERRQ(ierr);
21471d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
2148692f9cbeSHong Zhang   c    = (Mat_SeqSBAIJ*)C->data;
2149692f9cbeSHong Zhang 
2150273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2151d5f3da31SBarry Smith   C->factortype         = A->factortype;
215249b5e25fSSatish Balay   c->row                = 0;
215349b5e25fSSatish Balay   c->icol               = 0;
215449b5e25fSSatish Balay   c->saved_values       = 0;
2155a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
215649b5e25fSSatish Balay   C->assembled          = PETSC_TRUE;
215749b5e25fSSatish Balay 
21581e1e43feSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
21591e1e43feSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
216049b5e25fSSatish Balay   c->bs2  = a->bs2;
216149b5e25fSSatish Balay   c->mbs  = a->mbs;
216249b5e25fSSatish Balay   c->nbs  = a->nbs;
216349b5e25fSSatish Balay 
2164c760cd28SBarry Smith   if  (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2165c760cd28SBarry Smith     c->imax           = a->imax;
2166c760cd28SBarry Smith     c->ilen           = a->ilen;
2167c760cd28SBarry Smith     c->free_imax_ilen = PETSC_FALSE;
2168c760cd28SBarry Smith   } else {
21698777fc3fSSatish Balay     ierr = PetscMalloc2((mbs+1),PetscInt,&c->imax,(mbs+1),PetscInt,&c->ilen);CHKERRQ(ierr);
2170c760cd28SBarry Smith     ierr = PetscLogObjectMemory(C,2*(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
217149b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
217249b5e25fSSatish Balay       c->imax[i] = a->imax[i];
217349b5e25fSSatish Balay       c->ilen[i] = a->ilen[i];
217449b5e25fSSatish Balay     }
2175c760cd28SBarry Smith     c->free_imax_ilen = PETSC_TRUE;
2176c760cd28SBarry Smith   }
217749b5e25fSSatish Balay 
217849b5e25fSSatish Balay   /* allocate the matrix space */
21794da8f245SBarry Smith   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
21804da8f245SBarry Smith     ierr = PetscMalloc(bs2*nz*sizeof(MatScalar),&c->a);CHKERRQ(ierr);
21814da8f245SBarry Smith     ierr = PetscLogObjectMemory(C,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
21824da8f245SBarry Smith     c->singlemalloc = PETSC_FALSE;
21834da8f245SBarry Smith     c->free_ij      = PETSC_FALSE;
21844da8f245SBarry Smith     c->parent       = A;
21854da8f245SBarry Smith     ierr            = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
21864da8f245SBarry Smith     ierr            = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
21874da8f245SBarry Smith     ierr            = MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
21884da8f245SBarry Smith   } else {
2189b40805acSSatish Balay     ierr = PetscMalloc3(bs2*nz,MatScalar,&c->a,nz,PetscInt,&c->j,mbs+1,PetscInt,&c->i);CHKERRQ(ierr);
219013f74950SBarry Smith     ierr = PetscMemcpy(c->i,a->i,(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
2191b40805acSSatish Balay     ierr = PetscLogObjectMemory(C,(mbs+1)*sizeof(PetscInt) + nz*(bs2*sizeof(MatScalar) + sizeof(PetscInt)));CHKERRQ(ierr);
21924da8f245SBarry Smith     c->singlemalloc = PETSC_TRUE;
21934da8f245SBarry Smith     c->free_ij      = PETSC_TRUE;
21944da8f245SBarry Smith   }
219549b5e25fSSatish Balay   if (mbs > 0) {
21964da8f245SBarry Smith     if (cpvalues != MAT_SHARE_NONZERO_PATTERN) {
219713f74950SBarry Smith       ierr = PetscMemcpy(c->j,a->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
21984da8f245SBarry Smith     }
219949b5e25fSSatish Balay     if (cpvalues == MAT_COPY_VALUES) {
220049b5e25fSSatish Balay       ierr = PetscMemcpy(c->a,a->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
220149b5e25fSSatish Balay     } else {
220249b5e25fSSatish Balay       ierr = PetscMemzero(c->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
220349b5e25fSSatish Balay     }
2204a1c3900fSBarry Smith     if (a->jshort) {
22054da8f245SBarry Smith       if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
22064da8f245SBarry Smith         c->jshort      = a->jshort;
22074da8f245SBarry Smith         c->free_jshort = PETSC_FALSE;
22084da8f245SBarry Smith       } else {
2209a1c3900fSBarry Smith         ierr = PetscMalloc(nz*sizeof(unsigned short),&c->jshort);CHKERRQ(ierr);
2210c760cd28SBarry Smith         ierr = PetscLogObjectMemory(C,nz*sizeof(unsigned short));CHKERRQ(ierr);
2211a1c3900fSBarry Smith         ierr = PetscMemcpy(c->jshort,a->jshort,nz*sizeof(unsigned short));CHKERRQ(ierr);
22124da8f245SBarry Smith         c->free_jshort = PETSC_TRUE;
22134da8f245SBarry Smith       }
2214a1c3900fSBarry Smith     }
221549b5e25fSSatish Balay   }
221649b5e25fSSatish Balay 
221749b5e25fSSatish Balay   c->roworiented = a->roworiented;
221849b5e25fSSatish Balay   c->nonew       = a->nonew;
221949b5e25fSSatish Balay 
222049b5e25fSSatish Balay   if (a->diag) {
2221c760cd28SBarry Smith     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2222c760cd28SBarry Smith       c->diag      = a->diag;
2223c760cd28SBarry Smith       c->free_diag = PETSC_FALSE;
2224c760cd28SBarry Smith     } else {
22252ed38d0bSJed Brown       ierr = PetscMalloc(mbs*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
22262ed38d0bSJed Brown       ierr = PetscLogObjectMemory(C,mbs*sizeof(PetscInt));CHKERRQ(ierr);
222749b5e25fSSatish Balay       for (i=0; i<mbs; i++) {
222849b5e25fSSatish Balay 	c->diag[i] = a->diag[i];
222949b5e25fSSatish Balay       }
2230c760cd28SBarry Smith       c->free_diag = PETSC_TRUE;
2231c760cd28SBarry Smith     }
223249b5e25fSSatish Balay   } else c->diag  = 0;
22336c6c5352SBarry Smith   c->nz           = a->nz;
2234f2cbd3d5SJed Brown   c->maxnz        = a->nz; /* Since we allocate exactly the right amount */
223549b5e25fSSatish Balay   c->solve_work   = 0;
223649b5e25fSSatish Balay   c->mult_work    = 0;
2237e6b907acSBarry Smith   c->free_a       = PETSC_TRUE;
223849b5e25fSSatish Balay   *B = C;
22397adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
224049b5e25fSSatish Balay   PetscFunctionReturn(0);
224149b5e25fSSatish Balay }
224249b5e25fSSatish Balay 
22434a2ae208SSatish Balay #undef __FUNCT__
22445bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_SeqSBAIJ"
2245112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqSBAIJ(Mat newmat,PetscViewer viewer)
22462f480046SShri Abhyankar {
22472f480046SShri Abhyankar   Mat_SeqSBAIJ   *a;
22482f480046SShri Abhyankar   PetscErrorCode ierr;
22492f480046SShri Abhyankar   int            fd;
22502f480046SShri Abhyankar   PetscMPIInt    size;
22512f480046SShri Abhyankar   PetscInt       i,nz,header[4],*rowlengths=0,M,N,bs=1;
22522f480046SShri Abhyankar   PetscInt       *mask,mbs,*jj,j,rowcount,nzcount,k,*s_browlengths,maskcount;
22532f480046SShri Abhyankar   PetscInt       kmax,jcount,block,idx,point,nzcountb,extra_rows,rows,cols;
22542f480046SShri Abhyankar   PetscInt       *masked,nmask,tmp,bs2,ishift;
22552f480046SShri Abhyankar   PetscScalar    *aa;
22562f480046SShri Abhyankar   MPI_Comm       comm = ((PetscObject)viewer)->comm;
22572f480046SShri Abhyankar 
22582f480046SShri Abhyankar   PetscFunctionBegin;
2259c55dd799SBarry Smith   ierr = PetscOptionsGetInt(((PetscObject)newmat)->prefix,"-matload_block_size",&bs,PETSC_NULL);CHKERRQ(ierr);
22602f480046SShri Abhyankar   bs2  = bs*bs;
22612f480046SShri Abhyankar 
22622f480046SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
22632f480046SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
22642f480046SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
22652f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
22662f480046SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not Mat object");
22672f480046SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
22682f480046SShri Abhyankar 
22692f480046SShri Abhyankar   if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as SeqSBAIJ");
22702f480046SShri Abhyankar 
22712f480046SShri Abhyankar   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");
22722f480046SShri Abhyankar 
22732f480046SShri Abhyankar   /*
22742f480046SShri Abhyankar      This code adds extra rows to make sure the number of rows is
22752f480046SShri Abhyankar     divisible by the blocksize
22762f480046SShri Abhyankar   */
22772f480046SShri Abhyankar   mbs        = M/bs;
22782f480046SShri Abhyankar   extra_rows = bs - M + bs*(mbs);
22792f480046SShri Abhyankar   if (extra_rows == bs) extra_rows = 0;
22802f480046SShri Abhyankar   else                  mbs++;
22812f480046SShri Abhyankar   if (extra_rows) {
22822f480046SShri Abhyankar     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
22832f480046SShri Abhyankar   }
22842f480046SShri Abhyankar 
22852f480046SShri Abhyankar   /* Set global sizes if not already set */
22862f480046SShri Abhyankar   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) {
22872f480046SShri Abhyankar     ierr = MatSetSizes(newmat,PETSC_DECIDE,PETSC_DECIDE,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
22882f480046SShri Abhyankar   } else { /* Check if the matrix global sizes are correct */
22892f480046SShri Abhyankar     ierr = MatGetSize(newmat,&rows,&cols);CHKERRQ(ierr);
22902f480046SShri 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);
22912f480046SShri Abhyankar   }
22922f480046SShri Abhyankar 
22932f480046SShri Abhyankar   /* read in row lengths */
22942f480046SShri Abhyankar   ierr = PetscMalloc((M+extra_rows)*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
22952f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
22962f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
22972f480046SShri Abhyankar 
22982f480046SShri Abhyankar   /* read in column indices */
22992f480046SShri Abhyankar   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscInt),&jj);CHKERRQ(ierr);
23002f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,jj,nz,PETSC_INT);CHKERRQ(ierr);
23012f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) jj[nz+i] = M+i;
23022f480046SShri Abhyankar 
23032f480046SShri Abhyankar   /* loop over row lengths determining block row lengths */
23042f480046SShri Abhyankar   ierr     = PetscMalloc(mbs*sizeof(PetscInt),&s_browlengths);CHKERRQ(ierr);
23052f480046SShri Abhyankar   ierr     = PetscMemzero(s_browlengths,mbs*sizeof(PetscInt));CHKERRQ(ierr);
23062f480046SShri Abhyankar   ierr     = PetscMalloc2(mbs,PetscInt,&mask,mbs,PetscInt,&masked);CHKERRQ(ierr);
23072f480046SShri Abhyankar   ierr     = PetscMemzero(mask,mbs*sizeof(PetscInt));CHKERRQ(ierr);
23082f480046SShri Abhyankar   rowcount = 0;
23092f480046SShri Abhyankar   nzcount  = 0;
23102f480046SShri Abhyankar   for (i=0; i<mbs; i++) {
23112f480046SShri Abhyankar     nmask = 0;
23122f480046SShri Abhyankar     for (j=0; j<bs; j++) {
23132f480046SShri Abhyankar       kmax = rowlengths[rowcount];
23142f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
23152f480046SShri Abhyankar         tmp = jj[nzcount++]/bs;   /* block col. index */
23162f480046SShri Abhyankar         if (!mask[tmp] && tmp >= i) {masked[nmask++] = tmp; mask[tmp] = 1;}
23172f480046SShri Abhyankar       }
23182f480046SShri Abhyankar       rowcount++;
23192f480046SShri Abhyankar     }
23202f480046SShri Abhyankar     s_browlengths[i] += nmask;
23212f480046SShri Abhyankar 
23222f480046SShri Abhyankar     /* zero out the mask elements we set */
23232f480046SShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
23242f480046SShri Abhyankar   }
23252f480046SShri Abhyankar 
23262f480046SShri Abhyankar   /* Do preallocation */
23272f480046SShri Abhyankar   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(newmat,bs,0,s_browlengths);CHKERRQ(ierr);
23282f480046SShri Abhyankar   a = (Mat_SeqSBAIJ*)newmat->data;
23292f480046SShri Abhyankar 
23302f480046SShri Abhyankar   /* set matrix "i" values */
23312f480046SShri Abhyankar   a->i[0] = 0;
23322f480046SShri Abhyankar   for (i=1; i<= mbs; i++) {
23332f480046SShri Abhyankar     a->i[i]      = a->i[i-1] + s_browlengths[i-1];
23342f480046SShri Abhyankar     a->ilen[i-1] = s_browlengths[i-1];
23352f480046SShri Abhyankar   }
23362f480046SShri Abhyankar   a->nz = a->i[mbs];
23372f480046SShri Abhyankar 
23382f480046SShri Abhyankar   /* read in nonzero values */
23392f480046SShri Abhyankar   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscScalar),&aa);CHKERRQ(ierr);
23402f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,aa,nz,PETSC_SCALAR);CHKERRQ(ierr);
23412f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) aa[nz+i] = 1.0;
23422f480046SShri Abhyankar 
23432f480046SShri Abhyankar   /* set "a" and "j" values into matrix */
23442f480046SShri Abhyankar   nzcount = 0; jcount = 0;
23452f480046SShri Abhyankar   for (i=0; i<mbs; i++) {
23462f480046SShri Abhyankar     nzcountb = nzcount;
23472f480046SShri Abhyankar     nmask    = 0;
23482f480046SShri Abhyankar     for (j=0; j<bs; j++) {
23492f480046SShri Abhyankar       kmax = rowlengths[i*bs+j];
23502f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
23512f480046SShri Abhyankar         tmp = jj[nzcount++]/bs; /* block col. index */
23522f480046SShri Abhyankar         if (!mask[tmp] && tmp >= i) { masked[nmask++] = tmp; mask[tmp] = 1;}
23532f480046SShri Abhyankar       }
23542f480046SShri Abhyankar     }
23552f480046SShri Abhyankar     /* sort the masked values */
23562f480046SShri Abhyankar     ierr = PetscSortInt(nmask,masked);CHKERRQ(ierr);
23572f480046SShri Abhyankar 
23582f480046SShri Abhyankar     /* set "j" values into matrix */
23592f480046SShri Abhyankar     maskcount = 1;
23602f480046SShri Abhyankar     for (j=0; j<nmask; j++) {
23612f480046SShri Abhyankar       a->j[jcount++]  = masked[j];
23622f480046SShri Abhyankar       mask[masked[j]] = maskcount++;
23632f480046SShri Abhyankar     }
23642f480046SShri Abhyankar 
23652f480046SShri Abhyankar     /* set "a" values into matrix */
23662f480046SShri Abhyankar     ishift = bs2*a->i[i];
23672f480046SShri Abhyankar     for (j=0; j<bs; j++) {
23682f480046SShri Abhyankar       kmax = rowlengths[i*bs+j];
23692f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
23702f480046SShri Abhyankar         tmp       = jj[nzcountb]/bs ; /* block col. index */
23712f480046SShri Abhyankar         if (tmp >= i){
23722f480046SShri Abhyankar           block     = mask[tmp] - 1;
23732f480046SShri Abhyankar           point     = jj[nzcountb] - bs*tmp;
23742f480046SShri Abhyankar           idx       = ishift + bs2*block + j + bs*point;
23752f480046SShri Abhyankar           a->a[idx] = aa[nzcountb];
23762f480046SShri Abhyankar         }
23772f480046SShri Abhyankar         nzcountb++;
23782f480046SShri Abhyankar       }
23792f480046SShri Abhyankar     }
23802f480046SShri Abhyankar     /* zero out the mask elements we set */
23812f480046SShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
23822f480046SShri Abhyankar   }
23832f480046SShri Abhyankar   if (jcount != a->nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Bad binary matrix");
23842f480046SShri Abhyankar 
23852f480046SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
23862f480046SShri Abhyankar   ierr = PetscFree(s_browlengths);CHKERRQ(ierr);
23872f480046SShri Abhyankar   ierr = PetscFree(aa);CHKERRQ(ierr);
23882f480046SShri Abhyankar   ierr = PetscFree(jj);CHKERRQ(ierr);
23892f480046SShri Abhyankar   ierr = PetscFree2(mask,masked);CHKERRQ(ierr);
23902f480046SShri Abhyankar 
23912f480046SShri Abhyankar   ierr = MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
23922f480046SShri Abhyankar   ierr = MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
23932f480046SShri Abhyankar   ierr = MatView_Private(newmat);CHKERRQ(ierr);
23942f480046SShri Abhyankar   PetscFunctionReturn(0);
23952f480046SShri Abhyankar }
23962f480046SShri Abhyankar 
23972f480046SShri Abhyankar #undef __FUNCT__
2398c75a6043SHong Zhang #define __FUNCT__ "MatCreateSeqSBAIJWithArrays"
2399c75a6043SHong Zhang /*@
2400c75a6043SHong Zhang      MatCreateSeqSBAIJWithArrays - Creates an sequential SBAIJ matrix using matrix elements
2401c75a6043SHong Zhang               (upper triangular entries in CSR format) provided by the user.
2402c75a6043SHong Zhang 
2403c75a6043SHong Zhang      Collective on MPI_Comm
2404c75a6043SHong Zhang 
2405c75a6043SHong Zhang    Input Parameters:
2406c75a6043SHong Zhang +  comm - must be an MPI communicator of size 1
2407c75a6043SHong Zhang .  bs - size of block
2408c75a6043SHong Zhang .  m - number of rows
2409c75a6043SHong Zhang .  n - number of columns
2410c75a6043SHong Zhang .  i - row indices
2411c75a6043SHong Zhang .  j - column indices
2412c75a6043SHong Zhang -  a - matrix values
2413c75a6043SHong Zhang 
2414c75a6043SHong Zhang    Output Parameter:
2415c75a6043SHong Zhang .  mat - the matrix
2416c75a6043SHong Zhang 
2417dfb205c3SBarry Smith    Level: advanced
2418c75a6043SHong Zhang 
2419c75a6043SHong Zhang    Notes:
2420c75a6043SHong Zhang        The i, j, and a arrays are not copied by this routine, the user must free these arrays
2421c75a6043SHong Zhang     once the matrix is destroyed
2422c75a6043SHong Zhang 
2423c75a6043SHong Zhang        You cannot set new nonzero locations into this matrix, that will generate an error.
2424c75a6043SHong Zhang 
2425c75a6043SHong Zhang        The i and j indices are 0 based
2426c75a6043SHong Zhang 
2427dfb205c3SBarry 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
2428dfb205c3SBarry Smith        it is the regular CSR format excluding the lower triangular elements.
2429dfb205c3SBarry Smith 
243069b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSBAIJ(), MatCreateSeqSBAIJ()
2431c75a6043SHong Zhang 
2432c75a6043SHong Zhang @*/
24337087cfbeSBarry Smith PetscErrorCode  MatCreateSeqSBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
2434c75a6043SHong Zhang {
2435c75a6043SHong Zhang   PetscErrorCode ierr;
2436c75a6043SHong Zhang   PetscInt       ii;
2437c75a6043SHong Zhang   Mat_SeqSBAIJ   *sbaij;
2438c75a6043SHong Zhang 
2439c75a6043SHong Zhang   PetscFunctionBegin;
2440e32f2f54SBarry Smith   if (bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"block size %D > 1 is not supported yet",bs);
2441e32f2f54SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
2442c75a6043SHong Zhang 
2443c75a6043SHong Zhang   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
2444c75a6043SHong Zhang   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
2445c75a6043SHong Zhang   ierr = MatSetType(*mat,MATSEQSBAIJ);CHKERRQ(ierr);
2446c75a6043SHong Zhang   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*mat,bs,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
2447c75a6043SHong Zhang   sbaij = (Mat_SeqSBAIJ*)(*mat)->data;
2448c75a6043SHong Zhang   ierr = PetscMalloc2(m,PetscInt,&sbaij->imax,m,PetscInt,&sbaij->ilen);CHKERRQ(ierr);
2449c760cd28SBarry Smith   ierr = PetscLogObjectMemory(*mat,2*m*sizeof(PetscInt));CHKERRQ(ierr);
2450c75a6043SHong Zhang 
2451c75a6043SHong Zhang   sbaij->i = i;
2452c75a6043SHong Zhang   sbaij->j = j;
2453c75a6043SHong Zhang   sbaij->a = a;
2454c75a6043SHong Zhang   sbaij->singlemalloc = PETSC_FALSE;
2455c75a6043SHong Zhang   sbaij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
2456e6b907acSBarry Smith   sbaij->free_a       = PETSC_FALSE;
2457e6b907acSBarry Smith   sbaij->free_ij      = PETSC_FALSE;
2458c75a6043SHong Zhang 
2459c75a6043SHong Zhang   for (ii=0; ii<m; ii++) {
2460c75a6043SHong Zhang     sbaij->ilen[ii] = sbaij->imax[ii] = i[ii+1] - i[ii];
2461c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2462e32f2f54SBarry 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]);
2463c75a6043SHong Zhang #endif
2464c75a6043SHong Zhang   }
2465c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2466c75a6043SHong Zhang   for (ii=0; ii<sbaij->i[m]; ii++) {
2467e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
2468e32f2f54SBarry 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]);
2469c75a6043SHong Zhang   }
2470c75a6043SHong Zhang #endif
2471c75a6043SHong Zhang 
2472c75a6043SHong Zhang   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2473c75a6043SHong Zhang   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2474c75a6043SHong Zhang   PetscFunctionReturn(0);
2475c75a6043SHong Zhang }
2476d06b337dSHong Zhang 
2477d06b337dSHong Zhang 
2478d06b337dSHong Zhang 
247949b5e25fSSatish Balay 
248049b5e25fSSatish Balay 
2481