xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 73a71a0f875e342bef2af9dbfc05b9368f8a1bc4)
1b377110cSBarry Smith 
2d5d45c9bSBarry Smith /*
33369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
4d5d45c9bSBarry Smith   matrix storage format.
5d5d45c9bSBarry Smith */
63369ce9aSBarry Smith 
77c4f633dSBarry Smith 
8c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/aij.h>          /*I "petscmat.h" I*/
9c6db04a5SJed Brown #include <petscblaslapack.h>
10c6db04a5SJed Brown #include <petscbt.h>
11d441b888SJed Brown #include <../src/mat/blocktranspose.h>
1278b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
1378b84d54SShri Abhyankar #include <petscthreadcomm.h>
1478b84d54SShri Abhyankar #endif
150716a85fSBarry Smith 
160716a85fSBarry Smith #undef __FUNCT__
170716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_SeqAIJ"
180716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms)
190716a85fSBarry Smith {
200716a85fSBarry Smith   PetscErrorCode ierr;
210716a85fSBarry Smith   PetscInt       i,m,n;
220716a85fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;
230716a85fSBarry Smith 
240716a85fSBarry Smith   PetscFunctionBegin;
250716a85fSBarry Smith   ierr = MatGetSize(A,&m,&n);CHKERRQ(ierr);
260716a85fSBarry Smith   ierr = PetscMemzero(norms,n*sizeof(PetscReal));CHKERRQ(ierr);
270716a85fSBarry Smith   if (type == NORM_2) {
280716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
290716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]);
300716a85fSBarry Smith     }
310716a85fSBarry Smith   } else if (type == NORM_1) {
320716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
330716a85fSBarry Smith       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]);
340716a85fSBarry Smith     }
350716a85fSBarry Smith   } else if (type == NORM_INFINITY) {
360716a85fSBarry Smith     for (i=0; i<aij->i[m]; i++) {
370716a85fSBarry Smith       norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]);
380716a85fSBarry Smith     }
390716a85fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType");
400716a85fSBarry Smith 
410716a85fSBarry Smith   if (type == NORM_2) {
428f1a2a5eSBarry Smith     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
430716a85fSBarry Smith   }
440716a85fSBarry Smith   PetscFunctionReturn(0);
450716a85fSBarry Smith }
460716a85fSBarry Smith 
474a2ae208SSatish Balay #undef __FUNCT__
48f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ_Private"
49f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat A,PetscInt *nrows,PetscInt **zrows)
506ce1633cSBarry Smith {
516ce1633cSBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
526ce1633cSBarry Smith   const MatScalar   *aa = a->a;
536ce1633cSBarry Smith   PetscInt          i,m=A->rmap->n,cnt = 0;
546ce1633cSBarry Smith   const PetscInt    *jj = a->j,*diag;
556ce1633cSBarry Smith   PetscInt          *rows;
566ce1633cSBarry Smith   PetscErrorCode    ierr;
576ce1633cSBarry Smith 
586ce1633cSBarry Smith   PetscFunctionBegin;
596ce1633cSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
606ce1633cSBarry Smith   diag = a->diag;
616ce1633cSBarry Smith   for (i=0; i<m; i++) {
626ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
636ce1633cSBarry Smith       cnt++;
646ce1633cSBarry Smith     }
656ce1633cSBarry Smith   }
666ce1633cSBarry Smith   ierr = PetscMalloc(cnt*sizeof(PetscInt),&rows);CHKERRQ(ierr);
676ce1633cSBarry Smith   cnt  = 0;
686ce1633cSBarry Smith   for (i=0; i<m; i++) {
696ce1633cSBarry Smith     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
706ce1633cSBarry Smith       rows[cnt++] = i;
716ce1633cSBarry Smith     }
726ce1633cSBarry Smith   }
73f1f41ecbSJed Brown   *nrows = cnt;
74f1f41ecbSJed Brown   *zrows = rows;
75f1f41ecbSJed Brown   PetscFunctionReturn(0);
76f1f41ecbSJed Brown }
77f1f41ecbSJed Brown 
78f1f41ecbSJed Brown #undef __FUNCT__
79f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_SeqAIJ"
80f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
81f1f41ecbSJed Brown {
82f1f41ecbSJed Brown   PetscInt       nrows,*rows;
83f1f41ecbSJed Brown   PetscErrorCode ierr;
84f1f41ecbSJed Brown 
85f1f41ecbSJed Brown   PetscFunctionBegin;
86f1f41ecbSJed Brown   *zrows = PETSC_NULL;
87f1f41ecbSJed Brown   ierr = MatFindZeroDiagonals_SeqAIJ_Private(A,&nrows,&rows);CHKERRQ(ierr);
88f1f41ecbSJed Brown   ierr = ISCreateGeneral(((PetscObject)A)->comm,nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr);
896ce1633cSBarry Smith   PetscFunctionReturn(0);
906ce1633cSBarry Smith }
916ce1633cSBarry Smith 
926ce1633cSBarry Smith #undef __FUNCT__
93b3a44c85SBarry Smith #define __FUNCT__ "MatFindNonzeroRows_SeqAIJ"
94b3a44c85SBarry Smith PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows)
95b3a44c85SBarry Smith {
96b3a44c85SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
97b3a44c85SBarry Smith   const MatScalar   *aa;
98b3a44c85SBarry Smith   PetscInt          m=A->rmap->n,cnt = 0;
99b3a44c85SBarry Smith   const PetscInt    *ii;
100b3a44c85SBarry Smith   PetscInt          n,i,j,*rows;
101b3a44c85SBarry Smith   PetscErrorCode    ierr;
102b3a44c85SBarry Smith 
103b3a44c85SBarry Smith   PetscFunctionBegin;
104b3a44c85SBarry Smith   *keptrows = 0;
105b3a44c85SBarry Smith   ii        = a->i;
106b3a44c85SBarry Smith   for (i=0; i<m; i++) {
107b3a44c85SBarry Smith     n   = ii[i+1] - ii[i];
108b3a44c85SBarry Smith     if (!n) {
109b3a44c85SBarry Smith       cnt++;
110b3a44c85SBarry Smith       goto ok1;
111b3a44c85SBarry Smith     }
112b3a44c85SBarry Smith     aa  = a->a + ii[i];
113b3a44c85SBarry Smith     for (j=0; j<n; j++) {
114b3a44c85SBarry Smith       if (aa[j] != 0.0) goto ok1;
115b3a44c85SBarry Smith     }
116b3a44c85SBarry Smith     cnt++;
117b3a44c85SBarry Smith     ok1:;
118b3a44c85SBarry Smith   }
119b3a44c85SBarry Smith   if (!cnt) PetscFunctionReturn(0);
120b3a44c85SBarry Smith   ierr = PetscMalloc((A->rmap->n-cnt)*sizeof(PetscInt),&rows);CHKERRQ(ierr);
121b3a44c85SBarry Smith   cnt  = 0;
122b3a44c85SBarry Smith   for (i=0; i<m; i++) {
123b3a44c85SBarry Smith     n   = ii[i+1] - ii[i];
124b3a44c85SBarry Smith     if (!n) continue;
125b3a44c85SBarry Smith     aa  = a->a + ii[i];
126b3a44c85SBarry Smith     for (j=0; j<n; j++) {
127b3a44c85SBarry Smith       if (aa[j] != 0.0) {
128b3a44c85SBarry Smith         rows[cnt++] = i;
129b3a44c85SBarry Smith         break;
130b3a44c85SBarry Smith       }
131b3a44c85SBarry Smith     }
132b3a44c85SBarry Smith   }
133b3a44c85SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr);
134b3a44c85SBarry Smith   PetscFunctionReturn(0);
135b3a44c85SBarry Smith }
136b3a44c85SBarry Smith 
137b3a44c85SBarry Smith #undef __FUNCT__
13879299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1397087cfbeSBarry Smith PetscErrorCode  MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
14079299369SBarry Smith {
14179299369SBarry Smith   PetscErrorCode ierr;
14279299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
143d0f46423SBarry Smith   PetscInt       i,*diag, m = Y->rmap->n;
14454f21887SBarry Smith   MatScalar      *aa = aij->a;
14554f21887SBarry Smith   PetscScalar    *v;
146ace3abfcSBarry Smith   PetscBool      missing;
14779299369SBarry Smith 
14879299369SBarry Smith   PetscFunctionBegin;
14909f38230SBarry Smith   if (Y->assembled) {
15009f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
15109f38230SBarry Smith     if (!missing) {
15279299369SBarry Smith       diag = aij->diag;
15379299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
15479299369SBarry Smith       if (is == INSERT_VALUES) {
15579299369SBarry Smith 	for (i=0; i<m; i++) {
15679299369SBarry Smith 	  aa[diag[i]] = v[i];
15779299369SBarry Smith 	}
15879299369SBarry Smith       } else {
15979299369SBarry Smith 	for (i=0; i<m; i++) {
16079299369SBarry Smith 	  aa[diag[i]] += v[i];
16179299369SBarry Smith 	}
16279299369SBarry Smith       }
16379299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
16479299369SBarry Smith       PetscFunctionReturn(0);
16579299369SBarry Smith     }
16686c113feSBarry Smith     aij->idiagvalid  = PETSC_FALSE;
16786c113feSBarry Smith     aij->ibdiagvalid = PETSC_FALSE;
16809f38230SBarry Smith   }
16909f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
17009f38230SBarry Smith   PetscFunctionReturn(0);
17109f38230SBarry Smith }
17279299369SBarry Smith 
17379299369SBarry Smith #undef __FUNCT__
1744a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
175ace3abfcSBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
17617ab2063SBarry Smith {
177416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
178dfbe8321SBarry Smith   PetscErrorCode ierr;
17997f1f81fSBarry Smith   PetscInt       i,ishift;
18017ab2063SBarry Smith 
1813a40ed3dSBarry Smith   PetscFunctionBegin;
182d0f46423SBarry Smith   *m     = A->rmap->n;
1833a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
184bfeeae90SHong Zhang   ishift = 0;
18553e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
186d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
187bfeeae90SHong Zhang   } else if (oshift == 1) {
188d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
1893b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
190d0f46423SBarry Smith     ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
191d0f46423SBarry Smith     for (i=0; i<A->rmap->n+1; i++) (*ia)[i] = a->i[i] + 1;
192ecc77c7aSBarry Smith     if (ja) {
19397f1f81fSBarry Smith       ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
1943b2fbd54SBarry Smith       for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
195ecc77c7aSBarry Smith     }
1966945ee14SBarry Smith   } else {
197ecc77c7aSBarry Smith     *ia = a->i;
198ecc77c7aSBarry Smith     if (ja) *ja = a->j;
199a2ce50c7SBarry Smith   }
2003a40ed3dSBarry Smith   PetscFunctionReturn(0);
201a2744918SBarry Smith }
202a2744918SBarry Smith 
2034a2ae208SSatish Balay #undef __FUNCT__
2044a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
205ace3abfcSBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
2066945ee14SBarry Smith {
207dfbe8321SBarry Smith   PetscErrorCode ierr;
2086945ee14SBarry Smith 
2093a40ed3dSBarry Smith   PetscFunctionBegin;
2103a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
211bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
212606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
213ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
214bcd2baecSBarry Smith   }
2153a40ed3dSBarry Smith   PetscFunctionReturn(0);
21617ab2063SBarry Smith }
21717ab2063SBarry Smith 
2184a2ae208SSatish Balay #undef __FUNCT__
2194a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
220ace3abfcSBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
2213b2fbd54SBarry Smith {
2223b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
223dfbe8321SBarry Smith   PetscErrorCode ierr;
224d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
22597f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
2263b2fbd54SBarry Smith 
2273a40ed3dSBarry Smith   PetscFunctionBegin;
228899cda47SBarry Smith   *nn = n;
2293a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2303b2fbd54SBarry Smith   if (symmetric) {
231d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
2323b2fbd54SBarry Smith   } else {
23397f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
23497f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
23597f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
23697f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
2373b2fbd54SBarry Smith     jj = a->j;
2383b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
239bfeeae90SHong Zhang       collengths[jj[i]]++;
2403b2fbd54SBarry Smith     }
2413b2fbd54SBarry Smith     cia[0] = oshift;
2423b2fbd54SBarry Smith     for (i=0; i<n; i++) {
2433b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
2443b2fbd54SBarry Smith     }
24597f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
2463b2fbd54SBarry Smith     jj   = a->j;
247a93ec695SBarry Smith     for (row=0; row<m; row++) {
248a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
249a93ec695SBarry Smith       for (i=0; i<mr; i++) {
250bfeeae90SHong Zhang         col = *jj++;
2513b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
2523b2fbd54SBarry Smith       }
2533b2fbd54SBarry Smith     }
254606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
2553b2fbd54SBarry Smith     *ia = cia; *ja = cja;
2563b2fbd54SBarry Smith   }
2573a40ed3dSBarry Smith   PetscFunctionReturn(0);
2583b2fbd54SBarry Smith }
2593b2fbd54SBarry Smith 
2604a2ae208SSatish Balay #undef __FUNCT__
2614a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
262ace3abfcSBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool  symmetric,PetscBool  inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscBool  *done)
2633b2fbd54SBarry Smith {
264dfbe8321SBarry Smith   PetscErrorCode ierr;
265606d414cSSatish Balay 
2663a40ed3dSBarry Smith   PetscFunctionBegin;
2673a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
2683b2fbd54SBarry Smith 
269606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
270606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
2713b2fbd54SBarry Smith 
2723a40ed3dSBarry Smith   PetscFunctionReturn(0);
2733b2fbd54SBarry Smith }
2743b2fbd54SBarry Smith 
27587d4246cSBarry Smith #undef __FUNCT__
27687d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
27787d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
27887d4246cSBarry Smith {
27987d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
28087d4246cSBarry Smith   PetscInt       *ai = a->i;
28187d4246cSBarry Smith   PetscErrorCode ierr;
28287d4246cSBarry Smith 
28387d4246cSBarry Smith   PetscFunctionBegin;
28487d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
28587d4246cSBarry Smith   PetscFunctionReturn(0);
28687d4246cSBarry Smith }
28787d4246cSBarry Smith 
2884a2ae208SSatish Balay #undef __FUNCT__
2894a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
29097f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
29117ab2063SBarry Smith {
292416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
293e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
29497f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
2956849ba73SBarry Smith   PetscErrorCode ierr;
296e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
29754f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
298ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
299ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
30017ab2063SBarry Smith 
3013a40ed3dSBarry Smith   PetscFunctionBegin;
30271fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
30317ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
304416022c9SBarry Smith     row  = im[k];
3055ef9f2a5SBarry Smith     if (row < 0) continue;
3062515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
307e32f2f54SBarry 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);
3083b2fbd54SBarry Smith #endif
309bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
31017ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
311416022c9SBarry Smith     low  = 0;
312c71e6ed7SBarry Smith     high = nrow;
31317ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
3145ef9f2a5SBarry Smith       if (in[l] < 0) continue;
3152515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
316e32f2f54SBarry 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);
3173b2fbd54SBarry Smith #endif
318bfeeae90SHong Zhang       col = in[l];
31916371a99SBarry Smith       if (v) {
3204b0e389bSBarry Smith 	if (roworiented) {
3215ef9f2a5SBarry Smith 	  value = v[l + k*n];
322bef8e0ddSBarry Smith 	} else {
3234b0e389bSBarry Smith 	  value = v[k + l*m];
3244b0e389bSBarry Smith 	}
32516371a99SBarry Smith       } else {
32675567043SBarry Smith         value = 0.;
32716371a99SBarry Smith       }
328abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
32936db0b34SBarry Smith 
3307cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
331e2ee6c50SBarry Smith       lastcol = col;
332416022c9SBarry Smith       while (high-low > 5) {
333416022c9SBarry Smith         t = (low+high)/2;
334416022c9SBarry Smith         if (rp[t] > col) high = t;
335416022c9SBarry Smith         else             low  = t;
33617ab2063SBarry Smith       }
337416022c9SBarry Smith       for (i=low; i<high; i++) {
33817ab2063SBarry Smith         if (rp[i] > col) break;
33917ab2063SBarry Smith         if (rp[i] == col) {
340416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
34117ab2063SBarry Smith           else                  ap[i] = value;
342e44c0bd4SBarry Smith           low = i + 1;
34317ab2063SBarry Smith           goto noinsert;
34417ab2063SBarry Smith         }
34517ab2063SBarry Smith       }
346abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
347c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
348e32f2f54SBarry Smith       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
349fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
350c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
351416022c9SBarry Smith       /* shift up all the later entries in this row */
352416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
35317ab2063SBarry Smith         rp[ii+1] = rp[ii];
35417ab2063SBarry Smith         ap[ii+1] = ap[ii];
35517ab2063SBarry Smith       }
35617ab2063SBarry Smith       rp[i] = col;
35717ab2063SBarry Smith       ap[i] = value;
358416022c9SBarry Smith       low   = i + 1;
359e44c0bd4SBarry Smith       noinsert:;
36017ab2063SBarry Smith     }
36117ab2063SBarry Smith     ailen[row] = nrow;
36217ab2063SBarry Smith   }
36388e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
3643a40ed3dSBarry Smith   PetscFunctionReturn(0);
36517ab2063SBarry Smith }
36617ab2063SBarry Smith 
36781824310SBarry Smith 
3684a2ae208SSatish Balay #undef __FUNCT__
3694a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
370a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
3717eb43aa7SLois Curfman McInnes {
3727eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
37397f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
37497f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
37554f21887SBarry Smith   MatScalar    *ap,*aa = a->a;
3767eb43aa7SLois Curfman McInnes 
3773a40ed3dSBarry Smith   PetscFunctionBegin;
3787eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
3797eb43aa7SLois Curfman McInnes     row  = im[k];
380e32f2f54SBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
381e32f2f54SBarry 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);
382bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
3837eb43aa7SLois Curfman McInnes     nrow = ailen[row];
3847eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
385e32f2f54SBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
386e32f2f54SBarry 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);
387bfeeae90SHong Zhang       col = in[l] ;
3887eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
3897eb43aa7SLois Curfman McInnes       while (high-low > 5) {
3907eb43aa7SLois Curfman McInnes         t = (low+high)/2;
3917eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
3927eb43aa7SLois Curfman McInnes         else             low  = t;
3937eb43aa7SLois Curfman McInnes       }
3947eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
3957eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
3967eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
397b49de8d1SLois Curfman McInnes           *v++ = ap[i];
3987eb43aa7SLois Curfman McInnes           goto finished;
3997eb43aa7SLois Curfman McInnes         }
4007eb43aa7SLois Curfman McInnes       }
40197e567efSBarry Smith       *v++ = 0.0;
4027eb43aa7SLois Curfman McInnes       finished:;
4037eb43aa7SLois Curfman McInnes     }
4047eb43aa7SLois Curfman McInnes   }
4053a40ed3dSBarry Smith   PetscFunctionReturn(0);
4067eb43aa7SLois Curfman McInnes }
4077eb43aa7SLois Curfman McInnes 
40817ab2063SBarry Smith 
4094a2ae208SSatish Balay #undef __FUNCT__
4104a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
411dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
41217ab2063SBarry Smith {
413416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
4146849ba73SBarry Smith   PetscErrorCode ierr;
4156f69ff64SBarry Smith   PetscInt       i,*col_lens;
4166f69ff64SBarry Smith   int            fd;
417b37d52dbSMark F. Adams   FILE           *file;
41817ab2063SBarry Smith 
4193a40ed3dSBarry Smith   PetscFunctionBegin;
420b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
421d0f46423SBarry Smith   ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
4220700a824SBarry Smith   col_lens[0] = MAT_FILE_CLASSID;
423d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
424d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
425416022c9SBarry Smith   col_lens[3] = a->nz;
426416022c9SBarry Smith 
427416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
428d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
429416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
43017ab2063SBarry Smith   }
431d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
432606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
433416022c9SBarry Smith 
434416022c9SBarry Smith   /* store column indices (zero start index) */
4356f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
436416022c9SBarry Smith 
437416022c9SBarry Smith   /* store nonzero values */
4386f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
439b37d52dbSMark F. Adams 
440b37d52dbSMark F. Adams   ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr);
441b37d52dbSMark F. Adams   if (file) {
442b37d52dbSMark F. Adams     fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
443b37d52dbSMark F. Adams   }
444b37d52dbSMark F. Adams 
4453a40ed3dSBarry Smith   PetscFunctionReturn(0);
44617ab2063SBarry Smith }
447416022c9SBarry Smith 
44809573ac7SBarry Smith extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
449cd155464SBarry Smith 
4504a2ae208SSatish Balay #undef __FUNCT__
4514a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
452dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
453416022c9SBarry Smith {
454416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
455dfbe8321SBarry Smith   PetscErrorCode    ierr;
456d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,shift=0;
457e060cb09SBarry Smith   const char        *name;
458f3ef73ceSBarry Smith   PetscViewerFormat format;
45917ab2063SBarry Smith 
4603a40ed3dSBarry Smith   PetscFunctionBegin;
461b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
46271c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
46397f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
464d0f46423SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) {
465d00d2cf4SBarry Smith       nofinalvalue = 1;
466d00d2cf4SBarry Smith     }
467d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
468d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
46977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
47077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
471b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
47217ab2063SBarry Smith 
47317ab2063SBarry Smith     for (i=0; i<m; i++) {
474416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
475aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
47677431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e + %18.16ei \n",i+1,a->j[j]+!shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
47717ab2063SBarry Smith #else
47877431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
47917ab2063SBarry Smith #endif
48017ab2063SBarry Smith       }
48117ab2063SBarry Smith     }
482d00d2cf4SBarry Smith     if (nofinalvalue) {
483d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
484d00d2cf4SBarry Smith     }
485317d6ea6SBarry Smith     ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
486fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
487d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
48868369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
489cd155464SBarry Smith      PetscFunctionReturn(0);
490fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
491d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
4927566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
49344cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
49477431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
49544cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
496aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
49736db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
498ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
49936db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
500ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
50136db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
502ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
5036831982aSBarry Smith         }
50444cd7ae7SLois Curfman McInnes #else
505ba0e910bSBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,(double)a->a[j]);CHKERRQ(ierr);}
50644cd7ae7SLois Curfman McInnes #endif
50744cd7ae7SLois Curfman McInnes       }
508b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
50944cd7ae7SLois Curfman McInnes     }
510d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
511fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
51297f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
513d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5147566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
51597f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
516496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
517496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
518496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
519496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
520aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
52136db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
522496be53dSLois Curfman McInnes #else
523496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
524496be53dSLois Curfman McInnes #endif
525496be53dSLois Curfman McInnes         }
526496be53dSLois Curfman McInnes       }
527496be53dSLois Curfman McInnes     }
5282e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
52977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
5302e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
53177431f27SBarry Smith       if (i+4<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4],sptr[i+5]);CHKERRQ(ierr);}
53277431f27SBarry Smith       else if (i+3<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);CHKERRQ(ierr);}
53377431f27SBarry Smith       else if (i+2<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);}
53477431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
53577431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
53677431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
537496be53dSLois Curfman McInnes     }
538b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
539606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
540496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
541496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
54277431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
543496be53dSLois Curfman McInnes       }
544b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
545496be53dSLois Curfman McInnes     }
546b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
547496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
548496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
549496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
550aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
55136db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
552b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
5536831982aSBarry Smith           }
554496be53dSLois Curfman McInnes #else
555b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
556496be53dSLois Curfman McInnes #endif
557496be53dSLois Curfman McInnes         }
558496be53dSLois Curfman McInnes       }
559b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
560496be53dSLois Curfman McInnes     }
561d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
562fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
56397f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
56487828ca2SBarry Smith     PetscScalar value;
56502594712SBarry Smith 
566d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5677566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
56802594712SBarry Smith     for (i=0; i<m; i++) {
56902594712SBarry Smith       jcnt = 0;
570d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
571e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
57202594712SBarry Smith           value = a->a[cnt++];
573e24b481bSBarry Smith           jcnt++;
57402594712SBarry Smith         } else {
57502594712SBarry Smith           value = 0.0;
57602594712SBarry Smith         }
577aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
578b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
57902594712SBarry Smith #else
580b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
58102594712SBarry Smith #endif
58202594712SBarry Smith       }
583b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
58402594712SBarry Smith     }
585d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
5863c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
587d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
5887566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
5893c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
5903c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
5913c215bfdSMatthew Knepley #else
5923c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
5933c215bfdSMatthew Knepley #endif
594d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
5953c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
5963c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
5973c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
5983c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
599ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %g %g\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
6003c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
601ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %g -%g\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
6023c215bfdSMatthew Knepley         } else {
603ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %g\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
6043c215bfdSMatthew Knepley         }
6053c215bfdSMatthew Knepley #else
606ba0e910bSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+shift, a->j[j]+shift, (double)a->a[j]);CHKERRQ(ierr);
6073c215bfdSMatthew Knepley #endif
6083c215bfdSMatthew Knepley       }
6093c215bfdSMatthew Knepley     }
610d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
6113a40ed3dSBarry Smith   } else {
612d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
6137566de4bSShri Abhyankar     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)A,viewer,"Matrix Object");CHKERRQ(ierr);
614d5f3da31SBarry Smith     if (A->factortype){
61516cd7e1dSShri Abhyankar       for (i=0; i<m; i++) {
61616cd7e1dSShri Abhyankar         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
61716cd7e1dSShri Abhyankar         /* L part */
61816cd7e1dSShri Abhyankar 	for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
61916cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
62016cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
621ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
62216cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
623ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
62416cd7e1dSShri Abhyankar           } else {
625ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
62616cd7e1dSShri Abhyankar           }
62716cd7e1dSShri Abhyankar #else
628ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,(double)a->a[j]);CHKERRQ(ierr);
62916cd7e1dSShri Abhyankar #endif
63016cd7e1dSShri Abhyankar         }
63116cd7e1dSShri Abhyankar 	/* diagonal */
63216cd7e1dSShri Abhyankar 	j = a->diag[i];
63316cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
63416cd7e1dSShri Abhyankar         if (PetscImaginaryPart(a->a[j]) > 0.0) {
635ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(1.0/a->a[j]),PetscImaginaryPart(1.0/a->a[j]));CHKERRQ(ierr);
63616cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
637ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(1.0/a->a[j]),-PetscImaginaryPart(1.0/a->a[j]));CHKERRQ(ierr);
63816cd7e1dSShri Abhyankar           } else {
639ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(1.0/a->a[j]));CHKERRQ(ierr);
64016cd7e1dSShri Abhyankar           }
64116cd7e1dSShri Abhyankar #else
642ba0e910bSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,(double)1.0/a->a[j]);CHKERRQ(ierr);
64316cd7e1dSShri Abhyankar #endif
64416cd7e1dSShri Abhyankar 
64516cd7e1dSShri Abhyankar 	/* U part */
64616cd7e1dSShri Abhyankar 	for (j=a->diag[i+1]+1+shift; j<a->diag[i]+shift; j++) {
64716cd7e1dSShri Abhyankar #if defined(PETSC_USE_COMPLEX)
64816cd7e1dSShri Abhyankar           if (PetscImaginaryPart(a->a[j]) > 0.0) {
649ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
65016cd7e1dSShri Abhyankar           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
651ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
65216cd7e1dSShri Abhyankar           } else {
653ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
65416cd7e1dSShri Abhyankar           }
65516cd7e1dSShri Abhyankar #else
656ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,(double)a->a[j]);CHKERRQ(ierr);
65716cd7e1dSShri Abhyankar #endif
65816cd7e1dSShri Abhyankar }
65916cd7e1dSShri Abhyankar 	  ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
66016cd7e1dSShri Abhyankar         }
66116cd7e1dSShri Abhyankar     } else {
66217ab2063SBarry Smith       for (i=0; i<m; i++) {
66377431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
664416022c9SBarry Smith         for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
665aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
66636db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) > 0.0) {
667ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
66836db0b34SBarry Smith           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
669ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
6703a40ed3dSBarry Smith           } else {
671ba0e910bSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
67217ab2063SBarry Smith           }
67317ab2063SBarry Smith #else
674ba0e910bSBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j]+shift,(double)a->a[j]);CHKERRQ(ierr);
67517ab2063SBarry Smith #endif
67617ab2063SBarry Smith         }
677b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
67817ab2063SBarry Smith       }
67916cd7e1dSShri Abhyankar     }
680d00279f6SBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
68117ab2063SBarry Smith   }
682b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6833a40ed3dSBarry Smith   PetscFunctionReturn(0);
684416022c9SBarry Smith }
685416022c9SBarry Smith 
6864a2ae208SSatish Balay #undef __FUNCT__
6874a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
688dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
689416022c9SBarry Smith {
690480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
691416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
692dfbe8321SBarry Smith   PetscErrorCode    ierr;
693d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
69436db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
695b0a32e0cSBarry Smith   PetscViewer       viewer;
696f3ef73ceSBarry Smith   PetscViewerFormat format;
697cddf8d76SBarry Smith 
6983a40ed3dSBarry Smith   PetscFunctionBegin;
699480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
700b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
70119bcc07fSBarry Smith 
702b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
703416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
7040513a670SBarry Smith 
705fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
7060513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
707b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
708416022c9SBarry Smith     for (i=0; i<m; i++) {
709cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
710bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
711bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
712aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
71336db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
714cddf8d76SBarry Smith #else
715cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
716cddf8d76SBarry Smith #endif
717b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
718cddf8d76SBarry Smith       }
719cddf8d76SBarry Smith     }
720b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
721cddf8d76SBarry Smith     for (i=0; i<m; i++) {
722cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
723bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
724bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
725cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
726b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
727cddf8d76SBarry Smith       }
728cddf8d76SBarry Smith     }
729b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
730cddf8d76SBarry Smith     for (i=0; i<m; i++) {
731cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
732bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
733bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
734aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
73536db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
736cddf8d76SBarry Smith #else
737cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
738cddf8d76SBarry Smith #endif
739b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
740416022c9SBarry Smith       }
741416022c9SBarry Smith     }
7420513a670SBarry Smith   } else {
7430513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
7440513a670SBarry Smith     /* first determine max of all nonzero values */
74597f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
746b0a32e0cSBarry Smith     PetscDraw   popup;
74736db0b34SBarry Smith     PetscReal scale;
7480513a670SBarry Smith 
7490513a670SBarry Smith     for (i=0; i<nz; i++) {
7500513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
7510513a670SBarry Smith     }
752b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
753b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
754b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
7550513a670SBarry Smith     count = 0;
7560513a670SBarry Smith     for (i=0; i<m; i++) {
7570513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
758bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
759bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
76097f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
761b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
7620513a670SBarry Smith         count++;
7630513a670SBarry Smith       }
7640513a670SBarry Smith     }
7650513a670SBarry Smith   }
766480ef9eaSBarry Smith   PetscFunctionReturn(0);
767480ef9eaSBarry Smith }
768cddf8d76SBarry Smith 
7694a2ae208SSatish Balay #undef __FUNCT__
7704a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
771dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
772480ef9eaSBarry Smith {
773dfbe8321SBarry Smith   PetscErrorCode ierr;
774b0a32e0cSBarry Smith   PetscDraw      draw;
77536db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
776ace3abfcSBarry Smith   PetscBool      isnull;
777480ef9eaSBarry Smith 
778480ef9eaSBarry Smith   PetscFunctionBegin;
779b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
780b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
781480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
782480ef9eaSBarry Smith 
783480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
784d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
785480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
786b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
787b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
788480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
7893a40ed3dSBarry Smith   PetscFunctionReturn(0);
790416022c9SBarry Smith }
791416022c9SBarry Smith 
7924a2ae208SSatish Balay #undef __FUNCT__
7934a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
794dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
795416022c9SBarry Smith {
796dfbe8321SBarry Smith   PetscErrorCode ierr;
797ace3abfcSBarry Smith   PetscBool      iascii,isbinary,isdraw;
798416022c9SBarry Smith 
7993a40ed3dSBarry Smith   PetscFunctionBegin;
800251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
801251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
802251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
803c45a1595SBarry Smith   if (iascii) {
8043a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
8050f5bd95cSBarry Smith   } else if (isbinary) {
8063a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
8070f5bd95cSBarry Smith   } else if (isdraw) {
8083a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
809913ac41fSBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
8104108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
8113a40ed3dSBarry Smith   PetscFunctionReturn(0);
81217ab2063SBarry Smith }
81319bcc07fSBarry Smith 
8144a2ae208SSatish Balay #undef __FUNCT__
8154a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
816dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
81717ab2063SBarry Smith {
818416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8196849ba73SBarry Smith   PetscErrorCode ierr;
82097f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
821d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
82254f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
8233447b6efSHong Zhang   PetscReal      ratio=0.6;
82417ab2063SBarry Smith 
8253a40ed3dSBarry Smith   PetscFunctionBegin;
8263a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
82717ab2063SBarry Smith 
82843ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
82917ab2063SBarry Smith   for (i=1; i<m; i++) {
830416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
83117ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
83294a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
83317ab2063SBarry Smith     if (fshift) {
834bfeeae90SHong Zhang       ip = aj + ai[i] ;
835bfeeae90SHong Zhang       ap = aa + ai[i] ;
83617ab2063SBarry Smith       N  = ailen[i];
83717ab2063SBarry Smith       for (j=0; j<N; j++) {
83817ab2063SBarry Smith         ip[j-fshift] = ip[j];
83917ab2063SBarry Smith         ap[j-fshift] = ap[j];
84017ab2063SBarry Smith       }
84117ab2063SBarry Smith     }
84217ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
84317ab2063SBarry Smith   }
84417ab2063SBarry Smith   if (m) {
84517ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
84617ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
84717ab2063SBarry Smith   }
84817ab2063SBarry Smith   /* reset ilen and imax for each row */
84917ab2063SBarry Smith   for (i=0; i<m; i++) {
85017ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
85117ab2063SBarry Smith   }
852bfeeae90SHong Zhang   a->nz = ai[m];
85365e19b50SBarry Smith   if (fshift && a->nounused == -1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift);
85417ab2063SBarry Smith 
85509f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
856d0f46423SBarry Smith   ierr = PetscInfo4(A,"Matrix size: %D X %D; storage space: %D unneeded,%D used\n",m,A->cmap->n,fshift,a->nz);CHKERRQ(ierr);
857ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
858ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
8598e58a170SBarry Smith   A->info.mallocs     += a->reallocs;
860dd5f02e7SSatish Balay   a->reallocs          = 0;
8614e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
86236db0b34SBarry Smith   a->rmax              = rmax;
8634e220ebcSLois Curfman McInnes 
864cd6b891eSBarry Smith   ierr = MatCheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
86588e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
86671c2f376SKris Buschelman 
8674108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
86871f1c65dSBarry Smith 
86971f1c65dSBarry Smith   a->idiagvalid  = PETSC_FALSE;
870bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_FALSE;
8713a40ed3dSBarry Smith   PetscFunctionReturn(0);
87217ab2063SBarry Smith }
87317ab2063SBarry Smith 
8744a2ae208SSatish Balay #undef __FUNCT__
87599cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
87699cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
87799cafbc1SBarry Smith {
87899cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
87999cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
88054f21887SBarry Smith   MatScalar      *aa = a->a;
88199cafbc1SBarry Smith 
88299cafbc1SBarry Smith   PetscFunctionBegin;
88399cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
88486c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
88586c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
88699cafbc1SBarry Smith   PetscFunctionReturn(0);
88799cafbc1SBarry Smith }
88899cafbc1SBarry Smith 
88999cafbc1SBarry Smith #undef __FUNCT__
89099cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
89199cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
89299cafbc1SBarry Smith {
89399cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
89499cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
89554f21887SBarry Smith   MatScalar      *aa = a->a;
89699cafbc1SBarry Smith 
89799cafbc1SBarry Smith   PetscFunctionBegin;
89899cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
89986c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
90086c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
90199cafbc1SBarry Smith   PetscFunctionReturn(0);
90299cafbc1SBarry Smith }
90399cafbc1SBarry Smith 
90478b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
90578b84d54SShri Abhyankar PetscErrorCode MatZeroEntries_SeqAIJ_Kernel(PetscInt thread_id,Mat A)
90678b84d54SShri Abhyankar {
90778b84d54SShri Abhyankar   PetscErrorCode ierr;
90878b84d54SShri Abhyankar   PetscInt       *trstarts=A->rmap->trstarts;
90978b84d54SShri Abhyankar   PetscInt       n,start,end;
91078b84d54SShri Abhyankar   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
91178b84d54SShri Abhyankar 
91278b84d54SShri Abhyankar   start = trstarts[thread_id];
91378b84d54SShri Abhyankar   end   = trstarts[thread_id+1];
91478b84d54SShri Abhyankar   n     = end - start;
91578b84d54SShri Abhyankar   ierr = PetscMemzero(a->a+start,n*sizeof(PetscScalar));CHKERRQ(ierr);
91678b84d54SShri Abhyankar   return 0;
91778b84d54SShri Abhyankar }
91878b84d54SShri Abhyankar 
91978b84d54SShri Abhyankar #undef __FUNCT__
92078b84d54SShri Abhyankar #define __FUNCT__ "MatZeroEntries_SeqAIJ"
92178b84d54SShri Abhyankar PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
92278b84d54SShri Abhyankar {
92378b84d54SShri Abhyankar   PetscErrorCode ierr;
92478b84d54SShri Abhyankar   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
92578b84d54SShri Abhyankar 
92678b84d54SShri Abhyankar   PetscFunctionBegin;
92778b84d54SShri Abhyankar   ierr = PetscThreadCommRunKernel(((PetscObject)A)->comm,(PetscThreadKernel)MatZeroEntries_SeqAIJ_Kernel,1,A);CHKERRQ(ierr);
92878b84d54SShri Abhyankar   a->idiagvalid = PETSC_FALSE;
92978b84d54SShri Abhyankar   a->ibdiagvalid = PETSC_FALSE;
93078b84d54SShri Abhyankar   PetscFunctionReturn(0);
93178b84d54SShri Abhyankar }
93278b84d54SShri Abhyankar #else
93399cafbc1SBarry Smith #undef __FUNCT__
9344a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
935dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
93617ab2063SBarry Smith {
937416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
938dfbe8321SBarry Smith   PetscErrorCode ierr;
9393a40ed3dSBarry Smith 
9403a40ed3dSBarry Smith   PetscFunctionBegin;
941d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
94286c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
94386c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
9443a40ed3dSBarry Smith   PetscFunctionReturn(0);
94517ab2063SBarry Smith }
94678b84d54SShri Abhyankar #endif
947416022c9SBarry Smith 
9484a2ae208SSatish Balay #undef __FUNCT__
9494a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
950dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
95117ab2063SBarry Smith {
952416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
953dfbe8321SBarry Smith   PetscErrorCode ierr;
954d5d45c9bSBarry Smith 
9553a40ed3dSBarry Smith   PetscFunctionBegin;
956aa482453SBarry Smith #if defined(PETSC_USE_LOG)
957d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
95817ab2063SBarry Smith #endif
959e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
9606bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
9616bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
96205b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
963d48dcb14SBarry Smith   ierr = PetscFree(a->ibdiag);CHKERRQ(ierr);
96405b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
96571f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
96605b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
9676bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
96805b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
9696bf464f9SBarry Smith   ierr = ISColoringDestroy(&a->coloring);CHKERRQ(ierr);
97005b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
9716bf464f9SBarry Smith   ierr = MatDestroy(&a->XtoY);CHKERRQ(ierr);
972cd6b891eSBarry Smith   ierr = PetscFree2(a->compressedrow.i,a->compressedrow.rindex);CHKERRQ(ierr);
9730b7e3e3dSHong Zhang   ierr = PetscFree(a->matmult_abdense);CHKERRQ(ierr);
974a30b2313SHong Zhang 
9754108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
976bf0cc555SLisandro Dalcin   ierr = PetscFree(A->data);CHKERRQ(ierr);
977901853e0SKris Buschelman 
978dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
979901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
980901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
981901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
982901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
983901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
9845a11e1b2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqaijperm_C","",PETSC_NULL);CHKERRQ(ierr);
985901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
986901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
987a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
988901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
9893a40ed3dSBarry Smith   PetscFunctionReturn(0);
99017ab2063SBarry Smith }
99117ab2063SBarry Smith 
9924a2ae208SSatish Balay #undef __FUNCT__
9934a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
994ace3abfcSBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool  flg)
99517ab2063SBarry Smith {
996416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
9974846f1f5SKris Buschelman   PetscErrorCode ierr;
9983a40ed3dSBarry Smith 
9993a40ed3dSBarry Smith   PetscFunctionBegin;
1000a65d3064SKris Buschelman   switch (op) {
1001a65d3064SKris Buschelman   case MAT_ROW_ORIENTED:
10024e0d8c25SBarry Smith     a->roworiented       = flg;
1003a65d3064SKris Buschelman     break;
1004a9817697SBarry Smith   case MAT_KEEP_NONZERO_PATTERN:
1005a9817697SBarry Smith     a->keepnonzeropattern    = flg;
1006a65d3064SKris Buschelman     break;
1007512a5fc5SBarry Smith   case MAT_NEW_NONZERO_LOCATIONS:
1008512a5fc5SBarry Smith     a->nonew             = (flg ? 0 : 1);
1009a65d3064SKris Buschelman     break;
1010a65d3064SKris Buschelman   case MAT_NEW_NONZERO_LOCATION_ERR:
10114e0d8c25SBarry Smith     a->nonew             = (flg ? -1 : 0);
1012a65d3064SKris Buschelman     break;
1013a65d3064SKris Buschelman   case MAT_NEW_NONZERO_ALLOCATION_ERR:
10144e0d8c25SBarry Smith     a->nonew             = (flg ? -2 : 0);
1015a65d3064SKris Buschelman     break;
101628b2fa4aSMatthew Knepley   case MAT_UNUSED_NONZERO_LOCATION_ERR:
101728b2fa4aSMatthew Knepley     a->nounused          = (flg ? -1 : 0);
101828b2fa4aSMatthew Knepley     break;
1019a65d3064SKris Buschelman   case MAT_IGNORE_ZERO_ENTRIES:
10204e0d8c25SBarry Smith     a->ignorezeroentries = flg;
10210df259c2SBarry Smith     break;
1022cd6b891eSBarry Smith   case MAT_CHECK_COMPRESSED_ROW:
1023cd6b891eSBarry Smith     a->compressedrow.check = flg;
1024d487561eSHong Zhang     break;
10253d472b54SHong Zhang   case MAT_SPD:
10263d472b54SHong Zhang     A->spd_set                         = PETSC_TRUE;
10273d472b54SHong Zhang     A->spd                             = flg;
10283d472b54SHong Zhang     if (flg) {
10293d472b54SHong Zhang       A->symmetric                     = PETSC_TRUE;
10303d472b54SHong Zhang       A->structurally_symmetric        = PETSC_TRUE;
10313d472b54SHong Zhang       A->symmetric_set                 = PETSC_TRUE;
10323d472b54SHong Zhang       A->structurally_symmetric_set    = PETSC_TRUE;
10333d472b54SHong Zhang     }
10343d472b54SHong Zhang     break;
1035b1646e73SJed Brown   case MAT_SYMMETRIC:
1036b1646e73SJed Brown   case MAT_STRUCTURALLY_SYMMETRIC:
1037b1646e73SJed Brown   case MAT_HERMITIAN:
1038b1646e73SJed Brown   case MAT_SYMMETRY_ETERNAL:
10394e0d8c25SBarry Smith   case MAT_NEW_DIAGONALS:
1040a65d3064SKris Buschelman   case MAT_IGNORE_OFF_PROC_ENTRIES:
1041a65d3064SKris Buschelman   case MAT_USE_HASH_TABLE:
1042290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
1043a65d3064SKris Buschelman     break;
1044b87ac2d8SJed Brown   case MAT_USE_INODES:
1045b87ac2d8SJed Brown     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1046b87ac2d8SJed Brown     break;
1047a65d3064SKris Buschelman   default:
1048e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1049a65d3064SKris Buschelman   }
10504108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
10513a40ed3dSBarry Smith   PetscFunctionReturn(0);
105217ab2063SBarry Smith }
105317ab2063SBarry Smith 
10544a2ae208SSatish Balay #undef __FUNCT__
10554a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
1056dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
105717ab2063SBarry Smith {
1058416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10596849ba73SBarry Smith   PetscErrorCode ierr;
1060d3e70bfaSHong Zhang   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
106135e7444dSHong Zhang   PetscScalar    *aa=a->a,*x,zero=0.0;
106217ab2063SBarry Smith 
10633a40ed3dSBarry Smith   PetscFunctionBegin;
1064d3e70bfaSHong Zhang   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
1065e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
106635e7444dSHong Zhang 
1067d5f3da31SBarry Smith   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU){
1068d3e70bfaSHong Zhang     PetscInt *diag=a->diag;
106935e7444dSHong Zhang     ierr = VecGetArray(v,&x);CHKERRQ(ierr);
10702c990fa1SHong Zhang     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
107135e7444dSHong Zhang     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
107235e7444dSHong Zhang     PetscFunctionReturn(0);
107335e7444dSHong Zhang   }
107435e7444dSHong Zhang 
10752dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
10761ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
107735e7444dSHong Zhang   for (i=0; i<n; i++) {
107835e7444dSHong Zhang     nz = ai[i+1] - ai[i];
10792f5a7c2eSBarry Smith     if (!nz) x[i] = 0.0;
108035e7444dSHong Zhang     for (j=ai[i]; j<ai[i+1]; j++){
108135e7444dSHong Zhang       if (aj[j] == i) {
108235e7444dSHong Zhang         x[i] = aa[j];
108317ab2063SBarry Smith         break;
108417ab2063SBarry Smith       }
108517ab2063SBarry Smith     }
108617ab2063SBarry Smith   }
10871ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
10883a40ed3dSBarry Smith   PetscFunctionReturn(0);
108917ab2063SBarry Smith }
109017ab2063SBarry Smith 
1091c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
10924a2ae208SSatish Balay #undef __FUNCT__
10934a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
1094dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
109517ab2063SBarry Smith {
1096416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
10975c897100SBarry Smith   PetscScalar       *x,*y;
1098dfbe8321SBarry Smith   PetscErrorCode    ierr;
1099d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
11005c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1101a77337e4SBarry Smith   MatScalar         *v;
1102a77337e4SBarry Smith   PetscScalar       alpha;
110304fbf559SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=PETSC_NULL;
11043447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
1105ace3abfcSBarry Smith   PetscBool         usecprow = cprow.use;
11065c897100SBarry Smith #endif
110717ab2063SBarry Smith 
11083a40ed3dSBarry Smith   PetscFunctionBegin;
11092e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
11101ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
11111ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
11125c897100SBarry Smith 
11135c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1114bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
11155c897100SBarry Smith #else
11163447b6efSHong Zhang   if (usecprow){
11173447b6efSHong Zhang     m    = cprow.nrows;
11183447b6efSHong Zhang     ii   = cprow.i;
11197b2bb3b9SHong Zhang     ridx = cprow.rindex;
11203447b6efSHong Zhang   } else {
11213447b6efSHong Zhang     ii = a->i;
11223447b6efSHong Zhang   }
112317ab2063SBarry Smith   for (i=0; i<m; i++) {
11243447b6efSHong Zhang     idx   = a->j + ii[i] ;
11253447b6efSHong Zhang     v     = a->a + ii[i] ;
11263447b6efSHong Zhang     n     = ii[i+1] - ii[i];
11273447b6efSHong Zhang     if (usecprow){
11287b2bb3b9SHong Zhang       alpha = x[ridx[i]];
11293447b6efSHong Zhang     } else {
113017ab2063SBarry Smith       alpha = x[i];
11313447b6efSHong Zhang     }
113204fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
113317ab2063SBarry Smith   }
11345c897100SBarry Smith #endif
1135dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
11361ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11371ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
11383a40ed3dSBarry Smith   PetscFunctionReturn(0);
113917ab2063SBarry Smith }
114017ab2063SBarry Smith 
11414a2ae208SSatish Balay #undef __FUNCT__
11425c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
1143dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
11445c897100SBarry Smith {
1145dfbe8321SBarry Smith   PetscErrorCode ierr;
11465c897100SBarry Smith 
11475c897100SBarry Smith   PetscFunctionBegin;
1148170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
11495c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
11505c897100SBarry Smith   PetscFunctionReturn(0);
11515c897100SBarry Smith }
11525c897100SBarry Smith 
1153c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
115478b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
115578b84d54SShri Abhyankar PetscErrorCode MatMult_SeqAIJ_Kernel(PetscInt thread_id,Mat A,Vec xx,Vec yy)
115678b84d54SShri Abhyankar {
115778b84d54SShri Abhyankar   PetscErrorCode    ierr;
115878b84d54SShri Abhyankar   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
115978b84d54SShri Abhyankar   PetscScalar       *y;
116078b84d54SShri Abhyankar   const PetscScalar *x;
116178b84d54SShri Abhyankar   const MatScalar   *aa;
116278b84d54SShri Abhyankar   PetscInt          *trstarts=A->rmap->trstarts;
116378b84d54SShri Abhyankar   PetscInt          n,start,end,i;
116478b84d54SShri Abhyankar   const PetscInt   *aj,*ai;
116578b84d54SShri Abhyankar   PetscScalar      sum;
116678b84d54SShri Abhyankar 
116778b84d54SShri Abhyankar   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
116878b84d54SShri Abhyankar   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
116978b84d54SShri Abhyankar   start = trstarts[thread_id];
117078b84d54SShri Abhyankar   end   = trstarts[thread_id+1];
117178b84d54SShri Abhyankar   aj    = a->j;
117278b84d54SShri Abhyankar   aa    = a->a;
117378b84d54SShri Abhyankar   ai    = a->i;
117478b84d54SShri Abhyankar   for(i=start;i<end;i++) {
117578b84d54SShri Abhyankar     n = ai[i+1] - ai[i];
117678b84d54SShri Abhyankar     aj = a->j + ai[i];
117778b84d54SShri Abhyankar     aa = a->a + ai[i];
117878b84d54SShri Abhyankar     sum = 0.0;
117978b84d54SShri Abhyankar     PetscSparseDensePlusDot(sum,x,aa,aj,n);
118078b84d54SShri Abhyankar     y[i] = sum;
118178b84d54SShri Abhyankar   }
118278b84d54SShri Abhyankar   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
118378b84d54SShri Abhyankar   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
118478b84d54SShri Abhyankar   return 0;
118578b84d54SShri Abhyankar }
118678b84d54SShri Abhyankar 
118778b84d54SShri Abhyankar #undef __FUNCT__
118878b84d54SShri Abhyankar #define __FUNCT__ "MatMult_SeqAIJ"
118978b84d54SShri Abhyankar PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
119078b84d54SShri Abhyankar {
119178b84d54SShri Abhyankar   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
119278b84d54SShri Abhyankar   PetscScalar       *y;
119378b84d54SShri Abhyankar   const PetscScalar *x;
119478b84d54SShri Abhyankar   const MatScalar   *aa;
119578b84d54SShri Abhyankar   PetscErrorCode    ierr;
119678b84d54SShri Abhyankar   PetscInt          m=A->rmap->n;
119778b84d54SShri Abhyankar   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
119878b84d54SShri Abhyankar   PetscInt          n,i,nonzerorow=0;
119978b84d54SShri Abhyankar   PetscScalar       sum;
120078b84d54SShri Abhyankar   PetscBool         usecprow=a->compressedrow.use;
120178b84d54SShri Abhyankar 
120278b84d54SShri Abhyankar #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
120378b84d54SShri Abhyankar #pragma disjoint(*x,*y,*aa)
120478b84d54SShri Abhyankar #endif
120578b84d54SShri Abhyankar 
120678b84d54SShri Abhyankar   PetscFunctionBegin;
120778b84d54SShri Abhyankar   aj  = a->j;
120878b84d54SShri Abhyankar   aa  = a->a;
120978b84d54SShri Abhyankar   ii  = a->i;
121078b84d54SShri Abhyankar   if (usecprow){ /* use compressed row format */
121178b84d54SShri Abhyankar     ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
121278b84d54SShri Abhyankar     ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
121378b84d54SShri Abhyankar     m    = a->compressedrow.nrows;
121478b84d54SShri Abhyankar     ii   = a->compressedrow.i;
121578b84d54SShri Abhyankar     ridx = a->compressedrow.rindex;
121678b84d54SShri Abhyankar     for (i=0; i<m; i++){
121778b84d54SShri Abhyankar       n   = ii[i+1] - ii[i];
121878b84d54SShri Abhyankar       aj  = a->j + ii[i];
121978b84d54SShri Abhyankar       aa  = a->a + ii[i];
122078b84d54SShri Abhyankar       sum = 0.0;
122178b84d54SShri Abhyankar       nonzerorow += (n>0);
122278b84d54SShri Abhyankar       PetscSparseDensePlusDot(sum,x,aa,aj,n);
122378b84d54SShri Abhyankar       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
122478b84d54SShri Abhyankar       y[*ridx++] = sum;
122578b84d54SShri Abhyankar     }
122678b84d54SShri Abhyankar     ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
122778b84d54SShri Abhyankar     ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
122878b84d54SShri Abhyankar   } else { /* do not use compressed row format */
122978b84d54SShri Abhyankar #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
123078b84d54SShri Abhyankar     fortranmultaij_(&m,x,ii,aj,aa,y);
123178b84d54SShri Abhyankar #else
123278b84d54SShri Abhyankar     ierr = PetscThreadCommRunKernel(((PetscObject)A)->comm,(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
123378b84d54SShri Abhyankar #endif
123478b84d54SShri Abhyankar   }
123578b84d54SShri Abhyankar   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
123678b84d54SShri Abhyankar   PetscFunctionReturn(0);
123778b84d54SShri Abhyankar }
123878b84d54SShri Abhyankar #else
12395c897100SBarry Smith #undef __FUNCT__
12404a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
1241dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
124217ab2063SBarry Smith {
1243416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1244d9fead3dSBarry Smith   PetscScalar       *y;
124554f21887SBarry Smith   const PetscScalar *x;
124654f21887SBarry Smith   const MatScalar   *aa;
1247dfbe8321SBarry Smith   PetscErrorCode    ierr;
1248003131ecSBarry Smith   PetscInt          m=A->rmap->n;
1249003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
12508aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
1251362ced78SSatish Balay   PetscScalar       sum;
1252ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
125317ab2063SBarry Smith 
1254b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
125597952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
1256fee21e36SBarry Smith #endif
1257fee21e36SBarry Smith 
12583a40ed3dSBarry Smith   PetscFunctionBegin;
12593649974fSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
12601ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
126197952fefSHong Zhang   aj  = a->j;
126297952fefSHong Zhang   aa  = a->a;
1263416022c9SBarry Smith   ii  = a->i;
12644eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
126597952fefSHong Zhang     m    = a->compressedrow.nrows;
126697952fefSHong Zhang     ii   = a->compressedrow.i;
126797952fefSHong Zhang     ridx = a->compressedrow.rindex;
126897952fefSHong Zhang     for (i=0; i<m; i++){
126997952fefSHong Zhang       n   = ii[i+1] - ii[i];
127097952fefSHong Zhang       aj  = a->j + ii[i];
127197952fefSHong Zhang       aa  = a->a + ii[i];
127297952fefSHong Zhang       sum = 0.0;
1273a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1274003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1275003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
127697952fefSHong Zhang       y[*ridx++] = sum;
127797952fefSHong Zhang     }
127897952fefSHong Zhang   } else { /* do not use compressed row format */
1279b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1280b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
1281b05257ddSBarry Smith #else
128278b84d54SShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
128378b84d54SShri Abhyankar     ierr = PetscThreadCommRunKernel(((PetscObject)A)->comm,(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);CHKERRQ(ierr);
128478b84d54SShri Abhyankar #else
128517ab2063SBarry Smith     for (i=0; i<m; i++) {
1286003131ecSBarry Smith       n   = ii[i+1] - ii[i];
1287003131ecSBarry Smith       aj  = a->j + ii[i];
1288003131ecSBarry Smith       aa  = a->a + ii[i];
128917ab2063SBarry Smith       sum  = 0.0;
1290a46b3154SVictor Eijkhout       nonzerorow += (n>0);
1291003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
129217ab2063SBarry Smith       y[i] = sum;
129317ab2063SBarry Smith     }
12948d195f9aSBarry Smith #endif
129578b84d54SShri Abhyankar #endif
1296b05257ddSBarry Smith   }
1297dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
12983649974fSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
12991ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
13003a40ed3dSBarry Smith   PetscFunctionReturn(0);
130117ab2063SBarry Smith }
130278b84d54SShri Abhyankar #endif
130317ab2063SBarry Smith 
1304c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
13054a2ae208SSatish Balay #undef __FUNCT__
13064a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
1307dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
130817ab2063SBarry Smith {
1309416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1310f15663dcSBarry Smith   PetscScalar       *y,*z;
1311f15663dcSBarry Smith   const PetscScalar *x;
131254f21887SBarry Smith   const MatScalar   *aa;
1313dfbe8321SBarry Smith   PetscErrorCode    ierr;
1314d0f46423SBarry Smith   PetscInt          m = A->rmap->n,*aj,*ii;
1315f15663dcSBarry Smith   PetscInt          n,i,*ridx=PETSC_NULL;
1316362ced78SSatish Balay   PetscScalar       sum;
1317ace3abfcSBarry Smith   PetscBool         usecprow=a->compressedrow.use;
13189ea0dfa2SSatish Balay 
13193a40ed3dSBarry Smith   PetscFunctionBegin;
1320f15663dcSBarry Smith   ierr = VecGetArrayRead(xx,&x);CHKERRQ(ierr);
13211ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
13222e8a6d31SBarry Smith   if (zz != yy) {
13231ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
13242e8a6d31SBarry Smith   } else {
13252e8a6d31SBarry Smith     z = y;
13262e8a6d31SBarry Smith   }
1327bfeeae90SHong Zhang 
132897952fefSHong Zhang   aj  = a->j;
132997952fefSHong Zhang   aa  = a->a;
1330cddf8d76SBarry Smith   ii  = a->i;
13314eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
13324eb6d288SHong Zhang     if (zz != yy){
13334eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
13344eb6d288SHong Zhang     }
133597952fefSHong Zhang     m    = a->compressedrow.nrows;
133697952fefSHong Zhang     ii   = a->compressedrow.i;
133797952fefSHong Zhang     ridx = a->compressedrow.rindex;
133897952fefSHong Zhang     for (i=0; i<m; i++){
133997952fefSHong Zhang       n  = ii[i+1] - ii[i];
134097952fefSHong Zhang       aj  = a->j + ii[i];
134197952fefSHong Zhang       aa  = a->a + ii[i];
134297952fefSHong Zhang       sum = y[*ridx];
1343f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
134497952fefSHong Zhang       z[*ridx++] = sum;
134597952fefSHong Zhang     }
134697952fefSHong Zhang   } else { /* do not use compressed row format */
1347f15663dcSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1348f15663dcSBarry Smith   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1349f15663dcSBarry Smith #else
135017ab2063SBarry Smith     for (i=0; i<m; i++) {
1351f15663dcSBarry Smith       n    = ii[i+1] - ii[i];
1352f15663dcSBarry Smith       aj  = a->j + ii[i];
1353f15663dcSBarry Smith       aa  = a->a + ii[i];
135417ab2063SBarry Smith       sum  = y[i];
1355f15663dcSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
135617ab2063SBarry Smith       z[i] = sum;
135717ab2063SBarry Smith     }
135802ab625aSSatish Balay #endif
1359f15663dcSBarry Smith   }
1360dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
1361f15663dcSBarry Smith   ierr = VecRestoreArrayRead(xx,&x);CHKERRQ(ierr);
13621ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
13632e8a6d31SBarry Smith   if (zz != yy) {
13641ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
13652e8a6d31SBarry Smith   }
13668154be41SBarry Smith #if defined(PETSC_HAVE_CUSP)
13676b375ea7SVictor Minden   /*
1368918e98c3SVictor Minden   ierr = VecView(xx,0);CHKERRQ(ierr);
1369918e98c3SVictor Minden   ierr = VecView(zz,0);CHKERRQ(ierr);
1370918e98c3SVictor Minden   ierr = MatView(A,0);CHKERRQ(ierr);
13716b375ea7SVictor Minden   */
1372918e98c3SVictor Minden #endif
13733a40ed3dSBarry Smith   PetscFunctionReturn(0);
137417ab2063SBarry Smith }
137517ab2063SBarry Smith 
137617ab2063SBarry Smith /*
137717ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
137817ab2063SBarry Smith */
13794a2ae208SSatish Balay #undef __FUNCT__
13804a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1381dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
138217ab2063SBarry Smith {
1383416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
13846849ba73SBarry Smith   PetscErrorCode ierr;
1385d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
138617ab2063SBarry Smith 
13873a40ed3dSBarry Smith   PetscFunctionBegin;
138809f38230SBarry Smith   if (!a->diag) {
138909f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
13909518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
139109f38230SBarry Smith   }
1392d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
139309f38230SBarry Smith     a->diag[i] = a->i[i+1];
1394bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1395bfeeae90SHong Zhang       if (a->j[j] == i) {
139609f38230SBarry Smith         a->diag[i] = j;
139717ab2063SBarry Smith         break;
139817ab2063SBarry Smith       }
139917ab2063SBarry Smith     }
140017ab2063SBarry Smith   }
14013a40ed3dSBarry Smith   PetscFunctionReturn(0);
140217ab2063SBarry Smith }
140317ab2063SBarry Smith 
1404be5855fcSBarry Smith /*
1405be5855fcSBarry Smith      Checks for missing diagonals
1406be5855fcSBarry Smith */
14074a2ae208SSatish Balay #undef __FUNCT__
14084a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
1409ace3abfcSBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1410be5855fcSBarry Smith {
1411be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
141297f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1413be5855fcSBarry Smith 
1414be5855fcSBarry Smith   PetscFunctionBegin;
141509f38230SBarry Smith   *missing = PETSC_FALSE;
1416d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
141709f38230SBarry Smith     *missing  = PETSC_TRUE;
141809f38230SBarry Smith     if (d) *d = 0;
1419358d2f5dSShri Abhyankar     PetscInfo(A,"Matrix has no entries therefore is missing diagonal");
142009f38230SBarry Smith   } else {
1421f1e2ffcdSBarry Smith     diag = a->diag;
1422d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1423bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
142409f38230SBarry Smith 	*missing = PETSC_TRUE;
142509f38230SBarry Smith 	if (d) *d = i;
142609f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
1427358d2f5dSShri Abhyankar 	break;
142809f38230SBarry Smith       }
1429be5855fcSBarry Smith     }
1430be5855fcSBarry Smith   }
1431be5855fcSBarry Smith   PetscFunctionReturn(0);
1432be5855fcSBarry Smith }
1433be5855fcSBarry Smith 
143471f1c65dSBarry Smith EXTERN_C_BEGIN
143571f1c65dSBarry Smith #undef __FUNCT__
143671f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
14377087cfbeSBarry Smith PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
143871f1c65dSBarry Smith {
143971f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
144071f1c65dSBarry Smith   PetscErrorCode ierr;
1441d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
144254f21887SBarry Smith   MatScalar      *v = a->a;
144354f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
144471f1c65dSBarry Smith 
144571f1c65dSBarry Smith   PetscFunctionBegin;
144671f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
144771f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
144871f1c65dSBarry Smith   diag = a->diag;
144971f1c65dSBarry Smith   if (!a->idiag) {
145071f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
145171f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
145271f1c65dSBarry Smith     v        = a->a;
145371f1c65dSBarry Smith   }
145471f1c65dSBarry Smith   mdiag = a->mdiag;
145571f1c65dSBarry Smith   idiag = a->idiag;
145671f1c65dSBarry Smith 
1457028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
145871f1c65dSBarry Smith     for (i=0; i<m; i++) {
145971f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
1460e32f2f54SBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
146171f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
146271f1c65dSBarry Smith     }
146371f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
146471f1c65dSBarry Smith   } else {
146571f1c65dSBarry Smith     for (i=0; i<m; i++) {
146671f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
146771f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
146871f1c65dSBarry Smith     }
1469dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
147071f1c65dSBarry Smith   }
147171f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
147271f1c65dSBarry Smith   PetscFunctionReturn(0);
147371f1c65dSBarry Smith }
14745a9745a3SMatthew Knepley EXTERN_C_END
147571f1c65dSBarry Smith 
1476c6db04a5SJed Brown #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
14774a2ae208SSatish Balay #undef __FUNCT__
147841f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
147941f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
148017ab2063SBarry Smith {
1481416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1482e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1483e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
148454f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1485dfbe8321SBarry Smith   PetscErrorCode     ierr;
1486d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
148797f1f81fSBarry Smith   const PetscInt     *idx,*diag;
148817ab2063SBarry Smith 
14893a40ed3dSBarry Smith   PetscFunctionBegin;
1490b965ef7fSBarry Smith   its = its*lits;
149191723122SBarry Smith 
149271f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
149371f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
149471f1c65dSBarry Smith   a->fshift = fshift;
149571f1c65dSBarry Smith   a->omega  = omega;
1496ed480e8bSBarry Smith 
149771f1c65dSBarry Smith   diag = a->diag;
149871f1c65dSBarry Smith   t     = a->ssor_work;
1499ed480e8bSBarry Smith   idiag = a->idiag;
150071f1c65dSBarry Smith   mdiag = a->mdiag;
1501ed480e8bSBarry Smith 
15021ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
15033649974fSBarry Smith   ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr);
150471f1c65dSBarry Smith   CHKMEMQ;
1505ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
150617ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
150717ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1508ed480e8bSBarry Smith     bs = b;
150917ab2063SBarry Smith     for (i=0; i<m; i++) {
151071f1c65dSBarry Smith         d    = fshift + mdiag[i];
1511416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1512ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1513ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
151417ab2063SBarry Smith         sum  = b[i]*d/omega;
1515003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
151617ab2063SBarry Smith         x[i] = sum;
151717ab2063SBarry Smith     }
15181ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
15193649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
1520efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
15213a40ed3dSBarry Smith     PetscFunctionReturn(0);
152217ab2063SBarry Smith   }
1523c783ea89SBarry Smith 
152448af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
1525e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
15263a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
152717ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1528887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
152917ab2063SBarry Smith 
153017ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
153117ab2063SBarry Smith 
1532887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
153317ab2063SBarry Smith     */
153417ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
153517ab2063SBarry Smith 
153617ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
153717ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1538416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1539ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1540ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
154117ab2063SBarry Smith       sum  = b[i];
1542e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1543ed480e8bSBarry Smith       x[i] = sum*idiag[i];
154417ab2063SBarry Smith     }
154517ab2063SBarry Smith 
154617ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1547416022c9SBarry Smith     v = a->a;
1548ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
154917ab2063SBarry Smith 
155017ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1551ed480e8bSBarry Smith     ts = t;
1552416022c9SBarry Smith     diag = a->diag;
155317ab2063SBarry Smith     for (i=0; i<m; i++) {
1554416022c9SBarry Smith       n    = diag[i] - a->i[i];
1555ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1556ed480e8bSBarry Smith       v    = a->a + a->i[i];
155717ab2063SBarry Smith       sum  = t[i];
1558003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1559ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1560733d66baSBarry Smith       /*  x = x + t */
1561733d66baSBarry Smith       x[i] += t[i];
156217ab2063SBarry Smith     }
156317ab2063SBarry Smith 
1564dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
15651ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
15663649974fSBarry Smith     ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
15673a40ed3dSBarry Smith     PetscFunctionReturn(0);
156817ab2063SBarry Smith   }
156917ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
157017ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
157117ab2063SBarry Smith       for (i=0; i<m; i++) {
1572416022c9SBarry Smith         n    = diag[i] - a->i[i];
1573ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1574ed480e8bSBarry Smith         v    = a->a + a->i[i];
157517ab2063SBarry Smith         sum  = b[i];
1576e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
15775c99c7daSBarry Smith         t[i] = sum;
1578ed480e8bSBarry Smith         x[i] = sum*idiag[i];
157917ab2063SBarry Smith       }
15805c99c7daSBarry Smith       xb = t;
1581efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
15823a40ed3dSBarry Smith     } else xb = b;
158317ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
158417ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1585416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1586ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1587ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
158817ab2063SBarry Smith         sum  = xb[i];
1589e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
15905c99c7daSBarry Smith         if (xb == b) {
1591ed480e8bSBarry Smith           x[i] = sum*idiag[i];
15925c99c7daSBarry Smith         } else {
15935c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
159417ab2063SBarry Smith         }
15955c99c7daSBarry Smith       }
1596efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
159717ab2063SBarry Smith     }
159817ab2063SBarry Smith     its--;
159917ab2063SBarry Smith   }
160017ab2063SBarry Smith   while (its--) {
160117ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
160217ab2063SBarry Smith       for (i=0; i<m; i++) {
1603416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1604ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1605ed480e8bSBarry Smith         v    = a->a + a->i[i];
160617ab2063SBarry Smith         sum  = b[i];
1607e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1608ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
160917ab2063SBarry Smith       }
16109f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
161117ab2063SBarry Smith     }
161217ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
161317ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1614416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1615ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1616ed480e8bSBarry Smith         v    = a->a + a->i[i];
161717ab2063SBarry Smith         sum  = b[i];
1618e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1619ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
162017ab2063SBarry Smith       }
16219f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
162217ab2063SBarry Smith     }
162317ab2063SBarry Smith   }
16241ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
16253649974fSBarry Smith   ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr);
162671f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
162717ab2063SBarry Smith }
162817ab2063SBarry Smith 
16292af78befSBarry Smith 
16304a2ae208SSatish Balay #undef __FUNCT__
16314a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1632dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
163317ab2063SBarry Smith {
1634416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
16354e220ebcSLois Curfman McInnes 
16363a40ed3dSBarry Smith   PetscFunctionBegin;
16374e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
16384e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
16394e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
16404e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
16414e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
16428e58a170SBarry Smith   info->mallocs        = (double)A->info.mallocs;
16437adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
1644d5f3da31SBarry Smith   if (A->factortype) {
16454e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
16464e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
16474e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
16484e220ebcSLois Curfman McInnes   } else {
16494e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
16504e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
16514e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
16524e220ebcSLois Curfman McInnes   }
16533a40ed3dSBarry Smith   PetscFunctionReturn(0);
165417ab2063SBarry Smith }
165517ab2063SBarry Smith 
16564a2ae208SSatish Balay #undef __FUNCT__
16574a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
16582b40b63fSBarry Smith PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
165917ab2063SBarry Smith {
1660416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
16613b98c0a2SBarry Smith   PetscInt          i,m = A->rmap->n - 1,d = 0;
16626849ba73SBarry Smith   PetscErrorCode    ierr;
166397b48c8fSBarry Smith   const PetscScalar *xx;
166497b48c8fSBarry Smith   PetscScalar       *bb;
1665ace3abfcSBarry Smith   PetscBool         missing;
166617ab2063SBarry Smith 
16673a40ed3dSBarry Smith   PetscFunctionBegin;
166897b48c8fSBarry Smith   if (x && b) {
166997b48c8fSBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
167097b48c8fSBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
167197b48c8fSBarry Smith     for (i=0; i<N; i++) {
167297b48c8fSBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
167397b48c8fSBarry Smith       bb[rows[i]] = diag*xx[rows[i]];
167497b48c8fSBarry Smith     }
167597b48c8fSBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
167697b48c8fSBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
167797b48c8fSBarry Smith   }
167897b48c8fSBarry Smith 
1679a9817697SBarry Smith   if (a->keepnonzeropattern) {
1680f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
1681e32f2f54SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1682bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1683f1e2ffcdSBarry Smith     }
1684f4df32b1SMatthew Knepley     if (diag != 0.0) {
168509f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
1686e32f2f54SBarry Smith       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1687f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1688f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1689f1e2ffcdSBarry Smith       }
1690f1e2ffcdSBarry Smith     }
169188e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1692f1e2ffcdSBarry Smith   } else {
1693f4df32b1SMatthew Knepley     if (diag != 0.0) {
169417ab2063SBarry Smith       for (i=0; i<N; i++) {
1695e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
16967ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1697416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1698f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1699bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
17007ae801bdSBarry Smith         } else { /* in case row was completely empty */
1701f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
170217ab2063SBarry Smith         }
170317ab2063SBarry Smith       }
17043a40ed3dSBarry Smith     } else {
170517ab2063SBarry Smith       for (i=0; i<N; i++) {
1706e32f2f54SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1707416022c9SBarry Smith         a->ilen[rows[i]] = 0;
170817ab2063SBarry Smith       }
170917ab2063SBarry Smith     }
171088e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1711f1e2ffcdSBarry Smith   }
171243a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17133a40ed3dSBarry Smith   PetscFunctionReturn(0);
171417ab2063SBarry Smith }
171517ab2063SBarry Smith 
17164a2ae208SSatish Balay #undef __FUNCT__
17176e169961SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_SeqAIJ"
17186e169961SBarry Smith PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
17196e169961SBarry Smith {
17206e169961SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
17216e169961SBarry Smith   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
17226e169961SBarry Smith   PetscErrorCode    ierr;
17232b40b63fSBarry Smith   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
17246e169961SBarry Smith   const PetscScalar *xx;
17256e169961SBarry Smith   PetscScalar       *bb;
17266e169961SBarry Smith 
17276e169961SBarry Smith   PetscFunctionBegin;
17286e169961SBarry Smith   if (x && b) {
17296e169961SBarry Smith     ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
17306e169961SBarry Smith     ierr = VecGetArray(b,&bb);CHKERRQ(ierr);
17312b40b63fSBarry Smith     vecs = PETSC_TRUE;
17326e169961SBarry Smith   }
17336e169961SBarry Smith   ierr = PetscMalloc(A->rmap->n*sizeof(PetscBool),&zeroed);CHKERRQ(ierr);
17346e169961SBarry Smith   ierr = PetscMemzero(zeroed,A->rmap->n*sizeof(PetscBool));CHKERRQ(ierr);
17356e169961SBarry Smith   for (i=0; i<N; i++) {
17366e169961SBarry Smith     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
17376e169961SBarry Smith     ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
17386e169961SBarry Smith     zeroed[rows[i]] = PETSC_TRUE;
17396e169961SBarry Smith   }
17406e169961SBarry Smith   for (i=0; i<A->rmap->n; i++) {
17416e169961SBarry Smith     if (!zeroed[i]) {
17426e169961SBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
17436e169961SBarry Smith         if (zeroed[a->j[j]]) {
17442b40b63fSBarry Smith           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
17456e169961SBarry Smith           a->a[j] = 0.0;
17466e169961SBarry Smith         }
17476e169961SBarry Smith       }
17482b40b63fSBarry Smith     } else if (vecs) bb[i] = diag*xx[i];
17496e169961SBarry Smith   }
17506e169961SBarry Smith   if (x && b) {
17516e169961SBarry Smith     ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
17526e169961SBarry Smith     ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr);
17536e169961SBarry Smith   }
17546e169961SBarry Smith   ierr = PetscFree(zeroed);CHKERRQ(ierr);
17556e169961SBarry Smith   if (diag != 0.0) {
17566e169961SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
17576e169961SBarry Smith     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
17586e169961SBarry Smith     for (i=0; i<N; i++) {
17596e169961SBarry Smith       a->a[a->diag[rows[i]]] = diag;
17606e169961SBarry Smith     }
17616e169961SBarry Smith   }
17626e169961SBarry Smith   A->same_nonzero = PETSC_TRUE;
17636e169961SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
17646e169961SBarry Smith   PetscFunctionReturn(0);
17656e169961SBarry Smith }
17666e169961SBarry Smith 
17676e169961SBarry Smith #undef __FUNCT__
17684a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1769a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
177017ab2063SBarry Smith {
1771416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
177297f1f81fSBarry Smith   PetscInt   *itmp;
177317ab2063SBarry Smith 
17743a40ed3dSBarry Smith   PetscFunctionBegin;
1775e32f2f54SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
177617ab2063SBarry Smith 
1777416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1778bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
177917ab2063SBarry Smith   if (idx) {
1780bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1781bfeeae90SHong Zhang     if (*nz) {
17824e093b46SBarry Smith       *idx = itmp;
178317ab2063SBarry Smith     }
178417ab2063SBarry Smith     else *idx = 0;
178517ab2063SBarry Smith   }
17863a40ed3dSBarry Smith   PetscFunctionReturn(0);
178717ab2063SBarry Smith }
178817ab2063SBarry Smith 
1789bfeeae90SHong Zhang /* remove this function? */
17904a2ae208SSatish Balay #undef __FUNCT__
17914a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1792a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
179317ab2063SBarry Smith {
17943a40ed3dSBarry Smith   PetscFunctionBegin;
17953a40ed3dSBarry Smith   PetscFunctionReturn(0);
179617ab2063SBarry Smith }
179717ab2063SBarry Smith 
17984a2ae208SSatish Balay #undef __FUNCT__
17994a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1800dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
180117ab2063SBarry Smith {
1802416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
180354f21887SBarry Smith   MatScalar      *v = a->a;
180436db0b34SBarry Smith   PetscReal      sum = 0.0;
18056849ba73SBarry Smith   PetscErrorCode ierr;
180697f1f81fSBarry Smith   PetscInt       i,j;
180717ab2063SBarry Smith 
18083a40ed3dSBarry Smith   PetscFunctionBegin;
180917ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1810416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1811aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
181236db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
181317ab2063SBarry Smith #else
181417ab2063SBarry Smith       sum += (*v)*(*v); v++;
181517ab2063SBarry Smith #endif
181617ab2063SBarry Smith     }
18178f1a2a5eSBarry Smith     *nrm = PetscSqrtReal(sum);
18183a40ed3dSBarry Smith   } else if (type == NORM_1) {
181936db0b34SBarry Smith     PetscReal *tmp;
182097f1f81fSBarry Smith     PetscInt    *jj = a->j;
1821d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1822d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1823064f8208SBarry Smith     *nrm = 0.0;
1824416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1825bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
182617ab2063SBarry Smith     }
1827d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1828064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
182917ab2063SBarry Smith     }
1830606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
18313a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1832064f8208SBarry Smith     *nrm = 0.0;
1833d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1834bfeeae90SHong Zhang       v = a->a + a->i[j];
183517ab2063SBarry Smith       sum = 0.0;
1836416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1837cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
183817ab2063SBarry Smith       }
1839064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
184017ab2063SBarry Smith     }
18413a40ed3dSBarry Smith   } else {
1842e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
184317ab2063SBarry Smith   }
18443a40ed3dSBarry Smith   PetscFunctionReturn(0);
184517ab2063SBarry Smith }
184617ab2063SBarry Smith 
18474e938277SHong Zhang /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
18484e938277SHong Zhang #undef __FUNCT__
18494e938277SHong Zhang #define __FUNCT__ "MatTransposeSymbolic_SeqAIJ"
18504e938277SHong Zhang PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
18514e938277SHong Zhang {
18524e938277SHong Zhang   PetscErrorCode ierr;
18534e938277SHong Zhang   PetscInt       i,j,anzj;
18544e938277SHong Zhang   Mat_SeqAIJ     *a=(Mat_SeqAIJ *)A->data,*b;
18554e938277SHong Zhang   PetscInt       an=A->cmap->N,am=A->rmap->N;
18564e938277SHong Zhang   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
18574e938277SHong Zhang 
18584e938277SHong Zhang   PetscFunctionBegin;
18594e938277SHong Zhang   /* Allocate space for symbolic transpose info and work array */
18604e938277SHong Zhang   ierr = PetscMalloc((an+1)*sizeof(PetscInt),&ati);CHKERRQ(ierr);
18614e938277SHong Zhang   ierr = PetscMalloc(ai[am]*sizeof(PetscInt),&atj);CHKERRQ(ierr);
18624e938277SHong Zhang   ierr = PetscMalloc(an*sizeof(PetscInt),&atfill);CHKERRQ(ierr);
18634e938277SHong Zhang   ierr = PetscMemzero(ati,(an+1)*sizeof(PetscInt));CHKERRQ(ierr);
18644e938277SHong Zhang 
18654e938277SHong Zhang   /* Walk through aj and count ## of non-zeros in each row of A^T. */
18664e938277SHong Zhang   /* Note: offset by 1 for fast conversion into csr format. */
18674e938277SHong Zhang   for (i=0;i<ai[am];i++) {
18684e938277SHong Zhang     ati[aj[i]+1] += 1;
18694e938277SHong Zhang   }
18704e938277SHong Zhang   /* Form ati for csr format of A^T. */
18714e938277SHong Zhang   for (i=0;i<an;i++) {
18724e938277SHong Zhang     ati[i+1] += ati[i];
18734e938277SHong Zhang   }
18744e938277SHong Zhang 
18754e938277SHong Zhang   /* Copy ati into atfill so we have locations of the next free space in atj */
18764e938277SHong Zhang   ierr = PetscMemcpy(atfill,ati,an*sizeof(PetscInt));CHKERRQ(ierr);
18774e938277SHong Zhang 
18784e938277SHong Zhang   /* Walk through A row-wise and mark nonzero entries of A^T. */
18794e938277SHong Zhang   for (i=0;i<am;i++) {
18804e938277SHong Zhang     anzj = ai[i+1] - ai[i];
18814e938277SHong Zhang     for (j=0;j<anzj;j++) {
18824e938277SHong Zhang       atj[atfill[*aj]] = i;
18834e938277SHong Zhang       atfill[*aj++]   += 1;
18844e938277SHong Zhang     }
18854e938277SHong Zhang   }
18864e938277SHong Zhang 
18874e938277SHong Zhang   /* Clean up temporary space and complete requests. */
18884e938277SHong Zhang   ierr = PetscFree(atfill);CHKERRQ(ierr);
18894e938277SHong Zhang   ierr = MatCreateSeqAIJWithArrays(((PetscObject)A)->comm,an,am,ati,atj,PETSC_NULL,B);CHKERRQ(ierr);
1890a2f3521dSMark F. Adams   (*B)->rmap->bs = A->cmap->bs;
1891a2f3521dSMark F. Adams   (*B)->cmap->bs = A->rmap->bs;
1892a2f3521dSMark F. Adams 
18934e938277SHong Zhang   b = (Mat_SeqAIJ *)((*B)->data);
18944e938277SHong Zhang   b->free_a   = PETSC_FALSE;
18954e938277SHong Zhang   b->free_ij  = PETSC_TRUE;
18964e938277SHong Zhang   b->nonew    = 0;
18974e938277SHong Zhang   PetscFunctionReturn(0);
18984e938277SHong Zhang }
18994e938277SHong Zhang 
19004a2ae208SSatish Balay #undef __FUNCT__
19014a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1902fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
190317ab2063SBarry Smith {
1904416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1905416022c9SBarry Smith   Mat            C;
19066849ba73SBarry Smith   PetscErrorCode ierr;
1907d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
190854f21887SBarry Smith   MatScalar      *array = a->a;
190917ab2063SBarry Smith 
19103a40ed3dSBarry Smith   PetscFunctionBegin;
1911e32f2f54SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1912fc4dec0aSBarry Smith 
1913fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1914d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1915d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1916bfeeae90SHong Zhang 
1917bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
19187adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1919d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
1920a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(C,A->cmap->bs,A->rmap->bs);CHKERRQ(ierr);
19217adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1922ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1923606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1924a541d17aSBarry Smith   } else {
1925a541d17aSBarry Smith     C = *B;
1926a541d17aSBarry Smith   }
1927a541d17aSBarry Smith 
192817ab2063SBarry Smith   for (i=0; i<m; i++) {
192917ab2063SBarry Smith     len    = ai[i+1]-ai[i];
193087d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1931b9b97703SBarry Smith     array += len;
1932b9b97703SBarry Smith     aj    += len;
193317ab2063SBarry Smith   }
19346d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
19356d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
193617ab2063SBarry Smith 
1937815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1938416022c9SBarry Smith     *B = C;
193917ab2063SBarry Smith   } else {
1940eb6b5d47SBarry Smith     ierr = MatHeaderMerge(A,C);CHKERRQ(ierr);
194117ab2063SBarry Smith   }
19423a40ed3dSBarry Smith   PetscFunctionReturn(0);
194317ab2063SBarry Smith }
194417ab2063SBarry Smith 
1945cd0d46ebSvictorle EXTERN_C_BEGIN
1946cd0d46ebSvictorle #undef __FUNCT__
19475fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
19487087cfbeSBarry Smith PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
1949cd0d46ebSvictorle {
1950cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
195154f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
195254f21887SBarry Smith   MatScalar      *va,*vb;
19536849ba73SBarry Smith   PetscErrorCode ierr;
195497f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1955cd0d46ebSvictorle 
1956cd0d46ebSvictorle   PetscFunctionBegin;
1957cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1958cd0d46ebSvictorle 
1959cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1960cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
19615485867bSBarry Smith   if (ma!=nb || na!=mb){
19625485867bSBarry Smith     *f = PETSC_FALSE;
19635485867bSBarry Smith     PetscFunctionReturn(0);
19645485867bSBarry Smith   }
1965cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1966cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1967cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
196897f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
196997f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1970cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1971cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1972cd0d46ebSvictorle 
1973cd0d46ebSvictorle   *f = PETSC_TRUE;
1974cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1975cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
197697f1f81fSBarry Smith       PetscInt         idc,idr;
19775485867bSBarry Smith       PetscScalar vc,vr;
1978cd0d46ebSvictorle       /* column/row index/value */
19795485867bSBarry Smith       idc = adx[aptr[i]];
19805485867bSBarry Smith       idr = bdx[bptr[idc]];
19815485867bSBarry Smith       vc  = va[aptr[i]];
19825485867bSBarry Smith       vr  = vb[bptr[idc]];
19835485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
19845485867bSBarry Smith         *f = PETSC_FALSE;
19855485867bSBarry Smith         goto done;
1986cd0d46ebSvictorle       } else {
19875485867bSBarry Smith         aptr[i]++;
19885485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1989cd0d46ebSvictorle       }
1990cd0d46ebSvictorle     }
1991cd0d46ebSvictorle   }
1992cd0d46ebSvictorle  done:
1993cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
19943aeef889SHong Zhang   ierr = PetscFree(bptr);CHKERRQ(ierr);
1995cd0d46ebSvictorle   PetscFunctionReturn(0);
1996cd0d46ebSvictorle }
1997cd0d46ebSvictorle EXTERN_C_END
1998cd0d46ebSvictorle 
19991cbb95d3SBarry Smith EXTERN_C_BEGIN
20001cbb95d3SBarry Smith #undef __FUNCT__
20011cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
20027087cfbeSBarry Smith PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
20031cbb95d3SBarry Smith {
20041cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
200554f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
200654f21887SBarry Smith   MatScalar      *va,*vb;
20071cbb95d3SBarry Smith   PetscErrorCode ierr;
20081cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
20091cbb95d3SBarry Smith 
20101cbb95d3SBarry Smith   PetscFunctionBegin;
20111cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
20121cbb95d3SBarry Smith 
20131cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
20141cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
20151cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
20161cbb95d3SBarry Smith     *f = PETSC_FALSE;
20171cbb95d3SBarry Smith     PetscFunctionReturn(0);
20181cbb95d3SBarry Smith   }
20191cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
20201cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
20211cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
20221cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
20231cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
20241cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
20251cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
20261cbb95d3SBarry Smith 
20271cbb95d3SBarry Smith   *f = PETSC_TRUE;
20281cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
20291cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
20301cbb95d3SBarry Smith       PetscInt         idc,idr;
20311cbb95d3SBarry Smith       PetscScalar vc,vr;
20321cbb95d3SBarry Smith       /* column/row index/value */
20331cbb95d3SBarry Smith       idc = adx[aptr[i]];
20341cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
20351cbb95d3SBarry Smith       vc  = va[aptr[i]];
20361cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
20371cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
20381cbb95d3SBarry Smith         *f = PETSC_FALSE;
20391cbb95d3SBarry Smith         goto done;
20401cbb95d3SBarry Smith       } else {
20411cbb95d3SBarry Smith         aptr[i]++;
20421cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
20431cbb95d3SBarry Smith       }
20441cbb95d3SBarry Smith     }
20451cbb95d3SBarry Smith   }
20461cbb95d3SBarry Smith  done:
20471cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
20481cbb95d3SBarry Smith   ierr = PetscFree(bptr);CHKERRQ(ierr);
20491cbb95d3SBarry Smith   PetscFunctionReturn(0);
20501cbb95d3SBarry Smith }
20511cbb95d3SBarry Smith EXTERN_C_END
20521cbb95d3SBarry Smith 
20539e29f15eSvictorle #undef __FUNCT__
20549e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
2055ace3abfcSBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
20569e29f15eSvictorle {
2057dfbe8321SBarry Smith   PetscErrorCode ierr;
20589e29f15eSvictorle   PetscFunctionBegin;
20595485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
20609e29f15eSvictorle   PetscFunctionReturn(0);
20619e29f15eSvictorle }
20629e29f15eSvictorle 
20634a2ae208SSatish Balay #undef __FUNCT__
20641cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
2065ace3abfcSBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
20661cbb95d3SBarry Smith {
20671cbb95d3SBarry Smith   PetscErrorCode ierr;
20681cbb95d3SBarry Smith   PetscFunctionBegin;
20691cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
20701cbb95d3SBarry Smith   PetscFunctionReturn(0);
20711cbb95d3SBarry Smith }
20721cbb95d3SBarry Smith 
20731cbb95d3SBarry Smith #undef __FUNCT__
20744a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
2075dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
207617ab2063SBarry Smith {
2077416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
207854f21887SBarry Smith   PetscScalar    *l,*r,x;
207954f21887SBarry Smith   MatScalar      *v;
2080dfbe8321SBarry Smith   PetscErrorCode ierr;
2081d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
208217ab2063SBarry Smith 
20833a40ed3dSBarry Smith   PetscFunctionBegin;
208417ab2063SBarry Smith   if (ll) {
20853ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
20863ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
2087e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
2088e32f2f54SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
20891ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
2090416022c9SBarry Smith     v = a->a;
209117ab2063SBarry Smith     for (i=0; i<m; i++) {
209217ab2063SBarry Smith       x = l[i];
2093416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
209417ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
209517ab2063SBarry Smith     }
20961ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
2097efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
209817ab2063SBarry Smith   }
209917ab2063SBarry Smith   if (rr) {
2100e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
2101e32f2f54SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
21021ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
2103416022c9SBarry Smith     v = a->a; jj = a->j;
210417ab2063SBarry Smith     for (i=0; i<nz; i++) {
2105bfeeae90SHong Zhang       (*v++) *= r[*jj++];
210617ab2063SBarry Smith     }
21071ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
2108efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
210917ab2063SBarry Smith   }
211086c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
211186c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
21123a40ed3dSBarry Smith   PetscFunctionReturn(0);
211317ab2063SBarry Smith }
211417ab2063SBarry Smith 
21154a2ae208SSatish Balay #undef __FUNCT__
21164a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
211797f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
211817ab2063SBarry Smith {
2119db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
21206849ba73SBarry Smith   PetscErrorCode ierr;
2121d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
212297f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
21235d0c19d7SBarry Smith   const PetscInt *irow,*icol;
21245d0c19d7SBarry Smith   PetscInt       nrows,ncols;
212597f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
212654f21887SBarry Smith   MatScalar      *a_new,*mat_a;
2127416022c9SBarry Smith   Mat            C;
2128ace3abfcSBarry Smith   PetscBool      stride,sorted;
212917ab2063SBarry Smith 
21303a40ed3dSBarry Smith   PetscFunctionBegin;
213114ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
2132e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
213314ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
2134e32f2f54SBarry Smith   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
213599141d43SSatish Balay 
213617ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
2137b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
2138b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
213917ab2063SBarry Smith 
2140fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
2141251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);CHKERRQ(ierr);
2142fee21e36SBarry Smith   if (stride && step == 1) {
214302834360SBarry Smith     /* special case of contiguous rows */
21440e83c824SBarry Smith     ierr = PetscMalloc2(nrows,PetscInt,&lens,nrows,PetscInt,&starts);CHKERRQ(ierr);
214502834360SBarry Smith     /* loop over new rows determining lens and starting points */
214602834360SBarry Smith     for (i=0; i<nrows; i++) {
2147bfeeae90SHong Zhang       kstart  = ai[irow[i]];
2148a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
214902834360SBarry Smith       for (k=kstart; k<kend; k++) {
2150bfeeae90SHong Zhang         if (aj[k] >= first) {
215102834360SBarry Smith           starts[i] = k;
215202834360SBarry Smith           break;
215302834360SBarry Smith 	}
215402834360SBarry Smith       }
2155a2744918SBarry Smith       sum = 0;
215602834360SBarry Smith       while (k < kend) {
2157bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
2158a2744918SBarry Smith         sum++;
215902834360SBarry Smith       }
2160a2744918SBarry Smith       lens[i] = sum;
216102834360SBarry Smith     }
216202834360SBarry Smith     /* create submatrix */
2163cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
216497f1f81fSBarry Smith       PetscInt n_cols,n_rows;
216508480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
2166e32f2f54SBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2167d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
216808480c60SBarry Smith       C = *B;
21693a40ed3dSBarry Smith     } else {
21703bef6203SJed Brown       PetscInt rbs,cbs;
21717adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2172f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
21733bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
21743bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
21753bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
21767adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2177ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
217808480c60SBarry Smith     }
2179db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
2180db02288aSLois Curfman McInnes 
218102834360SBarry Smith     /* loop over rows inserting into submatrix */
2182db02288aSLois Curfman McInnes     a_new    = c->a;
2183db02288aSLois Curfman McInnes     j_new    = c->j;
2184db02288aSLois Curfman McInnes     i_new    = c->i;
2185bfeeae90SHong Zhang 
218602834360SBarry Smith     for (i=0; i<nrows; i++) {
2187a2744918SBarry Smith       ii    = starts[i];
2188a2744918SBarry Smith       lensi = lens[i];
2189a2744918SBarry Smith       for (k=0; k<lensi; k++) {
2190a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
219102834360SBarry Smith       }
219287828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
2193a2744918SBarry Smith       a_new      += lensi;
2194a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
2195a2744918SBarry Smith       c->ilen[i]  = lensi;
219602834360SBarry Smith     }
21970e83c824SBarry Smith     ierr = PetscFree2(lens,starts);CHKERRQ(ierr);
21983a40ed3dSBarry Smith   } else {
219902834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
22000e83c824SBarry Smith     ierr  = PetscMalloc(oldcols*sizeof(PetscInt),&smap);CHKERRQ(ierr);
220197f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
22020e83c824SBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
22034dcab191SBarry Smith     for (i=0; i<ncols; i++) {
22044dcab191SBarry Smith #if defined(PETSC_USE_DEBUG)
22054dcab191SBarry Smith       if (icol[i] >= oldcols) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Requesting column beyond largest column icol[%D] %D <= A->cmap->n %D",i,icol[i],oldcols);
22064dcab191SBarry Smith #endif
22074dcab191SBarry Smith       smap[icol[i]] = i+1;
22084dcab191SBarry Smith     }
22094dcab191SBarry Smith 
221002834360SBarry Smith     /* determine lens of each row */
221102834360SBarry Smith     for (i=0; i<nrows; i++) {
2212bfeeae90SHong Zhang       kstart  = ai[irow[i]];
221302834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
221402834360SBarry Smith       lens[i] = 0;
221502834360SBarry Smith       for (k=kstart; k<kend; k++) {
2216bfeeae90SHong Zhang         if (smap[aj[k]]) {
221702834360SBarry Smith           lens[i]++;
221802834360SBarry Smith         }
221902834360SBarry Smith       }
222002834360SBarry Smith     }
222117ab2063SBarry Smith     /* Create and fill new matrix */
2222a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
2223ace3abfcSBarry Smith       PetscBool  equal;
22240f5bd95cSBarry Smith 
222599141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
2226e32f2f54SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2227d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
22280f5bd95cSBarry Smith       if (!equal) {
2229e32f2f54SBarry Smith         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
223099141d43SSatish Balay       }
2231d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
223208480c60SBarry Smith       C = *B;
22333a40ed3dSBarry Smith     } else {
22343bef6203SJed Brown       PetscInt rbs,cbs;
22357adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
2236f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
22373bef6203SJed Brown       ierr = ISGetBlockSize(isrow,&rbs);CHKERRQ(ierr);
22383bef6203SJed Brown       ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr);
22393bef6203SJed Brown       ierr = MatSetBlockSizes(C,rbs,cbs);CHKERRQ(ierr);
22407adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
2241ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
224208480c60SBarry Smith     }
224399141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
224417ab2063SBarry Smith     for (i=0; i<nrows; i++) {
224599141d43SSatish Balay       row    = irow[i];
2246bfeeae90SHong Zhang       kstart = ai[row];
224799141d43SSatish Balay       kend   = kstart + a->ilen[row];
2248bfeeae90SHong Zhang       mat_i  = c->i[i];
224999141d43SSatish Balay       mat_j  = c->j + mat_i;
225099141d43SSatish Balay       mat_a  = c->a + mat_i;
225199141d43SSatish Balay       mat_ilen = c->ilen + i;
225217ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
2253bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
2254ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
225599141d43SSatish Balay           *mat_a++ = a->a[k];
225699141d43SSatish Balay           (*mat_ilen)++;
225799141d43SSatish Balay 
225817ab2063SBarry Smith         }
225917ab2063SBarry Smith       }
226017ab2063SBarry Smith     }
226102834360SBarry Smith     /* Free work space */
226202834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
2263606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
2264606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
226502834360SBarry Smith   }
22666d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22676d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
226817ab2063SBarry Smith 
226917ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
2270416022c9SBarry Smith   *B = C;
22713a40ed3dSBarry Smith   PetscFunctionReturn(0);
227217ab2063SBarry Smith }
227317ab2063SBarry Smith 
22741df811f5SHong Zhang #undef __FUNCT__
227582d44351SHong Zhang #define __FUNCT__ "MatGetMultiProcBlock_SeqAIJ"
2276fc08c53fSHong Zhang PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat* subMat)
227782d44351SHong Zhang {
227882d44351SHong Zhang   PetscErrorCode ierr;
227982d44351SHong Zhang   Mat            B;
228082d44351SHong Zhang 
228182d44351SHong Zhang   PetscFunctionBegin;
228282d44351SHong Zhang   ierr = MatCreate(subComm,&B);CHKERRQ(ierr);
228382d44351SHong Zhang   ierr = MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);CHKERRQ(ierr);
2284a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(B,mat->rmap->bs,mat->cmap->bs); CHKERRQ(ierr);
228582d44351SHong Zhang   ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr);
228682d44351SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);CHKERRQ(ierr);
228782d44351SHong Zhang   *subMat = B;
228882d44351SHong Zhang   PetscFunctionReturn(0);
228982d44351SHong Zhang }
229082d44351SHong Zhang 
229182d44351SHong Zhang #undef __FUNCT__
22924a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
22930481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2294a871dcd8SBarry Smith {
229563b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2296dfbe8321SBarry Smith   PetscErrorCode ierr;
229763b91edcSBarry Smith   Mat            outA;
2298ace3abfcSBarry Smith   PetscBool      row_identity,col_identity;
229963b91edcSBarry Smith 
23003a40ed3dSBarry Smith   PetscFunctionBegin;
2301e32f2f54SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
23021df811f5SHong Zhang 
2303b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
2304b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
2305a871dcd8SBarry Smith 
230663b91edcSBarry Smith   outA              = inA;
2307d5f3da31SBarry Smith   outA->factortype  = MAT_FACTOR_LU;
2308c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
23096bf464f9SBarry Smith   ierr = ISDestroy(&a->row);CHKERRQ(ierr);
2310c3122656SLisandro Dalcin   a->row = row;
2311c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
23126bf464f9SBarry Smith   ierr = ISDestroy(&a->col);CHKERRQ(ierr);
2313c3122656SLisandro Dalcin   a->col = col;
231463b91edcSBarry Smith 
231536db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
23166bf464f9SBarry Smith   ierr = ISDestroy(&a->icol);CHKERRQ(ierr);
23174c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
231852e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
2319f0ec6fceSSatish Balay 
232094a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
2321d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
2322d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
232394a9d846SBarry Smith   }
232463b91edcSBarry Smith 
2325f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
2326137fb511SHong Zhang   if (row_identity && col_identity) {
2327ad04f41aSHong Zhang     ierr = MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);CHKERRQ(ierr);
2328137fb511SHong Zhang   } else {
2329719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
2330137fb511SHong Zhang   }
23313a40ed3dSBarry Smith   PetscFunctionReturn(0);
2332a871dcd8SBarry Smith }
2333a871dcd8SBarry Smith 
23344a2ae208SSatish Balay #undef __FUNCT__
23354a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
2336f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2337f0b747eeSBarry Smith {
2338f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2339f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
2340efee365bSSatish Balay   PetscErrorCode ierr;
23410805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
23423a40ed3dSBarry Smith 
23433a40ed3dSBarry Smith   PetscFunctionBegin;
2344f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
2345efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
234686c113feSBarry Smith   a->idiagvalid  = PETSC_FALSE;
234786c113feSBarry Smith   a->ibdiagvalid = PETSC_FALSE;
23483a40ed3dSBarry Smith   PetscFunctionReturn(0);
2349f0b747eeSBarry Smith }
2350f0b747eeSBarry Smith 
23514a2ae208SSatish Balay #undef __FUNCT__
23524a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
235397f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2354cddf8d76SBarry Smith {
2355dfbe8321SBarry Smith   PetscErrorCode ierr;
235697f1f81fSBarry Smith   PetscInt       i;
2357cddf8d76SBarry Smith 
23583a40ed3dSBarry Smith   PetscFunctionBegin;
2359cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
2360b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
2361cddf8d76SBarry Smith   }
2362cddf8d76SBarry Smith 
2363cddf8d76SBarry Smith   for (i=0; i<n; i++) {
23646a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
2365cddf8d76SBarry Smith   }
23663a40ed3dSBarry Smith   PetscFunctionReturn(0);
2367cddf8d76SBarry Smith }
2368cddf8d76SBarry Smith 
23694a2ae208SSatish Balay #undef __FUNCT__
23704a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
237197f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
23724dcbc457SBarry Smith {
2373e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
23746849ba73SBarry Smith   PetscErrorCode ierr;
23755d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
23765d0c19d7SBarry Smith   const PetscInt *idx;
237797f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
2378f1af5d2fSBarry Smith   PetscBT        table;
2379bbd702dbSSatish Balay 
23803a40ed3dSBarry Smith   PetscFunctionBegin;
2381d0f46423SBarry Smith   m     = A->rmap->n;
2382e4d965acSSatish Balay   ai    = a->i;
2383bfeeae90SHong Zhang   aj    = a->j;
23848a047759SSatish Balay 
2385e32f2f54SBarry Smith   if (ov < 0)  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
238606763907SSatish Balay 
238797f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
238853b8de81SBarry Smith   ierr = PetscBTCreate(m,&table);CHKERRQ(ierr);
238906763907SSatish Balay 
2390e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
2391b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
2392e4d965acSSatish Balay     isz  = 0;
23936831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
2394e4d965acSSatish Balay 
2395e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
23964dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
2397b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
2398e4d965acSSatish Balay 
2399dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2400e4d965acSSatish Balay     for (j=0; j<n ; ++j){
2401f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
24024dcbc457SBarry Smith     }
240306763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
24046bf464f9SBarry Smith     ierr = ISDestroy(&is[i]);CHKERRQ(ierr);
2405e4d965acSSatish Balay 
240604a348a9SBarry Smith     k = 0;
240704a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
240804a348a9SBarry Smith       n = isz;
240906763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
2410e4d965acSSatish Balay         row   = nidx[k];
2411e4d965acSSatish Balay         start = ai[row];
2412e4d965acSSatish Balay         end   = ai[row+1];
241304a348a9SBarry Smith         for (l = start; l<end ; l++){
2414efb16452SHong Zhang           val = aj[l] ;
2415f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
2416e4d965acSSatish Balay         }
2417e4d965acSSatish Balay       }
2418e4d965acSSatish Balay     }
241970b3c8c7SBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));CHKERRQ(ierr);
2420e4d965acSSatish Balay   }
242194bacf5dSBarry Smith   ierr = PetscBTDestroy(&table);CHKERRQ(ierr);
2422606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
24233a40ed3dSBarry Smith   PetscFunctionReturn(0);
24244dcbc457SBarry Smith }
242517ab2063SBarry Smith 
24260513a670SBarry Smith /* -------------------------------------------------------------- */
24274a2ae208SSatish Balay #undef __FUNCT__
24284a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
2429dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
24300513a670SBarry Smith {
24310513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
24326849ba73SBarry Smith   PetscErrorCode ierr;
24333b98c0a2SBarry Smith   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
24345d0c19d7SBarry Smith   const PetscInt *row,*col;
24355d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
243656cd22aeSBarry Smith   IS             icolp,irowp;
24373b98c0a2SBarry Smith   PetscInt       *cwork = PETSC_NULL;
24383b98c0a2SBarry Smith   PetscScalar    *vwork = PETSC_NULL;
24390513a670SBarry Smith 
24403a40ed3dSBarry Smith   PetscFunctionBegin;
24414c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
244256cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
24434c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
244456cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
24450513a670SBarry Smith 
24460513a670SBarry Smith   /* determine lengths of permuted rows */
244797f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
24480513a670SBarry Smith   for (i=0; i<m; i++) {
24490513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
24500513a670SBarry Smith   }
24517adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
2452f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
2453a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(*B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr);
24547adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2455ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2456606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
24570513a670SBarry Smith 
245897f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
24590513a670SBarry Smith   for (i=0; i<m; i++) {
246032ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
24610513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2462cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
246332ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
24640513a670SBarry Smith   }
2465606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
24663c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
24670513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
24680513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
246956cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
247056cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
24716bf464f9SBarry Smith   ierr = ISDestroy(&irowp);CHKERRQ(ierr);
24726bf464f9SBarry Smith   ierr = ISDestroy(&icolp);CHKERRQ(ierr);
24733a40ed3dSBarry Smith   PetscFunctionReturn(0);
24740513a670SBarry Smith }
24750513a670SBarry Smith 
24764a2ae208SSatish Balay #undef __FUNCT__
24774a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2478dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2479cb5b572fSBarry Smith {
2480dfbe8321SBarry Smith   PetscErrorCode ierr;
2481cb5b572fSBarry Smith 
2482cb5b572fSBarry Smith   PetscFunctionBegin;
248333f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
248433f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2485be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2486be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2487be6bf707SBarry Smith 
2488700c5bfcSBarry 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");
2489d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2490cb5b572fSBarry Smith   } else {
2491cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2492cb5b572fSBarry Smith   }
2493cb5b572fSBarry Smith   PetscFunctionReturn(0);
2494cb5b572fSBarry Smith }
2495cb5b572fSBarry Smith 
24964a2ae208SSatish Balay #undef __FUNCT__
24974994cf47SJed Brown #define __FUNCT__ "MatSetUp_SeqAIJ"
24984994cf47SJed Brown PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2499273d9f13SBarry Smith {
2500dfbe8321SBarry Smith   PetscErrorCode ierr;
2501273d9f13SBarry Smith 
2502273d9f13SBarry Smith   PetscFunctionBegin;
2503ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2504273d9f13SBarry Smith   PetscFunctionReturn(0);
2505273d9f13SBarry Smith }
2506273d9f13SBarry Smith 
25074a2ae208SSatish Balay #undef __FUNCT__
25084a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2509a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
25106c0721eeSBarry Smith {
25116c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
25126c0721eeSBarry Smith   PetscFunctionBegin;
25136c0721eeSBarry Smith   *array = a->a;
25146c0721eeSBarry Smith   PetscFunctionReturn(0);
25156c0721eeSBarry Smith }
25166c0721eeSBarry Smith 
25174a2ae208SSatish Balay #undef __FUNCT__
25184a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2519dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
25206c0721eeSBarry Smith {
25216c0721eeSBarry Smith   PetscFunctionBegin;
25226c0721eeSBarry Smith   PetscFunctionReturn(0);
25236c0721eeSBarry Smith }
2524273d9f13SBarry Smith 
2525ee4f033dSBarry Smith #undef __FUNCT__
2526ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2527dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2528ee4f033dSBarry Smith {
25296849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
25306849ba73SBarry Smith   PetscErrorCode ierr;
25314e269d77SPeter Brune   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow;
2532efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
253387828ca2SBarry Smith   PetscScalar    *vscale_array;
2534ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2535ee4f033dSBarry Smith   Vec            w1,w2,w3;
2536ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
2537ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
2538ee4f033dSBarry Smith 
2539ee4f033dSBarry Smith   PetscFunctionBegin;
2540ee4f033dSBarry Smith   if (!coloring->w1) {
2541ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
254252e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2543ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
254452e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2545ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
254652e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2547ee4f033dSBarry Smith   }
2548ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2549ee4f033dSBarry Smith 
2550ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
2551acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2552ee4f033dSBarry Smith   if (flg) {
2553ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2554ee4f033dSBarry Smith   } else {
2555ace3abfcSBarry Smith     PetscBool  assembled;
25560b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
25570b9b6f31SBarry Smith     if (assembled) {
2558ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2559ee4f033dSBarry Smith     }
25600b9b6f31SBarry Smith   }
2561ee4f033dSBarry Smith 
2562ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2563ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2564ee4f033dSBarry Smith 
25654e269d77SPeter Brune   if (!coloring->fset) {
256666f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2567ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
256866f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
25694e269d77SPeter Brune   } else {
25704e269d77SPeter Brune     coloring->fset = PETSC_FALSE;
2571ee4f033dSBarry Smith   }
2572ee4f033dSBarry Smith 
2573ee4f033dSBarry Smith   /*
2574ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2575ee4f033dSBarry Smith   */
25761ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
25771ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2578ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2579ee4f033dSBarry Smith     /*
2580ee4f033dSBarry Smith        Loop over each column associated with color adding the
2581ee4f033dSBarry Smith        perturbation to the vector w3.
2582ee4f033dSBarry Smith     */
2583ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2584ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2585ee4f033dSBarry Smith       dx  = xx[col];
2586ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2587ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2588ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2589ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2590ee4f033dSBarry Smith #else
2591ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2592ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2593ee4f033dSBarry Smith #endif
2594ee4f033dSBarry Smith       dx                *= epsilon;
2595ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2596ee4f033dSBarry Smith     }
2597ee4f033dSBarry Smith   }
25981ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2599ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2600ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2601ee4f033dSBarry Smith 
2602ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2603ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2604ee4f033dSBarry Smith 
2605ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2606ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2607ee4f033dSBarry Smith 
26081ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2609ee4f033dSBarry Smith   /*
2610ee4f033dSBarry Smith       Loop over each color
2611ee4f033dSBarry Smith   */
2612ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
261349b058dcSBarry Smith     coloring->currentcolor = k;
2614ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
26151ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2616ee4f033dSBarry Smith     /*
2617ee4f033dSBarry Smith        Loop over each column associated with color adding the
2618ee4f033dSBarry Smith        perturbation to the vector w3.
2619ee4f033dSBarry Smith     */
2620ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2621ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2622ee4f033dSBarry Smith       dx  = xx[col];
26235b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2624ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2625ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2626ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2627ee4f033dSBarry Smith #else
2628ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2629ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2630ee4f033dSBarry Smith #endif
2631ee4f033dSBarry Smith       dx            *= epsilon;
2632e32f2f54SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2633ee4f033dSBarry Smith       w3_array[col] += dx;
2634ee4f033dSBarry Smith     }
26351ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2636ee4f033dSBarry Smith 
2637ee4f033dSBarry Smith     /*
2638ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2639ee4f033dSBarry Smith     */
2640ee4f033dSBarry Smith 
264166f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2642ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
264366f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2644efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2645ee4f033dSBarry Smith 
2646ee4f033dSBarry Smith     /*
2647ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2648ee4f033dSBarry Smith     */
26491ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2650ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2651ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2652ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2653ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2654ee4f033dSBarry Smith       srow   = row + start;
2655ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2656ee4f033dSBarry Smith     }
26571ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2658ee4f033dSBarry Smith   }
265949b058dcSBarry Smith   coloring->currentcolor = k;
26601ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
26611ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2662ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2663ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2664ee4f033dSBarry Smith   PetscFunctionReturn(0);
2665ee4f033dSBarry Smith }
2666ee4f033dSBarry Smith 
26678229c054SShri Abhyankar /*
26688229c054SShri Abhyankar    Computes the number of nonzeros per row needed for preallocation when X and Y
26698229c054SShri Abhyankar    have different nonzero structure.
26708229c054SShri Abhyankar */
2671ac90fabeSBarry Smith #undef __FUNCT__
26728229c054SShri Abhyankar #define __FUNCT__ "MatAXPYGetPreallocation_SeqAIJ"
26738229c054SShri Abhyankar PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt* nnz)
2674ec7775f6SShri Abhyankar {
26758229c054SShri Abhyankar   PetscInt          i,m=Y->rmap->N;
2676ec7775f6SShri Abhyankar   Mat_SeqAIJ        *x = (Mat_SeqAIJ*)X->data;
2677ec7775f6SShri Abhyankar   Mat_SeqAIJ        *y = (Mat_SeqAIJ*)Y->data;
2678ec7775f6SShri Abhyankar   const PetscInt    *xi = x->i,*yi = y->i;
2679ec7775f6SShri Abhyankar 
2680ec7775f6SShri Abhyankar   PetscFunctionBegin;
2681ec7775f6SShri Abhyankar   /* Set the number of nonzeros in the new matrix */
2682ec7775f6SShri Abhyankar   for(i=0; i<m; i++) {
26838af7cee1SJed Brown     PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i];
26848af7cee1SJed Brown     const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i];
26858af7cee1SJed Brown     nnz[i] = 0;
26868af7cee1SJed Brown     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
26878af7cee1SJed Brown       for (; k<nzy && yj[k]<xj[j]; k++) nnz[i]++; /* Catch up to X */
26888af7cee1SJed Brown       if (k<nzy && yj[k]==xj[j]) k++;             /* Skip duplicate */
26898af7cee1SJed Brown       nnz[i]++;
26908af7cee1SJed Brown     }
26918af7cee1SJed Brown     for (; k<nzy; k++) nnz[i]++;
2692ec7775f6SShri Abhyankar   }
2693ec7775f6SShri Abhyankar   PetscFunctionReturn(0);
2694ec7775f6SShri Abhyankar }
2695ec7775f6SShri Abhyankar 
2696ec7775f6SShri Abhyankar #undef __FUNCT__
2697ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2698f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2699ac90fabeSBarry Smith {
2700dfbe8321SBarry Smith   PetscErrorCode ierr;
270197f1f81fSBarry Smith   PetscInt       i;
2702ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
27030805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2704ac90fabeSBarry Smith 
2705ac90fabeSBarry Smith   PetscFunctionBegin;
2706ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2707f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2708f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
270986c113feSBarry Smith     y->idiagvalid  = PETSC_FALSE;
271086c113feSBarry Smith     y->ibdiagvalid = PETSC_FALSE;
2711c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2712a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2713a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
27146bf464f9SBarry Smith       ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr);
2715a30b2313SHong Zhang     }
2716a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2717d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2718a30b2313SHong Zhang       y->XtoY = X;
2719407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2720c537a176SHong Zhang     }
2721f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
2722ba0e910bSBarry Smith     ierr = PetscInfo3(Y,"ratio of nnz(X)/nnz(Y): %d/%d = %g\n",x->nz,y->nz,(double)(PetscReal)(x->nz)/(y->nz+1));CHKERRQ(ierr);
2723ac90fabeSBarry Smith   } else {
27248229c054SShri Abhyankar     Mat      B;
27258229c054SShri Abhyankar     PetscInt *nnz;
272616b2e9dcSShri Abhyankar     ierr = PetscMalloc(Y->rmap->N*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
2727ec7775f6SShri Abhyankar     ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr);
2728bc5a2726SShri Abhyankar     ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr);
27294aa94f47SShri Abhyankar     ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr);
2730a2f3521dSMark F. Adams     ierr = MatSetBlockSizes(B,Y->rmap->bs,Y->cmap->bs);CHKERRQ(ierr);
2731176df525SBarry Smith     ierr = MatSetType(B,(MatType) ((PetscObject)Y)->type_name);CHKERRQ(ierr);
27328229c054SShri Abhyankar     ierr = MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);CHKERRQ(ierr);
2733ecd8bba6SJed Brown     ierr = MatSeqAIJSetPreallocation(B,0,nnz);CHKERRQ(ierr);
2734ec7775f6SShri Abhyankar     ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr);
2735ec7775f6SShri Abhyankar     ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr);
27368229c054SShri Abhyankar     ierr = PetscFree(nnz);CHKERRQ(ierr);
2737ac90fabeSBarry Smith   }
2738ac90fabeSBarry Smith   PetscFunctionReturn(0);
2739ac90fabeSBarry Smith }
2740ac90fabeSBarry Smith 
2741521d7252SBarry Smith #undef __FUNCT__
2742354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
27437087cfbeSBarry Smith PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2744354c94deSBarry Smith {
2745354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2746354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2747354c94deSBarry Smith   PetscInt    i,nz;
2748354c94deSBarry Smith   PetscScalar *a;
2749354c94deSBarry Smith 
2750354c94deSBarry Smith   PetscFunctionBegin;
2751354c94deSBarry Smith   nz = aij->nz;
2752354c94deSBarry Smith   a  = aij->a;
2753354c94deSBarry Smith   for (i=0; i<nz; i++) {
2754354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2755354c94deSBarry Smith   }
2756354c94deSBarry Smith #else
2757354c94deSBarry Smith   PetscFunctionBegin;
2758354c94deSBarry Smith #endif
2759354c94deSBarry Smith   PetscFunctionReturn(0);
2760354c94deSBarry Smith }
2761354c94deSBarry Smith 
2762e34fafa9SBarry Smith #undef __FUNCT__
2763985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2764985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2765e34fafa9SBarry Smith {
2766e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2767e34fafa9SBarry Smith   PetscErrorCode ierr;
2768d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2769e34fafa9SBarry Smith   PetscReal      atmp;
2770985db425SBarry Smith   PetscScalar    *x;
2771e34fafa9SBarry Smith   MatScalar      *aa;
2772e34fafa9SBarry Smith 
2773e34fafa9SBarry Smith   PetscFunctionBegin;
2774e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2775e34fafa9SBarry Smith   aa   = a->a;
2776e34fafa9SBarry Smith   ai   = a->i;
2777e34fafa9SBarry Smith   aj   = a->j;
2778e34fafa9SBarry Smith 
2779985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2780e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2781e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2782e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2783e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2784e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
27859189402eSHong Zhang     x[i] = 0.0;
2786e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2787985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2788985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2789985db425SBarry Smith       aa++; aj++;
2790985db425SBarry Smith     }
2791985db425SBarry Smith   }
2792985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2793985db425SBarry Smith   PetscFunctionReturn(0);
2794985db425SBarry Smith }
2795985db425SBarry Smith 
2796985db425SBarry Smith #undef __FUNCT__
2797985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2798985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2799985db425SBarry Smith {
2800985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2801985db425SBarry Smith   PetscErrorCode ierr;
2802d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2803985db425SBarry Smith   PetscScalar    *x;
2804985db425SBarry Smith   MatScalar      *aa;
2805985db425SBarry Smith 
2806985db425SBarry Smith   PetscFunctionBegin;
2807e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2808985db425SBarry Smith   aa   = a->a;
2809985db425SBarry Smith   ai   = a->i;
2810985db425SBarry Smith   aj   = a->j;
2811985db425SBarry Smith 
2812985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2813985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2814985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2815e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2816985db425SBarry Smith   for (i=0; i<m; i++) {
2817985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2818d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2819985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2820985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2821985db425SBarry Smith       x[i] = 0.0;
2822985db425SBarry Smith       if (idx) {
2823985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2824985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2825985db425SBarry Smith           if (aj[j] > j) {
2826985db425SBarry Smith             idx[i] = j;
2827985db425SBarry Smith             break;
2828985db425SBarry Smith           }
2829985db425SBarry Smith         }
2830985db425SBarry Smith       }
2831985db425SBarry Smith     }
2832985db425SBarry Smith     for (j=0; j<ncols; j++){
2833985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2834985db425SBarry Smith       aa++; aj++;
2835985db425SBarry Smith     }
2836985db425SBarry Smith   }
2837985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2838985db425SBarry Smith   PetscFunctionReturn(0);
2839985db425SBarry Smith }
2840985db425SBarry Smith 
2841985db425SBarry Smith #undef __FUNCT__
2842c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2843c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2844c87e5d42SMatthew Knepley {
2845c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2846c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2847c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2848c87e5d42SMatthew Knepley   PetscReal      atmp;
2849c87e5d42SMatthew Knepley   PetscScalar    *x;
2850c87e5d42SMatthew Knepley   MatScalar      *aa;
2851c87e5d42SMatthew Knepley 
2852c87e5d42SMatthew Knepley   PetscFunctionBegin;
2853e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2854c87e5d42SMatthew Knepley   aa   = a->a;
2855c87e5d42SMatthew Knepley   ai   = a->i;
2856c87e5d42SMatthew Knepley   aj   = a->j;
2857c87e5d42SMatthew Knepley 
2858c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2859c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2860c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
28613bb78c5cSMatthew G Knepley   if (n != A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector, %d vs. %d rows", A->rmap->n, n);
2862c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2863c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2864289a08f5SMatthew Knepley     if (ncols) {
2865289a08f5SMatthew Knepley       /* Get first nonzero */
2866289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2867289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2868289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2869289a08f5SMatthew Knepley       }
287012431cb0SMatthew G Knepley       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
2871289a08f5SMatthew Knepley     } else {
2872289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2873289a08f5SMatthew Knepley     }
2874c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2875c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2876289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2877c87e5d42SMatthew Knepley       aa++; aj++;
2878c87e5d42SMatthew Knepley     }
2879c87e5d42SMatthew Knepley   }
2880c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2881c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2882c87e5d42SMatthew Knepley }
2883c87e5d42SMatthew Knepley 
2884c87e5d42SMatthew Knepley #undef __FUNCT__
2885985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2886985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2887985db425SBarry Smith {
2888985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2889985db425SBarry Smith   PetscErrorCode ierr;
2890d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2891985db425SBarry Smith   PetscScalar    *x;
2892985db425SBarry Smith   MatScalar      *aa;
2893985db425SBarry Smith 
2894985db425SBarry Smith   PetscFunctionBegin;
2895e32f2f54SBarry Smith   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2896985db425SBarry Smith   aa   = a->a;
2897985db425SBarry Smith   ai   = a->i;
2898985db425SBarry Smith   aj   = a->j;
2899985db425SBarry Smith 
2900985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2901985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2902985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2903e32f2f54SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2904985db425SBarry Smith   for (i=0; i<m; i++) {
2905985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2906d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2907985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2908985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2909985db425SBarry Smith       x[i] = 0.0;
2910985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2911985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2912985db425SBarry Smith         for (j=0;j<ncols;j++) {
2913985db425SBarry Smith           if (aj[j] > j) {
2914985db425SBarry Smith             idx[i] = j;
2915985db425SBarry Smith             break;
2916985db425SBarry Smith           }
2917985db425SBarry Smith         }
2918985db425SBarry Smith       }
2919985db425SBarry Smith     }
2920985db425SBarry Smith     for (j=0; j<ncols; j++){
2921985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2922985db425SBarry Smith       aa++; aj++;
2923e34fafa9SBarry Smith     }
2924e34fafa9SBarry Smith   }
2925e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2926e34fafa9SBarry Smith   PetscFunctionReturn(0);
2927e34fafa9SBarry Smith }
2928bbead8a2SBarry Smith 
2929bbead8a2SBarry Smith #include <petscblaslapack.h>
2930bbead8a2SBarry Smith #include <../src/mat/blockinvert.h>
2931bbead8a2SBarry Smith 
2932bbead8a2SBarry Smith #undef __FUNCT__
2933bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_SeqAIJ"
2934713ccfa9SJed Brown PetscErrorCode  MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
2935bbead8a2SBarry Smith {
2936bbead8a2SBarry Smith   Mat_SeqAIJ    *a = (Mat_SeqAIJ*) A->data;
2937bbead8a2SBarry Smith   PetscErrorCode ierr;
293834fc4b71SJed Brown   PetscInt       i,bs = A->rmap->bs,mbs = A->rmap->n/A->rmap->bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
2939bbead8a2SBarry Smith   MatScalar      *diag,work[25],*v_work;
2940bbead8a2SBarry Smith   PetscReal      shift = 0.0;
2941bbead8a2SBarry Smith 
2942bbead8a2SBarry Smith   PetscFunctionBegin;
29434a0d0026SBarry Smith   if (a->ibdiagvalid) {
29444a0d0026SBarry Smith     if (values) *values = a->ibdiag;
29454a0d0026SBarry Smith     PetscFunctionReturn(0);
29464a0d0026SBarry Smith   }
2947bbead8a2SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
2948bbead8a2SBarry Smith   if (!a->ibdiag) {
2949bbead8a2SBarry Smith     ierr = PetscMalloc(bs2*mbs*sizeof(PetscScalar),&a->ibdiag);CHKERRQ(ierr);
2950bbead8a2SBarry Smith     ierr = PetscLogObjectMemory(A,bs2*mbs*sizeof(PetscScalar));CHKERRQ(ierr);
2951bbead8a2SBarry Smith   }
2952bbead8a2SBarry Smith   diag    = a->ibdiag;
2953bbead8a2SBarry Smith   if (values) *values = a->ibdiag;
2954bbead8a2SBarry Smith   /* factor and invert each block */
2955bbead8a2SBarry Smith   switch (bs){
2956bbead8a2SBarry Smith     case 1:
2957bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2958bbead8a2SBarry Smith         ierr    = MatGetValues(A,1,&i,1,&i,diag+i);CHKERRQ(ierr);
2959bbead8a2SBarry Smith         diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
2960bbead8a2SBarry Smith       }
2961bbead8a2SBarry Smith       break;
2962bbead8a2SBarry Smith     case 2:
2963bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2964bbead8a2SBarry Smith         ij[0] = 2*i; ij[1] = 2*i + 1;
2965bbead8a2SBarry Smith         ierr  = MatGetValues(A,2,ij,2,ij,diag);CHKERRQ(ierr);
296696b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_2(diag,shift);CHKERRQ(ierr);
296796b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_2(diag);CHKERRQ(ierr);
2968bbead8a2SBarry Smith 	diag  += 4;
2969bbead8a2SBarry Smith       }
2970bbead8a2SBarry Smith       break;
2971bbead8a2SBarry Smith     case 3:
2972bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2973bbead8a2SBarry Smith         ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
2974bbead8a2SBarry Smith         ierr  = MatGetValues(A,3,ij,3,ij,diag);CHKERRQ(ierr);
297596b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_3(diag,shift);CHKERRQ(ierr);
297696b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_3(diag);CHKERRQ(ierr);
2977bbead8a2SBarry Smith 	diag    += 9;
2978bbead8a2SBarry Smith       }
2979bbead8a2SBarry Smith       break;
2980bbead8a2SBarry Smith     case 4:
2981bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2982bbead8a2SBarry Smith         ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
2983bbead8a2SBarry Smith         ierr  = MatGetValues(A,4,ij,4,ij,diag);CHKERRQ(ierr);
298496b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_4(diag,shift);CHKERRQ(ierr);
298596b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_4(diag);CHKERRQ(ierr);
2986bbead8a2SBarry Smith 	diag  += 16;
2987bbead8a2SBarry Smith       }
2988bbead8a2SBarry Smith       break;
2989bbead8a2SBarry Smith     case 5:
2990bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
2991bbead8a2SBarry Smith         ij[0] = 5*i; ij[1] = 5*i + 1; ij[2] = 5*i + 2; ij[3] = 5*i + 3; ij[4] = 5*i + 4;
2992bbead8a2SBarry Smith         ierr  = MatGetValues(A,5,ij,5,ij,diag);CHKERRQ(ierr);
299396b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift);CHKERRQ(ierr);
299496b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_5(diag);CHKERRQ(ierr);
2995bbead8a2SBarry Smith 	diag  += 25;
2996bbead8a2SBarry Smith       }
2997bbead8a2SBarry Smith       break;
2998bbead8a2SBarry Smith     case 6:
2999bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
3000bbead8a2SBarry Smith         ij[0] = 6*i; ij[1] = 6*i + 1; ij[2] = 6*i + 2; ij[3] = 6*i + 3; ij[4] = 6*i + 4; ij[5] = 6*i + 5;
3001bbead8a2SBarry Smith         ierr  = MatGetValues(A,6,ij,6,ij,diag);CHKERRQ(ierr);
300296b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_6(diag,shift);CHKERRQ(ierr);
300396b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_6(diag);CHKERRQ(ierr);
3004bbead8a2SBarry Smith 	diag  += 36;
3005bbead8a2SBarry Smith       }
3006bbead8a2SBarry Smith       break;
3007bbead8a2SBarry Smith     case 7:
3008bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
3009bbead8a2SBarry Smith         ij[0] = 7*i; ij[1] = 7*i + 1; ij[2] = 7*i + 2; ij[3] = 7*i + 3; ij[4] = 7*i + 4; ij[5] = 7*i + 5; ij[5] = 7*i + 6;
3010bbead8a2SBarry Smith         ierr  = MatGetValues(A,7,ij,7,ij,diag);CHKERRQ(ierr);
301196b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A_7(diag,shift);CHKERRQ(ierr);
301296b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_7(diag);CHKERRQ(ierr);
3013bbead8a2SBarry Smith 	diag  += 49;
3014bbead8a2SBarry Smith       }
3015bbead8a2SBarry Smith       break;
3016bbead8a2SBarry Smith     default:
3017bbead8a2SBarry Smith       ierr = PetscMalloc3(bs,MatScalar,&v_work,bs,PetscInt,&v_pivots,bs,PetscInt,&IJ);CHKERRQ(ierr);
3018bbead8a2SBarry Smith       for (i=0; i<mbs; i++) {
3019bbead8a2SBarry Smith         for (j=0; j<bs; j++) {
3020bbead8a2SBarry Smith           IJ[j] = bs*i + j;
3021bbead8a2SBarry Smith         }
3022bbead8a2SBarry Smith         ierr  = MatGetValues(A,bs,IJ,bs,IJ,diag);CHKERRQ(ierr);
302396b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work);CHKERRQ(ierr);
302496b95a6bSBarry Smith         ierr  = PetscKernel_A_gets_transpose_A_N(diag,bs);CHKERRQ(ierr);
3025bbead8a2SBarry Smith 	diag  += bs2;
3026bbead8a2SBarry Smith       }
3027bbead8a2SBarry Smith       ierr = PetscFree3(v_work,v_pivots,IJ);CHKERRQ(ierr);
3028bbead8a2SBarry Smith   }
3029bbead8a2SBarry Smith   a->ibdiagvalid = PETSC_TRUE;
3030bbead8a2SBarry Smith   PetscFunctionReturn(0);
3031bbead8a2SBarry Smith }
3032bbead8a2SBarry Smith 
3033*73a71a0fSBarry Smith #undef __FUNCT__
3034*73a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_SeqAIJ"
3035*73a71a0fSBarry Smith static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
3036*73a71a0fSBarry Smith {
3037*73a71a0fSBarry Smith   PetscErrorCode ierr;
3038*73a71a0fSBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
3039*73a71a0fSBarry Smith   PetscScalar    a;
3040*73a71a0fSBarry Smith   PetscInt       m,n,i,j,col;
3041*73a71a0fSBarry Smith 
3042*73a71a0fSBarry Smith   PetscFunctionBegin;
3043*73a71a0fSBarry Smith   if (!x->assembled) {
3044*73a71a0fSBarry Smith     ierr = MatGetSize(x,&m,&n);CHKERRQ(ierr);
3045*73a71a0fSBarry Smith     for (i=0; i<m; i++) {
3046*73a71a0fSBarry Smith       for (j=0; j<aij->imax[i]; j++) {
3047*73a71a0fSBarry Smith         ierr  = PetscRandomGetValue(rctx,&a);CHKERRQ(ierr);
3048*73a71a0fSBarry Smith         col   = (PetscInt)(n*PetscRealPart(a));
3049*73a71a0fSBarry Smith         ierr  = MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);CHKERRQ(ierr);
3050*73a71a0fSBarry Smith       }
3051*73a71a0fSBarry Smith     }
3052*73a71a0fSBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
3053*73a71a0fSBarry Smith   ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3054*73a71a0fSBarry Smith   ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3055*73a71a0fSBarry Smith   PetscFunctionReturn(0);
3056*73a71a0fSBarry Smith }
3057*73a71a0fSBarry Smith 
30587087cfbeSBarry Smith extern PetscErrorCode  MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
3059682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
30600a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
3061cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
3062cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
3063cb5b572fSBarry Smith        MatMult_SeqAIJ,
306497304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
30657c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
30667c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
3067db4efbfdSBarry Smith        0,
3068db4efbfdSBarry Smith        0,
3069db4efbfdSBarry Smith        0,
3070db4efbfdSBarry Smith /*10*/ 0,
3071cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
3072cb5b572fSBarry Smith        0,
307341f059aeSBarry Smith        MatSOR_SeqAIJ,
307417ab2063SBarry Smith        MatTranspose_SeqAIJ,
307597304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
3076cb5b572fSBarry Smith        MatEqual_SeqAIJ,
3077cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
3078cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
3079cb5b572fSBarry Smith        MatNorm_SeqAIJ,
308097304618SKris Buschelman /*20*/ 0,
3081cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
3082cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
3083cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
3084d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
3085db4efbfdSBarry Smith        0,
3086db4efbfdSBarry Smith        0,
3087db4efbfdSBarry Smith        0,
3088db4efbfdSBarry Smith        0,
30894994cf47SJed Brown /*29*/ MatSetUp_SeqAIJ,
3090db4efbfdSBarry Smith        0,
3091db4efbfdSBarry Smith        0,
30926c0721eeSBarry Smith        MatGetArray_SeqAIJ,
30936c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
3094d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
3095cb5b572fSBarry Smith        0,
3096cb5b572fSBarry Smith        0,
3097cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
3098cb5b572fSBarry Smith        0,
3099d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
3100cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
3101cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
3102cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
3103cb5b572fSBarry Smith        MatCopy_SeqAIJ,
3104d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
3105cb5b572fSBarry Smith        MatScale_SeqAIJ,
3106cb5b572fSBarry Smith        0,
310779299369SBarry Smith        MatDiagonalSet_SeqAIJ,
31086e169961SBarry Smith        MatZeroRowsColumns_SeqAIJ,
3109*73a71a0fSBarry Smith /*49*/ MatSetRandom_SeqAIJ,
31103b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
31113b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
31123b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
3113a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
3114d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
3115b9617806SBarry Smith        0,
31160513a670SBarry Smith        0,
3117cda55fadSBarry Smith        MatPermute_SeqAIJ,
3118cda55fadSBarry Smith        0,
3119d519adbfSMatthew Knepley /*59*/ 0,
3120b9b97703SBarry Smith        MatDestroy_SeqAIJ,
3121b9b97703SBarry Smith        MatView_SeqAIJ,
3122357abbc8SBarry Smith        0,
3123ee4f033dSBarry Smith        0,
3124d519adbfSMatthew Knepley /*64*/ 0,
3125ee4f033dSBarry Smith        0,
3126ee4f033dSBarry Smith        0,
3127ee4f033dSBarry Smith        0,
3128ee4f033dSBarry Smith        0,
3129d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
3130c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
3131ee4f033dSBarry Smith        0,
3132ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
3133dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3134ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
3135dcf5cc72SBarry Smith #else
3136dcf5cc72SBarry Smith        0,
3137dcf5cc72SBarry Smith #endif
3138d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
31393acb8795SBarry Smith        MatFDColoringApply_AIJ,
314097304618SKris Buschelman        0,
314197304618SKris Buschelman        0,
314297304618SKris Buschelman        0,
31436ce1633cSBarry Smith /*79*/ MatFindZeroDiagonals_SeqAIJ,
314497304618SKris Buschelman        0,
314597304618SKris Buschelman        0,
314697304618SKris Buschelman        0,
3147bc011b1eSHong Zhang        MatLoad_SeqAIJ,
3148d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
31491cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
31506284ec50SHong Zhang        0,
31516284ec50SHong Zhang        0,
3152bc011b1eSHong Zhang        0,
3153d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
315426be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
315526be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
3156d439da42SKris Buschelman        MatPtAP_Basic,
31577ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
3158d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
31596fc122caSHong Zhang        MatMatTransposeMult_SeqAIJ_SeqAIJ,
31606fc122caSHong Zhang        MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
31616fc122caSHong Zhang        MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
31627ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
3163d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
3164609c6c4dSKris Buschelman        0,
3165609c6c4dSKris Buschelman        0,
316687d4246cSBarry Smith        MatConjugate_SeqAIJ,
316787d4246cSBarry Smith        0,
3168d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
316999cafbc1SBarry Smith        MatRealPart_SeqAIJ,
3170f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
3171f5edf698SHong Zhang        0,
31722bebee5dSHong Zhang        0,
3173cbd44569SHong Zhang /*109*/MatMatSolve_SeqAIJ,
3174985db425SBarry Smith        0,
31752af78befSBarry Smith        MatGetRowMin_SeqAIJ,
31762af78befSBarry Smith        0,
3177599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
3178d519adbfSMatthew Knepley /*114*/0,
3179599ef60dSHong Zhang        0,
31803c2a7987SHong Zhang        0,
3181fe97e370SBarry Smith        0,
3182fbdbba38SShri Abhyankar        0,
3183fbdbba38SShri Abhyankar /*119*/0,
3184fbdbba38SShri Abhyankar        0,
3185fbdbba38SShri Abhyankar        0,
318682d44351SHong Zhang        0,
3187b3a44c85SBarry Smith        MatGetMultiProcBlock_SeqAIJ,
31880716a85fSBarry Smith /*124*/MatFindNonzeroRows_SeqAIJ,
3189bbead8a2SBarry Smith        MatGetColumnNorms_SeqAIJ,
319037868618SMatthew G Knepley        MatInvertBlockDiagonal_SeqAIJ,
319137868618SMatthew G Knepley        0,
319237868618SMatthew G Knepley        0,
31935df89d91SHong Zhang /*129*/0,
319475648e8dSHong Zhang        MatTransposeMatMult_SeqAIJ_SeqAIJ,
319575648e8dSHong Zhang        MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
319675648e8dSHong Zhang        MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3197b9af6bddSHong Zhang        MatTransposeColoringCreate_SeqAIJ,
3198b9af6bddSHong Zhang /*134*/MatTransColoringApplySpToDen_SeqAIJ,
31992b8ad9a3SHong Zhang        MatTransColoringApplyDenToSp_SeqAIJ,
32002b8ad9a3SHong Zhang        MatRARt_SeqAIJ_SeqAIJ,
32012b8ad9a3SHong Zhang        MatRARtSymbolic_SeqAIJ_SeqAIJ,
32022b8ad9a3SHong Zhang        MatRARtNumeric_SeqAIJ_SeqAIJ
32039e29f15eSvictorle };
320417ab2063SBarry Smith 
3205fb2e594dSBarry Smith EXTERN_C_BEGIN
32064a2ae208SSatish Balay #undef __FUNCT__
32074a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
32087087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3209bef8e0ddSBarry Smith {
3210bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
321197f1f81fSBarry Smith   PetscInt   i,nz,n;
3212bef8e0ddSBarry Smith 
3213bef8e0ddSBarry Smith   PetscFunctionBegin;
3214bef8e0ddSBarry Smith 
3215bef8e0ddSBarry Smith   nz = aij->maxnz;
3216d0f46423SBarry Smith   n  = mat->rmap->n;
3217bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
3218bef8e0ddSBarry Smith     aij->j[i] = indices[i];
3219bef8e0ddSBarry Smith   }
3220bef8e0ddSBarry Smith   aij->nz = nz;
3221bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
3222bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
3223bef8e0ddSBarry Smith   }
3224bef8e0ddSBarry Smith 
3225bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3226bef8e0ddSBarry Smith }
3227fb2e594dSBarry Smith EXTERN_C_END
3228bef8e0ddSBarry Smith 
32294a2ae208SSatish Balay #undef __FUNCT__
32304a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
3231bef8e0ddSBarry Smith /*@
3232bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3233bef8e0ddSBarry Smith        in the matrix.
3234bef8e0ddSBarry Smith 
3235bef8e0ddSBarry Smith   Input Parameters:
3236bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
3237bef8e0ddSBarry Smith -  indices - the column indices
3238bef8e0ddSBarry Smith 
323915091d37SBarry Smith   Level: advanced
324015091d37SBarry Smith 
3241bef8e0ddSBarry Smith   Notes:
3242bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
3243bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
3244bef8e0ddSBarry Smith   of the MatSetValues() operation.
3245bef8e0ddSBarry Smith 
3246bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
3247d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3248bef8e0ddSBarry Smith 
3249bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
3250bef8e0ddSBarry Smith 
3251b9617806SBarry Smith     The indices should start with zero, not one.
3252b9617806SBarry Smith 
3253bef8e0ddSBarry Smith @*/
32547087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3255bef8e0ddSBarry Smith {
32564ac538c5SBarry Smith   PetscErrorCode ierr;
3257bef8e0ddSBarry Smith 
3258bef8e0ddSBarry Smith   PetscFunctionBegin;
32590700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
32604482741eSBarry Smith   PetscValidPointer(indices,2);
32614ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt *),(mat,indices));CHKERRQ(ierr);
3262bef8e0ddSBarry Smith   PetscFunctionReturn(0);
3263bef8e0ddSBarry Smith }
3264bef8e0ddSBarry Smith 
3265be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
3266be6bf707SBarry Smith 
3267fb2e594dSBarry Smith EXTERN_C_BEGIN
32684a2ae208SSatish Balay #undef __FUNCT__
32694a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
32707087cfbeSBarry Smith PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3271be6bf707SBarry Smith {
3272be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
32736849ba73SBarry Smith   PetscErrorCode ierr;
3274d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
3275be6bf707SBarry Smith 
3276be6bf707SBarry Smith   PetscFunctionBegin;
3277be6bf707SBarry Smith   if (aij->nonew != 1) {
3278e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3279be6bf707SBarry Smith   }
3280be6bf707SBarry Smith 
3281be6bf707SBarry Smith   /* allocate space for values if not already there */
3282be6bf707SBarry Smith   if (!aij->saved_values) {
328387828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
32849518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
3285be6bf707SBarry Smith   }
3286be6bf707SBarry Smith 
3287be6bf707SBarry Smith   /* copy values over */
328887828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3289be6bf707SBarry Smith   PetscFunctionReturn(0);
3290be6bf707SBarry Smith }
3291fb2e594dSBarry Smith EXTERN_C_END
3292be6bf707SBarry Smith 
32934a2ae208SSatish Balay #undef __FUNCT__
3294b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
3295be6bf707SBarry Smith /*@
3296be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3297be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3298be6bf707SBarry Smith        nonlinear portion.
3299be6bf707SBarry Smith 
3300be6bf707SBarry Smith    Collect on Mat
3301be6bf707SBarry Smith 
3302be6bf707SBarry Smith   Input Parameters:
33030e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
3304be6bf707SBarry Smith 
330515091d37SBarry Smith   Level: advanced
330615091d37SBarry Smith 
3307be6bf707SBarry Smith   Common Usage, with SNESSolve():
3308be6bf707SBarry Smith $    Create Jacobian matrix
3309be6bf707SBarry Smith $    Set linear terms into matrix
3310be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
3311be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
3312be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
3313512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3314be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3315be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
3316be6bf707SBarry Smith $    In your Jacobian routine
3317be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
3318be6bf707SBarry Smith $      Set nonlinear terms in matrix
3319be6bf707SBarry Smith 
3320be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3321be6bf707SBarry Smith $    // build linear portion of Jacobian
3322512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3323be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
3324be6bf707SBarry Smith $    loop over nonlinear iterations
3325be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
3326be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3327be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
3328be6bf707SBarry Smith $       Solve linear system with Jacobian
3329be6bf707SBarry Smith $    endloop
3330be6bf707SBarry Smith 
3331be6bf707SBarry Smith   Notes:
3332be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
3333512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3334be6bf707SBarry Smith     calling this routine.
3335be6bf707SBarry Smith 
33360c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
33370c468ba9SBarry Smith     and does not allocated additional space.
33380c468ba9SBarry Smith 
3339be6bf707SBarry Smith .seealso: MatRetrieveValues()
3340be6bf707SBarry Smith 
3341be6bf707SBarry Smith @*/
33427087cfbeSBarry Smith PetscErrorCode  MatStoreValues(Mat mat)
3343be6bf707SBarry Smith {
33444ac538c5SBarry Smith   PetscErrorCode ierr;
3345be6bf707SBarry Smith 
3346be6bf707SBarry Smith   PetscFunctionBegin;
33470700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3348e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3349e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
33504ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));CHKERRQ(ierr);
3351be6bf707SBarry Smith   PetscFunctionReturn(0);
3352be6bf707SBarry Smith }
3353be6bf707SBarry Smith 
3354fb2e594dSBarry Smith EXTERN_C_BEGIN
33554a2ae208SSatish Balay #undef __FUNCT__
33564a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
33577087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3358be6bf707SBarry Smith {
3359be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
33606849ba73SBarry Smith   PetscErrorCode ierr;
3361d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
3362be6bf707SBarry Smith 
3363be6bf707SBarry Smith   PetscFunctionBegin;
3364be6bf707SBarry Smith   if (aij->nonew != 1) {
3365e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3366be6bf707SBarry Smith   }
3367be6bf707SBarry Smith   if (!aij->saved_values) {
3368e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3369be6bf707SBarry Smith   }
3370be6bf707SBarry Smith   /* copy values over */
337187828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
3372be6bf707SBarry Smith   PetscFunctionReturn(0);
3373be6bf707SBarry Smith }
3374fb2e594dSBarry Smith EXTERN_C_END
3375be6bf707SBarry Smith 
33764a2ae208SSatish Balay #undef __FUNCT__
33774a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
3378be6bf707SBarry Smith /*@
3379be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3380be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
3381be6bf707SBarry Smith        nonlinear portion.
3382be6bf707SBarry Smith 
3383be6bf707SBarry Smith    Collect on Mat
3384be6bf707SBarry Smith 
3385be6bf707SBarry Smith   Input Parameters:
3386be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
3387be6bf707SBarry Smith 
338815091d37SBarry Smith   Level: advanced
338915091d37SBarry Smith 
3390be6bf707SBarry Smith .seealso: MatStoreValues()
3391be6bf707SBarry Smith 
3392be6bf707SBarry Smith @*/
33937087cfbeSBarry Smith PetscErrorCode  MatRetrieveValues(Mat mat)
3394be6bf707SBarry Smith {
33954ac538c5SBarry Smith   PetscErrorCode ierr;
3396be6bf707SBarry Smith 
3397be6bf707SBarry Smith   PetscFunctionBegin;
33980700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
3399e32f2f54SBarry Smith   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3400e32f2f54SBarry Smith   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
34014ac538c5SBarry Smith   ierr = PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));CHKERRQ(ierr);
3402be6bf707SBarry Smith   PetscFunctionReturn(0);
3403be6bf707SBarry Smith }
3404be6bf707SBarry Smith 
3405f83d6046SBarry Smith 
3406be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
34074a2ae208SSatish Balay #undef __FUNCT__
34084a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
340917ab2063SBarry Smith /*@C
3410682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
34110d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
34126e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
341351c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
34142bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
341517ab2063SBarry Smith 
3416db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
3417db81eaa0SLois Curfman McInnes 
341817ab2063SBarry Smith    Input Parameters:
3419db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
342017ab2063SBarry Smith .  m - number of rows
342117ab2063SBarry Smith .  n - number of columns
342217ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
342351c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
34242bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
342517ab2063SBarry Smith 
342617ab2063SBarry Smith    Output Parameter:
3427416022c9SBarry Smith .  A - the matrix
342817ab2063SBarry Smith 
3429175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3430ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3431175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3432175b88e8SBarry Smith 
3433b259b22eSLois Curfman McInnes    Notes:
343449a6f317SBarry Smith    If nnz is given then nz is ignored
343549a6f317SBarry Smith 
343617ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
343717ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
34380002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
343944cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
344017ab2063SBarry Smith 
344117ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3442a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
34433d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
34446da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
344517ab2063SBarry Smith 
3446682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
34474fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
3448682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
34496c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
34506c7ebb05SLois Curfman McInnes 
34516c7ebb05SLois Curfman McInnes    Options Database Keys:
3452698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
34539db58ca8SBarry Smith -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
345417ab2063SBarry Smith 
3455027ccd11SLois Curfman McInnes    Level: intermediate
3456027ccd11SLois Curfman McInnes 
345769b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
345836db0b34SBarry Smith 
345917ab2063SBarry Smith @*/
34607087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
346117ab2063SBarry Smith {
3462dfbe8321SBarry Smith   PetscErrorCode ierr;
34636945ee14SBarry Smith 
34643a40ed3dSBarry Smith   PetscFunctionBegin;
3465f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
3466117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
3467c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
3468d28bb7d2SJed Brown   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);CHKERRQ(ierr);
3469273d9f13SBarry Smith   PetscFunctionReturn(0);
3470273d9f13SBarry Smith }
3471273d9f13SBarry Smith 
34724a2ae208SSatish Balay #undef __FUNCT__
34734a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
3474273d9f13SBarry Smith /*@C
3475273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
3476273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
3477273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
3478273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
3479273d9f13SBarry Smith 
3480273d9f13SBarry Smith    Collective on MPI_Comm
3481273d9f13SBarry Smith 
3482273d9f13SBarry Smith    Input Parameters:
3483117016b1SBarry Smith +  B - The matrix-free
3484273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
3485273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
3486273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
3487273d9f13SBarry Smith 
3488273d9f13SBarry Smith    Notes:
348949a6f317SBarry Smith      If nnz is given then nz is ignored
349049a6f317SBarry Smith 
3491273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
3492273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
3493273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
3494273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
3495273d9f13SBarry Smith 
3496273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
3497273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
3498273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
3499273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
3500273d9f13SBarry Smith 
3501aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
3502aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3503aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
3504aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
3505aa95bbe8SBarry Smith 
3506a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3507a96a251dSBarry Smith    entries or columns indices
3508a96a251dSBarry Smith 
3509273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
3510273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
3511273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
3512273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
3513273d9f13SBarry Smith 
3514273d9f13SBarry Smith    Options Database Keys:
3515698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
3516698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3517273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
3518273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
3519273d9f13SBarry Smith         the user still MUST index entries starting at 0!
3520273d9f13SBarry Smith 
3521273d9f13SBarry Smith    Level: intermediate
3522273d9f13SBarry Smith 
352369b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3524273d9f13SBarry Smith 
3525273d9f13SBarry Smith @*/
35267087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3527273d9f13SBarry Smith {
35284ac538c5SBarry Smith   PetscErrorCode ierr;
3529a23d5eceSKris Buschelman 
3530a23d5eceSKris Buschelman   PetscFunctionBegin;
35316ba663aaSJed Brown   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
35326ba663aaSJed Brown   PetscValidType(B,1);
35334ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));CHKERRQ(ierr);
3534a23d5eceSKris Buschelman   PetscFunctionReturn(0);
3535a23d5eceSKris Buschelman }
3536a23d5eceSKris Buschelman 
3537a23d5eceSKris Buschelman EXTERN_C_BEGIN
3538a23d5eceSKris Buschelman #undef __FUNCT__
3539a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
35407087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3541a23d5eceSKris Buschelman {
3542273d9f13SBarry Smith   Mat_SeqAIJ     *b;
35432576faa2SJed Brown   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
35446849ba73SBarry Smith   PetscErrorCode ierr;
354597f1f81fSBarry Smith   PetscInt       i;
3546273d9f13SBarry Smith 
3547273d9f13SBarry Smith   PetscFunctionBegin;
35482576faa2SJed Brown   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3549a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
3550c461c341SBarry Smith     skipallocation = PETSC_TRUE;
3551c461c341SBarry Smith     nz             = 0;
3552c461c341SBarry Smith   }
3553c461c341SBarry Smith 
355426283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
355526283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3556899cda47SBarry Smith 
3557435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3558e32f2f54SBarry Smith   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
3559b73539f3SBarry Smith   if (nnz) {
3560d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
3561e32f2f54SBarry 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]);
3562e32f2f54SBarry Smith       if (nnz[i] > B->cmap->n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->cmap->n);
3563b73539f3SBarry Smith     }
3564b73539f3SBarry Smith   }
3565b73539f3SBarry Smith 
3566273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
3567273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
3568273d9f13SBarry Smith 
3569ab93d7beSBarry Smith   if (!skipallocation) {
35702ee49352SLisandro Dalcin     if (!b->imax) {
3571d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
3572d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
35732ee49352SLisandro Dalcin     }
3574273d9f13SBarry Smith     if (!nnz) {
3575435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3576c62bd62aSJed Brown       else if (nz < 0) nz = 1;
3577d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3578d0f46423SBarry Smith       nz = nz*B->rmap->n;
3579273d9f13SBarry Smith     } else {
3580273d9f13SBarry Smith       nz = 0;
3581d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3582273d9f13SBarry Smith     }
3583ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
3584d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
3585ab93d7beSBarry Smith 
3586273d9f13SBarry Smith     /* allocate the matrix space */
35872ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
3588d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
3589d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
3590bfeeae90SHong Zhang     b->i[0] = 0;
3591d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
35925da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
35935da197adSKris Buschelman     }
3594273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
3595e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
3596e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3597b31eba2aSShri Abhyankar #if defined(PETSC_THREADCOMM_ACTIVE)
3598b31eba2aSShri Abhyankar   ierr = MatZeroEntries_SeqAIJ(B);CHKERRQ(ierr);
3599b31eba2aSShri Abhyankar #endif
3600c461c341SBarry Smith   } else {
3601e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
3602e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
3603c461c341SBarry Smith   }
3604273d9f13SBarry Smith 
3605273d9f13SBarry Smith   b->nz                = 0;
3606273d9f13SBarry Smith   b->maxnz             = nz;
3607273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
36082576faa2SJed Brown   if (realalloc) {ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);}
3609273d9f13SBarry Smith   PetscFunctionReturn(0);
3610273d9f13SBarry Smith }
3611a23d5eceSKris Buschelman EXTERN_C_END
3612273d9f13SBarry Smith 
3613a1661176SMatthew Knepley #undef  __FUNCT__
3614a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
361558d36128SBarry Smith /*@
3616a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3617a1661176SMatthew Knepley 
3618a1661176SMatthew Knepley    Input Parameters:
3619a1661176SMatthew Knepley +  B - the matrix
3620a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3621a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3622a1661176SMatthew Knepley -  v - optional values in the matrix
3623a1661176SMatthew Knepley 
3624a1661176SMatthew Knepley    Level: developer
3625a1661176SMatthew Knepley 
362658d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
362758d36128SBarry Smith 
3628a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3629a1661176SMatthew Knepley 
3630a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3631a1661176SMatthew Knepley @*/
3632a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3633a1661176SMatthew Knepley {
3634a1661176SMatthew Knepley   PetscErrorCode ierr;
3635a1661176SMatthew Knepley 
3636a1661176SMatthew Knepley   PetscFunctionBegin;
36370700a824SBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,1);
36386ba663aaSJed Brown   PetscValidType(B,1);
36394ac538c5SBarry Smith   ierr = PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr);
3640a1661176SMatthew Knepley   PetscFunctionReturn(0);
3641a1661176SMatthew Knepley }
3642a1661176SMatthew Knepley 
3643a1661176SMatthew Knepley EXTERN_C_BEGIN
3644a1661176SMatthew Knepley #undef  __FUNCT__
3645a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
36467087cfbeSBarry Smith PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3647a1661176SMatthew Knepley {
3648a1661176SMatthew Knepley   PetscInt       i;
3649a1661176SMatthew Knepley   PetscInt       m,n;
3650a1661176SMatthew Knepley   PetscInt       nz;
3651a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3652a1661176SMatthew Knepley   PetscScalar    *values;
3653a1661176SMatthew Knepley   PetscErrorCode ierr;
3654a1661176SMatthew Knepley 
3655a1661176SMatthew Knepley   PetscFunctionBegin;
365665e19b50SBarry Smith   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3657779a8d59SSatish Balay 
3658779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
3659779a8d59SSatish Balay   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
3660779a8d59SSatish Balay 
3661779a8d59SSatish Balay   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3662a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3663a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3664b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3665a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
366665e19b50SBarry Smith     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3667a1661176SMatthew Knepley     nnz[i] = nz;
3668a1661176SMatthew Knepley   }
3669a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3670a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3671a1661176SMatthew Knepley 
3672a1661176SMatthew Knepley   if (v) {
3673a1661176SMatthew Knepley     values = (PetscScalar*) v;
3674a1661176SMatthew Knepley   } else {
36750e83c824SBarry Smith     ierr = PetscMalloc(nz_max*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3676a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3677a1661176SMatthew Knepley   }
3678a1661176SMatthew Knepley 
3679a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3680b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3681b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3682a1661176SMatthew Knepley   }
3683a1661176SMatthew Knepley 
3684a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3685a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3686a1661176SMatthew Knepley 
3687a1661176SMatthew Knepley   if (!v) {
3688a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3689a1661176SMatthew Knepley   }
36907827cd58SJed Brown   ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3691a1661176SMatthew Knepley   PetscFunctionReturn(0);
3692a1661176SMatthew Knepley }
3693a1661176SMatthew Knepley EXTERN_C_END
3694a1661176SMatthew Knepley 
3695c6db04a5SJed Brown #include <../src/mat/impls/dense/seq/dense.h>
3696b45d2f2cSJed Brown #include <petsc-private/petscaxpy.h>
3697170fe5c8SBarry Smith 
3698170fe5c8SBarry Smith #undef __FUNCT__
3699170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3700170fe5c8SBarry Smith /*
3701170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3702170fe5c8SBarry Smith 
3703170fe5c8SBarry Smith                n                       p                          p
3704170fe5c8SBarry Smith         (              )       (              )         (                  )
3705170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3706170fe5c8SBarry Smith         (              )       (              )         (                  )
3707170fe5c8SBarry Smith 
3708170fe5c8SBarry Smith */
3709170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3710170fe5c8SBarry Smith {
3711170fe5c8SBarry Smith   PetscErrorCode     ierr;
3712170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3713170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3714170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
37151de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3716170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3717170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3718170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3719170fe5c8SBarry Smith 
3720170fe5c8SBarry Smith   PetscFunctionBegin;
3721d0f46423SBarry Smith   m = A->rmap->n;
3722d0f46423SBarry Smith   n = A->cmap->n;
3723d0f46423SBarry Smith   p = B->cmap->n;
3724170fe5c8SBarry Smith   a = sub_a->v;
3725170fe5c8SBarry Smith   b = sub_b->a;
3726170fe5c8SBarry Smith   c = sub_c->v;
3727170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3728170fe5c8SBarry Smith 
3729170fe5c8SBarry Smith   ii  = sub_b->i;
3730170fe5c8SBarry Smith   idx = sub_b->j;
3731170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3732170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3733170fe5c8SBarry Smith     while (q-->0) {
3734170fe5c8SBarry Smith       c_q = c + m*(*idx);
3735170fe5c8SBarry Smith       a_q = a + m*i;
3736be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3737170fe5c8SBarry Smith       idx++;
3738170fe5c8SBarry Smith       b++;
3739170fe5c8SBarry Smith     }
3740170fe5c8SBarry Smith   }
3741170fe5c8SBarry Smith   PetscFunctionReturn(0);
3742170fe5c8SBarry Smith }
3743170fe5c8SBarry Smith 
3744170fe5c8SBarry Smith #undef __FUNCT__
3745170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3746170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3747170fe5c8SBarry Smith {
3748170fe5c8SBarry Smith   PetscErrorCode ierr;
3749d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3750170fe5c8SBarry Smith   Mat            Cmat;
3751170fe5c8SBarry Smith 
3752170fe5c8SBarry Smith   PetscFunctionBegin;
3753e32f2f54SBarry Smith   if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"A->cmap->n %d != B->rmap->n %d\n",A->cmap->n,B->rmap->n);
375439804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3755170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3756a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(Cmat,A->rmap->bs,B->cmap->bs);CHKERRQ(ierr);
3757170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3758170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3759170fe5c8SBarry Smith   Cmat->assembled    = PETSC_TRUE;
37608cdbd757SHong Zhang   Cmat->ops->matmult = MatMatMult_SeqDense_SeqAIJ;
3761170fe5c8SBarry Smith   *C = Cmat;
3762170fe5c8SBarry Smith   PetscFunctionReturn(0);
3763170fe5c8SBarry Smith }
3764170fe5c8SBarry Smith 
3765170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3766170fe5c8SBarry Smith #undef __FUNCT__
3767170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3768170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3769170fe5c8SBarry Smith {
3770170fe5c8SBarry Smith   PetscErrorCode ierr;
3771170fe5c8SBarry Smith 
3772170fe5c8SBarry Smith   PetscFunctionBegin;
3773170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3774170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3775170fe5c8SBarry Smith   }
3776170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3777170fe5c8SBarry Smith   PetscFunctionReturn(0);
3778170fe5c8SBarry Smith }
3779170fe5c8SBarry Smith 
3780170fe5c8SBarry Smith 
37810bad9183SKris Buschelman /*MC
3782fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
37830bad9183SKris Buschelman    based on compressed sparse row format.
37840bad9183SKris Buschelman 
37850bad9183SKris Buschelman    Options Database Keys:
37860bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
37870bad9183SKris Buschelman 
37880bad9183SKris Buschelman   Level: beginner
37890bad9183SKris Buschelman 
3790f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
37910bad9183SKris Buschelman M*/
37920bad9183SKris Buschelman 
3793ccd284c7SBarry Smith /*MC
3794ccd284c7SBarry Smith    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
3795ccd284c7SBarry Smith 
3796ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
3797ccd284c7SBarry Smith    and MATMPIAIJ otherwise.  As a result, for single process communicators,
3798ccd284c7SBarry Smith   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
3799ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3800ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3801ccd284c7SBarry Smith 
3802ccd284c7SBarry Smith    Options Database Keys:
3803ccd284c7SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
3804ccd284c7SBarry Smith 
3805ccd284c7SBarry Smith   Developer Notes: Subclasses include MATAIJCUSP, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when
3806ccd284c7SBarry Smith    enough exist.
3807ccd284c7SBarry Smith 
3808ccd284c7SBarry Smith   Level: beginner
3809ccd284c7SBarry Smith 
3810ccd284c7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
3811ccd284c7SBarry Smith M*/
3812ccd284c7SBarry Smith 
3813ccd284c7SBarry Smith /*MC
3814ccd284c7SBarry Smith    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
3815ccd284c7SBarry Smith 
3816ccd284c7SBarry Smith    This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
3817ccd284c7SBarry Smith    and MATMPIAIJCRL otherwise.  As a result, for single process communicators,
3818ccd284c7SBarry Smith    MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
3819ccd284c7SBarry Smith   for communicators controlling multiple processes.  It is recommended that you call both of
3820ccd284c7SBarry Smith   the above preallocation routines for simplicity.
3821ccd284c7SBarry Smith 
3822ccd284c7SBarry Smith    Options Database Keys:
3823ccd284c7SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
3824ccd284c7SBarry Smith 
3825ccd284c7SBarry Smith   Level: beginner
3826ccd284c7SBarry Smith 
3827ccd284c7SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
3828ccd284c7SBarry Smith M*/
3829ccd284c7SBarry Smith 
3830a6175056SHong Zhang EXTERN_C_BEGIN
3831b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3832b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3833b5e56a35SBarry Smith #endif
3834ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3835af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3836af1023dbSSatish Balay #endif
38377087cfbeSBarry Smith extern PetscErrorCode  MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
38387087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
38397087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
38407087cfbeSBarry Smith extern PetscErrorCode  MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool  *);
3841611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
38427087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3843611f576cSBarry Smith #endif
3844611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
38457087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3846611f576cSBarry Smith #endif
3847f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3848f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3849f3c0ef26SHong Zhang #endif
3850611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
38517087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3852611f576cSBarry Smith #endif
3853eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
38547087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3855eb3b5408SSatish Balay #endif
3856586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
38577087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3858586621ddSJed Brown #endif
3859719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
38607087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3861719d5645SBarry Smith #endif
3862b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
38637087cfbeSBarry Smith extern PetscErrorCode  MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
38647087cfbeSBarry Smith extern PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
38657087cfbeSBarry Smith extern PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3866b3866ffcSBarry Smith #endif
386717f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
386817f1a0eaSHong Zhang extern PetscErrorCode MatGetFactor_aij_clique(Mat,MatFactorType,Mat*);
386917f1a0eaSHong Zhang #endif
387017667f90SBarry Smith EXTERN_C_END
387117667f90SBarry Smith 
3872c0c8ee5eSDmitry Karpeev 
387317667f90SBarry Smith EXTERN_C_BEGIN
38744a2ae208SSatish Balay #undef __FUNCT__
38754a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
38767087cfbeSBarry Smith PetscErrorCode  MatCreate_SeqAIJ(Mat B)
3877273d9f13SBarry Smith {
3878273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3879dfbe8321SBarry Smith   PetscErrorCode ierr;
388038baddfdSBarry Smith   PetscMPIInt    size;
3881273d9f13SBarry Smith 
3882273d9f13SBarry Smith   PetscFunctionBegin;
38837adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3884e32f2f54SBarry Smith   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3885273d9f13SBarry Smith 
388638f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3887b0a32e0cSBarry Smith   B->data             = (void*)b;
3888549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
3889416022c9SBarry Smith   b->row              = 0;
3890416022c9SBarry Smith   b->col              = 0;
389182bf6240SBarry Smith   b->icol             = 0;
3892b810aeb4SBarry Smith   b->reallocs         = 0;
389336db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3894f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3895416022c9SBarry Smith   b->nonew             = 0;
3896416022c9SBarry Smith   b->diag              = 0;
3897416022c9SBarry Smith   b->solve_work        = 0;
38982a1b7f2aSHong Zhang   B->spptr             = 0;
3899be6bf707SBarry Smith   b->saved_values      = 0;
3900d7f994e1SBarry Smith   b->idiag             = 0;
390171f1c65dSBarry Smith   b->mdiag             = 0;
390271f1c65dSBarry Smith   b->ssor_work         = 0;
390371f1c65dSBarry Smith   b->omega             = 1.0;
390471f1c65dSBarry Smith   b->fshift            = 0.0;
390571f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3906bbead8a2SBarry Smith   b->ibdiagvalid       = PETSC_FALSE;
3907a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3908a30b2313SHong Zhang   b->xtoy              = 0;
3909a30b2313SHong Zhang   b->XtoY              = 0;
391088e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
391117ab2063SBarry Smith 
391235d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3913b3866ffcSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
3914700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_matlab_C","MatGetFactor_seqaij_matlab",MatGetFactor_seqaij_matlab);CHKERRQ(ierr);
3915b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatlabEnginePut_SeqAIJ",MatlabEnginePut_SeqAIJ);CHKERRQ(ierr);
3916b3866ffcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatlabEngineGet_SeqAIJ",MatlabEngineGet_SeqAIJ);CHKERRQ(ierr);
3917b3866ffcSBarry Smith #endif
3918b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3919700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C","MatGetFactor_seqaij_pastix",MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3920b5e56a35SBarry Smith #endif
3921ce63c4c1SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3922700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C","MatGetFactor_seqaij_essl",MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3923719d5645SBarry Smith #endif
3924611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3925700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C","MatGetFactor_seqaij_superlu",MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3926611f576cSBarry Smith #endif
3927f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3928700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C","MatGetFactor_seqaij_superlu_dist",MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3929f3c0ef26SHong Zhang #endif
3930611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
3931700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C","MatGetFactor_seqaij_spooles",MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3932611f576cSBarry Smith #endif
3933611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3934700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C","MatGetFactor_aij_mumps",MatGetFactor_aij_mumps);CHKERRQ(ierr);
3935611f576cSBarry Smith #endif
3936eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3937700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C","MatGetFactor_seqaij_umfpack",MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3938eb3b5408SSatish Balay #endif
3939586621ddSJed Brown #if defined(PETSC_HAVE_CHOLMOD)
3940700c5bfcSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_cholmod_C","MatGetFactor_seqaij_cholmod",MatGetFactor_seqaij_cholmod);CHKERRQ(ierr);
3941586621ddSJed Brown #endif
3942719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
394317f1a0eaSHong Zhang     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C","MatGetFactor_aij_lusol",MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3944719d5645SBarry Smith #endif
394517f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE)
394617f1a0eaSHong Zhang     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_clique_C","MatGetFactor_aij_clique",MatGetFactor_aij_clique);CHKERRQ(ierr);
394717f1a0eaSHong Zhang #endif
394817f1a0eaSHong Zhang 
3949700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C","MatGetFactor_seqaij_petsc",MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3950700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C","MatGetFactorAvailable_seqaij_petsc",MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3951700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_bas_C","MatGetFactor_seqaij_bas",MatGetFactor_seqaij_bas);CHKERRQ(ierr);
3952700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C","MatSeqAIJSetColumnIndices_SeqAIJ",MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3953700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C","MatStoreValues_SeqAIJ",MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3954700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C","MatRetrieveValues_SeqAIJ",MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3955700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C","MatConvert_SeqAIJ_SeqSBAIJ",MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
3956700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C","MatConvert_SeqAIJ_SeqBAIJ",MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3957700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijperm_C","MatConvert_SeqAIJ_SeqAIJPERM",MatConvert_SeqAIJ_SeqAIJPERM);CHKERRQ(ierr);
3958700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C","MatConvert_SeqAIJ_SeqAIJCRL",MatConvert_SeqAIJ_SeqAIJCRL);CHKERRQ(ierr);
3959700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C","MatIsTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3960700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C","MatIsHermitianTranspose_SeqAIJ",MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3961700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C","MatSeqAIJSetPreallocation_SeqAIJ",MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3962700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C","MatSeqAIJSetPreallocationCSR_SeqAIJ",MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
3963700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C","MatReorderForNonzeroDiagonal_SeqAIJ",MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3964700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C","MatMatMult_SeqDense_SeqAIJ",MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3965700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C","MatMatMultSymbolic_SeqDense_SeqAIJ",MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3966700c5bfcSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C","MatMatMultNumeric_SeqDense_SeqAIJ",MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
39674108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
396817667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
39693a40ed3dSBarry Smith   PetscFunctionReturn(0);
397017ab2063SBarry Smith }
3971273d9f13SBarry Smith EXTERN_C_END
397217ab2063SBarry Smith 
39734a2ae208SSatish Balay #undef __FUNCT__
3974b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3975b24902e0SBarry Smith /*
3976b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3977b24902e0SBarry Smith */
3978ace3abfcSBarry Smith PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool  mallocmatspace)
397917ab2063SBarry Smith {
3980416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
39816849ba73SBarry Smith   PetscErrorCode ierr;
3982d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
398317ab2063SBarry Smith 
39843a40ed3dSBarry Smith   PetscFunctionBegin;
3985273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3986273d9f13SBarry Smith 
3987d5f3da31SBarry Smith   C->factortype     = A->factortype;
3988416022c9SBarry Smith   c->row            = 0;
3989416022c9SBarry Smith   c->col            = 0;
399082bf6240SBarry Smith   c->icol           = 0;
39916ad4291fSHong Zhang   c->reallocs       = 0;
399217ab2063SBarry Smith 
39936ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
399417ab2063SBarry Smith 
3995aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->rmap,&C->rmap);CHKERRQ(ierr);
3996aa5ea44dSBarry Smith   ierr = PetscLayoutReference(A->cmap,&C->cmap);CHKERRQ(ierr);
3997eec197d1SBarry Smith 
399833b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
39999518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
400017ab2063SBarry Smith   for (i=0; i<m; i++) {
4001416022c9SBarry Smith     c->imax[i] = a->imax[i];
4002416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
400317ab2063SBarry Smith   }
400417ab2063SBarry Smith 
400517ab2063SBarry Smith   /* allocate the matrix space */
4006f77e22a1SHong Zhang   if (mallocmatspace){
4007a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
40089518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
4009f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
401097f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
401117ab2063SBarry Smith     if (m > 0) {
401297f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
4013be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
4014bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
4015be6bf707SBarry Smith       } else {
4016bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
401717ab2063SBarry Smith       }
401808480c60SBarry Smith     }
4019f77e22a1SHong Zhang   }
402017ab2063SBarry Smith 
40216ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
4022416022c9SBarry Smith   c->roworiented       = a->roworiented;
4023416022c9SBarry Smith   c->nonew             = a->nonew;
4024416022c9SBarry Smith   if (a->diag) {
402597f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
402652e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
402717ab2063SBarry Smith     for (i=0; i<m; i++) {
4028416022c9SBarry Smith       c->diag[i] = a->diag[i];
402917ab2063SBarry Smith     }
40303a40ed3dSBarry Smith   } else c->diag           = 0;
40316ad4291fSHong Zhang   c->solve_work            = 0;
40326ad4291fSHong Zhang   c->saved_values          = 0;
40336ad4291fSHong Zhang   c->idiag                 = 0;
403471f1c65dSBarry Smith   c->ssor_work             = 0;
4035a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
4036e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
4037e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
40386ad4291fSHong Zhang   c->xtoy                  = 0;
40396ad4291fSHong Zhang   c->XtoY                  = 0;
40406ad4291fSHong Zhang 
4041893ad86cSHong Zhang   c->rmax               = a->rmax;
4042416022c9SBarry Smith   c->nz                 = a->nz;
40438ed568f8SMatthew G Knepley   c->maxnz              = a->nz; /* Since we allocate exactly the right amount */
4044273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
4045754ec7b1SSatish Balay 
40466ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
40476ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
4048cd6b891eSBarry Smith   c->compressedrow.check   = a->compressedrow.check;
4049cd6b891eSBarry Smith   if (a->compressedrow.use){
40506ad4291fSHong Zhang     i = a->compressedrow.nrows;
40510e83c824SBarry Smith     ierr = PetscMalloc2(i+1,PetscInt,&c->compressedrow.i,i,PetscInt,&c->compressedrow.rindex);CHKERRQ(ierr);
40526ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
40536ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
405427ea64f8SHong Zhang   } else {
405527ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
405627ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
405727ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
40586ad4291fSHong Zhang   }
405988e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
40604108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
40614846f1f5SKris Buschelman 
40627adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
40633a40ed3dSBarry Smith   PetscFunctionReturn(0);
406417ab2063SBarry Smith }
406517ab2063SBarry Smith 
40664a2ae208SSatish Balay #undef __FUNCT__
4067b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
4068b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4069b24902e0SBarry Smith {
4070b24902e0SBarry Smith   PetscErrorCode ierr;
4071b24902e0SBarry Smith 
4072b24902e0SBarry Smith   PetscFunctionBegin;
4073b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
40744b6263acSBarry Smith   ierr = MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
4075a2f3521dSMark F. Adams   ierr = MatSetBlockSizes(*B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr);
4076a54f2f98SBarry Smith   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
4077f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
4078b24902e0SBarry Smith   PetscFunctionReturn(0);
4079b24902e0SBarry Smith }
4080b24902e0SBarry Smith 
4081b24902e0SBarry Smith #undef __FUNCT__
40824a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
4083112444f4SShri Abhyankar PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4084fbdbba38SShri Abhyankar {
4085fbdbba38SShri Abhyankar   Mat_SeqAIJ     *a;
4086fbdbba38SShri Abhyankar   PetscErrorCode ierr;
4087fbdbba38SShri Abhyankar   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4088fbdbba38SShri Abhyankar   int            fd;
4089fbdbba38SShri Abhyankar   PetscMPIInt    size;
4090fbdbba38SShri Abhyankar   MPI_Comm       comm;
4091bbead8a2SBarry Smith   PetscInt       bs = 1;
4092fbdbba38SShri Abhyankar 
4093fbdbba38SShri Abhyankar   PetscFunctionBegin;
4094fbdbba38SShri Abhyankar   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
4095fbdbba38SShri Abhyankar   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
4096fbdbba38SShri Abhyankar   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");
4097bbead8a2SBarry Smith 
4098bbead8a2SBarry Smith   ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr);
4099bbead8a2SBarry Smith   ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr);
4100bbead8a2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4101bbead8a2SBarry Smith 
4102fbdbba38SShri Abhyankar   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
4103fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
4104fbdbba38SShri Abhyankar   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4105fbdbba38SShri Abhyankar   M = header[1]; N = header[2]; nz = header[3];
4106fbdbba38SShri Abhyankar 
4107bbead8a2SBarry Smith   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
4108fbdbba38SShri Abhyankar 
4109fbdbba38SShri Abhyankar   /* read in row lengths */
4110fbdbba38SShri Abhyankar   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
4111fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
4112fbdbba38SShri Abhyankar 
4113fbdbba38SShri Abhyankar   /* check if sum of rowlengths is same as nz */
4114fbdbba38SShri Abhyankar   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
4115fbdbba38SShri Abhyankar   if (sum != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
4116fbdbba38SShri Abhyankar 
4117fbdbba38SShri Abhyankar   /* set global size if not set already*/
4118f501eaabSShri Abhyankar   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4119fbdbba38SShri Abhyankar     ierr = MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
4120aabbc4fbSShri Abhyankar   } else {
4121fbdbba38SShri Abhyankar     /* if sizes and type are already set, check if the vector global sizes are correct */
4122fbdbba38SShri Abhyankar     ierr = MatGetSize(newMat,&rows,&cols);CHKERRQ(ierr);
41234c5b953cSHong Zhang     if (rows < 0 && cols < 0){ /* user might provide local size instead of global size */
41244c5b953cSHong Zhang       ierr = MatGetLocalSize(newMat,&rows,&cols);CHKERRQ(ierr);
41254c5b953cSHong Zhang     }
4126f501eaabSShri 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);
4127aabbc4fbSShri Abhyankar   }
4128fbdbba38SShri Abhyankar   ierr = MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);CHKERRQ(ierr);
4129fbdbba38SShri Abhyankar   a = (Mat_SeqAIJ*)newMat->data;
4130fbdbba38SShri Abhyankar 
4131fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
4132fbdbba38SShri Abhyankar 
4133fbdbba38SShri Abhyankar   /* read in nonzero values */
4134fbdbba38SShri Abhyankar   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
4135fbdbba38SShri Abhyankar 
4136fbdbba38SShri Abhyankar   /* set matrix "i" values */
4137fbdbba38SShri Abhyankar   a->i[0] = 0;
4138fbdbba38SShri Abhyankar   for (i=1; i<= M; i++) {
4139fbdbba38SShri Abhyankar     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4140fbdbba38SShri Abhyankar     a->ilen[i-1] = rowlengths[i-1];
4141fbdbba38SShri Abhyankar   }
4142fbdbba38SShri Abhyankar   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
4143fbdbba38SShri Abhyankar 
4144fbdbba38SShri Abhyankar   ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4145fbdbba38SShri Abhyankar   ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4146bbead8a2SBarry Smith   if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);}
4147fbdbba38SShri Abhyankar   PetscFunctionReturn(0);
4148fbdbba38SShri Abhyankar }
4149fbdbba38SShri Abhyankar 
4150fbdbba38SShri Abhyankar #undef __FUNCT__
4151b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
4152ace3abfcSBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
41537264ac53SSatish Balay {
41547264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
4155dfbe8321SBarry Smith   PetscErrorCode ierr;
4156eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4157eeffb40dSHong Zhang   PetscInt k;
4158eeffb40dSHong Zhang #endif
41597264ac53SSatish Balay 
41603a40ed3dSBarry Smith   PetscFunctionBegin;
4161bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
4162d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4163ca44d042SBarry Smith     *flg = PETSC_FALSE;
4164ca44d042SBarry Smith     PetscFunctionReturn(0);
4165bcd2baecSBarry Smith   }
41667264ac53SSatish Balay 
41677264ac53SSatish Balay   /* if the a->i are the same */
4168d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4169abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
41707264ac53SSatish Balay 
41717264ac53SSatish Balay   /* if a->j are the same */
417297f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
4173abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
4174bcd2baecSBarry Smith 
4175bcd2baecSBarry Smith   /* if a->a are the same */
4176eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
4177eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
4178eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
4179eeffb40dSHong Zhang       *flg = PETSC_FALSE;
41803a40ed3dSBarry Smith       PetscFunctionReturn(0);
4181eeffb40dSHong Zhang     }
4182eeffb40dSHong Zhang   }
4183eeffb40dSHong Zhang #else
4184eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
4185eeffb40dSHong Zhang #endif
4186eeffb40dSHong Zhang   PetscFunctionReturn(0);
41877264ac53SSatish Balay }
418836db0b34SBarry Smith 
41894a2ae208SSatish Balay #undef __FUNCT__
41904a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
419105869f15SSatish Balay /*@
419236db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
419336db0b34SBarry Smith               provided by the user.
419436db0b34SBarry Smith 
4195c75a6043SHong Zhang       Collective on MPI_Comm
419636db0b34SBarry Smith 
419736db0b34SBarry Smith    Input Parameters:
419836db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
419936db0b34SBarry Smith .   m - number of rows
420036db0b34SBarry Smith .   n - number of columns
420136db0b34SBarry Smith .   i - row indices
420236db0b34SBarry Smith .   j - column indices
420336db0b34SBarry Smith -   a - matrix values
420436db0b34SBarry Smith 
420536db0b34SBarry Smith    Output Parameter:
420636db0b34SBarry Smith .   mat - the matrix
420736db0b34SBarry Smith 
420836db0b34SBarry Smith    Level: intermediate
420936db0b34SBarry Smith 
421036db0b34SBarry Smith    Notes:
42110551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4212292fb18eSBarry Smith     once the matrix is destroyed and not before
421336db0b34SBarry Smith 
421436db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
421536db0b34SBarry Smith 
4216bfeeae90SHong Zhang        The i and j indices are 0 based
421736db0b34SBarry Smith 
4218a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
4219a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
4220a4552177SSatish Balay     as shown:
4221a4552177SSatish Balay 
4222a4552177SSatish Balay         1 0 0
4223a4552177SSatish Balay         2 0 3
4224a4552177SSatish Balay         4 5 6
4225a4552177SSatish Balay 
4226a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
42279985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
4228a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
4229a4552177SSatish Balay 
42309985e31cSBarry Smith 
423169b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
423236db0b34SBarry Smith 
423336db0b34SBarry Smith @*/
42347087cfbeSBarry Smith PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
423536db0b34SBarry Smith {
4236dfbe8321SBarry Smith   PetscErrorCode ierr;
4237cbcfb4deSHong Zhang   PetscInt       ii;
423836db0b34SBarry Smith   Mat_SeqAIJ     *aij;
4239cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
4240cbcfb4deSHong Zhang   PetscInt       jj;
4241cbcfb4deSHong Zhang #endif
424236db0b34SBarry Smith 
424336db0b34SBarry Smith   PetscFunctionBegin;
4244a96a251dSBarry Smith   if (i[0]) {
4245e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
424636db0b34SBarry Smith   }
4247f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
4248f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4249a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
4250ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
4251ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
4252ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
4253ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
4254ab93d7beSBarry Smith 
425536db0b34SBarry Smith   aij->i = i;
425636db0b34SBarry Smith   aij->j = j;
425736db0b34SBarry Smith   aij->a = a;
425836db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
425936db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4260e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
4261e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
426236db0b34SBarry Smith 
426336db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
426436db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
42652515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
4266e32f2f54SBarry 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]);
42679985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4268e32f2f54SBarry Smith       if (j[jj] < j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
4269e32f2f54SBarry Smith       if (j[jj] == j[jj]-1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii);
42709985e31cSBarry Smith     }
427136db0b34SBarry Smith #endif
427236db0b34SBarry Smith   }
42732515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
427436db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
4275e32f2f54SBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
4276e32f2f54SBarry 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]);
427736db0b34SBarry Smith   }
427836db0b34SBarry Smith #endif
427936db0b34SBarry Smith 
4280b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4281b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
428236db0b34SBarry Smith   PetscFunctionReturn(0);
428336db0b34SBarry Smith }
42848a0b0e6bSVictor Minden #undef __FUNCT__
42858a0b0e6bSVictor Minden #define __FUNCT__ "MatCreateSeqAIJFromTriple"
428680ef6e79SMatthew G Knepley /*@C
4287d021a1c5SVictor Minden      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
42888a0b0e6bSVictor Minden               provided by the user.
42898a0b0e6bSVictor Minden 
42908a0b0e6bSVictor Minden       Collective on MPI_Comm
42918a0b0e6bSVictor Minden 
42928a0b0e6bSVictor Minden    Input Parameters:
42938a0b0e6bSVictor Minden +   comm - must be an MPI communicator of size 1
42948a0b0e6bSVictor Minden .   m   - number of rows
42958a0b0e6bSVictor Minden .   n   - number of columns
42968a0b0e6bSVictor Minden .   i   - row indices
42978a0b0e6bSVictor Minden .   j   - column indices
42981230e6d1SVictor Minden .   a   - matrix values
42991230e6d1SVictor Minden .   nz  - number of nonzeros
43001230e6d1SVictor Minden -   idx - 0 or 1 based
43018a0b0e6bSVictor Minden 
43028a0b0e6bSVictor Minden    Output Parameter:
43038a0b0e6bSVictor Minden .   mat - the matrix
43048a0b0e6bSVictor Minden 
43058a0b0e6bSVictor Minden    Level: intermediate
43068a0b0e6bSVictor Minden 
43078a0b0e6bSVictor Minden    Notes:
43088a0b0e6bSVictor Minden        The i and j indices are 0 based
43098a0b0e6bSVictor Minden 
43108a0b0e6bSVictor Minden        The format which is used for the sparse matrix input, is equivalent to a
43118a0b0e6bSVictor Minden     row-major ordering.. i.e for the following matrix, the input data expected is
43128a0b0e6bSVictor Minden     as shown:
43138a0b0e6bSVictor Minden 
43148a0b0e6bSVictor Minden         1 0 0
43158a0b0e6bSVictor Minden         2 0 3
43168a0b0e6bSVictor Minden         4 5 6
43178a0b0e6bSVictor Minden 
43188a0b0e6bSVictor Minden         i =  {0,1,1,2,2,2}
43198a0b0e6bSVictor Minden         j =  {0,0,2,0,1,2}
43208a0b0e6bSVictor Minden         v =  {1,2,3,4,5,6}
43218a0b0e6bSVictor Minden 
43228a0b0e6bSVictor Minden 
432369b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
43248a0b0e6bSVictor Minden 
43258a0b0e6bSVictor Minden @*/
43261230e6d1SVictor Minden PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat,PetscInt nz,PetscBool idx)
43278a0b0e6bSVictor Minden {
43288a0b0e6bSVictor Minden   PetscErrorCode ierr;
4329d021a1c5SVictor Minden   PetscInt       ii, *nnz, one = 1,row,col;
43308a0b0e6bSVictor Minden 
43318a0b0e6bSVictor Minden 
43328a0b0e6bSVictor Minden   PetscFunctionBegin;
4333d021a1c5SVictor Minden   ierr = PetscMalloc(m*sizeof(PetscInt),&nnz);CHKERRQ(ierr);
43341230e6d1SVictor Minden   ierr = PetscMemzero(nnz,m*sizeof(PetscInt));CHKERRQ(ierr);
43351230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++){
43361230e6d1SVictor Minden     nnz[i[ii]] += 1;
43371230e6d1SVictor Minden   }
43388a0b0e6bSVictor Minden   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
43398a0b0e6bSVictor Minden   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
4340a2f3521dSMark F. Adams   /* ierr = MatSetBlockSizes(*mat,,);CHKERRQ(ierr); */
43418a0b0e6bSVictor Minden   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
43421230e6d1SVictor Minden   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);CHKERRQ(ierr);
43431230e6d1SVictor Minden   for (ii = 0; ii < nz; ii++){
43441230e6d1SVictor Minden     if (idx){
43451230e6d1SVictor Minden       row = i[ii] - 1;
43461230e6d1SVictor Minden       col = j[ii] - 1;
43471230e6d1SVictor Minden     } else {
43481230e6d1SVictor Minden       row = i[ii];
43491230e6d1SVictor Minden       col = j[ii];
43508a0b0e6bSVictor Minden     }
43511230e6d1SVictor Minden     ierr = MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);CHKERRQ(ierr);
43528a0b0e6bSVictor Minden   }
43538a0b0e6bSVictor Minden   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
43548a0b0e6bSVictor Minden   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4355d021a1c5SVictor Minden   ierr = PetscFree(nnz);CHKERRQ(ierr);
43568a0b0e6bSVictor Minden   PetscFunctionReturn(0);
43578a0b0e6bSVictor Minden }
435836db0b34SBarry Smith 
4359cc8ba8e1SBarry Smith #undef __FUNCT__
4360ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
4361dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4362cc8ba8e1SBarry Smith {
4363dfbe8321SBarry Smith   PetscErrorCode ierr;
4364cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
436536db0b34SBarry Smith 
4366cc8ba8e1SBarry Smith   PetscFunctionBegin;
43678ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
4368cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
4369cc8ba8e1SBarry Smith     a->coloring = coloring;
437012c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
437197f1f81fSBarry Smith     PetscInt             i,*larray;
437212c595b3SBarry Smith     ISColoring      ocoloring;
437308b6dcc0SBarry Smith     ISColoringValue *colors;
437412c595b3SBarry Smith 
437512c595b3SBarry Smith     /* set coloring for diagonal portion */
43760e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(PetscInt),&larray);CHKERRQ(ierr);
4377d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
437812c595b3SBarry Smith       larray[i] = i;
437912c595b3SBarry Smith     }
4380992144d0SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
43810e83c824SBarry Smith     ierr = PetscMalloc(A->cmap->n*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
4382d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
438312c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
438412c595b3SBarry Smith     }
438512c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
4386d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
438712c595b3SBarry Smith     a->coloring = ocoloring;
438812c595b3SBarry Smith   }
4389cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4390cc8ba8e1SBarry Smith }
4391cc8ba8e1SBarry Smith 
4392dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
4393ee4f033dSBarry Smith EXTERN_C_BEGIN
4394c6db04a5SJed Brown #include <adic/ad_utils.h>
4395ee4f033dSBarry Smith EXTERN_C_END
4396cc8ba8e1SBarry Smith 
4397cc8ba8e1SBarry Smith #undef __FUNCT__
4398ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
4399dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
4400cc8ba8e1SBarry Smith {
4401cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
4402d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
44034440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
440408b6dcc0SBarry Smith   ISColoringValue *color;
4405cc8ba8e1SBarry Smith 
4406cc8ba8e1SBarry Smith   PetscFunctionBegin;
4407e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
44084440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
4409cc8ba8e1SBarry Smith   color = a->coloring->colors;
4410cc8ba8e1SBarry Smith   /* loop over rows */
4411cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
4412cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
4413cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
4414cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
4415cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
4416cc8ba8e1SBarry Smith     }
44174440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
4418ee4f033dSBarry Smith   }
4419ee4f033dSBarry Smith   PetscFunctionReturn(0);
4420ee4f033dSBarry Smith }
4421ee4f033dSBarry Smith #endif
4422ee4f033dSBarry Smith 
4423ee4f033dSBarry Smith #undef __FUNCT__
4424ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
442597f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4426ee4f033dSBarry Smith {
4427ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
4428d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
442954f21887SBarry Smith   MatScalar       *v = a->a;
443054f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
443108b6dcc0SBarry Smith   ISColoringValue *color;
4432ee4f033dSBarry Smith 
4433ee4f033dSBarry Smith   PetscFunctionBegin;
4434e32f2f54SBarry Smith   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4435ee4f033dSBarry Smith   color = a->coloring->colors;
4436ee4f033dSBarry Smith   /* loop over rows */
4437ee4f033dSBarry Smith   for (i=0; i<m; i++) {
4438ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
4439ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
4440ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
4441ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
4442ee4f033dSBarry Smith     }
4443ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
4444cc8ba8e1SBarry Smith   }
4445cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
4446cc8ba8e1SBarry Smith }
444736db0b34SBarry Smith 
444881824310SBarry Smith /*
444981824310SBarry Smith     Special version for direct calls from Fortran
445081824310SBarry Smith */
4451b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h>
445281824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
445381824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
445481824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
445581824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
445681824310SBarry Smith #endif
445781824310SBarry Smith 
445881824310SBarry Smith /* Change these macros so can be used in void function */
445981824310SBarry Smith #undef CHKERRQ
44607adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
446181824310SBarry Smith #undef SETERRQ2
4462e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
44634994cf47SJed Brown #undef SETERRQ3
44644994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
446581824310SBarry Smith 
446681824310SBarry Smith EXTERN_C_BEGIN
446781824310SBarry Smith #undef __FUNCT__
446881824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
44691f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
447081824310SBarry Smith {
447181824310SBarry Smith   Mat            A = *AA;
447281824310SBarry Smith   PetscInt       m = *mm, n = *nn;
447381824310SBarry Smith   InsertMode     is = *isis;
447481824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
447581824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
447681824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
447781824310SBarry Smith   PetscErrorCode ierr;
447881824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
447954f21887SBarry Smith   MatScalar      *ap,value,*aa;
4480ace3abfcSBarry Smith   PetscBool      ignorezeroentries = a->ignorezeroentries;
4481ace3abfcSBarry Smith   PetscBool      roworiented = a->roworiented;
448281824310SBarry Smith 
448381824310SBarry Smith   PetscFunctionBegin;
44844994cf47SJed Brown   MatCheckPreallocated(A,1);
448581824310SBarry Smith   imax = a->imax;
448681824310SBarry Smith   ai = a->i;
448781824310SBarry Smith   ailen = a->ilen;
448881824310SBarry Smith   aj = a->j;
448981824310SBarry Smith   aa = a->a;
449081824310SBarry Smith 
449181824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
449281824310SBarry Smith     row  = im[k];
449381824310SBarry Smith     if (row < 0) continue;
449481824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4495d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
449681824310SBarry Smith #endif
449781824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
449881824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
449981824310SBarry Smith     low  = 0;
450081824310SBarry Smith     high = nrow;
450181824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
450281824310SBarry Smith       if (in[l] < 0) continue;
450381824310SBarry Smith #if defined(PETSC_USE_DEBUG)
4504d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
450581824310SBarry Smith #endif
450681824310SBarry Smith       col = in[l];
450781824310SBarry Smith       if (roworiented) {
450881824310SBarry Smith         value = v[l + k*n];
450981824310SBarry Smith       } else {
451081824310SBarry Smith         value = v[k + l*m];
451181824310SBarry Smith       }
451281824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
451381824310SBarry Smith 
451481824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
451581824310SBarry Smith       lastcol = col;
451681824310SBarry Smith       while (high-low > 5) {
451781824310SBarry Smith         t = (low+high)/2;
451881824310SBarry Smith         if (rp[t] > col) high = t;
451981824310SBarry Smith         else             low  = t;
452081824310SBarry Smith       }
452181824310SBarry Smith       for (i=low; i<high; i++) {
452281824310SBarry Smith         if (rp[i] > col) break;
452381824310SBarry Smith         if (rp[i] == col) {
452481824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
452581824310SBarry Smith           else                  ap[i] = value;
452681824310SBarry Smith           goto noinsert;
452781824310SBarry Smith         }
452881824310SBarry Smith       }
452981824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
453081824310SBarry Smith       if (nonew == 1) goto noinsert;
45317adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4532fef13f97SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
453381824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
453481824310SBarry Smith       /* shift up all the later entries in this row */
453581824310SBarry Smith       for (ii=N; ii>=i; ii--) {
453681824310SBarry Smith         rp[ii+1] = rp[ii];
453781824310SBarry Smith         ap[ii+1] = ap[ii];
453881824310SBarry Smith       }
453981824310SBarry Smith       rp[i] = col;
454081824310SBarry Smith       ap[i] = value;
454181824310SBarry Smith       noinsert:;
454281824310SBarry Smith       low = i + 1;
454381824310SBarry Smith     }
454481824310SBarry Smith     ailen[row] = nrow;
454581824310SBarry Smith   }
454681824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
454781824310SBarry Smith   PetscFunctionReturnVoid();
454881824310SBarry Smith }
454981824310SBarry Smith EXTERN_C_END
455062298a1eSBarry Smith 
4551