xref: /petsc/src/ksp/pc/impls/telescope/telescope_dmda.c (revision 1e07b27e81e393854908e91a98c07414449fdbdc)
1*1e07b27eSBarry Smith 
2*1e07b27eSBarry Smith #include <petsc/private/pcimpl.h>
3*1e07b27eSBarry Smith #include <petscksp.h>           /*I "petscksp.h" I*/
4*1e07b27eSBarry Smith #include <petscdm.h>
5*1e07b27eSBarry Smith #include <petscdmda.h>
6*1e07b27eSBarry Smith 
7*1e07b27eSBarry Smith #include "telescope.h"
8*1e07b27eSBarry Smith 
9*1e07b27eSBarry Smith #undef __FUNCT__
10*1e07b27eSBarry Smith #define __FUNCT__ "_DMDADetermineRankFromGlobalIJK"
11*1e07b27eSBarry Smith PetscErrorCode _DMDADetermineRankFromGlobalIJK(PetscInt dim,PetscInt i,PetscInt j,PetscInt k,
12*1e07b27eSBarry Smith                                                PetscInt Mp,PetscInt Np,PetscInt Pp,
13*1e07b27eSBarry Smith                                                PetscInt start_i[],PetscInt start_j[],PetscInt start_k[],
14*1e07b27eSBarry Smith                                                PetscInt span_i[],PetscInt span_j[],PetscInt span_k[],
15*1e07b27eSBarry Smith                                                PetscMPIInt *_pi,PetscMPIInt *_pj,PetscMPIInt *_pk,PetscMPIInt *rank_re)
16*1e07b27eSBarry Smith {
17*1e07b27eSBarry Smith   PetscInt pi,pj,pk,n;
18*1e07b27eSBarry Smith 
19*1e07b27eSBarry Smith   PetscFunctionBegin;
20*1e07b27eSBarry Smith   pi = pj = pk = -1;
21*1e07b27eSBarry Smith   if (_pi) {
22*1e07b27eSBarry Smith     for (n=0; n<Mp; n++) {
23*1e07b27eSBarry Smith       if ( (i >= start_i[n]) && (i < start_i[n]+span_i[n]) ) {
24*1e07b27eSBarry Smith         pi = n;
25*1e07b27eSBarry Smith         break;
26*1e07b27eSBarry Smith       }
27*1e07b27eSBarry Smith     }
28*1e07b27eSBarry Smith     if (pi == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"[dmda-ijk] pi cannot be determined : range %D, val %D",Mp,i);
29*1e07b27eSBarry Smith     *_pi = pi;
30*1e07b27eSBarry Smith   }
31*1e07b27eSBarry Smith 
32*1e07b27eSBarry Smith   if (_pj) {
33*1e07b27eSBarry Smith     for (n=0; n<Np; n++) {
34*1e07b27eSBarry Smith       if ( (j >= start_j[n]) && (j < start_j[n]+span_j[n]) ) {
35*1e07b27eSBarry Smith         pj = n;
36*1e07b27eSBarry Smith         break;
37*1e07b27eSBarry Smith       }
38*1e07b27eSBarry Smith     }
39*1e07b27eSBarry Smith     if (pj == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"[dmda-ijk] pj cannot be determined : range %D, val %D",Np,j);
40*1e07b27eSBarry Smith     *_pj = pj;
41*1e07b27eSBarry Smith   }
42*1e07b27eSBarry Smith 
43*1e07b27eSBarry Smith   if (_pk) {
44*1e07b27eSBarry Smith     for (n=0; n<Pp; n++) {
45*1e07b27eSBarry Smith       if ( (k >= start_k[n]) && (k < start_k[n]+span_k[n]) ) {
46*1e07b27eSBarry Smith         pk = n;
47*1e07b27eSBarry Smith         break;
48*1e07b27eSBarry Smith       }
49*1e07b27eSBarry Smith     }
50*1e07b27eSBarry Smith     if (pk == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"[dmda-ijk] pk cannot be determined : range %D, val %D",Pp,k);
51*1e07b27eSBarry Smith     *_pk = pk;
52*1e07b27eSBarry Smith   }
53*1e07b27eSBarry Smith 
54*1e07b27eSBarry Smith   switch (dim) {
55*1e07b27eSBarry Smith     case 1:
56*1e07b27eSBarry Smith       *rank_re = pi;
57*1e07b27eSBarry Smith       break;
58*1e07b27eSBarry Smith     case 2:
59*1e07b27eSBarry Smith       *rank_re = pi + pj * Mp;
60*1e07b27eSBarry Smith       break;
61*1e07b27eSBarry Smith     case 3:
62*1e07b27eSBarry Smith       *rank_re = pi + pj * Mp + pk * (Mp*Np);
63*1e07b27eSBarry Smith       break;
64*1e07b27eSBarry Smith   }
65*1e07b27eSBarry Smith   PetscFunctionReturn(0);
66*1e07b27eSBarry Smith }
67*1e07b27eSBarry Smith 
68*1e07b27eSBarry Smith #undef __FUNCT__
69*1e07b27eSBarry Smith #define __FUNCT__ "_DMDADetermineGlobalS0"
70*1e07b27eSBarry Smith PetscErrorCode _DMDADetermineGlobalS0(PetscInt dim,PetscMPIInt rank_re,PetscInt Mp_re,PetscInt Np_re,PetscInt Pp_re,
71*1e07b27eSBarry Smith                                       PetscInt range_i_re[],PetscInt range_j_re[],PetscInt range_k_re[],PetscInt *s0)
72*1e07b27eSBarry Smith {
73*1e07b27eSBarry Smith   PetscInt i,j,k,start_IJK;
74*1e07b27eSBarry Smith   PetscInt rank_ijk;
75*1e07b27eSBarry Smith 
76*1e07b27eSBarry Smith   PetscFunctionBegin;
77*1e07b27eSBarry Smith   switch (dim) {
78*1e07b27eSBarry Smith     case 1:
79*1e07b27eSBarry Smith       start_IJK = 0;
80*1e07b27eSBarry Smith       for (i=0; i<Mp_re; i++) {
81*1e07b27eSBarry Smith         rank_ijk = i;
82*1e07b27eSBarry Smith         if (rank_ijk < rank_re) {
83*1e07b27eSBarry Smith           start_IJK += range_i_re[i];
84*1e07b27eSBarry Smith         }
85*1e07b27eSBarry Smith       }
86*1e07b27eSBarry Smith       break;
87*1e07b27eSBarry Smith     case 2:
88*1e07b27eSBarry Smith       start_IJK = 0;
89*1e07b27eSBarry Smith       for (j=0; j<Np_re; j++) {
90*1e07b27eSBarry Smith         for (i=0; i<Mp_re; i++) {
91*1e07b27eSBarry Smith           rank_ijk = i + j*Mp_re;
92*1e07b27eSBarry Smith           if (rank_ijk < rank_re) {
93*1e07b27eSBarry Smith             start_IJK += range_i_re[i]*range_j_re[j];
94*1e07b27eSBarry Smith           }
95*1e07b27eSBarry Smith         }
96*1e07b27eSBarry Smith       }
97*1e07b27eSBarry Smith       break;
98*1e07b27eSBarry Smith     case 3:
99*1e07b27eSBarry Smith       start_IJK = 0;
100*1e07b27eSBarry Smith       for (k=0; k<Pp_re; k++) {
101*1e07b27eSBarry Smith         for (j=0; j<Np_re; j++) {
102*1e07b27eSBarry Smith           for (i=0; i<Mp_re; i++) {
103*1e07b27eSBarry Smith             rank_ijk = i + j*Mp_re + k*Mp_re*Np_re;
104*1e07b27eSBarry Smith             if (rank_ijk < rank_re) {
105*1e07b27eSBarry Smith               start_IJK += range_i_re[i]*range_j_re[j]*range_k_re[k];
106*1e07b27eSBarry Smith             }
107*1e07b27eSBarry Smith           }
108*1e07b27eSBarry Smith         }
109*1e07b27eSBarry Smith       }
110*1e07b27eSBarry Smith       break;
111*1e07b27eSBarry Smith   }
112*1e07b27eSBarry Smith   *s0 = start_IJK;
113*1e07b27eSBarry Smith   PetscFunctionReturn(0);
114*1e07b27eSBarry Smith }
115*1e07b27eSBarry Smith 
116*1e07b27eSBarry Smith #undef __FUNCT__
117*1e07b27eSBarry Smith #define __FUNCT__ "PCTelescopeSetUp_dmda_repart_coors2d"
118*1e07b27eSBarry Smith PetscErrorCode PCTelescopeSetUp_dmda_repart_coors2d(PetscSubcomm psubcomm,DM dm,DM subdm)
119*1e07b27eSBarry Smith {
120*1e07b27eSBarry Smith   PetscErrorCode ierr;
121*1e07b27eSBarry Smith   DM             cdm;
122*1e07b27eSBarry Smith   Vec            coor,coor_natural,perm_coors;
123*1e07b27eSBarry Smith   PetscInt       i,j,si,sj,ni,nj,M,N,Ml,Nl,c,nidx;
124*1e07b27eSBarry Smith   PetscInt       *fine_indices;
125*1e07b27eSBarry Smith   IS             is_fine,is_local;
126*1e07b27eSBarry Smith   VecScatter     sctx;
127*1e07b27eSBarry Smith 
128*1e07b27eSBarry Smith   PetscFunctionBegin;
129*1e07b27eSBarry Smith   ierr = DMGetCoordinates(dm,&coor);CHKERRQ(ierr);
130*1e07b27eSBarry Smith   if (!coor) return(0);
131*1e07b27eSBarry Smith   if (isActiveRank(psubcomm)) {
132*1e07b27eSBarry Smith     ierr = DMDASetUniformCoordinates(subdm,0.0,1.0,0.0,1.0,0.0,1.0);CHKERRQ(ierr);
133*1e07b27eSBarry Smith   }
134*1e07b27eSBarry Smith   /* Get the coordinate vector from the distributed array */
135*1e07b27eSBarry Smith   ierr = DMGetCoordinateDM(dm,&cdm);CHKERRQ(ierr);
136*1e07b27eSBarry Smith   ierr = DMDACreateNaturalVector(cdm,&coor_natural);CHKERRQ(ierr);
137*1e07b27eSBarry Smith 
138*1e07b27eSBarry Smith   ierr = DMDAGlobalToNaturalBegin(cdm,coor,INSERT_VALUES,coor_natural);CHKERRQ(ierr);
139*1e07b27eSBarry Smith   ierr = DMDAGlobalToNaturalEnd(cdm,coor,INSERT_VALUES,coor_natural);CHKERRQ(ierr);
140*1e07b27eSBarry Smith 
141*1e07b27eSBarry Smith   /* get indices of the guys I want to grab */
142*1e07b27eSBarry Smith   ierr = DMDAGetInfo(dm,NULL,&M,&N,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
143*1e07b27eSBarry Smith   if (isActiveRank(psubcomm)) {
144*1e07b27eSBarry Smith     ierr = DMDAGetCorners(subdm,&si,&sj,NULL,&ni,&nj,NULL);CHKERRQ(ierr);
145*1e07b27eSBarry Smith 
146*1e07b27eSBarry Smith     Ml = ni;
147*1e07b27eSBarry Smith     Nl = nj;
148*1e07b27eSBarry Smith   } else {
149*1e07b27eSBarry Smith     Ml = Nl = 1;
150*1e07b27eSBarry Smith   }
151*1e07b27eSBarry Smith 
152*1e07b27eSBarry Smith   ierr = PetscMalloc(sizeof(PetscInt)*Ml*Nl*2,&fine_indices);CHKERRQ(ierr);
153*1e07b27eSBarry Smith   c = 0;
154*1e07b27eSBarry Smith   if (isActiveRank(psubcomm)) {
155*1e07b27eSBarry Smith     for (j=sj; j<sj+nj; j++) {
156*1e07b27eSBarry Smith       for (i=si; i<si+ni; i++) {
157*1e07b27eSBarry Smith         nidx = (i) + (j)*M;
158*1e07b27eSBarry Smith         fine_indices[c  ] = 2 * nidx     ;
159*1e07b27eSBarry Smith         fine_indices[c+1] = 2 * nidx + 1 ;
160*1e07b27eSBarry Smith         c = c + 2;
161*1e07b27eSBarry Smith       }
162*1e07b27eSBarry Smith     }
163*1e07b27eSBarry Smith   } else {
164*1e07b27eSBarry Smith     i = si;
165*1e07b27eSBarry Smith     j = sj;
166*1e07b27eSBarry Smith     nidx = (i) + (j)*M;
167*1e07b27eSBarry Smith     fine_indices[0] = 2 * nidx     ;
168*1e07b27eSBarry Smith     fine_indices[1] = 2 * nidx + 1 ;
169*1e07b27eSBarry Smith   }
170*1e07b27eSBarry Smith 
171*1e07b27eSBarry Smith   /* generate scatter */
172*1e07b27eSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm),Ml*Nl*2,fine_indices,PETSC_USE_POINTER,&is_fine);CHKERRQ(ierr);
173*1e07b27eSBarry Smith   ierr = ISCreateStride(PETSC_COMM_SELF,Ml*Nl*2,0,1,&is_local);CHKERRQ(ierr);
174*1e07b27eSBarry Smith 
175*1e07b27eSBarry Smith   /* scatter */
176*1e07b27eSBarry Smith   ierr = VecCreate(PETSC_COMM_SELF,&perm_coors);CHKERRQ(ierr);
177*1e07b27eSBarry Smith   ierr = VecSetSizes(perm_coors,PETSC_DECIDE,Ml*Nl*2);CHKERRQ(ierr);
178*1e07b27eSBarry Smith   ierr = VecSetType(perm_coors,VECSEQ);CHKERRQ(ierr);
179*1e07b27eSBarry Smith 
180*1e07b27eSBarry Smith   ierr = VecScatterCreate(coor_natural,is_fine,perm_coors,is_local,&sctx);CHKERRQ(ierr);
181*1e07b27eSBarry Smith   ierr = VecScatterBegin(sctx,coor_natural,perm_coors,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
182*1e07b27eSBarry Smith   ierr = VecScatterEnd(  sctx,coor_natural,perm_coors,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
183*1e07b27eSBarry Smith   /* access */
184*1e07b27eSBarry Smith   if (isActiveRank(psubcomm)) {
185*1e07b27eSBarry Smith     Vec               _coors;
186*1e07b27eSBarry Smith     const PetscScalar *LA_perm;
187*1e07b27eSBarry Smith     PetscScalar       *LA_coors;
188*1e07b27eSBarry Smith 
189*1e07b27eSBarry Smith     ierr = DMGetCoordinates(subdm,&_coors);CHKERRQ(ierr);
190*1e07b27eSBarry Smith     ierr = VecGetArrayRead(perm_coors,&LA_perm);CHKERRQ(ierr);
191*1e07b27eSBarry Smith     ierr = VecGetArray(_coors,&LA_coors);CHKERRQ(ierr);
192*1e07b27eSBarry Smith     for (i=0; i<Ml*Nl*2; i++) {
193*1e07b27eSBarry Smith       LA_coors[i] = LA_perm[i];
194*1e07b27eSBarry Smith     }
195*1e07b27eSBarry Smith     ierr = VecRestoreArray(_coors,&LA_coors);CHKERRQ(ierr);
196*1e07b27eSBarry Smith     ierr = VecRestoreArrayRead(perm_coors,&LA_perm);CHKERRQ(ierr);
197*1e07b27eSBarry Smith   }
198*1e07b27eSBarry Smith 
199*1e07b27eSBarry Smith   /* update local coords */
200*1e07b27eSBarry Smith   if (isActiveRank(psubcomm)) {
201*1e07b27eSBarry Smith     DM  _dmc;
202*1e07b27eSBarry Smith     Vec _coors,_coors_local;
203*1e07b27eSBarry Smith     ierr = DMGetCoordinateDM(subdm,&_dmc);CHKERRQ(ierr);
204*1e07b27eSBarry Smith     ierr = DMGetCoordinates(subdm,&_coors);CHKERRQ(ierr);
205*1e07b27eSBarry Smith     ierr = DMGetCoordinatesLocal(subdm,&_coors_local);CHKERRQ(ierr);
206*1e07b27eSBarry Smith     ierr = DMGlobalToLocalBegin(_dmc,_coors,INSERT_VALUES,_coors_local);CHKERRQ(ierr);
207*1e07b27eSBarry Smith     ierr = DMGlobalToLocalEnd(_dmc,_coors,INSERT_VALUES,_coors_local);CHKERRQ(ierr);
208*1e07b27eSBarry Smith   }
209*1e07b27eSBarry Smith   ierr = VecScatterDestroy(&sctx);CHKERRQ(ierr);
210*1e07b27eSBarry Smith   ierr = ISDestroy(&is_fine);CHKERRQ(ierr);
211*1e07b27eSBarry Smith   ierr = PetscFree(fine_indices);CHKERRQ(ierr);
212*1e07b27eSBarry Smith   ierr = ISDestroy(&is_local);CHKERRQ(ierr);
213*1e07b27eSBarry Smith   ierr = VecDestroy(&perm_coors);CHKERRQ(ierr);
214*1e07b27eSBarry Smith   ierr = VecDestroy(&coor_natural);CHKERRQ(ierr);
215*1e07b27eSBarry Smith   PetscFunctionReturn(0);
216*1e07b27eSBarry Smith }
217*1e07b27eSBarry Smith 
218*1e07b27eSBarry Smith #undef __FUNCT__
219*1e07b27eSBarry Smith #define __FUNCT__ "PCTelescopeSetUp_dmda_repart_coors3d"
220*1e07b27eSBarry Smith PetscErrorCode PCTelescopeSetUp_dmda_repart_coors3d(PetscSubcomm psubcomm,DM dm,DM subdm)
221*1e07b27eSBarry Smith {
222*1e07b27eSBarry Smith   PetscErrorCode ierr;
223*1e07b27eSBarry Smith   DM             cdm;
224*1e07b27eSBarry Smith   Vec            coor,coor_natural,perm_coors;
225*1e07b27eSBarry Smith   PetscInt       i,j,k,si,sj,sk,ni,nj,nk,M,N,P,Ml,Nl,Pl,c,nidx;
226*1e07b27eSBarry Smith   PetscInt       *fine_indices;
227*1e07b27eSBarry Smith   IS             is_fine,is_local;
228*1e07b27eSBarry Smith   VecScatter     sctx;
229*1e07b27eSBarry Smith 
230*1e07b27eSBarry Smith   PetscFunctionBegin;
231*1e07b27eSBarry Smith   ierr = DMGetCoordinates(dm,&coor);CHKERRQ(ierr);
232*1e07b27eSBarry Smith   if (!coor) PetscFunctionReturn(0);
233*1e07b27eSBarry Smith 
234*1e07b27eSBarry Smith   if (isActiveRank(psubcomm)) {
235*1e07b27eSBarry Smith     ierr = DMDASetUniformCoordinates(subdm,0.0,1.0,0.0,1.0,0.0,1.0);CHKERRQ(ierr);
236*1e07b27eSBarry Smith   }
237*1e07b27eSBarry Smith 
238*1e07b27eSBarry Smith   /* Get the coordinate vector from the distributed array */
239*1e07b27eSBarry Smith   ierr = DMGetCoordinateDM(dm,&cdm);CHKERRQ(ierr);
240*1e07b27eSBarry Smith   ierr = DMDACreateNaturalVector(cdm,&coor_natural);CHKERRQ(ierr);
241*1e07b27eSBarry Smith   ierr = DMDAGlobalToNaturalBegin(cdm,coor,INSERT_VALUES,coor_natural);CHKERRQ(ierr);
242*1e07b27eSBarry Smith   ierr = DMDAGlobalToNaturalEnd(cdm,coor,INSERT_VALUES,coor_natural);CHKERRQ(ierr);
243*1e07b27eSBarry Smith 
244*1e07b27eSBarry Smith   /* get indices of the guys I want to grab */
245*1e07b27eSBarry Smith   ierr = DMDAGetInfo(dm,NULL,&M,&N,&P,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
246*1e07b27eSBarry Smith 
247*1e07b27eSBarry Smith   if (isActiveRank(psubcomm)) {
248*1e07b27eSBarry Smith     ierr = DMDAGetCorners(subdm,&si,&sj,&sk,&ni,&nj,&nk);CHKERRQ(ierr);
249*1e07b27eSBarry Smith 
250*1e07b27eSBarry Smith     Ml = ni;
251*1e07b27eSBarry Smith     Nl = nj;
252*1e07b27eSBarry Smith     Pl = nk;
253*1e07b27eSBarry Smith   } else {
254*1e07b27eSBarry Smith     Ml = Nl = Pl = 1;
255*1e07b27eSBarry Smith   }
256*1e07b27eSBarry Smith 
257*1e07b27eSBarry Smith   ierr = PetscMalloc(sizeof(PetscInt)*Ml*Nl*Pl*3,&fine_indices);CHKERRQ(ierr);
258*1e07b27eSBarry Smith 
259*1e07b27eSBarry Smith   c = 0;
260*1e07b27eSBarry Smith   if (isActiveRank(psubcomm)) {
261*1e07b27eSBarry Smith     for (k=sk; k<sk+nk; k++) {
262*1e07b27eSBarry Smith       for (j=sj; j<sj+nj; j++) {
263*1e07b27eSBarry Smith         for (i=si; i<si+ni; i++) {
264*1e07b27eSBarry Smith           nidx = (i) + (j)*M + (k)*M*N;
265*1e07b27eSBarry Smith           fine_indices[c  ] = 3 * nidx     ;
266*1e07b27eSBarry Smith           fine_indices[c+1] = 3 * nidx + 1 ;
267*1e07b27eSBarry Smith           fine_indices[c+2] = 3 * nidx + 2 ;
268*1e07b27eSBarry Smith           c = c + 3;
269*1e07b27eSBarry Smith         }
270*1e07b27eSBarry Smith       }
271*1e07b27eSBarry Smith     }
272*1e07b27eSBarry Smith   } else {
273*1e07b27eSBarry Smith     i = si;
274*1e07b27eSBarry Smith     j = sj;
275*1e07b27eSBarry Smith     k = sk;
276*1e07b27eSBarry Smith     nidx = (i) + (j)*M + (k)*M*N;
277*1e07b27eSBarry Smith     fine_indices[0] = 3 * nidx     ;
278*1e07b27eSBarry Smith     fine_indices[1] = 3 * nidx + 1 ;
279*1e07b27eSBarry Smith     fine_indices[2] = 3 * nidx + 2 ;
280*1e07b27eSBarry Smith   }
281*1e07b27eSBarry Smith 
282*1e07b27eSBarry Smith   /* generate scatter */
283*1e07b27eSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm),Ml*Nl*Pl*3,fine_indices,PETSC_USE_POINTER,&is_fine);CHKERRQ(ierr);
284*1e07b27eSBarry Smith   ierr = ISCreateStride(PETSC_COMM_SELF,Ml*Nl*Pl*3,0,1,&is_local);CHKERRQ(ierr);
285*1e07b27eSBarry Smith 
286*1e07b27eSBarry Smith   /* scatter */
287*1e07b27eSBarry Smith   ierr = VecCreate(PETSC_COMM_SELF,&perm_coors);CHKERRQ(ierr);
288*1e07b27eSBarry Smith   ierr = VecSetSizes(perm_coors,PETSC_DECIDE,Ml*Nl*Pl*3);CHKERRQ(ierr);
289*1e07b27eSBarry Smith   ierr = VecSetType(perm_coors,VECSEQ);CHKERRQ(ierr);
290*1e07b27eSBarry Smith   ierr = VecScatterCreate(coor_natural,is_fine,perm_coors,is_local,&sctx);CHKERRQ(ierr);
291*1e07b27eSBarry Smith   ierr = VecScatterBegin(sctx,coor_natural,perm_coors,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
292*1e07b27eSBarry Smith   ierr = VecScatterEnd(  sctx,coor_natural,perm_coors,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
293*1e07b27eSBarry Smith 
294*1e07b27eSBarry Smith   /* access */
295*1e07b27eSBarry Smith   if (isActiveRank(psubcomm)) {
296*1e07b27eSBarry Smith     Vec               _coors;
297*1e07b27eSBarry Smith     const PetscScalar *LA_perm;
298*1e07b27eSBarry Smith     PetscScalar       *LA_coors;
299*1e07b27eSBarry Smith 
300*1e07b27eSBarry Smith     ierr = DMGetCoordinates(subdm,&_coors);CHKERRQ(ierr);
301*1e07b27eSBarry Smith     ierr = VecGetArrayRead(perm_coors,&LA_perm);CHKERRQ(ierr);
302*1e07b27eSBarry Smith     ierr = VecGetArray(_coors,&LA_coors);CHKERRQ(ierr);
303*1e07b27eSBarry Smith     for (i=0; i<Ml*Nl*Pl*3; i++) {
304*1e07b27eSBarry Smith       LA_coors[i] = LA_perm[i];
305*1e07b27eSBarry Smith     }
306*1e07b27eSBarry Smith     ierr = VecRestoreArray(_coors,&LA_coors);CHKERRQ(ierr);
307*1e07b27eSBarry Smith     ierr = VecRestoreArrayRead(perm_coors,&LA_perm);CHKERRQ(ierr);
308*1e07b27eSBarry Smith   }
309*1e07b27eSBarry Smith 
310*1e07b27eSBarry Smith   /* update local coords */
311*1e07b27eSBarry Smith   if (isActiveRank(psubcomm)) {
312*1e07b27eSBarry Smith     DM  _dmc;
313*1e07b27eSBarry Smith     Vec _coors,_coors_local;
314*1e07b27eSBarry Smith 
315*1e07b27eSBarry Smith     ierr = DMGetCoordinateDM(subdm,&_dmc);CHKERRQ(ierr);
316*1e07b27eSBarry Smith     ierr = DMGetCoordinates(subdm,&_coors);CHKERRQ(ierr);
317*1e07b27eSBarry Smith     ierr = DMGetCoordinatesLocal(subdm,&_coors_local);CHKERRQ(ierr);
318*1e07b27eSBarry Smith     ierr = DMGlobalToLocalBegin(_dmc,_coors,INSERT_VALUES,_coors_local);CHKERRQ(ierr);
319*1e07b27eSBarry Smith     ierr = DMGlobalToLocalEnd(_dmc,_coors,INSERT_VALUES,_coors_local);CHKERRQ(ierr);
320*1e07b27eSBarry Smith   }
321*1e07b27eSBarry Smith 
322*1e07b27eSBarry Smith   ierr = VecScatterDestroy(&sctx);CHKERRQ(ierr);
323*1e07b27eSBarry Smith   ierr = ISDestroy(&is_fine);CHKERRQ(ierr);
324*1e07b27eSBarry Smith   ierr = PetscFree(fine_indices);CHKERRQ(ierr);
325*1e07b27eSBarry Smith   ierr = ISDestroy(&is_local);CHKERRQ(ierr);
326*1e07b27eSBarry Smith   ierr = VecDestroy(&perm_coors);CHKERRQ(ierr);
327*1e07b27eSBarry Smith   ierr = VecDestroy(&coor_natural);CHKERRQ(ierr);
328*1e07b27eSBarry Smith   PetscFunctionReturn(0);
329*1e07b27eSBarry Smith }
330*1e07b27eSBarry Smith 
331*1e07b27eSBarry Smith #undef __FUNCT__
332*1e07b27eSBarry Smith #define __FUNCT__ "PCTelescopeSetUp_dmda_repart_coors"
333*1e07b27eSBarry Smith PetscErrorCode PCTelescopeSetUp_dmda_repart_coors(PC pc,PC_Telescope sred,PC_Telescope_DMDACtx *ctx)
334*1e07b27eSBarry Smith {
335*1e07b27eSBarry Smith   PetscInt       dim;
336*1e07b27eSBarry Smith   DM             dm,subdm;
337*1e07b27eSBarry Smith   PetscSubcomm   psubcomm;
338*1e07b27eSBarry Smith   MPI_Comm       comm;
339*1e07b27eSBarry Smith   Vec            coor;
340*1e07b27eSBarry Smith   PetscErrorCode ierr;
341*1e07b27eSBarry Smith 
342*1e07b27eSBarry Smith   PetscFunctionBegin;
343*1e07b27eSBarry Smith   ierr = PCGetDM(pc,&dm);CHKERRQ(ierr);
344*1e07b27eSBarry Smith   ierr = DMGetCoordinates(dm,&coor);CHKERRQ(ierr);
345*1e07b27eSBarry Smith   if (!coor) PetscFunctionReturn(0);
346*1e07b27eSBarry Smith   psubcomm = sred->psubcomm;
347*1e07b27eSBarry Smith   comm = PetscSubcommParent(psubcomm);
348*1e07b27eSBarry Smith   subdm = ctx->dmrepart;
349*1e07b27eSBarry Smith 
350*1e07b27eSBarry Smith 
351*1e07b27eSBarry Smith   ierr = PetscInfo(pc,"PCTelescope: setting up the coordinates (DMDA)\n");CHKERRQ(ierr);
352*1e07b27eSBarry Smith   ierr = DMDAGetInfo(dm,&dim,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
353*1e07b27eSBarry Smith   switch (dim) {
354*1e07b27eSBarry Smith   case 1: SETERRQ(comm,PETSC_ERR_SUP,"Telescope: DMDA (1D) repartitioning not provided");
355*1e07b27eSBarry Smith     break;
356*1e07b27eSBarry Smith   case 2: PCTelescopeSetUp_dmda_repart_coors2d(psubcomm,dm,subdm);
357*1e07b27eSBarry Smith     break;
358*1e07b27eSBarry Smith   case 3: PCTelescopeSetUp_dmda_repart_coors3d(psubcomm,dm,subdm);
359*1e07b27eSBarry Smith     break;
360*1e07b27eSBarry Smith   }
361*1e07b27eSBarry Smith   PetscFunctionReturn(0);
362*1e07b27eSBarry Smith }
363*1e07b27eSBarry Smith 
364*1e07b27eSBarry Smith /* setup repartitioned dm */
365*1e07b27eSBarry Smith #undef __FUNCT__
366*1e07b27eSBarry Smith #define __FUNCT__ "PCTelescopeSetUp_dmda_repart"
367*1e07b27eSBarry Smith PetscErrorCode PCTelescopeSetUp_dmda_repart(PC pc,PC_Telescope sred,PC_Telescope_DMDACtx *ctx)
368*1e07b27eSBarry Smith {
369*1e07b27eSBarry Smith   PetscErrorCode  ierr;
370*1e07b27eSBarry Smith   DM              dm;
371*1e07b27eSBarry Smith   PetscInt        dim,nx,ny,nz,ndof,nsw,sum,k;
372*1e07b27eSBarry Smith   DMBoundaryType  bx,by,bz;
373*1e07b27eSBarry Smith   DMDAStencilType stencil;
374*1e07b27eSBarry Smith   const PetscInt  *_range_i_re;
375*1e07b27eSBarry Smith   const PetscInt  *_range_j_re;
376*1e07b27eSBarry Smith   const PetscInt  *_range_k_re;
377*1e07b27eSBarry Smith   DMDAInterpolationType itype;
378*1e07b27eSBarry Smith   PetscInt              refine_x,refine_y,refine_z;
379*1e07b27eSBarry Smith   MPI_Comm              comm,subcomm;
380*1e07b27eSBarry Smith   const char            *prefix;
381*1e07b27eSBarry Smith 
382*1e07b27eSBarry Smith   PetscFunctionBegin;
383*1e07b27eSBarry Smith   comm = PetscSubcommParent(sred->psubcomm);
384*1e07b27eSBarry Smith   subcomm = PetscSubcommChild(sred->psubcomm);
385*1e07b27eSBarry Smith   ierr = PCGetDM(pc,&dm);CHKERRQ(ierr);
386*1e07b27eSBarry Smith 
387*1e07b27eSBarry Smith   ierr = DMDAGetInfo(dm,&dim,&nx,&ny,&nz,NULL,NULL,NULL,&ndof,&nsw,&bx,&by,&bz,&stencil);CHKERRQ(ierr);
388*1e07b27eSBarry Smith   ierr = DMDAGetInterpolationType(dm,&itype);CHKERRQ(ierr);
389*1e07b27eSBarry Smith   ierr = DMDAGetRefinementFactor(dm,&refine_x,&refine_y,&refine_z);CHKERRQ(ierr);
390*1e07b27eSBarry Smith 
391*1e07b27eSBarry Smith   ctx->dmrepart = NULL;
392*1e07b27eSBarry Smith   _range_i_re = _range_j_re = _range_k_re = NULL;
393*1e07b27eSBarry Smith   /* Create DMDA on the child communicator */
394*1e07b27eSBarry Smith   if (isActiveRank(sred->psubcomm)) {
395*1e07b27eSBarry Smith     switch (dim) {
396*1e07b27eSBarry Smith     case 1:
397*1e07b27eSBarry Smith       ierr = PetscInfo(pc,"PCTelescope: setting up the DMDA on comm subset (1D)\n");CHKERRQ(ierr);
398*1e07b27eSBarry Smith       /*ierr = DMDACreate1d(subcomm,bx,nx,ndof,nsw,NULL,&ctx->dmrepart);CHKERRQ(ierr);*/
399*1e07b27eSBarry Smith       ny = nz = 1;
400*1e07b27eSBarry Smith       by = bz = DM_BOUNDARY_NONE;
401*1e07b27eSBarry Smith       break;
402*1e07b27eSBarry Smith     case 2:
403*1e07b27eSBarry Smith       ierr = PetscInfo(pc,"PCTelescope: setting up the DMDA on comm subset (2D)\n");CHKERRQ(ierr);
404*1e07b27eSBarry Smith       /*ierr = DMDACreate2d(subcomm,bx,by,stencil,nx,ny, PETSC_DECIDE,PETSC_DECIDE, ndof,nsw, NULL,NULL,&ctx->dmrepart);CHKERRQ(ierr);*/
405*1e07b27eSBarry Smith       nz = 1;
406*1e07b27eSBarry Smith       bz = DM_BOUNDARY_NONE;
407*1e07b27eSBarry Smith       break;
408*1e07b27eSBarry Smith     case 3:
409*1e07b27eSBarry Smith       ierr = PetscInfo(pc,"PCTelescope: setting up the DMDA on comm subset (3D)\n");CHKERRQ(ierr);
410*1e07b27eSBarry Smith       /*ierr = DMDACreate3d(subcomm,bx,by,bz,stencil,nx,ny,nz, PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE, ndof,nsw, NULL,NULL,NULL,&ctx->dmrepart);CHKERRQ(ierr);*/
411*1e07b27eSBarry Smith       break;
412*1e07b27eSBarry Smith     }
413*1e07b27eSBarry Smith     /*
414*1e07b27eSBarry Smith      The API DMDACreate1d(), DMDACreate2d(), DMDACreate3d() does not allow us to set/append
415*1e07b27eSBarry Smith      a unique option prefix for the DM, thus I prefer to expose the contents of these API's here.
416*1e07b27eSBarry Smith      This allows users to control the partitioning of the subDM.
417*1e07b27eSBarry Smith     */
418*1e07b27eSBarry Smith     ierr = DMDACreate(subcomm,&ctx->dmrepart);CHKERRQ(ierr);
419*1e07b27eSBarry Smith     /* Set unique option prefix name */
420*1e07b27eSBarry Smith     ierr = DMGetOptionsPrefix(dm,&prefix);CHKERRQ(ierr);
421*1e07b27eSBarry Smith     ierr = DMSetOptionsPrefix(ctx->dmrepart,prefix);CHKERRQ(ierr);
422*1e07b27eSBarry Smith     ierr = DMAppendOptionsPrefix(ctx->dmrepart,"repart_");CHKERRQ(ierr);
423*1e07b27eSBarry Smith     /* standard setup from DMDACreate{1,2,3}d() */
424*1e07b27eSBarry Smith     ierr = DMSetDimension(ctx->dmrepart,dim);CHKERRQ(ierr);
425*1e07b27eSBarry Smith     ierr = DMDASetSizes(ctx->dmrepart,nx,ny,nz);CHKERRQ(ierr);
426*1e07b27eSBarry Smith     ierr = DMDASetNumProcs(ctx->dmrepart,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr);
427*1e07b27eSBarry Smith     ierr = DMDASetBoundaryType(ctx->dmrepart,bx,by,bz);CHKERRQ(ierr);
428*1e07b27eSBarry Smith     ierr = DMDASetDof(ctx->dmrepart,ndof);CHKERRQ(ierr);
429*1e07b27eSBarry Smith     ierr = DMDASetStencilType(ctx->dmrepart,stencil);CHKERRQ(ierr);
430*1e07b27eSBarry Smith     ierr = DMDASetStencilWidth(ctx->dmrepart,nsw);CHKERRQ(ierr);
431*1e07b27eSBarry Smith     ierr = DMDASetOwnershipRanges(ctx->dmrepart,NULL,NULL,NULL);CHKERRQ(ierr);
432*1e07b27eSBarry Smith     ierr = DMSetFromOptions(ctx->dmrepart);CHKERRQ(ierr);
433*1e07b27eSBarry Smith     ierr = DMSetUp(ctx->dmrepart);CHKERRQ(ierr);
434*1e07b27eSBarry Smith     /* Set refinement factors and interpolation type from the partent */
435*1e07b27eSBarry Smith     ierr = DMDASetRefinementFactor(ctx->dmrepart,refine_x,refine_y,refine_z);CHKERRQ(ierr);
436*1e07b27eSBarry Smith     ierr = DMDASetInterpolationType(ctx->dmrepart,itype);CHKERRQ(ierr);
437*1e07b27eSBarry Smith 
438*1e07b27eSBarry Smith     ierr = DMDAGetInfo(ctx->dmrepart,NULL,NULL,NULL,NULL,&ctx->Mp_re,&ctx->Np_re,&ctx->Pp_re,NULL,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
439*1e07b27eSBarry Smith     ierr = DMDAGetOwnershipRanges(ctx->dmrepart,&_range_i_re,&_range_j_re,&_range_k_re);CHKERRQ(ierr);
440*1e07b27eSBarry Smith   }
441*1e07b27eSBarry Smith 
442*1e07b27eSBarry Smith   /* generate ranges for repartitioned dm */
443*1e07b27eSBarry Smith   /* note - assume rank 0 always participates */
444*1e07b27eSBarry Smith   ierr = MPI_Bcast(&ctx->Mp_re,1,MPIU_INT,0,comm);CHKERRQ(ierr);
445*1e07b27eSBarry Smith   ierr = MPI_Bcast(&ctx->Np_re,1,MPIU_INT,0,comm);CHKERRQ(ierr);
446*1e07b27eSBarry Smith   ierr = MPI_Bcast(&ctx->Pp_re,1,MPIU_INT,0,comm);CHKERRQ(ierr);
447*1e07b27eSBarry Smith 
448*1e07b27eSBarry Smith   ierr = PetscMalloc(sizeof(PetscInt)*ctx->Mp_re,&ctx->range_i_re);CHKERRQ(ierr);
449*1e07b27eSBarry Smith   ierr = PetscMalloc(sizeof(PetscInt)*ctx->Np_re,&ctx->range_j_re);CHKERRQ(ierr);
450*1e07b27eSBarry Smith   ierr = PetscMalloc(sizeof(PetscInt)*ctx->Pp_re,&ctx->range_k_re);CHKERRQ(ierr);
451*1e07b27eSBarry Smith 
452*1e07b27eSBarry Smith   if (_range_i_re != NULL) {ierr = PetscMemcpy(ctx->range_i_re,_range_i_re,sizeof(PetscInt)*ctx->Mp_re);CHKERRQ(ierr);}
453*1e07b27eSBarry Smith   if (_range_j_re != NULL) {ierr = PetscMemcpy(ctx->range_j_re,_range_j_re,sizeof(PetscInt)*ctx->Np_re);CHKERRQ(ierr);}
454*1e07b27eSBarry Smith   if (_range_k_re != NULL) {ierr = PetscMemcpy(ctx->range_k_re,_range_k_re,sizeof(PetscInt)*ctx->Pp_re);CHKERRQ(ierr);}
455*1e07b27eSBarry Smith 
456*1e07b27eSBarry Smith   ierr = MPI_Bcast(ctx->range_i_re,ctx->Mp_re,MPIU_INT,0,comm);CHKERRQ(ierr);
457*1e07b27eSBarry Smith   ierr = MPI_Bcast(ctx->range_j_re,ctx->Np_re,MPIU_INT,0,comm);CHKERRQ(ierr);
458*1e07b27eSBarry Smith   ierr = MPI_Bcast(ctx->range_k_re,ctx->Pp_re,MPIU_INT,0,comm);CHKERRQ(ierr);
459*1e07b27eSBarry Smith 
460*1e07b27eSBarry Smith   ierr = PetscMalloc(sizeof(PetscInt)*ctx->Mp_re,&ctx->start_i_re);CHKERRQ(ierr);
461*1e07b27eSBarry Smith   ierr = PetscMalloc(sizeof(PetscInt)*ctx->Np_re,&ctx->start_j_re);CHKERRQ(ierr);
462*1e07b27eSBarry Smith   ierr = PetscMalloc(sizeof(PetscInt)*ctx->Pp_re,&ctx->start_k_re);CHKERRQ(ierr);
463*1e07b27eSBarry Smith 
464*1e07b27eSBarry Smith   sum = 0;
465*1e07b27eSBarry Smith   for (k=0; k<ctx->Mp_re; k++) {
466*1e07b27eSBarry Smith     ctx->start_i_re[k] = sum;
467*1e07b27eSBarry Smith     sum += ctx->range_i_re[k];
468*1e07b27eSBarry Smith   }
469*1e07b27eSBarry Smith 
470*1e07b27eSBarry Smith   sum = 0;
471*1e07b27eSBarry Smith   for (k=0; k<ctx->Np_re; k++) {
472*1e07b27eSBarry Smith     ctx->start_j_re[k] = sum;
473*1e07b27eSBarry Smith     sum += ctx->range_j_re[k];
474*1e07b27eSBarry Smith   }
475*1e07b27eSBarry Smith 
476*1e07b27eSBarry Smith   sum = 0;
477*1e07b27eSBarry Smith   for (k=0; k<ctx->Pp_re; k++) {
478*1e07b27eSBarry Smith     ctx->start_k_re[k] = sum;
479*1e07b27eSBarry Smith     sum += ctx->range_k_re[k];
480*1e07b27eSBarry Smith   }
481*1e07b27eSBarry Smith 
482*1e07b27eSBarry Smith   /* attach dm to ksp on sub communicator */
483*1e07b27eSBarry Smith   if (isActiveRank(sred->psubcomm)) {
484*1e07b27eSBarry Smith     ierr = KSPSetDM(sred->ksp,ctx->dmrepart);CHKERRQ(ierr);
485*1e07b27eSBarry Smith     ierr = KSPSetDMActive(sred->ksp,PETSC_FALSE);CHKERRQ(ierr);
486*1e07b27eSBarry Smith   }
487*1e07b27eSBarry Smith   PetscFunctionReturn(0);
488*1e07b27eSBarry Smith }
489*1e07b27eSBarry Smith 
490*1e07b27eSBarry Smith #undef __FUNCT__
491*1e07b27eSBarry Smith #define __FUNCT__ "PCTelescopeSetUp_dmda_permutation_3d"
492*1e07b27eSBarry Smith PetscErrorCode PCTelescopeSetUp_dmda_permutation_3d(PC pc,PC_Telescope sred,PC_Telescope_DMDACtx *ctx)
493*1e07b27eSBarry Smith {
494*1e07b27eSBarry Smith   PetscErrorCode ierr;
495*1e07b27eSBarry Smith   DM             dm;
496*1e07b27eSBarry Smith   MPI_Comm       comm;
497*1e07b27eSBarry Smith   Mat            Pscalar,P;
498*1e07b27eSBarry Smith   PetscInt       ndof;
499*1e07b27eSBarry Smith   PetscInt       i,j,k,location,startI[3],endI[3],lenI[3],nx,ny,nz;
500*1e07b27eSBarry Smith   PetscInt       sr,er,Mr;
501*1e07b27eSBarry Smith   Vec            V;
502*1e07b27eSBarry Smith 
503*1e07b27eSBarry Smith   PetscFunctionBegin;
504*1e07b27eSBarry Smith   ierr = PetscInfo(pc,"PCTelescope: setting up the permutation matrix (DMDA-3D)\n");CHKERRQ(ierr);
505*1e07b27eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr);
506*1e07b27eSBarry Smith 
507*1e07b27eSBarry Smith   ierr = PCGetDM(pc,&dm);CHKERRQ(ierr);
508*1e07b27eSBarry Smith   ierr = DMDAGetInfo(dm,NULL,&nx,&ny,&nz,NULL,NULL,NULL,&ndof,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
509*1e07b27eSBarry Smith 
510*1e07b27eSBarry Smith   ierr = DMGetGlobalVector(dm,&V);CHKERRQ(ierr);
511*1e07b27eSBarry Smith   ierr = VecGetSize(V,&Mr);CHKERRQ(ierr);
512*1e07b27eSBarry Smith   ierr = VecGetOwnershipRange(V,&sr,&er);CHKERRQ(ierr);
513*1e07b27eSBarry Smith   ierr = DMRestoreGlobalVector(dm,&V);CHKERRQ(ierr);
514*1e07b27eSBarry Smith   sr = sr / ndof;
515*1e07b27eSBarry Smith   er = er / ndof;
516*1e07b27eSBarry Smith   Mr = Mr / ndof;
517*1e07b27eSBarry Smith 
518*1e07b27eSBarry Smith   ierr = MatCreate(comm,&Pscalar);CHKERRQ(ierr);
519*1e07b27eSBarry Smith   ierr = MatSetSizes(Pscalar,(er-sr),(er-sr),Mr,Mr);CHKERRQ(ierr);
520*1e07b27eSBarry Smith   ierr = MatSetType(Pscalar,MATAIJ);CHKERRQ(ierr);
521*1e07b27eSBarry Smith   ierr = MatSeqAIJSetPreallocation(Pscalar,2,NULL);CHKERRQ(ierr);
522*1e07b27eSBarry Smith   ierr = MatMPIAIJSetPreallocation(Pscalar,2,NULL,2,NULL);CHKERRQ(ierr);
523*1e07b27eSBarry Smith 
524*1e07b27eSBarry Smith   ierr = DMDAGetCorners(dm,NULL,NULL,NULL,&lenI[0],&lenI[1],&lenI[2]);CHKERRQ(ierr);
525*1e07b27eSBarry Smith   ierr = DMDAGetCorners(dm,&startI[0],&startI[1],&startI[2],&endI[0],&endI[1],&endI[2]);CHKERRQ(ierr);
526*1e07b27eSBarry Smith   endI[0] += startI[0];
527*1e07b27eSBarry Smith   endI[1] += startI[1];
528*1e07b27eSBarry Smith   endI[2] += startI[2];
529*1e07b27eSBarry Smith 
530*1e07b27eSBarry Smith   for (k=startI[2]; k<endI[2]; k++) {
531*1e07b27eSBarry Smith     for (j=startI[1]; j<endI[1]; j++) {
532*1e07b27eSBarry Smith       for (i=startI[0]; i<endI[0]; i++) {
533*1e07b27eSBarry Smith         PetscMPIInt rank_ijk_re,rank_reI[3];
534*1e07b27eSBarry Smith         PetscInt    s0_re;
535*1e07b27eSBarry Smith         PetscInt    ii,jj,kk,local_ijk_re,mapped_ijk,natural_ijk;
536*1e07b27eSBarry Smith         PetscInt    lenI_re[3];
537*1e07b27eSBarry Smith 
538*1e07b27eSBarry Smith         location = (i - startI[0]) + (j - startI[1])*lenI[0] + (k - startI[2])*lenI[0]*lenI[1];
539*1e07b27eSBarry Smith         ierr = _DMDADetermineRankFromGlobalIJK(3,i,j,k,   ctx->Mp_re,ctx->Np_re,ctx->Pp_re,
540*1e07b27eSBarry Smith                                                ctx->start_i_re,ctx->start_j_re,ctx->start_k_re,
541*1e07b27eSBarry Smith                                                ctx->range_i_re,ctx->range_j_re,ctx->range_k_re,
542*1e07b27eSBarry Smith                                                &rank_reI[0],&rank_reI[1],&rank_reI[2],&rank_ijk_re);CHKERRQ(ierr);
543*1e07b27eSBarry Smith         ierr = _DMDADetermineGlobalS0(3,rank_ijk_re, ctx->Mp_re,ctx->Np_re,ctx->Pp_re, ctx->range_i_re,ctx->range_j_re,ctx->range_k_re, &s0_re);CHKERRQ(ierr);
544*1e07b27eSBarry Smith         ii = i - ctx->start_i_re[ rank_reI[0] ];
545*1e07b27eSBarry Smith         if (ii < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"[dmdarepart-perm3d] index error ii");
546*1e07b27eSBarry Smith         jj = j - ctx->start_j_re[ rank_reI[1] ];
547*1e07b27eSBarry Smith         if (jj < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"[dmdarepart-perm3d] index error jj");
548*1e07b27eSBarry Smith         kk = k - ctx->start_k_re[ rank_reI[2] ];
549*1e07b27eSBarry Smith         if (kk < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"[dmdarepart-perm3d] index error kk");
550*1e07b27eSBarry Smith         lenI_re[0] = ctx->range_i_re[ rank_reI[0] ];
551*1e07b27eSBarry Smith         lenI_re[1] = ctx->range_j_re[ rank_reI[1] ];
552*1e07b27eSBarry Smith         lenI_re[2] = ctx->range_k_re[ rank_reI[2] ];
553*1e07b27eSBarry Smith         local_ijk_re = ii + jj * lenI_re[0] + kk * lenI_re[0] * lenI_re[1];
554*1e07b27eSBarry Smith         mapped_ijk = s0_re + local_ijk_re;
555*1e07b27eSBarry Smith         natural_ijk = i + j*nx + k*nx*ny;
556*1e07b27eSBarry Smith         ierr = MatSetValue(Pscalar,sr+location,mapped_ijk,1.0,INSERT_VALUES);CHKERRQ(ierr);
557*1e07b27eSBarry Smith       }
558*1e07b27eSBarry Smith     }
559*1e07b27eSBarry Smith   }
560*1e07b27eSBarry Smith   ierr = MatAssemblyBegin(Pscalar,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
561*1e07b27eSBarry Smith   ierr = MatAssemblyEnd(Pscalar,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
562*1e07b27eSBarry Smith   ierr = MatCreateMAIJ(Pscalar,ndof,&P);CHKERRQ(ierr);
563*1e07b27eSBarry Smith   ierr = MatDestroy(&Pscalar);CHKERRQ(ierr);
564*1e07b27eSBarry Smith   ctx->permutation = P;
565*1e07b27eSBarry Smith   PetscFunctionReturn(0);
566*1e07b27eSBarry Smith }
567*1e07b27eSBarry Smith 
568*1e07b27eSBarry Smith #undef __FUNCT__
569*1e07b27eSBarry Smith #define __FUNCT__ "PCTelescopeSetUp_dmda_permutation_2d"
570*1e07b27eSBarry Smith PetscErrorCode PCTelescopeSetUp_dmda_permutation_2d(PC pc,PC_Telescope sred,PC_Telescope_DMDACtx *ctx)
571*1e07b27eSBarry Smith {
572*1e07b27eSBarry Smith   PetscErrorCode ierr;
573*1e07b27eSBarry Smith   DM             dm;
574*1e07b27eSBarry Smith   MPI_Comm       comm;
575*1e07b27eSBarry Smith   Mat            Pscalar,P;
576*1e07b27eSBarry Smith   PetscInt       ndof;
577*1e07b27eSBarry Smith   PetscInt       i,j,location,startI[2],endI[2],lenI[2],nx,ny,nz;
578*1e07b27eSBarry Smith   PetscInt       sr,er,Mr;
579*1e07b27eSBarry Smith   Vec            V;
580*1e07b27eSBarry Smith 
581*1e07b27eSBarry Smith   PetscFunctionBegin;
582*1e07b27eSBarry Smith   ierr = PetscInfo(pc,"PCTelescope: setting up the permutation matrix (DMDA-2D)\n");CHKERRQ(ierr);
583*1e07b27eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr);
584*1e07b27eSBarry Smith   ierr = PCGetDM(pc,&dm);CHKERRQ(ierr);
585*1e07b27eSBarry Smith   ierr = DMDAGetInfo(dm,NULL,&nx,&ny,&nz,NULL,NULL,NULL,&ndof,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
586*1e07b27eSBarry Smith   ierr = DMGetGlobalVector(dm,&V);CHKERRQ(ierr);
587*1e07b27eSBarry Smith   ierr = VecGetSize(V,&Mr);CHKERRQ(ierr);
588*1e07b27eSBarry Smith   ierr = VecGetOwnershipRange(V,&sr,&er);CHKERRQ(ierr);
589*1e07b27eSBarry Smith   ierr = DMRestoreGlobalVector(dm,&V);CHKERRQ(ierr);
590*1e07b27eSBarry Smith   sr = sr / ndof;
591*1e07b27eSBarry Smith   er = er / ndof;
592*1e07b27eSBarry Smith   Mr = Mr / ndof;
593*1e07b27eSBarry Smith 
594*1e07b27eSBarry Smith   ierr = MatCreate(comm,&Pscalar);CHKERRQ(ierr);
595*1e07b27eSBarry Smith   ierr = MatSetSizes(Pscalar,(er-sr),(er-sr),Mr,Mr);CHKERRQ(ierr);
596*1e07b27eSBarry Smith   ierr = MatSetType(Pscalar,MATAIJ);CHKERRQ(ierr);
597*1e07b27eSBarry Smith   ierr = MatSeqAIJSetPreallocation(Pscalar,2,NULL);CHKERRQ(ierr);
598*1e07b27eSBarry Smith   ierr = MatMPIAIJSetPreallocation(Pscalar,2,NULL,2,NULL);CHKERRQ(ierr);
599*1e07b27eSBarry Smith 
600*1e07b27eSBarry Smith   ierr = DMDAGetCorners(dm,NULL,NULL,NULL,&lenI[0],&lenI[1],NULL);CHKERRQ(ierr);
601*1e07b27eSBarry Smith   ierr = DMDAGetCorners(dm,&startI[0],&startI[1],NULL,&endI[0],&endI[1],NULL);CHKERRQ(ierr);
602*1e07b27eSBarry Smith   endI[0] += startI[0];
603*1e07b27eSBarry Smith   endI[1] += startI[1];
604*1e07b27eSBarry Smith 
605*1e07b27eSBarry Smith   for (j=startI[1]; j<endI[1]; j++) {
606*1e07b27eSBarry Smith     for (i=startI[0]; i<endI[0]; i++) {
607*1e07b27eSBarry Smith       PetscMPIInt rank_ijk_re,rank_reI[3];
608*1e07b27eSBarry Smith       PetscInt    s0_re;
609*1e07b27eSBarry Smith       PetscInt    ii,jj,local_ijk_re,mapped_ijk,natural_ijk;
610*1e07b27eSBarry Smith       PetscInt    lenI_re[3];
611*1e07b27eSBarry Smith 
612*1e07b27eSBarry Smith       location = (i - startI[0]) + (j - startI[1])*lenI[0];
613*1e07b27eSBarry Smith       ierr = _DMDADetermineRankFromGlobalIJK(2,i,j,0,   ctx->Mp_re,ctx->Np_re,ctx->Pp_re,
614*1e07b27eSBarry Smith                                              ctx->start_i_re,ctx->start_j_re,ctx->start_k_re,
615*1e07b27eSBarry Smith                                              ctx->range_i_re,ctx->range_j_re,ctx->range_k_re,
616*1e07b27eSBarry Smith                                              &rank_reI[0],&rank_reI[1],NULL,&rank_ijk_re);CHKERRQ(ierr);
617*1e07b27eSBarry Smith 
618*1e07b27eSBarry Smith       ierr = _DMDADetermineGlobalS0(2,rank_ijk_re, ctx->Mp_re,ctx->Np_re,ctx->Pp_re, ctx->range_i_re,ctx->range_j_re,ctx->range_k_re, &s0_re);CHKERRQ(ierr);
619*1e07b27eSBarry Smith 
620*1e07b27eSBarry Smith       ii = i - ctx->start_i_re[ rank_reI[0] ];
621*1e07b27eSBarry Smith       if (ii < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"[dmdarepart-perm2d] index error ii");
622*1e07b27eSBarry Smith       jj = j - ctx->start_j_re[ rank_reI[1] ];
623*1e07b27eSBarry Smith       if (jj < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"[dmdarepart-perm2d] index error jj");
624*1e07b27eSBarry Smith 
625*1e07b27eSBarry Smith       lenI_re[0] = ctx->range_i_re[ rank_reI[0] ];
626*1e07b27eSBarry Smith       lenI_re[1] = ctx->range_j_re[ rank_reI[1] ];
627*1e07b27eSBarry Smith       local_ijk_re = ii + jj * lenI_re[0];
628*1e07b27eSBarry Smith       mapped_ijk = s0_re + local_ijk_re;
629*1e07b27eSBarry Smith       natural_ijk = i + j*nx;
630*1e07b27eSBarry Smith       ierr = MatSetValue(Pscalar,sr+location,mapped_ijk,1.0,INSERT_VALUES);CHKERRQ(ierr);
631*1e07b27eSBarry Smith     }
632*1e07b27eSBarry Smith   }
633*1e07b27eSBarry Smith   ierr = MatAssemblyBegin(Pscalar,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
634*1e07b27eSBarry Smith   ierr = MatAssemblyEnd(Pscalar,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
635*1e07b27eSBarry Smith   ierr = MatCreateMAIJ(Pscalar,ndof,&P);CHKERRQ(ierr);
636*1e07b27eSBarry Smith   ierr = MatDestroy(&Pscalar);CHKERRQ(ierr);
637*1e07b27eSBarry Smith   ctx->permutation = P;
638*1e07b27eSBarry Smith   PetscFunctionReturn(0);
639*1e07b27eSBarry Smith }
640*1e07b27eSBarry Smith 
641*1e07b27eSBarry Smith #undef __FUNCT__
642*1e07b27eSBarry Smith #define __FUNCT__ "PCTelescopeSetUp_dmda_scatters"
643*1e07b27eSBarry Smith PetscErrorCode PCTelescopeSetUp_dmda_scatters(PC pc,PC_Telescope sred,PC_Telescope_DMDACtx *ctx)
644*1e07b27eSBarry Smith {
645*1e07b27eSBarry Smith   PetscErrorCode ierr;
646*1e07b27eSBarry Smith   Vec            xred,yred,xtmp,x,xp;
647*1e07b27eSBarry Smith   VecScatter     scatter;
648*1e07b27eSBarry Smith   IS             isin;
649*1e07b27eSBarry Smith   Mat            B;
650*1e07b27eSBarry Smith   PetscInt       m,bs,st,ed;
651*1e07b27eSBarry Smith   MPI_Comm       comm;
652*1e07b27eSBarry Smith 
653*1e07b27eSBarry Smith   PetscFunctionBegin;
654*1e07b27eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr);
655*1e07b27eSBarry Smith   ierr = PCGetOperators(pc,NULL,&B);CHKERRQ(ierr);
656*1e07b27eSBarry Smith   ierr = MatCreateVecs(B,&x,NULL);CHKERRQ(ierr);
657*1e07b27eSBarry Smith   ierr = MatGetBlockSize(B,&bs);CHKERRQ(ierr);
658*1e07b27eSBarry Smith   ierr = VecDuplicate(x,&xp);CHKERRQ(ierr);
659*1e07b27eSBarry Smith   m = bs;
660*1e07b27eSBarry Smith   xred = NULL;
661*1e07b27eSBarry Smith   yred = NULL;
662*1e07b27eSBarry Smith   if (isActiveRank(sred->psubcomm)) {
663*1e07b27eSBarry Smith     ierr = DMCreateGlobalVector(ctx->dmrepart,&xred);CHKERRQ(ierr);
664*1e07b27eSBarry Smith     ierr = VecDuplicate(xred,&yred);CHKERRQ(ierr);
665*1e07b27eSBarry Smith     ierr = VecGetOwnershipRange(xred,&st,&ed);CHKERRQ(ierr);
666*1e07b27eSBarry Smith     ierr = ISCreateStride(comm,ed-st,st,1,&isin);CHKERRQ(ierr);
667*1e07b27eSBarry Smith     ierr = VecGetLocalSize(xred,&m);
668*1e07b27eSBarry Smith   } else {
669*1e07b27eSBarry Smith     /* fetch some local owned data - just to deal with avoiding zero length ownership on range */
670*1e07b27eSBarry Smith     ierr = VecGetOwnershipRange(x,&st,&ed);CHKERRQ(ierr);
671*1e07b27eSBarry Smith     ierr = ISCreateStride(comm,bs,st,1,&isin);CHKERRQ(ierr);
672*1e07b27eSBarry Smith   }
673*1e07b27eSBarry Smith   ierr = ISSetBlockSize(isin,bs);CHKERRQ(ierr);
674*1e07b27eSBarry Smith   ierr = VecCreate(comm,&xtmp);CHKERRQ(ierr);
675*1e07b27eSBarry Smith   ierr = VecSetSizes(xtmp,m,PETSC_DECIDE);CHKERRQ(ierr);
676*1e07b27eSBarry Smith   ierr = VecSetBlockSize(xtmp,bs);CHKERRQ(ierr);
677*1e07b27eSBarry Smith   ierr = VecSetType(xtmp,((PetscObject)x)->type_name);CHKERRQ(ierr);
678*1e07b27eSBarry Smith   ierr = VecScatterCreate(x,isin,xtmp,NULL,&scatter);CHKERRQ(ierr);
679*1e07b27eSBarry Smith   sred->xred    = xred;
680*1e07b27eSBarry Smith   sred->yred    = yred;
681*1e07b27eSBarry Smith   sred->isin    = isin;
682*1e07b27eSBarry Smith   sred->scatter = scatter;
683*1e07b27eSBarry Smith   sred->xtmp    = xtmp;
684*1e07b27eSBarry Smith 
685*1e07b27eSBarry Smith   ctx->xp       = xp;
686*1e07b27eSBarry Smith   ierr = VecDestroy(&x);CHKERRQ(ierr);
687*1e07b27eSBarry Smith   PetscFunctionReturn(0);
688*1e07b27eSBarry Smith }
689*1e07b27eSBarry Smith 
690*1e07b27eSBarry Smith #undef __FUNCT__
691*1e07b27eSBarry Smith #define __FUNCT__ "PCTelescopeSetUp_dmda"
692*1e07b27eSBarry Smith PetscErrorCode PCTelescopeSetUp_dmda(PC pc,PC_Telescope sred)
693*1e07b27eSBarry Smith {
694*1e07b27eSBarry Smith   PC_Telescope_DMDACtx *ctx;
695*1e07b27eSBarry Smith   PetscInt                 dim;
696*1e07b27eSBarry Smith   DM                       dm;
697*1e07b27eSBarry Smith   MPI_Comm                 comm;
698*1e07b27eSBarry Smith   PetscErrorCode           ierr;
699*1e07b27eSBarry Smith 
700*1e07b27eSBarry Smith   PetscFunctionBegin;
701*1e07b27eSBarry Smith   ierr = PetscInfo(pc,"PCTelescope: setup (DMDA)\n");CHKERRQ(ierr);
702*1e07b27eSBarry Smith   PetscMalloc(sizeof(PC_Telescope_DMDACtx),&ctx);
703*1e07b27eSBarry Smith   PetscMemzero(ctx,sizeof(PC_Telescope_DMDACtx));
704*1e07b27eSBarry Smith   sred->dm_ctx = (void*)ctx;
705*1e07b27eSBarry Smith 
706*1e07b27eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr);
707*1e07b27eSBarry Smith   ierr = PCGetDM(pc,&dm);CHKERRQ(ierr);
708*1e07b27eSBarry Smith   ierr = DMDAGetInfo(dm,&dim,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
709*1e07b27eSBarry Smith 
710*1e07b27eSBarry Smith   PCTelescopeSetUp_dmda_repart(pc,sred,ctx);
711*1e07b27eSBarry Smith   PCTelescopeSetUp_dmda_repart_coors(pc,sred,ctx);
712*1e07b27eSBarry Smith   switch (dim) {
713*1e07b27eSBarry Smith   case 1: SETERRQ(comm,PETSC_ERR_SUP,"Telescope: DMDA (1D) repartitioning not provided");
714*1e07b27eSBarry Smith     break;
715*1e07b27eSBarry Smith   case 2: ierr = PCTelescopeSetUp_dmda_permutation_2d(pc,sred,ctx);CHKERRQ(ierr);
716*1e07b27eSBarry Smith     break;
717*1e07b27eSBarry Smith   case 3: ierr = PCTelescopeSetUp_dmda_permutation_3d(pc,sred,ctx);CHKERRQ(ierr);
718*1e07b27eSBarry Smith     break;
719*1e07b27eSBarry Smith   }
720*1e07b27eSBarry Smith   ierr = PCTelescopeSetUp_dmda_scatters(pc,sred,ctx);CHKERRQ(ierr);
721*1e07b27eSBarry Smith   PetscFunctionReturn(0);
722*1e07b27eSBarry Smith }
723*1e07b27eSBarry Smith 
724*1e07b27eSBarry Smith #undef __FUNCT__
725*1e07b27eSBarry Smith #define __FUNCT__ "PCTelescopeMatCreate_dmda"
726*1e07b27eSBarry Smith PetscErrorCode PCTelescopeMatCreate_dmda(PC pc,PC_Telescope sred,MatReuse reuse,Mat *A)
727*1e07b27eSBarry Smith {
728*1e07b27eSBarry Smith   PetscErrorCode ierr;
729*1e07b27eSBarry Smith   PC_Telescope_DMDACtx *ctx;
730*1e07b27eSBarry Smith   MPI_Comm       comm,subcomm;
731*1e07b27eSBarry Smith   Mat            Bperm,Bred,B,P;
732*1e07b27eSBarry Smith   PetscInt       nr,nc;
733*1e07b27eSBarry Smith   IS             isrow,iscol;
734*1e07b27eSBarry Smith   Mat            Blocal,*_Blocal;
735*1e07b27eSBarry Smith 
736*1e07b27eSBarry Smith   PetscFunctionBegin;
737*1e07b27eSBarry Smith   ierr = PetscInfo(pc,"PCTelescope: updating the redundant preconditioned operator (DMDA)\n");CHKERRQ(ierr);
738*1e07b27eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr);
739*1e07b27eSBarry Smith     subcomm = PetscSubcommChild(sred->psubcomm);
740*1e07b27eSBarry Smith   ctx = (PC_Telescope_DMDACtx*)sred->dm_ctx;
741*1e07b27eSBarry Smith 
742*1e07b27eSBarry Smith   ierr = PCGetOperators(pc,NULL,&B);CHKERRQ(ierr);
743*1e07b27eSBarry Smith   ierr = MatGetSize(B,&nr,&nc);CHKERRQ(ierr);
744*1e07b27eSBarry Smith 
745*1e07b27eSBarry Smith   P = ctx->permutation;
746*1e07b27eSBarry Smith   ierr = MatPtAP(B,P,MAT_INITIAL_MATRIX,1.1,&Bperm);CHKERRQ(ierr);
747*1e07b27eSBarry Smith 
748*1e07b27eSBarry Smith   /* Get submatrices */
749*1e07b27eSBarry Smith   isrow = sred->isin;
750*1e07b27eSBarry Smith   ierr = ISCreateStride(comm,nc,0,1,&iscol);CHKERRQ(ierr);
751*1e07b27eSBarry Smith 
752*1e07b27eSBarry Smith   ierr = MatGetSubMatrices(Bperm,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&_Blocal);CHKERRQ(ierr);
753*1e07b27eSBarry Smith   Blocal = *_Blocal;
754*1e07b27eSBarry Smith   Bred = NULL;
755*1e07b27eSBarry Smith   if (isActiveRank(sred->psubcomm)) {
756*1e07b27eSBarry Smith     PetscInt mm;
757*1e07b27eSBarry Smith 
758*1e07b27eSBarry Smith     if (reuse != MAT_INITIAL_MATRIX) {Bred = *A;}
759*1e07b27eSBarry Smith     ierr = MatGetSize(Blocal,&mm,NULL);CHKERRQ(ierr);
760*1e07b27eSBarry Smith     //ierr = MatCreateMPIMatConcatenateSeqMat(subcomm,Blocal,PETSC_DECIDE,reuse,&Bred);CHKERRQ(ierr);
761*1e07b27eSBarry Smith     ierr = MatCreateMPIMatConcatenateSeqMat(subcomm,Blocal,mm,reuse,&Bred);CHKERRQ(ierr);
762*1e07b27eSBarry Smith   }
763*1e07b27eSBarry Smith   *A = Bred;
764*1e07b27eSBarry Smith 
765*1e07b27eSBarry Smith   ierr = ISDestroy(&iscol);CHKERRQ(ierr);
766*1e07b27eSBarry Smith   ierr = MatDestroy(&Bperm);CHKERRQ(ierr);
767*1e07b27eSBarry Smith   ierr = MatDestroyMatrices(1,&_Blocal);CHKERRQ(ierr);
768*1e07b27eSBarry Smith   PetscFunctionReturn(0);
769*1e07b27eSBarry Smith }
770*1e07b27eSBarry Smith 
771*1e07b27eSBarry Smith #undef __FUNCT__
772*1e07b27eSBarry Smith #define __FUNCT__ "PCTelescopeMatNullSpaceCreate_dmda"
773*1e07b27eSBarry Smith PetscErrorCode PCTelescopeMatNullSpaceCreate_dmda(PC pc,PC_Telescope sred,Mat sub_mat)
774*1e07b27eSBarry Smith {
775*1e07b27eSBarry Smith   PetscErrorCode   ierr;
776*1e07b27eSBarry Smith   MatNullSpace     nullspace,sub_nullspace;
777*1e07b27eSBarry Smith   Mat              A,B;
778*1e07b27eSBarry Smith   PetscBool        has_const;
779*1e07b27eSBarry Smith   PetscInt         i,k,n;
780*1e07b27eSBarry Smith   const Vec        *vecs;
781*1e07b27eSBarry Smith   Vec              *sub_vecs;
782*1e07b27eSBarry Smith   MPI_Comm         subcomm;
783*1e07b27eSBarry Smith   PC_Telescope_DMDACtx *ctx;
784*1e07b27eSBarry Smith 
785*1e07b27eSBarry Smith   PetscFunctionBegin;
786*1e07b27eSBarry Smith   ierr = PCGetOperators(pc,&A,&B);CHKERRQ(ierr);
787*1e07b27eSBarry Smith   ierr = MatGetNullSpace(B,&nullspace);CHKERRQ(ierr);
788*1e07b27eSBarry Smith   if (!nullspace) return(0);
789*1e07b27eSBarry Smith 
790*1e07b27eSBarry Smith   ierr = PetscInfo(pc,"PCTelescope: generating nullspace (DMDA)\n");CHKERRQ(ierr);
791*1e07b27eSBarry Smith   ctx = (PC_Telescope_DMDACtx*)sred->dm_ctx;
792*1e07b27eSBarry Smith   subcomm = PetscSubcommChild(sred->psubcomm);
793*1e07b27eSBarry Smith   ierr = MatNullSpaceGetVecs(nullspace,&has_const,&n,&vecs);CHKERRQ(ierr);
794*1e07b27eSBarry Smith 
795*1e07b27eSBarry Smith   if (isActiveRank(sred->psubcomm)) {
796*1e07b27eSBarry Smith     sub_vecs = NULL;
797*1e07b27eSBarry Smith     /* create new vectors */
798*1e07b27eSBarry Smith     if (n != 0) {
799*1e07b27eSBarry Smith       PetscMalloc(sizeof(Vec)*n,&sub_vecs);
800*1e07b27eSBarry Smith       for (k=0; k<n; k++) {
801*1e07b27eSBarry Smith         ierr = VecDuplicate(sred->xred,&sub_vecs[k]);CHKERRQ(ierr);
802*1e07b27eSBarry Smith       }
803*1e07b27eSBarry Smith     }
804*1e07b27eSBarry Smith   }
805*1e07b27eSBarry Smith 
806*1e07b27eSBarry Smith   /* copy entries */
807*1e07b27eSBarry Smith   for (k=0; k<n; k++) {
808*1e07b27eSBarry Smith     const PetscScalar *x_array;
809*1e07b27eSBarry Smith     PetscScalar       *LA_sub_vec;
810*1e07b27eSBarry Smith     PetscInt          st,ed,bs;
811*1e07b27eSBarry Smith 
812*1e07b27eSBarry Smith     /* permute vector into ordering associated with re-partitioned dmda */
813*1e07b27eSBarry Smith     ierr = MatMultTranspose(ctx->permutation,vecs[k],ctx->xp);CHKERRQ(ierr);
814*1e07b27eSBarry Smith 
815*1e07b27eSBarry Smith     /* pull in vector x->xtmp */
816*1e07b27eSBarry Smith     ierr = VecScatterBegin(sred->scatter,ctx->xp,sred->xtmp,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
817*1e07b27eSBarry Smith     ierr = VecScatterEnd(sred->scatter,ctx->xp,sred->xtmp,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
818*1e07b27eSBarry Smith 
819*1e07b27eSBarry Smith     /* copy vector entires into xred */
820*1e07b27eSBarry Smith     ierr = VecGetBlockSize(sred->xtmp,&bs);CHKERRQ(ierr);
821*1e07b27eSBarry Smith     ierr = VecGetArrayRead(sred->xtmp,&x_array);CHKERRQ(ierr);
822*1e07b27eSBarry Smith     if (sub_vecs[k]) {
823*1e07b27eSBarry Smith       ierr = VecGetOwnershipRange(sub_vecs[k],&st,&ed);CHKERRQ(ierr);
824*1e07b27eSBarry Smith       ierr = VecGetArray(sub_vecs[k],&LA_sub_vec);CHKERRQ(ierr);
825*1e07b27eSBarry Smith       for (i=0; i<ed-st; i++) {
826*1e07b27eSBarry Smith         LA_sub_vec[i] = x_array[i];
827*1e07b27eSBarry Smith       }
828*1e07b27eSBarry Smith       ierr = VecRestoreArray(sub_vecs[k],&LA_sub_vec);CHKERRQ(ierr);
829*1e07b27eSBarry Smith     }
830*1e07b27eSBarry Smith     ierr = VecRestoreArrayRead(sred->xtmp,&x_array);CHKERRQ(ierr);
831*1e07b27eSBarry Smith   }
832*1e07b27eSBarry Smith 
833*1e07b27eSBarry Smith   if (isActiveRank(sred->psubcomm)) {
834*1e07b27eSBarry Smith     /* create new nullspace for redundant object */
835*1e07b27eSBarry Smith     ierr = MatNullSpaceCreate(subcomm,has_const,n,sub_vecs,&sub_nullspace);CHKERRQ(ierr);
836*1e07b27eSBarry Smith 
837*1e07b27eSBarry Smith     /* attach redundant nullspace to Bred */
838*1e07b27eSBarry Smith     ierr = MatSetNullSpace(sub_mat,sub_nullspace);CHKERRQ(ierr);
839*1e07b27eSBarry Smith 
840*1e07b27eSBarry Smith     for (k=0; k<n; k++) {
841*1e07b27eSBarry Smith       ierr = VecDestroy(&sub_vecs[k]);CHKERRQ(ierr);
842*1e07b27eSBarry Smith     }
843*1e07b27eSBarry Smith     ierr = PetscFree(sub_vecs);CHKERRQ(ierr);
844*1e07b27eSBarry Smith   }
845*1e07b27eSBarry Smith   PetscFunctionReturn(0);
846*1e07b27eSBarry Smith }
847*1e07b27eSBarry Smith 
848*1e07b27eSBarry Smith #undef __FUNCT__
849*1e07b27eSBarry Smith #define __FUNCT__ "PCApply_Telescope_dmda"
850*1e07b27eSBarry Smith PetscErrorCode PCApply_Telescope_dmda(PC pc,Vec x,Vec y)
851*1e07b27eSBarry Smith {
852*1e07b27eSBarry Smith   PC_Telescope      sred = (PC_Telescope)pc->data;
853*1e07b27eSBarry Smith   PetscErrorCode    ierr;
854*1e07b27eSBarry Smith   Mat               perm;
855*1e07b27eSBarry Smith   Vec               xtmp,xp,xred,yred;
856*1e07b27eSBarry Smith   PetscInt          i,st,ed,bs;
857*1e07b27eSBarry Smith   VecScatter        scatter;
858*1e07b27eSBarry Smith   PetscScalar       *array;
859*1e07b27eSBarry Smith   const PetscScalar *x_array;
860*1e07b27eSBarry Smith   PC_Telescope_DMDACtx *ctx;
861*1e07b27eSBarry Smith 
862*1e07b27eSBarry Smith   ctx = (PC_Telescope_DMDACtx*)sred->dm_ctx;
863*1e07b27eSBarry Smith   xtmp    = sred->xtmp;
864*1e07b27eSBarry Smith   scatter = sred->scatter;
865*1e07b27eSBarry Smith   xred    = sred->xred;
866*1e07b27eSBarry Smith   yred    = sred->yred;
867*1e07b27eSBarry Smith   perm  = ctx->permutation;
868*1e07b27eSBarry Smith   xp    = ctx->xp;
869*1e07b27eSBarry Smith 
870*1e07b27eSBarry Smith   PetscFunctionBegin;
871*1e07b27eSBarry Smith   /* permute vector into ordering associated with re-partitioned dmda */
872*1e07b27eSBarry Smith   ierr = MatMultTranspose(perm,x,xp);CHKERRQ(ierr);
873*1e07b27eSBarry Smith 
874*1e07b27eSBarry Smith   /* pull in vector x->xtmp */
875*1e07b27eSBarry Smith   ierr = VecScatterBegin(scatter,xp,xtmp,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
876*1e07b27eSBarry Smith   ierr = VecScatterEnd(scatter,xp,xtmp,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
877*1e07b27eSBarry Smith 
878*1e07b27eSBarry Smith   /* copy vector entires into xred */
879*1e07b27eSBarry Smith   ierr = VecGetBlockSize(xtmp,&bs);CHKERRQ(ierr);
880*1e07b27eSBarry Smith   ierr = VecGetArrayRead(xtmp,&x_array);CHKERRQ(ierr);
881*1e07b27eSBarry Smith   if (xred) {
882*1e07b27eSBarry Smith     PetscScalar *LA_xred;
883*1e07b27eSBarry Smith     ierr = VecGetOwnershipRange(xred,&st,&ed);CHKERRQ(ierr);
884*1e07b27eSBarry Smith 
885*1e07b27eSBarry Smith     ierr = VecGetArray(xred,&LA_xred);CHKERRQ(ierr);
886*1e07b27eSBarry Smith     for (i=0; i<ed-st; i++) {
887*1e07b27eSBarry Smith       LA_xred[i] = x_array[i];
888*1e07b27eSBarry Smith     }
889*1e07b27eSBarry Smith     ierr = VecRestoreArray(xred,&LA_xred);CHKERRQ(ierr);
890*1e07b27eSBarry Smith   }
891*1e07b27eSBarry Smith   ierr = VecRestoreArrayRead(xtmp,&x_array);CHKERRQ(ierr);
892*1e07b27eSBarry Smith 
893*1e07b27eSBarry Smith   /* solve */
894*1e07b27eSBarry Smith   if (isActiveRank(sred->psubcomm)) {
895*1e07b27eSBarry Smith     ierr = KSPSolve(sred->ksp,xred,yred);CHKERRQ(ierr);
896*1e07b27eSBarry Smith   }
897*1e07b27eSBarry Smith 
898*1e07b27eSBarry Smith   /* return vector */
899*1e07b27eSBarry Smith   ierr = VecGetBlockSize(xtmp,&bs);CHKERRQ(ierr);
900*1e07b27eSBarry Smith   ierr = VecGetArray(xtmp,&array);CHKERRQ(ierr);
901*1e07b27eSBarry Smith   if (yred) {
902*1e07b27eSBarry Smith     const PetscScalar *LA_yred;
903*1e07b27eSBarry Smith     ierr = VecGetOwnershipRange(yred,&st,&ed);CHKERRQ(ierr);
904*1e07b27eSBarry Smith     ierr = VecGetArrayRead(yred,&LA_yred);CHKERRQ(ierr);
905*1e07b27eSBarry Smith     for (i=0; i<ed-st; i++) {
906*1e07b27eSBarry Smith       array[i] = LA_yred[i];
907*1e07b27eSBarry Smith     }
908*1e07b27eSBarry Smith     ierr = VecRestoreArrayRead(yred,&LA_yred);CHKERRQ(ierr);
909*1e07b27eSBarry Smith   } else {
910*1e07b27eSBarry Smith     for (i=0; i<bs; i++) {
911*1e07b27eSBarry Smith       array[i] = 0.0;
912*1e07b27eSBarry Smith     }
913*1e07b27eSBarry Smith   }
914*1e07b27eSBarry Smith   ierr = VecRestoreArray(xtmp,&array);CHKERRQ(ierr);
915*1e07b27eSBarry Smith   ierr = VecScatterBegin(scatter,xtmp,xp,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
916*1e07b27eSBarry Smith   ierr = VecScatterEnd(scatter,xtmp,xp,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
917*1e07b27eSBarry Smith   ierr = MatMult(perm,xp,y);CHKERRQ(ierr);
918*1e07b27eSBarry Smith   PetscFunctionReturn(0);
919*1e07b27eSBarry Smith }
920*1e07b27eSBarry Smith 
921*1e07b27eSBarry Smith #undef __FUNCT__
922*1e07b27eSBarry Smith #define __FUNCT__ "PCReset_Telescope_dmda"
923*1e07b27eSBarry Smith PetscErrorCode PCReset_Telescope_dmda(PC pc)
924*1e07b27eSBarry Smith {
925*1e07b27eSBarry Smith   PetscErrorCode       ierr;
926*1e07b27eSBarry Smith   PC_Telescope         sred = (PC_Telescope)pc->data;
927*1e07b27eSBarry Smith   PC_Telescope_DMDACtx *ctx;
928*1e07b27eSBarry Smith 
929*1e07b27eSBarry Smith   PetscFunctionBegin;
930*1e07b27eSBarry Smith   ctx = (PC_Telescope_DMDACtx*)sred->dm_ctx;
931*1e07b27eSBarry Smith   ierr = VecDestroy(&ctx->xp);CHKERRQ(ierr);
932*1e07b27eSBarry Smith   ierr = MatDestroy(&ctx->permutation);CHKERRQ(ierr);
933*1e07b27eSBarry Smith   ierr = DMDestroy(&ctx->dmrepart);CHKERRQ(ierr);
934*1e07b27eSBarry Smith   ierr = PetscFree(ctx->range_i_re);CHKERRQ(ierr);
935*1e07b27eSBarry Smith   ierr = PetscFree(ctx->range_j_re);CHKERRQ(ierr);
936*1e07b27eSBarry Smith   ierr = PetscFree(ctx->range_k_re);CHKERRQ(ierr);
937*1e07b27eSBarry Smith   ierr = PetscFree(ctx->start_i_re);CHKERRQ(ierr);
938*1e07b27eSBarry Smith   ierr = PetscFree(ctx->start_j_re);CHKERRQ(ierr);
939*1e07b27eSBarry Smith   ierr = PetscFree(ctx->start_k_re);CHKERRQ(ierr);
940*1e07b27eSBarry Smith   PetscFunctionReturn(0);
941*1e07b27eSBarry Smith }
942*1e07b27eSBarry Smith 
943*1e07b27eSBarry Smith #undef __FUNCT__
944*1e07b27eSBarry Smith #define __FUNCT__ "DMView_DMDAShort_3d"
945*1e07b27eSBarry Smith PetscErrorCode DMView_DMDAShort_3d(DM dm,PetscViewer v)
946*1e07b27eSBarry Smith {
947*1e07b27eSBarry Smith   PetscInt       M,N,P,m,n,p,ndof,nsw;
948*1e07b27eSBarry Smith   MPI_Comm       comm;
949*1e07b27eSBarry Smith   PetscMPIInt    size;
950*1e07b27eSBarry Smith   const char*    prefix;
951*1e07b27eSBarry Smith   PetscErrorCode ierr;
952*1e07b27eSBarry Smith 
953*1e07b27eSBarry Smith   PetscFunctionBegin;
954*1e07b27eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
955*1e07b27eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
956*1e07b27eSBarry Smith   ierr = DMGetOptionsPrefix(dm,&prefix);CHKERRQ(ierr);
957*1e07b27eSBarry Smith   ierr = DMDAGetInfo(dm,NULL,&M,&N,&P,&m,&n,&p,&ndof,&nsw,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
958*1e07b27eSBarry Smith   if (prefix) {ierr = PetscViewerASCIIPrintf(v,"DMDA Object:    (%s)    %d MPI processes\n",prefix,size);CHKERRQ(ierr);}
959*1e07b27eSBarry Smith   else {ierr = PetscViewerASCIIPrintf(v,"DMDA Object:    %d MPI processes\n",size);CHKERRQ(ierr);}
960*1e07b27eSBarry Smith   ierr = PetscViewerASCIIPrintf(v,"  M %D N %D P %D m %D n %D p %D dof %D overlap %D\n",M,N,P,m,n,p,ndof,nsw);CHKERRQ(ierr);
961*1e07b27eSBarry Smith   PetscFunctionReturn(0);
962*1e07b27eSBarry Smith }
963*1e07b27eSBarry Smith 
964*1e07b27eSBarry Smith #undef __FUNCT__
965*1e07b27eSBarry Smith #define __FUNCT__ "DMView_DMDAShort_2d"
966*1e07b27eSBarry Smith PetscErrorCode DMView_DMDAShort_2d(DM dm,PetscViewer v)
967*1e07b27eSBarry Smith {
968*1e07b27eSBarry Smith   PetscInt       M,N,m,n,ndof,nsw;
969*1e07b27eSBarry Smith   MPI_Comm       comm;
970*1e07b27eSBarry Smith   PetscMPIInt    size;
971*1e07b27eSBarry Smith   const char*    prefix;
972*1e07b27eSBarry Smith   PetscErrorCode ierr;
973*1e07b27eSBarry Smith 
974*1e07b27eSBarry Smith   PetscFunctionBegin;
975*1e07b27eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
976*1e07b27eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
977*1e07b27eSBarry Smith   ierr = DMGetOptionsPrefix(dm,&prefix);CHKERRQ(ierr);
978*1e07b27eSBarry Smith   ierr = DMDAGetInfo(dm,NULL,&M,&N,NULL,&m,&n,NULL,&ndof,&nsw,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
979*1e07b27eSBarry Smith   if (prefix) {PetscViewerASCIIPrintf(v,"DMDA Object:    (%s)    %d MPI processes\n",prefix,size);CHKERRQ(ierr);}
980*1e07b27eSBarry Smith   else {ierr = PetscViewerASCIIPrintf(v,"DMDA Object:    %d MPI processes\n",size);CHKERRQ(ierr);}
981*1e07b27eSBarry Smith   ierr = PetscViewerASCIIPrintf(v,"  M %D N %D m %D n %D dof %D overlap %D\n",M,N,m,n,ndof,nsw);CHKERRQ(ierr);
982*1e07b27eSBarry Smith   PetscFunctionReturn(0);
983*1e07b27eSBarry Smith }
984*1e07b27eSBarry Smith 
985*1e07b27eSBarry Smith #undef __FUNCT__
986*1e07b27eSBarry Smith #define __FUNCT__ "DMView_DMDAShort"
987*1e07b27eSBarry Smith PetscErrorCode DMView_DMDAShort(DM dm,PetscViewer v)
988*1e07b27eSBarry Smith {
989*1e07b27eSBarry Smith   PetscErrorCode ierr;
990*1e07b27eSBarry Smith   PetscInt       dim;
991*1e07b27eSBarry Smith 
992*1e07b27eSBarry Smith   PetscFunctionBegin;
993*1e07b27eSBarry Smith   ierr = DMDAGetInfo(dm,&dim,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
994*1e07b27eSBarry Smith   switch (dim) {
995*1e07b27eSBarry Smith   case 2: ierr = DMView_DMDAShort_2d(dm,v);CHKERRQ(ierr);
996*1e07b27eSBarry Smith     break;
997*1e07b27eSBarry Smith   case 3: ierr = DMView_DMDAShort_3d(dm,v);CHKERRQ(ierr);
998*1e07b27eSBarry Smith     break;
999*1e07b27eSBarry Smith   }
1000*1e07b27eSBarry Smith   PetscFunctionReturn(0);
1001*1e07b27eSBarry Smith }
1002*1e07b27eSBarry Smith 
1003