Actual source code: aij.c
petsc-3.13.6 2020-09-29
1: /*
2: Defines the basic matrix operations for the AIJ (compressed row)
3: matrix storage format.
4: */
7: #include <../src/mat/impls/aij/seq/aij.h>
8: #include <petscblaslapack.h>
9: #include <petscbt.h>
10: #include <petsc/private/kernels/blocktranspose.h>
12: PetscErrorCode MatSeqAIJSetTypeFromOptions(Mat A)
13: {
14: PetscErrorCode ierr;
15: PetscBool flg;
16: char type[256];
19: PetscObjectOptionsBegin((PetscObject)A);
20: PetscOptionsFList("-mat_seqaij_type","Matrix SeqAIJ type","MatSeqAIJSetType",MatSeqAIJList,"seqaij",type,256,&flg);
21: if (flg) {
22: MatSeqAIJSetType(A,type);
23: }
24: PetscOptionsEnd();
25: return(0);
26: }
28: PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms)
29: {
31: PetscInt i,m,n;
32: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)A->data;
35: MatGetSize(A,&m,&n);
36: PetscArrayzero(norms,n);
37: if (type == NORM_2) {
38: for (i=0; i<aij->i[m]; i++) {
39: norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]);
40: }
41: } else if (type == NORM_1) {
42: for (i=0; i<aij->i[m]; i++) {
43: norms[aij->j[i]] += PetscAbsScalar(aij->a[i]);
44: }
45: } else if (type == NORM_INFINITY) {
46: for (i=0; i<aij->i[m]; i++) {
47: norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]);
48: }
49: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType");
51: if (type == NORM_2) {
52: for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
53: }
54: return(0);
55: }
57: PetscErrorCode MatFindOffBlockDiagonalEntries_SeqAIJ(Mat A,IS *is)
58: {
59: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
60: PetscInt i,m=A->rmap->n,cnt = 0, bs = A->rmap->bs;
61: const PetscInt *jj = a->j,*ii = a->i;
62: PetscInt *rows;
63: PetscErrorCode ierr;
66: for (i=0; i<m; i++) {
67: if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) {
68: cnt++;
69: }
70: }
71: PetscMalloc1(cnt,&rows);
72: cnt = 0;
73: for (i=0; i<m; i++) {
74: if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) {
75: rows[cnt] = i;
76: cnt++;
77: }
78: }
79: ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,is);
80: return(0);
81: }
83: PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat A,PetscInt *nrows,PetscInt **zrows)
84: {
85: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
86: const MatScalar *aa = a->a;
87: PetscInt i,m=A->rmap->n,cnt = 0;
88: const PetscInt *ii = a->i,*jj = a->j,*diag;
89: PetscInt *rows;
90: PetscErrorCode ierr;
93: MatMarkDiagonal_SeqAIJ(A);
94: diag = a->diag;
95: for (i=0; i<m; i++) {
96: if ((diag[i] >= ii[i+1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
97: cnt++;
98: }
99: }
100: PetscMalloc1(cnt,&rows);
101: cnt = 0;
102: for (i=0; i<m; i++) {
103: if ((diag[i] >= ii[i+1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
104: rows[cnt++] = i;
105: }
106: }
107: *nrows = cnt;
108: *zrows = rows;
109: return(0);
110: }
112: PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
113: {
114: PetscInt nrows,*rows;
118: *zrows = NULL;
119: MatFindZeroDiagonals_SeqAIJ_Private(A,&nrows,&rows);
120: ISCreateGeneral(PetscObjectComm((PetscObject)A),nrows,rows,PETSC_OWN_POINTER,zrows);
121: return(0);
122: }
124: PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows)
125: {
126: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
127: const MatScalar *aa;
128: PetscInt m=A->rmap->n,cnt = 0;
129: const PetscInt *ii;
130: PetscInt n,i,j,*rows;
131: PetscErrorCode ierr;
134: *keptrows = 0;
135: ii = a->i;
136: for (i=0; i<m; i++) {
137: n = ii[i+1] - ii[i];
138: if (!n) {
139: cnt++;
140: goto ok1;
141: }
142: aa = a->a + ii[i];
143: for (j=0; j<n; j++) {
144: if (aa[j] != 0.0) goto ok1;
145: }
146: cnt++;
147: ok1:;
148: }
149: if (!cnt) return(0);
150: PetscMalloc1(A->rmap->n-cnt,&rows);
151: cnt = 0;
152: for (i=0; i<m; i++) {
153: n = ii[i+1] - ii[i];
154: if (!n) continue;
155: aa = a->a + ii[i];
156: for (j=0; j<n; j++) {
157: if (aa[j] != 0.0) {
158: rows[cnt++] = i;
159: break;
160: }
161: }
162: }
163: ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);
164: return(0);
165: }
167: PetscErrorCode MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
168: {
169: PetscErrorCode ierr;
170: Mat_SeqAIJ *aij = (Mat_SeqAIJ*) Y->data;
171: PetscInt i,m = Y->rmap->n;
172: const PetscInt *diag;
173: MatScalar *aa = aij->a;
174: const PetscScalar *v;
175: PetscBool missing;
176: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
177: PetscBool inserted = PETSC_FALSE;
178: #endif
181: if (Y->assembled) {
182: MatMissingDiagonal_SeqAIJ(Y,&missing,NULL);
183: if (!missing) {
184: diag = aij->diag;
185: VecGetArrayRead(D,&v);
186: if (is == INSERT_VALUES) {
187: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
188: inserted = PETSC_TRUE;
189: #endif
190: for (i=0; i<m; i++) {
191: aa[diag[i]] = v[i];
192: }
193: } else {
194: for (i=0; i<m; i++) {
195: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
196: if (v[i] != 0.0) inserted = PETSC_TRUE;
197: #endif
198: aa[diag[i]] += v[i];
199: }
200: }
201: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
202: if (inserted) Y->offloadmask = PETSC_OFFLOAD_CPU;
203: #endif
204: VecRestoreArrayRead(D,&v);
205: return(0);
206: }
207: MatSeqAIJInvalidateDiagonal(Y);
208: }
209: MatDiagonalSet_Default(Y,D,is);
210: return(0);
211: }
213: PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *m,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done)
214: {
215: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
217: PetscInt i,ishift;
220: *m = A->rmap->n;
221: if (!ia) return(0);
222: ishift = 0;
223: if (symmetric && !A->structurally_symmetric) {
224: MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,ishift,oshift,(PetscInt**)ia,(PetscInt**)ja);
225: } else if (oshift == 1) {
226: PetscInt *tia;
227: PetscInt nz = a->i[A->rmap->n];
228: /* malloc space and add 1 to i and j indices */
229: PetscMalloc1(A->rmap->n+1,&tia);
230: for (i=0; i<A->rmap->n+1; i++) tia[i] = a->i[i] + 1;
231: *ia = tia;
232: if (ja) {
233: PetscInt *tja;
234: PetscMalloc1(nz+1,&tja);
235: for (i=0; i<nz; i++) tja[i] = a->j[i] + 1;
236: *ja = tja;
237: }
238: } else {
239: *ia = a->i;
240: if (ja) *ja = a->j;
241: }
242: return(0);
243: }
245: PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done)
246: {
250: if (!ia) return(0);
251: if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
252: PetscFree(*ia);
253: if (ja) {PetscFree(*ja);}
254: }
255: return(0);
256: }
258: PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done)
259: {
260: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
262: PetscInt i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
263: PetscInt nz = a->i[m],row,*jj,mr,col;
266: *nn = n;
267: if (!ia) return(0);
268: if (symmetric) {
269: MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,0,oshift,(PetscInt**)ia,(PetscInt**)ja);
270: } else {
271: PetscCalloc1(n,&collengths);
272: PetscMalloc1(n+1,&cia);
273: PetscMalloc1(nz,&cja);
274: jj = a->j;
275: for (i=0; i<nz; i++) {
276: collengths[jj[i]]++;
277: }
278: cia[0] = oshift;
279: for (i=0; i<n; i++) {
280: cia[i+1] = cia[i] + collengths[i];
281: }
282: PetscArrayzero(collengths,n);
283: jj = a->j;
284: for (row=0; row<m; row++) {
285: mr = a->i[row+1] - a->i[row];
286: for (i=0; i<mr; i++) {
287: col = *jj++;
289: cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
290: }
291: }
292: PetscFree(collengths);
293: *ia = cia; *ja = cja;
294: }
295: return(0);
296: }
298: PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool *done)
299: {
303: if (!ia) return(0);
305: PetscFree(*ia);
306: PetscFree(*ja);
307: return(0);
308: }
310: /*
311: MatGetColumnIJ_SeqAIJ_Color() and MatRestoreColumnIJ_SeqAIJ_Color() are customized from
312: MatGetColumnIJ_SeqAIJ() and MatRestoreColumnIJ_SeqAIJ() by adding an output
313: spidx[], index of a->a, to be used in MatTransposeColoringCreate_SeqAIJ() and MatFDColoringCreate_SeqXAIJ()
314: */
315: PetscErrorCode MatGetColumnIJ_SeqAIJ_Color(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscInt *spidx[],PetscBool *done)
316: {
317: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
319: PetscInt i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
320: PetscInt nz = a->i[m],row,mr,col,tmp;
321: PetscInt *cspidx;
322: const PetscInt *jj;
325: *nn = n;
326: if (!ia) return(0);
328: PetscCalloc1(n,&collengths);
329: PetscMalloc1(n+1,&cia);
330: PetscMalloc1(nz,&cja);
331: PetscMalloc1(nz,&cspidx);
332: jj = a->j;
333: for (i=0; i<nz; i++) {
334: collengths[jj[i]]++;
335: }
336: cia[0] = oshift;
337: for (i=0; i<n; i++) {
338: cia[i+1] = cia[i] + collengths[i];
339: }
340: PetscArrayzero(collengths,n);
341: jj = a->j;
342: for (row=0; row<m; row++) {
343: mr = a->i[row+1] - a->i[row];
344: for (i=0; i<mr; i++) {
345: col = *jj++;
346: tmp = cia[col] + collengths[col]++ - oshift;
347: cspidx[tmp] = a->i[row] + i; /* index of a->j */
348: cja[tmp] = row + oshift;
349: }
350: }
351: PetscFree(collengths);
352: *ia = cia;
353: *ja = cja;
354: *spidx = cspidx;
355: return(0);
356: }
358: PetscErrorCode MatRestoreColumnIJ_SeqAIJ_Color(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscInt *spidx[],PetscBool *done)
359: {
363: MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);
364: PetscFree(*spidx);
365: return(0);
366: }
368: PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
369: {
370: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
371: PetscInt *ai = a->i;
375: PetscArraycpy(a->a+ai[row],v,ai[row+1]-ai[row]);
376: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
377: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && ai[row+1]-ai[row]) A->offloadmask = PETSC_OFFLOAD_CPU;
378: #endif
379: return(0);
380: }
382: /*
383: MatSeqAIJSetValuesLocalFast - An optimized version of MatSetValuesLocal() for SeqAIJ matrices with several assumptions
385: - a single row of values is set with each call
386: - no row or column indices are negative or (in error) larger than the number of rows or columns
387: - the values are always added to the matrix, not set
388: - no new locations are introduced in the nonzero structure of the matrix
390: This does NOT assume the global column indices are sorted
392: */
394: #include <petsc/private/isimpl.h>
395: PetscErrorCode MatSeqAIJSetValuesLocalFast(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
396: {
397: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
398: PetscInt low,high,t,row,nrow,i,col,l;
399: const PetscInt *rp,*ai = a->i,*ailen = a->ilen,*aj = a->j;
400: PetscInt lastcol = -1;
401: MatScalar *ap,value,*aa = a->a;
402: const PetscInt *ridx = A->rmap->mapping->indices,*cidx = A->cmap->mapping->indices;
404: row = ridx[im[0]];
405: rp = aj + ai[row];
406: ap = aa + ai[row];
407: nrow = ailen[row];
408: low = 0;
409: high = nrow;
410: for (l=0; l<n; l++) { /* loop over added columns */
411: col = cidx[in[l]];
412: value = v[l];
414: if (col <= lastcol) low = 0;
415: else high = nrow;
416: lastcol = col;
417: while (high-low > 5) {
418: t = (low+high)/2;
419: if (rp[t] > col) high = t;
420: else low = t;
421: }
422: for (i=low; i<high; i++) {
423: if (rp[i] == col) {
424: ap[i] += value;
425: low = i + 1;
426: break;
427: }
428: }
429: }
430: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
431: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && m && n) A->offloadmask = PETSC_OFFLOAD_CPU;
432: #endif
433: return 0;
434: }
436: PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
437: {
438: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
439: PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
440: PetscInt *imax = a->imax,*ai = a->i,*ailen = a->ilen;
442: PetscInt *aj = a->j,nonew = a->nonew,lastcol = -1;
443: MatScalar *ap=NULL,value=0.0,*aa = a->a;
444: PetscBool ignorezeroentries = a->ignorezeroentries;
445: PetscBool roworiented = a->roworiented;
446: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
447: PetscBool inserted = PETSC_FALSE;
448: #endif
451: for (k=0; k<m; k++) { /* loop over added rows */
452: row = im[k];
453: if (row < 0) continue;
454: #if defined(PETSC_USE_DEBUG)
455: 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);
456: #endif
457: rp = aj + ai[row];
458: if (!A->structure_only) ap = aa + ai[row];
459: rmax = imax[row]; nrow = ailen[row];
460: low = 0;
461: high = nrow;
462: for (l=0; l<n; l++) { /* loop over added columns */
463: if (in[l] < 0) continue;
464: #if defined(PETSC_USE_DEBUG)
465: 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);
466: #endif
467: col = in[l];
468: if (v && !A->structure_only) value = roworiented ? v[l + k*n] : v[k + l*m];
469: if (!A->structure_only && value == 0.0 && ignorezeroentries && is == ADD_VALUES && row != col) continue;
471: if (col <= lastcol) low = 0;
472: else high = nrow;
473: lastcol = col;
474: while (high-low > 5) {
475: t = (low+high)/2;
476: if (rp[t] > col) high = t;
477: else low = t;
478: }
479: for (i=low; i<high; i++) {
480: if (rp[i] > col) break;
481: if (rp[i] == col) {
482: if (!A->structure_only) {
483: if (is == ADD_VALUES) {
484: ap[i] += value;
485: (void)PetscLogFlops(1.0);
486: }
487: else ap[i] = value;
488: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
489: inserted = PETSC_TRUE;
490: #endif
491: }
492: low = i + 1;
493: goto noinsert;
494: }
495: }
496: if (value == 0.0 && ignorezeroentries && row != col) goto noinsert;
497: if (nonew == 1) goto noinsert;
498: if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
499: if (A->structure_only) {
500: MatSeqXAIJReallocateAIJ_structure_only(A,A->rmap->n,1,nrow,row,col,rmax,ai,aj,rp,imax,nonew,MatScalar);
501: } else {
502: MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
503: }
504: N = nrow++ - 1; a->nz++; high++;
505: /* shift up all the later entries in this row */
506: PetscArraymove(rp+i+1,rp+i,N-i+1);
507: rp[i] = col;
508: if (!A->structure_only){
509: PetscArraymove(ap+i+1,ap+i,N-i+1);
510: ap[i] = value;
511: }
512: low = i + 1;
513: A->nonzerostate++;
514: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
515: inserted = PETSC_TRUE;
516: #endif
517: noinsert:;
518: }
519: ailen[row] = nrow;
520: }
521: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
522: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && inserted) A->offloadmask = PETSC_OFFLOAD_CPU;
523: #endif
524: return(0);
525: }
527: PetscErrorCode MatSetValues_SeqAIJ_SortedFull(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
528: {
529: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
530: PetscInt *rp,k,row;
531: PetscInt *ai = a->i,*ailen = a->ilen;
533: PetscInt *aj = a->j;
534: MatScalar *aa = a->a,*ap;
537: for (k=0; k<m; k++) { /* loop over added rows */
538: row = im[k];
539: rp = aj + ai[row];
540: ap = aa + ai[row];
541: if (!A->was_assembled) {
542: PetscMemcpy(rp,in,n*sizeof(PetscInt));
543: }
544: if (!A->structure_only) {
545: if (v) {
546: PetscMemcpy(ap,v,n*sizeof(PetscScalar));
547: v += n;
548: } else {
549: PetscMemzero(ap,n*sizeof(PetscScalar));
550: }
551: }
552: ailen[row] = n;
553: a->nz += n;
554: }
555: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
556: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && m && n) A->offloadmask = PETSC_OFFLOAD_CPU;
557: #endif
558: return(0);
559: }
562: PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
563: {
564: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
565: PetscInt *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
566: PetscInt *ai = a->i,*ailen = a->ilen;
567: MatScalar *ap,*aa = a->a;
570: for (k=0; k<m; k++) { /* loop over rows */
571: row = im[k];
572: if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
573: 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);
574: rp = aj + ai[row]; ap = aa + ai[row];
575: nrow = ailen[row];
576: for (l=0; l<n; l++) { /* loop over columns */
577: if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
578: 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);
579: col = in[l];
580: high = nrow; low = 0; /* assume unsorted */
581: while (high-low > 5) {
582: t = (low+high)/2;
583: if (rp[t] > col) high = t;
584: else low = t;
585: }
586: for (i=low; i<high; i++) {
587: if (rp[i] > col) break;
588: if (rp[i] == col) {
589: *v++ = ap[i];
590: goto finished;
591: }
592: }
593: *v++ = 0.0;
594: finished:;
595: }
596: }
597: return(0);
598: }
600: PetscErrorCode MatView_SeqAIJ_Binary(Mat mat,PetscViewer viewer)
601: {
602: Mat_SeqAIJ *A = (Mat_SeqAIJ*)mat->data;
603: PetscInt header[4],M,N,m,nz,i;
604: PetscInt *rowlens;
608: PetscViewerSetUp(viewer);
610: M = mat->rmap->N;
611: N = mat->cmap->N;
612: m = mat->rmap->n;
613: nz = A->nz;
615: /* write matrix header */
616: header[0] = MAT_FILE_CLASSID;
617: header[1] = M; header[2] = N; header[3] = nz;
618: PetscViewerBinaryWrite(viewer,header,4,PETSC_INT);
620: /* fill in and store row lengths */
621: PetscMalloc1(m,&rowlens);
622: for (i=0; i<m; i++) rowlens[i] = A->i[i+1] - A->i[i];
623: PetscViewerBinaryWrite(viewer,rowlens,m,PETSC_INT);
624: PetscFree(rowlens);
625: /* store column indices */
626: PetscViewerBinaryWrite(viewer,A->j,nz,PETSC_INT);
627: /* store nonzero values */
628: PetscViewerBinaryWrite(viewer,A->a,nz,PETSC_SCALAR);
630: /* write block size option to the viewer's .info file */
631: MatView_Binary_BlockSizes(mat,viewer);
632: return(0);
633: }
635: static PetscErrorCode MatView_SeqAIJ_ASCII_structonly(Mat A,PetscViewer viewer)
636: {
638: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
639: PetscInt i,k,m=A->rmap->N;
642: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
643: for (i=0; i<m; i++) {
644: PetscViewerASCIIPrintf(viewer,"row %D:",i);
645: for (k=a->i[i]; k<a->i[i+1]; k++) {
646: PetscViewerASCIIPrintf(viewer," (%D) ",a->j[k]);
647: }
648: PetscViewerASCIIPrintf(viewer,"\n");
649: }
650: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
651: return(0);
652: }
654: extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
656: PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
657: {
658: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
659: PetscErrorCode ierr;
660: PetscInt i,j,m = A->rmap->n;
661: const char *name;
662: PetscViewerFormat format;
665: if (A->structure_only) {
666: MatView_SeqAIJ_ASCII_structonly(A,viewer);
667: return(0);
668: }
670: PetscViewerGetFormat(viewer,&format);
671: if (format == PETSC_VIEWER_ASCII_MATLAB) {
672: PetscInt nofinalvalue = 0;
673: if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) {
674: /* Need a dummy value to ensure the dimension of the matrix. */
675: nofinalvalue = 1;
676: }
677: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
678: PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);
679: PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);
680: #if defined(PETSC_USE_COMPLEX)
681: PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);
682: #else
683: PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);
684: #endif
685: PetscViewerASCIIPrintf(viewer,"zzz = [\n");
687: for (i=0; i<m; i++) {
688: for (j=a->i[i]; j<a->i[i+1]; j++) {
689: #if defined(PETSC_USE_COMPLEX)
690: PetscViewerASCIIPrintf(viewer,"%D %D %18.16e %18.16e\n",i+1,a->j[j]+1,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
691: #else
692: PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);
693: #endif
694: }
695: }
696: if (nofinalvalue) {
697: #if defined(PETSC_USE_COMPLEX)
698: PetscViewerASCIIPrintf(viewer,"%D %D %18.16e %18.16e\n",m,A->cmap->n,0.,0.);
699: #else
700: PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",m,A->cmap->n,0.0);
701: #endif
702: }
703: PetscObjectGetName((PetscObject)A,&name);
704: PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);
705: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
706: } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
707: return(0);
708: } else if (format == PETSC_VIEWER_ASCII_COMMON) {
709: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
710: for (i=0; i<m; i++) {
711: PetscViewerASCIIPrintf(viewer,"row %D:",i);
712: for (j=a->i[i]; j<a->i[i+1]; j++) {
713: #if defined(PETSC_USE_COMPLEX)
714: if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
715: PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
716: } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
717: PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));
718: } else if (PetscRealPart(a->a[j]) != 0.0) {
719: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
720: }
721: #else
722: if (a->a[j] != 0.0) {PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);}
723: #endif
724: }
725: PetscViewerASCIIPrintf(viewer,"\n");
726: }
727: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
728: } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
729: PetscInt nzd=0,fshift=1,*sptr;
730: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
731: PetscMalloc1(m+1,&sptr);
732: for (i=0; i<m; i++) {
733: sptr[i] = nzd+1;
734: for (j=a->i[i]; j<a->i[i+1]; j++) {
735: if (a->j[j] >= i) {
736: #if defined(PETSC_USE_COMPLEX)
737: if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
738: #else
739: if (a->a[j] != 0.0) nzd++;
740: #endif
741: }
742: }
743: }
744: sptr[m] = nzd+1;
745: PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);
746: for (i=0; i<m+1; i+=6) {
747: if (i+4<m) {
748: 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]);
749: } else if (i+3<m) {
750: PetscViewerASCIIPrintf(viewer," %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);
751: } else if (i+2<m) {
752: PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);
753: } else if (i+1<m) {
754: PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);
755: } else if (i<m) {
756: PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);
757: } else {
758: PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);
759: }
760: }
761: PetscViewerASCIIPrintf(viewer,"\n");
762: PetscFree(sptr);
763: for (i=0; i<m; i++) {
764: for (j=a->i[i]; j<a->i[i+1]; j++) {
765: if (a->j[j] >= i) {PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);}
766: }
767: PetscViewerASCIIPrintf(viewer,"\n");
768: }
769: PetscViewerASCIIPrintf(viewer,"\n");
770: for (i=0; i<m; i++) {
771: for (j=a->i[i]; j<a->i[i+1]; j++) {
772: if (a->j[j] >= i) {
773: #if defined(PETSC_USE_COMPLEX)
774: if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
775: PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
776: }
777: #else
778: if (a->a[j] != 0.0) {PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);}
779: #endif
780: }
781: }
782: PetscViewerASCIIPrintf(viewer,"\n");
783: }
784: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
785: } else if (format == PETSC_VIEWER_ASCII_DENSE) {
786: PetscInt cnt = 0,jcnt;
787: PetscScalar value;
788: #if defined(PETSC_USE_COMPLEX)
789: PetscBool realonly = PETSC_TRUE;
791: for (i=0; i<a->i[m]; i++) {
792: if (PetscImaginaryPart(a->a[i]) != 0.0) {
793: realonly = PETSC_FALSE;
794: break;
795: }
796: }
797: #endif
799: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
800: for (i=0; i<m; i++) {
801: jcnt = 0;
802: for (j=0; j<A->cmap->n; j++) {
803: if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
804: value = a->a[cnt++];
805: jcnt++;
806: } else {
807: value = 0.0;
808: }
809: #if defined(PETSC_USE_COMPLEX)
810: if (realonly) {
811: PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));
812: } else {
813: PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));
814: }
815: #else
816: PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);
817: #endif
818: }
819: PetscViewerASCIIPrintf(viewer,"\n");
820: }
821: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
822: } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
823: PetscInt fshift=1;
824: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
825: #if defined(PETSC_USE_COMPLEX)
826: PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate complex general\n");
827: #else
828: PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate real general\n");
829: #endif
830: PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);
831: for (i=0; i<m; i++) {
832: for (j=a->i[i]; j<a->i[i+1]; j++) {
833: #if defined(PETSC_USE_COMPLEX)
834: PetscViewerASCIIPrintf(viewer,"%D %D %g %g\n", i+fshift,a->j[j]+fshift,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
835: #else
836: PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);
837: #endif
838: }
839: }
840: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
841: } else {
842: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
843: if (A->factortype) {
844: for (i=0; i<m; i++) {
845: PetscViewerASCIIPrintf(viewer,"row %D:",i);
846: /* L part */
847: for (j=a->i[i]; j<a->i[i+1]; j++) {
848: #if defined(PETSC_USE_COMPLEX)
849: if (PetscImaginaryPart(a->a[j]) > 0.0) {
850: PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
851: } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
852: PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));
853: } else {
854: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
855: }
856: #else
857: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
858: #endif
859: }
860: /* diagonal */
861: j = a->diag[i];
862: #if defined(PETSC_USE_COMPLEX)
863: if (PetscImaginaryPart(a->a[j]) > 0.0) {
864: PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)PetscImaginaryPart(1.0/a->a[j]));
865: } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
866: PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)(-PetscImaginaryPart(1.0/a->a[j])));
867: } else {
868: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));
869: }
870: #else
871: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));
872: #endif
874: /* U part */
875: for (j=a->diag[i+1]+1; j<a->diag[i]; j++) {
876: #if defined(PETSC_USE_COMPLEX)
877: if (PetscImaginaryPart(a->a[j]) > 0.0) {
878: PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
879: } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
880: PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));
881: } else {
882: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
883: }
884: #else
885: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
886: #endif
887: }
888: PetscViewerASCIIPrintf(viewer,"\n");
889: }
890: } else {
891: for (i=0; i<m; i++) {
892: PetscViewerASCIIPrintf(viewer,"row %D:",i);
893: for (j=a->i[i]; j<a->i[i+1]; j++) {
894: #if defined(PETSC_USE_COMPLEX)
895: if (PetscImaginaryPart(a->a[j]) > 0.0) {
896: PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
897: } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
898: PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));
899: } else {
900: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
901: }
902: #else
903: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
904: #endif
905: }
906: PetscViewerASCIIPrintf(viewer,"\n");
907: }
908: }
909: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
910: }
911: PetscViewerFlush(viewer);
912: return(0);
913: }
915: #include <petscdraw.h>
916: PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
917: {
918: Mat A = (Mat) Aa;
919: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
920: PetscErrorCode ierr;
921: PetscInt i,j,m = A->rmap->n;
922: int color;
923: PetscReal xl,yl,xr,yr,x_l,x_r,y_l,y_r;
924: PetscViewer viewer;
925: PetscViewerFormat format;
928: PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);
929: PetscViewerGetFormat(viewer,&format);
930: PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);
932: /* loop over matrix elements drawing boxes */
934: if (format != PETSC_VIEWER_DRAW_CONTOUR) {
935: PetscDrawCollectiveBegin(draw);
936: /* Blue for negative, Cyan for zero and Red for positive */
937: color = PETSC_DRAW_BLUE;
938: for (i=0; i<m; i++) {
939: y_l = m - i - 1.0; y_r = y_l + 1.0;
940: for (j=a->i[i]; j<a->i[i+1]; j++) {
941: x_l = a->j[j]; x_r = x_l + 1.0;
942: if (PetscRealPart(a->a[j]) >= 0.) continue;
943: PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
944: }
945: }
946: color = PETSC_DRAW_CYAN;
947: for (i=0; i<m; i++) {
948: y_l = m - i - 1.0; y_r = y_l + 1.0;
949: for (j=a->i[i]; j<a->i[i+1]; j++) {
950: x_l = a->j[j]; x_r = x_l + 1.0;
951: if (a->a[j] != 0.) continue;
952: PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
953: }
954: }
955: color = PETSC_DRAW_RED;
956: for (i=0; i<m; i++) {
957: y_l = m - i - 1.0; y_r = y_l + 1.0;
958: for (j=a->i[i]; j<a->i[i+1]; j++) {
959: x_l = a->j[j]; x_r = x_l + 1.0;
960: if (PetscRealPart(a->a[j]) <= 0.) continue;
961: PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
962: }
963: }
964: PetscDrawCollectiveEnd(draw);
965: } else {
966: /* use contour shading to indicate magnitude of values */
967: /* first determine max of all nonzero values */
968: PetscReal minv = 0.0, maxv = 0.0;
969: PetscInt nz = a->nz, count = 0;
970: PetscDraw popup;
972: for (i=0; i<nz; i++) {
973: if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
974: }
975: if (minv >= maxv) maxv = minv + PETSC_SMALL;
976: PetscDrawGetPopup(draw,&popup);
977: PetscDrawScalePopup(popup,minv,maxv);
979: PetscDrawCollectiveBegin(draw);
980: for (i=0; i<m; i++) {
981: y_l = m - i - 1.0;
982: y_r = y_l + 1.0;
983: for (j=a->i[i]; j<a->i[i+1]; j++) {
984: x_l = a->j[j];
985: x_r = x_l + 1.0;
986: color = PetscDrawRealToColor(PetscAbsScalar(a->a[count]),minv,maxv);
987: PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
988: count++;
989: }
990: }
991: PetscDrawCollectiveEnd(draw);
992: }
993: return(0);
994: }
996: #include <petscdraw.h>
997: PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
998: {
1000: PetscDraw draw;
1001: PetscReal xr,yr,xl,yl,h,w;
1002: PetscBool isnull;
1005: PetscViewerDrawGetDraw(viewer,0,&draw);
1006: PetscDrawIsNull(draw,&isnull);
1007: if (isnull) return(0);
1009: xr = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
1010: xr += w; yr += h; xl = -w; yl = -h;
1011: PetscDrawSetCoordinates(draw,xl,yl,xr,yr);
1012: PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);
1013: PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);
1014: PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);
1015: PetscDrawSave(draw);
1016: return(0);
1017: }
1019: PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
1020: {
1022: PetscBool iascii,isbinary,isdraw;
1025: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1026: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1027: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1028: if (iascii) {
1029: MatView_SeqAIJ_ASCII(A,viewer);
1030: } else if (isbinary) {
1031: MatView_SeqAIJ_Binary(A,viewer);
1032: } else if (isdraw) {
1033: MatView_SeqAIJ_Draw(A,viewer);
1034: }
1035: MatView_SeqAIJ_Inode(A,viewer);
1036: return(0);
1037: }
1039: PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
1040: {
1041: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1043: PetscInt fshift = 0,i,*ai = a->i,*aj = a->j,*imax = a->imax;
1044: PetscInt m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
1045: MatScalar *aa = a->a,*ap;
1046: PetscReal ratio = 0.6;
1049: if (mode == MAT_FLUSH_ASSEMBLY) return(0);
1050: MatSeqAIJInvalidateDiagonal(A);
1051: if (A->was_assembled && A->ass_nonzerostate == A->nonzerostate) return(0);
1053: if (m) rmax = ailen[0]; /* determine row with most nonzeros */
1054: for (i=1; i<m; i++) {
1055: /* move each row back by the amount of empty slots (fshift) before it*/
1056: fshift += imax[i-1] - ailen[i-1];
1057: rmax = PetscMax(rmax,ailen[i]);
1058: if (fshift) {
1059: ip = aj + ai[i];
1060: ap = aa + ai[i];
1061: N = ailen[i];
1062: PetscArraymove(ip-fshift,ip,N);
1063: if (!A->structure_only) {
1064: PetscArraymove(ap-fshift,ap,N);
1065: }
1066: }
1067: ai[i] = ai[i-1] + ailen[i-1];
1068: }
1069: if (m) {
1070: fshift += imax[m-1] - ailen[m-1];
1071: ai[m] = ai[m-1] + ailen[m-1];
1072: }
1074: /* reset ilen and imax for each row */
1075: a->nonzerorowcnt = 0;
1076: if (A->structure_only) {
1077: PetscFree(a->imax);
1078: PetscFree(a->ilen);
1079: } else { /* !A->structure_only */
1080: for (i=0; i<m; i++) {
1081: ailen[i] = imax[i] = ai[i+1] - ai[i];
1082: a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
1083: }
1084: }
1085: a->nz = ai[m];
1086: 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);
1088: MatMarkDiagonal_SeqAIJ(A);
1089: PetscInfo4(A,"Matrix size: %D X %D; storage space: %D unneeded,%D used\n",m,A->cmap->n,fshift,a->nz);
1090: PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);
1091: PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);
1093: A->info.mallocs += a->reallocs;
1094: a->reallocs = 0;
1095: A->info.nz_unneeded = (PetscReal)fshift;
1096: a->rmax = rmax;
1098: if (!A->structure_only) {
1099: MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);
1100: }
1101: MatAssemblyEnd_SeqAIJ_Inode(A,mode);
1102: return(0);
1103: }
1105: PetscErrorCode MatRealPart_SeqAIJ(Mat A)
1106: {
1107: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1108: PetscInt i,nz = a->nz;
1109: MatScalar *aa = a->a;
1113: for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1114: MatSeqAIJInvalidateDiagonal(A);
1115: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1116: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
1117: #endif
1118: return(0);
1119: }
1121: PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
1122: {
1123: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1124: PetscInt i,nz = a->nz;
1125: MatScalar *aa = a->a;
1129: for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1130: MatSeqAIJInvalidateDiagonal(A);
1131: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1132: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
1133: #endif
1134: return(0);
1135: }
1137: PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
1138: {
1139: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1143: PetscArrayzero(a->a,a->i[A->rmap->n]);
1144: MatSeqAIJInvalidateDiagonal(A);
1145: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1146: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
1147: #endif
1148: return(0);
1149: }
1151: PetscErrorCode MatDestroy_SeqAIJ(Mat A)
1152: {
1153: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1157: #if defined(PETSC_USE_LOG)
1158: PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
1159: #endif
1160: MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);
1161: ISDestroy(&a->row);
1162: ISDestroy(&a->col);
1163: PetscFree(a->diag);
1164: PetscFree(a->ibdiag);
1165: PetscFree(a->imax);
1166: PetscFree(a->ilen);
1167: PetscFree(a->ipre);
1168: PetscFree3(a->idiag,a->mdiag,a->ssor_work);
1169: PetscFree(a->solve_work);
1170: ISDestroy(&a->icol);
1171: PetscFree(a->saved_values);
1172: ISColoringDestroy(&a->coloring);
1173: PetscFree2(a->compressedrow.i,a->compressedrow.rindex);
1174: PetscFree(a->matmult_abdense);
1176: MatDestroy_SeqAIJ_Inode(A);
1177: PetscFree(A->data);
1179: PetscObjectChangeTypeName((PetscObject)A,0);
1180: PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);
1181: PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);
1182: PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);
1183: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);
1184: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);
1185: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);
1187: #if defined(PETSC_HAVE_CUDA)
1188: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijcusparse_C",NULL);
1189: PetscObjectComposeFunction((PetscObject)A,"MatSetFromOptions_seqaijcusparse_seqaij_C",NULL);
1190: #endif
1191: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijcrl_C",NULL);
1192: #if defined(PETSC_HAVE_ELEMENTAL)
1193: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);
1194: #endif
1195: #if defined(PETSC_HAVE_HYPRE)
1196: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_hypre_C",NULL);
1197: PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_transpose_seqaij_seqaij_C",NULL);
1198: #endif
1199: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);
1200: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsell_C",NULL);
1201: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_is_C",NULL);
1202: PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);
1203: PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);
1204: PetscObjectComposeFunction((PetscObject)A,"MatResetPreallocation_C",NULL);
1205: PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);
1206: PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);
1207: PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_is_seqaij_C",NULL);
1208: PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqdense_seqaij_C",NULL);
1209: PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaij_seqaij_C",NULL);
1210: return(0);
1211: }
1213: PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
1214: {
1215: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1219: switch (op) {
1220: case MAT_ROW_ORIENTED:
1221: a->roworiented = flg;
1222: break;
1223: case MAT_KEEP_NONZERO_PATTERN:
1224: a->keepnonzeropattern = flg;
1225: break;
1226: case MAT_NEW_NONZERO_LOCATIONS:
1227: a->nonew = (flg ? 0 : 1);
1228: break;
1229: case MAT_NEW_NONZERO_LOCATION_ERR:
1230: a->nonew = (flg ? -1 : 0);
1231: break;
1232: case MAT_NEW_NONZERO_ALLOCATION_ERR:
1233: a->nonew = (flg ? -2 : 0);
1234: break;
1235: case MAT_UNUSED_NONZERO_LOCATION_ERR:
1236: a->nounused = (flg ? -1 : 0);
1237: break;
1238: case MAT_IGNORE_ZERO_ENTRIES:
1239: a->ignorezeroentries = flg;
1240: break;
1241: case MAT_SPD:
1242: case MAT_SYMMETRIC:
1243: case MAT_STRUCTURALLY_SYMMETRIC:
1244: case MAT_HERMITIAN:
1245: case MAT_SYMMETRY_ETERNAL:
1246: case MAT_STRUCTURE_ONLY:
1247: /* These options are handled directly by MatSetOption() */
1248: break;
1249: case MAT_NEW_DIAGONALS:
1250: case MAT_IGNORE_OFF_PROC_ENTRIES:
1251: case MAT_USE_HASH_TABLE:
1252: PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1253: break;
1254: case MAT_USE_INODES:
1255: /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1256: break;
1257: case MAT_SUBMAT_SINGLEIS:
1258: A->submat_singleis = flg;
1259: break;
1260: case MAT_SORTED_FULL:
1261: if (flg) A->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;
1262: else A->ops->setvalues = MatSetValues_SeqAIJ;
1263: break;
1264: default:
1265: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1266: }
1267: MatSetOption_SeqAIJ_Inode(A,op,flg);
1268: return(0);
1269: }
1271: PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
1272: {
1273: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1275: PetscInt i,j,n,*ai=a->i,*aj=a->j;
1276: PetscScalar *aa=a->a,*x;
1279: VecGetLocalSize(v,&n);
1280: if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
1282: if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1283: PetscInt *diag=a->diag;
1284: VecGetArrayWrite(v,&x);
1285: for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
1286: VecRestoreArrayWrite(v,&x);
1287: return(0);
1288: }
1290: VecGetArrayWrite(v,&x);
1291: for (i=0; i<n; i++) {
1292: x[i] = 0.0;
1293: for (j=ai[i]; j<ai[i+1]; j++) {
1294: if (aj[j] == i) {
1295: x[i] = aa[j];
1296: break;
1297: }
1298: }
1299: }
1300: VecRestoreArrayWrite(v,&x);
1301: return(0);
1302: }
1304: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1305: PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
1306: {
1307: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1308: PetscScalar *y;
1309: const PetscScalar *x;
1310: PetscErrorCode ierr;
1311: PetscInt m = A->rmap->n;
1312: #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1313: const MatScalar *v;
1314: PetscScalar alpha;
1315: PetscInt n,i,j;
1316: const PetscInt *idx,*ii,*ridx=NULL;
1317: Mat_CompressedRow cprow = a->compressedrow;
1318: PetscBool usecprow = cprow.use;
1319: #endif
1322: if (zz != yy) {VecCopy(zz,yy);}
1323: VecGetArrayRead(xx,&x);
1324: VecGetArray(yy,&y);
1326: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1327: fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
1328: #else
1329: if (usecprow) {
1330: m = cprow.nrows;
1331: ii = cprow.i;
1332: ridx = cprow.rindex;
1333: } else {
1334: ii = a->i;
1335: }
1336: for (i=0; i<m; i++) {
1337: idx = a->j + ii[i];
1338: v = a->a + ii[i];
1339: n = ii[i+1] - ii[i];
1340: if (usecprow) {
1341: alpha = x[ridx[i]];
1342: } else {
1343: alpha = x[i];
1344: }
1345: for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
1346: }
1347: #endif
1348: PetscLogFlops(2.0*a->nz);
1349: VecRestoreArrayRead(xx,&x);
1350: VecRestoreArray(yy,&y);
1351: return(0);
1352: }
1354: PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
1355: {
1359: VecSet(yy,0.0);
1360: MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);
1361: return(0);
1362: }
1364: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1366: PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
1367: {
1368: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1369: PetscScalar *y;
1370: const PetscScalar *x;
1371: const MatScalar *aa;
1372: PetscErrorCode ierr;
1373: PetscInt m=A->rmap->n;
1374: const PetscInt *aj,*ii,*ridx=NULL;
1375: PetscInt n,i;
1376: PetscScalar sum;
1377: PetscBool usecprow=a->compressedrow.use;
1379: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1380: #pragma disjoint(*x,*y,*aa)
1381: #endif
1384: VecGetArrayRead(xx,&x);
1385: VecGetArray(yy,&y);
1386: ii = a->i;
1387: if (usecprow) { /* use compressed row format */
1388: PetscArrayzero(y,m);
1389: m = a->compressedrow.nrows;
1390: ii = a->compressedrow.i;
1391: ridx = a->compressedrow.rindex;
1392: for (i=0; i<m; i++) {
1393: n = ii[i+1] - ii[i];
1394: aj = a->j + ii[i];
1395: aa = a->a + ii[i];
1396: sum = 0.0;
1397: PetscSparseDensePlusDot(sum,x,aa,aj,n);
1398: /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1399: y[*ridx++] = sum;
1400: }
1401: } else { /* do not use compressed row format */
1402: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1403: aj = a->j;
1404: aa = a->a;
1405: fortranmultaij_(&m,x,ii,aj,aa,y);
1406: #else
1407: for (i=0; i<m; i++) {
1408: n = ii[i+1] - ii[i];
1409: aj = a->j + ii[i];
1410: aa = a->a + ii[i];
1411: sum = 0.0;
1412: PetscSparseDensePlusDot(sum,x,aa,aj,n);
1413: y[i] = sum;
1414: }
1415: #endif
1416: }
1417: PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);
1418: VecRestoreArrayRead(xx,&x);
1419: VecRestoreArray(yy,&y);
1420: return(0);
1421: }
1423: PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1424: {
1425: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1426: PetscScalar *y;
1427: const PetscScalar *x;
1428: const MatScalar *aa;
1429: PetscErrorCode ierr;
1430: PetscInt m=A->rmap->n;
1431: const PetscInt *aj,*ii,*ridx=NULL;
1432: PetscInt n,i,nonzerorow=0;
1433: PetscScalar sum;
1434: PetscBool usecprow=a->compressedrow.use;
1436: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1437: #pragma disjoint(*x,*y,*aa)
1438: #endif
1441: VecGetArrayRead(xx,&x);
1442: VecGetArray(yy,&y);
1443: if (usecprow) { /* use compressed row format */
1444: m = a->compressedrow.nrows;
1445: ii = a->compressedrow.i;
1446: ridx = a->compressedrow.rindex;
1447: for (i=0; i<m; i++) {
1448: n = ii[i+1] - ii[i];
1449: aj = a->j + ii[i];
1450: aa = a->a + ii[i];
1451: sum = 0.0;
1452: nonzerorow += (n>0);
1453: PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1454: /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1455: y[*ridx++] = sum;
1456: }
1457: } else { /* do not use compressed row format */
1458: ii = a->i;
1459: for (i=0; i<m; i++) {
1460: n = ii[i+1] - ii[i];
1461: aj = a->j + ii[i];
1462: aa = a->a + ii[i];
1463: sum = 0.0;
1464: nonzerorow += (n>0);
1465: PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1466: y[i] = sum;
1467: }
1468: }
1469: PetscLogFlops(2.0*a->nz - nonzerorow);
1470: VecRestoreArrayRead(xx,&x);
1471: VecRestoreArray(yy,&y);
1472: return(0);
1473: }
1475: PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1476: {
1477: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1478: PetscScalar *y,*z;
1479: const PetscScalar *x;
1480: const MatScalar *aa;
1481: PetscErrorCode ierr;
1482: PetscInt m = A->rmap->n,*aj,*ii;
1483: PetscInt n,i,*ridx=NULL;
1484: PetscScalar sum;
1485: PetscBool usecprow=a->compressedrow.use;
1488: VecGetArrayRead(xx,&x);
1489: VecGetArrayPair(yy,zz,&y,&z);
1490: if (usecprow) { /* use compressed row format */
1491: if (zz != yy) {
1492: PetscArraycpy(z,y,m);
1493: }
1494: m = a->compressedrow.nrows;
1495: ii = a->compressedrow.i;
1496: ridx = a->compressedrow.rindex;
1497: for (i=0; i<m; i++) {
1498: n = ii[i+1] - ii[i];
1499: aj = a->j + ii[i];
1500: aa = a->a + ii[i];
1501: sum = y[*ridx];
1502: PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1503: z[*ridx++] = sum;
1504: }
1505: } else { /* do not use compressed row format */
1506: ii = a->i;
1507: for (i=0; i<m; i++) {
1508: n = ii[i+1] - ii[i];
1509: aj = a->j + ii[i];
1510: aa = a->a + ii[i];
1511: sum = y[i];
1512: PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1513: z[i] = sum;
1514: }
1515: }
1516: PetscLogFlops(2.0*a->nz);
1517: VecRestoreArrayRead(xx,&x);
1518: VecRestoreArrayPair(yy,zz,&y,&z);
1519: return(0);
1520: }
1522: #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
1523: PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1524: {
1525: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1526: PetscScalar *y,*z;
1527: const PetscScalar *x;
1528: const MatScalar *aa;
1529: PetscErrorCode ierr;
1530: const PetscInt *aj,*ii,*ridx=NULL;
1531: PetscInt m = A->rmap->n,n,i;
1532: PetscScalar sum;
1533: PetscBool usecprow=a->compressedrow.use;
1536: VecGetArrayRead(xx,&x);
1537: VecGetArrayPair(yy,zz,&y,&z);
1538: if (usecprow) { /* use compressed row format */
1539: if (zz != yy) {
1540: PetscArraycpy(z,y,m);
1541: }
1542: m = a->compressedrow.nrows;
1543: ii = a->compressedrow.i;
1544: ridx = a->compressedrow.rindex;
1545: for (i=0; i<m; i++) {
1546: n = ii[i+1] - ii[i];
1547: aj = a->j + ii[i];
1548: aa = a->a + ii[i];
1549: sum = y[*ridx];
1550: PetscSparseDensePlusDot(sum,x,aa,aj,n);
1551: z[*ridx++] = sum;
1552: }
1553: } else { /* do not use compressed row format */
1554: ii = a->i;
1555: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1556: aj = a->j;
1557: aa = a->a;
1558: fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1559: #else
1560: for (i=0; i<m; i++) {
1561: n = ii[i+1] - ii[i];
1562: aj = a->j + ii[i];
1563: aa = a->a + ii[i];
1564: sum = y[i];
1565: PetscSparseDensePlusDot(sum,x,aa,aj,n);
1566: z[i] = sum;
1567: }
1568: #endif
1569: }
1570: PetscLogFlops(2.0*a->nz);
1571: VecRestoreArrayRead(xx,&x);
1572: VecRestoreArrayPair(yy,zz,&y,&z);
1573: return(0);
1574: }
1576: /*
1577: Adds diagonal pointers to sparse matrix structure.
1578: */
1579: PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
1580: {
1581: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1583: PetscInt i,j,m = A->rmap->n;
1586: if (!a->diag) {
1587: PetscMalloc1(m,&a->diag);
1588: PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));
1589: }
1590: for (i=0; i<A->rmap->n; i++) {
1591: a->diag[i] = a->i[i+1];
1592: for (j=a->i[i]; j<a->i[i+1]; j++) {
1593: if (a->j[j] == i) {
1594: a->diag[i] = j;
1595: break;
1596: }
1597: }
1598: }
1599: return(0);
1600: }
1602: PetscErrorCode MatShift_SeqAIJ(Mat A,PetscScalar v)
1603: {
1604: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1605: const PetscInt *diag = (const PetscInt*)a->diag;
1606: const PetscInt *ii = (const PetscInt*) a->i;
1607: PetscInt i,*mdiag = NULL;
1608: PetscErrorCode ierr;
1609: PetscInt cnt = 0; /* how many diagonals are missing */
1612: if (!A->preallocated || !a->nz) {
1613: MatSeqAIJSetPreallocation(A,1,NULL);
1614: MatShift_Basic(A,v);
1615: return(0);
1616: }
1618: if (a->diagonaldense) {
1619: cnt = 0;
1620: } else {
1621: PetscCalloc1(A->rmap->n,&mdiag);
1622: for (i=0; i<A->rmap->n; i++) {
1623: if (diag[i] >= ii[i+1]) {
1624: cnt++;
1625: mdiag[i] = 1;
1626: }
1627: }
1628: }
1629: if (!cnt) {
1630: MatShift_Basic(A,v);
1631: } else {
1632: PetscScalar *olda = a->a; /* preserve pointers to current matrix nonzeros structure and values */
1633: PetscInt *oldj = a->j, *oldi = a->i;
1634: PetscBool singlemalloc = a->singlemalloc,free_a = a->free_a,free_ij = a->free_ij;
1636: a->a = NULL;
1637: a->j = NULL;
1638: a->i = NULL;
1639: /* increase the values in imax for each row where a diagonal is being inserted then reallocate the matrix data structures */
1640: for (i=0; i<A->rmap->n; i++) {
1641: a->imax[i] += mdiag[i];
1642: a->imax[i] = PetscMin(a->imax[i],A->cmap->n);
1643: }
1644: MatSeqAIJSetPreallocation_SeqAIJ(A,0,a->imax);
1646: /* copy old values into new matrix data structure */
1647: for (i=0; i<A->rmap->n; i++) {
1648: MatSetValues(A,1,&i,a->imax[i] - mdiag[i],&oldj[oldi[i]],&olda[oldi[i]],ADD_VALUES);
1649: if (i < A->cmap->n) {
1650: MatSetValue(A,i,i,v,ADD_VALUES);
1651: }
1652: }
1653: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1654: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1655: if (singlemalloc) {
1656: PetscFree3(olda,oldj,oldi);
1657: } else {
1658: if (free_a) {PetscFree(olda);}
1659: if (free_ij) {PetscFree(oldj);}
1660: if (free_ij) {PetscFree(oldi);}
1661: }
1662: }
1663: PetscFree(mdiag);
1664: a->diagonaldense = PETSC_TRUE;
1665: return(0);
1666: }
1668: /*
1669: Checks for missing diagonals
1670: */
1671: PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool *missing,PetscInt *d)
1672: {
1673: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1674: PetscInt *diag,*ii = a->i,i;
1678: *missing = PETSC_FALSE;
1679: if (A->rmap->n > 0 && !ii) {
1680: *missing = PETSC_TRUE;
1681: if (d) *d = 0;
1682: PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");
1683: } else {
1684: PetscInt n;
1685: n = PetscMin(A->rmap->n, A->cmap->n);
1686: diag = a->diag;
1687: for (i=0; i<n; i++) {
1688: if (diag[i] >= ii[i+1]) {
1689: *missing = PETSC_TRUE;
1690: if (d) *d = i;
1691: PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);
1692: break;
1693: }
1694: }
1695: }
1696: return(0);
1697: }
1699: #include <petscblaslapack.h>
1700: #include <petsc/private/kernels/blockinvert.h>
1702: /*
1703: Note that values is allocated externally by the PC and then passed into this routine
1704: */
1705: PetscErrorCode MatInvertVariableBlockDiagonal_SeqAIJ(Mat A,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *diag)
1706: {
1707: PetscErrorCode ierr;
1708: PetscInt n = A->rmap->n, i, ncnt = 0, *indx,j,bsizemax = 0,*v_pivots;
1709: PetscBool allowzeropivot,zeropivotdetected=PETSC_FALSE;
1710: const PetscReal shift = 0.0;
1711: PetscInt ipvt[5];
1712: PetscScalar work[25],*v_work;
1715: allowzeropivot = PetscNot(A->erroriffailure);
1716: for (i=0; i<nblocks; i++) ncnt += bsizes[i];
1717: if (ncnt != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Total blocksizes %D doesn't match number matrix rows %D",ncnt,n);
1718: for (i=0; i<nblocks; i++) {
1719: bsizemax = PetscMax(bsizemax,bsizes[i]);
1720: }
1721: PetscMalloc1(bsizemax,&indx);
1722: if (bsizemax > 7) {
1723: PetscMalloc2(bsizemax,&v_work,bsizemax,&v_pivots);
1724: }
1725: ncnt = 0;
1726: for (i=0; i<nblocks; i++) {
1727: for (j=0; j<bsizes[i]; j++) indx[j] = ncnt+j;
1728: MatGetValues(A,bsizes[i],indx,bsizes[i],indx,diag);
1729: switch (bsizes[i]) {
1730: case 1:
1731: *diag = 1.0/(*diag);
1732: break;
1733: case 2:
1734: PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
1735: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1736: PetscKernel_A_gets_transpose_A_2(diag);
1737: break;
1738: case 3:
1739: PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);
1740: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1741: PetscKernel_A_gets_transpose_A_3(diag);
1742: break;
1743: case 4:
1744: PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);
1745: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1746: PetscKernel_A_gets_transpose_A_4(diag);
1747: break;
1748: case 5:
1749: PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);
1750: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1751: PetscKernel_A_gets_transpose_A_5(diag);
1752: break;
1753: case 6:
1754: PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);
1755: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1756: PetscKernel_A_gets_transpose_A_6(diag);
1757: break;
1758: case 7:
1759: PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);
1760: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1761: PetscKernel_A_gets_transpose_A_7(diag);
1762: break;
1763: default:
1764: PetscKernel_A_gets_inverse_A(bsizes[i],diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);
1765: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1766: PetscKernel_A_gets_transpose_A_N(diag,bsizes[i]);
1767: }
1768: ncnt += bsizes[i];
1769: diag += bsizes[i]*bsizes[i];
1770: }
1771: if (bsizemax > 7) {
1772: PetscFree2(v_work,v_pivots);
1773: }
1774: PetscFree(indx);
1775: return(0);
1776: }
1778: /*
1779: Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1780: */
1781: PetscErrorCode MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
1782: {
1783: Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data;
1785: PetscInt i,*diag,m = A->rmap->n;
1786: MatScalar *v = a->a;
1787: PetscScalar *idiag,*mdiag;
1790: if (a->idiagvalid) return(0);
1791: MatMarkDiagonal_SeqAIJ(A);
1792: diag = a->diag;
1793: if (!a->idiag) {
1794: PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);
1795: PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));
1796: v = a->a;
1797: }
1798: mdiag = a->mdiag;
1799: idiag = a->idiag;
1801: if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
1802: for (i=0; i<m; i++) {
1803: mdiag[i] = v[diag[i]];
1804: if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1805: if (PetscRealPart(fshift)) {
1806: PetscInfo1(A,"Zero diagonal on row %D\n",i);
1807: A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1808: A->factorerror_zeropivot_value = 0.0;
1809: A->factorerror_zeropivot_row = i;
1810: } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
1811: }
1812: idiag[i] = 1.0/v[diag[i]];
1813: }
1814: PetscLogFlops(m);
1815: } else {
1816: for (i=0; i<m; i++) {
1817: mdiag[i] = v[diag[i]];
1818: idiag[i] = omega/(fshift + v[diag[i]]);
1819: }
1820: PetscLogFlops(2.0*m);
1821: }
1822: a->idiagvalid = PETSC_TRUE;
1823: return(0);
1824: }
1826: #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
1827: PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
1828: {
1829: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1830: PetscScalar *x,d,sum,*t,scale;
1831: const MatScalar *v,*idiag=0,*mdiag;
1832: const PetscScalar *b, *bs,*xb, *ts;
1833: PetscErrorCode ierr;
1834: PetscInt n,m = A->rmap->n,i;
1835: const PetscInt *idx,*diag;
1838: its = its*lits;
1840: if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
1841: if (!a->idiagvalid) {MatInvertDiagonal_SeqAIJ(A,omega,fshift);}
1842: a->fshift = fshift;
1843: a->omega = omega;
1845: diag = a->diag;
1846: t = a->ssor_work;
1847: idiag = a->idiag;
1848: mdiag = a->mdiag;
1850: VecGetArray(xx,&x);
1851: VecGetArrayRead(bb,&b);
1852: /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1853: if (flag == SOR_APPLY_UPPER) {
1854: /* apply (U + D/omega) to the vector */
1855: bs = b;
1856: for (i=0; i<m; i++) {
1857: d = fshift + mdiag[i];
1858: n = a->i[i+1] - diag[i] - 1;
1859: idx = a->j + diag[i] + 1;
1860: v = a->a + diag[i] + 1;
1861: sum = b[i]*d/omega;
1862: PetscSparseDensePlusDot(sum,bs,v,idx,n);
1863: x[i] = sum;
1864: }
1865: VecRestoreArray(xx,&x);
1866: VecRestoreArrayRead(bb,&b);
1867: PetscLogFlops(a->nz);
1868: return(0);
1869: }
1871: if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
1872: else if (flag & SOR_EISENSTAT) {
1873: /* Let A = L + U + D; where L is lower triangular,
1874: U is upper triangular, E = D/omega; This routine applies
1876: (L + E)^{-1} A (U + E)^{-1}
1878: to a vector efficiently using Eisenstat's trick.
1879: */
1880: scale = (2.0/omega) - 1.0;
1882: /* x = (E + U)^{-1} b */
1883: for (i=m-1; i>=0; i--) {
1884: n = a->i[i+1] - diag[i] - 1;
1885: idx = a->j + diag[i] + 1;
1886: v = a->a + diag[i] + 1;
1887: sum = b[i];
1888: PetscSparseDenseMinusDot(sum,x,v,idx,n);
1889: x[i] = sum*idiag[i];
1890: }
1892: /* t = b - (2*E - D)x */
1893: v = a->a;
1894: for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i];
1896: /* t = (E + L)^{-1}t */
1897: ts = t;
1898: diag = a->diag;
1899: for (i=0; i<m; i++) {
1900: n = diag[i] - a->i[i];
1901: idx = a->j + a->i[i];
1902: v = a->a + a->i[i];
1903: sum = t[i];
1904: PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1905: t[i] = sum*idiag[i];
1906: /* x = x + t */
1907: x[i] += t[i];
1908: }
1910: PetscLogFlops(6.0*m-1 + 2.0*a->nz);
1911: VecRestoreArray(xx,&x);
1912: VecRestoreArrayRead(bb,&b);
1913: return(0);
1914: }
1915: if (flag & SOR_ZERO_INITIAL_GUESS) {
1916: if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1917: for (i=0; i<m; i++) {
1918: n = diag[i] - a->i[i];
1919: idx = a->j + a->i[i];
1920: v = a->a + a->i[i];
1921: sum = b[i];
1922: PetscSparseDenseMinusDot(sum,x,v,idx,n);
1923: t[i] = sum;
1924: x[i] = sum*idiag[i];
1925: }
1926: xb = t;
1927: PetscLogFlops(a->nz);
1928: } else xb = b;
1929: if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1930: for (i=m-1; i>=0; i--) {
1931: n = a->i[i+1] - diag[i] - 1;
1932: idx = a->j + diag[i] + 1;
1933: v = a->a + diag[i] + 1;
1934: sum = xb[i];
1935: PetscSparseDenseMinusDot(sum,x,v,idx,n);
1936: if (xb == b) {
1937: x[i] = sum*idiag[i];
1938: } else {
1939: x[i] = (1-omega)*x[i] + sum*idiag[i]; /* omega in idiag */
1940: }
1941: }
1942: PetscLogFlops(a->nz); /* assumes 1/2 in upper */
1943: }
1944: its--;
1945: }
1946: while (its--) {
1947: if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1948: for (i=0; i<m; i++) {
1949: /* lower */
1950: n = diag[i] - a->i[i];
1951: idx = a->j + a->i[i];
1952: v = a->a + a->i[i];
1953: sum = b[i];
1954: PetscSparseDenseMinusDot(sum,x,v,idx,n);
1955: t[i] = sum; /* save Section 1.5 Writing Application Codes with PETSc of the lower-triangular part */
1956: /* upper */
1957: n = a->i[i+1] - diag[i] - 1;
1958: idx = a->j + diag[i] + 1;
1959: v = a->a + diag[i] + 1;
1960: PetscSparseDenseMinusDot(sum,x,v,idx,n);
1961: x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
1962: }
1963: xb = t;
1964: PetscLogFlops(2.0*a->nz);
1965: } else xb = b;
1966: if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1967: for (i=m-1; i>=0; i--) {
1968: sum = xb[i];
1969: if (xb == b) {
1970: /* whole matrix (no checkpointing available) */
1971: n = a->i[i+1] - a->i[i];
1972: idx = a->j + a->i[i];
1973: v = a->a + a->i[i];
1974: PetscSparseDenseMinusDot(sum,x,v,idx,n);
1975: x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
1976: } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1977: n = a->i[i+1] - diag[i] - 1;
1978: idx = a->j + diag[i] + 1;
1979: v = a->a + diag[i] + 1;
1980: PetscSparseDenseMinusDot(sum,x,v,idx,n);
1981: x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
1982: }
1983: }
1984: if (xb == b) {
1985: PetscLogFlops(2.0*a->nz);
1986: } else {
1987: PetscLogFlops(a->nz); /* assumes 1/2 in upper */
1988: }
1989: }
1990: }
1991: VecRestoreArray(xx,&x);
1992: VecRestoreArrayRead(bb,&b);
1993: return(0);
1994: }
1997: PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
1998: {
1999: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2002: info->block_size = 1.0;
2003: info->nz_allocated = a->maxnz;
2004: info->nz_used = a->nz;
2005: info->nz_unneeded = (a->maxnz - a->nz);
2006: info->assemblies = A->num_ass;
2007: info->mallocs = A->info.mallocs;
2008: info->memory = ((PetscObject)A)->mem;
2009: if (A->factortype) {
2010: info->fill_ratio_given = A->info.fill_ratio_given;
2011: info->fill_ratio_needed = A->info.fill_ratio_needed;
2012: info->factor_mallocs = A->info.factor_mallocs;
2013: } else {
2014: info->fill_ratio_given = 0;
2015: info->fill_ratio_needed = 0;
2016: info->factor_mallocs = 0;
2017: }
2018: return(0);
2019: }
2021: PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
2022: {
2023: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2024: PetscInt i,m = A->rmap->n - 1;
2025: PetscErrorCode ierr;
2026: const PetscScalar *xx;
2027: PetscScalar *bb;
2028: PetscInt d = 0;
2031: if (x && b) {
2032: VecGetArrayRead(x,&xx);
2033: VecGetArray(b,&bb);
2034: for (i=0; i<N; i++) {
2035: if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2036: if (rows[i] >= A->cmap->n) continue;
2037: bb[rows[i]] = diag*xx[rows[i]];
2038: }
2039: VecRestoreArrayRead(x,&xx);
2040: VecRestoreArray(b,&bb);
2041: }
2043: if (a->keepnonzeropattern) {
2044: for (i=0; i<N; i++) {
2045: if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2046: PetscArrayzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]);
2047: }
2048: if (diag != 0.0) {
2049: for (i=0; i<N; i++) {
2050: d = rows[i];
2051: if (rows[i] >= A->cmap->n) continue;
2052: if (a->diag[d] >= a->i[d+1]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in the zeroed row %D",d);
2053: }
2054: for (i=0; i<N; i++) {
2055: if (rows[i] >= A->cmap->n) continue;
2056: a->a[a->diag[rows[i]]] = diag;
2057: }
2058: }
2059: } else {
2060: if (diag != 0.0) {
2061: for (i=0; i<N; i++) {
2062: if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2063: if (a->ilen[rows[i]] > 0) {
2064: if (rows[i] >= A->cmap->n) {
2065: a->ilen[rows[i]] = 0;
2066: } else {
2067: a->ilen[rows[i]] = 1;
2068: a->a[a->i[rows[i]]] = diag;
2069: a->j[a->i[rows[i]]] = rows[i];
2070: }
2071: } else if (rows[i] < A->cmap->n) { /* in case row was completely empty */
2072: MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);
2073: }
2074: }
2075: } else {
2076: for (i=0; i<N; i++) {
2077: if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2078: a->ilen[rows[i]] = 0;
2079: }
2080: }
2081: A->nonzerostate++;
2082: }
2083: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2084: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
2085: #endif
2086: (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);
2087: return(0);
2088: }
2090: PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
2091: {
2092: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2093: PetscInt i,j,m = A->rmap->n - 1,d = 0;
2094: PetscErrorCode ierr;
2095: PetscBool missing,*zeroed,vecs = PETSC_FALSE;
2096: const PetscScalar *xx;
2097: PetscScalar *bb;
2100: if (x && b) {
2101: VecGetArrayRead(x,&xx);
2102: VecGetArray(b,&bb);
2103: vecs = PETSC_TRUE;
2104: }
2105: PetscCalloc1(A->rmap->n,&zeroed);
2106: for (i=0; i<N; i++) {
2107: if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2108: PetscArrayzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]);
2110: zeroed[rows[i]] = PETSC_TRUE;
2111: }
2112: for (i=0; i<A->rmap->n; i++) {
2113: if (!zeroed[i]) {
2114: for (j=a->i[i]; j<a->i[i+1]; j++) {
2115: if (a->j[j] < A->rmap->n && zeroed[a->j[j]]) {
2116: if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
2117: a->a[j] = 0.0;
2118: }
2119: }
2120: } else if (vecs && i < A->cmap->N) bb[i] = diag*xx[i];
2121: }
2122: if (x && b) {
2123: VecRestoreArrayRead(x,&xx);
2124: VecRestoreArray(b,&bb);
2125: }
2126: PetscFree(zeroed);
2127: if (diag != 0.0) {
2128: MatMissingDiagonal_SeqAIJ(A,&missing,&d);
2129: if (missing) {
2130: for (i=0; i<N; i++) {
2131: if (rows[i] >= A->cmap->N) continue;
2132: if (a->nonew && rows[i] >= d) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D (%D)",d,rows[i]);
2133: MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);
2134: }
2135: } else {
2136: for (i=0; i<N; i++) {
2137: a->a[a->diag[rows[i]]] = diag;
2138: }
2139: }
2140: }
2141: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2142: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
2143: #endif
2144: (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);
2145: return(0);
2146: }
2148: PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
2149: {
2150: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2151: PetscInt *itmp;
2154: if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
2156: *nz = a->i[row+1] - a->i[row];
2157: if (v) *v = a->a + a->i[row];
2158: if (idx) {
2159: itmp = a->j + a->i[row];
2160: if (*nz) *idx = itmp;
2161: else *idx = 0;
2162: }
2163: return(0);
2164: }
2166: /* remove this function? */
2167: PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
2168: {
2170: return(0);
2171: }
2173: PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
2174: {
2175: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2176: MatScalar *v = a->a;
2177: PetscReal sum = 0.0;
2179: PetscInt i,j;
2182: if (type == NORM_FROBENIUS) {
2183: #if defined(PETSC_USE_REAL___FP16)
2184: PetscBLASInt one = 1,nz = a->nz;
2185: *nrm = BLASnrm2_(&nz,v,&one);
2186: #else
2187: for (i=0; i<a->nz; i++) {
2188: sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
2189: }
2190: *nrm = PetscSqrtReal(sum);
2191: #endif
2192: PetscLogFlops(2.0*a->nz);
2193: } else if (type == NORM_1) {
2194: PetscReal *tmp;
2195: PetscInt *jj = a->j;
2196: PetscCalloc1(A->cmap->n+1,&tmp);
2197: *nrm = 0.0;
2198: for (j=0; j<a->nz; j++) {
2199: tmp[*jj++] += PetscAbsScalar(*v); v++;
2200: }
2201: for (j=0; j<A->cmap->n; j++) {
2202: if (tmp[j] > *nrm) *nrm = tmp[j];
2203: }
2204: PetscFree(tmp);
2205: PetscLogFlops(PetscMax(a->nz-1,0));
2206: } else if (type == NORM_INFINITY) {
2207: *nrm = 0.0;
2208: for (j=0; j<A->rmap->n; j++) {
2209: v = a->a + a->i[j];
2210: sum = 0.0;
2211: for (i=0; i<a->i[j+1]-a->i[j]; i++) {
2212: sum += PetscAbsScalar(*v); v++;
2213: }
2214: if (sum > *nrm) *nrm = sum;
2215: }
2216: PetscLogFlops(PetscMax(a->nz-1,0));
2217: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
2218: return(0);
2219: }
2221: /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
2222: PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
2223: {
2225: PetscInt i,j,anzj;
2226: Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b;
2227: PetscInt an=A->cmap->N,am=A->rmap->N;
2228: PetscInt *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
2231: /* Allocate space for symbolic transpose info and work array */
2232: PetscCalloc1(an+1,&ati);
2233: PetscMalloc1(ai[am],&atj);
2234: PetscMalloc1(an,&atfill);
2236: /* Walk through aj and count ## of non-zeros in each row of A^T. */
2237: /* Note: offset by 1 for fast conversion into csr format. */
2238: for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1;
2239: /* Form ati for csr format of A^T. */
2240: for (i=0;i<an;i++) ati[i+1] += ati[i];
2242: /* Copy ati into atfill so we have locations of the next free space in atj */
2243: PetscArraycpy(atfill,ati,an);
2245: /* Walk through A row-wise and mark nonzero entries of A^T. */
2246: for (i=0;i<am;i++) {
2247: anzj = ai[i+1] - ai[i];
2248: for (j=0;j<anzj;j++) {
2249: atj[atfill[*aj]] = i;
2250: atfill[*aj++] += 1;
2251: }
2252: }
2254: /* Clean up temporary space and complete requests. */
2255: PetscFree(atfill);
2256: MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);
2257: MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));
2258: MatSetType(*B,((PetscObject)A)->type_name);
2260: b = (Mat_SeqAIJ*)((*B)->data);
2261: b->free_a = PETSC_FALSE;
2262: b->free_ij = PETSC_TRUE;
2263: b->nonew = 0;
2264: return(0);
2265: }
2267: PetscErrorCode MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool *f)
2268: {
2269: Mat_SeqAIJ *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
2270: PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr;
2271: MatScalar *va,*vb;
2273: PetscInt ma,na,mb,nb, i;
2276: MatGetSize(A,&ma,&na);
2277: MatGetSize(B,&mb,&nb);
2278: if (ma!=nb || na!=mb) {
2279: *f = PETSC_FALSE;
2280: return(0);
2281: }
2282: aii = aij->i; bii = bij->i;
2283: adx = aij->j; bdx = bij->j;
2284: va = aij->a; vb = bij->a;
2285: PetscMalloc1(ma,&aptr);
2286: PetscMalloc1(mb,&bptr);
2287: for (i=0; i<ma; i++) aptr[i] = aii[i];
2288: for (i=0; i<mb; i++) bptr[i] = bii[i];
2290: *f = PETSC_TRUE;
2291: for (i=0; i<ma; i++) {
2292: while (aptr[i]<aii[i+1]) {
2293: PetscInt idc,idr;
2294: PetscScalar vc,vr;
2295: /* column/row index/value */
2296: idc = adx[aptr[i]];
2297: idr = bdx[bptr[idc]];
2298: vc = va[aptr[i]];
2299: vr = vb[bptr[idc]];
2300: if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
2301: *f = PETSC_FALSE;
2302: goto done;
2303: } else {
2304: aptr[i]++;
2305: if (B || i!=idc) bptr[idc]++;
2306: }
2307: }
2308: }
2309: done:
2310: PetscFree(aptr);
2311: PetscFree(bptr);
2312: return(0);
2313: }
2315: PetscErrorCode MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool *f)
2316: {
2317: Mat_SeqAIJ *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
2318: PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr;
2319: MatScalar *va,*vb;
2321: PetscInt ma,na,mb,nb, i;
2324: MatGetSize(A,&ma,&na);
2325: MatGetSize(B,&mb,&nb);
2326: if (ma!=nb || na!=mb) {
2327: *f = PETSC_FALSE;
2328: return(0);
2329: }
2330: aii = aij->i; bii = bij->i;
2331: adx = aij->j; bdx = bij->j;
2332: va = aij->a; vb = bij->a;
2333: PetscMalloc1(ma,&aptr);
2334: PetscMalloc1(mb,&bptr);
2335: for (i=0; i<ma; i++) aptr[i] = aii[i];
2336: for (i=0; i<mb; i++) bptr[i] = bii[i];
2338: *f = PETSC_TRUE;
2339: for (i=0; i<ma; i++) {
2340: while (aptr[i]<aii[i+1]) {
2341: PetscInt idc,idr;
2342: PetscScalar vc,vr;
2343: /* column/row index/value */
2344: idc = adx[aptr[i]];
2345: idr = bdx[bptr[idc]];
2346: vc = va[aptr[i]];
2347: vr = vb[bptr[idc]];
2348: if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
2349: *f = PETSC_FALSE;
2350: goto done;
2351: } else {
2352: aptr[i]++;
2353: if (B || i!=idc) bptr[idc]++;
2354: }
2355: }
2356: }
2357: done:
2358: PetscFree(aptr);
2359: PetscFree(bptr);
2360: return(0);
2361: }
2363: PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool *f)
2364: {
2368: MatIsTranspose_SeqAIJ(A,A,tol,f);
2369: return(0);
2370: }
2372: PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool *f)
2373: {
2377: MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);
2378: return(0);
2379: }
2381: PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
2382: {
2383: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2384: const PetscScalar *l,*r;
2385: PetscScalar x;
2386: MatScalar *v;
2387: PetscErrorCode ierr;
2388: PetscInt i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz;
2389: const PetscInt *jj;
2392: if (ll) {
2393: /* The local size is used so that VecMPI can be passed to this routine
2394: by MatDiagonalScale_MPIAIJ */
2395: VecGetLocalSize(ll,&m);
2396: if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
2397: VecGetArrayRead(ll,&l);
2398: v = a->a;
2399: for (i=0; i<m; i++) {
2400: x = l[i];
2401: M = a->i[i+1] - a->i[i];
2402: for (j=0; j<M; j++) (*v++) *= x;
2403: }
2404: VecRestoreArrayRead(ll,&l);
2405: PetscLogFlops(nz);
2406: }
2407: if (rr) {
2408: VecGetLocalSize(rr,&n);
2409: if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
2410: VecGetArrayRead(rr,&r);
2411: v = a->a; jj = a->j;
2412: for (i=0; i<nz; i++) (*v++) *= r[*jj++];
2413: VecRestoreArrayRead(rr,&r);
2414: PetscLogFlops(nz);
2415: }
2416: MatSeqAIJInvalidateDiagonal(A);
2417: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2418: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
2419: #endif
2420: return(0);
2421: }
2423: PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
2424: {
2425: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*c;
2427: PetscInt *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
2428: PetscInt row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
2429: const PetscInt *irow,*icol;
2430: PetscInt nrows,ncols;
2431: PetscInt *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
2432: MatScalar *a_new,*mat_a;
2433: Mat C;
2434: PetscBool stride;
2438: ISGetIndices(isrow,&irow);
2439: ISGetLocalSize(isrow,&nrows);
2440: ISGetLocalSize(iscol,&ncols);
2442: PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);
2443: if (stride) {
2444: ISStrideGetInfo(iscol,&first,&step);
2445: } else {
2446: first = 0;
2447: step = 0;
2448: }
2449: if (stride && step == 1) {
2450: /* special case of contiguous rows */
2451: PetscMalloc2(nrows,&lens,nrows,&starts);
2452: /* loop over new rows determining lens and starting points */
2453: for (i=0; i<nrows; i++) {
2454: kstart = ai[irow[i]];
2455: kend = kstart + ailen[irow[i]];
2456: starts[i] = kstart;
2457: for (k=kstart; k<kend; k++) {
2458: if (aj[k] >= first) {
2459: starts[i] = k;
2460: break;
2461: }
2462: }
2463: sum = 0;
2464: while (k < kend) {
2465: if (aj[k++] >= first+ncols) break;
2466: sum++;
2467: }
2468: lens[i] = sum;
2469: }
2470: /* create submatrix */
2471: if (scall == MAT_REUSE_MATRIX) {
2472: PetscInt n_cols,n_rows;
2473: MatGetSize(*B,&n_rows,&n_cols);
2474: if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2475: MatZeroEntries(*B);
2476: C = *B;
2477: } else {
2478: PetscInt rbs,cbs;
2479: MatCreate(PetscObjectComm((PetscObject)A),&C);
2480: MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);
2481: ISGetBlockSize(isrow,&rbs);
2482: ISGetBlockSize(iscol,&cbs);
2483: MatSetBlockSizes(C,rbs,cbs);
2484: MatSetType(C,((PetscObject)A)->type_name);
2485: MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);
2486: }
2487: c = (Mat_SeqAIJ*)C->data;
2489: /* loop over rows inserting into submatrix */
2490: a_new = c->a;
2491: j_new = c->j;
2492: i_new = c->i;
2494: for (i=0; i<nrows; i++) {
2495: ii = starts[i];
2496: lensi = lens[i];
2497: for (k=0; k<lensi; k++) {
2498: *j_new++ = aj[ii+k] - first;
2499: }
2500: PetscArraycpy(a_new,a->a + starts[i],lensi);
2501: a_new += lensi;
2502: i_new[i+1] = i_new[i] + lensi;
2503: c->ilen[i] = lensi;
2504: }
2505: PetscFree2(lens,starts);
2506: } else {
2507: ISGetIndices(iscol,&icol);
2508: PetscCalloc1(oldcols,&smap);
2509: PetscMalloc1(1+nrows,&lens);
2510: for (i=0; i<ncols; i++) {
2511: #if defined(PETSC_USE_DEBUG)
2512: 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);
2513: #endif
2514: smap[icol[i]] = i+1;
2515: }
2517: /* determine lens of each row */
2518: for (i=0; i<nrows; i++) {
2519: kstart = ai[irow[i]];
2520: kend = kstart + a->ilen[irow[i]];
2521: lens[i] = 0;
2522: for (k=kstart; k<kend; k++) {
2523: if (smap[aj[k]]) {
2524: lens[i]++;
2525: }
2526: }
2527: }
2528: /* Create and fill new matrix */
2529: if (scall == MAT_REUSE_MATRIX) {
2530: PetscBool equal;
2532: c = (Mat_SeqAIJ*)((*B)->data);
2533: if ((*B)->rmap->n != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2534: PetscArraycmp(c->ilen,lens,(*B)->rmap->n,&equal);
2535: if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2536: PetscArrayzero(c->ilen,(*B)->rmap->n);
2537: C = *B;
2538: } else {
2539: PetscInt rbs,cbs;
2540: MatCreate(PetscObjectComm((PetscObject)A),&C);
2541: MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);
2542: ISGetBlockSize(isrow,&rbs);
2543: ISGetBlockSize(iscol,&cbs);
2544: MatSetBlockSizes(C,rbs,cbs);
2545: MatSetType(C,((PetscObject)A)->type_name);
2546: MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);
2547: }
2548: c = (Mat_SeqAIJ*)(C->data);
2549: for (i=0; i<nrows; i++) {
2550: row = irow[i];
2551: kstart = ai[row];
2552: kend = kstart + a->ilen[row];
2553: mat_i = c->i[i];
2554: mat_j = c->j + mat_i;
2555: mat_a = c->a + mat_i;
2556: mat_ilen = c->ilen + i;
2557: for (k=kstart; k<kend; k++) {
2558: if ((tcol=smap[a->j[k]])) {
2559: *mat_j++ = tcol - 1;
2560: *mat_a++ = a->a[k];
2561: (*mat_ilen)++;
2563: }
2564: }
2565: }
2566: /* Free work space */
2567: ISRestoreIndices(iscol,&icol);
2568: PetscFree(smap);
2569: PetscFree(lens);
2570: /* sort */
2571: for (i = 0; i < nrows; i++) {
2572: PetscInt ilen;
2574: mat_i = c->i[i];
2575: mat_j = c->j + mat_i;
2576: mat_a = c->a + mat_i;
2577: ilen = c->ilen[i];
2578: PetscSortIntWithScalarArray(ilen,mat_j,mat_a);
2579: }
2580: }
2581: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2582: MatBindToCPU(C,A->boundtocpu);
2583: #endif
2584: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2585: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
2587: ISRestoreIndices(isrow,&irow);
2588: *B = C;
2589: return(0);
2590: }
2592: PetscErrorCode MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
2593: {
2595: Mat B;
2598: if (scall == MAT_INITIAL_MATRIX) {
2599: MatCreate(subComm,&B);
2600: MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);
2601: MatSetBlockSizesFromMats(B,mat,mat);
2602: MatSetType(B,MATSEQAIJ);
2603: MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);
2604: *subMat = B;
2605: } else {
2606: MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);
2607: }
2608: return(0);
2609: }
2611: PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2612: {
2613: Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
2615: Mat outA;
2616: PetscBool row_identity,col_identity;
2619: if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
2621: ISIdentity(row,&row_identity);
2622: ISIdentity(col,&col_identity);
2624: outA = inA;
2625: outA->factortype = MAT_FACTOR_LU;
2626: PetscFree(inA->solvertype);
2627: PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);
2629: PetscObjectReference((PetscObject)row);
2630: ISDestroy(&a->row);
2632: a->row = row;
2634: PetscObjectReference((PetscObject)col);
2635: ISDestroy(&a->col);
2637: a->col = col;
2639: /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
2640: ISDestroy(&a->icol);
2641: ISInvertPermutation(col,PETSC_DECIDE,&a->icol);
2642: PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);
2644: if (!a->solve_work) { /* this matrix may have been factored before */
2645: PetscMalloc1(inA->rmap->n+1,&a->solve_work);
2646: PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));
2647: }
2649: MatMarkDiagonal_SeqAIJ(inA);
2650: if (row_identity && col_identity) {
2651: MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);
2652: } else {
2653: MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);
2654: }
2655: return(0);
2656: }
2658: PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2659: {
2660: Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
2661: PetscScalar oalpha = alpha;
2663: PetscBLASInt one = 1,bnz;
2666: PetscBLASIntCast(a->nz,&bnz);
2667: PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2668: PetscLogFlops(a->nz);
2669: MatSeqAIJInvalidateDiagonal(inA);
2670: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2671: if (inA->offloadmask != PETSC_OFFLOAD_UNALLOCATED) inA->offloadmask = PETSC_OFFLOAD_CPU;
2672: #endif
2673: return(0);
2674: }
2676: PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj)
2677: {
2679: PetscInt i;
2682: if (!submatj->id) { /* delete data that are linked only to submats[id=0] */
2683: PetscFree4(submatj->sbuf1,submatj->ptr,submatj->tmp,submatj->ctr);
2685: for (i=0; i<submatj->nrqr; ++i) {
2686: PetscFree(submatj->sbuf2[i]);
2687: }
2688: PetscFree3(submatj->sbuf2,submatj->req_size,submatj->req_source1);
2690: if (submatj->rbuf1) {
2691: PetscFree(submatj->rbuf1[0]);
2692: PetscFree(submatj->rbuf1);
2693: }
2695: for (i=0; i<submatj->nrqs; ++i) {
2696: PetscFree(submatj->rbuf3[i]);
2697: }
2698: PetscFree3(submatj->req_source2,submatj->rbuf2,submatj->rbuf3);
2699: PetscFree(submatj->pa);
2700: }
2702: #if defined(PETSC_USE_CTABLE)
2703: PetscTableDestroy((PetscTable*)&submatj->rmap);
2704: if (submatj->cmap_loc) {PetscFree(submatj->cmap_loc);}
2705: PetscFree(submatj->rmap_loc);
2706: #else
2707: PetscFree(submatj->rmap);
2708: #endif
2710: if (!submatj->allcolumns) {
2711: #if defined(PETSC_USE_CTABLE)
2712: PetscTableDestroy((PetscTable*)&submatj->cmap);
2713: #else
2714: PetscFree(submatj->cmap);
2715: #endif
2716: }
2717: PetscFree(submatj->row2proc);
2719: PetscFree(submatj);
2720: return(0);
2721: }
2723: PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C)
2724: {
2726: Mat_SeqAIJ *c = (Mat_SeqAIJ*)C->data;
2727: Mat_SubSppt *submatj = c->submatis1;
2730: (*submatj->destroy)(C);
2731: MatDestroySubMatrix_Private(submatj);
2732: return(0);
2733: }
2735: PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n,Mat *mat[])
2736: {
2738: PetscInt i;
2739: Mat C;
2740: Mat_SeqAIJ *c;
2741: Mat_SubSppt *submatj;
2744: for (i=0; i<n; i++) {
2745: C = (*mat)[i];
2746: c = (Mat_SeqAIJ*)C->data;
2747: submatj = c->submatis1;
2748: if (submatj) {
2749: if (--((PetscObject)C)->refct <= 0) {
2750: (*submatj->destroy)(C);
2751: MatDestroySubMatrix_Private(submatj);
2752: PetscFree(C->defaultvectype);
2753: PetscLayoutDestroy(&C->rmap);
2754: PetscLayoutDestroy(&C->cmap);
2755: PetscHeaderDestroy(&C);
2756: }
2757: } else {
2758: MatDestroy(&C);
2759: }
2760: }
2762: /* Destroy Dummy submatrices created for reuse */
2763: MatDestroySubMatrices_Dummy(n,mat);
2765: PetscFree(*mat);
2766: return(0);
2767: }
2769: PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2770: {
2772: PetscInt i;
2775: if (scall == MAT_INITIAL_MATRIX) {
2776: PetscCalloc1(n+1,B);
2777: }
2779: for (i=0; i<n; i++) {
2780: MatCreateSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);
2781: }
2782: return(0);
2783: }
2785: PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
2786: {
2787: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2789: PetscInt row,i,j,k,l,m,n,*nidx,isz,val;
2790: const PetscInt *idx;
2791: PetscInt start,end,*ai,*aj;
2792: PetscBT table;
2795: m = A->rmap->n;
2796: ai = a->i;
2797: aj = a->j;
2799: if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
2801: PetscMalloc1(m+1,&nidx);
2802: PetscBTCreate(m,&table);
2804: for (i=0; i<is_max; i++) {
2805: /* Initialize the two local arrays */
2806: isz = 0;
2807: PetscBTMemzero(m,table);
2809: /* Extract the indices, assume there can be duplicate entries */
2810: ISGetIndices(is[i],&idx);
2811: ISGetLocalSize(is[i],&n);
2813: /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2814: for (j=0; j<n; ++j) {
2815: if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
2816: }
2817: ISRestoreIndices(is[i],&idx);
2818: ISDestroy(&is[i]);
2820: k = 0;
2821: for (j=0; j<ov; j++) { /* for each overlap */
2822: n = isz;
2823: for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2824: row = nidx[k];
2825: start = ai[row];
2826: end = ai[row+1];
2827: for (l = start; l<end; l++) {
2828: val = aj[l];
2829: if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2830: }
2831: }
2832: }
2833: ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));
2834: }
2835: PetscBTDestroy(&table);
2836: PetscFree(nidx);
2837: return(0);
2838: }
2840: /* -------------------------------------------------------------- */
2841: PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
2842: {
2843: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2845: PetscInt i,nz = 0,m = A->rmap->n,n = A->cmap->n;
2846: const PetscInt *row,*col;
2847: PetscInt *cnew,j,*lens;
2848: IS icolp,irowp;
2849: PetscInt *cwork = NULL;
2850: PetscScalar *vwork = NULL;
2853: ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);
2854: ISGetIndices(irowp,&row);
2855: ISInvertPermutation(colp,PETSC_DECIDE,&icolp);
2856: ISGetIndices(icolp,&col);
2858: /* determine lengths of permuted rows */
2859: PetscMalloc1(m+1,&lens);
2860: for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2861: MatCreate(PetscObjectComm((PetscObject)A),B);
2862: MatSetSizes(*B,m,n,m,n);
2863: MatSetBlockSizesFromMats(*B,A,A);
2864: MatSetType(*B,((PetscObject)A)->type_name);
2865: MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);
2866: PetscFree(lens);
2868: PetscMalloc1(n,&cnew);
2869: for (i=0; i<m; i++) {
2870: MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
2871: for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2872: MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);
2873: MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
2874: }
2875: PetscFree(cnew);
2877: (*B)->assembled = PETSC_FALSE;
2879: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2880: MatBindToCPU(*B,A->boundtocpu);
2881: #endif
2882: MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);
2883: MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);
2884: ISRestoreIndices(irowp,&row);
2885: ISRestoreIndices(icolp,&col);
2886: ISDestroy(&irowp);
2887: ISDestroy(&icolp);
2888: if (rowp == colp) {
2889: if (A->symmetric) {
2890: MatSetOption(*B,MAT_SYMMETRIC,PETSC_TRUE);
2891: }
2892: if (A->hermitian) {
2893: MatSetOption(*B,MAT_HERMITIAN,PETSC_TRUE);
2894: }
2895: }
2896: return(0);
2897: }
2899: PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2900: {
2904: /* If the two matrices have the same copy implementation, use fast copy. */
2905: if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2906: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2907: Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2909: if (a->i[A->rmap->n] != b->i[B->rmap->n]) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different %D != %D",a->i[A->rmap->n],b->i[B->rmap->n]);
2910: PetscArraycpy(b->a,a->a,a->i[A->rmap->n]);
2911: PetscObjectStateIncrease((PetscObject)B);
2912: } else {
2913: MatCopy_Basic(A,B,str);
2914: }
2915: return(0);
2916: }
2918: PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2919: {
2923: MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);
2924: return(0);
2925: }
2927: PETSC_INTERN PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
2928: {
2929: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2932: *array = a->a;
2933: return(0);
2934: }
2936: PETSC_INTERN PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
2937: {
2939: *array = NULL;
2940: return(0);
2941: }
2943: /*
2944: Computes the number of nonzeros per row needed for preallocation when X and Y
2945: have different nonzero structure.
2946: */
2947: PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
2948: {
2949: PetscInt i,j,k,nzx,nzy;
2952: /* Set the number of nonzeros in the new matrix */
2953: for (i=0; i<m; i++) {
2954: const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2955: nzx = xi[i+1] - xi[i];
2956: nzy = yi[i+1] - yi[i];
2957: nnz[i] = 0;
2958: for (j=0,k=0; j<nzx; j++) { /* Point in X */
2959: for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
2960: if (k<nzy && yjj[k]==xjj[j]) k++; /* Skip duplicate */
2961: nnz[i]++;
2962: }
2963: for (; k<nzy; k++) nnz[i]++;
2964: }
2965: return(0);
2966: }
2968: PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2969: {
2970: PetscInt m = Y->rmap->N;
2971: Mat_SeqAIJ *x = (Mat_SeqAIJ*)X->data;
2972: Mat_SeqAIJ *y = (Mat_SeqAIJ*)Y->data;
2976: /* Set the number of nonzeros in the new matrix */
2977: MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);
2978: return(0);
2979: }
2981: PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2982: {
2984: Mat_SeqAIJ *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2985: PetscBLASInt one=1,bnz;
2988: PetscBLASIntCast(x->nz,&bnz);
2989: if (str == SAME_NONZERO_PATTERN) {
2990: PetscScalar alpha = a;
2991: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2992: MatSeqAIJInvalidateDiagonal(Y);
2993: PetscObjectStateIncrease((PetscObject)Y);
2994: /* the MatAXPY_Basic* subroutines calls MatAssembly, so the matrix on the GPU
2995: will be updated */
2996: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2997: if (Y->offloadmask != PETSC_OFFLOAD_UNALLOCATED) {
2998: Y->offloadmask = PETSC_OFFLOAD_CPU;
2999: }
3000: #endif
3001: } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
3002: MatAXPY_Basic(Y,a,X,str);
3003: } else {
3004: Mat B;
3005: PetscInt *nnz;
3006: PetscMalloc1(Y->rmap->N,&nnz);
3007: MatCreate(PetscObjectComm((PetscObject)Y),&B);
3008: PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
3009: MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
3010: MatSetBlockSizesFromMats(B,Y,Y);
3011: MatSetType(B,(MatType) ((PetscObject)Y)->type_name);
3012: MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);
3013: MatSeqAIJSetPreallocation(B,0,nnz);
3014: MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
3015: MatHeaderReplace(Y,&B);
3016: PetscFree(nnz);
3017: }
3018: return(0);
3019: }
3021: PetscErrorCode MatConjugate_SeqAIJ(Mat mat)
3022: {
3023: #if defined(PETSC_USE_COMPLEX)
3024: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3025: PetscInt i,nz;
3026: PetscScalar *a;
3029: nz = aij->nz;
3030: a = aij->a;
3031: for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
3032: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
3033: if (mat->offloadmask != PETSC_OFFLOAD_UNALLOCATED) mat->offloadmask = PETSC_OFFLOAD_CPU;
3034: #endif
3035: #else
3037: #endif
3038: return(0);
3039: }
3041: PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3042: {
3043: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
3045: PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3046: PetscReal atmp;
3047: PetscScalar *x;
3048: MatScalar *aa;
3051: if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3052: aa = a->a;
3053: ai = a->i;
3054: aj = a->j;
3056: VecSet(v,0.0);
3057: VecGetArray(v,&x);
3058: VecGetLocalSize(v,&n);
3059: if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3060: for (i=0; i<m; i++) {
3061: ncols = ai[1] - ai[0]; ai++;
3062: x[i] = 0.0;
3063: for (j=0; j<ncols; j++) {
3064: atmp = PetscAbsScalar(*aa);
3065: if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
3066: aa++; aj++;
3067: }
3068: }
3069: VecRestoreArray(v,&x);
3070: return(0);
3071: }
3073: PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3074: {
3075: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
3077: PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3078: PetscScalar *x;
3079: MatScalar *aa;
3082: if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3083: aa = a->a;
3084: ai = a->i;
3085: aj = a->j;
3087: VecSet(v,0.0);
3088: VecGetArray(v,&x);
3089: VecGetLocalSize(v,&n);
3090: if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3091: for (i=0; i<m; i++) {
3092: ncols = ai[1] - ai[0]; ai++;
3093: if (ncols == A->cmap->n) { /* row is dense */
3094: x[i] = *aa; if (idx) idx[i] = 0;
3095: } else { /* row is sparse so already KNOW maximum is 0.0 or higher */
3096: x[i] = 0.0;
3097: if (idx) {
3098: idx[i] = 0; /* in case ncols is zero */
3099: for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
3100: if (aj[j] > j) {
3101: idx[i] = j;
3102: break;
3103: }
3104: }
3105: }
3106: }
3107: for (j=0; j<ncols; j++) {
3108: if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3109: aa++; aj++;
3110: }
3111: }
3112: VecRestoreArray(v,&x);
3113: return(0);
3114: }
3116: PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3117: {
3118: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
3120: PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3121: PetscReal atmp;
3122: PetscScalar *x;
3123: MatScalar *aa;
3126: if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3127: aa = a->a;
3128: ai = a->i;
3129: aj = a->j;
3131: VecSet(v,0.0);
3132: VecGetArray(v,&x);
3133: VecGetLocalSize(v,&n);
3134: 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);
3135: for (i=0; i<m; i++) {
3136: ncols = ai[1] - ai[0]; ai++;
3137: if (ncols) {
3138: /* Get first nonzero */
3139: for (j = 0; j < ncols; j++) {
3140: atmp = PetscAbsScalar(aa[j]);
3141: if (atmp > 1.0e-12) {
3142: x[i] = atmp;
3143: if (idx) idx[i] = aj[j];
3144: break;
3145: }
3146: }
3147: if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
3148: } else {
3149: x[i] = 0.0; if (idx) idx[i] = 0;
3150: }
3151: for (j = 0; j < ncols; j++) {
3152: atmp = PetscAbsScalar(*aa);
3153: if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
3154: aa++; aj++;
3155: }
3156: }
3157: VecRestoreArray(v,&x);
3158: return(0);
3159: }
3161: PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3162: {
3163: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
3164: PetscErrorCode ierr;
3165: PetscInt i,j,m = A->rmap->n,ncols,n;
3166: const PetscInt *ai,*aj;
3167: PetscScalar *x;
3168: const MatScalar *aa;
3171: if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3172: aa = a->a;
3173: ai = a->i;
3174: aj = a->j;
3176: VecSet(v,0.0);
3177: VecGetArray(v,&x);
3178: VecGetLocalSize(v,&n);
3179: if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3180: for (i=0; i<m; i++) {
3181: ncols = ai[1] - ai[0]; ai++;
3182: if (ncols == A->cmap->n) { /* row is dense */
3183: x[i] = *aa; if (idx) idx[i] = 0;
3184: } else { /* row is sparse so already KNOW minimum is 0.0 or lower */
3185: x[i] = 0.0;
3186: if (idx) { /* find first implicit 0.0 in the row */
3187: idx[i] = 0; /* in case ncols is zero */
3188: for (j=0; j<ncols; j++) {
3189: if (aj[j] > j) {
3190: idx[i] = j;
3191: break;
3192: }
3193: }
3194: }
3195: }
3196: for (j=0; j<ncols; j++) {
3197: if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3198: aa++; aj++;
3199: }
3200: }
3201: VecRestoreArray(v,&x);
3202: return(0);
3203: }
3205: PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
3206: {
3207: Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data;
3208: PetscErrorCode ierr;
3209: PetscInt i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
3210: MatScalar *diag,work[25],*v_work;
3211: const PetscReal shift = 0.0;
3212: PetscBool allowzeropivot,zeropivotdetected=PETSC_FALSE;
3215: allowzeropivot = PetscNot(A->erroriffailure);
3216: if (a->ibdiagvalid) {
3217: if (values) *values = a->ibdiag;
3218: return(0);
3219: }
3220: MatMarkDiagonal_SeqAIJ(A);
3221: if (!a->ibdiag) {
3222: PetscMalloc1(bs2*mbs,&a->ibdiag);
3223: PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));
3224: }
3225: diag = a->ibdiag;
3226: if (values) *values = a->ibdiag;
3227: /* factor and invert each block */
3228: switch (bs) {
3229: case 1:
3230: for (i=0; i<mbs; i++) {
3231: MatGetValues(A,1,&i,1,&i,diag+i);
3232: if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
3233: if (allowzeropivot) {
3234: A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3235: A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]);
3236: A->factorerror_zeropivot_row = i;
3237: PetscInfo3(A,"Zero pivot, row %D pivot %g tolerance %g\n",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);
3238: } else SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot, row %D pivot %g tolerance %g",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);
3239: }
3240: diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3241: }
3242: break;
3243: case 2:
3244: for (i=0; i<mbs; i++) {
3245: ij[0] = 2*i; ij[1] = 2*i + 1;
3246: MatGetValues(A,2,ij,2,ij,diag);
3247: PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
3248: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3249: PetscKernel_A_gets_transpose_A_2(diag);
3250: diag += 4;
3251: }
3252: break;
3253: case 3:
3254: for (i=0; i<mbs; i++) {
3255: ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3256: MatGetValues(A,3,ij,3,ij,diag);
3257: PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);
3258: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3259: PetscKernel_A_gets_transpose_A_3(diag);
3260: diag += 9;
3261: }
3262: break;
3263: case 4:
3264: for (i=0; i<mbs; i++) {
3265: ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3266: MatGetValues(A,4,ij,4,ij,diag);
3267: PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);
3268: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3269: PetscKernel_A_gets_transpose_A_4(diag);
3270: diag += 16;
3271: }
3272: break;
3273: case 5:
3274: for (i=0; i<mbs; i++) {
3275: ij[0] = 5*i; ij[1] = 5*i + 1; ij[2] = 5*i + 2; ij[3] = 5*i + 3; ij[4] = 5*i + 4;
3276: MatGetValues(A,5,ij,5,ij,diag);
3277: PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);
3278: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3279: PetscKernel_A_gets_transpose_A_5(diag);
3280: diag += 25;
3281: }
3282: break;
3283: case 6:
3284: for (i=0; i<mbs; i++) {
3285: 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;
3286: MatGetValues(A,6,ij,6,ij,diag);
3287: PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);
3288: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3289: PetscKernel_A_gets_transpose_A_6(diag);
3290: diag += 36;
3291: }
3292: break;
3293: case 7:
3294: for (i=0; i<mbs; i++) {
3295: 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;
3296: MatGetValues(A,7,ij,7,ij,diag);
3297: PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);
3298: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3299: PetscKernel_A_gets_transpose_A_7(diag);
3300: diag += 49;
3301: }
3302: break;
3303: default:
3304: PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);
3305: for (i=0; i<mbs; i++) {
3306: for (j=0; j<bs; j++) {
3307: IJ[j] = bs*i + j;
3308: }
3309: MatGetValues(A,bs,IJ,bs,IJ,diag);
3310: PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);
3311: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3312: PetscKernel_A_gets_transpose_A_N(diag,bs);
3313: diag += bs2;
3314: }
3315: PetscFree3(v_work,v_pivots,IJ);
3316: }
3317: a->ibdiagvalid = PETSC_TRUE;
3318: return(0);
3319: }
3321: static PetscErrorCode MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
3322: {
3324: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)x->data;
3325: PetscScalar a;
3326: PetscInt m,n,i,j,col;
3329: if (!x->assembled) {
3330: MatGetSize(x,&m,&n);
3331: for (i=0; i<m; i++) {
3332: for (j=0; j<aij->imax[i]; j++) {
3333: PetscRandomGetValue(rctx,&a);
3334: col = (PetscInt)(n*PetscRealPart(a));
3335: MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);
3336: }
3337: }
3338: } else {
3339: for (i=0; i<aij->nz; i++) {PetscRandomGetValue(rctx,aij->a+i);}
3340: }
3341: MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
3342: MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
3343: return(0);
3344: }
3346: /* Like MatSetRandom_SeqAIJ, but do not set values on columns in range of [low, high) */
3347: PetscErrorCode MatSetRandomSkipColumnRange_SeqAIJ_Private(Mat x,PetscInt low,PetscInt high,PetscRandom rctx)
3348: {
3350: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)x->data;
3351: PetscScalar a;
3352: PetscInt m,n,i,j,col,nskip;
3355: nskip = high - low;
3356: MatGetSize(x,&m,&n);
3357: n -= nskip; /* shrink number of columns where nonzeros can be set */
3358: for (i=0; i<m; i++) {
3359: for (j=0; j<aij->imax[i]; j++) {
3360: PetscRandomGetValue(rctx,&a);
3361: col = (PetscInt)(n*PetscRealPart(a));
3362: if (col >= low) col += nskip; /* shift col rightward to skip the hole */
3363: MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);
3364: }
3365: }
3366: MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
3367: MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
3368: return(0);
3369: }
3372: /* -------------------------------------------------------------------*/
3373: static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3374: MatGetRow_SeqAIJ,
3375: MatRestoreRow_SeqAIJ,
3376: MatMult_SeqAIJ,
3377: /* 4*/ MatMultAdd_SeqAIJ,
3378: MatMultTranspose_SeqAIJ,
3379: MatMultTransposeAdd_SeqAIJ,
3380: 0,
3381: 0,
3382: 0,
3383: /* 10*/ 0,
3384: MatLUFactor_SeqAIJ,
3385: 0,
3386: MatSOR_SeqAIJ,
3387: MatTranspose_SeqAIJ,
3388: /*1 5*/ MatGetInfo_SeqAIJ,
3389: MatEqual_SeqAIJ,
3390: MatGetDiagonal_SeqAIJ,
3391: MatDiagonalScale_SeqAIJ,
3392: MatNorm_SeqAIJ,
3393: /* 20*/ 0,
3394: MatAssemblyEnd_SeqAIJ,
3395: MatSetOption_SeqAIJ,
3396: MatZeroEntries_SeqAIJ,
3397: /* 24*/ MatZeroRows_SeqAIJ,
3398: 0,
3399: 0,
3400: 0,
3401: 0,
3402: /* 29*/ MatSetUp_SeqAIJ,
3403: 0,
3404: 0,
3405: 0,
3406: 0,
3407: /* 34*/ MatDuplicate_SeqAIJ,
3408: 0,
3409: 0,
3410: MatILUFactor_SeqAIJ,
3411: 0,
3412: /* 39*/ MatAXPY_SeqAIJ,
3413: MatCreateSubMatrices_SeqAIJ,
3414: MatIncreaseOverlap_SeqAIJ,
3415: MatGetValues_SeqAIJ,
3416: MatCopy_SeqAIJ,
3417: /* 44*/ MatGetRowMax_SeqAIJ,
3418: MatScale_SeqAIJ,
3419: MatShift_SeqAIJ,
3420: MatDiagonalSet_SeqAIJ,
3421: MatZeroRowsColumns_SeqAIJ,
3422: /* 49*/ MatSetRandom_SeqAIJ,
3423: MatGetRowIJ_SeqAIJ,
3424: MatRestoreRowIJ_SeqAIJ,
3425: MatGetColumnIJ_SeqAIJ,
3426: MatRestoreColumnIJ_SeqAIJ,
3427: /* 54*/ MatFDColoringCreate_SeqXAIJ,
3428: 0,
3429: 0,
3430: MatPermute_SeqAIJ,
3431: 0,
3432: /* 59*/ 0,
3433: MatDestroy_SeqAIJ,
3434: MatView_SeqAIJ,
3435: 0,
3436: 0,
3437: /* 64*/ 0,
3438: MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3439: 0,
3440: 0,
3441: 0,
3442: /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3443: MatGetRowMinAbs_SeqAIJ,
3444: 0,
3445: 0,
3446: 0,
3447: /* 74*/ 0,
3448: MatFDColoringApply_AIJ,
3449: 0,
3450: 0,
3451: 0,
3452: /* 79*/ MatFindZeroDiagonals_SeqAIJ,
3453: 0,
3454: 0,
3455: 0,
3456: MatLoad_SeqAIJ,
3457: /* 84*/ MatIsSymmetric_SeqAIJ,
3458: MatIsHermitian_SeqAIJ,
3459: 0,
3460: 0,
3461: 0,
3462: /* 89*/ 0,
3463: 0,
3464: MatMatMultNumeric_SeqAIJ_SeqAIJ,
3465: 0,
3466: 0,
3467: /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy,
3468: 0,
3469: 0,
3470: MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
3471: 0,
3472: /* 99*/ MatProductSetFromOptions_SeqAIJ,
3473: 0,
3474: 0,
3475: MatConjugate_SeqAIJ,
3476: 0,
3477: /*104*/ MatSetValuesRow_SeqAIJ,
3478: MatRealPart_SeqAIJ,
3479: MatImaginaryPart_SeqAIJ,
3480: 0,
3481: 0,
3482: /*109*/ MatMatSolve_SeqAIJ,
3483: 0,
3484: MatGetRowMin_SeqAIJ,
3485: 0,
3486: MatMissingDiagonal_SeqAIJ,
3487: /*114*/ 0,
3488: 0,
3489: 0,
3490: 0,
3491: 0,
3492: /*119*/ 0,
3493: 0,
3494: 0,
3495: 0,
3496: MatGetMultiProcBlock_SeqAIJ,
3497: /*124*/ MatFindNonzeroRows_SeqAIJ,
3498: MatGetColumnNorms_SeqAIJ,
3499: MatInvertBlockDiagonal_SeqAIJ,
3500: MatInvertVariableBlockDiagonal_SeqAIJ,
3501: 0,
3502: /*129*/ 0,
3503: 0,
3504: 0,
3505: MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3506: MatTransposeColoringCreate_SeqAIJ,
3507: /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
3508: MatTransColoringApplyDenToSp_SeqAIJ,
3509: 0,
3510: 0,
3511: MatRARtNumeric_SeqAIJ_SeqAIJ,
3512: /*139*/0,
3513: 0,
3514: 0,
3515: MatFDColoringSetUp_SeqXAIJ,
3516: MatFindOffBlockDiagonalEntries_SeqAIJ,
3517: MatCreateMPIMatConcatenateSeqMat_SeqAIJ,
3518: /*145*/MatDestroySubMatrices_SeqAIJ,
3519: 0,
3520: 0
3521: };
3523: PetscErrorCode MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3524: {
3525: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3526: PetscInt i,nz,n;
3529: nz = aij->maxnz;
3530: n = mat->rmap->n;
3531: for (i=0; i<nz; i++) {
3532: aij->j[i] = indices[i];
3533: }
3534: aij->nz = nz;
3535: for (i=0; i<n; i++) {
3536: aij->ilen[i] = aij->imax[i];
3537: }
3538: return(0);
3539: }
3541: /*
3542: * When a sparse matrix has many zero columns, we should compact them out to save the space
3543: * This happens in MatPtAPSymbolic_MPIAIJ_MPIAIJ_scalable()
3544: * */
3545: PetscErrorCode MatSeqAIJCompactOutExtraColumns_SeqAIJ(Mat mat, ISLocalToGlobalMapping *mapping)
3546: {
3547: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3548: PetscTable gid1_lid1;
3549: PetscTablePosition tpos;
3550: PetscInt gid,lid,i,j,ncols,ec;
3551: PetscInt *garray;
3552: PetscErrorCode ierr;
3557: /* use a table */
3558: PetscTableCreate(mat->rmap->n,mat->cmap->N+1,&gid1_lid1);
3559: ec = 0;
3560: for (i=0; i<mat->rmap->n; i++) {
3561: ncols = aij->i[i+1] - aij->i[i];
3562: for (j=0; j<ncols; j++) {
3563: PetscInt data,gid1 = aij->j[aij->i[i] + j] + 1;
3564: PetscTableFind(gid1_lid1,gid1,&data);
3565: if (!data) {
3566: /* one based table */
3567: PetscTableAdd(gid1_lid1,gid1,++ec,INSERT_VALUES);
3568: }
3569: }
3570: }
3571: /* form array of columns we need */
3572: PetscMalloc1(ec+1,&garray);
3573: PetscTableGetHeadPosition(gid1_lid1,&tpos);
3574: while (tpos) {
3575: PetscTableGetNext(gid1_lid1,&tpos,&gid,&lid);
3576: gid--;
3577: lid--;
3578: garray[lid] = gid;
3579: }
3580: PetscSortInt(ec,garray); /* sort, and rebuild */
3581: PetscTableRemoveAll(gid1_lid1);
3582: for (i=0; i<ec; i++) {
3583: PetscTableAdd(gid1_lid1,garray[i]+1,i+1,INSERT_VALUES);
3584: }
3585: /* compact out the extra columns in B */
3586: for (i=0; i<mat->rmap->n; i++) {
3587: ncols = aij->i[i+1] - aij->i[i];
3588: for (j=0; j<ncols; j++) {
3589: PetscInt gid1 = aij->j[aij->i[i] + j] + 1;
3590: PetscTableFind(gid1_lid1,gid1,&lid);
3591: lid--;
3592: aij->j[aij->i[i] + j] = lid;
3593: }
3594: }
3595: PetscLayoutDestroy(&mat->cmap);
3596: PetscLayoutCreateFromSizes(PetscObjectComm((PetscObject)mat),ec,ec,1,&mat->cmap);
3597: PetscTableDestroy(&gid1_lid1);
3598: ISLocalToGlobalMappingCreate(PETSC_COMM_SELF,mat->cmap->bs,mat->cmap->n,garray,PETSC_OWN_POINTER,mapping);
3599: ISLocalToGlobalMappingSetType(*mapping,ISLOCALTOGLOBALMAPPINGHASH);
3600: return(0);
3601: }
3603: /*@
3604: MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3605: in the matrix.
3607: Input Parameters:
3608: + mat - the SeqAIJ matrix
3609: - indices - the column indices
3611: Level: advanced
3613: Notes:
3614: This can be called if you have precomputed the nonzero structure of the
3615: matrix and want to provide it to the matrix object to improve the performance
3616: of the MatSetValues() operation.
3618: You MUST have set the correct numbers of nonzeros per row in the call to
3619: MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3621: MUST be called before any calls to MatSetValues();
3623: The indices should start with zero, not one.
3625: @*/
3626: PetscErrorCode MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3627: {
3633: PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));
3634: return(0);
3635: }
3637: /* ----------------------------------------------------------------------------------------*/
3639: PetscErrorCode MatStoreValues_SeqAIJ(Mat mat)
3640: {
3641: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3643: size_t nz = aij->i[mat->rmap->n];
3646: if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3648: /* allocate space for values if not already there */
3649: if (!aij->saved_values) {
3650: PetscMalloc1(nz+1,&aij->saved_values);
3651: PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));
3652: }
3654: /* copy values over */
3655: PetscArraycpy(aij->saved_values,aij->a,nz);
3656: return(0);
3657: }
3659: /*@
3660: MatStoreValues - Stashes a copy of the matrix values; this allows, for
3661: example, reuse of the linear part of a Jacobian, while recomputing the
3662: nonlinear portion.
3664: Collect on Mat
3666: Input Parameters:
3667: . mat - the matrix (currently only AIJ matrices support this option)
3669: Level: advanced
3671: Common Usage, with SNESSolve():
3672: $ Create Jacobian matrix
3673: $ Set linear terms into matrix
3674: $ Apply boundary conditions to matrix, at this time matrix must have
3675: $ final nonzero structure (i.e. setting the nonlinear terms and applying
3676: $ boundary conditions again will not change the nonzero structure
3677: $ MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3678: $ MatStoreValues(mat);
3679: $ Call SNESSetJacobian() with matrix
3680: $ In your Jacobian routine
3681: $ MatRetrieveValues(mat);
3682: $ Set nonlinear terms in matrix
3684: Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3685: $ // build linear portion of Jacobian
3686: $ MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3687: $ MatStoreValues(mat);
3688: $ loop over nonlinear iterations
3689: $ MatRetrieveValues(mat);
3690: $ // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3691: $ // call MatAssemblyBegin/End() on matrix
3692: $ Solve linear system with Jacobian
3693: $ endloop
3695: Notes:
3696: Matrix must already be assemblied before calling this routine
3697: Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3698: calling this routine.
3700: When this is called multiple times it overwrites the previous set of stored values
3701: and does not allocated additional space.
3703: .seealso: MatRetrieveValues()
3705: @*/
3706: PetscErrorCode MatStoreValues(Mat mat)
3707: {
3712: if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3713: if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3714: PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));
3715: return(0);
3716: }
3718: PetscErrorCode MatRetrieveValues_SeqAIJ(Mat mat)
3719: {
3720: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3722: PetscInt nz = aij->i[mat->rmap->n];
3725: if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3726: if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3727: /* copy values over */
3728: PetscArraycpy(aij->a,aij->saved_values,nz);
3729: return(0);
3730: }
3732: /*@
3733: MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3734: example, reuse of the linear part of a Jacobian, while recomputing the
3735: nonlinear portion.
3737: Collect on Mat
3739: Input Parameters:
3740: . mat - the matrix (currently only AIJ matrices support this option)
3742: Level: advanced
3744: .seealso: MatStoreValues()
3746: @*/
3747: PetscErrorCode MatRetrieveValues(Mat mat)
3748: {
3753: if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3754: if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3755: PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));
3756: return(0);
3757: }
3760: /* --------------------------------------------------------------------------------*/
3761: /*@C
3762: MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
3763: (the default parallel PETSc format). For good matrix assembly performance
3764: the user should preallocate the matrix storage by setting the parameter nz
3765: (or the array nnz). By setting these parameters accurately, performance
3766: during matrix assembly can be increased by more than a factor of 50.
3768: Collective
3770: Input Parameters:
3771: + comm - MPI communicator, set to PETSC_COMM_SELF
3772: . m - number of rows
3773: . n - number of columns
3774: . nz - number of nonzeros per row (same for all rows)
3775: - nnz - array containing the number of nonzeros in the various rows
3776: (possibly different for each row) or NULL
3778: Output Parameter:
3779: . A - the matrix
3781: It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3782: MatXXXXSetPreallocation() paradigm instead of this routine directly.
3783: [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3785: Notes:
3786: If nnz is given then nz is ignored
3788: The AIJ format (also called the Yale sparse matrix format or
3789: compressed row storage), is fully compatible with standard Fortran 77
3790: storage. That is, the stored row and column indices can begin at
3791: either one (as in Fortran) or zero. See the users' manual for details.
3793: Specify the preallocated storage with either nz or nnz (not both).
3794: Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3795: allocation. For large problems you MUST preallocate memory or you
3796: will get TERRIBLE performance, see the users' manual chapter on matrices.
3798: By default, this format uses inodes (identical nodes) when possible, to
3799: improve numerical efficiency of matrix-vector products and solves. We
3800: search for consecutive rows with the same nonzero structure, thereby
3801: reusing matrix information to achieve increased efficiency.
3803: Options Database Keys:
3804: + -mat_no_inode - Do not use inodes
3805: - -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3807: Level: intermediate
3809: .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
3811: @*/
3812: PetscErrorCode MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
3813: {
3817: MatCreate(comm,A);
3818: MatSetSizes(*A,m,n,m,n);
3819: MatSetType(*A,MATSEQAIJ);
3820: MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);
3821: return(0);
3822: }
3824: /*@C
3825: MatSeqAIJSetPreallocation - For good matrix assembly performance
3826: the user should preallocate the matrix storage by setting the parameter nz
3827: (or the array nnz). By setting these parameters accurately, performance
3828: during matrix assembly can be increased by more than a factor of 50.
3830: Collective
3832: Input Parameters:
3833: + B - The matrix
3834: . nz - number of nonzeros per row (same for all rows)
3835: - nnz - array containing the number of nonzeros in the various rows
3836: (possibly different for each row) or NULL
3838: Notes:
3839: If nnz is given then nz is ignored
3841: The AIJ format (also called the Yale sparse matrix format or
3842: compressed row storage), is fully compatible with standard Fortran 77
3843: storage. That is, the stored row and column indices can begin at
3844: either one (as in Fortran) or zero. See the users' manual for details.
3846: Specify the preallocated storage with either nz or nnz (not both).
3847: Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3848: allocation. For large problems you MUST preallocate memory or you
3849: will get TERRIBLE performance, see the users' manual chapter on matrices.
3851: You can call MatGetInfo() to get information on how effective the preallocation was;
3852: for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3853: You can also run with the option -info and look for messages with the string
3854: malloc in them to see if additional memory allocation was needed.
3856: Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3857: entries or columns indices
3859: By default, this format uses inodes (identical nodes) when possible, to
3860: improve numerical efficiency of matrix-vector products and solves. We
3861: search for consecutive rows with the same nonzero structure, thereby
3862: reusing matrix information to achieve increased efficiency.
3864: Options Database Keys:
3865: + -mat_no_inode - Do not use inodes
3866: - -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3868: Level: intermediate
3870: .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
3872: @*/
3873: PetscErrorCode MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3874: {
3880: PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));
3881: return(0);
3882: }
3884: PetscErrorCode MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3885: {
3886: Mat_SeqAIJ *b;
3887: PetscBool skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
3889: PetscInt i;
3892: if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3893: if (nz == MAT_SKIP_ALLOCATION) {
3894: skipallocation = PETSC_TRUE;
3895: nz = 0;
3896: }
3897: PetscLayoutSetUp(B->rmap);
3898: PetscLayoutSetUp(B->cmap);
3900: if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3901: if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3902: #if defined(PETSC_USE_DEBUG)
3903: if (nnz) {
3904: for (i=0; i<B->rmap->n; i++) {
3905: 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]);
3906: 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);
3907: }
3908: }
3909: #endif
3911: B->preallocated = PETSC_TRUE;
3913: b = (Mat_SeqAIJ*)B->data;
3915: if (!skipallocation) {
3916: if (!b->imax) {
3917: PetscMalloc1(B->rmap->n,&b->imax);
3918: PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
3919: }
3920: if (!b->ilen) {
3921: /* b->ilen will count nonzeros in each row so far. */
3922: PetscCalloc1(B->rmap->n,&b->ilen);
3923: PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
3924: } else {
3925: PetscMemzero(b->ilen,B->rmap->n*sizeof(PetscInt));
3926: }
3927: if (!b->ipre) {
3928: PetscMalloc1(B->rmap->n,&b->ipre);
3929: PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
3930: }
3931: if (!nnz) {
3932: if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3933: else if (nz < 0) nz = 1;
3934: nz = PetscMin(nz,B->cmap->n);
3935: for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3936: nz = nz*B->rmap->n;
3937: } else {
3938: PetscInt64 nz64 = 0;
3939: for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz64 += nnz[i];}
3940: PetscIntCast(nz64,&nz);
3941: }
3943: /* allocate the matrix space */
3944: /* FIXME: should B's old memory be unlogged? */
3945: MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);
3946: if (B->structure_only) {
3947: PetscMalloc1(nz,&b->j);
3948: PetscMalloc1(B->rmap->n+1,&b->i);
3949: PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*sizeof(PetscInt));
3950: } else {
3951: PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);
3952: PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));
3953: }
3954: b->i[0] = 0;
3955: for (i=1; i<B->rmap->n+1; i++) {
3956: b->i[i] = b->i[i-1] + b->imax[i-1];
3957: }
3958: if (B->structure_only) {
3959: b->singlemalloc = PETSC_FALSE;
3960: b->free_a = PETSC_FALSE;
3961: } else {
3962: b->singlemalloc = PETSC_TRUE;
3963: b->free_a = PETSC_TRUE;
3964: }
3965: b->free_ij = PETSC_TRUE;
3966: } else {
3967: b->free_a = PETSC_FALSE;
3968: b->free_ij = PETSC_FALSE;
3969: }
3971: if (b->ipre && nnz != b->ipre && b->imax) {
3972: /* reserve user-requested sparsity */
3973: PetscArraycpy(b->ipre,b->imax,B->rmap->n);
3974: }
3977: b->nz = 0;
3978: b->maxnz = nz;
3979: B->info.nz_unneeded = (double)b->maxnz;
3980: if (realalloc) {
3981: MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
3982: }
3983: B->was_assembled = PETSC_FALSE;
3984: B->assembled = PETSC_FALSE;
3985: return(0);
3986: }
3989: PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A)
3990: {
3991: Mat_SeqAIJ *a;
3992: PetscInt i;
3998: /* Check local size. If zero, then return */
3999: if (!A->rmap->n) return(0);
4001: a = (Mat_SeqAIJ*)A->data;
4002: /* if no saved info, we error out */
4003: if (!a->ipre) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"No saved preallocation info \n");
4005: if (!a->i || !a->j || !a->a || !a->imax || !a->ilen) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Memory info is incomplete, and can not reset preallocation \n");
4007: PetscArraycpy(a->imax,a->ipre,A->rmap->n);
4008: PetscArrayzero(a->ilen,A->rmap->n);
4009: a->i[0] = 0;
4010: for (i=1; i<A->rmap->n+1; i++) {
4011: a->i[i] = a->i[i-1] + a->imax[i-1];
4012: }
4013: A->preallocated = PETSC_TRUE;
4014: a->nz = 0;
4015: a->maxnz = a->i[A->rmap->n];
4016: A->info.nz_unneeded = (double)a->maxnz;
4017: A->was_assembled = PETSC_FALSE;
4018: A->assembled = PETSC_FALSE;
4019: return(0);
4020: }
4022: /*@
4023: MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
4025: Input Parameters:
4026: + B - the matrix
4027: . i - the indices into j for the start of each row (starts with zero)
4028: . j - the column indices for each row (starts with zero) these must be sorted for each row
4029: - v - optional values in the matrix
4031: Level: developer
4033: The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
4035: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), MATSEQAIJ
4036: @*/
4037: PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
4038: {
4044: PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));
4045: return(0);
4046: }
4048: PetscErrorCode MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
4049: {
4050: PetscInt i;
4051: PetscInt m,n;
4052: PetscInt nz;
4053: PetscInt *nnz, nz_max = 0;
4057: if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
4059: PetscLayoutSetUp(B->rmap);
4060: PetscLayoutSetUp(B->cmap);
4062: MatGetSize(B, &m, &n);
4063: PetscMalloc1(m+1, &nnz);
4064: for (i = 0; i < m; i++) {
4065: nz = Ii[i+1]- Ii[i];
4066: nz_max = PetscMax(nz_max, nz);
4067: if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
4068: nnz[i] = nz;
4069: }
4070: MatSeqAIJSetPreallocation(B, 0, nnz);
4071: PetscFree(nnz);
4073: for (i = 0; i < m; i++) {
4074: MatSetValues_SeqAIJ(B, 1, &i, Ii[i+1] - Ii[i], J+Ii[i], v ? v + Ii[i] : NULL, INSERT_VALUES);
4075: }
4077: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
4078: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
4080: MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
4081: return(0);
4082: }
4084: #include <../src/mat/impls/dense/seq/dense.h>
4085: #include <petsc/private/kernels/petscaxpy.h>
4087: /*
4088: Computes (B'*A')' since computing B*A directly is untenable
4090: n p p
4091: ( ) ( ) ( )
4092: m ( A ) * n ( B ) = m ( C )
4093: ( ) ( ) ( )
4095: */
4096: PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
4097: {
4098: PetscErrorCode ierr;
4099: Mat_SeqDense *sub_a = (Mat_SeqDense*)A->data;
4100: Mat_SeqAIJ *sub_b = (Mat_SeqAIJ*)B->data;
4101: Mat_SeqDense *sub_c = (Mat_SeqDense*)C->data;
4102: PetscInt i,n,m,q,p;
4103: const PetscInt *ii,*idx;
4104: const PetscScalar *b,*a,*a_q;
4105: PetscScalar *c,*c_q;
4108: m = A->rmap->n;
4109: n = A->cmap->n;
4110: p = B->cmap->n;
4111: a = sub_a->v;
4112: b = sub_b->a;
4113: c = sub_c->v;
4114: PetscArrayzero(c,m*p);
4116: ii = sub_b->i;
4117: idx = sub_b->j;
4118: for (i=0; i<n; i++) {
4119: q = ii[i+1] - ii[i];
4120: while (q-->0) {
4121: c_q = c + m*(*idx);
4122: a_q = a + m*i;
4123: PetscKernelAXPY(c_q,*b,a_q,m);
4124: idx++;
4125: b++;
4126: }
4127: }
4128: return(0);
4129: }
4131: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat C)
4132: {
4134: PetscInt m=A->rmap->n,n=B->cmap->n;
4137: 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);
4138: MatSetSizes(C,m,n,m,n);
4139: MatSetBlockSizesFromMats(C,A,B);
4140: MatSetType(C,MATSEQDENSE);
4141: MatSeqDenseSetPreallocation(C,NULL);
4143: C->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
4144: return(0);
4145: }
4147: /* ----------------------------------------------------------------*/
4148: /*MC
4149: MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
4150: based on compressed sparse row format.
4152: Options Database Keys:
4153: . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
4155: Level: beginner
4157: Notes:
4158: MatSetValues() may be called for this matrix type with a NULL argument for the numerical values,
4159: in this case the values associated with the rows and columns one passes in are set to zero
4160: in the matrix
4162: MatSetOptions(,MAT_STRUCTURE_ONLY,PETSC_TRUE) may be called for this matrix type. In this no
4163: space is allocated for the nonzero entries and any entries passed with MatSetValues() are ignored
4165: Developer Notes:
4166: It would be nice if all matrix formats supported passing NULL in for the numerical values
4168: .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
4169: M*/
4171: /*MC
4172: MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
4174: This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
4175: and MATMPIAIJ otherwise. As a result, for single process communicators,
4176: MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation() is supported
4177: for communicators controlling multiple processes. It is recommended that you call both of
4178: the above preallocation routines for simplicity.
4180: Options Database Keys:
4181: . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
4183: Developer Notes:
4184: Subclasses include MATAIJCUSPARSE, MATAIJPERM, MATAIJSELL, MATAIJMKL, MATAIJCRL, and also automatically switches over to use inodes when
4185: enough exist.
4187: Level: beginner
4189: .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
4190: M*/
4192: /*MC
4193: MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
4195: This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
4196: and MATMPIAIJCRL otherwise. As a result, for single process communicators,
4197: MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
4198: for communicators controlling multiple processes. It is recommended that you call both of
4199: the above preallocation routines for simplicity.
4201: Options Database Keys:
4202: . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
4204: Level: beginner
4206: .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
4207: M*/
4209: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
4210: #if defined(PETSC_HAVE_ELEMENTAL)
4211: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
4212: #endif
4213: #if defined(PETSC_HAVE_HYPRE)
4214: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A,MatType,MatReuse,Mat*);
4215: #endif
4216: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*);
4218: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat,MatType,MatReuse,Mat*);
4219: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat,MatType,MatReuse,Mat*);
4220: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_IS_XAIJ(Mat);
4222: /*@C
4223: MatSeqAIJGetArray - gives read/write access to the array where the data for a MATSEQAIJ matrix is stored
4225: Not Collective
4227: Input Parameter:
4228: . mat - a MATSEQAIJ matrix
4230: Output Parameter:
4231: . array - pointer to the data
4233: Level: intermediate
4235: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4236: @*/
4237: PetscErrorCode MatSeqAIJGetArray(Mat A,PetscScalar **array)
4238: {
4242: PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));
4243: return(0);
4244: }
4246: /*@C
4247: MatSeqAIJGetArrayRead - gives read-only access to the array where the data for a MATSEQAIJ matrix is stored
4249: Not Collective
4251: Input Parameter:
4252: . mat - a MATSEQAIJ matrix
4254: Output Parameter:
4255: . array - pointer to the data
4257: Level: intermediate
4259: .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayRead()
4260: @*/
4261: PetscErrorCode MatSeqAIJGetArrayRead(Mat A,const PetscScalar **array)
4262: {
4263: #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL)
4264: PetscOffloadMask oval;
4265: #endif
4269: #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL)
4270: oval = A->offloadmask;
4271: #endif
4272: MatSeqAIJGetArray(A,(PetscScalar**)array);
4273: #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL)
4274: if (oval == PETSC_OFFLOAD_GPU || oval == PETSC_OFFLOAD_BOTH) A->offloadmask = PETSC_OFFLOAD_BOTH;
4275: #endif
4276: return(0);
4277: }
4279: /*@C
4280: MatSeqAIJRestoreArrayRead - restore the read-only access array obtained from MatSeqAIJGetArrayRead
4282: Not Collective
4284: Input Parameter:
4285: . mat - a MATSEQAIJ matrix
4287: Output Parameter:
4288: . array - pointer to the data
4290: Level: intermediate
4292: .seealso: MatSeqAIJGetArray(), MatSeqAIJGetArrayRead()
4293: @*/
4294: PetscErrorCode MatSeqAIJRestoreArrayRead(Mat A,const PetscScalar **array)
4295: {
4296: #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL)
4297: PetscOffloadMask oval;
4298: #endif
4302: #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL)
4303: oval = A->offloadmask;
4304: #endif
4305: MatSeqAIJRestoreArray(A,(PetscScalar**)array);
4306: #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL)
4307: A->offloadmask = oval;
4308: #endif
4309: return(0);
4310: }
4312: /*@C
4313: MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row
4315: Not Collective
4317: Input Parameter:
4318: . mat - a MATSEQAIJ matrix
4320: Output Parameter:
4321: . nz - the maximum number of nonzeros in any row
4323: Level: intermediate
4325: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4326: @*/
4327: PetscErrorCode MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
4328: {
4329: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)A->data;
4332: *nz = aij->rmax;
4333: return(0);
4334: }
4336: /*@C
4337: MatSeqAIJRestoreArray - returns access to the array where the data for a MATSEQAIJ matrix is stored obtained by MatSeqAIJGetArray()
4339: Not Collective
4341: Input Parameters:
4342: + mat - a MATSEQAIJ matrix
4343: - array - pointer to the data
4345: Level: intermediate
4347: .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
4348: @*/
4349: PetscErrorCode MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
4350: {
4354: PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));
4355: return(0);
4356: }
4358: #if defined(PETSC_HAVE_CUDA)
4359: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat);
4360: #endif
4362: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4363: {
4364: Mat_SeqAIJ *b;
4366: PetscMPIInt size;
4369: MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);
4370: if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
4372: PetscNewLog(B,&b);
4374: B->data = (void*)b;
4376: PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
4377: if (B->sortedfull) B->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;
4379: b->row = 0;
4380: b->col = 0;
4381: b->icol = 0;
4382: b->reallocs = 0;
4383: b->ignorezeroentries = PETSC_FALSE;
4384: b->roworiented = PETSC_TRUE;
4385: b->nonew = 0;
4386: b->diag = 0;
4387: b->solve_work = 0;
4388: B->spptr = 0;
4389: b->saved_values = 0;
4390: b->idiag = 0;
4391: b->mdiag = 0;
4392: b->ssor_work = 0;
4393: b->omega = 1.0;
4394: b->fshift = 0.0;
4395: b->idiagvalid = PETSC_FALSE;
4396: b->ibdiagvalid = PETSC_FALSE;
4397: b->keepnonzeropattern = PETSC_FALSE;
4399: PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4400: PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);
4401: PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);
4403: #if defined(PETSC_HAVE_MATLAB_ENGINE)
4404: PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);
4405: PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);
4406: #endif
4408: PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);
4409: PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);
4410: PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);
4411: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);
4412: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);
4413: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);
4414: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijsell_C",MatConvert_SeqAIJ_SeqAIJSELL);
4415: #if defined(PETSC_HAVE_MKL_SPARSE)
4416: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijmkl_C",MatConvert_SeqAIJ_SeqAIJMKL);
4417: #endif
4418: #if defined(PETSC_HAVE_CUDA)
4419: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcusparse_C",MatConvert_SeqAIJ_SeqAIJCUSPARSE);
4420: PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqaijcusparse_seqaij_C",MatProductSetFromOptions_SeqAIJ);
4421: #endif
4422: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);
4423: #if defined(PETSC_HAVE_ELEMENTAL)
4424: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);
4425: #endif
4426: #if defined(PETSC_HAVE_HYPRE)
4427: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_hypre_C",MatConvert_AIJ_HYPRE);
4428: PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_transpose_seqaij_seqaij_C",MatProductSetFromOptions_Transpose_AIJ_AIJ);
4429: #endif
4430: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);
4431: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsell_C",MatConvert_SeqAIJ_SeqSELL);
4432: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_is_C",MatConvert_XAIJ_IS);
4433: PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);
4434: PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);
4435: PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);
4436: PetscObjectComposeFunction((PetscObject)B,"MatResetPreallocation_C",MatResetPreallocation_SeqAIJ);
4437: PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);
4438: PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);
4439: PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);
4440: PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);
4441: PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_is_seqaij_C",MatProductSetFromOptions_IS_XAIJ);
4442: PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqdense_seqaij_C",MatProductSetFromOptions_SeqDense_SeqAIJ);
4443: PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqaij_seqaij_C",MatProductSetFromOptions_SeqAIJ);
4444: MatCreate_SeqAIJ_Inode(B);
4445: PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4446: MatSeqAIJSetTypeFromOptions(B); /* this allows changing the matrix subtype to say MATSEQAIJPERM */
4447: return(0);
4448: }
4450: /*
4451: Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4452: */
4453: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
4454: {
4455: Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data;
4457: PetscInt m = A->rmap->n,i;
4460: c = (Mat_SeqAIJ*)C->data;
4462: C->factortype = A->factortype;
4463: c->row = 0;
4464: c->col = 0;
4465: c->icol = 0;
4466: c->reallocs = 0;
4468: C->assembled = PETSC_TRUE;
4470: PetscLayoutReference(A->rmap,&C->rmap);
4471: PetscLayoutReference(A->cmap,&C->cmap);
4473: PetscMalloc1(m,&c->imax);
4474: PetscMemcpy(c->imax,a->imax,m*sizeof(PetscInt));
4475: PetscMalloc1(m,&c->ilen);
4476: PetscMemcpy(c->ilen,a->ilen,m*sizeof(PetscInt));
4477: PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));
4479: /* allocate the matrix space */
4480: if (mallocmatspace) {
4481: PetscMalloc3(a->i[m],&c->a,a->i[m],&c->j,m+1,&c->i);
4482: PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));
4484: c->singlemalloc = PETSC_TRUE;
4486: PetscArraycpy(c->i,a->i,m+1);
4487: if (m > 0) {
4488: PetscArraycpy(c->j,a->j,a->i[m]);
4489: if (cpvalues == MAT_COPY_VALUES) {
4490: PetscArraycpy(c->a,a->a,a->i[m]);
4491: } else {
4492: PetscArrayzero(c->a,a->i[m]);
4493: }
4494: }
4495: }
4497: c->ignorezeroentries = a->ignorezeroentries;
4498: c->roworiented = a->roworiented;
4499: c->nonew = a->nonew;
4500: if (a->diag) {
4501: PetscMalloc1(m+1,&c->diag);
4502: PetscMemcpy(c->diag,a->diag,m*sizeof(PetscInt));
4503: PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));
4504: } else c->diag = NULL;
4506: c->solve_work = 0;
4507: c->saved_values = 0;
4508: c->idiag = 0;
4509: c->ssor_work = 0;
4510: c->keepnonzeropattern = a->keepnonzeropattern;
4511: c->free_a = PETSC_TRUE;
4512: c->free_ij = PETSC_TRUE;
4514: c->rmax = a->rmax;
4515: c->nz = a->nz;
4516: c->maxnz = a->nz; /* Since we allocate exactly the right amount */
4517: C->preallocated = PETSC_TRUE;
4519: c->compressedrow.use = a->compressedrow.use;
4520: c->compressedrow.nrows = a->compressedrow.nrows;
4521: if (a->compressedrow.use) {
4522: i = a->compressedrow.nrows;
4523: PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);
4524: PetscArraycpy(c->compressedrow.i,a->compressedrow.i,i+1);
4525: PetscArraycpy(c->compressedrow.rindex,a->compressedrow.rindex,i);
4526: } else {
4527: c->compressedrow.use = PETSC_FALSE;
4528: c->compressedrow.i = NULL;
4529: c->compressedrow.rindex = NULL;
4530: }
4531: c->nonzerorowcnt = a->nonzerorowcnt;
4532: C->nonzerostate = A->nonzerostate;
4534: MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);
4535: PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);
4536: return(0);
4537: }
4539: PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4540: {
4544: MatCreate(PetscObjectComm((PetscObject)A),B);
4545: MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);
4546: if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
4547: MatSetBlockSizesFromMats(*B,A,A);
4548: }
4549: MatSetType(*B,((PetscObject)A)->type_name);
4550: MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);
4551: return(0);
4552: }
4554: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4555: {
4556: PetscBool isbinary, ishdf5;
4562: /* force binary viewer to load .info file if it has not yet done so */
4563: PetscViewerSetUp(viewer);
4564: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
4565: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5, &ishdf5);
4566: if (isbinary) {
4567: MatLoad_SeqAIJ_Binary(newMat,viewer);
4568: } else if (ishdf5) {
4569: #if defined(PETSC_HAVE_HDF5)
4570: MatLoad_AIJ_HDF5(newMat,viewer);
4571: #else
4572: SETERRQ(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
4573: #endif
4574: } else {
4575: SETERRQ2(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"Viewer type %s not yet supported for reading %s matrices",((PetscObject)viewer)->type_name,((PetscObject)newMat)->type_name);
4576: }
4577: return(0);
4578: }
4580: PetscErrorCode MatLoad_SeqAIJ_Binary(Mat mat, PetscViewer viewer)
4581: {
4582: Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->data;
4584: PetscInt header[4],*rowlens,M,N,nz,sum,rows,cols,i;
4587: PetscViewerSetUp(viewer);
4589: /* read in matrix header */
4590: PetscViewerBinaryRead(viewer,header,4,NULL,PETSC_INT);
4591: if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Not a matrix object in file");
4592: M = header[1]; N = header[2]; nz = header[3];
4593: if (M < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Matrix row size (%D) in file is negative",M);
4594: if (N < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Matrix column size (%D) in file is negative",N);
4595: if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk, cannot load as SeqAIJ");
4597: /* set block sizes from the viewer's .info file */
4598: MatLoad_Binary_BlockSizes(mat,viewer);
4599: /* set local and global sizes if not set already */
4600: if (mat->rmap->n < 0) mat->rmap->n = M;
4601: if (mat->cmap->n < 0) mat->cmap->n = N;
4602: if (mat->rmap->N < 0) mat->rmap->N = M;
4603: if (mat->cmap->N < 0) mat->cmap->N = N;
4604: PetscLayoutSetUp(mat->rmap);
4605: PetscLayoutSetUp(mat->cmap);
4607: /* check if the matrix sizes are correct */
4608: MatGetSize(mat,&rows,&cols);
4609: if (M != rows || N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different sizes (%D, %D) than the input matrix (%D, %D)",M,N,rows,cols);
4611: /* read in row lengths */
4612: PetscMalloc1(M,&rowlens);
4613: PetscViewerBinaryRead(viewer,rowlens,M,NULL,PETSC_INT);
4614: /* check if sum(rowlens) is same as nz */
4615: sum = 0; for (i=0; i<M; i++) sum += rowlens[i];
4616: if (sum != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Inconsistent matrix data in file: nonzeros = %D, sum-row-lengths = %D\n",nz,sum);
4617: /* preallocate and check sizes */
4618: MatSeqAIJSetPreallocation_SeqAIJ(mat,0,rowlens);
4619: MatGetSize(mat,&rows,&cols);
4620: 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);
4621: /* store row lengths */
4622: PetscArraycpy(a->ilen,rowlens,M);
4623: PetscFree(rowlens);
4625: /* fill in "i" row pointers */
4626: a->i[0] = 0; for (i=0; i<M; i++) a->i[i+1] = a->i[i] + a->ilen[i];
4627: /* read in "j" column indices */
4628: PetscViewerBinaryRead(viewer,a->j,nz,NULL,PETSC_INT);
4629: /* read in "a" nonzero values */
4630: PetscViewerBinaryRead(viewer,a->a,nz,NULL,PETSC_SCALAR);
4632: MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
4633: MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
4634: return(0);
4635: }
4637: PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
4638: {
4639: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4641: #if defined(PETSC_USE_COMPLEX)
4642: PetscInt k;
4643: #endif
4646: /* If the matrix dimensions are not equal,or no of nonzeros */
4647: if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4648: *flg = PETSC_FALSE;
4649: return(0);
4650: }
4652: /* if the a->i are the same */
4653: PetscArraycmp(a->i,b->i,A->rmap->n+1,flg);
4654: if (!*flg) return(0);
4656: /* if a->j are the same */
4657: PetscArraycmp(a->j,b->j,a->nz,flg);
4658: if (!*flg) return(0);
4660: /* if a->a are the same */
4661: #if defined(PETSC_USE_COMPLEX)
4662: for (k=0; k<a->nz; k++) {
4663: if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4664: *flg = PETSC_FALSE;
4665: return(0);
4666: }
4667: }
4668: #else
4669: PetscArraycmp(a->a,b->a,a->nz,flg);
4670: #endif
4671: return(0);
4672: }
4674: /*@
4675: MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
4676: provided by the user.
4678: Collective
4680: Input Parameters:
4681: + comm - must be an MPI communicator of size 1
4682: . m - number of rows
4683: . n - number of columns
4684: . i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
4685: . j - column indices
4686: - a - matrix values
4688: Output Parameter:
4689: . mat - the matrix
4691: Level: intermediate
4693: Notes:
4694: The i, j, and a arrays are not copied by this routine, the user must free these arrays
4695: once the matrix is destroyed and not before
4697: You cannot set new nonzero locations into this matrix, that will generate an error.
4699: The i and j indices are 0 based
4701: The format which is used for the sparse matrix input, is equivalent to a
4702: row-major ordering.. i.e for the following matrix, the input data expected is
4703: as shown
4705: $ 1 0 0
4706: $ 2 0 3
4707: $ 4 5 6
4708: $
4709: $ i = {0,1,3,6} [size = nrow+1 = 3+1]
4710: $ j = {0,0,2,0,1,2} [size = 6]; values must be sorted for each row
4711: $ v = {1,2,3,4,5,6} [size = 6]
4714: .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
4716: @*/
4717: PetscErrorCode MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat)
4718: {
4720: PetscInt ii;
4721: Mat_SeqAIJ *aij;
4722: #if defined(PETSC_USE_DEBUG)
4723: PetscInt jj;
4724: #endif
4727: if (m > 0 && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4728: MatCreate(comm,mat);
4729: MatSetSizes(*mat,m,n,m,n);
4730: /* MatSetBlockSizes(*mat,,); */
4731: MatSetType(*mat,MATSEQAIJ);
4732: MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);
4733: aij = (Mat_SeqAIJ*)(*mat)->data;
4734: PetscMalloc1(m,&aij->imax);
4735: PetscMalloc1(m,&aij->ilen);
4737: aij->i = i;
4738: aij->j = j;
4739: aij->a = a;
4740: aij->singlemalloc = PETSC_FALSE;
4741: aij->nonew = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4742: aij->free_a = PETSC_FALSE;
4743: aij->free_ij = PETSC_FALSE;
4745: for (ii=0; ii<m; ii++) {
4746: aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
4747: #if defined(PETSC_USE_DEBUG)
4748: 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]);
4749: for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4750: if (j[jj] < j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual column %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
4751: if (j[jj] == j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual column %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii);
4752: }
4753: #endif
4754: }
4755: #if defined(PETSC_USE_DEBUG)
4756: for (ii=0; ii<aij->i[m]; ii++) {
4757: if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
4758: 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]);
4759: }
4760: #endif
4762: MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4763: MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4764: return(0);
4765: }
4766: /*@C
4767: MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
4768: provided by the user.
4770: Collective
4772: Input Parameters:
4773: + comm - must be an MPI communicator of size 1
4774: . m - number of rows
4775: . n - number of columns
4776: . i - row indices
4777: . j - column indices
4778: . a - matrix values
4779: . nz - number of nonzeros
4780: - idx - 0 or 1 based
4782: Output Parameter:
4783: . mat - the matrix
4785: Level: intermediate
4787: Notes:
4788: The i and j indices are 0 based
4790: The format which is used for the sparse matrix input, is equivalent to a
4791: row-major ordering.. i.e for the following matrix, the input data expected is
4792: as shown:
4794: 1 0 0
4795: 2 0 3
4796: 4 5 6
4798: i = {0,1,1,2,2,2}
4799: j = {0,0,2,0,1,2}
4800: v = {1,2,3,4,5,6}
4803: .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
4805: @*/
4806: PetscErrorCode MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat,PetscInt nz,PetscBool idx)
4807: {
4809: PetscInt ii, *nnz, one = 1,row,col;
4813: PetscCalloc1(m,&nnz);
4814: for (ii = 0; ii < nz; ii++) {
4815: nnz[i[ii] - !!idx] += 1;
4816: }
4817: MatCreate(comm,mat);
4818: MatSetSizes(*mat,m,n,m,n);
4819: MatSetType(*mat,MATSEQAIJ);
4820: MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);
4821: for (ii = 0; ii < nz; ii++) {
4822: if (idx) {
4823: row = i[ii] - 1;
4824: col = j[ii] - 1;
4825: } else {
4826: row = i[ii];
4827: col = j[ii];
4828: }
4829: MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);
4830: }
4831: MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4832: MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4833: PetscFree(nnz);
4834: return(0);
4835: }
4837: PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4838: {
4839: Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data;
4843: a->idiagvalid = PETSC_FALSE;
4844: a->ibdiagvalid = PETSC_FALSE;
4846: MatSeqAIJInvalidateDiagonal_Inode(A);
4847: return(0);
4848: }
4850: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
4851: {
4853: PetscMPIInt size;
4856: MPI_Comm_size(comm,&size);
4857: if (size == 1) {
4858: if (scall == MAT_INITIAL_MATRIX) {
4859: MatDuplicate(inmat,MAT_COPY_VALUES,outmat);
4860: } else {
4861: MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);
4862: }
4863: } else {
4864: MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);
4865: }
4866: return(0);
4867: }
4869: /*
4870: Permute A into C's *local* index space using rowemb,colemb.
4871: The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
4872: of [0,m), colemb is in [0,n).
4873: If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
4874: */
4875: PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B)
4876: {
4877: /* If making this function public, change the error returned in this function away from _PLIB. */
4879: Mat_SeqAIJ *Baij;
4880: PetscBool seqaij;
4881: PetscInt m,n,*nz,i,j,count;
4882: PetscScalar v;
4883: const PetscInt *rowindices,*colindices;
4886: if (!B) return(0);
4887: /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
4888: PetscObjectBaseTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);
4889: if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type");
4890: if (rowemb) {
4891: ISGetLocalSize(rowemb,&m);
4892: if (m != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Row IS of size %D is incompatible with matrix row size %D",m,B->rmap->n);
4893: } else {
4894: if (C->rmap->n != B->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix");
4895: }
4896: if (colemb) {
4897: ISGetLocalSize(colemb,&n);
4898: if (n != B->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Diag col IS of size %D is incompatible with input matrix col size %D",n,B->cmap->n);
4899: } else {
4900: if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix");
4901: }
4903: Baij = (Mat_SeqAIJ*)(B->data);
4904: if (pattern == DIFFERENT_NONZERO_PATTERN) {
4905: PetscMalloc1(B->rmap->n,&nz);
4906: for (i=0; i<B->rmap->n; i++) {
4907: nz[i] = Baij->i[i+1] - Baij->i[i];
4908: }
4909: MatSeqAIJSetPreallocation(C,0,nz);
4910: PetscFree(nz);
4911: }
4912: if (pattern == SUBSET_NONZERO_PATTERN) {
4913: MatZeroEntries(C);
4914: }
4915: count = 0;
4916: rowindices = NULL;
4917: colindices = NULL;
4918: if (rowemb) {
4919: ISGetIndices(rowemb,&rowindices);
4920: }
4921: if (colemb) {
4922: ISGetIndices(colemb,&colindices);
4923: }
4924: for (i=0; i<B->rmap->n; i++) {
4925: PetscInt row;
4926: row = i;
4927: if (rowindices) row = rowindices[i];
4928: for (j=Baij->i[i]; j<Baij->i[i+1]; j++) {
4929: PetscInt col;
4930: col = Baij->j[count];
4931: if (colindices) col = colindices[col];
4932: v = Baij->a[count];
4933: MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);
4934: ++count;
4935: }
4936: }
4937: /* FIXME: set C's nonzerostate correctly. */
4938: /* Assembly for C is necessary. */
4939: C->preallocated = PETSC_TRUE;
4940: C->assembled = PETSC_TRUE;
4941: C->was_assembled = PETSC_FALSE;
4942: return(0);
4943: }
4945: PetscFunctionList MatSeqAIJList = NULL;
4947: /*@C
4948: MatSeqAIJSetType - Converts a MATSEQAIJ matrix to a subtype
4950: Collective on Mat
4952: Input Parameters:
4953: + mat - the matrix object
4954: - matype - matrix type
4956: Options Database Key:
4957: . -mat_seqai_type <method> - for example seqaijcrl
4960: Level: intermediate
4962: .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
4963: @*/
4964: PetscErrorCode MatSeqAIJSetType(Mat mat, MatType matype)
4965: {
4966: PetscErrorCode ierr,(*r)(Mat,MatType,MatReuse,Mat*);
4967: PetscBool sametype;
4971: PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);
4972: if (sametype) return(0);
4974: PetscFunctionListFind(MatSeqAIJList,matype,&r);
4975: if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
4976: (*r)(mat,matype,MAT_INPLACE_MATRIX,&mat);
4977: return(0);
4978: }
4981: /*@C
4982: MatSeqAIJRegister - - Adds a new sub-matrix type for sequential AIJ matrices
4984: Not Collective
4986: Input Parameters:
4987: + name - name of a new user-defined matrix type, for example MATSEQAIJCRL
4988: - function - routine to convert to subtype
4990: Notes:
4991: MatSeqAIJRegister() may be called multiple times to add several user-defined solvers.
4994: Then, your matrix can be chosen with the procedural interface at runtime via the option
4995: $ -mat_seqaij_type my_mat
4997: Level: advanced
4999: .seealso: MatSeqAIJRegisterAll()
5002: Level: advanced
5003: @*/
5004: PetscErrorCode MatSeqAIJRegister(const char sname[],PetscErrorCode (*function)(Mat,MatType,MatReuse,Mat *))
5005: {
5009: MatInitializePackage();
5010: PetscFunctionListAdd(&MatSeqAIJList,sname,function);
5011: return(0);
5012: }
5014: PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE;
5016: /*@C
5017: MatSeqAIJRegisterAll - Registers all of the matrix subtypes of SeqAIJ
5019: Not Collective
5021: Level: advanced
5023: Developers Note: CUSP and CUSPARSE do not yet support the MatConvert_SeqAIJ..() paradigm and thus cannot be registered here
5025: .seealso: MatRegisterAll(), MatSeqAIJRegister()
5026: @*/
5027: PetscErrorCode MatSeqAIJRegisterAll(void)
5028: {
5032: if (MatSeqAIJRegisterAllCalled) return(0);
5033: MatSeqAIJRegisterAllCalled = PETSC_TRUE;
5035: MatSeqAIJRegister(MATSEQAIJCRL, MatConvert_SeqAIJ_SeqAIJCRL);
5036: MatSeqAIJRegister(MATSEQAIJPERM, MatConvert_SeqAIJ_SeqAIJPERM);
5037: MatSeqAIJRegister(MATSEQAIJSELL, MatConvert_SeqAIJ_SeqAIJSELL);
5038: #if defined(PETSC_HAVE_MKL_SPARSE)
5039: MatSeqAIJRegister(MATSEQAIJMKL, MatConvert_SeqAIJ_SeqAIJMKL);
5040: #endif
5041: #if defined(PETSC_HAVE_VIENNACL) && defined(PETSC_HAVE_VIENNACL_NO_CUDA)
5042: MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL);
5043: #endif
5044: return(0);
5045: }
5047: /*
5048: Special version for direct calls from Fortran
5049: */
5050: #include <petsc/private/fortranimpl.h>
5051: #if defined(PETSC_HAVE_FORTRAN_CAPS)
5052: #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
5053: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
5054: #define matsetvaluesseqaij_ matsetvaluesseqaij
5055: #endif
5057: /* Change these macros so can be used in void function */
5058: #undef CHKERRQ
5059: #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
5060: #undef SETERRQ2
5061: #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
5062: #undef SETERRQ3
5063: #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
5065: PETSC_EXTERN void matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
5066: {
5067: Mat A = *AA;
5068: PetscInt m = *mm, n = *nn;
5069: InsertMode is = *isis;
5070: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
5071: PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
5072: PetscInt *imax,*ai,*ailen;
5074: PetscInt *aj,nonew = a->nonew,lastcol = -1;
5075: MatScalar *ap,value,*aa;
5076: PetscBool ignorezeroentries = a->ignorezeroentries;
5077: PetscBool roworiented = a->roworiented;
5080: MatCheckPreallocated(A,1);
5081: imax = a->imax;
5082: ai = a->i;
5083: ailen = a->ilen;
5084: aj = a->j;
5085: aa = a->a;
5087: for (k=0; k<m; k++) { /* loop over added rows */
5088: row = im[k];
5089: if (row < 0) continue;
5090: #if defined(PETSC_USE_DEBUG)
5091: if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
5092: #endif
5093: rp = aj + ai[row]; ap = aa + ai[row];
5094: rmax = imax[row]; nrow = ailen[row];
5095: low = 0;
5096: high = nrow;
5097: for (l=0; l<n; l++) { /* loop over added columns */
5098: if (in[l] < 0) continue;
5099: #if defined(PETSC_USE_DEBUG)
5100: if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
5101: #endif
5102: col = in[l];
5103: if (roworiented) value = v[l + k*n];
5104: else value = v[k + l*m];
5106: if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
5108: if (col <= lastcol) low = 0;
5109: else high = nrow;
5110: lastcol = col;
5111: while (high-low > 5) {
5112: t = (low+high)/2;
5113: if (rp[t] > col) high = t;
5114: else low = t;
5115: }
5116: for (i=low; i<high; i++) {
5117: if (rp[i] > col) break;
5118: if (rp[i] == col) {
5119: if (is == ADD_VALUES) ap[i] += value;
5120: else ap[i] = value;
5121: goto noinsert;
5122: }
5123: }
5124: if (value == 0.0 && ignorezeroentries) goto noinsert;
5125: if (nonew == 1) goto noinsert;
5126: if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
5127: MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
5128: N = nrow++ - 1; a->nz++; high++;
5129: /* shift up all the later entries in this row */
5130: for (ii=N; ii>=i; ii--) {
5131: rp[ii+1] = rp[ii];
5132: ap[ii+1] = ap[ii];
5133: }
5134: rp[i] = col;
5135: ap[i] = value;
5136: A->nonzerostate++;
5137: noinsert:;
5138: low = i + 1;
5139: }
5140: ailen[row] = nrow;
5141: }
5142: PetscFunctionReturnVoid();
5143: }