Actual source code: aij.c
petsc-3.14.0 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 = NULL;
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_DEVICE)
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_DEVICE)
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_DEVICE)
196: if (v[i] != 0.0) inserted = PETSC_TRUE;
197: #endif
198: aa[diag[i]] += v[i];
199: }
200: }
201: #if defined(PETSC_HAVE_DEVICE)
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_DEVICE)
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_DEVICE)
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_DEVICE)
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 (PetscUnlikelyDebug(row >= A->rmap->n)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
455: rp = aj + ai[row];
456: if (!A->structure_only) ap = aa + ai[row];
457: rmax = imax[row]; nrow = ailen[row];
458: low = 0;
459: high = nrow;
460: for (l=0; l<n; l++) { /* loop over added columns */
461: if (in[l] < 0) continue;
462: if (PetscUnlikelyDebug(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);
463: col = in[l];
464: if (v && !A->structure_only) value = roworiented ? v[l + k*n] : v[k + l*m];
465: if (!A->structure_only && value == 0.0 && ignorezeroentries && is == ADD_VALUES && row != col) continue;
467: if (col <= lastcol) low = 0;
468: else high = nrow;
469: lastcol = col;
470: while (high-low > 5) {
471: t = (low+high)/2;
472: if (rp[t] > col) high = t;
473: else low = t;
474: }
475: for (i=low; i<high; i++) {
476: if (rp[i] > col) break;
477: if (rp[i] == col) {
478: if (!A->structure_only) {
479: if (is == ADD_VALUES) {
480: ap[i] += value;
481: (void)PetscLogFlops(1.0);
482: }
483: else ap[i] = value;
484: #if defined(PETSC_HAVE_DEVICE)
485: inserted = PETSC_TRUE;
486: #endif
487: }
488: low = i + 1;
489: goto noinsert;
490: }
491: }
492: if (value == 0.0 && ignorezeroentries && row != col) goto noinsert;
493: if (nonew == 1) goto noinsert;
494: if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
495: if (A->structure_only) {
496: MatSeqXAIJReallocateAIJ_structure_only(A,A->rmap->n,1,nrow,row,col,rmax,ai,aj,rp,imax,nonew,MatScalar);
497: } else {
498: MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
499: }
500: N = nrow++ - 1; a->nz++; high++;
501: /* shift up all the later entries in this row */
502: PetscArraymove(rp+i+1,rp+i,N-i+1);
503: rp[i] = col;
504: if (!A->structure_only){
505: PetscArraymove(ap+i+1,ap+i,N-i+1);
506: ap[i] = value;
507: }
508: low = i + 1;
509: A->nonzerostate++;
510: #if defined(PETSC_HAVE_DEVICE)
511: inserted = PETSC_TRUE;
512: #endif
513: noinsert:;
514: }
515: ailen[row] = nrow;
516: }
517: #if defined(PETSC_HAVE_DEVICE)
518: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && inserted) A->offloadmask = PETSC_OFFLOAD_CPU;
519: #endif
520: return(0);
521: }
524: PetscErrorCode MatSetValues_SeqAIJ_SortedFullNoPreallocation(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
525: {
526: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
527: PetscInt *rp,k,row;
528: PetscInt *ai = a->i;
530: PetscInt *aj = a->j;
531: MatScalar *aa = a->a,*ap;
534: if (A->was_assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot call on assembled matrix.");
535: if (m*n+a->nz > a->maxnz) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of entries in matrix will be larger than maximum nonzeros allocated for %D in MatSeqAIJSetTotalPreallocation()",a->maxnz);
536: for (k=0; k<m; k++) { /* loop over added rows */
537: row = im[k];
538: rp = aj + ai[row];
539: ap = aa + ai[row];
541: PetscMemcpy(rp,in,n*sizeof(PetscInt));
542: if (!A->structure_only) {
543: if (v) {
544: PetscMemcpy(ap,v,n*sizeof(PetscScalar));
545: v += n;
546: } else {
547: PetscMemzero(ap,n*sizeof(PetscScalar));
548: }
549: }
550: a->ilen[row] = n;
551: a->imax[row] = n;
552: a->i[row+1] = a->i[row]+n;
553: a->nz += n;
554: }
555: #if defined(PETSC_HAVE_DEVICE)
556: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && m && n) A->offloadmask = PETSC_OFFLOAD_CPU;
557: #endif
558: return(0);
559: }
561: /*@
562: MatSeqAIJSetTotalPreallocation - Sets an upper bound on the total number of expected nonzeros in the matrix.
564: Input Parameters:
565: + A - the SeqAIJ matrix
566: - nztotal - bound on the number of nonzeros
568: Level: advanced
570: Notes:
571: This can be called if you will be provided the matrix row by row (from row zero) with sorted column indices for each row.
572: Simply call MatSetValues() after this call to provide the matrix entries in the usual manner. This matrix may be used
573: as always with multiple matrix assemblies.
575: .seealso: MatSetOption(), MAT_SORTED_FULL, MatSetValues(), MatSeqAIJSetPreallocation()
576: @*/
578: PetscErrorCode MatSeqAIJSetTotalPreallocation(Mat A,PetscInt nztotal)
579: {
581: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
584: PetscLayoutSetUp(A->rmap);
585: PetscLayoutSetUp(A->cmap);
586: a->maxnz = nztotal;
587: if (!a->imax) {
588: PetscMalloc1(A->rmap->n,&a->imax);
589: PetscLogObjectMemory((PetscObject)A,A->rmap->n*sizeof(PetscInt));
590: }
591: if (!a->ilen) {
592: PetscMalloc1(A->rmap->n,&a->ilen);
593: PetscLogObjectMemory((PetscObject)A,A->rmap->n*sizeof(PetscInt));
594: } else {
595: PetscMemzero(a->ilen,A->rmap->n*sizeof(PetscInt));
596: }
598: /* allocate the matrix space */
599: if (A->structure_only) {
600: PetscMalloc1(nztotal,&a->j);
601: PetscMalloc1(A->rmap->n+1,&a->i);
602: PetscLogObjectMemory((PetscObject)A,(A->rmap->n+1)*sizeof(PetscInt)+nztotal*sizeof(PetscInt));
603: } else {
604: PetscMalloc3(nztotal,&a->a,nztotal,&a->j,A->rmap->n+1,&a->i);
605: PetscLogObjectMemory((PetscObject)A,(A->rmap->n+1)*sizeof(PetscInt)+nztotal*(sizeof(PetscScalar)+sizeof(PetscInt)));
606: }
607: a->i[0] = 0;
608: if (A->structure_only) {
609: a->singlemalloc = PETSC_FALSE;
610: a->free_a = PETSC_FALSE;
611: } else {
612: a->singlemalloc = PETSC_TRUE;
613: a->free_a = PETSC_TRUE;
614: }
615: a->free_ij = PETSC_TRUE;
616: A->ops->setvalues = MatSetValues_SeqAIJ_SortedFullNoPreallocation;
617: A->preallocated = PETSC_TRUE;
618: return(0);
619: }
621: PetscErrorCode MatSetValues_SeqAIJ_SortedFull(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
622: {
623: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
624: PetscInt *rp,k,row;
625: PetscInt *ai = a->i,*ailen = a->ilen;
627: PetscInt *aj = a->j;
628: MatScalar *aa = a->a,*ap;
631: for (k=0; k<m; k++) { /* loop over added rows */
632: row = im[k];
633: if (PetscUnlikelyDebug(n > a->imax[row])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Preallocation for row %D does not match number of columns provided",n);
634: rp = aj + ai[row];
635: ap = aa + ai[row];
636: if (!A->was_assembled) {
637: PetscMemcpy(rp,in,n*sizeof(PetscInt));
638: }
639: if (!A->structure_only) {
640: if (v) {
641: PetscMemcpy(ap,v,n*sizeof(PetscScalar));
642: v += n;
643: } else {
644: PetscMemzero(ap,n*sizeof(PetscScalar));
645: }
646: }
647: ailen[row] = n;
648: a->nz += n;
649: }
650: #if defined(PETSC_HAVE_DEVICE)
651: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && m && n) A->offloadmask = PETSC_OFFLOAD_CPU;
652: #endif
653: return(0);
654: }
657: PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
658: {
659: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
660: PetscInt *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
661: PetscInt *ai = a->i,*ailen = a->ilen;
662: MatScalar *ap,*aa = a->a;
665: for (k=0; k<m; k++) { /* loop over rows */
666: row = im[k];
667: if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
668: 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);
669: rp = aj + ai[row]; ap = aa + ai[row];
670: nrow = ailen[row];
671: for (l=0; l<n; l++) { /* loop over columns */
672: if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
673: 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);
674: col = in[l];
675: high = nrow; low = 0; /* assume unsorted */
676: while (high-low > 5) {
677: t = (low+high)/2;
678: if (rp[t] > col) high = t;
679: else low = t;
680: }
681: for (i=low; i<high; i++) {
682: if (rp[i] > col) break;
683: if (rp[i] == col) {
684: *v++ = ap[i];
685: goto finished;
686: }
687: }
688: *v++ = 0.0;
689: finished:;
690: }
691: }
692: return(0);
693: }
695: PetscErrorCode MatView_SeqAIJ_Binary(Mat mat,PetscViewer viewer)
696: {
697: Mat_SeqAIJ *A = (Mat_SeqAIJ*)mat->data;
698: PetscInt header[4],M,N,m,nz,i;
699: PetscInt *rowlens;
703: PetscViewerSetUp(viewer);
705: M = mat->rmap->N;
706: N = mat->cmap->N;
707: m = mat->rmap->n;
708: nz = A->nz;
710: /* write matrix header */
711: header[0] = MAT_FILE_CLASSID;
712: header[1] = M; header[2] = N; header[3] = nz;
713: PetscViewerBinaryWrite(viewer,header,4,PETSC_INT);
715: /* fill in and store row lengths */
716: PetscMalloc1(m,&rowlens);
717: for (i=0; i<m; i++) rowlens[i] = A->i[i+1] - A->i[i];
718: PetscViewerBinaryWrite(viewer,rowlens,m,PETSC_INT);
719: PetscFree(rowlens);
720: /* store column indices */
721: PetscViewerBinaryWrite(viewer,A->j,nz,PETSC_INT);
722: /* store nonzero values */
723: PetscViewerBinaryWrite(viewer,A->a,nz,PETSC_SCALAR);
725: /* write block size option to the viewer's .info file */
726: MatView_Binary_BlockSizes(mat,viewer);
727: return(0);
728: }
730: static PetscErrorCode MatView_SeqAIJ_ASCII_structonly(Mat A,PetscViewer viewer)
731: {
733: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
734: PetscInt i,k,m=A->rmap->N;
737: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
738: for (i=0; i<m; i++) {
739: PetscViewerASCIIPrintf(viewer,"row %D:",i);
740: for (k=a->i[i]; k<a->i[i+1]; k++) {
741: PetscViewerASCIIPrintf(viewer," (%D) ",a->j[k]);
742: }
743: PetscViewerASCIIPrintf(viewer,"\n");
744: }
745: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
746: return(0);
747: }
749: extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
751: PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
752: {
753: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
754: PetscErrorCode ierr;
755: PetscInt i,j,m = A->rmap->n;
756: const char *name;
757: PetscViewerFormat format;
760: if (A->structure_only) {
761: MatView_SeqAIJ_ASCII_structonly(A,viewer);
762: return(0);
763: }
765: PetscViewerGetFormat(viewer,&format);
766: if (format == PETSC_VIEWER_ASCII_MATLAB) {
767: PetscInt nofinalvalue = 0;
768: if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) {
769: /* Need a dummy value to ensure the dimension of the matrix. */
770: nofinalvalue = 1;
771: }
772: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
773: PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);
774: PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);
775: #if defined(PETSC_USE_COMPLEX)
776: PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);
777: #else
778: PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);
779: #endif
780: PetscViewerASCIIPrintf(viewer,"zzz = [\n");
782: for (i=0; i<m; i++) {
783: for (j=a->i[i]; j<a->i[i+1]; j++) {
784: #if defined(PETSC_USE_COMPLEX)
785: 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]));
786: #else
787: PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);
788: #endif
789: }
790: }
791: if (nofinalvalue) {
792: #if defined(PETSC_USE_COMPLEX)
793: PetscViewerASCIIPrintf(viewer,"%D %D %18.16e %18.16e\n",m,A->cmap->n,0.,0.);
794: #else
795: PetscViewerASCIIPrintf(viewer,"%D %D %18.16e\n",m,A->cmap->n,0.0);
796: #endif
797: }
798: PetscObjectGetName((PetscObject)A,&name);
799: PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);
800: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
801: } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
802: return(0);
803: } else if (format == PETSC_VIEWER_ASCII_COMMON) {
804: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
805: for (i=0; i<m; i++) {
806: PetscViewerASCIIPrintf(viewer,"row %D:",i);
807: for (j=a->i[i]; j<a->i[i+1]; j++) {
808: #if defined(PETSC_USE_COMPLEX)
809: if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
810: PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
811: } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
812: PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));
813: } else if (PetscRealPart(a->a[j]) != 0.0) {
814: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
815: }
816: #else
817: if (a->a[j] != 0.0) {PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);}
818: #endif
819: }
820: PetscViewerASCIIPrintf(viewer,"\n");
821: }
822: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
823: } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
824: PetscInt nzd=0,fshift=1,*sptr;
825: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
826: PetscMalloc1(m+1,&sptr);
827: for (i=0; i<m; i++) {
828: sptr[i] = nzd+1;
829: for (j=a->i[i]; j<a->i[i+1]; j++) {
830: if (a->j[j] >= i) {
831: #if defined(PETSC_USE_COMPLEX)
832: if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
833: #else
834: if (a->a[j] != 0.0) nzd++;
835: #endif
836: }
837: }
838: }
839: sptr[m] = nzd+1;
840: PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);
841: for (i=0; i<m+1; i+=6) {
842: if (i+4<m) {
843: 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]);
844: } else if (i+3<m) {
845: PetscViewerASCIIPrintf(viewer," %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);
846: } else if (i+2<m) {
847: PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);
848: } else if (i+1<m) {
849: PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);
850: } else if (i<m) {
851: PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);
852: } else {
853: PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);
854: }
855: }
856: PetscViewerASCIIPrintf(viewer,"\n");
857: PetscFree(sptr);
858: for (i=0; i<m; i++) {
859: for (j=a->i[i]; j<a->i[i+1]; j++) {
860: if (a->j[j] >= i) {PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);}
861: }
862: PetscViewerASCIIPrintf(viewer,"\n");
863: }
864: PetscViewerASCIIPrintf(viewer,"\n");
865: for (i=0; i<m; i++) {
866: for (j=a->i[i]; j<a->i[i+1]; j++) {
867: if (a->j[j] >= i) {
868: #if defined(PETSC_USE_COMPLEX)
869: if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
870: PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
871: }
872: #else
873: if (a->a[j] != 0.0) {PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);}
874: #endif
875: }
876: }
877: PetscViewerASCIIPrintf(viewer,"\n");
878: }
879: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
880: } else if (format == PETSC_VIEWER_ASCII_DENSE) {
881: PetscInt cnt = 0,jcnt;
882: PetscScalar value;
883: #if defined(PETSC_USE_COMPLEX)
884: PetscBool realonly = PETSC_TRUE;
886: for (i=0; i<a->i[m]; i++) {
887: if (PetscImaginaryPart(a->a[i]) != 0.0) {
888: realonly = PETSC_FALSE;
889: break;
890: }
891: }
892: #endif
894: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
895: for (i=0; i<m; i++) {
896: jcnt = 0;
897: for (j=0; j<A->cmap->n; j++) {
898: if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
899: value = a->a[cnt++];
900: jcnt++;
901: } else {
902: value = 0.0;
903: }
904: #if defined(PETSC_USE_COMPLEX)
905: if (realonly) {
906: PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));
907: } else {
908: PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));
909: }
910: #else
911: PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);
912: #endif
913: }
914: PetscViewerASCIIPrintf(viewer,"\n");
915: }
916: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
917: } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
918: PetscInt fshift=1;
919: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
920: #if defined(PETSC_USE_COMPLEX)
921: PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate complex general\n");
922: #else
923: PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate real general\n");
924: #endif
925: PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);
926: for (i=0; i<m; i++) {
927: for (j=a->i[i]; j<a->i[i+1]; j++) {
928: #if defined(PETSC_USE_COMPLEX)
929: PetscViewerASCIIPrintf(viewer,"%D %D %g %g\n", i+fshift,a->j[j]+fshift,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
930: #else
931: PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);
932: #endif
933: }
934: }
935: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
936: } else {
937: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
938: if (A->factortype) {
939: for (i=0; i<m; i++) {
940: PetscViewerASCIIPrintf(viewer,"row %D:",i);
941: /* L part */
942: for (j=a->i[i]; j<a->i[i+1]; j++) {
943: #if defined(PETSC_USE_COMPLEX)
944: if (PetscImaginaryPart(a->a[j]) > 0.0) {
945: PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
946: } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
947: PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));
948: } else {
949: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
950: }
951: #else
952: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
953: #endif
954: }
955: /* diagonal */
956: j = a->diag[i];
957: #if defined(PETSC_USE_COMPLEX)
958: if (PetscImaginaryPart(a->a[j]) > 0.0) {
959: PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)PetscImaginaryPart(1.0/a->a[j]));
960: } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
961: PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)(-PetscImaginaryPart(1.0/a->a[j])));
962: } else {
963: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));
964: }
965: #else
966: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));
967: #endif
969: /* U part */
970: for (j=a->diag[i+1]+1; j<a->diag[i]; j++) {
971: #if defined(PETSC_USE_COMPLEX)
972: if (PetscImaginaryPart(a->a[j]) > 0.0) {
973: PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
974: } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
975: PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));
976: } else {
977: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
978: }
979: #else
980: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
981: #endif
982: }
983: PetscViewerASCIIPrintf(viewer,"\n");
984: }
985: } else {
986: for (i=0; i<m; i++) {
987: PetscViewerASCIIPrintf(viewer,"row %D:",i);
988: for (j=a->i[i]; j<a->i[i+1]; j++) {
989: #if defined(PETSC_USE_COMPLEX)
990: if (PetscImaginaryPart(a->a[j]) > 0.0) {
991: PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
992: } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
993: PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));
994: } else {
995: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
996: }
997: #else
998: PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
999: #endif
1000: }
1001: PetscViewerASCIIPrintf(viewer,"\n");
1002: }
1003: }
1004: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
1005: }
1006: PetscViewerFlush(viewer);
1007: return(0);
1008: }
1010: #include <petscdraw.h>
1011: PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
1012: {
1013: Mat A = (Mat) Aa;
1014: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1015: PetscErrorCode ierr;
1016: PetscInt i,j,m = A->rmap->n;
1017: int color;
1018: PetscReal xl,yl,xr,yr,x_l,x_r,y_l,y_r;
1019: PetscViewer viewer;
1020: PetscViewerFormat format;
1023: PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);
1024: PetscViewerGetFormat(viewer,&format);
1025: PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);
1027: /* loop over matrix elements drawing boxes */
1029: if (format != PETSC_VIEWER_DRAW_CONTOUR) {
1030: PetscDrawCollectiveBegin(draw);
1031: /* Blue for negative, Cyan for zero and Red for positive */
1032: color = PETSC_DRAW_BLUE;
1033: for (i=0; i<m; i++) {
1034: y_l = m - i - 1.0; y_r = y_l + 1.0;
1035: for (j=a->i[i]; j<a->i[i+1]; j++) {
1036: x_l = a->j[j]; x_r = x_l + 1.0;
1037: if (PetscRealPart(a->a[j]) >= 0.) continue;
1038: PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
1039: }
1040: }
1041: color = PETSC_DRAW_CYAN;
1042: for (i=0; i<m; i++) {
1043: y_l = m - i - 1.0; y_r = y_l + 1.0;
1044: for (j=a->i[i]; j<a->i[i+1]; j++) {
1045: x_l = a->j[j]; x_r = x_l + 1.0;
1046: if (a->a[j] != 0.) continue;
1047: PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
1048: }
1049: }
1050: color = PETSC_DRAW_RED;
1051: for (i=0; i<m; i++) {
1052: y_l = m - i - 1.0; y_r = y_l + 1.0;
1053: for (j=a->i[i]; j<a->i[i+1]; j++) {
1054: x_l = a->j[j]; x_r = x_l + 1.0;
1055: if (PetscRealPart(a->a[j]) <= 0.) continue;
1056: PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
1057: }
1058: }
1059: PetscDrawCollectiveEnd(draw);
1060: } else {
1061: /* use contour shading to indicate magnitude of values */
1062: /* first determine max of all nonzero values */
1063: PetscReal minv = 0.0, maxv = 0.0;
1064: PetscInt nz = a->nz, count = 0;
1065: PetscDraw popup;
1067: for (i=0; i<nz; i++) {
1068: if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
1069: }
1070: if (minv >= maxv) maxv = minv + PETSC_SMALL;
1071: PetscDrawGetPopup(draw,&popup);
1072: PetscDrawScalePopup(popup,minv,maxv);
1074: PetscDrawCollectiveBegin(draw);
1075: for (i=0; i<m; i++) {
1076: y_l = m - i - 1.0;
1077: y_r = y_l + 1.0;
1078: for (j=a->i[i]; j<a->i[i+1]; j++) {
1079: x_l = a->j[j];
1080: x_r = x_l + 1.0;
1081: color = PetscDrawRealToColor(PetscAbsScalar(a->a[count]),minv,maxv);
1082: PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
1083: count++;
1084: }
1085: }
1086: PetscDrawCollectiveEnd(draw);
1087: }
1088: return(0);
1089: }
1091: #include <petscdraw.h>
1092: PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
1093: {
1095: PetscDraw draw;
1096: PetscReal xr,yr,xl,yl,h,w;
1097: PetscBool isnull;
1100: PetscViewerDrawGetDraw(viewer,0,&draw);
1101: PetscDrawIsNull(draw,&isnull);
1102: if (isnull) return(0);
1104: xr = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
1105: xr += w; yr += h; xl = -w; yl = -h;
1106: PetscDrawSetCoordinates(draw,xl,yl,xr,yr);
1107: PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);
1108: PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);
1109: PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);
1110: PetscDrawSave(draw);
1111: return(0);
1112: }
1114: PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
1115: {
1117: PetscBool iascii,isbinary,isdraw;
1120: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1121: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1122: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1123: if (iascii) {
1124: MatView_SeqAIJ_ASCII(A,viewer);
1125: } else if (isbinary) {
1126: MatView_SeqAIJ_Binary(A,viewer);
1127: } else if (isdraw) {
1128: MatView_SeqAIJ_Draw(A,viewer);
1129: }
1130: MatView_SeqAIJ_Inode(A,viewer);
1131: return(0);
1132: }
1134: PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
1135: {
1136: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1138: PetscInt fshift = 0,i,*ai = a->i,*aj = a->j,*imax = a->imax;
1139: PetscInt m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
1140: MatScalar *aa = a->a,*ap;
1141: PetscReal ratio = 0.6;
1144: if (mode == MAT_FLUSH_ASSEMBLY) return(0);
1145: MatSeqAIJInvalidateDiagonal(A);
1146: if (A->was_assembled && A->ass_nonzerostate == A->nonzerostate) return(0);
1148: if (m) rmax = ailen[0]; /* determine row with most nonzeros */
1149: for (i=1; i<m; i++) {
1150: /* move each row back by the amount of empty slots (fshift) before it*/
1151: fshift += imax[i-1] - ailen[i-1];
1152: rmax = PetscMax(rmax,ailen[i]);
1153: if (fshift) {
1154: ip = aj + ai[i];
1155: ap = aa + ai[i];
1156: N = ailen[i];
1157: PetscArraymove(ip-fshift,ip,N);
1158: if (!A->structure_only) {
1159: PetscArraymove(ap-fshift,ap,N);
1160: }
1161: }
1162: ai[i] = ai[i-1] + ailen[i-1];
1163: }
1164: if (m) {
1165: fshift += imax[m-1] - ailen[m-1];
1166: ai[m] = ai[m-1] + ailen[m-1];
1167: }
1169: /* reset ilen and imax for each row */
1170: a->nonzerorowcnt = 0;
1171: if (A->structure_only) {
1172: PetscFree(a->imax);
1173: PetscFree(a->ilen);
1174: } else { /* !A->structure_only */
1175: for (i=0; i<m; i++) {
1176: ailen[i] = imax[i] = ai[i+1] - ai[i];
1177: a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
1178: }
1179: }
1180: a->nz = ai[m];
1181: 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);
1183: MatMarkDiagonal_SeqAIJ(A);
1184: PetscInfo4(A,"Matrix size: %D X %D; storage space: %D unneeded,%D used\n",m,A->cmap->n,fshift,a->nz);
1185: PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);
1186: PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);
1188: A->info.mallocs += a->reallocs;
1189: a->reallocs = 0;
1190: A->info.nz_unneeded = (PetscReal)fshift;
1191: a->rmax = rmax;
1193: if (!A->structure_only) {
1194: MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);
1195: }
1196: MatAssemblyEnd_SeqAIJ_Inode(A,mode);
1197: return(0);
1198: }
1200: PetscErrorCode MatRealPart_SeqAIJ(Mat A)
1201: {
1202: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1203: PetscInt i,nz = a->nz;
1204: MatScalar *aa = a->a;
1208: for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1209: MatSeqAIJInvalidateDiagonal(A);
1210: #if defined(PETSC_HAVE_DEVICE)
1211: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
1212: #endif
1213: return(0);
1214: }
1216: PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
1217: {
1218: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1219: PetscInt i,nz = a->nz;
1220: MatScalar *aa = a->a;
1224: for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1225: MatSeqAIJInvalidateDiagonal(A);
1226: #if defined(PETSC_HAVE_DEVICE)
1227: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
1228: #endif
1229: return(0);
1230: }
1232: PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
1233: {
1234: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1238: PetscArrayzero(a->a,a->i[A->rmap->n]);
1239: MatSeqAIJInvalidateDiagonal(A);
1240: #if defined(PETSC_HAVE_DEVICE)
1241: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
1242: #endif
1243: return(0);
1244: }
1246: PetscErrorCode MatDestroy_SeqAIJ(Mat A)
1247: {
1248: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1252: #if defined(PETSC_USE_LOG)
1253: PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
1254: #endif
1255: MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);
1256: ISDestroy(&a->row);
1257: ISDestroy(&a->col);
1258: PetscFree(a->diag);
1259: PetscFree(a->ibdiag);
1260: PetscFree(a->imax);
1261: PetscFree(a->ilen);
1262: PetscFree(a->ipre);
1263: PetscFree3(a->idiag,a->mdiag,a->ssor_work);
1264: PetscFree(a->solve_work);
1265: ISDestroy(&a->icol);
1266: PetscFree(a->saved_values);
1267: PetscFree2(a->compressedrow.i,a->compressedrow.rindex);
1269: MatDestroy_SeqAIJ_Inode(A);
1270: PetscFree(A->data);
1272: /* MatMatMultNumeric_SeqAIJ_SeqAIJ_Sorted may allocate this.
1273: That function is so heavily used (sometimes in an hidden way through multnumeric function pointers)
1274: that is hard to properly add this data to the MatProduct data. We free it here to avoid
1275: users reusing the matrix object with different data to incur in obscure segmentation faults
1276: due to different matrix sizes */
1277: PetscObjectCompose((PetscObject)A,"__PETSc__ab_dense",NULL);
1279: PetscObjectChangeTypeName((PetscObject)A,NULL);
1280: PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);
1281: PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);
1282: PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);
1283: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);
1284: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);
1285: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);
1287: #if defined(PETSC_HAVE_CUDA)
1288: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijcusparse_C",NULL);
1289: PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaijcusparse_seqaij_C",NULL);
1290: #endif
1291: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijcrl_C",NULL);
1292: #if defined(PETSC_HAVE_ELEMENTAL)
1293: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);
1294: #endif
1295: #if defined(PETSC_HAVE_SCALAPACK)
1296: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_scalapack_C",NULL);
1297: #endif
1298: #if defined(PETSC_HAVE_HYPRE)
1299: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_hypre_C",NULL);
1300: PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_transpose_seqaij_seqaij_C",NULL);
1301: #endif
1302: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);
1303: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsell_C",NULL);
1304: PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_is_C",NULL);
1305: PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);
1306: PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);
1307: PetscObjectComposeFunction((PetscObject)A,"MatResetPreallocation_C",NULL);
1308: PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);
1309: PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);
1310: PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_is_seqaij_C",NULL);
1311: PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqdense_seqaij_C",NULL);
1312: PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaij_seqaij_C",NULL);
1313: return(0);
1314: }
1316: PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
1317: {
1318: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1322: switch (op) {
1323: case MAT_ROW_ORIENTED:
1324: a->roworiented = flg;
1325: break;
1326: case MAT_KEEP_NONZERO_PATTERN:
1327: a->keepnonzeropattern = flg;
1328: break;
1329: case MAT_NEW_NONZERO_LOCATIONS:
1330: a->nonew = (flg ? 0 : 1);
1331: break;
1332: case MAT_NEW_NONZERO_LOCATION_ERR:
1333: a->nonew = (flg ? -1 : 0);
1334: break;
1335: case MAT_NEW_NONZERO_ALLOCATION_ERR:
1336: a->nonew = (flg ? -2 : 0);
1337: break;
1338: case MAT_UNUSED_NONZERO_LOCATION_ERR:
1339: a->nounused = (flg ? -1 : 0);
1340: break;
1341: case MAT_IGNORE_ZERO_ENTRIES:
1342: a->ignorezeroentries = flg;
1343: break;
1344: case MAT_SPD:
1345: case MAT_SYMMETRIC:
1346: case MAT_STRUCTURALLY_SYMMETRIC:
1347: case MAT_HERMITIAN:
1348: case MAT_SYMMETRY_ETERNAL:
1349: case MAT_STRUCTURE_ONLY:
1350: /* These options are handled directly by MatSetOption() */
1351: break;
1352: case MAT_NEW_DIAGONALS:
1353: case MAT_IGNORE_OFF_PROC_ENTRIES:
1354: case MAT_USE_HASH_TABLE:
1355: PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1356: break;
1357: case MAT_USE_INODES:
1358: /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1359: break;
1360: case MAT_SUBMAT_SINGLEIS:
1361: A->submat_singleis = flg;
1362: break;
1363: case MAT_SORTED_FULL:
1364: if (flg) A->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;
1365: else A->ops->setvalues = MatSetValues_SeqAIJ;
1366: break;
1367: default:
1368: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1369: }
1370: MatSetOption_SeqAIJ_Inode(A,op,flg);
1371: return(0);
1372: }
1374: PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
1375: {
1376: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1378: PetscInt i,j,n,*ai=a->i,*aj=a->j;
1379: PetscScalar *aa=a->a,*x;
1382: VecGetLocalSize(v,&n);
1383: if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
1385: if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1386: PetscInt *diag=a->diag;
1387: VecGetArrayWrite(v,&x);
1388: for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
1389: VecRestoreArrayWrite(v,&x);
1390: return(0);
1391: }
1393: VecGetArrayWrite(v,&x);
1394: for (i=0; i<n; i++) {
1395: x[i] = 0.0;
1396: for (j=ai[i]; j<ai[i+1]; j++) {
1397: if (aj[j] == i) {
1398: x[i] = aa[j];
1399: break;
1400: }
1401: }
1402: }
1403: VecRestoreArrayWrite(v,&x);
1404: return(0);
1405: }
1407: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1408: PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
1409: {
1410: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1411: PetscScalar *y;
1412: const PetscScalar *x;
1413: PetscErrorCode ierr;
1414: PetscInt m = A->rmap->n;
1415: #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1416: const MatScalar *v;
1417: PetscScalar alpha;
1418: PetscInt n,i,j;
1419: const PetscInt *idx,*ii,*ridx=NULL;
1420: Mat_CompressedRow cprow = a->compressedrow;
1421: PetscBool usecprow = cprow.use;
1422: #endif
1425: if (zz != yy) {VecCopy(zz,yy);}
1426: VecGetArrayRead(xx,&x);
1427: VecGetArray(yy,&y);
1429: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1430: fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
1431: #else
1432: if (usecprow) {
1433: m = cprow.nrows;
1434: ii = cprow.i;
1435: ridx = cprow.rindex;
1436: } else {
1437: ii = a->i;
1438: }
1439: for (i=0; i<m; i++) {
1440: idx = a->j + ii[i];
1441: v = a->a + ii[i];
1442: n = ii[i+1] - ii[i];
1443: if (usecprow) {
1444: alpha = x[ridx[i]];
1445: } else {
1446: alpha = x[i];
1447: }
1448: for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
1449: }
1450: #endif
1451: PetscLogFlops(2.0*a->nz);
1452: VecRestoreArrayRead(xx,&x);
1453: VecRestoreArray(yy,&y);
1454: return(0);
1455: }
1457: PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
1458: {
1462: VecSet(yy,0.0);
1463: MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);
1464: return(0);
1465: }
1467: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1469: PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
1470: {
1471: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1472: PetscScalar *y;
1473: const PetscScalar *x;
1474: const MatScalar *aa;
1475: PetscErrorCode ierr;
1476: PetscInt m=A->rmap->n;
1477: const PetscInt *aj,*ii,*ridx=NULL;
1478: PetscInt n,i;
1479: PetscScalar sum;
1480: PetscBool usecprow=a->compressedrow.use;
1482: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1483: #pragma disjoint(*x,*y,*aa)
1484: #endif
1487: VecGetArrayRead(xx,&x);
1488: VecGetArray(yy,&y);
1489: ii = a->i;
1490: if (usecprow) { /* use compressed row format */
1491: PetscArrayzero(y,m);
1492: m = a->compressedrow.nrows;
1493: ii = a->compressedrow.i;
1494: ridx = a->compressedrow.rindex;
1495: for (i=0; i<m; i++) {
1496: n = ii[i+1] - ii[i];
1497: aj = a->j + ii[i];
1498: aa = a->a + ii[i];
1499: sum = 0.0;
1500: PetscSparseDensePlusDot(sum,x,aa,aj,n);
1501: /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1502: y[*ridx++] = sum;
1503: }
1504: } else { /* do not use compressed row format */
1505: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1506: aj = a->j;
1507: aa = a->a;
1508: fortranmultaij_(&m,x,ii,aj,aa,y);
1509: #else
1510: for (i=0; i<m; i++) {
1511: n = ii[i+1] - ii[i];
1512: aj = a->j + ii[i];
1513: aa = a->a + ii[i];
1514: sum = 0.0;
1515: PetscSparseDensePlusDot(sum,x,aa,aj,n);
1516: y[i] = sum;
1517: }
1518: #endif
1519: }
1520: PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);
1521: VecRestoreArrayRead(xx,&x);
1522: VecRestoreArray(yy,&y);
1523: return(0);
1524: }
1526: PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1527: {
1528: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1529: PetscScalar *y;
1530: const PetscScalar *x;
1531: const MatScalar *aa;
1532: PetscErrorCode ierr;
1533: PetscInt m=A->rmap->n;
1534: const PetscInt *aj,*ii,*ridx=NULL;
1535: PetscInt n,i,nonzerorow=0;
1536: PetscScalar sum;
1537: PetscBool usecprow=a->compressedrow.use;
1539: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1540: #pragma disjoint(*x,*y,*aa)
1541: #endif
1544: VecGetArrayRead(xx,&x);
1545: VecGetArray(yy,&y);
1546: if (usecprow) { /* use compressed row format */
1547: m = a->compressedrow.nrows;
1548: ii = a->compressedrow.i;
1549: ridx = a->compressedrow.rindex;
1550: for (i=0; i<m; i++) {
1551: n = ii[i+1] - ii[i];
1552: aj = a->j + ii[i];
1553: aa = a->a + ii[i];
1554: sum = 0.0;
1555: nonzerorow += (n>0);
1556: PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1557: /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1558: y[*ridx++] = sum;
1559: }
1560: } else { /* do not use compressed row format */
1561: ii = a->i;
1562: for (i=0; i<m; i++) {
1563: n = ii[i+1] - ii[i];
1564: aj = a->j + ii[i];
1565: aa = a->a + ii[i];
1566: sum = 0.0;
1567: nonzerorow += (n>0);
1568: PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1569: y[i] = sum;
1570: }
1571: }
1572: PetscLogFlops(2.0*a->nz - nonzerorow);
1573: VecRestoreArrayRead(xx,&x);
1574: VecRestoreArray(yy,&y);
1575: return(0);
1576: }
1578: PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1579: {
1580: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1581: PetscScalar *y,*z;
1582: const PetscScalar *x;
1583: const MatScalar *aa;
1584: PetscErrorCode ierr;
1585: PetscInt m = A->rmap->n,*aj,*ii;
1586: PetscInt n,i,*ridx=NULL;
1587: PetscScalar sum;
1588: PetscBool usecprow=a->compressedrow.use;
1591: VecGetArrayRead(xx,&x);
1592: VecGetArrayPair(yy,zz,&y,&z);
1593: if (usecprow) { /* use compressed row format */
1594: if (zz != yy) {
1595: PetscArraycpy(z,y,m);
1596: }
1597: m = a->compressedrow.nrows;
1598: ii = a->compressedrow.i;
1599: ridx = a->compressedrow.rindex;
1600: for (i=0; i<m; i++) {
1601: n = ii[i+1] - ii[i];
1602: aj = a->j + ii[i];
1603: aa = a->a + ii[i];
1604: sum = y[*ridx];
1605: PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1606: z[*ridx++] = sum;
1607: }
1608: } else { /* do not use compressed row format */
1609: ii = a->i;
1610: for (i=0; i<m; i++) {
1611: n = ii[i+1] - ii[i];
1612: aj = a->j + ii[i];
1613: aa = a->a + ii[i];
1614: sum = y[i];
1615: PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1616: z[i] = sum;
1617: }
1618: }
1619: PetscLogFlops(2.0*a->nz);
1620: VecRestoreArrayRead(xx,&x);
1621: VecRestoreArrayPair(yy,zz,&y,&z);
1622: return(0);
1623: }
1625: #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
1626: PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1627: {
1628: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1629: PetscScalar *y,*z;
1630: const PetscScalar *x;
1631: const MatScalar *aa;
1632: PetscErrorCode ierr;
1633: const PetscInt *aj,*ii,*ridx=NULL;
1634: PetscInt m = A->rmap->n,n,i;
1635: PetscScalar sum;
1636: PetscBool usecprow=a->compressedrow.use;
1639: VecGetArrayRead(xx,&x);
1640: VecGetArrayPair(yy,zz,&y,&z);
1641: if (usecprow) { /* use compressed row format */
1642: if (zz != yy) {
1643: PetscArraycpy(z,y,m);
1644: }
1645: m = a->compressedrow.nrows;
1646: ii = a->compressedrow.i;
1647: ridx = a->compressedrow.rindex;
1648: for (i=0; i<m; i++) {
1649: n = ii[i+1] - ii[i];
1650: aj = a->j + ii[i];
1651: aa = a->a + ii[i];
1652: sum = y[*ridx];
1653: PetscSparseDensePlusDot(sum,x,aa,aj,n);
1654: z[*ridx++] = sum;
1655: }
1656: } else { /* do not use compressed row format */
1657: ii = a->i;
1658: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1659: aj = a->j;
1660: aa = a->a;
1661: fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1662: #else
1663: for (i=0; i<m; i++) {
1664: n = ii[i+1] - ii[i];
1665: aj = a->j + ii[i];
1666: aa = a->a + ii[i];
1667: sum = y[i];
1668: PetscSparseDensePlusDot(sum,x,aa,aj,n);
1669: z[i] = sum;
1670: }
1671: #endif
1672: }
1673: PetscLogFlops(2.0*a->nz);
1674: VecRestoreArrayRead(xx,&x);
1675: VecRestoreArrayPair(yy,zz,&y,&z);
1676: return(0);
1677: }
1679: /*
1680: Adds diagonal pointers to sparse matrix structure.
1681: */
1682: PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
1683: {
1684: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1686: PetscInt i,j,m = A->rmap->n;
1689: if (!a->diag) {
1690: PetscMalloc1(m,&a->diag);
1691: PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));
1692: }
1693: for (i=0; i<A->rmap->n; i++) {
1694: a->diag[i] = a->i[i+1];
1695: for (j=a->i[i]; j<a->i[i+1]; j++) {
1696: if (a->j[j] == i) {
1697: a->diag[i] = j;
1698: break;
1699: }
1700: }
1701: }
1702: return(0);
1703: }
1705: PetscErrorCode MatShift_SeqAIJ(Mat A,PetscScalar v)
1706: {
1707: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1708: const PetscInt *diag = (const PetscInt*)a->diag;
1709: const PetscInt *ii = (const PetscInt*) a->i;
1710: PetscInt i,*mdiag = NULL;
1711: PetscErrorCode ierr;
1712: PetscInt cnt = 0; /* how many diagonals are missing */
1715: if (!A->preallocated || !a->nz) {
1716: MatSeqAIJSetPreallocation(A,1,NULL);
1717: MatShift_Basic(A,v);
1718: return(0);
1719: }
1721: if (a->diagonaldense) {
1722: cnt = 0;
1723: } else {
1724: PetscCalloc1(A->rmap->n,&mdiag);
1725: for (i=0; i<A->rmap->n; i++) {
1726: if (diag[i] >= ii[i+1]) {
1727: cnt++;
1728: mdiag[i] = 1;
1729: }
1730: }
1731: }
1732: if (!cnt) {
1733: MatShift_Basic(A,v);
1734: } else {
1735: PetscScalar *olda = a->a; /* preserve pointers to current matrix nonzeros structure and values */
1736: PetscInt *oldj = a->j, *oldi = a->i;
1737: PetscBool singlemalloc = a->singlemalloc,free_a = a->free_a,free_ij = a->free_ij;
1739: a->a = NULL;
1740: a->j = NULL;
1741: a->i = NULL;
1742: /* increase the values in imax for each row where a diagonal is being inserted then reallocate the matrix data structures */
1743: for (i=0; i<A->rmap->n; i++) {
1744: a->imax[i] += mdiag[i];
1745: a->imax[i] = PetscMin(a->imax[i],A->cmap->n);
1746: }
1747: MatSeqAIJSetPreallocation_SeqAIJ(A,0,a->imax);
1749: /* copy old values into new matrix data structure */
1750: for (i=0; i<A->rmap->n; i++) {
1751: MatSetValues(A,1,&i,a->imax[i] - mdiag[i],&oldj[oldi[i]],&olda[oldi[i]],ADD_VALUES);
1752: if (i < A->cmap->n) {
1753: MatSetValue(A,i,i,v,ADD_VALUES);
1754: }
1755: }
1756: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1757: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1758: if (singlemalloc) {
1759: PetscFree3(olda,oldj,oldi);
1760: } else {
1761: if (free_a) {PetscFree(olda);}
1762: if (free_ij) {PetscFree(oldj);}
1763: if (free_ij) {PetscFree(oldi);}
1764: }
1765: }
1766: PetscFree(mdiag);
1767: a->diagonaldense = PETSC_TRUE;
1768: return(0);
1769: }
1771: /*
1772: Checks for missing diagonals
1773: */
1774: PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool *missing,PetscInt *d)
1775: {
1776: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1777: PetscInt *diag,*ii = a->i,i;
1781: *missing = PETSC_FALSE;
1782: if (A->rmap->n > 0 && !ii) {
1783: *missing = PETSC_TRUE;
1784: if (d) *d = 0;
1785: PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");
1786: } else {
1787: PetscInt n;
1788: n = PetscMin(A->rmap->n, A->cmap->n);
1789: diag = a->diag;
1790: for (i=0; i<n; i++) {
1791: if (diag[i] >= ii[i+1]) {
1792: *missing = PETSC_TRUE;
1793: if (d) *d = i;
1794: PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);
1795: break;
1796: }
1797: }
1798: }
1799: return(0);
1800: }
1802: #include <petscblaslapack.h>
1803: #include <petsc/private/kernels/blockinvert.h>
1805: /*
1806: Note that values is allocated externally by the PC and then passed into this routine
1807: */
1808: PetscErrorCode MatInvertVariableBlockDiagonal_SeqAIJ(Mat A,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *diag)
1809: {
1810: PetscErrorCode ierr;
1811: PetscInt n = A->rmap->n, i, ncnt = 0, *indx,j,bsizemax = 0,*v_pivots;
1812: PetscBool allowzeropivot,zeropivotdetected=PETSC_FALSE;
1813: const PetscReal shift = 0.0;
1814: PetscInt ipvt[5];
1815: PetscScalar work[25],*v_work;
1818: allowzeropivot = PetscNot(A->erroriffailure);
1819: for (i=0; i<nblocks; i++) ncnt += bsizes[i];
1820: if (ncnt != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Total blocksizes %D doesn't match number matrix rows %D",ncnt,n);
1821: for (i=0; i<nblocks; i++) {
1822: bsizemax = PetscMax(bsizemax,bsizes[i]);
1823: }
1824: PetscMalloc1(bsizemax,&indx);
1825: if (bsizemax > 7) {
1826: PetscMalloc2(bsizemax,&v_work,bsizemax,&v_pivots);
1827: }
1828: ncnt = 0;
1829: for (i=0; i<nblocks; i++) {
1830: for (j=0; j<bsizes[i]; j++) indx[j] = ncnt+j;
1831: MatGetValues(A,bsizes[i],indx,bsizes[i],indx,diag);
1832: switch (bsizes[i]) {
1833: case 1:
1834: *diag = 1.0/(*diag);
1835: break;
1836: case 2:
1837: PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
1838: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1839: PetscKernel_A_gets_transpose_A_2(diag);
1840: break;
1841: case 3:
1842: PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);
1843: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1844: PetscKernel_A_gets_transpose_A_3(diag);
1845: break;
1846: case 4:
1847: PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);
1848: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1849: PetscKernel_A_gets_transpose_A_4(diag);
1850: break;
1851: case 5:
1852: PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);
1853: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1854: PetscKernel_A_gets_transpose_A_5(diag);
1855: break;
1856: case 6:
1857: PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);
1858: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1859: PetscKernel_A_gets_transpose_A_6(diag);
1860: break;
1861: case 7:
1862: PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);
1863: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1864: PetscKernel_A_gets_transpose_A_7(diag);
1865: break;
1866: default:
1867: PetscKernel_A_gets_inverse_A(bsizes[i],diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);
1868: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1869: PetscKernel_A_gets_transpose_A_N(diag,bsizes[i]);
1870: }
1871: ncnt += bsizes[i];
1872: diag += bsizes[i]*bsizes[i];
1873: }
1874: if (bsizemax > 7) {
1875: PetscFree2(v_work,v_pivots);
1876: }
1877: PetscFree(indx);
1878: return(0);
1879: }
1881: /*
1882: Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1883: */
1884: PetscErrorCode MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
1885: {
1886: Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data;
1888: PetscInt i,*diag,m = A->rmap->n;
1889: MatScalar *v = a->a;
1890: PetscScalar *idiag,*mdiag;
1893: if (a->idiagvalid) return(0);
1894: MatMarkDiagonal_SeqAIJ(A);
1895: diag = a->diag;
1896: if (!a->idiag) {
1897: PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);
1898: PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));
1899: v = a->a;
1900: }
1901: mdiag = a->mdiag;
1902: idiag = a->idiag;
1904: if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
1905: for (i=0; i<m; i++) {
1906: mdiag[i] = v[diag[i]];
1907: if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1908: if (PetscRealPart(fshift)) {
1909: PetscInfo1(A,"Zero diagonal on row %D\n",i);
1910: A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1911: A->factorerror_zeropivot_value = 0.0;
1912: A->factorerror_zeropivot_row = i;
1913: } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
1914: }
1915: idiag[i] = 1.0/v[diag[i]];
1916: }
1917: PetscLogFlops(m);
1918: } else {
1919: for (i=0; i<m; i++) {
1920: mdiag[i] = v[diag[i]];
1921: idiag[i] = omega/(fshift + v[diag[i]]);
1922: }
1923: PetscLogFlops(2.0*m);
1924: }
1925: a->idiagvalid = PETSC_TRUE;
1926: return(0);
1927: }
1929: #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
1930: PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
1931: {
1932: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1933: PetscScalar *x,d,sum,*t,scale;
1934: const MatScalar *v,*idiag=NULL,*mdiag;
1935: const PetscScalar *b, *bs,*xb, *ts;
1936: PetscErrorCode ierr;
1937: PetscInt n,m = A->rmap->n,i;
1938: const PetscInt *idx,*diag;
1941: its = its*lits;
1943: if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
1944: if (!a->idiagvalid) {MatInvertDiagonal_SeqAIJ(A,omega,fshift);}
1945: a->fshift = fshift;
1946: a->omega = omega;
1948: diag = a->diag;
1949: t = a->ssor_work;
1950: idiag = a->idiag;
1951: mdiag = a->mdiag;
1953: VecGetArray(xx,&x);
1954: VecGetArrayRead(bb,&b);
1955: /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1956: if (flag == SOR_APPLY_UPPER) {
1957: /* apply (U + D/omega) to the vector */
1958: bs = b;
1959: for (i=0; i<m; i++) {
1960: d = fshift + mdiag[i];
1961: n = a->i[i+1] - diag[i] - 1;
1962: idx = a->j + diag[i] + 1;
1963: v = a->a + diag[i] + 1;
1964: sum = b[i]*d/omega;
1965: PetscSparseDensePlusDot(sum,bs,v,idx,n);
1966: x[i] = sum;
1967: }
1968: VecRestoreArray(xx,&x);
1969: VecRestoreArrayRead(bb,&b);
1970: PetscLogFlops(a->nz);
1971: return(0);
1972: }
1974: if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
1975: else if (flag & SOR_EISENSTAT) {
1976: /* Let A = L + U + D; where L is lower triangular,
1977: U is upper triangular, E = D/omega; This routine applies
1979: (L + E)^{-1} A (U + E)^{-1}
1981: to a vector efficiently using Eisenstat's trick.
1982: */
1983: scale = (2.0/omega) - 1.0;
1985: /* x = (E + U)^{-1} b */
1986: for (i=m-1; i>=0; i--) {
1987: n = a->i[i+1] - diag[i] - 1;
1988: idx = a->j + diag[i] + 1;
1989: v = a->a + diag[i] + 1;
1990: sum = b[i];
1991: PetscSparseDenseMinusDot(sum,x,v,idx,n);
1992: x[i] = sum*idiag[i];
1993: }
1995: /* t = b - (2*E - D)x */
1996: v = a->a;
1997: for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i];
1999: /* t = (E + L)^{-1}t */
2000: ts = t;
2001: diag = a->diag;
2002: for (i=0; i<m; i++) {
2003: n = diag[i] - a->i[i];
2004: idx = a->j + a->i[i];
2005: v = a->a + a->i[i];
2006: sum = t[i];
2007: PetscSparseDenseMinusDot(sum,ts,v,idx,n);
2008: t[i] = sum*idiag[i];
2009: /* x = x + t */
2010: x[i] += t[i];
2011: }
2013: PetscLogFlops(6.0*m-1 + 2.0*a->nz);
2014: VecRestoreArray(xx,&x);
2015: VecRestoreArrayRead(bb,&b);
2016: return(0);
2017: }
2018: if (flag & SOR_ZERO_INITIAL_GUESS) {
2019: if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
2020: for (i=0; i<m; i++) {
2021: n = diag[i] - a->i[i];
2022: idx = a->j + a->i[i];
2023: v = a->a + a->i[i];
2024: sum = b[i];
2025: PetscSparseDenseMinusDot(sum,x,v,idx,n);
2026: t[i] = sum;
2027: x[i] = sum*idiag[i];
2028: }
2029: xb = t;
2030: PetscLogFlops(a->nz);
2031: } else xb = b;
2032: if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
2033: for (i=m-1; i>=0; i--) {
2034: n = a->i[i+1] - diag[i] - 1;
2035: idx = a->j + diag[i] + 1;
2036: v = a->a + diag[i] + 1;
2037: sum = xb[i];
2038: PetscSparseDenseMinusDot(sum,x,v,idx,n);
2039: if (xb == b) {
2040: x[i] = sum*idiag[i];
2041: } else {
2042: x[i] = (1-omega)*x[i] + sum*idiag[i]; /* omega in idiag */
2043: }
2044: }
2045: PetscLogFlops(a->nz); /* assumes 1/2 in upper */
2046: }
2047: its--;
2048: }
2049: while (its--) {
2050: if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
2051: for (i=0; i<m; i++) {
2052: /* lower */
2053: n = diag[i] - a->i[i];
2054: idx = a->j + a->i[i];
2055: v = a->a + a->i[i];
2056: sum = b[i];
2057: PetscSparseDenseMinusDot(sum,x,v,idx,n);
2058: t[i] = sum; /* save application of the lower-triangular part */
2059: /* upper */
2060: n = a->i[i+1] - diag[i] - 1;
2061: idx = a->j + diag[i] + 1;
2062: v = a->a + diag[i] + 1;
2063: PetscSparseDenseMinusDot(sum,x,v,idx,n);
2064: x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
2065: }
2066: xb = t;
2067: PetscLogFlops(2.0*a->nz);
2068: } else xb = b;
2069: if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
2070: for (i=m-1; i>=0; i--) {
2071: sum = xb[i];
2072: if (xb == b) {
2073: /* whole matrix (no checkpointing available) */
2074: n = a->i[i+1] - a->i[i];
2075: idx = a->j + a->i[i];
2076: v = a->a + a->i[i];
2077: PetscSparseDenseMinusDot(sum,x,v,idx,n);
2078: x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
2079: } else { /* lower-triangular part has been saved, so only apply upper-triangular */
2080: n = a->i[i+1] - diag[i] - 1;
2081: idx = a->j + diag[i] + 1;
2082: v = a->a + diag[i] + 1;
2083: PetscSparseDenseMinusDot(sum,x,v,idx,n);
2084: x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
2085: }
2086: }
2087: if (xb == b) {
2088: PetscLogFlops(2.0*a->nz);
2089: } else {
2090: PetscLogFlops(a->nz); /* assumes 1/2 in upper */
2091: }
2092: }
2093: }
2094: VecRestoreArray(xx,&x);
2095: VecRestoreArrayRead(bb,&b);
2096: return(0);
2097: }
2100: PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
2101: {
2102: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2105: info->block_size = 1.0;
2106: info->nz_allocated = a->maxnz;
2107: info->nz_used = a->nz;
2108: info->nz_unneeded = (a->maxnz - a->nz);
2109: info->assemblies = A->num_ass;
2110: info->mallocs = A->info.mallocs;
2111: info->memory = ((PetscObject)A)->mem;
2112: if (A->factortype) {
2113: info->fill_ratio_given = A->info.fill_ratio_given;
2114: info->fill_ratio_needed = A->info.fill_ratio_needed;
2115: info->factor_mallocs = A->info.factor_mallocs;
2116: } else {
2117: info->fill_ratio_given = 0;
2118: info->fill_ratio_needed = 0;
2119: info->factor_mallocs = 0;
2120: }
2121: return(0);
2122: }
2124: PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
2125: {
2126: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2127: PetscInt i,m = A->rmap->n - 1;
2128: PetscErrorCode ierr;
2129: const PetscScalar *xx;
2130: PetscScalar *bb;
2131: PetscInt d = 0;
2134: if (x && b) {
2135: VecGetArrayRead(x,&xx);
2136: VecGetArray(b,&bb);
2137: for (i=0; i<N; i++) {
2138: if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2139: if (rows[i] >= A->cmap->n) continue;
2140: bb[rows[i]] = diag*xx[rows[i]];
2141: }
2142: VecRestoreArrayRead(x,&xx);
2143: VecRestoreArray(b,&bb);
2144: }
2146: if (a->keepnonzeropattern) {
2147: for (i=0; i<N; i++) {
2148: if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2149: PetscArrayzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]);
2150: }
2151: if (diag != 0.0) {
2152: for (i=0; i<N; i++) {
2153: d = rows[i];
2154: if (rows[i] >= A->cmap->n) continue;
2155: 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);
2156: }
2157: for (i=0; i<N; i++) {
2158: if (rows[i] >= A->cmap->n) continue;
2159: a->a[a->diag[rows[i]]] = diag;
2160: }
2161: }
2162: } else {
2163: if (diag != 0.0) {
2164: for (i=0; i<N; i++) {
2165: if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2166: if (a->ilen[rows[i]] > 0) {
2167: if (rows[i] >= A->cmap->n) {
2168: a->ilen[rows[i]] = 0;
2169: } else {
2170: a->ilen[rows[i]] = 1;
2171: a->a[a->i[rows[i]]] = diag;
2172: a->j[a->i[rows[i]]] = rows[i];
2173: }
2174: } else if (rows[i] < A->cmap->n) { /* in case row was completely empty */
2175: MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);
2176: }
2177: }
2178: } else {
2179: for (i=0; i<N; i++) {
2180: if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2181: a->ilen[rows[i]] = 0;
2182: }
2183: }
2184: A->nonzerostate++;
2185: }
2186: #if defined(PETSC_HAVE_DEVICE)
2187: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
2188: #endif
2189: (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);
2190: return(0);
2191: }
2193: PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
2194: {
2195: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2196: PetscInt i,j,m = A->rmap->n - 1,d = 0;
2197: PetscErrorCode ierr;
2198: PetscBool missing,*zeroed,vecs = PETSC_FALSE;
2199: const PetscScalar *xx;
2200: PetscScalar *bb;
2203: if (x && b) {
2204: VecGetArrayRead(x,&xx);
2205: VecGetArray(b,&bb);
2206: vecs = PETSC_TRUE;
2207: }
2208: PetscCalloc1(A->rmap->n,&zeroed);
2209: for (i=0; i<N; i++) {
2210: if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2211: PetscArrayzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]);
2213: zeroed[rows[i]] = PETSC_TRUE;
2214: }
2215: for (i=0; i<A->rmap->n; i++) {
2216: if (!zeroed[i]) {
2217: for (j=a->i[i]; j<a->i[i+1]; j++) {
2218: if (a->j[j] < A->rmap->n && zeroed[a->j[j]]) {
2219: if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
2220: a->a[j] = 0.0;
2221: }
2222: }
2223: } else if (vecs && i < A->cmap->N) bb[i] = diag*xx[i];
2224: }
2225: if (x && b) {
2226: VecRestoreArrayRead(x,&xx);
2227: VecRestoreArray(b,&bb);
2228: }
2229: PetscFree(zeroed);
2230: if (diag != 0.0) {
2231: MatMissingDiagonal_SeqAIJ(A,&missing,&d);
2232: if (missing) {
2233: for (i=0; i<N; i++) {
2234: if (rows[i] >= A->cmap->N) continue;
2235: 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]);
2236: MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);
2237: }
2238: } else {
2239: for (i=0; i<N; i++) {
2240: a->a[a->diag[rows[i]]] = diag;
2241: }
2242: }
2243: }
2244: #if defined(PETSC_HAVE_DEVICE)
2245: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
2246: #endif
2247: (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);
2248: return(0);
2249: }
2251: PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
2252: {
2253: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2254: PetscInt *itmp;
2257: if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
2259: *nz = a->i[row+1] - a->i[row];
2260: if (v) *v = a->a + a->i[row];
2261: if (idx) {
2262: itmp = a->j + a->i[row];
2263: if (*nz) *idx = itmp;
2264: else *idx = NULL;
2265: }
2266: return(0);
2267: }
2269: /* remove this function? */
2270: PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
2271: {
2273: return(0);
2274: }
2276: PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
2277: {
2278: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2279: MatScalar *v = a->a;
2280: PetscReal sum = 0.0;
2282: PetscInt i,j;
2285: if (type == NORM_FROBENIUS) {
2286: #if defined(PETSC_USE_REAL___FP16)
2287: PetscBLASInt one = 1,nz = a->nz;
2288: *nrm = BLASnrm2_(&nz,v,&one);
2289: #else
2290: for (i=0; i<a->nz; i++) {
2291: sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
2292: }
2293: *nrm = PetscSqrtReal(sum);
2294: #endif
2295: PetscLogFlops(2.0*a->nz);
2296: } else if (type == NORM_1) {
2297: PetscReal *tmp;
2298: PetscInt *jj = a->j;
2299: PetscCalloc1(A->cmap->n+1,&tmp);
2300: *nrm = 0.0;
2301: for (j=0; j<a->nz; j++) {
2302: tmp[*jj++] += PetscAbsScalar(*v); v++;
2303: }
2304: for (j=0; j<A->cmap->n; j++) {
2305: if (tmp[j] > *nrm) *nrm = tmp[j];
2306: }
2307: PetscFree(tmp);
2308: PetscLogFlops(PetscMax(a->nz-1,0));
2309: } else if (type == NORM_INFINITY) {
2310: *nrm = 0.0;
2311: for (j=0; j<A->rmap->n; j++) {
2312: v = a->a + a->i[j];
2313: sum = 0.0;
2314: for (i=0; i<a->i[j+1]-a->i[j]; i++) {
2315: sum += PetscAbsScalar(*v); v++;
2316: }
2317: if (sum > *nrm) *nrm = sum;
2318: }
2319: PetscLogFlops(PetscMax(a->nz-1,0));
2320: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
2321: return(0);
2322: }
2324: /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
2325: PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
2326: {
2328: PetscInt i,j,anzj;
2329: Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b;
2330: PetscInt an=A->cmap->N,am=A->rmap->N;
2331: PetscInt *ati,*atj,*atfill,*ai=a->i,*aj=a->j;
2334: /* Allocate space for symbolic transpose info and work array */
2335: PetscCalloc1(an+1,&ati);
2336: PetscMalloc1(ai[am],&atj);
2337: PetscMalloc1(an,&atfill);
2339: /* Walk through aj and count ## of non-zeros in each row of A^T. */
2340: /* Note: offset by 1 for fast conversion into csr format. */
2341: for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1;
2342: /* Form ati for csr format of A^T. */
2343: for (i=0;i<an;i++) ati[i+1] += ati[i];
2345: /* Copy ati into atfill so we have locations of the next free space in atj */
2346: PetscArraycpy(atfill,ati,an);
2348: /* Walk through A row-wise and mark nonzero entries of A^T. */
2349: for (i=0;i<am;i++) {
2350: anzj = ai[i+1] - ai[i];
2351: for (j=0;j<anzj;j++) {
2352: atj[atfill[*aj]] = i;
2353: atfill[*aj++] += 1;
2354: }
2355: }
2357: /* Clean up temporary space and complete requests. */
2358: PetscFree(atfill);
2359: MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);
2360: MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));
2361: MatSetType(*B,((PetscObject)A)->type_name);
2363: b = (Mat_SeqAIJ*)((*B)->data);
2364: b->free_a = PETSC_FALSE;
2365: b->free_ij = PETSC_TRUE;
2366: b->nonew = 0;
2367: return(0);
2368: }
2370: PetscErrorCode MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool *f)
2371: {
2372: Mat_SeqAIJ *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
2373: PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr;
2374: MatScalar *va,*vb;
2376: PetscInt ma,na,mb,nb, i;
2379: MatGetSize(A,&ma,&na);
2380: MatGetSize(B,&mb,&nb);
2381: if (ma!=nb || na!=mb) {
2382: *f = PETSC_FALSE;
2383: return(0);
2384: }
2385: aii = aij->i; bii = bij->i;
2386: adx = aij->j; bdx = bij->j;
2387: va = aij->a; vb = bij->a;
2388: PetscMalloc1(ma,&aptr);
2389: PetscMalloc1(mb,&bptr);
2390: for (i=0; i<ma; i++) aptr[i] = aii[i];
2391: for (i=0; i<mb; i++) bptr[i] = bii[i];
2393: *f = PETSC_TRUE;
2394: for (i=0; i<ma; i++) {
2395: while (aptr[i]<aii[i+1]) {
2396: PetscInt idc,idr;
2397: PetscScalar vc,vr;
2398: /* column/row index/value */
2399: idc = adx[aptr[i]];
2400: idr = bdx[bptr[idc]];
2401: vc = va[aptr[i]];
2402: vr = vb[bptr[idc]];
2403: if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
2404: *f = PETSC_FALSE;
2405: goto done;
2406: } else {
2407: aptr[i]++;
2408: if (B || i!=idc) bptr[idc]++;
2409: }
2410: }
2411: }
2412: done:
2413: PetscFree(aptr);
2414: PetscFree(bptr);
2415: return(0);
2416: }
2418: PetscErrorCode MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool *f)
2419: {
2420: Mat_SeqAIJ *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
2421: PetscInt *adx,*bdx,*aii,*bii,*aptr,*bptr;
2422: MatScalar *va,*vb;
2424: PetscInt ma,na,mb,nb, i;
2427: MatGetSize(A,&ma,&na);
2428: MatGetSize(B,&mb,&nb);
2429: if (ma!=nb || na!=mb) {
2430: *f = PETSC_FALSE;
2431: return(0);
2432: }
2433: aii = aij->i; bii = bij->i;
2434: adx = aij->j; bdx = bij->j;
2435: va = aij->a; vb = bij->a;
2436: PetscMalloc1(ma,&aptr);
2437: PetscMalloc1(mb,&bptr);
2438: for (i=0; i<ma; i++) aptr[i] = aii[i];
2439: for (i=0; i<mb; i++) bptr[i] = bii[i];
2441: *f = PETSC_TRUE;
2442: for (i=0; i<ma; i++) {
2443: while (aptr[i]<aii[i+1]) {
2444: PetscInt idc,idr;
2445: PetscScalar vc,vr;
2446: /* column/row index/value */
2447: idc = adx[aptr[i]];
2448: idr = bdx[bptr[idc]];
2449: vc = va[aptr[i]];
2450: vr = vb[bptr[idc]];
2451: if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
2452: *f = PETSC_FALSE;
2453: goto done;
2454: } else {
2455: aptr[i]++;
2456: if (B || i!=idc) bptr[idc]++;
2457: }
2458: }
2459: }
2460: done:
2461: PetscFree(aptr);
2462: PetscFree(bptr);
2463: return(0);
2464: }
2466: PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool *f)
2467: {
2471: MatIsTranspose_SeqAIJ(A,A,tol,f);
2472: return(0);
2473: }
2475: PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool *f)
2476: {
2480: MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);
2481: return(0);
2482: }
2484: PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
2485: {
2486: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2487: const PetscScalar *l,*r;
2488: PetscScalar x;
2489: MatScalar *v;
2490: PetscErrorCode ierr;
2491: PetscInt i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz;
2492: const PetscInt *jj;
2495: if (ll) {
2496: /* The local size is used so that VecMPI can be passed to this routine
2497: by MatDiagonalScale_MPIAIJ */
2498: VecGetLocalSize(ll,&m);
2499: if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
2500: VecGetArrayRead(ll,&l);
2501: v = a->a;
2502: for (i=0; i<m; i++) {
2503: x = l[i];
2504: M = a->i[i+1] - a->i[i];
2505: for (j=0; j<M; j++) (*v++) *= x;
2506: }
2507: VecRestoreArrayRead(ll,&l);
2508: PetscLogFlops(nz);
2509: }
2510: if (rr) {
2511: VecGetLocalSize(rr,&n);
2512: if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
2513: VecGetArrayRead(rr,&r);
2514: v = a->a; jj = a->j;
2515: for (i=0; i<nz; i++) (*v++) *= r[*jj++];
2516: VecRestoreArrayRead(rr,&r);
2517: PetscLogFlops(nz);
2518: }
2519: MatSeqAIJInvalidateDiagonal(A);
2520: #if defined(PETSC_HAVE_DEVICE)
2521: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
2522: #endif
2523: return(0);
2524: }
2526: PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
2527: {
2528: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*c;
2530: PetscInt *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
2531: PetscInt row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
2532: const PetscInt *irow,*icol;
2533: PetscInt nrows,ncols;
2534: PetscInt *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
2535: MatScalar *a_new,*mat_a;
2536: Mat C;
2537: PetscBool stride;
2541: ISGetIndices(isrow,&irow);
2542: ISGetLocalSize(isrow,&nrows);
2543: ISGetLocalSize(iscol,&ncols);
2545: PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);
2546: if (stride) {
2547: ISStrideGetInfo(iscol,&first,&step);
2548: } else {
2549: first = 0;
2550: step = 0;
2551: }
2552: if (stride && step == 1) {
2553: /* special case of contiguous rows */
2554: PetscMalloc2(nrows,&lens,nrows,&starts);
2555: /* loop over new rows determining lens and starting points */
2556: for (i=0; i<nrows; i++) {
2557: kstart = ai[irow[i]];
2558: kend = kstart + ailen[irow[i]];
2559: starts[i] = kstart;
2560: for (k=kstart; k<kend; k++) {
2561: if (aj[k] >= first) {
2562: starts[i] = k;
2563: break;
2564: }
2565: }
2566: sum = 0;
2567: while (k < kend) {
2568: if (aj[k++] >= first+ncols) break;
2569: sum++;
2570: }
2571: lens[i] = sum;
2572: }
2573: /* create submatrix */
2574: if (scall == MAT_REUSE_MATRIX) {
2575: PetscInt n_cols,n_rows;
2576: MatGetSize(*B,&n_rows,&n_cols);
2577: if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2578: MatZeroEntries(*B);
2579: C = *B;
2580: } else {
2581: PetscInt rbs,cbs;
2582: MatCreate(PetscObjectComm((PetscObject)A),&C);
2583: MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);
2584: ISGetBlockSize(isrow,&rbs);
2585: ISGetBlockSize(iscol,&cbs);
2586: MatSetBlockSizes(C,rbs,cbs);
2587: MatSetType(C,((PetscObject)A)->type_name);
2588: MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);
2589: }
2590: c = (Mat_SeqAIJ*)C->data;
2592: /* loop over rows inserting into submatrix */
2593: a_new = c->a;
2594: j_new = c->j;
2595: i_new = c->i;
2597: for (i=0; i<nrows; i++) {
2598: ii = starts[i];
2599: lensi = lens[i];
2600: for (k=0; k<lensi; k++) {
2601: *j_new++ = aj[ii+k] - first;
2602: }
2603: PetscArraycpy(a_new,a->a + starts[i],lensi);
2604: a_new += lensi;
2605: i_new[i+1] = i_new[i] + lensi;
2606: c->ilen[i] = lensi;
2607: }
2608: PetscFree2(lens,starts);
2609: } else {
2610: ISGetIndices(iscol,&icol);
2611: PetscCalloc1(oldcols,&smap);
2612: PetscMalloc1(1+nrows,&lens);
2613: for (i=0; i<ncols; i++) {
2614: if (PetscUnlikelyDebug(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);
2615: smap[icol[i]] = i+1;
2616: }
2618: /* determine lens of each row */
2619: for (i=0; i<nrows; i++) {
2620: kstart = ai[irow[i]];
2621: kend = kstart + a->ilen[irow[i]];
2622: lens[i] = 0;
2623: for (k=kstart; k<kend; k++) {
2624: if (smap[aj[k]]) {
2625: lens[i]++;
2626: }
2627: }
2628: }
2629: /* Create and fill new matrix */
2630: if (scall == MAT_REUSE_MATRIX) {
2631: PetscBool equal;
2633: c = (Mat_SeqAIJ*)((*B)->data);
2634: if ((*B)->rmap->n != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2635: PetscArraycmp(c->ilen,lens,(*B)->rmap->n,&equal);
2636: if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2637: PetscArrayzero(c->ilen,(*B)->rmap->n);
2638: C = *B;
2639: } else {
2640: PetscInt rbs,cbs;
2641: MatCreate(PetscObjectComm((PetscObject)A),&C);
2642: MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);
2643: ISGetBlockSize(isrow,&rbs);
2644: ISGetBlockSize(iscol,&cbs);
2645: MatSetBlockSizes(C,rbs,cbs);
2646: MatSetType(C,((PetscObject)A)->type_name);
2647: MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);
2648: }
2649: c = (Mat_SeqAIJ*)(C->data);
2650: for (i=0; i<nrows; i++) {
2651: row = irow[i];
2652: kstart = ai[row];
2653: kend = kstart + a->ilen[row];
2654: mat_i = c->i[i];
2655: mat_j = c->j + mat_i;
2656: mat_a = c->a + mat_i;
2657: mat_ilen = c->ilen + i;
2658: for (k=kstart; k<kend; k++) {
2659: if ((tcol=smap[a->j[k]])) {
2660: *mat_j++ = tcol - 1;
2661: *mat_a++ = a->a[k];
2662: (*mat_ilen)++;
2664: }
2665: }
2666: }
2667: /* Free work space */
2668: ISRestoreIndices(iscol,&icol);
2669: PetscFree(smap);
2670: PetscFree(lens);
2671: /* sort */
2672: for (i = 0; i < nrows; i++) {
2673: PetscInt ilen;
2675: mat_i = c->i[i];
2676: mat_j = c->j + mat_i;
2677: mat_a = c->a + mat_i;
2678: ilen = c->ilen[i];
2679: PetscSortIntWithScalarArray(ilen,mat_j,mat_a);
2680: }
2681: }
2682: #if defined(PETSC_HAVE_DEVICE)
2683: MatBindToCPU(C,A->boundtocpu);
2684: #endif
2685: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2686: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
2688: ISRestoreIndices(isrow,&irow);
2689: *B = C;
2690: return(0);
2691: }
2693: PetscErrorCode MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
2694: {
2696: Mat B;
2699: if (scall == MAT_INITIAL_MATRIX) {
2700: MatCreate(subComm,&B);
2701: MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);
2702: MatSetBlockSizesFromMats(B,mat,mat);
2703: MatSetType(B,MATSEQAIJ);
2704: MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);
2705: *subMat = B;
2706: } else {
2707: MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);
2708: }
2709: return(0);
2710: }
2712: PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2713: {
2714: Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
2716: Mat outA;
2717: PetscBool row_identity,col_identity;
2720: if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
2722: ISIdentity(row,&row_identity);
2723: ISIdentity(col,&col_identity);
2725: outA = inA;
2726: outA->factortype = MAT_FACTOR_LU;
2727: PetscFree(inA->solvertype);
2728: PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);
2730: PetscObjectReference((PetscObject)row);
2731: ISDestroy(&a->row);
2733: a->row = row;
2735: PetscObjectReference((PetscObject)col);
2736: ISDestroy(&a->col);
2738: a->col = col;
2740: /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
2741: ISDestroy(&a->icol);
2742: ISInvertPermutation(col,PETSC_DECIDE,&a->icol);
2743: PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);
2745: if (!a->solve_work) { /* this matrix may have been factored before */
2746: PetscMalloc1(inA->rmap->n+1,&a->solve_work);
2747: PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));
2748: }
2750: MatMarkDiagonal_SeqAIJ(inA);
2751: if (row_identity && col_identity) {
2752: MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);
2753: } else {
2754: MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);
2755: }
2756: return(0);
2757: }
2759: PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2760: {
2761: Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data;
2762: PetscScalar oalpha = alpha;
2764: PetscBLASInt one = 1,bnz;
2767: PetscBLASIntCast(a->nz,&bnz);
2768: PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2769: PetscLogFlops(a->nz);
2770: MatSeqAIJInvalidateDiagonal(inA);
2771: #if defined(PETSC_HAVE_DEVICE)
2772: if (inA->offloadmask != PETSC_OFFLOAD_UNALLOCATED) inA->offloadmask = PETSC_OFFLOAD_CPU;
2773: #endif
2774: return(0);
2775: }
2777: PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj)
2778: {
2780: PetscInt i;
2783: if (!submatj->id) { /* delete data that are linked only to submats[id=0] */
2784: PetscFree4(submatj->sbuf1,submatj->ptr,submatj->tmp,submatj->ctr);
2786: for (i=0; i<submatj->nrqr; ++i) {
2787: PetscFree(submatj->sbuf2[i]);
2788: }
2789: PetscFree3(submatj->sbuf2,submatj->req_size,submatj->req_source1);
2791: if (submatj->rbuf1) {
2792: PetscFree(submatj->rbuf1[0]);
2793: PetscFree(submatj->rbuf1);
2794: }
2796: for (i=0; i<submatj->nrqs; ++i) {
2797: PetscFree(submatj->rbuf3[i]);
2798: }
2799: PetscFree3(submatj->req_source2,submatj->rbuf2,submatj->rbuf3);
2800: PetscFree(submatj->pa);
2801: }
2803: #if defined(PETSC_USE_CTABLE)
2804: PetscTableDestroy((PetscTable*)&submatj->rmap);
2805: if (submatj->cmap_loc) {PetscFree(submatj->cmap_loc);}
2806: PetscFree(submatj->rmap_loc);
2807: #else
2808: PetscFree(submatj->rmap);
2809: #endif
2811: if (!submatj->allcolumns) {
2812: #if defined(PETSC_USE_CTABLE)
2813: PetscTableDestroy((PetscTable*)&submatj->cmap);
2814: #else
2815: PetscFree(submatj->cmap);
2816: #endif
2817: }
2818: PetscFree(submatj->row2proc);
2820: PetscFree(submatj);
2821: return(0);
2822: }
2824: PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C)
2825: {
2827: Mat_SeqAIJ *c = (Mat_SeqAIJ*)C->data;
2828: Mat_SubSppt *submatj = c->submatis1;
2831: (*submatj->destroy)(C);
2832: MatDestroySubMatrix_Private(submatj);
2833: return(0);
2834: }
2836: PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n,Mat *mat[])
2837: {
2839: PetscInt i;
2840: Mat C;
2841: Mat_SeqAIJ *c;
2842: Mat_SubSppt *submatj;
2845: for (i=0; i<n; i++) {
2846: C = (*mat)[i];
2847: c = (Mat_SeqAIJ*)C->data;
2848: submatj = c->submatis1;
2849: if (submatj) {
2850: if (--((PetscObject)C)->refct <= 0) {
2851: (*submatj->destroy)(C);
2852: MatDestroySubMatrix_Private(submatj);
2853: PetscFree(C->defaultvectype);
2854: PetscLayoutDestroy(&C->rmap);
2855: PetscLayoutDestroy(&C->cmap);
2856: PetscHeaderDestroy(&C);
2857: }
2858: } else {
2859: MatDestroy(&C);
2860: }
2861: }
2863: /* Destroy Dummy submatrices created for reuse */
2864: MatDestroySubMatrices_Dummy(n,mat);
2866: PetscFree(*mat);
2867: return(0);
2868: }
2870: PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2871: {
2873: PetscInt i;
2876: if (scall == MAT_INITIAL_MATRIX) {
2877: PetscCalloc1(n+1,B);
2878: }
2880: for (i=0; i<n; i++) {
2881: MatCreateSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);
2882: }
2883: return(0);
2884: }
2886: PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
2887: {
2888: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2890: PetscInt row,i,j,k,l,m,n,*nidx,isz,val;
2891: const PetscInt *idx;
2892: PetscInt start,end,*ai,*aj;
2893: PetscBT table;
2896: m = A->rmap->n;
2897: ai = a->i;
2898: aj = a->j;
2900: if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
2902: PetscMalloc1(m+1,&nidx);
2903: PetscBTCreate(m,&table);
2905: for (i=0; i<is_max; i++) {
2906: /* Initialize the two local arrays */
2907: isz = 0;
2908: PetscBTMemzero(m,table);
2910: /* Extract the indices, assume there can be duplicate entries */
2911: ISGetIndices(is[i],&idx);
2912: ISGetLocalSize(is[i],&n);
2914: /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2915: for (j=0; j<n; ++j) {
2916: if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
2917: }
2918: ISRestoreIndices(is[i],&idx);
2919: ISDestroy(&is[i]);
2921: k = 0;
2922: for (j=0; j<ov; j++) { /* for each overlap */
2923: n = isz;
2924: for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2925: row = nidx[k];
2926: start = ai[row];
2927: end = ai[row+1];
2928: for (l = start; l<end; l++) {
2929: val = aj[l];
2930: if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2931: }
2932: }
2933: }
2934: ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));
2935: }
2936: PetscBTDestroy(&table);
2937: PetscFree(nidx);
2938: return(0);
2939: }
2941: /* -------------------------------------------------------------- */
2942: PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
2943: {
2944: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2946: PetscInt i,nz = 0,m = A->rmap->n,n = A->cmap->n;
2947: const PetscInt *row,*col;
2948: PetscInt *cnew,j,*lens;
2949: IS icolp,irowp;
2950: PetscInt *cwork = NULL;
2951: PetscScalar *vwork = NULL;
2954: ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);
2955: ISGetIndices(irowp,&row);
2956: ISInvertPermutation(colp,PETSC_DECIDE,&icolp);
2957: ISGetIndices(icolp,&col);
2959: /* determine lengths of permuted rows */
2960: PetscMalloc1(m+1,&lens);
2961: for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2962: MatCreate(PetscObjectComm((PetscObject)A),B);
2963: MatSetSizes(*B,m,n,m,n);
2964: MatSetBlockSizesFromMats(*B,A,A);
2965: MatSetType(*B,((PetscObject)A)->type_name);
2966: MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);
2967: PetscFree(lens);
2969: PetscMalloc1(n,&cnew);
2970: for (i=0; i<m; i++) {
2971: MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
2972: for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2973: MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);
2974: MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
2975: }
2976: PetscFree(cnew);
2978: (*B)->assembled = PETSC_FALSE;
2980: #if defined(PETSC_HAVE_DEVICE)
2981: MatBindToCPU(*B,A->boundtocpu);
2982: #endif
2983: MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);
2984: MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);
2985: ISRestoreIndices(irowp,&row);
2986: ISRestoreIndices(icolp,&col);
2987: ISDestroy(&irowp);
2988: ISDestroy(&icolp);
2989: if (rowp == colp) {
2990: if (A->symmetric) {
2991: MatSetOption(*B,MAT_SYMMETRIC,PETSC_TRUE);
2992: }
2993: if (A->hermitian) {
2994: MatSetOption(*B,MAT_HERMITIAN,PETSC_TRUE);
2995: }
2996: }
2997: return(0);
2998: }
3000: PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
3001: {
3005: /* If the two matrices have the same copy implementation, use fast copy. */
3006: if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
3007: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
3008: Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
3010: 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]);
3011: PetscArraycpy(b->a,a->a,a->i[A->rmap->n]);
3012: PetscObjectStateIncrease((PetscObject)B);
3013: } else {
3014: MatCopy_Basic(A,B,str);
3015: }
3016: return(0);
3017: }
3019: PetscErrorCode MatSetUp_SeqAIJ(Mat A)
3020: {
3024: MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,NULL);
3025: return(0);
3026: }
3028: PETSC_INTERN PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
3029: {
3030: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
3033: *array = a->a;
3034: return(0);
3035: }
3037: PETSC_INTERN PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
3038: {
3040: *array = NULL;
3041: return(0);
3042: }
3044: /*
3045: Computes the number of nonzeros per row needed for preallocation when X and Y
3046: have different nonzero structure.
3047: */
3048: PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
3049: {
3050: PetscInt i,j,k,nzx,nzy;
3053: /* Set the number of nonzeros in the new matrix */
3054: for (i=0; i<m; i++) {
3055: const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
3056: nzx = xi[i+1] - xi[i];
3057: nzy = yi[i+1] - yi[i];
3058: nnz[i] = 0;
3059: for (j=0,k=0; j<nzx; j++) { /* Point in X */
3060: for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
3061: if (k<nzy && yjj[k]==xjj[j]) k++; /* Skip duplicate */
3062: nnz[i]++;
3063: }
3064: for (; k<nzy; k++) nnz[i]++;
3065: }
3066: return(0);
3067: }
3069: PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
3070: {
3071: PetscInt m = Y->rmap->N;
3072: Mat_SeqAIJ *x = (Mat_SeqAIJ*)X->data;
3073: Mat_SeqAIJ *y = (Mat_SeqAIJ*)Y->data;
3077: /* Set the number of nonzeros in the new matrix */
3078: MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);
3079: return(0);
3080: }
3082: PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
3083: {
3085: Mat_SeqAIJ *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
3088: if (str == DIFFERENT_NONZERO_PATTERN) {
3089: if (x->nz == y->nz) {
3090: PetscBool e;
3091: PetscArraycmp(x->i,y->i,Y->rmap->n+1,&e);
3092: if (e) {
3093: PetscArraycmp(x->j,y->j,y->nz,&e);
3094: if (e) {
3095: str = SAME_NONZERO_PATTERN;
3096: }
3097: }
3098: }
3099: }
3100: if (str == SAME_NONZERO_PATTERN) {
3101: PetscScalar alpha = a;
3102: PetscBLASInt one = 1,bnz;
3104: PetscBLASIntCast(x->nz,&bnz);
3105: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
3106: MatSeqAIJInvalidateDiagonal(Y);
3107: PetscObjectStateIncrease((PetscObject)Y);
3108: /* the MatAXPY_Basic* subroutines calls MatAssembly, so the matrix on the GPU will be updated */
3109: #if defined(PETSC_HAVE_DEVICE)
3110: if (Y->offloadmask != PETSC_OFFLOAD_UNALLOCATED) {
3111: Y->offloadmask = PETSC_OFFLOAD_CPU;
3112: }
3113: #endif
3114: } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
3115: MatAXPY_Basic(Y,a,X,str);
3116: } else {
3117: Mat B;
3118: PetscInt *nnz;
3119: PetscMalloc1(Y->rmap->N,&nnz);
3120: MatCreate(PetscObjectComm((PetscObject)Y),&B);
3121: PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
3122: MatSetLayouts(B,Y->rmap,Y->cmap);
3123: MatSetType(B,(MatType) ((PetscObject)Y)->type_name);
3124: MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);
3125: MatSeqAIJSetPreallocation(B,0,nnz);
3126: MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
3127: MatHeaderReplace(Y,&B);
3128: PetscFree(nnz);
3129: }
3130: return(0);
3131: }
3133: PetscErrorCode MatConjugate_SeqAIJ(Mat mat)
3134: {
3135: #if defined(PETSC_USE_COMPLEX)
3136: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3137: PetscInt i,nz;
3138: PetscScalar *a;
3141: nz = aij->nz;
3142: a = aij->a;
3143: for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
3144: #if defined(PETSC_HAVE_DEVICE)
3145: if (mat->offloadmask != PETSC_OFFLOAD_UNALLOCATED) mat->offloadmask = PETSC_OFFLOAD_CPU;
3146: #endif
3147: #else
3149: #endif
3150: return(0);
3151: }
3153: PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3154: {
3155: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
3157: PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3158: PetscReal atmp;
3159: PetscScalar *x;
3160: MatScalar *aa;
3163: if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3164: aa = a->a;
3165: ai = a->i;
3166: aj = a->j;
3168: VecSet(v,0.0);
3169: VecGetArrayWrite(v,&x);
3170: VecGetLocalSize(v,&n);
3171: if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3172: for (i=0; i<m; i++) {
3173: ncols = ai[1] - ai[0]; ai++;
3174: for (j=0; j<ncols; j++) {
3175: atmp = PetscAbsScalar(*aa);
3176: if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
3177: aa++; aj++;
3178: }
3179: }
3180: VecRestoreArrayWrite(v,&x);
3181: return(0);
3182: }
3184: PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3185: {
3186: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
3188: PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3189: PetscScalar *x;
3190: MatScalar *aa;
3193: if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3194: aa = a->a;
3195: ai = a->i;
3196: aj = a->j;
3198: VecSet(v,0.0);
3199: VecGetArrayWrite(v,&x);
3200: VecGetLocalSize(v,&n);
3201: if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3202: for (i=0; i<m; i++) {
3203: ncols = ai[1] - ai[0]; ai++;
3204: if (ncols == A->cmap->n) { /* row is dense */
3205: x[i] = *aa; if (idx) idx[i] = 0;
3206: } else { /* row is sparse so already KNOW maximum is 0.0 or higher */
3207: x[i] = 0.0;
3208: if (idx) {
3209: for (j=0; j<ncols; j++) { /* find first implicit 0.0 in the row */
3210: if (aj[j] > j) {
3211: idx[i] = j;
3212: break;
3213: }
3214: }
3215: /* in case first implicit 0.0 in the row occurs at ncols-th column */
3216: if (j==ncols && j < A->cmap->n) idx[i] = j;
3217: }
3218: }
3219: for (j=0; j<ncols; j++) {
3220: if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3221: aa++; aj++;
3222: }
3223: }
3224: VecRestoreArrayWrite(v,&x);
3225: return(0);
3226: }
3228: PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3229: {
3230: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
3232: PetscInt i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3233: PetscScalar *x,*aa;
3236: aa = a->a;
3237: ai = a->i;
3238: aj = a->j;
3240: VecSet(v,0.0);
3241: VecGetArrayWrite(v,&x);
3242: VecGetLocalSize(v,&n);
3243: if (n != m) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector, %D vs. %D rows", m, n);
3244: for (i=0; i<m; i++) {
3245: ncols = ai[1] - ai[0]; ai++;
3246: if (ncols == A->cmap->n) { /* row is dense */
3247: x[i] = *aa; if (idx) idx[i] = 0;
3248: } else { /* row is sparse so already KNOW minimum is 0.0 or higher */
3249: x[i] = 0.0;
3250: if (idx) { /* find first implicit 0.0 in the row */
3251: for (j=0; j<ncols; j++) {
3252: if (aj[j] > j) {
3253: idx[i] = j;
3254: break;
3255: }
3256: }
3257: /* in case first implicit 0.0 in the row occurs at ncols-th column */
3258: if (j==ncols && j < A->cmap->n) idx[i] = j;
3259: }
3260: }
3261: for (j=0; j<ncols; j++) {
3262: if (PetscAbsScalar(x[i]) > PetscAbsScalar(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3263: aa++; aj++;
3264: }
3265: }
3266: VecRestoreArrayWrite(v,&x);
3267: return(0);
3268: }
3270: PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3271: {
3272: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
3273: PetscErrorCode ierr;
3274: PetscInt i,j,m = A->rmap->n,ncols,n;
3275: const PetscInt *ai,*aj;
3276: PetscScalar *x;
3277: const MatScalar *aa;
3280: if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3281: aa = a->a;
3282: ai = a->i;
3283: aj = a->j;
3285: VecSet(v,0.0);
3286: VecGetArrayWrite(v,&x);
3287: VecGetLocalSize(v,&n);
3288: if (n != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3289: for (i=0; i<m; i++) {
3290: ncols = ai[1] - ai[0]; ai++;
3291: if (ncols == A->cmap->n) { /* row is dense */
3292: x[i] = *aa; if (idx) idx[i] = 0;
3293: } else { /* row is sparse so already KNOW minimum is 0.0 or lower */
3294: x[i] = 0.0;
3295: if (idx) { /* find first implicit 0.0 in the row */
3296: for (j=0; j<ncols; j++) {
3297: if (aj[j] > j) {
3298: idx[i] = j;
3299: break;
3300: }
3301: }
3302: /* in case first implicit 0.0 in the row occurs at ncols-th column */
3303: if (j==ncols && j < A->cmap->n) idx[i] = j;
3304: }
3305: }
3306: for (j=0; j<ncols; j++) {
3307: if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3308: aa++; aj++;
3309: }
3310: }
3311: VecRestoreArrayWrite(v,&x);
3312: return(0);
3313: }
3315: PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
3316: {
3317: Mat_SeqAIJ *a = (Mat_SeqAIJ*) A->data;
3318: PetscErrorCode ierr;
3319: PetscInt i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
3320: MatScalar *diag,work[25],*v_work;
3321: const PetscReal shift = 0.0;
3322: PetscBool allowzeropivot,zeropivotdetected=PETSC_FALSE;
3325: allowzeropivot = PetscNot(A->erroriffailure);
3326: if (a->ibdiagvalid) {
3327: if (values) *values = a->ibdiag;
3328: return(0);
3329: }
3330: MatMarkDiagonal_SeqAIJ(A);
3331: if (!a->ibdiag) {
3332: PetscMalloc1(bs2*mbs,&a->ibdiag);
3333: PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));
3334: }
3335: diag = a->ibdiag;
3336: if (values) *values = a->ibdiag;
3337: /* factor and invert each block */
3338: switch (bs) {
3339: case 1:
3340: for (i=0; i<mbs; i++) {
3341: MatGetValues(A,1,&i,1,&i,diag+i);
3342: if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
3343: if (allowzeropivot) {
3344: A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3345: A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]);
3346: A->factorerror_zeropivot_row = i;
3347: PetscInfo3(A,"Zero pivot, row %D pivot %g tolerance %g\n",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);
3348: } 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);
3349: }
3350: diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3351: }
3352: break;
3353: case 2:
3354: for (i=0; i<mbs; i++) {
3355: ij[0] = 2*i; ij[1] = 2*i + 1;
3356: MatGetValues(A,2,ij,2,ij,diag);
3357: PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
3358: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3359: PetscKernel_A_gets_transpose_A_2(diag);
3360: diag += 4;
3361: }
3362: break;
3363: case 3:
3364: for (i=0; i<mbs; i++) {
3365: ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3366: MatGetValues(A,3,ij,3,ij,diag);
3367: PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);
3368: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3369: PetscKernel_A_gets_transpose_A_3(diag);
3370: diag += 9;
3371: }
3372: break;
3373: case 4:
3374: for (i=0; i<mbs; i++) {
3375: ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3376: MatGetValues(A,4,ij,4,ij,diag);
3377: PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);
3378: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3379: PetscKernel_A_gets_transpose_A_4(diag);
3380: diag += 16;
3381: }
3382: break;
3383: case 5:
3384: for (i=0; i<mbs; i++) {
3385: ij[0] = 5*i; ij[1] = 5*i + 1; ij[2] = 5*i + 2; ij[3] = 5*i + 3; ij[4] = 5*i + 4;
3386: MatGetValues(A,5,ij,5,ij,diag);
3387: PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);
3388: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3389: PetscKernel_A_gets_transpose_A_5(diag);
3390: diag += 25;
3391: }
3392: break;
3393: case 6:
3394: for (i=0; i<mbs; i++) {
3395: 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;
3396: MatGetValues(A,6,ij,6,ij,diag);
3397: PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);
3398: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3399: PetscKernel_A_gets_transpose_A_6(diag);
3400: diag += 36;
3401: }
3402: break;
3403: case 7:
3404: for (i=0; i<mbs; i++) {
3405: 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;
3406: MatGetValues(A,7,ij,7,ij,diag);
3407: PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);
3408: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3409: PetscKernel_A_gets_transpose_A_7(diag);
3410: diag += 49;
3411: }
3412: break;
3413: default:
3414: PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);
3415: for (i=0; i<mbs; i++) {
3416: for (j=0; j<bs; j++) {
3417: IJ[j] = bs*i + j;
3418: }
3419: MatGetValues(A,bs,IJ,bs,IJ,diag);
3420: PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);
3421: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3422: PetscKernel_A_gets_transpose_A_N(diag,bs);
3423: diag += bs2;
3424: }
3425: PetscFree3(v_work,v_pivots,IJ);
3426: }
3427: a->ibdiagvalid = PETSC_TRUE;
3428: return(0);
3429: }
3431: static PetscErrorCode MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
3432: {
3434: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)x->data;
3435: PetscScalar a;
3436: PetscInt m,n,i,j,col;
3439: if (!x->assembled) {
3440: MatGetSize(x,&m,&n);
3441: for (i=0; i<m; i++) {
3442: for (j=0; j<aij->imax[i]; j++) {
3443: PetscRandomGetValue(rctx,&a);
3444: col = (PetscInt)(n*PetscRealPart(a));
3445: MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);
3446: }
3447: }
3448: } else {
3449: for (i=0; i<aij->nz; i++) {PetscRandomGetValue(rctx,aij->a+i);}
3450: }
3451: MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
3452: MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
3453: return(0);
3454: }
3456: /* Like MatSetRandom_SeqAIJ, but do not set values on columns in range of [low, high) */
3457: PetscErrorCode MatSetRandomSkipColumnRange_SeqAIJ_Private(Mat x,PetscInt low,PetscInt high,PetscRandom rctx)
3458: {
3460: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)x->data;
3461: PetscScalar a;
3462: PetscInt m,n,i,j,col,nskip;
3465: nskip = high - low;
3466: MatGetSize(x,&m,&n);
3467: n -= nskip; /* shrink number of columns where nonzeros can be set */
3468: for (i=0; i<m; i++) {
3469: for (j=0; j<aij->imax[i]; j++) {
3470: PetscRandomGetValue(rctx,&a);
3471: col = (PetscInt)(n*PetscRealPart(a));
3472: if (col >= low) col += nskip; /* shift col rightward to skip the hole */
3473: MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);
3474: }
3475: }
3476: MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
3477: MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
3478: return(0);
3479: }
3482: /* -------------------------------------------------------------------*/
3483: static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3484: MatGetRow_SeqAIJ,
3485: MatRestoreRow_SeqAIJ,
3486: MatMult_SeqAIJ,
3487: /* 4*/ MatMultAdd_SeqAIJ,
3488: MatMultTranspose_SeqAIJ,
3489: MatMultTransposeAdd_SeqAIJ,
3490: NULL,
3491: NULL,
3492: NULL,
3493: /* 10*/ NULL,
3494: MatLUFactor_SeqAIJ,
3495: NULL,
3496: MatSOR_SeqAIJ,
3497: MatTranspose_SeqAIJ,
3498: /*1 5*/ MatGetInfo_SeqAIJ,
3499: MatEqual_SeqAIJ,
3500: MatGetDiagonal_SeqAIJ,
3501: MatDiagonalScale_SeqAIJ,
3502: MatNorm_SeqAIJ,
3503: /* 20*/ NULL,
3504: MatAssemblyEnd_SeqAIJ,
3505: MatSetOption_SeqAIJ,
3506: MatZeroEntries_SeqAIJ,
3507: /* 24*/ MatZeroRows_SeqAIJ,
3508: NULL,
3509: NULL,
3510: NULL,
3511: NULL,
3512: /* 29*/ MatSetUp_SeqAIJ,
3513: NULL,
3514: NULL,
3515: NULL,
3516: NULL,
3517: /* 34*/ MatDuplicate_SeqAIJ,
3518: NULL,
3519: NULL,
3520: MatILUFactor_SeqAIJ,
3521: NULL,
3522: /* 39*/ MatAXPY_SeqAIJ,
3523: MatCreateSubMatrices_SeqAIJ,
3524: MatIncreaseOverlap_SeqAIJ,
3525: MatGetValues_SeqAIJ,
3526: MatCopy_SeqAIJ,
3527: /* 44*/ MatGetRowMax_SeqAIJ,
3528: MatScale_SeqAIJ,
3529: MatShift_SeqAIJ,
3530: MatDiagonalSet_SeqAIJ,
3531: MatZeroRowsColumns_SeqAIJ,
3532: /* 49*/ MatSetRandom_SeqAIJ,
3533: MatGetRowIJ_SeqAIJ,
3534: MatRestoreRowIJ_SeqAIJ,
3535: MatGetColumnIJ_SeqAIJ,
3536: MatRestoreColumnIJ_SeqAIJ,
3537: /* 54*/ MatFDColoringCreate_SeqXAIJ,
3538: NULL,
3539: NULL,
3540: MatPermute_SeqAIJ,
3541: NULL,
3542: /* 59*/ NULL,
3543: MatDestroy_SeqAIJ,
3544: MatView_SeqAIJ,
3545: NULL,
3546: NULL,
3547: /* 64*/ NULL,
3548: MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3549: NULL,
3550: NULL,
3551: NULL,
3552: /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3553: MatGetRowMinAbs_SeqAIJ,
3554: NULL,
3555: NULL,
3556: NULL,
3557: /* 74*/ NULL,
3558: MatFDColoringApply_AIJ,
3559: NULL,
3560: NULL,
3561: NULL,
3562: /* 79*/ MatFindZeroDiagonals_SeqAIJ,
3563: NULL,
3564: NULL,
3565: NULL,
3566: MatLoad_SeqAIJ,
3567: /* 84*/ MatIsSymmetric_SeqAIJ,
3568: MatIsHermitian_SeqAIJ,
3569: NULL,
3570: NULL,
3571: NULL,
3572: /* 89*/ NULL,
3573: NULL,
3574: MatMatMultNumeric_SeqAIJ_SeqAIJ,
3575: NULL,
3576: NULL,
3577: /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy,
3578: NULL,
3579: NULL,
3580: MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
3581: NULL,
3582: /* 99*/ MatProductSetFromOptions_SeqAIJ,
3583: NULL,
3584: NULL,
3585: MatConjugate_SeqAIJ,
3586: NULL,
3587: /*104*/ MatSetValuesRow_SeqAIJ,
3588: MatRealPart_SeqAIJ,
3589: MatImaginaryPart_SeqAIJ,
3590: NULL,
3591: NULL,
3592: /*109*/ MatMatSolve_SeqAIJ,
3593: NULL,
3594: MatGetRowMin_SeqAIJ,
3595: NULL,
3596: MatMissingDiagonal_SeqAIJ,
3597: /*114*/ NULL,
3598: NULL,
3599: NULL,
3600: NULL,
3601: NULL,
3602: /*119*/ NULL,
3603: NULL,
3604: NULL,
3605: NULL,
3606: MatGetMultiProcBlock_SeqAIJ,
3607: /*124*/ MatFindNonzeroRows_SeqAIJ,
3608: MatGetColumnNorms_SeqAIJ,
3609: MatInvertBlockDiagonal_SeqAIJ,
3610: MatInvertVariableBlockDiagonal_SeqAIJ,
3611: NULL,
3612: /*129*/ NULL,
3613: NULL,
3614: NULL,
3615: MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3616: MatTransposeColoringCreate_SeqAIJ,
3617: /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
3618: MatTransColoringApplyDenToSp_SeqAIJ,
3619: NULL,
3620: NULL,
3621: MatRARtNumeric_SeqAIJ_SeqAIJ,
3622: /*139*/NULL,
3623: NULL,
3624: NULL,
3625: MatFDColoringSetUp_SeqXAIJ,
3626: MatFindOffBlockDiagonalEntries_SeqAIJ,
3627: MatCreateMPIMatConcatenateSeqMat_SeqAIJ,
3628: /*145*/MatDestroySubMatrices_SeqAIJ,
3629: NULL,
3630: NULL
3631: };
3633: PetscErrorCode MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3634: {
3635: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3636: PetscInt i,nz,n;
3639: nz = aij->maxnz;
3640: n = mat->rmap->n;
3641: for (i=0; i<nz; i++) {
3642: aij->j[i] = indices[i];
3643: }
3644: aij->nz = nz;
3645: for (i=0; i<n; i++) {
3646: aij->ilen[i] = aij->imax[i];
3647: }
3648: return(0);
3649: }
3651: /*
3652: * When a sparse matrix has many zero columns, we should compact them out to save the space
3653: * This happens in MatPtAPSymbolic_MPIAIJ_MPIAIJ_scalable()
3654: * */
3655: PetscErrorCode MatSeqAIJCompactOutExtraColumns_SeqAIJ(Mat mat, ISLocalToGlobalMapping *mapping)
3656: {
3657: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3658: PetscTable gid1_lid1;
3659: PetscTablePosition tpos;
3660: PetscInt gid,lid,i,j,ncols,ec;
3661: PetscInt *garray;
3662: PetscErrorCode ierr;
3667: /* use a table */
3668: PetscTableCreate(mat->rmap->n,mat->cmap->N+1,&gid1_lid1);
3669: ec = 0;
3670: for (i=0; i<mat->rmap->n; i++) {
3671: ncols = aij->i[i+1] - aij->i[i];
3672: for (j=0; j<ncols; j++) {
3673: PetscInt data,gid1 = aij->j[aij->i[i] + j] + 1;
3674: PetscTableFind(gid1_lid1,gid1,&data);
3675: if (!data) {
3676: /* one based table */
3677: PetscTableAdd(gid1_lid1,gid1,++ec,INSERT_VALUES);
3678: }
3679: }
3680: }
3681: /* form array of columns we need */
3682: PetscMalloc1(ec+1,&garray);
3683: PetscTableGetHeadPosition(gid1_lid1,&tpos);
3684: while (tpos) {
3685: PetscTableGetNext(gid1_lid1,&tpos,&gid,&lid);
3686: gid--;
3687: lid--;
3688: garray[lid] = gid;
3689: }
3690: PetscSortInt(ec,garray); /* sort, and rebuild */
3691: PetscTableRemoveAll(gid1_lid1);
3692: for (i=0; i<ec; i++) {
3693: PetscTableAdd(gid1_lid1,garray[i]+1,i+1,INSERT_VALUES);
3694: }
3695: /* compact out the extra columns in B */
3696: for (i=0; i<mat->rmap->n; i++) {
3697: ncols = aij->i[i+1] - aij->i[i];
3698: for (j=0; j<ncols; j++) {
3699: PetscInt gid1 = aij->j[aij->i[i] + j] + 1;
3700: PetscTableFind(gid1_lid1,gid1,&lid);
3701: lid--;
3702: aij->j[aij->i[i] + j] = lid;
3703: }
3704: }
3705: PetscLayoutDestroy(&mat->cmap);
3706: PetscLayoutCreateFromSizes(PetscObjectComm((PetscObject)mat),ec,ec,1,&mat->cmap);
3707: PetscTableDestroy(&gid1_lid1);
3708: ISLocalToGlobalMappingCreate(PETSC_COMM_SELF,mat->cmap->bs,mat->cmap->n,garray,PETSC_OWN_POINTER,mapping);
3709: ISLocalToGlobalMappingSetType(*mapping,ISLOCALTOGLOBALMAPPINGHASH);
3710: return(0);
3711: }
3713: /*@
3714: MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3715: in the matrix.
3717: Input Parameters:
3718: + mat - the SeqAIJ matrix
3719: - indices - the column indices
3721: Level: advanced
3723: Notes:
3724: This can be called if you have precomputed the nonzero structure of the
3725: matrix and want to provide it to the matrix object to improve the performance
3726: of the MatSetValues() operation.
3728: You MUST have set the correct numbers of nonzeros per row in the call to
3729: MatCreateSeqAIJ(), and the columns indices MUST be sorted.
3731: MUST be called before any calls to MatSetValues();
3733: The indices should start with zero, not one.
3735: @*/
3736: PetscErrorCode MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3737: {
3743: PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));
3744: return(0);
3745: }
3747: /* ----------------------------------------------------------------------------------------*/
3749: PetscErrorCode MatStoreValues_SeqAIJ(Mat mat)
3750: {
3751: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3753: size_t nz = aij->i[mat->rmap->n];
3756: if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3758: /* allocate space for values if not already there */
3759: if (!aij->saved_values) {
3760: PetscMalloc1(nz+1,&aij->saved_values);
3761: PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));
3762: }
3764: /* copy values over */
3765: PetscArraycpy(aij->saved_values,aij->a,nz);
3766: return(0);
3767: }
3769: /*@
3770: MatStoreValues - Stashes a copy of the matrix values; this allows, for
3771: example, reuse of the linear part of a Jacobian, while recomputing the
3772: nonlinear portion.
3774: Collect on Mat
3776: Input Parameters:
3777: . mat - the matrix (currently only AIJ matrices support this option)
3779: Level: advanced
3781: Common Usage, with SNESSolve():
3782: $ Create Jacobian matrix
3783: $ Set linear terms into matrix
3784: $ Apply boundary conditions to matrix, at this time matrix must have
3785: $ final nonzero structure (i.e. setting the nonlinear terms and applying
3786: $ boundary conditions again will not change the nonzero structure
3787: $ MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3788: $ MatStoreValues(mat);
3789: $ Call SNESSetJacobian() with matrix
3790: $ In your Jacobian routine
3791: $ MatRetrieveValues(mat);
3792: $ Set nonlinear terms in matrix
3794: Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3795: $ // build linear portion of Jacobian
3796: $ MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3797: $ MatStoreValues(mat);
3798: $ loop over nonlinear iterations
3799: $ MatRetrieveValues(mat);
3800: $ // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3801: $ // call MatAssemblyBegin/End() on matrix
3802: $ Solve linear system with Jacobian
3803: $ endloop
3805: Notes:
3806: Matrix must already be assemblied before calling this routine
3807: Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3808: calling this routine.
3810: When this is called multiple times it overwrites the previous set of stored values
3811: and does not allocated additional space.
3813: .seealso: MatRetrieveValues()
3815: @*/
3816: PetscErrorCode MatStoreValues(Mat mat)
3817: {
3822: if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3823: if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3824: PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));
3825: return(0);
3826: }
3828: PetscErrorCode MatRetrieveValues_SeqAIJ(Mat mat)
3829: {
3830: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3832: PetscInt nz = aij->i[mat->rmap->n];
3835: if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3836: if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3837: /* copy values over */
3838: PetscArraycpy(aij->a,aij->saved_values,nz);
3839: return(0);
3840: }
3842: /*@
3843: MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3844: example, reuse of the linear part of a Jacobian, while recomputing the
3845: nonlinear portion.
3847: Collect on Mat
3849: Input Parameters:
3850: . mat - the matrix (currently only AIJ matrices support this option)
3852: Level: advanced
3854: .seealso: MatStoreValues()
3856: @*/
3857: PetscErrorCode MatRetrieveValues(Mat mat)
3858: {
3863: if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3864: if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3865: PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));
3866: return(0);
3867: }
3870: /* --------------------------------------------------------------------------------*/
3871: /*@C
3872: MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
3873: (the default parallel PETSc format). For good matrix assembly performance
3874: the user should preallocate the matrix storage by setting the parameter nz
3875: (or the array nnz). By setting these parameters accurately, performance
3876: during matrix assembly can be increased by more than a factor of 50.
3878: Collective
3880: Input Parameters:
3881: + comm - MPI communicator, set to PETSC_COMM_SELF
3882: . m - number of rows
3883: . n - number of columns
3884: . nz - number of nonzeros per row (same for all rows)
3885: - nnz - array containing the number of nonzeros in the various rows
3886: (possibly different for each row) or NULL
3888: Output Parameter:
3889: . A - the matrix
3891: It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3892: MatXXXXSetPreallocation() paradigm instead of this routine directly.
3893: [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3895: Notes:
3896: If nnz is given then nz is ignored
3898: The AIJ format (also called the Yale sparse matrix format or
3899: compressed row storage), is fully compatible with standard Fortran 77
3900: storage. That is, the stored row and column indices can begin at
3901: either one (as in Fortran) or zero. See the users' manual for details.
3903: Specify the preallocated storage with either nz or nnz (not both).
3904: Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3905: allocation. For large problems you MUST preallocate memory or you
3906: will get TERRIBLE performance, see the users' manual chapter on matrices.
3908: By default, this format uses inodes (identical nodes) when possible, to
3909: improve numerical efficiency of matrix-vector products and solves. We
3910: search for consecutive rows with the same nonzero structure, thereby
3911: reusing matrix information to achieve increased efficiency.
3913: Options Database Keys:
3914: + -mat_no_inode - Do not use inodes
3915: - -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3917: Level: intermediate
3919: .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
3921: @*/
3922: PetscErrorCode MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
3923: {
3927: MatCreate(comm,A);
3928: MatSetSizes(*A,m,n,m,n);
3929: MatSetType(*A,MATSEQAIJ);
3930: MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);
3931: return(0);
3932: }
3934: /*@C
3935: MatSeqAIJSetPreallocation - For good matrix assembly performance
3936: the user should preallocate the matrix storage by setting the parameter nz
3937: (or the array nnz). By setting these parameters accurately, performance
3938: during matrix assembly can be increased by more than a factor of 50.
3940: Collective
3942: Input Parameters:
3943: + B - The matrix
3944: . nz - number of nonzeros per row (same for all rows)
3945: - nnz - array containing the number of nonzeros in the various rows
3946: (possibly different for each row) or NULL
3948: Notes:
3949: If nnz is given then nz is ignored
3951: The AIJ format (also called the Yale sparse matrix format or
3952: compressed row storage), is fully compatible with standard Fortran 77
3953: storage. That is, the stored row and column indices can begin at
3954: either one (as in Fortran) or zero. See the users' manual for details.
3956: Specify the preallocated storage with either nz or nnz (not both).
3957: Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
3958: allocation. For large problems you MUST preallocate memory or you
3959: will get TERRIBLE performance, see the users' manual chapter on matrices.
3961: You can call MatGetInfo() to get information on how effective the preallocation was;
3962: for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3963: You can also run with the option -info and look for messages with the string
3964: malloc in them to see if additional memory allocation was needed.
3966: Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
3967: entries or columns indices
3969: By default, this format uses inodes (identical nodes) when possible, to
3970: improve numerical efficiency of matrix-vector products and solves. We
3971: search for consecutive rows with the same nonzero structure, thereby
3972: reusing matrix information to achieve increased efficiency.
3974: Options Database Keys:
3975: + -mat_no_inode - Do not use inodes
3976: - -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3978: Level: intermediate
3980: .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo(),
3981: MatSeqAIJSetTotalPreallocation()
3983: @*/
3984: PetscErrorCode MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3985: {
3991: PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));
3992: return(0);
3993: }
3995: PetscErrorCode MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3996: {
3997: Mat_SeqAIJ *b;
3998: PetscBool skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
4000: PetscInt i;
4003: if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
4004: if (nz == MAT_SKIP_ALLOCATION) {
4005: skipallocation = PETSC_TRUE;
4006: nz = 0;
4007: }
4008: PetscLayoutSetUp(B->rmap);
4009: PetscLayoutSetUp(B->cmap);
4011: if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
4012: if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
4013: if (PetscUnlikelyDebug(nnz)) {
4014: for (i=0; i<B->rmap->n; i++) {
4015: 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]);
4016: 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);
4017: }
4018: }
4020: B->preallocated = PETSC_TRUE;
4022: b = (Mat_SeqAIJ*)B->data;
4024: if (!skipallocation) {
4025: if (!b->imax) {
4026: PetscMalloc1(B->rmap->n,&b->imax);
4027: PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
4028: }
4029: if (!b->ilen) {
4030: /* b->ilen will count nonzeros in each row so far. */
4031: PetscCalloc1(B->rmap->n,&b->ilen);
4032: PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
4033: } else {
4034: PetscMemzero(b->ilen,B->rmap->n*sizeof(PetscInt));
4035: }
4036: if (!b->ipre) {
4037: PetscMalloc1(B->rmap->n,&b->ipre);
4038: PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
4039: }
4040: if (!nnz) {
4041: if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
4042: else if (nz < 0) nz = 1;
4043: nz = PetscMin(nz,B->cmap->n);
4044: for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
4045: nz = nz*B->rmap->n;
4046: } else {
4047: PetscInt64 nz64 = 0;
4048: for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz64 += nnz[i];}
4049: PetscIntCast(nz64,&nz);
4050: }
4052: /* allocate the matrix space */
4053: /* FIXME: should B's old memory be unlogged? */
4054: MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);
4055: if (B->structure_only) {
4056: PetscMalloc1(nz,&b->j);
4057: PetscMalloc1(B->rmap->n+1,&b->i);
4058: PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*sizeof(PetscInt));
4059: } else {
4060: PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);
4061: PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));
4062: }
4063: b->i[0] = 0;
4064: for (i=1; i<B->rmap->n+1; i++) {
4065: b->i[i] = b->i[i-1] + b->imax[i-1];
4066: }
4067: if (B->structure_only) {
4068: b->singlemalloc = PETSC_FALSE;
4069: b->free_a = PETSC_FALSE;
4070: } else {
4071: b->singlemalloc = PETSC_TRUE;
4072: b->free_a = PETSC_TRUE;
4073: }
4074: b->free_ij = PETSC_TRUE;
4075: } else {
4076: b->free_a = PETSC_FALSE;
4077: b->free_ij = PETSC_FALSE;
4078: }
4080: if (b->ipre && nnz != b->ipre && b->imax) {
4081: /* reserve user-requested sparsity */
4082: PetscArraycpy(b->ipre,b->imax,B->rmap->n);
4083: }
4086: b->nz = 0;
4087: b->maxnz = nz;
4088: B->info.nz_unneeded = (double)b->maxnz;
4089: if (realalloc) {
4090: MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
4091: }
4092: B->was_assembled = PETSC_FALSE;
4093: B->assembled = PETSC_FALSE;
4094: return(0);
4095: }
4098: PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A)
4099: {
4100: Mat_SeqAIJ *a;
4101: PetscInt i;
4107: /* Check local size. If zero, then return */
4108: if (!A->rmap->n) return(0);
4110: a = (Mat_SeqAIJ*)A->data;
4111: /* if no saved info, we error out */
4112: if (!a->ipre) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"No saved preallocation info \n");
4114: 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");
4116: PetscArraycpy(a->imax,a->ipre,A->rmap->n);
4117: PetscArrayzero(a->ilen,A->rmap->n);
4118: a->i[0] = 0;
4119: for (i=1; i<A->rmap->n+1; i++) {
4120: a->i[i] = a->i[i-1] + a->imax[i-1];
4121: }
4122: A->preallocated = PETSC_TRUE;
4123: a->nz = 0;
4124: a->maxnz = a->i[A->rmap->n];
4125: A->info.nz_unneeded = (double)a->maxnz;
4126: A->was_assembled = PETSC_FALSE;
4127: A->assembled = PETSC_FALSE;
4128: return(0);
4129: }
4131: /*@
4132: MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
4134: Input Parameters:
4135: + B - the matrix
4136: . i - the indices into j for the start of each row (starts with zero)
4137: . j - the column indices for each row (starts with zero) these must be sorted for each row
4138: - v - optional values in the matrix
4140: Level: developer
4142: Notes:
4143: The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
4145: This routine may be called multiple times with different nonzero patterns (or the same nonzero pattern). The nonzero
4146: structure will be the union of all the previous nonzero structures.
4148: Developer Notes:
4149: An optimization could be added to the implementation where it checks if the i, and j are identical to the current i and j and
4150: then just copies the v values directly with PetscMemcpy().
4152: This routine could also take a PetscCopyMode argument to allow sharing the values instead of always copying them.
4154: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), MATSEQAIJ, MatResetPreallocation()
4155: @*/
4156: PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
4157: {
4163: PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));
4164: return(0);
4165: }
4167: PetscErrorCode MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
4168: {
4169: PetscInt i;
4170: PetscInt m,n;
4171: PetscInt nz;
4172: PetscInt *nnz;
4176: if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
4178: PetscLayoutSetUp(B->rmap);
4179: PetscLayoutSetUp(B->cmap);
4181: MatGetSize(B, &m, &n);
4182: PetscMalloc1(m+1, &nnz);
4183: for (i = 0; i < m; i++) {
4184: nz = Ii[i+1]- Ii[i];
4185: if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
4186: nnz[i] = nz;
4187: }
4188: MatSeqAIJSetPreallocation(B, 0, nnz);
4189: PetscFree(nnz);
4191: for (i = 0; i < m; i++) {
4192: MatSetValues_SeqAIJ(B, 1, &i, Ii[i+1] - Ii[i], J+Ii[i], v ? v + Ii[i] : NULL, INSERT_VALUES);
4193: }
4195: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
4196: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
4198: MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
4199: return(0);
4200: }
4202: #include <../src/mat/impls/dense/seq/dense.h>
4203: #include <petsc/private/kernels/petscaxpy.h>
4205: /*
4206: Computes (B'*A')' since computing B*A directly is untenable
4208: n p p
4209: [ ] [ ] [ ]
4210: m [ A ] * n [ B ] = m [ C ]
4211: [ ] [ ] [ ]
4213: */
4214: PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
4215: {
4216: PetscErrorCode ierr;
4217: Mat_SeqDense *sub_a = (Mat_SeqDense*)A->data;
4218: Mat_SeqAIJ *sub_b = (Mat_SeqAIJ*)B->data;
4219: Mat_SeqDense *sub_c = (Mat_SeqDense*)C->data;
4220: PetscInt i,j,n,m,q,p;
4221: const PetscInt *ii,*idx;
4222: const PetscScalar *b,*a,*a_q;
4223: PetscScalar *c,*c_q;
4224: PetscInt clda = sub_c->lda;
4225: PetscInt alda = sub_a->lda;
4228: m = A->rmap->n;
4229: n = A->cmap->n;
4230: p = B->cmap->n;
4231: a = sub_a->v;
4232: b = sub_b->a;
4233: c = sub_c->v;
4234: if (clda == m) {
4235: PetscArrayzero(c,m*p);
4236: } else {
4237: for (j=0;j<p;j++)
4238: for (i=0;i<m;i++)
4239: c[j*clda + i] = 0.0;
4240: }
4241: ii = sub_b->i;
4242: idx = sub_b->j;
4243: for (i=0; i<n; i++) {
4244: q = ii[i+1] - ii[i];
4245: while (q-->0) {
4246: c_q = c + clda*(*idx);
4247: a_q = a + alda*i;
4248: PetscKernelAXPY(c_q,*b,a_q,m);
4249: idx++;
4250: b++;
4251: }
4252: }
4253: return(0);
4254: }
4256: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat C)
4257: {
4259: PetscInt m=A->rmap->n,n=B->cmap->n;
4260: PetscBool cisdense;
4263: 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);
4264: MatSetSizes(C,m,n,m,n);
4265: MatSetBlockSizesFromMats(C,A,B);
4266: PetscObjectTypeCompareAny((PetscObject)C,&cisdense,MATSEQDENSE,MATSEQDENSECUDA,"");
4267: if (!cisdense) {
4268: MatSetType(C,MATDENSE);
4269: }
4270: MatSetUp(C);
4272: C->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
4273: return(0);
4274: }
4276: /* ----------------------------------------------------------------*/
4277: /*MC
4278: MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
4279: based on compressed sparse row format.
4281: Options Database Keys:
4282: . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
4284: Level: beginner
4286: Notes:
4287: MatSetValues() may be called for this matrix type with a NULL argument for the numerical values,
4288: in this case the values associated with the rows and columns one passes in are set to zero
4289: in the matrix
4291: MatSetOptions(,MAT_STRUCTURE_ONLY,PETSC_TRUE) may be called for this matrix type. In this no
4292: space is allocated for the nonzero entries and any entries passed with MatSetValues() are ignored
4294: Developer Notes:
4295: It would be nice if all matrix formats supported passing NULL in for the numerical values
4297: .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
4298: M*/
4300: /*MC
4301: MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
4303: This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
4304: and MATMPIAIJ otherwise. As a result, for single process communicators,
4305: MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation() is supported
4306: for communicators controlling multiple processes. It is recommended that you call both of
4307: the above preallocation routines for simplicity.
4309: Options Database Keys:
4310: . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
4312: Developer Notes:
4313: Subclasses include MATAIJCUSPARSE, MATAIJPERM, MATAIJSELL, MATAIJMKL, MATAIJCRL, and also automatically switches over to use inodes when
4314: enough exist.
4316: Level: beginner
4318: .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
4319: M*/
4321: /*MC
4322: MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
4324: This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
4325: and MATMPIAIJCRL otherwise. As a result, for single process communicators,
4326: MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
4327: for communicators controlling multiple processes. It is recommended that you call both of
4328: the above preallocation routines for simplicity.
4330: Options Database Keys:
4331: . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
4333: Level: beginner
4335: .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
4336: M*/
4338: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
4339: #if defined(PETSC_HAVE_ELEMENTAL)
4340: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
4341: #endif
4342: #if defined(PETSC_HAVE_SCALAPACK)
4343: PETSC_INTERN PetscErrorCode MatConvert_AIJ_ScaLAPACK(Mat,MatType,MatReuse,Mat*);
4344: #endif
4345: #if defined(PETSC_HAVE_HYPRE)
4346: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A,MatType,MatReuse,Mat*);
4347: #endif
4348: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*);
4350: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat,MatType,MatReuse,Mat*);
4351: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat,MatType,MatReuse,Mat*);
4352: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_IS_XAIJ(Mat);
4354: /*@C
4355: MatSeqAIJGetArray - gives read/write access to the array where the data for a MATSEQAIJ matrix is stored
4357: Not Collective
4359: Input Parameter:
4360: . mat - a MATSEQAIJ matrix
4362: Output Parameter:
4363: . array - pointer to the data
4365: Level: intermediate
4367: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4368: @*/
4369: PetscErrorCode MatSeqAIJGetArray(Mat A,PetscScalar **array)
4370: {
4374: PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));
4375: return(0);
4376: }
4378: /*@C
4379: MatSeqAIJGetArrayRead - gives read-only access to the array where the data for a MATSEQAIJ matrix is stored
4381: Not Collective
4383: Input Parameter:
4384: . mat - a MATSEQAIJ matrix
4386: Output Parameter:
4387: . array - pointer to the data
4389: Level: intermediate
4391: .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayRead()
4392: @*/
4393: PetscErrorCode MatSeqAIJGetArrayRead(Mat A,const PetscScalar **array)
4394: {
4395: #if defined(PETSC_HAVE_DEVICE)
4396: PetscOffloadMask oval;
4397: #endif
4401: #if defined(PETSC_HAVE_DEVICE)
4402: oval = A->offloadmask;
4403: #endif
4404: MatSeqAIJGetArray(A,(PetscScalar**)array);
4405: #if defined(PETSC_HAVE_DEVICE)
4406: if (oval == PETSC_OFFLOAD_GPU || oval == PETSC_OFFLOAD_BOTH) A->offloadmask = PETSC_OFFLOAD_BOTH;
4407: #endif
4408: return(0);
4409: }
4411: /*@C
4412: MatSeqAIJRestoreArrayRead - restore the read-only access array obtained from MatSeqAIJGetArrayRead
4414: Not Collective
4416: Input Parameter:
4417: . mat - a MATSEQAIJ matrix
4419: Output Parameter:
4420: . array - pointer to the data
4422: Level: intermediate
4424: .seealso: MatSeqAIJGetArray(), MatSeqAIJGetArrayRead()
4425: @*/
4426: PetscErrorCode MatSeqAIJRestoreArrayRead(Mat A,const PetscScalar **array)
4427: {
4428: #if defined(PETSC_HAVE_DEVICE)
4429: PetscOffloadMask oval;
4430: #endif
4434: #if defined(PETSC_HAVE_DEVICE)
4435: oval = A->offloadmask;
4436: #endif
4437: MatSeqAIJRestoreArray(A,(PetscScalar**)array);
4438: #if defined(PETSC_HAVE_DEVICE)
4439: A->offloadmask = oval;
4440: #endif
4441: return(0);
4442: }
4444: /*@C
4445: MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row
4447: Not Collective
4449: Input Parameter:
4450: . mat - a MATSEQAIJ matrix
4452: Output Parameter:
4453: . nz - the maximum number of nonzeros in any row
4455: Level: intermediate
4457: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4458: @*/
4459: PetscErrorCode MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
4460: {
4461: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)A->data;
4464: *nz = aij->rmax;
4465: return(0);
4466: }
4468: /*@C
4469: MatSeqAIJRestoreArray - returns access to the array where the data for a MATSEQAIJ matrix is stored obtained by MatSeqAIJGetArray()
4471: Not Collective
4473: Input Parameters:
4474: + mat - a MATSEQAIJ matrix
4475: - array - pointer to the data
4477: Level: intermediate
4479: .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
4480: @*/
4481: PetscErrorCode MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
4482: {
4486: PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));
4487: return(0);
4488: }
4490: #if defined(PETSC_HAVE_CUDA)
4491: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat);
4492: #endif
4494: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4495: {
4496: Mat_SeqAIJ *b;
4498: PetscMPIInt size;
4501: MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);
4502: if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
4504: PetscNewLog(B,&b);
4506: B->data = (void*)b;
4508: PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
4509: if (B->sortedfull) B->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;
4511: b->row = NULL;
4512: b->col = NULL;
4513: b->icol = NULL;
4514: b->reallocs = 0;
4515: b->ignorezeroentries = PETSC_FALSE;
4516: b->roworiented = PETSC_TRUE;
4517: b->nonew = 0;
4518: b->diag = NULL;
4519: b->solve_work = NULL;
4520: B->spptr = NULL;
4521: b->saved_values = NULL;
4522: b->idiag = NULL;
4523: b->mdiag = NULL;
4524: b->ssor_work = NULL;
4525: b->omega = 1.0;
4526: b->fshift = 0.0;
4527: b->idiagvalid = PETSC_FALSE;
4528: b->ibdiagvalid = PETSC_FALSE;
4529: b->keepnonzeropattern = PETSC_FALSE;
4531: PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4532: PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);
4533: PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);
4535: #if defined(PETSC_HAVE_MATLAB_ENGINE)
4536: PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);
4537: PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);
4538: #endif
4540: PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);
4541: PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);
4542: PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);
4543: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);
4544: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);
4545: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);
4546: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijsell_C",MatConvert_SeqAIJ_SeqAIJSELL);
4547: #if defined(PETSC_HAVE_MKL_SPARSE)
4548: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijmkl_C",MatConvert_SeqAIJ_SeqAIJMKL);
4549: #endif
4550: #if defined(PETSC_HAVE_CUDA)
4551: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcusparse_C",MatConvert_SeqAIJ_SeqAIJCUSPARSE);
4552: PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqaijcusparse_seqaij_C",MatProductSetFromOptions_SeqAIJ);
4553: #endif
4554: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);
4555: #if defined(PETSC_HAVE_ELEMENTAL)
4556: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);
4557: #endif
4558: #if defined(PETSC_HAVE_SCALAPACK)
4559: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_scalapack_C",MatConvert_AIJ_ScaLAPACK);
4560: #endif
4561: #if defined(PETSC_HAVE_HYPRE)
4562: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_hypre_C",MatConvert_AIJ_HYPRE);
4563: PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_transpose_seqaij_seqaij_C",MatProductSetFromOptions_Transpose_AIJ_AIJ);
4564: #endif
4565: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);
4566: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsell_C",MatConvert_SeqAIJ_SeqSELL);
4567: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_is_C",MatConvert_XAIJ_IS);
4568: PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);
4569: PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);
4570: PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);
4571: PetscObjectComposeFunction((PetscObject)B,"MatResetPreallocation_C",MatResetPreallocation_SeqAIJ);
4572: PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);
4573: PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);
4574: PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_is_seqaij_C",MatProductSetFromOptions_IS_XAIJ);
4575: PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqdense_seqaij_C",MatProductSetFromOptions_SeqDense_SeqAIJ);
4576: PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqaij_seqaij_C",MatProductSetFromOptions_SeqAIJ);
4577: MatCreate_SeqAIJ_Inode(B);
4578: PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4579: MatSeqAIJSetTypeFromOptions(B); /* this allows changing the matrix subtype to say MATSEQAIJPERM */
4580: return(0);
4581: }
4583: /*
4584: Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4585: */
4586: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
4587: {
4588: Mat_SeqAIJ *c = (Mat_SeqAIJ*)C->data,*a = (Mat_SeqAIJ*)A->data;
4590: PetscInt m = A->rmap->n,i;
4593: if (!A->assembled && cpvalues!=MAT_DO_NOT_COPY_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot duplicate unassembled matrix");
4595: C->factortype = A->factortype;
4596: c->row = NULL;
4597: c->col = NULL;
4598: c->icol = NULL;
4599: c->reallocs = 0;
4601: C->assembled = PETSC_TRUE;
4603: PetscLayoutReference(A->rmap,&C->rmap);
4604: PetscLayoutReference(A->cmap,&C->cmap);
4606: PetscMalloc1(m,&c->imax);
4607: PetscMemcpy(c->imax,a->imax,m*sizeof(PetscInt));
4608: PetscMalloc1(m,&c->ilen);
4609: PetscMemcpy(c->ilen,a->ilen,m*sizeof(PetscInt));
4610: PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));
4612: /* allocate the matrix space */
4613: if (mallocmatspace) {
4614: PetscMalloc3(a->i[m],&c->a,a->i[m],&c->j,m+1,&c->i);
4615: PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));
4617: c->singlemalloc = PETSC_TRUE;
4619: PetscArraycpy(c->i,a->i,m+1);
4620: if (m > 0) {
4621: PetscArraycpy(c->j,a->j,a->i[m]);
4622: if (cpvalues == MAT_COPY_VALUES) {
4623: PetscArraycpy(c->a,a->a,a->i[m]);
4624: } else {
4625: PetscArrayzero(c->a,a->i[m]);
4626: }
4627: }
4628: }
4630: c->ignorezeroentries = a->ignorezeroentries;
4631: c->roworiented = a->roworiented;
4632: c->nonew = a->nonew;
4633: if (a->diag) {
4634: PetscMalloc1(m+1,&c->diag);
4635: PetscMemcpy(c->diag,a->diag,m*sizeof(PetscInt));
4636: PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));
4637: } else c->diag = NULL;
4639: c->solve_work = NULL;
4640: c->saved_values = NULL;
4641: c->idiag = NULL;
4642: c->ssor_work = NULL;
4643: c->keepnonzeropattern = a->keepnonzeropattern;
4644: c->free_a = PETSC_TRUE;
4645: c->free_ij = PETSC_TRUE;
4647: c->rmax = a->rmax;
4648: c->nz = a->nz;
4649: c->maxnz = a->nz; /* Since we allocate exactly the right amount */
4650: C->preallocated = PETSC_TRUE;
4652: c->compressedrow.use = a->compressedrow.use;
4653: c->compressedrow.nrows = a->compressedrow.nrows;
4654: if (a->compressedrow.use) {
4655: i = a->compressedrow.nrows;
4656: PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);
4657: PetscArraycpy(c->compressedrow.i,a->compressedrow.i,i+1);
4658: PetscArraycpy(c->compressedrow.rindex,a->compressedrow.rindex,i);
4659: } else {
4660: c->compressedrow.use = PETSC_FALSE;
4661: c->compressedrow.i = NULL;
4662: c->compressedrow.rindex = NULL;
4663: }
4664: c->nonzerorowcnt = a->nonzerorowcnt;
4665: C->nonzerostate = A->nonzerostate;
4667: MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);
4668: PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);
4669: return(0);
4670: }
4672: PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4673: {
4677: MatCreate(PetscObjectComm((PetscObject)A),B);
4678: MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);
4679: if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
4680: MatSetBlockSizesFromMats(*B,A,A);
4681: }
4682: MatSetType(*B,((PetscObject)A)->type_name);
4683: MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);
4684: return(0);
4685: }
4687: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4688: {
4689: PetscBool isbinary, ishdf5;
4695: /* force binary viewer to load .info file if it has not yet done so */
4696: PetscViewerSetUp(viewer);
4697: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
4698: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5, &ishdf5);
4699: if (isbinary) {
4700: MatLoad_SeqAIJ_Binary(newMat,viewer);
4701: } else if (ishdf5) {
4702: #if defined(PETSC_HAVE_HDF5)
4703: MatLoad_AIJ_HDF5(newMat,viewer);
4704: #else
4705: SETERRQ(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
4706: #endif
4707: } else {
4708: 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);
4709: }
4710: return(0);
4711: }
4713: PetscErrorCode MatLoad_SeqAIJ_Binary(Mat mat, PetscViewer viewer)
4714: {
4715: Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->data;
4717: PetscInt header[4],*rowlens,M,N,nz,sum,rows,cols,i;
4720: PetscViewerSetUp(viewer);
4722: /* read in matrix header */
4723: PetscViewerBinaryRead(viewer,header,4,NULL,PETSC_INT);
4724: if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Not a matrix object in file");
4725: M = header[1]; N = header[2]; nz = header[3];
4726: if (M < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Matrix row size (%D) in file is negative",M);
4727: if (N < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Matrix column size (%D) in file is negative",N);
4728: if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk, cannot load as SeqAIJ");
4730: /* set block sizes from the viewer's .info file */
4731: MatLoad_Binary_BlockSizes(mat,viewer);
4732: /* set local and global sizes if not set already */
4733: if (mat->rmap->n < 0) mat->rmap->n = M;
4734: if (mat->cmap->n < 0) mat->cmap->n = N;
4735: if (mat->rmap->N < 0) mat->rmap->N = M;
4736: if (mat->cmap->N < 0) mat->cmap->N = N;
4737: PetscLayoutSetUp(mat->rmap);
4738: PetscLayoutSetUp(mat->cmap);
4740: /* check if the matrix sizes are correct */
4741: MatGetSize(mat,&rows,&cols);
4742: 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);
4744: /* read in row lengths */
4745: PetscMalloc1(M,&rowlens);
4746: PetscViewerBinaryRead(viewer,rowlens,M,NULL,PETSC_INT);
4747: /* check if sum(rowlens) is same as nz */
4748: sum = 0; for (i=0; i<M; i++) sum += rowlens[i];
4749: 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);
4750: /* preallocate and check sizes */
4751: MatSeqAIJSetPreallocation_SeqAIJ(mat,0,rowlens);
4752: MatGetSize(mat,&rows,&cols);
4753: 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);
4754: /* store row lengths */
4755: PetscArraycpy(a->ilen,rowlens,M);
4756: PetscFree(rowlens);
4758: /* fill in "i" row pointers */
4759: a->i[0] = 0; for (i=0; i<M; i++) a->i[i+1] = a->i[i] + a->ilen[i];
4760: /* read in "j" column indices */
4761: PetscViewerBinaryRead(viewer,a->j,nz,NULL,PETSC_INT);
4762: /* read in "a" nonzero values */
4763: PetscViewerBinaryRead(viewer,a->a,nz,NULL,PETSC_SCALAR);
4765: MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
4766: MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
4767: return(0);
4768: }
4770: PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
4771: {
4772: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4774: #if defined(PETSC_USE_COMPLEX)
4775: PetscInt k;
4776: #endif
4779: /* If the matrix dimensions are not equal,or no of nonzeros */
4780: if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4781: *flg = PETSC_FALSE;
4782: return(0);
4783: }
4785: /* if the a->i are the same */
4786: PetscArraycmp(a->i,b->i,A->rmap->n+1,flg);
4787: if (!*flg) return(0);
4789: /* if a->j are the same */
4790: PetscArraycmp(a->j,b->j,a->nz,flg);
4791: if (!*flg) return(0);
4793: /* if a->a are the same */
4794: #if defined(PETSC_USE_COMPLEX)
4795: for (k=0; k<a->nz; k++) {
4796: if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4797: *flg = PETSC_FALSE;
4798: return(0);
4799: }
4800: }
4801: #else
4802: PetscArraycmp(a->a,b->a,a->nz,flg);
4803: #endif
4804: return(0);
4805: }
4807: /*@
4808: MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
4809: provided by the user.
4811: Collective
4813: Input Parameters:
4814: + comm - must be an MPI communicator of size 1
4815: . m - number of rows
4816: . n - number of columns
4817: . i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
4818: . j - column indices
4819: - a - matrix values
4821: Output Parameter:
4822: . mat - the matrix
4824: Level: intermediate
4826: Notes:
4827: The i, j, and a arrays are not copied by this routine, the user must free these arrays
4828: once the matrix is destroyed and not before
4830: You cannot set new nonzero locations into this matrix, that will generate an error.
4832: The i and j indices are 0 based
4834: The format which is used for the sparse matrix input, is equivalent to a
4835: row-major ordering.. i.e for the following matrix, the input data expected is
4836: as shown
4838: $ 1 0 0
4839: $ 2 0 3
4840: $ 4 5 6
4841: $
4842: $ i = {0,1,3,6} [size = nrow+1 = 3+1]
4843: $ j = {0,0,2,0,1,2} [size = 6]; values must be sorted for each row
4844: $ v = {1,2,3,4,5,6} [size = 6]
4847: .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
4849: @*/
4850: PetscErrorCode MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat)
4851: {
4853: PetscInt ii;
4854: Mat_SeqAIJ *aij;
4855: PetscInt jj;
4858: if (m > 0 && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4859: MatCreate(comm,mat);
4860: MatSetSizes(*mat,m,n,m,n);
4861: /* MatSetBlockSizes(*mat,,); */
4862: MatSetType(*mat,MATSEQAIJ);
4863: MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,NULL);
4864: aij = (Mat_SeqAIJ*)(*mat)->data;
4865: PetscMalloc1(m,&aij->imax);
4866: PetscMalloc1(m,&aij->ilen);
4868: aij->i = i;
4869: aij->j = j;
4870: aij->a = a;
4871: aij->singlemalloc = PETSC_FALSE;
4872: aij->nonew = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4873: aij->free_a = PETSC_FALSE;
4874: aij->free_ij = PETSC_FALSE;
4876: for (ii=0; ii<m; ii++) {
4877: aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
4878: if (PetscDefined(USE_DEBUG)) {
4879: 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]);
4880: for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4881: 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);
4882: 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);
4883: }
4884: }
4885: }
4886: if (PetscDefined(USE_DEBUG)) {
4887: for (ii=0; ii<aij->i[m]; ii++) {
4888: if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
4889: 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]);
4890: }
4891: }
4893: MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4894: MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4895: return(0);
4896: }
4897: /*@C
4898: MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
4899: provided by the user.
4901: Collective
4903: Input Parameters:
4904: + comm - must be an MPI communicator of size 1
4905: . m - number of rows
4906: . n - number of columns
4907: . i - row indices
4908: . j - column indices
4909: . a - matrix values
4910: . nz - number of nonzeros
4911: - idx - 0 or 1 based
4913: Output Parameter:
4914: . mat - the matrix
4916: Level: intermediate
4918: Notes:
4919: The i and j indices are 0 based
4921: The format which is used for the sparse matrix input, is equivalent to a
4922: row-major ordering.. i.e for the following matrix, the input data expected is
4923: as shown:
4925: 1 0 0
4926: 2 0 3
4927: 4 5 6
4929: i = {0,1,1,2,2,2}
4930: j = {0,0,2,0,1,2}
4931: v = {1,2,3,4,5,6}
4934: .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
4936: @*/
4937: PetscErrorCode MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat,PetscInt nz,PetscBool idx)
4938: {
4940: PetscInt ii, *nnz, one = 1,row,col;
4944: PetscCalloc1(m,&nnz);
4945: for (ii = 0; ii < nz; ii++) {
4946: nnz[i[ii] - !!idx] += 1;
4947: }
4948: MatCreate(comm,mat);
4949: MatSetSizes(*mat,m,n,m,n);
4950: MatSetType(*mat,MATSEQAIJ);
4951: MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);
4952: for (ii = 0; ii < nz; ii++) {
4953: if (idx) {
4954: row = i[ii] - 1;
4955: col = j[ii] - 1;
4956: } else {
4957: row = i[ii];
4958: col = j[ii];
4959: }
4960: MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);
4961: }
4962: MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4963: MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4964: PetscFree(nnz);
4965: return(0);
4966: }
4968: PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4969: {
4970: Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data;
4974: a->idiagvalid = PETSC_FALSE;
4975: a->ibdiagvalid = PETSC_FALSE;
4977: MatSeqAIJInvalidateDiagonal_Inode(A);
4978: return(0);
4979: }
4981: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
4982: {
4984: PetscMPIInt size;
4987: MPI_Comm_size(comm,&size);
4988: if (size == 1) {
4989: if (scall == MAT_INITIAL_MATRIX) {
4990: MatDuplicate(inmat,MAT_COPY_VALUES,outmat);
4991: } else {
4992: MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);
4993: }
4994: } else {
4995: MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);
4996: }
4997: return(0);
4998: }
5000: /*
5001: Permute A into C's *local* index space using rowemb,colemb.
5002: The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
5003: of [0,m), colemb is in [0,n).
5004: If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
5005: */
5006: PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B)
5007: {
5008: /* If making this function public, change the error returned in this function away from _PLIB. */
5010: Mat_SeqAIJ *Baij;
5011: PetscBool seqaij;
5012: PetscInt m,n,*nz,i,j,count;
5013: PetscScalar v;
5014: const PetscInt *rowindices,*colindices;
5017: if (!B) return(0);
5018: /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
5019: PetscObjectBaseTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);
5020: if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type");
5021: if (rowemb) {
5022: ISGetLocalSize(rowemb,&m);
5023: 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);
5024: } else {
5025: if (C->rmap->n != B->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix");
5026: }
5027: if (colemb) {
5028: ISGetLocalSize(colemb,&n);
5029: 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);
5030: } else {
5031: if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix");
5032: }
5034: Baij = (Mat_SeqAIJ*)(B->data);
5035: if (pattern == DIFFERENT_NONZERO_PATTERN) {
5036: PetscMalloc1(B->rmap->n,&nz);
5037: for (i=0; i<B->rmap->n; i++) {
5038: nz[i] = Baij->i[i+1] - Baij->i[i];
5039: }
5040: MatSeqAIJSetPreallocation(C,0,nz);
5041: PetscFree(nz);
5042: }
5043: if (pattern == SUBSET_NONZERO_PATTERN) {
5044: MatZeroEntries(C);
5045: }
5046: count = 0;
5047: rowindices = NULL;
5048: colindices = NULL;
5049: if (rowemb) {
5050: ISGetIndices(rowemb,&rowindices);
5051: }
5052: if (colemb) {
5053: ISGetIndices(colemb,&colindices);
5054: }
5055: for (i=0; i<B->rmap->n; i++) {
5056: PetscInt row;
5057: row = i;
5058: if (rowindices) row = rowindices[i];
5059: for (j=Baij->i[i]; j<Baij->i[i+1]; j++) {
5060: PetscInt col;
5061: col = Baij->j[count];
5062: if (colindices) col = colindices[col];
5063: v = Baij->a[count];
5064: MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);
5065: ++count;
5066: }
5067: }
5068: /* FIXME: set C's nonzerostate correctly. */
5069: /* Assembly for C is necessary. */
5070: C->preallocated = PETSC_TRUE;
5071: C->assembled = PETSC_TRUE;
5072: C->was_assembled = PETSC_FALSE;
5073: return(0);
5074: }
5076: PetscFunctionList MatSeqAIJList = NULL;
5078: /*@C
5079: MatSeqAIJSetType - Converts a MATSEQAIJ matrix to a subtype
5081: Collective on Mat
5083: Input Parameters:
5084: + mat - the matrix object
5085: - matype - matrix type
5087: Options Database Key:
5088: . -mat_seqai_type <method> - for example seqaijcrl
5091: Level: intermediate
5093: .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
5094: @*/
5095: PetscErrorCode MatSeqAIJSetType(Mat mat, MatType matype)
5096: {
5097: PetscErrorCode ierr,(*r)(Mat,MatType,MatReuse,Mat*);
5098: PetscBool sametype;
5102: PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);
5103: if (sametype) return(0);
5105: PetscFunctionListFind(MatSeqAIJList,matype,&r);
5106: if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
5107: (*r)(mat,matype,MAT_INPLACE_MATRIX,&mat);
5108: return(0);
5109: }
5112: /*@C
5113: MatSeqAIJRegister - - Adds a new sub-matrix type for sequential AIJ matrices
5115: Not Collective
5117: Input Parameters:
5118: + name - name of a new user-defined matrix type, for example MATSEQAIJCRL
5119: - function - routine to convert to subtype
5121: Notes:
5122: MatSeqAIJRegister() may be called multiple times to add several user-defined solvers.
5125: Then, your matrix can be chosen with the procedural interface at runtime via the option
5126: $ -mat_seqaij_type my_mat
5128: Level: advanced
5130: .seealso: MatSeqAIJRegisterAll()
5133: Level: advanced
5134: @*/
5135: PetscErrorCode MatSeqAIJRegister(const char sname[],PetscErrorCode (*function)(Mat,MatType,MatReuse,Mat *))
5136: {
5140: MatInitializePackage();
5141: PetscFunctionListAdd(&MatSeqAIJList,sname,function);
5142: return(0);
5143: }
5145: PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE;
5147: /*@C
5148: MatSeqAIJRegisterAll - Registers all of the matrix subtypes of SeqAIJ
5150: Not Collective
5152: Level: advanced
5154: Developers Note: CUSP and CUSPARSE do not yet support the MatConvert_SeqAIJ..() paradigm and thus cannot be registered here
5156: .seealso: MatRegisterAll(), MatSeqAIJRegister()
5157: @*/
5158: PetscErrorCode MatSeqAIJRegisterAll(void)
5159: {
5163: if (MatSeqAIJRegisterAllCalled) return(0);
5164: MatSeqAIJRegisterAllCalled = PETSC_TRUE;
5166: MatSeqAIJRegister(MATSEQAIJCRL, MatConvert_SeqAIJ_SeqAIJCRL);
5167: MatSeqAIJRegister(MATSEQAIJPERM, MatConvert_SeqAIJ_SeqAIJPERM);
5168: MatSeqAIJRegister(MATSEQAIJSELL, MatConvert_SeqAIJ_SeqAIJSELL);
5169: #if defined(PETSC_HAVE_MKL_SPARSE)
5170: MatSeqAIJRegister(MATSEQAIJMKL, MatConvert_SeqAIJ_SeqAIJMKL);
5171: #endif
5172: #if defined(PETSC_HAVE_VIENNACL) && defined(PETSC_HAVE_VIENNACL_NO_CUDA)
5173: MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL);
5174: #endif
5175: return(0);
5176: }
5178: /*
5179: Special version for direct calls from Fortran
5180: */
5181: #include <petsc/private/fortranimpl.h>
5182: #if defined(PETSC_HAVE_FORTRAN_CAPS)
5183: #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
5184: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
5185: #define matsetvaluesseqaij_ matsetvaluesseqaij
5186: #endif
5188: /* Change these macros so can be used in void function */
5189: #undef CHKERRQ
5190: #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
5191: #undef SETERRQ2
5192: #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
5193: #undef SETERRQ3
5194: #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
5196: PETSC_EXTERN void matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
5197: {
5198: Mat A = *AA;
5199: PetscInt m = *mm, n = *nn;
5200: InsertMode is = *isis;
5201: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
5202: PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
5203: PetscInt *imax,*ai,*ailen;
5205: PetscInt *aj,nonew = a->nonew,lastcol = -1;
5206: MatScalar *ap,value,*aa;
5207: PetscBool ignorezeroentries = a->ignorezeroentries;
5208: PetscBool roworiented = a->roworiented;
5211: MatCheckPreallocated(A,1);
5212: imax = a->imax;
5213: ai = a->i;
5214: ailen = a->ilen;
5215: aj = a->j;
5216: aa = a->a;
5218: for (k=0; k<m; k++) { /* loop over added rows */
5219: row = im[k];
5220: if (row < 0) continue;
5221: if (PetscUnlikelyDebug(row >= A->rmap->n)) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
5222: rp = aj + ai[row]; ap = aa + ai[row];
5223: rmax = imax[row]; nrow = ailen[row];
5224: low = 0;
5225: high = nrow;
5226: for (l=0; l<n; l++) { /* loop over added columns */
5227: if (in[l] < 0) continue;
5228: if (PetscUnlikelyDebug(in[l] >= A->cmap->n)) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
5229: col = in[l];
5230: if (roworiented) value = v[l + k*n];
5231: else value = v[k + l*m];
5233: if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
5235: if (col <= lastcol) low = 0;
5236: else high = nrow;
5237: lastcol = col;
5238: while (high-low > 5) {
5239: t = (low+high)/2;
5240: if (rp[t] > col) high = t;
5241: else low = t;
5242: }
5243: for (i=low; i<high; i++) {
5244: if (rp[i] > col) break;
5245: if (rp[i] == col) {
5246: if (is == ADD_VALUES) ap[i] += value;
5247: else ap[i] = value;
5248: goto noinsert;
5249: }
5250: }
5251: if (value == 0.0 && ignorezeroentries) goto noinsert;
5252: if (nonew == 1) goto noinsert;
5253: if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
5254: MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
5255: N = nrow++ - 1; a->nz++; high++;
5256: /* shift up all the later entries in this row */
5257: for (ii=N; ii>=i; ii--) {
5258: rp[ii+1] = rp[ii];
5259: ap[ii+1] = ap[ii];
5260: }
5261: rp[i] = col;
5262: ap[i] = value;
5263: A->nonzerostate++;
5264: noinsert:;
5265: low = i + 1;
5266: }
5267: ailen[row] = nrow;
5268: }
5269: PetscFunctionReturnVoid();
5270: }