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