Actual source code: mpiaij.c
petsc-3.13.2 2020-06-02
1: #include <../src/mat/impls/aij/mpi/mpiaij.h>
2: #include <petsc/private/vecimpl.h>
3: #include <petsc/private/vecscatterimpl.h>
4: #include <petsc/private/isimpl.h>
5: #include <petscblaslapack.h>
6: #include <petscsf.h>
7: #include <petsc/private/hashmapi.h>
9: /*MC
10: MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
12: This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
13: and MATMPIAIJ otherwise. As a result, for single process communicators,
14: MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation() is supported
15: for communicators controlling multiple processes. It is recommended that you call both of
16: the above preallocation routines for simplicity.
18: Options Database Keys:
19: . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions()
21: Developer Notes:
22: Subclasses include MATAIJCUSP, MATAIJCUSPARSE, MATAIJPERM, MATAIJSELL, MATAIJMKL, MATAIJCRL, and also automatically switches over to use inodes when
23: enough exist.
25: Level: beginner
27: .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ, MATMPIAIJ
28: M*/
30: /*MC
31: MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
33: This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator,
34: and MATMPIAIJCRL otherwise. As a result, for single process communicators,
35: MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported
36: for communicators controlling multiple processes. It is recommended that you call both of
37: the above preallocation routines for simplicity.
39: Options Database Keys:
40: . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions()
42: Level: beginner
44: .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
45: M*/
47: static PetscErrorCode MatBindToCPU_MPIAIJ(Mat A,PetscBool flg)
48: {
49: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
53: #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_VIENNACL)
54: A->boundtocpu = flg;
55: #endif
56: if (a->A) {
57: MatBindToCPU(a->A,flg);
58: }
59: if (a->B) {
60: MatBindToCPU(a->B,flg);
61: }
62: return(0);
63: }
66: PetscErrorCode MatSetBlockSizes_MPIAIJ(Mat M, PetscInt rbs, PetscInt cbs)
67: {
69: Mat_MPIAIJ *mat = (Mat_MPIAIJ*)M->data;
72: if (mat->A) {
73: MatSetBlockSizes(mat->A,rbs,cbs);
74: MatSetBlockSizes(mat->B,rbs,1);
75: }
76: return(0);
77: }
79: PetscErrorCode MatFindNonzeroRows_MPIAIJ(Mat M,IS *keptrows)
80: {
81: PetscErrorCode ierr;
82: Mat_MPIAIJ *mat = (Mat_MPIAIJ*)M->data;
83: Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->A->data;
84: Mat_SeqAIJ *b = (Mat_SeqAIJ*)mat->B->data;
85: const PetscInt *ia,*ib;
86: const MatScalar *aa,*bb;
87: PetscInt na,nb,i,j,*rows,cnt=0,n0rows;
88: PetscInt m = M->rmap->n,rstart = M->rmap->rstart;
91: *keptrows = 0;
92: ia = a->i;
93: ib = b->i;
94: for (i=0; i<m; i++) {
95: na = ia[i+1] - ia[i];
96: nb = ib[i+1] - ib[i];
97: if (!na && !nb) {
98: cnt++;
99: goto ok1;
100: }
101: aa = a->a + ia[i];
102: for (j=0; j<na; j++) {
103: if (aa[j] != 0.0) goto ok1;
104: }
105: bb = b->a + ib[i];
106: for (j=0; j <nb; j++) {
107: if (bb[j] != 0.0) goto ok1;
108: }
109: cnt++;
110: ok1:;
111: }
112: MPIU_Allreduce(&cnt,&n0rows,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)M));
113: if (!n0rows) return(0);
114: PetscMalloc1(M->rmap->n-cnt,&rows);
115: cnt = 0;
116: for (i=0; i<m; i++) {
117: na = ia[i+1] - ia[i];
118: nb = ib[i+1] - ib[i];
119: if (!na && !nb) continue;
120: aa = a->a + ia[i];
121: for (j=0; j<na;j++) {
122: if (aa[j] != 0.0) {
123: rows[cnt++] = rstart + i;
124: goto ok2;
125: }
126: }
127: bb = b->a + ib[i];
128: for (j=0; j<nb; j++) {
129: if (bb[j] != 0.0) {
130: rows[cnt++] = rstart + i;
131: goto ok2;
132: }
133: }
134: ok2:;
135: }
136: ISCreateGeneral(PetscObjectComm((PetscObject)M),cnt,rows,PETSC_OWN_POINTER,keptrows);
137: return(0);
138: }
140: PetscErrorCode MatDiagonalSet_MPIAIJ(Mat Y,Vec D,InsertMode is)
141: {
142: PetscErrorCode ierr;
143: Mat_MPIAIJ *aij = (Mat_MPIAIJ*) Y->data;
144: PetscBool cong;
147: MatHasCongruentLayouts(Y,&cong);
148: if (Y->assembled && cong) {
149: MatDiagonalSet(aij->A,D,is);
150: } else {
151: MatDiagonalSet_Default(Y,D,is);
152: }
153: return(0);
154: }
156: PetscErrorCode MatFindZeroDiagonals_MPIAIJ(Mat M,IS *zrows)
157: {
158: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)M->data;
160: PetscInt i,rstart,nrows,*rows;
163: *zrows = NULL;
164: MatFindZeroDiagonals_SeqAIJ_Private(aij->A,&nrows,&rows);
165: MatGetOwnershipRange(M,&rstart,NULL);
166: for (i=0; i<nrows; i++) rows[i] += rstart;
167: ISCreateGeneral(PetscObjectComm((PetscObject)M),nrows,rows,PETSC_OWN_POINTER,zrows);
168: return(0);
169: }
171: PetscErrorCode MatGetColumnNorms_MPIAIJ(Mat A,NormType type,PetscReal *norms)
172: {
174: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)A->data;
175: PetscInt i,n,*garray = aij->garray;
176: Mat_SeqAIJ *a_aij = (Mat_SeqAIJ*) aij->A->data;
177: Mat_SeqAIJ *b_aij = (Mat_SeqAIJ*) aij->B->data;
178: PetscReal *work;
181: MatGetSize(A,NULL,&n);
182: PetscCalloc1(n,&work);
183: if (type == NORM_2) {
184: for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
185: work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]*a_aij->a[i]);
186: }
187: for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
188: work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]*b_aij->a[i]);
189: }
190: } else if (type == NORM_1) {
191: for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
192: work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]);
193: }
194: for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
195: work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]);
196: }
197: } else if (type == NORM_INFINITY) {
198: for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) {
199: work[A->cmap->rstart + a_aij->j[i]] = PetscMax(PetscAbsScalar(a_aij->a[i]), work[A->cmap->rstart + a_aij->j[i]]);
200: }
201: for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) {
202: work[garray[b_aij->j[i]]] = PetscMax(PetscAbsScalar(b_aij->a[i]),work[garray[b_aij->j[i]]]);
203: }
205: } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Unknown NormType");
206: if (type == NORM_INFINITY) {
207: MPIU_Allreduce(work,norms,n,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)A));
208: } else {
209: MPIU_Allreduce(work,norms,n,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));
210: }
211: PetscFree(work);
212: if (type == NORM_2) {
213: for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
214: }
215: return(0);
216: }
218: PetscErrorCode MatFindOffBlockDiagonalEntries_MPIAIJ(Mat A,IS *is)
219: {
220: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
221: IS sis,gis;
222: PetscErrorCode ierr;
223: const PetscInt *isis,*igis;
224: PetscInt n,*iis,nsis,ngis,rstart,i;
227: MatFindOffBlockDiagonalEntries(a->A,&sis);
228: MatFindNonzeroRows(a->B,&gis);
229: ISGetSize(gis,&ngis);
230: ISGetSize(sis,&nsis);
231: ISGetIndices(sis,&isis);
232: ISGetIndices(gis,&igis);
234: PetscMalloc1(ngis+nsis,&iis);
235: PetscArraycpy(iis,igis,ngis);
236: PetscArraycpy(iis+ngis,isis,nsis);
237: n = ngis + nsis;
238: PetscSortRemoveDupsInt(&n,iis);
239: MatGetOwnershipRange(A,&rstart,NULL);
240: for (i=0; i<n; i++) iis[i] += rstart;
241: ISCreateGeneral(PetscObjectComm((PetscObject)A),n,iis,PETSC_OWN_POINTER,is);
243: ISRestoreIndices(sis,&isis);
244: ISRestoreIndices(gis,&igis);
245: ISDestroy(&sis);
246: ISDestroy(&gis);
247: return(0);
248: }
250: /*
251: Distributes a SeqAIJ matrix across a set of processes. Code stolen from
252: MatLoad_MPIAIJ(). Horrible lack of reuse. Should be a routine for each matrix type.
254: Only for square matrices
256: Used by a preconditioner, hence PETSC_EXTERN
257: */
258: PETSC_EXTERN PetscErrorCode MatDistribute_MPIAIJ(MPI_Comm comm,Mat gmat,PetscInt m,MatReuse reuse,Mat *inmat)
259: {
260: PetscMPIInt rank,size;
261: PetscInt *rowners,*dlens,*olens,i,rstart,rend,j,jj,nz = 0,*gmataj,cnt,row,*ld,bses[2];
263: Mat mat;
264: Mat_SeqAIJ *gmata;
265: PetscMPIInt tag;
266: MPI_Status status;
267: PetscBool aij;
268: MatScalar *gmataa,*ao,*ad,*gmataarestore=0;
271: MPI_Comm_rank(comm,&rank);
272: MPI_Comm_size(comm,&size);
273: if (!rank) {
274: PetscObjectTypeCompare((PetscObject)gmat,MATSEQAIJ,&aij);
275: if (!aij) SETERRQ1(PetscObjectComm((PetscObject)gmat),PETSC_ERR_SUP,"Currently no support for input matrix of type %s\n",((PetscObject)gmat)->type_name);
276: }
277: if (reuse == MAT_INITIAL_MATRIX) {
278: MatCreate(comm,&mat);
279: MatSetSizes(mat,m,m,PETSC_DETERMINE,PETSC_DETERMINE);
280: MatGetBlockSizes(gmat,&bses[0],&bses[1]);
281: MPI_Bcast(bses,2,MPIU_INT,0,comm);
282: MatSetBlockSizes(mat,bses[0],bses[1]);
283: MatSetType(mat,MATAIJ);
284: PetscMalloc1(size+1,&rowners);
285: PetscMalloc2(m,&dlens,m,&olens);
286: MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);
288: rowners[0] = 0;
289: for (i=2; i<=size; i++) rowners[i] += rowners[i-1];
290: rstart = rowners[rank];
291: rend = rowners[rank+1];
292: PetscObjectGetNewTag((PetscObject)mat,&tag);
293: if (!rank) {
294: gmata = (Mat_SeqAIJ*) gmat->data;
295: /* send row lengths to all processors */
296: for (i=0; i<m; i++) dlens[i] = gmata->ilen[i];
297: for (i=1; i<size; i++) {
298: MPI_Send(gmata->ilen + rowners[i],rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);
299: }
300: /* determine number diagonal and off-diagonal counts */
301: PetscArrayzero(olens,m);
302: PetscCalloc1(m,&ld);
303: jj = 0;
304: for (i=0; i<m; i++) {
305: for (j=0; j<dlens[i]; j++) {
306: if (gmata->j[jj] < rstart) ld[i]++;
307: if (gmata->j[jj] < rstart || gmata->j[jj] >= rend) olens[i]++;
308: jj++;
309: }
310: }
311: /* send column indices to other processes */
312: for (i=1; i<size; i++) {
313: nz = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
314: MPI_Send(&nz,1,MPIU_INT,i,tag,comm);
315: MPI_Send(gmata->j + gmata->i[rowners[i]],nz,MPIU_INT,i,tag,comm);
316: }
318: /* send numerical values to other processes */
319: for (i=1; i<size; i++) {
320: nz = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
321: MPI_Send(gmata->a + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);
322: }
323: gmataa = gmata->a;
324: gmataj = gmata->j;
326: } else {
327: /* receive row lengths */
328: MPI_Recv(dlens,m,MPIU_INT,0,tag,comm,&status);
329: /* receive column indices */
330: MPI_Recv(&nz,1,MPIU_INT,0,tag,comm,&status);
331: PetscMalloc2(nz,&gmataa,nz,&gmataj);
332: MPI_Recv(gmataj,nz,MPIU_INT,0,tag,comm,&status);
333: /* determine number diagonal and off-diagonal counts */
334: PetscArrayzero(olens,m);
335: PetscCalloc1(m,&ld);
336: jj = 0;
337: for (i=0; i<m; i++) {
338: for (j=0; j<dlens[i]; j++) {
339: if (gmataj[jj] < rstart) ld[i]++;
340: if (gmataj[jj] < rstart || gmataj[jj] >= rend) olens[i]++;
341: jj++;
342: }
343: }
344: /* receive numerical values */
345: PetscArrayzero(gmataa,nz);
346: MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);
347: }
348: /* set preallocation */
349: for (i=0; i<m; i++) {
350: dlens[i] -= olens[i];
351: }
352: MatSeqAIJSetPreallocation(mat,0,dlens);
353: MatMPIAIJSetPreallocation(mat,0,dlens,0,olens);
355: for (i=0; i<m; i++) {
356: dlens[i] += olens[i];
357: }
358: cnt = 0;
359: for (i=0; i<m; i++) {
360: row = rstart + i;
361: MatSetValues(mat,1,&row,dlens[i],gmataj+cnt,gmataa+cnt,INSERT_VALUES);
362: cnt += dlens[i];
363: }
364: if (rank) {
365: PetscFree2(gmataa,gmataj);
366: }
367: PetscFree2(dlens,olens);
368: PetscFree(rowners);
370: ((Mat_MPIAIJ*)(mat->data))->ld = ld;
372: *inmat = mat;
373: } else { /* column indices are already set; only need to move over numerical values from process 0 */
374: Mat_SeqAIJ *Ad = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->A->data;
375: Mat_SeqAIJ *Ao = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->B->data;
376: mat = *inmat;
377: PetscObjectGetNewTag((PetscObject)mat,&tag);
378: if (!rank) {
379: /* send numerical values to other processes */
380: gmata = (Mat_SeqAIJ*) gmat->data;
381: MatGetOwnershipRanges(mat,(const PetscInt**)&rowners);
382: gmataa = gmata->a;
383: for (i=1; i<size; i++) {
384: nz = gmata->i[rowners[i+1]]-gmata->i[rowners[i]];
385: MPI_Send(gmataa + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);
386: }
387: nz = gmata->i[rowners[1]]-gmata->i[rowners[0]];
388: } else {
389: /* receive numerical values from process 0*/
390: nz = Ad->nz + Ao->nz;
391: PetscMalloc1(nz,&gmataa); gmataarestore = gmataa;
392: MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);
393: }
394: /* transfer numerical values into the diagonal A and off diagonal B parts of mat */
395: ld = ((Mat_MPIAIJ*)(mat->data))->ld;
396: ad = Ad->a;
397: ao = Ao->a;
398: if (mat->rmap->n) {
399: i = 0;
400: nz = ld[i]; PetscArraycpy(ao,gmataa,nz); ao += nz; gmataa += nz;
401: nz = Ad->i[i+1] - Ad->i[i]; PetscArraycpy(ad,gmataa,nz); ad += nz; gmataa += nz;
402: }
403: for (i=1; i<mat->rmap->n; i++) {
404: nz = Ao->i[i] - Ao->i[i-1] - ld[i-1] + ld[i]; PetscArraycpy(ao,gmataa,nz); ao += nz; gmataa += nz;
405: nz = Ad->i[i+1] - Ad->i[i]; PetscArraycpy(ad,gmataa,nz); ad += nz; gmataa += nz;
406: }
407: i--;
408: if (mat->rmap->n) {
409: nz = Ao->i[i+1] - Ao->i[i] - ld[i]; PetscArraycpy(ao,gmataa,nz);
410: }
411: if (rank) {
412: PetscFree(gmataarestore);
413: }
414: }
415: MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
416: MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
417: return(0);
418: }
420: /*
421: Local utility routine that creates a mapping from the global column
422: number to the local number in the off-diagonal part of the local
423: storage of the matrix. When PETSC_USE_CTABLE is used this is scalable at
424: a slightly higher hash table cost; without it it is not scalable (each processor
425: has an order N integer array but is fast to acess.
426: */
427: PetscErrorCode MatCreateColmap_MPIAIJ_Private(Mat mat)
428: {
429: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
431: PetscInt n = aij->B->cmap->n,i;
434: if (!aij->garray) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPIAIJ Matrix was assembled but is missing garray");
435: #if defined(PETSC_USE_CTABLE)
436: PetscTableCreate(n,mat->cmap->N+1,&aij->colmap);
437: for (i=0; i<n; i++) {
438: PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1,INSERT_VALUES);
439: }
440: #else
441: PetscCalloc1(mat->cmap->N+1,&aij->colmap);
442: PetscLogObjectMemory((PetscObject)mat,(mat->cmap->N+1)*sizeof(PetscInt));
443: for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
444: #endif
445: return(0);
446: }
448: #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv,orow,ocol) \
449: { \
450: if (col <= lastcol1) low1 = 0; \
451: else high1 = nrow1; \
452: lastcol1 = col;\
453: while (high1-low1 > 5) { \
454: t = (low1+high1)/2; \
455: if (rp1[t] > col) high1 = t; \
456: else low1 = t; \
457: } \
458: for (_i=low1; _i<high1; _i++) { \
459: if (rp1[_i] > col) break; \
460: if (rp1[_i] == col) { \
461: if (addv == ADD_VALUES) { \
462: ap1[_i] += value; \
463: /* Not sure LogFlops will slow dow the code or not */ \
464: (void)PetscLogFlops(1.0); \
465: } \
466: else ap1[_i] = value; \
467: inserted = PETSC_TRUE; \
468: goto a_noinsert; \
469: } \
470: } \
471: if (value == 0.0 && ignorezeroentries && row != col) {low1 = 0; high1 = nrow1;goto a_noinsert;} \
472: if (nonew == 1) {low1 = 0; high1 = nrow1; goto a_noinsert;} \
473: if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at global row/column (%D, %D) into matrix", orow, ocol); \
474: MatSeqXAIJReallocateAIJ(A,am,1,nrow1,row,col,rmax1,aa,ai,aj,rp1,ap1,aimax,nonew,MatScalar); \
475: N = nrow1++ - 1; a->nz++; high1++; \
476: /* shift up all the later entries in this row */ \
477: PetscArraymove(rp1+_i+1,rp1+_i,N-_i+1);\
478: PetscArraymove(ap1+_i+1,ap1+_i,N-_i+1);\
479: rp1[_i] = col; \
480: ap1[_i] = value; \
481: A->nonzerostate++;\
482: a_noinsert: ; \
483: ailen[row] = nrow1; \
484: }
486: #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv,orow,ocol) \
487: { \
488: if (col <= lastcol2) low2 = 0; \
489: else high2 = nrow2; \
490: lastcol2 = col; \
491: while (high2-low2 > 5) { \
492: t = (low2+high2)/2; \
493: if (rp2[t] > col) high2 = t; \
494: else low2 = t; \
495: } \
496: for (_i=low2; _i<high2; _i++) { \
497: if (rp2[_i] > col) break; \
498: if (rp2[_i] == col) { \
499: if (addv == ADD_VALUES) { \
500: ap2[_i] += value; \
501: (void)PetscLogFlops(1.0); \
502: } \
503: else ap2[_i] = value; \
504: inserted = PETSC_TRUE; \
505: goto b_noinsert; \
506: } \
507: } \
508: if (value == 0.0 && ignorezeroentries) {low2 = 0; high2 = nrow2; goto b_noinsert;} \
509: if (nonew == 1) {low2 = 0; high2 = nrow2; goto b_noinsert;} \
510: if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at global row/column (%D, %D) into matrix", orow, ocol); \
511: MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew,MatScalar); \
512: N = nrow2++ - 1; b->nz++; high2++; \
513: /* shift up all the later entries in this row */ \
514: PetscArraymove(rp2+_i+1,rp2+_i,N-_i+1);\
515: PetscArraymove(ap2+_i+1,ap2+_i,N-_i+1);\
516: rp2[_i] = col; \
517: ap2[_i] = value; \
518: B->nonzerostate++; \
519: b_noinsert: ; \
520: bilen[row] = nrow2; \
521: }
523: PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[])
524: {
525: Mat_MPIAIJ *mat = (Mat_MPIAIJ*)A->data;
526: Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data;
528: PetscInt l,*garray = mat->garray,diag;
531: /* code only works for square matrices A */
533: /* find size of row to the left of the diagonal part */
534: MatGetOwnershipRange(A,&diag,0);
535: row = row - diag;
536: for (l=0; l<b->i[row+1]-b->i[row]; l++) {
537: if (garray[b->j[b->i[row]+l]] > diag) break;
538: }
539: PetscArraycpy(b->a+b->i[row],v,l);
541: /* diagonal part */
542: PetscArraycpy(a->a+a->i[row],v+l,(a->i[row+1]-a->i[row]));
544: /* right of diagonal part */
545: PetscArraycpy(b->a+b->i[row]+l,v+l+a->i[row+1]-a->i[row],b->i[row+1]-b->i[row]-l);
546: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
547: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && (l || (a->i[row+1]-a->i[row]) || (b->i[row+1]-b->i[row]-l))) A->offloadmask = PETSC_OFFLOAD_CPU;
548: #endif
549: return(0);
550: }
552: PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
553: {
554: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
555: PetscScalar value = 0.0;
557: PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend;
558: PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
559: PetscBool roworiented = aij->roworiented;
561: /* Some Variables required in the macro */
562: Mat A = aij->A;
563: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
564: PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
565: MatScalar *aa = a->a;
566: PetscBool ignorezeroentries = a->ignorezeroentries;
567: Mat B = aij->B;
568: Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
569: PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
570: MatScalar *ba = b->a;
571: /* This variable below is only for the PETSC_HAVE_VIENNACL or PETSC_HAVE_CUDA cases, but we define it in all cases because we
572: * cannot use "#if defined" inside a macro. */
573: PETSC_UNUSED PetscBool inserted = PETSC_FALSE;
575: PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
576: PetscInt nonew;
577: MatScalar *ap1,*ap2;
580: for (i=0; i<m; i++) {
581: if (im[i] < 0) continue;
582: #if defined(PETSC_USE_DEBUG)
583: if (im[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1);
584: #endif
585: if (im[i] >= rstart && im[i] < rend) {
586: row = im[i] - rstart;
587: lastcol1 = -1;
588: rp1 = aj + ai[row];
589: ap1 = aa + ai[row];
590: rmax1 = aimax[row];
591: nrow1 = ailen[row];
592: low1 = 0;
593: high1 = nrow1;
594: lastcol2 = -1;
595: rp2 = bj + bi[row];
596: ap2 = ba + bi[row];
597: rmax2 = bimax[row];
598: nrow2 = bilen[row];
599: low2 = 0;
600: high2 = nrow2;
602: for (j=0; j<n; j++) {
603: if (v) value = roworiented ? v[i*n+j] : v[i+j*m];
604: if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES) && im[i] != in[j]) continue;
605: if (in[j] >= cstart && in[j] < cend) {
606: col = in[j] - cstart;
607: nonew = a->nonew;
608: MatSetValues_SeqAIJ_A_Private(row,col,value,addv,im[i],in[j]);
609: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
610: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && inserted) A->offloadmask = PETSC_OFFLOAD_CPU;
611: #endif
612: } else if (in[j] < 0) continue;
613: #if defined(PETSC_USE_DEBUG)
614: else if (in[j] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[j],mat->cmap->N-1);
615: #endif
616: else {
617: if (mat->was_assembled) {
618: if (!aij->colmap) {
619: MatCreateColmap_MPIAIJ_Private(mat);
620: }
621: #if defined(PETSC_USE_CTABLE)
622: PetscTableFind(aij->colmap,in[j]+1,&col);
623: col--;
624: #else
625: col = aij->colmap[in[j]] - 1;
626: #endif
627: if (col < 0 && !((Mat_SeqAIJ*)(aij->B->data))->nonew) {
628: MatDisAssemble_MPIAIJ(mat);
629: col = in[j];
630: /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
631: B = aij->B;
632: b = (Mat_SeqAIJ*)B->data;
633: bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; ba = b->a;
634: rp2 = bj + bi[row];
635: ap2 = ba + bi[row];
636: rmax2 = bimax[row];
637: nrow2 = bilen[row];
638: low2 = 0;
639: high2 = nrow2;
640: bm = aij->B->rmap->n;
641: ba = b->a;
642: inserted = PETSC_FALSE;
643: } else if (col < 0) {
644: if (1 == ((Mat_SeqAIJ*)(aij->B->data))->nonew) {
645: PetscInfo3(mat,"Skipping of insertion of new nonzero location in off-diagonal portion of matrix %g(%D,%D)\n",(double)PetscRealPart(value),im[i],in[j]);
646: } else SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at global row/column (%D, %D) into matrix", im[i], in[j]);
647: }
648: } else col = in[j];
649: nonew = b->nonew;
650: MatSetValues_SeqAIJ_B_Private(row,col,value,addv,im[i],in[j]);
651: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
652: if (B->offloadmask != PETSC_OFFLOAD_UNALLOCATED && inserted) B->offloadmask = PETSC_OFFLOAD_CPU;
653: #endif
654: }
655: }
656: } else {
657: if (mat->nooffprocentries) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Setting off process row %D even though MatSetOption(,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE) was set",im[i]);
658: if (!aij->donotstash) {
659: mat->assembled = PETSC_FALSE;
660: if (roworiented) {
661: MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
662: } else {
663: MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
664: }
665: }
666: }
667: }
668: return(0);
669: }
671: /*
672: This function sets the j and ilen arrays (of the diagonal and off-diagonal part) of an MPIAIJ-matrix.
673: The values in mat_i have to be sorted and the values in mat_j have to be sorted for each row (CSR-like).
674: No off-processor parts off the matrix are allowed here and mat->was_assembled has to be PETSC_FALSE.
675: */
676: PetscErrorCode MatSetValues_MPIAIJ_CopyFromCSRFormat_Symbolic(Mat mat,const PetscInt mat_j[],const PetscInt mat_i[])
677: {
678: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
679: Mat A = aij->A; /* diagonal part of the matrix */
680: Mat B = aij->B; /* offdiagonal part of the matrix */
681: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
682: Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
683: PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,col;
684: PetscInt *ailen = a->ilen,*aj = a->j;
685: PetscInt *bilen = b->ilen,*bj = b->j;
686: PetscInt am = aij->A->rmap->n,j;
687: PetscInt diag_so_far = 0,dnz;
688: PetscInt offd_so_far = 0,onz;
691: /* Iterate over all rows of the matrix */
692: for (j=0; j<am; j++) {
693: dnz = onz = 0;
694: /* Iterate over all non-zero columns of the current row */
695: for (col=mat_i[j]; col<mat_i[j+1]; col++) {
696: /* If column is in the diagonal */
697: if (mat_j[col] >= cstart && mat_j[col] < cend) {
698: aj[diag_so_far++] = mat_j[col] - cstart;
699: dnz++;
700: } else { /* off-diagonal entries */
701: bj[offd_so_far++] = mat_j[col];
702: onz++;
703: }
704: }
705: ailen[j] = dnz;
706: bilen[j] = onz;
707: }
708: return(0);
709: }
711: /*
712: This function sets the local j, a and ilen arrays (of the diagonal and off-diagonal part) of an MPIAIJ-matrix.
713: The values in mat_i have to be sorted and the values in mat_j have to be sorted for each row (CSR-like).
714: No off-processor parts off the matrix are allowed here, they are set at a later point by MatSetValues_MPIAIJ.
715: Also, mat->was_assembled has to be false, otherwise the statement aj[rowstart_diag+dnz_row] = mat_j[col] - cstart;
716: would not be true and the more complex MatSetValues_MPIAIJ has to be used.
717: */
718: PetscErrorCode MatSetValues_MPIAIJ_CopyFromCSRFormat(Mat mat,const PetscInt mat_j[],const PetscInt mat_i[],const PetscScalar mat_a[])
719: {
720: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
721: Mat A = aij->A; /* diagonal part of the matrix */
722: Mat B = aij->B; /* offdiagonal part of the matrix */
723: Mat_SeqAIJ *aijd =(Mat_SeqAIJ*)(aij->A)->data,*aijo=(Mat_SeqAIJ*)(aij->B)->data;
724: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
725: Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
726: PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend;
727: PetscInt *ailen = a->ilen,*aj = a->j;
728: PetscInt *bilen = b->ilen,*bj = b->j;
729: PetscInt am = aij->A->rmap->n,j;
730: PetscInt *full_diag_i=aijd->i,*full_offd_i=aijo->i; /* These variables can also include non-local elements, which are set at a later point. */
731: PetscInt col,dnz_row,onz_row,rowstart_diag,rowstart_offd;
732: PetscScalar *aa = a->a,*ba = b->a;
735: /* Iterate over all rows of the matrix */
736: for (j=0; j<am; j++) {
737: dnz_row = onz_row = 0;
738: rowstart_offd = full_offd_i[j];
739: rowstart_diag = full_diag_i[j];
740: /* Iterate over all non-zero columns of the current row */
741: for (col=mat_i[j]; col<mat_i[j+1]; col++) {
742: /* If column is in the diagonal */
743: if (mat_j[col] >= cstart && mat_j[col] < cend) {
744: aj[rowstart_diag+dnz_row] = mat_j[col] - cstart;
745: aa[rowstart_diag+dnz_row] = mat_a[col];
746: dnz_row++;
747: } else { /* off-diagonal entries */
748: bj[rowstart_offd+onz_row] = mat_j[col];
749: ba[rowstart_offd+onz_row] = mat_a[col];
750: onz_row++;
751: }
752: }
753: ailen[j] = dnz_row;
754: bilen[j] = onz_row;
755: }
756: return(0);
757: }
759: PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
760: {
761: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
763: PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend;
764: PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
767: for (i=0; i<m; i++) {
768: if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
769: if (idxm[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",idxm[i],mat->rmap->N-1);
770: if (idxm[i] >= rstart && idxm[i] < rend) {
771: row = idxm[i] - rstart;
772: for (j=0; j<n; j++) {
773: if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
774: if (idxn[j] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",idxn[j],mat->cmap->N-1);
775: if (idxn[j] >= cstart && idxn[j] < cend) {
776: col = idxn[j] - cstart;
777: MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);
778: } else {
779: if (!aij->colmap) {
780: MatCreateColmap_MPIAIJ_Private(mat);
781: }
782: #if defined(PETSC_USE_CTABLE)
783: PetscTableFind(aij->colmap,idxn[j]+1,&col);
784: col--;
785: #else
786: col = aij->colmap[idxn[j]] - 1;
787: #endif
788: if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
789: else {
790: MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);
791: }
792: }
793: }
794: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
795: }
796: return(0);
797: }
799: extern PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat,Vec,Vec);
801: PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
802: {
803: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
805: PetscInt nstash,reallocs;
808: if (aij->donotstash || mat->nooffprocentries) return(0);
810: MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);
811: MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
812: PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
813: return(0);
814: }
816: PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
817: {
818: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
819: Mat_SeqAIJ *a = (Mat_SeqAIJ*)aij->A->data;
821: PetscMPIInt n;
822: PetscInt i,j,rstart,ncols,flg;
823: PetscInt *row,*col;
824: PetscBool other_disassembled;
825: PetscScalar *val;
827: /* do not use 'b = (Mat_SeqAIJ*)aij->B->data' as B can be reset in disassembly */
830: if (!aij->donotstash && !mat->nooffprocentries) {
831: while (1) {
832: MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);
833: if (!flg) break;
835: for (i=0; i<n; ) {
836: /* Now identify the consecutive vals belonging to the same row */
837: for (j=i,rstart=row[j]; j<n; j++) {
838: if (row[j] != rstart) break;
839: }
840: if (j < n) ncols = j-i;
841: else ncols = n-i;
842: /* Now assemble all these values with a single function call */
843: MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,mat->insertmode);
844: i = j;
845: }
846: }
847: MatStashScatterEnd_Private(&mat->stash);
848: }
849: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
850: if (mat->offloadmask == PETSC_OFFLOAD_CPU) aij->A->offloadmask = PETSC_OFFLOAD_CPU;
851: /* We call MatBindToCPU() on aij->A and aij->B here, because if MatBindToCPU_MPIAIJ() is called before assembly, it cannot bind these. */
852: if (mat->boundtocpu) {
853: MatBindToCPU(aij->A,PETSC_TRUE);
854: MatBindToCPU(aij->B,PETSC_TRUE);
855: }
856: #endif
857: MatAssemblyBegin(aij->A,mode);
858: MatAssemblyEnd(aij->A,mode);
860: /* determine if any processor has disassembled, if so we must
861: also disassemble ourself, in order that we may reassemble. */
862: /*
863: if nonzero structure of submatrix B cannot change then we know that
864: no processor disassembled thus we can skip this stuff
865: */
866: if (!((Mat_SeqAIJ*)aij->B->data)->nonew) {
867: MPIU_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));
868: if (mat->was_assembled && !other_disassembled) {
869: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
870: aij->B->offloadmask = PETSC_OFFLOAD_BOTH; /* do not copy on the GPU when assembling inside MatDisAssemble_MPIAIJ */
871: #endif
872: MatDisAssemble_MPIAIJ(mat);
873: }
874: }
875: if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
876: MatSetUpMultiply_MPIAIJ(mat);
877: }
878: MatSetOption(aij->B,MAT_USE_INODES,PETSC_FALSE);
879: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
880: if (mat->offloadmask == PETSC_OFFLOAD_CPU && aij->B->offloadmask != PETSC_OFFLOAD_UNALLOCATED) aij->B->offloadmask = PETSC_OFFLOAD_CPU;
881: #endif
882: MatAssemblyBegin(aij->B,mode);
883: MatAssemblyEnd(aij->B,mode);
885: PetscFree2(aij->rowvalues,aij->rowindices);
887: aij->rowvalues = 0;
889: VecDestroy(&aij->diag);
890: if (a->inode.size) mat->ops->multdiagonalblock = MatMultDiagonalBlock_MPIAIJ;
892: /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */
893: if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
894: PetscObjectState state = aij->A->nonzerostate + aij->B->nonzerostate;
895: MPIU_Allreduce(&state,&mat->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)mat));
896: }
897: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
898: mat->offloadmask = PETSC_OFFLOAD_BOTH;
899: #endif
900: return(0);
901: }
903: PetscErrorCode MatZeroEntries_MPIAIJ(Mat A)
904: {
905: Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data;
909: MatZeroEntries(l->A);
910: MatZeroEntries(l->B);
911: return(0);
912: }
914: PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
915: {
916: Mat_MPIAIJ *mat = (Mat_MPIAIJ *) A->data;
917: PetscObjectState sA, sB;
918: PetscInt *lrows;
919: PetscInt r, len;
920: PetscBool cong, lch, gch;
921: PetscErrorCode ierr;
924: /* get locally owned rows */
925: MatZeroRowsMapLocal_Private(A,N,rows,&len,&lrows);
926: MatHasCongruentLayouts(A,&cong);
927: /* fix right hand side if needed */
928: if (x && b) {
929: const PetscScalar *xx;
930: PetscScalar *bb;
932: if (!cong) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Need matching row/col layout");
933: VecGetArrayRead(x, &xx);
934: VecGetArray(b, &bb);
935: for (r = 0; r < len; ++r) bb[lrows[r]] = diag*xx[lrows[r]];
936: VecRestoreArrayRead(x, &xx);
937: VecRestoreArray(b, &bb);
938: }
940: sA = mat->A->nonzerostate;
941: sB = mat->B->nonzerostate;
943: if (diag != 0.0 && cong) {
944: MatZeroRows(mat->A, len, lrows, diag, NULL, NULL);
945: MatZeroRows(mat->B, len, lrows, 0.0, NULL, NULL);
946: } else if (diag != 0.0) { /* non-square or non congruent layouts -> if keepnonzeropattern is false, we allow for new insertion */
947: Mat_SeqAIJ *aijA = (Mat_SeqAIJ*)mat->A->data;
948: Mat_SeqAIJ *aijB = (Mat_SeqAIJ*)mat->B->data;
949: PetscInt nnwA, nnwB;
950: PetscBool nnzA, nnzB;
952: nnwA = aijA->nonew;
953: nnwB = aijB->nonew;
954: nnzA = aijA->keepnonzeropattern;
955: nnzB = aijB->keepnonzeropattern;
956: if (!nnzA) {
957: PetscInfo(mat->A,"Requested to not keep the pattern and add a nonzero diagonal; may encounter reallocations on diagonal block.\n");
958: aijA->nonew = 0;
959: }
960: if (!nnzB) {
961: PetscInfo(mat->B,"Requested to not keep the pattern and add a nonzero diagonal; may encounter reallocations on off-diagonal block.\n");
962: aijB->nonew = 0;
963: }
964: /* Must zero here before the next loop */
965: MatZeroRows(mat->A, len, lrows, 0.0, NULL, NULL);
966: MatZeroRows(mat->B, len, lrows, 0.0, NULL, NULL);
967: for (r = 0; r < len; ++r) {
968: const PetscInt row = lrows[r] + A->rmap->rstart;
969: if (row >= A->cmap->N) continue;
970: MatSetValues(A, 1, &row, 1, &row, &diag, INSERT_VALUES);
971: }
972: aijA->nonew = nnwA;
973: aijB->nonew = nnwB;
974: } else {
975: MatZeroRows(mat->A, len, lrows, 0.0, NULL, NULL);
976: MatZeroRows(mat->B, len, lrows, 0.0, NULL, NULL);
977: }
978: PetscFree(lrows);
979: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
980: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
982: /* reduce nonzerostate */
983: lch = (PetscBool)(sA != mat->A->nonzerostate || sB != mat->B->nonzerostate);
984: MPIU_Allreduce(&lch,&gch,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)A));
985: if (gch) A->nonzerostate++;
986: return(0);
987: }
989: PetscErrorCode MatZeroRowsColumns_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
990: {
991: Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data;
992: PetscErrorCode ierr;
993: PetscMPIInt n = A->rmap->n;
994: PetscInt i,j,r,m,len = 0;
995: PetscInt *lrows,*owners = A->rmap->range;
996: PetscMPIInt p = 0;
997: PetscSFNode *rrows;
998: PetscSF sf;
999: const PetscScalar *xx;
1000: PetscScalar *bb,*mask;
1001: Vec xmask,lmask;
1002: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)l->B->data;
1003: const PetscInt *aj, *ii,*ridx;
1004: PetscScalar *aa;
1007: /* Create SF where leaves are input rows and roots are owned rows */
1008: PetscMalloc1(n, &lrows);
1009: for (r = 0; r < n; ++r) lrows[r] = -1;
1010: PetscMalloc1(N, &rrows);
1011: for (r = 0; r < N; ++r) {
1012: const PetscInt idx = rows[r];
1013: if (idx < 0 || A->rmap->N <= idx) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range [0,%D)",idx,A->rmap->N);
1014: if (idx < owners[p] || owners[p+1] <= idx) { /* short-circuit the search if the last p owns this row too */
1015: PetscLayoutFindOwner(A->rmap,idx,&p);
1016: }
1017: rrows[r].rank = p;
1018: rrows[r].index = rows[r] - owners[p];
1019: }
1020: PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);
1021: PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);
1022: /* Collect flags for rows to be zeroed */
1023: PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
1024: PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
1025: PetscSFDestroy(&sf);
1026: /* Compress and put in row numbers */
1027: for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r;
1028: /* zero diagonal part of matrix */
1029: MatZeroRowsColumns(l->A,len,lrows,diag,x,b);
1030: /* handle off diagonal part of matrix */
1031: MatCreateVecs(A,&xmask,NULL);
1032: VecDuplicate(l->lvec,&lmask);
1033: VecGetArray(xmask,&bb);
1034: for (i=0; i<len; i++) bb[lrows[i]] = 1;
1035: VecRestoreArray(xmask,&bb);
1036: VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
1037: VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
1038: VecDestroy(&xmask);
1039: if (x && b) { /* this code is buggy when the row and column layout don't match */
1040: PetscBool cong;
1042: MatHasCongruentLayouts(A,&cong);
1043: if (!cong) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Need matching row/col layout");
1044: VecScatterBegin(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);
1045: VecScatterEnd(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);
1046: VecGetArrayRead(l->lvec,&xx);
1047: VecGetArray(b,&bb);
1048: }
1049: VecGetArray(lmask,&mask);
1050: /* remove zeroed rows of off diagonal matrix */
1051: ii = aij->i;
1052: for (i=0; i<len; i++) {
1053: PetscArrayzero(aij->a + ii[lrows[i]],ii[lrows[i]+1] - ii[lrows[i]]);
1054: }
1055: /* loop over all elements of off process part of matrix zeroing removed columns*/
1056: if (aij->compressedrow.use) {
1057: m = aij->compressedrow.nrows;
1058: ii = aij->compressedrow.i;
1059: ridx = aij->compressedrow.rindex;
1060: for (i=0; i<m; i++) {
1061: n = ii[i+1] - ii[i];
1062: aj = aij->j + ii[i];
1063: aa = aij->a + ii[i];
1065: for (j=0; j<n; j++) {
1066: if (PetscAbsScalar(mask[*aj])) {
1067: if (b) bb[*ridx] -= *aa*xx[*aj];
1068: *aa = 0.0;
1069: }
1070: aa++;
1071: aj++;
1072: }
1073: ridx++;
1074: }
1075: } else { /* do not use compressed row format */
1076: m = l->B->rmap->n;
1077: for (i=0; i<m; i++) {
1078: n = ii[i+1] - ii[i];
1079: aj = aij->j + ii[i];
1080: aa = aij->a + ii[i];
1081: for (j=0; j<n; j++) {
1082: if (PetscAbsScalar(mask[*aj])) {
1083: if (b) bb[i] -= *aa*xx[*aj];
1084: *aa = 0.0;
1085: }
1086: aa++;
1087: aj++;
1088: }
1089: }
1090: }
1091: if (x && b) {
1092: VecRestoreArray(b,&bb);
1093: VecRestoreArrayRead(l->lvec,&xx);
1094: }
1095: VecRestoreArray(lmask,&mask);
1096: VecDestroy(&lmask);
1097: PetscFree(lrows);
1099: /* only change matrix nonzero state if pattern was allowed to be changed */
1100: if (!((Mat_SeqAIJ*)(l->A->data))->keepnonzeropattern) {
1101: PetscObjectState state = l->A->nonzerostate + l->B->nonzerostate;
1102: MPIU_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));
1103: }
1104: return(0);
1105: }
1107: PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
1108: {
1109: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1111: PetscInt nt;
1112: VecScatter Mvctx = a->Mvctx;
1115: VecGetLocalSize(xx,&nt);
1116: if (nt != A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%D) and xx (%D)",A->cmap->n,nt);
1118: VecScatterBegin(Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1119: (*a->A->ops->mult)(a->A,xx,yy);
1120: VecScatterEnd(Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1121: (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
1122: return(0);
1123: }
1125: PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx)
1126: {
1127: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1131: MatMultDiagonalBlock(a->A,bb,xx);
1132: return(0);
1133: }
1135: PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1136: {
1137: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1139: VecScatter Mvctx = a->Mvctx;
1142: if (a->Mvctx_mpi1_flg) Mvctx = a->Mvctx_mpi1;
1143: VecScatterBegin(Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1144: (*a->A->ops->multadd)(a->A,xx,yy,zz);
1145: VecScatterEnd(Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1146: (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);
1147: return(0);
1148: }
1150: PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
1151: {
1152: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1156: /* do nondiagonal part */
1157: (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1158: /* do local part */
1159: (*a->A->ops->multtranspose)(a->A,xx,yy);
1160: /* add partial results together */
1161: VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1162: VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1163: return(0);
1164: }
1166: PetscErrorCode MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool *f)
1167: {
1168: MPI_Comm comm;
1169: Mat_MPIAIJ *Aij = (Mat_MPIAIJ*) Amat->data, *Bij;
1170: Mat Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
1171: IS Me,Notme;
1173: PetscInt M,N,first,last,*notme,i;
1174: PetscBool lf;
1175: PetscMPIInt size;
1178: /* Easy test: symmetric diagonal block */
1179: Bij = (Mat_MPIAIJ*) Bmat->data; Bdia = Bij->A;
1180: MatIsTranspose(Adia,Bdia,tol,&lf);
1181: MPIU_Allreduce(&lf,f,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)Amat));
1182: if (!*f) return(0);
1183: PetscObjectGetComm((PetscObject)Amat,&comm);
1184: MPI_Comm_size(comm,&size);
1185: if (size == 1) return(0);
1187: /* Hard test: off-diagonal block. This takes a MatCreateSubMatrix. */
1188: MatGetSize(Amat,&M,&N);
1189: MatGetOwnershipRange(Amat,&first,&last);
1190: PetscMalloc1(N-last+first,¬me);
1191: for (i=0; i<first; i++) notme[i] = i;
1192: for (i=last; i<M; i++) notme[i-last+first] = i;
1193: ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);
1194: ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);
1195: MatCreateSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);
1196: Aoff = Aoffs[0];
1197: MatCreateSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);
1198: Boff = Boffs[0];
1199: MatIsTranspose(Aoff,Boff,tol,f);
1200: MatDestroyMatrices(1,&Aoffs);
1201: MatDestroyMatrices(1,&Boffs);
1202: ISDestroy(&Me);
1203: ISDestroy(&Notme);
1204: PetscFree(notme);
1205: return(0);
1206: }
1208: PetscErrorCode MatIsSymmetric_MPIAIJ(Mat A,PetscReal tol,PetscBool *f)
1209: {
1213: MatIsTranspose_MPIAIJ(A,A,tol,f);
1214: return(0);
1215: }
1217: PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1218: {
1219: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1223: /* do nondiagonal part */
1224: (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1225: /* do local part */
1226: (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);
1227: /* add partial results together */
1228: VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1229: VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1230: return(0);
1231: }
1233: /*
1234: This only works correctly for square matrices where the subblock A->A is the
1235: diagonal block
1236: */
1237: PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v)
1238: {
1240: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1243: if (A->rmap->N != A->cmap->N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
1244: if (A->rmap->rstart != A->cmap->rstart || A->rmap->rend != A->cmap->rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"row partition must equal col partition");
1245: MatGetDiagonal(a->A,v);
1246: return(0);
1247: }
1249: PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa)
1250: {
1251: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1255: MatScale(a->A,aa);
1256: MatScale(a->B,aa);
1257: return(0);
1258: }
1260: PetscErrorCode MatDestroy_MPIAIJ(Mat mat)
1261: {
1262: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
1266: #if defined(PETSC_USE_LOG)
1267: PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N);
1268: #endif
1269: MatStashDestroy_Private(&mat->stash);
1270: VecDestroy(&aij->diag);
1271: MatDestroy(&aij->A);
1272: MatDestroy(&aij->B);
1273: #if defined(PETSC_USE_CTABLE)
1274: PetscTableDestroy(&aij->colmap);
1275: #else
1276: PetscFree(aij->colmap);
1277: #endif
1278: PetscFree(aij->garray);
1279: VecDestroy(&aij->lvec);
1280: VecScatterDestroy(&aij->Mvctx);
1281: if (aij->Mvctx_mpi1) {VecScatterDestroy(&aij->Mvctx_mpi1);}
1282: PetscFree2(aij->rowvalues,aij->rowindices);
1283: PetscFree(aij->ld);
1284: PetscFree(mat->data);
1286: PetscObjectChangeTypeName((PetscObject)mat,0);
1287: PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C",NULL);
1288: PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C",NULL);
1289: PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C",NULL);
1290: PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C",NULL);
1291: PetscObjectComposeFunction((PetscObject)mat,"MatResetPreallocation_C",NULL);
1292: PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C",NULL);
1293: PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",NULL);
1294: PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpibaij_C",NULL);
1295: PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C",NULL);
1296: #if defined(PETSC_HAVE_ELEMENTAL)
1297: PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_elemental_C",NULL);
1298: #endif
1299: #if defined(PETSC_HAVE_HYPRE)
1300: PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_hypre_C",NULL);
1301: PetscObjectComposeFunction((PetscObject)mat,"MatProductSetFromOptions_transpose_mpiaij_mpiaij_C",NULL);
1302: #endif
1303: PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_is_C",NULL);
1304: PetscObjectComposeFunction((PetscObject)mat,"MatProductSetFromOptions_is_mpiaij_C",NULL);
1305: PetscObjectComposeFunction((PetscObject)mat,"MatProductSetFromOptions_mpiaij_mpiaij_C",NULL);
1306: return(0);
1307: }
1309: PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
1310: {
1311: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
1312: Mat_SeqAIJ *A = (Mat_SeqAIJ*)aij->A->data;
1313: Mat_SeqAIJ *B = (Mat_SeqAIJ*)aij->B->data;
1314: const PetscInt *garray = aij->garray;
1315: PetscInt header[4],M,N,m,rs,cs,nz,cnt,i,ja,jb;
1316: PetscInt *rowlens;
1317: PetscInt *colidxs;
1318: PetscScalar *matvals;
1319: PetscErrorCode ierr;
1322: PetscViewerSetUp(viewer);
1324: M = mat->rmap->N;
1325: N = mat->cmap->N;
1326: m = mat->rmap->n;
1327: rs = mat->rmap->rstart;
1328: cs = mat->cmap->rstart;
1329: nz = A->nz + B->nz;
1331: /* write matrix header */
1332: header[0] = MAT_FILE_CLASSID;
1333: header[1] = M; header[2] = N; header[3] = nz;
1334: MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));
1335: PetscViewerBinaryWrite(viewer,header,4,PETSC_INT);
1337: /* fill in and store row lengths */
1338: PetscMalloc1(m,&rowlens);
1339: for (i=0; i<m; i++) rowlens[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
1340: PetscViewerBinaryWriteAll(viewer,rowlens,m,rs,M,PETSC_INT);
1341: PetscFree(rowlens);
1343: /* fill in and store column indices */
1344: PetscMalloc1(nz,&colidxs);
1345: for (cnt=0, i=0; i<m; i++) {
1346: for (jb=B->i[i]; jb<B->i[i+1]; jb++) {
1347: if (garray[B->j[jb]] > cs) break;
1348: colidxs[cnt++] = garray[B->j[jb]];
1349: }
1350: for (ja=A->i[i]; ja<A->i[i+1]; ja++)
1351: colidxs[cnt++] = A->j[ja] + cs;
1352: for (; jb<B->i[i+1]; jb++)
1353: colidxs[cnt++] = garray[B->j[jb]];
1354: }
1355: if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);
1356: PetscViewerBinaryWriteAll(viewer,colidxs,nz,PETSC_DETERMINE,PETSC_DETERMINE,PETSC_INT);
1357: PetscFree(colidxs);
1359: /* fill in and store nonzero values */
1360: PetscMalloc1(nz,&matvals);
1361: for (cnt=0, i=0; i<m; i++) {
1362: for (jb=B->i[i]; jb<B->i[i+1]; jb++) {
1363: if (garray[B->j[jb]] > cs) break;
1364: matvals[cnt++] = B->a[jb];
1365: }
1366: for (ja=A->i[i]; ja<A->i[i+1]; ja++)
1367: matvals[cnt++] = A->a[ja];
1368: for (; jb<B->i[i+1]; jb++)
1369: matvals[cnt++] = B->a[jb];
1370: }
1371: if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);
1372: PetscViewerBinaryWriteAll(viewer,matvals,nz,PETSC_DETERMINE,PETSC_DETERMINE,PETSC_SCALAR);
1373: PetscFree(matvals);
1375: /* write block size option to the viewer's .info file */
1376: MatView_Binary_BlockSizes(mat,viewer);
1377: return(0);
1378: }
1380: #include <petscdraw.h>
1381: PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
1382: {
1383: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
1384: PetscErrorCode ierr;
1385: PetscMPIInt rank = aij->rank,size = aij->size;
1386: PetscBool isdraw,iascii,isbinary;
1387: PetscViewer sviewer;
1388: PetscViewerFormat format;
1391: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1392: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1393: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1394: if (iascii) {
1395: PetscViewerGetFormat(viewer,&format);
1396: if (format == PETSC_VIEWER_LOAD_BALANCE) {
1397: PetscInt i,nmax = 0,nmin = PETSC_MAX_INT,navg = 0,*nz,nzlocal = ((Mat_SeqAIJ*) (aij->A->data))->nz + ((Mat_SeqAIJ*) (aij->B->data))->nz;
1398: PetscMalloc1(size,&nz);
1399: MPI_Allgather(&nzlocal,1,MPIU_INT,nz,1,MPIU_INT,PetscObjectComm((PetscObject)mat));
1400: for (i=0; i<(PetscInt)size; i++) {
1401: nmax = PetscMax(nmax,nz[i]);
1402: nmin = PetscMin(nmin,nz[i]);
1403: navg += nz[i];
1404: }
1405: PetscFree(nz);
1406: navg = navg/size;
1407: PetscViewerASCIIPrintf(viewer,"Load Balance - Nonzeros: Min %D avg %D max %D\n",nmin,navg,nmax);
1408: return(0);
1409: }
1410: PetscViewerGetFormat(viewer,&format);
1411: if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1412: MatInfo info;
1413: PetscBool inodes;
1415: MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
1416: MatGetInfo(mat,MAT_LOCAL,&info);
1417: MatInodeGetInodeSizes(aij->A,NULL,(PetscInt**)&inodes,NULL);
1418: PetscViewerASCIIPushSynchronized(viewer);
1419: if (!inodes) {
1420: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %g, not using I-node routines\n",
1421: rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(double)info.memory);
1422: } else {
1423: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %g, using I-node routines\n",
1424: rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(double)info.memory);
1425: }
1426: MatGetInfo(aij->A,MAT_LOCAL,&info);
1427: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
1428: MatGetInfo(aij->B,MAT_LOCAL,&info);
1429: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
1430: PetscViewerFlush(viewer);
1431: PetscViewerASCIIPopSynchronized(viewer);
1432: PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");
1433: VecScatterView(aij->Mvctx,viewer);
1434: return(0);
1435: } else if (format == PETSC_VIEWER_ASCII_INFO) {
1436: PetscInt inodecount,inodelimit,*inodes;
1437: MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);
1438: if (inodes) {
1439: PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);
1440: } else {
1441: PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");
1442: }
1443: return(0);
1444: } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
1445: return(0);
1446: }
1447: } else if (isbinary) {
1448: if (size == 1) {
1449: PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);
1450: MatView(aij->A,viewer);
1451: } else {
1452: MatView_MPIAIJ_Binary(mat,viewer);
1453: }
1454: return(0);
1455: } else if (iascii && size == 1) {
1456: PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);
1457: MatView(aij->A,viewer);
1458: return(0);
1459: } else if (isdraw) {
1460: PetscDraw draw;
1461: PetscBool isnull;
1462: PetscViewerDrawGetDraw(viewer,0,&draw);
1463: PetscDrawIsNull(draw,&isnull);
1464: if (isnull) return(0);
1465: }
1467: { /* assemble the entire matrix onto first processor */
1468: Mat A = NULL, Av;
1469: IS isrow,iscol;
1471: ISCreateStride(PetscObjectComm((PetscObject)mat),!rank ? mat->rmap->N : 0,0,1,&isrow);
1472: ISCreateStride(PetscObjectComm((PetscObject)mat),!rank ? mat->cmap->N : 0,0,1,&iscol);
1473: MatCreateSubMatrix(mat,isrow,iscol,MAT_INITIAL_MATRIX,&A);
1474: MatMPIAIJGetSeqAIJ(A,&Av,NULL,NULL);
1475: /* The commented code uses MatCreateSubMatrices instead */
1476: /*
1477: Mat *AA, A = NULL, Av;
1478: IS isrow,iscol;
1480: ISCreateStride(PetscObjectComm((PetscObject)mat),!rank ? mat->rmap->N : 0,0,1,&isrow);
1481: ISCreateStride(PetscObjectComm((PetscObject)mat),!rank ? mat->cmap->N : 0,0,1,&iscol);
1482: MatCreateSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&AA);
1483: if (!rank) {
1484: PetscObjectReference((PetscObject)AA[0]);
1485: A = AA[0];
1486: Av = AA[0];
1487: }
1488: MatDestroySubMatrices(1,&AA);
1489: */
1490: ISDestroy(&iscol);
1491: ISDestroy(&isrow);
1492: /*
1493: Everyone has to call to draw the matrix since the graphics waits are
1494: synchronized across all processors that share the PetscDraw object
1495: */
1496: PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
1497: if (!rank) {
1498: if (((PetscObject)mat)->name) {
1499: PetscObjectSetName((PetscObject)Av,((PetscObject)mat)->name);
1500: }
1501: MatView_SeqAIJ(Av,sviewer);
1502: }
1503: PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
1504: PetscViewerFlush(viewer);
1505: MatDestroy(&A);
1506: }
1507: return(0);
1508: }
1510: PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer)
1511: {
1513: PetscBool iascii,isdraw,issocket,isbinary;
1516: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1517: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1518: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1519: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);
1520: if (iascii || isdraw || isbinary || issocket) {
1521: MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);
1522: }
1523: return(0);
1524: }
1526: PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
1527: {
1528: Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
1530: Vec bb1 = 0;
1531: PetscBool hasop;
1534: if (flag == SOR_APPLY_UPPER) {
1535: (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1536: return(0);
1537: }
1539: if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) {
1540: VecDuplicate(bb,&bb1);
1541: }
1543: if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
1544: if (flag & SOR_ZERO_INITIAL_GUESS) {
1545: (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1546: its--;
1547: }
1549: while (its--) {
1550: VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1551: VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1553: /* update rhs: bb1 = bb - B*x */
1554: VecScale(mat->lvec,-1.0);
1555: (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);
1557: /* local sweep */
1558: (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);
1559: }
1560: } else if (flag & SOR_LOCAL_FORWARD_SWEEP) {
1561: if (flag & SOR_ZERO_INITIAL_GUESS) {
1562: (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1563: its--;
1564: }
1565: while (its--) {
1566: VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1567: VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1569: /* update rhs: bb1 = bb - B*x */
1570: VecScale(mat->lvec,-1.0);
1571: (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);
1573: /* local sweep */
1574: (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);
1575: }
1576: } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) {
1577: if (flag & SOR_ZERO_INITIAL_GUESS) {
1578: (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
1579: its--;
1580: }
1581: while (its--) {
1582: VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1583: VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1585: /* update rhs: bb1 = bb - B*x */
1586: VecScale(mat->lvec,-1.0);
1587: (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);
1589: /* local sweep */
1590: (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);
1591: }
1592: } else if (flag & SOR_EISENSTAT) {
1593: Vec xx1;
1595: VecDuplicate(bb,&xx1);
1596: (*mat->A->ops->sor)(mat->A,bb,omega,(MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_BACKWARD_SWEEP),fshift,lits,1,xx);
1598: VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1599: VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
1600: if (!mat->diag) {
1601: MatCreateVecs(matin,&mat->diag,NULL);
1602: MatGetDiagonal(matin,mat->diag);
1603: }
1604: MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);
1605: if (hasop) {
1606: MatMultDiagonalBlock(matin,xx,bb1);
1607: } else {
1608: VecPointwiseMult(bb1,mat->diag,xx);
1609: }
1610: VecAYPX(bb1,(omega-2.0)/omega,bb);
1612: MatMultAdd(mat->B,mat->lvec,bb1,bb1);
1614: /* local sweep */
1615: (*mat->A->ops->sor)(mat->A,bb1,omega,(MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_FORWARD_SWEEP),fshift,lits,1,xx1);
1616: VecAXPY(xx,1.0,xx1);
1617: VecDestroy(&xx1);
1618: } else SETERRQ(PetscObjectComm((PetscObject)matin),PETSC_ERR_SUP,"Parallel SOR not supported");
1620: VecDestroy(&bb1);
1622: matin->factorerrortype = mat->A->factorerrortype;
1623: return(0);
1624: }
1626: PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B)
1627: {
1628: Mat aA,aB,Aperm;
1629: const PetscInt *rwant,*cwant,*gcols,*ai,*bi,*aj,*bj;
1630: PetscScalar *aa,*ba;
1631: PetscInt i,j,m,n,ng,anz,bnz,*dnnz,*onnz,*tdnnz,*tonnz,*rdest,*cdest,*work,*gcdest;
1632: PetscSF rowsf,sf;
1633: IS parcolp = NULL;
1634: PetscBool done;
1638: MatGetLocalSize(A,&m,&n);
1639: ISGetIndices(rowp,&rwant);
1640: ISGetIndices(colp,&cwant);
1641: PetscMalloc3(PetscMax(m,n),&work,m,&rdest,n,&cdest);
1643: /* Invert row permutation to find out where my rows should go */
1644: PetscSFCreate(PetscObjectComm((PetscObject)A),&rowsf);
1645: PetscSFSetGraphLayout(rowsf,A->rmap,A->rmap->n,NULL,PETSC_OWN_POINTER,rwant);
1646: PetscSFSetFromOptions(rowsf);
1647: for (i=0; i<m; i++) work[i] = A->rmap->rstart + i;
1648: PetscSFReduceBegin(rowsf,MPIU_INT,work,rdest,MPIU_REPLACE);
1649: PetscSFReduceEnd(rowsf,MPIU_INT,work,rdest,MPIU_REPLACE);
1651: /* Invert column permutation to find out where my columns should go */
1652: PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);
1653: PetscSFSetGraphLayout(sf,A->cmap,A->cmap->n,NULL,PETSC_OWN_POINTER,cwant);
1654: PetscSFSetFromOptions(sf);
1655: for (i=0; i<n; i++) work[i] = A->cmap->rstart + i;
1656: PetscSFReduceBegin(sf,MPIU_INT,work,cdest,MPIU_REPLACE);
1657: PetscSFReduceEnd(sf,MPIU_INT,work,cdest,MPIU_REPLACE);
1658: PetscSFDestroy(&sf);
1660: ISRestoreIndices(rowp,&rwant);
1661: ISRestoreIndices(colp,&cwant);
1662: MatMPIAIJGetSeqAIJ(A,&aA,&aB,&gcols);
1664: /* Find out where my gcols should go */
1665: MatGetSize(aB,NULL,&ng);
1666: PetscMalloc1(ng,&gcdest);
1667: PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);
1668: PetscSFSetGraphLayout(sf,A->cmap,ng,NULL,PETSC_OWN_POINTER,gcols);
1669: PetscSFSetFromOptions(sf);
1670: PetscSFBcastBegin(sf,MPIU_INT,cdest,gcdest);
1671: PetscSFBcastEnd(sf,MPIU_INT,cdest,gcdest);
1672: PetscSFDestroy(&sf);
1674: PetscCalloc4(m,&dnnz,m,&onnz,m,&tdnnz,m,&tonnz);
1675: MatGetRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);
1676: MatGetRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);
1677: for (i=0; i<m; i++) {
1678: PetscInt row = rdest[i];
1679: PetscMPIInt rowner;
1680: PetscLayoutFindOwner(A->rmap,row,&rowner);
1681: for (j=ai[i]; j<ai[i+1]; j++) {
1682: PetscInt col = cdest[aj[j]];
1683: PetscMPIInt cowner;
1684: PetscLayoutFindOwner(A->cmap,col,&cowner); /* Could build an index for the columns to eliminate this search */
1685: if (rowner == cowner) dnnz[i]++;
1686: else onnz[i]++;
1687: }
1688: for (j=bi[i]; j<bi[i+1]; j++) {
1689: PetscInt col = gcdest[bj[j]];
1690: PetscMPIInt cowner;
1691: PetscLayoutFindOwner(A->cmap,col,&cowner);
1692: if (rowner == cowner) dnnz[i]++;
1693: else onnz[i]++;
1694: }
1695: }
1696: PetscSFBcastBegin(rowsf,MPIU_INT,dnnz,tdnnz);
1697: PetscSFBcastEnd(rowsf,MPIU_INT,dnnz,tdnnz);
1698: PetscSFBcastBegin(rowsf,MPIU_INT,onnz,tonnz);
1699: PetscSFBcastEnd(rowsf,MPIU_INT,onnz,tonnz);
1700: PetscSFDestroy(&rowsf);
1702: MatCreateAIJ(PetscObjectComm((PetscObject)A),A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N,0,tdnnz,0,tonnz,&Aperm);
1703: MatSeqAIJGetArray(aA,&aa);
1704: MatSeqAIJGetArray(aB,&ba);
1705: for (i=0; i<m; i++) {
1706: PetscInt *acols = dnnz,*bcols = onnz; /* Repurpose now-unneeded arrays */
1707: PetscInt j0,rowlen;
1708: rowlen = ai[i+1] - ai[i];
1709: for (j0=j=0; j<rowlen; j0=j) { /* rowlen could be larger than number of rows m, so sum in batches */
1710: for ( ; j<PetscMin(rowlen,j0+m); j++) acols[j-j0] = cdest[aj[ai[i]+j]];
1711: MatSetValues(Aperm,1,&rdest[i],j-j0,acols,aa+ai[i]+j0,INSERT_VALUES);
1712: }
1713: rowlen = bi[i+1] - bi[i];
1714: for (j0=j=0; j<rowlen; j0=j) {
1715: for ( ; j<PetscMin(rowlen,j0+m); j++) bcols[j-j0] = gcdest[bj[bi[i]+j]];
1716: MatSetValues(Aperm,1,&rdest[i],j-j0,bcols,ba+bi[i]+j0,INSERT_VALUES);
1717: }
1718: }
1719: MatAssemblyBegin(Aperm,MAT_FINAL_ASSEMBLY);
1720: MatAssemblyEnd(Aperm,MAT_FINAL_ASSEMBLY);
1721: MatRestoreRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);
1722: MatRestoreRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);
1723: MatSeqAIJRestoreArray(aA,&aa);
1724: MatSeqAIJRestoreArray(aB,&ba);
1725: PetscFree4(dnnz,onnz,tdnnz,tonnz);
1726: PetscFree3(work,rdest,cdest);
1727: PetscFree(gcdest);
1728: if (parcolp) {ISDestroy(&colp);}
1729: *B = Aperm;
1730: return(0);
1731: }
1733: PetscErrorCode MatGetGhosts_MPIAIJ(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[])
1734: {
1735: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
1739: MatGetSize(aij->B,NULL,nghosts);
1740: if (ghosts) *ghosts = aij->garray;
1741: return(0);
1742: }
1744: PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1745: {
1746: Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
1747: Mat A = mat->A,B = mat->B;
1749: PetscLogDouble isend[5],irecv[5];
1752: info->block_size = 1.0;
1753: MatGetInfo(A,MAT_LOCAL,info);
1755: isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
1756: isend[3] = info->memory; isend[4] = info->mallocs;
1758: MatGetInfo(B,MAT_LOCAL,info);
1760: isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1761: isend[3] += info->memory; isend[4] += info->mallocs;
1762: if (flag == MAT_LOCAL) {
1763: info->nz_used = isend[0];
1764: info->nz_allocated = isend[1];
1765: info->nz_unneeded = isend[2];
1766: info->memory = isend[3];
1767: info->mallocs = isend[4];
1768: } else if (flag == MAT_GLOBAL_MAX) {
1769: MPIU_Allreduce(isend,irecv,5,MPIU_PETSCLOGDOUBLE,MPI_MAX,PetscObjectComm((PetscObject)matin));
1771: info->nz_used = irecv[0];
1772: info->nz_allocated = irecv[1];
1773: info->nz_unneeded = irecv[2];
1774: info->memory = irecv[3];
1775: info->mallocs = irecv[4];
1776: } else if (flag == MAT_GLOBAL_SUM) {
1777: MPIU_Allreduce(isend,irecv,5,MPIU_PETSCLOGDOUBLE,MPI_SUM,PetscObjectComm((PetscObject)matin));
1779: info->nz_used = irecv[0];
1780: info->nz_allocated = irecv[1];
1781: info->nz_unneeded = irecv[2];
1782: info->memory = irecv[3];
1783: info->mallocs = irecv[4];
1784: }
1785: info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */
1786: info->fill_ratio_needed = 0;
1787: info->factor_mallocs = 0;
1788: return(0);
1789: }
1791: PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool flg)
1792: {
1793: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1797: switch (op) {
1798: case MAT_NEW_NONZERO_LOCATIONS:
1799: case MAT_NEW_NONZERO_ALLOCATION_ERR:
1800: case MAT_UNUSED_NONZERO_LOCATION_ERR:
1801: case MAT_KEEP_NONZERO_PATTERN:
1802: case MAT_NEW_NONZERO_LOCATION_ERR:
1803: case MAT_USE_INODES:
1804: case MAT_IGNORE_ZERO_ENTRIES:
1805: MatCheckPreallocated(A,1);
1806: MatSetOption(a->A,op,flg);
1807: MatSetOption(a->B,op,flg);
1808: break;
1809: case MAT_ROW_ORIENTED:
1810: MatCheckPreallocated(A,1);
1811: a->roworiented = flg;
1813: MatSetOption(a->A,op,flg);
1814: MatSetOption(a->B,op,flg);
1815: break;
1816: case MAT_NEW_DIAGONALS:
1817: case MAT_SORTED_FULL:
1818: PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1819: break;
1820: case MAT_IGNORE_OFF_PROC_ENTRIES:
1821: a->donotstash = flg;
1822: break;
1823: /* Symmetry flags are handled directly by MatSetOption() and they don't affect preallocation */
1824: case MAT_SPD:
1825: case MAT_SYMMETRIC:
1826: case MAT_STRUCTURALLY_SYMMETRIC:
1827: case MAT_HERMITIAN:
1828: case MAT_SYMMETRY_ETERNAL:
1829: break;
1830: case MAT_SUBMAT_SINGLEIS:
1831: A->submat_singleis = flg;
1832: break;
1833: case MAT_STRUCTURE_ONLY:
1834: /* The option is handled directly by MatSetOption() */
1835: break;
1836: default:
1837: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1838: }
1839: return(0);
1840: }
1842: PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1843: {
1844: Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
1845: PetscScalar *vworkA,*vworkB,**pvA,**pvB,*v_p;
1847: PetscInt i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart;
1848: PetscInt nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend;
1849: PetscInt *cmap,*idx_p;
1852: if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active");
1853: mat->getrowactive = PETSC_TRUE;
1855: if (!mat->rowvalues && (idx || v)) {
1856: /*
1857: allocate enough space to hold information from the longest row.
1858: */
1859: Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1860: PetscInt max = 1,tmp;
1861: for (i=0; i<matin->rmap->n; i++) {
1862: tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
1863: if (max < tmp) max = tmp;
1864: }
1865: PetscMalloc2(max,&mat->rowvalues,max,&mat->rowindices);
1866: }
1868: if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only local rows");
1869: lrow = row - rstart;
1871: pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1872: if (!v) {pvA = 0; pvB = 0;}
1873: if (!idx) {pcA = 0; if (!v) pcB = 0;}
1874: (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);
1875: (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);
1876: nztot = nzA + nzB;
1878: cmap = mat->garray;
1879: if (v || idx) {
1880: if (nztot) {
1881: /* Sort by increasing column numbers, assuming A and B already sorted */
1882: PetscInt imark = -1;
1883: if (v) {
1884: *v = v_p = mat->rowvalues;
1885: for (i=0; i<nzB; i++) {
1886: if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i];
1887: else break;
1888: }
1889: imark = i;
1890: for (i=0; i<nzA; i++) v_p[imark+i] = vworkA[i];
1891: for (i=imark; i<nzB; i++) v_p[nzA+i] = vworkB[i];
1892: }
1893: if (idx) {
1894: *idx = idx_p = mat->rowindices;
1895: if (imark > -1) {
1896: for (i=0; i<imark; i++) {
1897: idx_p[i] = cmap[cworkB[i]];
1898: }
1899: } else {
1900: for (i=0; i<nzB; i++) {
1901: if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]];
1902: else break;
1903: }
1904: imark = i;
1905: }
1906: for (i=0; i<nzA; i++) idx_p[imark+i] = cstart + cworkA[i];
1907: for (i=imark; i<nzB; i++) idx_p[nzA+i] = cmap[cworkB[i]];
1908: }
1909: } else {
1910: if (idx) *idx = 0;
1911: if (v) *v = 0;
1912: }
1913: }
1914: *nz = nztot;
1915: (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);
1916: (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);
1917: return(0);
1918: }
1920: PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1921: {
1922: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
1925: if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first");
1926: aij->getrowactive = PETSC_FALSE;
1927: return(0);
1928: }
1930: PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1931: {
1932: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
1933: Mat_SeqAIJ *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1935: PetscInt i,j,cstart = mat->cmap->rstart;
1936: PetscReal sum = 0.0;
1937: MatScalar *v;
1940: if (aij->size == 1) {
1941: MatNorm(aij->A,type,norm);
1942: } else {
1943: if (type == NORM_FROBENIUS) {
1944: v = amat->a;
1945: for (i=0; i<amat->nz; i++) {
1946: sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1947: }
1948: v = bmat->a;
1949: for (i=0; i<bmat->nz; i++) {
1950: sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1951: }
1952: MPIU_Allreduce(&sum,norm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
1953: *norm = PetscSqrtReal(*norm);
1954: PetscLogFlops(2*amat->nz+2*bmat->nz);
1955: } else if (type == NORM_1) { /* max column norm */
1956: PetscReal *tmp,*tmp2;
1957: PetscInt *jj,*garray = aij->garray;
1958: PetscCalloc1(mat->cmap->N+1,&tmp);
1959: PetscMalloc1(mat->cmap->N+1,&tmp2);
1960: *norm = 0.0;
1961: v = amat->a; jj = amat->j;
1962: for (j=0; j<amat->nz; j++) {
1963: tmp[cstart + *jj++] += PetscAbsScalar(*v); v++;
1964: }
1965: v = bmat->a; jj = bmat->j;
1966: for (j=0; j<bmat->nz; j++) {
1967: tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
1968: }
1969: MPIU_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
1970: for (j=0; j<mat->cmap->N; j++) {
1971: if (tmp2[j] > *norm) *norm = tmp2[j];
1972: }
1973: PetscFree(tmp);
1974: PetscFree(tmp2);
1975: PetscLogFlops(PetscMax(amat->nz+bmat->nz-1,0));
1976: } else if (type == NORM_INFINITY) { /* max row norm */
1977: PetscReal ntemp = 0.0;
1978: for (j=0; j<aij->A->rmap->n; j++) {
1979: v = amat->a + amat->i[j];
1980: sum = 0.0;
1981: for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1982: sum += PetscAbsScalar(*v); v++;
1983: }
1984: v = bmat->a + bmat->i[j];
1985: for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1986: sum += PetscAbsScalar(*v); v++;
1987: }
1988: if (sum > ntemp) ntemp = sum;
1989: }
1990: MPIU_Allreduce(&ntemp,norm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)mat));
1991: PetscLogFlops(PetscMax(amat->nz+bmat->nz-1,0));
1992: } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No support for two norm");
1993: }
1994: return(0);
1995: }
1997: PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout)
1998: {
1999: Mat_MPIAIJ *a =(Mat_MPIAIJ*)A->data,*b;
2000: Mat_SeqAIJ *Aloc =(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data,*sub_B_diag;
2001: PetscInt M = A->rmap->N,N=A->cmap->N,ma,na,mb,nb,row,*cols,*cols_tmp,*B_diag_ilen,i,ncol,A_diag_ncol;
2002: const PetscInt *ai,*aj,*bi,*bj,*B_diag_i;
2003: PetscErrorCode ierr;
2004: Mat B,A_diag,*B_diag;
2005: const MatScalar *array;
2008: ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n; nb = a->B->cmap->n;
2009: ai = Aloc->i; aj = Aloc->j;
2010: bi = Bloc->i; bj = Bloc->j;
2011: if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
2012: PetscInt *d_nnz,*g_nnz,*o_nnz;
2013: PetscSFNode *oloc;
2014: PETSC_UNUSED PetscSF sf;
2016: PetscMalloc4(na,&d_nnz,na,&o_nnz,nb,&g_nnz,nb,&oloc);
2017: /* compute d_nnz for preallocation */
2018: PetscArrayzero(d_nnz,na);
2019: for (i=0; i<ai[ma]; i++) {
2020: d_nnz[aj[i]]++;
2021: }
2022: /* compute local off-diagonal contributions */
2023: PetscArrayzero(g_nnz,nb);
2024: for (i=0; i<bi[ma]; i++) g_nnz[bj[i]]++;
2025: /* map those to global */
2026: PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);
2027: PetscSFSetGraphLayout(sf,A->cmap,nb,NULL,PETSC_USE_POINTER,a->garray);
2028: PetscSFSetFromOptions(sf);
2029: PetscArrayzero(o_nnz,na);
2030: PetscSFReduceBegin(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);
2031: PetscSFReduceEnd(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);
2032: PetscSFDestroy(&sf);
2034: MatCreate(PetscObjectComm((PetscObject)A),&B);
2035: MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);
2036: MatSetBlockSizes(B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));
2037: MatSetType(B,((PetscObject)A)->type_name);
2038: MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);
2039: PetscFree4(d_nnz,o_nnz,g_nnz,oloc);
2040: } else {
2041: B = *matout;
2042: MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
2043: }
2045: b = (Mat_MPIAIJ*)B->data;
2046: A_diag = a->A;
2047: B_diag = &b->A;
2048: sub_B_diag = (Mat_SeqAIJ*)(*B_diag)->data;
2049: A_diag_ncol = A_diag->cmap->N;
2050: B_diag_ilen = sub_B_diag->ilen;
2051: B_diag_i = sub_B_diag->i;
2053: /* Set ilen for diagonal of B */
2054: for (i=0; i<A_diag_ncol; i++) {
2055: B_diag_ilen[i] = B_diag_i[i+1] - B_diag_i[i];
2056: }
2058: /* Transpose the diagonal part of the matrix. In contrast to the offdiagonal part, this can be done
2059: very quickly (=without using MatSetValues), because all writes are local. */
2060: MatTranspose(A_diag,MAT_REUSE_MATRIX,B_diag);
2062: /* copy over the B part */
2063: PetscMalloc1(bi[mb],&cols);
2064: array = Bloc->a;
2065: row = A->rmap->rstart;
2066: for (i=0; i<bi[mb]; i++) cols[i] = a->garray[bj[i]];
2067: cols_tmp = cols;
2068: for (i=0; i<mb; i++) {
2069: ncol = bi[i+1]-bi[i];
2070: MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);
2071: row++;
2072: array += ncol; cols_tmp += ncol;
2073: }
2074: PetscFree(cols);
2076: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2077: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2078: if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) {
2079: *matout = B;
2080: } else {
2081: MatHeaderMerge(A,&B);
2082: }
2083: return(0);
2084: }
2086: PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
2087: {
2088: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
2089: Mat a = aij->A,b = aij->B;
2091: PetscInt s1,s2,s3;
2094: MatGetLocalSize(mat,&s2,&s3);
2095: if (rr) {
2096: VecGetLocalSize(rr,&s1);
2097: if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
2098: /* Overlap communication with computation. */
2099: VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);
2100: }
2101: if (ll) {
2102: VecGetLocalSize(ll,&s1);
2103: if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
2104: (*b->ops->diagonalscale)(b,ll,0);
2105: }
2106: /* scale the diagonal block */
2107: (*a->ops->diagonalscale)(a,ll,rr);
2109: if (rr) {
2110: /* Do a scatter end and then right scale the off-diagonal block */
2111: VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);
2112: (*b->ops->diagonalscale)(b,0,aij->lvec);
2113: }
2114: return(0);
2115: }
2117: PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A)
2118: {
2119: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2123: MatSetUnfactored(a->A);
2124: return(0);
2125: }
2127: PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool *flag)
2128: {
2129: Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
2130: Mat a,b,c,d;
2131: PetscBool flg;
2135: a = matA->A; b = matA->B;
2136: c = matB->A; d = matB->B;
2138: MatEqual(a,c,&flg);
2139: if (flg) {
2140: MatEqual(b,d,&flg);
2141: }
2142: MPIU_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
2143: return(0);
2144: }
2146: PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
2147: {
2149: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2150: Mat_MPIAIJ *b = (Mat_MPIAIJ*)B->data;
2153: /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
2154: if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
2155: /* because of the column compression in the off-processor part of the matrix a->B,
2156: the number of columns in a->B and b->B may be different, hence we cannot call
2157: the MatCopy() directly on the two parts. If need be, we can provide a more
2158: efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
2159: then copying the submatrices */
2160: MatCopy_Basic(A,B,str);
2161: } else {
2162: MatCopy(a->A,b->A,str);
2163: MatCopy(a->B,b->B,str);
2164: }
2165: PetscObjectStateIncrease((PetscObject)B);
2166: return(0);
2167: }
2169: PetscErrorCode MatSetUp_MPIAIJ(Mat A)
2170: {
2174: MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);
2175: return(0);
2176: }
2178: /*
2179: Computes the number of nonzeros per row needed for preallocation when X and Y
2180: have different nonzero structure.
2181: */
2182: PetscErrorCode MatAXPYGetPreallocation_MPIX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *xltog,const PetscInt *yi,const PetscInt *yj,const PetscInt *yltog,PetscInt *nnz)
2183: {
2184: PetscInt i,j,k,nzx,nzy;
2187: /* Set the number of nonzeros in the new matrix */
2188: for (i=0; i<m; i++) {
2189: const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2190: nzx = xi[i+1] - xi[i];
2191: nzy = yi[i+1] - yi[i];
2192: nnz[i] = 0;
2193: for (j=0,k=0; j<nzx; j++) { /* Point in X */
2194: for (; k<nzy && yltog[yjj[k]]<xltog[xjj[j]]; k++) nnz[i]++; /* Catch up to X */
2195: if (k<nzy && yltog[yjj[k]]==xltog[xjj[j]]) k++; /* Skip duplicate */
2196: nnz[i]++;
2197: }
2198: for (; k<nzy; k++) nnz[i]++;
2199: }
2200: return(0);
2201: }
2203: /* This is the same as MatAXPYGetPreallocation_SeqAIJ, except that the local-to-global map is provided */
2204: static PetscErrorCode MatAXPYGetPreallocation_MPIAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt *nnz)
2205: {
2207: PetscInt m = Y->rmap->N;
2208: Mat_SeqAIJ *x = (Mat_SeqAIJ*)X->data;
2209: Mat_SeqAIJ *y = (Mat_SeqAIJ*)Y->data;
2212: MatAXPYGetPreallocation_MPIX_private(m,x->i,x->j,xltog,y->i,y->j,yltog,nnz);
2213: return(0);
2214: }
2216: PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2217: {
2219: Mat_MPIAIJ *xx = (Mat_MPIAIJ*)X->data,*yy = (Mat_MPIAIJ*)Y->data;
2220: PetscBLASInt bnz,one=1;
2221: Mat_SeqAIJ *x,*y;
2224: if (str == SAME_NONZERO_PATTERN) {
2225: PetscScalar alpha = a;
2226: x = (Mat_SeqAIJ*)xx->A->data;
2227: PetscBLASIntCast(x->nz,&bnz);
2228: y = (Mat_SeqAIJ*)yy->A->data;
2229: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2230: x = (Mat_SeqAIJ*)xx->B->data;
2231: y = (Mat_SeqAIJ*)yy->B->data;
2232: PetscBLASIntCast(x->nz,&bnz);
2233: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2234: PetscObjectStateIncrease((PetscObject)Y);
2235: /* the MatAXPY_Basic* subroutines calls MatAssembly, so the matrix on the GPU
2236: will be updated */
2237: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2238: if (Y->offloadmask != PETSC_OFFLOAD_UNALLOCATED) {
2239: Y->offloadmask = PETSC_OFFLOAD_CPU;
2240: }
2241: #endif
2242: } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2243: MatAXPY_Basic(Y,a,X,str);
2244: } else {
2245: Mat B;
2246: PetscInt *nnz_d,*nnz_o;
2247: PetscMalloc1(yy->A->rmap->N,&nnz_d);
2248: PetscMalloc1(yy->B->rmap->N,&nnz_o);
2249: MatCreate(PetscObjectComm((PetscObject)Y),&B);
2250: PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
2251: MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
2252: MatSetBlockSizesFromMats(B,Y,Y);
2253: MatSetType(B,MATMPIAIJ);
2254: MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);
2255: MatAXPYGetPreallocation_MPIAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);
2256: MatMPIAIJSetPreallocation(B,0,nnz_d,0,nnz_o);
2257: MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
2258: MatHeaderReplace(Y,&B);
2259: PetscFree(nnz_d);
2260: PetscFree(nnz_o);
2261: }
2262: return(0);
2263: }
2265: extern PetscErrorCode MatConjugate_SeqAIJ(Mat);
2267: PetscErrorCode MatConjugate_MPIAIJ(Mat mat)
2268: {
2269: #if defined(PETSC_USE_COMPLEX)
2271: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
2274: MatConjugate_SeqAIJ(aij->A);
2275: MatConjugate_SeqAIJ(aij->B);
2276: #else
2278: #endif
2279: return(0);
2280: }
2282: PetscErrorCode MatRealPart_MPIAIJ(Mat A)
2283: {
2284: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2288: MatRealPart(a->A);
2289: MatRealPart(a->B);
2290: return(0);
2291: }
2293: PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A)
2294: {
2295: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2299: MatImaginaryPart(a->A);
2300: MatImaginaryPart(a->B);
2301: return(0);
2302: }
2304: PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2305: {
2306: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2308: PetscInt i,*idxb = 0;
2309: PetscScalar *va,*vb;
2310: Vec vtmp;
2313: MatGetRowMaxAbs(a->A,v,idx);
2314: VecGetArray(v,&va);
2315: if (idx) {
2316: for (i=0; i<A->rmap->n; i++) {
2317: if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2318: }
2319: }
2321: VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);
2322: if (idx) {
2323: PetscMalloc1(A->rmap->n,&idxb);
2324: }
2325: MatGetRowMaxAbs(a->B,vtmp,idxb);
2326: VecGetArray(vtmp,&vb);
2328: for (i=0; i<A->rmap->n; i++) {
2329: if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {
2330: va[i] = vb[i];
2331: if (idx) idx[i] = a->garray[idxb[i]];
2332: }
2333: }
2335: VecRestoreArray(v,&va);
2336: VecRestoreArray(vtmp,&vb);
2337: PetscFree(idxb);
2338: VecDestroy(&vtmp);
2339: return(0);
2340: }
2342: PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2343: {
2344: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2346: PetscInt i,*idxb = 0;
2347: PetscScalar *va,*vb;
2348: Vec vtmp;
2351: MatGetRowMinAbs(a->A,v,idx);
2352: VecGetArray(v,&va);
2353: if (idx) {
2354: for (i=0; i<A->cmap->n; i++) {
2355: if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
2356: }
2357: }
2359: VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);
2360: if (idx) {
2361: PetscMalloc1(A->rmap->n,&idxb);
2362: }
2363: MatGetRowMinAbs(a->B,vtmp,idxb);
2364: VecGetArray(vtmp,&vb);
2366: for (i=0; i<A->rmap->n; i++) {
2367: if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) {
2368: va[i] = vb[i];
2369: if (idx) idx[i] = a->garray[idxb[i]];
2370: }
2371: }
2373: VecRestoreArray(v,&va);
2374: VecRestoreArray(vtmp,&vb);
2375: PetscFree(idxb);
2376: VecDestroy(&vtmp);
2377: return(0);
2378: }
2380: PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2381: {
2382: Mat_MPIAIJ *mat = (Mat_MPIAIJ*) A->data;
2383: PetscInt n = A->rmap->n;
2384: PetscInt cstart = A->cmap->rstart;
2385: PetscInt *cmap = mat->garray;
2386: PetscInt *diagIdx, *offdiagIdx;
2387: Vec diagV, offdiagV;
2388: PetscScalar *a, *diagA, *offdiagA;
2389: PetscInt r;
2393: PetscMalloc2(n,&diagIdx,n,&offdiagIdx);
2394: VecCreateSeq(PetscObjectComm((PetscObject)A), n, &diagV);
2395: VecCreateSeq(PetscObjectComm((PetscObject)A), n, &offdiagV);
2396: MatGetRowMin(mat->A, diagV, diagIdx);
2397: MatGetRowMin(mat->B, offdiagV, offdiagIdx);
2398: VecGetArray(v, &a);
2399: VecGetArray(diagV, &diagA);
2400: VecGetArray(offdiagV, &offdiagA);
2401: for (r = 0; r < n; ++r) {
2402: if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) {
2403: a[r] = diagA[r];
2404: idx[r] = cstart + diagIdx[r];
2405: } else {
2406: a[r] = offdiagA[r];
2407: idx[r] = cmap[offdiagIdx[r]];
2408: }
2409: }
2410: VecRestoreArray(v, &a);
2411: VecRestoreArray(diagV, &diagA);
2412: VecRestoreArray(offdiagV, &offdiagA);
2413: VecDestroy(&diagV);
2414: VecDestroy(&offdiagV);
2415: PetscFree2(diagIdx, offdiagIdx);
2416: return(0);
2417: }
2419: PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[])
2420: {
2421: Mat_MPIAIJ *mat = (Mat_MPIAIJ*) A->data;
2422: PetscInt n = A->rmap->n;
2423: PetscInt cstart = A->cmap->rstart;
2424: PetscInt *cmap = mat->garray;
2425: PetscInt *diagIdx, *offdiagIdx;
2426: Vec diagV, offdiagV;
2427: PetscScalar *a, *diagA, *offdiagA;
2428: PetscInt r;
2432: PetscMalloc2(n,&diagIdx,n,&offdiagIdx);
2433: VecCreateSeq(PETSC_COMM_SELF, n, &diagV);
2434: VecCreateSeq(PETSC_COMM_SELF, n, &offdiagV);
2435: MatGetRowMax(mat->A, diagV, diagIdx);
2436: MatGetRowMax(mat->B, offdiagV, offdiagIdx);
2437: VecGetArray(v, &a);
2438: VecGetArray(diagV, &diagA);
2439: VecGetArray(offdiagV, &offdiagA);
2440: for (r = 0; r < n; ++r) {
2441: if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) {
2442: a[r] = diagA[r];
2443: idx[r] = cstart + diagIdx[r];
2444: } else {
2445: a[r] = offdiagA[r];
2446: idx[r] = cmap[offdiagIdx[r]];
2447: }
2448: }
2449: VecRestoreArray(v, &a);
2450: VecRestoreArray(diagV, &diagA);
2451: VecRestoreArray(offdiagV, &offdiagA);
2452: VecDestroy(&diagV);
2453: VecDestroy(&offdiagV);
2454: PetscFree2(diagIdx, offdiagIdx);
2455: return(0);
2456: }
2458: PetscErrorCode MatGetSeqNonzeroStructure_MPIAIJ(Mat mat,Mat *newmat)
2459: {
2461: Mat *dummy;
2464: MatCreateSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);
2465: *newmat = *dummy;
2466: PetscFree(dummy);
2467: return(0);
2468: }
2470: PetscErrorCode MatInvertBlockDiagonal_MPIAIJ(Mat A,const PetscScalar **values)
2471: {
2472: Mat_MPIAIJ *a = (Mat_MPIAIJ*) A->data;
2476: MatInvertBlockDiagonal(a->A,values);
2477: A->factorerrortype = a->A->factorerrortype;
2478: return(0);
2479: }
2481: static PetscErrorCode MatSetRandom_MPIAIJ(Mat x,PetscRandom rctx)
2482: {
2484: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)x->data;
2487: if (!x->assembled && !x->preallocated) SETERRQ(PetscObjectComm((PetscObject)x), PETSC_ERR_ARG_WRONGSTATE, "MatSetRandom on an unassembled and unpreallocated MATMPIAIJ is not allowed");
2488: MatSetRandom(aij->A,rctx);
2489: if (x->assembled) {
2490: MatSetRandom(aij->B,rctx);
2491: } else {
2492: MatSetRandomSkipColumnRange_SeqAIJ_Private(aij->B,x->cmap->rstart,x->cmap->rend,rctx);
2493: }
2494: MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
2495: MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
2496: return(0);
2497: }
2499: PetscErrorCode MatMPIAIJSetUseScalableIncreaseOverlap_MPIAIJ(Mat A,PetscBool sc)
2500: {
2502: if (sc) A->ops->increaseoverlap = MatIncreaseOverlap_MPIAIJ_Scalable;
2503: else A->ops->increaseoverlap = MatIncreaseOverlap_MPIAIJ;
2504: return(0);
2505: }
2507: /*@
2508: MatMPIAIJSetUseScalableIncreaseOverlap - Determine if the matrix uses a scalable algorithm to compute the overlap
2510: Collective on Mat
2512: Input Parameters:
2513: + A - the matrix
2514: - sc - PETSC_TRUE indicates use the scalable algorithm (default is not to use the scalable algorithm)
2516: Level: advanced
2518: @*/
2519: PetscErrorCode MatMPIAIJSetUseScalableIncreaseOverlap(Mat A,PetscBool sc)
2520: {
2521: PetscErrorCode ierr;
2524: PetscTryMethod(A,"MatMPIAIJSetUseScalableIncreaseOverlap_C",(Mat,PetscBool),(A,sc));
2525: return(0);
2526: }
2528: PetscErrorCode MatSetFromOptions_MPIAIJ(PetscOptionItems *PetscOptionsObject,Mat A)
2529: {
2530: PetscErrorCode ierr;
2531: PetscBool sc = PETSC_FALSE,flg;
2534: PetscOptionsHead(PetscOptionsObject,"MPIAIJ options");
2535: if (A->ops->increaseoverlap == MatIncreaseOverlap_MPIAIJ_Scalable) sc = PETSC_TRUE;
2536: PetscOptionsBool("-mat_increase_overlap_scalable","Use a scalable algorithm to compute the overlap","MatIncreaseOverlap",sc,&sc,&flg);
2537: if (flg) {
2538: MatMPIAIJSetUseScalableIncreaseOverlap(A,sc);
2539: }
2540: PetscOptionsTail();
2541: return(0);
2542: }
2544: PetscErrorCode MatShift_MPIAIJ(Mat Y,PetscScalar a)
2545: {
2547: Mat_MPIAIJ *maij = (Mat_MPIAIJ*)Y->data;
2548: Mat_SeqAIJ *aij = (Mat_SeqAIJ*)maij->A->data;
2551: if (!Y->preallocated) {
2552: MatMPIAIJSetPreallocation(Y,1,NULL,0,NULL);
2553: } else if (!aij->nz) {
2554: PetscInt nonew = aij->nonew;
2555: MatSeqAIJSetPreallocation(maij->A,1,NULL);
2556: aij->nonew = nonew;
2557: }
2558: MatShift_Basic(Y,a);
2559: return(0);
2560: }
2562: PetscErrorCode MatMissingDiagonal_MPIAIJ(Mat A,PetscBool *missing,PetscInt *d)
2563: {
2564: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2568: if (A->rmap->n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only works for square matrices");
2569: MatMissingDiagonal(a->A,missing,d);
2570: if (d) {
2571: PetscInt rstart;
2572: MatGetOwnershipRange(A,&rstart,NULL);
2573: *d += rstart;
2575: }
2576: return(0);
2577: }
2579: PetscErrorCode MatInvertVariableBlockDiagonal_MPIAIJ(Mat A,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *diag)
2580: {
2581: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2585: MatInvertVariableBlockDiagonal(a->A,nblocks,bsizes,diag);
2586: return(0);
2587: }
2589: /* -------------------------------------------------------------------*/
2590: static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
2591: MatGetRow_MPIAIJ,
2592: MatRestoreRow_MPIAIJ,
2593: MatMult_MPIAIJ,
2594: /* 4*/ MatMultAdd_MPIAIJ,
2595: MatMultTranspose_MPIAIJ,
2596: MatMultTransposeAdd_MPIAIJ,
2597: 0,
2598: 0,
2599: 0,
2600: /*10*/ 0,
2601: 0,
2602: 0,
2603: MatSOR_MPIAIJ,
2604: MatTranspose_MPIAIJ,
2605: /*15*/ MatGetInfo_MPIAIJ,
2606: MatEqual_MPIAIJ,
2607: MatGetDiagonal_MPIAIJ,
2608: MatDiagonalScale_MPIAIJ,
2609: MatNorm_MPIAIJ,
2610: /*20*/ MatAssemblyBegin_MPIAIJ,
2611: MatAssemblyEnd_MPIAIJ,
2612: MatSetOption_MPIAIJ,
2613: MatZeroEntries_MPIAIJ,
2614: /*24*/ MatZeroRows_MPIAIJ,
2615: 0,
2616: 0,
2617: 0,
2618: 0,
2619: /*29*/ MatSetUp_MPIAIJ,
2620: 0,
2621: 0,
2622: MatGetDiagonalBlock_MPIAIJ,
2623: 0,
2624: /*34*/ MatDuplicate_MPIAIJ,
2625: 0,
2626: 0,
2627: 0,
2628: 0,
2629: /*39*/ MatAXPY_MPIAIJ,
2630: MatCreateSubMatrices_MPIAIJ,
2631: MatIncreaseOverlap_MPIAIJ,
2632: MatGetValues_MPIAIJ,
2633: MatCopy_MPIAIJ,
2634: /*44*/ MatGetRowMax_MPIAIJ,
2635: MatScale_MPIAIJ,
2636: MatShift_MPIAIJ,
2637: MatDiagonalSet_MPIAIJ,
2638: MatZeroRowsColumns_MPIAIJ,
2639: /*49*/ MatSetRandom_MPIAIJ,
2640: 0,
2641: 0,
2642: 0,
2643: 0,
2644: /*54*/ MatFDColoringCreate_MPIXAIJ,
2645: 0,
2646: MatSetUnfactored_MPIAIJ,
2647: MatPermute_MPIAIJ,
2648: 0,
2649: /*59*/ MatCreateSubMatrix_MPIAIJ,
2650: MatDestroy_MPIAIJ,
2651: MatView_MPIAIJ,
2652: 0,
2653: 0,
2654: /*64*/ 0,
2655: MatMatMatMultNumeric_MPIAIJ_MPIAIJ_MPIAIJ,
2656: 0,
2657: 0,
2658: 0,
2659: /*69*/ MatGetRowMaxAbs_MPIAIJ,
2660: MatGetRowMinAbs_MPIAIJ,
2661: 0,
2662: 0,
2663: 0,
2664: 0,
2665: /*75*/ MatFDColoringApply_AIJ,
2666: MatSetFromOptions_MPIAIJ,
2667: 0,
2668: 0,
2669: MatFindZeroDiagonals_MPIAIJ,
2670: /*80*/ 0,
2671: 0,
2672: 0,
2673: /*83*/ MatLoad_MPIAIJ,
2674: MatIsSymmetric_MPIAIJ,
2675: 0,
2676: 0,
2677: 0,
2678: 0,
2679: /*89*/ 0,
2680: 0,
2681: MatMatMultNumeric_MPIAIJ_MPIAIJ,
2682: 0,
2683: 0,
2684: /*94*/ MatPtAPNumeric_MPIAIJ_MPIAIJ,
2685: 0,
2686: 0,
2687: 0,
2688: MatBindToCPU_MPIAIJ,
2689: /*99*/ MatProductSetFromOptions_MPIAIJ,
2690: 0,
2691: 0,
2692: MatConjugate_MPIAIJ,
2693: 0,
2694: /*104*/MatSetValuesRow_MPIAIJ,
2695: MatRealPart_MPIAIJ,
2696: MatImaginaryPart_MPIAIJ,
2697: 0,
2698: 0,
2699: /*109*/0,
2700: 0,
2701: MatGetRowMin_MPIAIJ,
2702: 0,
2703: MatMissingDiagonal_MPIAIJ,
2704: /*114*/MatGetSeqNonzeroStructure_MPIAIJ,
2705: 0,
2706: MatGetGhosts_MPIAIJ,
2707: 0,
2708: 0,
2709: /*119*/0,
2710: 0,
2711: 0,
2712: 0,
2713: MatGetMultiProcBlock_MPIAIJ,
2714: /*124*/MatFindNonzeroRows_MPIAIJ,
2715: MatGetColumnNorms_MPIAIJ,
2716: MatInvertBlockDiagonal_MPIAIJ,
2717: MatInvertVariableBlockDiagonal_MPIAIJ,
2718: MatCreateSubMatricesMPI_MPIAIJ,
2719: /*129*/0,
2720: 0,
2721: 0,
2722: MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ,
2723: 0,
2724: /*134*/0,
2725: 0,
2726: 0,
2727: 0,
2728: 0,
2729: /*139*/MatSetBlockSizes_MPIAIJ,
2730: 0,
2731: 0,
2732: MatFDColoringSetUp_MPIXAIJ,
2733: MatFindOffBlockDiagonalEntries_MPIAIJ,
2734: MatCreateMPIMatConcatenateSeqMat_MPIAIJ,
2735: /*145*/0,
2736: 0,
2737: 0
2738: };
2740: /* ----------------------------------------------------------------------------------------*/
2742: PetscErrorCode MatStoreValues_MPIAIJ(Mat mat)
2743: {
2744: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
2748: MatStoreValues(aij->A);
2749: MatStoreValues(aij->B);
2750: return(0);
2751: }
2753: PetscErrorCode MatRetrieveValues_MPIAIJ(Mat mat)
2754: {
2755: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
2759: MatRetrieveValues(aij->A);
2760: MatRetrieveValues(aij->B);
2761: return(0);
2762: }
2764: PetscErrorCode MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
2765: {
2766: Mat_MPIAIJ *b;
2768: PetscMPIInt size;
2771: PetscLayoutSetUp(B->rmap);
2772: PetscLayoutSetUp(B->cmap);
2773: b = (Mat_MPIAIJ*)B->data;
2775: #if defined(PETSC_USE_CTABLE)
2776: PetscTableDestroy(&b->colmap);
2777: #else
2778: PetscFree(b->colmap);
2779: #endif
2780: PetscFree(b->garray);
2781: VecDestroy(&b->lvec);
2782: VecScatterDestroy(&b->Mvctx);
2784: /* Because the B will have been resized we simply destroy it and create a new one each time */
2785: MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);
2786: MatDestroy(&b->B);
2787: MatCreate(PETSC_COMM_SELF,&b->B);
2788: MatSetSizes(b->B,B->rmap->n,size > 1 ? B->cmap->N : 0,B->rmap->n,size > 1 ? B->cmap->N : 0);
2789: MatSetBlockSizesFromMats(b->B,B,B);
2790: MatSetType(b->B,MATSEQAIJ);
2791: PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);
2793: if (!B->preallocated) {
2794: MatCreate(PETSC_COMM_SELF,&b->A);
2795: MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);
2796: MatSetBlockSizesFromMats(b->A,B,B);
2797: MatSetType(b->A,MATSEQAIJ);
2798: PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);
2799: }
2801: MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);
2802: MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);
2803: B->preallocated = PETSC_TRUE;
2804: B->was_assembled = PETSC_FALSE;
2805: B->assembled = PETSC_FALSE;
2806: return(0);
2807: }
2809: PetscErrorCode MatResetPreallocation_MPIAIJ(Mat B)
2810: {
2811: Mat_MPIAIJ *b;
2816: PetscLayoutSetUp(B->rmap);
2817: PetscLayoutSetUp(B->cmap);
2818: b = (Mat_MPIAIJ*)B->data;
2820: #if defined(PETSC_USE_CTABLE)
2821: PetscTableDestroy(&b->colmap);
2822: #else
2823: PetscFree(b->colmap);
2824: #endif
2825: PetscFree(b->garray);
2826: VecDestroy(&b->lvec);
2827: VecScatterDestroy(&b->Mvctx);
2829: MatResetPreallocation(b->A);
2830: MatResetPreallocation(b->B);
2831: B->preallocated = PETSC_TRUE;
2832: B->was_assembled = PETSC_FALSE;
2833: B->assembled = PETSC_FALSE;
2834: return(0);
2835: }
2837: PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
2838: {
2839: Mat mat;
2840: Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data;
2844: *newmat = 0;
2845: MatCreate(PetscObjectComm((PetscObject)matin),&mat);
2846: MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);
2847: MatSetBlockSizesFromMats(mat,matin,matin);
2848: MatSetType(mat,((PetscObject)matin)->type_name);
2849: a = (Mat_MPIAIJ*)mat->data;
2851: mat->factortype = matin->factortype;
2852: mat->assembled = matin->assembled;
2853: mat->insertmode = NOT_SET_VALUES;
2854: mat->preallocated = matin->preallocated;
2856: a->size = oldmat->size;
2857: a->rank = oldmat->rank;
2858: a->donotstash = oldmat->donotstash;
2859: a->roworiented = oldmat->roworiented;
2860: a->rowindices = NULL;
2861: a->rowvalues = NULL;
2862: a->getrowactive = PETSC_FALSE;
2864: PetscLayoutReference(matin->rmap,&mat->rmap);
2865: PetscLayoutReference(matin->cmap,&mat->cmap);
2867: if (oldmat->colmap) {
2868: #if defined(PETSC_USE_CTABLE)
2869: PetscTableCreateCopy(oldmat->colmap,&a->colmap);
2870: #else
2871: PetscMalloc1(mat->cmap->N,&a->colmap);
2872: PetscLogObjectMemory((PetscObject)mat,(mat->cmap->N)*sizeof(PetscInt));
2873: PetscArraycpy(a->colmap,oldmat->colmap,mat->cmap->N);
2874: #endif
2875: } else a->colmap = NULL;
2876: if (oldmat->garray) {
2877: PetscInt len;
2878: len = oldmat->B->cmap->n;
2879: PetscMalloc1(len+1,&a->garray);
2880: PetscLogObjectMemory((PetscObject)mat,len*sizeof(PetscInt));
2881: if (len) { PetscArraycpy(a->garray,oldmat->garray,len); }
2882: } else a->garray = NULL;
2884: /* It may happen MatDuplicate is called with a non-assembled matrix
2885: In fact, MatDuplicate only requires the matrix to be preallocated
2886: This may happen inside a DMCreateMatrix_Shell */
2887: if (oldmat->lvec) {
2888: VecDuplicate(oldmat->lvec,&a->lvec);
2889: PetscLogObjectParent((PetscObject)mat,(PetscObject)a->lvec);
2890: }
2891: if (oldmat->Mvctx) {
2892: VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
2893: PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx);
2894: }
2895: if (oldmat->Mvctx_mpi1) {
2896: VecScatterCopy(oldmat->Mvctx_mpi1,&a->Mvctx_mpi1);
2897: PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx_mpi1);
2898: }
2900: MatDuplicate(oldmat->A,cpvalues,&a->A);
2901: PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);
2902: MatDuplicate(oldmat->B,cpvalues,&a->B);
2903: PetscLogObjectParent((PetscObject)mat,(PetscObject)a->B);
2904: PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);
2905: *newmat = mat;
2906: return(0);
2907: }
2909: PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer)
2910: {
2911: PetscBool isbinary, ishdf5;
2917: /* force binary viewer to load .info file if it has not yet done so */
2918: PetscViewerSetUp(viewer);
2919: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
2920: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5, &ishdf5);
2921: if (isbinary) {
2922: MatLoad_MPIAIJ_Binary(newMat,viewer);
2923: } else if (ishdf5) {
2924: #if defined(PETSC_HAVE_HDF5)
2925: MatLoad_AIJ_HDF5(newMat,viewer);
2926: #else
2927: SETERRQ(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
2928: #endif
2929: } else {
2930: 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);
2931: }
2932: return(0);
2933: }
2935: PetscErrorCode MatLoad_MPIAIJ_Binary(Mat mat, PetscViewer viewer)
2936: {
2937: PetscInt header[4],M,N,m,nz,rows,cols,sum,i;
2938: PetscInt *rowidxs,*colidxs;
2939: PetscScalar *matvals;
2943: PetscViewerSetUp(viewer);
2945: /* read in matrix header */
2946: PetscViewerBinaryRead(viewer,header,4,NULL,PETSC_INT);
2947: if (header[0] != MAT_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Not a matrix object in file");
2948: M = header[1]; N = header[2]; nz = header[3];
2949: if (M < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Matrix row size (%D) in file is negative",M);
2950: if (N < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Matrix column size (%D) in file is negative",N);
2951: if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk, cannot load as MPIAIJ");
2953: /* set block sizes from the viewer's .info file */
2954: MatLoad_Binary_BlockSizes(mat,viewer);
2955: /* set global sizes if not set already */
2956: if (mat->rmap->N < 0) mat->rmap->N = M;
2957: if (mat->cmap->N < 0) mat->cmap->N = N;
2958: PetscLayoutSetUp(mat->rmap);
2959: PetscLayoutSetUp(mat->cmap);
2961: /* check if the matrix sizes are correct */
2962: MatGetSize(mat,&rows,&cols);
2963: if (M != rows || N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different sizes (%D, %D) than the input matrix (%D, %D)",M,N,rows,cols);
2965: /* read in row lengths and build row indices */
2966: MatGetLocalSize(mat,&m,NULL);
2967: PetscMalloc1(m+1,&rowidxs);
2968: PetscViewerBinaryReadAll(viewer,rowidxs+1,m,PETSC_DECIDE,M,PETSC_INT);
2969: rowidxs[0] = 0; for (i=0; i<m; i++) rowidxs[i+1] += rowidxs[i];
2970: MPIU_Allreduce(&rowidxs[m],&sum,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)viewer));
2971: if (sum != nz) SETERRQ2(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Inconsistent matrix data in file: nonzeros = %D, sum-row-lengths = %D\n",nz,sum);
2972: /* read in column indices and matrix values */
2973: PetscMalloc2(rowidxs[m],&colidxs,rowidxs[m],&matvals);
2974: PetscViewerBinaryReadAll(viewer,colidxs,rowidxs[m],PETSC_DETERMINE,PETSC_DETERMINE,PETSC_INT);
2975: PetscViewerBinaryReadAll(viewer,matvals,rowidxs[m],PETSC_DETERMINE,PETSC_DETERMINE,PETSC_SCALAR);
2976: /* store matrix indices and values */
2977: MatMPIAIJSetPreallocationCSR(mat,rowidxs,colidxs,matvals);
2978: PetscFree(rowidxs);
2979: PetscFree2(colidxs,matvals);
2980: return(0);
2981: }
2983: /* Not scalable because of ISAllGather() unless getting all columns. */
2984: PetscErrorCode ISGetSeqIS_Private(Mat mat,IS iscol,IS *isseq)
2985: {
2987: IS iscol_local;
2988: PetscBool isstride;
2989: PetscMPIInt lisstride=0,gisstride;
2992: /* check if we are grabbing all columns*/
2993: PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&isstride);
2995: if (isstride) {
2996: PetscInt start,len,mstart,mlen;
2997: ISStrideGetInfo(iscol,&start,NULL);
2998: ISGetLocalSize(iscol,&len);
2999: MatGetOwnershipRangeColumn(mat,&mstart,&mlen);
3000: if (mstart == start && mlen-mstart == len) lisstride = 1;
3001: }
3003: MPIU_Allreduce(&lisstride,&gisstride,1,MPI_INT,MPI_MIN,PetscObjectComm((PetscObject)mat));
3004: if (gisstride) {
3005: PetscInt N;
3006: MatGetSize(mat,NULL,&N);
3007: ISCreateStride(PETSC_COMM_SELF,N,0,1,&iscol_local);
3008: ISSetIdentity(iscol_local);
3009: PetscInfo(mat,"Optimizing for obtaining all columns of the matrix; skipping ISAllGather()\n");
3010: } else {
3011: PetscInt cbs;
3012: ISGetBlockSize(iscol,&cbs);
3013: ISAllGather(iscol,&iscol_local);
3014: ISSetBlockSize(iscol_local,cbs);
3015: }
3017: *isseq = iscol_local;
3018: return(0);
3019: }
3021: /*
3022: Used by MatCreateSubMatrix_MPIAIJ_SameRowColDist() to avoid ISAllGather() and global size of iscol_local
3023: (see MatCreateSubMatrix_MPIAIJ_nonscalable)
3025: Input Parameters:
3026: mat - matrix
3027: isrow - parallel row index set; its local indices are a subset of local columns of mat,
3028: i.e., mat->rstart <= isrow[i] < mat->rend
3029: iscol - parallel column index set; its local indices are a subset of local columns of mat,
3030: i.e., mat->cstart <= iscol[i] < mat->cend
3031: Output Parameter:
3032: isrow_d,iscol_d - sequential row and column index sets for retrieving mat->A
3033: iscol_o - sequential column index set for retrieving mat->B
3034: garray - column map; garray[i] indicates global location of iscol_o[i] in iscol
3035: */
3036: PetscErrorCode ISGetSeqIS_SameColDist_Private(Mat mat,IS isrow,IS iscol,IS *isrow_d,IS *iscol_d,IS *iscol_o,const PetscInt *garray[])
3037: {
3039: Vec x,cmap;
3040: const PetscInt *is_idx;
3041: PetscScalar *xarray,*cmaparray;
3042: PetscInt ncols,isstart,*idx,m,rstart,*cmap1,count;
3043: Mat_MPIAIJ *a=(Mat_MPIAIJ*)mat->data;
3044: Mat B=a->B;
3045: Vec lvec=a->lvec,lcmap;
3046: PetscInt i,cstart,cend,Bn=B->cmap->N;
3047: MPI_Comm comm;
3048: VecScatter Mvctx=a->Mvctx;
3051: PetscObjectGetComm((PetscObject)mat,&comm);
3052: ISGetLocalSize(iscol,&ncols);
3054: /* (1) iscol is a sub-column vector of mat, pad it with '-1.' to form a full vector x */
3055: MatCreateVecs(mat,&x,NULL);
3056: VecSet(x,-1.0);
3057: VecDuplicate(x,&cmap);
3058: VecSet(cmap,-1.0);
3060: /* Get start indices */
3061: MPI_Scan(&ncols,&isstart,1,MPIU_INT,MPI_SUM,comm);
3062: isstart -= ncols;
3063: MatGetOwnershipRangeColumn(mat,&cstart,&cend);
3065: ISGetIndices(iscol,&is_idx);
3066: VecGetArray(x,&xarray);
3067: VecGetArray(cmap,&cmaparray);
3068: PetscMalloc1(ncols,&idx);
3069: for (i=0; i<ncols; i++) {
3070: xarray[is_idx[i]-cstart] = (PetscScalar)is_idx[i];
3071: cmaparray[is_idx[i]-cstart] = i + isstart; /* global index of iscol[i] */
3072: idx[i] = is_idx[i]-cstart; /* local index of iscol[i] */
3073: }
3074: VecRestoreArray(x,&xarray);
3075: VecRestoreArray(cmap,&cmaparray);
3076: ISRestoreIndices(iscol,&is_idx);
3078: /* Get iscol_d */
3079: ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,iscol_d);
3080: ISGetBlockSize(iscol,&i);
3081: ISSetBlockSize(*iscol_d,i);
3083: /* Get isrow_d */
3084: ISGetLocalSize(isrow,&m);
3085: rstart = mat->rmap->rstart;
3086: PetscMalloc1(m,&idx);
3087: ISGetIndices(isrow,&is_idx);
3088: for (i=0; i<m; i++) idx[i] = is_idx[i]-rstart;
3089: ISRestoreIndices(isrow,&is_idx);
3091: ISCreateGeneral(PETSC_COMM_SELF,m,idx,PETSC_OWN_POINTER,isrow_d);
3092: ISGetBlockSize(isrow,&i);
3093: ISSetBlockSize(*isrow_d,i);
3095: /* (2) Scatter x and cmap using aij->Mvctx to get their off-process portions (see MatMult_MPIAIJ) */
3096: VecScatterBegin(Mvctx,x,lvec,INSERT_VALUES,SCATTER_FORWARD);
3097: VecScatterEnd(Mvctx,x,lvec,INSERT_VALUES,SCATTER_FORWARD);
3099: VecDuplicate(lvec,&lcmap);
3101: VecScatterBegin(Mvctx,cmap,lcmap,INSERT_VALUES,SCATTER_FORWARD);
3102: VecScatterEnd(Mvctx,cmap,lcmap,INSERT_VALUES,SCATTER_FORWARD);
3104: /* (3) create sequential iscol_o (a subset of iscol) and isgarray */
3105: /* off-process column indices */
3106: count = 0;
3107: PetscMalloc1(Bn,&idx);
3108: PetscMalloc1(Bn,&cmap1);
3110: VecGetArray(lvec,&xarray);
3111: VecGetArray(lcmap,&cmaparray);
3112: for (i=0; i<Bn; i++) {
3113: if (PetscRealPart(xarray[i]) > -1.0) {
3114: idx[count] = i; /* local column index in off-diagonal part B */
3115: cmap1[count] = (PetscInt)PetscRealPart(cmaparray[i]); /* column index in submat */
3116: count++;
3117: }
3118: }
3119: VecRestoreArray(lvec,&xarray);
3120: VecRestoreArray(lcmap,&cmaparray);
3122: ISCreateGeneral(PETSC_COMM_SELF,count,idx,PETSC_COPY_VALUES,iscol_o);
3123: /* cannot ensure iscol_o has same blocksize as iscol! */
3125: PetscFree(idx);
3126: *garray = cmap1;
3128: VecDestroy(&x);
3129: VecDestroy(&cmap);
3130: VecDestroy(&lcmap);
3131: return(0);
3132: }
3134: /* isrow and iscol have same processor distribution as mat, output *submat is a submatrix of local mat */
3135: PetscErrorCode MatCreateSubMatrix_MPIAIJ_SameRowColDist(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *submat)
3136: {
3138: Mat_MPIAIJ *a = (Mat_MPIAIJ*)mat->data,*asub;
3139: Mat M = NULL;
3140: MPI_Comm comm;
3141: IS iscol_d,isrow_d,iscol_o;
3142: Mat Asub = NULL,Bsub = NULL;
3143: PetscInt n;
3146: PetscObjectGetComm((PetscObject)mat,&comm);
3148: if (call == MAT_REUSE_MATRIX) {
3149: /* Retrieve isrow_d, iscol_d and iscol_o from submat */
3150: PetscObjectQuery((PetscObject)*submat,"isrow_d",(PetscObject*)&isrow_d);
3151: if (!isrow_d) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"isrow_d passed in was not used before, cannot reuse");
3153: PetscObjectQuery((PetscObject)*submat,"iscol_d",(PetscObject*)&iscol_d);
3154: if (!iscol_d) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"iscol_d passed in was not used before, cannot reuse");
3156: PetscObjectQuery((PetscObject)*submat,"iscol_o",(PetscObject*)&iscol_o);
3157: if (!iscol_o) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"iscol_o passed in was not used before, cannot reuse");
3159: /* Update diagonal and off-diagonal portions of submat */
3160: asub = (Mat_MPIAIJ*)(*submat)->data;
3161: MatCreateSubMatrix_SeqAIJ(a->A,isrow_d,iscol_d,PETSC_DECIDE,MAT_REUSE_MATRIX,&asub->A);
3162: ISGetLocalSize(iscol_o,&n);
3163: if (n) {
3164: MatCreateSubMatrix_SeqAIJ(a->B,isrow_d,iscol_o,PETSC_DECIDE,MAT_REUSE_MATRIX,&asub->B);
3165: }
3166: MatAssemblyBegin(*submat,MAT_FINAL_ASSEMBLY);
3167: MatAssemblyEnd(*submat,MAT_FINAL_ASSEMBLY);
3169: } else { /* call == MAT_INITIAL_MATRIX) */
3170: const PetscInt *garray;
3171: PetscInt BsubN;
3173: /* Create isrow_d, iscol_d, iscol_o and isgarray (replace isgarray with array?) */
3174: ISGetSeqIS_SameColDist_Private(mat,isrow,iscol,&isrow_d,&iscol_d,&iscol_o,&garray);
3176: /* Create local submatrices Asub and Bsub */
3177: MatCreateSubMatrix_SeqAIJ(a->A,isrow_d,iscol_d,PETSC_DECIDE,MAT_INITIAL_MATRIX,&Asub);
3178: MatCreateSubMatrix_SeqAIJ(a->B,isrow_d,iscol_o,PETSC_DECIDE,MAT_INITIAL_MATRIX,&Bsub);
3180: /* Create submatrix M */
3181: MatCreateMPIAIJWithSeqAIJ(comm,Asub,Bsub,garray,&M);
3183: /* If Bsub has empty columns, compress iscol_o such that it will retrieve condensed Bsub from a->B during reuse */
3184: asub = (Mat_MPIAIJ*)M->data;
3186: ISGetLocalSize(iscol_o,&BsubN);
3187: n = asub->B->cmap->N;
3188: if (BsubN > n) {
3189: /* This case can be tested using ~petsc/src/tao/bound/tutorials/runplate2_3 */
3190: const PetscInt *idx;
3191: PetscInt i,j,*idx_new,*subgarray = asub->garray;
3192: PetscInfo2(M,"submatrix Bn %D != BsubN %D, update iscol_o\n",n,BsubN);
3194: PetscMalloc1(n,&idx_new);
3195: j = 0;
3196: ISGetIndices(iscol_o,&idx);
3197: for (i=0; i<n; i++) {
3198: if (j >= BsubN) break;
3199: while (subgarray[i] > garray[j]) j++;
3201: if (subgarray[i] == garray[j]) {
3202: idx_new[i] = idx[j++];
3203: } else SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"subgarray[%D]=%D cannot < garray[%D]=%D",i,subgarray[i],j,garray[j]);
3204: }
3205: ISRestoreIndices(iscol_o,&idx);
3207: ISDestroy(&iscol_o);
3208: ISCreateGeneral(PETSC_COMM_SELF,n,idx_new,PETSC_OWN_POINTER,&iscol_o);
3210: } else if (BsubN < n) {
3211: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Columns of Bsub cannot be smaller than B's",BsubN,asub->B->cmap->N);
3212: }
3214: PetscFree(garray);
3215: *submat = M;
3217: /* Save isrow_d, iscol_d and iscol_o used in processor for next request */
3218: PetscObjectCompose((PetscObject)M,"isrow_d",(PetscObject)isrow_d);
3219: ISDestroy(&isrow_d);
3221: PetscObjectCompose((PetscObject)M,"iscol_d",(PetscObject)iscol_d);
3222: ISDestroy(&iscol_d);
3224: PetscObjectCompose((PetscObject)M,"iscol_o",(PetscObject)iscol_o);
3225: ISDestroy(&iscol_o);
3226: }
3227: return(0);
3228: }
3230: PetscErrorCode MatCreateSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
3231: {
3233: IS iscol_local=NULL,isrow_d;
3234: PetscInt csize;
3235: PetscInt n,i,j,start,end;
3236: PetscBool sameRowDist=PETSC_FALSE,sameDist[2],tsameDist[2];
3237: MPI_Comm comm;
3240: /* If isrow has same processor distribution as mat,
3241: call MatCreateSubMatrix_MPIAIJ_SameRowDist() to avoid using a hash table with global size of iscol */
3242: if (call == MAT_REUSE_MATRIX) {
3243: PetscObjectQuery((PetscObject)*newmat,"isrow_d",(PetscObject*)&isrow_d);
3244: if (isrow_d) {
3245: sameRowDist = PETSC_TRUE;
3246: tsameDist[1] = PETSC_TRUE; /* sameColDist */
3247: } else {
3248: PetscObjectQuery((PetscObject)*newmat,"SubIScol",(PetscObject*)&iscol_local);
3249: if (iscol_local) {
3250: sameRowDist = PETSC_TRUE;
3251: tsameDist[1] = PETSC_FALSE; /* !sameColDist */
3252: }
3253: }
3254: } else {
3255: /* Check if isrow has same processor distribution as mat */
3256: sameDist[0] = PETSC_FALSE;
3257: ISGetLocalSize(isrow,&n);
3258: if (!n) {
3259: sameDist[0] = PETSC_TRUE;
3260: } else {
3261: ISGetMinMax(isrow,&i,&j);
3262: MatGetOwnershipRange(mat,&start,&end);
3263: if (i >= start && j < end) {
3264: sameDist[0] = PETSC_TRUE;
3265: }
3266: }
3268: /* Check if iscol has same processor distribution as mat */
3269: sameDist[1] = PETSC_FALSE;
3270: ISGetLocalSize(iscol,&n);
3271: if (!n) {
3272: sameDist[1] = PETSC_TRUE;
3273: } else {
3274: ISGetMinMax(iscol,&i,&j);
3275: MatGetOwnershipRangeColumn(mat,&start,&end);
3276: if (i >= start && j < end) sameDist[1] = PETSC_TRUE;
3277: }
3279: PetscObjectGetComm((PetscObject)mat,&comm);
3280: MPIU_Allreduce(&sameDist,&tsameDist,2,MPIU_BOOL,MPI_LAND,comm);
3281: sameRowDist = tsameDist[0];
3282: }
3284: if (sameRowDist) {
3285: if (tsameDist[1]) { /* sameRowDist & sameColDist */
3286: /* isrow and iscol have same processor distribution as mat */
3287: MatCreateSubMatrix_MPIAIJ_SameRowColDist(mat,isrow,iscol,call,newmat);
3288: return(0);
3289: } else { /* sameRowDist */
3290: /* isrow has same processor distribution as mat */
3291: if (call == MAT_INITIAL_MATRIX) {
3292: PetscBool sorted;
3293: ISGetSeqIS_Private(mat,iscol,&iscol_local);
3294: ISGetLocalSize(iscol_local,&n); /* local size of iscol_local = global columns of newmat */
3295: ISGetSize(iscol,&i);
3296: if (n != i) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"n %d != size of iscol %d",n,i);
3298: ISSorted(iscol_local,&sorted);
3299: if (sorted) {
3300: /* MatCreateSubMatrix_MPIAIJ_SameRowDist() requires iscol_local be sorted; it can have duplicate indices */
3301: MatCreateSubMatrix_MPIAIJ_SameRowDist(mat,isrow,iscol,iscol_local,MAT_INITIAL_MATRIX,newmat);
3302: return(0);
3303: }
3304: } else { /* call == MAT_REUSE_MATRIX */
3305: IS iscol_sub;
3306: PetscObjectQuery((PetscObject)*newmat,"SubIScol",(PetscObject*)&iscol_sub);
3307: if (iscol_sub) {
3308: MatCreateSubMatrix_MPIAIJ_SameRowDist(mat,isrow,iscol,NULL,call,newmat);
3309: return(0);
3310: }
3311: }
3312: }
3313: }
3315: /* General case: iscol -> iscol_local which has global size of iscol */
3316: if (call == MAT_REUSE_MATRIX) {
3317: PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);
3318: if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
3319: } else {
3320: if (!iscol_local) {
3321: ISGetSeqIS_Private(mat,iscol,&iscol_local);
3322: }
3323: }
3325: ISGetLocalSize(iscol,&csize);
3326: MatCreateSubMatrix_MPIAIJ_nonscalable(mat,isrow,iscol_local,csize,call,newmat);
3328: if (call == MAT_INITIAL_MATRIX) {
3329: PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);
3330: ISDestroy(&iscol_local);
3331: }
3332: return(0);
3333: }
3335: /*@C
3336: MatCreateMPIAIJWithSeqAIJ - creates a MPIAIJ matrix using SeqAIJ matrices that contain the "diagonal"
3337: and "off-diagonal" part of the matrix in CSR format.
3339: Collective
3341: Input Parameters:
3342: + comm - MPI communicator
3343: . A - "diagonal" portion of matrix
3344: . B - "off-diagonal" portion of matrix, may have empty columns, will be destroyed by this routine
3345: - garray - global index of B columns
3347: Output Parameter:
3348: . mat - the matrix, with input A as its local diagonal matrix
3349: Level: advanced
3351: Notes:
3352: See MatCreateAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix.
3353: A becomes part of output mat, B is destroyed by this routine. The user cannot use A and B anymore.
3355: .seealso: MatCreateMPIAIJWithSplitArrays()
3356: @*/
3357: PetscErrorCode MatCreateMPIAIJWithSeqAIJ(MPI_Comm comm,Mat A,Mat B,const PetscInt garray[],Mat *mat)
3358: {
3360: Mat_MPIAIJ *maij;
3361: Mat_SeqAIJ *b=(Mat_SeqAIJ*)B->data,*bnew;
3362: PetscInt *oi=b->i,*oj=b->j,i,nz,col;
3363: PetscScalar *oa=b->a;
3364: Mat Bnew;
3365: PetscInt m,n,N;
3368: MatCreate(comm,mat);
3369: MatGetSize(A,&m,&n);
3370: if (m != B->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Am %D != Bm %D",m,B->rmap->N);
3371: if (A->rmap->bs != B->rmap->bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"A row bs %D != B row bs %D",A->rmap->bs,B->rmap->bs);
3372: /* remove check below; When B is created using iscol_o from ISGetSeqIS_SameColDist_Private(), its bs may not be same as A */
3373: /* if (A->cmap->bs != B->cmap->bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"A column bs %D != B column bs %D",A->cmap->bs,B->cmap->bs); */
3375: /* Get global columns of mat */
3376: MPIU_Allreduce(&n,&N,1,MPIU_INT,MPI_SUM,comm);
3378: MatSetSizes(*mat,m,n,PETSC_DECIDE,N);
3379: MatSetType(*mat,MATMPIAIJ);
3380: MatSetBlockSizes(*mat,A->rmap->bs,A->cmap->bs);
3381: maij = (Mat_MPIAIJ*)(*mat)->data;
3383: (*mat)->preallocated = PETSC_TRUE;
3385: PetscLayoutSetUp((*mat)->rmap);
3386: PetscLayoutSetUp((*mat)->cmap);
3388: /* Set A as diagonal portion of *mat */
3389: maij->A = A;
3391: nz = oi[m];
3392: for (i=0; i<nz; i++) {
3393: col = oj[i];
3394: oj[i] = garray[col];
3395: }
3397: /* Set Bnew as off-diagonal portion of *mat */
3398: MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,N,oi,oj,oa,&Bnew);
3399: bnew = (Mat_SeqAIJ*)Bnew->data;
3400: bnew->maxnz = b->maxnz; /* allocated nonzeros of B */
3401: maij->B = Bnew;
3403: if (B->rmap->N != Bnew->rmap->N) SETERRQ2(PETSC_COMM_SELF,0,"BN %d != BnewN %d",B->rmap->N,Bnew->rmap->N);
3405: b->singlemalloc = PETSC_FALSE; /* B arrays are shared by Bnew */
3406: b->free_a = PETSC_FALSE;
3407: b->free_ij = PETSC_FALSE;
3408: MatDestroy(&B);
3410: bnew->singlemalloc = PETSC_TRUE; /* arrays will be freed by MatDestroy(&Bnew) */
3411: bnew->free_a = PETSC_TRUE;
3412: bnew->free_ij = PETSC_TRUE;
3414: /* condense columns of maij->B */
3415: MatSetOption(*mat,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE);
3416: MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
3417: MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
3418: MatSetOption(*mat,MAT_NO_OFF_PROC_ENTRIES,PETSC_FALSE);
3419: MatSetOption(*mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3420: return(0);
3421: }
3423: extern PetscErrorCode MatCreateSubMatrices_MPIAIJ_SingleIS_Local(Mat,PetscInt,const IS[],const IS[],MatReuse,PetscBool,Mat*);
3425: PetscErrorCode MatCreateSubMatrix_MPIAIJ_SameRowDist(Mat mat,IS isrow,IS iscol,IS iscol_local,MatReuse call,Mat *newmat)
3426: {
3428: PetscInt i,m,n,rstart,row,rend,nz,j,bs,cbs;
3429: PetscInt *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
3430: Mat_MPIAIJ *a=(Mat_MPIAIJ*)mat->data;
3431: Mat M,Msub,B=a->B;
3432: MatScalar *aa;
3433: Mat_SeqAIJ *aij;
3434: PetscInt *garray = a->garray,*colsub,Ncols;
3435: PetscInt count,Bn=B->cmap->N,cstart=mat->cmap->rstart,cend=mat->cmap->rend;
3436: IS iscol_sub,iscmap;
3437: const PetscInt *is_idx,*cmap;
3438: PetscBool allcolumns=PETSC_FALSE;
3439: MPI_Comm comm;
3442: PetscObjectGetComm((PetscObject)mat,&comm);
3444: if (call == MAT_REUSE_MATRIX) {
3445: PetscObjectQuery((PetscObject)*newmat,"SubIScol",(PetscObject*)&iscol_sub);
3446: if (!iscol_sub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SubIScol passed in was not used before, cannot reuse");
3447: ISGetLocalSize(iscol_sub,&count);
3449: PetscObjectQuery((PetscObject)*newmat,"Subcmap",(PetscObject*)&iscmap);
3450: if (!iscmap) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Subcmap passed in was not used before, cannot reuse");
3452: PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Msub);
3453: if (!Msub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
3455: MatCreateSubMatrices_MPIAIJ_SingleIS_Local(mat,1,&isrow,&iscol_sub,MAT_REUSE_MATRIX,PETSC_FALSE,&Msub);
3457: } else { /* call == MAT_INITIAL_MATRIX) */
3458: PetscBool flg;
3460: ISGetLocalSize(iscol,&n);
3461: ISGetSize(iscol,&Ncols);
3463: /* (1) iscol -> nonscalable iscol_local */
3464: /* Check for special case: each processor gets entire matrix columns */
3465: ISIdentity(iscol_local,&flg);
3466: if (flg && n == mat->cmap->N) allcolumns = PETSC_TRUE;
3467: if (allcolumns) {
3468: iscol_sub = iscol_local;
3469: PetscObjectReference((PetscObject)iscol_local);
3470: ISCreateStride(PETSC_COMM_SELF,n,0,1,&iscmap);
3472: } else {
3473: /* (2) iscol_local -> iscol_sub and iscmap. Implementation below requires iscol_local be sorted, it can have duplicate indices */
3474: PetscInt *idx,*cmap1,k;
3475: PetscMalloc1(Ncols,&idx);
3476: PetscMalloc1(Ncols,&cmap1);
3477: ISGetIndices(iscol_local,&is_idx);
3478: count = 0;
3479: k = 0;
3480: for (i=0; i<Ncols; i++) {
3481: j = is_idx[i];
3482: if (j >= cstart && j < cend) {
3483: /* diagonal part of mat */
3484: idx[count] = j;
3485: cmap1[count++] = i; /* column index in submat */
3486: } else if (Bn) {
3487: /* off-diagonal part of mat */
3488: if (j == garray[k]) {
3489: idx[count] = j;
3490: cmap1[count++] = i; /* column index in submat */
3491: } else if (j > garray[k]) {
3492: while (j > garray[k] && k < Bn-1) k++;
3493: if (j == garray[k]) {
3494: idx[count] = j;
3495: cmap1[count++] = i; /* column index in submat */
3496: }
3497: }
3498: }
3499: }
3500: ISRestoreIndices(iscol_local,&is_idx);
3502: ISCreateGeneral(PETSC_COMM_SELF,count,idx,PETSC_OWN_POINTER,&iscol_sub);
3503: ISGetBlockSize(iscol,&cbs);
3504: ISSetBlockSize(iscol_sub,cbs);
3506: ISCreateGeneral(PetscObjectComm((PetscObject)iscol_local),count,cmap1,PETSC_OWN_POINTER,&iscmap);
3507: }
3509: /* (3) Create sequential Msub */
3510: MatCreateSubMatrices_MPIAIJ_SingleIS_Local(mat,1,&isrow,&iscol_sub,MAT_INITIAL_MATRIX,allcolumns,&Msub);
3511: }
3513: ISGetLocalSize(iscol_sub,&count);
3514: aij = (Mat_SeqAIJ*)(Msub)->data;
3515: ii = aij->i;
3516: ISGetIndices(iscmap,&cmap);
3518: /*
3519: m - number of local rows
3520: Ncols - number of columns (same on all processors)
3521: rstart - first row in new global matrix generated
3522: */
3523: MatGetSize(Msub,&m,NULL);
3525: if (call == MAT_INITIAL_MATRIX) {
3526: /* (4) Create parallel newmat */
3527: PetscMPIInt rank,size;
3528: PetscInt csize;
3530: MPI_Comm_size(comm,&size);
3531: MPI_Comm_rank(comm,&rank);
3533: /*
3534: Determine the number of non-zeros in the diagonal and off-diagonal
3535: portions of the matrix in order to do correct preallocation
3536: */
3538: /* first get start and end of "diagonal" columns */
3539: ISGetLocalSize(iscol,&csize);
3540: if (csize == PETSC_DECIDE) {
3541: ISGetSize(isrow,&mglobal);
3542: if (mglobal == Ncols) { /* square matrix */
3543: nlocal = m;
3544: } else {
3545: nlocal = Ncols/size + ((Ncols % size) > rank);
3546: }
3547: } else {
3548: nlocal = csize;
3549: }
3550: MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);
3551: rstart = rend - nlocal;
3552: if (rank == size - 1 && rend != Ncols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local column sizes %D do not add up to total number of columns %D",rend,Ncols);
3554: /* next, compute all the lengths */
3555: jj = aij->j;
3556: PetscMalloc1(2*m+1,&dlens);
3557: olens = dlens + m;
3558: for (i=0; i<m; i++) {
3559: jend = ii[i+1] - ii[i];
3560: olen = 0;
3561: dlen = 0;
3562: for (j=0; j<jend; j++) {
3563: if (cmap[*jj] < rstart || cmap[*jj] >= rend) olen++;
3564: else dlen++;
3565: jj++;
3566: }
3567: olens[i] = olen;
3568: dlens[i] = dlen;
3569: }
3571: ISGetBlockSize(isrow,&bs);
3572: ISGetBlockSize(iscol,&cbs);
3574: MatCreate(comm,&M);
3575: MatSetSizes(M,m,nlocal,PETSC_DECIDE,Ncols);
3576: MatSetBlockSizes(M,bs,cbs);
3577: MatSetType(M,((PetscObject)mat)->type_name);
3578: MatMPIAIJSetPreallocation(M,0,dlens,0,olens);
3579: PetscFree(dlens);
3581: } else { /* call == MAT_REUSE_MATRIX */
3582: M = *newmat;
3583: MatGetLocalSize(M,&i,NULL);
3584: if (i != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
3585: MatZeroEntries(M);
3586: /*
3587: The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
3588: rather than the slower MatSetValues().
3589: */
3590: M->was_assembled = PETSC_TRUE;
3591: M->assembled = PETSC_FALSE;
3592: }
3594: /* (5) Set values of Msub to *newmat */
3595: PetscMalloc1(count,&colsub);
3596: MatGetOwnershipRange(M,&rstart,NULL);
3598: jj = aij->j;
3599: aa = aij->a;
3600: for (i=0; i<m; i++) {
3601: row = rstart + i;
3602: nz = ii[i+1] - ii[i];
3603: for (j=0; j<nz; j++) colsub[j] = cmap[jj[j]];
3604: MatSetValues_MPIAIJ(M,1,&row,nz,colsub,aa,INSERT_VALUES);
3605: jj += nz; aa += nz;
3606: }
3607: ISRestoreIndices(iscmap,&cmap);
3609: MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
3610: MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);
3612: PetscFree(colsub);
3614: /* save Msub, iscol_sub and iscmap used in processor for next request */
3615: if (call == MAT_INITIAL_MATRIX) {
3616: *newmat = M;
3617: PetscObjectCompose((PetscObject)(*newmat),"SubMatrix",(PetscObject)Msub);
3618: MatDestroy(&Msub);
3620: PetscObjectCompose((PetscObject)(*newmat),"SubIScol",(PetscObject)iscol_sub);
3621: ISDestroy(&iscol_sub);
3623: PetscObjectCompose((PetscObject)(*newmat),"Subcmap",(PetscObject)iscmap);
3624: ISDestroy(&iscmap);
3626: if (iscol_local) {
3627: PetscObjectCompose((PetscObject)(*newmat),"ISAllGather",(PetscObject)iscol_local);
3628: ISDestroy(&iscol_local);
3629: }
3630: }
3631: return(0);
3632: }
3634: /*
3635: Not great since it makes two copies of the submatrix, first an SeqAIJ
3636: in local and then by concatenating the local matrices the end result.
3637: Writing it directly would be much like MatCreateSubMatrices_MPIAIJ()
3639: Note: This requires a sequential iscol with all indices.
3640: */
3641: PetscErrorCode MatCreateSubMatrix_MPIAIJ_nonscalable(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
3642: {
3644: PetscMPIInt rank,size;
3645: PetscInt i,m,n,rstart,row,rend,nz,*cwork,j,bs,cbs;
3646: PetscInt *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
3647: Mat M,Mreuse;
3648: MatScalar *aa,*vwork;
3649: MPI_Comm comm;
3650: Mat_SeqAIJ *aij;
3651: PetscBool colflag,allcolumns=PETSC_FALSE;
3654: PetscObjectGetComm((PetscObject)mat,&comm);
3655: MPI_Comm_rank(comm,&rank);
3656: MPI_Comm_size(comm,&size);
3658: /* Check for special case: each processor gets entire matrix columns */
3659: ISIdentity(iscol,&colflag);
3660: ISGetLocalSize(iscol,&n);
3661: if (colflag && n == mat->cmap->N) allcolumns = PETSC_TRUE;
3663: if (call == MAT_REUSE_MATRIX) {
3664: PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Mreuse);
3665: if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
3666: MatCreateSubMatrices_MPIAIJ_SingleIS_Local(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,allcolumns,&Mreuse);
3667: } else {
3668: MatCreateSubMatrices_MPIAIJ_SingleIS_Local(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,allcolumns,&Mreuse);
3669: }
3671: /*
3672: m - number of local rows
3673: n - number of columns (same on all processors)
3674: rstart - first row in new global matrix generated
3675: */
3676: MatGetSize(Mreuse,&m,&n);
3677: MatGetBlockSizes(Mreuse,&bs,&cbs);
3678: if (call == MAT_INITIAL_MATRIX) {
3679: aij = (Mat_SeqAIJ*)(Mreuse)->data;
3680: ii = aij->i;
3681: jj = aij->j;
3683: /*
3684: Determine the number of non-zeros in the diagonal and off-diagonal
3685: portions of the matrix in order to do correct preallocation
3686: */
3688: /* first get start and end of "diagonal" columns */
3689: if (csize == PETSC_DECIDE) {
3690: ISGetSize(isrow,&mglobal);
3691: if (mglobal == n) { /* square matrix */
3692: nlocal = m;
3693: } else {
3694: nlocal = n/size + ((n % size) > rank);
3695: }
3696: } else {
3697: nlocal = csize;
3698: }
3699: MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);
3700: rstart = rend - nlocal;
3701: if (rank == size - 1 && rend != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local column sizes %D do not add up to total number of columns %D",rend,n);
3703: /* next, compute all the lengths */
3704: PetscMalloc1(2*m+1,&dlens);
3705: olens = dlens + m;
3706: for (i=0; i<m; i++) {
3707: jend = ii[i+1] - ii[i];
3708: olen = 0;
3709: dlen = 0;
3710: for (j=0; j<jend; j++) {
3711: if (*jj < rstart || *jj >= rend) olen++;
3712: else dlen++;
3713: jj++;
3714: }
3715: olens[i] = olen;
3716: dlens[i] = dlen;
3717: }
3718: MatCreate(comm,&M);
3719: MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);
3720: MatSetBlockSizes(M,bs,cbs);
3721: MatSetType(M,((PetscObject)mat)->type_name);
3722: MatMPIAIJSetPreallocation(M,0,dlens,0,olens);
3723: PetscFree(dlens);
3724: } else {
3725: PetscInt ml,nl;
3727: M = *newmat;
3728: MatGetLocalSize(M,&ml,&nl);
3729: if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
3730: MatZeroEntries(M);
3731: /*
3732: The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
3733: rather than the slower MatSetValues().
3734: */
3735: M->was_assembled = PETSC_TRUE;
3736: M->assembled = PETSC_FALSE;
3737: }
3738: MatGetOwnershipRange(M,&rstart,&rend);
3739: aij = (Mat_SeqAIJ*)(Mreuse)->data;
3740: ii = aij->i;
3741: jj = aij->j;
3742: aa = aij->a;
3743: for (i=0; i<m; i++) {
3744: row = rstart + i;
3745: nz = ii[i+1] - ii[i];
3746: cwork = jj; jj += nz;
3747: vwork = aa; aa += nz;
3748: MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);
3749: }
3751: MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
3752: MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);
3753: *newmat = M;
3755: /* save submatrix used in processor for next request */
3756: if (call == MAT_INITIAL_MATRIX) {
3757: PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);
3758: MatDestroy(&Mreuse);
3759: }
3760: return(0);
3761: }
3763: PetscErrorCode MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3764: {
3765: PetscInt m,cstart, cend,j,nnz,i,d;
3766: PetscInt *d_nnz,*o_nnz,nnz_max = 0,rstart,ii;
3767: const PetscInt *JJ;
3769: PetscBool nooffprocentries;
3772: if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]);
3774: PetscLayoutSetUp(B->rmap);
3775: PetscLayoutSetUp(B->cmap);
3776: m = B->rmap->n;
3777: cstart = B->cmap->rstart;
3778: cend = B->cmap->rend;
3779: rstart = B->rmap->rstart;
3781: PetscCalloc2(m,&d_nnz,m,&o_nnz);
3783: #if defined(PETSC_USE_DEBUG)
3784: for (i=0; i<m; i++) {
3785: nnz = Ii[i+1]- Ii[i];
3786: JJ = J + Ii[i];
3787: if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz);
3788: if (nnz && (JJ[0] < 0)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,JJ[0]);
3789: if (nnz && (JJ[nnz-1] >= B->cmap->N)) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Row %D ends with too large a column index %D (max allowed %D)",i,JJ[nnz-1],B->cmap->N);
3790: }
3791: #endif
3793: for (i=0; i<m; i++) {
3794: nnz = Ii[i+1]- Ii[i];
3795: JJ = J + Ii[i];
3796: nnz_max = PetscMax(nnz_max,nnz);
3797: d = 0;
3798: for (j=0; j<nnz; j++) {
3799: if (cstart <= JJ[j] && JJ[j] < cend) d++;
3800: }
3801: d_nnz[i] = d;
3802: o_nnz[i] = nnz - d;
3803: }
3804: MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);
3805: PetscFree2(d_nnz,o_nnz);
3807: for (i=0; i<m; i++) {
3808: ii = i + rstart;
3809: MatSetValues_MPIAIJ(B,1,&ii,Ii[i+1] - Ii[i],J+Ii[i], v ? v + Ii[i] : NULL,INSERT_VALUES);
3810: }
3811: nooffprocentries = B->nooffprocentries;
3812: B->nooffprocentries = PETSC_TRUE;
3813: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
3814: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
3815: B->nooffprocentries = nooffprocentries;
3817: MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3818: return(0);
3819: }
3821: /*@
3822: MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format
3823: (the default parallel PETSc format).
3825: Collective
3827: Input Parameters:
3828: + B - the matrix
3829: . i - the indices into j for the start of each local row (starts with zero)
3830: . j - the column indices for each local row (starts with zero)
3831: - v - optional values in the matrix
3833: Level: developer
3835: Notes:
3836: The i, j, and v arrays ARE copied by this routine into the internal format used by PETSc;
3837: thus you CANNOT change the matrix entries by changing the values of v[] after you have
3838: called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.
3840: The i and j indices are 0 based, and i indices are indices corresponding to the local j array.
3842: The format which is used for the sparse matrix input, is equivalent to a
3843: row-major ordering.. i.e for the following matrix, the input data expected is
3844: as shown
3846: $ 1 0 0
3847: $ 2 0 3 P0
3848: $ -------
3849: $ 4 5 6 P1
3850: $
3851: $ Process0 [P0]: rows_owned=[0,1]
3852: $ i = {0,1,3} [size = nrow+1 = 2+1]
3853: $ j = {0,0,2} [size = 3]
3854: $ v = {1,2,3} [size = 3]
3855: $
3856: $ Process1 [P1]: rows_owned=[2]
3857: $ i = {0,3} [size = nrow+1 = 1+1]
3858: $ j = {0,1,2} [size = 3]
3859: $ v = {4,5,6} [size = 3]
3861: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateAIJ(), MATMPIAIJ,
3862: MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays()
3863: @*/
3864: PetscErrorCode MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
3865: {
3869: PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));
3870: return(0);
3871: }
3873: /*@C
3874: MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format
3875: (the default parallel PETSc format). For good matrix assembly performance
3876: the user should preallocate the matrix storage by setting the parameters
3877: d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately,
3878: performance can be increased by more than a factor of 50.
3880: Collective
3882: Input Parameters:
3883: + B - the matrix
3884: . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix
3885: (same value is used for all local rows)
3886: . d_nnz - array containing the number of nonzeros in the various rows of the
3887: DIAGONAL portion of the local submatrix (possibly different for each row)
3888: or NULL (PETSC_NULL_INTEGER in Fortran), if d_nz is used to specify the nonzero structure.
3889: The size of this array is equal to the number of local rows, i.e 'm'.
3890: For matrices that will be factored, you must leave room for (and set)
3891: the diagonal entry even if it is zero.
3892: . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local
3893: submatrix (same value is used for all local rows).
3894: - o_nnz - array containing the number of nonzeros in the various rows of the
3895: OFF-DIAGONAL portion of the local submatrix (possibly different for
3896: each row) or NULL (PETSC_NULL_INTEGER in Fortran), if o_nz is used to specify the nonzero
3897: structure. The size of this array is equal to the number
3898: of local rows, i.e 'm'.
3900: If the *_nnz parameter is given then the *_nz parameter is ignored
3902: The AIJ format (also called the Yale sparse matrix format or
3903: compressed row storage (CSR)), is fully compatible with standard Fortran 77
3904: storage. The stored row and column indices begin with zero.
3905: See Users-Manual: ch_mat for details.
3907: The parallel matrix is partitioned such that the first m0 rows belong to
3908: process 0, the next m1 rows belong to process 1, the next m2 rows belong
3909: to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
3911: The DIAGONAL portion of the local submatrix of a processor can be defined
3912: as the submatrix which is obtained by extraction the part corresponding to
3913: the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the
3914: first row that belongs to the processor, r2 is the last row belonging to
3915: the this processor, and c1-c2 is range of indices of the local part of a
3916: vector suitable for applying the matrix to. This is an mxn matrix. In the
3917: common case of a square matrix, the row and column ranges are the same and
3918: the DIAGONAL part is also square. The remaining portion of the local
3919: submatrix (mxN) constitute the OFF-DIAGONAL portion.
3921: If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
3923: You can call MatGetInfo() to get information on how effective the preallocation was;
3924: for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3925: You can also run with the option -info and look for messages with the string
3926: malloc in them to see if additional memory allocation was needed.
3928: Example usage:
3930: Consider the following 8x8 matrix with 34 non-zero values, that is
3931: assembled across 3 processors. Lets assume that proc0 owns 3 rows,
3932: proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
3933: as follows:
3935: .vb
3936: 1 2 0 | 0 3 0 | 0 4
3937: Proc0 0 5 6 | 7 0 0 | 8 0
3938: 9 0 10 | 11 0 0 | 12 0
3939: -------------------------------------
3940: 13 0 14 | 15 16 17 | 0 0
3941: Proc1 0 18 0 | 19 20 21 | 0 0
3942: 0 0 0 | 22 23 0 | 24 0
3943: -------------------------------------
3944: Proc2 25 26 27 | 0 0 28 | 29 0
3945: 30 0 0 | 31 32 33 | 0 34
3946: .ve
3948: This can be represented as a collection of submatrices as:
3950: .vb
3951: A B C
3952: D E F
3953: G H I
3954: .ve
3956: Where the submatrices A,B,C are owned by proc0, D,E,F are
3957: owned by proc1, G,H,I are owned by proc2.
3959: The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
3960: The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
3961: The 'M','N' parameters are 8,8, and have the same values on all procs.
3963: The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
3964: submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
3965: corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
3966: Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
3967: part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
3968: matrix, ans [DF] as another SeqAIJ matrix.
3970: When d_nz, o_nz parameters are specified, d_nz storage elements are
3971: allocated for every row of the local diagonal submatrix, and o_nz
3972: storage locations are allocated for every row of the OFF-DIAGONAL submat.
3973: One way to choose d_nz and o_nz is to use the max nonzerors per local
3974: rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
3975: In this case, the values of d_nz,o_nz are:
3976: .vb
3977: proc0 : dnz = 2, o_nz = 2
3978: proc1 : dnz = 3, o_nz = 2
3979: proc2 : dnz = 1, o_nz = 4
3980: .ve
3981: We are allocating m*(d_nz+o_nz) storage locations for every proc. This
3982: translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
3983: for proc3. i.e we are using 12+15+10=37 storage locations to store
3984: 34 values.
3986: When d_nnz, o_nnz parameters are specified, the storage is specified
3987: for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
3988: In the above case the values for d_nnz,o_nnz are:
3989: .vb
3990: proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
3991: proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
3992: proc2: d_nnz = [1,1] and o_nnz = [4,4]
3993: .ve
3994: Here the space allocated is sum of all the above values i.e 34, and
3995: hence pre-allocation is perfect.
3997: Level: intermediate
3999: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateAIJ(), MatMPIAIJSetPreallocationCSR(),
4000: MATMPIAIJ, MatGetInfo(), PetscSplitOwnership()
4001: @*/
4002: PetscErrorCode MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
4003: {
4009: PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));
4010: return(0);
4011: }
4013: /*@
4014: MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard
4015: CSR format for the local rows.
4017: Collective
4019: Input Parameters:
4020: + comm - MPI communicator
4021: . m - number of local rows (Cannot be PETSC_DECIDE)
4022: . n - This value should be the same as the local size used in creating the
4023: x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4024: calculated if N is given) For square matrices n is almost always m.
4025: . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4026: . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4027: . i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
4028: . j - column indices
4029: - a - matrix values
4031: Output Parameter:
4032: . mat - the matrix
4034: Level: intermediate
4036: Notes:
4037: The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
4038: thus you CANNOT change the matrix entries by changing the values of a[] after you have
4039: called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.
4041: The i and j indices are 0 based, and i indices are indices corresponding to the local j array.
4043: The format which is used for the sparse matrix input, is equivalent to a
4044: row-major ordering.. i.e for the following matrix, the input data expected is
4045: as shown
4047: Once you have created the matrix you can update it with new numerical values using MatUpdateMPIAIJWithArrays
4049: $ 1 0 0
4050: $ 2 0 3 P0
4051: $ -------
4052: $ 4 5 6 P1
4053: $
4054: $ Process0 [P0]: rows_owned=[0,1]
4055: $ i = {0,1,3} [size = nrow+1 = 2+1]
4056: $ j = {0,0,2} [size = 3]
4057: $ v = {1,2,3} [size = 3]
4058: $
4059: $ Process1 [P1]: rows_owned=[2]
4060: $ i = {0,3} [size = nrow+1 = 1+1]
4061: $ j = {0,1,2} [size = 3]
4062: $ v = {4,5,6} [size = 3]
4064: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
4065: MATMPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays(), MatUpdateMPIAIJWithArrays()
4066: @*/
4067: PetscErrorCode MatCreateMPIAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat)
4068: {
4072: if (i && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4073: if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
4074: MatCreate(comm,mat);
4075: MatSetSizes(*mat,m,n,M,N);
4076: /* MatSetBlockSizes(M,bs,cbs); */
4077: MatSetType(*mat,MATMPIAIJ);
4078: MatMPIAIJSetPreallocationCSR(*mat,i,j,a);
4079: return(0);
4080: }
4082: /*@
4083: MatUpdateMPIAIJWithArrays - updates a MPI AIJ matrix using arrays that contain in standard
4084: CSR format for the local rows. Only the numerical values are updated the other arrays must be identical
4086: Collective
4088: Input Parameters:
4089: + mat - the matrix
4090: . m - number of local rows (Cannot be PETSC_DECIDE)
4091: . n - This value should be the same as the local size used in creating the
4092: x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4093: calculated if N is given) For square matrices n is almost always m.
4094: . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4095: . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4096: . Ii - row indices; that is Ii[0] = 0, Ii[row] = Ii[row-1] + number of elements in that row of the matrix
4097: . J - column indices
4098: - v - matrix values
4100: Level: intermediate
4102: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
4103: MATMPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays(), MatUpdateMPIAIJWithArrays()
4104: @*/
4105: PetscErrorCode MatUpdateMPIAIJWithArrays(Mat mat,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
4106: {
4108: PetscInt cstart,nnz,i,j;
4109: PetscInt *ld;
4110: PetscBool nooffprocentries;
4111: Mat_MPIAIJ *Aij = (Mat_MPIAIJ*)mat->data;
4112: Mat_SeqAIJ *Ad = (Mat_SeqAIJ*)Aij->A->data, *Ao = (Mat_SeqAIJ*)Aij->B->data;
4113: PetscScalar *ad = Ad->a, *ao = Ao->a;
4114: const PetscInt *Adi = Ad->i;
4115: PetscInt ldi,Iii,md;
4118: if (Ii[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4119: if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
4120: if (m != mat->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Local number of rows cannot change from call to MatUpdateMPIAIJWithArrays()");
4121: if (n != mat->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Local number of columns cannot change from call to MatUpdateMPIAIJWithArrays()");
4123: cstart = mat->cmap->rstart;
4124: if (!Aij->ld) {
4125: /* count number of entries below block diagonal */
4126: PetscCalloc1(m,&ld);
4127: Aij->ld = ld;
4128: for (i=0; i<m; i++) {
4129: nnz = Ii[i+1]- Ii[i];
4130: j = 0;
4131: while (J[j] < cstart && j < nnz) {j++;}
4132: J += nnz;
4133: ld[i] = j;
4134: }
4135: } else {
4136: ld = Aij->ld;
4137: }
4139: for (i=0; i<m; i++) {
4140: nnz = Ii[i+1]- Ii[i];
4141: Iii = Ii[i];
4142: ldi = ld[i];
4143: md = Adi[i+1]-Adi[i];
4144: PetscArraycpy(ao,v + Iii,ldi);
4145: PetscArraycpy(ad,v + Iii + ldi,md);
4146: PetscArraycpy(ao + ldi,v + Iii + ldi + md,nnz - ldi - md);
4147: ad += md;
4148: ao += nnz - md;
4149: }
4150: nooffprocentries = mat->nooffprocentries;
4151: mat->nooffprocentries = PETSC_TRUE;
4152: PetscObjectStateIncrease((PetscObject)Aij->A);
4153: PetscObjectStateIncrease((PetscObject)Aij->B);
4154: PetscObjectStateIncrease((PetscObject)mat);
4155: MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
4156: MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
4157: mat->nooffprocentries = nooffprocentries;
4158: return(0);
4159: }
4161: /*@C
4162: MatCreateAIJ - Creates a sparse parallel matrix in AIJ format
4163: (the default parallel PETSc format). For good matrix assembly performance
4164: the user should preallocate the matrix storage by setting the parameters
4165: d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately,
4166: performance can be increased by more than a factor of 50.
4168: Collective
4170: Input Parameters:
4171: + comm - MPI communicator
4172: . m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
4173: This value should be the same as the local size used in creating the
4174: y vector for the matrix-vector product y = Ax.
4175: . n - This value should be the same as the local size used in creating the
4176: x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
4177: calculated if N is given) For square matrices n is almost always m.
4178: . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
4179: . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
4180: . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix
4181: (same value is used for all local rows)
4182: . d_nnz - array containing the number of nonzeros in the various rows of the
4183: DIAGONAL portion of the local submatrix (possibly different for each row)
4184: or NULL, if d_nz is used to specify the nonzero structure.
4185: The size of this array is equal to the number of local rows, i.e 'm'.
4186: . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local
4187: submatrix (same value is used for all local rows).
4188: - o_nnz - array containing the number of nonzeros in the various rows of the
4189: OFF-DIAGONAL portion of the local submatrix (possibly different for
4190: each row) or NULL, if o_nz is used to specify the nonzero
4191: structure. The size of this array is equal to the number
4192: of local rows, i.e 'm'.
4194: Output Parameter:
4195: . A - the matrix
4197: It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
4198: MatXXXXSetPreallocation() paradigm instead of this routine directly.
4199: [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
4201: Notes:
4202: If the *_nnz parameter is given then the *_nz parameter is ignored
4204: m,n,M,N parameters specify the size of the matrix, and its partitioning across
4205: processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
4206: storage requirements for this matrix.
4208: If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one
4209: processor than it must be used on all processors that share the object for
4210: that argument.
4212: The user MUST specify either the local or global matrix dimensions
4213: (possibly both).
4215: The parallel matrix is partitioned across processors such that the
4216: first m0 rows belong to process 0, the next m1 rows belong to
4217: process 1, the next m2 rows belong to process 2 etc.. where
4218: m0,m1,m2,.. are the input parameter 'm'. i.e each processor stores
4219: values corresponding to [m x N] submatrix.
4221: The columns are logically partitioned with the n0 columns belonging
4222: to 0th partition, the next n1 columns belonging to the next
4223: partition etc.. where n0,n1,n2... are the input parameter 'n'.
4225: The DIAGONAL portion of the local submatrix on any given processor
4226: is the submatrix corresponding to the rows and columns m,n
4227: corresponding to the given processor. i.e diagonal matrix on
4228: process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1]
4229: etc. The remaining portion of the local submatrix [m x (N-n)]
4230: constitute the OFF-DIAGONAL portion. The example below better
4231: illustrates this concept.
4233: For a square global matrix we define each processor's diagonal portion
4234: to be its local rows and the corresponding columns (a square submatrix);
4235: each processor's off-diagonal portion encompasses the remainder of the
4236: local matrix (a rectangular submatrix).
4238: If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
4240: When calling this routine with a single process communicator, a matrix of
4241: type SEQAIJ is returned. If a matrix of type MPIAIJ is desired for this
4242: type of communicator, use the construction mechanism
4243: .vb
4244: MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...);
4245: .ve
4247: $ MatCreate(...,&A);
4248: $ MatSetType(A,MATMPIAIJ);
4249: $ MatSetSizes(A, m,n,M,N);
4250: $ MatMPIAIJSetPreallocation(A,...);
4252: By default, this format uses inodes (identical nodes) when possible.
4253: We search for consecutive rows with the same nonzero structure, thereby
4254: reusing matrix information to achieve increased efficiency.
4256: Options Database Keys:
4257: + -mat_no_inode - Do not use inodes
4258: - -mat_inode_limit <limit> - Sets inode limit (max limit=5)
4262: Example usage:
4264: Consider the following 8x8 matrix with 34 non-zero values, that is
4265: assembled across 3 processors. Lets assume that proc0 owns 3 rows,
4266: proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
4267: as follows
4269: .vb
4270: 1 2 0 | 0 3 0 | 0 4
4271: Proc0 0 5 6 | 7 0 0 | 8 0
4272: 9 0 10 | 11 0 0 | 12 0
4273: -------------------------------------
4274: 13 0 14 | 15 16 17 | 0 0
4275: Proc1 0 18 0 | 19 20 21 | 0 0
4276: 0 0 0 | 22 23 0 | 24 0
4277: -------------------------------------
4278: Proc2 25 26 27 | 0 0 28 | 29 0
4279: 30 0 0 | 31 32 33 | 0 34
4280: .ve
4282: This can be represented as a collection of submatrices as
4284: .vb
4285: A B C
4286: D E F
4287: G H I
4288: .ve
4290: Where the submatrices A,B,C are owned by proc0, D,E,F are
4291: owned by proc1, G,H,I are owned by proc2.
4293: The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
4294: The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
4295: The 'M','N' parameters are 8,8, and have the same values on all procs.
4297: The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
4298: submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
4299: corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
4300: Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
4301: part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
4302: matrix, ans [DF] as another SeqAIJ matrix.
4304: When d_nz, o_nz parameters are specified, d_nz storage elements are
4305: allocated for every row of the local diagonal submatrix, and o_nz
4306: storage locations are allocated for every row of the OFF-DIAGONAL submat.
4307: One way to choose d_nz and o_nz is to use the max nonzerors per local
4308: rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
4309: In this case, the values of d_nz,o_nz are
4310: .vb
4311: proc0 : dnz = 2, o_nz = 2
4312: proc1 : dnz = 3, o_nz = 2
4313: proc2 : dnz = 1, o_nz = 4
4314: .ve
4315: We are allocating m*(d_nz+o_nz) storage locations for every proc. This
4316: translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
4317: for proc3. i.e we are using 12+15+10=37 storage locations to store
4318: 34 values.
4320: When d_nnz, o_nnz parameters are specified, the storage is specified
4321: for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
4322: In the above case the values for d_nnz,o_nnz are
4323: .vb
4324: proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
4325: proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
4326: proc2: d_nnz = [1,1] and o_nnz = [4,4]
4327: .ve
4328: Here the space allocated is sum of all the above values i.e 34, and
4329: hence pre-allocation is perfect.
4331: Level: intermediate
4333: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
4334: MATMPIAIJ, MatCreateMPIAIJWithArrays()
4335: @*/
4336: PetscErrorCode MatCreateAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[],Mat *A)
4337: {
4339: PetscMPIInt size;
4342: MatCreate(comm,A);
4343: MatSetSizes(*A,m,n,M,N);
4344: MPI_Comm_size(comm,&size);
4345: if (size > 1) {
4346: MatSetType(*A,MATMPIAIJ);
4347: MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);
4348: } else {
4349: MatSetType(*A,MATSEQAIJ);
4350: MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);
4351: }
4352: return(0);
4353: }
4355: /*@C
4356: MatMPIAIJGetSeqAIJ - Returns the local piece of this distributed matrix
4357:
4358: Not collective
4359:
4360: Input Parameter:
4361: . A - The MPIAIJ matrix
4363: Output Parameters:
4364: + Ad - The local diagonal block as a SeqAIJ matrix
4365: . Ao - The local off-diagonal block as a SeqAIJ matrix
4366: - colmap - An array mapping local column numbers of Ao to global column numbers of the parallel matrix
4368: Note: The rows in Ad and Ao are in [0, Nr), where Nr is the number of local rows on this process. The columns
4369: in Ad are in [0, Nc) where Nc is the number of local columns. The columns are Ao are in [0, Nco), where Nco is
4370: the number of nonzero columns in the local off-diagonal piece of the matrix A. The array colmap maps these
4371: local column numbers to global column numbers in the original matrix.
4373: Level: intermediate
4375: .seealso: MatMPIAIJGetLocalMat(), MatMPIAIJGetLocalMatCondensed(), MatCreateAIJ(), MATMPIAIJ, MATSEQAIJ
4376: @*/
4377: PetscErrorCode MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,const PetscInt *colmap[])
4378: {
4379: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
4380: PetscBool flg;
4384: PetscStrbeginswith(((PetscObject)A)->type_name,MATMPIAIJ,&flg);
4385: if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"This function requires a MATMPIAIJ matrix as input");
4386: if (Ad) *Ad = a->A;
4387: if (Ao) *Ao = a->B;
4388: if (colmap) *colmap = a->garray;
4389: return(0);
4390: }
4392: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_MPIAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
4393: {
4395: PetscInt m,N,i,rstart,nnz,Ii;
4396: PetscInt *indx;
4397: PetscScalar *values;
4400: MatGetSize(inmat,&m,&N);
4401: if (scall == MAT_INITIAL_MATRIX) { /* symbolic phase */
4402: PetscInt *dnz,*onz,sum,bs,cbs;
4404: if (n == PETSC_DECIDE) {
4405: PetscSplitOwnership(comm,&n,&N);
4406: }
4407: /* Check sum(n) = N */
4408: MPIU_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);
4409: if (sum != N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns %D != global columns %D",sum,N);
4411: MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);
4412: rstart -= m;
4414: MatPreallocateInitialize(comm,m,n,dnz,onz);
4415: for (i=0; i<m; i++) {
4416: MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);
4417: MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);
4418: MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);
4419: }
4421: MatCreate(comm,outmat);
4422: MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
4423: MatGetBlockSizes(inmat,&bs,&cbs);
4424: MatSetBlockSizes(*outmat,bs,cbs);
4425: MatSetType(*outmat,MATAIJ);
4426: MatSeqAIJSetPreallocation(*outmat,0,dnz);
4427: MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);
4428: MatPreallocateFinalize(dnz,onz);
4429: }
4431: /* numeric phase */
4432: MatGetOwnershipRange(*outmat,&rstart,NULL);
4433: for (i=0; i<m; i++) {
4434: MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);
4435: Ii = i + rstart;
4436: MatSetValues(*outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);
4437: MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);
4438: }
4439: MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);
4440: MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);
4441: return(0);
4442: }
4444: PetscErrorCode MatFileSplit(Mat A,char *outfile)
4445: {
4446: PetscErrorCode ierr;
4447: PetscMPIInt rank;
4448: PetscInt m,N,i,rstart,nnz;
4449: size_t len;
4450: const PetscInt *indx;
4451: PetscViewer out;
4452: char *name;
4453: Mat B;
4454: const PetscScalar *values;
4457: MatGetLocalSize(A,&m,0);
4458: MatGetSize(A,0,&N);
4459: /* Should this be the type of the diagonal block of A? */
4460: MatCreate(PETSC_COMM_SELF,&B);
4461: MatSetSizes(B,m,N,m,N);
4462: MatSetBlockSizesFromMats(B,A,A);
4463: MatSetType(B,MATSEQAIJ);
4464: MatSeqAIJSetPreallocation(B,0,NULL);
4465: MatGetOwnershipRange(A,&rstart,0);
4466: for (i=0; i<m; i++) {
4467: MatGetRow(A,i+rstart,&nnz,&indx,&values);
4468: MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);
4469: MatRestoreRow(A,i+rstart,&nnz,&indx,&values);
4470: }
4471: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
4472: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
4474: MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);
4475: PetscStrlen(outfile,&len);
4476: PetscMalloc1(len+5,&name);
4477: sprintf(name,"%s.%d",outfile,rank);
4478: PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);
4479: PetscFree(name);
4480: MatView(B,out);
4481: PetscViewerDestroy(&out);
4482: MatDestroy(&B);
4483: return(0);
4484: }
4486: PetscErrorCode MatDestroy_MPIAIJ_SeqsToMPI(Mat A)
4487: {
4488: PetscErrorCode ierr;
4489: Mat_Merge_SeqsToMPI *merge;
4490: PetscContainer container;
4493: PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject*)&container);
4494: if (container) {
4495: PetscContainerGetPointer(container,(void**)&merge);
4496: PetscFree(merge->id_r);
4497: PetscFree(merge->len_s);
4498: PetscFree(merge->len_r);
4499: PetscFree(merge->bi);
4500: PetscFree(merge->bj);
4501: PetscFree(merge->buf_ri[0]);
4502: PetscFree(merge->buf_ri);
4503: PetscFree(merge->buf_rj[0]);
4504: PetscFree(merge->buf_rj);
4505: PetscFree(merge->coi);
4506: PetscFree(merge->coj);
4507: PetscFree(merge->owners_co);
4508: PetscLayoutDestroy(&merge->rowmap);
4509: PetscFree(merge);
4510: PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);
4511: }
4512: MatDestroy_MPIAIJ(A);
4513: return(0);
4514: }
4516: #include <../src/mat/utils/freespace.h>
4517: #include <petscbt.h>
4519: PetscErrorCode MatCreateMPIAIJSumSeqAIJNumeric(Mat seqmat,Mat mpimat)
4520: {
4521: PetscErrorCode ierr;
4522: MPI_Comm comm;
4523: Mat_SeqAIJ *a =(Mat_SeqAIJ*)seqmat->data;
4524: PetscMPIInt size,rank,taga,*len_s;
4525: PetscInt N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj;
4526: PetscInt proc,m;
4527: PetscInt **buf_ri,**buf_rj;
4528: PetscInt k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj;
4529: PetscInt nrows,**buf_ri_k,**nextrow,**nextai;
4530: MPI_Request *s_waits,*r_waits;
4531: MPI_Status *status;
4532: MatScalar *aa=a->a;
4533: MatScalar **abuf_r,*ba_i;
4534: Mat_Merge_SeqsToMPI *merge;
4535: PetscContainer container;
4538: PetscObjectGetComm((PetscObject)mpimat,&comm);
4539: PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);
4541: MPI_Comm_size(comm,&size);
4542: MPI_Comm_rank(comm,&rank);
4544: PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject*)&container);
4545: PetscContainerGetPointer(container,(void**)&merge);
4547: bi = merge->bi;
4548: bj = merge->bj;
4549: buf_ri = merge->buf_ri;
4550: buf_rj = merge->buf_rj;
4552: PetscMalloc1(size,&status);
4553: owners = merge->rowmap->range;
4554: len_s = merge->len_s;
4556: /* send and recv matrix values */
4557: /*-----------------------------*/
4558: PetscObjectGetNewTag((PetscObject)mpimat,&taga);
4559: PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);
4561: PetscMalloc1(merge->nsend+1,&s_waits);
4562: for (proc=0,k=0; proc<size; proc++) {
4563: if (!len_s[proc]) continue;
4564: i = owners[proc];
4565: MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);
4566: k++;
4567: }
4569: if (merge->nrecv) {MPI_Waitall(merge->nrecv,r_waits,status);}
4570: if (merge->nsend) {MPI_Waitall(merge->nsend,s_waits,status);}
4571: PetscFree(status);
4573: PetscFree(s_waits);
4574: PetscFree(r_waits);
4576: /* insert mat values of mpimat */
4577: /*----------------------------*/
4578: PetscMalloc1(N,&ba_i);
4579: PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextai);
4581: for (k=0; k<merge->nrecv; k++) {
4582: buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
4583: nrows = *(buf_ri_k[k]);
4584: nextrow[k] = buf_ri_k[k]+1; /* next row number of k-th recved i-structure */
4585: nextai[k] = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure */
4586: }
4588: /* set values of ba */
4589: m = merge->rowmap->n;
4590: for (i=0; i<m; i++) {
4591: arow = owners[rank] + i;
4592: bj_i = bj+bi[i]; /* col indices of the i-th row of mpimat */
4593: bnzi = bi[i+1] - bi[i];
4594: PetscArrayzero(ba_i,bnzi);
4596: /* add local non-zero vals of this proc's seqmat into ba */
4597: anzi = ai[arow+1] - ai[arow];
4598: aj = a->j + ai[arow];
4599: aa = a->a + ai[arow];
4600: nextaj = 0;
4601: for (j=0; nextaj<anzi; j++) {
4602: if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */
4603: ba_i[j] += aa[nextaj++];
4604: }
4605: }
4607: /* add received vals into ba */
4608: for (k=0; k<merge->nrecv; k++) { /* k-th received message */
4609: /* i-th row */
4610: if (i == *nextrow[k]) {
4611: anzi = *(nextai[k]+1) - *nextai[k];
4612: aj = buf_rj[k] + *(nextai[k]);
4613: aa = abuf_r[k] + *(nextai[k]);
4614: nextaj = 0;
4615: for (j=0; nextaj<anzi; j++) {
4616: if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */
4617: ba_i[j] += aa[nextaj++];
4618: }
4619: }
4620: nextrow[k]++; nextai[k]++;
4621: }
4622: }
4623: MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);
4624: }
4625: MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);
4626: MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);
4628: PetscFree(abuf_r[0]);
4629: PetscFree(abuf_r);
4630: PetscFree(ba_i);
4631: PetscFree3(buf_ri_k,nextrow,nextai);
4632: PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);
4633: return(0);
4634: }
4636: PetscErrorCode MatCreateMPIAIJSumSeqAIJSymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat)
4637: {
4638: PetscErrorCode ierr;
4639: Mat B_mpi;
4640: Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data;
4641: PetscMPIInt size,rank,tagi,tagj,*len_s,*len_si,*len_ri;
4642: PetscInt **buf_rj,**buf_ri,**buf_ri_k;
4643: PetscInt M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j;
4644: PetscInt len,proc,*dnz,*onz,bs,cbs;
4645: PetscInt k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0;
4646: PetscInt nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai;
4647: MPI_Request *si_waits,*sj_waits,*ri_waits,*rj_waits;
4648: MPI_Status *status;
4649: PetscFreeSpaceList free_space=NULL,current_space=NULL;
4650: PetscBT lnkbt;
4651: Mat_Merge_SeqsToMPI *merge;
4652: PetscContainer container;
4655: PetscLogEventBegin(MAT_Seqstompisym,seqmat,0,0,0);
4657: /* make sure it is a PETSc comm */
4658: PetscCommDuplicate(comm,&comm,NULL);
4659: MPI_Comm_size(comm,&size);
4660: MPI_Comm_rank(comm,&rank);
4662: PetscNew(&merge);
4663: PetscMalloc1(size,&status);
4665: /* determine row ownership */
4666: /*---------------------------------------------------------*/
4667: PetscLayoutCreate(comm,&merge->rowmap);
4668: PetscLayoutSetLocalSize(merge->rowmap,m);
4669: PetscLayoutSetSize(merge->rowmap,M);
4670: PetscLayoutSetBlockSize(merge->rowmap,1);
4671: PetscLayoutSetUp(merge->rowmap);
4672: PetscMalloc1(size,&len_si);
4673: PetscMalloc1(size,&merge->len_s);
4675: m = merge->rowmap->n;
4676: owners = merge->rowmap->range;
4678: /* determine the number of messages to send, their lengths */
4679: /*---------------------------------------------------------*/
4680: len_s = merge->len_s;
4682: len = 0; /* length of buf_si[] */
4683: merge->nsend = 0;
4684: for (proc=0; proc<size; proc++) {
4685: len_si[proc] = 0;
4686: if (proc == rank) {
4687: len_s[proc] = 0;
4688: } else {
4689: len_si[proc] = owners[proc+1] - owners[proc] + 1;
4690: len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */
4691: }
4692: if (len_s[proc]) {
4693: merge->nsend++;
4694: nrows = 0;
4695: for (i=owners[proc]; i<owners[proc+1]; i++) {
4696: if (ai[i+1] > ai[i]) nrows++;
4697: }
4698: len_si[proc] = 2*(nrows+1);
4699: len += len_si[proc];
4700: }
4701: }
4703: /* determine the number and length of messages to receive for ij-structure */
4704: /*-------------------------------------------------------------------------*/
4705: PetscGatherNumberOfMessages(comm,NULL,len_s,&merge->nrecv);
4706: PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);
4708: /* post the Irecv of j-structure */
4709: /*-------------------------------*/
4710: PetscCommGetNewTag(comm,&tagj);
4711: PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);
4713: /* post the Isend of j-structure */
4714: /*--------------------------------*/
4715: PetscMalloc2(merge->nsend,&si_waits,merge->nsend,&sj_waits);
4717: for (proc=0, k=0; proc<size; proc++) {
4718: if (!len_s[proc]) continue;
4719: i = owners[proc];
4720: MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);
4721: k++;
4722: }
4724: /* receives and sends of j-structure are complete */
4725: /*------------------------------------------------*/
4726: if (merge->nrecv) {MPI_Waitall(merge->nrecv,rj_waits,status);}
4727: if (merge->nsend) {MPI_Waitall(merge->nsend,sj_waits,status);}
4729: /* send and recv i-structure */
4730: /*---------------------------*/
4731: PetscCommGetNewTag(comm,&tagi);
4732: PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);
4734: PetscMalloc1(len+1,&buf_s);
4735: buf_si = buf_s; /* points to the beginning of k-th msg to be sent */
4736: for (proc=0,k=0; proc<size; proc++) {
4737: if (!len_s[proc]) continue;
4738: /* form outgoing message for i-structure:
4739: buf_si[0]: nrows to be sent
4740: [1:nrows]: row index (global)
4741: [nrows+1:2*nrows+1]: i-structure index
4742: */
4743: /*-------------------------------------------*/
4744: nrows = len_si[proc]/2 - 1;
4745: buf_si_i = buf_si + nrows+1;
4746: buf_si[0] = nrows;
4747: buf_si_i[0] = 0;
4748: nrows = 0;
4749: for (i=owners[proc]; i<owners[proc+1]; i++) {
4750: anzi = ai[i+1] - ai[i];
4751: if (anzi) {
4752: buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */
4753: buf_si[nrows+1] = i-owners[proc]; /* local row index */
4754: nrows++;
4755: }
4756: }
4757: MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);
4758: k++;
4759: buf_si += len_si[proc];
4760: }
4762: if (merge->nrecv) {MPI_Waitall(merge->nrecv,ri_waits,status);}
4763: if (merge->nsend) {MPI_Waitall(merge->nsend,si_waits,status);}
4765: PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);
4766: for (i=0; i<merge->nrecv; i++) {
4767: PetscInfo3(seqmat,"recv len_ri=%D, len_rj=%D from [%D]\n",len_ri[i],merge->len_r[i],merge->id_r[i]);
4768: }
4770: PetscFree(len_si);
4771: PetscFree(len_ri);
4772: PetscFree(rj_waits);
4773: PetscFree2(si_waits,sj_waits);
4774: PetscFree(ri_waits);
4775: PetscFree(buf_s);
4776: PetscFree(status);
4778: /* compute a local seq matrix in each processor */
4779: /*----------------------------------------------*/
4780: /* allocate bi array and free space for accumulating nonzero column info */
4781: PetscMalloc1(m+1,&bi);
4782: bi[0] = 0;
4784: /* create and initialize a linked list */
4785: nlnk = N+1;
4786: PetscLLCreate(N,N,nlnk,lnk,lnkbt);
4788: /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */
4789: len = ai[owners[rank+1]] - ai[owners[rank]];
4790: PetscFreeSpaceGet(PetscIntMultTruncate(2,len)+1,&free_space);
4792: current_space = free_space;
4794: /* determine symbolic info for each local row */
4795: PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextai);
4797: for (k=0; k<merge->nrecv; k++) {
4798: buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
4799: nrows = *buf_ri_k[k];
4800: nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */
4801: nextai[k] = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure */
4802: }
4804: MatPreallocateInitialize(comm,m,n,dnz,onz);
4805: len = 0;
4806: for (i=0; i<m; i++) {
4807: bnzi = 0;
4808: /* add local non-zero cols of this proc's seqmat into lnk */
4809: arow = owners[rank] + i;
4810: anzi = ai[arow+1] - ai[arow];
4811: aj = a->j + ai[arow];
4812: PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);
4813: bnzi += nlnk;
4814: /* add received col data into lnk */
4815: for (k=0; k<merge->nrecv; k++) { /* k-th received message */
4816: if (i == *nextrow[k]) { /* i-th row */
4817: anzi = *(nextai[k]+1) - *nextai[k];
4818: aj = buf_rj[k] + *nextai[k];
4819: PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);
4820: bnzi += nlnk;
4821: nextrow[k]++; nextai[k]++;
4822: }
4823: }
4824: if (len < bnzi) len = bnzi; /* =max(bnzi) */
4826: /* if free space is not available, make more free space */
4827: if (current_space->local_remaining<bnzi) {
4828: PetscFreeSpaceGet(PetscIntSumTruncate(bnzi,current_space->total_array_size),¤t_space);
4829: nspacedouble++;
4830: }
4831: /* copy data into free space, then initialize lnk */
4832: PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);
4833: MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);
4835: current_space->array += bnzi;
4836: current_space->local_used += bnzi;
4837: current_space->local_remaining -= bnzi;
4839: bi[i+1] = bi[i] + bnzi;
4840: }
4842: PetscFree3(buf_ri_k,nextrow,nextai);
4844: PetscMalloc1(bi[m]+1,&bj);
4845: PetscFreeSpaceContiguous(&free_space,bj);
4846: PetscLLDestroy(lnk,lnkbt);
4848: /* create symbolic parallel matrix B_mpi */
4849: /*---------------------------------------*/
4850: MatGetBlockSizes(seqmat,&bs,&cbs);
4851: MatCreate(comm,&B_mpi);
4852: if (n==PETSC_DECIDE) {
4853: MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);
4854: } else {
4855: MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
4856: }
4857: MatSetBlockSizes(B_mpi,bs,cbs);
4858: MatSetType(B_mpi,MATMPIAIJ);
4859: MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);
4860: MatPreallocateFinalize(dnz,onz);
4861: MatSetOption(B_mpi,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);
4863: /* B_mpi is not ready for use - assembly will be done by MatCreateMPIAIJSumSeqAIJNumeric() */
4864: B_mpi->assembled = PETSC_FALSE;
4865: B_mpi->ops->destroy = MatDestroy_MPIAIJ_SeqsToMPI;
4866: merge->bi = bi;
4867: merge->bj = bj;
4868: merge->buf_ri = buf_ri;
4869: merge->buf_rj = buf_rj;
4870: merge->coi = NULL;
4871: merge->coj = NULL;
4872: merge->owners_co = NULL;
4874: PetscCommDestroy(&comm);
4876: /* attach the supporting struct to B_mpi for reuse */
4877: PetscContainerCreate(PETSC_COMM_SELF,&container);
4878: PetscContainerSetPointer(container,merge);
4879: PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);
4880: PetscContainerDestroy(&container);
4881: *mpimat = B_mpi;
4883: PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);
4884: return(0);
4885: }
4887: /*@C
4888: MatCreateMPIAIJSumSeqAIJ - Creates a MATMPIAIJ matrix by adding sequential
4889: matrices from each processor
4891: Collective
4893: Input Parameters:
4894: + comm - the communicators the parallel matrix will live on
4895: . seqmat - the input sequential matrices
4896: . m - number of local rows (or PETSC_DECIDE)
4897: . n - number of local columns (or PETSC_DECIDE)
4898: - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
4900: Output Parameter:
4901: . mpimat - the parallel matrix generated
4903: Level: advanced
4905: Notes:
4906: The dimensions of the sequential matrix in each processor MUST be the same.
4907: The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be
4908: destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat.
4909: @*/
4910: PetscErrorCode MatCreateMPIAIJSumSeqAIJ(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat)
4911: {
4913: PetscMPIInt size;
4916: MPI_Comm_size(comm,&size);
4917: if (size == 1) {
4918: PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);
4919: if (scall == MAT_INITIAL_MATRIX) {
4920: MatDuplicate(seqmat,MAT_COPY_VALUES,mpimat);
4921: } else {
4922: MatCopy(seqmat,*mpimat,SAME_NONZERO_PATTERN);
4923: }
4924: PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);
4925: return(0);
4926: }
4927: PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);
4928: if (scall == MAT_INITIAL_MATRIX) {
4929: MatCreateMPIAIJSumSeqAIJSymbolic(comm,seqmat,m,n,mpimat);
4930: }
4931: MatCreateMPIAIJSumSeqAIJNumeric(seqmat,*mpimat);
4932: PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);
4933: return(0);
4934: }
4936: /*@
4937: MatMPIAIJGetLocalMat - Creates a SeqAIJ from a MATMPIAIJ matrix by taking all its local rows and putting them into a sequential matrix with
4938: mlocal rows and n columns. Where mlocal is the row count obtained with MatGetLocalSize() and n is the global column count obtained
4939: with MatGetSize()
4941: Not Collective
4943: Input Parameters:
4944: + A - the matrix
4945: - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
4947: Output Parameter:
4948: . A_loc - the local sequential matrix generated
4950: Level: developer
4952: Notes:
4953: When the communicator associated with A has size 1 and MAT_INITIAL_MATRIX is requested, the matrix returned is the diagonal part of A.
4954: If MAT_REUSE_MATRIX is requested with comm size 1, MatCopy(Adiag,*A_loc,SAME_NONZERO_PATTERN) is called.
4955: This means that one can preallocate the proper sequential matrix first and then call this routine with MAT_REUSE_MATRIX to safely
4956: modify the values of the returned A_loc.
4958: .seealso: MatGetOwnershipRange(), MatMPIAIJGetLocalMatCondensed()
4960: @*/
4961: PetscErrorCode MatMPIAIJGetLocalMat(Mat A,MatReuse scall,Mat *A_loc)
4962: {
4964: Mat_MPIAIJ *mpimat=(Mat_MPIAIJ*)A->data;
4965: Mat_SeqAIJ *mat,*a,*b;
4966: PetscInt *ai,*aj,*bi,*bj,*cmap=mpimat->garray;
4967: MatScalar *aa,*ba,*cam;
4968: PetscScalar *ca;
4969: PetscMPIInt size;
4970: PetscInt am=A->rmap->n,i,j,k,cstart=A->cmap->rstart;
4971: PetscInt *ci,*cj,col,ncols_d,ncols_o,jo;
4972: PetscBool match;
4975: PetscStrbeginswith(((PetscObject)A)->type_name,MATMPIAIJ,&match);
4976: if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MATMPIAIJ matrix as input");
4977: MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
4978: if (size == 1) {
4979: if (scall == MAT_INITIAL_MATRIX) {
4980: PetscObjectReference((PetscObject)mpimat->A);
4981: *A_loc = mpimat->A;
4982: } else if (scall == MAT_REUSE_MATRIX) {
4983: MatCopy(mpimat->A,*A_loc,SAME_NONZERO_PATTERN);
4984: }
4985: return(0);
4986: }
4988: PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);
4989: a = (Mat_SeqAIJ*)(mpimat->A)->data;
4990: b = (Mat_SeqAIJ*)(mpimat->B)->data;
4991: ai = a->i; aj = a->j; bi = b->i; bj = b->j;
4992: aa = a->a; ba = b->a;
4993: if (scall == MAT_INITIAL_MATRIX) {
4994: PetscMalloc1(1+am,&ci);
4995: ci[0] = 0;
4996: for (i=0; i<am; i++) {
4997: ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]);
4998: }
4999: PetscMalloc1(1+ci[am],&cj);
5000: PetscMalloc1(1+ci[am],&ca);
5001: k = 0;
5002: for (i=0; i<am; i++) {
5003: ncols_o = bi[i+1] - bi[i];
5004: ncols_d = ai[i+1] - ai[i];
5005: /* off-diagonal portion of A */
5006: for (jo=0; jo<ncols_o; jo++) {
5007: col = cmap[*bj];
5008: if (col >= cstart) break;
5009: cj[k] = col; bj++;
5010: ca[k++] = *ba++;
5011: }
5012: /* diagonal portion of A */
5013: for (j=0; j<ncols_d; j++) {
5014: cj[k] = cstart + *aj++;
5015: ca[k++] = *aa++;
5016: }
5017: /* off-diagonal portion of A */
5018: for (j=jo; j<ncols_o; j++) {
5019: cj[k] = cmap[*bj++];
5020: ca[k++] = *ba++;
5021: }
5022: }
5023: /* put together the new matrix */
5024: MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);
5025: /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
5026: /* Since these are PETSc arrays, change flags to free them as necessary. */
5027: mat = (Mat_SeqAIJ*)(*A_loc)->data;
5028: mat->free_a = PETSC_TRUE;
5029: mat->free_ij = PETSC_TRUE;
5030: mat->nonew = 0;
5031: } else if (scall == MAT_REUSE_MATRIX) {
5032: mat=(Mat_SeqAIJ*)(*A_loc)->data;
5033: ci = mat->i; cj = mat->j; cam = mat->a;
5034: for (i=0; i<am; i++) {
5035: /* off-diagonal portion of A */
5036: ncols_o = bi[i+1] - bi[i];
5037: for (jo=0; jo<ncols_o; jo++) {
5038: col = cmap[*bj];
5039: if (col >= cstart) break;
5040: *cam++ = *ba++; bj++;
5041: }
5042: /* diagonal portion of A */
5043: ncols_d = ai[i+1] - ai[i];
5044: for (j=0; j<ncols_d; j++) *cam++ = *aa++;
5045: /* off-diagonal portion of A */
5046: for (j=jo; j<ncols_o; j++) {
5047: *cam++ = *ba++; bj++;
5048: }
5049: }
5050: } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
5051: PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);
5052: return(0);
5053: }
5055: /*@C
5056: MatMPIAIJGetLocalMatCondensed - Creates a SeqAIJ matrix from an MATMPIAIJ matrix by taking all its local rows and NON-ZERO columns
5058: Not Collective
5060: Input Parameters:
5061: + A - the matrix
5062: . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
5063: - row, col - index sets of rows and columns to extract (or NULL)
5065: Output Parameter:
5066: . A_loc - the local sequential matrix generated
5068: Level: developer
5070: .seealso: MatGetOwnershipRange(), MatMPIAIJGetLocalMat()
5072: @*/
5073: PetscErrorCode MatMPIAIJGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc)
5074: {
5075: Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data;
5077: PetscInt i,start,end,ncols,nzA,nzB,*cmap,imark,*idx;
5078: IS isrowa,iscola;
5079: Mat *aloc;
5080: PetscBool match;
5083: PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);
5084: if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MATMPIAIJ matrix as input");
5085: PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);
5086: if (!row) {
5087: start = A->rmap->rstart; end = A->rmap->rend;
5088: ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);
5089: } else {
5090: isrowa = *row;
5091: }
5092: if (!col) {
5093: start = A->cmap->rstart;
5094: cmap = a->garray;
5095: nzA = a->A->cmap->n;
5096: nzB = a->B->cmap->n;
5097: PetscMalloc1(nzA+nzB, &idx);
5098: ncols = 0;
5099: for (i=0; i<nzB; i++) {
5100: if (cmap[i] < start) idx[ncols++] = cmap[i];
5101: else break;
5102: }
5103: imark = i;
5104: for (i=0; i<nzA; i++) idx[ncols++] = start + i;
5105: for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i];
5106: ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&iscola);
5107: } else {
5108: iscola = *col;
5109: }
5110: if (scall != MAT_INITIAL_MATRIX) {
5111: PetscMalloc1(1,&aloc);
5112: aloc[0] = *A_loc;
5113: }
5114: MatCreateSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);
5115: if (!col) { /* attach global id of condensed columns */
5116: PetscObjectCompose((PetscObject)aloc[0],"_petsc_GetLocalMatCondensed_iscol",(PetscObject)iscola);
5117: }
5118: *A_loc = aloc[0];
5119: PetscFree(aloc);
5120: if (!row) {
5121: ISDestroy(&isrowa);
5122: }
5123: if (!col) {
5124: ISDestroy(&iscola);
5125: }
5126: PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);
5127: return(0);
5128: }
5130: /*
5131: * Destroy a mat that may be composed with PetscSF communication objects.
5132: * The SF objects were created in MatCreateSeqSubMatrixWithRows_Private.
5133: * */
5134: PetscErrorCode MatDestroy_SeqAIJ_PetscSF(Mat mat)
5135: {
5136: PetscSF sf,osf;
5137: IS map;
5138: PetscErrorCode ierr;
5141: PetscObjectQuery((PetscObject)mat,"diagsf",(PetscObject*)&sf);
5142: PetscObjectQuery((PetscObject)mat,"offdiagsf",(PetscObject*)&osf);
5143: PetscSFDestroy(&sf);
5144: PetscSFDestroy(&osf);
5145: PetscObjectQuery((PetscObject)mat,"aoffdiagtopothmapping",(PetscObject*)&map);
5146: ISDestroy(&map);
5147: MatDestroy_SeqAIJ(mat);
5148: return(0);
5149: }
5151: /*
5152: * Create a sequential AIJ matrix based on row indices. a whole column is extracted once a row is matched.
5153: * Row could be local or remote.The routine is designed to be scalable in memory so that nothing is based
5154: * on a global size.
5155: * */
5156: PetscErrorCode MatCreateSeqSubMatrixWithRows_Private(Mat P,IS rows,Mat *P_oth)
5157: {
5158: Mat_MPIAIJ *p=(Mat_MPIAIJ*)P->data;
5159: Mat_SeqAIJ *pd=(Mat_SeqAIJ*)(p->A)->data,*po=(Mat_SeqAIJ*)(p->B)->data,*p_oth;
5160: PetscInt plocalsize,nrows,*ilocal,*oilocal,i,lidx,*nrcols,*nlcols,ncol;
5161: PetscMPIInt owner;
5162: PetscSFNode *iremote,*oiremote;
5163: const PetscInt *lrowindices;
5164: PetscErrorCode ierr;
5165: PetscSF sf,osf;
5166: PetscInt pcstart,*roffsets,*loffsets,*pnnz,j;
5167: PetscInt ontotalcols,dntotalcols,ntotalcols,nout;
5168: MPI_Comm comm;
5169: ISLocalToGlobalMapping mapping;
5172: PetscObjectGetComm((PetscObject)P,&comm);
5173: /* plocalsize is the number of roots
5174: * nrows is the number of leaves
5175: * */
5176: MatGetLocalSize(P,&plocalsize,NULL);
5177: ISGetLocalSize(rows,&nrows);
5178: PetscCalloc1(nrows,&iremote);
5179: ISGetIndices(rows,&lrowindices);
5180: for (i=0;i<nrows;i++) {
5181: /* Find a remote index and an owner for a row
5182: * The row could be local or remote
5183: * */
5184: owner = 0;
5185: lidx = 0;
5186: PetscLayoutFindOwnerIndex(P->rmap,lrowindices[i],&owner,&lidx);
5187: iremote[i].index = lidx;
5188: iremote[i].rank = owner;
5189: }
5190: /* Create SF to communicate how many nonzero columns for each row */
5191: PetscSFCreate(comm,&sf);
5192: /* SF will figure out the number of nonzero colunms for each row, and their
5193: * offsets
5194: * */
5195: PetscSFSetGraph(sf,plocalsize,nrows,NULL,PETSC_OWN_POINTER,iremote,PETSC_OWN_POINTER);
5196: PetscSFSetFromOptions(sf);
5197: PetscSFSetUp(sf);
5199: PetscCalloc1(2*(plocalsize+1),&roffsets);
5200: PetscCalloc1(2*plocalsize,&nrcols);
5201: PetscCalloc1(nrows,&pnnz);
5202: roffsets[0] = 0;
5203: roffsets[1] = 0;
5204: for (i=0;i<plocalsize;i++) {
5205: /* diag */
5206: nrcols[i*2+0] = pd->i[i+1] - pd->i[i];
5207: /* off diag */
5208: nrcols[i*2+1] = po->i[i+1] - po->i[i];
5209: /* compute offsets so that we relative location for each row */
5210: roffsets[(i+1)*2+0] = roffsets[i*2+0] + nrcols[i*2+0];
5211: roffsets[(i+1)*2+1] = roffsets[i*2+1] + nrcols[i*2+1];
5212: }
5213: PetscCalloc1(2*nrows,&nlcols);
5214: PetscCalloc1(2*nrows,&loffsets);
5215: /* 'r' means root, and 'l' means leaf */
5216: PetscSFBcastBegin(sf,MPIU_2INT,nrcols,nlcols);
5217: PetscSFBcastBegin(sf,MPIU_2INT,roffsets,loffsets);
5218: PetscSFBcastEnd(sf,MPIU_2INT,nrcols,nlcols);
5219: PetscSFBcastEnd(sf,MPIU_2INT,roffsets,loffsets);
5220: PetscSFDestroy(&sf);
5221: PetscFree(roffsets);
5222: PetscFree(nrcols);
5223: dntotalcols = 0;
5224: ontotalcols = 0;
5225: ncol = 0;
5226: for (i=0;i<nrows;i++) {
5227: pnnz[i] = nlcols[i*2+0] + nlcols[i*2+1];
5228: ncol = PetscMax(pnnz[i],ncol);
5229: /* diag */
5230: dntotalcols += nlcols[i*2+0];
5231: /* off diag */
5232: ontotalcols += nlcols[i*2+1];
5233: }
5234: /* We do not need to figure the right number of columns
5235: * since all the calculations will be done by going through the raw data
5236: * */
5237: MatCreateSeqAIJ(PETSC_COMM_SELF,nrows,ncol,0,pnnz,P_oth);
5238: MatSetUp(*P_oth);
5239: PetscFree(pnnz);
5240: p_oth = (Mat_SeqAIJ*) (*P_oth)->data;
5241: /* diag */
5242: PetscCalloc1(dntotalcols,&iremote);
5243: /* off diag */
5244: PetscCalloc1(ontotalcols,&oiremote);
5245: /* diag */
5246: PetscCalloc1(dntotalcols,&ilocal);
5247: /* off diag */
5248: PetscCalloc1(ontotalcols,&oilocal);
5249: dntotalcols = 0;
5250: ontotalcols = 0;
5251: ntotalcols = 0;
5252: for (i=0;i<nrows;i++) {
5253: owner = 0;
5254: PetscLayoutFindOwnerIndex(P->rmap,lrowindices[i],&owner,NULL);
5255: /* Set iremote for diag matrix */
5256: for (j=0;j<nlcols[i*2+0];j++) {
5257: iremote[dntotalcols].index = loffsets[i*2+0] + j;
5258: iremote[dntotalcols].rank = owner;
5259: /* P_oth is seqAIJ so that ilocal need to point to the first part of memory */
5260: ilocal[dntotalcols++] = ntotalcols++;
5261: }
5262: /* off diag */
5263: for (j=0;j<nlcols[i*2+1];j++) {
5264: oiremote[ontotalcols].index = loffsets[i*2+1] + j;
5265: oiremote[ontotalcols].rank = owner;
5266: oilocal[ontotalcols++] = ntotalcols++;
5267: }
5268: }
5269: ISRestoreIndices(rows,&lrowindices);
5270: PetscFree(loffsets);
5271: PetscFree(nlcols);
5272: PetscSFCreate(comm,&sf);
5273: /* P serves as roots and P_oth is leaves
5274: * Diag matrix
5275: * */
5276: PetscSFSetGraph(sf,pd->i[plocalsize],dntotalcols,ilocal,PETSC_OWN_POINTER,iremote,PETSC_OWN_POINTER);
5277: PetscSFSetFromOptions(sf);
5278: PetscSFSetUp(sf);
5280: PetscSFCreate(comm,&osf);
5281: /* Off diag */
5282: PetscSFSetGraph(osf,po->i[plocalsize],ontotalcols,oilocal,PETSC_OWN_POINTER,oiremote,PETSC_OWN_POINTER);
5283: PetscSFSetFromOptions(osf);
5284: PetscSFSetUp(osf);
5285: /* We operate on the matrix internal data for saving memory */
5286: PetscSFBcastBegin(sf,MPIU_SCALAR,pd->a,p_oth->a);
5287: PetscSFBcastBegin(osf,MPIU_SCALAR,po->a,p_oth->a);
5288: MatGetOwnershipRangeColumn(P,&pcstart,NULL);
5289: /* Convert to global indices for diag matrix */
5290: for (i=0;i<pd->i[plocalsize];i++) pd->j[i] += pcstart;
5291: PetscSFBcastBegin(sf,MPIU_INT,pd->j,p_oth->j);
5292: /* We want P_oth store global indices */
5293: ISLocalToGlobalMappingCreate(comm,1,p->B->cmap->n,p->garray,PETSC_COPY_VALUES,&mapping);
5294: /* Use memory scalable approach */
5295: ISLocalToGlobalMappingSetType(mapping,ISLOCALTOGLOBALMAPPINGHASH);
5296: ISLocalToGlobalMappingApply(mapping,po->i[plocalsize],po->j,po->j);
5297: PetscSFBcastBegin(osf,MPIU_INT,po->j,p_oth->j);
5298: PetscSFBcastEnd(sf,MPIU_INT,pd->j,p_oth->j);
5299: /* Convert back to local indices */
5300: for (i=0;i<pd->i[plocalsize];i++) pd->j[i] -= pcstart;
5301: PetscSFBcastEnd(osf,MPIU_INT,po->j,p_oth->j);
5302: nout = 0;
5303: ISGlobalToLocalMappingApply(mapping,IS_GTOLM_DROP,po->i[plocalsize],po->j,&nout,po->j);
5304: if (nout != po->i[plocalsize]) SETERRQ2(comm,PETSC_ERR_ARG_INCOMP,"n %D does not equal to nout %D \n",po->i[plocalsize],nout);
5305: ISLocalToGlobalMappingDestroy(&mapping);
5306: /* Exchange values */
5307: PetscSFBcastEnd(sf,MPIU_SCALAR,pd->a,p_oth->a);
5308: PetscSFBcastEnd(osf,MPIU_SCALAR,po->a,p_oth->a);
5309: /* Stop PETSc from shrinking memory */
5310: for (i=0;i<nrows;i++) p_oth->ilen[i] = p_oth->imax[i];
5311: MatAssemblyBegin(*P_oth,MAT_FINAL_ASSEMBLY);
5312: MatAssemblyEnd(*P_oth,MAT_FINAL_ASSEMBLY);
5313: /* Attach PetscSF objects to P_oth so that we can reuse it later */
5314: PetscObjectCompose((PetscObject)*P_oth,"diagsf",(PetscObject)sf);
5315: PetscObjectCompose((PetscObject)*P_oth,"offdiagsf",(PetscObject)osf);
5316: /* ``New MatDestroy" takes care of PetscSF objects as well */
5317: (*P_oth)->ops->destroy = MatDestroy_SeqAIJ_PetscSF;
5318: return(0);
5319: }
5321: /*
5322: * Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A
5323: * This supports MPIAIJ and MAIJ
5324: * */
5325: PetscErrorCode MatGetBrowsOfAcols_MPIXAIJ(Mat A,Mat P,PetscInt dof,MatReuse reuse,Mat *P_oth)
5326: {
5327: Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data,*p=(Mat_MPIAIJ*)P->data;
5328: Mat_SeqAIJ *p_oth;
5329: Mat_SeqAIJ *pd=(Mat_SeqAIJ*)(p->A)->data,*po=(Mat_SeqAIJ*)(p->B)->data;
5330: IS rows,map;
5331: PetscHMapI hamp;
5332: PetscInt i,htsize,*rowindices,off,*mapping,key,count;
5333: MPI_Comm comm;
5334: PetscSF sf,osf;
5335: PetscBool has;
5336: PetscErrorCode ierr;
5339: PetscObjectGetComm((PetscObject)A,&comm);
5340: PetscLogEventBegin(MAT_GetBrowsOfAocols,A,P,0,0);
5341: /* If it is the first time, create an index set of off-diag nonzero columns of A,
5342: * and then create a submatrix (that often is an overlapping matrix)
5343: * */
5344: if (reuse==MAT_INITIAL_MATRIX) {
5345: /* Use a hash table to figure out unique keys */
5346: PetscHMapICreate(&hamp);
5347: PetscHMapIResize(hamp,a->B->cmap->n);
5348: PetscCalloc1(a->B->cmap->n,&mapping);
5349: count = 0;
5350: /* Assume that a->g is sorted, otherwise the following does not make sense */
5351: for (i=0;i<a->B->cmap->n;i++) {
5352: key = a->garray[i]/dof;
5353: PetscHMapIHas(hamp,key,&has);
5354: if (!has) {
5355: mapping[i] = count;
5356: PetscHMapISet(hamp,key,count++);
5357: } else {
5358: /* Current 'i' has the same value the previous step */
5359: mapping[i] = count-1;
5360: }
5361: }
5362: ISCreateGeneral(comm,a->B->cmap->n,mapping,PETSC_OWN_POINTER,&map);
5363: PetscHMapIGetSize(hamp,&htsize);
5364: if (htsize!=count) SETERRQ2(comm,PETSC_ERR_ARG_INCOMP," Size of hash map %D is inconsistent with count %D \n",htsize,count);
5365: PetscCalloc1(htsize,&rowindices);
5366: off = 0;
5367: PetscHMapIGetKeys(hamp,&off,rowindices);
5368: PetscHMapIDestroy(&hamp);
5369: PetscSortInt(htsize,rowindices);
5370: ISCreateGeneral(comm,htsize,rowindices,PETSC_OWN_POINTER,&rows);
5371: /* In case, the matrix was already created but users want to recreate the matrix */
5372: MatDestroy(P_oth);
5373: MatCreateSeqSubMatrixWithRows_Private(P,rows,P_oth);
5374: PetscObjectCompose((PetscObject)*P_oth,"aoffdiagtopothmapping",(PetscObject)map);
5375: ISDestroy(&rows);
5376: } else if (reuse==MAT_REUSE_MATRIX) {
5377: /* If matrix was already created, we simply update values using SF objects
5378: * that as attached to the matrix ealier.
5379: * */
5380: PetscObjectQuery((PetscObject)*P_oth,"diagsf",(PetscObject*)&sf);
5381: PetscObjectQuery((PetscObject)*P_oth,"offdiagsf",(PetscObject*)&osf);
5382: if (!sf || !osf) {
5383: SETERRQ(comm,PETSC_ERR_ARG_NULL,"Matrix is not initialized yet \n");
5384: }
5385: p_oth = (Mat_SeqAIJ*) (*P_oth)->data;
5386: /* Update values in place */
5387: PetscSFBcastBegin(sf,MPIU_SCALAR,pd->a,p_oth->a);
5388: PetscSFBcastBegin(osf,MPIU_SCALAR,po->a,p_oth->a);
5389: PetscSFBcastEnd(sf,MPIU_SCALAR,pd->a,p_oth->a);
5390: PetscSFBcastEnd(osf,MPIU_SCALAR,po->a,p_oth->a);
5391: } else {
5392: SETERRQ(comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown reuse type \n");
5393: }
5394: PetscLogEventEnd(MAT_GetBrowsOfAocols,A,P,0,0);
5395: return(0);
5396: }
5398: /*@C
5399: MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A
5401: Collective on Mat
5403: Input Parameters:
5404: + A,B - the matrices in mpiaij format
5405: . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
5406: - rowb, colb - index sets of rows and columns of B to extract (or NULL)
5408: Output Parameter:
5409: + rowb, colb - index sets of rows and columns of B to extract
5410: - B_seq - the sequential matrix generated
5412: Level: developer
5414: @*/
5415: PetscErrorCode MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,Mat *B_seq)
5416: {
5417: Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data;
5419: PetscInt *idx,i,start,ncols,nzA,nzB,*cmap,imark;
5420: IS isrowb,iscolb;
5421: Mat *bseq=NULL;
5424: if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) {
5425: SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, (%D, %D) != (%D,%D)",A->cmap->rstart,A->cmap->rend,B->rmap->rstart,B->rmap->rend);
5426: }
5427: PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);
5429: if (scall == MAT_INITIAL_MATRIX) {
5430: start = A->cmap->rstart;
5431: cmap = a->garray;
5432: nzA = a->A->cmap->n;
5433: nzB = a->B->cmap->n;
5434: PetscMalloc1(nzA+nzB, &idx);
5435: ncols = 0;
5436: for (i=0; i<nzB; i++) { /* row < local row index */
5437: if (cmap[i] < start) idx[ncols++] = cmap[i];
5438: else break;
5439: }
5440: imark = i;
5441: for (i=0; i<nzA; i++) idx[ncols++] = start + i; /* local rows */
5442: for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */
5443: ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&isrowb);
5444: ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);
5445: } else {
5446: if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX");
5447: isrowb = *rowb; iscolb = *colb;
5448: PetscMalloc1(1,&bseq);
5449: bseq[0] = *B_seq;
5450: }
5451: MatCreateSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);
5452: *B_seq = bseq[0];
5453: PetscFree(bseq);
5454: if (!rowb) {
5455: ISDestroy(&isrowb);
5456: } else {
5457: *rowb = isrowb;
5458: }
5459: if (!colb) {
5460: ISDestroy(&iscolb);
5461: } else {
5462: *colb = iscolb;
5463: }
5464: PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);
5465: return(0);
5466: }
5468: /*
5469: MatGetBrowsOfAoCols_MPIAIJ - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns
5470: of the OFF-DIAGONAL portion of local A
5472: Collective on Mat
5474: Input Parameters:
5475: + A,B - the matrices in mpiaij format
5476: - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
5478: Output Parameter:
5479: + startsj_s - starting point in B's sending j-arrays, saved for MAT_REUSE (or NULL)
5480: . startsj_r - starting point in B's receiving j-arrays, saved for MAT_REUSE (or NULL)
5481: . bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or NULL)
5482: - B_oth - the sequential matrix generated with size aBn=a->B->cmap->n by B->cmap->N
5484: Developer Notes: This directly accesses information inside the VecScatter associated with the matrix-vector product
5485: for this matrix. This is not desirable..
5487: Level: developer
5489: */
5490: PetscErrorCode MatGetBrowsOfAoCols_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscInt **startsj_s,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth)
5491: {
5492: PetscErrorCode ierr;
5493: Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data;
5494: Mat_SeqAIJ *b_oth;
5495: VecScatter ctx;
5496: MPI_Comm comm;
5497: const PetscMPIInt *rprocs,*sprocs;
5498: const PetscInt *srow,*rstarts,*sstarts;
5499: PetscInt *rowlen,*bufj,*bufJ,ncols = 0,aBn=a->B->cmap->n,row,*b_othi,*b_othj,*rvalues=NULL,*svalues=NULL,*cols,sbs,rbs;
5500: PetscInt i,j,k=0,l,ll,nrecvs,nsends,nrows,*rstartsj = 0,*sstartsj,len;
5501: PetscScalar *b_otha,*bufa,*bufA,*vals = NULL;
5502: MPI_Request *rwaits = NULL,*swaits = NULL;
5503: MPI_Status rstatus;
5504: PetscMPIInt jj,size,tag,rank,nsends_mpi,nrecvs_mpi;
5507: PetscObjectGetComm((PetscObject)A,&comm);
5508: MPI_Comm_size(comm,&size);
5510: if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) {
5511: SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, (%d, %d) != (%d,%d)",A->cmap->rstart,A->cmap->rend,B->rmap->rstart,B->rmap->rend);
5512: }
5513: PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);
5514: MPI_Comm_rank(comm,&rank);
5516: if (size == 1) {
5517: startsj_s = NULL;
5518: bufa_ptr = NULL;
5519: *B_oth = NULL;
5520: return(0);
5521: }
5523: ctx = a->Mvctx;
5524: tag = ((PetscObject)ctx)->tag;
5526: if (ctx->inuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE," Scatter ctx already in use");
5527: VecScatterGetRemote_Private(ctx,PETSC_TRUE/*send*/,&nsends,&sstarts,&srow,&sprocs,&sbs);
5528: /* rprocs[] must be ordered so that indices received from them are ordered in rvalues[], which is key to algorithms used in this subroutine */
5529: VecScatterGetRemoteOrdered_Private(ctx,PETSC_FALSE/*recv*/,&nrecvs,&rstarts,NULL/*indices not needed*/,&rprocs,&rbs);
5530: PetscMPIIntCast(nsends,&nsends_mpi);
5531: PetscMPIIntCast(nrecvs,&nrecvs_mpi);
5532: PetscMalloc2(nrecvs,&rwaits,nsends,&swaits);
5534: if (!startsj_s || !bufa_ptr) scall = MAT_INITIAL_MATRIX;
5535: if (scall == MAT_INITIAL_MATRIX) {
5536: /* i-array */
5537: /*---------*/
5538: /* post receives */
5539: if (nrecvs) {PetscMalloc1(rbs*(rstarts[nrecvs] - rstarts[0]),&rvalues);} /* rstarts can be NULL when nrecvs=0 */
5540: for (i=0; i<nrecvs; i++) {
5541: rowlen = rvalues + rstarts[i]*rbs;
5542: nrows = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */
5543: MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);
5544: }
5546: /* pack the outgoing message */
5547: PetscMalloc2(nsends+1,&sstartsj,nrecvs+1,&rstartsj);
5549: sstartsj[0] = 0;
5550: rstartsj[0] = 0;
5551: len = 0; /* total length of j or a array to be sent */
5552: if (nsends) {
5553: k = sstarts[0]; /* ATTENTION: sstarts[0] and rstarts[0] are not necessarily zero */
5554: PetscMalloc1(sbs*(sstarts[nsends]-sstarts[0]),&svalues);
5555: }
5556: for (i=0; i<nsends; i++) {
5557: rowlen = svalues + (sstarts[i]-sstarts[0])*sbs;
5558: nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
5559: for (j=0; j<nrows; j++) {
5560: row = srow[k] + B->rmap->range[rank]; /* global row idx */
5561: for (l=0; l<sbs; l++) {
5562: MatGetRow_MPIAIJ(B,row+l,&ncols,NULL,NULL); /* rowlength */
5564: rowlen[j*sbs+l] = ncols;
5566: len += ncols;
5567: MatRestoreRow_MPIAIJ(B,row+l,&ncols,NULL,NULL);
5568: }
5569: k++;
5570: }
5571: MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);
5573: sstartsj[i+1] = len; /* starting point of (i+1)-th outgoing msg in bufj and bufa */
5574: }
5575: /* recvs and sends of i-array are completed */
5576: i = nrecvs;
5577: while (i--) {
5578: MPI_Waitany(nrecvs_mpi,rwaits,&jj,&rstatus);
5579: }
5580: if (nsends) {MPI_Waitall(nsends_mpi,swaits,MPI_STATUSES_IGNORE);}
5581: PetscFree(svalues);
5583: /* allocate buffers for sending j and a arrays */
5584: PetscMalloc1(len+1,&bufj);
5585: PetscMalloc1(len+1,&bufa);
5587: /* create i-array of B_oth */
5588: PetscMalloc1(aBn+2,&b_othi);
5590: b_othi[0] = 0;
5591: len = 0; /* total length of j or a array to be received */
5592: k = 0;
5593: for (i=0; i<nrecvs; i++) {
5594: rowlen = rvalues + (rstarts[i]-rstarts[0])*rbs;
5595: nrows = (rstarts[i+1]-rstarts[i])*rbs; /* num of rows to be received */
5596: for (j=0; j<nrows; j++) {
5597: b_othi[k+1] = b_othi[k] + rowlen[j];
5598: PetscIntSumError(rowlen[j],len,&len);
5599: k++;
5600: }
5601: rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */
5602: }
5603: PetscFree(rvalues);
5605: /* allocate space for j and a arrrays of B_oth */
5606: PetscMalloc1(b_othi[aBn]+1,&b_othj);
5607: PetscMalloc1(b_othi[aBn]+1,&b_otha);
5609: /* j-array */
5610: /*---------*/
5611: /* post receives of j-array */
5612: for (i=0; i<nrecvs; i++) {
5613: nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
5614: MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);
5615: }
5617: /* pack the outgoing message j-array */
5618: if (nsends) k = sstarts[0];
5619: for (i=0; i<nsends; i++) {
5620: nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
5621: bufJ = bufj+sstartsj[i];
5622: for (j=0; j<nrows; j++) {
5623: row = srow[k++] + B->rmap->range[rank]; /* global row idx */
5624: for (ll=0; ll<sbs; ll++) {
5625: MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);
5626: for (l=0; l<ncols; l++) {
5627: *bufJ++ = cols[l];
5628: }
5629: MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);
5630: }
5631: }
5632: MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);
5633: }
5635: /* recvs and sends of j-array are completed */
5636: i = nrecvs;
5637: while (i--) {
5638: MPI_Waitany(nrecvs_mpi,rwaits,&jj,&rstatus);
5639: }
5640: if (nsends) {MPI_Waitall(nsends_mpi,swaits,MPI_STATUSES_IGNORE);}
5641: } else if (scall == MAT_REUSE_MATRIX) {
5642: sstartsj = *startsj_s;
5643: rstartsj = *startsj_r;
5644: bufa = *bufa_ptr;
5645: b_oth = (Mat_SeqAIJ*)(*B_oth)->data;
5646: b_otha = b_oth->a;
5647: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container");
5649: /* a-array */
5650: /*---------*/
5651: /* post receives of a-array */
5652: for (i=0; i<nrecvs; i++) {
5653: nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
5654: MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);
5655: }
5657: /* pack the outgoing message a-array */
5658: if (nsends) k = sstarts[0];
5659: for (i=0; i<nsends; i++) {
5660: nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */
5661: bufA = bufa+sstartsj[i];
5662: for (j=0; j<nrows; j++) {
5663: row = srow[k++] + B->rmap->range[rank]; /* global row idx */
5664: for (ll=0; ll<sbs; ll++) {
5665: MatGetRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);
5666: for (l=0; l<ncols; l++) {
5667: *bufA++ = vals[l];
5668: }
5669: MatRestoreRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);
5670: }
5671: }
5672: MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);
5673: }
5674: /* recvs and sends of a-array are completed */
5675: i = nrecvs;
5676: while (i--) {
5677: MPI_Waitany(nrecvs_mpi,rwaits,&jj,&rstatus);
5678: }
5679: if (nsends) {MPI_Waitall(nsends_mpi,swaits,MPI_STATUSES_IGNORE);}
5680: PetscFree2(rwaits,swaits);
5682: if (scall == MAT_INITIAL_MATRIX) {
5683: /* put together the new matrix */
5684: MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap->N,b_othi,b_othj,b_otha,B_oth);
5686: /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
5687: /* Since these are PETSc arrays, change flags to free them as necessary. */
5688: b_oth = (Mat_SeqAIJ*)(*B_oth)->data;
5689: b_oth->free_a = PETSC_TRUE;
5690: b_oth->free_ij = PETSC_TRUE;
5691: b_oth->nonew = 0;
5693: PetscFree(bufj);
5694: if (!startsj_s || !bufa_ptr) {
5695: PetscFree2(sstartsj,rstartsj);
5696: PetscFree(bufa_ptr);
5697: } else {
5698: *startsj_s = sstartsj;
5699: *startsj_r = rstartsj;
5700: *bufa_ptr = bufa;
5701: }
5702: }
5704: VecScatterRestoreRemote_Private(ctx,PETSC_TRUE,&nsends,&sstarts,&srow,&sprocs,&sbs);
5705: VecScatterRestoreRemoteOrdered_Private(ctx,PETSC_FALSE,&nrecvs,&rstarts,NULL,&rprocs,&rbs);
5706: PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);
5707: return(0);
5708: }
5710: /*@C
5711: MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication.
5713: Not Collective
5715: Input Parameters:
5716: . A - The matrix in mpiaij format
5718: Output Parameter:
5719: + lvec - The local vector holding off-process values from the argument to a matrix-vector product
5720: . colmap - A map from global column index to local index into lvec
5721: - multScatter - A scatter from the argument of a matrix-vector product to lvec
5723: Level: developer
5725: @*/
5726: #if defined(PETSC_USE_CTABLE)
5727: PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter)
5728: #else
5729: PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter)
5730: #endif
5731: {
5732: Mat_MPIAIJ *a;
5739: a = (Mat_MPIAIJ*) A->data;
5740: if (lvec) *lvec = a->lvec;
5741: if (colmap) *colmap = a->colmap;
5742: if (multScatter) *multScatter = a->Mvctx;
5743: return(0);
5744: }
5746: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJCRL(Mat,MatType,MatReuse,Mat*);
5747: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJPERM(Mat,MatType,MatReuse,Mat*);
5748: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJSELL(Mat,MatType,MatReuse,Mat*);
5749: #if defined(PETSC_HAVE_MKL_SPARSE)
5750: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJMKL(Mat,MatType,MatReuse,Mat*);
5751: #endif
5752: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPIBAIJ(Mat,MatType,MatReuse,Mat*);
5753: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPISBAIJ(Mat,MatType,MatReuse,Mat*);
5754: #if defined(PETSC_HAVE_ELEMENTAL)
5755: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
5756: #endif
5757: #if defined(PETSC_HAVE_HYPRE)
5758: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat,MatType,MatReuse,Mat*);
5759: #endif
5760: PETSC_INTERN PetscErrorCode MatConvert_MPIAIJ_MPISELL(Mat,MatType,MatReuse,Mat*);
5761: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat,MatType,MatReuse,Mat*);
5762: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_IS_XAIJ(Mat);
5764: /*
5765: Computes (B'*A')' since computing B*A directly is untenable
5767: n p p
5768: ( ) ( ) ( )
5769: m ( A ) * n ( B ) = m ( C )
5770: ( ) ( ) ( )
5772: */
5773: PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C)
5774: {
5776: Mat At,Bt,Ct;
5779: MatTranspose(A,MAT_INITIAL_MATRIX,&At);
5780: MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);
5781: MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);
5782: MatDestroy(&At);
5783: MatDestroy(&Bt);
5784: MatTranspose(Ct,MAT_REUSE_MATRIX,&C);
5785: MatDestroy(&Ct);
5786: return(0);
5787: }
5789: PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat C)
5790: {
5792: PetscInt m=A->rmap->n,n=B->cmap->n;
5795: 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);
5796: MatSetSizes(C,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
5797: MatSetBlockSizesFromMats(C,A,B);
5798: MatSetType(C,MATMPIDENSE);
5799: MatMPIDenseSetPreallocation(C,NULL);
5800: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
5801: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
5803: C->ops->matmultnumeric = MatMatMultNumeric_MPIDense_MPIAIJ;
5804: return(0);
5805: }
5807: /* ----------------------------------------------------------------*/
5808: static PetscErrorCode MatProductSetFromOptions_MPIDense_MPIAIJ_AB(Mat C)
5809: {
5810: Mat_Product *product = C->product;
5811: Mat A = product->A,B=product->B;
5814: if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend)
5815: SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, (%D, %D) != (%D,%D)",A->cmap->rstart,A->cmap->rend,B->rmap->rstart,B->rmap->rend);
5817: C->ops->matmultsymbolic = MatMatMultSymbolic_MPIDense_MPIAIJ;
5818: C->ops->productsymbolic = MatProductSymbolic_AB;
5819: return(0);
5820: }
5822: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_MPIDense_MPIAIJ(Mat C)
5823: {
5825: Mat_Product *product = C->product;
5828: if (product->type == MATPRODUCT_AB) {
5829: MatProductSetFromOptions_MPIDense_MPIAIJ_AB(C);
5830: } else SETERRQ1(PetscObjectComm((PetscObject)C),PETSC_ERR_SUP,"MatProduct type %s is not supported for MPIDense and MPIAIJ matrices",MatProductTypes[product->type]);
5831: return(0);
5832: }
5833: /* ----------------------------------------------------------------*/
5835: /*MC
5836: MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices.
5838: Options Database Keys:
5839: . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions()
5841: Level: beginner
5843: Notes:
5844: MatSetValues() may be called for this matrix type with a NULL argument for the numerical values,
5845: in this case the values associated with the rows and columns one passes in are set to zero
5846: in the matrix
5848: MatSetOptions(,MAT_STRUCTURE_ONLY,PETSC_TRUE) may be called for this matrix type. In this no
5849: space is allocated for the nonzero entries and any entries passed with MatSetValues() are ignored
5851: .seealso: MatCreateAIJ()
5852: M*/
5854: PETSC_EXTERN PetscErrorCode MatCreate_MPIAIJ(Mat B)
5855: {
5856: Mat_MPIAIJ *b;
5858: PetscMPIInt size;
5861: MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);
5863: PetscNewLog(B,&b);
5864: B->data = (void*)b;
5865: PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
5866: B->assembled = PETSC_FALSE;
5867: B->insertmode = NOT_SET_VALUES;
5868: b->size = size;
5870: MPI_Comm_rank(PetscObjectComm((PetscObject)B),&b->rank);
5872: /* build cache for off array entries formed */
5873: MatStashCreate_Private(PetscObjectComm((PetscObject)B),1,&B->stash);
5875: b->donotstash = PETSC_FALSE;
5876: b->colmap = 0;
5877: b->garray = 0;
5878: b->roworiented = PETSC_TRUE;
5880: /* stuff used for matrix vector multiply */
5881: b->lvec = NULL;
5882: b->Mvctx = NULL;
5884: /* stuff for MatGetRow() */
5885: b->rowindices = 0;
5886: b->rowvalues = 0;
5887: b->getrowactive = PETSC_FALSE;
5889: /* flexible pointer used in CUSP/CUSPARSE classes */
5890: b->spptr = NULL;
5892: PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetUseScalableIncreaseOverlap_C",MatMPIAIJSetUseScalableIncreaseOverlap_MPIAIJ);
5893: PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_MPIAIJ);
5894: PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_MPIAIJ);
5895: PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_MPIAIJ);
5896: PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",MatMPIAIJSetPreallocation_MPIAIJ);
5897: PetscObjectComposeFunction((PetscObject)B,"MatResetPreallocation_C",MatResetPreallocation_MPIAIJ);
5898: PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",MatMPIAIJSetPreallocationCSR_MPIAIJ);
5899: PetscObjectComposeFunction((PetscObject)B,"MatDiagonalScaleLocal_C",MatDiagonalScaleLocal_MPIAIJ);
5900: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C",MatConvert_MPIAIJ_MPIAIJPERM);
5901: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijsell_C",MatConvert_MPIAIJ_MPIAIJSELL);
5902: #if defined(PETSC_HAVE_MKL_SPARSE)
5903: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijmkl_C",MatConvert_MPIAIJ_MPIAIJMKL);
5904: #endif
5905: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C",MatConvert_MPIAIJ_MPIAIJCRL);
5906: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpibaij_C",MatConvert_MPIAIJ_MPIBAIJ);
5907: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C",MatConvert_MPIAIJ_MPISBAIJ);
5908: #if defined(PETSC_HAVE_ELEMENTAL)
5909: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_elemental_C",MatConvert_MPIAIJ_Elemental);
5910: #endif
5911: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_is_C",MatConvert_XAIJ_IS);
5912: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpisell_C",MatConvert_MPIAIJ_MPISELL);
5913: PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C",MatMatMultSymbolic_MPIDense_MPIAIJ);
5914: PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C",MatMatMultNumeric_MPIDense_MPIAIJ);
5915: #if defined(PETSC_HAVE_HYPRE)
5916: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_hypre_C",MatConvert_AIJ_HYPRE);
5917: PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_transpose_mpiaij_mpiaij_C",MatProductSetFromOptions_Transpose_AIJ_AIJ);
5918: #endif
5919: PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_is_mpiaij_C",MatProductSetFromOptions_IS_XAIJ);
5920: PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_mpiaij_mpiaij_C",MatProductSetFromOptions_MPIAIJ);
5921: PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);
5922: return(0);
5923: }
5925: /*@C
5926: MatCreateMPIAIJWithSplitArrays - creates a MPI AIJ matrix using arrays that contain the "diagonal"
5927: and "off-diagonal" part of the matrix in CSR format.
5929: Collective
5931: Input Parameters:
5932: + comm - MPI communicator
5933: . m - number of local rows (Cannot be PETSC_DECIDE)
5934: . n - This value should be the same as the local size used in creating the
5935: x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
5936: calculated if N is given) For square matrices n is almost always m.
5937: . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
5938: . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
5939: . i - row indices for "diagonal" portion of matrix; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
5940: . j - column indices
5941: . a - matrix values
5942: . oi - row indices for "off-diagonal" portion of matrix; that is oi[0] = 0, oi[row] = oi[row-1] + number of elements in that row of the matrix
5943: . oj - column indices
5944: - oa - matrix values
5946: Output Parameter:
5947: . mat - the matrix
5949: Level: advanced
5951: Notes:
5952: The i, j, and a arrays ARE NOT copied by this routine into the internal format used by PETSc. The user
5953: must free the arrays once the matrix has been destroyed and not before.
5955: The i and j indices are 0 based
5957: See MatCreateAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix
5959: This sets local rows and cannot be used to set off-processor values.
5961: Use of this routine is discouraged because it is inflexible and cumbersome to use. It is extremely rare that a
5962: legacy application natively assembles into exactly this split format. The code to do so is nontrivial and does
5963: not easily support in-place reassembly. It is recommended to use MatSetValues() (or a variant thereof) because
5964: the resulting assembly is easier to implement, will work with any matrix format, and the user does not have to
5965: keep track of the underlying array. Use MatSetOption(A,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE) to disable all
5966: communication if it is known that only local entries will be set.
5968: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
5969: MATMPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithArrays()
5970: @*/
5971: PetscErrorCode MatCreateMPIAIJWithSplitArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt i[],PetscInt j[],PetscScalar a[],PetscInt oi[], PetscInt oj[],PetscScalar oa[],Mat *mat)
5972: {
5974: Mat_MPIAIJ *maij;
5977: if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
5978: if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
5979: if (oi[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0");
5980: MatCreate(comm,mat);
5981: MatSetSizes(*mat,m,n,M,N);
5982: MatSetType(*mat,MATMPIAIJ);
5983: maij = (Mat_MPIAIJ*) (*mat)->data;
5985: (*mat)->preallocated = PETSC_TRUE;
5987: PetscLayoutSetUp((*mat)->rmap);
5988: PetscLayoutSetUp((*mat)->cmap);
5990: MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,n,i,j,a,&maij->A);
5991: MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,(*mat)->cmap->N,oi,oj,oa,&maij->B);
5993: MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);
5994: MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);
5995: MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);
5996: MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);
5998: MatSetOption(*mat,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE);
5999: MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
6000: MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
6001: MatSetOption(*mat,MAT_NO_OFF_PROC_ENTRIES,PETSC_FALSE);
6002: MatSetOption(*mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
6003: return(0);
6004: }
6006: /*
6007: Special version for direct calls from Fortran
6008: */
6009: #include <petsc/private/fortranimpl.h>
6011: /* Change these macros so can be used in void function */
6012: #undef CHKERRQ
6013: #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr)
6014: #undef SETERRQ2
6015: #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
6016: #undef SETERRQ3
6017: #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)
6018: #undef SETERRQ
6019: #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr)
6021: #if defined(PETSC_HAVE_FORTRAN_CAPS)
6022: #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ
6023: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
6024: #define matsetvaluesmpiaij_ matsetvaluesmpiaij
6025: #else
6026: #endif
6027: PETSC_EXTERN void matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr)
6028: {
6029: Mat mat = *mmat;
6030: PetscInt m = *mm, n = *mn;
6031: InsertMode addv = *maddv;
6032: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
6033: PetscScalar value;
6036: MatCheckPreallocated(mat,1);
6037: if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv;
6039: #if defined(PETSC_USE_DEBUG)
6040: else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
6041: #endif
6042: {
6043: PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend;
6044: PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col;
6045: PetscBool roworiented = aij->roworiented;
6047: /* Some Variables required in the macro */
6048: Mat A = aij->A;
6049: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
6050: PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
6051: MatScalar *aa = a->a;
6052: PetscBool ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES)) ? PETSC_TRUE : PETSC_FALSE);
6053: Mat B = aij->B;
6054: Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
6055: PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n;
6056: MatScalar *ba = b->a;
6057: /* This variable below is only for the PETSC_HAVE_VIENNACL or PETSC_HAVE_CUDA cases, but we define it in all cases because we
6058: * cannot use "#if defined" inside a macro. */
6059: PETSC_UNUSED PetscBool inserted = PETSC_FALSE;
6061: PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
6062: PetscInt nonew = a->nonew;
6063: MatScalar *ap1,*ap2;
6066: for (i=0; i<m; i++) {
6067: if (im[i] < 0) continue;
6068: #if defined(PETSC_USE_DEBUG)
6069: if (im[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1);
6070: #endif
6071: if (im[i] >= rstart && im[i] < rend) {
6072: row = im[i] - rstart;
6073: lastcol1 = -1;
6074: rp1 = aj + ai[row];
6075: ap1 = aa + ai[row];
6076: rmax1 = aimax[row];
6077: nrow1 = ailen[row];
6078: low1 = 0;
6079: high1 = nrow1;
6080: lastcol2 = -1;
6081: rp2 = bj + bi[row];
6082: ap2 = ba + bi[row];
6083: rmax2 = bimax[row];
6084: nrow2 = bilen[row];
6085: low2 = 0;
6086: high2 = nrow2;
6088: for (j=0; j<n; j++) {
6089: if (roworiented) value = v[i*n+j];
6090: else value = v[i+j*m];
6091: if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES) && im[i] != in[j]) continue;
6092: if (in[j] >= cstart && in[j] < cend) {
6093: col = in[j] - cstart;
6094: MatSetValues_SeqAIJ_A_Private(row,col,value,addv,im[i],in[j]);
6095: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
6096: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && inserted) A->offloadmask = PETSC_OFFLOAD_CPU;
6097: #endif
6098: } else if (in[j] < 0) continue;
6099: #if defined(PETSC_USE_DEBUG)
6100: /* extra brace on SETERRQ2() is required for --with-errorchecking=0 - due to the next 'else' clause */
6101: else if (in[j] >= mat->cmap->N) {SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[j],mat->cmap->N-1);}
6102: #endif
6103: else {
6104: if (mat->was_assembled) {
6105: if (!aij->colmap) {
6106: MatCreateColmap_MPIAIJ_Private(mat);
6107: }
6108: #if defined(PETSC_USE_CTABLE)
6109: PetscTableFind(aij->colmap,in[j]+1,&col);
6110: col--;
6111: #else
6112: col = aij->colmap[in[j]] - 1;
6113: #endif
6114: if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
6115: MatDisAssemble_MPIAIJ(mat);
6116: col = in[j];
6117: /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
6118: B = aij->B;
6119: b = (Mat_SeqAIJ*)B->data;
6120: bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
6121: rp2 = bj + bi[row];
6122: ap2 = ba + bi[row];
6123: rmax2 = bimax[row];
6124: nrow2 = bilen[row];
6125: low2 = 0;
6126: high2 = nrow2;
6127: bm = aij->B->rmap->n;
6128: ba = b->a;
6129: inserted = PETSC_FALSE;
6130: }
6131: } else col = in[j];
6132: MatSetValues_SeqAIJ_B_Private(row,col,value,addv,im[i],in[j]);
6133: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
6134: if (B->offloadmask != PETSC_OFFLOAD_UNALLOCATED && inserted) B->offloadmask = PETSC_OFFLOAD_CPU;
6135: #endif
6136: }
6137: }
6138: } else if (!aij->donotstash) {
6139: if (roworiented) {
6140: MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
6141: } else {
6142: MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));
6143: }
6144: }
6145: }
6146: }
6147: PetscFunctionReturnVoid();
6148: }