xref: /petsc/src/mat/impls/sbaij/seq/sbaij.c (revision cc2e6a90c05b27ffec69cb207fe793d447f14420)
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)
16*cc2e6a90SBarry Smith PETSC_INTERN 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)
6232f7d4af7SBarry Smith     if (row >= a->mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Block index row too large %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)
6352f7d4af7SBarry Smith       if (col >= a->nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Block index column too large %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;
6902f7d4af7SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new block index nonzero block (%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 
121228be2f97SBarry Smith     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 
13547d68702bSBarry Smith #undef __FUNCT__
13557d68702bSBarry Smith #define __FUNCT__ "MatShift_SeqSBAIJ"
13567d68702bSBarry Smith PetscErrorCode MatShift_SeqSBAIJ(Mat Y,PetscScalar a)
13577d68702bSBarry Smith {
13587d68702bSBarry Smith   PetscErrorCode ierr;
13597d68702bSBarry Smith   Mat_SeqSBAIJ    *aij = (Mat_SeqSBAIJ*)Y->data;
13607d68702bSBarry Smith 
13617d68702bSBarry Smith   PetscFunctionBegin;
13626f33a894SBarry Smith   if (!Y->preallocated || !aij->nz) {
13637d68702bSBarry Smith     ierr = MatSeqSBAIJSetPreallocation(Y,Y->rmap->bs,1,NULL);CHKERRQ(ierr);
13647d68702bSBarry Smith   }
13657d68702bSBarry Smith   ierr = MatShift_Basic(Y,a);CHKERRQ(ierr);
13667d68702bSBarry Smith   PetscFunctionReturn(0);
13677d68702bSBarry Smith }
13687d68702bSBarry Smith 
136949b5e25fSSatish Balay /* -------------------------------------------------------------------*/
13703964eb88SJed Brown static struct _MatOps MatOps_Values = {MatSetValues_SeqSBAIJ,
137149b5e25fSSatish Balay                                        MatGetRow_SeqSBAIJ,
137249b5e25fSSatish Balay                                        MatRestoreRow_SeqSBAIJ,
137349b5e25fSSatish Balay                                        MatMult_SeqSBAIJ_N,
137497304618SKris Buschelman                                /*  4*/ MatMultAdd_SeqSBAIJ_N,
1375431c96f7SBarry Smith                                        MatMult_SeqSBAIJ_N,       /* transpose versions are same as non-transpose versions */
1376e005ede5SBarry Smith                                        MatMultAdd_SeqSBAIJ_N,
1377db4efbfdSBarry Smith                                        0,
137849b5e25fSSatish Balay                                        0,
137949b5e25fSSatish Balay                                        0,
138097304618SKris Buschelman                                /* 10*/ 0,
138149b5e25fSSatish Balay                                        0,
1382c078aec8SLisandro Dalcin                                        MatCholeskyFactor_SeqSBAIJ,
138341f059aeSBarry Smith                                        MatSOR_SeqSBAIJ,
138449b5e25fSSatish Balay                                        MatTranspose_SeqSBAIJ,
138597304618SKris Buschelman                                /* 15*/ MatGetInfo_SeqSBAIJ,
138649b5e25fSSatish Balay                                        MatEqual_SeqSBAIJ,
138749b5e25fSSatish Balay                                        MatGetDiagonal_SeqSBAIJ,
138849b5e25fSSatish Balay                                        MatDiagonalScale_SeqSBAIJ,
138949b5e25fSSatish Balay                                        MatNorm_SeqSBAIJ,
139097304618SKris Buschelman                                /* 20*/ 0,
139149b5e25fSSatish Balay                                        MatAssemblyEnd_SeqSBAIJ,
139249b5e25fSSatish Balay                                        MatSetOption_SeqSBAIJ,
139349b5e25fSSatish Balay                                        MatZeroEntries_SeqSBAIJ,
1394d519adbfSMatthew Knepley                                /* 24*/ 0,
139549b5e25fSSatish Balay                                        0,
139649b5e25fSSatish Balay                                        0,
1397db4efbfdSBarry Smith                                        0,
1398db4efbfdSBarry Smith                                        0,
13994994cf47SJed Brown                                /* 29*/ MatSetUp_SeqSBAIJ,
1400c464158bSHong Zhang                                        0,
1401db4efbfdSBarry Smith                                        0,
14028c778c55SBarry Smith                                        0,
14038c778c55SBarry Smith                                        0,
1404d519adbfSMatthew Knepley                                /* 34*/ MatDuplicate_SeqSBAIJ,
1405719d5645SBarry Smith                                        0,
1406719d5645SBarry Smith                                        0,
140749b5e25fSSatish Balay                                        0,
1408c84f5b01SHong Zhang                                        MatICCFactor_SeqSBAIJ,
1409d519adbfSMatthew Knepley                                /* 39*/ MatAXPY_SeqSBAIJ,
141049b5e25fSSatish Balay                                        MatGetSubMatrices_SeqSBAIJ,
141149b5e25fSSatish Balay                                        MatIncreaseOverlap_SeqSBAIJ,
141249b5e25fSSatish Balay                                        MatGetValues_SeqSBAIJ,
14133c896bc6SHong Zhang                                        MatCopy_SeqSBAIJ,
1414d519adbfSMatthew Knepley                                /* 44*/ 0,
141549b5e25fSSatish Balay                                        MatScale_SeqSBAIJ,
14167d68702bSBarry Smith                                        MatShift_SeqSBAIJ,
141749b5e25fSSatish Balay                                        0,
14183bededecSBarry Smith                                        MatZeroRowsColumns_SeqSBAIJ,
1419f73d5cc4SBarry Smith                                /* 49*/ 0,
142049b5e25fSSatish Balay                                        MatGetRowIJ_SeqSBAIJ,
142149b5e25fSSatish Balay                                        MatRestoreRowIJ_SeqSBAIJ,
142249b5e25fSSatish Balay                                        0,
142349b5e25fSSatish Balay                                        0,
1424d519adbfSMatthew Knepley                                /* 54*/ 0,
142549b5e25fSSatish Balay                                        0,
142649b5e25fSSatish Balay                                        0,
142749b5e25fSSatish Balay                                        0,
142849b5e25fSSatish Balay                                        MatSetValuesBlocked_SeqSBAIJ,
1429d519adbfSMatthew Knepley                                /* 59*/ MatGetSubMatrix_SeqSBAIJ,
143049b5e25fSSatish Balay                                        0,
143149b5e25fSSatish Balay                                        0,
1432357abbc8SBarry Smith                                        0,
1433d959ec07SHong Zhang                                        0,
1434d519adbfSMatthew Knepley                                /* 64*/ 0,
1435d959ec07SHong Zhang                                        0,
1436d959ec07SHong Zhang                                        0,
1437d959ec07SHong Zhang                                        0,
1438d959ec07SHong Zhang                                        0,
1439d519adbfSMatthew Knepley                                /* 69*/ MatGetRowMaxAbs_SeqSBAIJ,
14403e0d88b5SBarry Smith                                        0,
14413e0d88b5SBarry Smith                                        0,
14423e0d88b5SBarry Smith                                        0,
14433e0d88b5SBarry Smith                                        0,
1444d519adbfSMatthew Knepley                                /* 74*/ 0,
14453e0d88b5SBarry Smith                                        0,
14463e0d88b5SBarry Smith                                        0,
14473e0d88b5SBarry Smith                                        0,
14483e0d88b5SBarry Smith                                        0,
1449d519adbfSMatthew Knepley                                /* 79*/ 0,
14503e0d88b5SBarry Smith                                        0,
14513e0d88b5SBarry Smith                                        0,
145297304618SKris Buschelman                                        MatGetInertia_SeqSBAIJ,
14535bba2384SShri Abhyankar                                        MatLoad_SeqSBAIJ,
1454d519adbfSMatthew Knepley                                /* 84*/ MatIsSymmetric_SeqSBAIJ,
1455865e5f61SKris Buschelman                                        MatIsHermitian_SeqSBAIJ,
1456efcf0fc3SBarry Smith                                        MatIsStructurallySymmetric_SeqSBAIJ,
1457865e5f61SKris Buschelman                                        0,
1458865e5f61SKris Buschelman                                        0,
1459d519adbfSMatthew Knepley                                /* 89*/ 0,
1460865e5f61SKris Buschelman                                        0,
1461865e5f61SKris Buschelman                                        0,
1462865e5f61SKris Buschelman                                        0,
1463865e5f61SKris Buschelman                                        0,
1464d519adbfSMatthew Knepley                                /* 94*/ 0,
1465865e5f61SKris Buschelman                                        0,
1466865e5f61SKris Buschelman                                        0,
146799cafbc1SBarry Smith                                        0,
146899cafbc1SBarry Smith                                        0,
1469d519adbfSMatthew Knepley                                /* 99*/ 0,
147099cafbc1SBarry Smith                                        0,
147199cafbc1SBarry Smith                                        0,
147299cafbc1SBarry Smith                                        0,
147399cafbc1SBarry Smith                                        0,
1474d519adbfSMatthew Knepley                                /*104*/ 0,
147599cafbc1SBarry Smith                                        MatRealPart_SeqSBAIJ,
1476f5edf698SHong Zhang                                        MatImaginaryPart_SeqSBAIJ,
1477f5edf698SHong Zhang                                        MatGetRowUpperTriangular_SeqSBAIJ,
14782af78befSBarry Smith                                        MatRestoreRowUpperTriangular_SeqSBAIJ,
1479d519adbfSMatthew Knepley                                /*109*/ 0,
14802af78befSBarry Smith                                        0,
14812af78befSBarry Smith                                        0,
14822af78befSBarry Smith                                        0,
1483547795f9SHong Zhang                                        MatMissingDiagonal_SeqSBAIJ,
1484547795f9SHong Zhang                                /*114*/ 0,
1485547795f9SHong Zhang                                        0,
1486547795f9SHong Zhang                                        0,
1487547795f9SHong Zhang                                        0,
1488547795f9SHong Zhang                                        0,
1489547795f9SHong Zhang                                /*119*/ 0,
1490547795f9SHong Zhang                                        0,
14912f480046SShri Abhyankar                                        0,
14923964eb88SJed Brown                                        0,
14933964eb88SJed Brown                                        0,
14943964eb88SJed Brown                                /*124*/ 0,
14953964eb88SJed Brown                                        0,
14963964eb88SJed Brown                                        0,
14973964eb88SJed Brown                                        0,
14983964eb88SJed Brown                                        0,
14993964eb88SJed Brown                                /*129*/ 0,
15003964eb88SJed Brown                                        0,
15013964eb88SJed Brown                                        0,
15023964eb88SJed Brown                                        0,
15033964eb88SJed Brown                                        0,
15043964eb88SJed Brown                                /*134*/ 0,
15053964eb88SJed Brown                                        0,
15063964eb88SJed Brown                                        0,
15073964eb88SJed Brown                                        0,
15083964eb88SJed Brown                                        0,
15093964eb88SJed Brown                                /*139*/ 0,
1510f9426fe0SMark Adams                                        0,
151159f5e6ceSHong Zhang                                        0,
151259f5e6ceSHong Zhang                                        0,
151359f5e6ceSHong Zhang                                        0,
151459f5e6ceSHong Zhang                                 /*144*/MatCreateMPIMatConcatenateSeqMat_SeqSBAIJ
151599cafbc1SBarry Smith };
1516be1d678aSKris Buschelman 
15174a2ae208SSatish Balay #undef __FUNCT__
15184a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqSBAIJ"
15197087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqSBAIJ(Mat mat)
152049b5e25fSSatish Balay {
15214afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ*)mat->data;
1522d0f46423SBarry Smith   PetscInt       nz   = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
1523dfbe8321SBarry Smith   PetscErrorCode ierr;
152449b5e25fSSatish Balay 
152549b5e25fSSatish Balay   PetscFunctionBegin;
1526e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
152749b5e25fSSatish Balay 
152849b5e25fSSatish Balay   /* allocate space for values if not already there */
152949b5e25fSSatish Balay   if (!aij->saved_values) {
1530854ce69bSBarry Smith     ierr = PetscMalloc1(nz+1,&aij->saved_values);CHKERRQ(ierr);
153149b5e25fSSatish Balay   }
153249b5e25fSSatish Balay 
153349b5e25fSSatish Balay   /* copy values over */
153487828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
153549b5e25fSSatish Balay   PetscFunctionReturn(0);
153649b5e25fSSatish Balay }
153749b5e25fSSatish Balay 
15384a2ae208SSatish Balay #undef __FUNCT__
15394a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqSBAIJ"
15407087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqSBAIJ(Mat mat)
154149b5e25fSSatish Balay {
15424afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ*)mat->data;
15436849ba73SBarry Smith   PetscErrorCode ierr;
1544d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
154549b5e25fSSatish Balay 
154649b5e25fSSatish Balay   PetscFunctionBegin;
1547e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
1548e7e72b3dSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
154949b5e25fSSatish Balay 
155049b5e25fSSatish Balay   /* copy values over */
155187828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
155249b5e25fSSatish Balay   PetscFunctionReturn(0);
155349b5e25fSSatish Balay }
155449b5e25fSSatish Balay 
15554a2ae208SSatish Balay #undef __FUNCT__
1556a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation_SeqSBAIJ"
15577087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetPreallocation_SeqSBAIJ(Mat B,PetscInt bs,PetscInt nz,PetscInt *nnz)
155849b5e25fSSatish Balay {
1559c464158bSHong Zhang   Mat_SeqSBAIJ   *b = (Mat_SeqSBAIJ*)B->data;
15606849ba73SBarry Smith   PetscErrorCode ierr;
15614dcd73b1SHong Zhang   PetscInt       i,mbs,nbs,bs2;
15622576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,flg = PETSC_FALSE,realalloc = PETSC_FALSE;
156349b5e25fSSatish Balay 
156449b5e25fSSatish Balay   PetscFunctionBegin;
15652576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
1566273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
1567db4efbfdSBarry Smith 
156833d57670SJed Brown   ierr = MatSetBlockSize(B,PetscAbs(bs));CHKERRQ(ierr);
156926283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
157026283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
1571e02043d6SBarry Smith   ierr = PetscLayoutGetBlockSize(B->rmap,&bs);CHKERRQ(ierr);
1572899cda47SBarry Smith 
1573d0f46423SBarry Smith   mbs = B->rmap->N/bs;
15744dcd73b1SHong Zhang   nbs = B->cmap->n/bs;
157549b5e25fSSatish Balay   bs2 = bs*bs;
157649b5e25fSSatish Balay 
15774dcd73b1SHong 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");
157849b5e25fSSatish Balay 
1579ab93d7beSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
1580ab93d7beSBarry Smith     skipallocation = PETSC_TRUE;
1581ab93d7beSBarry Smith     nz             = 0;
1582ab93d7beSBarry Smith   }
1583ab93d7beSBarry Smith 
1584435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 3;
1585e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
158649b5e25fSSatish Balay   if (nnz) {
158749b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
1588e32f2f54SBarry 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]);
1589de64b629SHong 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);
159049b5e25fSSatish Balay     }
159149b5e25fSSatish Balay   }
159249b5e25fSSatish Balay 
1593db4efbfdSBarry Smith   B->ops->mult             = MatMult_SeqSBAIJ_N;
1594db4efbfdSBarry Smith   B->ops->multadd          = MatMultAdd_SeqSBAIJ_N;
1595db4efbfdSBarry Smith   B->ops->multtranspose    = MatMult_SeqSBAIJ_N;
1596db4efbfdSBarry Smith   B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_N;
159726fbe8dcSKarl Rupp 
1598c5929fdfSBarry Smith   ierr  = PetscOptionsGetBool(((PetscObject)B)->options,((PetscObject)B)->prefix,"-mat_no_unroll",&flg,NULL);CHKERRQ(ierr);
159949b5e25fSSatish Balay   if (!flg) {
160049b5e25fSSatish Balay     switch (bs) {
160149b5e25fSSatish Balay     case 1:
160249b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_1;
160349b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_1;
1604431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_1;
1605431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_1;
160649b5e25fSSatish Balay       break;
160749b5e25fSSatish Balay     case 2:
160849b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_2;
160949b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_2;
1610431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_2;
1611431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_2;
161249b5e25fSSatish Balay       break;
161349b5e25fSSatish Balay     case 3:
161449b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_3;
161549b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_3;
1616431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_3;
1617431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_3;
161849b5e25fSSatish Balay       break;
161949b5e25fSSatish Balay     case 4:
162049b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_4;
162149b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_4;
1622431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_4;
1623431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_4;
162449b5e25fSSatish Balay       break;
162549b5e25fSSatish Balay     case 5:
162649b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_5;
162749b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_5;
1628431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_5;
1629431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_5;
163049b5e25fSSatish Balay       break;
163149b5e25fSSatish Balay     case 6:
163249b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_6;
163349b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_6;
1634431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_6;
1635431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_6;
163649b5e25fSSatish Balay       break;
163749b5e25fSSatish Balay     case 7:
1638de53e5efSHong Zhang       B->ops->mult             = MatMult_SeqSBAIJ_7;
163949b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_7;
1640431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_7;
1641431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_7;
164249b5e25fSSatish Balay       break;
164349b5e25fSSatish Balay     }
164449b5e25fSSatish Balay   }
164549b5e25fSSatish Balay 
164649b5e25fSSatish Balay   b->mbs = mbs;
16474dcd73b1SHong Zhang   b->nbs = nbs;
1648ab93d7beSBarry Smith   if (!skipallocation) {
16492ee49352SLisandro Dalcin     if (!b->imax) {
1650dcca6d9dSJed Brown       ierr = PetscMalloc2(mbs,&b->imax,mbs,&b->ilen);CHKERRQ(ierr);
165126fbe8dcSKarl Rupp 
1652c760cd28SBarry Smith       b->free_imax_ilen = PETSC_TRUE;
165326fbe8dcSKarl Rupp 
16543bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,2*mbs*sizeof(PetscInt));CHKERRQ(ierr);
16552ee49352SLisandro Dalcin     }
165649b5e25fSSatish Balay     if (!nnz) {
1657435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
165849b5e25fSSatish Balay       else if (nz <= 0) nz = 1;
165926fbe8dcSKarl Rupp       for (i=0; i<mbs; i++) b->imax[i] = nz;
1660153ea458SHong Zhang       nz = nz*mbs; /* total nz */
166149b5e25fSSatish Balay     } else {
166249b5e25fSSatish Balay       nz = 0;
16638cef66ccSHong Zhang       for (i=0; i<mbs; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
166449b5e25fSSatish Balay     }
16652ee49352SLisandro Dalcin     /* b->ilen will count nonzeros in each block row so far. */
166626fbe8dcSKarl Rupp     for (i=0; i<mbs; i++) b->ilen[i] = 0;
16676c6c5352SBarry Smith     /* nz=(nz+mbs)/2; */ /* total diagonal and superdiagonal nonzero blocks */
166849b5e25fSSatish Balay 
166949b5e25fSSatish Balay     /* allocate the matrix space */
16702ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
1671dcca6d9dSJed Brown     ierr = PetscMalloc3(bs2*nz,&b->a,nz,&b->j,B->rmap->N+1,&b->i);CHKERRQ(ierr);
16723bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)B,(B->rmap->N+1)*sizeof(PetscInt)+nz*(bs2*sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
16736c6c5352SBarry Smith     ierr = PetscMemzero(b->a,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
167413f74950SBarry Smith     ierr = PetscMemzero(b->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
167526fbe8dcSKarl Rupp 
167649b5e25fSSatish Balay     b->singlemalloc = PETSC_TRUE;
167749b5e25fSSatish Balay 
167849b5e25fSSatish Balay     /* pointer to beginning of each row */
1679e60cf9a0SBarry Smith     b->i[0] = 0;
168026fbe8dcSKarl Rupp     for (i=1; i<mbs+1; i++) b->i[i] = b->i[i-1] + b->imax[i-1];
168126fbe8dcSKarl Rupp 
1682e6b907acSBarry Smith     b->free_a  = PETSC_TRUE;
1683e6b907acSBarry Smith     b->free_ij = PETSC_TRUE;
1684e811da20SHong Zhang   } else {
1685e6b907acSBarry Smith     b->free_a  = PETSC_FALSE;
1686e6b907acSBarry Smith     b->free_ij = PETSC_FALSE;
1687ab93d7beSBarry Smith   }
168849b5e25fSSatish Balay 
1689d0f46423SBarry Smith   B->rmap->bs = bs;
169049b5e25fSSatish Balay   b->bs2      = bs2;
16916c6c5352SBarry Smith   b->nz       = 0;
1692b32cb4a7SJed Brown   b->maxnz    = nz;
1693153ea458SHong Zhang 
169416cdd363SHong Zhang   b->inew    = 0;
169516cdd363SHong Zhang   b->jnew    = 0;
169616cdd363SHong Zhang   b->anew    = 0;
169716cdd363SHong Zhang   b->a2anew  = 0;
16981a3463dfSHong Zhang   b->permute = PETSC_FALSE;
16992576faa2SJed Brown   if (realalloc) {ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);}
1700c464158bSHong Zhang   PetscFunctionReturn(0);
1701c464158bSHong Zhang }
1702153ea458SHong Zhang 
170338f409ebSLisandro Dalcin #undef __FUNCT__
170438f409ebSLisandro Dalcin #define __FUNCT__ "MatSeqSBAIJSetPreallocationCSR_SeqSBAIJ"
170538f409ebSLisandro Dalcin PetscErrorCode MatSeqSBAIJSetPreallocationCSR_SeqSBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[], const PetscScalar V[])
170638f409ebSLisandro Dalcin {
170738f409ebSLisandro Dalcin   PetscInt       i,j,m,nz,nz_max=0,*nnz;
170838f409ebSLisandro Dalcin   PetscScalar    *values=0;
170938f409ebSLisandro Dalcin   PetscBool      roworiented = ((Mat_SeqSBAIJ*)B->data)->roworiented;
171038f409ebSLisandro Dalcin   PetscErrorCode ierr;
171138f409ebSLisandro Dalcin   PetscFunctionBegin;
171238f409ebSLisandro Dalcin   if (bs < 1) SETERRQ1(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive but it is %D",bs);
171338f409ebSLisandro Dalcin   ierr   = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
171438f409ebSLisandro Dalcin   ierr   = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
171538f409ebSLisandro Dalcin   ierr   = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
171638f409ebSLisandro Dalcin   ierr   = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
171738f409ebSLisandro Dalcin   ierr   = PetscLayoutGetBlockSize(B->rmap,&bs);CHKERRQ(ierr);
171838f409ebSLisandro Dalcin   m      = B->rmap->n/bs;
171938f409ebSLisandro Dalcin 
172038f409ebSLisandro Dalcin   if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]);
1721854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&nnz);CHKERRQ(ierr);
172238f409ebSLisandro Dalcin   for (i=0; i<m; i++) {
172338f409ebSLisandro Dalcin     nz = ii[i+1] - ii[i];
172438f409ebSLisandro Dalcin     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D has a negative number of columns %D",i,nz);
172538f409ebSLisandro Dalcin     nz_max = PetscMax(nz_max,nz);
172638f409ebSLisandro Dalcin     nnz[i] = nz;
172738f409ebSLisandro Dalcin   }
172838f409ebSLisandro Dalcin   ierr = MatSeqSBAIJSetPreallocation(B,bs,0,nnz);CHKERRQ(ierr);
172938f409ebSLisandro Dalcin   ierr = PetscFree(nnz);CHKERRQ(ierr);
173038f409ebSLisandro Dalcin 
173138f409ebSLisandro Dalcin   values = (PetscScalar*)V;
173238f409ebSLisandro Dalcin   if (!values) {
17331795a4d1SJed Brown     ierr = PetscCalloc1(bs*bs*nz_max,&values);CHKERRQ(ierr);
173438f409ebSLisandro Dalcin   }
173538f409ebSLisandro Dalcin   for (i=0; i<m; i++) {
173638f409ebSLisandro Dalcin     PetscInt          ncols  = ii[i+1] - ii[i];
173738f409ebSLisandro Dalcin     const PetscInt    *icols = jj + ii[i];
173838f409ebSLisandro Dalcin     if (!roworiented || bs == 1) {
173938f409ebSLisandro Dalcin       const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
174038f409ebSLisandro Dalcin       ierr = MatSetValuesBlocked_SeqSBAIJ(B,1,&i,ncols,icols,svals,INSERT_VALUES);CHKERRQ(ierr);
174138f409ebSLisandro Dalcin     } else {
174238f409ebSLisandro Dalcin       for (j=0; j<ncols; j++) {
174338f409ebSLisandro Dalcin         const PetscScalar *svals = values + (V ? (bs*bs*(ii[i]+j)) : 0);
174438f409ebSLisandro Dalcin         ierr = MatSetValuesBlocked_SeqSBAIJ(B,1,&i,1,&icols[j],svals,INSERT_VALUES);CHKERRQ(ierr);
174538f409ebSLisandro Dalcin       }
174638f409ebSLisandro Dalcin     }
174738f409ebSLisandro Dalcin   }
174838f409ebSLisandro Dalcin   if (!V) { ierr = PetscFree(values);CHKERRQ(ierr); }
174938f409ebSLisandro Dalcin   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
175038f409ebSLisandro Dalcin   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
175138f409ebSLisandro Dalcin   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
175238f409ebSLisandro Dalcin   PetscFunctionReturn(0);
175338f409ebSLisandro Dalcin }
175438f409ebSLisandro Dalcin 
1755db4efbfdSBarry Smith /*
1756db4efbfdSBarry Smith    This is used to set the numeric factorization for both Cholesky and ICC symbolic factorization
1757db4efbfdSBarry Smith */
17588b1456e3SHong Zhang #undef __FUNCT__
1759d595f711SHong Zhang #define __FUNCT__ "MatSeqSBAIJSetNumericFactorization_inplace"
1760ace3abfcSBarry Smith PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat B,PetscBool natural)
1761db4efbfdSBarry Smith {
1762db4efbfdSBarry Smith   PetscErrorCode ierr;
1763ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
1764db4efbfdSBarry Smith   PetscInt       bs  = B->rmap->bs;
1765db4efbfdSBarry Smith 
1766db4efbfdSBarry Smith   PetscFunctionBegin;
1767c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)B)->options,((PetscObject)B)->prefix,"-mat_no_unroll",&flg,NULL);CHKERRQ(ierr);
1768db4efbfdSBarry Smith   if (flg) bs = 8;
1769db4efbfdSBarry Smith 
1770db4efbfdSBarry Smith   if (!natural) {
1771db4efbfdSBarry Smith     switch (bs) {
1772db4efbfdSBarry Smith     case 1:
1773d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_inplace;
1774db4efbfdSBarry Smith       break;
1775db4efbfdSBarry Smith     case 2:
1776db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2;
1777db4efbfdSBarry Smith       break;
1778db4efbfdSBarry Smith     case 3:
1779db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3;
1780db4efbfdSBarry Smith       break;
1781db4efbfdSBarry Smith     case 4:
1782db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4;
1783db4efbfdSBarry Smith       break;
1784db4efbfdSBarry Smith     case 5:
1785db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5;
1786db4efbfdSBarry Smith       break;
1787db4efbfdSBarry Smith     case 6:
1788db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6;
1789db4efbfdSBarry Smith       break;
1790db4efbfdSBarry Smith     case 7:
1791db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7;
1792db4efbfdSBarry Smith       break;
1793db4efbfdSBarry Smith     default:
1794db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N;
1795db4efbfdSBarry Smith       break;
1796db4efbfdSBarry Smith     }
1797db4efbfdSBarry Smith   } else {
1798db4efbfdSBarry Smith     switch (bs) {
1799db4efbfdSBarry Smith     case 1:
1800d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering_inplace;
1801db4efbfdSBarry Smith       break;
1802db4efbfdSBarry Smith     case 2:
1803db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering;
1804db4efbfdSBarry Smith       break;
1805db4efbfdSBarry Smith     case 3:
1806db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3_NaturalOrdering;
1807db4efbfdSBarry Smith       break;
1808db4efbfdSBarry Smith     case 4:
1809db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4_NaturalOrdering;
1810db4efbfdSBarry Smith       break;
1811db4efbfdSBarry Smith     case 5:
1812db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5_NaturalOrdering;
1813db4efbfdSBarry Smith       break;
1814db4efbfdSBarry Smith     case 6:
1815db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6_NaturalOrdering;
1816db4efbfdSBarry Smith       break;
1817db4efbfdSBarry Smith     case 7:
1818db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7_NaturalOrdering;
1819db4efbfdSBarry Smith       break;
1820db4efbfdSBarry Smith     default:
1821db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering;
1822db4efbfdSBarry Smith       break;
1823db4efbfdSBarry Smith     }
1824db4efbfdSBarry Smith   }
1825db4efbfdSBarry Smith   PetscFunctionReturn(0);
1826db4efbfdSBarry Smith }
1827db4efbfdSBarry Smith 
1828*cc2e6a90SBarry Smith PETSC_INTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqAIJ(Mat, MatType,MatReuse,Mat*);
1829*cc2e6a90SBarry Smith PETSC_INTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqBAIJ(Mat, MatType,MatReuse,Mat*);
1830d769727bSBarry Smith 
18315c9eb25fSBarry Smith #undef __FUNCT__
18325c9eb25fSBarry Smith #define __FUNCT__ "MatGetFactor_seqsbaij_petsc"
1833*cc2e6a90SBarry Smith PETSC_INTERN PetscErrorCode MatGetFactor_seqsbaij_petsc(Mat A,MatFactorType ftype,Mat *B)
18345c9eb25fSBarry Smith {
1835d0f46423SBarry Smith   PetscInt       n = A->rmap->n;
18365c9eb25fSBarry Smith   PetscErrorCode ierr;
18375c9eb25fSBarry Smith 
18385c9eb25fSBarry Smith   PetscFunctionBegin;
18390e92d65fSHong Zhang #if defined(PETSC_USE_COMPLEX)
18400e92d65fSHong Zhang   if (A->hermitian) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Hermitian Factor is not supported");
18410e92d65fSHong Zhang #endif
1842ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
18435c9eb25fSBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
18445c9eb25fSBarry Smith   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
18455c9eb25fSBarry Smith     ierr = MatSetType(*B,MATSEQSBAIJ);CHKERRQ(ierr);
18460298fd71SBarry Smith     ierr = MatSeqSBAIJSetPreallocation(*B,A->rmap->bs,MAT_SKIP_ALLOCATION,NULL);CHKERRQ(ierr);
184726fbe8dcSKarl Rupp 
18487b056e98SHong Zhang     (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqSBAIJ;
1849c6d0d4f0SHong Zhang     (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqSBAIJ;
1850e32f2f54SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported");
1851d5f3da31SBarry Smith   (*B)->factortype = ftype;
18525c9eb25fSBarry Smith   PetscFunctionReturn(0);
18535c9eb25fSBarry Smith }
18545c9eb25fSBarry Smith 
18550bad9183SKris Buschelman /*MC
1856fafad747SKris Buschelman   MATSEQSBAIJ - MATSEQSBAIJ = "seqsbaij" - A matrix type to be used for sequential symmetric block sparse matrices,
18570bad9183SKris Buschelman   based on block compressed sparse row format.  Only the upper triangular portion of the matrix is stored.
18580bad9183SKris Buschelman 
1859828413b8SBarry Smith   For complex numbers by default this matrix is symmetric, NOT Hermitian symmetric. To make it Hermitian symmetric you
186071dad5bbSBarry Smith   can call MatSetOption(Mat, MAT_HERMITIAN); after MatAssemblyEnd()
1861828413b8SBarry Smith 
18620bad9183SKris Buschelman   Options Database Keys:
18630bad9183SKris Buschelman   . -mat_type seqsbaij - sets the matrix type to "seqsbaij" during a call to MatSetFromOptions()
18640bad9183SKris Buschelman 
186571dad5bbSBarry Smith   Notes: By default if you insert values into the lower triangular part of the matrix they are simply ignored (since they are not
186671dad5bbSBarry 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
186771dad5bbSBarry 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.
186871dad5bbSBarry Smith 
186971dad5bbSBarry Smith 
18700bad9183SKris Buschelman   Level: beginner
18710bad9183SKris Buschelman 
18720bad9183SKris Buschelman   .seealso: MatCreateSeqSBAIJ
18730bad9183SKris Buschelman M*/
18740bad9183SKris Buschelman 
1875*cc2e6a90SBarry Smith PETSC_INTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqSBSTRM(Mat, MatType,MatReuse,Mat*);
1876aa5a9175SDahai Guo 
1877a23d5eceSKris Buschelman #undef __FUNCT__
1878a23d5eceSKris Buschelman #define __FUNCT__ "MatCreate_SeqSBAIJ"
18798cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqSBAIJ(Mat B)
1880a23d5eceSKris Buschelman {
1881a23d5eceSKris Buschelman   Mat_SeqSBAIJ   *b;
1882dfbe8321SBarry Smith   PetscErrorCode ierr;
188313f74950SBarry Smith   PetscMPIInt    size;
1884ace3abfcSBarry Smith   PetscBool      no_unroll = PETSC_FALSE,no_inode = PETSC_FALSE;
1885a23d5eceSKris Buschelman 
1886a23d5eceSKris Buschelman   PetscFunctionBegin;
1887ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
1888e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Comm must be of size 1");
1889a23d5eceSKris Buschelman 
1890b00a9115SJed Brown   ierr    = PetscNewLog(B,&b);CHKERRQ(ierr);
1891a23d5eceSKris Buschelman   B->data = (void*)b;
1892a23d5eceSKris Buschelman   ierr    = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
189326fbe8dcSKarl Rupp 
1894a23d5eceSKris Buschelman   B->ops->destroy    = MatDestroy_SeqSBAIJ;
1895a23d5eceSKris Buschelman   B->ops->view       = MatView_SeqSBAIJ;
1896a23d5eceSKris Buschelman   b->row             = 0;
1897a23d5eceSKris Buschelman   b->icol            = 0;
1898a23d5eceSKris Buschelman   b->reallocs        = 0;
1899a23d5eceSKris Buschelman   b->saved_values    = 0;
19000def2e27SBarry Smith   b->inode.limit     = 5;
19010def2e27SBarry Smith   b->inode.max_limit = 5;
1902a23d5eceSKris Buschelman 
1903a23d5eceSKris Buschelman   b->roworiented        = PETSC_TRUE;
1904a23d5eceSKris Buschelman   b->nonew              = 0;
1905a23d5eceSKris Buschelman   b->diag               = 0;
1906a23d5eceSKris Buschelman   b->solve_work         = 0;
1907a23d5eceSKris Buschelman   b->mult_work          = 0;
1908a23d5eceSKris Buschelman   B->spptr              = 0;
1909f2cbd3d5SJed Brown   B->info.nz_unneeded   = (PetscReal)b->maxnz*b->bs2;
1910a9817697SBarry Smith   b->keepnonzeropattern = PETSC_FALSE;
1911a23d5eceSKris Buschelman 
1912a23d5eceSKris Buschelman   b->inew    = 0;
1913a23d5eceSKris Buschelman   b->jnew    = 0;
1914a23d5eceSKris Buschelman   b->anew    = 0;
1915a23d5eceSKris Buschelman   b->a2anew  = 0;
1916a23d5eceSKris Buschelman   b->permute = PETSC_FALSE;
1917a23d5eceSKris Buschelman 
191871dad5bbSBarry Smith   b->ignore_ltriangular = PETSC_TRUE;
191926fbe8dcSKarl Rupp 
1920c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)B)->options,((PetscObject)B)->prefix,"-mat_ignore_lower_triangular",&b->ignore_ltriangular,NULL);CHKERRQ(ierr);
1921941593c8SHong Zhang 
1922f5edf698SHong Zhang   b->getrow_utriangular = PETSC_FALSE;
192326fbe8dcSKarl Rupp 
1924c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)B)->options,((PetscObject)B)->prefix,"-mat_getrow_uppertriangular",&b->getrow_utriangular,NULL);CHKERRQ(ierr);
1925f5edf698SHong Zhang 
1926bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqSBAIJ);CHKERRQ(ierr);
1927bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqSBAIJ);CHKERRQ(ierr);
1928bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqSBAIJSetColumnIndices_C",MatSeqSBAIJSetColumnIndices_SeqSBAIJ);CHKERRQ(ierr);
1929bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_seqaij_C",MatConvert_SeqSBAIJ_SeqAIJ);CHKERRQ(ierr);
1930bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_seqbaij_C",MatConvert_SeqSBAIJ_SeqBAIJ);CHKERRQ(ierr);
1931bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqSBAIJSetPreallocation_C",MatSeqSBAIJSetPreallocation_SeqSBAIJ);CHKERRQ(ierr);
193238f409ebSLisandro Dalcin   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqSBAIJSetPreallocationCSR_C",MatSeqSBAIJSetPreallocationCSR_SeqSBAIJ);CHKERRQ(ierr);
1933bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_seqsbstrm_C",MatConvert_SeqSBAIJ_SeqSBSTRM);CHKERRQ(ierr);
19346214f412SHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
19356214f412SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_elemental_C",MatConvert_SeqSBAIJ_Elemental);CHKERRQ(ierr);
19366214f412SHong Zhang #endif
193723ce1328SBarry Smith 
193823ce1328SBarry Smith   B->symmetric                  = PETSC_TRUE;
193923ce1328SBarry Smith   B->structurally_symmetric     = PETSC_TRUE;
194023ce1328SBarry Smith   B->symmetric_set              = PETSC_TRUE;
194123ce1328SBarry Smith   B->structurally_symmetric_set = PETSC_TRUE;
194226fbe8dcSKarl Rupp 
194317667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQSBAIJ);CHKERRQ(ierr);
19440def2e27SBarry Smith 
1945ce94432eSBarry Smith   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)B),((PetscObject)B)->prefix,"Options for SEQSBAIJ matrix","Mat");CHKERRQ(ierr);
19460298fd71SBarry Smith   ierr = PetscOptionsBool("-mat_no_unroll","Do not optimize for inodes (slower)",NULL,no_unroll,&no_unroll,NULL);CHKERRQ(ierr);
194726fbe8dcSKarl Rupp   if (no_unroll) {
194826fbe8dcSKarl Rupp     ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_unroll\n");CHKERRQ(ierr);
194926fbe8dcSKarl Rupp   }
19500298fd71SBarry Smith   ierr = PetscOptionsBool("-mat_no_inode","Do not optimize for inodes (slower)",NULL,no_inode,&no_inode,NULL);CHKERRQ(ierr);
195126fbe8dcSKarl Rupp   if (no_inode) {
195226fbe8dcSKarl Rupp     ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_inode\n");CHKERRQ(ierr);
195326fbe8dcSKarl Rupp   }
19540298fd71SBarry Smith   ierr = PetscOptionsInt("-mat_inode_limit","Do not use inodes larger then this value",NULL,b->inode.limit,&b->inode.limit,NULL);CHKERRQ(ierr);
19550def2e27SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1956ace3abfcSBarry Smith   b->inode.use = (PetscBool)(!(no_unroll || no_inode));
19570def2e27SBarry Smith   if (b->inode.limit > b->inode.max_limit) b->inode.limit = b->inode.max_limit;
1958a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1959a23d5eceSKris Buschelman }
1960a23d5eceSKris Buschelman 
1961a23d5eceSKris Buschelman #undef __FUNCT__
1962a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation"
1963a23d5eceSKris Buschelman /*@C
1964a23d5eceSKris Buschelman    MatSeqSBAIJSetPreallocation - Creates a sparse symmetric matrix in block AIJ (block
1965a23d5eceSKris Buschelman    compressed row) format.  For good matrix assembly performance the
1966a23d5eceSKris Buschelman    user should preallocate the matrix storage by setting the parameter nz
1967a23d5eceSKris Buschelman    (or the array nnz).  By setting these parameters accurately, performance
1968a23d5eceSKris Buschelman    during matrix assembly can be increased by more than a factor of 50.
1969a23d5eceSKris Buschelman 
1970a23d5eceSKris Buschelman    Collective on Mat
1971a23d5eceSKris Buschelman 
1972a23d5eceSKris Buschelman    Input Parameters:
19731c4f3114SJed Brown +  B - the symmetric matrix
1974bb7ae925SBarry 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
1975bb7ae925SBarry Smith           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
1976a23d5eceSKris Buschelman .  nz - number of block nonzeros per block row (same for all rows)
1977a23d5eceSKris Buschelman -  nnz - array containing the number of block nonzeros in the upper triangular plus
19780298fd71SBarry Smith          diagonal portion of each block (possibly different for each block row) or NULL
1979a23d5eceSKris Buschelman 
1980a23d5eceSKris Buschelman    Options Database Keys:
1981a23d5eceSKris Buschelman .   -mat_no_unroll - uses code that does not unroll the loops in the
1982a23d5eceSKris Buschelman                      block calculations (much slower)
1983db4efbfdSBarry Smith .   -mat_block_size - size of the blocks to use (only works if a negative bs is passed in
1984a23d5eceSKris Buschelman 
1985a23d5eceSKris Buschelman    Level: intermediate
1986a23d5eceSKris Buschelman 
1987a23d5eceSKris Buschelman    Notes:
1988a23d5eceSKris Buschelman    Specify the preallocated storage with either nz or nnz (not both).
19890298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
1990a7f22e61SSatish Balay    allocation.  See Users-Manual: ch_mat for details.
1991a23d5eceSKris Buschelman 
1992aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
1993aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
1994aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
1995aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
1996aa95bbe8SBarry Smith 
199749a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
199849a6f317SBarry Smith 
199949a6f317SBarry Smith 
200069b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateSBAIJ()
2001a23d5eceSKris Buschelman @*/
20027087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt nz,const PetscInt nnz[])
200313f74950SBarry Smith {
20044ac538c5SBarry Smith   PetscErrorCode ierr;
2005a23d5eceSKris Buschelman 
2006a23d5eceSKris Buschelman   PetscFunctionBegin;
20076ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
20086ba663aaSJed Brown   PetscValidType(B,1);
20096ba663aaSJed Brown   PetscValidLogicalCollectiveInt(B,bs,2);
20104ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqSBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[]),(B,bs,nz,nnz));CHKERRQ(ierr);
2011a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2012a23d5eceSKris Buschelman }
201349b5e25fSSatish Balay 
20144a2ae208SSatish Balay #undef  __FUNCT__
201538f409ebSLisandro Dalcin #define __FUNCT__ "MatSeqSBAIJSetPreallocationCSR"
201638f409ebSLisandro Dalcin /*@C
201738f409ebSLisandro Dalcin    MatSeqSBAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in symmetric block AIJ format.
201838f409ebSLisandro Dalcin 
201938f409ebSLisandro Dalcin    Input Parameters:
20201c4f3114SJed Brown +  B - the matrix
2021eab78319SHong Zhang .  bs - size of block, the blocks are ALWAYS square.
202238f409ebSLisandro Dalcin .  i - the indices into j for the start of each local row (starts with zero)
202338f409ebSLisandro Dalcin .  j - the column indices for each local row (starts with zero) these must be sorted for each row
202438f409ebSLisandro Dalcin -  v - optional values in the matrix
202538f409ebSLisandro Dalcin 
202638f409ebSLisandro Dalcin    Level: developer
202738f409ebSLisandro Dalcin 
202838f409ebSLisandro Dalcin    Notes:
202938f409ebSLisandro Dalcin    The order of the entries in values is specified by the MatOption MAT_ROW_ORIENTED.  For example, C programs
203038f409ebSLisandro 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
203138f409ebSLisandro Dalcin    over rows within a block and the last index is over columns within a block row.  Fortran programs will likely set
203238f409ebSLisandro 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
203338f409ebSLisandro Dalcin    block column and the second index is over columns within a block.
203438f409ebSLisandro Dalcin 
203538f409ebSLisandro Dalcin .keywords: matrix, block, aij, compressed row, sparse
203638f409ebSLisandro Dalcin 
203738f409ebSLisandro Dalcin .seealso: MatCreate(), MatCreateSeqSBAIJ(), MatSetValuesBlocked(), MatSeqSBAIJSetPreallocation(), MATSEQSBAIJ
203838f409ebSLisandro Dalcin @*/
203938f409ebSLisandro Dalcin PetscErrorCode MatSeqSBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
204038f409ebSLisandro Dalcin {
204138f409ebSLisandro Dalcin   PetscErrorCode ierr;
204238f409ebSLisandro Dalcin 
204338f409ebSLisandro Dalcin   PetscFunctionBegin;
204438f409ebSLisandro Dalcin   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
204538f409ebSLisandro Dalcin   PetscValidType(B,1);
204638f409ebSLisandro Dalcin   PetscValidLogicalCollectiveInt(B,bs,2);
204738f409ebSLisandro Dalcin   ierr = PetscTryMethod(B,"MatSeqSBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));CHKERRQ(ierr);
204838f409ebSLisandro Dalcin   PetscFunctionReturn(0);
204938f409ebSLisandro Dalcin }
205038f409ebSLisandro Dalcin 
205138f409ebSLisandro Dalcin #undef __FUNCT__
20524a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqSBAIJ"
2053c464158bSHong Zhang /*@C
2054c464158bSHong Zhang    MatCreateSeqSBAIJ - Creates a sparse symmetric matrix in block AIJ (block
2055c464158bSHong Zhang    compressed row) format.  For good matrix assembly performance the
2056c464158bSHong Zhang    user should preallocate the matrix storage by setting the parameter nz
2057c464158bSHong Zhang    (or the array nnz).  By setting these parameters accurately, performance
2058c464158bSHong Zhang    during matrix assembly can be increased by more than a factor of 50.
205949b5e25fSSatish Balay 
2060c464158bSHong Zhang    Collective on MPI_Comm
2061c464158bSHong Zhang 
2062c464158bSHong Zhang    Input Parameters:
2063c464158bSHong Zhang +  comm - MPI communicator, set to PETSC_COMM_SELF
2064bb7ae925SBarry 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
2065bb7ae925SBarry Smith           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
2066c464158bSHong Zhang .  m - number of rows, or number of columns
2067c464158bSHong Zhang .  nz - number of block nonzeros per block row (same for all rows)
2068744e8345SSatish Balay -  nnz - array containing the number of block nonzeros in the upper triangular plus
20690298fd71SBarry Smith          diagonal portion of each block (possibly different for each block row) or NULL
2070c464158bSHong Zhang 
2071c464158bSHong Zhang    Output Parameter:
2072c464158bSHong Zhang .  A - the symmetric matrix
2073c464158bSHong Zhang 
2074c464158bSHong Zhang    Options Database Keys:
2075c464158bSHong Zhang .   -mat_no_unroll - uses code that does not unroll the loops in the
2076c464158bSHong Zhang                      block calculations (much slower)
2077c464158bSHong Zhang .    -mat_block_size - size of the blocks to use
2078c464158bSHong Zhang 
2079c464158bSHong Zhang    Level: intermediate
2080c464158bSHong Zhang 
2081175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2082ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
2083175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2084175b88e8SBarry Smith 
2085c464158bSHong Zhang    Notes:
20866d6d819aSHong Zhang    The number of rows and columns must be divisible by blocksize.
20876d6d819aSHong Zhang    This matrix type does not support complex Hermitian operation.
2088c464158bSHong Zhang 
2089c464158bSHong Zhang    Specify the preallocated storage with either nz or nnz (not both).
20900298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
2091a7f22e61SSatish Balay    allocation.  See Users-Manual: ch_mat for details.
2092c464158bSHong Zhang 
209349a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
209449a6f317SBarry Smith 
209569b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateSBAIJ()
2096c464158bSHong Zhang @*/
20977087cfbeSBarry Smith PetscErrorCode  MatCreateSeqSBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
2098c464158bSHong Zhang {
2099dfbe8321SBarry Smith   PetscErrorCode ierr;
2100c464158bSHong Zhang 
2101c464158bSHong Zhang   PetscFunctionBegin;
2102f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2103f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2104c464158bSHong Zhang   ierr = MatSetType(*A,MATSEQSBAIJ);CHKERRQ(ierr);
2105ab93d7beSBarry Smith   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*A,bs,nz,(PetscInt*)nnz);CHKERRQ(ierr);
210649b5e25fSSatish Balay   PetscFunctionReturn(0);
210749b5e25fSSatish Balay }
210849b5e25fSSatish Balay 
21094a2ae208SSatish Balay #undef __FUNCT__
21104a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqSBAIJ"
2111dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqSBAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
211249b5e25fSSatish Balay {
211349b5e25fSSatish Balay   Mat            C;
211449b5e25fSSatish Balay   Mat_SeqSBAIJ   *c,*a = (Mat_SeqSBAIJ*)A->data;
21156849ba73SBarry Smith   PetscErrorCode ierr;
2116b40805acSSatish Balay   PetscInt       i,mbs = a->mbs,nz = a->nz,bs2 =a->bs2;
211749b5e25fSSatish Balay 
211849b5e25fSSatish Balay   PetscFunctionBegin;
2119e32f2f54SBarry Smith   if (a->i[mbs] != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupt matrix");
212049b5e25fSSatish Balay 
212149b5e25fSSatish Balay   *B   = 0;
2122ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2123d0f46423SBarry Smith   ierr = MatSetSizes(C,A->rmap->N,A->cmap->n,A->rmap->N,A->cmap->n);CHKERRQ(ierr);
21248e9a0fb8SHong Zhang   ierr = MatSetType(C,MATSEQSBAIJ);CHKERRQ(ierr);
21251d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
2126692f9cbeSHong Zhang   c    = (Mat_SeqSBAIJ*)C->data;
2127692f9cbeSHong Zhang 
2128273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2129d5f3da31SBarry Smith   C->factortype         = A->factortype;
213049b5e25fSSatish Balay   c->row                = 0;
213149b5e25fSSatish Balay   c->icol               = 0;
213249b5e25fSSatish Balay   c->saved_values       = 0;
2133a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
213449b5e25fSSatish Balay   C->assembled          = PETSC_TRUE;
213549b5e25fSSatish Balay 
21361e1e43feSBarry Smith   ierr   = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
21371e1e43feSBarry Smith   ierr   = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
213849b5e25fSSatish Balay   c->bs2 = a->bs2;
213949b5e25fSSatish Balay   c->mbs = a->mbs;
214049b5e25fSSatish Balay   c->nbs = a->nbs;
214149b5e25fSSatish Balay 
2142c760cd28SBarry Smith   if  (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2143c760cd28SBarry Smith     c->imax           = a->imax;
2144c760cd28SBarry Smith     c->ilen           = a->ilen;
2145c760cd28SBarry Smith     c->free_imax_ilen = PETSC_FALSE;
2146c760cd28SBarry Smith   } else {
2147dcca6d9dSJed Brown     ierr = PetscMalloc2((mbs+1),&c->imax,(mbs+1),&c->ilen);CHKERRQ(ierr);
21483bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C,2*(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
214949b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
215049b5e25fSSatish Balay       c->imax[i] = a->imax[i];
215149b5e25fSSatish Balay       c->ilen[i] = a->ilen[i];
215249b5e25fSSatish Balay     }
2153c760cd28SBarry Smith     c->free_imax_ilen = PETSC_TRUE;
2154c760cd28SBarry Smith   }
215549b5e25fSSatish Balay 
215649b5e25fSSatish Balay   /* allocate the matrix space */
21574da8f245SBarry Smith   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2158785e854fSJed Brown     ierr            = PetscMalloc1(bs2*nz,&c->a);CHKERRQ(ierr);
21593bb1ff40SBarry Smith     ierr            = PetscLogObjectMemory((PetscObject)C,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
216044e1c64aSLisandro Dalcin     c->i            = a->i;
216144e1c64aSLisandro Dalcin     c->j            = a->j;
21624da8f245SBarry Smith     c->singlemalloc = PETSC_FALSE;
216344e1c64aSLisandro Dalcin     c->free_a       = PETSC_TRUE;
21644da8f245SBarry Smith     c->free_ij      = PETSC_FALSE;
21654da8f245SBarry Smith     c->parent       = A;
21664da8f245SBarry Smith     ierr            = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
21674da8f245SBarry Smith     ierr            = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
21684da8f245SBarry Smith     ierr            = MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
21694da8f245SBarry Smith   } else {
2170dcca6d9dSJed Brown     ierr            = PetscMalloc3(bs2*nz,&c->a,nz,&c->j,mbs+1,&c->i);CHKERRQ(ierr);
217113f74950SBarry Smith     ierr            = PetscMemcpy(c->i,a->i,(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
21723bb1ff40SBarry Smith     ierr            = PetscLogObjectMemory((PetscObject)C,(mbs+1)*sizeof(PetscInt) + nz*(bs2*sizeof(MatScalar) + sizeof(PetscInt)));CHKERRQ(ierr);
21734da8f245SBarry Smith     c->singlemalloc = PETSC_TRUE;
217444e1c64aSLisandro Dalcin     c->free_a       = PETSC_TRUE;
21754da8f245SBarry Smith     c->free_ij      = PETSC_TRUE;
21764da8f245SBarry Smith   }
217749b5e25fSSatish Balay   if (mbs > 0) {
21784da8f245SBarry Smith     if (cpvalues != MAT_SHARE_NONZERO_PATTERN) {
217913f74950SBarry Smith       ierr = PetscMemcpy(c->j,a->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
21804da8f245SBarry Smith     }
218149b5e25fSSatish Balay     if (cpvalues == MAT_COPY_VALUES) {
218249b5e25fSSatish Balay       ierr = PetscMemcpy(c->a,a->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
218349b5e25fSSatish Balay     } else {
218449b5e25fSSatish Balay       ierr = PetscMemzero(c->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
218549b5e25fSSatish Balay     }
2186a1c3900fSBarry Smith     if (a->jshort) {
218744e1c64aSLisandro Dalcin       /* cannot share jshort, it is reallocated in MatAssemblyEnd_SeqSBAIJ() */
218844e1c64aSLisandro Dalcin       /* if the parent matrix is reassembled, this child matrix will never notice */
2189785e854fSJed Brown       ierr = PetscMalloc1(nz,&c->jshort);CHKERRQ(ierr);
21903bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)C,nz*sizeof(unsigned short));CHKERRQ(ierr);
2191a1c3900fSBarry Smith       ierr = PetscMemcpy(c->jshort,a->jshort,nz*sizeof(unsigned short));CHKERRQ(ierr);
219226fbe8dcSKarl Rupp 
21934da8f245SBarry Smith       c->free_jshort = PETSC_TRUE;
21944da8f245SBarry Smith     }
2195a1c3900fSBarry Smith   }
219649b5e25fSSatish Balay 
219749b5e25fSSatish Balay   c->roworiented = a->roworiented;
219849b5e25fSSatish Balay   c->nonew       = a->nonew;
219949b5e25fSSatish Balay 
220049b5e25fSSatish Balay   if (a->diag) {
2201c760cd28SBarry Smith     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2202c760cd28SBarry Smith       c->diag      = a->diag;
2203c760cd28SBarry Smith       c->free_diag = PETSC_FALSE;
2204c760cd28SBarry Smith     } else {
2205785e854fSJed Brown       ierr = PetscMalloc1(mbs,&c->diag);CHKERRQ(ierr);
22063bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)C,mbs*sizeof(PetscInt));CHKERRQ(ierr);
220726fbe8dcSKarl Rupp       for (i=0; i<mbs; i++) c->diag[i] = a->diag[i];
2208c760cd28SBarry Smith       c->free_diag = PETSC_TRUE;
2209c760cd28SBarry Smith     }
221044e1c64aSLisandro Dalcin   }
22116c6c5352SBarry Smith   c->nz         = a->nz;
2212f2cbd3d5SJed Brown   c->maxnz      = a->nz; /* Since we allocate exactly the right amount */
221349b5e25fSSatish Balay   c->solve_work = 0;
221449b5e25fSSatish Balay   c->mult_work  = 0;
221526fbe8dcSKarl Rupp 
221649b5e25fSSatish Balay   *B   = C;
2217140e18c1SBarry Smith   ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
221849b5e25fSSatish Balay   PetscFunctionReturn(0);
221949b5e25fSSatish Balay }
222049b5e25fSSatish Balay 
22214a2ae208SSatish Balay #undef __FUNCT__
22225bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_SeqSBAIJ"
2223112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqSBAIJ(Mat newmat,PetscViewer viewer)
22242f480046SShri Abhyankar {
22252f480046SShri Abhyankar   Mat_SeqSBAIJ   *a;
22262f480046SShri Abhyankar   PetscErrorCode ierr;
22272f480046SShri Abhyankar   int            fd;
22282f480046SShri Abhyankar   PetscMPIInt    size;
22293059b6faSBarry Smith   PetscInt       i,nz,header[4],*rowlengths=0,M,N,bs = newmat->rmap->bs;
22302f480046SShri Abhyankar   PetscInt       *mask,mbs,*jj,j,rowcount,nzcount,k,*s_browlengths,maskcount;
22312f480046SShri Abhyankar   PetscInt       kmax,jcount,block,idx,point,nzcountb,extra_rows,rows,cols;
22322f480046SShri Abhyankar   PetscInt       *masked,nmask,tmp,bs2,ishift;
22332f480046SShri Abhyankar   PetscScalar    *aa;
2234ce94432eSBarry Smith   MPI_Comm       comm;
22352f480046SShri Abhyankar 
22362f480046SShri Abhyankar   PetscFunctionBegin;
2237c98fd787SBarry Smith   /* force binary viewer to load .info file if it has not yet done so */
2238c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
2239ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
2240c5929fdfSBarry Smith   ierr = PetscOptionsGetInt(((PetscObject)newmat)->options,((PetscObject)newmat)->prefix,"-matload_block_size",&bs,NULL);CHKERRQ(ierr);
22413059b6faSBarry Smith   if (bs < 0) bs = 1;
22422f480046SShri Abhyankar   bs2  = bs*bs;
22432f480046SShri Abhyankar 
22442f480046SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
22452f480046SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
22462f480046SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
22472f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
22482f480046SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not Mat object");
22492f480046SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
22502f480046SShri Abhyankar 
22512f480046SShri Abhyankar   if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as SeqSBAIJ");
22522f480046SShri Abhyankar 
22532f480046SShri Abhyankar   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");
22542f480046SShri Abhyankar 
22552f480046SShri Abhyankar   /*
22562f480046SShri Abhyankar      This code adds extra rows to make sure the number of rows is
22572f480046SShri Abhyankar     divisible by the blocksize
22582f480046SShri Abhyankar   */
22592f480046SShri Abhyankar   mbs        = M/bs;
22602f480046SShri Abhyankar   extra_rows = bs - M + bs*(mbs);
22612f480046SShri Abhyankar   if (extra_rows == bs) extra_rows = 0;
22622f480046SShri Abhyankar   else                  mbs++;
22632f480046SShri Abhyankar   if (extra_rows) {
22642f480046SShri Abhyankar     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
22652f480046SShri Abhyankar   }
22662f480046SShri Abhyankar 
22672f480046SShri Abhyankar   /* Set global sizes if not already set */
22682f480046SShri Abhyankar   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) {
22692f480046SShri Abhyankar     ierr = MatSetSizes(newmat,PETSC_DECIDE,PETSC_DECIDE,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
22702f480046SShri Abhyankar   } else { /* Check if the matrix global sizes are correct */
22712f480046SShri Abhyankar     ierr = MatGetSize(newmat,&rows,&cols);CHKERRQ(ierr);
22722f480046SShri 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);
22732f480046SShri Abhyankar   }
22742f480046SShri Abhyankar 
22752f480046SShri Abhyankar   /* read in row lengths */
2276854ce69bSBarry Smith   ierr = PetscMalloc1(M+extra_rows,&rowlengths);CHKERRQ(ierr);
22772f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
22782f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
22792f480046SShri Abhyankar 
22802f480046SShri Abhyankar   /* read in column indices */
2281854ce69bSBarry Smith   ierr = PetscMalloc1(nz+extra_rows,&jj);CHKERRQ(ierr);
22822f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,jj,nz,PETSC_INT);CHKERRQ(ierr);
22832f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) jj[nz+i] = M+i;
22842f480046SShri Abhyankar 
22852f480046SShri Abhyankar   /* loop over row lengths determining block row lengths */
22861795a4d1SJed Brown   ierr     = PetscCalloc1(mbs,&s_browlengths);CHKERRQ(ierr);
2287dcca6d9dSJed Brown   ierr     = PetscMalloc2(mbs,&mask,mbs,&masked);CHKERRQ(ierr);
22882f480046SShri Abhyankar   ierr     = PetscMemzero(mask,mbs*sizeof(PetscInt));CHKERRQ(ierr);
22892f480046SShri Abhyankar   rowcount = 0;
22902f480046SShri Abhyankar   nzcount  = 0;
22912f480046SShri Abhyankar   for (i=0; i<mbs; i++) {
22922f480046SShri Abhyankar     nmask = 0;
22932f480046SShri Abhyankar     for (j=0; j<bs; j++) {
22942f480046SShri Abhyankar       kmax = rowlengths[rowcount];
22952f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
22962f480046SShri Abhyankar         tmp = jj[nzcount++]/bs;   /* block col. index */
22972f480046SShri Abhyankar         if (!mask[tmp] && tmp >= i) {masked[nmask++] = tmp; mask[tmp] = 1;}
22982f480046SShri Abhyankar       }
22992f480046SShri Abhyankar       rowcount++;
23002f480046SShri Abhyankar     }
23012f480046SShri Abhyankar     s_browlengths[i] += nmask;
23022f480046SShri Abhyankar 
23032f480046SShri Abhyankar     /* zero out the mask elements we set */
23042f480046SShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
23052f480046SShri Abhyankar   }
23062f480046SShri Abhyankar 
23072f480046SShri Abhyankar   /* Do preallocation */
23082f480046SShri Abhyankar   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(newmat,bs,0,s_browlengths);CHKERRQ(ierr);
23092f480046SShri Abhyankar   a    = (Mat_SeqSBAIJ*)newmat->data;
23102f480046SShri Abhyankar 
23112f480046SShri Abhyankar   /* set matrix "i" values */
23122f480046SShri Abhyankar   a->i[0] = 0;
23132f480046SShri Abhyankar   for (i=1; i<= mbs; i++) {
23142f480046SShri Abhyankar     a->i[i]      = a->i[i-1] + s_browlengths[i-1];
23152f480046SShri Abhyankar     a->ilen[i-1] = s_browlengths[i-1];
23162f480046SShri Abhyankar   }
23172f480046SShri Abhyankar   a->nz = a->i[mbs];
23182f480046SShri Abhyankar 
23192f480046SShri Abhyankar   /* read in nonzero values */
2320854ce69bSBarry Smith   ierr = PetscMalloc1(nz+extra_rows,&aa);CHKERRQ(ierr);
23212f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,aa,nz,PETSC_SCALAR);CHKERRQ(ierr);
23222f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) aa[nz+i] = 1.0;
23232f480046SShri Abhyankar 
23242f480046SShri Abhyankar   /* set "a" and "j" values into matrix */
23252f480046SShri Abhyankar   nzcount = 0; jcount = 0;
23262f480046SShri Abhyankar   for (i=0; i<mbs; i++) {
23272f480046SShri Abhyankar     nzcountb = nzcount;
23282f480046SShri Abhyankar     nmask    = 0;
23292f480046SShri Abhyankar     for (j=0; j<bs; j++) {
23302f480046SShri Abhyankar       kmax = rowlengths[i*bs+j];
23312f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
23322f480046SShri Abhyankar         tmp = jj[nzcount++]/bs; /* block col. index */
23332f480046SShri Abhyankar         if (!mask[tmp] && tmp >= i) { masked[nmask++] = tmp; mask[tmp] = 1;}
23342f480046SShri Abhyankar       }
23352f480046SShri Abhyankar     }
23362f480046SShri Abhyankar     /* sort the masked values */
23372f480046SShri Abhyankar     ierr = PetscSortInt(nmask,masked);CHKERRQ(ierr);
23382f480046SShri Abhyankar 
23392f480046SShri Abhyankar     /* set "j" values into matrix */
23402f480046SShri Abhyankar     maskcount = 1;
23412f480046SShri Abhyankar     for (j=0; j<nmask; j++) {
23422f480046SShri Abhyankar       a->j[jcount++]  = masked[j];
23432f480046SShri Abhyankar       mask[masked[j]] = maskcount++;
23442f480046SShri Abhyankar     }
23452f480046SShri Abhyankar 
23462f480046SShri Abhyankar     /* set "a" values into matrix */
23472f480046SShri Abhyankar     ishift = bs2*a->i[i];
23482f480046SShri Abhyankar     for (j=0; j<bs; j++) {
23492f480046SShri Abhyankar       kmax = rowlengths[i*bs+j];
23502f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
23512f480046SShri Abhyankar         tmp = jj[nzcountb]/bs;        /* block col. index */
23522f480046SShri Abhyankar         if (tmp >= i) {
23532f480046SShri Abhyankar           block     = mask[tmp] - 1;
23542f480046SShri Abhyankar           point     = jj[nzcountb] - bs*tmp;
23552f480046SShri Abhyankar           idx       = ishift + bs2*block + j + bs*point;
23562f480046SShri Abhyankar           a->a[idx] = aa[nzcountb];
23572f480046SShri Abhyankar         }
23582f480046SShri Abhyankar         nzcountb++;
23592f480046SShri Abhyankar       }
23602f480046SShri Abhyankar     }
23612f480046SShri Abhyankar     /* zero out the mask elements we set */
23622f480046SShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
23632f480046SShri Abhyankar   }
23642f480046SShri Abhyankar   if (jcount != a->nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Bad binary matrix");
23652f480046SShri Abhyankar 
23662f480046SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
23672f480046SShri Abhyankar   ierr = PetscFree(s_browlengths);CHKERRQ(ierr);
23682f480046SShri Abhyankar   ierr = PetscFree(aa);CHKERRQ(ierr);
23692f480046SShri Abhyankar   ierr = PetscFree(jj);CHKERRQ(ierr);
23702f480046SShri Abhyankar   ierr = PetscFree2(mask,masked);CHKERRQ(ierr);
23712f480046SShri Abhyankar 
23722f480046SShri Abhyankar   ierr = MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
23732f480046SShri Abhyankar   ierr = MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
23742f480046SShri Abhyankar   PetscFunctionReturn(0);
23752f480046SShri Abhyankar }
23762f480046SShri Abhyankar 
23772f480046SShri Abhyankar #undef __FUNCT__
2378c75a6043SHong Zhang #define __FUNCT__ "MatCreateSeqSBAIJWithArrays"
2379c75a6043SHong Zhang /*@
2380c75a6043SHong Zhang      MatCreateSeqSBAIJWithArrays - Creates an sequential SBAIJ matrix using matrix elements
2381c75a6043SHong Zhang               (upper triangular entries in CSR format) provided by the user.
2382c75a6043SHong Zhang 
2383c75a6043SHong Zhang      Collective on MPI_Comm
2384c75a6043SHong Zhang 
2385c75a6043SHong Zhang    Input Parameters:
2386c75a6043SHong Zhang +  comm - must be an MPI communicator of size 1
2387c75a6043SHong Zhang .  bs - size of block
2388c75a6043SHong Zhang .  m - number of rows
2389c75a6043SHong Zhang .  n - number of columns
2390c75a6043SHong Zhang .  i - row indices
2391c75a6043SHong Zhang .  j - column indices
2392c75a6043SHong Zhang -  a - matrix values
2393c75a6043SHong Zhang 
2394c75a6043SHong Zhang    Output Parameter:
2395c75a6043SHong Zhang .  mat - the matrix
2396c75a6043SHong Zhang 
2397dfb205c3SBarry Smith    Level: advanced
2398c75a6043SHong Zhang 
2399c75a6043SHong Zhang    Notes:
2400c75a6043SHong Zhang        The i, j, and a arrays are not copied by this routine, the user must free these arrays
2401c75a6043SHong Zhang     once the matrix is destroyed
2402c75a6043SHong Zhang 
2403c75a6043SHong Zhang        You cannot set new nonzero locations into this matrix, that will generate an error.
2404c75a6043SHong Zhang 
2405c75a6043SHong Zhang        The i and j indices are 0 based
2406c75a6043SHong Zhang 
2407dfb205c3SBarry 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
2408dfb205c3SBarry Smith        it is the regular CSR format excluding the lower triangular elements.
2409dfb205c3SBarry Smith 
241069b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSBAIJ(), MatCreateSeqSBAIJ()
2411c75a6043SHong Zhang 
2412c75a6043SHong Zhang @*/
24137087cfbeSBarry Smith PetscErrorCode  MatCreateSeqSBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
2414c75a6043SHong Zhang {
2415c75a6043SHong Zhang   PetscErrorCode ierr;
2416c75a6043SHong Zhang   PetscInt       ii;
2417c75a6043SHong Zhang   Mat_SeqSBAIJ   *sbaij;
2418c75a6043SHong Zhang 
2419c75a6043SHong Zhang   PetscFunctionBegin;
2420e32f2f54SBarry Smith   if (bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"block size %D > 1 is not supported yet",bs);
2421e32f2f54SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
2422c75a6043SHong Zhang 
2423c75a6043SHong Zhang   ierr  = MatCreate(comm,mat);CHKERRQ(ierr);
2424c75a6043SHong Zhang   ierr  = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
2425c75a6043SHong Zhang   ierr  = MatSetType(*mat,MATSEQSBAIJ);CHKERRQ(ierr);
2426c75a6043SHong Zhang   ierr  = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*mat,bs,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
2427c75a6043SHong Zhang   sbaij = (Mat_SeqSBAIJ*)(*mat)->data;
2428dcca6d9dSJed Brown   ierr  = PetscMalloc2(m,&sbaij->imax,m,&sbaij->ilen);CHKERRQ(ierr);
24293bb1ff40SBarry Smith   ierr  = PetscLogObjectMemory((PetscObject)*mat,2*m*sizeof(PetscInt));CHKERRQ(ierr);
2430c75a6043SHong Zhang 
2431c75a6043SHong Zhang   sbaij->i = i;
2432c75a6043SHong Zhang   sbaij->j = j;
2433c75a6043SHong Zhang   sbaij->a = a;
243426fbe8dcSKarl Rupp 
2435c75a6043SHong Zhang   sbaij->singlemalloc = PETSC_FALSE;
2436c75a6043SHong Zhang   sbaij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
2437e6b907acSBarry Smith   sbaij->free_a       = PETSC_FALSE;
2438e6b907acSBarry Smith   sbaij->free_ij      = PETSC_FALSE;
2439c75a6043SHong Zhang 
2440c75a6043SHong Zhang   for (ii=0; ii<m; ii++) {
2441c75a6043SHong Zhang     sbaij->ilen[ii] = sbaij->imax[ii] = i[ii+1] - i[ii];
2442c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2443e32f2f54SBarry 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]);
2444c75a6043SHong Zhang #endif
2445c75a6043SHong Zhang   }
2446c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2447c75a6043SHong Zhang   for (ii=0; ii<sbaij->i[m]; ii++) {
2448e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
2449e32f2f54SBarry 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]);
2450c75a6043SHong Zhang   }
2451c75a6043SHong Zhang #endif
2452c75a6043SHong Zhang 
2453c75a6043SHong Zhang   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2454c75a6043SHong Zhang   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2455c75a6043SHong Zhang   PetscFunctionReturn(0);
2456c75a6043SHong Zhang }
2457d06b337dSHong Zhang 
245859f5e6ceSHong Zhang #undef __FUNCT__
245959f5e6ceSHong Zhang #define __FUNCT__ "MatCreateMPIMatConcatenateSeqMat_SeqSBAIJ"
246059f5e6ceSHong Zhang PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqSBAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
246159f5e6ceSHong Zhang {
246259f5e6ceSHong Zhang   PetscErrorCode ierr;
246359f5e6ceSHong Zhang 
246459f5e6ceSHong Zhang   PetscFunctionBegin;
246559f5e6ceSHong Zhang   ierr = MatCreateMPIMatConcatenateSeqMat_MPISBAIJ(comm,inmat,n,scall,outmat);CHKERRQ(ierr);
246659f5e6ceSHong Zhang   PetscFunctionReturn(0);
246759f5e6ceSHong Zhang }
2468d06b337dSHong Zhang 
2469d06b337dSHong Zhang 
247049b5e25fSSatish Balay 
247149b5e25fSSatish Balay 
2472