xref: /petsc/src/mat/impls/sbaij/seq/sbaij.c (revision c98fd787ce49cfe1bba58d59168f91d3fdf7ec65)
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);
156214f412SHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
166214f412SHong Zhang PETSC_EXTERN PetscErrorCode MatConvert_SeqSBAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
176214f412SHong Zhang #endif
18b5b17502SBarry Smith 
1949b5e25fSSatish Balay /*
2049b5e25fSSatish Balay      Checks for missing diagonals
2149b5e25fSSatish Balay */
224a2ae208SSatish Balay #undef __FUNCT__
234a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqSBAIJ"
24ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqSBAIJ(Mat A,PetscBool  *missing,PetscInt *dd)
2549b5e25fSSatish Balay {
26045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
276849ba73SBarry Smith   PetscErrorCode ierr;
287734d3b5SMatthew G. Knepley   PetscInt       *diag,*ii = a->i,i;
2949b5e25fSSatish Balay 
3049b5e25fSSatish Balay   PetscFunctionBegin;
31045c9aa0SHong Zhang   ierr     = MatMarkDiagonal_SeqSBAIJ(A);CHKERRQ(ierr);
322af78befSBarry Smith   *missing = PETSC_FALSE;
337734d3b5SMatthew G. Knepley   if (A->rmap->n > 0 && !ii) {
34358d2f5dSShri Abhyankar     *missing = PETSC_TRUE;
35358d2f5dSShri Abhyankar     if (dd) *dd = 0;
36955c1f14SBarry Smith     ierr = PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");CHKERRQ(ierr);
37358d2f5dSShri Abhyankar   } else {
38358d2f5dSShri Abhyankar     diag = a->diag;
3949b5e25fSSatish Balay     for (i=0; i<a->mbs; i++) {
407734d3b5SMatthew G. Knepley       if (diag[i] >= ii[i+1]) {
412af78befSBarry Smith         *missing = PETSC_TRUE;
422af78befSBarry Smith         if (dd) *dd = i;
432af78befSBarry Smith         break;
442af78befSBarry Smith       }
4549b5e25fSSatish Balay     }
46358d2f5dSShri Abhyankar   }
4749b5e25fSSatish Balay   PetscFunctionReturn(0);
4849b5e25fSSatish Balay }
4949b5e25fSSatish Balay 
504a2ae208SSatish Balay #undef __FUNCT__
514a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqSBAIJ"
52dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqSBAIJ(Mat A)
5349b5e25fSSatish Balay {
54045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
556849ba73SBarry Smith   PetscErrorCode ierr;
5648dd3d27SHong Zhang   PetscInt       i,j;
5749b5e25fSSatish Balay 
5849b5e25fSSatish Balay   PetscFunctionBegin;
5909f38230SBarry Smith   if (!a->diag) {
60785e854fSJed Brown     ierr         = PetscMalloc1(a->mbs,&a->diag);CHKERRQ(ierr);
613bb1ff40SBarry Smith     ierr         = PetscLogObjectMemory((PetscObject)A,a->mbs*sizeof(PetscInt));CHKERRQ(ierr);
62c760cd28SBarry Smith     a->free_diag = PETSC_TRUE;
6309f38230SBarry Smith   }
6448dd3d27SHong Zhang   for (i=0; i<a->mbs; i++) {
6548dd3d27SHong Zhang     a->diag[i] = a->i[i+1];
6648dd3d27SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
6748dd3d27SHong Zhang       if (a->j[j] == i) {
6848dd3d27SHong Zhang         a->diag[i] = j;
6948dd3d27SHong Zhang         break;
7048dd3d27SHong Zhang       }
7148dd3d27SHong Zhang     }
7248dd3d27SHong Zhang   }
7349b5e25fSSatish Balay   PetscFunctionReturn(0);
7449b5e25fSSatish Balay }
7549b5e25fSSatish Balay 
764a2ae208SSatish Balay #undef __FUNCT__
774a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqSBAIJ"
781a83f524SJed Brown static PetscErrorCode MatGetRowIJ_SeqSBAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool blockcompressed,PetscInt *nn,const PetscInt *inia[],const PetscInt *inja[],PetscBool  *done)
7949b5e25fSSatish Balay {
80a6ece127SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
81d0f46423SBarry Smith   PetscInt       i,j,n = a->mbs,nz = a->i[n],bs = A->rmap->bs;
821a83f524SJed Brown   PetscInt       **ia = (PetscInt**)inia,**ja = (PetscInt**)inja;
838f7157efSSatish Balay   PetscErrorCode ierr;
8449b5e25fSSatish Balay 
8549b5e25fSSatish Balay   PetscFunctionBegin;
86d3e5a4abSHong Zhang   *nn = n;
87a1373b80SHong Zhang   if (!ia) PetscFunctionReturn(0);
888f7157efSSatish Balay   if (!blockcompressed) {
898f7157efSSatish Balay     /* malloc & create the natural set of indices */
90dcca6d9dSJed Brown     ierr = PetscMalloc2((n+1)*bs,ia,nz*bs,ja);CHKERRQ(ierr);
918f7157efSSatish Balay     for (i=0; i<n+1; i++) {
928f7157efSSatish Balay       for (j=0; j<bs; j++) {
93c6eed6cfSHong Zhang         (*ia)[i*bs+j] = a->i[i]*bs+j+oshift;
948f7157efSSatish Balay       }
958f7157efSSatish Balay     }
968f7157efSSatish Balay     for (i=0; i<nz; i++) {
978f7157efSSatish Balay       for (j=0; j<bs; j++) {
98c6eed6cfSHong Zhang         (*ja)[i*bs+j] = a->j[i]*bs+j+oshift;
998f7157efSSatish Balay       }
1008f7157efSSatish Balay     }
1018f7157efSSatish Balay   } else { /* blockcompressed */
102a6ece127SHong Zhang     if (oshift == 1) {
103a6ece127SHong Zhang       /* temporarily add 1 to i and j indices */
1046c6c5352SBarry Smith       for (i=0; i<nz; i++) a->j[i]++;
105a1373b80SHong Zhang       for (i=0; i<n+1; i++) a->i[i]++;
1068f7157efSSatish Balay     }
107a1373b80SHong Zhang     *ia = a->i; *ja = a->j;
108a6ece127SHong Zhang   }
10949b5e25fSSatish Balay   PetscFunctionReturn(0);
11049b5e25fSSatish Balay }
11149b5e25fSSatish Balay 
1124a2ae208SSatish Balay #undef __FUNCT__
1134a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqSBAIJ"
1141a83f524SJed Brown static PetscErrorCode MatRestoreRowIJ_SeqSBAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool blockcompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
11549b5e25fSSatish Balay {
116b7aaefc3SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
1178f7157efSSatish Balay   PetscInt       i,n = a->mbs,nz = a->i[n];
1188f7157efSSatish Balay   PetscErrorCode ierr;
119a6ece127SHong Zhang 
12049b5e25fSSatish Balay   PetscFunctionBegin;
12149b5e25fSSatish Balay   if (!ia) PetscFunctionReturn(0);
122a6ece127SHong Zhang 
1238f7157efSSatish Balay   if (!blockcompressed) {
1248f7157efSSatish Balay     ierr = PetscFree2(*ia,*ja);CHKERRQ(ierr);
1258f7157efSSatish Balay   } else if (oshift == 1) { /* blockcompressed */
1266c6c5352SBarry Smith     for (i=0; i<nz; i++) a->j[i]--;
127a6ece127SHong Zhang     for (i=0; i<n+1; i++) a->i[i]--;
128a6ece127SHong Zhang   }
129a6ece127SHong Zhang   PetscFunctionReturn(0);
13049b5e25fSSatish Balay }
13149b5e25fSSatish Balay 
1324a2ae208SSatish Balay #undef __FUNCT__
1334a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqSBAIJ"
134dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqSBAIJ(Mat A)
13549b5e25fSSatish Balay {
13649b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
137dfbe8321SBarry Smith   PetscErrorCode ierr;
13849b5e25fSSatish Balay 
13949b5e25fSSatish Balay   PetscFunctionBegin;
140a9f03627SSatish Balay #if defined(PETSC_USE_LOG)
141d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, NZ=%D",A->rmap->N,a->nz);
142a9f03627SSatish Balay #endif
143e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
1447f53bb6cSHong Zhang   if (a->free_diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
1456bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
1466bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
1476bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
148c31cb41cSBarry Smith   ierr = PetscFree(a->idiag);CHKERRQ(ierr);
149c31cb41cSBarry Smith   ierr = PetscFree(a->inode.size);CHKERRQ(ierr);
150c760cd28SBarry Smith   if (a->free_imax_ilen) {ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);}
15105b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
15241f059aeSBarry Smith   ierr = PetscFree(a->sor_work);CHKERRQ(ierr);
15305b42c5fSBarry Smith   ierr = PetscFree(a->solves_work);CHKERRQ(ierr);
15405b42c5fSBarry Smith   ierr = PetscFree(a->mult_work);CHKERRQ(ierr);
15505b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
1564da8f245SBarry Smith   if (a->free_jshort) {ierr = PetscFree(a->jshort);CHKERRQ(ierr);}
1571a3463dfSHong Zhang   ierr = PetscFree(a->inew);CHKERRQ(ierr);
1586bf464f9SBarry Smith   ierr = MatDestroy(&a->parent);CHKERRQ(ierr);
159bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
160901853e0SKris Buschelman 
161dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
162bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);CHKERRQ(ierr);
163bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);CHKERRQ(ierr);
164bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetColumnIndices_C",NULL);CHKERRQ(ierr);
165bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqaij_C",NULL);CHKERRQ(ierr);
166bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqbaij_C",NULL);CHKERRQ(ierr);
167bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetPreallocation_C",NULL);CHKERRQ(ierr);
16838f409ebSLisandro Dalcin   ierr = PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr);
169bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqsbstrm_C",NULL);CHKERRQ(ierr);
1706214f412SHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
1716214f412SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_elemental_C",NULL);CHKERRQ(ierr);
1726214f412SHong Zhang #endif
17349b5e25fSSatish Balay   PetscFunctionReturn(0);
17449b5e25fSSatish Balay }
17549b5e25fSSatish Balay 
1764a2ae208SSatish Balay #undef __FUNCT__
1774a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqSBAIJ"
178ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqSBAIJ(Mat A,MatOption op,PetscBool flg)
17949b5e25fSSatish Balay {
180045c9aa0SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
18163ba0a88SBarry Smith   PetscErrorCode ierr;
18249b5e25fSSatish Balay 
18349b5e25fSSatish Balay   PetscFunctionBegin;
1844d9d31abSKris Buschelman   switch (op) {
1854d9d31abSKris Buschelman   case MAT_ROW_ORIENTED:
1864e0d8c25SBarry Smith     a->roworiented = flg;
1874d9d31abSKris Buschelman     break;
188a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
189a9817697SBarry Smith     a->keepnonzeropattern = flg;
1904d9d31abSKris Buschelman     break;
191512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
192512a5fc5SBarry Smith     a->nonew = (flg ? 0 : 1);
1934d9d31abSKris Buschelman     break;
1944d9d31abSKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
1954e0d8c25SBarry Smith     a->nonew = (flg ? -1 : 0);
1964d9d31abSKris Buschelman     break;
1974d9d31abSKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1984e0d8c25SBarry Smith     a->nonew = (flg ? -2 : 0);
1994d9d31abSKris Buschelman     break;
20028b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
20128b2fa4aSMatthew Knepley     a->nounused = (flg ? -1 : 0);
20228b2fa4aSMatthew Knepley     break;
2034e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
2044d9d31abSKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
2054d9d31abSKris Buschelman   case MAT_USE_HASH_TABLE:
206290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
2074d9d31abSKris Buschelman     break;
2089a4540c5SBarry Smith   case MAT_HERMITIAN:
209e32f2f54SBarry Smith     if (!A->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call MatAssemblyEnd() first");
2100bc54ff2SBarry Smith     if (A->cmap->n < 65536 && A->cmap->bs == 1) {
211eeffb40dSHong Zhang       A->ops->mult = MatMult_SeqSBAIJ_1_Hermitian_ushort;
2120bc54ff2SBarry Smith     } else if (A->cmap->bs == 1) {
213eeffb40dSHong Zhang       A->ops->mult = MatMult_SeqSBAIJ_1_Hermitian;
214e32f2f54SBarry Smith     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for Hermitian with block size greater than 1");
215eeffb40dSHong Zhang     break;
2163d472b54SHong Zhang   case MAT_SPD:
2175021d80fSJed Brown     /* These options are handled directly by MatSetOption() */
2183d472b54SHong Zhang     break;
21977e54ba9SKris Buschelman   case MAT_SYMMETRIC:
22077e54ba9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
2219a4540c5SBarry Smith   case MAT_SYMMETRY_ETERNAL:
2224dcd73b1SHong Zhang     /* These options are handled directly by MatSetOption() */
223290bbb0aSBarry Smith     break;
224941593c8SHong Zhang   case MAT_IGNORE_LOWER_TRIANGULAR:
2254e0d8c25SBarry Smith     a->ignore_ltriangular = flg;
226941593c8SHong Zhang     break;
227941593c8SHong Zhang   case MAT_ERROR_LOWER_TRIANGULAR:
2284e0d8c25SBarry Smith     a->ignore_ltriangular = flg;
22977e54ba9SKris Buschelman     break;
230f5edf698SHong Zhang   case MAT_GETROW_UPPERTRIANGULAR:
2314e0d8c25SBarry Smith     a->getrow_utriangular = flg;
232f5edf698SHong Zhang     break;
2334d9d31abSKris Buschelman   default:
234e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
23549b5e25fSSatish Balay   }
23649b5e25fSSatish Balay   PetscFunctionReturn(0);
23749b5e25fSSatish Balay }
23849b5e25fSSatish Balay 
2394a2ae208SSatish Balay #undef __FUNCT__
2404a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqSBAIJ"
24152768537SHong Zhang PetscErrorCode MatGetRow_SeqSBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
24249b5e25fSSatish Balay {
24349b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
2446849ba73SBarry Smith   PetscErrorCode ierr;
24549b5e25fSSatish Balay 
24649b5e25fSSatish Balay   PetscFunctionBegin;
247e32f2f54SBarry 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()");
24852768537SHong Zhang 
249f5edf698SHong Zhang   /* Get the upper triangular part of the row */
25052768537SHong Zhang   ierr = MatGetRow_SeqBAIJ_private(A,row,nz,idx,v,a->i,a->j,a->a);CHKERRQ(ierr);
25149b5e25fSSatish Balay   PetscFunctionReturn(0);
25249b5e25fSSatish Balay }
25349b5e25fSSatish Balay 
2544a2ae208SSatish Balay #undef __FUNCT__
2554a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqSBAIJ"
25613f74950SBarry Smith PetscErrorCode MatRestoreRow_SeqSBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
25749b5e25fSSatish Balay {
258dfbe8321SBarry Smith   PetscErrorCode ierr;
25949b5e25fSSatish Balay 
26049b5e25fSSatish Balay   PetscFunctionBegin;
26105b42c5fSBarry Smith   if (idx) {ierr = PetscFree(*idx);CHKERRQ(ierr);}
26205b42c5fSBarry Smith   if (v)   {ierr = PetscFree(*v);CHKERRQ(ierr);}
26349b5e25fSSatish Balay   PetscFunctionReturn(0);
26449b5e25fSSatish Balay }
26549b5e25fSSatish Balay 
2664a2ae208SSatish Balay #undef __FUNCT__
267f5edf698SHong Zhang #define __FUNCT__ "MatGetRowUpperTriangular_SeqSBAIJ"
268f5edf698SHong Zhang PetscErrorCode MatGetRowUpperTriangular_SeqSBAIJ(Mat A)
269f5edf698SHong Zhang {
270f5edf698SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
271f5edf698SHong Zhang 
272f5edf698SHong Zhang   PetscFunctionBegin;
273f5edf698SHong Zhang   a->getrow_utriangular = PETSC_TRUE;
274f5edf698SHong Zhang   PetscFunctionReturn(0);
275f5edf698SHong Zhang }
276f5edf698SHong Zhang #undef __FUNCT__
277f5edf698SHong Zhang #define __FUNCT__ "MatRestoreRowUpperTriangular_SeqSBAIJ"
278f5edf698SHong Zhang PetscErrorCode MatRestoreRowUpperTriangular_SeqSBAIJ(Mat A)
279f5edf698SHong Zhang {
280f5edf698SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
281f5edf698SHong Zhang 
282f5edf698SHong Zhang   PetscFunctionBegin;
283f5edf698SHong Zhang   a->getrow_utriangular = PETSC_FALSE;
284f5edf698SHong Zhang   PetscFunctionReturn(0);
285f5edf698SHong Zhang }
286f5edf698SHong Zhang 
287f5edf698SHong Zhang #undef __FUNCT__
2884a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqSBAIJ"
289fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqSBAIJ(Mat A,MatReuse reuse,Mat *B)
29049b5e25fSSatish Balay {
291dfbe8321SBarry Smith   PetscErrorCode ierr;
2925fd66863SKarl Rupp 
29349b5e25fSSatish Balay   PetscFunctionBegin;
294815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
295999d9058SBarry Smith     ierr = MatDuplicate(A,MAT_COPY_VALUES,B);CHKERRQ(ierr);
296fc4dec0aSBarry Smith   }
2978115998fSBarry Smith   PetscFunctionReturn(0);
29849b5e25fSSatish Balay }
29949b5e25fSSatish Balay 
3004a2ae208SSatish Balay #undef __FUNCT__
3014a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_ASCII"
3027da1fb6eSBarry Smith PetscErrorCode MatView_SeqSBAIJ_ASCII(Mat A,PetscViewer viewer)
30349b5e25fSSatish Balay {
30449b5e25fSSatish Balay   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
305dfbe8321SBarry Smith   PetscErrorCode    ierr;
306d0f46423SBarry Smith   PetscInt          i,j,bs = A->rmap->bs,k,l,bs2=a->bs2;
307f3ef73ceSBarry Smith   PetscViewerFormat format;
308121deb67SSatish Balay   PetscInt          *diag;
30949b5e25fSSatish Balay 
31049b5e25fSSatish Balay   PetscFunctionBegin;
311b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
312456192e2SBarry Smith   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
31377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);CHKERRQ(ierr);
314fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
315d2507d54SMatthew Knepley     Mat        aij;
316ade3a672SBarry Smith     const char *matname;
317ade3a672SBarry Smith 
318d5f3da31SBarry Smith     if (A->factortype && bs>1) {
31970d5e725SHong 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);
32070d5e725SHong Zhang       PetscFunctionReturn(0);
32170d5e725SHong Zhang     }
322c9f458caSMatthew Knepley     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&aij);CHKERRQ(ierr);
323ade3a672SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&matname);CHKERRQ(ierr);
324ade3a672SBarry Smith     ierr = PetscObjectSetName((PetscObject)aij,matname);CHKERRQ(ierr);
325c9f458caSMatthew Knepley     ierr = MatView(aij,viewer);CHKERRQ(ierr);
3266bf464f9SBarry Smith     ierr = MatDestroy(&aij);CHKERRQ(ierr);
327fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
328d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
32949b5e25fSSatish Balay     for (i=0; i<a->mbs; i++) {
33049b5e25fSSatish Balay       for (j=0; j<bs; j++) {
33177431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
33249b5e25fSSatish Balay         for (k=a->i[i]; k<a->i[i+1]; k++) {
33349b5e25fSSatish Balay           for (l=0; l<bs; l++) {
33449b5e25fSSatish Balay #if defined(PETSC_USE_COMPLEX)
33549b5e25fSSatish Balay             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
33657622a8eSBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i) ",bs*a->j[k]+l,
33757622a8eSBarry Smith                                             (double)PetscRealPart(a->a[bs2*k + l*bs + j]),(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
33849b5e25fSSatish Balay             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
33957622a8eSBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i) ",bs*a->j[k]+l,
34057622a8eSBarry Smith                                             (double)PetscRealPart(a->a[bs2*k + l*bs + j]),-(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
34149b5e25fSSatish Balay             } else if (PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
34257622a8eSBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
34349b5e25fSSatish Balay             }
34449b5e25fSSatish Balay #else
34549b5e25fSSatish Balay             if (a->a[bs2*k + l*bs + j] != 0.0) {
34657622a8eSBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
34749b5e25fSSatish Balay             }
34849b5e25fSSatish Balay #endif
34949b5e25fSSatish Balay           }
35049b5e25fSSatish Balay         }
351b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
35249b5e25fSSatish Balay       }
35349b5e25fSSatish Balay     }
354d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
355c1490034SHong Zhang   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
356c1490034SHong Zhang     PetscFunctionReturn(0);
35749b5e25fSSatish Balay   } else {
358d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
3592c990fa1SHong Zhang     if (A->factortype) { /* for factored matrix */
3602c990fa1SHong Zhang       if (bs>1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"matrix is factored with bs>1. Not implemented yet");
3612c990fa1SHong Zhang 
362121deb67SSatish Balay       diag=a->diag;
363121deb67SSatish Balay       for (i=0; i<a->mbs; i++) { /* for row block i */
3642c990fa1SHong Zhang         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
3652c990fa1SHong Zhang         /* diagonal entry */
3662c990fa1SHong Zhang #if defined(PETSC_USE_COMPLEX)
3672c990fa1SHong Zhang         if (PetscImaginaryPart(a->a[diag[i]]) > 0.0) {
36857622a8eSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i) ",a->j[diag[i]],(double)PetscRealPart(1.0/a->a[diag[i]]),(double)PetscImaginaryPart(1.0/a->a[diag[i]]));CHKERRQ(ierr);
3692c990fa1SHong Zhang         } else if (PetscImaginaryPart(a->a[diag[i]]) < 0.0) {
37057622a8eSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i) ",a->j[diag[i]],(double)PetscRealPart(1.0/a->a[diag[i]]),-(double)PetscImaginaryPart(1.0/a->a[diag[i]]));CHKERRQ(ierr);
3712c990fa1SHong Zhang         } else {
37257622a8eSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[diag[i]],(double)PetscRealPart(1.0/a->a[diag[i]]));CHKERRQ(ierr);
3732c990fa1SHong Zhang         }
3742c990fa1SHong Zhang #else
3756712e2f1SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[diag[i]],(double)(1.0/a->a[diag[i]]));CHKERRQ(ierr);
3762c990fa1SHong Zhang #endif
3772c990fa1SHong Zhang         /* off-diagonal entries */
3782c990fa1SHong Zhang         for (k=a->i[i]; k<a->i[i+1]-1; k++) {
3792c990fa1SHong Zhang #if defined(PETSC_USE_COMPLEX)
380ca0704adSBarry Smith           if (PetscImaginaryPart(a->a[k]) > 0.0) {
38157622a8eSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i) ",bs*a->j[k],(double)PetscRealPart(a->a[k]),(double)PetscImaginaryPart(a->a[k]));CHKERRQ(ierr);
382ca0704adSBarry Smith           } else if (PetscImaginaryPart(a->a[k]) < 0.0) {
38357622a8eSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i) ",bs*a->j[k],(double)PetscRealPart(a->a[k]),-(double)PetscImaginaryPart(a->a[k]));CHKERRQ(ierr);
3842c990fa1SHong Zhang           } else {
38557622a8eSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k],(double)PetscRealPart(a->a[k]));CHKERRQ(ierr);
3862c990fa1SHong Zhang           }
3872c990fa1SHong Zhang #else
38857622a8eSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[k],(double)a->a[k]);CHKERRQ(ierr);
3892c990fa1SHong Zhang #endif
3902c990fa1SHong Zhang         }
3912c990fa1SHong Zhang         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
3922c990fa1SHong Zhang       }
3932c990fa1SHong Zhang 
3942c990fa1SHong Zhang     } else { /* for non-factored matrix */
3950c74a584SJed Brown       for (i=0; i<a->mbs; i++) { /* for row block i */
3960c74a584SJed Brown         for (j=0; j<bs; j++) {   /* for row bs*i + j */
39777431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
3980c74a584SJed Brown           for (k=a->i[i]; k<a->i[i+1]; k++) { /* for column block */
3990c74a584SJed Brown             for (l=0; l<bs; l++) {            /* for column */
40049b5e25fSSatish Balay #if defined(PETSC_USE_COMPLEX)
40149b5e25fSSatish Balay               if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0) {
40257622a8eSBarry Smith                 ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i) ",bs*a->j[k]+l,
40357622a8eSBarry Smith                                               (double)PetscRealPart(a->a[bs2*k + l*bs + j]),(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
40449b5e25fSSatish Balay               } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0) {
40557622a8eSBarry Smith                 ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i) ",bs*a->j[k]+l,
40657622a8eSBarry Smith                                               (double)PetscRealPart(a->a[bs2*k + l*bs + j]),-(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
40749b5e25fSSatish Balay               } else {
40857622a8eSBarry Smith                 ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
40949b5e25fSSatish Balay               }
41049b5e25fSSatish Balay #else
41157622a8eSBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
41249b5e25fSSatish Balay #endif
41349b5e25fSSatish Balay             }
41449b5e25fSSatish Balay           }
415b0a32e0cSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
41649b5e25fSSatish Balay         }
41749b5e25fSSatish Balay       }
4182c990fa1SHong Zhang     }
419d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
42049b5e25fSSatish Balay   }
421b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
42249b5e25fSSatish Balay   PetscFunctionReturn(0);
42349b5e25fSSatish Balay }
42449b5e25fSSatish Balay 
4259804daf3SBarry Smith #include <petscdraw.h>
4264a2ae208SSatish Balay #undef __FUNCT__
4274a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_Draw_Zoom"
4286849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
42949b5e25fSSatish Balay {
43049b5e25fSSatish Balay   Mat            A = (Mat) Aa;
43149b5e25fSSatish Balay   Mat_SeqSBAIJ   *a=(Mat_SeqSBAIJ*)A->data;
4326849ba73SBarry Smith   PetscErrorCode ierr;
433d0f46423SBarry Smith   PetscInt       row,i,j,k,l,mbs=a->mbs,color,bs=A->rmap->bs,bs2=a->bs2;
43413f74950SBarry Smith   PetscMPIInt    rank;
43549b5e25fSSatish Balay   PetscReal      xl,yl,xr,yr,x_l,x_r,y_l,y_r;
43649b5e25fSSatish Balay   MatScalar      *aa;
43749b5e25fSSatish Balay   MPI_Comm       comm;
438b0a32e0cSBarry Smith   PetscViewer    viewer;
43949b5e25fSSatish Balay 
44049b5e25fSSatish Balay   PetscFunctionBegin;
44149b5e25fSSatish Balay   /*
44249b5e25fSSatish Balay     This is nasty. If this is called from an originally parallel matrix
44349b5e25fSSatish Balay     then all processes call this,but only the first has the matrix so the
44449b5e25fSSatish Balay     rest should return immediately.
44549b5e25fSSatish Balay   */
44649b5e25fSSatish Balay   ierr = PetscObjectGetComm((PetscObject)draw,&comm);CHKERRQ(ierr);
44749b5e25fSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
44849b5e25fSSatish Balay   if (rank) PetscFunctionReturn(0);
44949b5e25fSSatish Balay 
45049b5e25fSSatish Balay   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
45149b5e25fSSatish Balay 
452b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
453b0a32e0cSBarry Smith   PetscDrawString(draw, .3*(xl+xr), .3*(yl+yr), PETSC_DRAW_BLACK, "symmetric");
45449b5e25fSSatish Balay 
45549b5e25fSSatish Balay   /* loop over matrix elements drawing boxes */
456b0a32e0cSBarry Smith   color = PETSC_DRAW_BLUE;
45749b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
45849b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
459d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
46049b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
46149b5e25fSSatish Balay       aa  = a->a + j*bs2;
46249b5e25fSSatish Balay       for (k=0; k<bs; k++) {
46349b5e25fSSatish Balay         for (l=0; l<bs; l++) {
46449b5e25fSSatish Balay           if (PetscRealPart(*aa++) >=  0.) continue;
465b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
46649b5e25fSSatish Balay         }
46749b5e25fSSatish Balay       }
46849b5e25fSSatish Balay     }
46949b5e25fSSatish Balay   }
470b0a32e0cSBarry Smith   color = PETSC_DRAW_CYAN;
47149b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
47249b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
473d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
47449b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
47549b5e25fSSatish Balay       aa = a->a + j*bs2;
47649b5e25fSSatish Balay       for (k=0; k<bs; k++) {
47749b5e25fSSatish Balay         for (l=0; l<bs; l++) {
47849b5e25fSSatish Balay           if (PetscRealPart(*aa++) != 0.) continue;
479b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
48049b5e25fSSatish Balay         }
48149b5e25fSSatish Balay       }
48249b5e25fSSatish Balay     }
48349b5e25fSSatish Balay   }
48449b5e25fSSatish Balay 
485b0a32e0cSBarry Smith   color = PETSC_DRAW_RED;
48649b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
48749b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
488d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
48949b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
49049b5e25fSSatish Balay       aa = a->a + j*bs2;
49149b5e25fSSatish Balay       for (k=0; k<bs; k++) {
49249b5e25fSSatish Balay         for (l=0; l<bs; l++) {
49349b5e25fSSatish Balay           if (PetscRealPart(*aa++) <= 0.) continue;
494b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
49549b5e25fSSatish Balay         }
49649b5e25fSSatish Balay       }
49749b5e25fSSatish Balay     }
49849b5e25fSSatish Balay   }
49949b5e25fSSatish Balay   PetscFunctionReturn(0);
50049b5e25fSSatish Balay }
50149b5e25fSSatish Balay 
5024a2ae208SSatish Balay #undef __FUNCT__
5034a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_Draw"
5046849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_Draw(Mat A,PetscViewer viewer)
50549b5e25fSSatish Balay {
506dfbe8321SBarry Smith   PetscErrorCode ierr;
50749b5e25fSSatish Balay   PetscReal      xl,yl,xr,yr,w,h;
508b0a32e0cSBarry Smith   PetscDraw      draw;
509ace3abfcSBarry Smith   PetscBool      isnull;
51049b5e25fSSatish Balay 
51149b5e25fSSatish Balay   PetscFunctionBegin;
512b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
513b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
51449b5e25fSSatish Balay 
51549b5e25fSSatish Balay   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
516d0f46423SBarry Smith   xr   = A->rmap->N; yr = A->rmap->N; h = yr/10.0; w = xr/10.0;
51749b5e25fSSatish Balay   xr  += w;    yr += h;  xl = -w;     yl = -h;
518b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
519b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqSBAIJ_Draw_Zoom,A);CHKERRQ(ierr);
5200298fd71SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr);
52149b5e25fSSatish Balay   PetscFunctionReturn(0);
52249b5e25fSSatish Balay }
52349b5e25fSSatish Balay 
5244a2ae208SSatish Balay #undef __FUNCT__
5254a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ"
526dfbe8321SBarry Smith PetscErrorCode MatView_SeqSBAIJ(Mat A,PetscViewer viewer)
52749b5e25fSSatish Balay {
528dfbe8321SBarry Smith   PetscErrorCode ierr;
529ace3abfcSBarry Smith   PetscBool      iascii,isdraw;
53008917f38SBarry Smith   FILE           *file = 0;
53149b5e25fSSatish Balay 
53249b5e25fSSatish Balay   PetscFunctionBegin;
533251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
534251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
53532077d6dSBarry Smith   if (iascii) {
53649b5e25fSSatish Balay     ierr = MatView_SeqSBAIJ_ASCII(A,viewer);CHKERRQ(ierr);
53749b5e25fSSatish Balay   } else if (isdraw) {
53849b5e25fSSatish Balay     ierr = MatView_SeqSBAIJ_Draw(A,viewer);CHKERRQ(ierr);
53949b5e25fSSatish Balay   } else {
540a5e6ed63SBarry Smith     Mat        B;
541ade3a672SBarry Smith     const char *matname;
542ceb03754SKris Buschelman     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&B);CHKERRQ(ierr);
543ade3a672SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&matname);CHKERRQ(ierr);
544ade3a672SBarry Smith     ierr = PetscObjectSetName((PetscObject)B,matname);CHKERRQ(ierr);
545a5e6ed63SBarry Smith     ierr = MatView(B,viewer);CHKERRQ(ierr);
5466bf464f9SBarry Smith     ierr = MatDestroy(&B);CHKERRQ(ierr);
54708917f38SBarry Smith     ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
54808917f38SBarry Smith     if (file) {
54908917f38SBarry Smith       fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
55008917f38SBarry Smith     }
55149b5e25fSSatish Balay   }
55249b5e25fSSatish Balay   PetscFunctionReturn(0);
55349b5e25fSSatish Balay }
55449b5e25fSSatish Balay 
55549b5e25fSSatish Balay 
5564a2ae208SSatish Balay #undef __FUNCT__
5574a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqSBAIJ"
55813f74950SBarry Smith PetscErrorCode MatGetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
55949b5e25fSSatish Balay {
560045c9aa0SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
56113f74950SBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
56213f74950SBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
563d0f46423SBarry Smith   PetscInt     brow,bcol,ridx,cidx,bs=A->rmap->bs,bs2=a->bs2;
56497e567efSBarry Smith   MatScalar    *ap,*aa = a->a;
56549b5e25fSSatish Balay 
56649b5e25fSSatish Balay   PetscFunctionBegin;
56749b5e25fSSatish Balay   for (k=0; k<m; k++) { /* loop over rows */
56849b5e25fSSatish Balay     row = im[k]; brow = row/bs;
569e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
570e32f2f54SBarry Smith     if (row >= A->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->N-1);
57149b5e25fSSatish Balay     rp   = aj + ai[brow]; ap = aa + bs2*ai[brow];
57249b5e25fSSatish Balay     nrow = ailen[brow];
57349b5e25fSSatish Balay     for (l=0; l<n; l++) { /* loop over columns */
574e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
575e32f2f54SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
57649b5e25fSSatish Balay       col  = in[l];
57749b5e25fSSatish Balay       bcol = col/bs;
57849b5e25fSSatish Balay       cidx = col%bs;
57949b5e25fSSatish Balay       ridx = row%bs;
58049b5e25fSSatish Balay       high = nrow;
58149b5e25fSSatish Balay       low  = 0; /* assume unsorted */
58249b5e25fSSatish Balay       while (high-low > 5) {
58349b5e25fSSatish Balay         t = (low+high)/2;
58449b5e25fSSatish Balay         if (rp[t] > bcol) high = t;
58549b5e25fSSatish Balay         else              low  = t;
58649b5e25fSSatish Balay       }
58749b5e25fSSatish Balay       for (i=low; i<high; i++) {
58849b5e25fSSatish Balay         if (rp[i] > bcol) break;
58949b5e25fSSatish Balay         if (rp[i] == bcol) {
59049b5e25fSSatish Balay           *v++ = ap[bs2*i+bs*cidx+ridx];
59149b5e25fSSatish Balay           goto finished;
59249b5e25fSSatish Balay         }
59349b5e25fSSatish Balay       }
59497e567efSBarry Smith       *v++ = 0.0;
59549b5e25fSSatish Balay finished:;
59649b5e25fSSatish Balay     }
59749b5e25fSSatish Balay   }
59849b5e25fSSatish Balay   PetscFunctionReturn(0);
59949b5e25fSSatish Balay }
60049b5e25fSSatish Balay 
60149b5e25fSSatish Balay 
6024a2ae208SSatish Balay #undef __FUNCT__
6034a2ae208SSatish Balay #define __FUNCT__ "MatSetValuesBlocked_SeqSBAIJ"
60413f74950SBarry Smith PetscErrorCode MatSetValuesBlocked_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
60549b5e25fSSatish Balay {
6060880e062SHong Zhang   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
6076849ba73SBarry Smith   PetscErrorCode    ierr;
608e2ee6c50SBarry Smith   PetscInt          *rp,k,low,high,t,ii,jj,row,nrow,i,col,l,rmax,N,lastcol = -1;
60913f74950SBarry Smith   PetscInt          *imax      =a->imax,*ai=a->i,*ailen=a->ilen;
610d0f46423SBarry Smith   PetscInt          *aj        =a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs,stepval;
611ace3abfcSBarry Smith   PetscBool         roworiented=a->roworiented;
612dd6ea824SBarry Smith   const PetscScalar *value     = v;
613f15d580aSBarry Smith   MatScalar         *ap,*aa = a->a,*bap;
6140880e062SHong Zhang 
61549b5e25fSSatish Balay   PetscFunctionBegin;
61626fbe8dcSKarl Rupp   if (roworiented) stepval = (n-1)*bs;
61726fbe8dcSKarl Rupp   else stepval = (m-1)*bs;
61826fbe8dcSKarl Rupp 
6190880e062SHong Zhang   for (k=0; k<m; k++) { /* loop over added rows */
6200880e062SHong Zhang     row = im[k];
6210880e062SHong Zhang     if (row < 0) continue;
6222515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
623e32f2f54SBarry Smith     if (row >= a->mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,a->mbs-1);
6240880e062SHong Zhang #endif
6250880e062SHong Zhang     rp   = aj + ai[row];
6260880e062SHong Zhang     ap   = aa + bs2*ai[row];
6270880e062SHong Zhang     rmax = imax[row];
6280880e062SHong Zhang     nrow = ailen[row];
6290880e062SHong Zhang     low  = 0;
630818f2c47SBarry Smith     high = nrow;
6310880e062SHong Zhang     for (l=0; l<n; l++) { /* loop over added columns */
6320880e062SHong Zhang       if (in[l] < 0) continue;
6330880e062SHong Zhang       col = in[l];
6342515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
635e32f2f54SBarry Smith       if (col >= a->nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",col,a->nbs-1);
636b1823623SSatish Balay #endif
637b98bf0e1SJed Brown       if (col < row) {
63826fbe8dcSKarl Rupp         if (a->ignore_ltriangular) continue; /* ignore lower triangular block */
63926fbe8dcSKarl Rupp         else 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)");
640b98bf0e1SJed Brown       }
64126fbe8dcSKarl Rupp       if (roworiented) value = v + k*(stepval+bs)*bs + l*bs;
64226fbe8dcSKarl Rupp       else value = v + l*(stepval+bs)*bs + k*bs;
64326fbe8dcSKarl Rupp 
64426fbe8dcSKarl Rupp       if (col <= lastcol) low = 0;
64526fbe8dcSKarl Rupp       else high = nrow;
64626fbe8dcSKarl Rupp 
647e2ee6c50SBarry Smith       lastcol = col;
6480880e062SHong Zhang       while (high-low > 7) {
6490880e062SHong Zhang         t = (low+high)/2;
6500880e062SHong Zhang         if (rp[t] > col) high = t;
6510880e062SHong Zhang         else             low  = t;
6520880e062SHong Zhang       }
6530880e062SHong Zhang       for (i=low; i<high; i++) {
6540880e062SHong Zhang         if (rp[i] > col) break;
6550880e062SHong Zhang         if (rp[i] == col) {
6560880e062SHong Zhang           bap = ap +  bs2*i;
6570880e062SHong Zhang           if (roworiented) {
6580880e062SHong Zhang             if (is == ADD_VALUES) {
6590880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6600880e062SHong Zhang                 for (jj=ii; jj<bs2; jj+=bs) {
6610880e062SHong Zhang                   bap[jj] += *value++;
6620880e062SHong Zhang                 }
6630880e062SHong Zhang               }
6640880e062SHong Zhang             } else {
6650880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6660880e062SHong Zhang                 for (jj=ii; jj<bs2; jj+=bs) {
6670880e062SHong Zhang                   bap[jj] = *value++;
6680880e062SHong Zhang                 }
6690880e062SHong Zhang                }
6700880e062SHong Zhang             }
6710880e062SHong Zhang           } else {
6720880e062SHong Zhang             if (is == ADD_VALUES) {
6730880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6740880e062SHong Zhang                 for (jj=0; jj<bs; jj++) {
6750880e062SHong Zhang                   *bap++ += *value++;
6760880e062SHong Zhang                 }
6770880e062SHong Zhang               }
6780880e062SHong Zhang             } else {
6790880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6800880e062SHong Zhang                 for (jj=0; jj<bs; jj++) {
6810880e062SHong Zhang                   *bap++  = *value++;
6820880e062SHong Zhang                 }
6830880e062SHong Zhang               }
6840880e062SHong Zhang             }
6850880e062SHong Zhang           }
6860880e062SHong Zhang           goto noinsert2;
6870880e062SHong Zhang         }
6880880e062SHong Zhang       }
6890880e062SHong Zhang       if (nonew == 1) goto noinsert2;
690e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
691fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
692c03d1d03SSatish Balay       N = nrow++ - 1; high++;
6930880e062SHong Zhang       /* shift up all the later entries in this row */
6940880e062SHong Zhang       for (ii=N; ii>=i; ii--) {
6950880e062SHong Zhang         rp[ii+1] = rp[ii];
6960880e062SHong Zhang         ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
6970880e062SHong Zhang       }
6980880e062SHong Zhang       if (N >= i) {
6990880e062SHong Zhang         ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
7000880e062SHong Zhang       }
7010880e062SHong Zhang       rp[i] = col;
7020880e062SHong Zhang       bap   = ap +  bs2*i;
7030880e062SHong Zhang       if (roworiented) {
7040880e062SHong Zhang         for (ii=0; ii<bs; ii++,value+=stepval) {
7050880e062SHong Zhang           for (jj=ii; jj<bs2; jj+=bs) {
7060880e062SHong Zhang             bap[jj] = *value++;
7070880e062SHong Zhang           }
7080880e062SHong Zhang         }
7090880e062SHong Zhang       } else {
7100880e062SHong Zhang         for (ii=0; ii<bs; ii++,value+=stepval) {
7110880e062SHong Zhang           for (jj=0; jj<bs; jj++) {
7120880e062SHong Zhang             *bap++ = *value++;
7130880e062SHong Zhang           }
7140880e062SHong Zhang         }
7150880e062SHong Zhang        }
7160880e062SHong Zhang     noinsert2:;
7170880e062SHong Zhang       low = i;
7180880e062SHong Zhang     }
7190880e062SHong Zhang     ailen[row] = nrow;
7200880e062SHong Zhang   }
7210880e062SHong Zhang   PetscFunctionReturn(0);
72249b5e25fSSatish Balay }
72349b5e25fSSatish Balay 
72464831d72SBarry Smith /*
72564831d72SBarry Smith     This is not yet used
72664831d72SBarry Smith */
7274a2ae208SSatish Balay #undef __FUNCT__
7284108e4d5SBarry Smith #define __FUNCT__ "MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode"
7294108e4d5SBarry Smith PetscErrorCode MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode(Mat A)
7300def2e27SBarry Smith {
7310def2e27SBarry Smith   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
7320def2e27SBarry Smith   PetscErrorCode ierr;
7330def2e27SBarry Smith   const PetscInt *ai = a->i, *aj = a->j,*cols;
7340def2e27SBarry Smith   PetscInt       i   = 0,j,blk_size,m = A->rmap->n,node_count = 0,nzx,nzy,*ns,row,nz,cnt,cnt2,*counts;
735ace3abfcSBarry Smith   PetscBool      flag;
7360def2e27SBarry Smith 
7370def2e27SBarry Smith   PetscFunctionBegin;
738785e854fSJed Brown   ierr = PetscMalloc1(m,&ns);CHKERRQ(ierr);
7390def2e27SBarry Smith   while (i < m) {
7400def2e27SBarry Smith     nzx = ai[i+1] - ai[i];       /* Number of nonzeros */
7410def2e27SBarry Smith     /* Limits the number of elements in a node to 'a->inode.limit' */
7420def2e27SBarry Smith     for (j=i+1,blk_size=1; j<m && blk_size <a->inode.limit; ++j,++blk_size) {
7430def2e27SBarry Smith       nzy = ai[j+1] - ai[j];
7440def2e27SBarry Smith       if (nzy != (nzx - j + i)) break;
7450def2e27SBarry Smith       ierr = PetscMemcmp(aj + ai[i] + j - i,aj + ai[j],nzy*sizeof(PetscInt),&flag);CHKERRQ(ierr);
7460def2e27SBarry Smith       if (!flag) break;
7470def2e27SBarry Smith     }
7480def2e27SBarry Smith     ns[node_count++] = blk_size;
74926fbe8dcSKarl Rupp 
7500def2e27SBarry Smith     i = j;
7510def2e27SBarry Smith   }
7520def2e27SBarry Smith   if (!a->inode.size && m && node_count > .9*m) {
7530def2e27SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
7540def2e27SBarry Smith     ierr = PetscInfo2(A,"Found %D nodes out of %D rows. Not using Inode routines\n",node_count,m);CHKERRQ(ierr);
7550def2e27SBarry Smith   } else {
7560def2e27SBarry Smith     a->inode.node_count = node_count;
75726fbe8dcSKarl Rupp 
758785e854fSJed Brown     ierr = PetscMalloc1(node_count,&a->inode.size);CHKERRQ(ierr);
7593bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A,node_count*sizeof(PetscInt));CHKERRQ(ierr);
76022d28d08SBarry Smith     ierr = PetscMemcpy(a->inode.size,ns,node_count*sizeof(PetscInt));CHKERRQ(ierr);
7610def2e27SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
7620def2e27SBarry Smith     ierr = PetscInfo3(A,"Found %D nodes of %D. Limit used: %D. Using Inode routines\n",node_count,m,a->inode.limit);CHKERRQ(ierr);
7630def2e27SBarry Smith 
7640def2e27SBarry Smith     /* count collections of adjacent columns in each inode */
7650def2e27SBarry Smith     row = 0;
7660def2e27SBarry Smith     cnt = 0;
7670def2e27SBarry Smith     for (i=0; i<node_count; i++) {
7680def2e27SBarry Smith       cols = aj + ai[row] + a->inode.size[i];
7690def2e27SBarry Smith       nz   = ai[row+1] - ai[row] - a->inode.size[i];
7700def2e27SBarry Smith       for (j=1; j<nz; j++) {
77126fbe8dcSKarl Rupp         if (cols[j] != cols[j-1]+1) cnt++;
7720def2e27SBarry Smith       }
7730def2e27SBarry Smith       cnt++;
7740def2e27SBarry Smith       row += a->inode.size[i];
7750def2e27SBarry Smith     }
776785e854fSJed Brown     ierr = PetscMalloc1(2*cnt,&counts);CHKERRQ(ierr);
7770def2e27SBarry Smith     cnt  = 0;
7780def2e27SBarry Smith     row  = 0;
7790def2e27SBarry Smith     for (i=0; i<node_count; i++) {
7800def2e27SBarry Smith       cols = aj + ai[row] + a->inode.size[i];
7810def2e27SBarry Smith       counts[2*cnt] = cols[0];
7820def2e27SBarry Smith       nz   = ai[row+1] - ai[row] - a->inode.size[i];
7830def2e27SBarry Smith       cnt2 = 1;
7840def2e27SBarry Smith       for (j=1; j<nz; j++) {
7850def2e27SBarry Smith         if (cols[j] != cols[j-1]+1) {
7860def2e27SBarry Smith           counts[2*(cnt++)+1] = cnt2;
7870def2e27SBarry Smith           counts[2*cnt]       = cols[j];
7880def2e27SBarry Smith           cnt2 = 1;
7890def2e27SBarry Smith         } else cnt2++;
7900def2e27SBarry Smith       }
7910def2e27SBarry Smith       counts[2*(cnt++)+1] = cnt2;
7920def2e27SBarry Smith       row += a->inode.size[i];
7930def2e27SBarry Smith     }
79422d28d08SBarry Smith     ierr = PetscIntView(2*cnt,counts,0);CHKERRQ(ierr);
7950def2e27SBarry Smith   }
79638702af4SBarry Smith   PetscFunctionReturn(0);
79738702af4SBarry Smith }
79838702af4SBarry Smith 
79938702af4SBarry Smith #undef __FUNCT__
8004a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqSBAIJ"
801dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqSBAIJ(Mat A,MatAssemblyType mode)
80249b5e25fSSatish Balay {
80349b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
8046849ba73SBarry Smith   PetscErrorCode ierr;
80513f74950SBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
806d0f46423SBarry Smith   PetscInt       m      = A->rmap->N,*ip,N,*ailen = a->ilen;
80713f74950SBarry Smith   PetscInt       mbs    = a->mbs,bs2 = a->bs2,rmax = 0;
80849b5e25fSSatish Balay   MatScalar      *aa    = a->a,*ap;
80949b5e25fSSatish Balay 
81049b5e25fSSatish Balay   PetscFunctionBegin;
81149b5e25fSSatish Balay   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
81249b5e25fSSatish Balay 
81349b5e25fSSatish Balay   if (m) rmax = ailen[0];
81449b5e25fSSatish Balay   for (i=1; i<mbs; i++) {
81549b5e25fSSatish Balay     /* move each row back by the amount of empty slots (fshift) before it*/
81649b5e25fSSatish Balay     fshift += imax[i-1] - ailen[i-1];
81749b5e25fSSatish Balay     rmax    = PetscMax(rmax,ailen[i]);
81849b5e25fSSatish Balay     if (fshift) {
81949b5e25fSSatish Balay       ip = aj + ai[i]; ap = aa + bs2*ai[i];
82049b5e25fSSatish Balay       N  = ailen[i];
82149b5e25fSSatish Balay       for (j=0; j<N; j++) {
82249b5e25fSSatish Balay         ip[j-fshift] = ip[j];
82349b5e25fSSatish Balay         ierr         = PetscMemcpy(ap+(j-fshift)*bs2,ap+j*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
82449b5e25fSSatish Balay       }
82549b5e25fSSatish Balay     }
82649b5e25fSSatish Balay     ai[i] = ai[i-1] + ailen[i-1];
82749b5e25fSSatish Balay   }
82849b5e25fSSatish Balay   if (mbs) {
82949b5e25fSSatish Balay     fshift += imax[mbs-1] - ailen[mbs-1];
83049b5e25fSSatish Balay     ai[mbs] = ai[mbs-1] + ailen[mbs-1];
83149b5e25fSSatish Balay   }
83249b5e25fSSatish Balay   /* reset ilen and imax for each row */
83349b5e25fSSatish Balay   for (i=0; i<mbs; i++) {
83449b5e25fSSatish Balay     ailen[i] = imax[i] = ai[i+1] - ai[i];
83549b5e25fSSatish Balay   }
8366c6c5352SBarry Smith   a->nz = ai[mbs];
83749b5e25fSSatish Balay 
838b424e231SHong Zhang   /* diagonals may have moved, reset it */
839b424e231SHong Zhang   if (a->diag) {
8402ed38d0bSJed Brown     ierr = PetscMemcpy(a->diag,ai,mbs*sizeof(PetscInt));CHKERRQ(ierr);
84149b5e25fSSatish Balay   }
84226fbe8dcSKarl Rupp   if (fshift && a->nounused == -1) 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);
84326fbe8dcSKarl Rupp 
844d0f46423SBarry 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);
845ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues is %D\n",a->reallocs);CHKERRQ(ierr);
846ae15b995SBarry Smith   ierr = PetscInfo1(A,"Most nonzeros blocks in any row is %D\n",rmax);CHKERRQ(ierr);
84726fbe8dcSKarl Rupp 
8488e58a170SBarry Smith   A->info.mallocs    += a->reallocs;
84949b5e25fSSatish Balay   a->reallocs         = 0;
85049b5e25fSSatish Balay   A->info.nz_unneeded = (PetscReal)fshift*bs2;
851061b2667SBarry Smith   a->idiagvalid       = PETSC_FALSE;
8524dcd73b1SHong Zhang   a->rmax             = rmax;
85338702af4SBarry Smith 
85438702af4SBarry Smith   if (A->cmap->n < 65536 && A->cmap->bs == 1) {
85544e1c64aSLisandro Dalcin     if (a->jshort && a->free_jshort) {
85617803ae8SHong Zhang       /* when matrix data structure is changed, previous jshort must be replaced */
85717803ae8SHong Zhang       ierr = PetscFree(a->jshort);CHKERRQ(ierr);
85817803ae8SHong Zhang     }
859785e854fSJed Brown     ierr = PetscMalloc1(a->i[A->rmap->n],&a->jshort);CHKERRQ(ierr);
8603bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A,a->i[A->rmap->n]*sizeof(unsigned short));CHKERRQ(ierr);
86138702af4SBarry Smith     for (i=0; i<a->i[A->rmap->n]; i++) a->jshort[i] = a->j[i];
86238702af4SBarry Smith     A->ops->mult   = MatMult_SeqSBAIJ_1_ushort;
86341f059aeSBarry Smith     A->ops->sor    = MatSOR_SeqSBAIJ_ushort;
8644da8f245SBarry Smith     a->free_jshort = PETSC_TRUE;
86538702af4SBarry Smith   }
86649b5e25fSSatish Balay   PetscFunctionReturn(0);
86749b5e25fSSatish Balay }
86849b5e25fSSatish Balay 
86949b5e25fSSatish Balay /*
87049b5e25fSSatish Balay    This function returns an array of flags which indicate the locations of contiguous
87149b5e25fSSatish Balay    blocks that should be zeroed. for eg: if bs = 3  and is = [0,1,2,3,5,6,7,8,9]
87249b5e25fSSatish Balay    then the resulting sizes = [3,1,1,3,1] correspondig to sets [(0,1,2),(3),(5),(6,7,8),(9)]
87349b5e25fSSatish Balay    Assume: sizes should be long enough to hold all the values.
87449b5e25fSSatish Balay */
8754a2ae208SSatish Balay #undef __FUNCT__
8764a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqSBAIJ_Check_Blocks"
87713f74950SBarry Smith PetscErrorCode MatZeroRows_SeqSBAIJ_Check_Blocks(PetscInt idx[],PetscInt n,PetscInt bs,PetscInt sizes[], PetscInt *bs_max)
87849b5e25fSSatish Balay {
87913f74950SBarry Smith   PetscInt  i,j,k,row;
880ace3abfcSBarry Smith   PetscBool flg;
88149b5e25fSSatish Balay 
88249b5e25fSSatish Balay   PetscFunctionBegin;
88349b5e25fSSatish Balay   for (i=0,j=0; i<n; j++) {
88449b5e25fSSatish Balay     row = idx[i];
88549b5e25fSSatish Balay     if (row%bs!=0) { /* Not the begining of a block */
88649b5e25fSSatish Balay       sizes[j] = 1;
88749b5e25fSSatish Balay       i++;
88849b5e25fSSatish Balay     } else if (i+bs > n) { /* Beginning of a block, but complete block doesn't exist (at idx end) */
88949b5e25fSSatish Balay       sizes[j] = 1;         /* Also makes sure atleast 'bs' values exist for next else */
89049b5e25fSSatish Balay       i++;
89149b5e25fSSatish Balay     } else { /* Begining of the block, so check if the complete block exists */
89249b5e25fSSatish Balay       flg = PETSC_TRUE;
89349b5e25fSSatish Balay       for (k=1; k<bs; k++) {
89449b5e25fSSatish Balay         if (row+k != idx[i+k]) { /* break in the block */
89549b5e25fSSatish Balay           flg = PETSC_FALSE;
89649b5e25fSSatish Balay           break;
89749b5e25fSSatish Balay         }
89849b5e25fSSatish Balay       }
899abc0a331SBarry Smith       if (flg) { /* No break in the bs */
90049b5e25fSSatish Balay         sizes[j] = bs;
90149b5e25fSSatish Balay         i       += bs;
90249b5e25fSSatish Balay       } else {
90349b5e25fSSatish Balay         sizes[j] = 1;
90449b5e25fSSatish Balay         i++;
90549b5e25fSSatish Balay       }
90649b5e25fSSatish Balay     }
90749b5e25fSSatish Balay   }
90849b5e25fSSatish Balay   *bs_max = j;
90949b5e25fSSatish Balay   PetscFunctionReturn(0);
91049b5e25fSSatish Balay }
91149b5e25fSSatish Balay 
91249b5e25fSSatish Balay 
91349b5e25fSSatish Balay /* Only add/insert a(i,j) with i<=j (blocks).
91449b5e25fSSatish Balay    Any a(i,j) with i>j input by user is ingored.
91549b5e25fSSatish Balay */
91649b5e25fSSatish Balay 
9174a2ae208SSatish Balay #undef __FUNCT__
9184a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqSBAIJ"
91913f74950SBarry Smith PetscErrorCode MatSetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
92049b5e25fSSatish Balay {
92149b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
9226849ba73SBarry Smith   PetscErrorCode ierr;
923e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,lastcol = -1;
92413f74950SBarry Smith   PetscInt       *imax=a->imax,*ai=a->i,*ailen=a->ilen,roworiented=a->roworiented;
925d0f46423SBarry Smith   PetscInt       *aj  =a->j,nonew=a->nonew,bs=A->rmap->bs,brow,bcol;
92613f74950SBarry Smith   PetscInt       ridx,cidx,bs2=a->bs2;
92749b5e25fSSatish Balay   MatScalar      *ap,value,*aa=a->a,*bap;
92849b5e25fSSatish Balay 
92949b5e25fSSatish Balay   PetscFunctionBegin;
93049b5e25fSSatish Balay   for (k=0; k<m; k++) { /* loop over added rows */
93149b5e25fSSatish Balay     row  = im[k];       /* row number */
93249b5e25fSSatish Balay     brow = row/bs;      /* block row number */
93349b5e25fSSatish Balay     if (row < 0) continue;
9342515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
935e32f2f54SBarry 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);
93649b5e25fSSatish Balay #endif
93749b5e25fSSatish Balay     rp   = aj + ai[brow]; /*ptr to beginning of column value of the row block*/
93849b5e25fSSatish Balay     ap   = aa + bs2*ai[brow]; /*ptr to beginning of element value of the row block*/
93949b5e25fSSatish Balay     rmax = imax[brow];  /* maximum space allocated for this row */
94049b5e25fSSatish Balay     nrow = ailen[brow]; /* actual length of this row */
94149b5e25fSSatish Balay     low  = 0;
94249b5e25fSSatish Balay 
94349b5e25fSSatish Balay     for (l=0; l<n; l++) { /* loop over added columns */
94449b5e25fSSatish Balay       if (in[l] < 0) continue;
9452515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
946e32f2f54SBarry 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);
94749b5e25fSSatish Balay #endif
94849b5e25fSSatish Balay       col  = in[l];
94949b5e25fSSatish Balay       bcol = col/bs;              /* block col number */
95049b5e25fSSatish Balay 
951941593c8SHong Zhang       if (brow > bcol) {
95226fbe8dcSKarl Rupp         if (a->ignore_ltriangular) continue; /* ignore lower triangular values */
95326fbe8dcSKarl Rupp         else 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)");
954941593c8SHong Zhang       }
955f4989cb3SHong Zhang 
95649b5e25fSSatish Balay       ridx = row % bs; cidx = col % bs; /*row and col index inside the block */
9578549e402SHong Zhang       if ((brow==bcol && ridx<=cidx) || (brow<bcol)) {
95849b5e25fSSatish Balay         /* element value a(k,l) */
95926fbe8dcSKarl Rupp         if (roworiented) value = v[l + k*n];
96026fbe8dcSKarl Rupp         else value = v[k + l*m];
96149b5e25fSSatish Balay 
96249b5e25fSSatish Balay         /* move pointer bap to a(k,l) quickly and add/insert value */
96326fbe8dcSKarl Rupp         if (col <= lastcol) low = 0;
96426fbe8dcSKarl Rupp         high = nrow;
965e2ee6c50SBarry Smith         lastcol = col;
96649b5e25fSSatish Balay         while (high-low > 7) {
96749b5e25fSSatish Balay           t = (low+high)/2;
96849b5e25fSSatish Balay           if (rp[t] > bcol) high = t;
96949b5e25fSSatish Balay           else              low  = t;
97049b5e25fSSatish Balay         }
97149b5e25fSSatish Balay         for (i=low; i<high; i++) {
97249b5e25fSSatish Balay           if (rp[i] > bcol) break;
97349b5e25fSSatish Balay           if (rp[i] == bcol) {
97449b5e25fSSatish Balay             bap = ap +  bs2*i + bs*cidx + ridx;
97549b5e25fSSatish Balay             if (is == ADD_VALUES) *bap += value;
97649b5e25fSSatish Balay             else                  *bap  = value;
9778549e402SHong Zhang             /* for diag block, add/insert its symmetric element a(cidx,ridx) */
9788549e402SHong Zhang             if (brow == bcol && ridx < cidx) {
9798549e402SHong Zhang               bap = ap +  bs2*i + bs*ridx + cidx;
9808549e402SHong Zhang               if (is == ADD_VALUES) *bap += value;
9818549e402SHong Zhang               else                  *bap  = value;
9828549e402SHong Zhang             }
98349b5e25fSSatish Balay             goto noinsert1;
98449b5e25fSSatish Balay           }
98549b5e25fSSatish Balay         }
98649b5e25fSSatish Balay 
98749b5e25fSSatish Balay         if (nonew == 1) goto noinsert1;
988e32f2f54SBarry Smith         if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
989fef13f97SBarry Smith         MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
99049b5e25fSSatish Balay 
991c03d1d03SSatish Balay         N = nrow++ - 1; high++;
99249b5e25fSSatish Balay         /* shift up all the later entries in this row */
99349b5e25fSSatish Balay         for (ii=N; ii>=i; ii--) {
99449b5e25fSSatish Balay           rp[ii+1] = rp[ii];
99549b5e25fSSatish Balay           ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
99649b5e25fSSatish Balay         }
99749b5e25fSSatish Balay         if (N>=i) {
99849b5e25fSSatish Balay           ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
99949b5e25fSSatish Balay         }
100049b5e25fSSatish Balay         rp[i]                      = bcol;
100149b5e25fSSatish Balay         ap[bs2*i + bs*cidx + ridx] = value;
1002e56f5c9eSBarry Smith         A->nonzerostate++;
100349b5e25fSSatish Balay noinsert1:;
100449b5e25fSSatish Balay         low = i;
10058549e402SHong Zhang       }
100649b5e25fSSatish Balay     }   /* end of loop over added columns */
100749b5e25fSSatish Balay     ailen[brow] = nrow;
100849b5e25fSSatish Balay   }   /* end of loop over added rows */
100949b5e25fSSatish Balay   PetscFunctionReturn(0);
101049b5e25fSSatish Balay }
101149b5e25fSSatish Balay 
10124a2ae208SSatish Balay #undef __FUNCT__
10134d101231SSatish Balay #define __FUNCT__ "MatICCFactor_SeqSBAIJ"
10140481f469SBarry Smith PetscErrorCode MatICCFactor_SeqSBAIJ(Mat inA,IS row,const MatFactorInfo *info)
101549b5e25fSSatish Balay {
10164ccecd49SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)inA->data;
101749b5e25fSSatish Balay   Mat            outA;
1018dfbe8321SBarry Smith   PetscErrorCode ierr;
1019ace3abfcSBarry Smith   PetscBool      row_identity;
102049b5e25fSSatish Balay 
102149b5e25fSSatish Balay   PetscFunctionBegin;
1022e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 is supported for in-place icc");
1023c84f5b01SHong Zhang   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1024e32f2f54SBarry Smith   if (!row_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix reordering is not supported");
1025e32f2f54SBarry 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()! */
1026c84f5b01SHong Zhang 
102749b5e25fSSatish Balay   outA            = inA;
1028d5f3da31SBarry Smith   inA->factortype = MAT_FACTOR_ICC;
102949b5e25fSSatish Balay 
10301a3463dfSHong Zhang   ierr = MatMarkDiagonal_SeqSBAIJ(inA);CHKERRQ(ierr);
1031d595f711SHong Zhang   ierr = MatSeqSBAIJSetNumericFactorization_inplace(inA,row_identity);CHKERRQ(ierr);
103249b5e25fSSatish Balay 
1033c3122656SLisandro Dalcin   ierr   = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
10346bf464f9SBarry Smith   ierr   = ISDestroy(&a->row);CHKERRQ(ierr);
1035c84f5b01SHong Zhang   a->row = row;
1036c3122656SLisandro Dalcin   ierr   = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
10376bf464f9SBarry Smith   ierr   = ISDestroy(&a->col);CHKERRQ(ierr);
1038c84f5b01SHong Zhang   a->col = row;
1039c84f5b01SHong Zhang 
1040c84f5b01SHong Zhang   /* Create the invert permutation so that it can be used in MatCholeskyFactorNumeric() */
1041c84f5b01SHong Zhang   if (a->icol) {ierr = ISInvertPermutation(row,PETSC_DECIDE, &a->icol);CHKERRQ(ierr);}
10423bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr);
104349b5e25fSSatish Balay 
104449b5e25fSSatish Balay   if (!a->solve_work) {
1045854ce69bSBarry Smith     ierr = PetscMalloc1(inA->rmap->N+inA->rmap->bs,&a->solve_work);CHKERRQ(ierr);
10463bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)inA,(inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar));CHKERRQ(ierr);
104749b5e25fSSatish Balay   }
104849b5e25fSSatish Balay 
1049719d5645SBarry Smith   ierr = MatCholeskyFactorNumeric(outA,inA,info);CHKERRQ(ierr);
105049b5e25fSSatish Balay   PetscFunctionReturn(0);
105149b5e25fSSatish Balay }
1052950f1e5bSHong Zhang 
10534a2ae208SSatish Balay #undef __FUNCT__
10544a2ae208SSatish Balay #define __FUNCT__ "MatSeqSBAIJSetColumnIndices_SeqSBAIJ"
10557087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetColumnIndices_SeqSBAIJ(Mat mat,PetscInt *indices)
105649b5e25fSSatish Balay {
1057045c9aa0SHong Zhang   Mat_SeqSBAIJ   *baij = (Mat_SeqSBAIJ*)mat->data;
105813f74950SBarry Smith   PetscInt       i,nz,n;
10597827cd58SJed Brown   PetscErrorCode ierr;
106049b5e25fSSatish Balay 
106149b5e25fSSatish Balay   PetscFunctionBegin;
10626c6c5352SBarry Smith   nz = baij->maxnz;
1063d0f46423SBarry Smith   n  = mat->cmap->n;
106426fbe8dcSKarl Rupp   for (i=0; i<nz; i++) baij->j[i] = indices[i];
106526fbe8dcSKarl Rupp 
10666c6c5352SBarry Smith   baij->nz = nz;
106726fbe8dcSKarl Rupp   for (i=0; i<n; i++) baij->ilen[i] = baij->imax[i];
106826fbe8dcSKarl Rupp 
10697827cd58SJed Brown   ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
107049b5e25fSSatish Balay   PetscFunctionReturn(0);
107149b5e25fSSatish Balay }
107249b5e25fSSatish Balay 
10734a2ae208SSatish Balay #undef __FUNCT__
10744a2ae208SSatish Balay #define __FUNCT__ "MatSeqSBAIJSetColumnIndices"
107549b5e25fSSatish Balay /*@
107619585528SSatish Balay   MatSeqSBAIJSetColumnIndices - Set the column indices for all the rows
107749b5e25fSSatish Balay   in the matrix.
107849b5e25fSSatish Balay 
107949b5e25fSSatish Balay   Input Parameters:
108019585528SSatish Balay   +  mat     - the SeqSBAIJ matrix
108149b5e25fSSatish Balay   -  indices - the column indices
108249b5e25fSSatish Balay 
108349b5e25fSSatish Balay   Level: advanced
108449b5e25fSSatish Balay 
108549b5e25fSSatish Balay   Notes:
108649b5e25fSSatish Balay   This can be called if you have precomputed the nonzero structure of the
108749b5e25fSSatish Balay   matrix and want to provide it to the matrix object to improve the performance
108849b5e25fSSatish Balay   of the MatSetValues() operation.
108949b5e25fSSatish Balay 
109049b5e25fSSatish Balay   You MUST have set the correct numbers of nonzeros per row in the call to
1091d1be2dadSMatthew Knepley   MatCreateSeqSBAIJ(), and the columns indices MUST be sorted.
109249b5e25fSSatish Balay 
1093ab9f2c04SSatish Balay   MUST be called before any calls to MatSetValues()
109449b5e25fSSatish Balay 
1095ab9f2c04SSatish Balay   .seealso: MatCreateSeqSBAIJ
109649b5e25fSSatish Balay @*/
10977087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetColumnIndices(Mat mat,PetscInt *indices)
109849b5e25fSSatish Balay {
10994ac538c5SBarry Smith   PetscErrorCode ierr;
110049b5e25fSSatish Balay 
110149b5e25fSSatish Balay   PetscFunctionBegin;
11020700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
11034482741eSBarry Smith   PetscValidPointer(indices,2);
11044ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqSBAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr);
110549b5e25fSSatish Balay   PetscFunctionReturn(0);
110649b5e25fSSatish Balay }
110749b5e25fSSatish Balay 
11084a2ae208SSatish Balay #undef __FUNCT__
11093c896bc6SHong Zhang #define __FUNCT__ "MatCopy_SeqSBAIJ"
11103c896bc6SHong Zhang PetscErrorCode MatCopy_SeqSBAIJ(Mat A,Mat B,MatStructure str)
11113c896bc6SHong Zhang {
11123c896bc6SHong Zhang   PetscErrorCode ierr;
11133c896bc6SHong Zhang 
11143c896bc6SHong Zhang   PetscFunctionBegin;
11153c896bc6SHong Zhang   /* If the two matrices have the same copy implementation, use fast copy. */
11163c896bc6SHong Zhang   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
11173c896bc6SHong Zhang     Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
11183c896bc6SHong Zhang     Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ*)B->data;
11193c896bc6SHong Zhang 
1120e7e72b3dSBarry 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");
1121d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->N])*sizeof(PetscScalar));CHKERRQ(ierr);
11223c896bc6SHong Zhang   } else {
1123f5edf698SHong Zhang     ierr = MatGetRowUpperTriangular(A);CHKERRQ(ierr);
11243c896bc6SHong Zhang     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1125f5edf698SHong Zhang     ierr = MatRestoreRowUpperTriangular(A);CHKERRQ(ierr);
11263c896bc6SHong Zhang   }
11273c896bc6SHong Zhang   PetscFunctionReturn(0);
11283c896bc6SHong Zhang }
11293c896bc6SHong Zhang 
11303c896bc6SHong Zhang #undef __FUNCT__
11314994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqSBAIJ"
11324994cf47SJed Brown PetscErrorCode MatSetUp_SeqSBAIJ(Mat A)
1133273d9f13SBarry Smith {
1134dfbe8321SBarry Smith   PetscErrorCode ierr;
1135273d9f13SBarry Smith 
1136273d9f13SBarry Smith   PetscFunctionBegin;
1137535b19f3SBarry Smith   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(A,A->rmap->bs,PETSC_DEFAULT,0);CHKERRQ(ierr);
1138273d9f13SBarry Smith   PetscFunctionReturn(0);
1139273d9f13SBarry Smith }
1140273d9f13SBarry Smith 
1141a6ece127SHong Zhang #undef __FUNCT__
11428c778c55SBarry Smith #define __FUNCT__ "MatSeqSBAIJGetArray_SeqSBAIJ"
11438c778c55SBarry Smith PetscErrorCode MatSeqSBAIJGetArray_SeqSBAIJ(Mat A,PetscScalar *array[])
1144a6ece127SHong Zhang {
1145a6ece127SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
11465fd66863SKarl Rupp 
1147a6ece127SHong Zhang   PetscFunctionBegin;
1148a6ece127SHong Zhang   *array = a->a;
1149a6ece127SHong Zhang   PetscFunctionReturn(0);
1150a6ece127SHong Zhang }
1151a6ece127SHong Zhang 
1152a6ece127SHong Zhang #undef __FUNCT__
11538c778c55SBarry Smith #define __FUNCT__ "MatSeqSBAIJRestoreArray_SeqSBAIJ"
11548c778c55SBarry Smith PetscErrorCode MatSeqSBAIJRestoreArray_SeqSBAIJ(Mat A,PetscScalar *array[])
1155a6ece127SHong Zhang {
1156a6ece127SHong Zhang   PetscFunctionBegin;
1157a6ece127SHong Zhang   PetscFunctionReturn(0);
1158a6ece127SHong Zhang }
1159a6ece127SHong Zhang 
116042ee4b1aSHong Zhang #undef __FUNCT__
116152768537SHong Zhang #define __FUNCT__ "MatAXPYGetPreallocation_SeqSBAIJ"
116252768537SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqSBAIJ(Mat Y,Mat X,PetscInt *nnz)
116352768537SHong Zhang {
1164b264fe52SHong Zhang   PetscInt       bs = Y->rmap->bs,mbs = Y->rmap->N/bs;
116552768537SHong Zhang   Mat_SeqSBAIJ   *x = (Mat_SeqSBAIJ*)X->data;
116652768537SHong Zhang   Mat_SeqSBAIJ   *y = (Mat_SeqSBAIJ*)Y->data;
1167b264fe52SHong Zhang   PetscErrorCode ierr;
116852768537SHong Zhang 
116952768537SHong Zhang   PetscFunctionBegin;
117052768537SHong Zhang   /* Set the number of nonzeros in the new matrix */
1171b264fe52SHong Zhang   ierr = MatAXPYGetPreallocation_SeqX_private(mbs,x->i,x->j,y->i,y->j,nnz);CHKERRQ(ierr);
117252768537SHong Zhang   PetscFunctionReturn(0);
117352768537SHong Zhang }
117452768537SHong Zhang 
117552768537SHong Zhang #undef __FUNCT__
117642ee4b1aSHong Zhang #define __FUNCT__ "MatAXPY_SeqSBAIJ"
1177f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqSBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
117842ee4b1aSHong Zhang {
117942ee4b1aSHong Zhang   Mat_SeqSBAIJ   *x=(Mat_SeqSBAIJ*)X->data, *y=(Mat_SeqSBAIJ*)Y->data;
1180dfbe8321SBarry Smith   PetscErrorCode ierr;
118131ce2d13SHong Zhang   PetscInt       bs=Y->rmap->bs,bs2=bs*bs;
1182e838b9e7SJed Brown   PetscBLASInt   one = 1;
118342ee4b1aSHong Zhang 
118442ee4b1aSHong Zhang   PetscFunctionBegin;
118542ee4b1aSHong Zhang   if (str == SAME_NONZERO_PATTERN) {
1186f4df32b1SMatthew Knepley     PetscScalar  alpha = a;
1187c5df96a5SBarry Smith     PetscBLASInt bnz;
1188c5df96a5SBarry Smith     ierr = PetscBLASIntCast(x->nz*bs2,&bnz);CHKERRQ(ierr);
11898b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
1190a3fa217bSJose E. Roman     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
1191ab784542SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
1192ab784542SHong Zhang     ierr = MatSetOption(X,MAT_GETROW_UPPERTRIANGULAR,PETSC_TRUE);CHKERRQ(ierr);
1193ab784542SHong Zhang     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
1194ab784542SHong Zhang     ierr = MatSetOption(X,MAT_GETROW_UPPERTRIANGULAR,PETSC_FALSE);CHKERRQ(ierr);
119542ee4b1aSHong Zhang   } else {
119652768537SHong Zhang     Mat      B;
119752768537SHong Zhang     PetscInt *nnz;
119852768537SHong Zhang     if (bs != X->rmap->bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrices must have same block size");
1199f5edf698SHong Zhang     ierr = MatGetRowUpperTriangular(X);CHKERRQ(ierr);
120052768537SHong Zhang     ierr = MatGetRowUpperTriangular(Y);CHKERRQ(ierr);
120152768537SHong Zhang     ierr = PetscMalloc1(Y->rmap->N,&nnz);CHKERRQ(ierr);
120252768537SHong Zhang     ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr);
120352768537SHong Zhang     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
120452768537SHong Zhang     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
120552768537SHong Zhang     ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr);
120652768537SHong Zhang     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
120752768537SHong Zhang     ierr = MatAXPYGetPreallocation_SeqSBAIJ(Y,X,nnz);CHKERRQ(ierr);
120852768537SHong Zhang     ierr = MatSeqSBAIJSetPreallocation(B,bs,0,nnz);CHKERRQ(ierr);
120952768537SHong Zhang 
121052768537SHong Zhang     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
121152768537SHong Zhang 
121252768537SHong Zhang     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
121352768537SHong Zhang     ierr = PetscFree(nnz);CHKERRQ(ierr);
1214f5edf698SHong Zhang     ierr = MatRestoreRowUpperTriangular(X);CHKERRQ(ierr);
121552768537SHong Zhang     ierr = MatRestoreRowUpperTriangular(Y);CHKERRQ(ierr);
121642ee4b1aSHong Zhang   }
121742ee4b1aSHong Zhang   PetscFunctionReturn(0);
121842ee4b1aSHong Zhang }
121942ee4b1aSHong Zhang 
1220efcf0fc3SBarry Smith #undef __FUNCT__
1221efcf0fc3SBarry Smith #define __FUNCT__ "MatIsSymmetric_SeqSBAIJ"
1222ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqSBAIJ(Mat A,PetscReal tol,PetscBool  *flg)
1223efcf0fc3SBarry Smith {
1224efcf0fc3SBarry Smith   PetscFunctionBegin;
1225efcf0fc3SBarry Smith   *flg = PETSC_TRUE;
1226efcf0fc3SBarry Smith   PetscFunctionReturn(0);
1227efcf0fc3SBarry Smith }
1228efcf0fc3SBarry Smith 
1229efcf0fc3SBarry Smith #undef __FUNCT__
1230efcf0fc3SBarry Smith #define __FUNCT__ "MatIsStructurallySymmetric_SeqSBAIJ"
1231ace3abfcSBarry Smith PetscErrorCode MatIsStructurallySymmetric_SeqSBAIJ(Mat A,PetscBool  *flg)
1232efcf0fc3SBarry Smith {
1233efcf0fc3SBarry Smith   PetscFunctionBegin;
1234efcf0fc3SBarry Smith   *flg = PETSC_TRUE;
1235efcf0fc3SBarry Smith   PetscFunctionReturn(0);
1236efcf0fc3SBarry Smith }
1237efcf0fc3SBarry Smith 
1238efcf0fc3SBarry Smith #undef __FUNCT__
1239efcf0fc3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqSBAIJ"
1240ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqSBAIJ(Mat A,PetscReal tol,PetscBool  *flg)
1241efcf0fc3SBarry Smith {
1242efcf0fc3SBarry Smith   PetscFunctionBegin;
1243efcf0fc3SBarry Smith   *flg = PETSC_FALSE;
1244efcf0fc3SBarry Smith   PetscFunctionReturn(0);
1245efcf0fc3SBarry Smith }
1246efcf0fc3SBarry Smith 
124799cafbc1SBarry Smith #undef __FUNCT__
124899cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqSBAIJ"
124999cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqSBAIJ(Mat A)
125099cafbc1SBarry Smith {
125199cafbc1SBarry Smith   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
125299cafbc1SBarry Smith   PetscInt     i,nz = a->bs2*a->i[a->mbs];
1253dd6ea824SBarry Smith   MatScalar    *aa = a->a;
125499cafbc1SBarry Smith 
125599cafbc1SBarry Smith   PetscFunctionBegin;
125699cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
125799cafbc1SBarry Smith   PetscFunctionReturn(0);
125899cafbc1SBarry Smith }
125999cafbc1SBarry Smith 
126099cafbc1SBarry Smith #undef __FUNCT__
126199cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqSBAIJ"
126299cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqSBAIJ(Mat A)
126399cafbc1SBarry Smith {
126499cafbc1SBarry Smith   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
126599cafbc1SBarry Smith   PetscInt     i,nz = a->bs2*a->i[a->mbs];
1266dd6ea824SBarry Smith   MatScalar    *aa = a->a;
126799cafbc1SBarry Smith 
126899cafbc1SBarry Smith   PetscFunctionBegin;
126999cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
127099cafbc1SBarry Smith   PetscFunctionReturn(0);
127199cafbc1SBarry Smith }
127299cafbc1SBarry Smith 
12733bededecSBarry Smith #undef __FUNCT__
12743bededecSBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqSBAIJ"
12753bededecSBarry Smith PetscErrorCode MatZeroRowsColumns_SeqSBAIJ(Mat A,PetscInt is_n,const PetscInt is_idx[],PetscScalar diag,Vec x, Vec b)
12763bededecSBarry Smith {
12773bededecSBarry Smith   Mat_SeqSBAIJ      *baij=(Mat_SeqSBAIJ*)A->data;
12783bededecSBarry Smith   PetscErrorCode    ierr;
12793bededecSBarry Smith   PetscInt          i,j,k,count;
12803bededecSBarry Smith   PetscInt          bs   =A->rmap->bs,bs2=baij->bs2,row,col;
12813bededecSBarry Smith   PetscScalar       zero = 0.0;
12823bededecSBarry Smith   MatScalar         *aa;
12833bededecSBarry Smith   const PetscScalar *xx;
12843bededecSBarry Smith   PetscScalar       *bb;
128556777dd2SBarry Smith   PetscBool         *zeroed,vecs = PETSC_FALSE;
12863bededecSBarry Smith 
12873bededecSBarry Smith   PetscFunctionBegin;
12883bededecSBarry Smith   /* fix right hand side if needed */
12893bededecSBarry Smith   if (x && b) {
12903bededecSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
12913bededecSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
129256777dd2SBarry Smith     vecs = PETSC_TRUE;
12933bededecSBarry Smith   }
12943bededecSBarry Smith 
12953bededecSBarry Smith   /* zero the columns */
12961795a4d1SJed Brown   ierr = PetscCalloc1(A->rmap->n,&zeroed);CHKERRQ(ierr);
12973bededecSBarry Smith   for (i=0; i<is_n; i++) {
12983bededecSBarry 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]);
12993bededecSBarry Smith     zeroed[is_idx[i]] = PETSC_TRUE;
13003bededecSBarry Smith   }
130156777dd2SBarry Smith   if (vecs) {
130256777dd2SBarry Smith     for (i=0; i<A->rmap->N; i++) {
130356777dd2SBarry Smith       row = i/bs;
130456777dd2SBarry Smith       for (j=baij->i[row]; j<baij->i[row+1]; j++) {
130556777dd2SBarry Smith         for (k=0; k<bs; k++) {
130656777dd2SBarry Smith           col = bs*baij->j[j] + k;
130756777dd2SBarry Smith           if (col <= i) continue;
130856777dd2SBarry Smith           aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
130926fbe8dcSKarl Rupp           if (!zeroed[i] && zeroed[col]) bb[i]   -= aa[0]*xx[col];
131026fbe8dcSKarl Rupp           if (zeroed[i] && !zeroed[col]) bb[col] -= aa[0]*xx[i];
131156777dd2SBarry Smith         }
131256777dd2SBarry Smith       }
131356777dd2SBarry Smith     }
131426fbe8dcSKarl Rupp     for (i=0; i<is_n; i++) bb[is_idx[i]] = diag*xx[is_idx[i]];
131556777dd2SBarry Smith   }
131656777dd2SBarry Smith 
13173bededecSBarry Smith   for (i=0; i<A->rmap->N; i++) {
13183bededecSBarry Smith     if (!zeroed[i]) {
13193bededecSBarry Smith       row = i/bs;
13203bededecSBarry Smith       for (j=baij->i[row]; j<baij->i[row+1]; j++) {
13213bededecSBarry Smith         for (k=0; k<bs; k++) {
13223bededecSBarry Smith           col = bs*baij->j[j] + k;
13233bededecSBarry Smith           if (zeroed[col]) {
13243bededecSBarry Smith             aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
13253bededecSBarry Smith             aa[0] = 0.0;
13263bededecSBarry Smith           }
13273bededecSBarry Smith         }
13283bededecSBarry Smith       }
13293bededecSBarry Smith     }
13303bededecSBarry Smith   }
13313bededecSBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
133256777dd2SBarry Smith   if (vecs) {
133356777dd2SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
133456777dd2SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
133556777dd2SBarry Smith   }
13363bededecSBarry Smith 
13373bededecSBarry Smith   /* zero the rows */
13383bededecSBarry Smith   for (i=0; i<is_n; i++) {
13393bededecSBarry Smith     row   = is_idx[i];
13403bededecSBarry Smith     count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
13413bededecSBarry Smith     aa    = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
13423bededecSBarry Smith     for (k=0; k<count; k++) {
13433bededecSBarry Smith       aa[0] =  zero;
13443bededecSBarry Smith       aa   += bs;
13453bededecSBarry Smith     }
13463bededecSBarry Smith     if (diag != 0.0) {
13473bededecSBarry Smith       ierr = (*A->ops->setvalues)(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr);
13483bededecSBarry Smith     }
13493bededecSBarry Smith   }
13503bededecSBarry Smith   ierr = MatAssemblyEnd_SeqSBAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13513bededecSBarry Smith   PetscFunctionReturn(0);
13523bededecSBarry Smith }
13533bededecSBarry Smith 
135449b5e25fSSatish Balay /* -------------------------------------------------------------------*/
13553964eb88SJed Brown static struct _MatOps MatOps_Values = {MatSetValues_SeqSBAIJ,
135649b5e25fSSatish Balay                                        MatGetRow_SeqSBAIJ,
135749b5e25fSSatish Balay                                        MatRestoreRow_SeqSBAIJ,
135849b5e25fSSatish Balay                                        MatMult_SeqSBAIJ_N,
135997304618SKris Buschelman                                /*  4*/ MatMultAdd_SeqSBAIJ_N,
1360431c96f7SBarry Smith                                        MatMult_SeqSBAIJ_N,       /* transpose versions are same as non-transpose versions */
1361e005ede5SBarry Smith                                        MatMultAdd_SeqSBAIJ_N,
1362db4efbfdSBarry Smith                                        0,
136349b5e25fSSatish Balay                                        0,
136449b5e25fSSatish Balay                                        0,
136597304618SKris Buschelman                                /* 10*/ 0,
136649b5e25fSSatish Balay                                        0,
1367c078aec8SLisandro Dalcin                                        MatCholeskyFactor_SeqSBAIJ,
136841f059aeSBarry Smith                                        MatSOR_SeqSBAIJ,
136949b5e25fSSatish Balay                                        MatTranspose_SeqSBAIJ,
137097304618SKris Buschelman                                /* 15*/ MatGetInfo_SeqSBAIJ,
137149b5e25fSSatish Balay                                        MatEqual_SeqSBAIJ,
137249b5e25fSSatish Balay                                        MatGetDiagonal_SeqSBAIJ,
137349b5e25fSSatish Balay                                        MatDiagonalScale_SeqSBAIJ,
137449b5e25fSSatish Balay                                        MatNorm_SeqSBAIJ,
137597304618SKris Buschelman                                /* 20*/ 0,
137649b5e25fSSatish Balay                                        MatAssemblyEnd_SeqSBAIJ,
137749b5e25fSSatish Balay                                        MatSetOption_SeqSBAIJ,
137849b5e25fSSatish Balay                                        MatZeroEntries_SeqSBAIJ,
1379d519adbfSMatthew Knepley                                /* 24*/ 0,
138049b5e25fSSatish Balay                                        0,
138149b5e25fSSatish Balay                                        0,
1382db4efbfdSBarry Smith                                        0,
1383db4efbfdSBarry Smith                                        0,
13844994cf47SJed Brown                                /* 29*/ MatSetUp_SeqSBAIJ,
1385c464158bSHong Zhang                                        0,
1386db4efbfdSBarry Smith                                        0,
13878c778c55SBarry Smith                                        0,
13888c778c55SBarry Smith                                        0,
1389d519adbfSMatthew Knepley                                /* 34*/ MatDuplicate_SeqSBAIJ,
1390719d5645SBarry Smith                                        0,
1391719d5645SBarry Smith                                        0,
139249b5e25fSSatish Balay                                        0,
1393c84f5b01SHong Zhang                                        MatICCFactor_SeqSBAIJ,
1394d519adbfSMatthew Knepley                                /* 39*/ MatAXPY_SeqSBAIJ,
139549b5e25fSSatish Balay                                        MatGetSubMatrices_SeqSBAIJ,
139649b5e25fSSatish Balay                                        MatIncreaseOverlap_SeqSBAIJ,
139749b5e25fSSatish Balay                                        MatGetValues_SeqSBAIJ,
13983c896bc6SHong Zhang                                        MatCopy_SeqSBAIJ,
1399d519adbfSMatthew Knepley                                /* 44*/ 0,
140049b5e25fSSatish Balay                                        MatScale_SeqSBAIJ,
140149b5e25fSSatish Balay                                        0,
140249b5e25fSSatish Balay                                        0,
14033bededecSBarry Smith                                        MatZeroRowsColumns_SeqSBAIJ,
1404f73d5cc4SBarry Smith                                /* 49*/ 0,
140549b5e25fSSatish Balay                                        MatGetRowIJ_SeqSBAIJ,
140649b5e25fSSatish Balay                                        MatRestoreRowIJ_SeqSBAIJ,
140749b5e25fSSatish Balay                                        0,
140849b5e25fSSatish Balay                                        0,
1409d519adbfSMatthew Knepley                                /* 54*/ 0,
141049b5e25fSSatish Balay                                        0,
141149b5e25fSSatish Balay                                        0,
141249b5e25fSSatish Balay                                        0,
141349b5e25fSSatish Balay                                        MatSetValuesBlocked_SeqSBAIJ,
1414d519adbfSMatthew Knepley                                /* 59*/ MatGetSubMatrix_SeqSBAIJ,
141549b5e25fSSatish Balay                                        0,
141649b5e25fSSatish Balay                                        0,
1417357abbc8SBarry Smith                                        0,
1418d959ec07SHong Zhang                                        0,
1419d519adbfSMatthew Knepley                                /* 64*/ 0,
1420d959ec07SHong Zhang                                        0,
1421d959ec07SHong Zhang                                        0,
1422d959ec07SHong Zhang                                        0,
1423d959ec07SHong Zhang                                        0,
1424d519adbfSMatthew Knepley                                /* 69*/ MatGetRowMaxAbs_SeqSBAIJ,
14253e0d88b5SBarry Smith                                        0,
14263e0d88b5SBarry Smith                                        0,
14273e0d88b5SBarry Smith                                        0,
14283e0d88b5SBarry Smith                                        0,
1429d519adbfSMatthew Knepley                                /* 74*/ 0,
14303e0d88b5SBarry Smith                                        0,
14313e0d88b5SBarry Smith                                        0,
14323e0d88b5SBarry Smith                                        0,
14333e0d88b5SBarry Smith                                        0,
1434d519adbfSMatthew Knepley                                /* 79*/ 0,
14353e0d88b5SBarry Smith                                        0,
14363e0d88b5SBarry Smith                                        0,
143797304618SKris Buschelman                                        MatGetInertia_SeqSBAIJ,
14385bba2384SShri Abhyankar                                        MatLoad_SeqSBAIJ,
1439d519adbfSMatthew Knepley                                /* 84*/ MatIsSymmetric_SeqSBAIJ,
1440865e5f61SKris Buschelman                                        MatIsHermitian_SeqSBAIJ,
1441efcf0fc3SBarry Smith                                        MatIsStructurallySymmetric_SeqSBAIJ,
1442865e5f61SKris Buschelman                                        0,
1443865e5f61SKris Buschelman                                        0,
1444d519adbfSMatthew Knepley                                /* 89*/ 0,
1445865e5f61SKris Buschelman                                        0,
1446865e5f61SKris Buschelman                                        0,
1447865e5f61SKris Buschelman                                        0,
1448865e5f61SKris Buschelman                                        0,
1449d519adbfSMatthew Knepley                                /* 94*/ 0,
1450865e5f61SKris Buschelman                                        0,
1451865e5f61SKris Buschelman                                        0,
145299cafbc1SBarry Smith                                        0,
145399cafbc1SBarry Smith                                        0,
1454d519adbfSMatthew Knepley                                /* 99*/ 0,
145599cafbc1SBarry Smith                                        0,
145699cafbc1SBarry Smith                                        0,
145799cafbc1SBarry Smith                                        0,
145899cafbc1SBarry Smith                                        0,
1459d519adbfSMatthew Knepley                                /*104*/ 0,
146099cafbc1SBarry Smith                                        MatRealPart_SeqSBAIJ,
1461f5edf698SHong Zhang                                        MatImaginaryPart_SeqSBAIJ,
1462f5edf698SHong Zhang                                        MatGetRowUpperTriangular_SeqSBAIJ,
14632af78befSBarry Smith                                        MatRestoreRowUpperTriangular_SeqSBAIJ,
1464d519adbfSMatthew Knepley                                /*109*/ 0,
14652af78befSBarry Smith                                        0,
14662af78befSBarry Smith                                        0,
14672af78befSBarry Smith                                        0,
1468547795f9SHong Zhang                                        MatMissingDiagonal_SeqSBAIJ,
1469547795f9SHong Zhang                                /*114*/ 0,
1470547795f9SHong Zhang                                        0,
1471547795f9SHong Zhang                                        0,
1472547795f9SHong Zhang                                        0,
1473547795f9SHong Zhang                                        0,
1474547795f9SHong Zhang                                /*119*/ 0,
1475547795f9SHong Zhang                                        0,
14762f480046SShri Abhyankar                                        0,
14773964eb88SJed Brown                                        0,
14783964eb88SJed Brown                                        0,
14793964eb88SJed Brown                                /*124*/ 0,
14803964eb88SJed Brown                                        0,
14813964eb88SJed Brown                                        0,
14823964eb88SJed Brown                                        0,
14833964eb88SJed Brown                                        0,
14843964eb88SJed Brown                                /*129*/ 0,
14853964eb88SJed Brown                                        0,
14863964eb88SJed Brown                                        0,
14873964eb88SJed Brown                                        0,
14883964eb88SJed Brown                                        0,
14893964eb88SJed Brown                                /*134*/ 0,
14903964eb88SJed Brown                                        0,
14913964eb88SJed Brown                                        0,
14923964eb88SJed Brown                                        0,
14933964eb88SJed Brown                                        0,
14943964eb88SJed Brown                                /*139*/ 0,
1495f9426fe0SMark Adams                                        0,
149659f5e6ceSHong Zhang                                        0,
149759f5e6ceSHong Zhang                                        0,
149859f5e6ceSHong Zhang                                        0,
149959f5e6ceSHong Zhang                                 /*144*/MatCreateMPIMatConcatenateSeqMat_SeqSBAIJ
150099cafbc1SBarry Smith };
1501be1d678aSKris Buschelman 
15024a2ae208SSatish Balay #undef __FUNCT__
15034a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqSBAIJ"
15047087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqSBAIJ(Mat mat)
150549b5e25fSSatish Balay {
15064afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ*)mat->data;
1507d0f46423SBarry Smith   PetscInt       nz   = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
1508dfbe8321SBarry Smith   PetscErrorCode ierr;
150949b5e25fSSatish Balay 
151049b5e25fSSatish Balay   PetscFunctionBegin;
1511e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
151249b5e25fSSatish Balay 
151349b5e25fSSatish Balay   /* allocate space for values if not already there */
151449b5e25fSSatish Balay   if (!aij->saved_values) {
1515854ce69bSBarry Smith     ierr = PetscMalloc1(nz+1,&aij->saved_values);CHKERRQ(ierr);
151649b5e25fSSatish Balay   }
151749b5e25fSSatish Balay 
151849b5e25fSSatish Balay   /* copy values over */
151987828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
152049b5e25fSSatish Balay   PetscFunctionReturn(0);
152149b5e25fSSatish Balay }
152249b5e25fSSatish Balay 
15234a2ae208SSatish Balay #undef __FUNCT__
15244a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqSBAIJ"
15257087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqSBAIJ(Mat mat)
152649b5e25fSSatish Balay {
15274afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ*)mat->data;
15286849ba73SBarry Smith   PetscErrorCode ierr;
1529d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
153049b5e25fSSatish Balay 
153149b5e25fSSatish Balay   PetscFunctionBegin;
1532e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
1533e7e72b3dSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
153449b5e25fSSatish Balay 
153549b5e25fSSatish Balay   /* copy values over */
153687828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
153749b5e25fSSatish Balay   PetscFunctionReturn(0);
153849b5e25fSSatish Balay }
153949b5e25fSSatish Balay 
15404a2ae208SSatish Balay #undef __FUNCT__
1541a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation_SeqSBAIJ"
15427087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetPreallocation_SeqSBAIJ(Mat B,PetscInt bs,PetscInt nz,PetscInt *nnz)
154349b5e25fSSatish Balay {
1544c464158bSHong Zhang   Mat_SeqSBAIJ   *b = (Mat_SeqSBAIJ*)B->data;
15456849ba73SBarry Smith   PetscErrorCode ierr;
15464dcd73b1SHong Zhang   PetscInt       i,mbs,nbs,bs2;
15472576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,flg = PETSC_FALSE,realalloc = PETSC_FALSE;
154849b5e25fSSatish Balay 
154949b5e25fSSatish Balay   PetscFunctionBegin;
15502576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
1551273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
1552db4efbfdSBarry Smith 
155333d57670SJed Brown   ierr = MatSetBlockSize(B,PetscAbs(bs));CHKERRQ(ierr);
155426283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
155526283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
1556e02043d6SBarry Smith   ierr = PetscLayoutGetBlockSize(B->rmap,&bs);CHKERRQ(ierr);
1557899cda47SBarry Smith 
1558d0f46423SBarry Smith   mbs = B->rmap->N/bs;
15594dcd73b1SHong Zhang   nbs = B->cmap->n/bs;
156049b5e25fSSatish Balay   bs2 = bs*bs;
156149b5e25fSSatish Balay 
15624dcd73b1SHong Zhang   if (mbs*bs != B->rmap->N || nbs*bs!=B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number rows, cols must be divisible by blocksize");
156349b5e25fSSatish Balay 
1564ab93d7beSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
1565ab93d7beSBarry Smith     skipallocation = PETSC_TRUE;
1566ab93d7beSBarry Smith     nz             = 0;
1567ab93d7beSBarry Smith   }
1568ab93d7beSBarry Smith 
1569435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 3;
1570e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
157149b5e25fSSatish Balay   if (nnz) {
157249b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
1573e32f2f54SBarry 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]);
1574de64b629SHong Zhang       if (nnz[i] > nbs) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than block row length: local row %D value %D block rowlength %D",i,nnz[i],nbs);
157549b5e25fSSatish Balay     }
157649b5e25fSSatish Balay   }
157749b5e25fSSatish Balay 
1578db4efbfdSBarry Smith   B->ops->mult             = MatMult_SeqSBAIJ_N;
1579db4efbfdSBarry Smith   B->ops->multadd          = MatMultAdd_SeqSBAIJ_N;
1580db4efbfdSBarry Smith   B->ops->multtranspose    = MatMult_SeqSBAIJ_N;
1581db4efbfdSBarry Smith   B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_N;
158226fbe8dcSKarl Rupp 
15830298fd71SBarry Smith   ierr  = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,NULL);CHKERRQ(ierr);
158449b5e25fSSatish Balay   if (!flg) {
158549b5e25fSSatish Balay     switch (bs) {
158649b5e25fSSatish Balay     case 1:
158749b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_1;
158849b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_1;
1589431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_1;
1590431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_1;
159149b5e25fSSatish Balay       break;
159249b5e25fSSatish Balay     case 2:
159349b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_2;
159449b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_2;
1595431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_2;
1596431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_2;
159749b5e25fSSatish Balay       break;
159849b5e25fSSatish Balay     case 3:
159949b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_3;
160049b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_3;
1601431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_3;
1602431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_3;
160349b5e25fSSatish Balay       break;
160449b5e25fSSatish Balay     case 4:
160549b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_4;
160649b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_4;
1607431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_4;
1608431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_4;
160949b5e25fSSatish Balay       break;
161049b5e25fSSatish Balay     case 5:
161149b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_5;
161249b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_5;
1613431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_5;
1614431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_5;
161549b5e25fSSatish Balay       break;
161649b5e25fSSatish Balay     case 6:
161749b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_6;
161849b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_6;
1619431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_6;
1620431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_6;
162149b5e25fSSatish Balay       break;
162249b5e25fSSatish Balay     case 7:
1623de53e5efSHong Zhang       B->ops->mult             = MatMult_SeqSBAIJ_7;
162449b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_7;
1625431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_7;
1626431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_7;
162749b5e25fSSatish Balay       break;
162849b5e25fSSatish Balay     }
162949b5e25fSSatish Balay   }
163049b5e25fSSatish Balay 
163149b5e25fSSatish Balay   b->mbs = mbs;
16324dcd73b1SHong Zhang   b->nbs = nbs;
1633ab93d7beSBarry Smith   if (!skipallocation) {
16342ee49352SLisandro Dalcin     if (!b->imax) {
1635dcca6d9dSJed Brown       ierr = PetscMalloc2(mbs,&b->imax,mbs,&b->ilen);CHKERRQ(ierr);
163626fbe8dcSKarl Rupp 
1637c760cd28SBarry Smith       b->free_imax_ilen = PETSC_TRUE;
163826fbe8dcSKarl Rupp 
16393bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,2*mbs*sizeof(PetscInt));CHKERRQ(ierr);
16402ee49352SLisandro Dalcin     }
164149b5e25fSSatish Balay     if (!nnz) {
1642435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
164349b5e25fSSatish Balay       else if (nz <= 0) nz = 1;
164426fbe8dcSKarl Rupp       for (i=0; i<mbs; i++) b->imax[i] = nz;
1645153ea458SHong Zhang       nz = nz*mbs; /* total nz */
164649b5e25fSSatish Balay     } else {
164749b5e25fSSatish Balay       nz = 0;
16488cef66ccSHong Zhang       for (i=0; i<mbs; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
164949b5e25fSSatish Balay     }
16502ee49352SLisandro Dalcin     /* b->ilen will count nonzeros in each block row so far. */
165126fbe8dcSKarl Rupp     for (i=0; i<mbs; i++) b->ilen[i] = 0;
16526c6c5352SBarry Smith     /* nz=(nz+mbs)/2; */ /* total diagonal and superdiagonal nonzero blocks */
165349b5e25fSSatish Balay 
165449b5e25fSSatish Balay     /* allocate the matrix space */
16552ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
1656dcca6d9dSJed Brown     ierr = PetscMalloc3(bs2*nz,&b->a,nz,&b->j,B->rmap->N+1,&b->i);CHKERRQ(ierr);
16573bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)B,(B->rmap->N+1)*sizeof(PetscInt)+nz*(bs2*sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
16586c6c5352SBarry Smith     ierr = PetscMemzero(b->a,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
165913f74950SBarry Smith     ierr = PetscMemzero(b->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
166026fbe8dcSKarl Rupp 
166149b5e25fSSatish Balay     b->singlemalloc = PETSC_TRUE;
166249b5e25fSSatish Balay 
166349b5e25fSSatish Balay     /* pointer to beginning of each row */
1664e60cf9a0SBarry Smith     b->i[0] = 0;
166526fbe8dcSKarl Rupp     for (i=1; i<mbs+1; i++) b->i[i] = b->i[i-1] + b->imax[i-1];
166626fbe8dcSKarl Rupp 
1667e6b907acSBarry Smith     b->free_a  = PETSC_TRUE;
1668e6b907acSBarry Smith     b->free_ij = PETSC_TRUE;
1669e811da20SHong Zhang   } else {
1670e6b907acSBarry Smith     b->free_a  = PETSC_FALSE;
1671e6b907acSBarry Smith     b->free_ij = PETSC_FALSE;
1672ab93d7beSBarry Smith   }
167349b5e25fSSatish Balay 
1674d0f46423SBarry Smith   B->rmap->bs = bs;
167549b5e25fSSatish Balay   b->bs2      = bs2;
16766c6c5352SBarry Smith   b->nz       = 0;
1677b32cb4a7SJed Brown   b->maxnz    = nz;
1678153ea458SHong Zhang 
167916cdd363SHong Zhang   b->inew    = 0;
168016cdd363SHong Zhang   b->jnew    = 0;
168116cdd363SHong Zhang   b->anew    = 0;
168216cdd363SHong Zhang   b->a2anew  = 0;
16831a3463dfSHong Zhang   b->permute = PETSC_FALSE;
16842576faa2SJed Brown   if (realalloc) {ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);}
1685c464158bSHong Zhang   PetscFunctionReturn(0);
1686c464158bSHong Zhang }
1687153ea458SHong Zhang 
168838f409ebSLisandro Dalcin #undef __FUNCT__
168938f409ebSLisandro Dalcin #define __FUNCT__ "MatSeqSBAIJSetPreallocationCSR_SeqSBAIJ"
169038f409ebSLisandro Dalcin PetscErrorCode MatSeqSBAIJSetPreallocationCSR_SeqSBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[], const PetscScalar V[])
169138f409ebSLisandro Dalcin {
169238f409ebSLisandro Dalcin   PetscInt       i,j,m,nz,nz_max=0,*nnz;
169338f409ebSLisandro Dalcin   PetscScalar    *values=0;
169438f409ebSLisandro Dalcin   PetscBool      roworiented = ((Mat_SeqSBAIJ*)B->data)->roworiented;
169538f409ebSLisandro Dalcin   PetscErrorCode ierr;
169638f409ebSLisandro Dalcin   PetscFunctionBegin;
169738f409ebSLisandro Dalcin   if (bs < 1) SETERRQ1(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive but it is %D",bs);
169838f409ebSLisandro Dalcin   ierr   = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
169938f409ebSLisandro Dalcin   ierr   = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
170038f409ebSLisandro Dalcin   ierr   = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
170138f409ebSLisandro Dalcin   ierr   = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
170238f409ebSLisandro Dalcin   ierr   = PetscLayoutGetBlockSize(B->rmap,&bs);CHKERRQ(ierr);
170338f409ebSLisandro Dalcin   m      = B->rmap->n/bs;
170438f409ebSLisandro Dalcin 
170538f409ebSLisandro Dalcin   if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]);
1706854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&nnz);CHKERRQ(ierr);
170738f409ebSLisandro Dalcin   for (i=0; i<m; i++) {
170838f409ebSLisandro Dalcin     nz = ii[i+1] - ii[i];
170938f409ebSLisandro Dalcin     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D has a negative number of columns %D",i,nz);
171038f409ebSLisandro Dalcin     nz_max = PetscMax(nz_max,nz);
171138f409ebSLisandro Dalcin     nnz[i] = nz;
171238f409ebSLisandro Dalcin   }
171338f409ebSLisandro Dalcin   ierr = MatSeqSBAIJSetPreallocation(B,bs,0,nnz);CHKERRQ(ierr);
171438f409ebSLisandro Dalcin   ierr = PetscFree(nnz);CHKERRQ(ierr);
171538f409ebSLisandro Dalcin 
171638f409ebSLisandro Dalcin   values = (PetscScalar*)V;
171738f409ebSLisandro Dalcin   if (!values) {
17181795a4d1SJed Brown     ierr = PetscCalloc1(bs*bs*nz_max,&values);CHKERRQ(ierr);
171938f409ebSLisandro Dalcin   }
172038f409ebSLisandro Dalcin   for (i=0; i<m; i++) {
172138f409ebSLisandro Dalcin     PetscInt          ncols  = ii[i+1] - ii[i];
172238f409ebSLisandro Dalcin     const PetscInt    *icols = jj + ii[i];
172338f409ebSLisandro Dalcin     if (!roworiented || bs == 1) {
172438f409ebSLisandro Dalcin       const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
172538f409ebSLisandro Dalcin       ierr = MatSetValuesBlocked_SeqSBAIJ(B,1,&i,ncols,icols,svals,INSERT_VALUES);CHKERRQ(ierr);
172638f409ebSLisandro Dalcin     } else {
172738f409ebSLisandro Dalcin       for (j=0; j<ncols; j++) {
172838f409ebSLisandro Dalcin         const PetscScalar *svals = values + (V ? (bs*bs*(ii[i]+j)) : 0);
172938f409ebSLisandro Dalcin         ierr = MatSetValuesBlocked_SeqSBAIJ(B,1,&i,1,&icols[j],svals,INSERT_VALUES);CHKERRQ(ierr);
173038f409ebSLisandro Dalcin       }
173138f409ebSLisandro Dalcin     }
173238f409ebSLisandro Dalcin   }
173338f409ebSLisandro Dalcin   if (!V) { ierr = PetscFree(values);CHKERRQ(ierr); }
173438f409ebSLisandro Dalcin   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
173538f409ebSLisandro Dalcin   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
173638f409ebSLisandro Dalcin   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
173738f409ebSLisandro Dalcin   PetscFunctionReturn(0);
173838f409ebSLisandro Dalcin }
173938f409ebSLisandro Dalcin 
1740db4efbfdSBarry Smith /*
1741db4efbfdSBarry Smith    This is used to set the numeric factorization for both Cholesky and ICC symbolic factorization
1742db4efbfdSBarry Smith */
17438b1456e3SHong Zhang #undef __FUNCT__
1744d595f711SHong Zhang #define __FUNCT__ "MatSeqSBAIJSetNumericFactorization_inplace"
1745ace3abfcSBarry Smith PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat B,PetscBool natural)
1746db4efbfdSBarry Smith {
1747db4efbfdSBarry Smith   PetscErrorCode ierr;
1748ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
1749db4efbfdSBarry Smith   PetscInt       bs  = B->rmap->bs;
1750db4efbfdSBarry Smith 
1751db4efbfdSBarry Smith   PetscFunctionBegin;
17520298fd71SBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,NULL);CHKERRQ(ierr);
1753db4efbfdSBarry Smith   if (flg) bs = 8;
1754db4efbfdSBarry Smith 
1755db4efbfdSBarry Smith   if (!natural) {
1756db4efbfdSBarry Smith     switch (bs) {
1757db4efbfdSBarry Smith     case 1:
1758d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_inplace;
1759db4efbfdSBarry Smith       break;
1760db4efbfdSBarry Smith     case 2:
1761db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2;
1762db4efbfdSBarry Smith       break;
1763db4efbfdSBarry Smith     case 3:
1764db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3;
1765db4efbfdSBarry Smith       break;
1766db4efbfdSBarry Smith     case 4:
1767db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4;
1768db4efbfdSBarry Smith       break;
1769db4efbfdSBarry Smith     case 5:
1770db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5;
1771db4efbfdSBarry Smith       break;
1772db4efbfdSBarry Smith     case 6:
1773db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6;
1774db4efbfdSBarry Smith       break;
1775db4efbfdSBarry Smith     case 7:
1776db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7;
1777db4efbfdSBarry Smith       break;
1778db4efbfdSBarry Smith     default:
1779db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N;
1780db4efbfdSBarry Smith       break;
1781db4efbfdSBarry Smith     }
1782db4efbfdSBarry Smith   } else {
1783db4efbfdSBarry Smith     switch (bs) {
1784db4efbfdSBarry Smith     case 1:
1785d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering_inplace;
1786db4efbfdSBarry Smith       break;
1787db4efbfdSBarry Smith     case 2:
1788db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering;
1789db4efbfdSBarry Smith       break;
1790db4efbfdSBarry Smith     case 3:
1791db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3_NaturalOrdering;
1792db4efbfdSBarry Smith       break;
1793db4efbfdSBarry Smith     case 4:
1794db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4_NaturalOrdering;
1795db4efbfdSBarry Smith       break;
1796db4efbfdSBarry Smith     case 5:
1797db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5_NaturalOrdering;
1798db4efbfdSBarry Smith       break;
1799db4efbfdSBarry Smith     case 6:
1800db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6_NaturalOrdering;
1801db4efbfdSBarry Smith       break;
1802db4efbfdSBarry Smith     case 7:
1803db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7_NaturalOrdering;
1804db4efbfdSBarry Smith       break;
1805db4efbfdSBarry Smith     default:
1806db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering;
1807db4efbfdSBarry Smith       break;
1808db4efbfdSBarry Smith     }
1809db4efbfdSBarry Smith   }
1810db4efbfdSBarry Smith   PetscFunctionReturn(0);
1811db4efbfdSBarry Smith }
1812db4efbfdSBarry Smith 
18138cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqAIJ(Mat, MatType,MatReuse,Mat*);
18148cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqBAIJ(Mat, MatType,MatReuse,Mat*);
1815d769727bSBarry Smith 
18165c9eb25fSBarry Smith #undef __FUNCT__
18175c9eb25fSBarry Smith #define __FUNCT__ "MatGetFactor_seqsbaij_petsc"
18188cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqsbaij_petsc(Mat A,MatFactorType ftype,Mat *B)
18195c9eb25fSBarry Smith {
1820d0f46423SBarry Smith   PetscInt       n = A->rmap->n;
18215c9eb25fSBarry Smith   PetscErrorCode ierr;
18225c9eb25fSBarry Smith 
18235c9eb25fSBarry Smith   PetscFunctionBegin;
18240e92d65fSHong Zhang #if defined(PETSC_USE_COMPLEX)
18250e92d65fSHong Zhang   if (A->hermitian) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Hermitian Factor is not supported");
18260e92d65fSHong Zhang #endif
1827ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
18285c9eb25fSBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
18295c9eb25fSBarry Smith   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
18305c9eb25fSBarry Smith     ierr = MatSetType(*B,MATSEQSBAIJ);CHKERRQ(ierr);
18310298fd71SBarry Smith     ierr = MatSeqSBAIJSetPreallocation(*B,A->rmap->bs,MAT_SKIP_ALLOCATION,NULL);CHKERRQ(ierr);
183226fbe8dcSKarl Rupp 
18337b056e98SHong Zhang     (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqSBAIJ;
1834c6d0d4f0SHong Zhang     (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqSBAIJ;
1835e32f2f54SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported");
1836d5f3da31SBarry Smith   (*B)->factortype = ftype;
18375c9eb25fSBarry Smith   PetscFunctionReturn(0);
18385c9eb25fSBarry Smith }
18395c9eb25fSBarry Smith 
18400bad9183SKris Buschelman /*MC
1841fafad747SKris Buschelman   MATSEQSBAIJ - MATSEQSBAIJ = "seqsbaij" - A matrix type to be used for sequential symmetric block sparse matrices,
18420bad9183SKris Buschelman   based on block compressed sparse row format.  Only the upper triangular portion of the matrix is stored.
18430bad9183SKris Buschelman 
1844828413b8SBarry Smith   For complex numbers by default this matrix is symmetric, NOT Hermitian symmetric. To make it Hermitian symmetric you
184571dad5bbSBarry Smith   can call MatSetOption(Mat, MAT_HERMITIAN); after MatAssemblyEnd()
1846828413b8SBarry Smith 
18470bad9183SKris Buschelman   Options Database Keys:
18480bad9183SKris Buschelman   . -mat_type seqsbaij - sets the matrix type to "seqsbaij" during a call to MatSetFromOptions()
18490bad9183SKris Buschelman 
185071dad5bbSBarry Smith   Notes: By default if you insert values into the lower triangular part of the matrix they are simply ignored (since they are not
185171dad5bbSBarry 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
185271dad5bbSBarry 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.
185371dad5bbSBarry Smith 
185471dad5bbSBarry Smith 
18550bad9183SKris Buschelman   Level: beginner
18560bad9183SKris Buschelman 
18570bad9183SKris Buschelman   .seealso: MatCreateSeqSBAIJ
18580bad9183SKris Buschelman M*/
18590bad9183SKris Buschelman 
18608cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqSBSTRM(Mat, MatType,MatReuse,Mat*);
1861aa5a9175SDahai Guo 
1862a23d5eceSKris Buschelman #undef __FUNCT__
1863a23d5eceSKris Buschelman #define __FUNCT__ "MatCreate_SeqSBAIJ"
18648cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqSBAIJ(Mat B)
1865a23d5eceSKris Buschelman {
1866a23d5eceSKris Buschelman   Mat_SeqSBAIJ   *b;
1867dfbe8321SBarry Smith   PetscErrorCode ierr;
186813f74950SBarry Smith   PetscMPIInt    size;
1869ace3abfcSBarry Smith   PetscBool      no_unroll = PETSC_FALSE,no_inode = PETSC_FALSE;
1870a23d5eceSKris Buschelman 
1871a23d5eceSKris Buschelman   PetscFunctionBegin;
1872ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
1873e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Comm must be of size 1");
1874a23d5eceSKris Buschelman 
1875b00a9115SJed Brown   ierr    = PetscNewLog(B,&b);CHKERRQ(ierr);
1876a23d5eceSKris Buschelman   B->data = (void*)b;
1877a23d5eceSKris Buschelman   ierr    = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
187826fbe8dcSKarl Rupp 
1879a23d5eceSKris Buschelman   B->ops->destroy    = MatDestroy_SeqSBAIJ;
1880a23d5eceSKris Buschelman   B->ops->view       = MatView_SeqSBAIJ;
1881a23d5eceSKris Buschelman   b->row             = 0;
1882a23d5eceSKris Buschelman   b->icol            = 0;
1883a23d5eceSKris Buschelman   b->reallocs        = 0;
1884a23d5eceSKris Buschelman   b->saved_values    = 0;
18850def2e27SBarry Smith   b->inode.limit     = 5;
18860def2e27SBarry Smith   b->inode.max_limit = 5;
1887a23d5eceSKris Buschelman 
1888a23d5eceSKris Buschelman   b->roworiented        = PETSC_TRUE;
1889a23d5eceSKris Buschelman   b->nonew              = 0;
1890a23d5eceSKris Buschelman   b->diag               = 0;
1891a23d5eceSKris Buschelman   b->solve_work         = 0;
1892a23d5eceSKris Buschelman   b->mult_work          = 0;
1893a23d5eceSKris Buschelman   B->spptr              = 0;
1894f2cbd3d5SJed Brown   B->info.nz_unneeded   = (PetscReal)b->maxnz*b->bs2;
1895a9817697SBarry Smith   b->keepnonzeropattern = PETSC_FALSE;
1896a23d5eceSKris Buschelman 
1897a23d5eceSKris Buschelman   b->inew    = 0;
1898a23d5eceSKris Buschelman   b->jnew    = 0;
1899a23d5eceSKris Buschelman   b->anew    = 0;
1900a23d5eceSKris Buschelman   b->a2anew  = 0;
1901a23d5eceSKris Buschelman   b->permute = PETSC_FALSE;
1902a23d5eceSKris Buschelman 
190371dad5bbSBarry Smith   b->ignore_ltriangular = PETSC_TRUE;
190426fbe8dcSKarl Rupp 
19050298fd71SBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_ignore_lower_triangular",&b->ignore_ltriangular,NULL);CHKERRQ(ierr);
1906941593c8SHong Zhang 
1907f5edf698SHong Zhang   b->getrow_utriangular = PETSC_FALSE;
190826fbe8dcSKarl Rupp 
19090298fd71SBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_getrow_uppertriangular",&b->getrow_utriangular,NULL);CHKERRQ(ierr);
1910f5edf698SHong Zhang 
1911bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqSBAIJ);CHKERRQ(ierr);
1912bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqSBAIJ);CHKERRQ(ierr);
1913bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqSBAIJSetColumnIndices_C",MatSeqSBAIJSetColumnIndices_SeqSBAIJ);CHKERRQ(ierr);
1914bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_seqaij_C",MatConvert_SeqSBAIJ_SeqAIJ);CHKERRQ(ierr);
1915bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_seqbaij_C",MatConvert_SeqSBAIJ_SeqBAIJ);CHKERRQ(ierr);
1916bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqSBAIJSetPreallocation_C",MatSeqSBAIJSetPreallocation_SeqSBAIJ);CHKERRQ(ierr);
191738f409ebSLisandro Dalcin   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqSBAIJSetPreallocationCSR_C",MatSeqSBAIJSetPreallocationCSR_SeqSBAIJ);CHKERRQ(ierr);
1918bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_seqsbstrm_C",MatConvert_SeqSBAIJ_SeqSBSTRM);CHKERRQ(ierr);
19196214f412SHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
19206214f412SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_elemental_C",MatConvert_SeqSBAIJ_Elemental);CHKERRQ(ierr);
19216214f412SHong Zhang #endif
192223ce1328SBarry Smith 
192323ce1328SBarry Smith   B->symmetric                  = PETSC_TRUE;
192423ce1328SBarry Smith   B->structurally_symmetric     = PETSC_TRUE;
192523ce1328SBarry Smith   B->symmetric_set              = PETSC_TRUE;
192623ce1328SBarry Smith   B->structurally_symmetric_set = PETSC_TRUE;
192726fbe8dcSKarl Rupp 
192817667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQSBAIJ);CHKERRQ(ierr);
19290def2e27SBarry Smith 
1930ce94432eSBarry Smith   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)B),((PetscObject)B)->prefix,"Options for SEQSBAIJ matrix","Mat");CHKERRQ(ierr);
19310298fd71SBarry Smith   ierr = PetscOptionsBool("-mat_no_unroll","Do not optimize for inodes (slower)",NULL,no_unroll,&no_unroll,NULL);CHKERRQ(ierr);
193226fbe8dcSKarl Rupp   if (no_unroll) {
193326fbe8dcSKarl Rupp     ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_unroll\n");CHKERRQ(ierr);
193426fbe8dcSKarl Rupp   }
19350298fd71SBarry Smith   ierr = PetscOptionsBool("-mat_no_inode","Do not optimize for inodes (slower)",NULL,no_inode,&no_inode,NULL);CHKERRQ(ierr);
193626fbe8dcSKarl Rupp   if (no_inode) {
193726fbe8dcSKarl Rupp     ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_inode\n");CHKERRQ(ierr);
193826fbe8dcSKarl Rupp   }
19390298fd71SBarry Smith   ierr = PetscOptionsInt("-mat_inode_limit","Do not use inodes larger then this value",NULL,b->inode.limit,&b->inode.limit,NULL);CHKERRQ(ierr);
19400def2e27SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1941ace3abfcSBarry Smith   b->inode.use = (PetscBool)(!(no_unroll || no_inode));
19420def2e27SBarry Smith   if (b->inode.limit > b->inode.max_limit) b->inode.limit = b->inode.max_limit;
1943a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1944a23d5eceSKris Buschelman }
1945a23d5eceSKris Buschelman 
1946a23d5eceSKris Buschelman #undef __FUNCT__
1947a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation"
1948a23d5eceSKris Buschelman /*@C
1949a23d5eceSKris Buschelman    MatSeqSBAIJSetPreallocation - Creates a sparse symmetric matrix in block AIJ (block
1950a23d5eceSKris Buschelman    compressed row) format.  For good matrix assembly performance the
1951a23d5eceSKris Buschelman    user should preallocate the matrix storage by setting the parameter nz
1952a23d5eceSKris Buschelman    (or the array nnz).  By setting these parameters accurately, performance
1953a23d5eceSKris Buschelman    during matrix assembly can be increased by more than a factor of 50.
1954a23d5eceSKris Buschelman 
1955a23d5eceSKris Buschelman    Collective on Mat
1956a23d5eceSKris Buschelman 
1957a23d5eceSKris Buschelman    Input Parameters:
19581c4f3114SJed Brown +  B - the symmetric matrix
1959bb7ae925SBarry Smith .  bs - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
1960bb7ae925SBarry Smith           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
1961a23d5eceSKris Buschelman .  nz - number of block nonzeros per block row (same for all rows)
1962a23d5eceSKris Buschelman -  nnz - array containing the number of block nonzeros in the upper triangular plus
19630298fd71SBarry Smith          diagonal portion of each block (possibly different for each block row) or NULL
1964a23d5eceSKris Buschelman 
1965a23d5eceSKris Buschelman    Options Database Keys:
1966a23d5eceSKris Buschelman .   -mat_no_unroll - uses code that does not unroll the loops in the
1967a23d5eceSKris Buschelman                      block calculations (much slower)
1968db4efbfdSBarry Smith .    -mat_block_size - size of the blocks to use (only works if a negative bs is passed in
1969a23d5eceSKris Buschelman 
1970a23d5eceSKris Buschelman    Level: intermediate
1971a23d5eceSKris Buschelman 
1972a23d5eceSKris Buschelman    Notes:
1973a23d5eceSKris Buschelman    Specify the preallocated storage with either nz or nnz (not both).
19740298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
1975a7f22e61SSatish Balay    allocation.  See Users-Manual: ch_mat for details.
1976a23d5eceSKris Buschelman 
1977aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
1978aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
1979aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
1980aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
1981aa95bbe8SBarry Smith 
198249a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
198349a6f317SBarry Smith 
198449a6f317SBarry Smith 
198569b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateSBAIJ()
1986a23d5eceSKris Buschelman @*/
19877087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt nz,const PetscInt nnz[])
198813f74950SBarry Smith {
19894ac538c5SBarry Smith   PetscErrorCode ierr;
1990a23d5eceSKris Buschelman 
1991a23d5eceSKris Buschelman   PetscFunctionBegin;
19926ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
19936ba663aaSJed Brown   PetscValidType(B,1);
19946ba663aaSJed Brown   PetscValidLogicalCollectiveInt(B,bs,2);
19954ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqSBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[]),(B,bs,nz,nnz));CHKERRQ(ierr);
1996a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1997a23d5eceSKris Buschelman }
199849b5e25fSSatish Balay 
19994a2ae208SSatish Balay #undef  __FUNCT__
200038f409ebSLisandro Dalcin #define __FUNCT__ "MatSeqSBAIJSetPreallocationCSR"
200138f409ebSLisandro Dalcin /*@C
200238f409ebSLisandro Dalcin    MatSeqSBAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in symmetric block AIJ format.
200338f409ebSLisandro Dalcin 
200438f409ebSLisandro Dalcin    Input Parameters:
20051c4f3114SJed Brown +  B - the matrix
200638f409ebSLisandro Dalcin .  i - the indices into j for the start of each local row (starts with zero)
200738f409ebSLisandro Dalcin .  j - the column indices for each local row (starts with zero) these must be sorted for each row
200838f409ebSLisandro Dalcin -  v - optional values in the matrix
200938f409ebSLisandro Dalcin 
201038f409ebSLisandro Dalcin    Level: developer
201138f409ebSLisandro Dalcin 
201238f409ebSLisandro Dalcin    Notes:
201338f409ebSLisandro Dalcin    The order of the entries in values is specified by the MatOption MAT_ROW_ORIENTED.  For example, C programs
201438f409ebSLisandro Dalcin    may want to use the default MAT_ROW_ORIENTED=PETSC_TRUE and use an array v[nnz][bs][bs] where the second index is
201538f409ebSLisandro Dalcin    over rows within a block and the last index is over columns within a block row.  Fortran programs will likely set
201638f409ebSLisandro Dalcin    MAT_ROW_ORIENTED=PETSC_FALSE and use a Fortran array v(bs,bs,nnz) in which the first index is over rows within a
201738f409ebSLisandro Dalcin    block column and the second index is over columns within a block.
201838f409ebSLisandro Dalcin 
201938f409ebSLisandro Dalcin .keywords: matrix, block, aij, compressed row, sparse
202038f409ebSLisandro Dalcin 
202138f409ebSLisandro Dalcin .seealso: MatCreate(), MatCreateSeqSBAIJ(), MatSetValuesBlocked(), MatSeqSBAIJSetPreallocation(), MATSEQSBAIJ
202238f409ebSLisandro Dalcin @*/
202338f409ebSLisandro Dalcin PetscErrorCode MatSeqSBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
202438f409ebSLisandro Dalcin {
202538f409ebSLisandro Dalcin   PetscErrorCode ierr;
202638f409ebSLisandro Dalcin 
202738f409ebSLisandro Dalcin   PetscFunctionBegin;
202838f409ebSLisandro Dalcin   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
202938f409ebSLisandro Dalcin   PetscValidType(B,1);
203038f409ebSLisandro Dalcin   PetscValidLogicalCollectiveInt(B,bs,2);
203138f409ebSLisandro Dalcin   ierr = PetscTryMethod(B,"MatSeqSBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));CHKERRQ(ierr);
203238f409ebSLisandro Dalcin   PetscFunctionReturn(0);
203338f409ebSLisandro Dalcin }
203438f409ebSLisandro Dalcin 
203538f409ebSLisandro Dalcin #undef __FUNCT__
20364a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqSBAIJ"
2037c464158bSHong Zhang /*@C
2038c464158bSHong Zhang    MatCreateSeqSBAIJ - Creates a sparse symmetric matrix in block AIJ (block
2039c464158bSHong Zhang    compressed row) format.  For good matrix assembly performance the
2040c464158bSHong Zhang    user should preallocate the matrix storage by setting the parameter nz
2041c464158bSHong Zhang    (or the array nnz).  By setting these parameters accurately, performance
2042c464158bSHong Zhang    during matrix assembly can be increased by more than a factor of 50.
204349b5e25fSSatish Balay 
2044c464158bSHong Zhang    Collective on MPI_Comm
2045c464158bSHong Zhang 
2046c464158bSHong Zhang    Input Parameters:
2047c464158bSHong Zhang +  comm - MPI communicator, set to PETSC_COMM_SELF
2048bb7ae925SBarry Smith .  bs - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
2049bb7ae925SBarry Smith           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
2050c464158bSHong Zhang .  m - number of rows, or number of columns
2051c464158bSHong Zhang .  nz - number of block nonzeros per block row (same for all rows)
2052744e8345SSatish Balay -  nnz - array containing the number of block nonzeros in the upper triangular plus
20530298fd71SBarry Smith          diagonal portion of each block (possibly different for each block row) or NULL
2054c464158bSHong Zhang 
2055c464158bSHong Zhang    Output Parameter:
2056c464158bSHong Zhang .  A - the symmetric matrix
2057c464158bSHong Zhang 
2058c464158bSHong Zhang    Options Database Keys:
2059c464158bSHong Zhang .   -mat_no_unroll - uses code that does not unroll the loops in the
2060c464158bSHong Zhang                      block calculations (much slower)
2061c464158bSHong Zhang .    -mat_block_size - size of the blocks to use
2062c464158bSHong Zhang 
2063c464158bSHong Zhang    Level: intermediate
2064c464158bSHong Zhang 
2065175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2066ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
2067175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2068175b88e8SBarry Smith 
2069c464158bSHong Zhang    Notes:
20706d6d819aSHong Zhang    The number of rows and columns must be divisible by blocksize.
20716d6d819aSHong Zhang    This matrix type does not support complex Hermitian operation.
2072c464158bSHong Zhang 
2073c464158bSHong Zhang    Specify the preallocated storage with either nz or nnz (not both).
20740298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
2075a7f22e61SSatish Balay    allocation.  See Users-Manual: ch_mat for details.
2076c464158bSHong Zhang 
207749a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
207849a6f317SBarry Smith 
207969b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateSBAIJ()
2080c464158bSHong Zhang @*/
20817087cfbeSBarry Smith PetscErrorCode  MatCreateSeqSBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
2082c464158bSHong Zhang {
2083dfbe8321SBarry Smith   PetscErrorCode ierr;
2084c464158bSHong Zhang 
2085c464158bSHong Zhang   PetscFunctionBegin;
2086f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2087f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2088c464158bSHong Zhang   ierr = MatSetType(*A,MATSEQSBAIJ);CHKERRQ(ierr);
2089ab93d7beSBarry Smith   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*A,bs,nz,(PetscInt*)nnz);CHKERRQ(ierr);
209049b5e25fSSatish Balay   PetscFunctionReturn(0);
209149b5e25fSSatish Balay }
209249b5e25fSSatish Balay 
20934a2ae208SSatish Balay #undef __FUNCT__
20944a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqSBAIJ"
2095dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqSBAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
209649b5e25fSSatish Balay {
209749b5e25fSSatish Balay   Mat            C;
209849b5e25fSSatish Balay   Mat_SeqSBAIJ   *c,*a = (Mat_SeqSBAIJ*)A->data;
20996849ba73SBarry Smith   PetscErrorCode ierr;
2100b40805acSSatish Balay   PetscInt       i,mbs = a->mbs,nz = a->nz,bs2 =a->bs2;
210149b5e25fSSatish Balay 
210249b5e25fSSatish Balay   PetscFunctionBegin;
2103e32f2f54SBarry Smith   if (a->i[mbs] != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupt matrix");
210449b5e25fSSatish Balay 
210549b5e25fSSatish Balay   *B   = 0;
2106ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2107d0f46423SBarry Smith   ierr = MatSetSizes(C,A->rmap->N,A->cmap->n,A->rmap->N,A->cmap->n);CHKERRQ(ierr);
21088e9a0fb8SHong Zhang   ierr = MatSetType(C,MATSEQSBAIJ);CHKERRQ(ierr);
21091d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
2110692f9cbeSHong Zhang   c    = (Mat_SeqSBAIJ*)C->data;
2111692f9cbeSHong Zhang 
2112273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2113d5f3da31SBarry Smith   C->factortype         = A->factortype;
211449b5e25fSSatish Balay   c->row                = 0;
211549b5e25fSSatish Balay   c->icol               = 0;
211649b5e25fSSatish Balay   c->saved_values       = 0;
2117a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
211849b5e25fSSatish Balay   C->assembled          = PETSC_TRUE;
211949b5e25fSSatish Balay 
21201e1e43feSBarry Smith   ierr   = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
21211e1e43feSBarry Smith   ierr   = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
212249b5e25fSSatish Balay   c->bs2 = a->bs2;
212349b5e25fSSatish Balay   c->mbs = a->mbs;
212449b5e25fSSatish Balay   c->nbs = a->nbs;
212549b5e25fSSatish Balay 
2126c760cd28SBarry Smith   if  (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2127c760cd28SBarry Smith     c->imax           = a->imax;
2128c760cd28SBarry Smith     c->ilen           = a->ilen;
2129c760cd28SBarry Smith     c->free_imax_ilen = PETSC_FALSE;
2130c760cd28SBarry Smith   } else {
2131dcca6d9dSJed Brown     ierr = PetscMalloc2((mbs+1),&c->imax,(mbs+1),&c->ilen);CHKERRQ(ierr);
21323bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C,2*(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
213349b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
213449b5e25fSSatish Balay       c->imax[i] = a->imax[i];
213549b5e25fSSatish Balay       c->ilen[i] = a->ilen[i];
213649b5e25fSSatish Balay     }
2137c760cd28SBarry Smith     c->free_imax_ilen = PETSC_TRUE;
2138c760cd28SBarry Smith   }
213949b5e25fSSatish Balay 
214049b5e25fSSatish Balay   /* allocate the matrix space */
21414da8f245SBarry Smith   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2142785e854fSJed Brown     ierr            = PetscMalloc1(bs2*nz,&c->a);CHKERRQ(ierr);
21433bb1ff40SBarry Smith     ierr            = PetscLogObjectMemory((PetscObject)C,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
214444e1c64aSLisandro Dalcin     c->i            = a->i;
214544e1c64aSLisandro Dalcin     c->j            = a->j;
21464da8f245SBarry Smith     c->singlemalloc = PETSC_FALSE;
214744e1c64aSLisandro Dalcin     c->free_a       = PETSC_TRUE;
21484da8f245SBarry Smith     c->free_ij      = PETSC_FALSE;
21494da8f245SBarry Smith     c->parent       = A;
21504da8f245SBarry Smith     ierr            = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
21514da8f245SBarry Smith     ierr            = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
21524da8f245SBarry Smith     ierr            = MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
21534da8f245SBarry Smith   } else {
2154dcca6d9dSJed Brown     ierr            = PetscMalloc3(bs2*nz,&c->a,nz,&c->j,mbs+1,&c->i);CHKERRQ(ierr);
215513f74950SBarry Smith     ierr            = PetscMemcpy(c->i,a->i,(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
21563bb1ff40SBarry Smith     ierr            = PetscLogObjectMemory((PetscObject)C,(mbs+1)*sizeof(PetscInt) + nz*(bs2*sizeof(MatScalar) + sizeof(PetscInt)));CHKERRQ(ierr);
21574da8f245SBarry Smith     c->singlemalloc = PETSC_TRUE;
215844e1c64aSLisandro Dalcin     c->free_a       = PETSC_TRUE;
21594da8f245SBarry Smith     c->free_ij      = PETSC_TRUE;
21604da8f245SBarry Smith   }
216149b5e25fSSatish Balay   if (mbs > 0) {
21624da8f245SBarry Smith     if (cpvalues != MAT_SHARE_NONZERO_PATTERN) {
216313f74950SBarry Smith       ierr = PetscMemcpy(c->j,a->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
21644da8f245SBarry Smith     }
216549b5e25fSSatish Balay     if (cpvalues == MAT_COPY_VALUES) {
216649b5e25fSSatish Balay       ierr = PetscMemcpy(c->a,a->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
216749b5e25fSSatish Balay     } else {
216849b5e25fSSatish Balay       ierr = PetscMemzero(c->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
216949b5e25fSSatish Balay     }
2170a1c3900fSBarry Smith     if (a->jshort) {
217144e1c64aSLisandro Dalcin       /* cannot share jshort, it is reallocated in MatAssemblyEnd_SeqSBAIJ() */
217244e1c64aSLisandro Dalcin       /* if the parent matrix is reassembled, this child matrix will never notice */
2173785e854fSJed Brown       ierr = PetscMalloc1(nz,&c->jshort);CHKERRQ(ierr);
21743bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)C,nz*sizeof(unsigned short));CHKERRQ(ierr);
2175a1c3900fSBarry Smith       ierr = PetscMemcpy(c->jshort,a->jshort,nz*sizeof(unsigned short));CHKERRQ(ierr);
217626fbe8dcSKarl Rupp 
21774da8f245SBarry Smith       c->free_jshort = PETSC_TRUE;
21784da8f245SBarry Smith     }
2179a1c3900fSBarry Smith   }
218049b5e25fSSatish Balay 
218149b5e25fSSatish Balay   c->roworiented = a->roworiented;
218249b5e25fSSatish Balay   c->nonew       = a->nonew;
218349b5e25fSSatish Balay 
218449b5e25fSSatish Balay   if (a->diag) {
2185c760cd28SBarry Smith     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2186c760cd28SBarry Smith       c->diag      = a->diag;
2187c760cd28SBarry Smith       c->free_diag = PETSC_FALSE;
2188c760cd28SBarry Smith     } else {
2189785e854fSJed Brown       ierr = PetscMalloc1(mbs,&c->diag);CHKERRQ(ierr);
21903bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)C,mbs*sizeof(PetscInt));CHKERRQ(ierr);
219126fbe8dcSKarl Rupp       for (i=0; i<mbs; i++) c->diag[i] = a->diag[i];
2192c760cd28SBarry Smith       c->free_diag = PETSC_TRUE;
2193c760cd28SBarry Smith     }
219444e1c64aSLisandro Dalcin   }
21956c6c5352SBarry Smith   c->nz         = a->nz;
2196f2cbd3d5SJed Brown   c->maxnz      = a->nz; /* Since we allocate exactly the right amount */
219749b5e25fSSatish Balay   c->solve_work = 0;
219849b5e25fSSatish Balay   c->mult_work  = 0;
219926fbe8dcSKarl Rupp 
220049b5e25fSSatish Balay   *B   = C;
2201140e18c1SBarry Smith   ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
220249b5e25fSSatish Balay   PetscFunctionReturn(0);
220349b5e25fSSatish Balay }
220449b5e25fSSatish Balay 
22054a2ae208SSatish Balay #undef __FUNCT__
22065bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_SeqSBAIJ"
2207112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqSBAIJ(Mat newmat,PetscViewer viewer)
22082f480046SShri Abhyankar {
22092f480046SShri Abhyankar   Mat_SeqSBAIJ   *a;
22102f480046SShri Abhyankar   PetscErrorCode ierr;
22112f480046SShri Abhyankar   int            fd;
22122f480046SShri Abhyankar   PetscMPIInt    size;
22133059b6faSBarry Smith   PetscInt       i,nz,header[4],*rowlengths=0,M,N,bs = newmat->rmap->bs;
22142f480046SShri Abhyankar   PetscInt       *mask,mbs,*jj,j,rowcount,nzcount,k,*s_browlengths,maskcount;
22152f480046SShri Abhyankar   PetscInt       kmax,jcount,block,idx,point,nzcountb,extra_rows,rows,cols;
22162f480046SShri Abhyankar   PetscInt       *masked,nmask,tmp,bs2,ishift;
22172f480046SShri Abhyankar   PetscScalar    *aa;
2218ce94432eSBarry Smith   MPI_Comm       comm;
22192f480046SShri Abhyankar 
22202f480046SShri Abhyankar   PetscFunctionBegin;
2221*c98fd787SBarry Smith   /* force binary viewer to load .info file if it has not yet done so */
2222*c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
2223ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
22240298fd71SBarry Smith   ierr = PetscOptionsGetInt(((PetscObject)newmat)->prefix,"-matload_block_size",&bs,NULL);CHKERRQ(ierr);
22253059b6faSBarry Smith   if (bs < 0) bs = 1;
22262f480046SShri Abhyankar   bs2  = bs*bs;
22272f480046SShri Abhyankar 
22282f480046SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
22292f480046SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
22302f480046SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
22312f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
22322f480046SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not Mat object");
22332f480046SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
22342f480046SShri Abhyankar 
22352f480046SShri Abhyankar   if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as SeqSBAIJ");
22362f480046SShri Abhyankar 
22372f480046SShri Abhyankar   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");
22382f480046SShri Abhyankar 
22392f480046SShri Abhyankar   /*
22402f480046SShri Abhyankar      This code adds extra rows to make sure the number of rows is
22412f480046SShri Abhyankar     divisible by the blocksize
22422f480046SShri Abhyankar   */
22432f480046SShri Abhyankar   mbs        = M/bs;
22442f480046SShri Abhyankar   extra_rows = bs - M + bs*(mbs);
22452f480046SShri Abhyankar   if (extra_rows == bs) extra_rows = 0;
22462f480046SShri Abhyankar   else                  mbs++;
22472f480046SShri Abhyankar   if (extra_rows) {
22482f480046SShri Abhyankar     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
22492f480046SShri Abhyankar   }
22502f480046SShri Abhyankar 
22512f480046SShri Abhyankar   /* Set global sizes if not already set */
22522f480046SShri Abhyankar   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) {
22532f480046SShri Abhyankar     ierr = MatSetSizes(newmat,PETSC_DECIDE,PETSC_DECIDE,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
22542f480046SShri Abhyankar   } else { /* Check if the matrix global sizes are correct */
22552f480046SShri Abhyankar     ierr = MatGetSize(newmat,&rows,&cols);CHKERRQ(ierr);
22562f480046SShri 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);
22572f480046SShri Abhyankar   }
22582f480046SShri Abhyankar 
22592f480046SShri Abhyankar   /* read in row lengths */
2260854ce69bSBarry Smith   ierr = PetscMalloc1(M+extra_rows,&rowlengths);CHKERRQ(ierr);
22612f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
22622f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
22632f480046SShri Abhyankar 
22642f480046SShri Abhyankar   /* read in column indices */
2265854ce69bSBarry Smith   ierr = PetscMalloc1(nz+extra_rows,&jj);CHKERRQ(ierr);
22662f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,jj,nz,PETSC_INT);CHKERRQ(ierr);
22672f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) jj[nz+i] = M+i;
22682f480046SShri Abhyankar 
22692f480046SShri Abhyankar   /* loop over row lengths determining block row lengths */
22701795a4d1SJed Brown   ierr     = PetscCalloc1(mbs,&s_browlengths);CHKERRQ(ierr);
2271dcca6d9dSJed Brown   ierr     = PetscMalloc2(mbs,&mask,mbs,&masked);CHKERRQ(ierr);
22722f480046SShri Abhyankar   ierr     = PetscMemzero(mask,mbs*sizeof(PetscInt));CHKERRQ(ierr);
22732f480046SShri Abhyankar   rowcount = 0;
22742f480046SShri Abhyankar   nzcount  = 0;
22752f480046SShri Abhyankar   for (i=0; i<mbs; i++) {
22762f480046SShri Abhyankar     nmask = 0;
22772f480046SShri Abhyankar     for (j=0; j<bs; j++) {
22782f480046SShri Abhyankar       kmax = rowlengths[rowcount];
22792f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
22802f480046SShri Abhyankar         tmp = jj[nzcount++]/bs;   /* block col. index */
22812f480046SShri Abhyankar         if (!mask[tmp] && tmp >= i) {masked[nmask++] = tmp; mask[tmp] = 1;}
22822f480046SShri Abhyankar       }
22832f480046SShri Abhyankar       rowcount++;
22842f480046SShri Abhyankar     }
22852f480046SShri Abhyankar     s_browlengths[i] += nmask;
22862f480046SShri Abhyankar 
22872f480046SShri Abhyankar     /* zero out the mask elements we set */
22882f480046SShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
22892f480046SShri Abhyankar   }
22902f480046SShri Abhyankar 
22912f480046SShri Abhyankar   /* Do preallocation */
22922f480046SShri Abhyankar   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(newmat,bs,0,s_browlengths);CHKERRQ(ierr);
22932f480046SShri Abhyankar   a    = (Mat_SeqSBAIJ*)newmat->data;
22942f480046SShri Abhyankar 
22952f480046SShri Abhyankar   /* set matrix "i" values */
22962f480046SShri Abhyankar   a->i[0] = 0;
22972f480046SShri Abhyankar   for (i=1; i<= mbs; i++) {
22982f480046SShri Abhyankar     a->i[i]      = a->i[i-1] + s_browlengths[i-1];
22992f480046SShri Abhyankar     a->ilen[i-1] = s_browlengths[i-1];
23002f480046SShri Abhyankar   }
23012f480046SShri Abhyankar   a->nz = a->i[mbs];
23022f480046SShri Abhyankar 
23032f480046SShri Abhyankar   /* read in nonzero values */
2304854ce69bSBarry Smith   ierr = PetscMalloc1(nz+extra_rows,&aa);CHKERRQ(ierr);
23052f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,aa,nz,PETSC_SCALAR);CHKERRQ(ierr);
23062f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) aa[nz+i] = 1.0;
23072f480046SShri Abhyankar 
23082f480046SShri Abhyankar   /* set "a" and "j" values into matrix */
23092f480046SShri Abhyankar   nzcount = 0; jcount = 0;
23102f480046SShri Abhyankar   for (i=0; i<mbs; i++) {
23112f480046SShri Abhyankar     nzcountb = nzcount;
23122f480046SShri Abhyankar     nmask    = 0;
23132f480046SShri Abhyankar     for (j=0; j<bs; j++) {
23142f480046SShri Abhyankar       kmax = rowlengths[i*bs+j];
23152f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
23162f480046SShri Abhyankar         tmp = jj[nzcount++]/bs; /* block col. index */
23172f480046SShri Abhyankar         if (!mask[tmp] && tmp >= i) { masked[nmask++] = tmp; mask[tmp] = 1;}
23182f480046SShri Abhyankar       }
23192f480046SShri Abhyankar     }
23202f480046SShri Abhyankar     /* sort the masked values */
23212f480046SShri Abhyankar     ierr = PetscSortInt(nmask,masked);CHKERRQ(ierr);
23222f480046SShri Abhyankar 
23232f480046SShri Abhyankar     /* set "j" values into matrix */
23242f480046SShri Abhyankar     maskcount = 1;
23252f480046SShri Abhyankar     for (j=0; j<nmask; j++) {
23262f480046SShri Abhyankar       a->j[jcount++]  = masked[j];
23272f480046SShri Abhyankar       mask[masked[j]] = maskcount++;
23282f480046SShri Abhyankar     }
23292f480046SShri Abhyankar 
23302f480046SShri Abhyankar     /* set "a" values into matrix */
23312f480046SShri Abhyankar     ishift = bs2*a->i[i];
23322f480046SShri Abhyankar     for (j=0; j<bs; j++) {
23332f480046SShri Abhyankar       kmax = rowlengths[i*bs+j];
23342f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
23352f480046SShri Abhyankar         tmp = jj[nzcountb]/bs;        /* block col. index */
23362f480046SShri Abhyankar         if (tmp >= i) {
23372f480046SShri Abhyankar           block     = mask[tmp] - 1;
23382f480046SShri Abhyankar           point     = jj[nzcountb] - bs*tmp;
23392f480046SShri Abhyankar           idx       = ishift + bs2*block + j + bs*point;
23402f480046SShri Abhyankar           a->a[idx] = aa[nzcountb];
23412f480046SShri Abhyankar         }
23422f480046SShri Abhyankar         nzcountb++;
23432f480046SShri Abhyankar       }
23442f480046SShri Abhyankar     }
23452f480046SShri Abhyankar     /* zero out the mask elements we set */
23462f480046SShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
23472f480046SShri Abhyankar   }
23482f480046SShri Abhyankar   if (jcount != a->nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Bad binary matrix");
23492f480046SShri Abhyankar 
23502f480046SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
23512f480046SShri Abhyankar   ierr = PetscFree(s_browlengths);CHKERRQ(ierr);
23522f480046SShri Abhyankar   ierr = PetscFree(aa);CHKERRQ(ierr);
23532f480046SShri Abhyankar   ierr = PetscFree(jj);CHKERRQ(ierr);
23542f480046SShri Abhyankar   ierr = PetscFree2(mask,masked);CHKERRQ(ierr);
23552f480046SShri Abhyankar 
23562f480046SShri Abhyankar   ierr = MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
23572f480046SShri Abhyankar   ierr = MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
23582f480046SShri Abhyankar   PetscFunctionReturn(0);
23592f480046SShri Abhyankar }
23602f480046SShri Abhyankar 
23612f480046SShri Abhyankar #undef __FUNCT__
2362c75a6043SHong Zhang #define __FUNCT__ "MatCreateSeqSBAIJWithArrays"
2363c75a6043SHong Zhang /*@
2364c75a6043SHong Zhang      MatCreateSeqSBAIJWithArrays - Creates an sequential SBAIJ matrix using matrix elements
2365c75a6043SHong Zhang               (upper triangular entries in CSR format) provided by the user.
2366c75a6043SHong Zhang 
2367c75a6043SHong Zhang      Collective on MPI_Comm
2368c75a6043SHong Zhang 
2369c75a6043SHong Zhang    Input Parameters:
2370c75a6043SHong Zhang +  comm - must be an MPI communicator of size 1
2371c75a6043SHong Zhang .  bs - size of block
2372c75a6043SHong Zhang .  m - number of rows
2373c75a6043SHong Zhang .  n - number of columns
2374c75a6043SHong Zhang .  i - row indices
2375c75a6043SHong Zhang .  j - column indices
2376c75a6043SHong Zhang -  a - matrix values
2377c75a6043SHong Zhang 
2378c75a6043SHong Zhang    Output Parameter:
2379c75a6043SHong Zhang .  mat - the matrix
2380c75a6043SHong Zhang 
2381dfb205c3SBarry Smith    Level: advanced
2382c75a6043SHong Zhang 
2383c75a6043SHong Zhang    Notes:
2384c75a6043SHong Zhang        The i, j, and a arrays are not copied by this routine, the user must free these arrays
2385c75a6043SHong Zhang     once the matrix is destroyed
2386c75a6043SHong Zhang 
2387c75a6043SHong Zhang        You cannot set new nonzero locations into this matrix, that will generate an error.
2388c75a6043SHong Zhang 
2389c75a6043SHong Zhang        The i and j indices are 0 based
2390c75a6043SHong Zhang 
2391dfb205c3SBarry 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
2392dfb205c3SBarry Smith        it is the regular CSR format excluding the lower triangular elements.
2393dfb205c3SBarry Smith 
239469b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSBAIJ(), MatCreateSeqSBAIJ()
2395c75a6043SHong Zhang 
2396c75a6043SHong Zhang @*/
23977087cfbeSBarry Smith PetscErrorCode  MatCreateSeqSBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
2398c75a6043SHong Zhang {
2399c75a6043SHong Zhang   PetscErrorCode ierr;
2400c75a6043SHong Zhang   PetscInt       ii;
2401c75a6043SHong Zhang   Mat_SeqSBAIJ   *sbaij;
2402c75a6043SHong Zhang 
2403c75a6043SHong Zhang   PetscFunctionBegin;
2404e32f2f54SBarry Smith   if (bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"block size %D > 1 is not supported yet",bs);
2405e32f2f54SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
2406c75a6043SHong Zhang 
2407c75a6043SHong Zhang   ierr  = MatCreate(comm,mat);CHKERRQ(ierr);
2408c75a6043SHong Zhang   ierr  = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
2409c75a6043SHong Zhang   ierr  = MatSetType(*mat,MATSEQSBAIJ);CHKERRQ(ierr);
2410c75a6043SHong Zhang   ierr  = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*mat,bs,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
2411c75a6043SHong Zhang   sbaij = (Mat_SeqSBAIJ*)(*mat)->data;
2412dcca6d9dSJed Brown   ierr  = PetscMalloc2(m,&sbaij->imax,m,&sbaij->ilen);CHKERRQ(ierr);
24133bb1ff40SBarry Smith   ierr  = PetscLogObjectMemory((PetscObject)*mat,2*m*sizeof(PetscInt));CHKERRQ(ierr);
2414c75a6043SHong Zhang 
2415c75a6043SHong Zhang   sbaij->i = i;
2416c75a6043SHong Zhang   sbaij->j = j;
2417c75a6043SHong Zhang   sbaij->a = a;
241826fbe8dcSKarl Rupp 
2419c75a6043SHong Zhang   sbaij->singlemalloc = PETSC_FALSE;
2420c75a6043SHong Zhang   sbaij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
2421e6b907acSBarry Smith   sbaij->free_a       = PETSC_FALSE;
2422e6b907acSBarry Smith   sbaij->free_ij      = PETSC_FALSE;
2423c75a6043SHong Zhang 
2424c75a6043SHong Zhang   for (ii=0; ii<m; ii++) {
2425c75a6043SHong Zhang     sbaij->ilen[ii] = sbaij->imax[ii] = i[ii+1] - i[ii];
2426c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2427e32f2f54SBarry 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]);
2428c75a6043SHong Zhang #endif
2429c75a6043SHong Zhang   }
2430c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2431c75a6043SHong Zhang   for (ii=0; ii<sbaij->i[m]; ii++) {
2432e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
2433e32f2f54SBarry 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]);
2434c75a6043SHong Zhang   }
2435c75a6043SHong Zhang #endif
2436c75a6043SHong Zhang 
2437c75a6043SHong Zhang   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2438c75a6043SHong Zhang   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2439c75a6043SHong Zhang   PetscFunctionReturn(0);
2440c75a6043SHong Zhang }
2441d06b337dSHong Zhang 
244259f5e6ceSHong Zhang #undef __FUNCT__
244359f5e6ceSHong Zhang #define __FUNCT__ "MatCreateMPIMatConcatenateSeqMat_SeqSBAIJ"
244459f5e6ceSHong Zhang PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqSBAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
244559f5e6ceSHong Zhang {
244659f5e6ceSHong Zhang   PetscErrorCode ierr;
244759f5e6ceSHong Zhang 
244859f5e6ceSHong Zhang   PetscFunctionBegin;
244959f5e6ceSHong Zhang   ierr = MatCreateMPIMatConcatenateSeqMat_MPISBAIJ(comm,inmat,n,scall,outmat);CHKERRQ(ierr);
245059f5e6ceSHong Zhang   PetscFunctionReturn(0);
245159f5e6ceSHong Zhang }
2452d06b337dSHong Zhang 
2453d06b337dSHong Zhang 
245449b5e25fSSatish Balay 
245549b5e25fSSatish Balay 
2456