xref: /petsc/src/mat/impls/sbaij/seq/sbaij.c (revision 666a761f690849d3cf42e718a982bcff41cfee09)
149b5e25fSSatish Balay 
249b5e25fSSatish Balay /*
3a1373b80SHong Zhang     Defines the basic matrix operations for the SBAIJ (compressed row)
449b5e25fSSatish Balay   matrix storage format.
549b5e25fSSatish Balay */
6c6db04a5SJed Brown #include <../src/mat/impls/baij/seq/baij.h>         /*I "petscmat.h" I*/
7c6db04a5SJed Brown #include <../src/mat/impls/sbaij/seq/sbaij.h>
8c6db04a5SJed Brown #include <petscblaslapack.h>
949b5e25fSSatish Balay 
10c6db04a5SJed Brown #include <../src/mat/impls/sbaij/seq/relax.h>
1170dcbbb9SBarry Smith #define USESHORT
12c6db04a5SJed Brown #include <../src/mat/impls/sbaij/seq/relax.h>
1370dcbbb9SBarry Smith 
14ace3abfcSBarry Smith extern PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat,PetscBool );
15b5b17502SBarry Smith 
1649b5e25fSSatish Balay /*
1749b5e25fSSatish Balay      Checks for missing diagonals
1849b5e25fSSatish Balay */
194a2ae208SSatish Balay #undef __FUNCT__
204a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqSBAIJ"
21ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqSBAIJ(Mat A,PetscBool  *missing,PetscInt *dd)
2249b5e25fSSatish Balay {
23045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
246849ba73SBarry Smith   PetscErrorCode ierr;
2513f74950SBarry Smith   PetscInt       *diag,*jj = a->j,i;
2649b5e25fSSatish Balay 
2749b5e25fSSatish Balay   PetscFunctionBegin;
28045c9aa0SHong Zhang   ierr = MatMarkDiagonal_SeqSBAIJ(A);CHKERRQ(ierr);
2949b5e25fSSatish Balay   diag = a->diag;
302af78befSBarry Smith   *missing = PETSC_FALSE;
3149b5e25fSSatish Balay   for (i=0; i<a->mbs; i++) {
322af78befSBarry Smith     if (jj[diag[i]] != i) {
332af78befSBarry Smith       *missing    = PETSC_TRUE;
342af78befSBarry Smith       if (dd) *dd = i;
352af78befSBarry Smith       break;
362af78befSBarry Smith     }
3749b5e25fSSatish Balay   }
3849b5e25fSSatish Balay   PetscFunctionReturn(0);
3949b5e25fSSatish Balay }
4049b5e25fSSatish Balay 
414a2ae208SSatish Balay #undef __FUNCT__
424a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqSBAIJ"
43dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqSBAIJ(Mat A)
4449b5e25fSSatish Balay {
45045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
466849ba73SBarry Smith   PetscErrorCode ierr;
4709f38230SBarry Smith   PetscInt       i;
4849b5e25fSSatish Balay 
4949b5e25fSSatish Balay   PetscFunctionBegin;
5009f38230SBarry Smith   if (!a->diag) {
5109f38230SBarry Smith     ierr = PetscMalloc(a->mbs*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
52c760cd28SBarry Smith     ierr = PetscLogObjectMemory(A,a->mbs*sizeof(PetscInt));CHKERRQ(ierr);
53c760cd28SBarry Smith     a->free_diag = PETSC_TRUE;
5409f38230SBarry Smith   }
5509f38230SBarry Smith   for (i=0; i<a->mbs; i++) a->diag[i] = a->i[i];
5649b5e25fSSatish Balay   PetscFunctionReturn(0);
5749b5e25fSSatish Balay }
5849b5e25fSSatish Balay 
594a2ae208SSatish Balay #undef __FUNCT__
604a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqSBAIJ"
61ace3abfcSBarry Smith static PetscErrorCode MatGetRowIJ_SeqSBAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
6249b5e25fSSatish Balay {
63a6ece127SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
64d0f46423SBarry Smith   PetscInt     i,j,n = a->mbs,nz = a->i[n],bs = A->rmap->bs;
658f7157efSSatish Balay   PetscErrorCode ierr;
6649b5e25fSSatish Balay 
6749b5e25fSSatish Balay   PetscFunctionBegin;
68d3e5a4abSHong Zhang   *nn = n;
69a1373b80SHong Zhang   if (!ia) PetscFunctionReturn(0);
708f7157efSSatish Balay   if (!blockcompressed) {
718f7157efSSatish Balay     /* malloc & create the natural set of indices */
72f1d0d59dSSatish Balay     ierr = PetscMalloc2((n+1)*bs,PetscInt,ia,nz*bs,PetscInt,ja);CHKERRQ(ierr);
738f7157efSSatish Balay     for (i=0; i<n+1; i++) {
748f7157efSSatish Balay       for (j=0; j<bs; j++) {
758f7157efSSatish Balay         *ia[i*bs+j] = a->i[i]*bs+j+oshift;
768f7157efSSatish Balay       }
778f7157efSSatish Balay     }
788f7157efSSatish Balay     for (i=0; i<nz; i++) {
798f7157efSSatish Balay       for (j=0; j<bs; j++) {
808f7157efSSatish Balay         *ja[i*bs+j] = a->j[i]*bs+j+oshift;
818f7157efSSatish Balay       }
828f7157efSSatish Balay     }
838f7157efSSatish Balay   } else { /* blockcompressed */
84a6ece127SHong Zhang     if (oshift == 1) {
85a6ece127SHong Zhang       /* temporarily add 1 to i and j indices */
866c6c5352SBarry Smith       for (i=0; i<nz; i++) a->j[i]++;
87a1373b80SHong Zhang       for (i=0; i<n+1; i++) a->i[i]++;
888f7157efSSatish Balay     }
89a1373b80SHong Zhang     *ia = a->i; *ja = a->j;
90a6ece127SHong Zhang   }
918f7157efSSatish Balay 
9249b5e25fSSatish Balay   PetscFunctionReturn(0);
9349b5e25fSSatish Balay }
9449b5e25fSSatish Balay 
954a2ae208SSatish Balay #undef __FUNCT__
964a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqSBAIJ"
97ace3abfcSBarry Smith static PetscErrorCode MatRestoreRowIJ_SeqSBAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  blockcompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
9849b5e25fSSatish Balay {
99b7aaefc3SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
1008f7157efSSatish Balay   PetscInt     i,n = a->mbs,nz = a->i[n];
1018f7157efSSatish Balay   PetscErrorCode ierr;
102a6ece127SHong Zhang 
10349b5e25fSSatish Balay   PetscFunctionBegin;
10449b5e25fSSatish Balay   if (!ia) PetscFunctionReturn(0);
105a6ece127SHong Zhang 
1068f7157efSSatish Balay   if (!blockcompressed) {
1078f7157efSSatish Balay     ierr = PetscFree2(*ia,*ja);CHKERRQ(ierr);
1088f7157efSSatish Balay   } else if (oshift == 1) { /* blockcompressed */
1096c6c5352SBarry Smith     for (i=0; i<nz; i++) a->j[i]--;
110a6ece127SHong Zhang     for (i=0; i<n+1; i++) a->i[i]--;
111a6ece127SHong Zhang   }
1128f7157efSSatish Balay 
113a6ece127SHong Zhang   PetscFunctionReturn(0);
11449b5e25fSSatish Balay }
11549b5e25fSSatish Balay 
1164a2ae208SSatish Balay #undef __FUNCT__
1174a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqSBAIJ"
118dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqSBAIJ(Mat A)
11949b5e25fSSatish Balay {
12049b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
121dfbe8321SBarry Smith   PetscErrorCode ierr;
12249b5e25fSSatish Balay 
12349b5e25fSSatish Balay   PetscFunctionBegin;
124a9f03627SSatish Balay #if defined(PETSC_USE_LOG)
125d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, NZ=%D",A->rmap->N,a->nz);
126a9f03627SSatish Balay #endif
127e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
1287f53bb6cSHong Zhang   if (a->free_diag){ierr = PetscFree(a->diag);CHKERRQ(ierr);}
1296bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
1306bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
1316bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
132c31cb41cSBarry Smith   ierr = PetscFree(a->idiag);CHKERRQ(ierr);
133c31cb41cSBarry Smith   ierr = PetscFree(a->inode.size);CHKERRQ(ierr);
134c31cb41cSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
135c760cd28SBarry Smith   if (a->free_imax_ilen) {ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);}
13605b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
13741f059aeSBarry Smith   ierr = PetscFree(a->sor_work);CHKERRQ(ierr);
13805b42c5fSBarry Smith   ierr = PetscFree(a->solves_work);CHKERRQ(ierr);
13905b42c5fSBarry Smith   ierr = PetscFree(a->mult_work);CHKERRQ(ierr);
14005b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
14105b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
1424da8f245SBarry Smith   if (a->free_jshort) {ierr = PetscFree(a->jshort);CHKERRQ(ierr);}
1431a3463dfSHong Zhang   ierr = PetscFree(a->inew);CHKERRQ(ierr);
1446bf464f9SBarry Smith   ierr = MatDestroy(&a->parent);CHKERRQ(ierr);
145bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
146901853e0SKris Buschelman 
147dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
148901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
149901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
150901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
151901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqaij_C","",PETSC_NULL);CHKERRQ(ierr);
152901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
153901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
154aa5a9175SDahai Guo   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqsbstrm_C","",PETSC_NULL);CHKERRQ(ierr);
15549b5e25fSSatish Balay   PetscFunctionReturn(0);
15649b5e25fSSatish Balay }
15749b5e25fSSatish Balay 
1584a2ae208SSatish Balay #undef __FUNCT__
1594a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqSBAIJ"
160ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqSBAIJ(Mat A,MatOption op,PetscBool  flg)
16149b5e25fSSatish Balay {
162045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
16363ba0a88SBarry Smith   PetscErrorCode ierr;
16449b5e25fSSatish Balay 
16549b5e25fSSatish Balay   PetscFunctionBegin;
1664d9d31abSKris Buschelman   switch (op) {
1674d9d31abSKris Buschelman   case MAT_ROW_ORIENTED:
1684e0d8c25SBarry Smith     a->roworiented = flg;
1694d9d31abSKris Buschelman     break;
170a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
171a9817697SBarry Smith     a->keepnonzeropattern = flg;
1724d9d31abSKris Buschelman     break;
173512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
174512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1754d9d31abSKris Buschelman     break;
1764d9d31abSKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
1774e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1784d9d31abSKris Buschelman     break;
1794d9d31abSKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1804e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1814d9d31abSKris Buschelman     break;
18228b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
18328b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
18428b2fa4aSMatthew Knepley     break;
1854e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1864d9d31abSKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1874d9d31abSKris Buschelman   case MAT_USE_HASH_TABLE:
188290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1894d9d31abSKris Buschelman     break;
1909a4540c5SBarry Smith   case MAT_HERMITIAN:
191e32f2f54SBarry Smith     if (!A->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call MatAssemblyEnd() first");
1920bc54ff2SBarry Smith     if (A->cmap->n < 65536 && A->cmap->bs == 1) {
193eeffb40dSHong Zhang       A->ops->mult = MatMult_SeqSBAIJ_1_Hermitian_ushort;
1940bc54ff2SBarry Smith     } else if (A->cmap->bs == 1) {
195eeffb40dSHong Zhang       A->ops->mult = MatMult_SeqSBAIJ_1_Hermitian;
196e32f2f54SBarry Smith     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for Hermitian with block size greater than 1");
197eeffb40dSHong Zhang     break;
1983d472b54SHong Zhang   case MAT_SPD:
1993d472b54SHong Zhang     A->spd_set                         = PETSC_TRUE;
2003d472b54SHong Zhang     A->spd                             = flg;
2013d472b54SHong Zhang     if (flg) {
2023d472b54SHong Zhang       A->symmetric                     = PETSC_TRUE;
2033d472b54SHong Zhang       A->structurally_symmetric        = PETSC_TRUE;
2043d472b54SHong Zhang       A->symmetric_set                 = PETSC_TRUE;
2053d472b54SHong Zhang       A->structurally_symmetric_set    = PETSC_TRUE;
2063d472b54SHong Zhang     }
2073d472b54SHong Zhang     break;
20877e54ba9SKris Buschelman   case MAT_SYMMETRIC:
20977e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
2109a4540c5SBarry Smith   case MAT_SYMMETRY_ETERNAL:
211e32f2f54SBarry Smith     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix must be symmetric");
212290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s not relevent\n",MatOptions[op]);CHKERRQ(ierr);
213290bbb0aSBarry Smith     break;
214941593c8SHong Zhang   case MAT_IGNORE_LOWER_TRIANGULAR:
2154e0d8c25SBarry Smith     a->ignore_ltriangular = flg;
216941593c8SHong Zhang     break;
217941593c8SHong Zhang   case MAT_ERROR_LOWER_TRIANGULAR:
2184e0d8c25SBarry Smith     a->ignore_ltriangular = flg;
21977e54ba9SKris Buschelman     break;
220f5edf698SHong Zhang   case MAT_GETROW_UPPERTRIANGULAR:
2214e0d8c25SBarry Smith     a->getrow_utriangular = flg;
222f5edf698SHong Zhang     break;
2234d9d31abSKris Buschelman   default:
224e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
22549b5e25fSSatish Balay   }
22649b5e25fSSatish Balay   PetscFunctionReturn(0);
22749b5e25fSSatish Balay }
22849b5e25fSSatish Balay 
2294a2ae208SSatish Balay #undef __FUNCT__
2304a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqSBAIJ"
23113f74950SBarry Smith PetscErrorCode MatGetRow_SeqSBAIJ(Mat A,PetscInt row,PetscInt *ncols,PetscInt **cols,PetscScalar **v)
23249b5e25fSSatish Balay {
23349b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
2346849ba73SBarry Smith   PetscErrorCode ierr;
23513f74950SBarry Smith   PetscInt       itmp,i,j,k,M,*ai,*aj,bs,bn,bp,*cols_i,bs2;
23649b5e25fSSatish Balay   MatScalar      *aa,*aa_i;
23787828ca2SBarry Smith   PetscScalar    *v_i;
23849b5e25fSSatish Balay 
23949b5e25fSSatish Balay   PetscFunctionBegin;
240e32f2f54SBarry Smith   if (A && !a->getrow_utriangular) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatGetRow is not supported for SBAIJ matrix format. Getting the upper triangular part of row, run with -mat_getrow_uppertriangular, call MatSetOption(mat,MAT_GETROW_UPPERTRIANGULAR,PETSC_TRUE) or MatGetRowUpperTriangular()");
241f5edf698SHong Zhang   /* Get the upper triangular part of the row */
242d0f46423SBarry Smith   bs  = A->rmap->bs;
24349b5e25fSSatish Balay   ai  = a->i;
24449b5e25fSSatish Balay   aj  = a->j;
24549b5e25fSSatish Balay   aa  = a->a;
24649b5e25fSSatish Balay   bs2 = a->bs2;
24749b5e25fSSatish Balay 
248e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Row %D out of range", row);
24949b5e25fSSatish Balay 
25049b5e25fSSatish Balay   bn  = row/bs;   /* Block number */
25149b5e25fSSatish Balay   bp  = row % bs; /* Block position */
25249b5e25fSSatish Balay   M   = ai[bn+1] - ai[bn];
25349b5e25fSSatish Balay   *ncols = bs*M;
25449b5e25fSSatish Balay 
25549b5e25fSSatish Balay   if (v) {
25649b5e25fSSatish Balay     *v = 0;
25749b5e25fSSatish Balay     if (*ncols) {
25887828ca2SBarry Smith       ierr = PetscMalloc((*ncols+row)*sizeof(PetscScalar),v);CHKERRQ(ierr);
25949b5e25fSSatish Balay       for (i=0; i<M; i++) { /* for each block in the block row */
26049b5e25fSSatish Balay         v_i  = *v + i*bs;
26149b5e25fSSatish Balay         aa_i = aa + bs2*(ai[bn] + i);
26249b5e25fSSatish Balay         for (j=bp,k=0; j<bs2; j+=bs,k++) {v_i[k] = aa_i[j];}
26349b5e25fSSatish Balay       }
26449b5e25fSSatish Balay     }
26549b5e25fSSatish Balay   }
26649b5e25fSSatish Balay 
26749b5e25fSSatish Balay   if (cols) {
26849b5e25fSSatish Balay     *cols = 0;
26949b5e25fSSatish Balay     if (*ncols) {
27013f74950SBarry Smith       ierr = PetscMalloc((*ncols+row)*sizeof(PetscInt),cols);CHKERRQ(ierr);
27149b5e25fSSatish Balay       for (i=0; i<M; i++) { /* for each block in the block row */
27249b5e25fSSatish Balay         cols_i = *cols + i*bs;
27349b5e25fSSatish Balay         itmp  = bs*aj[ai[bn] + i];
27449b5e25fSSatish Balay         for (j=0; j<bs; j++) {cols_i[j] = itmp++;}
27549b5e25fSSatish Balay       }
27649b5e25fSSatish Balay     }
27749b5e25fSSatish Balay   }
27849b5e25fSSatish Balay 
27949b5e25fSSatish Balay   /*search column A(0:row-1,row) (=A(row,0:row-1)). Could be expensive! */
2805ddb2528SHong Zhang   /* this segment is currently removed, so only entries in the upper triangle are obtained */
2815ddb2528SHong Zhang #ifdef column_search
28249b5e25fSSatish Balay   v_i    = *v    + M*bs;
28349b5e25fSSatish Balay   cols_i = *cols + M*bs;
28449b5e25fSSatish Balay   for (i=0; i<bn; i++){ /* for each block row */
28549b5e25fSSatish Balay     M = ai[i+1] - ai[i];
28649b5e25fSSatish Balay     for (j=0; j<M; j++){
28749b5e25fSSatish Balay       itmp = aj[ai[i] + j];    /* block column value */
28849b5e25fSSatish Balay       if (itmp == bn){
28949b5e25fSSatish Balay         aa_i   = aa    + bs2*(ai[i] + j) + bs*bp;
29049b5e25fSSatish Balay         for (k=0; k<bs; k++) {
29149b5e25fSSatish Balay           *cols_i++ = i*bs+k;
29249b5e25fSSatish Balay           *v_i++    = aa_i[k];
29349b5e25fSSatish Balay         }
29449b5e25fSSatish Balay         *ncols += bs;
29549b5e25fSSatish Balay         break;
29649b5e25fSSatish Balay       }
29749b5e25fSSatish Balay     }
29849b5e25fSSatish Balay   }
2995ddb2528SHong Zhang #endif
30049b5e25fSSatish Balay   PetscFunctionReturn(0);
30149b5e25fSSatish Balay }
30249b5e25fSSatish Balay 
3034a2ae208SSatish Balay #undef __FUNCT__
3044a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqSBAIJ"
30513f74950SBarry Smith PetscErrorCode MatRestoreRow_SeqSBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
30649b5e25fSSatish Balay {
307dfbe8321SBarry Smith   PetscErrorCode ierr;
30849b5e25fSSatish Balay 
30949b5e25fSSatish Balay   PetscFunctionBegin;
31005b42c5fSBarry Smith   if (idx) {ierr = PetscFree(*idx);CHKERRQ(ierr);}
31105b42c5fSBarry Smith   if (v)   {ierr = PetscFree(*v);CHKERRQ(ierr);}
31249b5e25fSSatish Balay   PetscFunctionReturn(0);
31349b5e25fSSatish Balay }
31449b5e25fSSatish Balay 
3154a2ae208SSatish Balay #undef __FUNCT__
316f5edf698SHong Zhang #define __FUNCT__ "MatGetRowUpperTriangular_SeqSBAIJ"
317f5edf698SHong Zhang PetscErrorCode MatGetRowUpperTriangular_SeqSBAIJ(Mat A)
318f5edf698SHong Zhang {
319f5edf698SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
320f5edf698SHong Zhang 
321f5edf698SHong Zhang   PetscFunctionBegin;
322f5edf698SHong Zhang   a->getrow_utriangular = PETSC_TRUE;
323f5edf698SHong Zhang   PetscFunctionReturn(0);
324f5edf698SHong Zhang }
325f5edf698SHong Zhang #undef __FUNCT__
326f5edf698SHong Zhang #define __FUNCT__ "MatRestoreRowUpperTriangular_SeqSBAIJ"
327f5edf698SHong Zhang PetscErrorCode MatRestoreRowUpperTriangular_SeqSBAIJ(Mat A)
328f5edf698SHong Zhang {
329f5edf698SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
330f5edf698SHong Zhang 
331f5edf698SHong Zhang   PetscFunctionBegin;
332f5edf698SHong Zhang   a->getrow_utriangular = PETSC_FALSE;
333f5edf698SHong Zhang   PetscFunctionReturn(0);
334f5edf698SHong Zhang }
335f5edf698SHong Zhang 
336f5edf698SHong Zhang #undef __FUNCT__
3374a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqSBAIJ"
338fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqSBAIJ(Mat A,MatReuse reuse,Mat *B)
33949b5e25fSSatish Balay {
340dfbe8321SBarry Smith   PetscErrorCode ierr;
34149b5e25fSSatish Balay   PetscFunctionBegin;
342815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
343999d9058SBarry Smith     ierr = MatDuplicate(A,MAT_COPY_VALUES,B);CHKERRQ(ierr);
344fc4dec0aSBarry Smith   }
3458115998fSBarry Smith   PetscFunctionReturn(0);
34649b5e25fSSatish Balay }
34749b5e25fSSatish Balay 
3484a2ae208SSatish Balay #undef __FUNCT__
3494a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_ASCII"
3506849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_ASCII(Mat A,PetscViewer viewer)
35149b5e25fSSatish Balay {
35249b5e25fSSatish Balay   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
353dfbe8321SBarry Smith   PetscErrorCode    ierr;
354d0f46423SBarry Smith   PetscInt          i,j,bs = A->rmap->bs,k,l,bs2=a->bs2;
355f3ef73ceSBarry Smith   PetscViewerFormat format;
35649b5e25fSSatish Balay 
35749b5e25fSSatish Balay   PetscFunctionBegin;
358b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
359456192e2SBarry Smith   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
36077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);CHKERRQ(ierr);
361fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
362d2507d54SMatthew Knepley     Mat aij;
363d5f3da31SBarry Smith     if (A->factortype && bs>1){
36470d5e725SHong 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);
36570d5e725SHong Zhang       PetscFunctionReturn(0);
36670d5e725SHong Zhang     }
367c9f458caSMatthew Knepley     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&aij);CHKERRQ(ierr);
368c9f458caSMatthew Knepley     ierr = MatView(aij,viewer);CHKERRQ(ierr);
3696bf464f9SBarry Smith     ierr = MatDestroy(&aij);CHKERRQ(ierr);
370fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
371d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
37249b5e25fSSatish Balay     for (i=0; i<a->mbs; i++) {
37349b5e25fSSatish Balay       for (j=0; j<bs; j++) {
37477431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
37549b5e25fSSatish Balay         for (k=a->i[i]; k<a->i[i+1]; k++) {
37649b5e25fSSatish Balay           for (l=0; l<bs; l++) {
37749b5e25fSSatish Balay #if defined(PETSC_USE_COMPLEX)
37849b5e25fSSatish Balay             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
379a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",bs*a->j[k]+l,
38049b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
38149b5e25fSSatish Balay             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
382a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",bs*a->j[k]+l,
38349b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),-PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
38449b5e25fSSatish Balay             } else if (PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
385a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
38649b5e25fSSatish Balay             }
38749b5e25fSSatish Balay #else
38849b5e25fSSatish Balay             if (a->a[bs2*k + l*bs + j] != 0.0) {
389a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
39049b5e25fSSatish Balay             }
39149b5e25fSSatish Balay #endif
39249b5e25fSSatish Balay           }
39349b5e25fSSatish Balay         }
394b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
39549b5e25fSSatish Balay       }
39649b5e25fSSatish Balay     }
397d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
398c1490034SHong Zhang   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
399c1490034SHong Zhang      PetscFunctionReturn(0);
40049b5e25fSSatish Balay   } else {
401d5f3da31SBarry Smith     if (A->factortype && bs>1){
4028608aa04SHong Zhang       ierr = PetscPrintf(PETSC_COMM_SELF,"Warning: matrix is factored. MatView_SeqSBAIJ_ASCII() may not display complete or logically correct entries!\n");CHKERRQ(ierr);
4038608aa04SHong Zhang     }
404d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4057566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
40649b5e25fSSatish Balay     for (i=0; i<a->mbs; i++) {
40749b5e25fSSatish Balay       for (j=0; j<bs; j++) {
40877431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
40949b5e25fSSatish Balay         for (k=a->i[i]; k<a->i[i+1]; k++) {
41049b5e25fSSatish Balay           for (l=0; l<bs; l++) {
41149b5e25fSSatish Balay #if defined(PETSC_USE_COMPLEX)
41249b5e25fSSatish Balay             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0) {
413a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",bs*a->j[k]+l,
41449b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
41549b5e25fSSatish Balay             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0) {
416a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i) ",bs*a->j[k]+l,
41749b5e25fSSatish Balay                                             PetscRealPart(a->a[bs2*k + l*bs + j]),-PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
41849b5e25fSSatish Balay             } else {
419a83599f4SBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
42049b5e25fSSatish Balay             }
42149b5e25fSSatish Balay #else
422e9f7bc9eSHong Zhang             ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",bs*a->j[k]+l,a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
42349b5e25fSSatish Balay #endif
42449b5e25fSSatish Balay           }
42549b5e25fSSatish Balay         }
426b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
42749b5e25fSSatish Balay       }
42849b5e25fSSatish Balay     }
429d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
43049b5e25fSSatish Balay   }
431b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
43249b5e25fSSatish Balay   PetscFunctionReturn(0);
43349b5e25fSSatish Balay }
43449b5e25fSSatish Balay 
4354a2ae208SSatish Balay #undef __FUNCT__
4364a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_Draw_Zoom"
4376849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
43849b5e25fSSatish Balay {
43949b5e25fSSatish Balay   Mat            A = (Mat) Aa;
44049b5e25fSSatish Balay   Mat_SeqSBAIJ   *a=(Mat_SeqSBAIJ*)A->data;
4416849ba73SBarry Smith   PetscErrorCode ierr;
442d0f46423SBarry Smith   PetscInt       row,i,j,k,l,mbs=a->mbs,color,bs=A->rmap->bs,bs2=a->bs2;
44313f74950SBarry Smith   PetscMPIInt    rank;
44449b5e25fSSatish Balay   PetscReal      xl,yl,xr,yr,x_l,x_r,y_l,y_r;
44549b5e25fSSatish Balay   MatScalar      *aa;
44649b5e25fSSatish Balay   MPI_Comm       comm;
447b0a32e0cSBarry Smith   PetscViewer    viewer;
44849b5e25fSSatish Balay 
44949b5e25fSSatish Balay   PetscFunctionBegin;
45049b5e25fSSatish Balay   /*
45149b5e25fSSatish Balay     This is nasty. If this is called from an originally parallel matrix
45249b5e25fSSatish Balay     then all processes call this,but only the first has the matrix so the
45349b5e25fSSatish Balay     rest should return immediately.
45449b5e25fSSatish Balay   */
45549b5e25fSSatish Balay   ierr = PetscObjectGetComm((PetscObject)draw,&comm);CHKERRQ(ierr);
45649b5e25fSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
45749b5e25fSSatish Balay   if (rank) PetscFunctionReturn(0);
45849b5e25fSSatish Balay 
45949b5e25fSSatish Balay   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
46049b5e25fSSatish Balay 
461b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
462b0a32e0cSBarry Smith   PetscDrawString(draw, .3*(xl+xr), .3*(yl+yr), PETSC_DRAW_BLACK, "symmetric");
46349b5e25fSSatish Balay 
46449b5e25fSSatish Balay   /* loop over matrix elements drawing boxes */
465b0a32e0cSBarry Smith   color = PETSC_DRAW_BLUE;
46649b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
46749b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
468d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
46949b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
47049b5e25fSSatish Balay       aa = a->a + j*bs2;
47149b5e25fSSatish Balay       for (k=0; k<bs; k++) {
47249b5e25fSSatish Balay         for (l=0; l<bs; l++) {
47349b5e25fSSatish Balay           if (PetscRealPart(*aa++) >=  0.) continue;
474b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
47549b5e25fSSatish Balay         }
47649b5e25fSSatish Balay       }
47749b5e25fSSatish Balay     }
47849b5e25fSSatish Balay   }
479b0a32e0cSBarry Smith   color = PETSC_DRAW_CYAN;
48049b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
48149b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
482d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
48349b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
48449b5e25fSSatish Balay       aa = a->a + j*bs2;
48549b5e25fSSatish Balay       for (k=0; k<bs; k++) {
48649b5e25fSSatish Balay         for (l=0; l<bs; l++) {
48749b5e25fSSatish Balay           if (PetscRealPart(*aa++) != 0.) continue;
488b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
48949b5e25fSSatish Balay         }
49049b5e25fSSatish Balay       }
49149b5e25fSSatish Balay     }
49249b5e25fSSatish Balay   }
49349b5e25fSSatish Balay 
494b0a32e0cSBarry Smith   color = PETSC_DRAW_RED;
49549b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
49649b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
497d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
49849b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
49949b5e25fSSatish Balay       aa = a->a + j*bs2;
50049b5e25fSSatish Balay       for (k=0; k<bs; k++) {
50149b5e25fSSatish Balay         for (l=0; l<bs; l++) {
50249b5e25fSSatish Balay           if (PetscRealPart(*aa++) <= 0.) continue;
503b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
50449b5e25fSSatish Balay         }
50549b5e25fSSatish Balay       }
50649b5e25fSSatish Balay     }
50749b5e25fSSatish Balay   }
50849b5e25fSSatish Balay   PetscFunctionReturn(0);
50949b5e25fSSatish Balay }
51049b5e25fSSatish Balay 
5114a2ae208SSatish Balay #undef __FUNCT__
5124a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_Draw"
5136849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_Draw(Mat A,PetscViewer viewer)
51449b5e25fSSatish Balay {
515dfbe8321SBarry Smith   PetscErrorCode ierr;
51649b5e25fSSatish Balay   PetscReal      xl,yl,xr,yr,w,h;
517b0a32e0cSBarry Smith   PetscDraw      draw;
518ace3abfcSBarry Smith   PetscBool      isnull;
51949b5e25fSSatish Balay 
52049b5e25fSSatish Balay   PetscFunctionBegin;
521b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
522b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
52349b5e25fSSatish Balay 
52449b5e25fSSatish Balay   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
525d0f46423SBarry Smith   xr  = A->rmap->N; yr = A->rmap->N; h = yr/10.0; w = xr/10.0;
52649b5e25fSSatish Balay   xr += w;    yr += h;  xl = -w;     yl = -h;
527b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
528b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqSBAIJ_Draw_Zoom,A);CHKERRQ(ierr);
52949b5e25fSSatish Balay   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
53049b5e25fSSatish Balay   PetscFunctionReturn(0);
53149b5e25fSSatish Balay }
53249b5e25fSSatish Balay 
5334a2ae208SSatish Balay #undef __FUNCT__
5344a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ"
535dfbe8321SBarry Smith PetscErrorCode MatView_SeqSBAIJ(Mat A,PetscViewer viewer)
53649b5e25fSSatish Balay {
537dfbe8321SBarry Smith   PetscErrorCode ierr;
538ace3abfcSBarry Smith   PetscBool      iascii,isdraw;
53908917f38SBarry Smith   FILE           *file = 0;
54049b5e25fSSatish Balay 
54149b5e25fSSatish Balay   PetscFunctionBegin;
5422692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
5432692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
54432077d6dSBarry Smith   if (iascii){
54549b5e25fSSatish Balay     ierr = MatView_SeqSBAIJ_ASCII(A,viewer);CHKERRQ(ierr);
54649b5e25fSSatish Balay   } else if (isdraw) {
54749b5e25fSSatish Balay     ierr = MatView_SeqSBAIJ_Draw(A,viewer);CHKERRQ(ierr);
54849b5e25fSSatish Balay   } else {
549a5e6ed63SBarry Smith     Mat B;
550ceb03754SKris Buschelman     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&B);CHKERRQ(ierr);
551a5e6ed63SBarry Smith     ierr = MatView(B,viewer);CHKERRQ(ierr);
5526bf464f9SBarry Smith     ierr = MatDestroy(&B);CHKERRQ(ierr);
55308917f38SBarry Smith     ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
55408917f38SBarry Smith     if (file) {
55508917f38SBarry Smith       fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
55608917f38SBarry Smith     }
55749b5e25fSSatish Balay   }
55849b5e25fSSatish Balay   PetscFunctionReturn(0);
55949b5e25fSSatish Balay }
56049b5e25fSSatish Balay 
56149b5e25fSSatish Balay 
5624a2ae208SSatish Balay #undef __FUNCT__
5634a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqSBAIJ"
56413f74950SBarry Smith PetscErrorCode MatGetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
56549b5e25fSSatish Balay {
566045c9aa0SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
56713f74950SBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
56813f74950SBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
569d0f46423SBarry Smith   PetscInt     brow,bcol,ridx,cidx,bs=A->rmap->bs,bs2=a->bs2;
57097e567efSBarry Smith   MatScalar    *ap,*aa = a->a;
57149b5e25fSSatish Balay 
57249b5e25fSSatish Balay   PetscFunctionBegin;
57349b5e25fSSatish Balay   for (k=0; k<m; k++) { /* loop over rows */
57449b5e25fSSatish Balay     row  = im[k]; brow = row/bs;
575e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
576e32f2f54SBarry 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);
57749b5e25fSSatish Balay     rp   = aj + ai[brow] ; ap = aa + bs2*ai[brow] ;
57849b5e25fSSatish Balay     nrow = ailen[brow];
57949b5e25fSSatish Balay     for (l=0; l<n; l++) { /* loop over columns */
580e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
581e32f2f54SBarry 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);
58249b5e25fSSatish Balay       col  = in[l] ;
58349b5e25fSSatish Balay       bcol = col/bs;
58449b5e25fSSatish Balay       cidx = col%bs;
58549b5e25fSSatish Balay       ridx = row%bs;
58649b5e25fSSatish Balay       high = nrow;
58749b5e25fSSatish Balay       low  = 0; /* assume unsorted */
58849b5e25fSSatish Balay       while (high-low > 5) {
58949b5e25fSSatish Balay         t = (low+high)/2;
59049b5e25fSSatish Balay         if (rp[t] > bcol) high = t;
59149b5e25fSSatish Balay         else             low  = t;
59249b5e25fSSatish Balay       }
59349b5e25fSSatish Balay       for (i=low; i<high; i++) {
59449b5e25fSSatish Balay         if (rp[i] > bcol) break;
59549b5e25fSSatish Balay         if (rp[i] == bcol) {
59649b5e25fSSatish Balay           *v++ = ap[bs2*i+bs*cidx+ridx];
59749b5e25fSSatish Balay           goto finished;
59849b5e25fSSatish Balay         }
59949b5e25fSSatish Balay       }
60097e567efSBarry Smith       *v++ = 0.0;
60149b5e25fSSatish Balay       finished:;
60249b5e25fSSatish Balay     }
60349b5e25fSSatish Balay   }
60449b5e25fSSatish Balay   PetscFunctionReturn(0);
60549b5e25fSSatish Balay }
60649b5e25fSSatish Balay 
60749b5e25fSSatish Balay 
6084a2ae208SSatish Balay #undef __FUNCT__
6094a2ae208SSatish Balay #define __FUNCT__ "MatSetValuesBlocked_SeqSBAIJ"
61013f74950SBarry Smith PetscErrorCode MatSetValuesBlocked_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
61149b5e25fSSatish Balay {
6120880e062SHong Zhang   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
6136849ba73SBarry Smith   PetscErrorCode    ierr;
614e2ee6c50SBarry Smith   PetscInt          *rp,k,low,high,t,ii,jj,row,nrow,i,col,l,rmax,N,lastcol = -1;
61513f74950SBarry Smith   PetscInt          *imax=a->imax,*ai=a->i,*ailen=a->ilen;
616d0f46423SBarry Smith   PetscInt          *aj=a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs,stepval;
617ace3abfcSBarry Smith   PetscBool         roworiented=a->roworiented;
618dd6ea824SBarry Smith   const PetscScalar *value = v;
619f15d580aSBarry Smith   MatScalar         *ap,*aa = a->a,*bap;
6200880e062SHong Zhang 
62149b5e25fSSatish Balay   PetscFunctionBegin;
6220880e062SHong Zhang   if (roworiented) {
6230880e062SHong Zhang     stepval = (n-1)*bs;
6240880e062SHong Zhang   } else {
6250880e062SHong Zhang     stepval = (m-1)*bs;
6260880e062SHong Zhang   }
6270880e062SHong Zhang   for (k=0; k<m; k++) { /* loop over added rows */
6280880e062SHong Zhang     row  = im[k];
6290880e062SHong Zhang     if (row < 0) continue;
6302515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
631e32f2f54SBarry Smith     if (row >= a->mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,a->mbs-1);
6320880e062SHong Zhang #endif
6330880e062SHong Zhang     rp   = aj + ai[row];
6340880e062SHong Zhang     ap   = aa + bs2*ai[row];
6350880e062SHong Zhang     rmax = imax[row];
6360880e062SHong Zhang     nrow = ailen[row];
6370880e062SHong Zhang     low  = 0;
638818f2c47SBarry Smith     high = nrow;
6390880e062SHong Zhang     for (l=0; l<n; l++) { /* loop over added columns */
6400880e062SHong Zhang       if (in[l] < 0) continue;
6410880e062SHong Zhang       col = in[l];
6422515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
643e32f2f54SBarry Smith       if (col >= a->nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",col,a->nbs-1);
644b1823623SSatish Balay #endif
645b98bf0e1SJed Brown       if (col < row) {
646b98bf0e1SJed Brown         if (a->ignore_ltriangular) {
647b98bf0e1SJed Brown           continue; /* ignore lower triangular block */
648b98bf0e1SJed Brown         } else {
649e32f2f54SBarry 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)");
650b98bf0e1SJed Brown         }
651b98bf0e1SJed Brown       }
6520880e062SHong Zhang       if (roworiented) {
6530880e062SHong Zhang         value = v + k*(stepval+bs)*bs + l*bs;
6540880e062SHong Zhang       } else {
6550880e062SHong Zhang         value = v + l*(stepval+bs)*bs + k*bs;
6560880e062SHong Zhang       }
6577cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
658e2ee6c50SBarry Smith       lastcol = col;
6590880e062SHong Zhang       while (high-low > 7) {
6600880e062SHong Zhang         t = (low+high)/2;
6610880e062SHong Zhang         if (rp[t] > col) high = t;
6620880e062SHong Zhang         else             low  = t;
6630880e062SHong Zhang       }
6640880e062SHong Zhang       for (i=low; i<high; i++) {
6650880e062SHong Zhang         if (rp[i] > col) break;
6660880e062SHong Zhang         if (rp[i] == col) {
6670880e062SHong Zhang           bap  = ap +  bs2*i;
6680880e062SHong Zhang           if (roworiented) {
6690880e062SHong Zhang             if (is == ADD_VALUES) {
6700880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6710880e062SHong Zhang                 for (jj=ii; jj<bs2; jj+=bs) {
6720880e062SHong Zhang                   bap[jj] += *value++;
6730880e062SHong Zhang                 }
6740880e062SHong Zhang               }
6750880e062SHong Zhang             } else {
6760880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6770880e062SHong Zhang                 for (jj=ii; jj<bs2; jj+=bs) {
6780880e062SHong Zhang                   bap[jj] = *value++;
6790880e062SHong Zhang                 }
6800880e062SHong Zhang                }
6810880e062SHong Zhang             }
6820880e062SHong Zhang           } else {
6830880e062SHong Zhang             if (is == ADD_VALUES) {
6840880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6850880e062SHong Zhang                 for (jj=0; jj<bs; jj++) {
6860880e062SHong Zhang                   *bap++ += *value++;
6870880e062SHong Zhang                 }
6880880e062SHong Zhang               }
6890880e062SHong Zhang             } else {
6900880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6910880e062SHong Zhang                 for (jj=0; jj<bs; jj++) {
6920880e062SHong Zhang                   *bap++  = *value++;
6930880e062SHong Zhang                 }
6940880e062SHong Zhang               }
6950880e062SHong Zhang             }
6960880e062SHong Zhang           }
6970880e062SHong Zhang           goto noinsert2;
6980880e062SHong Zhang         }
6990880e062SHong Zhang       }
7000880e062SHong Zhang       if (nonew == 1) goto noinsert2;
701e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
702fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
703c03d1d03SSatish Balay       N = nrow++ - 1; high++;
7040880e062SHong Zhang       /* shift up all the later entries in this row */
7050880e062SHong Zhang       for (ii=N; ii>=i; ii--) {
7060880e062SHong Zhang         rp[ii+1] = rp[ii];
7070880e062SHong Zhang         ierr = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
7080880e062SHong Zhang       }
7090880e062SHong Zhang       if (N >= i) {
7100880e062SHong Zhang         ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
7110880e062SHong Zhang       }
7120880e062SHong Zhang       rp[i] = col;
7130880e062SHong Zhang       bap   = ap +  bs2*i;
7140880e062SHong Zhang       if (roworiented) {
7150880e062SHong Zhang         for (ii=0; ii<bs; ii++,value+=stepval) {
7160880e062SHong Zhang           for (jj=ii; jj<bs2; jj+=bs) {
7170880e062SHong Zhang             bap[jj] = *value++;
7180880e062SHong Zhang           }
7190880e062SHong Zhang         }
7200880e062SHong Zhang       } else {
7210880e062SHong Zhang         for (ii=0; ii<bs; ii++,value+=stepval) {
7220880e062SHong Zhang           for (jj=0; jj<bs; jj++) {
7230880e062SHong Zhang             *bap++  = *value++;
7240880e062SHong Zhang           }
7250880e062SHong Zhang         }
7260880e062SHong Zhang        }
7270880e062SHong Zhang     noinsert2:;
7280880e062SHong Zhang       low = i;
7290880e062SHong Zhang     }
7300880e062SHong Zhang     ailen[row] = nrow;
7310880e062SHong Zhang   }
7320880e062SHong Zhang    PetscFunctionReturn(0);
73349b5e25fSSatish Balay }
73449b5e25fSSatish Balay 
73564831d72SBarry Smith /*
73664831d72SBarry Smith     This is not yet used
73764831d72SBarry Smith */
7384a2ae208SSatish Balay #undef __FUNCT__
7394108e4d5SBarry Smith #define __FUNCT__ "MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode"
7404108e4d5SBarry Smith PetscErrorCode MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode(Mat A)
7410def2e27SBarry Smith {
7420def2e27SBarry Smith   Mat_SeqSBAIJ    *a = (Mat_SeqSBAIJ*)A->data;
7430def2e27SBarry Smith   PetscErrorCode  ierr;
7440def2e27SBarry Smith   const PetscInt  *ai = a->i, *aj = a->j,*cols;
7450def2e27SBarry Smith   PetscInt        i = 0,j,blk_size,m = A->rmap->n,node_count = 0,nzx,nzy,*ns,row,nz,cnt,cnt2,*counts;
746ace3abfcSBarry Smith   PetscBool       flag;
7470def2e27SBarry Smith 
7480def2e27SBarry Smith   PetscFunctionBegin;
7490def2e27SBarry Smith   ierr = PetscMalloc(m*sizeof(PetscInt),&ns);CHKERRQ(ierr);
7500def2e27SBarry Smith   while (i < m){
7510def2e27SBarry Smith     nzx = ai[i+1] - ai[i];       /* Number of nonzeros */
7520def2e27SBarry Smith     /* Limits the number of elements in a node to 'a->inode.limit' */
7530def2e27SBarry Smith     for (j=i+1,blk_size=1; j<m && blk_size <a->inode.limit; ++j,++blk_size) {
7540def2e27SBarry Smith       nzy  = ai[j+1] - ai[j];
7550def2e27SBarry Smith       if (nzy != (nzx - j + i)) break;
7560def2e27SBarry Smith       ierr = PetscMemcmp(aj + ai[i] + j - i,aj + ai[j],nzy*sizeof(PetscInt),&flag);CHKERRQ(ierr);
7570def2e27SBarry Smith       if (!flag) break;
7580def2e27SBarry Smith     }
7590def2e27SBarry Smith     ns[node_count++] = blk_size;
7600def2e27SBarry Smith     i = j;
7610def2e27SBarry Smith   }
7620def2e27SBarry Smith   if (!a->inode.size && m && node_count > .9*m) {
7630def2e27SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
7640def2e27SBarry Smith     ierr = PetscInfo2(A,"Found %D nodes out of %D rows. Not using Inode routines\n",node_count,m);CHKERRQ(ierr);
7650def2e27SBarry Smith   } else {
7660def2e27SBarry Smith     a->inode.node_count = node_count;
7670def2e27SBarry Smith     ierr = PetscMalloc(node_count*sizeof(PetscInt),&a->inode.size);CHKERRQ(ierr);
768c760cd28SBarry Smith     ierr = PetscLogObjectMemory(A,node_count*sizeof(PetscInt));CHKERRQ(ierr);
7690def2e27SBarry Smith     ierr = PetscMemcpy(a->inode.size,ns,node_count*sizeof(PetscInt));
7700def2e27SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
7710def2e27SBarry Smith     ierr = PetscInfo3(A,"Found %D nodes of %D. Limit used: %D. Using Inode routines\n",node_count,m,a->inode.limit);CHKERRQ(ierr);
7720def2e27SBarry Smith 
7730def2e27SBarry Smith     /* count collections of adjacent columns in each inode */
7740def2e27SBarry Smith     row = 0;
7750def2e27SBarry Smith     cnt = 0;
7760def2e27SBarry Smith     for (i=0; i<node_count; i++) {
7770def2e27SBarry Smith       cols = aj + ai[row] + a->inode.size[i];
7780def2e27SBarry Smith       nz   = ai[row+1] - ai[row] - a->inode.size[i];
7790def2e27SBarry Smith       for (j=1; j<nz; j++) {
7800def2e27SBarry Smith         if (cols[j] != cols[j-1]+1) {
7810def2e27SBarry Smith           cnt++;
7820def2e27SBarry Smith         }
7830def2e27SBarry Smith       }
7840def2e27SBarry Smith       cnt++;
7850def2e27SBarry Smith       row += a->inode.size[i];
7860def2e27SBarry Smith     }
7870def2e27SBarry Smith     ierr = PetscMalloc(2*cnt*sizeof(PetscInt),&counts);CHKERRQ(ierr);
7880def2e27SBarry Smith     cnt = 0;
7890def2e27SBarry Smith     row = 0;
7900def2e27SBarry Smith     for (i=0; i<node_count; i++) {
7910def2e27SBarry Smith       cols          = aj + ai[row] + a->inode.size[i];
7920def2e27SBarry Smith 	  CHKMEMQ;
7930def2e27SBarry Smith       counts[2*cnt] = cols[0];
7940def2e27SBarry Smith 	  CHKMEMQ;
7950def2e27SBarry Smith       nz            = ai[row+1] - ai[row] - a->inode.size[i];
7960def2e27SBarry Smith       cnt2          = 1;
7970def2e27SBarry Smith       for (j=1; j<nz; j++) {
7980def2e27SBarry Smith         if (cols[j] != cols[j-1]+1) {
7990def2e27SBarry Smith 	  CHKMEMQ;
8000def2e27SBarry Smith           counts[2*(cnt++)+1] = cnt2;
8010def2e27SBarry Smith           counts[2*cnt]       = cols[j];
8020def2e27SBarry Smith 	  CHKMEMQ;
8030def2e27SBarry Smith           cnt2                = 1;
8040def2e27SBarry Smith         } else cnt2++;
8050def2e27SBarry Smith       }
8060def2e27SBarry Smith 	  CHKMEMQ;
8070def2e27SBarry Smith       counts[2*(cnt++)+1] = cnt2;
8080def2e27SBarry Smith 	  CHKMEMQ;
8090def2e27SBarry Smith       row += a->inode.size[i];
8100def2e27SBarry Smith     }
8110def2e27SBarry Smith     ierr = PetscIntView(2*cnt,counts,0);
8120def2e27SBarry Smith   }
81338702af4SBarry Smith   PetscFunctionReturn(0);
81438702af4SBarry Smith }
81538702af4SBarry Smith 
81638702af4SBarry Smith #undef __FUNCT__
8174a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqSBAIJ"
818dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqSBAIJ(Mat A,MatAssemblyType mode)
81949b5e25fSSatish Balay {
82049b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
8216849ba73SBarry Smith   PetscErrorCode ierr;
82213f74950SBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
823d0f46423SBarry Smith   PetscInt       m = A->rmap->N,*ip,N,*ailen = a->ilen;
82413f74950SBarry Smith   PetscInt       mbs = a->mbs,bs2 = a->bs2,rmax = 0;
82549b5e25fSSatish Balay   MatScalar      *aa = a->a,*ap;
82649b5e25fSSatish Balay 
82749b5e25fSSatish Balay   PetscFunctionBegin;
82849b5e25fSSatish Balay   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
82949b5e25fSSatish Balay 
83049b5e25fSSatish Balay   if (m) rmax = ailen[0];
83149b5e25fSSatish Balay   for (i=1; i<mbs; i++) {
83249b5e25fSSatish Balay     /* move each row back by the amount of empty slots (fshift) before it*/
83349b5e25fSSatish Balay     fshift += imax[i-1] - ailen[i-1];
83449b5e25fSSatish Balay      rmax   = PetscMax(rmax,ailen[i]);
83549b5e25fSSatish Balay      if (fshift) {
83649b5e25fSSatish Balay        ip = aj + ai[i]; ap = aa + bs2*ai[i];
83749b5e25fSSatish Balay        N = ailen[i];
83849b5e25fSSatish Balay        for (j=0; j<N; j++) {
83949b5e25fSSatish Balay          ip[j-fshift] = ip[j];
84049b5e25fSSatish Balay          ierr = PetscMemcpy(ap+(j-fshift)*bs2,ap+j*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
84149b5e25fSSatish Balay        }
84249b5e25fSSatish Balay      }
84349b5e25fSSatish Balay      ai[i] = ai[i-1] + ailen[i-1];
84449b5e25fSSatish Balay   }
84549b5e25fSSatish Balay   if (mbs) {
84649b5e25fSSatish Balay     fshift += imax[mbs-1] - ailen[mbs-1];
84749b5e25fSSatish Balay      ai[mbs] = ai[mbs-1] + ailen[mbs-1];
84849b5e25fSSatish Balay   }
84949b5e25fSSatish Balay   /* reset ilen and imax for each row */
85049b5e25fSSatish Balay   for (i=0; i<mbs; i++) {
85149b5e25fSSatish Balay     ailen[i] = imax[i] = ai[i+1] - ai[i];
85249b5e25fSSatish Balay   }
8536c6c5352SBarry Smith   a->nz = ai[mbs];
85449b5e25fSSatish Balay 
855b424e231SHong Zhang   /* diagonals may have moved, reset it */
856b424e231SHong Zhang   if (a->diag) {
8572ed38d0bSJed Brown     ierr = PetscMemcpy(a->diag,ai,mbs*sizeof(PetscInt));CHKERRQ(ierr);
85849b5e25fSSatish Balay   }
85928b2fa4aSMatthew Knepley   if (fshift && a->nounused == -1) {
860e32f2f54SBarry 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);
86128b2fa4aSMatthew Knepley   }
862d0f46423SBarry 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);
863ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues is %D\n",a->reallocs);CHKERRQ(ierr);
864ae15b995SBarry Smith   ierr = PetscInfo1(A,"Most nonzeros blocks in any row is %D\n",rmax);CHKERRQ(ierr);
8658e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
86649b5e25fSSatish Balay   a->reallocs          = 0;
86749b5e25fSSatish Balay   A->info.nz_unneeded  = (PetscReal)fshift*bs2;
868061b2667SBarry Smith   a->idiagvalid = PETSC_FALSE;
86938702af4SBarry Smith 
87038702af4SBarry Smith   if (A->cmap->n < 65536 && A->cmap->bs == 1) {
87138702af4SBarry Smith     if (!a->jshort) {
87238702af4SBarry Smith       ierr = PetscMalloc(a->i[A->rmap->n]*sizeof(unsigned short),&a->jshort);CHKERRQ(ierr);
873c760cd28SBarry Smith       ierr = PetscLogObjectMemory(A,a->i[A->rmap->n]*sizeof(unsigned short));CHKERRQ(ierr);
87438702af4SBarry Smith       for (i=0; i<a->i[A->rmap->n]; i++) a->jshort[i] = a->j[i];
87538702af4SBarry Smith       A->ops->mult  = MatMult_SeqSBAIJ_1_ushort;
87641f059aeSBarry Smith       A->ops->sor = MatSOR_SeqSBAIJ_ushort;
8774da8f245SBarry Smith       a->free_jshort = PETSC_TRUE;
87838702af4SBarry Smith     }
87938702af4SBarry Smith   }
88049b5e25fSSatish Balay   PetscFunctionReturn(0);
88149b5e25fSSatish Balay }
88249b5e25fSSatish Balay 
88349b5e25fSSatish Balay /*
88449b5e25fSSatish Balay    This function returns an array of flags which indicate the locations of contiguous
88549b5e25fSSatish Balay    blocks that should be zeroed. for eg: if bs = 3  and is = [0,1,2,3,5,6,7,8,9]
88649b5e25fSSatish Balay    then the resulting sizes = [3,1,1,3,1] correspondig to sets [(0,1,2),(3),(5),(6,7,8),(9)]
88749b5e25fSSatish Balay    Assume: sizes should be long enough to hold all the values.
88849b5e25fSSatish Balay */
8894a2ae208SSatish Balay #undef __FUNCT__
8904a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqSBAIJ_Check_Blocks"
89113f74950SBarry Smith PetscErrorCode MatZeroRows_SeqSBAIJ_Check_Blocks(PetscInt idx[],PetscInt n,PetscInt bs,PetscInt sizes[], PetscInt *bs_max)
89249b5e25fSSatish Balay {
89313f74950SBarry Smith   PetscInt   i,j,k,row;
894ace3abfcSBarry Smith   PetscBool  flg;
89549b5e25fSSatish Balay 
89649b5e25fSSatish Balay   PetscFunctionBegin;
89749b5e25fSSatish Balay    for (i=0,j=0; i<n; j++) {
89849b5e25fSSatish Balay      row = idx[i];
89949b5e25fSSatish Balay      if (row%bs!=0) { /* Not the begining of a block */
90049b5e25fSSatish Balay        sizes[j] = 1;
90149b5e25fSSatish Balay        i++;
90249b5e25fSSatish Balay      } else if (i+bs > n) { /* Beginning of a block, but complete block doesn't exist (at idx end) */
90349b5e25fSSatish Balay        sizes[j] = 1;         /* Also makes sure atleast 'bs' values exist for next else */
90449b5e25fSSatish Balay        i++;
90549b5e25fSSatish Balay      } else { /* Begining of the block, so check if the complete block exists */
90649b5e25fSSatish Balay        flg = PETSC_TRUE;
90749b5e25fSSatish Balay        for (k=1; k<bs; k++) {
90849b5e25fSSatish Balay          if (row+k != idx[i+k]) { /* break in the block */
90949b5e25fSSatish Balay            flg = PETSC_FALSE;
91049b5e25fSSatish Balay            break;
91149b5e25fSSatish Balay          }
91249b5e25fSSatish Balay        }
913abc0a331SBarry Smith        if (flg) { /* No break in the bs */
91449b5e25fSSatish Balay          sizes[j] = bs;
91549b5e25fSSatish Balay          i+= bs;
91649b5e25fSSatish Balay        } else {
91749b5e25fSSatish Balay          sizes[j] = 1;
91849b5e25fSSatish Balay          i++;
91949b5e25fSSatish Balay        }
92049b5e25fSSatish Balay      }
92149b5e25fSSatish Balay    }
92249b5e25fSSatish Balay    *bs_max = j;
92349b5e25fSSatish Balay    PetscFunctionReturn(0);
92449b5e25fSSatish Balay }
92549b5e25fSSatish Balay 
92649b5e25fSSatish Balay 
92749b5e25fSSatish Balay /* Only add/insert a(i,j) with i<=j (blocks).
92849b5e25fSSatish Balay    Any a(i,j) with i>j input by user is ingored.
92949b5e25fSSatish Balay */
93049b5e25fSSatish Balay 
9314a2ae208SSatish Balay #undef __FUNCT__
9324a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqSBAIJ"
93313f74950SBarry Smith PetscErrorCode MatSetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
93449b5e25fSSatish Balay {
93549b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
9366849ba73SBarry Smith   PetscErrorCode ierr;
937e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,lastcol = -1;
93813f74950SBarry Smith   PetscInt       *imax=a->imax,*ai=a->i,*ailen=a->ilen,roworiented=a->roworiented;
939d0f46423SBarry Smith   PetscInt       *aj=a->j,nonew=a->nonew,bs=A->rmap->bs,brow,bcol;
94013f74950SBarry Smith   PetscInt       ridx,cidx,bs2=a->bs2;
94149b5e25fSSatish Balay   MatScalar      *ap,value,*aa=a->a,*bap;
94249b5e25fSSatish Balay 
94349b5e25fSSatish Balay   PetscFunctionBegin;
94471fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
94549b5e25fSSatish Balay   for (k=0; k<m; k++) { /* loop over added rows */
94649b5e25fSSatish Balay     row  = im[k];       /* row number */
94749b5e25fSSatish Balay     brow = row/bs;      /* block row number */
94849b5e25fSSatish Balay     if (row < 0) continue;
9492515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
950e32f2f54SBarry 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);
95149b5e25fSSatish Balay #endif
95249b5e25fSSatish Balay     rp   = aj + ai[brow]; /*ptr to beginning of column value of the row block*/
95349b5e25fSSatish Balay     ap   = aa + bs2*ai[brow]; /*ptr to beginning of element value of the row block*/
95449b5e25fSSatish Balay     rmax = imax[brow];  /* maximum space allocated for this row */
95549b5e25fSSatish Balay     nrow = ailen[brow]; /* actual length of this row */
95649b5e25fSSatish Balay     low  = 0;
95749b5e25fSSatish Balay 
95849b5e25fSSatish Balay     for (l=0; l<n; l++) { /* loop over added columns */
95949b5e25fSSatish Balay       if (in[l] < 0) continue;
9602515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
961e32f2f54SBarry 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);
96249b5e25fSSatish Balay #endif
96349b5e25fSSatish Balay       col = in[l];
96449b5e25fSSatish Balay       bcol = col/bs;              /* block col number */
96549b5e25fSSatish Balay 
966941593c8SHong Zhang       if (brow > bcol) {
967941593c8SHong Zhang         if (a->ignore_ltriangular){
968941593c8SHong Zhang           continue; /* ignore lower triangular values */
969941593c8SHong Zhang         } else {
970e32f2f54SBarry 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)");
971941593c8SHong Zhang         }
972941593c8SHong Zhang       }
973f4989cb3SHong Zhang 
97449b5e25fSSatish Balay       ridx = row % bs; cidx = col % bs; /*row and col index inside the block */
9758549e402SHong Zhang       if ((brow==bcol && ridx<=cidx) || (brow<bcol)){
97649b5e25fSSatish Balay         /* element value a(k,l) */
97749b5e25fSSatish Balay         if (roworiented) {
97849b5e25fSSatish Balay           value = v[l + k*n];
97949b5e25fSSatish Balay         } else {
98049b5e25fSSatish Balay           value = v[k + l*m];
98149b5e25fSSatish Balay         }
98249b5e25fSSatish Balay 
98349b5e25fSSatish Balay         /* move pointer bap to a(k,l) quickly and add/insert value */
9847cd84e04SBarry Smith         if (col <= lastcol) low = 0; high = nrow;
985e2ee6c50SBarry Smith         lastcol = col;
98649b5e25fSSatish Balay         while (high-low > 7) {
98749b5e25fSSatish Balay           t = (low+high)/2;
98849b5e25fSSatish Balay           if (rp[t] > bcol) high = t;
98949b5e25fSSatish Balay           else              low  = t;
99049b5e25fSSatish Balay         }
99149b5e25fSSatish Balay         for (i=low; i<high; i++) {
99249b5e25fSSatish Balay           if (rp[i] > bcol) break;
99349b5e25fSSatish Balay           if (rp[i] == bcol) {
99449b5e25fSSatish Balay             bap  = ap +  bs2*i + bs*cidx + ridx;
99549b5e25fSSatish Balay             if (is == ADD_VALUES) *bap += value;
99649b5e25fSSatish Balay             else                  *bap  = value;
9978549e402SHong Zhang             /* for diag block, add/insert its symmetric element a(cidx,ridx) */
9988549e402SHong Zhang             if (brow == bcol && ridx < cidx){
9998549e402SHong Zhang               bap  = ap +  bs2*i + bs*ridx + cidx;
10008549e402SHong Zhang               if (is == ADD_VALUES) *bap += value;
10018549e402SHong Zhang               else                  *bap  = value;
10028549e402SHong Zhang             }
100349b5e25fSSatish Balay             goto noinsert1;
100449b5e25fSSatish Balay           }
100549b5e25fSSatish Balay         }
100649b5e25fSSatish Balay 
100749b5e25fSSatish Balay         if (nonew == 1) goto noinsert1;
1008e32f2f54SBarry Smith         if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
1009fef13f97SBarry Smith         MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
101049b5e25fSSatish Balay 
1011c03d1d03SSatish Balay         N = nrow++ - 1; high++;
101249b5e25fSSatish Balay         /* shift up all the later entries in this row */
101349b5e25fSSatish Balay         for (ii=N; ii>=i; ii--) {
101449b5e25fSSatish Balay           rp[ii+1] = rp[ii];
101549b5e25fSSatish Balay           ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
101649b5e25fSSatish Balay         }
101749b5e25fSSatish Balay         if (N>=i) {
101849b5e25fSSatish Balay           ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
101949b5e25fSSatish Balay         }
102049b5e25fSSatish Balay         rp[i]                      = bcol;
102149b5e25fSSatish Balay         ap[bs2*i + bs*cidx + ridx] = value;
102249b5e25fSSatish Balay       noinsert1:;
102349b5e25fSSatish Balay         low = i;
10248549e402SHong Zhang       }
102549b5e25fSSatish Balay     }   /* end of loop over added columns */
102649b5e25fSSatish Balay     ailen[brow] = nrow;
102749b5e25fSSatish Balay   }   /* end of loop over added rows */
102849b5e25fSSatish Balay   PetscFunctionReturn(0);
102949b5e25fSSatish Balay }
103049b5e25fSSatish Balay 
10314a2ae208SSatish Balay #undef __FUNCT__
10324d101231SSatish Balay #define __FUNCT__ "MatICCFactor_SeqSBAIJ"
10330481f469SBarry Smith PetscErrorCode MatICCFactor_SeqSBAIJ(Mat inA,IS row,const MatFactorInfo *info)
103449b5e25fSSatish Balay {
10354ccecd49SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)inA->data;
103649b5e25fSSatish Balay   Mat            outA;
1037dfbe8321SBarry Smith   PetscErrorCode ierr;
1038ace3abfcSBarry Smith   PetscBool      row_identity;
103949b5e25fSSatish Balay 
104049b5e25fSSatish Balay   PetscFunctionBegin;
1041e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 is supported for in-place icc");
1042c84f5b01SHong Zhang   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1043e32f2f54SBarry Smith   if (!row_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix reordering is not supported");
1044e32f2f54SBarry 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()! */
1045c84f5b01SHong Zhang 
104649b5e25fSSatish Balay   outA            = inA;
1047d5f3da31SBarry Smith   inA->factortype = MAT_FACTOR_ICC;
104849b5e25fSSatish Balay 
10491a3463dfSHong Zhang   ierr = MatMarkDiagonal_SeqSBAIJ(inA);CHKERRQ(ierr);
1050d595f711SHong Zhang   ierr = MatSeqSBAIJSetNumericFactorization_inplace(inA,row_identity);CHKERRQ(ierr);
105149b5e25fSSatish Balay 
1052c3122656SLisandro Dalcin   ierr   = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
10536bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
1054c84f5b01SHong Zhang   a->row = row;
1055c3122656SLisandro Dalcin   ierr   = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
10566bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
1057c84f5b01SHong Zhang   a->col = row;
1058c84f5b01SHong Zhang 
1059c84f5b01SHong Zhang   /* Create the invert permutation so that it can be used in MatCholeskyFactorNumeric() */
1060c84f5b01SHong Zhang   if (a->icol) {ierr = ISInvertPermutation(row,PETSC_DECIDE, &a->icol);CHKERRQ(ierr);}
106152e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
106249b5e25fSSatish Balay 
106349b5e25fSSatish Balay   if (!a->solve_work) {
1064d0f46423SBarry Smith     ierr = PetscMalloc((inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
1065d0f46423SBarry Smith     ierr = PetscLogObjectMemory(inA,(inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar));CHKERRQ(ierr);
106649b5e25fSSatish Balay   }
106749b5e25fSSatish Balay 
1068719d5645SBarry Smith   ierr = MatCholeskyFactorNumeric(outA,inA,info);CHKERRQ(ierr);
106949b5e25fSSatish Balay   PetscFunctionReturn(0);
107049b5e25fSSatish Balay }
1071950f1e5bSHong Zhang 
107249b5e25fSSatish Balay EXTERN_C_BEGIN
10734a2ae208SSatish Balay #undef __FUNCT__
10744a2ae208SSatish Balay #define __FUNCT__ "MatSeqSBAIJSetColumnIndices_SeqSBAIJ"
10757087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetColumnIndices_SeqSBAIJ(Mat mat,PetscInt *indices)
107649b5e25fSSatish Balay {
1077045c9aa0SHong Zhang   Mat_SeqSBAIJ *baij = (Mat_SeqSBAIJ *)mat->data;
107813f74950SBarry Smith   PetscInt     i,nz,n;
107949b5e25fSSatish Balay 
108049b5e25fSSatish Balay   PetscFunctionBegin;
10816c6c5352SBarry Smith   nz = baij->maxnz;
1082d0f46423SBarry Smith   n  = mat->cmap->n;
108349b5e25fSSatish Balay   for (i=0; i<nz; i++) {
108449b5e25fSSatish Balay     baij->j[i] = indices[i];
108549b5e25fSSatish Balay   }
10866c6c5352SBarry Smith    baij->nz = nz;
108749b5e25fSSatish Balay    for (i=0; i<n; i++) {
108849b5e25fSSatish Balay      baij->ilen[i] = baij->imax[i];
108949b5e25fSSatish Balay    }
109049b5e25fSSatish Balay    PetscFunctionReturn(0);
109149b5e25fSSatish Balay }
109249b5e25fSSatish Balay EXTERN_C_END
109349b5e25fSSatish Balay 
10944a2ae208SSatish Balay #undef __FUNCT__
10954a2ae208SSatish Balay #define __FUNCT__ "MatSeqSBAIJSetColumnIndices"
109649b5e25fSSatish Balay /*@
109719585528SSatish Balay   MatSeqSBAIJSetColumnIndices - Set the column indices for all the rows
109849b5e25fSSatish Balay   in the matrix.
109949b5e25fSSatish Balay 
110049b5e25fSSatish Balay   Input Parameters:
110119585528SSatish Balay   +  mat     - the SeqSBAIJ matrix
110249b5e25fSSatish Balay   -  indices - the column indices
110349b5e25fSSatish Balay 
110449b5e25fSSatish Balay   Level: advanced
110549b5e25fSSatish Balay 
110649b5e25fSSatish Balay   Notes:
110749b5e25fSSatish Balay   This can be called if you have precomputed the nonzero structure of the
110849b5e25fSSatish Balay   matrix and want to provide it to the matrix object to improve the performance
110949b5e25fSSatish Balay   of the MatSetValues() operation.
111049b5e25fSSatish Balay 
111149b5e25fSSatish Balay   You MUST have set the correct numbers of nonzeros per row in the call to
1112d1be2dadSMatthew Knepley   MatCreateSeqSBAIJ(), and the columns indices MUST be sorted.
111349b5e25fSSatish Balay 
1114ab9f2c04SSatish Balay   MUST be called before any calls to MatSetValues()
111549b5e25fSSatish Balay 
1116ab9f2c04SSatish Balay   .seealso: MatCreateSeqSBAIJ
111749b5e25fSSatish Balay @*/
11187087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetColumnIndices(Mat mat,PetscInt *indices)
111949b5e25fSSatish Balay {
11204ac538c5SBarry Smith   PetscErrorCode ierr;
112149b5e25fSSatish Balay 
112249b5e25fSSatish Balay   PetscFunctionBegin;
11230700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
11244482741eSBarry Smith   PetscValidPointer(indices,2);
11254ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqSBAIJSetColumnIndices_C",(Mat,PetscInt *),(mat,indices));CHKERRQ(ierr);
112649b5e25fSSatish Balay   PetscFunctionReturn(0);
112749b5e25fSSatish Balay }
112849b5e25fSSatish Balay 
11294a2ae208SSatish Balay #undef __FUNCT__
11303c896bc6SHong Zhang #define __FUNCT__ "MatCopy_SeqSBAIJ"
11313c896bc6SHong Zhang PetscErrorCode MatCopy_SeqSBAIJ(Mat A,Mat B,MatStructure str)
11323c896bc6SHong Zhang {
11333c896bc6SHong Zhang   PetscErrorCode ierr;
11343c896bc6SHong Zhang 
11353c896bc6SHong Zhang   PetscFunctionBegin;
11363c896bc6SHong Zhang   /* If the two matrices have the same copy implementation, use fast copy. */
11373c896bc6SHong Zhang   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
11383c896bc6SHong Zhang     Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
11393c896bc6SHong Zhang     Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ*)B->data;
11403c896bc6SHong Zhang 
1141e7e72b3dSBarry 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");
1142d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->N])*sizeof(PetscScalar));CHKERRQ(ierr);
11433c896bc6SHong Zhang   } else {
1144f5edf698SHong Zhang     ierr = MatGetRowUpperTriangular(A);CHKERRQ(ierr);
11453c896bc6SHong Zhang     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1146f5edf698SHong Zhang     ierr = MatRestoreRowUpperTriangular(A);CHKERRQ(ierr);
11473c896bc6SHong Zhang   }
11483c896bc6SHong Zhang   PetscFunctionReturn(0);
11493c896bc6SHong Zhang }
11503c896bc6SHong Zhang 
11513c896bc6SHong Zhang #undef __FUNCT__
11524a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqSBAIJ"
1153dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqSBAIJ(Mat A)
1154273d9f13SBarry Smith {
1155dfbe8321SBarry Smith   PetscErrorCode ierr;
1156273d9f13SBarry Smith 
1157273d9f13SBarry Smith   PetscFunctionBegin;
1158db4efbfdSBarry Smith   ierr =  MatSeqSBAIJSetPreallocation_SeqSBAIJ(A,-PetscMax(A->rmap->bs,1),PETSC_DEFAULT,0);CHKERRQ(ierr);
1159273d9f13SBarry Smith   PetscFunctionReturn(0);
1160273d9f13SBarry Smith }
1161273d9f13SBarry Smith 
1162a6ece127SHong Zhang #undef __FUNCT__
1163a6ece127SHong Zhang #define __FUNCT__ "MatGetArray_SeqSBAIJ"
1164dfbe8321SBarry Smith PetscErrorCode MatGetArray_SeqSBAIJ(Mat A,PetscScalar *array[])
1165a6ece127SHong Zhang {
1166a6ece127SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
1167a6ece127SHong Zhang   PetscFunctionBegin;
1168a6ece127SHong Zhang   *array = a->a;
1169a6ece127SHong Zhang   PetscFunctionReturn(0);
1170a6ece127SHong Zhang }
1171a6ece127SHong Zhang 
1172a6ece127SHong Zhang #undef __FUNCT__
1173a6ece127SHong Zhang #define __FUNCT__ "MatRestoreArray_SeqSBAIJ"
1174dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqSBAIJ(Mat A,PetscScalar *array[])
1175a6ece127SHong Zhang {
1176a6ece127SHong Zhang   PetscFunctionBegin;
1177a6ece127SHong Zhang   PetscFunctionReturn(0);
1178a6ece127SHong Zhang  }
1179a6ece127SHong Zhang 
118042ee4b1aSHong Zhang #undef __FUNCT__
118142ee4b1aSHong Zhang #define __FUNCT__ "MatAXPY_SeqSBAIJ"
1182f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqSBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
118342ee4b1aSHong Zhang {
118442ee4b1aSHong Zhang   Mat_SeqSBAIJ   *x=(Mat_SeqSBAIJ *)X->data, *y=(Mat_SeqSBAIJ *)Y->data;
1185dfbe8321SBarry Smith   PetscErrorCode ierr;
1186e838b9e7SJed Brown   PetscInt       i,bs=Y->rmap->bs,bs2=bs*bs,j;
1187e838b9e7SJed Brown   PetscBLASInt   one = 1;
118842ee4b1aSHong Zhang 
118942ee4b1aSHong Zhang   PetscFunctionBegin;
119042ee4b1aSHong Zhang   if (str == SAME_NONZERO_PATTERN) {
1191f4df32b1SMatthew Knepley     PetscScalar alpha = a;
1192*666a761fSJed Brown     PetscBLASInt bnz = PetscBLASIntCast(x->nz*bs2);
1193f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
1194c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
1195c4319e64SHong Zhang     if (y->xtoy && y->XtoY != X) {
1196c4319e64SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
11976bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
1198c537a176SHong Zhang     }
1199c4319e64SHong Zhang     if (!y->xtoy) { /* get xtoy */
1200c4319e64SHong Zhang       ierr = MatAXPYGetxtoy_Private(x->mbs,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
1201c4319e64SHong Zhang       y->XtoY = X;
1202c537a176SHong Zhang     }
12036c6c5352SBarry Smith     for (i=0; i<x->nz; i++) {
1204c4319e64SHong Zhang       j = 0;
1205c4319e64SHong Zhang       while (j < bs2){
1206f4df32b1SMatthew Knepley         y->a[bs2*y->xtoy[i]+j] += a*(x->a[bs2*i+j]);
1207c4319e64SHong Zhang         j++;
1208c537a176SHong Zhang       }
1209c4319e64SHong Zhang     }
12101e2582c4SBarry 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);
121142ee4b1aSHong Zhang   } else {
1212f5edf698SHong Zhang     ierr = MatGetRowUpperTriangular(X);CHKERRQ(ierr);
1213f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
1214f5edf698SHong Zhang     ierr = MatRestoreRowUpperTriangular(X);CHKERRQ(ierr);
121542ee4b1aSHong Zhang   }
121642ee4b1aSHong Zhang   PetscFunctionReturn(0);
121742ee4b1aSHong Zhang }
121842ee4b1aSHong Zhang 
1219efcf0fc3SBarry Smith #undef __FUNCT__
12206363de48SJed Brown #define __FUNCT__ "MatSetBlockSize_SeqSBAIJ"
12216363de48SJed Brown PetscErrorCode MatSetBlockSize_SeqSBAIJ(Mat A,PetscInt bs)
12226363de48SJed Brown {
12236363de48SJed Brown   PetscInt rbs,cbs;
12246363de48SJed Brown   PetscErrorCode ierr;
12256363de48SJed Brown 
12266363de48SJed Brown   PetscFunctionBegin;
12276363de48SJed Brown   ierr = PetscLayoutGetBlockSize(A->rmap,&rbs);CHKERRQ(ierr);
12286363de48SJed Brown   ierr = PetscLayoutGetBlockSize(A->cmap,&cbs);CHKERRQ(ierr);
1229e32f2f54SBarry Smith   if (rbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with SBAIJ %d",bs,rbs);
1230e32f2f54SBarry Smith   if (cbs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Attempt to set block size %d with SBAIJ %d",bs,cbs);
12316363de48SJed Brown   PetscFunctionReturn(0);
12326363de48SJed Brown }
12336363de48SJed Brown 
12346363de48SJed Brown #undef __FUNCT__
1235efcf0fc3SBarry Smith #define __FUNCT__ "MatIsSymmetric_SeqSBAIJ"
1236ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqSBAIJ(Mat A,PetscReal tol,PetscBool  *flg)
1237efcf0fc3SBarry Smith {
1238efcf0fc3SBarry Smith   PetscFunctionBegin;
1239efcf0fc3SBarry Smith   *flg = PETSC_TRUE;
1240efcf0fc3SBarry Smith   PetscFunctionReturn(0);
1241efcf0fc3SBarry Smith }
1242efcf0fc3SBarry Smith 
1243efcf0fc3SBarry Smith #undef __FUNCT__
1244efcf0fc3SBarry Smith #define __FUNCT__ "MatIsStructurallySymmetric_SeqSBAIJ"
1245ace3abfcSBarry Smith PetscErrorCode MatIsStructurallySymmetric_SeqSBAIJ(Mat A,PetscBool  *flg)
1246efcf0fc3SBarry Smith {
1247efcf0fc3SBarry Smith    PetscFunctionBegin;
1248efcf0fc3SBarry Smith    *flg = PETSC_TRUE;
1249efcf0fc3SBarry Smith    PetscFunctionReturn(0);
1250efcf0fc3SBarry Smith }
1251efcf0fc3SBarry Smith 
1252efcf0fc3SBarry Smith #undef __FUNCT__
1253efcf0fc3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqSBAIJ"
1254ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqSBAIJ(Mat A,PetscReal tol,PetscBool  *flg)
1255efcf0fc3SBarry Smith  {
1256efcf0fc3SBarry Smith    PetscFunctionBegin;
1257efcf0fc3SBarry Smith    *flg = PETSC_FALSE;
1258efcf0fc3SBarry Smith    PetscFunctionReturn(0);
1259efcf0fc3SBarry Smith  }
1260efcf0fc3SBarry Smith 
126199cafbc1SBarry Smith #undef __FUNCT__
126299cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqSBAIJ"
126399cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqSBAIJ(Mat A)
126499cafbc1SBarry Smith {
126599cafbc1SBarry Smith   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
126699cafbc1SBarry Smith   PetscInt       i,nz = a->bs2*a->i[a->mbs];
1267dd6ea824SBarry Smith   MatScalar      *aa = a->a;
126899cafbc1SBarry Smith 
126999cafbc1SBarry Smith   PetscFunctionBegin;
127099cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
127199cafbc1SBarry Smith   PetscFunctionReturn(0);
127299cafbc1SBarry Smith }
127399cafbc1SBarry Smith 
127499cafbc1SBarry Smith #undef __FUNCT__
127599cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqSBAIJ"
127699cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqSBAIJ(Mat A)
127799cafbc1SBarry Smith {
127899cafbc1SBarry Smith   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
127999cafbc1SBarry Smith   PetscInt       i,nz = a->bs2*a->i[a->mbs];
1280dd6ea824SBarry Smith   MatScalar      *aa = a->a;
128199cafbc1SBarry Smith 
128299cafbc1SBarry Smith   PetscFunctionBegin;
128399cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
128499cafbc1SBarry Smith   PetscFunctionReturn(0);
128599cafbc1SBarry Smith }
128699cafbc1SBarry Smith 
12873bededecSBarry Smith #undef __FUNCT__
12883bededecSBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqSBAIJ"
12893bededecSBarry Smith PetscErrorCode MatZeroRowsColumns_SeqSBAIJ(Mat A,PetscInt is_n,const PetscInt is_idx[],PetscScalar diag,Vec x, Vec b)
12903bededecSBarry Smith {
12913bededecSBarry Smith   Mat_SeqSBAIJ      *baij=(Mat_SeqSBAIJ*)A->data;
12923bededecSBarry Smith   PetscErrorCode    ierr;
12933bededecSBarry Smith   PetscInt          i,j,k,count;
12943bededecSBarry Smith   PetscInt          bs=A->rmap->bs,bs2=baij->bs2,row,col;
12953bededecSBarry Smith   PetscScalar       zero = 0.0;
12963bededecSBarry Smith   MatScalar         *aa;
12973bededecSBarry Smith   const PetscScalar *xx;
12983bededecSBarry Smith   PetscScalar       *bb;
129956777dd2SBarry Smith   PetscBool         *zeroed,vecs = PETSC_FALSE;
13003bededecSBarry Smith 
13013bededecSBarry Smith   PetscFunctionBegin;
13023bededecSBarry Smith   /* fix right hand side if needed */
13033bededecSBarry Smith   if (x && b) {
13043bededecSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
13053bededecSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
130656777dd2SBarry Smith     vecs = PETSC_TRUE;
13073bededecSBarry Smith   }
13083bededecSBarry Smith   A->same_nonzero = PETSC_TRUE;
13093bededecSBarry Smith 
13103bededecSBarry Smith   /* zero the columns */
13113bededecSBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
13123bededecSBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
13133bededecSBarry Smith   for (i=0; i<is_n; i++) {
13143bededecSBarry 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]);
13153bededecSBarry Smith     zeroed[is_idx[i]] = PETSC_TRUE;
13163bededecSBarry Smith   }
131756777dd2SBarry Smith   if (vecs) {
131856777dd2SBarry Smith     for (i=0; i<A->rmap->N; i++) {
131956777dd2SBarry Smith       row = i/bs;
132056777dd2SBarry Smith       for (j=baij->i[row]; j<baij->i[row+1]; j++) {
132156777dd2SBarry Smith 	for (k=0; k<bs; k++) {
132256777dd2SBarry Smith 	  col = bs*baij->j[j] + k;
132356777dd2SBarry Smith           if (col <= i) continue;
132456777dd2SBarry Smith 	  aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
132556777dd2SBarry Smith 	  if (!zeroed[i] && zeroed[col]) {
132656777dd2SBarry Smith 	    bb[i] -= aa[0]*xx[col];
132756777dd2SBarry Smith 	  }
132856777dd2SBarry Smith 	  if (zeroed[i] && !zeroed[col]) {
132956777dd2SBarry Smith 	    bb[col] -= aa[0]*xx[i];
133056777dd2SBarry Smith 	  }
133156777dd2SBarry Smith 	}
133256777dd2SBarry Smith       }
133356777dd2SBarry Smith     }
133456777dd2SBarry Smith     for (i=0; i<is_n; i++) {
133556777dd2SBarry Smith       bb[is_idx[i]] = diag*xx[is_idx[i]];
133656777dd2SBarry Smith     }
133756777dd2SBarry Smith   }
133856777dd2SBarry Smith 
13393bededecSBarry Smith   for (i=0; i<A->rmap->N; i++) {
13403bededecSBarry Smith     if (!zeroed[i]) {
13413bededecSBarry Smith       row = i/bs;
13423bededecSBarry Smith       for (j=baij->i[row]; j<baij->i[row+1]; j++) {
13433bededecSBarry Smith         for (k=0; k<bs; k++) {
13443bededecSBarry Smith           col = bs*baij->j[j] + k;
13453bededecSBarry Smith 	  if (zeroed[col]) {
13463bededecSBarry Smith 	    aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
13473bededecSBarry Smith             aa[0] = 0.0;
13483bededecSBarry Smith           }
13493bededecSBarry Smith         }
13503bededecSBarry Smith       }
13513bededecSBarry Smith     }
13523bededecSBarry Smith   }
13533bededecSBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
135456777dd2SBarry Smith   if (vecs) {
135556777dd2SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
135656777dd2SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
135756777dd2SBarry Smith   }
13583bededecSBarry Smith 
13593bededecSBarry Smith   /* zero the rows */
13603bededecSBarry Smith   for (i=0; i<is_n; i++) {
13613bededecSBarry Smith     row   = is_idx[i];
13623bededecSBarry Smith     count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
13633bededecSBarry Smith     aa    = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
13643bededecSBarry Smith     for (k=0; k<count; k++) {
13653bededecSBarry Smith       aa[0] =  zero;
13663bededecSBarry Smith       aa    += bs;
13673bededecSBarry Smith     }
13683bededecSBarry Smith     if (diag != 0.0) {
13693bededecSBarry Smith       ierr = (*A->ops->setvalues)(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr);
13703bededecSBarry Smith     }
13713bededecSBarry Smith   }
13723bededecSBarry Smith   ierr = MatAssemblyEnd_SeqSBAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13733bededecSBarry Smith   PetscFunctionReturn(0);
13743bededecSBarry Smith }
13753bededecSBarry Smith 
137649b5e25fSSatish Balay /* -------------------------------------------------------------------*/
137749b5e25fSSatish Balay static struct _MatOps MatOps_Values = {MatSetValues_SeqSBAIJ,
137849b5e25fSSatish Balay        MatGetRow_SeqSBAIJ,
137949b5e25fSSatish Balay        MatRestoreRow_SeqSBAIJ,
138049b5e25fSSatish Balay        MatMult_SeqSBAIJ_N,
138197304618SKris Buschelman /* 4*/ MatMultAdd_SeqSBAIJ_N,
1382431c96f7SBarry Smith        MatMult_SeqSBAIJ_N,       /* transpose versions are same as non-transpose versions */
1383e005ede5SBarry Smith        MatMultAdd_SeqSBAIJ_N,
1384db4efbfdSBarry Smith        0,
138549b5e25fSSatish Balay        0,
138649b5e25fSSatish Balay        0,
138797304618SKris Buschelman /*10*/ 0,
138849b5e25fSSatish Balay        0,
1389c078aec8SLisandro Dalcin        MatCholeskyFactor_SeqSBAIJ,
139041f059aeSBarry Smith        MatSOR_SeqSBAIJ,
139149b5e25fSSatish Balay        MatTranspose_SeqSBAIJ,
139297304618SKris Buschelman /*15*/ MatGetInfo_SeqSBAIJ,
139349b5e25fSSatish Balay        MatEqual_SeqSBAIJ,
139449b5e25fSSatish Balay        MatGetDiagonal_SeqSBAIJ,
139549b5e25fSSatish Balay        MatDiagonalScale_SeqSBAIJ,
139649b5e25fSSatish Balay        MatNorm_SeqSBAIJ,
139797304618SKris Buschelman /*20*/ 0,
139849b5e25fSSatish Balay        MatAssemblyEnd_SeqSBAIJ,
139949b5e25fSSatish Balay        MatSetOption_SeqSBAIJ,
140049b5e25fSSatish Balay        MatZeroEntries_SeqSBAIJ,
1401d519adbfSMatthew Knepley /*24*/ 0,
140249b5e25fSSatish Balay        0,
140349b5e25fSSatish Balay        0,
1404db4efbfdSBarry Smith        0,
1405db4efbfdSBarry Smith        0,
1406d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqSBAIJ,
1407c464158bSHong Zhang        0,
1408db4efbfdSBarry Smith        0,
1409a6ece127SHong Zhang        MatGetArray_SeqSBAIJ,
1410a6ece127SHong Zhang        MatRestoreArray_SeqSBAIJ,
1411d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqSBAIJ,
1412719d5645SBarry Smith        0,
1413719d5645SBarry Smith        0,
141449b5e25fSSatish Balay        0,
1415c84f5b01SHong Zhang        MatICCFactor_SeqSBAIJ,
1416d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqSBAIJ,
141749b5e25fSSatish Balay        MatGetSubMatrices_SeqSBAIJ,
141849b5e25fSSatish Balay        MatIncreaseOverlap_SeqSBAIJ,
141949b5e25fSSatish Balay        MatGetValues_SeqSBAIJ,
14203c896bc6SHong Zhang        MatCopy_SeqSBAIJ,
1421d519adbfSMatthew Knepley /*44*/ 0,
142249b5e25fSSatish Balay        MatScale_SeqSBAIJ,
142349b5e25fSSatish Balay        0,
142449b5e25fSSatish Balay        0,
14253bededecSBarry Smith        MatZeroRowsColumns_SeqSBAIJ,
14266363de48SJed Brown /*49*/ MatSetBlockSize_SeqSBAIJ,
142749b5e25fSSatish Balay        MatGetRowIJ_SeqSBAIJ,
142849b5e25fSSatish Balay        MatRestoreRowIJ_SeqSBAIJ,
142949b5e25fSSatish Balay        0,
143049b5e25fSSatish Balay        0,
1431d519adbfSMatthew Knepley /*54*/ 0,
143249b5e25fSSatish Balay        0,
143349b5e25fSSatish Balay        0,
143449b5e25fSSatish Balay        0,
143549b5e25fSSatish Balay        MatSetValuesBlocked_SeqSBAIJ,
1436d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_SeqSBAIJ,
143749b5e25fSSatish Balay        0,
143849b5e25fSSatish Balay        0,
1439357abbc8SBarry Smith        0,
1440d959ec07SHong Zhang        0,
1441d519adbfSMatthew Knepley /*64*/ 0,
1442d959ec07SHong Zhang        0,
1443d959ec07SHong Zhang        0,
1444d959ec07SHong Zhang        0,
1445d959ec07SHong Zhang        0,
1446d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqSBAIJ,
14473e0d88b5SBarry Smith        0,
14483e0d88b5SBarry Smith        0,
14493e0d88b5SBarry Smith        0,
14503e0d88b5SBarry Smith        0,
1451d519adbfSMatthew Knepley /*74*/ 0,
14523e0d88b5SBarry Smith        0,
14533e0d88b5SBarry Smith        0,
14543e0d88b5SBarry Smith        0,
14553e0d88b5SBarry Smith        0,
1456d519adbfSMatthew Knepley /*79*/ 0,
14573e0d88b5SBarry Smith        0,
14583e0d88b5SBarry Smith        0,
145997304618SKris Buschelman        MatGetInertia_SeqSBAIJ,
14605bba2384SShri Abhyankar        MatLoad_SeqSBAIJ,
1461d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqSBAIJ,
1462865e5f61SKris Buschelman        MatIsHermitian_SeqSBAIJ,
1463efcf0fc3SBarry Smith        MatIsStructurallySymmetric_SeqSBAIJ,
1464865e5f61SKris Buschelman        0,
1465865e5f61SKris Buschelman        0,
1466d519adbfSMatthew Knepley /*89*/ 0,
1467865e5f61SKris Buschelman        0,
1468865e5f61SKris Buschelman        0,
1469865e5f61SKris Buschelman        0,
1470865e5f61SKris Buschelman        0,
1471d519adbfSMatthew Knepley /*94*/ 0,
1472865e5f61SKris Buschelman        0,
1473865e5f61SKris Buschelman        0,
147499cafbc1SBarry Smith        0,
147599cafbc1SBarry Smith        0,
1476d519adbfSMatthew Knepley /*99*/ 0,
147799cafbc1SBarry Smith        0,
147899cafbc1SBarry Smith        0,
147999cafbc1SBarry Smith        0,
148099cafbc1SBarry Smith        0,
1481d519adbfSMatthew Knepley /*104*/0,
148299cafbc1SBarry Smith        MatRealPart_SeqSBAIJ,
1483f5edf698SHong Zhang        MatImaginaryPart_SeqSBAIJ,
1484f5edf698SHong Zhang        MatGetRowUpperTriangular_SeqSBAIJ,
14852af78befSBarry Smith        MatRestoreRowUpperTriangular_SeqSBAIJ,
1486d519adbfSMatthew Knepley /*109*/0,
14872af78befSBarry Smith        0,
14882af78befSBarry Smith        0,
14892af78befSBarry Smith        0,
1490547795f9SHong Zhang        MatMissingDiagonal_SeqSBAIJ,
1491547795f9SHong Zhang /*114*/0,
1492547795f9SHong Zhang        0,
1493547795f9SHong Zhang        0,
1494547795f9SHong Zhang        0,
1495547795f9SHong Zhang        0,
1496547795f9SHong Zhang /*119*/0,
1497547795f9SHong Zhang        0,
14982f480046SShri Abhyankar        0,
14995bba2384SShri Abhyankar        0
150099cafbc1SBarry Smith };
1501be1d678aSKris Buschelman 
150249b5e25fSSatish Balay EXTERN_C_BEGIN
15034a2ae208SSatish Balay #undef __FUNCT__
15044a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqSBAIJ"
15057087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqSBAIJ(Mat mat)
150649b5e25fSSatish Balay {
15074afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ *)mat->data;
1508d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
1509dfbe8321SBarry Smith   PetscErrorCode ierr;
151049b5e25fSSatish Balay 
151149b5e25fSSatish Balay   PetscFunctionBegin;
1512e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
151349b5e25fSSatish Balay 
151449b5e25fSSatish Balay   /* allocate space for values if not already there */
151549b5e25fSSatish Balay   if (!aij->saved_values) {
151687828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
151749b5e25fSSatish Balay   }
151849b5e25fSSatish Balay 
151949b5e25fSSatish Balay   /* copy values over */
152087828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
152149b5e25fSSatish Balay   PetscFunctionReturn(0);
152249b5e25fSSatish Balay }
152349b5e25fSSatish Balay EXTERN_C_END
152449b5e25fSSatish Balay 
152549b5e25fSSatish Balay EXTERN_C_BEGIN
15264a2ae208SSatish Balay #undef __FUNCT__
15274a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqSBAIJ"
15287087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqSBAIJ(Mat mat)
152949b5e25fSSatish Balay {
15304afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ *)mat->data;
15316849ba73SBarry Smith   PetscErrorCode ierr;
1532d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
153349b5e25fSSatish Balay 
153449b5e25fSSatish Balay   PetscFunctionBegin;
1535e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
1536e7e72b3dSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
153749b5e25fSSatish Balay 
153849b5e25fSSatish Balay   /* copy values over */
153987828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
154049b5e25fSSatish Balay   PetscFunctionReturn(0);
154149b5e25fSSatish Balay }
154249b5e25fSSatish Balay EXTERN_C_END
154349b5e25fSSatish Balay 
15448549e402SHong Zhang EXTERN_C_BEGIN
15454a2ae208SSatish Balay #undef __FUNCT__
1546a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation_SeqSBAIJ"
15477087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetPreallocation_SeqSBAIJ(Mat B,PetscInt bs,PetscInt nz,PetscInt *nnz)
154849b5e25fSSatish Balay {
1549c464158bSHong Zhang   Mat_SeqSBAIJ   *b = (Mat_SeqSBAIJ*)B->data;
15506849ba73SBarry Smith   PetscErrorCode ierr;
1551db4efbfdSBarry Smith   PetscInt       i,mbs,bs2, newbs = PetscAbs(bs);
1552ace3abfcSBarry Smith   PetscBool      skipallocation = PETSC_FALSE,flg = PETSC_FALSE;
155349b5e25fSSatish Balay 
155449b5e25fSSatish Balay   PetscFunctionBegin;
1555273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
1556db4efbfdSBarry Smith   if (bs < 0) {
1557db4efbfdSBarry Smith     ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Options for MPISBAIJ matrix","Mat");CHKERRQ(ierr);
1558db4efbfdSBarry Smith       ierr = PetscOptionsInt("-mat_block_size","Set the blocksize used to store the matrix","MatSeqSBAIJSetPreallocation",newbs,&newbs,PETSC_NULL);CHKERRQ(ierr);
1559db4efbfdSBarry Smith     ierr = PetscOptionsEnd();CHKERRQ(ierr);
1560db4efbfdSBarry Smith     bs   = PetscAbs(bs);
1561db4efbfdSBarry Smith   }
1562e7e72b3dSBarry Smith   if (nnz && newbs != bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot change blocksize from command line if setting nnz");
1563db4efbfdSBarry Smith   bs = newbs;
1564db4efbfdSBarry Smith 
156526283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
156626283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
156726283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
156826283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
1569899cda47SBarry Smith 
1570d0f46423SBarry Smith   mbs  = B->rmap->N/bs;
157149b5e25fSSatish Balay   bs2  = bs*bs;
157249b5e25fSSatish Balay 
1573e7e72b3dSBarry Smith   if (mbs*bs != B->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number rows, cols must be divisible by blocksize");
157449b5e25fSSatish Balay 
1575ab93d7beSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
1576ab93d7beSBarry Smith     skipallocation = PETSC_TRUE;
1577ab93d7beSBarry Smith     nz             = 0;
1578ab93d7beSBarry Smith   }
1579ab93d7beSBarry Smith 
1580435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 3;
1581e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
158249b5e25fSSatish Balay   if (nnz) {
158349b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
1584e32f2f54SBarry 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]);
1585e32f2f54SBarry 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);
158649b5e25fSSatish Balay     }
158749b5e25fSSatish Balay   }
158849b5e25fSSatish Balay 
1589db4efbfdSBarry Smith   B->ops->mult             = MatMult_SeqSBAIJ_N;
1590db4efbfdSBarry Smith   B->ops->multadd          = MatMultAdd_SeqSBAIJ_N;
1591db4efbfdSBarry Smith   B->ops->multtranspose    = MatMult_SeqSBAIJ_N;
1592db4efbfdSBarry Smith   B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_N;
1593acfcf0e5SJed Brown   ierr  = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,PETSC_NULL);CHKERRQ(ierr);
159449b5e25fSSatish Balay   if (!flg) {
159549b5e25fSSatish Balay     switch (bs) {
159649b5e25fSSatish Balay     case 1:
159749b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_1;
159849b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_1;
1599431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_1;
1600431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_1;
160149b5e25fSSatish Balay       break;
160249b5e25fSSatish Balay     case 2:
160349b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_2;
160449b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_2;
1605431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_2;
1606431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_2;
160749b5e25fSSatish Balay       break;
160849b5e25fSSatish Balay     case 3:
160949b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_3;
161049b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_3;
1611431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_3;
1612431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_3;
161349b5e25fSSatish Balay       break;
161449b5e25fSSatish Balay     case 4:
161549b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_4;
161649b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_4;
1617431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_4;
1618431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_4;
161949b5e25fSSatish Balay       break;
162049b5e25fSSatish Balay     case 5:
162149b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_5;
162249b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_5;
1623431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_5;
1624431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_5;
162549b5e25fSSatish Balay       break;
162649b5e25fSSatish Balay     case 6:
162749b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_6;
162849b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_6;
1629431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_6;
1630431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_6;
163149b5e25fSSatish Balay       break;
163249b5e25fSSatish Balay     case 7:
1633de53e5efSHong Zhang       B->ops->mult             = MatMult_SeqSBAIJ_7;
163449b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_7;
1635431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_7;
1636431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_7;
163749b5e25fSSatish Balay       break;
163849b5e25fSSatish Balay     }
163949b5e25fSSatish Balay   }
164049b5e25fSSatish Balay 
164149b5e25fSSatish Balay   b->mbs = mbs;
16424afc71dfSHong Zhang   b->nbs = mbs;
1643ab93d7beSBarry Smith   if (!skipallocation) {
16442ee49352SLisandro Dalcin     if (!b->imax) {
1645ab93d7beSBarry Smith       ierr = PetscMalloc2(mbs,PetscInt,&b->imax,mbs,PetscInt,&b->ilen);CHKERRQ(ierr);
1646c760cd28SBarry Smith       b->free_imax_ilen = PETSC_TRUE;
16472ee49352SLisandro Dalcin       ierr = PetscLogObjectMemory(B,2*mbs*sizeof(PetscInt));CHKERRQ(ierr);
16482ee49352SLisandro Dalcin     }
164949b5e25fSSatish Balay     if (!nnz) {
1650435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
165149b5e25fSSatish Balay       else if (nz <= 0)        nz = 1;
165249b5e25fSSatish Balay       for (i=0; i<mbs; i++) {
16538cef66ccSHong Zhang         b->imax[i] = nz;
165449b5e25fSSatish Balay       }
1655153ea458SHong Zhang       nz = nz*mbs; /* total nz */
165649b5e25fSSatish Balay     } else {
165749b5e25fSSatish Balay       nz = 0;
16588cef66ccSHong Zhang       for (i=0; i<mbs; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
165949b5e25fSSatish Balay     }
16602ee49352SLisandro Dalcin     /* b->ilen will count nonzeros in each block row so far. */
16612ee49352SLisandro Dalcin     for (i=0; i<mbs; i++) { b->ilen[i] = 0;}
16626c6c5352SBarry Smith     /* nz=(nz+mbs)/2; */ /* total diagonal and superdiagonal nonzero blocks */
166349b5e25fSSatish Balay 
166449b5e25fSSatish Balay     /* allocate the matrix space */
16652ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
1666d0f46423SBarry Smith     ierr = PetscMalloc3(bs2*nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->N+1,PetscInt,&b->i);CHKERRQ(ierr);
1667d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->N+1)*sizeof(PetscInt)+nz*(bs2*sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
16686c6c5352SBarry Smith     ierr = PetscMemzero(b->a,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
166913f74950SBarry Smith     ierr = PetscMemzero(b->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
167049b5e25fSSatish Balay     b->singlemalloc = PETSC_TRUE;
167149b5e25fSSatish Balay 
167249b5e25fSSatish Balay     /* pointer to beginning of each row */
1673e60cf9a0SBarry Smith     b->i[0] = 0;
167449b5e25fSSatish Balay     for (i=1; i<mbs+1; i++) {
167549b5e25fSSatish Balay       b->i[i] = b->i[i-1] + b->imax[i-1];
167649b5e25fSSatish Balay     }
1677e6b907acSBarry Smith     b->free_a     = PETSC_TRUE;
1678e6b907acSBarry Smith     b->free_ij    = PETSC_TRUE;
1679e811da20SHong Zhang   } else {
1680e6b907acSBarry Smith     b->free_a     = PETSC_FALSE;
1681e6b907acSBarry Smith     b->free_ij    = PETSC_FALSE;
1682ab93d7beSBarry Smith   }
168349b5e25fSSatish Balay 
1684d0f46423SBarry Smith   B->rmap->bs     = bs;
168549b5e25fSSatish Balay   b->bs2          = bs2;
16866c6c5352SBarry Smith   b->nz           = 0;
1687b32cb4a7SJed Brown   b->maxnz        = nz;
1688153ea458SHong Zhang 
168916cdd363SHong Zhang   b->inew             = 0;
169016cdd363SHong Zhang   b->jnew             = 0;
169116cdd363SHong Zhang   b->anew             = 0;
169216cdd363SHong Zhang   b->a2anew           = 0;
16931a3463dfSHong Zhang   b->permute          = PETSC_FALSE;
1694c464158bSHong Zhang   PetscFunctionReturn(0);
1695c464158bSHong Zhang }
1696a23d5eceSKris Buschelman EXTERN_C_END
1697153ea458SHong Zhang 
1698db4efbfdSBarry Smith /*
1699db4efbfdSBarry Smith    This is used to set the numeric factorization for both Cholesky and ICC symbolic factorization
1700db4efbfdSBarry Smith */
17018b1456e3SHong Zhang #undef __FUNCT__
1702d595f711SHong Zhang #define __FUNCT__ "MatSeqSBAIJSetNumericFactorization_inplace"
1703ace3abfcSBarry Smith PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat B,PetscBool  natural)
1704db4efbfdSBarry Smith {
1705db4efbfdSBarry Smith   PetscErrorCode ierr;
1706ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
1707db4efbfdSBarry Smith   PetscInt       bs = B->rmap->bs;
1708db4efbfdSBarry Smith 
1709db4efbfdSBarry Smith   PetscFunctionBegin;
1710acfcf0e5SJed Brown   ierr    = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,PETSC_NULL);CHKERRQ(ierr);
1711db4efbfdSBarry Smith   if (flg) bs = 8;
1712db4efbfdSBarry Smith 
1713db4efbfdSBarry Smith   if (!natural) {
1714db4efbfdSBarry Smith     switch (bs) {
1715db4efbfdSBarry Smith     case 1:
1716d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_inplace;
1717db4efbfdSBarry Smith       break;
1718db4efbfdSBarry Smith     case 2:
1719db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2;
1720db4efbfdSBarry Smith       break;
1721db4efbfdSBarry Smith     case 3:
1722db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3;
1723db4efbfdSBarry Smith       break;
1724db4efbfdSBarry Smith     case 4:
1725db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4;
1726db4efbfdSBarry Smith       break;
1727db4efbfdSBarry Smith     case 5:
1728db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5;
1729db4efbfdSBarry Smith       break;
1730db4efbfdSBarry Smith     case 6:
1731db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6;
1732db4efbfdSBarry Smith       break;
1733db4efbfdSBarry Smith     case 7:
1734db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7;
1735db4efbfdSBarry Smith       break;
1736db4efbfdSBarry Smith     default:
1737db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N;
1738db4efbfdSBarry Smith       break;
1739db4efbfdSBarry Smith     }
1740db4efbfdSBarry Smith   } else {
1741db4efbfdSBarry Smith     switch (bs) {
1742db4efbfdSBarry Smith     case 1:
1743d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering_inplace;
1744db4efbfdSBarry Smith       break;
1745db4efbfdSBarry Smith     case 2:
1746db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering;
1747db4efbfdSBarry Smith       break;
1748db4efbfdSBarry Smith     case 3:
1749db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3_NaturalOrdering;
1750db4efbfdSBarry Smith       break;
1751db4efbfdSBarry Smith     case 4:
1752db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4_NaturalOrdering;
1753db4efbfdSBarry Smith       break;
1754db4efbfdSBarry Smith     case 5:
1755db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5_NaturalOrdering;
1756db4efbfdSBarry Smith       break;
1757db4efbfdSBarry Smith     case 6:
1758db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6_NaturalOrdering;
1759db4efbfdSBarry Smith       break;
1760db4efbfdSBarry Smith     case 7:
1761db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7_NaturalOrdering;
1762db4efbfdSBarry Smith       break;
1763db4efbfdSBarry Smith     default:
1764db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering;
1765db4efbfdSBarry Smith       break;
1766db4efbfdSBarry Smith     }
1767db4efbfdSBarry Smith   }
1768db4efbfdSBarry Smith   PetscFunctionReturn(0);
1769db4efbfdSBarry Smith }
1770db4efbfdSBarry Smith 
1771d769727bSBarry Smith EXTERN_C_BEGIN
17727087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqSBAIJ_SeqAIJ(Mat, MatType,MatReuse,Mat*);
17737087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqSBAIJ_SeqBAIJ(Mat, MatType,MatReuse,Mat*);
1774d769727bSBarry Smith EXTERN_C_END
1775d769727bSBarry Smith 
1776e631078cSBarry Smith 
1777e631078cSBarry Smith EXTERN_C_BEGIN
17785c9eb25fSBarry Smith #undef __FUNCT__
17795c9eb25fSBarry Smith #define __FUNCT__ "MatGetFactor_seqsbaij_petsc"
17805c9eb25fSBarry Smith PetscErrorCode MatGetFactor_seqsbaij_petsc(Mat A,MatFactorType ftype,Mat *B)
17815c9eb25fSBarry Smith {
1782d0f46423SBarry Smith   PetscInt           n = A->rmap->n;
17835c9eb25fSBarry Smith   PetscErrorCode     ierr;
17845c9eb25fSBarry Smith 
17855c9eb25fSBarry Smith   PetscFunctionBegin;
17865c9eb25fSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
17875c9eb25fSBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
17885c9eb25fSBarry Smith   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
17895c9eb25fSBarry Smith     ierr = MatSetType(*B,MATSEQSBAIJ);CHKERRQ(ierr);
17905c9eb25fSBarry Smith     ierr = MatSeqSBAIJSetPreallocation(*B,1,MAT_SKIP_ALLOCATION,PETSC_NULL);CHKERRQ(ierr);
17917b056e98SHong Zhang     (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqSBAIJ;
1792c6d0d4f0SHong Zhang     (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqSBAIJ;
1793e32f2f54SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported");
1794d5f3da31SBarry Smith   (*B)->factortype = ftype;
17955c9eb25fSBarry Smith   PetscFunctionReturn(0);
17965c9eb25fSBarry Smith }
1797e631078cSBarry Smith EXTERN_C_END
17985c9eb25fSBarry Smith 
17995c9eb25fSBarry Smith EXTERN_C_BEGIN
1800db4efbfdSBarry Smith #undef __FUNCT__
1801db4efbfdSBarry Smith #define __FUNCT__ "MatGetFactorAvailable_seqsbaij_petsc"
1802ace3abfcSBarry Smith PetscErrorCode MatGetFactorAvailable_seqsbaij_petsc(Mat A,MatFactorType ftype,PetscBool  *flg)
1803db4efbfdSBarry Smith {
1804db4efbfdSBarry Smith   PetscFunctionBegin;
1805db4efbfdSBarry Smith   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
1806db4efbfdSBarry Smith     *flg = PETSC_TRUE;
1807db4efbfdSBarry Smith   } else {
1808db4efbfdSBarry Smith     *flg = PETSC_FALSE;
1809db4efbfdSBarry Smith   }
1810db4efbfdSBarry Smith   PetscFunctionReturn(0);
1811db4efbfdSBarry Smith }
1812db4efbfdSBarry Smith EXTERN_C_END
1813db4efbfdSBarry Smith 
1814db4efbfdSBarry Smith EXTERN_C_BEGIN
1815611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
1816bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_sbaij_mumps(Mat,MatFactorType,Mat*);
1817611f576cSBarry Smith #endif
1818611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
18195c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_seqsbaij_spooles(Mat,MatFactorType,Mat*);
1820611f576cSBarry Smith #endif
1821b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
1822b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqsbaij_pastix(Mat,MatFactorType,Mat*);
1823b5e56a35SBarry Smith #endif
182420db9a53SJed Brown #if defined(PETSC_HAVE_CHOLMOD)
182520db9a53SJed Brown extern PetscErrorCode MatGetFactor_seqsbaij_cholmod(Mat,MatFactorType,Mat*);
182620db9a53SJed Brown #endif
18272938c13dSDahai Guo extern PetscErrorCode MatGetFactor_seqsbaij_sbstrm(Mat,MatFactorType,Mat*);
1828b1f23a54SSatish Balay EXTERN_C_END
18295c9eb25fSBarry Smith 
18300bad9183SKris Buschelman /*MC
1831fafad747SKris Buschelman   MATSEQSBAIJ - MATSEQSBAIJ = "seqsbaij" - A matrix type to be used for sequential symmetric block sparse matrices,
18320bad9183SKris Buschelman   based on block compressed sparse row format.  Only the upper triangular portion of the matrix is stored.
18330bad9183SKris Buschelman 
1834828413b8SBarry Smith   For complex numbers by default this matrix is symmetric, NOT Hermitian symmetric. To make it Hermitian symmetric you
183571dad5bbSBarry Smith   can call MatSetOption(Mat, MAT_HERMITIAN); after MatAssemblyEnd()
1836828413b8SBarry Smith 
18370bad9183SKris Buschelman   Options Database Keys:
18380bad9183SKris Buschelman   . -mat_type seqsbaij - sets the matrix type to "seqsbaij" during a call to MatSetFromOptions()
18390bad9183SKris Buschelman 
184071dad5bbSBarry Smith   Notes: By default if you insert values into the lower triangular part of the matrix they are simply ignored (since they are not
184171dad5bbSBarry Smith      stored and it is assumed they symmetric to the upper triangular). If you call MatSetOption(Mat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_FALSE) or use
184271dad5bbSBarry Smith      the options database -mat_ignore_lower_triangular false it will generate an error if you try to set a value in the lower triangular portion.
184371dad5bbSBarry Smith 
184471dad5bbSBarry Smith 
18450bad9183SKris Buschelman   Level: beginner
18460bad9183SKris Buschelman 
18470bad9183SKris Buschelman   .seealso: MatCreateSeqSBAIJ
18480bad9183SKris Buschelman M*/
18490bad9183SKris Buschelman 
1850a23d5eceSKris Buschelman EXTERN_C_BEGIN
1851aa5a9175SDahai Guo extern PetscErrorCode  MatConvert_SeqSBAIJ_SeqSBSTRM(Mat, MatType,MatReuse,Mat*);
1852aa5a9175SDahai Guo EXTERN_C_END
1853aa5a9175SDahai Guo 
1854aa5a9175SDahai Guo 
1855aa5a9175SDahai Guo EXTERN_C_BEGIN
1856a23d5eceSKris Buschelman #undef __FUNCT__
1857a23d5eceSKris Buschelman #define __FUNCT__ "MatCreate_SeqSBAIJ"
18587087cfbeSBarry Smith PetscErrorCode  MatCreate_SeqSBAIJ(Mat B)
1859a23d5eceSKris Buschelman {
1860a23d5eceSKris Buschelman   Mat_SeqSBAIJ   *b;
1861dfbe8321SBarry Smith   PetscErrorCode ierr;
186213f74950SBarry Smith   PetscMPIInt    size;
1863ace3abfcSBarry Smith   PetscBool      no_unroll = PETSC_FALSE,no_inode = PETSC_FALSE;
1864a23d5eceSKris Buschelman 
1865a23d5eceSKris Buschelman   PetscFunctionBegin;
18667adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
1867e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Comm must be of size 1");
1868a23d5eceSKris Buschelman 
186938f2d2fdSLisandro Dalcin   ierr    = PetscNewLog(B,Mat_SeqSBAIJ,&b);CHKERRQ(ierr);
1870a23d5eceSKris Buschelman   B->data = (void*)b;
1871a23d5eceSKris Buschelman   ierr    = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
1872a23d5eceSKris Buschelman   B->ops->destroy     = MatDestroy_SeqSBAIJ;
1873a23d5eceSKris Buschelman   B->ops->view        = MatView_SeqSBAIJ;
1874a23d5eceSKris Buschelman   b->row              = 0;
1875a23d5eceSKris Buschelman   b->icol             = 0;
1876a23d5eceSKris Buschelman   b->reallocs         = 0;
1877a23d5eceSKris Buschelman   b->saved_values     = 0;
18780def2e27SBarry Smith   b->inode.limit      = 5;
18790def2e27SBarry Smith   b->inode.max_limit  = 5;
1880a23d5eceSKris Buschelman 
1881a23d5eceSKris Buschelman   b->roworiented      = PETSC_TRUE;
1882a23d5eceSKris Buschelman   b->nonew            = 0;
1883a23d5eceSKris Buschelman   b->diag             = 0;
1884a23d5eceSKris Buschelman   b->solve_work       = 0;
1885a23d5eceSKris Buschelman   b->mult_work        = 0;
1886a23d5eceSKris Buschelman   B->spptr            = 0;
1887f2cbd3d5SJed Brown   B->info.nz_unneeded = (PetscReal)b->maxnz*b->bs2;
1888a9817697SBarry Smith   b->keepnonzeropattern   = PETSC_FALSE;
1889a23d5eceSKris Buschelman   b->xtoy             = 0;
1890a23d5eceSKris Buschelman   b->XtoY             = 0;
1891a23d5eceSKris Buschelman 
1892a23d5eceSKris Buschelman   b->inew             = 0;
1893a23d5eceSKris Buschelman   b->jnew             = 0;
1894a23d5eceSKris Buschelman   b->anew             = 0;
1895a23d5eceSKris Buschelman   b->a2anew           = 0;
1896a23d5eceSKris Buschelman   b->permute          = PETSC_FALSE;
1897a23d5eceSKris Buschelman 
189871dad5bbSBarry Smith   b->ignore_ltriangular = PETSC_TRUE;
1899acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_ignore_lower_triangular",&b->ignore_ltriangular,PETSC_NULL);CHKERRQ(ierr);
1900941593c8SHong Zhang 
1901f5edf698SHong Zhang   b->getrow_utriangular = PETSC_FALSE;
1902acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_getrow_uppertriangular",&b->getrow_utriangular,PETSC_NULL);CHKERRQ(ierr);
1903f5edf698SHong Zhang 
1904b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
1905ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C",
1906b5e56a35SBarry Smith 					   "MatGetFactor_seqsbaij_pastix",
1907b5e56a35SBarry Smith 					   MatGetFactor_seqsbaij_pastix);CHKERRQ(ierr);
1908b5e56a35SBarry Smith #endif
1909611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
1910ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C",
19115c9eb25fSBarry Smith                                      "MatGetFactor_seqsbaij_spooles",
19125c9eb25fSBarry Smith                                      MatGetFactor_seqsbaij_spooles);CHKERRQ(ierr);
1913611f576cSBarry Smith #endif
1914611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
1915ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C",
1916bccb9932SShri Abhyankar                                      "MatGetFactor_sbaij_mumps",
1917bccb9932SShri Abhyankar                                      MatGetFactor_sbaij_mumps);CHKERRQ(ierr);
1918611f576cSBarry Smith #endif
191920db9a53SJed Brown #if defined(PETSC_HAVE_CHOLMOD)
192020db9a53SJed Brown   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C",
192120db9a53SJed Brown                                      "MatGetFactor_seqsbaij_cholmod",
192220db9a53SJed Brown                                      MatGetFactor_seqsbaij_cholmod);CHKERRQ(ierr);
192320db9a53SJed Brown #endif
1924ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C",
1925db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqsbaij_petsc",
1926db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqsbaij_petsc);CHKERRQ(ierr);
1927ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C",
19285c9eb25fSBarry Smith                                      "MatGetFactor_seqsbaij_petsc",
19295c9eb25fSBarry Smith                                      MatGetFactor_seqsbaij_petsc);CHKERRQ(ierr);
19303edee7c7SSatish Balay   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_sbstrm_C",
19312938c13dSDahai Guo                                      "MatGetFactor_seqsbaij_sbstrm",
19322938c13dSDahai Guo                                      MatGetFactor_seqsbaij_sbstrm);CHKERRQ(ierr);
1933a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
1934a23d5eceSKris Buschelman                                      "MatStoreValues_SeqSBAIJ",
1935a23d5eceSKris Buschelman                                      MatStoreValues_SeqSBAIJ);CHKERRQ(ierr);
1936a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
1937a23d5eceSKris Buschelman                                      "MatRetrieveValues_SeqSBAIJ",
1938a23d5eceSKris Buschelman                                      (void*)MatRetrieveValues_SeqSBAIJ);CHKERRQ(ierr);
1939a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqSBAIJSetColumnIndices_C",
1940a23d5eceSKris Buschelman                                      "MatSeqSBAIJSetColumnIndices_SeqSBAIJ",
1941a23d5eceSKris Buschelman                                      MatSeqSBAIJSetColumnIndices_SeqSBAIJ);CHKERRQ(ierr);
19424e5e7fe4SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqsbaij_seqaij_C",
19434e5e7fe4SHong Zhang                                      "MatConvert_SeqSBAIJ_SeqAIJ",
19444e5e7fe4SHong Zhang                                       MatConvert_SeqSBAIJ_SeqAIJ);CHKERRQ(ierr);
1945a0e1a404SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqsbaij_seqbaij_C",
1946a0e1a404SHong Zhang                                      "MatConvert_SeqSBAIJ_SeqBAIJ",
1947a0e1a404SHong Zhang                                       MatConvert_SeqSBAIJ_SeqBAIJ);CHKERRQ(ierr);
1948a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqSBAIJSetPreallocation_C",
1949a23d5eceSKris Buschelman                                      "MatSeqSBAIJSetPreallocation_SeqSBAIJ",
1950a23d5eceSKris Buschelman                                      MatSeqSBAIJSetPreallocation_SeqSBAIJ);CHKERRQ(ierr);
1951aa5a9175SDahai Guo   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqsbaij_seqsbstrm_C",
1952aa5a9175SDahai Guo                                      "MatConvert_SeqSBAIJ_SeqSBSTRM",
1953aa5a9175SDahai Guo                                       MatConvert_SeqSBAIJ_SeqSBSTRM);CHKERRQ(ierr);
195423ce1328SBarry Smith 
195523ce1328SBarry Smith   B->symmetric                  = PETSC_TRUE;
195623ce1328SBarry Smith   B->structurally_symmetric     = PETSC_TRUE;
195723ce1328SBarry Smith   B->symmetric_set              = PETSC_TRUE;
195823ce1328SBarry Smith   B->structurally_symmetric_set = PETSC_TRUE;
195917667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQSBAIJ);CHKERRQ(ierr);
19600def2e27SBarry Smith 
19610def2e27SBarry Smith   ierr = PetscOptionsBegin(((PetscObject)B)->comm,((PetscObject)B)->prefix,"Options for SEQSBAIJ matrix","Mat");CHKERRQ(ierr);
1962acfcf0e5SJed Brown     ierr = PetscOptionsBool("-mat_no_unroll","Do not optimize for inodes (slower)",PETSC_NULL,no_unroll,&no_unroll,PETSC_NULL);CHKERRQ(ierr);
19630def2e27SBarry Smith     if (no_unroll) {ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_unroll\n");CHKERRQ(ierr);}
1964acfcf0e5SJed Brown     ierr = PetscOptionsBool("-mat_no_inode","Do not optimize for inodes (slower)",PETSC_NULL,no_inode,&no_inode,PETSC_NULL);CHKERRQ(ierr);
19650def2e27SBarry Smith     if (no_inode) {ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_inode\n");CHKERRQ(ierr);}
19660def2e27SBarry 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);
19670def2e27SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1968ace3abfcSBarry Smith   b->inode.use = (PetscBool)(!(no_unroll || no_inode));
19690def2e27SBarry Smith   if (b->inode.limit > b->inode.max_limit) b->inode.limit = b->inode.max_limit;
19700def2e27SBarry Smith 
1971a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1972a23d5eceSKris Buschelman }
1973a23d5eceSKris Buschelman EXTERN_C_END
1974a23d5eceSKris Buschelman 
1975a23d5eceSKris Buschelman #undef __FUNCT__
1976a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation"
1977a23d5eceSKris Buschelman /*@C
1978a23d5eceSKris Buschelman    MatSeqSBAIJSetPreallocation - Creates a sparse symmetric matrix in block AIJ (block
1979a23d5eceSKris Buschelman    compressed row) format.  For good matrix assembly performance the
1980a23d5eceSKris Buschelman    user should preallocate the matrix storage by setting the parameter nz
1981a23d5eceSKris Buschelman    (or the array nnz).  By setting these parameters accurately, performance
1982a23d5eceSKris Buschelman    during matrix assembly can be increased by more than a factor of 50.
1983a23d5eceSKris Buschelman 
1984a23d5eceSKris Buschelman    Collective on Mat
1985a23d5eceSKris Buschelman 
1986a23d5eceSKris Buschelman    Input Parameters:
1987a23d5eceSKris Buschelman +  A - the symmetric matrix
1988a23d5eceSKris Buschelman .  bs - size of block
1989a23d5eceSKris Buschelman .  nz - number of block nonzeros per block row (same for all rows)
1990a23d5eceSKris Buschelman -  nnz - array containing the number of block nonzeros in the upper triangular plus
1991a23d5eceSKris Buschelman          diagonal portion of each block (possibly different for each block row) or PETSC_NULL
1992a23d5eceSKris Buschelman 
1993a23d5eceSKris Buschelman    Options Database Keys:
1994a23d5eceSKris Buschelman .   -mat_no_unroll - uses code that does not unroll the loops in the
1995a23d5eceSKris Buschelman                      block calculations (much slower)
1996db4efbfdSBarry Smith .    -mat_block_size - size of the blocks to use (only works if a negative bs is passed in
1997a23d5eceSKris Buschelman 
1998a23d5eceSKris Buschelman    Level: intermediate
1999a23d5eceSKris Buschelman 
2000a23d5eceSKris Buschelman    Notes:
2001a23d5eceSKris Buschelman    Specify the preallocated storage with either nz or nnz (not both).
2002a23d5eceSKris Buschelman    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
20030598bfebSBarry Smith    allocation.  See the <a href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</a> for details.
2004a23d5eceSKris Buschelman 
2005aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
2006aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
2007aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
2008aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
2009aa95bbe8SBarry Smith 
201049a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
201149a6f317SBarry Smith 
201249a6f317SBarry Smith 
2013a23d5eceSKris Buschelman .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPISBAIJ()
2014a23d5eceSKris Buschelman @*/
20157087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt nz,const PetscInt nnz[])
201613f74950SBarry Smith {
20174ac538c5SBarry Smith   PetscErrorCode ierr;
2018a23d5eceSKris Buschelman 
2019a23d5eceSKris Buschelman   PetscFunctionBegin;
20204ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqSBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[]),(B,bs,nz,nnz));CHKERRQ(ierr);
2021a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2022a23d5eceSKris Buschelman }
202349b5e25fSSatish Balay 
20244a2ae208SSatish Balay #undef __FUNCT__
20254a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqSBAIJ"
2026c464158bSHong Zhang /*@C
2027c464158bSHong Zhang    MatCreateSeqSBAIJ - Creates a sparse symmetric matrix in block AIJ (block
2028c464158bSHong Zhang    compressed row) format.  For good matrix assembly performance the
2029c464158bSHong Zhang    user should preallocate the matrix storage by setting the parameter nz
2030c464158bSHong Zhang    (or the array nnz).  By setting these parameters accurately, performance
2031c464158bSHong Zhang    during matrix assembly can be increased by more than a factor of 50.
203249b5e25fSSatish Balay 
2033c464158bSHong Zhang    Collective on MPI_Comm
2034c464158bSHong Zhang 
2035c464158bSHong Zhang    Input Parameters:
2036c464158bSHong Zhang +  comm - MPI communicator, set to PETSC_COMM_SELF
2037c464158bSHong Zhang .  bs - size of block
2038c464158bSHong Zhang .  m - number of rows, or number of columns
2039c464158bSHong Zhang .  nz - number of block nonzeros per block row (same for all rows)
2040744e8345SSatish Balay -  nnz - array containing the number of block nonzeros in the upper triangular plus
2041744e8345SSatish Balay          diagonal portion of each block (possibly different for each block row) or PETSC_NULL
2042c464158bSHong Zhang 
2043c464158bSHong Zhang    Output Parameter:
2044c464158bSHong Zhang .  A - the symmetric matrix
2045c464158bSHong Zhang 
2046c464158bSHong Zhang    Options Database Keys:
2047c464158bSHong Zhang .   -mat_no_unroll - uses code that does not unroll the loops in the
2048c464158bSHong Zhang                      block calculations (much slower)
2049c464158bSHong Zhang .    -mat_block_size - size of the blocks to use
2050c464158bSHong Zhang 
2051c464158bSHong Zhang    Level: intermediate
2052c464158bSHong Zhang 
2053175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2054ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
2055175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2056175b88e8SBarry Smith 
2057c464158bSHong Zhang    Notes:
20586d6d819aSHong Zhang    The number of rows and columns must be divisible by blocksize.
20596d6d819aSHong Zhang    This matrix type does not support complex Hermitian operation.
2060c464158bSHong Zhang 
2061c464158bSHong Zhang    Specify the preallocated storage with either nz or nnz (not both).
2062c464158bSHong Zhang    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
20630598bfebSBarry Smith    allocation.  See the <a href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</a> for details.
2064c464158bSHong Zhang 
206549a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
206649a6f317SBarry Smith 
2067c464158bSHong Zhang .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPISBAIJ()
2068c464158bSHong Zhang @*/
20697087cfbeSBarry Smith PetscErrorCode  MatCreateSeqSBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
2070c464158bSHong Zhang {
2071dfbe8321SBarry Smith   PetscErrorCode ierr;
2072c464158bSHong Zhang 
2073c464158bSHong Zhang   PetscFunctionBegin;
2074f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2075f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2076c464158bSHong Zhang   ierr = MatSetType(*A,MATSEQSBAIJ);CHKERRQ(ierr);
2077ab93d7beSBarry Smith   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*A,bs,nz,(PetscInt*)nnz);CHKERRQ(ierr);
207849b5e25fSSatish Balay   PetscFunctionReturn(0);
207949b5e25fSSatish Balay }
208049b5e25fSSatish Balay 
20814a2ae208SSatish Balay #undef __FUNCT__
20824a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqSBAIJ"
2083dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqSBAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
208449b5e25fSSatish Balay {
208549b5e25fSSatish Balay   Mat            C;
208649b5e25fSSatish Balay   Mat_SeqSBAIJ   *c,*a = (Mat_SeqSBAIJ*)A->data;
20876849ba73SBarry Smith   PetscErrorCode ierr;
2088b40805acSSatish Balay   PetscInt       i,mbs = a->mbs,nz = a->nz,bs2 =a->bs2;
208949b5e25fSSatish Balay 
209049b5e25fSSatish Balay   PetscFunctionBegin;
2091e32f2f54SBarry Smith   if (a->i[mbs] != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupt matrix");
209249b5e25fSSatish Balay 
209349b5e25fSSatish Balay   *B = 0;
20947adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2095d0f46423SBarry Smith   ierr = MatSetSizes(C,A->rmap->N,A->cmap->n,A->rmap->N,A->cmap->n);CHKERRQ(ierr);
20968e9a0fb8SHong Zhang   ierr = MatSetType(C,MATSEQSBAIJ);CHKERRQ(ierr);
20971d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
2098692f9cbeSHong Zhang   c    = (Mat_SeqSBAIJ*)C->data;
2099692f9cbeSHong Zhang 
2100273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2101d5f3da31SBarry Smith   C->factortype         = A->factortype;
210249b5e25fSSatish Balay   c->row                = 0;
210349b5e25fSSatish Balay   c->icol               = 0;
210449b5e25fSSatish Balay   c->saved_values       = 0;
2105a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
210649b5e25fSSatish Balay   C->assembled          = PETSC_TRUE;
210749b5e25fSSatish Balay 
21081e1e43feSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
21091e1e43feSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
211049b5e25fSSatish Balay   c->bs2  = a->bs2;
211149b5e25fSSatish Balay   c->mbs  = a->mbs;
211249b5e25fSSatish Balay   c->nbs  = a->nbs;
211349b5e25fSSatish Balay 
2114c760cd28SBarry Smith   if  (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2115c760cd28SBarry Smith     c->imax           = a->imax;
2116c760cd28SBarry Smith     c->ilen           = a->ilen;
2117c760cd28SBarry Smith     c->free_imax_ilen = PETSC_FALSE;
2118c760cd28SBarry Smith   } else {
21198777fc3fSSatish Balay     ierr = PetscMalloc2((mbs+1),PetscInt,&c->imax,(mbs+1),PetscInt,&c->ilen);CHKERRQ(ierr);
2120c760cd28SBarry Smith     ierr = PetscLogObjectMemory(C,2*(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
212149b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
212249b5e25fSSatish Balay       c->imax[i] = a->imax[i];
212349b5e25fSSatish Balay       c->ilen[i] = a->ilen[i];
212449b5e25fSSatish Balay     }
2125c760cd28SBarry Smith     c->free_imax_ilen = PETSC_TRUE;
2126c760cd28SBarry Smith   }
212749b5e25fSSatish Balay 
212849b5e25fSSatish Balay   /* allocate the matrix space */
21294da8f245SBarry Smith   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
21304da8f245SBarry Smith     ierr = PetscMalloc(bs2*nz*sizeof(MatScalar),&c->a);CHKERRQ(ierr);
21314da8f245SBarry Smith     ierr = PetscLogObjectMemory(C,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
21324da8f245SBarry Smith     c->singlemalloc = PETSC_FALSE;
21334da8f245SBarry Smith     c->free_ij      = PETSC_FALSE;
21344da8f245SBarry Smith     c->parent       = A;
21354da8f245SBarry Smith     ierr            = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
21364da8f245SBarry Smith     ierr            = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
21374da8f245SBarry Smith     ierr            = MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
21384da8f245SBarry Smith   } else {
2139b40805acSSatish Balay     ierr = PetscMalloc3(bs2*nz,MatScalar,&c->a,nz,PetscInt,&c->j,mbs+1,PetscInt,&c->i);CHKERRQ(ierr);
214013f74950SBarry Smith     ierr = PetscMemcpy(c->i,a->i,(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
2141b40805acSSatish Balay     ierr = PetscLogObjectMemory(C,(mbs+1)*sizeof(PetscInt) + nz*(bs2*sizeof(MatScalar) + sizeof(PetscInt)));CHKERRQ(ierr);
21424da8f245SBarry Smith     c->singlemalloc = PETSC_TRUE;
21434da8f245SBarry Smith     c->free_ij      = PETSC_TRUE;
21444da8f245SBarry Smith   }
214549b5e25fSSatish Balay   if (mbs > 0) {
21464da8f245SBarry Smith     if (cpvalues != MAT_SHARE_NONZERO_PATTERN) {
214713f74950SBarry Smith       ierr = PetscMemcpy(c->j,a->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
21484da8f245SBarry Smith     }
214949b5e25fSSatish Balay     if (cpvalues == MAT_COPY_VALUES) {
215049b5e25fSSatish Balay       ierr = PetscMemcpy(c->a,a->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
215149b5e25fSSatish Balay     } else {
215249b5e25fSSatish Balay       ierr = PetscMemzero(c->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
215349b5e25fSSatish Balay     }
2154a1c3900fSBarry Smith     if (a->jshort) {
21554da8f245SBarry Smith       if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
21564da8f245SBarry Smith         c->jshort      = a->jshort;
21574da8f245SBarry Smith         c->free_jshort = PETSC_FALSE;
21584da8f245SBarry Smith       } else {
2159a1c3900fSBarry Smith         ierr = PetscMalloc(nz*sizeof(unsigned short),&c->jshort);CHKERRQ(ierr);
2160c760cd28SBarry Smith         ierr = PetscLogObjectMemory(C,nz*sizeof(unsigned short));CHKERRQ(ierr);
2161a1c3900fSBarry Smith         ierr = PetscMemcpy(c->jshort,a->jshort,nz*sizeof(unsigned short));CHKERRQ(ierr);
21624da8f245SBarry Smith         c->free_jshort = PETSC_TRUE;
21634da8f245SBarry Smith       }
2164a1c3900fSBarry Smith     }
216549b5e25fSSatish Balay   }
216649b5e25fSSatish Balay 
216749b5e25fSSatish Balay   c->roworiented = a->roworiented;
216849b5e25fSSatish Balay   c->nonew       = a->nonew;
216949b5e25fSSatish Balay 
217049b5e25fSSatish Balay   if (a->diag) {
2171c760cd28SBarry Smith     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2172c760cd28SBarry Smith       c->diag      = a->diag;
2173c760cd28SBarry Smith       c->free_diag = PETSC_FALSE;
2174c760cd28SBarry Smith     } else {
21752ed38d0bSJed Brown       ierr = PetscMalloc(mbs*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
21762ed38d0bSJed Brown       ierr = PetscLogObjectMemory(C,mbs*sizeof(PetscInt));CHKERRQ(ierr);
217749b5e25fSSatish Balay       for (i=0; i<mbs; i++) {
217849b5e25fSSatish Balay 	c->diag[i] = a->diag[i];
217949b5e25fSSatish Balay       }
2180c760cd28SBarry Smith       c->free_diag = PETSC_TRUE;
2181c760cd28SBarry Smith     }
218249b5e25fSSatish Balay   } else c->diag  = 0;
21836c6c5352SBarry Smith   c->nz           = a->nz;
2184f2cbd3d5SJed Brown   c->maxnz        = a->nz; /* Since we allocate exactly the right amount */
218549b5e25fSSatish Balay   c->solve_work   = 0;
218649b5e25fSSatish Balay   c->mult_work    = 0;
2187e6b907acSBarry Smith   c->free_a       = PETSC_TRUE;
218849b5e25fSSatish Balay   *B = C;
21897adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
219049b5e25fSSatish Balay   PetscFunctionReturn(0);
219149b5e25fSSatish Balay }
219249b5e25fSSatish Balay 
21934a2ae208SSatish Balay #undef __FUNCT__
21945bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_SeqSBAIJ"
2195112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqSBAIJ(Mat newmat,PetscViewer viewer)
21962f480046SShri Abhyankar {
21972f480046SShri Abhyankar   Mat_SeqSBAIJ   *a;
21982f480046SShri Abhyankar   PetscErrorCode ierr;
21992f480046SShri Abhyankar   int            fd;
22002f480046SShri Abhyankar   PetscMPIInt    size;
22012f480046SShri Abhyankar   PetscInt       i,nz,header[4],*rowlengths=0,M,N,bs=1;
22022f480046SShri Abhyankar   PetscInt       *mask,mbs,*jj,j,rowcount,nzcount,k,*s_browlengths,maskcount;
22032f480046SShri Abhyankar   PetscInt       kmax,jcount,block,idx,point,nzcountb,extra_rows,rows,cols;
22042f480046SShri Abhyankar   PetscInt       *masked,nmask,tmp,bs2,ishift;
22052f480046SShri Abhyankar   PetscScalar    *aa;
22062f480046SShri Abhyankar   MPI_Comm       comm = ((PetscObject)viewer)->comm;
22072f480046SShri Abhyankar 
22082f480046SShri Abhyankar   PetscFunctionBegin;
22092f480046SShri Abhyankar   ierr = PetscOptionsGetInt(PETSC_NULL,"-matload_block_size",&bs,PETSC_NULL);CHKERRQ(ierr);
22102f480046SShri Abhyankar   bs2  = bs*bs;
22112f480046SShri Abhyankar 
22122f480046SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
22132f480046SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
22142f480046SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
22152f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
22162f480046SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not Mat object");
22172f480046SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
22182f480046SShri Abhyankar 
22192f480046SShri Abhyankar   if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as SeqSBAIJ");
22202f480046SShri Abhyankar 
22212f480046SShri Abhyankar   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");
22222f480046SShri Abhyankar 
22232f480046SShri Abhyankar   /*
22242f480046SShri Abhyankar      This code adds extra rows to make sure the number of rows is
22252f480046SShri Abhyankar     divisible by the blocksize
22262f480046SShri Abhyankar   */
22272f480046SShri Abhyankar   mbs        = M/bs;
22282f480046SShri Abhyankar   extra_rows = bs - M + bs*(mbs);
22292f480046SShri Abhyankar   if (extra_rows == bs) extra_rows = 0;
22302f480046SShri Abhyankar   else                  mbs++;
22312f480046SShri Abhyankar   if (extra_rows) {
22322f480046SShri Abhyankar     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
22332f480046SShri Abhyankar   }
22342f480046SShri Abhyankar 
22352f480046SShri Abhyankar   /* Set global sizes if not already set */
22362f480046SShri Abhyankar   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) {
22372f480046SShri Abhyankar     ierr = MatSetSizes(newmat,PETSC_DECIDE,PETSC_DECIDE,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
22382f480046SShri Abhyankar   } else { /* Check if the matrix global sizes are correct */
22392f480046SShri Abhyankar     ierr = MatGetSize(newmat,&rows,&cols);CHKERRQ(ierr);
22402f480046SShri 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);
22412f480046SShri Abhyankar   }
22422f480046SShri Abhyankar 
22432f480046SShri Abhyankar   /* read in row lengths */
22442f480046SShri Abhyankar   ierr = PetscMalloc((M+extra_rows)*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
22452f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
22462f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
22472f480046SShri Abhyankar 
22482f480046SShri Abhyankar   /* read in column indices */
22492f480046SShri Abhyankar   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscInt),&jj);CHKERRQ(ierr);
22502f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,jj,nz,PETSC_INT);CHKERRQ(ierr);
22512f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) jj[nz+i] = M+i;
22522f480046SShri Abhyankar 
22532f480046SShri Abhyankar   /* loop over row lengths determining block row lengths */
22542f480046SShri Abhyankar   ierr     = PetscMalloc(mbs*sizeof(PetscInt),&s_browlengths);CHKERRQ(ierr);
22552f480046SShri Abhyankar   ierr     = PetscMemzero(s_browlengths,mbs*sizeof(PetscInt));CHKERRQ(ierr);
22562f480046SShri Abhyankar   ierr     = PetscMalloc2(mbs,PetscInt,&mask,mbs,PetscInt,&masked);CHKERRQ(ierr);
22572f480046SShri Abhyankar   ierr     = PetscMemzero(mask,mbs*sizeof(PetscInt));CHKERRQ(ierr);
22582f480046SShri Abhyankar   rowcount = 0;
22592f480046SShri Abhyankar   nzcount  = 0;
22602f480046SShri Abhyankar   for (i=0; i<mbs; i++) {
22612f480046SShri Abhyankar     nmask = 0;
22622f480046SShri Abhyankar     for (j=0; j<bs; j++) {
22632f480046SShri Abhyankar       kmax = rowlengths[rowcount];
22642f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
22652f480046SShri Abhyankar         tmp = jj[nzcount++]/bs;   /* block col. index */
22662f480046SShri Abhyankar         if (!mask[tmp] && tmp >= i) {masked[nmask++] = tmp; mask[tmp] = 1;}
22672f480046SShri Abhyankar       }
22682f480046SShri Abhyankar       rowcount++;
22692f480046SShri Abhyankar     }
22702f480046SShri Abhyankar     s_browlengths[i] += nmask;
22712f480046SShri Abhyankar 
22722f480046SShri Abhyankar     /* zero out the mask elements we set */
22732f480046SShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
22742f480046SShri Abhyankar   }
22752f480046SShri Abhyankar 
22762f480046SShri Abhyankar   /* Do preallocation */
22772f480046SShri Abhyankar   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(newmat,bs,0,s_browlengths);CHKERRQ(ierr);
22782f480046SShri Abhyankar   a = (Mat_SeqSBAIJ*)newmat->data;
22792f480046SShri Abhyankar 
22802f480046SShri Abhyankar   /* set matrix "i" values */
22812f480046SShri Abhyankar   a->i[0] = 0;
22822f480046SShri Abhyankar   for (i=1; i<= mbs; i++) {
22832f480046SShri Abhyankar     a->i[i]      = a->i[i-1] + s_browlengths[i-1];
22842f480046SShri Abhyankar     a->ilen[i-1] = s_browlengths[i-1];
22852f480046SShri Abhyankar   }
22862f480046SShri Abhyankar   a->nz = a->i[mbs];
22872f480046SShri Abhyankar 
22882f480046SShri Abhyankar   /* read in nonzero values */
22892f480046SShri Abhyankar   ierr = PetscMalloc((nz+extra_rows)*sizeof(PetscScalar),&aa);CHKERRQ(ierr);
22902f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,aa,nz,PETSC_SCALAR);CHKERRQ(ierr);
22912f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) aa[nz+i] = 1.0;
22922f480046SShri Abhyankar 
22932f480046SShri Abhyankar   /* set "a" and "j" values into matrix */
22942f480046SShri Abhyankar   nzcount = 0; jcount = 0;
22952f480046SShri Abhyankar   for (i=0; i<mbs; i++) {
22962f480046SShri Abhyankar     nzcountb = nzcount;
22972f480046SShri Abhyankar     nmask    = 0;
22982f480046SShri Abhyankar     for (j=0; j<bs; j++) {
22992f480046SShri Abhyankar       kmax = rowlengths[i*bs+j];
23002f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
23012f480046SShri Abhyankar         tmp = jj[nzcount++]/bs; /* block col. index */
23022f480046SShri Abhyankar         if (!mask[tmp] && tmp >= i) { masked[nmask++] = tmp; mask[tmp] = 1;}
23032f480046SShri Abhyankar       }
23042f480046SShri Abhyankar     }
23052f480046SShri Abhyankar     /* sort the masked values */
23062f480046SShri Abhyankar     ierr = PetscSortInt(nmask,masked);CHKERRQ(ierr);
23072f480046SShri Abhyankar 
23082f480046SShri Abhyankar     /* set "j" values into matrix */
23092f480046SShri Abhyankar     maskcount = 1;
23102f480046SShri Abhyankar     for (j=0; j<nmask; j++) {
23112f480046SShri Abhyankar       a->j[jcount++]  = masked[j];
23122f480046SShri Abhyankar       mask[masked[j]] = maskcount++;
23132f480046SShri Abhyankar     }
23142f480046SShri Abhyankar 
23152f480046SShri Abhyankar     /* set "a" values into matrix */
23162f480046SShri Abhyankar     ishift = bs2*a->i[i];
23172f480046SShri Abhyankar     for (j=0; j<bs; j++) {
23182f480046SShri Abhyankar       kmax = rowlengths[i*bs+j];
23192f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
23202f480046SShri Abhyankar         tmp       = jj[nzcountb]/bs ; /* block col. index */
23212f480046SShri Abhyankar         if (tmp >= i){
23222f480046SShri Abhyankar           block     = mask[tmp] - 1;
23232f480046SShri Abhyankar           point     = jj[nzcountb] - bs*tmp;
23242f480046SShri Abhyankar           idx       = ishift + bs2*block + j + bs*point;
23252f480046SShri Abhyankar           a->a[idx] = aa[nzcountb];
23262f480046SShri Abhyankar         }
23272f480046SShri Abhyankar         nzcountb++;
23282f480046SShri Abhyankar       }
23292f480046SShri Abhyankar     }
23302f480046SShri Abhyankar     /* zero out the mask elements we set */
23312f480046SShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
23322f480046SShri Abhyankar   }
23332f480046SShri Abhyankar   if (jcount != a->nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Bad binary matrix");
23342f480046SShri Abhyankar 
23352f480046SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
23362f480046SShri Abhyankar   ierr = PetscFree(s_browlengths);CHKERRQ(ierr);
23372f480046SShri Abhyankar   ierr = PetscFree(aa);CHKERRQ(ierr);
23382f480046SShri Abhyankar   ierr = PetscFree(jj);CHKERRQ(ierr);
23392f480046SShri Abhyankar   ierr = PetscFree2(mask,masked);CHKERRQ(ierr);
23402f480046SShri Abhyankar 
23412f480046SShri Abhyankar   ierr = MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
23422f480046SShri Abhyankar   ierr = MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
23432f480046SShri Abhyankar   ierr = MatView_Private(newmat);CHKERRQ(ierr);
23442f480046SShri Abhyankar   PetscFunctionReturn(0);
23452f480046SShri Abhyankar }
23462f480046SShri Abhyankar 
23472f480046SShri Abhyankar #undef __FUNCT__
2348c75a6043SHong Zhang #define __FUNCT__ "MatCreateSeqSBAIJWithArrays"
2349c75a6043SHong Zhang /*@
2350c75a6043SHong Zhang      MatCreateSeqSBAIJWithArrays - Creates an sequential SBAIJ matrix using matrix elements
2351c75a6043SHong Zhang               (upper triangular entries in CSR format) provided by the user.
2352c75a6043SHong Zhang 
2353c75a6043SHong Zhang      Collective on MPI_Comm
2354c75a6043SHong Zhang 
2355c75a6043SHong Zhang    Input Parameters:
2356c75a6043SHong Zhang +  comm - must be an MPI communicator of size 1
2357c75a6043SHong Zhang .  bs - size of block
2358c75a6043SHong Zhang .  m - number of rows
2359c75a6043SHong Zhang .  n - number of columns
2360c75a6043SHong Zhang .  i - row indices
2361c75a6043SHong Zhang .  j - column indices
2362c75a6043SHong Zhang -  a - matrix values
2363c75a6043SHong Zhang 
2364c75a6043SHong Zhang    Output Parameter:
2365c75a6043SHong Zhang .  mat - the matrix
2366c75a6043SHong Zhang 
2367dfb205c3SBarry Smith    Level: advanced
2368c75a6043SHong Zhang 
2369c75a6043SHong Zhang    Notes:
2370c75a6043SHong Zhang        The i, j, and a arrays are not copied by this routine, the user must free these arrays
2371c75a6043SHong Zhang     once the matrix is destroyed
2372c75a6043SHong Zhang 
2373c75a6043SHong Zhang        You cannot set new nonzero locations into this matrix, that will generate an error.
2374c75a6043SHong Zhang 
2375c75a6043SHong Zhang        The i and j indices are 0 based
2376c75a6043SHong Zhang 
2377dfb205c3SBarry 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
2378dfb205c3SBarry Smith        it is the regular CSR format excluding the lower triangular elements.
2379dfb205c3SBarry Smith 
2380c75a6043SHong Zhang .seealso: MatCreate(), MatCreateMPISBAIJ(), MatCreateSeqSBAIJ()
2381c75a6043SHong Zhang 
2382c75a6043SHong Zhang @*/
23837087cfbeSBarry Smith PetscErrorCode  MatCreateSeqSBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
2384c75a6043SHong Zhang {
2385c75a6043SHong Zhang   PetscErrorCode ierr;
2386c75a6043SHong Zhang   PetscInt       ii;
2387c75a6043SHong Zhang   Mat_SeqSBAIJ   *sbaij;
2388c75a6043SHong Zhang 
2389c75a6043SHong Zhang   PetscFunctionBegin;
2390e32f2f54SBarry Smith   if (bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"block size %D > 1 is not supported yet",bs);
2391e32f2f54SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
2392c75a6043SHong Zhang 
2393c75a6043SHong Zhang   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
2394c75a6043SHong Zhang   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
2395c75a6043SHong Zhang   ierr = MatSetType(*mat,MATSEQSBAIJ);CHKERRQ(ierr);
2396c75a6043SHong Zhang   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*mat,bs,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
2397c75a6043SHong Zhang   sbaij = (Mat_SeqSBAIJ*)(*mat)->data;
2398c75a6043SHong Zhang   ierr = PetscMalloc2(m,PetscInt,&sbaij->imax,m,PetscInt,&sbaij->ilen);CHKERRQ(ierr);
2399c760cd28SBarry Smith   ierr = PetscLogObjectMemory(*mat,2*m*sizeof(PetscInt));CHKERRQ(ierr);
2400c75a6043SHong Zhang 
2401c75a6043SHong Zhang   sbaij->i = i;
2402c75a6043SHong Zhang   sbaij->j = j;
2403c75a6043SHong Zhang   sbaij->a = a;
2404c75a6043SHong Zhang   sbaij->singlemalloc = PETSC_FALSE;
2405c75a6043SHong Zhang   sbaij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
2406e6b907acSBarry Smith   sbaij->free_a       = PETSC_FALSE;
2407e6b907acSBarry Smith   sbaij->free_ij      = PETSC_FALSE;
2408c75a6043SHong Zhang 
2409c75a6043SHong Zhang   for (ii=0; ii<m; ii++) {
2410c75a6043SHong Zhang     sbaij->ilen[ii] = sbaij->imax[ii] = i[ii+1] - i[ii];
2411c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2412e32f2f54SBarry 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]);
2413c75a6043SHong Zhang #endif
2414c75a6043SHong Zhang   }
2415c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2416c75a6043SHong Zhang   for (ii=0; ii<sbaij->i[m]; ii++) {
2417e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
2418e32f2f54SBarry 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]);
2419c75a6043SHong Zhang   }
2420c75a6043SHong Zhang #endif
2421c75a6043SHong Zhang 
2422c75a6043SHong Zhang   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2423c75a6043SHong Zhang   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2424c75a6043SHong Zhang   PetscFunctionReturn(0);
2425c75a6043SHong Zhang }
2426d06b337dSHong Zhang 
2427d06b337dSHong Zhang 
2428d06b337dSHong Zhang 
242949b5e25fSSatish Balay 
243049b5e25fSSatish Balay 
2431