xref: /petsc/src/dm/impls/patch/patch.c (revision 60c22052288e8457b4b22117d1110c07a835c0b9)
1af0996ceSBarry Smith #include <petsc/private/dmpatchimpl.h>   /*I      "petscdmpatch.h"   I*/
23a19ef87SMatthew G Knepley #include <petscdmda.h>
30c312b8eSJed Brown #include <petscsf.h>
43a19ef87SMatthew G Knepley 
55ee0e871SMatthew G Knepley /*
65ee0e871SMatthew G Knepley Solver loop to update \tau:
75ee0e871SMatthew G Knepley 
85ee0e871SMatthew G Knepley   DMZoom(dmc, &dmz)
95ee0e871SMatthew G Knepley   DMRefine(dmz, &dmf),
105ee0e871SMatthew G Knepley   Scatter Xcoarse -> Xzoom,
115ee0e871SMatthew G Knepley   Interpolate Xzoom -> Xfine (note that this may be on subcomms),
125ee0e871SMatthew G Knepley   Smooth Xfine using two-step smoother
135ee0e871SMatthew G Knepley     normal smoother plus Kaczmarz---moves back and forth from dmzoom to dmfine
145ee0e871SMatthew G Knepley   Compute residual Rfine
155ee0e871SMatthew G Knepley   Restrict Rfine to Rzoom_restricted
165ee0e871SMatthew G Knepley   Scatter Rzoom_restricted -> Rcoarse_restricted
175ee0e871SMatthew G Knepley   Compute global residual Rcoarse
185ee0e871SMatthew G Knepley   TauCoarse = Rcoarse - Rcoarse_restricted
195ee0e871SMatthew G Knepley */
205ee0e871SMatthew G Knepley 
21*60c22052SBarry Smith /*@C
22*60c22052SBarry Smith   DMPatchZoom - Create patches of a DMDA on subsets of processes, indicated by commz
2359583ba0SMatthew G Knepley 
24d083f849SBarry Smith   Collective on dm
25a487c981SJed Brown 
2659583ba0SMatthew G Knepley   Input Parameters:
2759583ba0SMatthew G Knepley   + dm - the DM
28*60c22052SBarry Smith   . lower,upper - the upper right corner and the lower left corner of the requested patch
29*60c22052SBarry Smith   - commz - the new communicator for the patch, MPI_COMM_NULL indicates that the given rank will not own a patch
3059583ba0SMatthew G Knepley 
3159583ba0SMatthew G Knepley   Output Parameters:
3259583ba0SMatthew G Knepley   + dmz  - the patch DM
33*60c22052SBarry Smith   . sfz  - the PetscSF mapping the patch+halo to the zoomed version (optional)
34*60c22052SBarry Smith   - sfzr - the PetscSF mapping the patch to the restricted zoomed version
3559583ba0SMatthew G Knepley 
3659583ba0SMatthew G Knepley   Level: intermediate
3759583ba0SMatthew G Knepley 
38*60c22052SBarry Smith .seealso: DMPatchSolve(), DMDACreatePatchIS()
39*60c22052SBarry Smith @*/
40*60c22052SBarry Smith PetscErrorCode DMPatchZoom(DM dm, MatStencil lower, MatStencil upper, MPI_Comm commz, DM *dmz, PetscSF *sfz, PetscSF *sfzr)
4159583ba0SMatthew G Knepley {
4259583ba0SMatthew G Knepley   DMDAStencilType st;
43bd1050a3SMatthew G Knepley   MatStencil      blower, bupper, loclower, locupper;
44bd1050a3SMatthew G Knepley   IS              is;
45bd1050a3SMatthew G Knepley   const PetscInt  *ranges, *indices;
460298fd71SBarry Smith   PetscInt        *localPoints  = NULL;
470298fd71SBarry Smith   PetscSFNode     *remotePoints = NULL;
48392fa6c6SMatthew G Knepley   PetscInt        dim, dof;
49*60c22052SBarry Smith   PetscInt        M, N, P, rM, rN, rP, halo = 1, sxb, syb, szb, sxr, syr, szr, exr, eyr, ezr, mxb, myb, mzb, i, j, k, l, q;
50bd1050a3SMatthew G Knepley   PetscMPIInt     size;
513b5e53cdSSajid Ali   PetscBool       patchis_offproc = PETSC_TRUE;
5259583ba0SMatthew G Knepley   PetscErrorCode  ierr;
53*60c22052SBarry Smith   Vec             X;
5459583ba0SMatthew G Knepley 
5559583ba0SMatthew G Knepley   PetscFunctionBegin;
56*60c22052SBarry Smith   if (!sfz) halo = 0;
5782f516ccSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
5859583ba0SMatthew G Knepley   /* Create patch DM */
59ea78f98cSLisandro Dalcin   ierr = DMDAGetInfo(dm, &dim, &M, &N, &P, NULL,NULL,NULL, &dof, NULL,NULL,NULL,NULL, &st);CHKERRQ(ierr);
60a07761baSMatthew G Knepley 
61a07761baSMatthew G Knepley   /* Get piece for rank r, expanded by halo */
62392fa6c6SMatthew G Knepley   bupper.i = PetscMin(M, upper.i + halo); blower.i = PetscMax(lower.i - halo, 0);
63392fa6c6SMatthew G Knepley   bupper.j = PetscMin(N, upper.j + halo); blower.j = PetscMax(lower.j - halo, 0);
64392fa6c6SMatthew G Knepley   bupper.k = PetscMin(P, upper.k + halo); blower.k = PetscMax(lower.k - halo, 0);
65392fa6c6SMatthew G Knepley   rM       = bupper.i - blower.i;
66392fa6c6SMatthew G Knepley   rN       = bupper.j - blower.j;
67392fa6c6SMatthew G Knepley   rP       = bupper.k - blower.k;
6859583ba0SMatthew G Knepley 
69bb71ef15SMatthew G Knepley   if (commz != MPI_COMM_NULL) {
7059583ba0SMatthew G Knepley     ierr = DMDACreate(commz, dmz);CHKERRQ(ierr);
71c73cfb54SMatthew G. Knepley     ierr = DMSetDimension(*dmz, dim);CHKERRQ(ierr);
72a07761baSMatthew G Knepley     ierr = DMDASetSizes(*dmz, rM, rN, rP);CHKERRQ(ierr);
7359583ba0SMatthew G Knepley     ierr = DMDASetNumProcs(*dmz, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE);CHKERRQ(ierr);
74bff4a2f0SMatthew G. Knepley     ierr = DMDASetBoundaryType(*dmz, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE);CHKERRQ(ierr);
7559583ba0SMatthew G Knepley     ierr = DMDASetDof(*dmz, dof);CHKERRQ(ierr);
7659583ba0SMatthew G Knepley     ierr = DMDASetStencilType(*dmz, st);CHKERRQ(ierr);
77392fa6c6SMatthew G Knepley     ierr = DMDASetStencilWidth(*dmz, 0);CHKERRQ(ierr);
780298fd71SBarry Smith     ierr = DMDASetOwnershipRanges(*dmz, NULL, NULL, NULL);CHKERRQ(ierr);
7959583ba0SMatthew G Knepley     ierr = DMSetFromOptions(*dmz);CHKERRQ(ierr);
8059583ba0SMatthew G Knepley     ierr = DMSetUp(*dmz);CHKERRQ(ierr);
81bd1050a3SMatthew G Knepley     ierr = DMDAGetCorners(*dmz, &sxb, &syb, &szb, &mxb, &myb, &mzb);CHKERRQ(ierr);
82bd1050a3SMatthew G Knepley     sxr  = PetscMax(sxb,     lower.i - blower.i);
83bd1050a3SMatthew G Knepley     syr  = PetscMax(syb,     lower.j - blower.j);
84bd1050a3SMatthew G Knepley     szr  = PetscMax(szb,     lower.k - blower.k);
85bd1050a3SMatthew G Knepley     exr  = PetscMin(sxb+mxb, upper.i - blower.i);
86bd1050a3SMatthew G Knepley     eyr  = PetscMin(syb+myb, upper.j - blower.j);
87bd1050a3SMatthew G Knepley     ezr  = PetscMin(szb+mzb, upper.k - blower.k);
88*60c22052SBarry Smith     ierr = PetscMalloc2(dof*rM*rN*PetscMax(rP,1),&localPoints,dof*rM*rN*PetscMax(rP,1),&remotePoints);CHKERRQ(ierr);
89bb71ef15SMatthew G Knepley   } else {
90bb71ef15SMatthew G Knepley     sxr = syr = szr = exr = eyr = ezr = sxb = syb = szb = mxb = myb = mzb = 0;
91bb71ef15SMatthew G Knepley   }
92392fa6c6SMatthew G Knepley 
9359583ba0SMatthew G Knepley   /* Create SF for restricted map */
94*60c22052SBarry Smith   ierr = DMCreateGlobalVector(dm,&X);CHKERRQ(ierr);
95077aedafSJed Brown   ierr = VecGetOwnershipRanges(X,&ranges);CHKERRQ(ierr);
968865f1eaSKarl Rupp 
97bd1050a3SMatthew G Knepley   loclower.i = blower.i + sxr; locupper.i = blower.i + exr;
98bd1050a3SMatthew G Knepley   loclower.j = blower.j + syr; locupper.j = blower.j + eyr;
99bd1050a3SMatthew G Knepley   loclower.k = blower.k + szr; locupper.k = blower.k + ezr;
1008865f1eaSKarl Rupp 
1013b5e53cdSSajid Ali   ierr = DMDACreatePatchIS(dm, &loclower, &locupper, &is, patchis_offproc);CHKERRQ(ierr);
102bd1050a3SMatthew G Knepley   ierr = ISGetIndices(is, &indices);CHKERRQ(ierr);
1038865f1eaSKarl Rupp 
104*60c22052SBarry Smith   if (dim < 3) {mzb = 1; ezr = 1;}
105392fa6c6SMatthew G Knepley   q = 0;
106bd1050a3SMatthew G Knepley   for (k = szb; k < szb+mzb; ++k) {
107bd1050a3SMatthew G Knepley     if ((k < szr) || (k >= ezr)) continue;
108bd1050a3SMatthew G Knepley     for (j = syb; j < syb+myb; ++j) {
109bd1050a3SMatthew G Knepley       if ((j < syr) || (j >= eyr)) continue;
110bd1050a3SMatthew G Knepley       for (i = sxb; i < sxb+mxb; ++i) {
111*60c22052SBarry Smith         for (l=0; l<dof; l++) {
112*60c22052SBarry Smith           const PetscInt lp = l + dof*(((k-szb)*rN + (j-syb))*rM + i-sxb);
113bd1050a3SMatthew G Knepley           PetscInt       r;
114392fa6c6SMatthew G Knepley 
115bd1050a3SMatthew G Knepley           if ((i < sxr) || (i >= exr)) continue;
116392fa6c6SMatthew G Knepley           localPoints[q]        = lp;
117bd1050a3SMatthew G Knepley           ierr = PetscFindInt(indices[q], size+1, ranges, &r);CHKERRQ(ierr);
1188865f1eaSKarl Rupp 
119bd1050a3SMatthew G Knepley           remotePoints[q].rank  = r < 0 ? -(r+1) - 1 : r;
120bd1050a3SMatthew G Knepley           remotePoints[q].index = indices[q] - ranges[remotePoints[q].rank];
121bd1050a3SMatthew G Knepley           ++q;
122392fa6c6SMatthew G Knepley         }
123392fa6c6SMatthew G Knepley       }
124392fa6c6SMatthew G Knepley     }
125*60c22052SBarry Smith   }
126bd1050a3SMatthew G Knepley   ierr = ISRestoreIndices(is, &indices);CHKERRQ(ierr);
127bd1050a3SMatthew G Knepley   ierr = ISDestroy(&is);CHKERRQ(ierr);
12882f516ccSBarry Smith   ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm), sfzr);CHKERRQ(ierr);
129bd1050a3SMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) *sfzr, "Restricted Map");CHKERRQ(ierr);
130*60c22052SBarry Smith   ierr = PetscSFSetGraph(*sfzr, dof*M*N*P, q, localPoints, PETSC_COPY_VALUES, remotePoints, PETSC_COPY_VALUES);CHKERRQ(ierr);
131392fa6c6SMatthew G Knepley 
132*60c22052SBarry Smith   if (sfz) {
133392fa6c6SMatthew G Knepley     /* Create SF for buffered map */
134bd1050a3SMatthew G Knepley     loclower.i = blower.i + sxb; locupper.i = blower.i + sxb+mxb;
135bd1050a3SMatthew G Knepley     loclower.j = blower.j + syb; locupper.j = blower.j + syb+myb;
136bd1050a3SMatthew G Knepley     loclower.k = blower.k + szb; locupper.k = blower.k + szb+mzb;
1378865f1eaSKarl Rupp 
1383b5e53cdSSajid Ali     ierr = DMDACreatePatchIS(dm, &loclower, &locupper, &is, patchis_offproc);CHKERRQ(ierr);
139bd1050a3SMatthew G Knepley     ierr = ISGetIndices(is, &indices);CHKERRQ(ierr);
1408865f1eaSKarl Rupp 
141392fa6c6SMatthew G Knepley     q = 0;
142392fa6c6SMatthew G Knepley     for (k = szb; k < szb+mzb; ++k) {
143392fa6c6SMatthew G Knepley       for (j = syb; j < syb+myb; ++j) {
144392fa6c6SMatthew G Knepley         for (i = sxb; i < sxb+mxb; ++i, ++q) {
145bd1050a3SMatthew G Knepley           PetscInt r;
146392fa6c6SMatthew G Knepley 
147bd1050a3SMatthew G Knepley           localPoints[q]        = q;
148bd1050a3SMatthew G Knepley           ierr = PetscFindInt(indices[q], size+1, ranges, &r);CHKERRQ(ierr);
149bd1050a3SMatthew G Knepley           remotePoints[q].rank  = r < 0 ? -(r+1) - 1 : r;
150bd1050a3SMatthew G Knepley           remotePoints[q].index = indices[q] - ranges[remotePoints[q].rank];
151392fa6c6SMatthew G Knepley         }
152392fa6c6SMatthew G Knepley       }
153392fa6c6SMatthew G Knepley     }
154bd1050a3SMatthew G Knepley     ierr = ISRestoreIndices(is, &indices);CHKERRQ(ierr);
155bd1050a3SMatthew G Knepley     ierr = ISDestroy(&is);CHKERRQ(ierr);
15682f516ccSBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm), sfz);CHKERRQ(ierr);
157bd1050a3SMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) *sfz, "Buffered Map");CHKERRQ(ierr);
158392fa6c6SMatthew G Knepley     ierr = PetscSFSetGraph(*sfz, M*N*P, q, localPoints, PETSC_COPY_VALUES, remotePoints, PETSC_COPY_VALUES);CHKERRQ(ierr);
159*60c22052SBarry Smith   }
160392fa6c6SMatthew G Knepley 
161*60c22052SBarry Smith   ierr = VecDestroy(&X);CHKERRQ(ierr);
162392fa6c6SMatthew G Knepley   ierr = PetscFree2(localPoints, remotePoints);CHKERRQ(ierr);
16359583ba0SMatthew G Knepley   PetscFunctionReturn(0);
16459583ba0SMatthew G Knepley }
16559583ba0SMatthew G Knepley 
166bd1050a3SMatthew G Knepley typedef enum {PATCH_COMM_TYPE_WORLD = 0, PATCH_COMM_TYPE_SELF = 1} PatchCommType;
167bd1050a3SMatthew G Knepley 
16859583ba0SMatthew G Knepley PetscErrorCode DMPatchSolve(DM dm)
16959583ba0SMatthew G Knepley {
17082f516ccSBarry Smith   MPI_Comm       comm;
171bb71ef15SMatthew G Knepley   MPI_Comm       commz;
172bb71ef15SMatthew G Knepley   DM             dmc;
173392fa6c6SMatthew G Knepley   PetscSF        sfz, sfzr;
174bb71ef15SMatthew G Knepley   Vec            XC;
175bb71ef15SMatthew G Knepley   MatStencil     patchSize, commSize, gridRank, lower, upper;
176bb71ef15SMatthew G Knepley   PetscInt       M, N, P, i, j, k, l, m, n, p = 0;
177bb71ef15SMatthew G Knepley   PetscMPIInt    rank, size;
178bb71ef15SMatthew G Knepley   PetscInt       debug = 0;
17959583ba0SMatthew G Knepley   PetscErrorCode ierr;
18059583ba0SMatthew G Knepley 
18159583ba0SMatthew G Knepley   PetscFunctionBegin;
18282f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
183bb71ef15SMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
184bb71ef15SMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
185bd1050a3SMatthew G Knepley   ierr = DMPatchGetCoarse(dm, &dmc);CHKERRQ(ierr);
186392fa6c6SMatthew G Knepley   ierr = DMPatchGetPatchSize(dm, &patchSize);CHKERRQ(ierr);
187bb71ef15SMatthew G Knepley   ierr = DMPatchGetCommSize(dm, &commSize);CHKERRQ(ierr);
188bb71ef15SMatthew G Knepley   ierr = DMPatchGetCommSize(dm, &commSize);CHKERRQ(ierr);
189bd1050a3SMatthew G Knepley   ierr = DMGetGlobalVector(dmc, &XC);CHKERRQ(ierr);
190ea78f98cSLisandro Dalcin   ierr = DMDAGetInfo(dmc, NULL, &M, &N, &P, &l, &m, &n, NULL,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
191bb71ef15SMatthew G Knepley   M    = PetscMax(M, 1); l = PetscMax(l, 1);
192bb71ef15SMatthew G Knepley   N    = PetscMax(N, 1); m = PetscMax(m, 1);
193bb71ef15SMatthew G Knepley   P    = PetscMax(P, 1); n = PetscMax(n, 1);
1948865f1eaSKarl Rupp 
195bb71ef15SMatthew G Knepley   gridRank.i = rank       % l;
196bb71ef15SMatthew G Knepley   gridRank.j = rank/l     % m;
197bb71ef15SMatthew G Knepley   gridRank.k = rank/(l*m) % n;
1988865f1eaSKarl Rupp 
199bb71ef15SMatthew G Knepley   if (commSize.i*commSize.j*commSize.k == size || commSize.i*commSize.j*commSize.k == 0) {
200bb71ef15SMatthew G Knepley     commSize.i = l; commSize.j = m; commSize.k = n;
201bb71ef15SMatthew G Knepley     commz      = comm;
202bb71ef15SMatthew G Knepley   } else if (commSize.i*commSize.j*commSize.k == 1) {
203bb71ef15SMatthew G Knepley     commz = PETSC_COMM_SELF;
204bb71ef15SMatthew G Knepley   } else {
205bb71ef15SMatthew G Knepley     const PetscMPIInt newComm = ((gridRank.k/commSize.k)*(m/commSize.j) + gridRank.j/commSize.j)*(l/commSize.i) + (gridRank.i/commSize.i);
206bb71ef15SMatthew G Knepley     const PetscMPIInt newRank = ((gridRank.k%commSize.k)*commSize.j     + gridRank.j%commSize.j)*commSize.i     + (gridRank.i%commSize.i);
207bb71ef15SMatthew G Knepley 
208bb71ef15SMatthew G Knepley     ierr = MPI_Comm_split(comm, newComm, newRank, &commz);CHKERRQ(ierr);
209bf336ccaSMatthew G Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "Rank %d color %d key %d commz %d\n", rank, newComm, newRank, *((PetscMPIInt*) &commz));CHKERRQ(ierr);}
210bb71ef15SMatthew G Knepley   }
211bb71ef15SMatthew G Knepley   /*
212bb71ef15SMatthew G Knepley    Assumptions:
213bb71ef15SMatthew G Knepley      - patchSize divides gridSize
214bb71ef15SMatthew G Knepley      - commSize divides gridSize
215bb71ef15SMatthew G Knepley      - commSize divides l,m,n
216bb71ef15SMatthew G Knepley    Ignore multiple patches per rank for now
217bb71ef15SMatthew G Knepley 
218bb71ef15SMatthew G Knepley    Multiple ranks per patch:
219bb71ef15SMatthew G Knepley      - l,m,n divides patchSize
220bb71ef15SMatthew G Knepley      - commSize divides patchSize
221bb71ef15SMatthew G Knepley    */
222392fa6c6SMatthew G Knepley   for (k = 0; k < P; k += PetscMax(patchSize.k, 1)) {
223392fa6c6SMatthew G Knepley     for (j = 0; j < N; j += PetscMax(patchSize.j, 1)) {
224392fa6c6SMatthew G Knepley       for (i = 0; i < M; i += PetscMax(patchSize.i, 1), ++p) {
225bb71ef15SMatthew G Knepley         MPI_Comm    commp = MPI_COMM_NULL;
2260298fd71SBarry Smith         DM          dmz   = NULL;
2272f857c34SHong Zhang #if 0
2280298fd71SBarry Smith         DM          dmf     = NULL;
2290298fd71SBarry Smith         Mat         interpz = NULL;
2302f857c34SHong Zhang #endif
2310298fd71SBarry Smith         Vec         XZ       = NULL;
2320298fd71SBarry Smith         PetscScalar *xcarray = NULL;
2330298fd71SBarry Smith         PetscScalar *xzarray = NULL;
234bb87e006SMatthew G Knepley 
235bb71ef15SMatthew G Knepley         if ((gridRank.k/commSize.k == p/(l/commSize.i * m/commSize.j) % n/commSize.k) &&
236bb71ef15SMatthew G Knepley             (gridRank.j/commSize.j == p/(l/commSize.i)                % m/commSize.j) &&
237bb71ef15SMatthew G Knepley             (gridRank.i/commSize.i == p                               % l/commSize.i)) {
238bb71ef15SMatthew G Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "Rank %d is accepting Patch %d\n", rank, p);CHKERRQ(ierr);}
239bb71ef15SMatthew G Knepley           commp = commz;
240bb71ef15SMatthew G Knepley         }
241bb87e006SMatthew G Knepley         /* Zoom to coarse patch */
242392fa6c6SMatthew G Knepley         lower.i = i; lower.j = j; lower.k = k;
243392fa6c6SMatthew G Knepley         upper.i = i + patchSize.i; upper.j = j + patchSize.j; upper.k = k + patchSize.k;
244*60c22052SBarry Smith         ierr    = DMPatchZoom(dmc, lower, upper, commp, &dmz, &sfz, &sfzr);CHKERRQ(ierr);
245da80777bSKarl Rupp         lower.c = 0; /* initialize member, otherwise compiler issues warnings */
246da80777bSKarl Rupp         upper.c = 0; /* initialize member, otherwise compiler issues warnings */
247*60c22052SBarry Smith         if (debug) {ierr = PetscPrintf(comm, "Patch %d: (%d, %d, %d)--(%d, %d, %d)\n", p, lower.i, lower.j, lower.k, upper.i, upper.j, upper.k);CHKERRQ(ierr);}
248bb71ef15SMatthew G Knepley         if (dmz) {ierr = DMView(dmz, PETSC_VIEWER_STDOUT_(commz));CHKERRQ(ierr);}
249bb71ef15SMatthew G Knepley         ierr = PetscSFView(sfz,  PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr);
250bb71ef15SMatthew G Knepley         ierr = PetscSFView(sfzr, PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr);
25159583ba0SMatthew G Knepley         /* Scatter Xcoarse -> Xzoom */
252bb71ef15SMatthew G Knepley         if (dmz) {ierr = DMGetGlobalVector(dmz, &XZ);CHKERRQ(ierr);}
253bb71ef15SMatthew G Knepley         if (XZ)  {ierr = VecGetArray(XZ, &xzarray);CHKERRQ(ierr);}
254bd1050a3SMatthew G Knepley         ierr = VecGetArray(XC, &xcarray);CHKERRQ(ierr);
25556668867SJed Brown         ierr = PetscSFBcastBegin(sfz, MPIU_SCALAR, xcarray, xzarray);CHKERRQ(ierr);
25656668867SJed Brown         ierr = PetscSFBcastEnd(sfz, MPIU_SCALAR, xcarray, xzarray);CHKERRQ(ierr);
257bd1050a3SMatthew G Knepley         ierr = VecRestoreArray(XC, &xcarray);CHKERRQ(ierr);
258bb71ef15SMatthew G Knepley         if (XZ)  {ierr = VecRestoreArray(XZ, &xzarray);CHKERRQ(ierr);}
259bd1050a3SMatthew G Knepley #if 0
26059583ba0SMatthew G Knepley         /* Interpolate Xzoom -> Xfine, note that this may be on subcomms */
26159583ba0SMatthew G Knepley         ierr = DMRefine(dmz, MPI_COMM_NULL, &dmf);CHKERRQ(ierr);
2620298fd71SBarry Smith         ierr = DMCreateInterpolation(dmz, dmf, &interpz, NULL);CHKERRQ(ierr);
26359583ba0SMatthew G Knepley         ierr = DMInterpolate(dmz, interpz, dmf);CHKERRQ(ierr);
26459583ba0SMatthew G Knepley         /* Smooth Xfine using two-step smoother, normal smoother plus Kaczmarz---moves back and forth from dmzoom to dmfine */
26559583ba0SMatthew G Knepley         /* Compute residual Rfine */
26659583ba0SMatthew G Knepley         /* Restrict Rfine to Rzoom_restricted */
267bd1050a3SMatthew G Knepley #endif
26859583ba0SMatthew G Knepley         /* Scatter Rzoom_restricted -> Rcoarse_restricted */
269bb71ef15SMatthew G Knepley         if (XZ)  {ierr = VecGetArray(XZ, &xzarray);CHKERRQ(ierr);}
270bd1050a3SMatthew G Knepley         ierr = VecGetArray(XC, &xcarray);CHKERRQ(ierr);
271a9b180a6SBarry Smith         ierr = PetscSFReduceBegin(sfzr, MPIU_SCALAR, xzarray, xcarray, MPIU_SUM);CHKERRQ(ierr);
272a9b180a6SBarry Smith         ierr = PetscSFReduceEnd(sfzr, MPIU_SCALAR, xzarray, xcarray, MPIU_SUM);CHKERRQ(ierr);
273bd1050a3SMatthew G Knepley         ierr = VecRestoreArray(XC, &xcarray);CHKERRQ(ierr);
274bb71ef15SMatthew G Knepley         if (XZ)  {ierr = VecRestoreArray(XZ, &xzarray);CHKERRQ(ierr);}
275bb71ef15SMatthew G Knepley         if (dmz) {ierr = DMRestoreGlobalVector(dmz, &XZ);CHKERRQ(ierr);}
27659583ba0SMatthew G Knepley         /* Compute global residual Rcoarse */
27759583ba0SMatthew G Knepley         /* TauCoarse = Rcoarse - Rcoarse_restricted */
278bb87e006SMatthew G Knepley 
2796fbb21edSJed Brown         ierr = PetscSFDestroy(&sfz);CHKERRQ(ierr);
2806fbb21edSJed Brown         ierr = PetscSFDestroy(&sfzr);CHKERRQ(ierr);
2816fbb21edSJed Brown         ierr = DMDestroy(&dmz);CHKERRQ(ierr);
282a07761baSMatthew G Knepley       }
283392fa6c6SMatthew G Knepley     }
284392fa6c6SMatthew G Knepley   }
285bd1050a3SMatthew G Knepley   ierr = DMRestoreGlobalVector(dmc, &XC);CHKERRQ(ierr);
28659583ba0SMatthew G Knepley   PetscFunctionReturn(0);
28759583ba0SMatthew G Knepley }
28859583ba0SMatthew G Knepley 
289*60c22052SBarry Smith PetscErrorCode DMPatchView_ASCII(DM dm, PetscViewer viewer)
2903a19ef87SMatthew G Knepley {
2913a19ef87SMatthew G Knepley   DM_Patch          *mesh = (DM_Patch*) dm->data;
2923a19ef87SMatthew G Knepley   PetscViewerFormat format;
293392fa6c6SMatthew G Knepley   const char        *name;
2943a19ef87SMatthew G Knepley   PetscErrorCode    ierr;
2953a19ef87SMatthew G Knepley 
2963a19ef87SMatthew G Knepley   PetscFunctionBegin;
2973a19ef87SMatthew G Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
2983a19ef87SMatthew G Knepley   /* if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) */
299392fa6c6SMatthew G Knepley   ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
300392fa6c6SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Patch DM %s\n", name);CHKERRQ(ierr);
3013a19ef87SMatthew G Knepley   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
3023a19ef87SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Coarse DM\n");CHKERRQ(ierr);
3033a19ef87SMatthew G Knepley   ierr = DMView(mesh->dmCoarse, viewer);CHKERRQ(ierr);
3043a19ef87SMatthew G Knepley   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
3053a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3063a19ef87SMatthew G Knepley }
3073a19ef87SMatthew G Knepley 
3083a19ef87SMatthew G Knepley PetscErrorCode DMView_Patch(DM dm, PetscViewer viewer)
3093a19ef87SMatthew G Knepley {
3103a19ef87SMatthew G Knepley   PetscBool      iascii, isbinary;
3113a19ef87SMatthew G Knepley   PetscErrorCode ierr;
3123a19ef87SMatthew G Knepley 
3133a19ef87SMatthew G Knepley   PetscFunctionBegin;
3143a19ef87SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3153a19ef87SMatthew G Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
3163a19ef87SMatthew G Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
3173a19ef87SMatthew G Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);CHKERRQ(ierr);
3183a19ef87SMatthew G Knepley   if (iascii) {
319*60c22052SBarry Smith     ierr = DMPatchView_ASCII(dm, viewer);CHKERRQ(ierr);
32011aeaf0aSBarry Smith   }
3213a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3223a19ef87SMatthew G Knepley }
3233a19ef87SMatthew G Knepley 
3243a19ef87SMatthew G Knepley PetscErrorCode DMDestroy_Patch(DM dm)
3253a19ef87SMatthew G Knepley {
3263a19ef87SMatthew G Knepley   DM_Patch       *mesh = (DM_Patch*) dm->data;
3273a19ef87SMatthew G Knepley   PetscErrorCode ierr;
3283a19ef87SMatthew G Knepley 
3293a19ef87SMatthew G Knepley   PetscFunctionBegin;
3308865f1eaSKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
3313a19ef87SMatthew G Knepley   ierr = DMDestroy(&mesh->dmCoarse);CHKERRQ(ierr);
3323a19ef87SMatthew G Knepley   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
3333a19ef87SMatthew G Knepley   ierr = PetscFree(mesh);CHKERRQ(ierr);
3343a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3353a19ef87SMatthew G Knepley }
3363a19ef87SMatthew G Knepley 
3373a19ef87SMatthew G Knepley PetscErrorCode DMSetUp_Patch(DM dm)
3383a19ef87SMatthew G Knepley {
3393a19ef87SMatthew G Knepley   DM_Patch       *mesh = (DM_Patch*) dm->data;
3403a19ef87SMatthew G Knepley   PetscErrorCode ierr;
3413a19ef87SMatthew G Knepley 
3423a19ef87SMatthew G Knepley   PetscFunctionBegin;
3433a19ef87SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
344392fa6c6SMatthew G Knepley   ierr = DMSetUp(mesh->dmCoarse);CHKERRQ(ierr);
3453a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3463a19ef87SMatthew G Knepley }
3473a19ef87SMatthew G Knepley 
3483a19ef87SMatthew G Knepley PetscErrorCode DMCreateGlobalVector_Patch(DM dm, Vec *g)
3493a19ef87SMatthew G Knepley {
3503a19ef87SMatthew G Knepley   DM_Patch       *mesh = (DM_Patch*) dm->data;
3513a19ef87SMatthew G Knepley   PetscErrorCode ierr;
3523a19ef87SMatthew G Knepley 
3533a19ef87SMatthew G Knepley   PetscFunctionBegin;
3543a19ef87SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
355392fa6c6SMatthew G Knepley   ierr = DMCreateGlobalVector(mesh->dmCoarse, g);CHKERRQ(ierr);
3563a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3573a19ef87SMatthew G Knepley }
3583a19ef87SMatthew G Knepley 
3593a19ef87SMatthew G Knepley PetscErrorCode DMCreateLocalVector_Patch(DM dm, Vec *l)
3603a19ef87SMatthew G Knepley {
3613a19ef87SMatthew G Knepley   DM_Patch       *mesh = (DM_Patch*) dm->data;
3623a19ef87SMatthew G Knepley   PetscErrorCode ierr;
3633a19ef87SMatthew G Knepley 
3643a19ef87SMatthew G Knepley   PetscFunctionBegin;
3653a19ef87SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
366392fa6c6SMatthew G Knepley   ierr = DMCreateLocalVector(mesh->dmCoarse, l);CHKERRQ(ierr);
3673a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3683a19ef87SMatthew G Knepley }
3693a19ef87SMatthew G Knepley 
370276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Patch(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
3713a19ef87SMatthew G Knepley {
37282f516ccSBarry Smith   SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Tell me to code this");
3733a19ef87SMatthew G Knepley }
374392fa6c6SMatthew G Knepley 
375392fa6c6SMatthew G Knepley PetscErrorCode DMPatchGetCoarse(DM dm, DM *dmCoarse)
376392fa6c6SMatthew G Knepley {
377392fa6c6SMatthew G Knepley   DM_Patch *mesh = (DM_Patch*) dm->data;
378392fa6c6SMatthew G Knepley 
379392fa6c6SMatthew G Knepley   PetscFunctionBegin;
380392fa6c6SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
381392fa6c6SMatthew G Knepley   *dmCoarse = mesh->dmCoarse;
382392fa6c6SMatthew G Knepley   PetscFunctionReturn(0);
383392fa6c6SMatthew G Knepley }
384392fa6c6SMatthew G Knepley 
385392fa6c6SMatthew G Knepley PetscErrorCode DMPatchGetPatchSize(DM dm, MatStencil *patchSize)
386392fa6c6SMatthew G Knepley {
387392fa6c6SMatthew G Knepley   DM_Patch *mesh = (DM_Patch*) dm->data;
388392fa6c6SMatthew G Knepley 
389392fa6c6SMatthew G Knepley   PetscFunctionBegin;
390392fa6c6SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
391392fa6c6SMatthew G Knepley   PetscValidPointer(patchSize, 2);
392392fa6c6SMatthew G Knepley   *patchSize = mesh->patchSize;
393392fa6c6SMatthew G Knepley   PetscFunctionReturn(0);
394392fa6c6SMatthew G Knepley }
395392fa6c6SMatthew G Knepley 
396392fa6c6SMatthew G Knepley PetscErrorCode DMPatchSetPatchSize(DM dm, MatStencil patchSize)
397392fa6c6SMatthew G Knepley {
398392fa6c6SMatthew G Knepley   DM_Patch *mesh = (DM_Patch*) dm->data;
399392fa6c6SMatthew G Knepley 
400392fa6c6SMatthew G Knepley   PetscFunctionBegin;
401392fa6c6SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
402392fa6c6SMatthew G Knepley   mesh->patchSize = patchSize;
403392fa6c6SMatthew G Knepley   PetscFunctionReturn(0);
404392fa6c6SMatthew G Knepley }
405bb71ef15SMatthew G Knepley 
406bb71ef15SMatthew G Knepley PetscErrorCode DMPatchGetCommSize(DM dm, MatStencil *commSize)
407bb71ef15SMatthew G Knepley {
408bb71ef15SMatthew G Knepley   DM_Patch *mesh = (DM_Patch*) dm->data;
409bb71ef15SMatthew G Knepley 
410bb71ef15SMatthew G Knepley   PetscFunctionBegin;
411bb71ef15SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
412bb71ef15SMatthew G Knepley   PetscValidPointer(commSize, 2);
413bb71ef15SMatthew G Knepley   *commSize = mesh->commSize;
414bb71ef15SMatthew G Knepley   PetscFunctionReturn(0);
415bb71ef15SMatthew G Knepley }
416bb71ef15SMatthew G Knepley 
417bb71ef15SMatthew G Knepley PetscErrorCode DMPatchSetCommSize(DM dm, MatStencil commSize)
418bb71ef15SMatthew G Knepley {
419bb71ef15SMatthew G Knepley   DM_Patch *mesh = (DM_Patch*) dm->data;
420bb71ef15SMatthew G Knepley 
421bb71ef15SMatthew G Knepley   PetscFunctionBegin;
422bb71ef15SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
423bb71ef15SMatthew G Knepley   mesh->commSize = commSize;
424bb71ef15SMatthew G Knepley   PetscFunctionReturn(0);
425bb71ef15SMatthew G Knepley }
426