xref: /petsc/src/mat/impls/sbaij/seq/sbaij.c (revision 6214f4126138aa4cc9867da86c269cb48c3e22ce)
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);
15*6214f412SHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
16*6214f412SHong Zhang PETSC_EXTERN PetscErrorCode MatConvert_SeqSBAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
17*6214f412SHong 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++) {
938f7157efSSatish Balay         *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++) {
988f7157efSSatish Balay         *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);
170*6214f412SHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
171*6214f412SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_elemental_C",NULL);CHKERRQ(ierr);
172*6214f412SHong 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;
316d5f3da31SBarry Smith     if (A->factortype && bs>1) {
31770d5e725SHong 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);
31870d5e725SHong Zhang       PetscFunctionReturn(0);
31970d5e725SHong Zhang     }
320c9f458caSMatthew Knepley     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&aij);CHKERRQ(ierr);
321c9f458caSMatthew Knepley     ierr = MatView(aij,viewer);CHKERRQ(ierr);
3226bf464f9SBarry Smith     ierr = MatDestroy(&aij);CHKERRQ(ierr);
323fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
324d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
32549b5e25fSSatish Balay     for (i=0; i<a->mbs; i++) {
32649b5e25fSSatish Balay       for (j=0; j<bs; j++) {
32777431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
32849b5e25fSSatish Balay         for (k=a->i[i]; k<a->i[i+1]; k++) {
32949b5e25fSSatish Balay           for (l=0; l<bs; l++) {
33049b5e25fSSatish Balay #if defined(PETSC_USE_COMPLEX)
33149b5e25fSSatish Balay             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
33257622a8eSBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i) ",bs*a->j[k]+l,
33357622a8eSBarry Smith                                             (double)PetscRealPart(a->a[bs2*k + l*bs + j]),(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
33449b5e25fSSatish Balay             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
33557622a8eSBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i) ",bs*a->j[k]+l,
33657622a8eSBarry Smith                                             (double)PetscRealPart(a->a[bs2*k + l*bs + j]),-(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
33749b5e25fSSatish Balay             } else if (PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
33857622a8eSBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
33949b5e25fSSatish Balay             }
34049b5e25fSSatish Balay #else
34149b5e25fSSatish Balay             if (a->a[bs2*k + l*bs + j] != 0.0) {
34257622a8eSBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
34349b5e25fSSatish Balay             }
34449b5e25fSSatish Balay #endif
34549b5e25fSSatish Balay           }
34649b5e25fSSatish Balay         }
347b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
34849b5e25fSSatish Balay       }
34949b5e25fSSatish Balay     }
350d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
351c1490034SHong Zhang   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
352c1490034SHong Zhang     PetscFunctionReturn(0);
35349b5e25fSSatish Balay   } else {
354d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
3552c990fa1SHong Zhang     if (A->factortype) { /* for factored matrix */
3562c990fa1SHong Zhang       if (bs>1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"matrix is factored with bs>1. Not implemented yet");
3572c990fa1SHong Zhang 
358121deb67SSatish Balay       diag=a->diag;
359121deb67SSatish Balay       for (i=0; i<a->mbs; i++) { /* for row block i */
3602c990fa1SHong Zhang         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
3612c990fa1SHong Zhang         /* diagonal entry */
3622c990fa1SHong Zhang #if defined(PETSC_USE_COMPLEX)
3632c990fa1SHong Zhang         if (PetscImaginaryPart(a->a[diag[i]]) > 0.0) {
36457622a8eSBarry 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);
3652c990fa1SHong Zhang         } else if (PetscImaginaryPart(a->a[diag[i]]) < 0.0) {
36657622a8eSBarry 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);
3672c990fa1SHong Zhang         } else {
36857622a8eSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[diag[i]],(double)PetscRealPart(1.0/a->a[diag[i]]));CHKERRQ(ierr);
3692c990fa1SHong Zhang         }
3702c990fa1SHong Zhang #else
3716712e2f1SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[diag[i]],(double)(1.0/a->a[diag[i]]));CHKERRQ(ierr);
3722c990fa1SHong Zhang #endif
3732c990fa1SHong Zhang         /* off-diagonal entries */
3742c990fa1SHong Zhang         for (k=a->i[i]; k<a->i[i+1]-1; k++) {
3752c990fa1SHong Zhang #if defined(PETSC_USE_COMPLEX)
376ca0704adSBarry Smith           if (PetscImaginaryPart(a->a[k]) > 0.0) {
37757622a8eSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i) ",bs*a->j[k],(double)PetscRealPart(a->a[k]),(double)PetscImaginaryPart(a->a[k]));CHKERRQ(ierr);
378ca0704adSBarry Smith           } else if (PetscImaginaryPart(a->a[k]) < 0.0) {
37957622a8eSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i) ",bs*a->j[k],(double)PetscRealPart(a->a[k]),-(double)PetscImaginaryPart(a->a[k]));CHKERRQ(ierr);
3802c990fa1SHong Zhang           } else {
38157622a8eSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k],(double)PetscRealPart(a->a[k]));CHKERRQ(ierr);
3822c990fa1SHong Zhang           }
3832c990fa1SHong Zhang #else
38457622a8eSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[k],(double)a->a[k]);CHKERRQ(ierr);
3852c990fa1SHong Zhang #endif
3862c990fa1SHong Zhang         }
3872c990fa1SHong Zhang         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
3882c990fa1SHong Zhang       }
3892c990fa1SHong Zhang 
3902c990fa1SHong Zhang     } else { /* for non-factored matrix */
3910c74a584SJed Brown       for (i=0; i<a->mbs; i++) { /* for row block i */
3920c74a584SJed Brown         for (j=0; j<bs; j++) {   /* for row bs*i + j */
39377431f27SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);CHKERRQ(ierr);
3940c74a584SJed Brown           for (k=a->i[i]; k<a->i[i+1]; k++) { /* for column block */
3950c74a584SJed Brown             for (l=0; l<bs; l++) {            /* for column */
39649b5e25fSSatish Balay #if defined(PETSC_USE_COMPLEX)
39749b5e25fSSatish Balay               if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0) {
39857622a8eSBarry Smith                 ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i) ",bs*a->j[k]+l,
39957622a8eSBarry Smith                                               (double)PetscRealPart(a->a[bs2*k + l*bs + j]),(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
40049b5e25fSSatish Balay               } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0) {
40157622a8eSBarry Smith                 ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i) ",bs*a->j[k]+l,
40257622a8eSBarry Smith                                               (double)PetscRealPart(a->a[bs2*k + l*bs + j]),-(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
40349b5e25fSSatish Balay               } else {
40457622a8eSBarry Smith                 ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)PetscRealPart(a->a[bs2*k + l*bs + j]));CHKERRQ(ierr);
40549b5e25fSSatish Balay               }
40649b5e25fSSatish Balay #else
40757622a8eSBarry Smith               ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)a->a[bs2*k + l*bs + j]);CHKERRQ(ierr);
40849b5e25fSSatish Balay #endif
40949b5e25fSSatish Balay             }
41049b5e25fSSatish Balay           }
411b0a32e0cSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
41249b5e25fSSatish Balay         }
41349b5e25fSSatish Balay       }
4142c990fa1SHong Zhang     }
415d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
41649b5e25fSSatish Balay   }
417b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
41849b5e25fSSatish Balay   PetscFunctionReturn(0);
41949b5e25fSSatish Balay }
42049b5e25fSSatish Balay 
4219804daf3SBarry Smith #include <petscdraw.h>
4224a2ae208SSatish Balay #undef __FUNCT__
4234a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_Draw_Zoom"
4246849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
42549b5e25fSSatish Balay {
42649b5e25fSSatish Balay   Mat            A = (Mat) Aa;
42749b5e25fSSatish Balay   Mat_SeqSBAIJ   *a=(Mat_SeqSBAIJ*)A->data;
4286849ba73SBarry Smith   PetscErrorCode ierr;
429d0f46423SBarry Smith   PetscInt       row,i,j,k,l,mbs=a->mbs,color,bs=A->rmap->bs,bs2=a->bs2;
43013f74950SBarry Smith   PetscMPIInt    rank;
43149b5e25fSSatish Balay   PetscReal      xl,yl,xr,yr,x_l,x_r,y_l,y_r;
43249b5e25fSSatish Balay   MatScalar      *aa;
43349b5e25fSSatish Balay   MPI_Comm       comm;
434b0a32e0cSBarry Smith   PetscViewer    viewer;
43549b5e25fSSatish Balay 
43649b5e25fSSatish Balay   PetscFunctionBegin;
43749b5e25fSSatish Balay   /*
43849b5e25fSSatish Balay     This is nasty. If this is called from an originally parallel matrix
43949b5e25fSSatish Balay     then all processes call this,but only the first has the matrix so the
44049b5e25fSSatish Balay     rest should return immediately.
44149b5e25fSSatish Balay   */
44249b5e25fSSatish Balay   ierr = PetscObjectGetComm((PetscObject)draw,&comm);CHKERRQ(ierr);
44349b5e25fSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
44449b5e25fSSatish Balay   if (rank) PetscFunctionReturn(0);
44549b5e25fSSatish Balay 
44649b5e25fSSatish Balay   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
44749b5e25fSSatish Balay 
448b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
449b0a32e0cSBarry Smith   PetscDrawString(draw, .3*(xl+xr), .3*(yl+yr), PETSC_DRAW_BLACK, "symmetric");
45049b5e25fSSatish Balay 
45149b5e25fSSatish Balay   /* loop over matrix elements drawing boxes */
452b0a32e0cSBarry Smith   color = PETSC_DRAW_BLUE;
45349b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
45449b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
455d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
45649b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
45749b5e25fSSatish Balay       aa  = a->a + j*bs2;
45849b5e25fSSatish Balay       for (k=0; k<bs; k++) {
45949b5e25fSSatish Balay         for (l=0; l<bs; l++) {
46049b5e25fSSatish Balay           if (PetscRealPart(*aa++) >=  0.) continue;
461b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
46249b5e25fSSatish Balay         }
46349b5e25fSSatish Balay       }
46449b5e25fSSatish Balay     }
46549b5e25fSSatish Balay   }
466b0a32e0cSBarry Smith   color = PETSC_DRAW_CYAN;
46749b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
46849b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
469d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
47049b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
47149b5e25fSSatish Balay       aa = a->a + j*bs2;
47249b5e25fSSatish Balay       for (k=0; k<bs; k++) {
47349b5e25fSSatish Balay         for (l=0; l<bs; l++) {
47449b5e25fSSatish Balay           if (PetscRealPart(*aa++) != 0.) continue;
475b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
47649b5e25fSSatish Balay         }
47749b5e25fSSatish Balay       }
47849b5e25fSSatish Balay     }
47949b5e25fSSatish Balay   }
48049b5e25fSSatish Balay 
481b0a32e0cSBarry Smith   color = PETSC_DRAW_RED;
48249b5e25fSSatish Balay   for (i=0,row=0; i<mbs; i++,row+=bs) {
48349b5e25fSSatish Balay     for (j=a->i[i]; j<a->i[i+1]; j++) {
484d0f46423SBarry Smith       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
48549b5e25fSSatish Balay       x_l = a->j[j]*bs; x_r = x_l + 1.0;
48649b5e25fSSatish Balay       aa = a->a + j*bs2;
48749b5e25fSSatish Balay       for (k=0; k<bs; k++) {
48849b5e25fSSatish Balay         for (l=0; l<bs; l++) {
48949b5e25fSSatish Balay           if (PetscRealPart(*aa++) <= 0.) continue;
490b0a32e0cSBarry Smith           ierr = PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);CHKERRQ(ierr);
49149b5e25fSSatish Balay         }
49249b5e25fSSatish Balay       }
49349b5e25fSSatish Balay     }
49449b5e25fSSatish Balay   }
49549b5e25fSSatish Balay   PetscFunctionReturn(0);
49649b5e25fSSatish Balay }
49749b5e25fSSatish Balay 
4984a2ae208SSatish Balay #undef __FUNCT__
4994a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ_Draw"
5006849ba73SBarry Smith static PetscErrorCode MatView_SeqSBAIJ_Draw(Mat A,PetscViewer viewer)
50149b5e25fSSatish Balay {
502dfbe8321SBarry Smith   PetscErrorCode ierr;
50349b5e25fSSatish Balay   PetscReal      xl,yl,xr,yr,w,h;
504b0a32e0cSBarry Smith   PetscDraw      draw;
505ace3abfcSBarry Smith   PetscBool      isnull;
50649b5e25fSSatish Balay 
50749b5e25fSSatish Balay   PetscFunctionBegin;
508b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
509b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
51049b5e25fSSatish Balay 
51149b5e25fSSatish Balay   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
512d0f46423SBarry Smith   xr   = A->rmap->N; yr = A->rmap->N; h = yr/10.0; w = xr/10.0;
51349b5e25fSSatish Balay   xr  += w;    yr += h;  xl = -w;     yl = -h;
514b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
515b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqSBAIJ_Draw_Zoom,A);CHKERRQ(ierr);
5160298fd71SBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);CHKERRQ(ierr);
51749b5e25fSSatish Balay   PetscFunctionReturn(0);
51849b5e25fSSatish Balay }
51949b5e25fSSatish Balay 
5204a2ae208SSatish Balay #undef __FUNCT__
5214a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqSBAIJ"
522dfbe8321SBarry Smith PetscErrorCode MatView_SeqSBAIJ(Mat A,PetscViewer viewer)
52349b5e25fSSatish Balay {
524dfbe8321SBarry Smith   PetscErrorCode ierr;
525ace3abfcSBarry Smith   PetscBool      iascii,isdraw;
52608917f38SBarry Smith   FILE           *file = 0;
52749b5e25fSSatish Balay 
52849b5e25fSSatish Balay   PetscFunctionBegin;
529251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
530251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
53132077d6dSBarry Smith   if (iascii) {
53249b5e25fSSatish Balay     ierr = MatView_SeqSBAIJ_ASCII(A,viewer);CHKERRQ(ierr);
53349b5e25fSSatish Balay   } else if (isdraw) {
53449b5e25fSSatish Balay     ierr = MatView_SeqSBAIJ_Draw(A,viewer);CHKERRQ(ierr);
53549b5e25fSSatish Balay   } else {
536a5e6ed63SBarry Smith     Mat B;
537ceb03754SKris Buschelman     ierr = MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&B);CHKERRQ(ierr);
538a5e6ed63SBarry Smith     ierr = MatView(B,viewer);CHKERRQ(ierr);
5396bf464f9SBarry Smith     ierr = MatDestroy(&B);CHKERRQ(ierr);
54008917f38SBarry Smith     ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
54108917f38SBarry Smith     if (file) {
54208917f38SBarry Smith       fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
54308917f38SBarry Smith     }
54449b5e25fSSatish Balay   }
54549b5e25fSSatish Balay   PetscFunctionReturn(0);
54649b5e25fSSatish Balay }
54749b5e25fSSatish Balay 
54849b5e25fSSatish Balay 
5494a2ae208SSatish Balay #undef __FUNCT__
5504a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqSBAIJ"
55113f74950SBarry Smith PetscErrorCode MatGetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
55249b5e25fSSatish Balay {
553045c9aa0SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
55413f74950SBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
55513f74950SBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
556d0f46423SBarry Smith   PetscInt     brow,bcol,ridx,cidx,bs=A->rmap->bs,bs2=a->bs2;
55797e567efSBarry Smith   MatScalar    *ap,*aa = a->a;
55849b5e25fSSatish Balay 
55949b5e25fSSatish Balay   PetscFunctionBegin;
56049b5e25fSSatish Balay   for (k=0; k<m; k++) { /* loop over rows */
56149b5e25fSSatish Balay     row = im[k]; brow = row/bs;
562e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
563e32f2f54SBarry 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);
56449b5e25fSSatish Balay     rp   = aj + ai[brow]; ap = aa + bs2*ai[brow];
56549b5e25fSSatish Balay     nrow = ailen[brow];
56649b5e25fSSatish Balay     for (l=0; l<n; l++) { /* loop over columns */
567e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
568e32f2f54SBarry 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);
56949b5e25fSSatish Balay       col  = in[l];
57049b5e25fSSatish Balay       bcol = col/bs;
57149b5e25fSSatish Balay       cidx = col%bs;
57249b5e25fSSatish Balay       ridx = row%bs;
57349b5e25fSSatish Balay       high = nrow;
57449b5e25fSSatish Balay       low  = 0; /* assume unsorted */
57549b5e25fSSatish Balay       while (high-low > 5) {
57649b5e25fSSatish Balay         t = (low+high)/2;
57749b5e25fSSatish Balay         if (rp[t] > bcol) high = t;
57849b5e25fSSatish Balay         else              low  = t;
57949b5e25fSSatish Balay       }
58049b5e25fSSatish Balay       for (i=low; i<high; i++) {
58149b5e25fSSatish Balay         if (rp[i] > bcol) break;
58249b5e25fSSatish Balay         if (rp[i] == bcol) {
58349b5e25fSSatish Balay           *v++ = ap[bs2*i+bs*cidx+ridx];
58449b5e25fSSatish Balay           goto finished;
58549b5e25fSSatish Balay         }
58649b5e25fSSatish Balay       }
58797e567efSBarry Smith       *v++ = 0.0;
58849b5e25fSSatish Balay finished:;
58949b5e25fSSatish Balay     }
59049b5e25fSSatish Balay   }
59149b5e25fSSatish Balay   PetscFunctionReturn(0);
59249b5e25fSSatish Balay }
59349b5e25fSSatish Balay 
59449b5e25fSSatish Balay 
5954a2ae208SSatish Balay #undef __FUNCT__
5964a2ae208SSatish Balay #define __FUNCT__ "MatSetValuesBlocked_SeqSBAIJ"
59713f74950SBarry Smith PetscErrorCode MatSetValuesBlocked_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
59849b5e25fSSatish Balay {
5990880e062SHong Zhang   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
6006849ba73SBarry Smith   PetscErrorCode    ierr;
601e2ee6c50SBarry Smith   PetscInt          *rp,k,low,high,t,ii,jj,row,nrow,i,col,l,rmax,N,lastcol = -1;
60213f74950SBarry Smith   PetscInt          *imax      =a->imax,*ai=a->i,*ailen=a->ilen;
603d0f46423SBarry Smith   PetscInt          *aj        =a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs,stepval;
604ace3abfcSBarry Smith   PetscBool         roworiented=a->roworiented;
605dd6ea824SBarry Smith   const PetscScalar *value     = v;
606f15d580aSBarry Smith   MatScalar         *ap,*aa = a->a,*bap;
6070880e062SHong Zhang 
60849b5e25fSSatish Balay   PetscFunctionBegin;
60926fbe8dcSKarl Rupp   if (roworiented) stepval = (n-1)*bs;
61026fbe8dcSKarl Rupp   else stepval = (m-1)*bs;
61126fbe8dcSKarl Rupp 
6120880e062SHong Zhang   for (k=0; k<m; k++) { /* loop over added rows */
6130880e062SHong Zhang     row = im[k];
6140880e062SHong Zhang     if (row < 0) continue;
6152515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
616e32f2f54SBarry Smith     if (row >= a->mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,a->mbs-1);
6170880e062SHong Zhang #endif
6180880e062SHong Zhang     rp   = aj + ai[row];
6190880e062SHong Zhang     ap   = aa + bs2*ai[row];
6200880e062SHong Zhang     rmax = imax[row];
6210880e062SHong Zhang     nrow = ailen[row];
6220880e062SHong Zhang     low  = 0;
623818f2c47SBarry Smith     high = nrow;
6240880e062SHong Zhang     for (l=0; l<n; l++) { /* loop over added columns */
6250880e062SHong Zhang       if (in[l] < 0) continue;
6260880e062SHong Zhang       col = in[l];
6272515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
628e32f2f54SBarry Smith       if (col >= a->nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",col,a->nbs-1);
629b1823623SSatish Balay #endif
630b98bf0e1SJed Brown       if (col < row) {
63126fbe8dcSKarl Rupp         if (a->ignore_ltriangular) continue; /* ignore lower triangular block */
63226fbe8dcSKarl 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)");
633b98bf0e1SJed Brown       }
63426fbe8dcSKarl Rupp       if (roworiented) value = v + k*(stepval+bs)*bs + l*bs;
63526fbe8dcSKarl Rupp       else value = v + l*(stepval+bs)*bs + k*bs;
63626fbe8dcSKarl Rupp 
63726fbe8dcSKarl Rupp       if (col <= lastcol) low = 0;
63826fbe8dcSKarl Rupp       else high = nrow;
63926fbe8dcSKarl Rupp 
640e2ee6c50SBarry Smith       lastcol = col;
6410880e062SHong Zhang       while (high-low > 7) {
6420880e062SHong Zhang         t = (low+high)/2;
6430880e062SHong Zhang         if (rp[t] > col) high = t;
6440880e062SHong Zhang         else             low  = t;
6450880e062SHong Zhang       }
6460880e062SHong Zhang       for (i=low; i<high; i++) {
6470880e062SHong Zhang         if (rp[i] > col) break;
6480880e062SHong Zhang         if (rp[i] == col) {
6490880e062SHong Zhang           bap = ap +  bs2*i;
6500880e062SHong Zhang           if (roworiented) {
6510880e062SHong Zhang             if (is == ADD_VALUES) {
6520880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6530880e062SHong Zhang                 for (jj=ii; jj<bs2; jj+=bs) {
6540880e062SHong Zhang                   bap[jj] += *value++;
6550880e062SHong Zhang                 }
6560880e062SHong Zhang               }
6570880e062SHong Zhang             } else {
6580880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6590880e062SHong Zhang                 for (jj=ii; jj<bs2; jj+=bs) {
6600880e062SHong Zhang                   bap[jj] = *value++;
6610880e062SHong Zhang                 }
6620880e062SHong Zhang                }
6630880e062SHong Zhang             }
6640880e062SHong Zhang           } else {
6650880e062SHong Zhang             if (is == ADD_VALUES) {
6660880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6670880e062SHong Zhang                 for (jj=0; jj<bs; jj++) {
6680880e062SHong Zhang                   *bap++ += *value++;
6690880e062SHong Zhang                 }
6700880e062SHong Zhang               }
6710880e062SHong Zhang             } else {
6720880e062SHong Zhang               for (ii=0; ii<bs; ii++,value+=stepval) {
6730880e062SHong Zhang                 for (jj=0; jj<bs; jj++) {
6740880e062SHong Zhang                   *bap++  = *value++;
6750880e062SHong Zhang                 }
6760880e062SHong Zhang               }
6770880e062SHong Zhang             }
6780880e062SHong Zhang           }
6790880e062SHong Zhang           goto noinsert2;
6800880e062SHong Zhang         }
6810880e062SHong Zhang       }
6820880e062SHong Zhang       if (nonew == 1) goto noinsert2;
683e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
684fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
685c03d1d03SSatish Balay       N = nrow++ - 1; high++;
6860880e062SHong Zhang       /* shift up all the later entries in this row */
6870880e062SHong Zhang       for (ii=N; ii>=i; ii--) {
6880880e062SHong Zhang         rp[ii+1] = rp[ii];
6890880e062SHong Zhang         ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
6900880e062SHong Zhang       }
6910880e062SHong Zhang       if (N >= i) {
6920880e062SHong Zhang         ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
6930880e062SHong Zhang       }
6940880e062SHong Zhang       rp[i] = col;
6950880e062SHong Zhang       bap   = ap +  bs2*i;
6960880e062SHong Zhang       if (roworiented) {
6970880e062SHong Zhang         for (ii=0; ii<bs; ii++,value+=stepval) {
6980880e062SHong Zhang           for (jj=ii; jj<bs2; jj+=bs) {
6990880e062SHong Zhang             bap[jj] = *value++;
7000880e062SHong Zhang           }
7010880e062SHong Zhang         }
7020880e062SHong Zhang       } else {
7030880e062SHong Zhang         for (ii=0; ii<bs; ii++,value+=stepval) {
7040880e062SHong Zhang           for (jj=0; jj<bs; jj++) {
7050880e062SHong Zhang             *bap++ = *value++;
7060880e062SHong Zhang           }
7070880e062SHong Zhang         }
7080880e062SHong Zhang        }
7090880e062SHong Zhang     noinsert2:;
7100880e062SHong Zhang       low = i;
7110880e062SHong Zhang     }
7120880e062SHong Zhang     ailen[row] = nrow;
7130880e062SHong Zhang   }
7140880e062SHong Zhang   PetscFunctionReturn(0);
71549b5e25fSSatish Balay }
71649b5e25fSSatish Balay 
71764831d72SBarry Smith /*
71864831d72SBarry Smith     This is not yet used
71964831d72SBarry Smith */
7204a2ae208SSatish Balay #undef __FUNCT__
7214108e4d5SBarry Smith #define __FUNCT__ "MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode"
7224108e4d5SBarry Smith PetscErrorCode MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode(Mat A)
7230def2e27SBarry Smith {
7240def2e27SBarry Smith   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
7250def2e27SBarry Smith   PetscErrorCode ierr;
7260def2e27SBarry Smith   const PetscInt *ai = a->i, *aj = a->j,*cols;
7270def2e27SBarry Smith   PetscInt       i   = 0,j,blk_size,m = A->rmap->n,node_count = 0,nzx,nzy,*ns,row,nz,cnt,cnt2,*counts;
728ace3abfcSBarry Smith   PetscBool      flag;
7290def2e27SBarry Smith 
7300def2e27SBarry Smith   PetscFunctionBegin;
731785e854fSJed Brown   ierr = PetscMalloc1(m,&ns);CHKERRQ(ierr);
7320def2e27SBarry Smith   while (i < m) {
7330def2e27SBarry Smith     nzx = ai[i+1] - ai[i];       /* Number of nonzeros */
7340def2e27SBarry Smith     /* Limits the number of elements in a node to 'a->inode.limit' */
7350def2e27SBarry Smith     for (j=i+1,blk_size=1; j<m && blk_size <a->inode.limit; ++j,++blk_size) {
7360def2e27SBarry Smith       nzy = ai[j+1] - ai[j];
7370def2e27SBarry Smith       if (nzy != (nzx - j + i)) break;
7380def2e27SBarry Smith       ierr = PetscMemcmp(aj + ai[i] + j - i,aj + ai[j],nzy*sizeof(PetscInt),&flag);CHKERRQ(ierr);
7390def2e27SBarry Smith       if (!flag) break;
7400def2e27SBarry Smith     }
7410def2e27SBarry Smith     ns[node_count++] = blk_size;
74226fbe8dcSKarl Rupp 
7430def2e27SBarry Smith     i = j;
7440def2e27SBarry Smith   }
7450def2e27SBarry Smith   if (!a->inode.size && m && node_count > .9*m) {
7460def2e27SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
7470def2e27SBarry Smith     ierr = PetscInfo2(A,"Found %D nodes out of %D rows. Not using Inode routines\n",node_count,m);CHKERRQ(ierr);
7480def2e27SBarry Smith   } else {
7490def2e27SBarry Smith     a->inode.node_count = node_count;
75026fbe8dcSKarl Rupp 
751785e854fSJed Brown     ierr = PetscMalloc1(node_count,&a->inode.size);CHKERRQ(ierr);
7523bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A,node_count*sizeof(PetscInt));CHKERRQ(ierr);
75322d28d08SBarry Smith     ierr = PetscMemcpy(a->inode.size,ns,node_count*sizeof(PetscInt));CHKERRQ(ierr);
7540def2e27SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
7550def2e27SBarry Smith     ierr = PetscInfo3(A,"Found %D nodes of %D. Limit used: %D. Using Inode routines\n",node_count,m,a->inode.limit);CHKERRQ(ierr);
7560def2e27SBarry Smith 
7570def2e27SBarry Smith     /* count collections of adjacent columns in each inode */
7580def2e27SBarry Smith     row = 0;
7590def2e27SBarry Smith     cnt = 0;
7600def2e27SBarry Smith     for (i=0; i<node_count; i++) {
7610def2e27SBarry Smith       cols = aj + ai[row] + a->inode.size[i];
7620def2e27SBarry Smith       nz   = ai[row+1] - ai[row] - a->inode.size[i];
7630def2e27SBarry Smith       for (j=1; j<nz; j++) {
76426fbe8dcSKarl Rupp         if (cols[j] != cols[j-1]+1) cnt++;
7650def2e27SBarry Smith       }
7660def2e27SBarry Smith       cnt++;
7670def2e27SBarry Smith       row += a->inode.size[i];
7680def2e27SBarry Smith     }
769785e854fSJed Brown     ierr = PetscMalloc1(2*cnt,&counts);CHKERRQ(ierr);
7700def2e27SBarry Smith     cnt  = 0;
7710def2e27SBarry Smith     row  = 0;
7720def2e27SBarry Smith     for (i=0; i<node_count; i++) {
7730def2e27SBarry Smith       cols = aj + ai[row] + a->inode.size[i];
7740def2e27SBarry Smith       counts[2*cnt] = cols[0];
7750def2e27SBarry Smith       nz   = ai[row+1] - ai[row] - a->inode.size[i];
7760def2e27SBarry Smith       cnt2 = 1;
7770def2e27SBarry Smith       for (j=1; j<nz; j++) {
7780def2e27SBarry Smith         if (cols[j] != cols[j-1]+1) {
7790def2e27SBarry Smith           counts[2*(cnt++)+1] = cnt2;
7800def2e27SBarry Smith           counts[2*cnt]       = cols[j];
7810def2e27SBarry Smith           cnt2 = 1;
7820def2e27SBarry Smith         } else cnt2++;
7830def2e27SBarry Smith       }
7840def2e27SBarry Smith       counts[2*(cnt++)+1] = cnt2;
7850def2e27SBarry Smith       row += a->inode.size[i];
7860def2e27SBarry Smith     }
78722d28d08SBarry Smith     ierr = PetscIntView(2*cnt,counts,0);CHKERRQ(ierr);
7880def2e27SBarry Smith   }
78938702af4SBarry Smith   PetscFunctionReturn(0);
79038702af4SBarry Smith }
79138702af4SBarry Smith 
79238702af4SBarry Smith #undef __FUNCT__
7934a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqSBAIJ"
794dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqSBAIJ(Mat A,MatAssemblyType mode)
79549b5e25fSSatish Balay {
79649b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
7976849ba73SBarry Smith   PetscErrorCode ierr;
79813f74950SBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
799d0f46423SBarry Smith   PetscInt       m      = A->rmap->N,*ip,N,*ailen = a->ilen;
80013f74950SBarry Smith   PetscInt       mbs    = a->mbs,bs2 = a->bs2,rmax = 0;
80149b5e25fSSatish Balay   MatScalar      *aa    = a->a,*ap;
80249b5e25fSSatish Balay 
80349b5e25fSSatish Balay   PetscFunctionBegin;
80449b5e25fSSatish Balay   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
80549b5e25fSSatish Balay 
80649b5e25fSSatish Balay   if (m) rmax = ailen[0];
80749b5e25fSSatish Balay   for (i=1; i<mbs; i++) {
80849b5e25fSSatish Balay     /* move each row back by the amount of empty slots (fshift) before it*/
80949b5e25fSSatish Balay     fshift += imax[i-1] - ailen[i-1];
81049b5e25fSSatish Balay     rmax    = PetscMax(rmax,ailen[i]);
81149b5e25fSSatish Balay     if (fshift) {
81249b5e25fSSatish Balay       ip = aj + ai[i]; ap = aa + bs2*ai[i];
81349b5e25fSSatish Balay       N  = ailen[i];
81449b5e25fSSatish Balay       for (j=0; j<N; j++) {
81549b5e25fSSatish Balay         ip[j-fshift] = ip[j];
81649b5e25fSSatish Balay         ierr         = PetscMemcpy(ap+(j-fshift)*bs2,ap+j*bs2,bs2*sizeof(MatScalar));CHKERRQ(ierr);
81749b5e25fSSatish Balay       }
81849b5e25fSSatish Balay     }
81949b5e25fSSatish Balay     ai[i] = ai[i-1] + ailen[i-1];
82049b5e25fSSatish Balay   }
82149b5e25fSSatish Balay   if (mbs) {
82249b5e25fSSatish Balay     fshift += imax[mbs-1] - ailen[mbs-1];
82349b5e25fSSatish Balay     ai[mbs] = ai[mbs-1] + ailen[mbs-1];
82449b5e25fSSatish Balay   }
82549b5e25fSSatish Balay   /* reset ilen and imax for each row */
82649b5e25fSSatish Balay   for (i=0; i<mbs; i++) {
82749b5e25fSSatish Balay     ailen[i] = imax[i] = ai[i+1] - ai[i];
82849b5e25fSSatish Balay   }
8296c6c5352SBarry Smith   a->nz = ai[mbs];
83049b5e25fSSatish Balay 
831b424e231SHong Zhang   /* diagonals may have moved, reset it */
832b424e231SHong Zhang   if (a->diag) {
8332ed38d0bSJed Brown     ierr = PetscMemcpy(a->diag,ai,mbs*sizeof(PetscInt));CHKERRQ(ierr);
83449b5e25fSSatish Balay   }
83526fbe8dcSKarl 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);
83626fbe8dcSKarl Rupp 
837d0f46423SBarry 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);
838ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues is %D\n",a->reallocs);CHKERRQ(ierr);
839ae15b995SBarry Smith   ierr = PetscInfo1(A,"Most nonzeros blocks in any row is %D\n",rmax);CHKERRQ(ierr);
84026fbe8dcSKarl Rupp 
8418e58a170SBarry Smith   A->info.mallocs    += a->reallocs;
84249b5e25fSSatish Balay   a->reallocs         = 0;
84349b5e25fSSatish Balay   A->info.nz_unneeded = (PetscReal)fshift*bs2;
844061b2667SBarry Smith   a->idiagvalid       = PETSC_FALSE;
8454dcd73b1SHong Zhang   a->rmax             = rmax;
84638702af4SBarry Smith 
84738702af4SBarry Smith   if (A->cmap->n < 65536 && A->cmap->bs == 1) {
84844e1c64aSLisandro Dalcin     if (a->jshort && a->free_jshort) {
84917803ae8SHong Zhang       /* when matrix data structure is changed, previous jshort must be replaced */
85017803ae8SHong Zhang       ierr = PetscFree(a->jshort);CHKERRQ(ierr);
85117803ae8SHong Zhang     }
852785e854fSJed Brown     ierr = PetscMalloc1(a->i[A->rmap->n],&a->jshort);CHKERRQ(ierr);
8533bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)A,a->i[A->rmap->n]*sizeof(unsigned short));CHKERRQ(ierr);
85438702af4SBarry Smith     for (i=0; i<a->i[A->rmap->n]; i++) a->jshort[i] = a->j[i];
85538702af4SBarry Smith     A->ops->mult   = MatMult_SeqSBAIJ_1_ushort;
85641f059aeSBarry Smith     A->ops->sor    = MatSOR_SeqSBAIJ_ushort;
8574da8f245SBarry Smith     a->free_jshort = PETSC_TRUE;
85838702af4SBarry Smith   }
85949b5e25fSSatish Balay   PetscFunctionReturn(0);
86049b5e25fSSatish Balay }
86149b5e25fSSatish Balay 
86249b5e25fSSatish Balay /*
86349b5e25fSSatish Balay    This function returns an array of flags which indicate the locations of contiguous
86449b5e25fSSatish Balay    blocks that should be zeroed. for eg: if bs = 3  and is = [0,1,2,3,5,6,7,8,9]
86549b5e25fSSatish Balay    then the resulting sizes = [3,1,1,3,1] correspondig to sets [(0,1,2),(3),(5),(6,7,8),(9)]
86649b5e25fSSatish Balay    Assume: sizes should be long enough to hold all the values.
86749b5e25fSSatish Balay */
8684a2ae208SSatish Balay #undef __FUNCT__
8694a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqSBAIJ_Check_Blocks"
87013f74950SBarry Smith PetscErrorCode MatZeroRows_SeqSBAIJ_Check_Blocks(PetscInt idx[],PetscInt n,PetscInt bs,PetscInt sizes[], PetscInt *bs_max)
87149b5e25fSSatish Balay {
87213f74950SBarry Smith   PetscInt  i,j,k,row;
873ace3abfcSBarry Smith   PetscBool flg;
87449b5e25fSSatish Balay 
87549b5e25fSSatish Balay   PetscFunctionBegin;
87649b5e25fSSatish Balay   for (i=0,j=0; i<n; j++) {
87749b5e25fSSatish Balay     row = idx[i];
87849b5e25fSSatish Balay     if (row%bs!=0) { /* Not the begining of a block */
87949b5e25fSSatish Balay       sizes[j] = 1;
88049b5e25fSSatish Balay       i++;
88149b5e25fSSatish Balay     } else if (i+bs > n) { /* Beginning of a block, but complete block doesn't exist (at idx end) */
88249b5e25fSSatish Balay       sizes[j] = 1;         /* Also makes sure atleast 'bs' values exist for next else */
88349b5e25fSSatish Balay       i++;
88449b5e25fSSatish Balay     } else { /* Begining of the block, so check if the complete block exists */
88549b5e25fSSatish Balay       flg = PETSC_TRUE;
88649b5e25fSSatish Balay       for (k=1; k<bs; k++) {
88749b5e25fSSatish Balay         if (row+k != idx[i+k]) { /* break in the block */
88849b5e25fSSatish Balay           flg = PETSC_FALSE;
88949b5e25fSSatish Balay           break;
89049b5e25fSSatish Balay         }
89149b5e25fSSatish Balay       }
892abc0a331SBarry Smith       if (flg) { /* No break in the bs */
89349b5e25fSSatish Balay         sizes[j] = bs;
89449b5e25fSSatish Balay         i       += bs;
89549b5e25fSSatish Balay       } else {
89649b5e25fSSatish Balay         sizes[j] = 1;
89749b5e25fSSatish Balay         i++;
89849b5e25fSSatish Balay       }
89949b5e25fSSatish Balay     }
90049b5e25fSSatish Balay   }
90149b5e25fSSatish Balay   *bs_max = j;
90249b5e25fSSatish Balay   PetscFunctionReturn(0);
90349b5e25fSSatish Balay }
90449b5e25fSSatish Balay 
90549b5e25fSSatish Balay 
90649b5e25fSSatish Balay /* Only add/insert a(i,j) with i<=j (blocks).
90749b5e25fSSatish Balay    Any a(i,j) with i>j input by user is ingored.
90849b5e25fSSatish Balay */
90949b5e25fSSatish Balay 
9104a2ae208SSatish Balay #undef __FUNCT__
9114a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqSBAIJ"
91213f74950SBarry Smith PetscErrorCode MatSetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
91349b5e25fSSatish Balay {
91449b5e25fSSatish Balay   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
9156849ba73SBarry Smith   PetscErrorCode ierr;
916e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,lastcol = -1;
91713f74950SBarry Smith   PetscInt       *imax=a->imax,*ai=a->i,*ailen=a->ilen,roworiented=a->roworiented;
918d0f46423SBarry Smith   PetscInt       *aj  =a->j,nonew=a->nonew,bs=A->rmap->bs,brow,bcol;
91913f74950SBarry Smith   PetscInt       ridx,cidx,bs2=a->bs2;
92049b5e25fSSatish Balay   MatScalar      *ap,value,*aa=a->a,*bap;
92149b5e25fSSatish Balay 
92249b5e25fSSatish Balay   PetscFunctionBegin;
92349b5e25fSSatish Balay   for (k=0; k<m; k++) { /* loop over added rows */
92449b5e25fSSatish Balay     row  = im[k];       /* row number */
92549b5e25fSSatish Balay     brow = row/bs;      /* block row number */
92649b5e25fSSatish Balay     if (row < 0) continue;
9272515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
928e32f2f54SBarry 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);
92949b5e25fSSatish Balay #endif
93049b5e25fSSatish Balay     rp   = aj + ai[brow]; /*ptr to beginning of column value of the row block*/
93149b5e25fSSatish Balay     ap   = aa + bs2*ai[brow]; /*ptr to beginning of element value of the row block*/
93249b5e25fSSatish Balay     rmax = imax[brow];  /* maximum space allocated for this row */
93349b5e25fSSatish Balay     nrow = ailen[brow]; /* actual length of this row */
93449b5e25fSSatish Balay     low  = 0;
93549b5e25fSSatish Balay 
93649b5e25fSSatish Balay     for (l=0; l<n; l++) { /* loop over added columns */
93749b5e25fSSatish Balay       if (in[l] < 0) continue;
9382515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
939e32f2f54SBarry 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);
94049b5e25fSSatish Balay #endif
94149b5e25fSSatish Balay       col  = in[l];
94249b5e25fSSatish Balay       bcol = col/bs;              /* block col number */
94349b5e25fSSatish Balay 
944941593c8SHong Zhang       if (brow > bcol) {
94526fbe8dcSKarl Rupp         if (a->ignore_ltriangular) continue; /* ignore lower triangular values */
94626fbe8dcSKarl 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)");
947941593c8SHong Zhang       }
948f4989cb3SHong Zhang 
94949b5e25fSSatish Balay       ridx = row % bs; cidx = col % bs; /*row and col index inside the block */
9508549e402SHong Zhang       if ((brow==bcol && ridx<=cidx) || (brow<bcol)) {
95149b5e25fSSatish Balay         /* element value a(k,l) */
95226fbe8dcSKarl Rupp         if (roworiented) value = v[l + k*n];
95326fbe8dcSKarl Rupp         else value = v[k + l*m];
95449b5e25fSSatish Balay 
95549b5e25fSSatish Balay         /* move pointer bap to a(k,l) quickly and add/insert value */
95626fbe8dcSKarl Rupp         if (col <= lastcol) low = 0;
95726fbe8dcSKarl Rupp         high = nrow;
958e2ee6c50SBarry Smith         lastcol = col;
95949b5e25fSSatish Balay         while (high-low > 7) {
96049b5e25fSSatish Balay           t = (low+high)/2;
96149b5e25fSSatish Balay           if (rp[t] > bcol) high = t;
96249b5e25fSSatish Balay           else              low  = t;
96349b5e25fSSatish Balay         }
96449b5e25fSSatish Balay         for (i=low; i<high; i++) {
96549b5e25fSSatish Balay           if (rp[i] > bcol) break;
96649b5e25fSSatish Balay           if (rp[i] == bcol) {
96749b5e25fSSatish Balay             bap = ap +  bs2*i + bs*cidx + ridx;
96849b5e25fSSatish Balay             if (is == ADD_VALUES) *bap += value;
96949b5e25fSSatish Balay             else                  *bap  = value;
9708549e402SHong Zhang             /* for diag block, add/insert its symmetric element a(cidx,ridx) */
9718549e402SHong Zhang             if (brow == bcol && ridx < cidx) {
9728549e402SHong Zhang               bap = ap +  bs2*i + bs*ridx + cidx;
9738549e402SHong Zhang               if (is == ADD_VALUES) *bap += value;
9748549e402SHong Zhang               else                  *bap  = value;
9758549e402SHong Zhang             }
97649b5e25fSSatish Balay             goto noinsert1;
97749b5e25fSSatish Balay           }
97849b5e25fSSatish Balay         }
97949b5e25fSSatish Balay 
98049b5e25fSSatish Balay         if (nonew == 1) goto noinsert1;
981e32f2f54SBarry Smith         if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
982fef13f97SBarry Smith         MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
98349b5e25fSSatish Balay 
984c03d1d03SSatish Balay         N = nrow++ - 1; high++;
98549b5e25fSSatish Balay         /* shift up all the later entries in this row */
98649b5e25fSSatish Balay         for (ii=N; ii>=i; ii--) {
98749b5e25fSSatish Balay           rp[ii+1] = rp[ii];
98849b5e25fSSatish Balay           ierr     = PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));CHKERRQ(ierr);
98949b5e25fSSatish Balay         }
99049b5e25fSSatish Balay         if (N>=i) {
99149b5e25fSSatish Balay           ierr = PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));CHKERRQ(ierr);
99249b5e25fSSatish Balay         }
99349b5e25fSSatish Balay         rp[i]                      = bcol;
99449b5e25fSSatish Balay         ap[bs2*i + bs*cidx + ridx] = value;
995e56f5c9eSBarry Smith         A->nonzerostate++;
99649b5e25fSSatish Balay noinsert1:;
99749b5e25fSSatish Balay         low = i;
9988549e402SHong Zhang       }
99949b5e25fSSatish Balay     }   /* end of loop over added columns */
100049b5e25fSSatish Balay     ailen[brow] = nrow;
100149b5e25fSSatish Balay   }   /* end of loop over added rows */
100249b5e25fSSatish Balay   PetscFunctionReturn(0);
100349b5e25fSSatish Balay }
100449b5e25fSSatish Balay 
10054a2ae208SSatish Balay #undef __FUNCT__
10064d101231SSatish Balay #define __FUNCT__ "MatICCFactor_SeqSBAIJ"
10070481f469SBarry Smith PetscErrorCode MatICCFactor_SeqSBAIJ(Mat inA,IS row,const MatFactorInfo *info)
100849b5e25fSSatish Balay {
10094ccecd49SHong Zhang   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)inA->data;
101049b5e25fSSatish Balay   Mat            outA;
1011dfbe8321SBarry Smith   PetscErrorCode ierr;
1012ace3abfcSBarry Smith   PetscBool      row_identity;
101349b5e25fSSatish Balay 
101449b5e25fSSatish Balay   PetscFunctionBegin;
1015e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 is supported for in-place icc");
1016c84f5b01SHong Zhang   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1017e32f2f54SBarry Smith   if (!row_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix reordering is not supported");
1018e32f2f54SBarry 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()! */
1019c84f5b01SHong Zhang 
102049b5e25fSSatish Balay   outA            = inA;
1021d5f3da31SBarry Smith   inA->factortype = MAT_FACTOR_ICC;
102249b5e25fSSatish Balay 
10231a3463dfSHong Zhang   ierr = MatMarkDiagonal_SeqSBAIJ(inA);CHKERRQ(ierr);
1024d595f711SHong Zhang   ierr = MatSeqSBAIJSetNumericFactorization_inplace(inA,row_identity);CHKERRQ(ierr);
102549b5e25fSSatish Balay 
1026c3122656SLisandro Dalcin   ierr   = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
10276bf464f9SBarry Smith   ierr   = ISDestroy(&a->row);CHKERRQ(ierr);
1028c84f5b01SHong Zhang   a->row = row;
1029c3122656SLisandro Dalcin   ierr   = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
10306bf464f9SBarry Smith   ierr   = ISDestroy(&a->col);CHKERRQ(ierr);
1031c84f5b01SHong Zhang   a->col = row;
1032c84f5b01SHong Zhang 
1033c84f5b01SHong Zhang   /* Create the invert permutation so that it can be used in MatCholeskyFactorNumeric() */
1034c84f5b01SHong Zhang   if (a->icol) {ierr = ISInvertPermutation(row,PETSC_DECIDE, &a->icol);CHKERRQ(ierr);}
10353bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);CHKERRQ(ierr);
103649b5e25fSSatish Balay 
103749b5e25fSSatish Balay   if (!a->solve_work) {
1038854ce69bSBarry Smith     ierr = PetscMalloc1(inA->rmap->N+inA->rmap->bs,&a->solve_work);CHKERRQ(ierr);
10393bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)inA,(inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar));CHKERRQ(ierr);
104049b5e25fSSatish Balay   }
104149b5e25fSSatish Balay 
1042719d5645SBarry Smith   ierr = MatCholeskyFactorNumeric(outA,inA,info);CHKERRQ(ierr);
104349b5e25fSSatish Balay   PetscFunctionReturn(0);
104449b5e25fSSatish Balay }
1045950f1e5bSHong Zhang 
10464a2ae208SSatish Balay #undef __FUNCT__
10474a2ae208SSatish Balay #define __FUNCT__ "MatSeqSBAIJSetColumnIndices_SeqSBAIJ"
10487087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetColumnIndices_SeqSBAIJ(Mat mat,PetscInt *indices)
104949b5e25fSSatish Balay {
1050045c9aa0SHong Zhang   Mat_SeqSBAIJ   *baij = (Mat_SeqSBAIJ*)mat->data;
105113f74950SBarry Smith   PetscInt       i,nz,n;
10527827cd58SJed Brown   PetscErrorCode ierr;
105349b5e25fSSatish Balay 
105449b5e25fSSatish Balay   PetscFunctionBegin;
10556c6c5352SBarry Smith   nz = baij->maxnz;
1056d0f46423SBarry Smith   n  = mat->cmap->n;
105726fbe8dcSKarl Rupp   for (i=0; i<nz; i++) baij->j[i] = indices[i];
105826fbe8dcSKarl Rupp 
10596c6c5352SBarry Smith   baij->nz = nz;
106026fbe8dcSKarl Rupp   for (i=0; i<n; i++) baij->ilen[i] = baij->imax[i];
106126fbe8dcSKarl Rupp 
10627827cd58SJed Brown   ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
106349b5e25fSSatish Balay   PetscFunctionReturn(0);
106449b5e25fSSatish Balay }
106549b5e25fSSatish Balay 
10664a2ae208SSatish Balay #undef __FUNCT__
10674a2ae208SSatish Balay #define __FUNCT__ "MatSeqSBAIJSetColumnIndices"
106849b5e25fSSatish Balay /*@
106919585528SSatish Balay   MatSeqSBAIJSetColumnIndices - Set the column indices for all the rows
107049b5e25fSSatish Balay   in the matrix.
107149b5e25fSSatish Balay 
107249b5e25fSSatish Balay   Input Parameters:
107319585528SSatish Balay   +  mat     - the SeqSBAIJ matrix
107449b5e25fSSatish Balay   -  indices - the column indices
107549b5e25fSSatish Balay 
107649b5e25fSSatish Balay   Level: advanced
107749b5e25fSSatish Balay 
107849b5e25fSSatish Balay   Notes:
107949b5e25fSSatish Balay   This can be called if you have precomputed the nonzero structure of the
108049b5e25fSSatish Balay   matrix and want to provide it to the matrix object to improve the performance
108149b5e25fSSatish Balay   of the MatSetValues() operation.
108249b5e25fSSatish Balay 
108349b5e25fSSatish Balay   You MUST have set the correct numbers of nonzeros per row in the call to
1084d1be2dadSMatthew Knepley   MatCreateSeqSBAIJ(), and the columns indices MUST be sorted.
108549b5e25fSSatish Balay 
1086ab9f2c04SSatish Balay   MUST be called before any calls to MatSetValues()
108749b5e25fSSatish Balay 
1088ab9f2c04SSatish Balay   .seealso: MatCreateSeqSBAIJ
108949b5e25fSSatish Balay @*/
10907087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetColumnIndices(Mat mat,PetscInt *indices)
109149b5e25fSSatish Balay {
10924ac538c5SBarry Smith   PetscErrorCode ierr;
109349b5e25fSSatish Balay 
109449b5e25fSSatish Balay   PetscFunctionBegin;
10950700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
10964482741eSBarry Smith   PetscValidPointer(indices,2);
10974ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqSBAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));CHKERRQ(ierr);
109849b5e25fSSatish Balay   PetscFunctionReturn(0);
109949b5e25fSSatish Balay }
110049b5e25fSSatish Balay 
11014a2ae208SSatish Balay #undef __FUNCT__
11023c896bc6SHong Zhang #define __FUNCT__ "MatCopy_SeqSBAIJ"
11033c896bc6SHong Zhang PetscErrorCode MatCopy_SeqSBAIJ(Mat A,Mat B,MatStructure str)
11043c896bc6SHong Zhang {
11053c896bc6SHong Zhang   PetscErrorCode ierr;
11063c896bc6SHong Zhang 
11073c896bc6SHong Zhang   PetscFunctionBegin;
11083c896bc6SHong Zhang   /* If the two matrices have the same copy implementation, use fast copy. */
11093c896bc6SHong Zhang   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
11103c896bc6SHong Zhang     Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
11113c896bc6SHong Zhang     Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ*)B->data;
11123c896bc6SHong Zhang 
1113e7e72b3dSBarry 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");
1114d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->N])*sizeof(PetscScalar));CHKERRQ(ierr);
11153c896bc6SHong Zhang   } else {
1116f5edf698SHong Zhang     ierr = MatGetRowUpperTriangular(A);CHKERRQ(ierr);
11173c896bc6SHong Zhang     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
1118f5edf698SHong Zhang     ierr = MatRestoreRowUpperTriangular(A);CHKERRQ(ierr);
11193c896bc6SHong Zhang   }
11203c896bc6SHong Zhang   PetscFunctionReturn(0);
11213c896bc6SHong Zhang }
11223c896bc6SHong Zhang 
11233c896bc6SHong Zhang #undef __FUNCT__
11244994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqSBAIJ"
11254994cf47SJed Brown PetscErrorCode MatSetUp_SeqSBAIJ(Mat A)
1126273d9f13SBarry Smith {
1127dfbe8321SBarry Smith   PetscErrorCode ierr;
1128273d9f13SBarry Smith 
1129273d9f13SBarry Smith   PetscFunctionBegin;
1130535b19f3SBarry Smith   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(A,A->rmap->bs,PETSC_DEFAULT,0);CHKERRQ(ierr);
1131273d9f13SBarry Smith   PetscFunctionReturn(0);
1132273d9f13SBarry Smith }
1133273d9f13SBarry Smith 
1134a6ece127SHong Zhang #undef __FUNCT__
11358c778c55SBarry Smith #define __FUNCT__ "MatSeqSBAIJGetArray_SeqSBAIJ"
11368c778c55SBarry Smith PetscErrorCode MatSeqSBAIJGetArray_SeqSBAIJ(Mat A,PetscScalar *array[])
1137a6ece127SHong Zhang {
1138a6ece127SHong Zhang   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
11395fd66863SKarl Rupp 
1140a6ece127SHong Zhang   PetscFunctionBegin;
1141a6ece127SHong Zhang   *array = a->a;
1142a6ece127SHong Zhang   PetscFunctionReturn(0);
1143a6ece127SHong Zhang }
1144a6ece127SHong Zhang 
1145a6ece127SHong Zhang #undef __FUNCT__
11468c778c55SBarry Smith #define __FUNCT__ "MatSeqSBAIJRestoreArray_SeqSBAIJ"
11478c778c55SBarry Smith PetscErrorCode MatSeqSBAIJRestoreArray_SeqSBAIJ(Mat A,PetscScalar *array[])
1148a6ece127SHong Zhang {
1149a6ece127SHong Zhang   PetscFunctionBegin;
1150a6ece127SHong Zhang   PetscFunctionReturn(0);
1151a6ece127SHong Zhang }
1152a6ece127SHong Zhang 
115342ee4b1aSHong Zhang #undef __FUNCT__
115452768537SHong Zhang #define __FUNCT__ "MatAXPYGetPreallocation_SeqSBAIJ"
115552768537SHong Zhang PetscErrorCode MatAXPYGetPreallocation_SeqSBAIJ(Mat Y,Mat X,PetscInt *nnz)
115652768537SHong Zhang {
1157b264fe52SHong Zhang   PetscInt       bs = Y->rmap->bs,mbs = Y->rmap->N/bs;
115852768537SHong Zhang   Mat_SeqSBAIJ   *x = (Mat_SeqSBAIJ*)X->data;
115952768537SHong Zhang   Mat_SeqSBAIJ   *y = (Mat_SeqSBAIJ*)Y->data;
1160b264fe52SHong Zhang   PetscErrorCode ierr;
116152768537SHong Zhang 
116252768537SHong Zhang   PetscFunctionBegin;
116352768537SHong Zhang   /* Set the number of nonzeros in the new matrix */
1164b264fe52SHong Zhang   ierr = MatAXPYGetPreallocation_SeqX_private(mbs,x->i,x->j,y->i,y->j,nnz);CHKERRQ(ierr);
116552768537SHong Zhang   PetscFunctionReturn(0);
116652768537SHong Zhang }
116752768537SHong Zhang 
116852768537SHong Zhang #undef __FUNCT__
116942ee4b1aSHong Zhang #define __FUNCT__ "MatAXPY_SeqSBAIJ"
1170f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqSBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
117142ee4b1aSHong Zhang {
117242ee4b1aSHong Zhang   Mat_SeqSBAIJ   *x=(Mat_SeqSBAIJ*)X->data, *y=(Mat_SeqSBAIJ*)Y->data;
1173dfbe8321SBarry Smith   PetscErrorCode ierr;
117431ce2d13SHong Zhang   PetscInt       bs=Y->rmap->bs,bs2=bs*bs;
1175e838b9e7SJed Brown   PetscBLASInt   one = 1;
117642ee4b1aSHong Zhang 
117742ee4b1aSHong Zhang   PetscFunctionBegin;
117842ee4b1aSHong Zhang   if (str == SAME_NONZERO_PATTERN) {
1179f4df32b1SMatthew Knepley     PetscScalar  alpha = a;
1180c5df96a5SBarry Smith     PetscBLASInt bnz;
1181c5df96a5SBarry Smith     ierr = PetscBLASIntCast(x->nz*bs2,&bnz);CHKERRQ(ierr);
11828b83055fSJed Brown     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
1183a3fa217bSJose E. Roman     ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr);
1184ab784542SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
1185ab784542SHong Zhang     ierr = MatSetOption(X,MAT_GETROW_UPPERTRIANGULAR,PETSC_TRUE);CHKERRQ(ierr);
1186ab784542SHong Zhang     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
1187ab784542SHong Zhang     ierr = MatSetOption(X,MAT_GETROW_UPPERTRIANGULAR,PETSC_FALSE);CHKERRQ(ierr);
118842ee4b1aSHong Zhang   } else {
118952768537SHong Zhang     Mat      B;
119052768537SHong Zhang     PetscInt *nnz;
119152768537SHong Zhang     if (bs != X->rmap->bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrices must have same block size");
1192f5edf698SHong Zhang     ierr = MatGetRowUpperTriangular(X);CHKERRQ(ierr);
119352768537SHong Zhang     ierr = MatGetRowUpperTriangular(Y);CHKERRQ(ierr);
119452768537SHong Zhang     ierr = PetscMalloc1(Y->rmap->N,&nnz);CHKERRQ(ierr);
119552768537SHong Zhang     ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr);
119652768537SHong Zhang     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
119752768537SHong Zhang     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
119852768537SHong Zhang     ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr);
119952768537SHong Zhang     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
120052768537SHong Zhang     ierr = MatAXPYGetPreallocation_SeqSBAIJ(Y,X,nnz);CHKERRQ(ierr);
120152768537SHong Zhang     ierr = MatSeqSBAIJSetPreallocation(B,bs,0,nnz);CHKERRQ(ierr);
120252768537SHong Zhang 
120352768537SHong Zhang     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
120452768537SHong Zhang 
120552768537SHong Zhang     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
120652768537SHong Zhang     ierr = PetscFree(nnz);CHKERRQ(ierr);
1207f5edf698SHong Zhang     ierr = MatRestoreRowUpperTriangular(X);CHKERRQ(ierr);
120852768537SHong Zhang     ierr = MatRestoreRowUpperTriangular(Y);CHKERRQ(ierr);
120942ee4b1aSHong Zhang   }
121042ee4b1aSHong Zhang   PetscFunctionReturn(0);
121142ee4b1aSHong Zhang }
121242ee4b1aSHong Zhang 
1213efcf0fc3SBarry Smith #undef __FUNCT__
1214efcf0fc3SBarry Smith #define __FUNCT__ "MatIsSymmetric_SeqSBAIJ"
1215ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqSBAIJ(Mat A,PetscReal tol,PetscBool  *flg)
1216efcf0fc3SBarry Smith {
1217efcf0fc3SBarry Smith   PetscFunctionBegin;
1218efcf0fc3SBarry Smith   *flg = PETSC_TRUE;
1219efcf0fc3SBarry Smith   PetscFunctionReturn(0);
1220efcf0fc3SBarry Smith }
1221efcf0fc3SBarry Smith 
1222efcf0fc3SBarry Smith #undef __FUNCT__
1223efcf0fc3SBarry Smith #define __FUNCT__ "MatIsStructurallySymmetric_SeqSBAIJ"
1224ace3abfcSBarry Smith PetscErrorCode MatIsStructurallySymmetric_SeqSBAIJ(Mat A,PetscBool  *flg)
1225efcf0fc3SBarry Smith {
1226efcf0fc3SBarry Smith   PetscFunctionBegin;
1227efcf0fc3SBarry Smith   *flg = PETSC_TRUE;
1228efcf0fc3SBarry Smith   PetscFunctionReturn(0);
1229efcf0fc3SBarry Smith }
1230efcf0fc3SBarry Smith 
1231efcf0fc3SBarry Smith #undef __FUNCT__
1232efcf0fc3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqSBAIJ"
1233ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqSBAIJ(Mat A,PetscReal tol,PetscBool  *flg)
1234efcf0fc3SBarry Smith {
1235efcf0fc3SBarry Smith   PetscFunctionBegin;
1236efcf0fc3SBarry Smith   *flg = PETSC_FALSE;
1237efcf0fc3SBarry Smith   PetscFunctionReturn(0);
1238efcf0fc3SBarry Smith }
1239efcf0fc3SBarry Smith 
124099cafbc1SBarry Smith #undef __FUNCT__
124199cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqSBAIJ"
124299cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqSBAIJ(Mat A)
124399cafbc1SBarry Smith {
124499cafbc1SBarry Smith   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
124599cafbc1SBarry Smith   PetscInt     i,nz = a->bs2*a->i[a->mbs];
1246dd6ea824SBarry Smith   MatScalar    *aa = a->a;
124799cafbc1SBarry Smith 
124899cafbc1SBarry Smith   PetscFunctionBegin;
124999cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
125099cafbc1SBarry Smith   PetscFunctionReturn(0);
125199cafbc1SBarry Smith }
125299cafbc1SBarry Smith 
125399cafbc1SBarry Smith #undef __FUNCT__
125499cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqSBAIJ"
125599cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqSBAIJ(Mat A)
125699cafbc1SBarry Smith {
125799cafbc1SBarry Smith   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
125899cafbc1SBarry Smith   PetscInt     i,nz = a->bs2*a->i[a->mbs];
1259dd6ea824SBarry Smith   MatScalar    *aa = a->a;
126099cafbc1SBarry Smith 
126199cafbc1SBarry Smith   PetscFunctionBegin;
126299cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
126399cafbc1SBarry Smith   PetscFunctionReturn(0);
126499cafbc1SBarry Smith }
126599cafbc1SBarry Smith 
12663bededecSBarry Smith #undef __FUNCT__
12673bededecSBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqSBAIJ"
12683bededecSBarry Smith PetscErrorCode MatZeroRowsColumns_SeqSBAIJ(Mat A,PetscInt is_n,const PetscInt is_idx[],PetscScalar diag,Vec x, Vec b)
12693bededecSBarry Smith {
12703bededecSBarry Smith   Mat_SeqSBAIJ      *baij=(Mat_SeqSBAIJ*)A->data;
12713bededecSBarry Smith   PetscErrorCode    ierr;
12723bededecSBarry Smith   PetscInt          i,j,k,count;
12733bededecSBarry Smith   PetscInt          bs   =A->rmap->bs,bs2=baij->bs2,row,col;
12743bededecSBarry Smith   PetscScalar       zero = 0.0;
12753bededecSBarry Smith   MatScalar         *aa;
12763bededecSBarry Smith   const PetscScalar *xx;
12773bededecSBarry Smith   PetscScalar       *bb;
127856777dd2SBarry Smith   PetscBool         *zeroed,vecs = PETSC_FALSE;
12793bededecSBarry Smith 
12803bededecSBarry Smith   PetscFunctionBegin;
12813bededecSBarry Smith   /* fix right hand side if needed */
12823bededecSBarry Smith   if (x && b) {
12833bededecSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
12843bededecSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
128556777dd2SBarry Smith     vecs = PETSC_TRUE;
12863bededecSBarry Smith   }
12873bededecSBarry Smith 
12883bededecSBarry Smith   /* zero the columns */
12891795a4d1SJed Brown   ierr = PetscCalloc1(A->rmap->n,&zeroed);CHKERRQ(ierr);
12903bededecSBarry Smith   for (i=0; i<is_n; i++) {
12913bededecSBarry 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]);
12923bededecSBarry Smith     zeroed[is_idx[i]] = PETSC_TRUE;
12933bededecSBarry Smith   }
129456777dd2SBarry Smith   if (vecs) {
129556777dd2SBarry Smith     for (i=0; i<A->rmap->N; i++) {
129656777dd2SBarry Smith       row = i/bs;
129756777dd2SBarry Smith       for (j=baij->i[row]; j<baij->i[row+1]; j++) {
129856777dd2SBarry Smith         for (k=0; k<bs; k++) {
129956777dd2SBarry Smith           col = bs*baij->j[j] + k;
130056777dd2SBarry Smith           if (col <= i) continue;
130156777dd2SBarry Smith           aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
130226fbe8dcSKarl Rupp           if (!zeroed[i] && zeroed[col]) bb[i]   -= aa[0]*xx[col];
130326fbe8dcSKarl Rupp           if (zeroed[i] && !zeroed[col]) bb[col] -= aa[0]*xx[i];
130456777dd2SBarry Smith         }
130556777dd2SBarry Smith       }
130656777dd2SBarry Smith     }
130726fbe8dcSKarl Rupp     for (i=0; i<is_n; i++) bb[is_idx[i]] = diag*xx[is_idx[i]];
130856777dd2SBarry Smith   }
130956777dd2SBarry Smith 
13103bededecSBarry Smith   for (i=0; i<A->rmap->N; i++) {
13113bededecSBarry Smith     if (!zeroed[i]) {
13123bededecSBarry Smith       row = i/bs;
13133bededecSBarry Smith       for (j=baij->i[row]; j<baij->i[row+1]; j++) {
13143bededecSBarry Smith         for (k=0; k<bs; k++) {
13153bededecSBarry Smith           col = bs*baij->j[j] + k;
13163bededecSBarry Smith           if (zeroed[col]) {
13173bededecSBarry Smith             aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
13183bededecSBarry Smith             aa[0] = 0.0;
13193bededecSBarry Smith           }
13203bededecSBarry Smith         }
13213bededecSBarry Smith       }
13223bededecSBarry Smith     }
13233bededecSBarry Smith   }
13243bededecSBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
132556777dd2SBarry Smith   if (vecs) {
132656777dd2SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
132756777dd2SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
132856777dd2SBarry Smith   }
13293bededecSBarry Smith 
13303bededecSBarry Smith   /* zero the rows */
13313bededecSBarry Smith   for (i=0; i<is_n; i++) {
13323bededecSBarry Smith     row   = is_idx[i];
13333bededecSBarry Smith     count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
13343bededecSBarry Smith     aa    = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
13353bededecSBarry Smith     for (k=0; k<count; k++) {
13363bededecSBarry Smith       aa[0] =  zero;
13373bededecSBarry Smith       aa   += bs;
13383bededecSBarry Smith     }
13393bededecSBarry Smith     if (diag != 0.0) {
13403bededecSBarry Smith       ierr = (*A->ops->setvalues)(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr);
13413bededecSBarry Smith     }
13423bededecSBarry Smith   }
13433bededecSBarry Smith   ierr = MatAssemblyEnd_SeqSBAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13443bededecSBarry Smith   PetscFunctionReturn(0);
13453bededecSBarry Smith }
13463bededecSBarry Smith 
134749b5e25fSSatish Balay /* -------------------------------------------------------------------*/
13483964eb88SJed Brown static struct _MatOps MatOps_Values = {MatSetValues_SeqSBAIJ,
134949b5e25fSSatish Balay                                        MatGetRow_SeqSBAIJ,
135049b5e25fSSatish Balay                                        MatRestoreRow_SeqSBAIJ,
135149b5e25fSSatish Balay                                        MatMult_SeqSBAIJ_N,
135297304618SKris Buschelman                                /*  4*/ MatMultAdd_SeqSBAIJ_N,
1353431c96f7SBarry Smith                                        MatMult_SeqSBAIJ_N,       /* transpose versions are same as non-transpose versions */
1354e005ede5SBarry Smith                                        MatMultAdd_SeqSBAIJ_N,
1355db4efbfdSBarry Smith                                        0,
135649b5e25fSSatish Balay                                        0,
135749b5e25fSSatish Balay                                        0,
135897304618SKris Buschelman                                /* 10*/ 0,
135949b5e25fSSatish Balay                                        0,
1360c078aec8SLisandro Dalcin                                        MatCholeskyFactor_SeqSBAIJ,
136141f059aeSBarry Smith                                        MatSOR_SeqSBAIJ,
136249b5e25fSSatish Balay                                        MatTranspose_SeqSBAIJ,
136397304618SKris Buschelman                                /* 15*/ MatGetInfo_SeqSBAIJ,
136449b5e25fSSatish Balay                                        MatEqual_SeqSBAIJ,
136549b5e25fSSatish Balay                                        MatGetDiagonal_SeqSBAIJ,
136649b5e25fSSatish Balay                                        MatDiagonalScale_SeqSBAIJ,
136749b5e25fSSatish Balay                                        MatNorm_SeqSBAIJ,
136897304618SKris Buschelman                                /* 20*/ 0,
136949b5e25fSSatish Balay                                        MatAssemblyEnd_SeqSBAIJ,
137049b5e25fSSatish Balay                                        MatSetOption_SeqSBAIJ,
137149b5e25fSSatish Balay                                        MatZeroEntries_SeqSBAIJ,
1372d519adbfSMatthew Knepley                                /* 24*/ 0,
137349b5e25fSSatish Balay                                        0,
137449b5e25fSSatish Balay                                        0,
1375db4efbfdSBarry Smith                                        0,
1376db4efbfdSBarry Smith                                        0,
13774994cf47SJed Brown                                /* 29*/ MatSetUp_SeqSBAIJ,
1378c464158bSHong Zhang                                        0,
1379db4efbfdSBarry Smith                                        0,
13808c778c55SBarry Smith                                        0,
13818c778c55SBarry Smith                                        0,
1382d519adbfSMatthew Knepley                                /* 34*/ MatDuplicate_SeqSBAIJ,
1383719d5645SBarry Smith                                        0,
1384719d5645SBarry Smith                                        0,
138549b5e25fSSatish Balay                                        0,
1386c84f5b01SHong Zhang                                        MatICCFactor_SeqSBAIJ,
1387d519adbfSMatthew Knepley                                /* 39*/ MatAXPY_SeqSBAIJ,
138849b5e25fSSatish Balay                                        MatGetSubMatrices_SeqSBAIJ,
138949b5e25fSSatish Balay                                        MatIncreaseOverlap_SeqSBAIJ,
139049b5e25fSSatish Balay                                        MatGetValues_SeqSBAIJ,
13913c896bc6SHong Zhang                                        MatCopy_SeqSBAIJ,
1392d519adbfSMatthew Knepley                                /* 44*/ 0,
139349b5e25fSSatish Balay                                        MatScale_SeqSBAIJ,
139449b5e25fSSatish Balay                                        0,
139549b5e25fSSatish Balay                                        0,
13963bededecSBarry Smith                                        MatZeroRowsColumns_SeqSBAIJ,
1397f73d5cc4SBarry Smith                                /* 49*/ 0,
139849b5e25fSSatish Balay                                        MatGetRowIJ_SeqSBAIJ,
139949b5e25fSSatish Balay                                        MatRestoreRowIJ_SeqSBAIJ,
140049b5e25fSSatish Balay                                        0,
140149b5e25fSSatish Balay                                        0,
1402d519adbfSMatthew Knepley                                /* 54*/ 0,
140349b5e25fSSatish Balay                                        0,
140449b5e25fSSatish Balay                                        0,
140549b5e25fSSatish Balay                                        0,
140649b5e25fSSatish Balay                                        MatSetValuesBlocked_SeqSBAIJ,
1407d519adbfSMatthew Knepley                                /* 59*/ MatGetSubMatrix_SeqSBAIJ,
140849b5e25fSSatish Balay                                        0,
140949b5e25fSSatish Balay                                        0,
1410357abbc8SBarry Smith                                        0,
1411d959ec07SHong Zhang                                        0,
1412d519adbfSMatthew Knepley                                /* 64*/ 0,
1413d959ec07SHong Zhang                                        0,
1414d959ec07SHong Zhang                                        0,
1415d959ec07SHong Zhang                                        0,
1416d959ec07SHong Zhang                                        0,
1417d519adbfSMatthew Knepley                                /* 69*/ MatGetRowMaxAbs_SeqSBAIJ,
14183e0d88b5SBarry Smith                                        0,
14193e0d88b5SBarry Smith                                        0,
14203e0d88b5SBarry Smith                                        0,
14213e0d88b5SBarry Smith                                        0,
1422d519adbfSMatthew Knepley                                /* 74*/ 0,
14233e0d88b5SBarry Smith                                        0,
14243e0d88b5SBarry Smith                                        0,
14253e0d88b5SBarry Smith                                        0,
14263e0d88b5SBarry Smith                                        0,
1427d519adbfSMatthew Knepley                                /* 79*/ 0,
14283e0d88b5SBarry Smith                                        0,
14293e0d88b5SBarry Smith                                        0,
143097304618SKris Buschelman                                        MatGetInertia_SeqSBAIJ,
14315bba2384SShri Abhyankar                                        MatLoad_SeqSBAIJ,
1432d519adbfSMatthew Knepley                                /* 84*/ MatIsSymmetric_SeqSBAIJ,
1433865e5f61SKris Buschelman                                        MatIsHermitian_SeqSBAIJ,
1434efcf0fc3SBarry Smith                                        MatIsStructurallySymmetric_SeqSBAIJ,
1435865e5f61SKris Buschelman                                        0,
1436865e5f61SKris Buschelman                                        0,
1437d519adbfSMatthew Knepley                                /* 89*/ 0,
1438865e5f61SKris Buschelman                                        0,
1439865e5f61SKris Buschelman                                        0,
1440865e5f61SKris Buschelman                                        0,
1441865e5f61SKris Buschelman                                        0,
1442d519adbfSMatthew Knepley                                /* 94*/ 0,
1443865e5f61SKris Buschelman                                        0,
1444865e5f61SKris Buschelman                                        0,
144599cafbc1SBarry Smith                                        0,
144699cafbc1SBarry Smith                                        0,
1447d519adbfSMatthew Knepley                                /* 99*/ 0,
144899cafbc1SBarry Smith                                        0,
144999cafbc1SBarry Smith                                        0,
145099cafbc1SBarry Smith                                        0,
145199cafbc1SBarry Smith                                        0,
1452d519adbfSMatthew Knepley                                /*104*/ 0,
145399cafbc1SBarry Smith                                        MatRealPart_SeqSBAIJ,
1454f5edf698SHong Zhang                                        MatImaginaryPart_SeqSBAIJ,
1455f5edf698SHong Zhang                                        MatGetRowUpperTriangular_SeqSBAIJ,
14562af78befSBarry Smith                                        MatRestoreRowUpperTriangular_SeqSBAIJ,
1457d519adbfSMatthew Knepley                                /*109*/ 0,
14582af78befSBarry Smith                                        0,
14592af78befSBarry Smith                                        0,
14602af78befSBarry Smith                                        0,
1461547795f9SHong Zhang                                        MatMissingDiagonal_SeqSBAIJ,
1462547795f9SHong Zhang                                /*114*/ 0,
1463547795f9SHong Zhang                                        0,
1464547795f9SHong Zhang                                        0,
1465547795f9SHong Zhang                                        0,
1466547795f9SHong Zhang                                        0,
1467547795f9SHong Zhang                                /*119*/ 0,
1468547795f9SHong Zhang                                        0,
14692f480046SShri Abhyankar                                        0,
14703964eb88SJed Brown                                        0,
14713964eb88SJed Brown                                        0,
14723964eb88SJed Brown                                /*124*/ 0,
14733964eb88SJed Brown                                        0,
14743964eb88SJed Brown                                        0,
14753964eb88SJed Brown                                        0,
14763964eb88SJed Brown                                        0,
14773964eb88SJed Brown                                /*129*/ 0,
14783964eb88SJed Brown                                        0,
14793964eb88SJed Brown                                        0,
14803964eb88SJed Brown                                        0,
14813964eb88SJed Brown                                        0,
14823964eb88SJed Brown                                /*134*/ 0,
14833964eb88SJed Brown                                        0,
14843964eb88SJed Brown                                        0,
14853964eb88SJed Brown                                        0,
14863964eb88SJed Brown                                        0,
14873964eb88SJed Brown                                /*139*/ 0,
1488f9426fe0SMark Adams                                        0,
148959f5e6ceSHong Zhang                                        0,
149059f5e6ceSHong Zhang                                        0,
149159f5e6ceSHong Zhang                                        0,
149259f5e6ceSHong Zhang                                 /*144*/MatCreateMPIMatConcatenateSeqMat_SeqSBAIJ
149399cafbc1SBarry Smith };
1494be1d678aSKris Buschelman 
14954a2ae208SSatish Balay #undef __FUNCT__
14964a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqSBAIJ"
14977087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqSBAIJ(Mat mat)
149849b5e25fSSatish Balay {
14994afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ*)mat->data;
1500d0f46423SBarry Smith   PetscInt       nz   = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
1501dfbe8321SBarry Smith   PetscErrorCode ierr;
150249b5e25fSSatish Balay 
150349b5e25fSSatish Balay   PetscFunctionBegin;
1504e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
150549b5e25fSSatish Balay 
150649b5e25fSSatish Balay   /* allocate space for values if not already there */
150749b5e25fSSatish Balay   if (!aij->saved_values) {
1508854ce69bSBarry Smith     ierr = PetscMalloc1(nz+1,&aij->saved_values);CHKERRQ(ierr);
150949b5e25fSSatish Balay   }
151049b5e25fSSatish Balay 
151149b5e25fSSatish Balay   /* copy values over */
151287828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
151349b5e25fSSatish Balay   PetscFunctionReturn(0);
151449b5e25fSSatish Balay }
151549b5e25fSSatish Balay 
15164a2ae208SSatish Balay #undef __FUNCT__
15174a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqSBAIJ"
15187087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqSBAIJ(Mat mat)
151949b5e25fSSatish Balay {
15204afc71dfSHong Zhang   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ*)mat->data;
15216849ba73SBarry Smith   PetscErrorCode ierr;
1522d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;
152349b5e25fSSatish Balay 
152449b5e25fSSatish Balay   PetscFunctionBegin;
1525e7e72b3dSBarry Smith   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
1526e7e72b3dSBarry Smith   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
152749b5e25fSSatish Balay 
152849b5e25fSSatish Balay   /* copy values over */
152987828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
153049b5e25fSSatish Balay   PetscFunctionReturn(0);
153149b5e25fSSatish Balay }
153249b5e25fSSatish Balay 
15334a2ae208SSatish Balay #undef __FUNCT__
1534a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation_SeqSBAIJ"
15357087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetPreallocation_SeqSBAIJ(Mat B,PetscInt bs,PetscInt nz,PetscInt *nnz)
153649b5e25fSSatish Balay {
1537c464158bSHong Zhang   Mat_SeqSBAIJ   *b = (Mat_SeqSBAIJ*)B->data;
15386849ba73SBarry Smith   PetscErrorCode ierr;
15394dcd73b1SHong Zhang   PetscInt       i,mbs,nbs,bs2;
15402576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,flg = PETSC_FALSE,realalloc = PETSC_FALSE;
154149b5e25fSSatish Balay 
154249b5e25fSSatish Balay   PetscFunctionBegin;
15432576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
1544273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
1545db4efbfdSBarry Smith 
154633d57670SJed Brown   ierr = MatSetBlockSize(B,PetscAbs(bs));CHKERRQ(ierr);
154726283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
154826283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
1549e02043d6SBarry Smith   ierr = PetscLayoutGetBlockSize(B->rmap,&bs);CHKERRQ(ierr);
1550899cda47SBarry Smith 
1551d0f46423SBarry Smith   mbs = B->rmap->N/bs;
15524dcd73b1SHong Zhang   nbs = B->cmap->n/bs;
155349b5e25fSSatish Balay   bs2 = bs*bs;
155449b5e25fSSatish Balay 
15554dcd73b1SHong 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");
155649b5e25fSSatish Balay 
1557ab93d7beSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
1558ab93d7beSBarry Smith     skipallocation = PETSC_TRUE;
1559ab93d7beSBarry Smith     nz             = 0;
1560ab93d7beSBarry Smith   }
1561ab93d7beSBarry Smith 
1562435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 3;
1563e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
156449b5e25fSSatish Balay   if (nnz) {
156549b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
1566e32f2f54SBarry 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]);
1567de64b629SHong 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);
156849b5e25fSSatish Balay     }
156949b5e25fSSatish Balay   }
157049b5e25fSSatish Balay 
1571db4efbfdSBarry Smith   B->ops->mult             = MatMult_SeqSBAIJ_N;
1572db4efbfdSBarry Smith   B->ops->multadd          = MatMultAdd_SeqSBAIJ_N;
1573db4efbfdSBarry Smith   B->ops->multtranspose    = MatMult_SeqSBAIJ_N;
1574db4efbfdSBarry Smith   B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_N;
157526fbe8dcSKarl Rupp 
15760298fd71SBarry Smith   ierr  = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,NULL);CHKERRQ(ierr);
157749b5e25fSSatish Balay   if (!flg) {
157849b5e25fSSatish Balay     switch (bs) {
157949b5e25fSSatish Balay     case 1:
158049b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_1;
158149b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_1;
1582431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_1;
1583431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_1;
158449b5e25fSSatish Balay       break;
158549b5e25fSSatish Balay     case 2:
158649b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_2;
158749b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_2;
1588431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_2;
1589431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_2;
159049b5e25fSSatish Balay       break;
159149b5e25fSSatish Balay     case 3:
159249b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_3;
159349b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_3;
1594431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_3;
1595431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_3;
159649b5e25fSSatish Balay       break;
159749b5e25fSSatish Balay     case 4:
159849b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_4;
159949b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_4;
1600431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_4;
1601431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_4;
160249b5e25fSSatish Balay       break;
160349b5e25fSSatish Balay     case 5:
160449b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_5;
160549b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_5;
1606431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_5;
1607431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_5;
160849b5e25fSSatish Balay       break;
160949b5e25fSSatish Balay     case 6:
161049b5e25fSSatish Balay       B->ops->mult             = MatMult_SeqSBAIJ_6;
161149b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_6;
1612431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_6;
1613431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_6;
161449b5e25fSSatish Balay       break;
161549b5e25fSSatish Balay     case 7:
1616de53e5efSHong Zhang       B->ops->mult             = MatMult_SeqSBAIJ_7;
161749b5e25fSSatish Balay       B->ops->multadd          = MatMultAdd_SeqSBAIJ_7;
1618431c96f7SBarry Smith       B->ops->multtranspose    = MatMult_SeqSBAIJ_7;
1619431c96f7SBarry Smith       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_7;
162049b5e25fSSatish Balay       break;
162149b5e25fSSatish Balay     }
162249b5e25fSSatish Balay   }
162349b5e25fSSatish Balay 
162449b5e25fSSatish Balay   b->mbs = mbs;
16254dcd73b1SHong Zhang   b->nbs = nbs;
1626ab93d7beSBarry Smith   if (!skipallocation) {
16272ee49352SLisandro Dalcin     if (!b->imax) {
1628dcca6d9dSJed Brown       ierr = PetscMalloc2(mbs,&b->imax,mbs,&b->ilen);CHKERRQ(ierr);
162926fbe8dcSKarl Rupp 
1630c760cd28SBarry Smith       b->free_imax_ilen = PETSC_TRUE;
163126fbe8dcSKarl Rupp 
16323bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)B,2*mbs*sizeof(PetscInt));CHKERRQ(ierr);
16332ee49352SLisandro Dalcin     }
163449b5e25fSSatish Balay     if (!nnz) {
1635435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
163649b5e25fSSatish Balay       else if (nz <= 0) nz = 1;
163726fbe8dcSKarl Rupp       for (i=0; i<mbs; i++) b->imax[i] = nz;
1638153ea458SHong Zhang       nz = nz*mbs; /* total nz */
163949b5e25fSSatish Balay     } else {
164049b5e25fSSatish Balay       nz = 0;
16418cef66ccSHong Zhang       for (i=0; i<mbs; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
164249b5e25fSSatish Balay     }
16432ee49352SLisandro Dalcin     /* b->ilen will count nonzeros in each block row so far. */
164426fbe8dcSKarl Rupp     for (i=0; i<mbs; i++) b->ilen[i] = 0;
16456c6c5352SBarry Smith     /* nz=(nz+mbs)/2; */ /* total diagonal and superdiagonal nonzero blocks */
164649b5e25fSSatish Balay 
164749b5e25fSSatish Balay     /* allocate the matrix space */
16482ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
1649dcca6d9dSJed Brown     ierr = PetscMalloc3(bs2*nz,&b->a,nz,&b->j,B->rmap->N+1,&b->i);CHKERRQ(ierr);
16503bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)B,(B->rmap->N+1)*sizeof(PetscInt)+nz*(bs2*sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
16516c6c5352SBarry Smith     ierr = PetscMemzero(b->a,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
165213f74950SBarry Smith     ierr = PetscMemzero(b->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
165326fbe8dcSKarl Rupp 
165449b5e25fSSatish Balay     b->singlemalloc = PETSC_TRUE;
165549b5e25fSSatish Balay 
165649b5e25fSSatish Balay     /* pointer to beginning of each row */
1657e60cf9a0SBarry Smith     b->i[0] = 0;
165826fbe8dcSKarl Rupp     for (i=1; i<mbs+1; i++) b->i[i] = b->i[i-1] + b->imax[i-1];
165926fbe8dcSKarl Rupp 
1660e6b907acSBarry Smith     b->free_a  = PETSC_TRUE;
1661e6b907acSBarry Smith     b->free_ij = PETSC_TRUE;
1662e811da20SHong Zhang   } else {
1663e6b907acSBarry Smith     b->free_a  = PETSC_FALSE;
1664e6b907acSBarry Smith     b->free_ij = PETSC_FALSE;
1665ab93d7beSBarry Smith   }
166649b5e25fSSatish Balay 
1667d0f46423SBarry Smith   B->rmap->bs = bs;
166849b5e25fSSatish Balay   b->bs2      = bs2;
16696c6c5352SBarry Smith   b->nz       = 0;
1670b32cb4a7SJed Brown   b->maxnz    = nz;
1671153ea458SHong Zhang 
167216cdd363SHong Zhang   b->inew    = 0;
167316cdd363SHong Zhang   b->jnew    = 0;
167416cdd363SHong Zhang   b->anew    = 0;
167516cdd363SHong Zhang   b->a2anew  = 0;
16761a3463dfSHong Zhang   b->permute = PETSC_FALSE;
16772576faa2SJed Brown   if (realalloc) {ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);}
1678c464158bSHong Zhang   PetscFunctionReturn(0);
1679c464158bSHong Zhang }
1680153ea458SHong Zhang 
168138f409ebSLisandro Dalcin #undef __FUNCT__
168238f409ebSLisandro Dalcin #define __FUNCT__ "MatSeqSBAIJSetPreallocationCSR_SeqSBAIJ"
168338f409ebSLisandro Dalcin PetscErrorCode MatSeqSBAIJSetPreallocationCSR_SeqSBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[], const PetscScalar V[])
168438f409ebSLisandro Dalcin {
168538f409ebSLisandro Dalcin   PetscInt       i,j,m,nz,nz_max=0,*nnz;
168638f409ebSLisandro Dalcin   PetscScalar    *values=0;
168738f409ebSLisandro Dalcin   PetscBool      roworiented = ((Mat_SeqSBAIJ*)B->data)->roworiented;
168838f409ebSLisandro Dalcin   PetscErrorCode ierr;
168938f409ebSLisandro Dalcin   PetscFunctionBegin;
169038f409ebSLisandro Dalcin   if (bs < 1) SETERRQ1(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive but it is %D",bs);
169138f409ebSLisandro Dalcin   ierr   = PetscLayoutSetBlockSize(B->rmap,bs);CHKERRQ(ierr);
169238f409ebSLisandro Dalcin   ierr   = PetscLayoutSetBlockSize(B->cmap,bs);CHKERRQ(ierr);
169338f409ebSLisandro Dalcin   ierr   = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
169438f409ebSLisandro Dalcin   ierr   = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
169538f409ebSLisandro Dalcin   ierr   = PetscLayoutGetBlockSize(B->rmap,&bs);CHKERRQ(ierr);
169638f409ebSLisandro Dalcin   m      = B->rmap->n/bs;
169738f409ebSLisandro Dalcin 
169838f409ebSLisandro Dalcin   if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]);
1699854ce69bSBarry Smith   ierr = PetscMalloc1(m+1,&nnz);CHKERRQ(ierr);
170038f409ebSLisandro Dalcin   for (i=0; i<m; i++) {
170138f409ebSLisandro Dalcin     nz = ii[i+1] - ii[i];
170238f409ebSLisandro Dalcin     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D has a negative number of columns %D",i,nz);
170338f409ebSLisandro Dalcin     nz_max = PetscMax(nz_max,nz);
170438f409ebSLisandro Dalcin     nnz[i] = nz;
170538f409ebSLisandro Dalcin   }
170638f409ebSLisandro Dalcin   ierr = MatSeqSBAIJSetPreallocation(B,bs,0,nnz);CHKERRQ(ierr);
170738f409ebSLisandro Dalcin   ierr = PetscFree(nnz);CHKERRQ(ierr);
170838f409ebSLisandro Dalcin 
170938f409ebSLisandro Dalcin   values = (PetscScalar*)V;
171038f409ebSLisandro Dalcin   if (!values) {
17111795a4d1SJed Brown     ierr = PetscCalloc1(bs*bs*nz_max,&values);CHKERRQ(ierr);
171238f409ebSLisandro Dalcin   }
171338f409ebSLisandro Dalcin   for (i=0; i<m; i++) {
171438f409ebSLisandro Dalcin     PetscInt          ncols  = ii[i+1] - ii[i];
171538f409ebSLisandro Dalcin     const PetscInt    *icols = jj + ii[i];
171638f409ebSLisandro Dalcin     if (!roworiented || bs == 1) {
171738f409ebSLisandro Dalcin       const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
171838f409ebSLisandro Dalcin       ierr = MatSetValuesBlocked_SeqSBAIJ(B,1,&i,ncols,icols,svals,INSERT_VALUES);CHKERRQ(ierr);
171938f409ebSLisandro Dalcin     } else {
172038f409ebSLisandro Dalcin       for (j=0; j<ncols; j++) {
172138f409ebSLisandro Dalcin         const PetscScalar *svals = values + (V ? (bs*bs*(ii[i]+j)) : 0);
172238f409ebSLisandro Dalcin         ierr = MatSetValuesBlocked_SeqSBAIJ(B,1,&i,1,&icols[j],svals,INSERT_VALUES);CHKERRQ(ierr);
172338f409ebSLisandro Dalcin       }
172438f409ebSLisandro Dalcin     }
172538f409ebSLisandro Dalcin   }
172638f409ebSLisandro Dalcin   if (!V) { ierr = PetscFree(values);CHKERRQ(ierr); }
172738f409ebSLisandro Dalcin   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
172838f409ebSLisandro Dalcin   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
172938f409ebSLisandro Dalcin   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
173038f409ebSLisandro Dalcin   PetscFunctionReturn(0);
173138f409ebSLisandro Dalcin }
173238f409ebSLisandro Dalcin 
1733db4efbfdSBarry Smith /*
1734db4efbfdSBarry Smith    This is used to set the numeric factorization for both Cholesky and ICC symbolic factorization
1735db4efbfdSBarry Smith */
17368b1456e3SHong Zhang #undef __FUNCT__
1737d595f711SHong Zhang #define __FUNCT__ "MatSeqSBAIJSetNumericFactorization_inplace"
1738ace3abfcSBarry Smith PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat B,PetscBool natural)
1739db4efbfdSBarry Smith {
1740db4efbfdSBarry Smith   PetscErrorCode ierr;
1741ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
1742db4efbfdSBarry Smith   PetscInt       bs  = B->rmap->bs;
1743db4efbfdSBarry Smith 
1744db4efbfdSBarry Smith   PetscFunctionBegin;
17450298fd71SBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,NULL);CHKERRQ(ierr);
1746db4efbfdSBarry Smith   if (flg) bs = 8;
1747db4efbfdSBarry Smith 
1748db4efbfdSBarry Smith   if (!natural) {
1749db4efbfdSBarry Smith     switch (bs) {
1750db4efbfdSBarry Smith     case 1:
1751d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_inplace;
1752db4efbfdSBarry Smith       break;
1753db4efbfdSBarry Smith     case 2:
1754db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2;
1755db4efbfdSBarry Smith       break;
1756db4efbfdSBarry Smith     case 3:
1757db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3;
1758db4efbfdSBarry Smith       break;
1759db4efbfdSBarry Smith     case 4:
1760db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4;
1761db4efbfdSBarry Smith       break;
1762db4efbfdSBarry Smith     case 5:
1763db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5;
1764db4efbfdSBarry Smith       break;
1765db4efbfdSBarry Smith     case 6:
1766db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6;
1767db4efbfdSBarry Smith       break;
1768db4efbfdSBarry Smith     case 7:
1769db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7;
1770db4efbfdSBarry Smith       break;
1771db4efbfdSBarry Smith     default:
1772db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N;
1773db4efbfdSBarry Smith       break;
1774db4efbfdSBarry Smith     }
1775db4efbfdSBarry Smith   } else {
1776db4efbfdSBarry Smith     switch (bs) {
1777db4efbfdSBarry Smith     case 1:
1778d595f711SHong Zhang       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering_inplace;
1779db4efbfdSBarry Smith       break;
1780db4efbfdSBarry Smith     case 2:
1781db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering;
1782db4efbfdSBarry Smith       break;
1783db4efbfdSBarry Smith     case 3:
1784db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3_NaturalOrdering;
1785db4efbfdSBarry Smith       break;
1786db4efbfdSBarry Smith     case 4:
1787db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4_NaturalOrdering;
1788db4efbfdSBarry Smith       break;
1789db4efbfdSBarry Smith     case 5:
1790db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5_NaturalOrdering;
1791db4efbfdSBarry Smith       break;
1792db4efbfdSBarry Smith     case 6:
1793db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6_NaturalOrdering;
1794db4efbfdSBarry Smith       break;
1795db4efbfdSBarry Smith     case 7:
1796db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7_NaturalOrdering;
1797db4efbfdSBarry Smith       break;
1798db4efbfdSBarry Smith     default:
1799db4efbfdSBarry Smith       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering;
1800db4efbfdSBarry Smith       break;
1801db4efbfdSBarry Smith     }
1802db4efbfdSBarry Smith   }
1803db4efbfdSBarry Smith   PetscFunctionReturn(0);
1804db4efbfdSBarry Smith }
1805db4efbfdSBarry Smith 
18068cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqAIJ(Mat, MatType,MatReuse,Mat*);
18078cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqBAIJ(Mat, MatType,MatReuse,Mat*);
1808d769727bSBarry Smith 
18095c9eb25fSBarry Smith #undef __FUNCT__
18105c9eb25fSBarry Smith #define __FUNCT__ "MatGetFactor_seqsbaij_petsc"
18118cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_seqsbaij_petsc(Mat A,MatFactorType ftype,Mat *B)
18125c9eb25fSBarry Smith {
1813d0f46423SBarry Smith   PetscInt       n = A->rmap->n;
18145c9eb25fSBarry Smith   PetscErrorCode ierr;
18155c9eb25fSBarry Smith 
18165c9eb25fSBarry Smith   PetscFunctionBegin;
18170e92d65fSHong Zhang #if defined(PETSC_USE_COMPLEX)
18180e92d65fSHong Zhang   if (A->hermitian) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Hermitian Factor is not supported");
18190e92d65fSHong Zhang #endif
1820ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),B);CHKERRQ(ierr);
18215c9eb25fSBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
18225c9eb25fSBarry Smith   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
18235c9eb25fSBarry Smith     ierr = MatSetType(*B,MATSEQSBAIJ);CHKERRQ(ierr);
18240298fd71SBarry Smith     ierr = MatSeqSBAIJSetPreallocation(*B,A->rmap->bs,MAT_SKIP_ALLOCATION,NULL);CHKERRQ(ierr);
182526fbe8dcSKarl Rupp 
18267b056e98SHong Zhang     (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqSBAIJ;
1827c6d0d4f0SHong Zhang     (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqSBAIJ;
1828e32f2f54SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported");
1829d5f3da31SBarry Smith   (*B)->factortype = ftype;
18305c9eb25fSBarry Smith   PetscFunctionReturn(0);
18315c9eb25fSBarry Smith }
18325c9eb25fSBarry Smith 
18330bad9183SKris Buschelman /*MC
1834fafad747SKris Buschelman   MATSEQSBAIJ - MATSEQSBAIJ = "seqsbaij" - A matrix type to be used for sequential symmetric block sparse matrices,
18350bad9183SKris Buschelman   based on block compressed sparse row format.  Only the upper triangular portion of the matrix is stored.
18360bad9183SKris Buschelman 
1837828413b8SBarry Smith   For complex numbers by default this matrix is symmetric, NOT Hermitian symmetric. To make it Hermitian symmetric you
183871dad5bbSBarry Smith   can call MatSetOption(Mat, MAT_HERMITIAN); after MatAssemblyEnd()
1839828413b8SBarry Smith 
18400bad9183SKris Buschelman   Options Database Keys:
18410bad9183SKris Buschelman   . -mat_type seqsbaij - sets the matrix type to "seqsbaij" during a call to MatSetFromOptions()
18420bad9183SKris Buschelman 
184371dad5bbSBarry Smith   Notes: By default if you insert values into the lower triangular part of the matrix they are simply ignored (since they are not
184471dad5bbSBarry 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
184571dad5bbSBarry 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.
184671dad5bbSBarry Smith 
184771dad5bbSBarry Smith 
18480bad9183SKris Buschelman   Level: beginner
18490bad9183SKris Buschelman 
18500bad9183SKris Buschelman   .seealso: MatCreateSeqSBAIJ
18510bad9183SKris Buschelman M*/
18520bad9183SKris Buschelman 
18538cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqSBSTRM(Mat, MatType,MatReuse,Mat*);
1854aa5a9175SDahai Guo 
1855a23d5eceSKris Buschelman #undef __FUNCT__
1856a23d5eceSKris Buschelman #define __FUNCT__ "MatCreate_SeqSBAIJ"
18578cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_SeqSBAIJ(Mat B)
1858a23d5eceSKris Buschelman {
1859a23d5eceSKris Buschelman   Mat_SeqSBAIJ   *b;
1860dfbe8321SBarry Smith   PetscErrorCode ierr;
186113f74950SBarry Smith   PetscMPIInt    size;
1862ace3abfcSBarry Smith   PetscBool      no_unroll = PETSC_FALSE,no_inode = PETSC_FALSE;
1863a23d5eceSKris Buschelman 
1864a23d5eceSKris Buschelman   PetscFunctionBegin;
1865ce94432eSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr);
1866e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Comm must be of size 1");
1867a23d5eceSKris Buschelman 
1868b00a9115SJed Brown   ierr    = PetscNewLog(B,&b);CHKERRQ(ierr);
1869a23d5eceSKris Buschelman   B->data = (void*)b;
1870a23d5eceSKris Buschelman   ierr    = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
187126fbe8dcSKarl Rupp 
1872a23d5eceSKris Buschelman   B->ops->destroy    = MatDestroy_SeqSBAIJ;
1873a23d5eceSKris Buschelman   B->ops->view       = MatView_SeqSBAIJ;
1874a23d5eceSKris Buschelman   b->row             = 0;
1875a23d5eceSKris Buschelman   b->icol            = 0;
1876a23d5eceSKris Buschelman   b->reallocs        = 0;
1877a23d5eceSKris Buschelman   b->saved_values    = 0;
18780def2e27SBarry Smith   b->inode.limit     = 5;
18790def2e27SBarry Smith   b->inode.max_limit = 5;
1880a23d5eceSKris Buschelman 
1881a23d5eceSKris Buschelman   b->roworiented        = PETSC_TRUE;
1882a23d5eceSKris Buschelman   b->nonew              = 0;
1883a23d5eceSKris Buschelman   b->diag               = 0;
1884a23d5eceSKris Buschelman   b->solve_work         = 0;
1885a23d5eceSKris Buschelman   b->mult_work          = 0;
1886a23d5eceSKris Buschelman   B->spptr              = 0;
1887f2cbd3d5SJed Brown   B->info.nz_unneeded   = (PetscReal)b->maxnz*b->bs2;
1888a9817697SBarry Smith   b->keepnonzeropattern = PETSC_FALSE;
1889a23d5eceSKris Buschelman 
1890a23d5eceSKris Buschelman   b->inew    = 0;
1891a23d5eceSKris Buschelman   b->jnew    = 0;
1892a23d5eceSKris Buschelman   b->anew    = 0;
1893a23d5eceSKris Buschelman   b->a2anew  = 0;
1894a23d5eceSKris Buschelman   b->permute = PETSC_FALSE;
1895a23d5eceSKris Buschelman 
189671dad5bbSBarry Smith   b->ignore_ltriangular = PETSC_TRUE;
189726fbe8dcSKarl Rupp 
18980298fd71SBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_ignore_lower_triangular",&b->ignore_ltriangular,NULL);CHKERRQ(ierr);
1899941593c8SHong Zhang 
1900f5edf698SHong Zhang   b->getrow_utriangular = PETSC_FALSE;
190126fbe8dcSKarl Rupp 
19020298fd71SBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_getrow_uppertriangular",&b->getrow_utriangular,NULL);CHKERRQ(ierr);
1903f5edf698SHong Zhang 
1904bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqSBAIJ);CHKERRQ(ierr);
1905bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqSBAIJ);CHKERRQ(ierr);
1906bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqSBAIJSetColumnIndices_C",MatSeqSBAIJSetColumnIndices_SeqSBAIJ);CHKERRQ(ierr);
1907bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_seqaij_C",MatConvert_SeqSBAIJ_SeqAIJ);CHKERRQ(ierr);
1908bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_seqbaij_C",MatConvert_SeqSBAIJ_SeqBAIJ);CHKERRQ(ierr);
1909bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqSBAIJSetPreallocation_C",MatSeqSBAIJSetPreallocation_SeqSBAIJ);CHKERRQ(ierr);
191038f409ebSLisandro Dalcin   ierr = PetscObjectComposeFunction((PetscObject)B,"MatSeqSBAIJSetPreallocationCSR_C",MatSeqSBAIJSetPreallocationCSR_SeqSBAIJ);CHKERRQ(ierr);
1911bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_seqsbstrm_C",MatConvert_SeqSBAIJ_SeqSBSTRM);CHKERRQ(ierr);
1912*6214f412SHong Zhang #if defined(PETSC_HAVE_ELEMENTAL)
1913*6214f412SHong Zhang   ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_elemental_C",MatConvert_SeqSBAIJ_Elemental);CHKERRQ(ierr);
1914*6214f412SHong Zhang #endif
191523ce1328SBarry Smith 
191623ce1328SBarry Smith   B->symmetric                  = PETSC_TRUE;
191723ce1328SBarry Smith   B->structurally_symmetric     = PETSC_TRUE;
191823ce1328SBarry Smith   B->symmetric_set              = PETSC_TRUE;
191923ce1328SBarry Smith   B->structurally_symmetric_set = PETSC_TRUE;
192026fbe8dcSKarl Rupp 
192117667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQSBAIJ);CHKERRQ(ierr);
19220def2e27SBarry Smith 
1923ce94432eSBarry Smith   ierr = PetscOptionsBegin(PetscObjectComm((PetscObject)B),((PetscObject)B)->prefix,"Options for SEQSBAIJ matrix","Mat");CHKERRQ(ierr);
19240298fd71SBarry Smith   ierr = PetscOptionsBool("-mat_no_unroll","Do not optimize for inodes (slower)",NULL,no_unroll,&no_unroll,NULL);CHKERRQ(ierr);
192526fbe8dcSKarl Rupp   if (no_unroll) {
192626fbe8dcSKarl Rupp     ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_unroll\n");CHKERRQ(ierr);
192726fbe8dcSKarl Rupp   }
19280298fd71SBarry Smith   ierr = PetscOptionsBool("-mat_no_inode","Do not optimize for inodes (slower)",NULL,no_inode,&no_inode,NULL);CHKERRQ(ierr);
192926fbe8dcSKarl Rupp   if (no_inode) {
193026fbe8dcSKarl Rupp     ierr = PetscInfo(B,"Not using Inode routines due to -mat_no_inode\n");CHKERRQ(ierr);
193126fbe8dcSKarl Rupp   }
19320298fd71SBarry Smith   ierr = PetscOptionsInt("-mat_inode_limit","Do not use inodes larger then this value",NULL,b->inode.limit,&b->inode.limit,NULL);CHKERRQ(ierr);
19330def2e27SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1934ace3abfcSBarry Smith   b->inode.use = (PetscBool)(!(no_unroll || no_inode));
19350def2e27SBarry Smith   if (b->inode.limit > b->inode.max_limit) b->inode.limit = b->inode.max_limit;
1936a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1937a23d5eceSKris Buschelman }
1938a23d5eceSKris Buschelman 
1939a23d5eceSKris Buschelman #undef __FUNCT__
1940a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqSBAIJSetPreallocation"
1941a23d5eceSKris Buschelman /*@C
1942a23d5eceSKris Buschelman    MatSeqSBAIJSetPreallocation - Creates a sparse symmetric matrix in block AIJ (block
1943a23d5eceSKris Buschelman    compressed row) format.  For good matrix assembly performance the
1944a23d5eceSKris Buschelman    user should preallocate the matrix storage by setting the parameter nz
1945a23d5eceSKris Buschelman    (or the array nnz).  By setting these parameters accurately, performance
1946a23d5eceSKris Buschelman    during matrix assembly can be increased by more than a factor of 50.
1947a23d5eceSKris Buschelman 
1948a23d5eceSKris Buschelman    Collective on Mat
1949a23d5eceSKris Buschelman 
1950a23d5eceSKris Buschelman    Input Parameters:
19511c4f3114SJed Brown +  B - the symmetric matrix
1952bb7ae925SBarry 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
1953bb7ae925SBarry Smith           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
1954a23d5eceSKris Buschelman .  nz - number of block nonzeros per block row (same for all rows)
1955a23d5eceSKris Buschelman -  nnz - array containing the number of block nonzeros in the upper triangular plus
19560298fd71SBarry Smith          diagonal portion of each block (possibly different for each block row) or NULL
1957a23d5eceSKris Buschelman 
1958a23d5eceSKris Buschelman    Options Database Keys:
1959a23d5eceSKris Buschelman .   -mat_no_unroll - uses code that does not unroll the loops in the
1960a23d5eceSKris Buschelman                      block calculations (much slower)
1961db4efbfdSBarry Smith .    -mat_block_size - size of the blocks to use (only works if a negative bs is passed in
1962a23d5eceSKris Buschelman 
1963a23d5eceSKris Buschelman    Level: intermediate
1964a23d5eceSKris Buschelman 
1965a23d5eceSKris Buschelman    Notes:
1966a23d5eceSKris Buschelman    Specify the preallocated storage with either nz or nnz (not both).
19670298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
1968a7f22e61SSatish Balay    allocation.  See Users-Manual: ch_mat for details.
1969a23d5eceSKris Buschelman 
1970aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
1971aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
1972aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
1973aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
1974aa95bbe8SBarry Smith 
197549a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
197649a6f317SBarry Smith 
197749a6f317SBarry Smith 
197869b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateSBAIJ()
1979a23d5eceSKris Buschelman @*/
19807087cfbeSBarry Smith PetscErrorCode  MatSeqSBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt nz,const PetscInt nnz[])
198113f74950SBarry Smith {
19824ac538c5SBarry Smith   PetscErrorCode ierr;
1983a23d5eceSKris Buschelman 
1984a23d5eceSKris Buschelman   PetscFunctionBegin;
19856ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
19866ba663aaSJed Brown   PetscValidType(B,1);
19876ba663aaSJed Brown   PetscValidLogicalCollectiveInt(B,bs,2);
19884ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqSBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[]),(B,bs,nz,nnz));CHKERRQ(ierr);
1989a23d5eceSKris Buschelman   PetscFunctionReturn(0);
1990a23d5eceSKris Buschelman }
199149b5e25fSSatish Balay 
19924a2ae208SSatish Balay #undef  __FUNCT__
199338f409ebSLisandro Dalcin #define __FUNCT__ "MatSeqSBAIJSetPreallocationCSR"
199438f409ebSLisandro Dalcin /*@C
199538f409ebSLisandro Dalcin    MatSeqSBAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in symmetric block AIJ format.
199638f409ebSLisandro Dalcin 
199738f409ebSLisandro Dalcin    Input Parameters:
19981c4f3114SJed Brown +  B - the matrix
199938f409ebSLisandro Dalcin .  i - the indices into j for the start of each local row (starts with zero)
200038f409ebSLisandro Dalcin .  j - the column indices for each local row (starts with zero) these must be sorted for each row
200138f409ebSLisandro Dalcin -  v - optional values in the matrix
200238f409ebSLisandro Dalcin 
200338f409ebSLisandro Dalcin    Level: developer
200438f409ebSLisandro Dalcin 
200538f409ebSLisandro Dalcin    Notes:
200638f409ebSLisandro Dalcin    The order of the entries in values is specified by the MatOption MAT_ROW_ORIENTED.  For example, C programs
200738f409ebSLisandro 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
200838f409ebSLisandro Dalcin    over rows within a block and the last index is over columns within a block row.  Fortran programs will likely set
200938f409ebSLisandro 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
201038f409ebSLisandro Dalcin    block column and the second index is over columns within a block.
201138f409ebSLisandro Dalcin 
201238f409ebSLisandro Dalcin .keywords: matrix, block, aij, compressed row, sparse
201338f409ebSLisandro Dalcin 
201438f409ebSLisandro Dalcin .seealso: MatCreate(), MatCreateSeqSBAIJ(), MatSetValuesBlocked(), MatSeqSBAIJSetPreallocation(), MATSEQSBAIJ
201538f409ebSLisandro Dalcin @*/
201638f409ebSLisandro Dalcin PetscErrorCode MatSeqSBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
201738f409ebSLisandro Dalcin {
201838f409ebSLisandro Dalcin   PetscErrorCode ierr;
201938f409ebSLisandro Dalcin 
202038f409ebSLisandro Dalcin   PetscFunctionBegin;
202138f409ebSLisandro Dalcin   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
202238f409ebSLisandro Dalcin   PetscValidType(B,1);
202338f409ebSLisandro Dalcin   PetscValidLogicalCollectiveInt(B,bs,2);
202438f409ebSLisandro Dalcin   ierr = PetscTryMethod(B,"MatSeqSBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));CHKERRQ(ierr);
202538f409ebSLisandro Dalcin   PetscFunctionReturn(0);
202638f409ebSLisandro Dalcin }
202738f409ebSLisandro Dalcin 
202838f409ebSLisandro Dalcin #undef __FUNCT__
20294a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqSBAIJ"
2030c464158bSHong Zhang /*@C
2031c464158bSHong Zhang    MatCreateSeqSBAIJ - Creates a sparse symmetric matrix in block AIJ (block
2032c464158bSHong Zhang    compressed row) format.  For good matrix assembly performance the
2033c464158bSHong Zhang    user should preallocate the matrix storage by setting the parameter nz
2034c464158bSHong Zhang    (or the array nnz).  By setting these parameters accurately, performance
2035c464158bSHong Zhang    during matrix assembly can be increased by more than a factor of 50.
203649b5e25fSSatish Balay 
2037c464158bSHong Zhang    Collective on MPI_Comm
2038c464158bSHong Zhang 
2039c464158bSHong Zhang    Input Parameters:
2040c464158bSHong Zhang +  comm - MPI communicator, set to PETSC_COMM_SELF
2041bb7ae925SBarry 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
2042bb7ae925SBarry Smith           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
2043c464158bSHong Zhang .  m - number of rows, or number of columns
2044c464158bSHong Zhang .  nz - number of block nonzeros per block row (same for all rows)
2045744e8345SSatish Balay -  nnz - array containing the number of block nonzeros in the upper triangular plus
20460298fd71SBarry Smith          diagonal portion of each block (possibly different for each block row) or NULL
2047c464158bSHong Zhang 
2048c464158bSHong Zhang    Output Parameter:
2049c464158bSHong Zhang .  A - the symmetric matrix
2050c464158bSHong Zhang 
2051c464158bSHong Zhang    Options Database Keys:
2052c464158bSHong Zhang .   -mat_no_unroll - uses code that does not unroll the loops in the
2053c464158bSHong Zhang                      block calculations (much slower)
2054c464158bSHong Zhang .    -mat_block_size - size of the blocks to use
2055c464158bSHong Zhang 
2056c464158bSHong Zhang    Level: intermediate
2057c464158bSHong Zhang 
2058175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2059ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
2060175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2061175b88e8SBarry Smith 
2062c464158bSHong Zhang    Notes:
20636d6d819aSHong Zhang    The number of rows and columns must be divisible by blocksize.
20646d6d819aSHong Zhang    This matrix type does not support complex Hermitian operation.
2065c464158bSHong Zhang 
2066c464158bSHong Zhang    Specify the preallocated storage with either nz or nnz (not both).
20670298fd71SBarry Smith    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
2068a7f22e61SSatish Balay    allocation.  See Users-Manual: ch_mat for details.
2069c464158bSHong Zhang 
207049a6f317SBarry Smith    If the nnz parameter is given then the nz parameter is ignored
207149a6f317SBarry Smith 
207269b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateSBAIJ()
2073c464158bSHong Zhang @*/
20747087cfbeSBarry Smith PetscErrorCode  MatCreateSeqSBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
2075c464158bSHong Zhang {
2076dfbe8321SBarry Smith   PetscErrorCode ierr;
2077c464158bSHong Zhang 
2078c464158bSHong Zhang   PetscFunctionBegin;
2079f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2080f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2081c464158bSHong Zhang   ierr = MatSetType(*A,MATSEQSBAIJ);CHKERRQ(ierr);
2082ab93d7beSBarry Smith   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*A,bs,nz,(PetscInt*)nnz);CHKERRQ(ierr);
208349b5e25fSSatish Balay   PetscFunctionReturn(0);
208449b5e25fSSatish Balay }
208549b5e25fSSatish Balay 
20864a2ae208SSatish Balay #undef __FUNCT__
20874a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqSBAIJ"
2088dfbe8321SBarry Smith PetscErrorCode MatDuplicate_SeqSBAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
208949b5e25fSSatish Balay {
209049b5e25fSSatish Balay   Mat            C;
209149b5e25fSSatish Balay   Mat_SeqSBAIJ   *c,*a = (Mat_SeqSBAIJ*)A->data;
20926849ba73SBarry Smith   PetscErrorCode ierr;
2093b40805acSSatish Balay   PetscInt       i,mbs = a->mbs,nz = a->nz,bs2 =a->bs2;
209449b5e25fSSatish Balay 
209549b5e25fSSatish Balay   PetscFunctionBegin;
2096e32f2f54SBarry Smith   if (a->i[mbs] != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupt matrix");
209749b5e25fSSatish Balay 
209849b5e25fSSatish Balay   *B   = 0;
2099ce94432eSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr);
2100d0f46423SBarry Smith   ierr = MatSetSizes(C,A->rmap->N,A->cmap->n,A->rmap->N,A->cmap->n);CHKERRQ(ierr);
21018e9a0fb8SHong Zhang   ierr = MatSetType(C,MATSEQSBAIJ);CHKERRQ(ierr);
21021d5dac46SHong Zhang   ierr = PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));CHKERRQ(ierr);
2103692f9cbeSHong Zhang   c    = (Mat_SeqSBAIJ*)C->data;
2104692f9cbeSHong Zhang 
2105273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
2106d5f3da31SBarry Smith   C->factortype         = A->factortype;
210749b5e25fSSatish Balay   c->row                = 0;
210849b5e25fSSatish Balay   c->icol               = 0;
210949b5e25fSSatish Balay   c->saved_values       = 0;
2110a9817697SBarry Smith   c->keepnonzeropattern = a->keepnonzeropattern;
211149b5e25fSSatish Balay   C->assembled          = PETSC_TRUE;
211249b5e25fSSatish Balay 
21131e1e43feSBarry Smith   ierr   = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
21141e1e43feSBarry Smith   ierr   = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
211549b5e25fSSatish Balay   c->bs2 = a->bs2;
211649b5e25fSSatish Balay   c->mbs = a->mbs;
211749b5e25fSSatish Balay   c->nbs = a->nbs;
211849b5e25fSSatish Balay 
2119c760cd28SBarry Smith   if  (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2120c760cd28SBarry Smith     c->imax           = a->imax;
2121c760cd28SBarry Smith     c->ilen           = a->ilen;
2122c760cd28SBarry Smith     c->free_imax_ilen = PETSC_FALSE;
2123c760cd28SBarry Smith   } else {
2124dcca6d9dSJed Brown     ierr = PetscMalloc2((mbs+1),&c->imax,(mbs+1),&c->ilen);CHKERRQ(ierr);
21253bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)C,2*(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
212649b5e25fSSatish Balay     for (i=0; i<mbs; i++) {
212749b5e25fSSatish Balay       c->imax[i] = a->imax[i];
212849b5e25fSSatish Balay       c->ilen[i] = a->ilen[i];
212949b5e25fSSatish Balay     }
2130c760cd28SBarry Smith     c->free_imax_ilen = PETSC_TRUE;
2131c760cd28SBarry Smith   }
213249b5e25fSSatish Balay 
213349b5e25fSSatish Balay   /* allocate the matrix space */
21344da8f245SBarry Smith   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2135785e854fSJed Brown     ierr            = PetscMalloc1(bs2*nz,&c->a);CHKERRQ(ierr);
21363bb1ff40SBarry Smith     ierr            = PetscLogObjectMemory((PetscObject)C,nz*bs2*sizeof(MatScalar));CHKERRQ(ierr);
213744e1c64aSLisandro Dalcin     c->i            = a->i;
213844e1c64aSLisandro Dalcin     c->j            = a->j;
21394da8f245SBarry Smith     c->singlemalloc = PETSC_FALSE;
214044e1c64aSLisandro Dalcin     c->free_a       = PETSC_TRUE;
21414da8f245SBarry Smith     c->free_ij      = PETSC_FALSE;
21424da8f245SBarry Smith     c->parent       = A;
21434da8f245SBarry Smith     ierr            = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
21444da8f245SBarry Smith     ierr            = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
21454da8f245SBarry Smith     ierr            = MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
21464da8f245SBarry Smith   } else {
2147dcca6d9dSJed Brown     ierr            = PetscMalloc3(bs2*nz,&c->a,nz,&c->j,mbs+1,&c->i);CHKERRQ(ierr);
214813f74950SBarry Smith     ierr            = PetscMemcpy(c->i,a->i,(mbs+1)*sizeof(PetscInt));CHKERRQ(ierr);
21493bb1ff40SBarry Smith     ierr            = PetscLogObjectMemory((PetscObject)C,(mbs+1)*sizeof(PetscInt) + nz*(bs2*sizeof(MatScalar) + sizeof(PetscInt)));CHKERRQ(ierr);
21504da8f245SBarry Smith     c->singlemalloc = PETSC_TRUE;
215144e1c64aSLisandro Dalcin     c->free_a       = PETSC_TRUE;
21524da8f245SBarry Smith     c->free_ij      = PETSC_TRUE;
21534da8f245SBarry Smith   }
215449b5e25fSSatish Balay   if (mbs > 0) {
21554da8f245SBarry Smith     if (cpvalues != MAT_SHARE_NONZERO_PATTERN) {
215613f74950SBarry Smith       ierr = PetscMemcpy(c->j,a->j,nz*sizeof(PetscInt));CHKERRQ(ierr);
21574da8f245SBarry Smith     }
215849b5e25fSSatish Balay     if (cpvalues == MAT_COPY_VALUES) {
215949b5e25fSSatish Balay       ierr = PetscMemcpy(c->a,a->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
216049b5e25fSSatish Balay     } else {
216149b5e25fSSatish Balay       ierr = PetscMemzero(c->a,bs2*nz*sizeof(MatScalar));CHKERRQ(ierr);
216249b5e25fSSatish Balay     }
2163a1c3900fSBarry Smith     if (a->jshort) {
216444e1c64aSLisandro Dalcin       /* cannot share jshort, it is reallocated in MatAssemblyEnd_SeqSBAIJ() */
216544e1c64aSLisandro Dalcin       /* if the parent matrix is reassembled, this child matrix will never notice */
2166785e854fSJed Brown       ierr = PetscMalloc1(nz,&c->jshort);CHKERRQ(ierr);
21673bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)C,nz*sizeof(unsigned short));CHKERRQ(ierr);
2168a1c3900fSBarry Smith       ierr = PetscMemcpy(c->jshort,a->jshort,nz*sizeof(unsigned short));CHKERRQ(ierr);
216926fbe8dcSKarl Rupp 
21704da8f245SBarry Smith       c->free_jshort = PETSC_TRUE;
21714da8f245SBarry Smith     }
2172a1c3900fSBarry Smith   }
217349b5e25fSSatish Balay 
217449b5e25fSSatish Balay   c->roworiented = a->roworiented;
217549b5e25fSSatish Balay   c->nonew       = a->nonew;
217649b5e25fSSatish Balay 
217749b5e25fSSatish Balay   if (a->diag) {
2178c760cd28SBarry Smith     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2179c760cd28SBarry Smith       c->diag      = a->diag;
2180c760cd28SBarry Smith       c->free_diag = PETSC_FALSE;
2181c760cd28SBarry Smith     } else {
2182785e854fSJed Brown       ierr = PetscMalloc1(mbs,&c->diag);CHKERRQ(ierr);
21833bb1ff40SBarry Smith       ierr = PetscLogObjectMemory((PetscObject)C,mbs*sizeof(PetscInt));CHKERRQ(ierr);
218426fbe8dcSKarl Rupp       for (i=0; i<mbs; i++) c->diag[i] = a->diag[i];
2185c760cd28SBarry Smith       c->free_diag = PETSC_TRUE;
2186c760cd28SBarry Smith     }
218744e1c64aSLisandro Dalcin   }
21886c6c5352SBarry Smith   c->nz         = a->nz;
2189f2cbd3d5SJed Brown   c->maxnz      = a->nz; /* Since we allocate exactly the right amount */
219049b5e25fSSatish Balay   c->solve_work = 0;
219149b5e25fSSatish Balay   c->mult_work  = 0;
219226fbe8dcSKarl Rupp 
219349b5e25fSSatish Balay   *B   = C;
2194140e18c1SBarry Smith   ierr = PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
219549b5e25fSSatish Balay   PetscFunctionReturn(0);
219649b5e25fSSatish Balay }
219749b5e25fSSatish Balay 
21984a2ae208SSatish Balay #undef __FUNCT__
21995bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_SeqSBAIJ"
2200112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqSBAIJ(Mat newmat,PetscViewer viewer)
22012f480046SShri Abhyankar {
22022f480046SShri Abhyankar   Mat_SeqSBAIJ   *a;
22032f480046SShri Abhyankar   PetscErrorCode ierr;
22042f480046SShri Abhyankar   int            fd;
22052f480046SShri Abhyankar   PetscMPIInt    size;
22063059b6faSBarry Smith   PetscInt       i,nz,header[4],*rowlengths=0,M,N,bs = newmat->rmap->bs;
22072f480046SShri Abhyankar   PetscInt       *mask,mbs,*jj,j,rowcount,nzcount,k,*s_browlengths,maskcount;
22082f480046SShri Abhyankar   PetscInt       kmax,jcount,block,idx,point,nzcountb,extra_rows,rows,cols;
22092f480046SShri Abhyankar   PetscInt       *masked,nmask,tmp,bs2,ishift;
22102f480046SShri Abhyankar   PetscScalar    *aa;
2211ce94432eSBarry Smith   MPI_Comm       comm;
22122f480046SShri Abhyankar 
22132f480046SShri Abhyankar   PetscFunctionBegin;
2214ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
22150298fd71SBarry Smith   ierr = PetscOptionsGetInt(((PetscObject)newmat)->prefix,"-matload_block_size",&bs,NULL);CHKERRQ(ierr);
22163059b6faSBarry Smith   if (bs < 0) bs = 1;
22172f480046SShri Abhyankar   bs2  = bs*bs;
22182f480046SShri Abhyankar 
22192f480046SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
22202f480046SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
22212f480046SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
22222f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
22232f480046SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not Mat object");
22242f480046SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
22252f480046SShri Abhyankar 
22262f480046SShri Abhyankar   if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as SeqSBAIJ");
22272f480046SShri Abhyankar 
22282f480046SShri Abhyankar   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");
22292f480046SShri Abhyankar 
22302f480046SShri Abhyankar   /*
22312f480046SShri Abhyankar      This code adds extra rows to make sure the number of rows is
22322f480046SShri Abhyankar     divisible by the blocksize
22332f480046SShri Abhyankar   */
22342f480046SShri Abhyankar   mbs        = M/bs;
22352f480046SShri Abhyankar   extra_rows = bs - M + bs*(mbs);
22362f480046SShri Abhyankar   if (extra_rows == bs) extra_rows = 0;
22372f480046SShri Abhyankar   else                  mbs++;
22382f480046SShri Abhyankar   if (extra_rows) {
22392f480046SShri Abhyankar     ierr = PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");CHKERRQ(ierr);
22402f480046SShri Abhyankar   }
22412f480046SShri Abhyankar 
22422f480046SShri Abhyankar   /* Set global sizes if not already set */
22432f480046SShri Abhyankar   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) {
22442f480046SShri Abhyankar     ierr = MatSetSizes(newmat,PETSC_DECIDE,PETSC_DECIDE,M+extra_rows,N+extra_rows);CHKERRQ(ierr);
22452f480046SShri Abhyankar   } else { /* Check if the matrix global sizes are correct */
22462f480046SShri Abhyankar     ierr = MatGetSize(newmat,&rows,&cols);CHKERRQ(ierr);
22472f480046SShri 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);
22482f480046SShri Abhyankar   }
22492f480046SShri Abhyankar 
22502f480046SShri Abhyankar   /* read in row lengths */
2251854ce69bSBarry Smith   ierr = PetscMalloc1(M+extra_rows,&rowlengths);CHKERRQ(ierr);
22522f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
22532f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
22542f480046SShri Abhyankar 
22552f480046SShri Abhyankar   /* read in column indices */
2256854ce69bSBarry Smith   ierr = PetscMalloc1(nz+extra_rows,&jj);CHKERRQ(ierr);
22572f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,jj,nz,PETSC_INT);CHKERRQ(ierr);
22582f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) jj[nz+i] = M+i;
22592f480046SShri Abhyankar 
22602f480046SShri Abhyankar   /* loop over row lengths determining block row lengths */
22611795a4d1SJed Brown   ierr     = PetscCalloc1(mbs,&s_browlengths);CHKERRQ(ierr);
2262dcca6d9dSJed Brown   ierr     = PetscMalloc2(mbs,&mask,mbs,&masked);CHKERRQ(ierr);
22632f480046SShri Abhyankar   ierr     = PetscMemzero(mask,mbs*sizeof(PetscInt));CHKERRQ(ierr);
22642f480046SShri Abhyankar   rowcount = 0;
22652f480046SShri Abhyankar   nzcount  = 0;
22662f480046SShri Abhyankar   for (i=0; i<mbs; i++) {
22672f480046SShri Abhyankar     nmask = 0;
22682f480046SShri Abhyankar     for (j=0; j<bs; j++) {
22692f480046SShri Abhyankar       kmax = rowlengths[rowcount];
22702f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
22712f480046SShri Abhyankar         tmp = jj[nzcount++]/bs;   /* block col. index */
22722f480046SShri Abhyankar         if (!mask[tmp] && tmp >= i) {masked[nmask++] = tmp; mask[tmp] = 1;}
22732f480046SShri Abhyankar       }
22742f480046SShri Abhyankar       rowcount++;
22752f480046SShri Abhyankar     }
22762f480046SShri Abhyankar     s_browlengths[i] += nmask;
22772f480046SShri Abhyankar 
22782f480046SShri Abhyankar     /* zero out the mask elements we set */
22792f480046SShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
22802f480046SShri Abhyankar   }
22812f480046SShri Abhyankar 
22822f480046SShri Abhyankar   /* Do preallocation */
22832f480046SShri Abhyankar   ierr = MatSeqSBAIJSetPreallocation_SeqSBAIJ(newmat,bs,0,s_browlengths);CHKERRQ(ierr);
22842f480046SShri Abhyankar   a    = (Mat_SeqSBAIJ*)newmat->data;
22852f480046SShri Abhyankar 
22862f480046SShri Abhyankar   /* set matrix "i" values */
22872f480046SShri Abhyankar   a->i[0] = 0;
22882f480046SShri Abhyankar   for (i=1; i<= mbs; i++) {
22892f480046SShri Abhyankar     a->i[i]      = a->i[i-1] + s_browlengths[i-1];
22902f480046SShri Abhyankar     a->ilen[i-1] = s_browlengths[i-1];
22912f480046SShri Abhyankar   }
22922f480046SShri Abhyankar   a->nz = a->i[mbs];
22932f480046SShri Abhyankar 
22942f480046SShri Abhyankar   /* read in nonzero values */
2295854ce69bSBarry Smith   ierr = PetscMalloc1(nz+extra_rows,&aa);CHKERRQ(ierr);
22962f480046SShri Abhyankar   ierr = PetscBinaryRead(fd,aa,nz,PETSC_SCALAR);CHKERRQ(ierr);
22972f480046SShri Abhyankar   for (i=0; i<extra_rows; i++) aa[nz+i] = 1.0;
22982f480046SShri Abhyankar 
22992f480046SShri Abhyankar   /* set "a" and "j" values into matrix */
23002f480046SShri Abhyankar   nzcount = 0; jcount = 0;
23012f480046SShri Abhyankar   for (i=0; i<mbs; i++) {
23022f480046SShri Abhyankar     nzcountb = nzcount;
23032f480046SShri Abhyankar     nmask    = 0;
23042f480046SShri Abhyankar     for (j=0; j<bs; j++) {
23052f480046SShri Abhyankar       kmax = rowlengths[i*bs+j];
23062f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
23072f480046SShri Abhyankar         tmp = jj[nzcount++]/bs; /* block col. index */
23082f480046SShri Abhyankar         if (!mask[tmp] && tmp >= i) { masked[nmask++] = tmp; mask[tmp] = 1;}
23092f480046SShri Abhyankar       }
23102f480046SShri Abhyankar     }
23112f480046SShri Abhyankar     /* sort the masked values */
23122f480046SShri Abhyankar     ierr = PetscSortInt(nmask,masked);CHKERRQ(ierr);
23132f480046SShri Abhyankar 
23142f480046SShri Abhyankar     /* set "j" values into matrix */
23152f480046SShri Abhyankar     maskcount = 1;
23162f480046SShri Abhyankar     for (j=0; j<nmask; j++) {
23172f480046SShri Abhyankar       a->j[jcount++]  = masked[j];
23182f480046SShri Abhyankar       mask[masked[j]] = maskcount++;
23192f480046SShri Abhyankar     }
23202f480046SShri Abhyankar 
23212f480046SShri Abhyankar     /* set "a" values into matrix */
23222f480046SShri Abhyankar     ishift = bs2*a->i[i];
23232f480046SShri Abhyankar     for (j=0; j<bs; j++) {
23242f480046SShri Abhyankar       kmax = rowlengths[i*bs+j];
23252f480046SShri Abhyankar       for (k=0; k<kmax; k++) {
23262f480046SShri Abhyankar         tmp = jj[nzcountb]/bs;        /* block col. index */
23272f480046SShri Abhyankar         if (tmp >= i) {
23282f480046SShri Abhyankar           block     = mask[tmp] - 1;
23292f480046SShri Abhyankar           point     = jj[nzcountb] - bs*tmp;
23302f480046SShri Abhyankar           idx       = ishift + bs2*block + j + bs*point;
23312f480046SShri Abhyankar           a->a[idx] = aa[nzcountb];
23322f480046SShri Abhyankar         }
23332f480046SShri Abhyankar         nzcountb++;
23342f480046SShri Abhyankar       }
23352f480046SShri Abhyankar     }
23362f480046SShri Abhyankar     /* zero out the mask elements we set */
23372f480046SShri Abhyankar     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
23382f480046SShri Abhyankar   }
23392f480046SShri Abhyankar   if (jcount != a->nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Bad binary matrix");
23402f480046SShri Abhyankar 
23412f480046SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
23422f480046SShri Abhyankar   ierr = PetscFree(s_browlengths);CHKERRQ(ierr);
23432f480046SShri Abhyankar   ierr = PetscFree(aa);CHKERRQ(ierr);
23442f480046SShri Abhyankar   ierr = PetscFree(jj);CHKERRQ(ierr);
23452f480046SShri Abhyankar   ierr = PetscFree2(mask,masked);CHKERRQ(ierr);
23462f480046SShri Abhyankar 
23472f480046SShri Abhyankar   ierr = MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
23482f480046SShri Abhyankar   ierr = MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
23492f480046SShri Abhyankar   PetscFunctionReturn(0);
23502f480046SShri Abhyankar }
23512f480046SShri Abhyankar 
23522f480046SShri Abhyankar #undef __FUNCT__
2353c75a6043SHong Zhang #define __FUNCT__ "MatCreateSeqSBAIJWithArrays"
2354c75a6043SHong Zhang /*@
2355c75a6043SHong Zhang      MatCreateSeqSBAIJWithArrays - Creates an sequential SBAIJ matrix using matrix elements
2356c75a6043SHong Zhang               (upper triangular entries in CSR format) provided by the user.
2357c75a6043SHong Zhang 
2358c75a6043SHong Zhang      Collective on MPI_Comm
2359c75a6043SHong Zhang 
2360c75a6043SHong Zhang    Input Parameters:
2361c75a6043SHong Zhang +  comm - must be an MPI communicator of size 1
2362c75a6043SHong Zhang .  bs - size of block
2363c75a6043SHong Zhang .  m - number of rows
2364c75a6043SHong Zhang .  n - number of columns
2365c75a6043SHong Zhang .  i - row indices
2366c75a6043SHong Zhang .  j - column indices
2367c75a6043SHong Zhang -  a - matrix values
2368c75a6043SHong Zhang 
2369c75a6043SHong Zhang    Output Parameter:
2370c75a6043SHong Zhang .  mat - the matrix
2371c75a6043SHong Zhang 
2372dfb205c3SBarry Smith    Level: advanced
2373c75a6043SHong Zhang 
2374c75a6043SHong Zhang    Notes:
2375c75a6043SHong Zhang        The i, j, and a arrays are not copied by this routine, the user must free these arrays
2376c75a6043SHong Zhang     once the matrix is destroyed
2377c75a6043SHong Zhang 
2378c75a6043SHong Zhang        You cannot set new nonzero locations into this matrix, that will generate an error.
2379c75a6043SHong Zhang 
2380c75a6043SHong Zhang        The i and j indices are 0 based
2381c75a6043SHong Zhang 
2382dfb205c3SBarry 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
2383dfb205c3SBarry Smith        it is the regular CSR format excluding the lower triangular elements.
2384dfb205c3SBarry Smith 
238569b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSBAIJ(), MatCreateSeqSBAIJ()
2386c75a6043SHong Zhang 
2387c75a6043SHong Zhang @*/
23887087cfbeSBarry Smith PetscErrorCode  MatCreateSeqSBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
2389c75a6043SHong Zhang {
2390c75a6043SHong Zhang   PetscErrorCode ierr;
2391c75a6043SHong Zhang   PetscInt       ii;
2392c75a6043SHong Zhang   Mat_SeqSBAIJ   *sbaij;
2393c75a6043SHong Zhang 
2394c75a6043SHong Zhang   PetscFunctionBegin;
2395e32f2f54SBarry Smith   if (bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"block size %D > 1 is not supported yet",bs);
2396e32f2f54SBarry Smith   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
2397c75a6043SHong Zhang 
2398c75a6043SHong Zhang   ierr  = MatCreate(comm,mat);CHKERRQ(ierr);
2399c75a6043SHong Zhang   ierr  = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
2400c75a6043SHong Zhang   ierr  = MatSetType(*mat,MATSEQSBAIJ);CHKERRQ(ierr);
2401c75a6043SHong Zhang   ierr  = MatSeqSBAIJSetPreallocation_SeqSBAIJ(*mat,bs,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
2402c75a6043SHong Zhang   sbaij = (Mat_SeqSBAIJ*)(*mat)->data;
2403dcca6d9dSJed Brown   ierr  = PetscMalloc2(m,&sbaij->imax,m,&sbaij->ilen);CHKERRQ(ierr);
24043bb1ff40SBarry Smith   ierr  = PetscLogObjectMemory((PetscObject)*mat,2*m*sizeof(PetscInt));CHKERRQ(ierr);
2405c75a6043SHong Zhang 
2406c75a6043SHong Zhang   sbaij->i = i;
2407c75a6043SHong Zhang   sbaij->j = j;
2408c75a6043SHong Zhang   sbaij->a = a;
240926fbe8dcSKarl Rupp 
2410c75a6043SHong Zhang   sbaij->singlemalloc = PETSC_FALSE;
2411c75a6043SHong Zhang   sbaij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
2412e6b907acSBarry Smith   sbaij->free_a       = PETSC_FALSE;
2413e6b907acSBarry Smith   sbaij->free_ij      = PETSC_FALSE;
2414c75a6043SHong Zhang 
2415c75a6043SHong Zhang   for (ii=0; ii<m; ii++) {
2416c75a6043SHong Zhang     sbaij->ilen[ii] = sbaij->imax[ii] = i[ii+1] - i[ii];
2417c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2418e32f2f54SBarry 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]);
2419c75a6043SHong Zhang #endif
2420c75a6043SHong Zhang   }
2421c75a6043SHong Zhang #if defined(PETSC_USE_DEBUG)
2422c75a6043SHong Zhang   for (ii=0; ii<sbaij->i[m]; ii++) {
2423e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
2424e32f2f54SBarry 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]);
2425c75a6043SHong Zhang   }
2426c75a6043SHong Zhang #endif
2427c75a6043SHong Zhang 
2428c75a6043SHong Zhang   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2429c75a6043SHong Zhang   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2430c75a6043SHong Zhang   PetscFunctionReturn(0);
2431c75a6043SHong Zhang }
2432d06b337dSHong Zhang 
243359f5e6ceSHong Zhang #undef __FUNCT__
243459f5e6ceSHong Zhang #define __FUNCT__ "MatCreateMPIMatConcatenateSeqMat_SeqSBAIJ"
243559f5e6ceSHong Zhang PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqSBAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
243659f5e6ceSHong Zhang {
243759f5e6ceSHong Zhang   PetscErrorCode ierr;
243859f5e6ceSHong Zhang 
243959f5e6ceSHong Zhang   PetscFunctionBegin;
244059f5e6ceSHong Zhang   ierr = MatCreateMPIMatConcatenateSeqMat_MPISBAIJ(comm,inmat,n,scall,outmat);CHKERRQ(ierr);
244159f5e6ceSHong Zhang   PetscFunctionReturn(0);
244259f5e6ceSHong Zhang }
2443d06b337dSHong Zhang 
2444d06b337dSHong Zhang 
244549b5e25fSSatish Balay 
244649b5e25fSSatish Balay 
2447