Actual source code: matis.c
petsc-3.13.6 2020-09-29
1: /*
2: Creates a matrix class for using the Neumann-Neumann type preconditioners.
3: This stores the matrices in globally unassembled form. Each processor
4: assembles only its local Neumann problem and the parallel matrix vector
5: product is handled "implicitly".
7: Currently this allows for only one subdomain per processor.
8: */
10: #include <../src/mat/impls/is/matis.h>
11: #include <petsc/private/sfimpl.h>
12: #include <petsc/private/vecimpl.h>
14: #define MATIS_MAX_ENTRIES_INSERTION 2048
15: static PetscErrorCode MatSetValuesLocal_IS(Mat,PetscInt,const PetscInt*,PetscInt,const PetscInt*,const PetscScalar*,InsertMode);
16: static PetscErrorCode MatSetValuesBlockedLocal_IS(Mat,PetscInt,const PetscInt*,PetscInt,const PetscInt*,const PetscScalar*,InsertMode);
17: static PetscErrorCode MatISSetUpScatters_Private(Mat);
19: static PetscErrorCode MatISContainerDestroyPtAP_Private(void *ptr)
20: {
21: MatISPtAP ptap = (MatISPtAP)ptr;
25: MatDestroySubMatrices(ptap->ris1 ? 2 : 1,&ptap->lP);
26: ISDestroy(&ptap->cis0);
27: ISDestroy(&ptap->cis1);
28: ISDestroy(&ptap->ris0);
29: ISDestroy(&ptap->ris1);
30: PetscFree(ptap);
31: return(0);
32: }
34: static PetscErrorCode MatPtAPNumeric_IS_XAIJ(Mat A, Mat P, Mat C)
35: {
36: MatISPtAP ptap;
37: Mat_IS *matis = (Mat_IS*)A->data;
38: Mat lA,lC;
39: MatReuse reuse;
40: IS ris[2],cis[2];
41: PetscContainer c;
42: PetscInt n;
46: PetscObjectQuery((PetscObject)C,"_MatIS_PtAP",(PetscObject*)&c);
47: if (!c) SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_PLIB,"Missing PtAP information");
48: PetscContainerGetPointer(c,(void**)&ptap);
49: ris[0] = ptap->ris0;
50: ris[1] = ptap->ris1;
51: cis[0] = ptap->cis0;
52: cis[1] = ptap->cis1;
53: n = ptap->ris1 ? 2 : 1;
54: reuse = ptap->lP ? MAT_REUSE_MATRIX : MAT_INITIAL_MATRIX;
55: MatCreateSubMatrices(P,n,ris,cis,reuse,&ptap->lP);
57: MatISGetLocalMat(A,&lA);
58: MatISGetLocalMat(C,&lC);
59: if (ptap->ris1) { /* unsymmetric A mapping */
60: Mat lPt;
62: MatTranspose(ptap->lP[1],MAT_INITIAL_MATRIX,&lPt);
63: MatMatMatMult(lPt,lA,ptap->lP[0],reuse,ptap->fill,&lC);
64: if (matis->storel2l) {
65: PetscObjectCompose((PetscObject)(A),"_MatIS_PtAP_l2l",(PetscObject)lPt);
66: }
67: MatDestroy(&lPt);
68: } else {
69: MatPtAP(lA,ptap->lP[0],reuse,ptap->fill,&lC);
70: if (matis->storel2l) {
71: PetscObjectCompose((PetscObject)C,"_MatIS_PtAP_l2l",(PetscObject)ptap->lP[0]);
72: }
73: }
74: if (reuse == MAT_INITIAL_MATRIX) {
75: MatISSetLocalMat(C,lC);
76: MatDestroy(&lC);
77: }
78: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
79: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
80: return(0);
81: }
83: static PetscErrorCode MatGetNonzeroColumnsLocal_Private(Mat PT,IS *cis)
84: {
85: Mat Po,Pd;
86: IS zd,zo;
87: const PetscInt *garray;
88: PetscInt *aux,i,bs;
89: PetscInt dc,stc,oc,ctd,cto;
90: PetscBool ismpiaij,ismpibaij,isseqaij,isseqbaij;
91: MPI_Comm comm;
97: PetscObjectGetComm((PetscObject)PT,&comm);
98: bs = 1;
99: PetscObjectBaseTypeCompare((PetscObject)PT,MATMPIAIJ,&ismpiaij);
100: PetscObjectBaseTypeCompare((PetscObject)PT,MATMPIBAIJ,&ismpibaij);
101: PetscObjectBaseTypeCompare((PetscObject)PT,MATSEQAIJ,&isseqaij);
102: PetscObjectTypeCompare((PetscObject)PT,MATSEQBAIJ,&isseqbaij);
103: if (isseqaij || isseqbaij) {
104: Pd = PT;
105: Po = NULL;
106: garray = NULL;
107: } else if (ismpiaij) {
108: MatMPIAIJGetSeqAIJ(PT,&Pd,&Po,&garray);
109: } else if (ismpibaij) {
110: MatMPIBAIJGetSeqBAIJ(PT,&Pd,&Po,&garray);
111: MatGetBlockSize(PT,&bs);
112: } else SETERRQ1(comm,PETSC_ERR_SUP,"Not for matrix type %s",((PetscObject)(PT))->type_name);
114: /* identify any null columns in Pd or Po */
115: /* We use a tolerance comparison since it may happen that, with geometric multigrid,
116: some of the columns are not really zero, but very close to */
117: zo = zd = NULL;
118: if (Po) {
119: MatFindNonzeroRowsOrCols_Basic(Po,PETSC_TRUE,PETSC_SMALL,&zo);
120: }
121: MatFindNonzeroRowsOrCols_Basic(Pd,PETSC_TRUE,PETSC_SMALL,&zd);
123: MatGetLocalSize(PT,NULL,&dc);
124: MatGetOwnershipRangeColumn(PT,&stc,NULL);
125: if (Po) { MatGetLocalSize(Po,NULL,&oc); }
126: else oc = 0;
127: PetscMalloc1((dc+oc)/bs,&aux);
128: if (zd) {
129: const PetscInt *idxs;
130: PetscInt nz;
132: /* this will throw an error if bs is not valid */
133: ISSetBlockSize(zd,bs);
134: ISGetLocalSize(zd,&nz);
135: ISGetIndices(zd,&idxs);
136: ctd = nz/bs;
137: for (i=0; i<ctd; i++) aux[i] = (idxs[bs*i]+stc)/bs;
138: ISRestoreIndices(zd,&idxs);
139: } else {
140: ctd = dc/bs;
141: for (i=0; i<ctd; i++) aux[i] = i+stc/bs;
142: }
143: if (zo) {
144: const PetscInt *idxs;
145: PetscInt nz;
147: /* this will throw an error if bs is not valid */
148: ISSetBlockSize(zo,bs);
149: ISGetLocalSize(zo,&nz);
150: ISGetIndices(zo,&idxs);
151: cto = nz/bs;
152: for (i=0; i<cto; i++) aux[i+ctd] = garray[idxs[bs*i]/bs];
153: ISRestoreIndices(zo,&idxs);
154: } else {
155: cto = oc/bs;
156: for (i=0; i<cto; i++) aux[i+ctd] = garray[i];
157: }
158: ISCreateBlock(comm,bs,ctd+cto,aux,PETSC_OWN_POINTER,cis);
159: ISDestroy(&zd);
160: ISDestroy(&zo);
161: return(0);
162: }
164: static PetscErrorCode MatPtAPSymbolic_IS_XAIJ(Mat A,Mat P,PetscReal fill,Mat C)
165: {
166: Mat PT,lA;
167: MatISPtAP ptap;
168: ISLocalToGlobalMapping Crl2g,Ccl2g,rl2g,cl2g;
169: PetscContainer c;
170: MatType lmtype;
171: const PetscInt *garray;
172: PetscInt ibs,N,dc;
173: MPI_Comm comm;
174: PetscErrorCode ierr;
177: PetscObjectGetComm((PetscObject)A,&comm);
178: MatSetType(C,MATIS);
179: MatISGetLocalMat(A,&lA);
180: MatGetType(lA,&lmtype);
181: MatISSetLocalMatType(C,lmtype);
182: MatGetSize(P,NULL,&N);
183: MatGetLocalSize(P,NULL,&dc);
184: MatSetSizes(C,dc,dc,N,N);
185: /* Not sure about this
186: MatGetBlockSizes(P,NULL,&ibs);
187: MatSetBlockSize(*C,ibs);
188: */
190: PetscNew(&ptap);
191: PetscContainerCreate(PETSC_COMM_SELF,&c);
192: PetscContainerSetPointer(c,ptap);
193: PetscContainerSetUserDestroy(c,MatISContainerDestroyPtAP_Private);
194: PetscObjectCompose((PetscObject)C,"_MatIS_PtAP",(PetscObject)c);
195: PetscContainerDestroy(&c);
196: ptap->fill = fill;
198: MatGetLocalToGlobalMapping(A,&rl2g,&cl2g);
200: ISLocalToGlobalMappingGetBlockSize(cl2g,&ibs);
201: ISLocalToGlobalMappingGetSize(cl2g,&N);
202: ISLocalToGlobalMappingGetBlockIndices(cl2g,&garray);
203: ISCreateBlock(comm,ibs,N/ibs,garray,PETSC_COPY_VALUES,&ptap->ris0);
204: ISLocalToGlobalMappingRestoreBlockIndices(cl2g,&garray);
206: MatCreateSubMatrix(P,ptap->ris0,NULL,MAT_INITIAL_MATRIX,&PT);
207: MatGetNonzeroColumnsLocal_Private(PT,&ptap->cis0);
208: ISLocalToGlobalMappingCreateIS(ptap->cis0,&Ccl2g);
209: MatDestroy(&PT);
211: Crl2g = NULL;
212: if (rl2g != cl2g) { /* unsymmetric A mapping */
213: PetscBool same,lsame = PETSC_FALSE;
214: PetscInt N1,ibs1;
216: ISLocalToGlobalMappingGetSize(rl2g,&N1);
217: ISLocalToGlobalMappingGetBlockSize(rl2g,&ibs1);
218: ISLocalToGlobalMappingGetBlockIndices(rl2g,&garray);
219: ISCreateBlock(comm,ibs,N/ibs,garray,PETSC_COPY_VALUES,&ptap->ris1);
220: ISLocalToGlobalMappingRestoreBlockIndices(rl2g,&garray);
221: if (ibs1 == ibs && N1 == N) { /* check if the l2gmaps are the same */
222: const PetscInt *i1,*i2;
224: ISBlockGetIndices(ptap->ris0,&i1);
225: ISBlockGetIndices(ptap->ris1,&i2);
226: PetscArraycmp(i1,i2,N,&lsame);
227: }
228: MPIU_Allreduce(&lsame,&same,1,MPIU_BOOL,MPI_LAND,comm);
229: if (same) {
230: ISDestroy(&ptap->ris1);
231: } else {
232: MatCreateSubMatrix(P,ptap->ris1,NULL,MAT_INITIAL_MATRIX,&PT);
233: MatGetNonzeroColumnsLocal_Private(PT,&ptap->cis1);
234: ISLocalToGlobalMappingCreateIS(ptap->cis1,&Crl2g);
235: MatDestroy(&PT);
236: }
237: }
238: /* Not sure about this
239: if (!Crl2g) {
240: MatGetBlockSize(C,&ibs);
241: ISLocalToGlobalMappingSetBlockSize(Ccl2g,ibs);
242: }
243: */
244: MatSetLocalToGlobalMapping(C,Crl2g ? Crl2g : Ccl2g,Ccl2g);
245: ISLocalToGlobalMappingDestroy(&Crl2g);
246: ISLocalToGlobalMappingDestroy(&Ccl2g);
248: C->ops->ptapnumeric = MatPtAPNumeric_IS_XAIJ;
249: return(0);
250: }
252: /* ----------------------------------------- */
253: static PetscErrorCode MatProductSymbolic_PtAP_IS_XAIJ(Mat C)
254: {
256: Mat_Product *product = C->product;
257: Mat A=product->A,P=product->B;
258: PetscReal fill=product->fill;
261: MatPtAPSymbolic_IS_XAIJ(A,P,fill,C);
262: C->ops->productnumeric = MatProductNumeric_PtAP;
263: return(0);
264: }
266: static PetscErrorCode MatProductSetFromOptions_IS_XAIJ_PtAP(Mat C)
267: {
269: C->ops->productsymbolic = MatProductSymbolic_PtAP_IS_XAIJ;
270: return(0);
271: }
273: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_IS_XAIJ(Mat C)
274: {
276: Mat_Product *product = C->product;
279: if (product->type == MATPRODUCT_PtAP) {
280: MatProductSetFromOptions_IS_XAIJ_PtAP(C);
281: } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatProduct type %s is not supported for IS and XAIJ matrices",MatProductTypes[product->type]);
282: return(0);
283: }
285: /* ----------------------------------------- */
286: static PetscErrorCode MatISContainerDestroyFields_Private(void *ptr)
287: {
288: MatISLocalFields lf = (MatISLocalFields)ptr;
289: PetscInt i;
290: PetscErrorCode ierr;
293: for (i=0;i<lf->nr;i++) {
294: ISDestroy(&lf->rf[i]);
295: }
296: for (i=0;i<lf->nc;i++) {
297: ISDestroy(&lf->cf[i]);
298: }
299: PetscFree2(lf->rf,lf->cf);
300: PetscFree(lf);
301: return(0);
302: }
304: static PetscErrorCode MatConvert_SeqXAIJ_IS(Mat A,MatType type,MatReuse reuse,Mat *newmat)
305: {
306: Mat B,lB;
310: if (reuse != MAT_REUSE_MATRIX) {
311: ISLocalToGlobalMapping rl2g,cl2g;
312: PetscInt bs;
313: IS is;
315: MatGetBlockSize(A,&bs);
316: ISCreateStride(PetscObjectComm((PetscObject)A),A->rmap->n/bs,0,1,&is);
317: if (bs > 1) {
318: IS is2;
319: PetscInt i,*aux;
321: ISGetLocalSize(is,&i);
322: ISGetIndices(is,(const PetscInt**)&aux);
323: ISCreateBlock(PetscObjectComm((PetscObject)A),bs,i,aux,PETSC_COPY_VALUES,&is2);
324: ISRestoreIndices(is,(const PetscInt**)&aux);
325: ISDestroy(&is);
326: is = is2;
327: }
328: ISSetIdentity(is);
329: ISLocalToGlobalMappingCreateIS(is,&rl2g);
330: ISDestroy(&is);
331: ISCreateStride(PetscObjectComm((PetscObject)A),A->cmap->n/bs,0,1,&is);
332: if (bs > 1) {
333: IS is2;
334: PetscInt i,*aux;
336: ISGetLocalSize(is,&i);
337: ISGetIndices(is,(const PetscInt**)&aux);
338: ISCreateBlock(PetscObjectComm((PetscObject)A),bs,i,aux,PETSC_COPY_VALUES,&is2);
339: ISRestoreIndices(is,(const PetscInt**)&aux);
340: ISDestroy(&is);
341: is = is2;
342: }
343: ISSetIdentity(is);
344: ISLocalToGlobalMappingCreateIS(is,&cl2g);
345: ISDestroy(&is);
346: MatCreateIS(PetscObjectComm((PetscObject)A),bs,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N,rl2g,cl2g,&B);
347: ISLocalToGlobalMappingDestroy(&rl2g);
348: ISLocalToGlobalMappingDestroy(&cl2g);
349: MatDuplicate(A,MAT_COPY_VALUES,&lB);
350: if (reuse == MAT_INITIAL_MATRIX) *newmat = B;
351: } else {
352: B = *newmat;
353: PetscObjectReference((PetscObject)A);
354: lB = A;
355: }
356: MatISSetLocalMat(B,lB);
357: MatDestroy(&lB);
358: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
359: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
360: if (reuse == MAT_INPLACE_MATRIX) {
361: MatHeaderReplace(A,&B);
362: }
363: return(0);
364: }
366: static PetscErrorCode MatISScaleDisassembling_Private(Mat A)
367: {
368: Mat_IS *matis = (Mat_IS*)(A->data);
369: PetscScalar *aa;
370: const PetscInt *ii,*jj;
371: PetscInt i,n,m;
372: PetscInt *ecount,**eneighs;
373: PetscBool flg;
377: MatGetRowIJ(matis->A,0,PETSC_FALSE,PETSC_FALSE,&m,&ii,&jj,&flg);
378: if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get IJ structure");
379: ISLocalToGlobalMappingGetNodeInfo(A->rmap->mapping,&n,&ecount,&eneighs);
380: if (m != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected %D != %D",m,n);
381: MatSeqAIJGetArray(matis->A,&aa);
382: for (i=0;i<n;i++) {
383: if (ecount[i] > 1) {
384: PetscInt j;
386: for (j=ii[i];j<ii[i+1];j++) {
387: PetscInt i2 = jj[j],p,p2;
388: PetscReal scal = 0.0;
390: for (p=0;p<ecount[i];p++) {
391: for (p2=0;p2<ecount[i2];p2++) {
392: if (eneighs[i][p] == eneighs[i2][p2]) { scal += 1.0; break; }
393: }
394: }
395: if (scal) aa[j] /= scal;
396: }
397: }
398: }
399: ISLocalToGlobalMappingRestoreNodeInfo(A->rmap->mapping,&n,&ecount,&eneighs);
400: MatSeqAIJRestoreArray(matis->A,&aa);
401: MatRestoreRowIJ(matis->A,0,PETSC_FALSE,PETSC_FALSE,&m,&ii,&jj,&flg);
402: if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot restore IJ structure");
403: return(0);
404: }
406: typedef enum {MAT_IS_DISASSEMBLE_L2G_NATURAL,MAT_IS_DISASSEMBLE_L2G_MAT, MAT_IS_DISASSEMBLE_L2G_ND} MatISDisassemblel2gType;
408: static PetscErrorCode MatMPIXAIJComputeLocalToGlobalMapping_Private(Mat A, ISLocalToGlobalMapping *l2g)
409: {
410: Mat Ad,Ao;
411: IS is,ndmap,ndsub;
412: MPI_Comm comm;
413: const PetscInt *garray,*ndmapi;
414: PetscInt bs,i,cnt,nl,*ncount,*ndmapc;
415: PetscBool ismpiaij,ismpibaij;
416: const char *const MatISDisassemblel2gTypes[] = {"NATURAL","MAT","ND","MatISDisassemblel2gType","MAT_IS_DISASSEMBLE_L2G_",0};
417: MatISDisassemblel2gType mode = MAT_IS_DISASSEMBLE_L2G_NATURAL;
418: MatPartitioning part;
419: PetscSF sf;
420: PetscErrorCode ierr;
423: PetscOptionsBegin(PetscObjectComm((PetscObject)A),((PetscObject)A)->prefix,"MatIS l2g disassembling options","Mat");
424: PetscOptionsEnum("-mat_is_disassemble_l2g_type","Type of local-to-global mapping to be used for disassembling","MatISDisassemblel2gType",MatISDisassemblel2gTypes,(PetscEnum)mode,(PetscEnum*)&mode,NULL);
425: PetscOptionsEnd();
426: if (mode == MAT_IS_DISASSEMBLE_L2G_MAT) {
427: MatGetLocalToGlobalMapping(A,l2g,NULL);
428: return(0);
429: }
430: PetscObjectGetComm((PetscObject)A,&comm);
431: PetscObjectBaseTypeCompare((PetscObject)A,MATMPIAIJ ,&ismpiaij);
432: PetscObjectBaseTypeCompare((PetscObject)A,MATMPIBAIJ,&ismpibaij);
433: MatGetBlockSize(A,&bs);
434: switch (mode) {
435: case MAT_IS_DISASSEMBLE_L2G_ND:
436: MatPartitioningCreate(comm,&part);
437: MatPartitioningSetAdjacency(part,A);
438: PetscObjectSetOptionsPrefix((PetscObject)part,((PetscObject)A)->prefix);
439: MatPartitioningSetFromOptions(part);
440: MatPartitioningApplyND(part,&ndmap);
441: MatPartitioningDestroy(&part);
442: ISBuildTwoSided(ndmap,NULL,&ndsub);
443: MatMPIAIJSetUseScalableIncreaseOverlap(A,PETSC_TRUE);
444: MatIncreaseOverlap(A,1,&ndsub,1);
445: ISLocalToGlobalMappingCreateIS(ndsub,l2g);
447: /* it may happen that a separator node is not properly shared */
448: ISLocalToGlobalMappingGetNodeInfo(*l2g,&nl,&ncount,NULL);
449: PetscSFCreate(comm,&sf);
450: ISLocalToGlobalMappingGetIndices(*l2g,&garray);
451: PetscSFSetGraphLayout(sf,A->rmap,nl,NULL,PETSC_OWN_POINTER,garray);
452: ISLocalToGlobalMappingRestoreIndices(*l2g,&garray);
453: PetscCalloc1(A->rmap->n,&ndmapc);
454: PetscSFReduceBegin(sf,MPIU_INT,ncount,ndmapc,MPIU_REPLACE);
455: PetscSFReduceEnd(sf,MPIU_INT,ncount,ndmapc,MPIU_REPLACE);
456: ISLocalToGlobalMappingRestoreNodeInfo(*l2g,NULL,&ncount,NULL);
457: ISGetIndices(ndmap,&ndmapi);
458: for (i = 0, cnt = 0; i < A->rmap->n; i++)
459: if (ndmapi[i] < 0 && ndmapc[i] < 2)
460: cnt++;
462: MPIU_Allreduce(&cnt,&i,1,MPIU_INT,MPI_MAX,comm);
463: if (i) { /* we detected isolated separator nodes */
464: Mat A2,A3;
465: IS *workis,is2;
466: PetscScalar *vals;
467: PetscInt gcnt = i,*dnz,*onz,j,*lndmapi;
468: ISLocalToGlobalMapping ll2g;
469: PetscBool flg;
470: const PetscInt *ii,*jj;
472: /* communicate global id of separators */
473: MatPreallocateInitialize(comm,A->rmap->n,A->cmap->n,dnz,onz);
474: for (i = 0, cnt = 0; i < A->rmap->n; i++)
475: dnz[i] = ndmapi[i] < 0 ? i + A->rmap->rstart : -1;
477: PetscMalloc1(nl,&lndmapi);
478: PetscSFBcastBegin(sf,MPIU_INT,dnz,lndmapi);
480: /* compute adjacency of isolated separators node */
481: PetscMalloc1(gcnt,&workis);
482: for (i = 0, cnt = 0; i < A->rmap->n; i++) {
483: if (ndmapi[i] < 0 && ndmapc[i] < 2) {
484: ISCreateStride(comm,1,i+A->rmap->rstart,1,&workis[cnt++]);
485: }
486: }
487: for (i = cnt; i < gcnt; i++) {
488: ISCreateStride(comm,0,0,1,&workis[i]);
489: }
490: for (i = 0; i < gcnt; i++) {
491: PetscObjectSetName((PetscObject)workis[i],"ISOLATED");
492: ISViewFromOptions(workis[i],NULL,"-view_isolated_separators");
493: }
495: /* no communications since all the ISes correspond to locally owned rows */
496: MatIncreaseOverlap(A,gcnt,workis,1);
498: /* end communicate global id of separators */
499: PetscSFBcastEnd(sf,MPIU_INT,dnz,lndmapi);
501: /* communicate new layers : create a matrix and transpose it */
502: PetscArrayzero(dnz,A->rmap->n);
503: PetscArrayzero(onz,A->rmap->n);
504: for (i = 0, j = 0; i < A->rmap->n; i++) {
505: if (ndmapi[i] < 0 && ndmapc[i] < 2) {
506: const PetscInt* idxs;
507: PetscInt s;
509: ISGetLocalSize(workis[j],&s);
510: ISGetIndices(workis[j],&idxs);
511: MatPreallocateSet(i+A->rmap->rstart,s,idxs,dnz,onz);
512: j++;
513: }
514: }
515: if (j != cnt) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected local count %D != %D",j,cnt);
517: for (i = 0; i < gcnt; i++) {
518: PetscObjectSetName((PetscObject)workis[i],"EXTENDED");
519: ISViewFromOptions(workis[i],NULL,"-view_isolated_separators");
520: }
522: for (i = 0, j = 0; i < A->rmap->n; i++) j = PetscMax(j,dnz[i]+onz[i]);
523: PetscMalloc1(j,&vals);
524: for (i = 0; i < j; i++) vals[i] = 1.0;
526: MatCreate(comm,&A2);
527: MatSetType(A2,MATMPIAIJ);
528: MatSetSizes(A2,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);
529: MatMPIAIJSetPreallocation(A2,0,dnz,0,onz);
530: MatSetOption(A2,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE);
531: for (i = 0, j = 0; i < A2->rmap->n; i++) {
532: PetscInt row = i+A2->rmap->rstart,s = dnz[i] + onz[i];
533: const PetscInt* idxs;
535: if (s) {
536: ISGetIndices(workis[j],&idxs);
537: MatSetValues(A2,1,&row,s,idxs,vals,INSERT_VALUES);
538: ISRestoreIndices(workis[j],&idxs);
539: j++;
540: }
541: }
542: if (j != cnt) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected local count %D != %D",j,cnt);
543: PetscFree(vals);
544: MatAssemblyBegin(A2,MAT_FINAL_ASSEMBLY);
545: MatAssemblyEnd(A2,MAT_FINAL_ASSEMBLY);
546: MatTranspose(A2,MAT_INPLACE_MATRIX,&A2);
548: /* extract submatrix corresponding to the coupling "owned separators" x "isolated separators" */
549: for (i = 0, j = 0; i < nl; i++)
550: if (lndmapi[i] >= 0) lndmapi[j++] = lndmapi[i];
551: ISCreateGeneral(comm,j,lndmapi,PETSC_USE_POINTER,&is);
552: MatMPIAIJGetLocalMatCondensed(A2,MAT_INITIAL_MATRIX,&is,NULL,&A3);
553: ISDestroy(&is);
554: MatDestroy(&A2);
556: /* extend local to global map to include connected isolated separators */
557: PetscObjectQuery((PetscObject)A3,"_petsc_GetLocalMatCondensed_iscol",(PetscObject*)&is);
558: if (!is) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Missing column map");
559: ISLocalToGlobalMappingCreateIS(is,&ll2g);
560: MatGetRowIJ(A3,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&flg);
561: if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get IJ structure");
562: ISCreateGeneral(PETSC_COMM_SELF,ii[i],jj,PETSC_COPY_VALUES,&is);
563: MatRestoreRowIJ(A3,0,PETSC_FALSE,PETSC_FALSE,&i,&ii,&jj,&flg);
564: if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get IJ structure");
565: ISLocalToGlobalMappingApplyIS(ll2g,is,&is2);
566: ISDestroy(&is);
567: ISLocalToGlobalMappingDestroy(&ll2g);
569: /* add new nodes to the local-to-global map */
570: ISLocalToGlobalMappingDestroy(l2g);
571: ISExpand(ndsub,is2,&is);
572: ISDestroy(&is2);
573: ISLocalToGlobalMappingCreateIS(is,l2g);
574: ISDestroy(&is);
576: MatDestroy(&A3);
577: PetscFree(lndmapi);
578: MatPreallocateFinalize(dnz,onz);
579: for (i = 0; i < gcnt; i++) {
580: ISDestroy(&workis[i]);
581: }
582: PetscFree(workis);
583: }
584: ISRestoreIndices(ndmap,&ndmapi);
585: PetscSFDestroy(&sf);
586: PetscFree(ndmapc);
587: ISDestroy(&ndmap);
588: ISDestroy(&ndsub);
589: ISLocalToGlobalMappingSetBlockSize(*l2g,bs);
590: ISLocalToGlobalMappingViewFromOptions(*l2g,NULL,"-matis_nd_l2g_view");
591: break;
592: case MAT_IS_DISASSEMBLE_L2G_NATURAL:
593: if (ismpiaij) {
594: MatMPIAIJGetSeqAIJ(A,&Ad,&Ao,&garray);
595: } else if (ismpibaij) {
596: MatMPIBAIJGetSeqBAIJ(A,&Ad,&Ao,&garray);
597: } else SETERRQ1(comm,PETSC_ERR_SUP,"Type %s",((PetscObject)A)->type_name);
598: if (!garray) SETERRQ(comm,PETSC_ERR_ARG_WRONGSTATE,"garray not present");
599: if (A->rmap->n) {
600: PetscInt dc,oc,stc,*aux;
602: MatGetLocalSize(A,NULL,&dc);
603: MatGetLocalSize(Ao,NULL,&oc);
604: MatGetOwnershipRangeColumn(A,&stc,NULL);
605: PetscMalloc1((dc+oc)/bs,&aux);
606: for (i=0; i<dc/bs; i++) aux[i] = i+stc/bs;
607: for (i=0; i<oc/bs; i++) aux[i+dc/bs] = garray[i];
608: ISCreateBlock(comm,bs,(dc+oc)/bs,aux,PETSC_OWN_POINTER,&is);
609: } else {
610: ISCreateBlock(comm,1,0,NULL,PETSC_OWN_POINTER,&is);
611: }
612: ISLocalToGlobalMappingCreateIS(is,l2g);
613: ISDestroy(&is);
614: break;
615: default:
616: SETERRQ1(comm,PETSC_ERR_ARG_WRONG,"Unsupported l2g disassembling type %D",mode);
617: }
618: return(0);
619: }
621: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat A,MatType type,MatReuse reuse,Mat *newmat)
622: {
623: Mat lA,Ad,Ao,B = NULL;
624: ISLocalToGlobalMapping rl2g,cl2g;
625: IS is;
626: MPI_Comm comm;
627: void *ptrs[2];
628: const char *names[2] = {"_convert_csr_aux","_convert_csr_data"};
629: const PetscInt *garray;
630: PetscScalar *dd,*od,*aa,*data;
631: const PetscInt *di,*dj,*oi,*oj;
632: const PetscInt *odi,*odj,*ooi,*ooj;
633: PetscInt *aux,*ii,*jj;
634: PetscInt bs,lc,dr,dc,oc,str,stc,nnz,i,jd,jo,cum;
635: PetscBool flg,ismpiaij,ismpibaij,was_inplace = PETSC_FALSE;
636: PetscMPIInt size;
637: PetscErrorCode ierr;
640: PetscObjectGetComm((PetscObject)A,&comm);
641: MPI_Comm_size(comm,&size);
642: if (size == 1) {
643: MatConvert_SeqXAIJ_IS(A,type,reuse,newmat);
644: return(0);
645: }
646: if (reuse != MAT_REUSE_MATRIX && A->cmap->N == A->rmap->N) {
647: MatMPIXAIJComputeLocalToGlobalMapping_Private(A,&rl2g);
648: MatCreate(comm,&B);
649: MatSetType(B,MATIS);
650: MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);
651: MatSetLocalToGlobalMapping(B,rl2g,rl2g);
652: MatGetBlockSize(A,&bs);
653: MatSetBlockSize(B,bs);
654: ISLocalToGlobalMappingDestroy(&rl2g);
655: if (reuse == MAT_INPLACE_MATRIX) was_inplace = PETSC_TRUE;
656: reuse = MAT_REUSE_MATRIX;
657: }
658: if (reuse == MAT_REUSE_MATRIX) {
659: Mat *newlA, lA;
660: IS rows, cols;
661: const PetscInt *ridx, *cidx;
662: PetscInt rbs, cbs, nr, nc;
664: if (!B) B = *newmat;
665: MatGetLocalToGlobalMapping(B,&rl2g,&cl2g);
666: ISLocalToGlobalMappingGetBlockIndices(rl2g,&ridx);
667: ISLocalToGlobalMappingGetBlockIndices(cl2g,&cidx);
668: ISLocalToGlobalMappingGetSize(rl2g,&nr);
669: ISLocalToGlobalMappingGetSize(cl2g,&nc);
670: ISLocalToGlobalMappingGetBlockSize(rl2g,&rbs);
671: ISLocalToGlobalMappingGetBlockSize(cl2g,&cbs);
672: ISCreateBlock(comm,rbs,nr/rbs,ridx,PETSC_USE_POINTER,&rows);
673: if (rl2g != cl2g) {
674: ISCreateBlock(comm,cbs,nc/cbs,cidx,PETSC_USE_POINTER,&cols);
675: } else {
676: PetscObjectReference((PetscObject)rows);
677: cols = rows;
678: }
679: MatISGetLocalMat(B,&lA);
680: MatCreateSubMatrices(A,1,&rows,&cols,MAT_INITIAL_MATRIX,&newlA);
681: MatConvert(newlA[0],MATSEQAIJ,MAT_INPLACE_MATRIX,&newlA[0]);
682: ISLocalToGlobalMappingRestoreBlockIndices(rl2g,&ridx);
683: ISLocalToGlobalMappingRestoreBlockIndices(cl2g,&cidx);
684: ISDestroy(&rows);
685: ISDestroy(&cols);
686: if (!lA->preallocated) { /* first time */
687: MatDuplicate(newlA[0],MAT_COPY_VALUES,&lA);
688: MatISSetLocalMat(B,lA);
689: PetscObjectDereference((PetscObject)lA);
690: }
691: MatCopy(newlA[0],lA,SAME_NONZERO_PATTERN);
692: MatDestroySubMatrices(1,&newlA);
693: MatISScaleDisassembling_Private(B);
694: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
695: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
696: if (was_inplace) { MatHeaderReplace(A,&B); }
697: else *newmat = B;
698: return(0);
699: }
700: /* rectangular case, just compress out the column space */
701: PetscObjectBaseTypeCompare((PetscObject)A,MATMPIAIJ ,&ismpiaij);
702: PetscObjectBaseTypeCompare((PetscObject)A,MATMPIBAIJ,&ismpibaij);
703: if (ismpiaij) {
704: bs = 1;
705: MatMPIAIJGetSeqAIJ(A,&Ad,&Ao,&garray);
706: } else if (ismpibaij) {
707: MatGetBlockSize(A,&bs);
708: MatMPIBAIJGetSeqBAIJ(A,&Ad,&Ao,&garray);
709: MatConvert(Ad,MATSEQAIJ,MAT_INITIAL_MATRIX,&Ad);
710: MatConvert(Ao,MATSEQAIJ,MAT_INITIAL_MATRIX,&Ao);
711: } else SETERRQ1(comm,PETSC_ERR_SUP,"Type %s",((PetscObject)A)->type_name);
712: MatSeqAIJGetArray(Ad,&dd);
713: MatSeqAIJGetArray(Ao,&od);
714: if (!garray) SETERRQ(comm,PETSC_ERR_ARG_WRONGSTATE,"garray not present");
716: /* access relevant information from MPIAIJ */
717: MatGetOwnershipRange(A,&str,NULL);
718: MatGetOwnershipRangeColumn(A,&stc,NULL);
719: MatGetLocalSize(A,&dr,&dc);
720: MatGetLocalSize(Ao,NULL,&oc);
721: MatGetRowIJ(Ad,0,PETSC_FALSE,PETSC_FALSE,&i,&di,&dj,&flg);
722: if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get IJ structure");
723: MatGetRowIJ(Ao,0,PETSC_FALSE,PETSC_FALSE,&i,&oi,&oj,&flg);
724: if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get IJ structure");
725: nnz = di[dr] + oi[dr];
726: /* store original pointers to be restored later */
727: odi = di; odj = dj; ooi = oi; ooj = oj;
729: /* generate l2g maps for rows and cols */
730: ISCreateStride(comm,dr/bs,str/bs,1,&is);
731: if (bs > 1) {
732: IS is2;
734: ISGetLocalSize(is,&i);
735: ISGetIndices(is,(const PetscInt**)&aux);
736: ISCreateBlock(comm,bs,i,aux,PETSC_COPY_VALUES,&is2);
737: ISRestoreIndices(is,(const PetscInt**)&aux);
738: ISDestroy(&is);
739: is = is2;
740: }
741: ISLocalToGlobalMappingCreateIS(is,&rl2g);
742: ISDestroy(&is);
743: if (dr) {
744: PetscMalloc1((dc+oc)/bs,&aux);
745: for (i=0; i<dc/bs; i++) aux[i] = i+stc/bs;
746: for (i=0; i<oc/bs; i++) aux[i+dc/bs] = garray[i];
747: ISCreateBlock(comm,bs,(dc+oc)/bs,aux,PETSC_OWN_POINTER,&is);
748: lc = dc+oc;
749: } else {
750: ISCreateBlock(comm,bs,0,NULL,PETSC_OWN_POINTER,&is);
751: lc = 0;
752: }
753: ISLocalToGlobalMappingCreateIS(is,&cl2g);
754: ISDestroy(&is);
756: /* create MATIS object */
757: MatCreate(comm,&B);
758: MatSetSizes(B,dr,dc,PETSC_DECIDE,PETSC_DECIDE);
759: MatSetType(B,MATIS);
760: MatSetBlockSize(B,bs);
761: MatSetLocalToGlobalMapping(B,rl2g,cl2g);
762: ISLocalToGlobalMappingDestroy(&rl2g);
763: ISLocalToGlobalMappingDestroy(&cl2g);
765: /* merge local matrices */
766: PetscMalloc1(nnz+dr+1,&aux);
767: PetscMalloc1(nnz,&data);
768: ii = aux;
769: jj = aux+dr+1;
770: aa = data;
771: *ii = *(di++) + *(oi++);
772: for (jd=0,jo=0,cum=0;*ii<nnz;cum++)
773: {
774: for (;jd<*di;jd++) { *jj++ = *dj++; *aa++ = *dd++; }
775: for (;jo<*oi;jo++) { *jj++ = *oj++ + dc; *aa++ = *od++; }
776: *(++ii) = *(di++) + *(oi++);
777: }
778: for (;cum<dr;cum++) *(++ii) = nnz;
780: MatRestoreRowIJ(Ad,0,PETSC_FALSE,PETSC_FALSE,&i,&odi,&odj,&flg);
781: if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot restore IJ structure");
782: MatRestoreRowIJ(Ao,0,PETSC_FALSE,PETSC_FALSE,&i,&ooi,&ooj,&flg);
783: if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot restore IJ structure");
784: MatSeqAIJRestoreArray(Ad,&dd);
785: MatSeqAIJRestoreArray(Ao,&od);
787: ii = aux;
788: jj = aux+dr+1;
789: aa = data;
790: MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,dr,lc,ii,jj,aa,&lA);
792: /* create containers to destroy the data */
793: ptrs[0] = aux;
794: ptrs[1] = data;
795: for (i=0; i<2; i++) {
796: PetscContainer c;
798: PetscContainerCreate(PETSC_COMM_SELF,&c);
799: PetscContainerSetPointer(c,ptrs[i]);
800: PetscContainerSetUserDestroy(c,PetscContainerUserDestroyDefault);
801: PetscObjectCompose((PetscObject)lA,names[i],(PetscObject)c);
802: PetscContainerDestroy(&c);
803: }
804: if (ismpibaij) { /* destroy converted local matrices */
805: MatDestroy(&Ad);
806: MatDestroy(&Ao);
807: }
809: /* finalize matrix */
810: MatISSetLocalMat(B,lA);
811: MatDestroy(&lA);
812: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
813: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
814: if (reuse == MAT_INPLACE_MATRIX) {
815: MatHeaderReplace(A,&B);
816: } else *newmat = B;
817: return(0);
818: }
820: PETSC_INTERN PetscErrorCode MatConvert_Nest_IS(Mat A,MatType type,MatReuse reuse,Mat *newmat)
821: {
822: Mat **nest,*snest,**rnest,lA,B;
823: IS *iscol,*isrow,*islrow,*islcol;
824: ISLocalToGlobalMapping rl2g,cl2g;
825: MPI_Comm comm;
826: PetscInt *lr,*lc,*l2gidxs;
827: PetscInt i,j,nr,nc,rbs,cbs;
828: PetscBool convert,lreuse,*istrans;
829: PetscErrorCode ierr;
832: MatNestGetSubMats(A,&nr,&nc,&nest);
833: lreuse = PETSC_FALSE;
834: rnest = NULL;
835: if (reuse == MAT_REUSE_MATRIX) {
836: PetscBool ismatis,isnest;
838: PetscObjectTypeCompare((PetscObject)*newmat,MATIS,&ismatis);
839: if (!ismatis) SETERRQ1(PetscObjectComm((PetscObject)*newmat),PETSC_ERR_USER,"Cannot reuse matrix of type %s",((PetscObject)(*newmat))->type_name);
840: MatISGetLocalMat(*newmat,&lA);
841: PetscObjectTypeCompare((PetscObject)lA,MATNEST,&isnest);
842: if (isnest) {
843: MatNestGetSubMats(lA,&i,&j,&rnest);
844: lreuse = (PetscBool)(i == nr && j == nc);
845: if (!lreuse) rnest = NULL;
846: }
847: }
848: PetscObjectGetComm((PetscObject)A,&comm);
849: PetscCalloc2(nr,&lr,nc,&lc);
850: PetscCalloc6(nr,&isrow,nc,&iscol,nr,&islrow,nc,&islcol,nr*nc,&snest,nr*nc,&istrans);
851: MatNestGetISs(A,isrow,iscol);
852: for (i=0;i<nr;i++) {
853: for (j=0;j<nc;j++) {
854: PetscBool ismatis;
855: PetscInt l1,l2,lb1,lb2,ij=i*nc+j;
857: /* Null matrix pointers are allowed in MATNEST */
858: if (!nest[i][j]) continue;
860: /* Nested matrices should be of type MATIS */
861: PetscObjectTypeCompare((PetscObject)nest[i][j],MATTRANSPOSEMAT,&istrans[ij]);
862: if (istrans[ij]) {
863: Mat T,lT;
864: MatTransposeGetMat(nest[i][j],&T);
865: PetscObjectTypeCompare((PetscObject)T,MATIS,&ismatis);
866: if (!ismatis) SETERRQ2(comm,PETSC_ERR_SUP,"Cannot convert from MATNEST to MATIS! Matrix block (%D,%D) (transposed) is not of type MATIS",i,j);
867: MatISGetLocalMat(T,&lT);
868: MatCreateTranspose(lT,&snest[ij]);
869: } else {
870: PetscObjectTypeCompare((PetscObject)nest[i][j],MATIS,&ismatis);
871: if (!ismatis) SETERRQ2(comm,PETSC_ERR_SUP,"Cannot convert from MATNEST to MATIS! Matrix block (%D,%D) is not of type MATIS",i,j);
872: MatISGetLocalMat(nest[i][j],&snest[ij]);
873: }
875: /* Check compatibility of local sizes */
876: MatGetSize(snest[ij],&l1,&l2);
877: MatGetBlockSizes(snest[ij],&lb1,&lb2);
878: if (!l1 || !l2) continue;
879: if (lr[i] && l1 != lr[i]) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot convert from MATNEST to MATIS! Matrix block (%D,%D) has invalid local size %D != %D",i,j,lr[i],l1);
880: if (lc[j] && l2 != lc[j]) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot convert from MATNEST to MATIS! Matrix block (%D,%D) has invalid local size %D != %D",i,j,lc[j],l2);
881: lr[i] = l1;
882: lc[j] = l2;
884: /* check compatibilty for local matrix reusage */
885: if (rnest && !rnest[i][j] != !snest[ij]) lreuse = PETSC_FALSE;
886: }
887: }
889: #if defined (PETSC_USE_DEBUG)
890: /* Check compatibility of l2g maps for rows */
891: for (i=0;i<nr;i++) {
892: rl2g = NULL;
893: for (j=0;j<nc;j++) {
894: PetscInt n1,n2;
896: if (!nest[i][j]) continue;
897: if (istrans[i*nc+j]) {
898: Mat T;
900: MatTransposeGetMat(nest[i][j],&T);
901: MatGetLocalToGlobalMapping(T,NULL,&cl2g);
902: } else {
903: MatGetLocalToGlobalMapping(nest[i][j],&cl2g,NULL);
904: }
905: ISLocalToGlobalMappingGetSize(cl2g,&n1);
906: if (!n1) continue;
907: if (!rl2g) {
908: rl2g = cl2g;
909: } else {
910: const PetscInt *idxs1,*idxs2;
911: PetscBool same;
913: ISLocalToGlobalMappingGetSize(rl2g,&n2);
914: if (n1 != n2) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot convert from MATNEST to MATIS! Matrix block (%D,%D) has invalid row l2gmap size %D != %D",i,j,n1,n2);
915: ISLocalToGlobalMappingGetIndices(cl2g,&idxs1);
916: ISLocalToGlobalMappingGetIndices(rl2g,&idxs2);
917: PetscArraycmp(idxs1,idxs2,n1,&same);
918: ISLocalToGlobalMappingRestoreIndices(cl2g,&idxs1);
919: ISLocalToGlobalMappingRestoreIndices(rl2g,&idxs2);
920: if (!same) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot convert from MATNEST to MATIS! Matrix block (%D,%D) has invalid row l2gmap",i,j);
921: }
922: }
923: }
924: /* Check compatibility of l2g maps for columns */
925: for (i=0;i<nc;i++) {
926: rl2g = NULL;
927: for (j=0;j<nr;j++) {
928: PetscInt n1,n2;
930: if (!nest[j][i]) continue;
931: if (istrans[j*nc+i]) {
932: Mat T;
934: MatTransposeGetMat(nest[j][i],&T);
935: MatGetLocalToGlobalMapping(T,&cl2g,NULL);
936: } else {
937: MatGetLocalToGlobalMapping(nest[j][i],NULL,&cl2g);
938: }
939: ISLocalToGlobalMappingGetSize(cl2g,&n1);
940: if (!n1) continue;
941: if (!rl2g) {
942: rl2g = cl2g;
943: } else {
944: const PetscInt *idxs1,*idxs2;
945: PetscBool same;
947: ISLocalToGlobalMappingGetSize(rl2g,&n2);
948: if (n1 != n2) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot convert from MATNEST to MATIS! Matrix block (%D,%D) has invalid column l2gmap size %D != %D",j,i,n1,n2);
949: ISLocalToGlobalMappingGetIndices(cl2g,&idxs1);
950: ISLocalToGlobalMappingGetIndices(rl2g,&idxs2);
951: PetscArraycmp(idxs1,idxs2,n1,&same);
952: ISLocalToGlobalMappingRestoreIndices(cl2g,&idxs1);
953: ISLocalToGlobalMappingRestoreIndices(rl2g,&idxs2);
954: if (!same) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot convert from MATNEST to MATIS! Matrix block (%D,%D) has invalid column l2gmap",j,i);
955: }
956: }
957: }
958: #endif
960: B = NULL;
961: if (reuse != MAT_REUSE_MATRIX) {
962: PetscInt stl;
964: /* Create l2g map for the rows of the new matrix and index sets for the local MATNEST */
965: for (i=0,stl=0;i<nr;i++) stl += lr[i];
966: PetscMalloc1(stl,&l2gidxs);
967: for (i=0,stl=0;i<nr;i++) {
968: Mat usedmat;
969: Mat_IS *matis;
970: const PetscInt *idxs;
972: /* local IS for local NEST */
973: ISCreateStride(PETSC_COMM_SELF,lr[i],stl,1,&islrow[i]);
975: /* l2gmap */
976: j = 0;
977: usedmat = nest[i][j];
978: while (!usedmat && j < nc-1) usedmat = nest[i][++j];
979: if (!usedmat) SETERRQ(comm,PETSC_ERR_SUP,"Cannot find valid row mat");
981: if (istrans[i*nc+j]) {
982: Mat T;
983: MatTransposeGetMat(usedmat,&T);
984: usedmat = T;
985: }
986: matis = (Mat_IS*)(usedmat->data);
987: ISGetIndices(isrow[i],&idxs);
988: if (istrans[i*nc+j]) {
989: PetscSFBcastBegin(matis->csf,MPIU_INT,idxs,l2gidxs+stl);
990: PetscSFBcastEnd(matis->csf,MPIU_INT,idxs,l2gidxs+stl);
991: } else {
992: PetscSFBcastBegin(matis->sf,MPIU_INT,idxs,l2gidxs+stl);
993: PetscSFBcastEnd(matis->sf,MPIU_INT,idxs,l2gidxs+stl);
994: }
995: ISRestoreIndices(isrow[i],&idxs);
996: stl += lr[i];
997: }
998: ISLocalToGlobalMappingCreate(comm,1,stl,l2gidxs,PETSC_OWN_POINTER,&rl2g);
1000: /* Create l2g map for columns of the new matrix and index sets for the local MATNEST */
1001: for (i=0,stl=0;i<nc;i++) stl += lc[i];
1002: PetscMalloc1(stl,&l2gidxs);
1003: for (i=0,stl=0;i<nc;i++) {
1004: Mat usedmat;
1005: Mat_IS *matis;
1006: const PetscInt *idxs;
1008: /* local IS for local NEST */
1009: ISCreateStride(PETSC_COMM_SELF,lc[i],stl,1,&islcol[i]);
1011: /* l2gmap */
1012: j = 0;
1013: usedmat = nest[j][i];
1014: while (!usedmat && j < nr-1) usedmat = nest[++j][i];
1015: if (!usedmat) SETERRQ(comm,PETSC_ERR_SUP,"Cannot find valid column mat");
1016: if (istrans[j*nc+i]) {
1017: Mat T;
1018: MatTransposeGetMat(usedmat,&T);
1019: usedmat = T;
1020: }
1021: matis = (Mat_IS*)(usedmat->data);
1022: ISGetIndices(iscol[i],&idxs);
1023: if (istrans[j*nc+i]) {
1024: PetscSFBcastBegin(matis->sf,MPIU_INT,idxs,l2gidxs+stl);
1025: PetscSFBcastEnd(matis->sf,MPIU_INT,idxs,l2gidxs+stl);
1026: } else {
1027: PetscSFBcastBegin(matis->csf,MPIU_INT,idxs,l2gidxs+stl);
1028: PetscSFBcastEnd(matis->csf,MPIU_INT,idxs,l2gidxs+stl);
1029: }
1030: ISRestoreIndices(iscol[i],&idxs);
1031: stl += lc[i];
1032: }
1033: ISLocalToGlobalMappingCreate(comm,1,stl,l2gidxs,PETSC_OWN_POINTER,&cl2g);
1035: /* Create MATIS */
1036: MatCreate(comm,&B);
1037: MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);
1038: MatGetBlockSizes(A,&rbs,&cbs);
1039: MatSetBlockSizes(B,rbs,cbs);
1040: MatSetType(B,MATIS);
1041: MatISSetLocalMatType(B,MATNEST);
1042: { /* hack : avoid setup of scatters */
1043: Mat_IS *matis = (Mat_IS*)(B->data);
1044: matis->islocalref = PETSC_TRUE;
1045: }
1046: MatSetLocalToGlobalMapping(B,rl2g,cl2g);
1047: ISLocalToGlobalMappingDestroy(&rl2g);
1048: ISLocalToGlobalMappingDestroy(&cl2g);
1049: MatCreateNest(PETSC_COMM_SELF,nr,islrow,nc,islcol,snest,&lA);
1050: MatNestSetVecType(lA,VECNEST);
1051: for (i=0;i<nr*nc;i++) {
1052: if (istrans[i]) {
1053: MatDestroy(&snest[i]);
1054: }
1055: }
1056: MatISSetLocalMat(B,lA);
1057: MatDestroy(&lA);
1058: { /* hack : setup of scatters done here */
1059: Mat_IS *matis = (Mat_IS*)(B->data);
1061: matis->islocalref = PETSC_FALSE;
1062: MatISSetUpScatters_Private(B);
1063: }
1064: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
1065: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
1066: if (reuse == MAT_INPLACE_MATRIX) {
1067: MatHeaderReplace(A,&B);
1068: } else {
1069: *newmat = B;
1070: }
1071: } else {
1072: if (lreuse) {
1073: MatISGetLocalMat(*newmat,&lA);
1074: for (i=0;i<nr;i++) {
1075: for (j=0;j<nc;j++) {
1076: if (snest[i*nc+j]) {
1077: MatNestSetSubMat(lA,i,j,snest[i*nc+j]);
1078: if (istrans[i*nc+j]) {
1079: MatDestroy(&snest[i*nc+j]);
1080: }
1081: }
1082: }
1083: }
1084: } else {
1085: PetscInt stl;
1086: for (i=0,stl=0;i<nr;i++) {
1087: ISCreateStride(PETSC_COMM_SELF,lr[i],stl,1,&islrow[i]);
1088: stl += lr[i];
1089: }
1090: for (i=0,stl=0;i<nc;i++) {
1091: ISCreateStride(PETSC_COMM_SELF,lc[i],stl,1,&islcol[i]);
1092: stl += lc[i];
1093: }
1094: MatCreateNest(PETSC_COMM_SELF,nr,islrow,nc,islcol,snest,&lA);
1095: for (i=0;i<nr*nc;i++) {
1096: if (istrans[i]) {
1097: MatDestroy(&snest[i]);
1098: }
1099: }
1100: MatISSetLocalMat(*newmat,lA);
1101: MatDestroy(&lA);
1102: }
1103: MatAssemblyBegin(*newmat,MAT_FINAL_ASSEMBLY);
1104: MatAssemblyEnd(*newmat,MAT_FINAL_ASSEMBLY);
1105: }
1107: /* Create local matrix in MATNEST format */
1108: convert = PETSC_FALSE;
1109: PetscOptionsGetBool(NULL,((PetscObject)A)->prefix,"-matis_convert_local_nest",&convert,NULL);
1110: if (convert) {
1111: Mat M;
1112: MatISLocalFields lf;
1113: PetscContainer c;
1115: MatISGetLocalMat(*newmat,&lA);
1116: MatConvert(lA,MATAIJ,MAT_INITIAL_MATRIX,&M);
1117: MatISSetLocalMat(*newmat,M);
1118: MatDestroy(&M);
1120: /* attach local fields to the matrix */
1121: PetscNew(&lf);
1122: PetscMalloc2(nr,&lf->rf,nc,&lf->cf);
1123: for (i=0;i<nr;i++) {
1124: PetscInt n,st;
1126: ISGetLocalSize(islrow[i],&n);
1127: ISStrideGetInfo(islrow[i],&st,NULL);
1128: ISCreateStride(comm,n,st,1,&lf->rf[i]);
1129: }
1130: for (i=0;i<nc;i++) {
1131: PetscInt n,st;
1133: ISGetLocalSize(islcol[i],&n);
1134: ISStrideGetInfo(islcol[i],&st,NULL);
1135: ISCreateStride(comm,n,st,1,&lf->cf[i]);
1136: }
1137: lf->nr = nr;
1138: lf->nc = nc;
1139: PetscContainerCreate(PetscObjectComm((PetscObject)(*newmat)),&c);
1140: PetscContainerSetPointer(c,lf);
1141: PetscContainerSetUserDestroy(c,MatISContainerDestroyFields_Private);
1142: PetscObjectCompose((PetscObject)(*newmat),"_convert_nest_lfields",(PetscObject)c);
1143: PetscContainerDestroy(&c);
1144: }
1146: /* Free workspace */
1147: for (i=0;i<nr;i++) {
1148: ISDestroy(&islrow[i]);
1149: }
1150: for (i=0;i<nc;i++) {
1151: ISDestroy(&islcol[i]);
1152: }
1153: PetscFree6(isrow,iscol,islrow,islcol,snest,istrans);
1154: PetscFree2(lr,lc);
1155: return(0);
1156: }
1158: static PetscErrorCode MatDiagonalScale_IS(Mat A, Vec l, Vec r)
1159: {
1160: Mat_IS *matis = (Mat_IS*)A->data;
1161: Vec ll,rr;
1162: const PetscScalar *Y,*X;
1163: PetscScalar *x,*y;
1164: PetscErrorCode ierr;
1167: if (l) {
1168: ll = matis->y;
1169: VecGetArrayRead(l,&Y);
1170: VecGetArray(ll,&y);
1171: PetscSFBcastBegin(matis->sf,MPIU_SCALAR,Y,y);
1172: } else {
1173: ll = NULL;
1174: }
1175: if (r) {
1176: rr = matis->x;
1177: VecGetArrayRead(r,&X);
1178: VecGetArray(rr,&x);
1179: PetscSFBcastBegin(matis->csf,MPIU_SCALAR,X,x);
1180: } else {
1181: rr = NULL;
1182: }
1183: if (ll) {
1184: PetscSFBcastEnd(matis->sf,MPIU_SCALAR,Y,y);
1185: VecRestoreArrayRead(l,&Y);
1186: VecRestoreArray(ll,&y);
1187: }
1188: if (rr) {
1189: PetscSFBcastEnd(matis->csf,MPIU_SCALAR,X,x);
1190: VecRestoreArrayRead(r,&X);
1191: VecRestoreArray(rr,&x);
1192: }
1193: MatDiagonalScale(matis->A,ll,rr);
1194: return(0);
1195: }
1197: static PetscErrorCode MatGetInfo_IS(Mat A,MatInfoType flag,MatInfo *ginfo)
1198: {
1199: Mat_IS *matis = (Mat_IS*)A->data;
1200: MatInfo info;
1201: PetscLogDouble isend[6],irecv[6];
1202: PetscInt bs;
1206: MatGetBlockSize(A,&bs);
1207: if (matis->A->ops->getinfo) {
1208: MatGetInfo(matis->A,MAT_LOCAL,&info);
1209: isend[0] = info.nz_used;
1210: isend[1] = info.nz_allocated;
1211: isend[2] = info.nz_unneeded;
1212: isend[3] = info.memory;
1213: isend[4] = info.mallocs;
1214: } else {
1215: isend[0] = 0.;
1216: isend[1] = 0.;
1217: isend[2] = 0.;
1218: isend[3] = 0.;
1219: isend[4] = 0.;
1220: }
1221: isend[5] = matis->A->num_ass;
1222: if (flag == MAT_LOCAL) {
1223: ginfo->nz_used = isend[0];
1224: ginfo->nz_allocated = isend[1];
1225: ginfo->nz_unneeded = isend[2];
1226: ginfo->memory = isend[3];
1227: ginfo->mallocs = isend[4];
1228: ginfo->assemblies = isend[5];
1229: } else if (flag == MAT_GLOBAL_MAX) {
1230: MPIU_Allreduce(isend,irecv,6,MPIU_PETSCLOGDOUBLE,MPI_MAX,PetscObjectComm((PetscObject)A));
1232: ginfo->nz_used = irecv[0];
1233: ginfo->nz_allocated = irecv[1];
1234: ginfo->nz_unneeded = irecv[2];
1235: ginfo->memory = irecv[3];
1236: ginfo->mallocs = irecv[4];
1237: ginfo->assemblies = irecv[5];
1238: } else if (flag == MAT_GLOBAL_SUM) {
1239: MPIU_Allreduce(isend,irecv,5,MPIU_PETSCLOGDOUBLE,MPI_SUM,PetscObjectComm((PetscObject)A));
1241: ginfo->nz_used = irecv[0];
1242: ginfo->nz_allocated = irecv[1];
1243: ginfo->nz_unneeded = irecv[2];
1244: ginfo->memory = irecv[3];
1245: ginfo->mallocs = irecv[4];
1246: ginfo->assemblies = A->num_ass;
1247: }
1248: ginfo->block_size = bs;
1249: ginfo->fill_ratio_given = 0;
1250: ginfo->fill_ratio_needed = 0;
1251: ginfo->factor_mallocs = 0;
1252: return(0);
1253: }
1255: PetscErrorCode MatTranspose_IS(Mat A,MatReuse reuse,Mat *B)
1256: {
1257: Mat C,lC,lA;
1258: PetscErrorCode ierr;
1261: if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_INPLACE_MATRIX) {
1262: ISLocalToGlobalMapping rl2g,cl2g;
1263: MatCreate(PetscObjectComm((PetscObject)A),&C);
1264: MatSetSizes(C,A->cmap->n,A->rmap->n,A->cmap->N,A->rmap->N);
1265: MatSetBlockSizes(C,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));
1266: MatSetType(C,MATIS);
1267: MatGetLocalToGlobalMapping(A,&rl2g,&cl2g);
1268: MatSetLocalToGlobalMapping(C,cl2g,rl2g);
1269: } else {
1270: C = *B;
1271: }
1273: /* perform local transposition */
1274: MatISGetLocalMat(A,&lA);
1275: MatTranspose(lA,MAT_INITIAL_MATRIX,&lC);
1276: MatISSetLocalMat(C,lC);
1277: MatDestroy(&lC);
1279: if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) {
1280: *B = C;
1281: } else {
1282: MatHeaderMerge(A,&C);
1283: }
1284: MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);
1285: MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);
1286: return(0);
1287: }
1289: PetscErrorCode MatDiagonalSet_IS(Mat A,Vec D,InsertMode insmode)
1290: {
1291: Mat_IS *is = (Mat_IS*)A->data;
1295: if (D) { /* MatShift_IS pass D = NULL */
1296: VecScatterBegin(is->rctx,D,is->y,INSERT_VALUES,SCATTER_FORWARD);
1297: VecScatterEnd(is->rctx,D,is->y,INSERT_VALUES,SCATTER_FORWARD);
1298: }
1299: VecPointwiseDivide(is->y,is->y,is->counter);
1300: MatDiagonalSet(is->A,is->y,insmode);
1301: return(0);
1302: }
1304: PetscErrorCode MatShift_IS(Mat A,PetscScalar a)
1305: {
1306: Mat_IS *is = (Mat_IS*)A->data;
1310: VecSet(is->y,a);
1311: MatDiagonalSet_IS(A,NULL,ADD_VALUES);
1312: return(0);
1313: }
1315: static PetscErrorCode MatSetValuesLocal_SubMat_IS(Mat A,PetscInt m,const PetscInt *rows, PetscInt n,const PetscInt *cols,const PetscScalar *values,InsertMode addv)
1316: {
1318: PetscInt rows_l[MATIS_MAX_ENTRIES_INSERTION],cols_l[MATIS_MAX_ENTRIES_INSERTION];
1321: #if defined(PETSC_USE_DEBUG)
1322: if (m > MATIS_MAX_ENTRIES_INSERTION || n > MATIS_MAX_ENTRIES_INSERTION) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_SUP,"Number of row/column indices must be <= %D: they are %D %D",MATIS_MAX_ENTRIES_INSERTION,m,n);
1323: #endif
1324: ISLocalToGlobalMappingApply(A->rmap->mapping,m,rows,rows_l);
1325: ISLocalToGlobalMappingApply(A->cmap->mapping,n,cols,cols_l);
1326: MatSetValuesLocal_IS(A,m,rows_l,n,cols_l,values,addv);
1327: return(0);
1328: }
1330: static PetscErrorCode MatSetValuesBlockedLocal_SubMat_IS(Mat A,PetscInt m,const PetscInt *rows, PetscInt n,const PetscInt *cols,const PetscScalar *values,InsertMode addv)
1331: {
1333: PetscInt rows_l[MATIS_MAX_ENTRIES_INSERTION],cols_l[MATIS_MAX_ENTRIES_INSERTION];
1336: #if defined(PETSC_USE_DEBUG)
1337: if (m > MATIS_MAX_ENTRIES_INSERTION || n > MATIS_MAX_ENTRIES_INSERTION) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_SUP,"Number of row/column block indices must be <= %D: they are %D %D",MATIS_MAX_ENTRIES_INSERTION,m,n);
1338: #endif
1339: ISLocalToGlobalMappingApplyBlock(A->rmap->mapping,m,rows,rows_l);
1340: ISLocalToGlobalMappingApplyBlock(A->cmap->mapping,n,cols,cols_l);
1341: MatSetValuesBlockedLocal_IS(A,m,rows_l,n,cols_l,values,addv);
1342: return(0);
1343: }
1345: static PetscErrorCode MatCreateSubMatrix_IS(Mat mat,IS irow,IS icol,MatReuse scall,Mat *newmat)
1346: {
1347: Mat locmat,newlocmat;
1348: Mat_IS *newmatis;
1349: #if defined(PETSC_USE_DEBUG)
1350: Vec rtest,ltest;
1351: const PetscScalar *array;
1352: #endif
1353: const PetscInt *idxs;
1354: PetscInt i,m,n;
1355: PetscErrorCode ierr;
1358: if (scall == MAT_REUSE_MATRIX) {
1359: PetscBool ismatis;
1361: PetscObjectTypeCompare((PetscObject)*newmat,MATIS,&ismatis);
1362: if (!ismatis) SETERRQ(PetscObjectComm((PetscObject)*newmat),PETSC_ERR_ARG_WRONG,"Cannot reuse matrix! Not of MATIS type");
1363: newmatis = (Mat_IS*)(*newmat)->data;
1364: if (!newmatis->getsub_ris) SETERRQ(PetscObjectComm((PetscObject)*newmat),PETSC_ERR_ARG_WRONG,"Cannot reuse matrix! Misses local row IS");
1365: if (!newmatis->getsub_cis) SETERRQ(PetscObjectComm((PetscObject)*newmat),PETSC_ERR_ARG_WRONG,"Cannot reuse matrix! Misses local col IS");
1366: }
1367: /* irow and icol may not have duplicate entries */
1368: #if defined(PETSC_USE_DEBUG)
1369: MatCreateVecs(mat,<est,&rtest);
1370: ISGetLocalSize(irow,&n);
1371: ISGetIndices(irow,&idxs);
1372: for (i=0;i<n;i++) {
1373: VecSetValue(rtest,idxs[i],1.0,ADD_VALUES);
1374: }
1375: VecAssemblyBegin(rtest);
1376: VecAssemblyEnd(rtest);
1377: VecGetLocalSize(rtest,&n);
1378: VecGetOwnershipRange(rtest,&m,NULL);
1379: VecGetArrayRead(rtest,&array);
1380: for (i=0;i<n;i++) if (array[i] != 0. && array[i] != 1.) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Index %D counted %D times! Irow may not have duplicate entries",i+m,(PetscInt)PetscRealPart(array[i]));
1381: VecRestoreArrayRead(rtest,&array);
1382: ISRestoreIndices(irow,&idxs);
1383: ISGetLocalSize(icol,&n);
1384: ISGetIndices(icol,&idxs);
1385: for (i=0;i<n;i++) {
1386: VecSetValue(ltest,idxs[i],1.0,ADD_VALUES);
1387: }
1388: VecAssemblyBegin(ltest);
1389: VecAssemblyEnd(ltest);
1390: VecGetLocalSize(ltest,&n);
1391: VecGetOwnershipRange(ltest,&m,NULL);
1392: VecGetArrayRead(ltest,&array);
1393: for (i=0;i<n;i++) if (array[i] != 0. && array[i] != 1.) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Index %D counted %D times! Icol may not have duplicate entries",i+m,(PetscInt)PetscRealPart(array[i]));
1394: VecRestoreArrayRead(ltest,&array);
1395: ISRestoreIndices(icol,&idxs);
1396: VecDestroy(&rtest);
1397: VecDestroy(<est);
1398: #endif
1399: if (scall == MAT_INITIAL_MATRIX) {
1400: Mat_IS *matis = (Mat_IS*)mat->data;
1401: ISLocalToGlobalMapping rl2g;
1402: IS is;
1403: PetscInt *lidxs,*lgidxs,*newgidxs;
1404: PetscInt ll,newloc,irbs,icbs,arbs,acbs,rbs,cbs;
1405: PetscBool cong;
1406: MPI_Comm comm;
1408: PetscObjectGetComm((PetscObject)mat,&comm);
1409: MatGetBlockSizes(mat,&arbs,&acbs);
1410: ISGetBlockSize(irow,&irbs);
1411: ISGetBlockSize(icol,&icbs);
1412: rbs = arbs == irbs ? irbs : 1;
1413: cbs = acbs == icbs ? icbs : 1;
1414: ISGetLocalSize(irow,&m);
1415: ISGetLocalSize(icol,&n);
1416: MatCreate(comm,newmat);
1417: MatSetType(*newmat,MATIS);
1418: MatSetSizes(*newmat,m,n,PETSC_DECIDE,PETSC_DECIDE);
1419: MatSetBlockSizes(*newmat,rbs,cbs);
1420: /* communicate irow to their owners in the layout */
1421: ISGetIndices(irow,&idxs);
1422: PetscLayoutMapLocal(mat->rmap,m,idxs,&ll,&lidxs,&lgidxs);
1423: ISRestoreIndices(irow,&idxs);
1424: PetscArrayzero(matis->sf_rootdata,matis->sf->nroots);
1425: for (i=0;i<ll;i++) matis->sf_rootdata[lidxs[i]] = lgidxs[i]+1;
1426: PetscFree(lidxs);
1427: PetscFree(lgidxs);
1428: PetscSFBcastBegin(matis->sf,MPIU_INT,matis->sf_rootdata,matis->sf_leafdata);
1429: PetscSFBcastEnd(matis->sf,MPIU_INT,matis->sf_rootdata,matis->sf_leafdata);
1430: for (i=0,newloc=0;i<matis->sf->nleaves;i++) if (matis->sf_leafdata[i]) newloc++;
1431: PetscMalloc1(newloc,&newgidxs);
1432: PetscMalloc1(newloc,&lidxs);
1433: for (i=0,newloc=0;i<matis->sf->nleaves;i++)
1434: if (matis->sf_leafdata[i]) {
1435: lidxs[newloc] = i;
1436: newgidxs[newloc++] = matis->sf_leafdata[i]-1;
1437: }
1438: ISCreateGeneral(comm,newloc,newgidxs,PETSC_OWN_POINTER,&is);
1439: ISLocalToGlobalMappingCreateIS(is,&rl2g);
1440: ISLocalToGlobalMappingSetBlockSize(rl2g,rbs);
1441: ISDestroy(&is);
1442: /* local is to extract local submatrix */
1443: newmatis = (Mat_IS*)(*newmat)->data;
1444: ISCreateGeneral(comm,newloc,lidxs,PETSC_OWN_POINTER,&newmatis->getsub_ris);
1445: MatHasCongruentLayouts(mat,&cong);
1446: if (cong && irow == icol && matis->csf == matis->sf) {
1447: MatSetLocalToGlobalMapping(*newmat,rl2g,rl2g);
1448: PetscObjectReference((PetscObject)newmatis->getsub_ris);
1449: newmatis->getsub_cis = newmatis->getsub_ris;
1450: } else {
1451: ISLocalToGlobalMapping cl2g;
1453: /* communicate icol to their owners in the layout */
1454: ISGetIndices(icol,&idxs);
1455: PetscLayoutMapLocal(mat->cmap,n,idxs,&ll,&lidxs,&lgidxs);
1456: ISRestoreIndices(icol,&idxs);
1457: PetscArrayzero(matis->csf_rootdata,matis->csf->nroots);
1458: for (i=0;i<ll;i++) matis->csf_rootdata[lidxs[i]] = lgidxs[i]+1;
1459: PetscFree(lidxs);
1460: PetscFree(lgidxs);
1461: PetscSFBcastBegin(matis->csf,MPIU_INT,matis->csf_rootdata,matis->csf_leafdata);
1462: PetscSFBcastEnd(matis->csf,MPIU_INT,matis->csf_rootdata,matis->csf_leafdata);
1463: for (i=0,newloc=0;i<matis->csf->nleaves;i++) if (matis->csf_leafdata[i]) newloc++;
1464: PetscMalloc1(newloc,&newgidxs);
1465: PetscMalloc1(newloc,&lidxs);
1466: for (i=0,newloc=0;i<matis->csf->nleaves;i++)
1467: if (matis->csf_leafdata[i]) {
1468: lidxs[newloc] = i;
1469: newgidxs[newloc++] = matis->csf_leafdata[i]-1;
1470: }
1471: ISCreateGeneral(comm,newloc,newgidxs,PETSC_OWN_POINTER,&is);
1472: ISLocalToGlobalMappingCreateIS(is,&cl2g);
1473: ISLocalToGlobalMappingSetBlockSize(cl2g,cbs);
1474: ISDestroy(&is);
1475: /* local is to extract local submatrix */
1476: ISCreateGeneral(comm,newloc,lidxs,PETSC_OWN_POINTER,&newmatis->getsub_cis);
1477: MatSetLocalToGlobalMapping(*newmat,rl2g,cl2g);
1478: ISLocalToGlobalMappingDestroy(&cl2g);
1479: }
1480: ISLocalToGlobalMappingDestroy(&rl2g);
1481: } else {
1482: MatISGetLocalMat(*newmat,&newlocmat);
1483: }
1484: MatISGetLocalMat(mat,&locmat);
1485: newmatis = (Mat_IS*)(*newmat)->data;
1486: MatCreateSubMatrix(locmat,newmatis->getsub_ris,newmatis->getsub_cis,scall,&newlocmat);
1487: if (scall == MAT_INITIAL_MATRIX) {
1488: MatISSetLocalMat(*newmat,newlocmat);
1489: MatDestroy(&newlocmat);
1490: }
1491: MatAssemblyBegin(*newmat,MAT_FINAL_ASSEMBLY);
1492: MatAssemblyEnd(*newmat,MAT_FINAL_ASSEMBLY);
1493: return(0);
1494: }
1496: static PetscErrorCode MatCopy_IS(Mat A,Mat B,MatStructure str)
1497: {
1498: Mat_IS *a = (Mat_IS*)A->data,*b;
1499: PetscBool ismatis;
1503: PetscObjectTypeCompare((PetscObject)B,MATIS,&ismatis);
1504: if (!ismatis) SETERRQ(PetscObjectComm((PetscObject)B),PETSC_ERR_SUP,"Need to be implemented");
1505: b = (Mat_IS*)B->data;
1506: MatCopy(a->A,b->A,str);
1507: PetscObjectStateIncrease((PetscObject)B);
1508: return(0);
1509: }
1511: static PetscErrorCode MatMissingDiagonal_IS(Mat A,PetscBool *missing,PetscInt *d)
1512: {
1513: Vec v;
1514: const PetscScalar *array;
1515: PetscInt i,n;
1516: PetscErrorCode ierr;
1519: *missing = PETSC_FALSE;
1520: MatCreateVecs(A,NULL,&v);
1521: MatGetDiagonal(A,v);
1522: VecGetLocalSize(v,&n);
1523: VecGetArrayRead(v,&array);
1524: for (i=0;i<n;i++) if (array[i] == 0.) break;
1525: VecRestoreArrayRead(v,&array);
1526: VecDestroy(&v);
1527: if (i != n) *missing = PETSC_TRUE;
1528: if (d) {
1529: *d = -1;
1530: if (*missing) {
1531: PetscInt rstart;
1532: MatGetOwnershipRange(A,&rstart,NULL);
1533: *d = i+rstart;
1534: }
1535: }
1536: return(0);
1537: }
1539: static PetscErrorCode MatISSetUpSF_IS(Mat B)
1540: {
1541: Mat_IS *matis = (Mat_IS*)(B->data);
1542: const PetscInt *gidxs;
1543: PetscInt nleaves;
1547: if (matis->sf) return(0);
1548: PetscSFCreate(PetscObjectComm((PetscObject)B),&matis->sf);
1549: ISLocalToGlobalMappingGetIndices(B->rmap->mapping,&gidxs);
1550: ISLocalToGlobalMappingGetSize(B->rmap->mapping,&nleaves);
1551: PetscSFSetGraphLayout(matis->sf,B->rmap,nleaves,NULL,PETSC_OWN_POINTER,gidxs);
1552: ISLocalToGlobalMappingRestoreIndices(B->rmap->mapping,&gidxs);
1553: PetscMalloc2(matis->sf->nroots,&matis->sf_rootdata,matis->sf->nleaves,&matis->sf_leafdata);
1554: if (B->rmap->mapping != B->cmap->mapping) { /* setup SF for columns */
1555: ISLocalToGlobalMappingGetSize(B->cmap->mapping,&nleaves);
1556: PetscSFCreate(PetscObjectComm((PetscObject)B),&matis->csf);
1557: ISLocalToGlobalMappingGetIndices(B->cmap->mapping,&gidxs);
1558: PetscSFSetGraphLayout(matis->csf,B->cmap,nleaves,NULL,PETSC_OWN_POINTER,gidxs);
1559: ISLocalToGlobalMappingRestoreIndices(B->cmap->mapping,&gidxs);
1560: PetscMalloc2(matis->csf->nroots,&matis->csf_rootdata,matis->csf->nleaves,&matis->csf_leafdata);
1561: } else {
1562: matis->csf = matis->sf;
1563: matis->csf_leafdata = matis->sf_leafdata;
1564: matis->csf_rootdata = matis->sf_rootdata;
1565: }
1566: return(0);
1567: }
1569: /*@
1570: MatISStoreL2L - Store local-to-local operators during the Galerkin process of MatPtAP.
1572: Collective
1574: Input Parameters:
1575: + A - the matrix
1576: - store - the boolean flag
1578: Level: advanced
1580: Notes:
1582: .seealso: MatCreate(), MatCreateIS(), MatISSetPreallocation(), MatPtAP()
1583: @*/
1584: PetscErrorCode MatISStoreL2L(Mat A, PetscBool store)
1585: {
1592: PetscTryMethod(A,"MatISStoreL2L_C",(Mat,PetscBool),(A,store));
1593: return(0);
1594: }
1596: static PetscErrorCode MatISStoreL2L_IS(Mat A, PetscBool store)
1597: {
1598: Mat_IS *matis = (Mat_IS*)(A->data);
1602: matis->storel2l = store;
1603: if (!store) {
1604: PetscObjectCompose((PetscObject)(A),"_MatIS_PtAP_l2l",NULL);
1605: }
1606: return(0);
1607: }
1609: /*@
1610: MatISFixLocalEmpty - Compress out zero local rows from the local matrices
1612: Collective
1614: Input Parameters:
1615: + A - the matrix
1616: - fix - the boolean flag
1618: Level: advanced
1620: Notes: When fix is true, new local matrices and l2g maps are generated during the final assembly process.
1622: .seealso: MatCreate(), MatCreateIS(), MatISSetPreallocation(), MatAssemblyEnd(), MAT_FINAL_ASSEMBLY
1623: @*/
1624: PetscErrorCode MatISFixLocalEmpty(Mat A, PetscBool fix)
1625: {
1632: PetscTryMethod(A,"MatISFixLocalEmpty_C",(Mat,PetscBool),(A,fix));
1633: return(0);
1634: }
1636: static PetscErrorCode MatISFixLocalEmpty_IS(Mat A, PetscBool fix)
1637: {
1638: Mat_IS *matis = (Mat_IS*)(A->data);
1641: matis->locempty = fix;
1642: return(0);
1643: }
1645: /*@
1646: MatISSetPreallocation - Preallocates memory for a MATIS parallel matrix.
1648: Collective
1650: Input Parameters:
1651: + B - the matrix
1652: . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix
1653: (same value is used for all local rows)
1654: . d_nnz - array containing the number of nonzeros in the various rows of the
1655: DIAGONAL portion of the local submatrix (possibly different for each row)
1656: or NULL, if d_nz is used to specify the nonzero structure.
1657: The size of this array is equal to the number of local rows, i.e 'm'.
1658: For matrices that will be factored, you must leave room for (and set)
1659: the diagonal entry even if it is zero.
1660: . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local
1661: submatrix (same value is used for all local rows).
1662: - o_nnz - array containing the number of nonzeros in the various rows of the
1663: OFF-DIAGONAL portion of the local submatrix (possibly different for
1664: each row) or NULL, if o_nz is used to specify the nonzero
1665: structure. The size of this array is equal to the number
1666: of local rows, i.e 'm'.
1668: If the *_nnz parameter is given then the *_nz parameter is ignored
1670: Level: intermediate
1672: Notes:
1673: This function has the same interface as the MPIAIJ preallocation routine in order to simplify the transition
1674: from the asssembled format to the unassembled one. It overestimates the preallocation of MATIS local
1675: matrices; for exact preallocation, the user should set the preallocation directly on local matrix objects.
1677: .seealso: MatCreate(), MatCreateIS(), MatMPIAIJSetPreallocation(), MatISGetLocalMat(), MATIS
1678: @*/
1679: PetscErrorCode MatISSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
1680: {
1686: PetscTryMethod(B,"MatISSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));
1687: return(0);
1688: }
1690: /* this is used by DMDA */
1691: PETSC_EXTERN PetscErrorCode MatISSetPreallocation_IS(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
1692: {
1693: Mat_IS *matis = (Mat_IS*)(B->data);
1694: PetscInt bs,i,nlocalcols;
1698: if (!matis->A) SETERRQ(PetscObjectComm((PetscObject)B),PETSC_ERR_SUP,"You should first call MatSetLocalToGlobalMapping");
1700: if (!d_nnz) for (i=0;i<matis->sf->nroots;i++) matis->sf_rootdata[i] = d_nz;
1701: else for (i=0;i<matis->sf->nroots;i++) matis->sf_rootdata[i] = d_nnz[i];
1703: if (!o_nnz) for (i=0;i<matis->sf->nroots;i++) matis->sf_rootdata[i] += o_nz;
1704: else for (i=0;i<matis->sf->nroots;i++) matis->sf_rootdata[i] += o_nnz[i];
1706: PetscSFBcastBegin(matis->sf,MPIU_INT,matis->sf_rootdata,matis->sf_leafdata);
1707: MatGetSize(matis->A,NULL,&nlocalcols);
1708: MatGetBlockSize(matis->A,&bs);
1709: PetscSFBcastEnd(matis->sf,MPIU_INT,matis->sf_rootdata,matis->sf_leafdata);
1711: for (i=0;i<matis->sf->nleaves;i++) matis->sf_leafdata[i] = PetscMin(matis->sf_leafdata[i],nlocalcols);
1712: MatSeqAIJSetPreallocation(matis->A,0,matis->sf_leafdata);
1713: #if defined(PETSC_HAVE_HYPRE)
1714: MatHYPRESetPreallocation(matis->A,0,matis->sf_leafdata,0,NULL);
1715: #endif
1717: for (i=0;i<matis->sf->nleaves/bs;i++) matis->sf_leafdata[i] = matis->sf_leafdata[i*bs]/bs;
1718: MatSeqBAIJSetPreallocation(matis->A,bs,0,matis->sf_leafdata);
1720: nlocalcols /= bs;
1721: for (i=0;i<matis->sf->nleaves/bs;i++) matis->sf_leafdata[i] = PetscMin(matis->sf_leafdata[i],nlocalcols - i);
1722: MatSeqSBAIJSetPreallocation(matis->A,bs,0,matis->sf_leafdata);
1724: /* for other matrix types */
1725: MatSetUp(matis->A);
1726: return(0);
1727: }
1729: PETSC_EXTERN PetscErrorCode MatISSetMPIXAIJPreallocation_Private(Mat A, Mat B, PetscBool maxreduce)
1730: {
1731: Mat_IS *matis = (Mat_IS*)(A->data);
1732: PetscInt *my_dnz,*my_onz,*dnz,*onz,*mat_ranges,*row_ownership;
1733: const PetscInt *global_indices_r,*global_indices_c;
1734: PetscInt i,j,bs,rows,cols;
1735: PetscInt lrows,lcols;
1736: PetscInt local_rows,local_cols;
1737: PetscMPIInt size;
1738: PetscBool isdense,issbaij;
1739: PetscErrorCode ierr;
1742: MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
1743: MatGetSize(A,&rows,&cols);
1744: MatGetBlockSize(A,&bs);
1745: MatGetSize(matis->A,&local_rows,&local_cols);
1746: PetscObjectBaseTypeCompare((PetscObject)matis->A,MATSEQDENSE,&isdense);
1747: PetscObjectBaseTypeCompare((PetscObject)matis->A,MATSEQSBAIJ,&issbaij);
1748: ISLocalToGlobalMappingGetIndices(A->rmap->mapping,&global_indices_r);
1749: if (A->rmap->mapping != A->cmap->mapping) {
1750: ISLocalToGlobalMappingGetIndices(A->cmap->mapping,&global_indices_c);
1751: } else {
1752: global_indices_c = global_indices_r;
1753: }
1755: if (issbaij) {
1756: MatGetRowUpperTriangular(matis->A);
1757: }
1758: /*
1759: An SF reduce is needed to sum up properly on shared rows.
1760: Note that generally preallocation is not exact, since it overestimates nonzeros
1761: */
1762: MatGetLocalSize(A,&lrows,&lcols);
1763: MatPreallocateInitialize(PetscObjectComm((PetscObject)A),lrows,lcols,dnz,onz);
1764: /* All processes need to compute entire row ownership */
1765: PetscMalloc1(rows,&row_ownership);
1766: MatGetOwnershipRanges(A,(const PetscInt**)&mat_ranges);
1767: for (i=0;i<size;i++) {
1768: for (j=mat_ranges[i];j<mat_ranges[i+1];j++) {
1769: row_ownership[j] = i;
1770: }
1771: }
1772: MatGetOwnershipRangesColumn(A,(const PetscInt**)&mat_ranges);
1774: /*
1775: my_dnz and my_onz contains exact contribution to preallocation from each local mat
1776: then, they will be summed up properly. This way, preallocation is always sufficient
1777: */
1778: PetscCalloc2(local_rows,&my_dnz,local_rows,&my_onz);
1779: /* preallocation as a MATAIJ */
1780: if (isdense) { /* special case for dense local matrices */
1781: for (i=0;i<local_rows;i++) {
1782: PetscInt owner = row_ownership[global_indices_r[i]];
1783: for (j=0;j<local_cols;j++) {
1784: PetscInt index_col = global_indices_c[j];
1785: if (index_col > mat_ranges[owner]-1 && index_col < mat_ranges[owner+1]) { /* diag block */
1786: my_dnz[i] += 1;
1787: } else { /* offdiag block */
1788: my_onz[i] += 1;
1789: }
1790: }
1791: }
1792: } else if (matis->A->ops->getrowij) {
1793: const PetscInt *ii,*jj,*jptr;
1794: PetscBool done;
1795: MatGetRowIJ(matis->A,0,PETSC_FALSE,PETSC_FALSE,&local_rows,&ii,&jj,&done);
1796: if (!done) SETERRQ(PetscObjectComm((PetscObject)(matis->A)),PETSC_ERR_PLIB,"Error in MatGetRowIJ");
1797: jptr = jj;
1798: for (i=0;i<local_rows;i++) {
1799: PetscInt index_row = global_indices_r[i];
1800: for (j=0;j<ii[i+1]-ii[i];j++,jptr++) {
1801: PetscInt owner = row_ownership[index_row];
1802: PetscInt index_col = global_indices_c[*jptr];
1803: if (index_col > mat_ranges[owner]-1 && index_col < mat_ranges[owner+1] ) { /* diag block */
1804: my_dnz[i] += 1;
1805: } else { /* offdiag block */
1806: my_onz[i] += 1;
1807: }
1808: /* same as before, interchanging rows and cols */
1809: if (issbaij && index_col != index_row) {
1810: owner = row_ownership[index_col];
1811: if (index_row > mat_ranges[owner]-1 && index_row < mat_ranges[owner+1] ) {
1812: my_dnz[*jptr] += 1;
1813: } else {
1814: my_onz[*jptr] += 1;
1815: }
1816: }
1817: }
1818: }
1819: MatRestoreRowIJ(matis->A,0,PETSC_FALSE,PETSC_FALSE,&local_rows,&ii,&jj,&done);
1820: if (!done) SETERRQ(PetscObjectComm((PetscObject)(matis->A)),PETSC_ERR_PLIB,"Error in MatRestoreRowIJ");
1821: } else { /* loop over rows and use MatGetRow */
1822: for (i=0;i<local_rows;i++) {
1823: const PetscInt *cols;
1824: PetscInt ncols,index_row = global_indices_r[i];
1825: MatGetRow(matis->A,i,&ncols,&cols,NULL);
1826: for (j=0;j<ncols;j++) {
1827: PetscInt owner = row_ownership[index_row];
1828: PetscInt index_col = global_indices_c[cols[j]];
1829: if (index_col > mat_ranges[owner]-1 && index_col < mat_ranges[owner+1] ) { /* diag block */
1830: my_dnz[i] += 1;
1831: } else { /* offdiag block */
1832: my_onz[i] += 1;
1833: }
1834: /* same as before, interchanging rows and cols */
1835: if (issbaij && index_col != index_row) {
1836: owner = row_ownership[index_col];
1837: if (index_row > mat_ranges[owner]-1 && index_row < mat_ranges[owner+1] ) {
1838: my_dnz[cols[j]] += 1;
1839: } else {
1840: my_onz[cols[j]] += 1;
1841: }
1842: }
1843: }
1844: MatRestoreRow(matis->A,i,&ncols,&cols,NULL);
1845: }
1846: }
1847: if (global_indices_c != global_indices_r) {
1848: ISLocalToGlobalMappingRestoreIndices(A->cmap->mapping,&global_indices_c);
1849: }
1850: ISLocalToGlobalMappingRestoreIndices(A->rmap->mapping,&global_indices_r);
1851: PetscFree(row_ownership);
1853: /* Reduce my_dnz and my_onz */
1854: if (maxreduce) {
1855: PetscSFReduceBegin(matis->sf,MPIU_INT,my_dnz,dnz,MPI_MAX);
1856: PetscSFReduceBegin(matis->sf,MPIU_INT,my_onz,onz,MPI_MAX);
1857: PetscSFReduceEnd(matis->sf,MPIU_INT,my_dnz,dnz,MPI_MAX);
1858: PetscSFReduceEnd(matis->sf,MPIU_INT,my_onz,onz,MPI_MAX);
1859: } else {
1860: PetscSFReduceBegin(matis->sf,MPIU_INT,my_dnz,dnz,MPI_SUM);
1861: PetscSFReduceBegin(matis->sf,MPIU_INT,my_onz,onz,MPI_SUM);
1862: PetscSFReduceEnd(matis->sf,MPIU_INT,my_dnz,dnz,MPI_SUM);
1863: PetscSFReduceEnd(matis->sf,MPIU_INT,my_onz,onz,MPI_SUM);
1864: }
1865: PetscFree2(my_dnz,my_onz);
1867: /* Resize preallocation if overestimated */
1868: for (i=0;i<lrows;i++) {
1869: dnz[i] = PetscMin(dnz[i],lcols);
1870: onz[i] = PetscMin(onz[i],cols-lcols);
1871: }
1873: /* Set preallocation */
1874: MatSeqAIJSetPreallocation(B,0,dnz);
1875: MatMPIAIJSetPreallocation(B,0,dnz,0,onz);
1876: for (i=0;i<lrows;i+=bs) {
1877: PetscInt b, d = dnz[i],o = onz[i];
1879: for (b=1;b<bs;b++) {
1880: d = PetscMax(d,dnz[i+b]);
1881: o = PetscMax(o,onz[i+b]);
1882: }
1883: dnz[i/bs] = PetscMin(d/bs + d%bs,lcols/bs);
1884: onz[i/bs] = PetscMin(o/bs + o%bs,(cols-lcols)/bs);
1885: }
1886: MatSeqBAIJSetPreallocation(B,bs,0,dnz);
1887: MatMPIBAIJSetPreallocation(B,bs,0,dnz,0,onz);
1888: MatMPISBAIJSetPreallocation(B,bs,0,dnz,0,onz);
1889: MatPreallocateFinalize(dnz,onz);
1890: if (issbaij) {
1891: MatRestoreRowUpperTriangular(matis->A);
1892: }
1893: MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
1894: return(0);
1895: }
1897: PETSC_INTERN PetscErrorCode MatConvert_IS_XAIJ(Mat mat, MatType mtype, MatReuse reuse, Mat *M)
1898: {
1899: Mat_IS *matis = (Mat_IS*)(mat->data);
1900: Mat local_mat,MT;
1901: PetscInt rbs,cbs,rows,cols,lrows,lcols;
1902: PetscInt local_rows,local_cols;
1903: PetscBool isseqdense,isseqsbaij,isseqaij,isseqbaij;
1904: #if defined (PETSC_USE_DEBUG)
1905: PetscBool lb[4],bb[4];
1906: #endif
1907: PetscMPIInt size;
1908: const PetscScalar *array;
1909: PetscErrorCode ierr;
1912: MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
1913: if (size == 1 && mat->rmap->N == matis->A->rmap->N && mat->cmap->N == matis->A->cmap->N) {
1914: Mat B;
1915: IS irows = NULL,icols = NULL;
1916: PetscInt rbs,cbs;
1918: ISLocalToGlobalMappingGetBlockSize(mat->rmap->mapping,&rbs);
1919: ISLocalToGlobalMappingGetBlockSize(mat->cmap->mapping,&cbs);
1920: if (reuse != MAT_REUSE_MATRIX) { /* check if l2g maps are one-to-one */
1921: IS rows,cols;
1922: const PetscInt *ridxs,*cidxs;
1923: PetscInt i,nw,*work;
1925: ISLocalToGlobalMappingGetBlockIndices(mat->rmap->mapping,&ridxs);
1926: ISLocalToGlobalMappingGetSize(mat->rmap->mapping,&nw);
1927: nw = nw/rbs;
1928: PetscCalloc1(nw,&work);
1929: for (i=0;i<nw;i++) work[ridxs[i]] += 1;
1930: for (i=0;i<nw;i++) if (!work[i] || work[i] > 1) break;
1931: if (i == nw) {
1932: ISCreateBlock(PETSC_COMM_SELF,rbs,nw,ridxs,PETSC_USE_POINTER,&rows);
1933: ISSetPermutation(rows);
1934: ISInvertPermutation(rows,PETSC_DECIDE,&irows);
1935: ISDestroy(&rows);
1936: }
1937: ISLocalToGlobalMappingRestoreBlockIndices(mat->rmap->mapping,&ridxs);
1938: PetscFree(work);
1939: if (irows && mat->rmap->mapping != mat->cmap->mapping) {
1940: ISLocalToGlobalMappingGetBlockIndices(mat->cmap->mapping,&cidxs);
1941: ISLocalToGlobalMappingGetSize(mat->cmap->mapping,&nw);
1942: nw = nw/cbs;
1943: PetscCalloc1(nw,&work);
1944: for (i=0;i<nw;i++) work[cidxs[i]] += 1;
1945: for (i=0;i<nw;i++) if (!work[i] || work[i] > 1) break;
1946: if (i == nw) {
1947: ISCreateBlock(PETSC_COMM_SELF,cbs,nw,cidxs,PETSC_USE_POINTER,&cols);
1948: ISSetPermutation(cols);
1949: ISInvertPermutation(cols,PETSC_DECIDE,&icols);
1950: ISDestroy(&cols);
1951: }
1952: ISLocalToGlobalMappingRestoreBlockIndices(mat->cmap->mapping,&cidxs);
1953: PetscFree(work);
1954: } else if (irows) {
1955: PetscObjectReference((PetscObject)irows);
1956: icols = irows;
1957: }
1958: } else {
1959: PetscObjectQuery((PetscObject)(*M),"_MatIS_IS_XAIJ_irows",(PetscObject*)&irows);
1960: PetscObjectQuery((PetscObject)(*M),"_MatIS_IS_XAIJ_icols",(PetscObject*)&icols);
1961: if (irows) { PetscObjectReference((PetscObject)irows); }
1962: if (icols) { PetscObjectReference((PetscObject)icols); }
1963: }
1964: if (!irows || !icols) {
1965: ISDestroy(&icols);
1966: ISDestroy(&irows);
1967: goto general_assembly;
1968: }
1969: MatConvert(matis->A,mtype,MAT_INITIAL_MATRIX,&B);
1970: if (reuse != MAT_INPLACE_MATRIX) {
1971: MatCreateSubMatrix(B,irows,icols,reuse,M);
1972: PetscObjectCompose((PetscObject)(*M),"_MatIS_IS_XAIJ_irows",(PetscObject)irows);
1973: PetscObjectCompose((PetscObject)(*M),"_MatIS_IS_XAIJ_icols",(PetscObject)icols);
1974: } else {
1975: Mat C;
1977: MatCreateSubMatrix(B,irows,icols,MAT_INITIAL_MATRIX,&C);
1978: MatHeaderReplace(mat,&C);
1979: }
1980: MatDestroy(&B);
1981: ISDestroy(&icols);
1982: ISDestroy(&irows);
1983: return(0);
1984: }
1985: general_assembly:
1986: MatGetSize(mat,&rows,&cols);
1987: ISLocalToGlobalMappingGetBlockSize(mat->rmap->mapping,&rbs);
1988: ISLocalToGlobalMappingGetBlockSize(mat->cmap->mapping,&cbs);
1989: MatGetLocalSize(mat,&lrows,&lcols);
1990: MatGetSize(matis->A,&local_rows,&local_cols);
1991: PetscObjectBaseTypeCompare((PetscObject)matis->A,MATSEQDENSE,&isseqdense);
1992: PetscObjectBaseTypeCompare((PetscObject)matis->A,MATSEQAIJ,&isseqaij);
1993: PetscObjectBaseTypeCompare((PetscObject)matis->A,MATSEQBAIJ,&isseqbaij);
1994: PetscObjectBaseTypeCompare((PetscObject)matis->A,MATSEQSBAIJ,&isseqsbaij);
1995: if (!isseqdense && !isseqaij && !isseqbaij && !isseqsbaij) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not for matrix type %s",((PetscObject)(matis->A))->type_name);
1996: #if defined (PETSC_USE_DEBUG)
1997: lb[0] = isseqdense;
1998: lb[1] = isseqaij;
1999: lb[2] = isseqbaij;
2000: lb[3] = isseqsbaij;
2001: MPIU_Allreduce(lb,bb,4,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)mat));
2002: if (!bb[0] && !bb[1] && !bb[2] && !bb[3]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Local matrices must have the same type");
2003: #endif
2005: if (reuse != MAT_REUSE_MATRIX) {
2006: MatCreate(PetscObjectComm((PetscObject)mat),&MT);
2007: MatSetSizes(MT,lrows,lcols,rows,cols);
2008: MatSetType(MT,mtype);
2009: MatSetBlockSizes(MT,rbs,cbs);
2010: MatISSetMPIXAIJPreallocation_Private(mat,MT,PETSC_FALSE);
2011: } else {
2012: PetscInt mrbs,mcbs,mrows,mcols,mlrows,mlcols;
2014: /* some checks */
2015: MT = *M;
2016: MatGetBlockSizes(MT,&mrbs,&mcbs);
2017: MatGetSize(MT,&mrows,&mcols);
2018: MatGetLocalSize(MT,&mlrows,&mlcols);
2019: if (mrows != rows) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot reuse matrix. Wrong number of rows (%d != %d)",rows,mrows);
2020: if (mcols != cols) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot reuse matrix. Wrong number of cols (%d != %d)",cols,mcols);
2021: if (mlrows != lrows) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot reuse matrix. Wrong number of local rows (%d != %d)",lrows,mlrows);
2022: if (mlcols != lcols) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot reuse matrix. Wrong number of local cols (%d != %d)",lcols,mlcols);
2023: if (mrbs != rbs) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot reuse matrix. Wrong row block size (%d != %d)",rbs,mrbs);
2024: if (mcbs != cbs) SETERRQ2(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot reuse matrix. Wrong col block size (%d != %d)",cbs,mcbs);
2025: MatZeroEntries(MT);
2026: }
2028: if (isseqsbaij || isseqbaij) {
2029: MatConvert(matis->A,MATSEQAIJ,MAT_INITIAL_MATRIX,&local_mat);
2030: isseqaij = PETSC_TRUE;
2031: } else {
2032: PetscObjectReference((PetscObject)matis->A);
2033: local_mat = matis->A;
2034: }
2036: /* Set values */
2037: MatSetLocalToGlobalMapping(MT,mat->rmap->mapping,mat->cmap->mapping);
2038: if (isseqdense) { /* special case for dense local matrices */
2039: PetscInt i,*dummy;
2041: PetscMalloc1(PetscMax(local_rows,local_cols),&dummy);
2042: for (i=0;i<PetscMax(local_rows,local_cols);i++) dummy[i] = i;
2043: MatSetOption(MT,MAT_ROW_ORIENTED,PETSC_FALSE);
2044: MatDenseGetArrayRead(local_mat,&array);
2045: MatSetValuesLocal(MT,local_rows,dummy,local_cols,dummy,array,ADD_VALUES);
2046: MatDenseRestoreArrayRead(local_mat,&array);
2047: PetscFree(dummy);
2048: } else if (isseqaij) {
2049: const PetscInt *blocks;
2050: PetscInt i,nvtxs,*xadj,*adjncy, nb;
2051: PetscBool done;
2052: PetscScalar *sarray;
2054: MatGetRowIJ(local_mat,0,PETSC_FALSE,PETSC_FALSE,&nvtxs,(const PetscInt**)&xadj,(const PetscInt**)&adjncy,&done);
2055: if (!done) SETERRQ(PetscObjectComm((PetscObject)local_mat),PETSC_ERR_PLIB,"Error in MatGetRowIJ");
2056: MatSeqAIJGetArray(local_mat,&sarray);
2057: MatGetVariableBlockSizes(local_mat,&nb,&blocks);
2058: if (nb) { /* speed up assembly for special blocked matrices (used by BDDC) */
2059: PetscInt sum;
2061: for (i=0,sum=0;i<nb;i++) sum += blocks[i];
2062: if (sum == nvtxs) {
2063: PetscInt r;
2065: for (i=0,r=0;i<nb;i++) {
2066: #if defined(PETSC_USE_DEBUG)
2067: if (blocks[i] != xadj[r+1] - xadj[r]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Invalid block sizes prescribed for block %D: expected %D, got %D",i,blocks[i],xadj[r+1] - xadj[r]);
2068: #endif
2069: MatSetValuesLocal(MT,blocks[i],adjncy+xadj[r],blocks[i],adjncy+xadj[r],sarray+xadj[r],ADD_VALUES);
2070: r += blocks[i];
2071: }
2072: } else {
2073: for (i=0;i<nvtxs;i++) {
2074: MatSetValuesLocal(MT,1,&i,xadj[i+1]-xadj[i],adjncy+xadj[i],sarray+xadj[i],ADD_VALUES);
2075: }
2076: }
2077: } else {
2078: for (i=0;i<nvtxs;i++) {
2079: MatSetValuesLocal(MT,1,&i,xadj[i+1]-xadj[i],adjncy+xadj[i],sarray+xadj[i],ADD_VALUES);
2080: }
2081: }
2082: MatRestoreRowIJ(local_mat,0,PETSC_FALSE,PETSC_FALSE,&nvtxs,(const PetscInt**)&xadj,(const PetscInt**)&adjncy,&done);
2083: if (!done) SETERRQ(PetscObjectComm((PetscObject)local_mat),PETSC_ERR_PLIB,"Error in MatRestoreRowIJ");
2084: MatSeqAIJRestoreArray(local_mat,&sarray);
2085: } else { /* very basic values insertion for all other matrix types */
2086: PetscInt i;
2088: for (i=0;i<local_rows;i++) {
2089: PetscInt j;
2090: const PetscInt *local_indices_cols;
2092: MatGetRow(local_mat,i,&j,&local_indices_cols,&array);
2093: MatSetValuesLocal(MT,1,&i,j,local_indices_cols,array,ADD_VALUES);
2094: MatRestoreRow(local_mat,i,&j,&local_indices_cols,&array);
2095: }
2096: }
2097: MatAssemblyBegin(MT,MAT_FINAL_ASSEMBLY);
2098: MatDestroy(&local_mat);
2099: MatAssemblyEnd(MT,MAT_FINAL_ASSEMBLY);
2100: if (isseqdense) {
2101: MatSetOption(MT,MAT_ROW_ORIENTED,PETSC_TRUE);
2102: }
2103: if (reuse == MAT_INPLACE_MATRIX) {
2104: MatHeaderReplace(mat,&MT);
2105: } else if (reuse == MAT_INITIAL_MATRIX) {
2106: *M = MT;
2107: }
2108: return(0);
2109: }
2111: /*@
2112: MatISGetMPIXAIJ - Converts MATIS matrix into a parallel AIJ format
2114: Input Parameter:
2115: + mat - the matrix (should be of type MATIS)
2116: - reuse - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
2118: Output Parameter:
2119: . newmat - the matrix in AIJ format
2121: Level: developer
2123: Notes:
2124: This function has been deprecated and it will be removed in future releases. Update your code to use the MatConvert() interface.
2126: .seealso: MATIS, MatConvert()
2127: @*/
2128: PetscErrorCode MatISGetMPIXAIJ(Mat mat, MatReuse reuse, Mat *newmat)
2129: {
2136: if (reuse == MAT_REUSE_MATRIX) {
2139: if (mat == *newmat) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot reuse the same matrix");
2140: }
2141: PetscUseMethod(mat,"MatISGetMPIXAIJ_C",(Mat,MatType,MatReuse,Mat*),(mat,MATAIJ,reuse,newmat));
2142: return(0);
2143: }
2145: PetscErrorCode MatDuplicate_IS(Mat mat,MatDuplicateOption op,Mat *newmat)
2146: {
2148: Mat_IS *matis = (Mat_IS*)(mat->data);
2149: PetscInt rbs,cbs,m,n,M,N;
2150: Mat B,localmat;
2153: ISLocalToGlobalMappingGetBlockSize(mat->rmap->mapping,&rbs);
2154: ISLocalToGlobalMappingGetBlockSize(mat->cmap->mapping,&cbs);
2155: MatGetSize(mat,&M,&N);
2156: MatGetLocalSize(mat,&m,&n);
2157: MatCreate(PetscObjectComm((PetscObject)mat),&B);
2158: MatSetSizes(B,m,n,M,N);
2159: MatSetBlockSize(B,rbs == cbs ? rbs : 1);
2160: MatSetType(B,MATIS);
2161: MatISSetLocalMatType(B,matis->lmattype);
2162: MatSetLocalToGlobalMapping(B,mat->rmap->mapping,mat->cmap->mapping);
2163: MatDuplicate(matis->A,op,&localmat);
2164: MatISSetLocalMat(B,localmat);
2165: MatDestroy(&localmat);
2166: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2167: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2168: *newmat = B;
2169: return(0);
2170: }
2172: static PetscErrorCode MatIsHermitian_IS(Mat A,PetscReal tol,PetscBool *flg)
2173: {
2175: Mat_IS *matis = (Mat_IS*)A->data;
2176: PetscBool local_sym;
2179: MatIsHermitian(matis->A,tol,&local_sym);
2180: MPIU_Allreduce(&local_sym,flg,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
2181: return(0);
2182: }
2184: static PetscErrorCode MatIsSymmetric_IS(Mat A,PetscReal tol,PetscBool *flg)
2185: {
2187: Mat_IS *matis = (Mat_IS*)A->data;
2188: PetscBool local_sym;
2191: MatIsSymmetric(matis->A,tol,&local_sym);
2192: MPIU_Allreduce(&local_sym,flg,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
2193: return(0);
2194: }
2196: static PetscErrorCode MatIsStructurallySymmetric_IS(Mat A,PetscBool *flg)
2197: {
2199: Mat_IS *matis = (Mat_IS*)A->data;
2200: PetscBool local_sym;
2203: if (A->rmap->mapping != A->cmap->mapping) {
2204: *flg = PETSC_FALSE;
2205: return(0);
2206: }
2207: MatIsStructurallySymmetric(matis->A,&local_sym);
2208: MPIU_Allreduce(&local_sym,flg,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
2209: return(0);
2210: }
2212: static PetscErrorCode MatDestroy_IS(Mat A)
2213: {
2215: Mat_IS *b = (Mat_IS*)A->data;
2218: PetscFree(b->bdiag);
2219: PetscFree(b->lmattype);
2220: MatDestroy(&b->A);
2221: VecScatterDestroy(&b->cctx);
2222: VecScatterDestroy(&b->rctx);
2223: VecDestroy(&b->x);
2224: VecDestroy(&b->y);
2225: VecDestroy(&b->counter);
2226: ISDestroy(&b->getsub_ris);
2227: ISDestroy(&b->getsub_cis);
2228: if (b->sf != b->csf) {
2229: PetscSFDestroy(&b->csf);
2230: PetscFree2(b->csf_rootdata,b->csf_leafdata);
2231: } else b->csf = NULL;
2232: PetscSFDestroy(&b->sf);
2233: PetscFree2(b->sf_rootdata,b->sf_leafdata);
2234: PetscFree(A->data);
2235: PetscObjectChangeTypeName((PetscObject)A,0);
2236: PetscObjectComposeFunction((PetscObject)A,"MatISSetLocalMatType_C",NULL);
2237: PetscObjectComposeFunction((PetscObject)A,"MatISGetLocalMat_C",NULL);
2238: PetscObjectComposeFunction((PetscObject)A,"MatISSetLocalMat_C",NULL);
2239: PetscObjectComposeFunction((PetscObject)A,"MatISGetMPIXAIJ_C",NULL);
2240: PetscObjectComposeFunction((PetscObject)A,"MatISSetPreallocation_C",NULL);
2241: PetscObjectComposeFunction((PetscObject)A,"MatISStoreL2L_C",NULL);
2242: PetscObjectComposeFunction((PetscObject)A,"MatISFixLocalEmpty_C",NULL);
2243: PetscObjectComposeFunction((PetscObject)A,"MatConvert_is_mpiaij_C",NULL);
2244: PetscObjectComposeFunction((PetscObject)A,"MatConvert_is_mpibaij_C",NULL);
2245: PetscObjectComposeFunction((PetscObject)A,"MatConvert_is_mpisbaij_C",NULL);
2246: PetscObjectComposeFunction((PetscObject)A,"MatConvert_is_seqaij_C",NULL);
2247: PetscObjectComposeFunction((PetscObject)A,"MatConvert_is_seqbaij_C",NULL);
2248: PetscObjectComposeFunction((PetscObject)A,"MatConvert_is_seqsbaij_C",NULL);
2249: PetscObjectComposeFunction((PetscObject)A,"MatConvert_is_aij_C",NULL);
2250: return(0);
2251: }
2253: static PetscErrorCode MatMult_IS(Mat A,Vec x,Vec y)
2254: {
2256: Mat_IS *is = (Mat_IS*)A->data;
2257: PetscScalar zero = 0.0;
2260: /* scatter the global vector x into the local work vector */
2261: VecScatterBegin(is->cctx,x,is->x,INSERT_VALUES,SCATTER_FORWARD);
2262: VecScatterEnd(is->cctx,x,is->x,INSERT_VALUES,SCATTER_FORWARD);
2264: /* multiply the local matrix */
2265: MatMult(is->A,is->x,is->y);
2267: /* scatter product back into global memory */
2268: VecSet(y,zero);
2269: VecScatterBegin(is->rctx,is->y,y,ADD_VALUES,SCATTER_REVERSE);
2270: VecScatterEnd(is->rctx,is->y,y,ADD_VALUES,SCATTER_REVERSE);
2271: return(0);
2272: }
2274: static PetscErrorCode MatMultAdd_IS(Mat A,Vec v1,Vec v2,Vec v3)
2275: {
2276: Vec temp_vec;
2280: if (v3 != v2) {
2281: MatMult(A,v1,v3);
2282: VecAXPY(v3,1.0,v2);
2283: } else {
2284: VecDuplicate(v2,&temp_vec);
2285: MatMult(A,v1,temp_vec);
2286: VecAXPY(temp_vec,1.0,v2);
2287: VecCopy(temp_vec,v3);
2288: VecDestroy(&temp_vec);
2289: }
2290: return(0);
2291: }
2293: static PetscErrorCode MatMultTranspose_IS(Mat A,Vec y,Vec x)
2294: {
2295: Mat_IS *is = (Mat_IS*)A->data;
2299: /* scatter the global vector x into the local work vector */
2300: VecScatterBegin(is->rctx,y,is->y,INSERT_VALUES,SCATTER_FORWARD);
2301: VecScatterEnd(is->rctx,y,is->y,INSERT_VALUES,SCATTER_FORWARD);
2303: /* multiply the local matrix */
2304: MatMultTranspose(is->A,is->y,is->x);
2306: /* scatter product back into global vector */
2307: VecSet(x,0);
2308: VecScatterBegin(is->cctx,is->x,x,ADD_VALUES,SCATTER_REVERSE);
2309: VecScatterEnd(is->cctx,is->x,x,ADD_VALUES,SCATTER_REVERSE);
2310: return(0);
2311: }
2313: static PetscErrorCode MatMultTransposeAdd_IS(Mat A,Vec v1,Vec v2,Vec v3)
2314: {
2315: Vec temp_vec;
2319: if (v3 != v2) {
2320: MatMultTranspose(A,v1,v3);
2321: VecAXPY(v3,1.0,v2);
2322: } else {
2323: VecDuplicate(v2,&temp_vec);
2324: MatMultTranspose(A,v1,temp_vec);
2325: VecAXPY(temp_vec,1.0,v2);
2326: VecCopy(temp_vec,v3);
2327: VecDestroy(&temp_vec);
2328: }
2329: return(0);
2330: }
2332: static PetscErrorCode MatView_IS(Mat A,PetscViewer viewer)
2333: {
2334: Mat_IS *a = (Mat_IS*)A->data;
2336: PetscViewer sviewer;
2337: PetscBool isascii,view = PETSC_TRUE;
2340: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
2341: if (isascii) {
2342: PetscViewerFormat format;
2344: PetscViewerGetFormat(viewer,&format);
2345: if (format == PETSC_VIEWER_ASCII_INFO) view = PETSC_FALSE;
2346: }
2347: if (!view) return(0);
2348: PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
2349: MatView(a->A,sviewer);
2350: PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
2351: PetscViewerFlush(viewer);
2352: return(0);
2353: }
2355: static PetscErrorCode MatInvertBlockDiagonal_IS(Mat mat,const PetscScalar **values)
2356: {
2357: Mat_IS *is = (Mat_IS*)mat->data;
2358: MPI_Datatype nodeType;
2359: const PetscScalar *lv;
2360: PetscInt bs;
2361: PetscErrorCode ierr;
2364: MatGetBlockSize(mat,&bs);
2365: MatSetBlockSize(is->A,bs);
2366: MatInvertBlockDiagonal(is->A,&lv);
2367: if (!is->bdiag) {
2368: PetscMalloc1(bs*mat->rmap->n,&is->bdiag);
2369: }
2370: MPI_Type_contiguous(bs,MPIU_SCALAR,&nodeType);
2371: MPI_Type_commit(&nodeType);
2372: PetscSFReduceBegin(is->sf,nodeType,lv,is->bdiag,MPIU_REPLACE);
2373: PetscSFReduceEnd(is->sf,nodeType,lv,is->bdiag,MPIU_REPLACE);
2374: MPI_Type_free(&nodeType);
2375: if (values) *values = is->bdiag;
2376: return(0);
2377: }
2379: static PetscErrorCode MatISSetUpScatters_Private(Mat A)
2380: {
2381: Vec cglobal,rglobal;
2382: IS from;
2383: Mat_IS *is = (Mat_IS*)A->data;
2384: PetscScalar sum;
2385: const PetscInt *garray;
2386: PetscInt nr,rbs,nc,cbs;
2387: PetscBool iscuda;
2391: ISLocalToGlobalMappingGetSize(A->rmap->mapping,&nr);
2392: ISLocalToGlobalMappingGetBlockSize(A->rmap->mapping,&rbs);
2393: ISLocalToGlobalMappingGetSize(A->cmap->mapping,&nc);
2394: ISLocalToGlobalMappingGetBlockSize(A->cmap->mapping,&cbs);
2395: VecDestroy(&is->x);
2396: VecDestroy(&is->y);
2397: VecDestroy(&is->counter);
2398: VecScatterDestroy(&is->rctx);
2399: VecScatterDestroy(&is->cctx);
2400: MatCreateVecs(is->A,&is->x,&is->y);
2401: VecBindToCPU(is->y,PETSC_TRUE);
2402: PetscObjectTypeCompare((PetscObject)is->y,VECSEQCUDA,&iscuda);
2403: if (iscuda) {
2404: PetscFree(A->defaultvectype);
2405: PetscStrallocpy(VECCUDA,&A->defaultvectype);
2406: }
2407: MatCreateVecs(A,&cglobal,&rglobal);
2408: VecBindToCPU(rglobal,PETSC_TRUE);
2409: ISLocalToGlobalMappingGetBlockIndices(A->rmap->mapping,&garray);
2410: ISCreateBlock(PetscObjectComm((PetscObject)A),rbs,nr/rbs,garray,PETSC_USE_POINTER,&from);
2411: VecScatterCreate(rglobal,from,is->y,NULL,&is->rctx);
2412: ISLocalToGlobalMappingRestoreBlockIndices(A->rmap->mapping,&garray);
2413: ISDestroy(&from);
2414: if (A->rmap->mapping != A->cmap->mapping) {
2415: ISLocalToGlobalMappingGetBlockIndices(A->cmap->mapping,&garray);
2416: ISCreateBlock(PetscObjectComm((PetscObject)A),cbs,nc/cbs,garray,PETSC_USE_POINTER,&from);
2417: VecScatterCreate(cglobal,from,is->x,NULL,&is->cctx);
2418: ISLocalToGlobalMappingRestoreBlockIndices(A->cmap->mapping,&garray);
2419: ISDestroy(&from);
2420: } else {
2421: PetscObjectReference((PetscObject)is->rctx);
2422: is->cctx = is->rctx;
2423: }
2424: VecDestroy(&cglobal);
2426: /* interface counter vector (local) */
2427: VecDuplicate(is->y,&is->counter);
2428: VecBindToCPU(is->counter,PETSC_TRUE);
2429: VecSet(is->y,1.);
2430: VecScatterBegin(is->rctx,is->y,rglobal,ADD_VALUES,SCATTER_REVERSE);
2431: VecScatterEnd(is->rctx,is->y,rglobal,ADD_VALUES,SCATTER_REVERSE);
2432: VecScatterBegin(is->rctx,rglobal,is->counter,INSERT_VALUES,SCATTER_FORWARD);
2433: VecScatterEnd(is->rctx,rglobal,is->counter,INSERT_VALUES,SCATTER_FORWARD);
2434: VecBindToCPU(is->y,PETSC_FALSE);
2435: VecBindToCPU(is->counter,PETSC_FALSE);
2437: /* special functions for block-diagonal matrices */
2438: VecSum(rglobal,&sum);
2439: if ((PetscInt)(PetscRealPart(sum)) == A->rmap->N && A->rmap->N == A->cmap->N && A->rmap->mapping == A->cmap->mapping) {
2440: A->ops->invertblockdiagonal = MatInvertBlockDiagonal_IS;
2441: } else {
2442: A->ops->invertblockdiagonal = NULL;
2443: }
2444: VecDestroy(&rglobal);
2446: /* setup SF for general purpose shared indices based communications */
2447: MatISSetUpSF_IS(A);
2448: return(0);
2449: }
2451: static PetscErrorCode MatSetLocalToGlobalMapping_IS(Mat A,ISLocalToGlobalMapping rmapping,ISLocalToGlobalMapping cmapping)
2452: {
2454: PetscInt nr,rbs,nc,cbs;
2455: Mat_IS *is = (Mat_IS*)A->data;
2460: MatDestroy(&is->A);
2461: if (is->csf != is->sf) {
2462: PetscSFDestroy(&is->csf);
2463: PetscFree2(is->csf_rootdata,is->csf_leafdata);
2464: } else is->csf = NULL;
2465: PetscSFDestroy(&is->sf);
2466: PetscFree2(is->sf_rootdata,is->sf_leafdata);
2467: PetscFree(is->bdiag);
2469: /* Setup Layout and set local to global maps */
2470: PetscLayoutSetUp(A->rmap);
2471: PetscLayoutSetUp(A->cmap);
2472: ISLocalToGlobalMappingGetSize(rmapping,&nr);
2473: ISLocalToGlobalMappingGetBlockSize(rmapping,&rbs);
2474: ISLocalToGlobalMappingGetSize(cmapping,&nc);
2475: ISLocalToGlobalMappingGetBlockSize(cmapping,&cbs);
2476: /* check if the two mappings are actually the same for square matrices (DOLFIN passes 2 different objects) */
2477: if (rmapping != cmapping && A->rmap->N == A->cmap->N) {
2478: PetscBool same,gsame;
2480: same = PETSC_FALSE;
2481: if (nr == nc && cbs == rbs) {
2482: const PetscInt *idxs1,*idxs2;
2484: ISLocalToGlobalMappingGetBlockIndices(rmapping,&idxs1);
2485: ISLocalToGlobalMappingGetBlockIndices(cmapping,&idxs2);
2486: PetscArraycmp(idxs1,idxs2,nr/rbs,&same);
2487: ISLocalToGlobalMappingRestoreBlockIndices(rmapping,&idxs1);
2488: ISLocalToGlobalMappingRestoreBlockIndices(cmapping,&idxs2);
2489: }
2490: MPIU_Allreduce(&same,&gsame,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
2491: if (gsame) cmapping = rmapping;
2492: }
2493: PetscLayoutSetBlockSize(A->rmap,rbs);
2494: PetscLayoutSetBlockSize(A->cmap,cbs);
2495: PetscLayoutSetISLocalToGlobalMapping(A->rmap,rmapping);
2496: PetscLayoutSetISLocalToGlobalMapping(A->cmap,cmapping);
2498: /* Create the local matrix A */
2499: MatCreate(PETSC_COMM_SELF,&is->A);
2500: MatSetType(is->A,is->lmattype);
2501: MatSetSizes(is->A,nr,nc,nr,nc);
2502: MatSetBlockSizes(is->A,rbs,cbs);
2503: MatSetOptionsPrefix(is->A,"is_");
2504: MatAppendOptionsPrefix(is->A,((PetscObject)A)->prefix);
2505: PetscLayoutSetUp(is->A->rmap);
2506: PetscLayoutSetUp(is->A->cmap);
2508: if (!is->islocalref) { /* setup scatters and local vectors for MatMult */
2509: MatISSetUpScatters_Private(A);
2510: }
2511: MatSetUp(A);
2512: return(0);
2513: }
2515: static PetscErrorCode MatSetValues_IS(Mat mat, PetscInt m,const PetscInt *rows, PetscInt n,const PetscInt *cols, const PetscScalar *values, InsertMode addv)
2516: {
2517: Mat_IS *is = (Mat_IS*)mat->data;
2519: #if defined(PETSC_USE_DEBUG)
2520: PetscInt i,zm,zn;
2521: #endif
2522: PetscInt rows_l[MATIS_MAX_ENTRIES_INSERTION],cols_l[MATIS_MAX_ENTRIES_INSERTION];
2525: #if defined(PETSC_USE_DEBUG)
2526: if (m > MATIS_MAX_ENTRIES_INSERTION || n > MATIS_MAX_ENTRIES_INSERTION) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_SUP,"Number of row/column indices must be <= %D: they are %D %D",MATIS_MAX_ENTRIES_INSERTION,m,n);
2527: /* count negative indices */
2528: for (i=0,zm=0;i<m;i++) if (rows[i] < 0) zm++;
2529: for (i=0,zn=0;i<n;i++) if (cols[i] < 0) zn++;
2530: #endif
2531: ISGlobalToLocalMappingApply(mat->rmap->mapping,IS_GTOLM_MASK,m,rows,&m,rows_l);
2532: ISGlobalToLocalMappingApply(mat->cmap->mapping,IS_GTOLM_MASK,n,cols,&n,cols_l);
2533: #if defined(PETSC_USE_DEBUG)
2534: /* count negative indices (should be the same as before) */
2535: for (i=0;i<m;i++) if (rows_l[i] < 0) zm--;
2536: for (i=0;i<n;i++) if (cols_l[i] < 0) zn--;
2537: if (!is->A->rmap->mapping && zm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Some of the row indices can not be mapped! Maybe you should not use MATIS");
2538: if (!is->A->cmap->mapping && zn) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Some of the column indices can not be mapped! Maybe you should not use MATIS");
2539: #endif
2540: MatSetValues(is->A,m,rows_l,n,cols_l,values,addv);
2541: return(0);
2542: }
2544: static PetscErrorCode MatSetValuesBlocked_IS(Mat mat, PetscInt m,const PetscInt *rows, PetscInt n,const PetscInt *cols, const PetscScalar *values, InsertMode addv)
2545: {
2546: Mat_IS *is = (Mat_IS*)mat->data;
2548: #if defined(PETSC_USE_DEBUG)
2549: PetscInt i,zm,zn;
2550: #endif
2551: PetscInt rows_l[MATIS_MAX_ENTRIES_INSERTION],cols_l[MATIS_MAX_ENTRIES_INSERTION];
2554: #if defined(PETSC_USE_DEBUG)
2555: if (m > MATIS_MAX_ENTRIES_INSERTION || n > MATIS_MAX_ENTRIES_INSERTION) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_SUP,"Number of row/column block indices must be <= %D: they are %D %D",MATIS_MAX_ENTRIES_INSERTION,m,n);
2556: /* count negative indices */
2557: for (i=0,zm=0;i<m;i++) if (rows[i] < 0) zm++;
2558: for (i=0,zn=0;i<n;i++) if (cols[i] < 0) zn++;
2559: #endif
2560: ISGlobalToLocalMappingApplyBlock(mat->rmap->mapping,IS_GTOLM_MASK,m,rows,&m,rows_l);
2561: ISGlobalToLocalMappingApplyBlock(mat->cmap->mapping,IS_GTOLM_MASK,n,cols,&n,cols_l);
2562: #if defined(PETSC_USE_DEBUG)
2563: /* count negative indices (should be the same as before) */
2564: for (i=0;i<m;i++) if (rows_l[i] < 0) zm--;
2565: for (i=0;i<n;i++) if (cols_l[i] < 0) zn--;
2566: if (!is->A->rmap->mapping && zm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Some of the row indices can not be mapped! Maybe you should not use MATIS");
2567: if (!is->A->cmap->mapping && zn) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Some of the column indices can not be mapped! Maybe you should not use MATIS");
2568: #endif
2569: MatSetValuesBlocked(is->A,m,rows_l,n,cols_l,values,addv);
2570: return(0);
2571: }
2573: static PetscErrorCode MatSetValuesLocal_IS(Mat A,PetscInt m,const PetscInt *rows, PetscInt n,const PetscInt *cols,const PetscScalar *values,InsertMode addv)
2574: {
2576: Mat_IS *is = (Mat_IS*)A->data;
2579: if (is->A->rmap->mapping) {
2580: MatSetValuesLocal(is->A,m,rows,n,cols,values,addv);
2581: } else {
2582: MatSetValues(is->A,m,rows,n,cols,values,addv);
2583: }
2584: return(0);
2585: }
2587: static PetscErrorCode MatSetValuesBlockedLocal_IS(Mat A,PetscInt m,const PetscInt *rows, PetscInt n,const PetscInt *cols,const PetscScalar *values,InsertMode addv)
2588: {
2590: Mat_IS *is = (Mat_IS*)A->data;
2593: if (is->A->rmap->mapping) {
2594: #if defined(PETSC_USE_DEBUG)
2595: PetscInt ibs,bs;
2597: ISLocalToGlobalMappingGetBlockSize(is->A->rmap->mapping,&ibs);
2598: MatGetBlockSize(is->A,&bs);
2599: if (ibs != bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Different block sizes! local mat %D, local l2g map %D",bs,ibs);
2600: #endif
2601: MatSetValuesBlockedLocal(is->A,m,rows,n,cols,values,addv);
2602: } else {
2603: MatSetValuesBlocked(is->A,m,rows,n,cols,values,addv);
2604: }
2605: return(0);
2606: }
2608: static PetscErrorCode MatISZeroRowsColumnsLocal_Private(Mat A,PetscInt n,const PetscInt rows[],PetscScalar diag,PetscBool columns)
2609: {
2610: Mat_IS *is = (Mat_IS*)A->data;
2614: if (!n) {
2615: is->pure_neumann = PETSC_TRUE;
2616: } else {
2617: PetscInt i;
2618: is->pure_neumann = PETSC_FALSE;
2620: if (columns) {
2621: MatZeroRowsColumns(is->A,n,rows,diag,0,0);
2622: } else {
2623: MatZeroRows(is->A,n,rows,diag,0,0);
2624: }
2625: if (diag != 0.) {
2626: const PetscScalar *array;
2627: VecGetArrayRead(is->counter,&array);
2628: for (i=0; i<n; i++) {
2629: MatSetValue(is->A,rows[i],rows[i],diag/(array[rows[i]]),INSERT_VALUES);
2630: }
2631: VecRestoreArrayRead(is->counter,&array);
2632: }
2633: MatAssemblyBegin(is->A,MAT_FINAL_ASSEMBLY);
2634: MatAssemblyEnd(is->A,MAT_FINAL_ASSEMBLY);
2635: }
2636: return(0);
2637: }
2639: static PetscErrorCode MatZeroRowsColumns_Private_IS(Mat A,PetscInt n,const PetscInt rows[],PetscScalar diag,Vec x,Vec b,PetscBool columns)
2640: {
2641: Mat_IS *matis = (Mat_IS*)A->data;
2642: PetscInt nr,nl,len,i;
2643: PetscInt *lrows;
2647: #if defined(PETSC_USE_DEBUG)
2648: if (columns || diag != 0. || (x && b)) {
2649: PetscBool cong;
2651: PetscLayoutCompare(A->rmap,A->cmap,&cong);
2652: cong = (PetscBool)(cong && matis->sf == matis->csf);
2653: if (!cong && columns) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Columns can be zeroed if and only if A->rmap and A->cmap are congruent and the l2g maps are the same for MATIS");
2654: if (!cong && diag != 0.) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Nonzero diagonal value supported if and only if A->rmap and A->cmap are congruent and the l2g maps are the same for MATIS");
2655: if (!cong && x && b) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"A->rmap and A->cmap need to be congruent, and the l2g maps be the same");
2656: }
2657: #endif
2658: /* get locally owned rows */
2659: PetscLayoutMapLocal(A->rmap,n,rows,&len,&lrows,NULL);
2660: /* fix right hand side if needed */
2661: if (x && b) {
2662: const PetscScalar *xx;
2663: PetscScalar *bb;
2665: VecGetArrayRead(x, &xx);
2666: VecGetArray(b, &bb);
2667: for (i=0;i<len;++i) bb[lrows[i]] = diag*xx[lrows[i]];
2668: VecRestoreArrayRead(x, &xx);
2669: VecRestoreArray(b, &bb);
2670: }
2671: /* get rows associated to the local matrices */
2672: MatGetSize(matis->A,&nl,NULL);
2673: PetscArrayzero(matis->sf_leafdata,nl);
2674: PetscArrayzero(matis->sf_rootdata,A->rmap->n);
2675: for (i=0;i<len;i++) matis->sf_rootdata[lrows[i]] = 1;
2676: PetscFree(lrows);
2677: PetscSFBcastBegin(matis->sf,MPIU_INT,matis->sf_rootdata,matis->sf_leafdata);
2678: PetscSFBcastEnd(matis->sf,MPIU_INT,matis->sf_rootdata,matis->sf_leafdata);
2679: PetscMalloc1(nl,&lrows);
2680: for (i=0,nr=0;i<nl;i++) if (matis->sf_leafdata[i]) lrows[nr++] = i;
2681: MatISZeroRowsColumnsLocal_Private(A,nr,lrows,diag,columns);
2682: PetscFree(lrows);
2683: return(0);
2684: }
2686: static PetscErrorCode MatZeroRows_IS(Mat A,PetscInt n,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
2687: {
2691: MatZeroRowsColumns_Private_IS(A,n,rows,diag,x,b,PETSC_FALSE);
2692: return(0);
2693: }
2695: static PetscErrorCode MatZeroRowsColumns_IS(Mat A,PetscInt n,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
2696: {
2700: MatZeroRowsColumns_Private_IS(A,n,rows,diag,x,b,PETSC_TRUE);
2701: return(0);
2702: }
2704: static PetscErrorCode MatAssemblyBegin_IS(Mat A,MatAssemblyType type)
2705: {
2706: Mat_IS *is = (Mat_IS*)A->data;
2710: MatAssemblyBegin(is->A,type);
2711: return(0);
2712: }
2714: static PetscErrorCode MatAssemblyEnd_IS(Mat A,MatAssemblyType type)
2715: {
2716: Mat_IS *is = (Mat_IS*)A->data;
2720: MatAssemblyEnd(is->A,type);
2721: /* fix for local empty rows/cols */
2722: if (is->locempty && type == MAT_FINAL_ASSEMBLY) {
2723: Mat newlA;
2724: ISLocalToGlobalMapping rl2g,cl2g;
2725: IS nzr,nzc;
2726: PetscInt nr,nc,nnzr,nnzc;
2727: PetscBool lnewl2g,newl2g;
2729: MatGetSize(is->A,&nr,&nc);
2730: MatFindNonzeroRowsOrCols_Basic(is->A,PETSC_FALSE,PETSC_SMALL,&nzr);
2731: if (!nzr) {
2732: ISCreateStride(PetscObjectComm((PetscObject)is->A),nr,0,1,&nzr);
2733: }
2734: MatFindNonzeroRowsOrCols_Basic(is->A,PETSC_TRUE,PETSC_SMALL,&nzc);
2735: if (!nzc) {
2736: ISCreateStride(PetscObjectComm((PetscObject)is->A),nc,0,1,&nzc);
2737: }
2738: ISGetSize(nzr,&nnzr);
2739: ISGetSize(nzc,&nnzc);
2740: if (nnzr != nr || nnzc != nc) {
2741: ISLocalToGlobalMapping l2g;
2742: IS is1,is2;
2744: /* need new global l2g map */
2745: lnewl2g = PETSC_TRUE;
2746: MPI_Allreduce(&lnewl2g,&newl2g,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)A));
2748: /* extract valid submatrix */
2749: MatCreateSubMatrix(is->A,nzr,nzc,MAT_INITIAL_MATRIX,&newlA);
2751: /* attach local l2g maps for successive calls of MatSetValues on the local matrix */
2752: ISLocalToGlobalMappingCreateIS(nzr,&l2g);
2753: ISCreateStride(PetscObjectComm((PetscObject)is->A),nr,0,1,&is1);
2754: ISGlobalToLocalMappingApplyIS(l2g,IS_GTOLM_MASK,is1,&is2);
2755: ISLocalToGlobalMappingDestroy(&l2g);
2756: if (is->A->rmap->mapping) { /* the matrix has a local-to-local map already attached (probably DMDA generated) */
2757: const PetscInt *idxs1,*idxs2;
2758: PetscInt j,i,nl,*tidxs;
2760: ISLocalToGlobalMappingGetSize(is->A->rmap->mapping,&nl);
2761: ISLocalToGlobalMappingGetIndices(is->A->rmap->mapping,&idxs1);
2762: PetscMalloc1(nl,&tidxs);
2763: ISGetIndices(is2,&idxs2);
2764: for (i=0,j=0;i<nl;i++) tidxs[i] = idxs1[i] < 0 ? -1 : idxs2[j++];
2765: if (j != nr) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected count %D != %D",j,nr);
2766: ISLocalToGlobalMappingRestoreIndices(is->A->rmap->mapping,&idxs1);
2767: ISRestoreIndices(is2,&idxs2);
2768: ISDestroy(&is2);
2769: ISCreateGeneral(PetscObjectComm((PetscObject)is->A->rmap->mapping),nl,tidxs,PETSC_OWN_POINTER,&is2);
2770: }
2771: ISLocalToGlobalMappingCreateIS(is2,&rl2g);
2772: ISDestroy(&is1);
2773: ISDestroy(&is2);
2775: ISLocalToGlobalMappingCreateIS(nzc,&l2g);
2776: ISCreateStride(PetscObjectComm((PetscObject)is->A),nc,0,1,&is1);
2777: ISGlobalToLocalMappingApplyIS(l2g,IS_GTOLM_MASK,is1,&is2);
2778: ISLocalToGlobalMappingDestroy(&l2g);
2779: if (is->A->cmap->mapping) { /* the matrix has a local-to-local map already attached (probably DMDA generated) */
2780: const PetscInt *idxs1,*idxs2;
2781: PetscInt j,i,nl,*tidxs;
2783: ISLocalToGlobalMappingGetSize(is->A->cmap->mapping,&nl);
2784: ISLocalToGlobalMappingGetIndices(is->A->cmap->mapping,&idxs1);
2785: PetscMalloc1(nl,&tidxs);
2786: ISGetIndices(is2,&idxs2);
2787: for (i=0,j=0;i<nl;i++) tidxs[i] = idxs1[i] < 0 ? -1 : idxs2[j++];
2788: if (j != nc) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected count %D != %D",j,nc);
2789: ISLocalToGlobalMappingRestoreIndices(is->A->cmap->mapping,&idxs1);
2790: ISRestoreIndices(is2,&idxs2);
2791: ISDestroy(&is2);
2792: ISCreateGeneral(PetscObjectComm((PetscObject)is->A->cmap->mapping),nl,tidxs,PETSC_OWN_POINTER,&is2);
2793: }
2794: ISLocalToGlobalMappingCreateIS(is2,&cl2g);
2795: ISDestroy(&is1);
2796: ISDestroy(&is2);
2798: MatSetLocalToGlobalMapping(newlA,rl2g,cl2g);
2800: ISLocalToGlobalMappingDestroy(&rl2g);
2801: ISLocalToGlobalMappingDestroy(&cl2g);
2802: } else { /* local matrix fully populated */
2803: lnewl2g = PETSC_FALSE;
2804: MPI_Allreduce(&lnewl2g,&newl2g,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)A));
2805: PetscObjectReference((PetscObject)is->A);
2806: newlA = is->A;
2807: }
2808: /* attach new global l2g map if needed */
2809: if (newl2g) {
2810: IS gnzr,gnzc;
2811: const PetscInt *grid,*gcid;
2813: ISLocalToGlobalMappingApplyIS(A->rmap->mapping,nzr,&gnzr);
2814: ISLocalToGlobalMappingApplyIS(A->cmap->mapping,nzc,&gnzc);
2815: ISGetIndices(gnzr,&grid);
2816: ISGetIndices(gnzc,&gcid);
2817: ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)A),1,nnzr,grid,PETSC_COPY_VALUES,&rl2g);
2818: ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)A),1,nnzc,gcid,PETSC_COPY_VALUES,&cl2g);
2819: ISRestoreIndices(gnzr,&grid);
2820: ISRestoreIndices(gnzc,&gcid);
2821: ISDestroy(&gnzr);
2822: ISDestroy(&gnzc);
2823: MatSetLocalToGlobalMapping(A,rl2g,cl2g);
2824: ISLocalToGlobalMappingDestroy(&rl2g);
2825: ISLocalToGlobalMappingDestroy(&cl2g);
2826: }
2827: MatISSetLocalMat(A,newlA);
2828: MatDestroy(&newlA);
2829: ISDestroy(&nzr);
2830: ISDestroy(&nzc);
2831: is->locempty = PETSC_FALSE;
2832: }
2833: return(0);
2834: }
2836: static PetscErrorCode MatISGetLocalMat_IS(Mat mat,Mat *local)
2837: {
2838: Mat_IS *is = (Mat_IS*)mat->data;
2841: *local = is->A;
2842: return(0);
2843: }
2845: static PetscErrorCode MatISRestoreLocalMat_IS(Mat mat,Mat *local)
2846: {
2848: *local = NULL;
2849: return(0);
2850: }
2852: /*@
2853: MatISGetLocalMat - Gets the local matrix stored inside a MATIS matrix.
2855: Input Parameter:
2856: . mat - the matrix
2858: Output Parameter:
2859: . local - the local matrix
2861: Level: advanced
2863: Notes:
2864: This can be called if you have precomputed the nonzero structure of the
2865: matrix and want to provide it to the inner matrix object to improve the performance
2866: of the MatSetValues() operation.
2868: Call MatISRestoreLocalMat() when finished with the local matrix.
2870: .seealso: MATIS
2871: @*/
2872: PetscErrorCode MatISGetLocalMat(Mat mat,Mat *local)
2873: {
2879: PetscUseMethod(mat,"MatISGetLocalMat_C",(Mat,Mat*),(mat,local));
2880: return(0);
2881: }
2883: /*@
2884: MatISRestoreLocalMat - Restores the local matrix obtained with MatISGetLocalMat()
2886: Input Parameter:
2887: . mat - the matrix
2889: Output Parameter:
2890: . local - the local matrix
2892: Level: advanced
2894: .seealso: MATIS
2895: @*/
2896: PetscErrorCode MatISRestoreLocalMat(Mat mat,Mat *local)
2897: {
2903: PetscUseMethod(mat,"MatISRestoreLocalMat_C",(Mat,Mat*),(mat,local));
2904: return(0);
2905: }
2907: static PetscErrorCode MatISSetLocalMatType_IS(Mat mat,MatType mtype)
2908: {
2909: Mat_IS *is = (Mat_IS*)mat->data;
2913: if (is->A) {
2914: MatSetType(is->A,mtype);
2915: }
2916: PetscFree(is->lmattype);
2917: PetscStrallocpy(mtype,&is->lmattype);
2918: return(0);
2919: }
2921: /*@
2922: MatISSetLocalMatType - Specifies the type of local matrix
2924: Input Parameter:
2925: + mat - the matrix
2926: - mtype - the local matrix type
2928: Output Parameter:
2930: Level: advanced
2932: .seealso: MATIS, MatSetType(), MatType
2933: @*/
2934: PetscErrorCode MatISSetLocalMatType(Mat mat,MatType mtype)
2935: {
2940: PetscUseMethod(mat,"MatISSetLocalMatType_C",(Mat,MatType),(mat,mtype));
2941: return(0);
2942: }
2944: static PetscErrorCode MatISSetLocalMat_IS(Mat mat,Mat local)
2945: {
2946: Mat_IS *is = (Mat_IS*)mat->data;
2947: PetscInt nrows,ncols,orows,ocols;
2949: MatType mtype,otype;
2950: PetscBool sametype = PETSC_TRUE;
2953: if (is->A) {
2954: MatGetSize(is->A,&orows,&ocols);
2955: MatGetSize(local,&nrows,&ncols);
2956: if (orows != nrows || ocols != ncols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local MATIS matrix should be of size %Dx%D (you passed a %Dx%D matrix)",orows,ocols,nrows,ncols);
2957: MatGetType(local,&mtype);
2958: MatGetType(is->A,&otype);
2959: PetscStrcmp(mtype,otype,&sametype);
2960: }
2961: PetscObjectReference((PetscObject)local);
2962: MatDestroy(&is->A);
2963: is->A = local;
2964: MatGetType(is->A,&mtype);
2965: MatISSetLocalMatType(mat,mtype);
2966: if (!sametype && !is->islocalref) {
2967: MatISSetUpScatters_Private(mat);
2968: }
2969: return(0);
2970: }
2972: /*@
2973: MatISSetLocalMat - Replace the local matrix stored inside a MATIS object.
2975: Collective on Mat
2977: Input Parameter:
2978: + mat - the matrix
2979: - local - the local matrix
2981: Output Parameter:
2983: Level: advanced
2985: Notes:
2986: This can be called if you have precomputed the local matrix and
2987: want to provide it to the matrix object MATIS.
2989: .seealso: MATIS
2990: @*/
2991: PetscErrorCode MatISSetLocalMat(Mat mat,Mat local)
2992: {
2998: PetscUseMethod(mat,"MatISSetLocalMat_C",(Mat,Mat),(mat,local));
2999: return(0);
3000: }
3002: static PetscErrorCode MatZeroEntries_IS(Mat A)
3003: {
3004: Mat_IS *a = (Mat_IS*)A->data;
3008: MatZeroEntries(a->A);
3009: return(0);
3010: }
3012: static PetscErrorCode MatScale_IS(Mat A,PetscScalar a)
3013: {
3014: Mat_IS *is = (Mat_IS*)A->data;
3018: MatScale(is->A,a);
3019: return(0);
3020: }
3022: static PetscErrorCode MatGetDiagonal_IS(Mat A, Vec v)
3023: {
3024: Mat_IS *is = (Mat_IS*)A->data;
3028: /* get diagonal of the local matrix */
3029: MatGetDiagonal(is->A,is->y);
3031: /* scatter diagonal back into global vector */
3032: VecSet(v,0);
3033: VecScatterBegin(is->rctx,is->y,v,ADD_VALUES,SCATTER_REVERSE);
3034: VecScatterEnd(is->rctx,is->y,v,ADD_VALUES,SCATTER_REVERSE);
3035: return(0);
3036: }
3038: static PetscErrorCode MatSetOption_IS(Mat A,MatOption op,PetscBool flg)
3039: {
3040: Mat_IS *a = (Mat_IS*)A->data;
3044: MatSetOption(a->A,op,flg);
3045: return(0);
3046: }
3048: static PetscErrorCode MatAXPY_IS(Mat Y,PetscScalar a,Mat X,MatStructure str)
3049: {
3050: Mat_IS *y = (Mat_IS*)Y->data;
3051: Mat_IS *x;
3052: #if defined(PETSC_USE_DEBUG)
3053: PetscBool ismatis;
3054: #endif
3058: #if defined(PETSC_USE_DEBUG)
3059: PetscObjectTypeCompare((PetscObject)X,MATIS,&ismatis);
3060: if (!ismatis) SETERRQ(PetscObjectComm((PetscObject)Y),PETSC_ERR_SUP,"Cannot call MatAXPY(Y,a,X,str) with X not of type MATIS");
3061: #endif
3062: x = (Mat_IS*)X->data;
3063: MatAXPY(y->A,a,x->A,str);
3064: return(0);
3065: }
3067: static PetscErrorCode MatGetLocalSubMatrix_IS(Mat A,IS row,IS col,Mat *submat)
3068: {
3069: Mat lA;
3070: Mat_IS *matis;
3071: ISLocalToGlobalMapping rl2g,cl2g;
3072: IS is;
3073: const PetscInt *rg,*rl;
3074: PetscInt nrg;
3075: PetscInt N,M,nrl,i,*idxs;
3076: PetscErrorCode ierr;
3079: ISLocalToGlobalMappingGetIndices(A->rmap->mapping,&rg);
3080: ISGetLocalSize(row,&nrl);
3081: ISGetIndices(row,&rl);
3082: ISLocalToGlobalMappingGetSize(A->rmap->mapping,&nrg);
3083: #if defined(PETSC_USE_DEBUG)
3084: for (i=0;i<nrl;i++) if (rl[i]>=nrg) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Local row index %D -> %D greater then maximum possible %D",i,rl[i],nrg);
3085: #endif
3086: PetscMalloc1(nrg,&idxs);
3087: /* map from [0,nrl) to row */
3088: for (i=0;i<nrl;i++) idxs[i] = rl[i];
3089: for (i=nrl;i<nrg;i++) idxs[i] = -1;
3090: ISRestoreIndices(row,&rl);
3091: ISLocalToGlobalMappingRestoreIndices(A->rmap->mapping,&rg);
3092: ISCreateGeneral(PetscObjectComm((PetscObject)A),nrg,idxs,PETSC_OWN_POINTER,&is);
3093: ISLocalToGlobalMappingCreateIS(is,&rl2g);
3094: ISDestroy(&is);
3095: /* compute new l2g map for columns */
3096: if (col != row || A->rmap->mapping != A->cmap->mapping) {
3097: const PetscInt *cg,*cl;
3098: PetscInt ncg;
3099: PetscInt ncl;
3101: ISLocalToGlobalMappingGetIndices(A->cmap->mapping,&cg);
3102: ISGetLocalSize(col,&ncl);
3103: ISGetIndices(col,&cl);
3104: ISLocalToGlobalMappingGetSize(A->cmap->mapping,&ncg);
3105: #if defined(PETSC_USE_DEBUG)
3106: for (i=0;i<ncl;i++) if (cl[i]>=ncg) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Local column index %D -> %D greater then maximum possible %D",i,cl[i],ncg);
3107: #endif
3108: PetscMalloc1(ncg,&idxs);
3109: /* map from [0,ncl) to col */
3110: for (i=0;i<ncl;i++) idxs[i] = cl[i];
3111: for (i=ncl;i<ncg;i++) idxs[i] = -1;
3112: ISRestoreIndices(col,&cl);
3113: ISLocalToGlobalMappingRestoreIndices(A->cmap->mapping,&cg);
3114: ISCreateGeneral(PetscObjectComm((PetscObject)A),ncg,idxs,PETSC_OWN_POINTER,&is);
3115: ISLocalToGlobalMappingCreateIS(is,&cl2g);
3116: ISDestroy(&is);
3117: } else {
3118: PetscObjectReference((PetscObject)rl2g);
3119: cl2g = rl2g;
3120: }
3121: /* create the MATIS submatrix */
3122: MatGetSize(A,&M,&N);
3123: MatCreate(PetscObjectComm((PetscObject)A),submat);
3124: MatSetSizes(*submat,PETSC_DECIDE,PETSC_DECIDE,M,N);
3125: MatSetType(*submat,MATIS);
3126: matis = (Mat_IS*)((*submat)->data);
3127: matis->islocalref = PETSC_TRUE;
3128: MatSetLocalToGlobalMapping(*submat,rl2g,cl2g);
3129: MatISGetLocalMat(A,&lA);
3130: MatISSetLocalMat(*submat,lA);
3131: MatSetUp(*submat);
3132: MatAssemblyBegin(*submat,MAT_FINAL_ASSEMBLY);
3133: MatAssemblyEnd(*submat,MAT_FINAL_ASSEMBLY);
3134: ISLocalToGlobalMappingDestroy(&rl2g);
3135: ISLocalToGlobalMappingDestroy(&cl2g);
3136: /* remove unsupported ops */
3137: PetscMemzero((*submat)->ops,sizeof(struct _MatOps));
3138: (*submat)->ops->destroy = MatDestroy_IS;
3139: (*submat)->ops->setvalueslocal = MatSetValuesLocal_SubMat_IS;
3140: (*submat)->ops->setvaluesblockedlocal = MatSetValuesBlockedLocal_SubMat_IS;
3141: (*submat)->ops->assemblybegin = MatAssemblyBegin_IS;
3142: (*submat)->ops->assemblyend = MatAssemblyEnd_IS;
3143: return(0);
3144: }
3146: static PetscErrorCode MatSetFromOptions_IS(PetscOptionItems *PetscOptionsObject, Mat A)
3147: {
3148: Mat_IS *a = (Mat_IS*)A->data;
3149: char type[256];
3150: PetscBool flg;
3154: PetscOptionsHead(PetscOptionsObject,"MATIS options");
3155: PetscOptionsBool("-matis_fixempty","Fix local matrices in case of empty local rows/columns","MatISFixLocalEmpty",a->locempty,&a->locempty,NULL);
3156: PetscOptionsBool("-matis_storel2l","Store local-to-local matrices generated from PtAP operations","MatISStoreL2L",a->storel2l,&a->storel2l,NULL);
3157: PetscOptionsFList("-matis_localmat_type","Matrix type","MatISSetLocalMatType",MatList,a->lmattype,type,256,&flg);
3158: if (flg) {
3159: MatISSetLocalMatType(A,type);
3160: }
3161: if (a->A) {
3162: MatSetFromOptions(a->A);
3163: }
3164: PetscOptionsTail();
3165: return(0);
3166: }
3168: /*@
3169: MatCreateIS - Creates a "process" unassembled matrix, assembled on each
3170: process but not across processes.
3172: Input Parameters:
3173: + comm - MPI communicator that will share the matrix
3174: . bs - block size of the matrix
3175: . m,n,M,N - local and/or global sizes of the left and right vector used in matrix vector products
3176: . rmap - local to global map for rows
3177: - cmap - local to global map for cols
3179: Output Parameter:
3180: . A - the resulting matrix
3182: Level: advanced
3184: Notes:
3185: See MATIS for more details.
3186: m and n are NOT related to the size of the map; they represent the size of the local parts of the vectors
3187: used in MatMult operations. The sizes of rmap and cmap define the size of the local matrices.
3188: If either rmap or cmap are NULL, then the matrix is assumed to be square.
3190: .seealso: MATIS, MatSetLocalToGlobalMapping()
3191: @*/
3192: PetscErrorCode MatCreateIS(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt M,PetscInt N,ISLocalToGlobalMapping rmap,ISLocalToGlobalMapping cmap,Mat *A)
3193: {
3197: if (!rmap && !cmap) SETERRQ(comm,PETSC_ERR_USER,"You need to provide at least one of the mappings");
3198: MatCreate(comm,A);
3199: MatSetSizes(*A,m,n,M,N);
3200: if (bs > 0) {
3201: MatSetBlockSize(*A,bs);
3202: }
3203: MatSetType(*A,MATIS);
3204: if (rmap && cmap) {
3205: MatSetLocalToGlobalMapping(*A,rmap,cmap);
3206: } else if (!rmap) {
3207: MatSetLocalToGlobalMapping(*A,cmap,cmap);
3208: } else {
3209: MatSetLocalToGlobalMapping(*A,rmap,rmap);
3210: }
3211: return(0);
3212: }
3214: /*MC
3215: MATIS - MATIS = "is" - A matrix type to be used for using the non-overlapping domain decomposition methods (e.g. PCBDDC or KSPFETIDP).
3216: This stores the matrices in globally unassembled form. Each processor assembles only its local Neumann problem and the parallel matrix vector
3217: product is handled "implicitly".
3219: Options Database Keys:
3220: + -mat_type is - sets the matrix type to "is" during a call to MatSetFromOptions()
3221: . -matis_fixempty - Fixes local matrices in case of empty local rows/columns.
3222: - -matis_storel2l - stores the local-to-local operators generated by the Galerkin process of MatPtAP().
3224: Notes:
3225: Options prefix for the inner matrix are given by -is_mat_xxx
3227: You must call MatSetLocalToGlobalMapping() before using this matrix type.
3229: You can do matrix preallocation on the local matrix after you obtain it with
3230: MatISGetLocalMat(); otherwise, you could use MatISSetPreallocation()
3232: Level: advanced
3234: .seealso: Mat, MatISGetLocalMat(), MatSetLocalToGlobalMapping(), MatISSetPreallocation(), MatCreateIS(), PCBDDC, KSPFETIDP
3236: M*/
3238: PETSC_EXTERN PetscErrorCode MatCreate_IS(Mat A)
3239: {
3241: Mat_IS *b;
3244: PetscNewLog(A,&b);
3245: PetscStrallocpy(MATAIJ,&b->lmattype);
3246: A->data = (void*)b;
3248: /* matrix ops */
3249: PetscMemzero(A->ops,sizeof(struct _MatOps));
3250: A->ops->mult = MatMult_IS;
3251: A->ops->multadd = MatMultAdd_IS;
3252: A->ops->multtranspose = MatMultTranspose_IS;
3253: A->ops->multtransposeadd = MatMultTransposeAdd_IS;
3254: A->ops->destroy = MatDestroy_IS;
3255: A->ops->setlocaltoglobalmapping = MatSetLocalToGlobalMapping_IS;
3256: A->ops->setvalues = MatSetValues_IS;
3257: A->ops->setvaluesblocked = MatSetValuesBlocked_IS;
3258: A->ops->setvalueslocal = MatSetValuesLocal_IS;
3259: A->ops->setvaluesblockedlocal = MatSetValuesBlockedLocal_IS;
3260: A->ops->zerorows = MatZeroRows_IS;
3261: A->ops->zerorowscolumns = MatZeroRowsColumns_IS;
3262: A->ops->assemblybegin = MatAssemblyBegin_IS;
3263: A->ops->assemblyend = MatAssemblyEnd_IS;
3264: A->ops->view = MatView_IS;
3265: A->ops->zeroentries = MatZeroEntries_IS;
3266: A->ops->scale = MatScale_IS;
3267: A->ops->getdiagonal = MatGetDiagonal_IS;
3268: A->ops->setoption = MatSetOption_IS;
3269: A->ops->ishermitian = MatIsHermitian_IS;
3270: A->ops->issymmetric = MatIsSymmetric_IS;
3271: A->ops->isstructurallysymmetric = MatIsStructurallySymmetric_IS;
3272: A->ops->duplicate = MatDuplicate_IS;
3273: A->ops->missingdiagonal = MatMissingDiagonal_IS;
3274: A->ops->copy = MatCopy_IS;
3275: A->ops->getlocalsubmatrix = MatGetLocalSubMatrix_IS;
3276: A->ops->createsubmatrix = MatCreateSubMatrix_IS;
3277: A->ops->axpy = MatAXPY_IS;
3278: A->ops->diagonalset = MatDiagonalSet_IS;
3279: A->ops->shift = MatShift_IS;
3280: A->ops->transpose = MatTranspose_IS;
3281: A->ops->getinfo = MatGetInfo_IS;
3282: A->ops->diagonalscale = MatDiagonalScale_IS;
3283: A->ops->setfromoptions = MatSetFromOptions_IS;
3285: /* special MATIS functions */
3286: PetscObjectComposeFunction((PetscObject)A,"MatISSetLocalMatType_C",MatISSetLocalMatType_IS);
3287: PetscObjectComposeFunction((PetscObject)A,"MatISGetLocalMat_C",MatISGetLocalMat_IS);
3288: PetscObjectComposeFunction((PetscObject)A,"MatISRestoreLocalMat_C",MatISRestoreLocalMat_IS);
3289: PetscObjectComposeFunction((PetscObject)A,"MatISSetLocalMat_C",MatISSetLocalMat_IS);
3290: PetscObjectComposeFunction((PetscObject)A,"MatISGetMPIXAIJ_C",MatConvert_IS_XAIJ);
3291: PetscObjectComposeFunction((PetscObject)A,"MatISSetPreallocation_C",MatISSetPreallocation_IS);
3292: PetscObjectComposeFunction((PetscObject)A,"MatISStoreL2L_C",MatISStoreL2L_IS);
3293: PetscObjectComposeFunction((PetscObject)A,"MatISFixLocalEmpty_C",MatISFixLocalEmpty_IS);
3294: PetscObjectComposeFunction((PetscObject)A,"MatConvert_is_mpiaij_C",MatConvert_IS_XAIJ);
3295: PetscObjectComposeFunction((PetscObject)A,"MatConvert_is_mpibaij_C",MatConvert_IS_XAIJ);
3296: PetscObjectComposeFunction((PetscObject)A,"MatConvert_is_mpisbaij_C",MatConvert_IS_XAIJ);
3297: PetscObjectComposeFunction((PetscObject)A,"MatConvert_is_seqaij_C",MatConvert_IS_XAIJ);
3298: PetscObjectComposeFunction((PetscObject)A,"MatConvert_is_seqbaij_C",MatConvert_IS_XAIJ);
3299: PetscObjectComposeFunction((PetscObject)A,"MatConvert_is_seqsbaij_C",MatConvert_IS_XAIJ);
3300: PetscObjectComposeFunction((PetscObject)A,"MatConvert_is_aij_C",MatConvert_IS_XAIJ);
3301: PetscObjectChangeTypeName((PetscObject)A,MATIS);
3302: return(0);
3303: }