xref: /petsc/src/dm/impls/patch/patch.c (revision 3b5e53cdb169e94712b800a2d1c9e10766bebf6e)
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 
2159583ba0SMatthew G Knepley /*
2259583ba0SMatthew G Knepley   DMPatchZoom - Create a version of the coarse patch (identified by rank) with halo on communicator commz
2359583ba0SMatthew G Knepley 
24d083f849SBarry Smith   Collective on dm
25a487c981SJed Brown 
2659583ba0SMatthew G Knepley   Input Parameters:
2759583ba0SMatthew G Knepley   + dm - the DM
2859583ba0SMatthew G Knepley   . rank - the rank which holds the given patch
2959583ba0SMatthew G Knepley   - commz - the new communicator for the patch
3059583ba0SMatthew G Knepley 
3159583ba0SMatthew G Knepley   Output Parameters:
3259583ba0SMatthew G Knepley   + dmz  - the patch DM
3359583ba0SMatthew G Knepley   . sfz  - the PetscSF mapping the patch+halo to the zoomed version
3459583ba0SMatthew G Knepley   . sfzr - the PetscSF mapping the patch to the restricted zoomed version
3559583ba0SMatthew G Knepley 
3659583ba0SMatthew G Knepley   Level: intermediate
3759583ba0SMatthew G Knepley 
3859583ba0SMatthew G Knepley   Note: All processes in commz should have the same rank (could autosplit comm)
3959583ba0SMatthew G Knepley 
4059583ba0SMatthew G Knepley .seealso: DMPatchSolve()
4159583ba0SMatthew G Knepley */
42bb71ef15SMatthew G Knepley PetscErrorCode DMPatchZoom(DM dm, Vec X, MatStencil lower, MatStencil upper, MPI_Comm commz, DM *dmz, PetscSF *sfz, PetscSF *sfzr)
4359583ba0SMatthew G Knepley {
4459583ba0SMatthew G Knepley   DMDAStencilType st;
45bd1050a3SMatthew G Knepley   MatStencil      blower, bupper, loclower, locupper;
46bd1050a3SMatthew G Knepley   IS              is;
47bd1050a3SMatthew G Knepley   const PetscInt  *ranges, *indices;
480298fd71SBarry Smith   PetscInt        *localPoints  = NULL;
490298fd71SBarry Smith   PetscSFNode     *remotePoints = NULL;
50392fa6c6SMatthew G Knepley   PetscInt        dim, dof;
51bd1050a3SMatthew G Knepley   PetscInt        M, N, P, rM, rN, rP, halo = 1, sxb, syb, szb, sxr, syr, szr, exr, eyr, ezr, mxb, myb, mzb, i, j, k, q;
52bd1050a3SMatthew G Knepley   PetscMPIInt     size;
53*3b5e53cdSSajid Ali   PetscBool       patchis_offproc = PETSC_TRUE;
5459583ba0SMatthew G Knepley   PetscErrorCode  ierr;
5559583ba0SMatthew G Knepley 
5659583ba0SMatthew G Knepley   PetscFunctionBegin;
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);
88dcca6d9dSJed Brown     ierr = PetscMalloc2(rM*rN*rP,&localPoints,rM*rN*rP,&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 */
94077aedafSJed Brown   ierr = VecGetOwnershipRanges(X,&ranges);CHKERRQ(ierr);
958865f1eaSKarl Rupp 
96bd1050a3SMatthew G Knepley   loclower.i = blower.i + sxr; locupper.i = blower.i + exr;
97bd1050a3SMatthew G Knepley   loclower.j = blower.j + syr; locupper.j = blower.j + eyr;
98bd1050a3SMatthew G Knepley   loclower.k = blower.k + szr; locupper.k = blower.k + ezr;
998865f1eaSKarl Rupp 
100*3b5e53cdSSajid Ali   ierr = DMDACreatePatchIS(dm, &loclower, &locupper, &is, patchis_offproc);CHKERRQ(ierr);
101bd1050a3SMatthew G Knepley   ierr = ISGetIndices(is, &indices);CHKERRQ(ierr);
1028865f1eaSKarl Rupp 
103392fa6c6SMatthew G Knepley   q = 0;
104bd1050a3SMatthew G Knepley   for (k = szb; k < szb+mzb; ++k) {
105bd1050a3SMatthew G Knepley     if ((k < szr) || (k >= ezr)) continue;
106bd1050a3SMatthew G Knepley     for (j = syb; j < syb+myb; ++j) {
107bd1050a3SMatthew G Knepley       if ((j < syr) || (j >= eyr)) continue;
108bd1050a3SMatthew G Knepley       for (i = sxb; i < sxb+mxb; ++i) {
109bd1050a3SMatthew G Knepley         const PetscInt lp = ((k-szb)*rN + (j-syb))*rM + i-sxb;
110bd1050a3SMatthew G Knepley         PetscInt       r;
111392fa6c6SMatthew G Knepley 
112bd1050a3SMatthew G Knepley         if ((i < sxr) || (i >= exr)) continue;
113392fa6c6SMatthew G Knepley         localPoints[q]        = lp;
114bd1050a3SMatthew G Knepley         ierr = PetscFindInt(indices[q], size+1, ranges, &r);CHKERRQ(ierr);
1158865f1eaSKarl Rupp 
116bd1050a3SMatthew G Knepley         remotePoints[q].rank  = r < 0 ? -(r+1) - 1 : r;
117bd1050a3SMatthew G Knepley         remotePoints[q].index = indices[q] - ranges[remotePoints[q].rank];
118bd1050a3SMatthew G Knepley         ++q;
119392fa6c6SMatthew G Knepley       }
120392fa6c6SMatthew G Knepley     }
121392fa6c6SMatthew G Knepley   }
122bd1050a3SMatthew G Knepley   ierr = ISRestoreIndices(is, &indices);CHKERRQ(ierr);
123bd1050a3SMatthew G Knepley   ierr = ISDestroy(&is);CHKERRQ(ierr);
12482f516ccSBarry Smith   ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm), sfzr);CHKERRQ(ierr);
125bd1050a3SMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) *sfzr, "Restricted Map");CHKERRQ(ierr);
126392fa6c6SMatthew G Knepley   ierr = PetscSFSetGraph(*sfzr, M*N*P, q, localPoints, PETSC_COPY_VALUES, remotePoints, PETSC_COPY_VALUES);CHKERRQ(ierr);
127392fa6c6SMatthew G Knepley 
128392fa6c6SMatthew G Knepley   /* Create SF for buffered map */
129bd1050a3SMatthew G Knepley   loclower.i = blower.i + sxb; locupper.i = blower.i + sxb+mxb;
130bd1050a3SMatthew G Knepley   loclower.j = blower.j + syb; locupper.j = blower.j + syb+myb;
131bd1050a3SMatthew G Knepley   loclower.k = blower.k + szb; locupper.k = blower.k + szb+mzb;
1328865f1eaSKarl Rupp 
133*3b5e53cdSSajid Ali   ierr = DMDACreatePatchIS(dm, &loclower, &locupper, &is, patchis_offproc);CHKERRQ(ierr);
134bd1050a3SMatthew G Knepley   ierr = ISGetIndices(is, &indices);CHKERRQ(ierr);
1358865f1eaSKarl Rupp 
136392fa6c6SMatthew G Knepley   q = 0;
137392fa6c6SMatthew G Knepley   for (k = szb; k < szb+mzb; ++k) {
138392fa6c6SMatthew G Knepley     for (j = syb; j < syb+myb; ++j) {
139392fa6c6SMatthew G Knepley       for (i = sxb; i < sxb+mxb; ++i, ++q) {
140bd1050a3SMatthew G Knepley         PetscInt r;
141392fa6c6SMatthew G Knepley 
142bd1050a3SMatthew G Knepley         localPoints[q]        = q;
143bd1050a3SMatthew G Knepley         ierr = PetscFindInt(indices[q], size+1, ranges, &r);CHKERRQ(ierr);
144bd1050a3SMatthew G Knepley         remotePoints[q].rank  = r < 0 ? -(r+1) - 1 : r;
145bd1050a3SMatthew G Knepley         remotePoints[q].index = indices[q] - ranges[remotePoints[q].rank];
146392fa6c6SMatthew G Knepley       }
147392fa6c6SMatthew G Knepley     }
148392fa6c6SMatthew G Knepley   }
149bd1050a3SMatthew G Knepley   ierr = ISRestoreIndices(is, &indices);CHKERRQ(ierr);
150bd1050a3SMatthew G Knepley   ierr = ISDestroy(&is);CHKERRQ(ierr);
15182f516ccSBarry Smith   ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm), sfz);CHKERRQ(ierr);
152bd1050a3SMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) *sfz, "Buffered Map");CHKERRQ(ierr);
153392fa6c6SMatthew G Knepley   ierr = PetscSFSetGraph(*sfz, M*N*P, q, localPoints, PETSC_COPY_VALUES, remotePoints, PETSC_COPY_VALUES);CHKERRQ(ierr);
154392fa6c6SMatthew G Knepley 
155392fa6c6SMatthew G Knepley   ierr = PetscFree2(localPoints, remotePoints);CHKERRQ(ierr);
15659583ba0SMatthew G Knepley   PetscFunctionReturn(0);
15759583ba0SMatthew G Knepley }
15859583ba0SMatthew G Knepley 
159bd1050a3SMatthew G Knepley typedef enum {PATCH_COMM_TYPE_WORLD = 0, PATCH_COMM_TYPE_SELF = 1} PatchCommType;
160bd1050a3SMatthew G Knepley 
16159583ba0SMatthew G Knepley PetscErrorCode DMPatchSolve(DM dm)
16259583ba0SMatthew G Knepley {
16382f516ccSBarry Smith   MPI_Comm       comm;
164bb71ef15SMatthew G Knepley   MPI_Comm       commz;
165bb71ef15SMatthew G Knepley   DM             dmc;
166392fa6c6SMatthew G Knepley   PetscSF        sfz, sfzr;
167bb71ef15SMatthew G Knepley   Vec            XC;
168bb71ef15SMatthew G Knepley   MatStencil     patchSize, commSize, gridRank, lower, upper;
169bb71ef15SMatthew G Knepley   PetscInt       M, N, P, i, j, k, l, m, n, p = 0;
170bb71ef15SMatthew G Knepley   PetscMPIInt    rank, size;
171bb71ef15SMatthew G Knepley   PetscInt       debug = 0;
17259583ba0SMatthew G Knepley   PetscErrorCode ierr;
17359583ba0SMatthew G Knepley 
17459583ba0SMatthew G Knepley   PetscFunctionBegin;
17582f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
176bb71ef15SMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
177bb71ef15SMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
178bd1050a3SMatthew G Knepley   ierr = DMPatchGetCoarse(dm, &dmc);CHKERRQ(ierr);
179392fa6c6SMatthew G Knepley   ierr = DMPatchGetPatchSize(dm, &patchSize);CHKERRQ(ierr);
180bb71ef15SMatthew G Knepley   ierr = DMPatchGetCommSize(dm, &commSize);CHKERRQ(ierr);
181bb71ef15SMatthew G Knepley   ierr = DMPatchGetCommSize(dm, &commSize);CHKERRQ(ierr);
182bd1050a3SMatthew G Knepley   ierr = DMGetGlobalVector(dmc, &XC);CHKERRQ(ierr);
183ea78f98cSLisandro Dalcin   ierr = DMDAGetInfo(dmc, NULL, &M, &N, &P, &l, &m, &n, NULL,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
184bb71ef15SMatthew G Knepley   M    = PetscMax(M, 1); l = PetscMax(l, 1);
185bb71ef15SMatthew G Knepley   N    = PetscMax(N, 1); m = PetscMax(m, 1);
186bb71ef15SMatthew G Knepley   P    = PetscMax(P, 1); n = PetscMax(n, 1);
1878865f1eaSKarl Rupp 
188bb71ef15SMatthew G Knepley   gridRank.i = rank       % l;
189bb71ef15SMatthew G Knepley   gridRank.j = rank/l     % m;
190bb71ef15SMatthew G Knepley   gridRank.k = rank/(l*m) % n;
1918865f1eaSKarl Rupp 
192bb71ef15SMatthew G Knepley   if (commSize.i*commSize.j*commSize.k == size || commSize.i*commSize.j*commSize.k == 0) {
193bb71ef15SMatthew G Knepley     commSize.i = l; commSize.j = m; commSize.k = n;
194bb71ef15SMatthew G Knepley     commz      = comm;
195bb71ef15SMatthew G Knepley   } else if (commSize.i*commSize.j*commSize.k == 1) {
196bb71ef15SMatthew G Knepley     commz = PETSC_COMM_SELF;
197bb71ef15SMatthew G Knepley   } else {
198bb71ef15SMatthew G Knepley     const PetscMPIInt newComm = ((gridRank.k/commSize.k)*(m/commSize.j) + gridRank.j/commSize.j)*(l/commSize.i) + (gridRank.i/commSize.i);
199bb71ef15SMatthew G Knepley     const PetscMPIInt newRank = ((gridRank.k%commSize.k)*commSize.j     + gridRank.j%commSize.j)*commSize.i     + (gridRank.i%commSize.i);
200bb71ef15SMatthew G Knepley 
201bb71ef15SMatthew G Knepley     ierr = MPI_Comm_split(comm, newComm, newRank, &commz);CHKERRQ(ierr);
202bf336ccaSMatthew 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);}
203bb71ef15SMatthew G Knepley   }
204bb71ef15SMatthew G Knepley   /*
205bb71ef15SMatthew G Knepley    Assumptions:
206bb71ef15SMatthew G Knepley      - patchSize divides gridSize
207bb71ef15SMatthew G Knepley      - commSize divides gridSize
208bb71ef15SMatthew G Knepley      - commSize divides l,m,n
209bb71ef15SMatthew G Knepley    Ignore multiple patches per rank for now
210bb71ef15SMatthew G Knepley 
211bb71ef15SMatthew G Knepley    Multiple ranks per patch:
212bb71ef15SMatthew G Knepley      - l,m,n divides patchSize
213bb71ef15SMatthew G Knepley      - commSize divides patchSize
214bb71ef15SMatthew G Knepley    */
215392fa6c6SMatthew G Knepley   for (k = 0; k < P; k += PetscMax(patchSize.k, 1)) {
216392fa6c6SMatthew G Knepley     for (j = 0; j < N; j += PetscMax(patchSize.j, 1)) {
217392fa6c6SMatthew G Knepley       for (i = 0; i < M; i += PetscMax(patchSize.i, 1), ++p) {
218bb71ef15SMatthew G Knepley         MPI_Comm commp = MPI_COMM_NULL;
2190298fd71SBarry Smith         DM       dmz   = NULL;
2202f857c34SHong Zhang #if 0
2210298fd71SBarry Smith         DM  dmf     = NULL;
2220298fd71SBarry Smith         Mat interpz = NULL;
2232f857c34SHong Zhang #endif
2240298fd71SBarry Smith         Vec         XZ       = NULL;
2250298fd71SBarry Smith         PetscScalar *xcarray = NULL;
2260298fd71SBarry Smith         PetscScalar *xzarray = NULL;
227bb87e006SMatthew G Knepley 
228bb71ef15SMatthew G Knepley         if ((gridRank.k/commSize.k == p/(l/commSize.i * m/commSize.j) % n/commSize.k) &&
229bb71ef15SMatthew G Knepley             (gridRank.j/commSize.j == p/(l/commSize.i)                % m/commSize.j) &&
230bb71ef15SMatthew G Knepley             (gridRank.i/commSize.i == p                               % l/commSize.i)) {
231bb71ef15SMatthew G Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "Rank %d is accepting Patch %d\n", rank, p);CHKERRQ(ierr);}
232bb71ef15SMatthew G Knepley           commp = commz;
233bb71ef15SMatthew G Knepley         }
234bb87e006SMatthew G Knepley         /* Zoom to coarse patch */
235392fa6c6SMatthew G Knepley         lower.i = i; lower.j = j; lower.k = k;
236392fa6c6SMatthew G Knepley         upper.i = i + patchSize.i; upper.j = j + patchSize.j; upper.k = k + patchSize.k;
237bb71ef15SMatthew G Knepley         ierr    = DMPatchZoom(dmc, XC, lower, upper, commp, &dmz, &sfz, &sfzr);CHKERRQ(ierr);
238da80777bSKarl Rupp         lower.c = 0; /* initialize member, otherwise compiler issues warnings */
239da80777bSKarl Rupp         upper.c = 0; /* initialize member, otherwise compiler issues warnings */
240bb87e006SMatthew G Knepley         /* Debug */
241392fa6c6SMatthew G Knepley         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);
242bb71ef15SMatthew G Knepley         if (dmz) {ierr = DMView(dmz, PETSC_VIEWER_STDOUT_(commz));CHKERRQ(ierr);}
243bb71ef15SMatthew G Knepley         ierr = PetscSFView(sfz,  PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr);
244bb71ef15SMatthew G Knepley         ierr = PetscSFView(sfzr, PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr);
24559583ba0SMatthew G Knepley         /* Scatter Xcoarse -> Xzoom */
246bb71ef15SMatthew G Knepley         if (dmz) {ierr = DMGetGlobalVector(dmz, &XZ);CHKERRQ(ierr);}
247bb71ef15SMatthew G Knepley         if (XZ)  {ierr = VecGetArray(XZ, &xzarray);CHKERRQ(ierr);}
248bd1050a3SMatthew G Knepley         ierr = VecGetArray(XC, &xcarray);CHKERRQ(ierr);
24956668867SJed Brown         ierr = PetscSFBcastBegin(sfz, MPIU_SCALAR, xcarray, xzarray);CHKERRQ(ierr);
25056668867SJed Brown         ierr = PetscSFBcastEnd(sfz, MPIU_SCALAR, xcarray, xzarray);CHKERRQ(ierr);
251bd1050a3SMatthew G Knepley         ierr = VecRestoreArray(XC, &xcarray);CHKERRQ(ierr);
252bb71ef15SMatthew G Knepley         if (XZ)  {ierr = VecRestoreArray(XZ, &xzarray);CHKERRQ(ierr);}
253bd1050a3SMatthew G Knepley #if 0
25459583ba0SMatthew G Knepley         /* Interpolate Xzoom -> Xfine, note that this may be on subcomms */
25559583ba0SMatthew G Knepley         ierr = DMRefine(dmz, MPI_COMM_NULL, &dmf);CHKERRQ(ierr);
2560298fd71SBarry Smith         ierr = DMCreateInterpolation(dmz, dmf, &interpz, NULL);CHKERRQ(ierr);
25759583ba0SMatthew G Knepley         ierr = DMInterpolate(dmz, interpz, dmf);CHKERRQ(ierr);
25859583ba0SMatthew G Knepley         /* Smooth Xfine using two-step smoother, normal smoother plus Kaczmarz---moves back and forth from dmzoom to dmfine */
25959583ba0SMatthew G Knepley         /* Compute residual Rfine */
26059583ba0SMatthew G Knepley         /* Restrict Rfine to Rzoom_restricted */
261bd1050a3SMatthew G Knepley #endif
26259583ba0SMatthew G Knepley         /* Scatter Rzoom_restricted -> Rcoarse_restricted */
263bb71ef15SMatthew G Knepley         if (XZ)  {ierr = VecGetArray(XZ, &xzarray);CHKERRQ(ierr);}
264bd1050a3SMatthew G Knepley         ierr = VecGetArray(XC, &xcarray);CHKERRQ(ierr);
265a9b180a6SBarry Smith         ierr = PetscSFReduceBegin(sfzr, MPIU_SCALAR, xzarray, xcarray, MPIU_SUM);CHKERRQ(ierr);
266a9b180a6SBarry Smith         ierr = PetscSFReduceEnd(sfzr, MPIU_SCALAR, xzarray, xcarray, MPIU_SUM);CHKERRQ(ierr);
267bd1050a3SMatthew G Knepley         ierr = VecRestoreArray(XC, &xcarray);CHKERRQ(ierr);
268bb71ef15SMatthew G Knepley         if (XZ)  {ierr = VecRestoreArray(XZ, &xzarray);CHKERRQ(ierr);}
269bb71ef15SMatthew G Knepley         if (dmz) {ierr = DMRestoreGlobalVector(dmz, &XZ);CHKERRQ(ierr);}
27059583ba0SMatthew G Knepley         /* Compute global residual Rcoarse */
27159583ba0SMatthew G Knepley         /* TauCoarse = Rcoarse - Rcoarse_restricted */
272bb87e006SMatthew G Knepley 
2736fbb21edSJed Brown         ierr = PetscSFDestroy(&sfz);CHKERRQ(ierr);
2746fbb21edSJed Brown         ierr = PetscSFDestroy(&sfzr);CHKERRQ(ierr);
2756fbb21edSJed Brown         ierr = DMDestroy(&dmz);CHKERRQ(ierr);
276a07761baSMatthew G Knepley       }
277392fa6c6SMatthew G Knepley     }
278392fa6c6SMatthew G Knepley   }
279bd1050a3SMatthew G Knepley   ierr = DMRestoreGlobalVector(dmc, &XC);CHKERRQ(ierr);
28059583ba0SMatthew G Knepley   PetscFunctionReturn(0);
28159583ba0SMatthew G Knepley }
28259583ba0SMatthew G Knepley 
2833a19ef87SMatthew G Knepley PetscErrorCode DMPatchView_Ascii(DM dm, PetscViewer viewer)
2843a19ef87SMatthew G Knepley {
2853a19ef87SMatthew G Knepley   DM_Patch          *mesh = (DM_Patch*) dm->data;
2863a19ef87SMatthew G Knepley   PetscViewerFormat format;
287392fa6c6SMatthew G Knepley   const char        *name;
2883a19ef87SMatthew G Knepley   PetscErrorCode    ierr;
2893a19ef87SMatthew G Knepley 
2903a19ef87SMatthew G Knepley   PetscFunctionBegin;
2913a19ef87SMatthew G Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
2923a19ef87SMatthew G Knepley   /* if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) */
293392fa6c6SMatthew G Knepley   ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
294392fa6c6SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Patch DM %s\n", name);CHKERRQ(ierr);
2953a19ef87SMatthew G Knepley   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2963a19ef87SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Coarse DM\n");CHKERRQ(ierr);
2973a19ef87SMatthew G Knepley   ierr = DMView(mesh->dmCoarse, viewer);CHKERRQ(ierr);
2983a19ef87SMatthew G Knepley   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2993a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3003a19ef87SMatthew G Knepley }
3013a19ef87SMatthew G Knepley 
3023a19ef87SMatthew G Knepley PetscErrorCode DMView_Patch(DM dm, PetscViewer viewer)
3033a19ef87SMatthew G Knepley {
3043a19ef87SMatthew G Knepley   PetscBool      iascii, isbinary;
3053a19ef87SMatthew G Knepley   PetscErrorCode ierr;
3063a19ef87SMatthew G Knepley 
3073a19ef87SMatthew G Knepley   PetscFunctionBegin;
3083a19ef87SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3093a19ef87SMatthew G Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
3103a19ef87SMatthew G Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
3113a19ef87SMatthew G Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);CHKERRQ(ierr);
3123a19ef87SMatthew G Knepley   if (iascii) {
3133a19ef87SMatthew G Knepley     ierr = DMPatchView_Ascii(dm, viewer);CHKERRQ(ierr);
3143a19ef87SMatthew G Knepley #if 0
3153a19ef87SMatthew G Knepley   } else if (isbinary) {
3163a19ef87SMatthew G Knepley     ierr = DMPatchView_Binary(dm, viewer);CHKERRQ(ierr);
3173a19ef87SMatthew G Knepley #endif
31811aeaf0aSBarry Smith   }
3193a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3203a19ef87SMatthew G Knepley }
3213a19ef87SMatthew G Knepley 
3223a19ef87SMatthew G Knepley PetscErrorCode DMDestroy_Patch(DM dm)
3233a19ef87SMatthew G Knepley {
3243a19ef87SMatthew G Knepley   DM_Patch       *mesh = (DM_Patch*) dm->data;
3253a19ef87SMatthew G Knepley   PetscErrorCode ierr;
3263a19ef87SMatthew G Knepley 
3273a19ef87SMatthew G Knepley   PetscFunctionBegin;
3288865f1eaSKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
3293a19ef87SMatthew G Knepley   ierr = DMDestroy(&mesh->dmCoarse);CHKERRQ(ierr);
3303a19ef87SMatthew G Knepley   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
3313a19ef87SMatthew G Knepley   ierr = PetscFree(mesh);CHKERRQ(ierr);
3323a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3333a19ef87SMatthew G Knepley }
3343a19ef87SMatthew G Knepley 
3353a19ef87SMatthew G Knepley PetscErrorCode DMSetUp_Patch(DM dm)
3363a19ef87SMatthew G Knepley {
3373a19ef87SMatthew G Knepley   DM_Patch       *mesh = (DM_Patch*) dm->data;
3383a19ef87SMatthew G Knepley   PetscErrorCode ierr;
3393a19ef87SMatthew G Knepley 
3403a19ef87SMatthew G Knepley   PetscFunctionBegin;
3413a19ef87SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
342392fa6c6SMatthew G Knepley   ierr = DMSetUp(mesh->dmCoarse);CHKERRQ(ierr);
3433a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3443a19ef87SMatthew G Knepley }
3453a19ef87SMatthew G Knepley 
3463a19ef87SMatthew G Knepley PetscErrorCode DMCreateGlobalVector_Patch(DM dm, Vec *g)
3473a19ef87SMatthew G Knepley {
3483a19ef87SMatthew G Knepley   DM_Patch       *mesh = (DM_Patch*) dm->data;
3493a19ef87SMatthew G Knepley   PetscErrorCode ierr;
3503a19ef87SMatthew G Knepley 
3513a19ef87SMatthew G Knepley   PetscFunctionBegin;
3523a19ef87SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
353392fa6c6SMatthew G Knepley   ierr = DMCreateGlobalVector(mesh->dmCoarse, g);CHKERRQ(ierr);
3543a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3553a19ef87SMatthew G Knepley }
3563a19ef87SMatthew G Knepley 
3573a19ef87SMatthew G Knepley PetscErrorCode DMCreateLocalVector_Patch(DM dm, Vec *l)
3583a19ef87SMatthew G Knepley {
3593a19ef87SMatthew G Knepley   DM_Patch       *mesh = (DM_Patch*) dm->data;
3603a19ef87SMatthew G Knepley   PetscErrorCode ierr;
3613a19ef87SMatthew G Knepley 
3623a19ef87SMatthew G Knepley   PetscFunctionBegin;
3633a19ef87SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
364392fa6c6SMatthew G Knepley   ierr = DMCreateLocalVector(mesh->dmCoarse, l);CHKERRQ(ierr);
3653a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3663a19ef87SMatthew G Knepley }
3673a19ef87SMatthew G Knepley 
368276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Patch(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
3693a19ef87SMatthew G Knepley {
37082f516ccSBarry Smith   SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Tell me to code this");
3713a19ef87SMatthew G Knepley }
372392fa6c6SMatthew G Knepley 
373392fa6c6SMatthew G Knepley PetscErrorCode DMPatchGetCoarse(DM dm, DM *dmCoarse)
374392fa6c6SMatthew G Knepley {
375392fa6c6SMatthew G Knepley   DM_Patch *mesh = (DM_Patch*) dm->data;
376392fa6c6SMatthew G Knepley 
377392fa6c6SMatthew G Knepley   PetscFunctionBegin;
378392fa6c6SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
379392fa6c6SMatthew G Knepley   *dmCoarse = mesh->dmCoarse;
380392fa6c6SMatthew G Knepley   PetscFunctionReturn(0);
381392fa6c6SMatthew G Knepley }
382392fa6c6SMatthew G Knepley 
383392fa6c6SMatthew G Knepley PetscErrorCode DMPatchGetPatchSize(DM dm, MatStencil *patchSize)
384392fa6c6SMatthew G Knepley {
385392fa6c6SMatthew G Knepley   DM_Patch *mesh = (DM_Patch*) dm->data;
386392fa6c6SMatthew G Knepley 
387392fa6c6SMatthew G Knepley   PetscFunctionBegin;
388392fa6c6SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
389392fa6c6SMatthew G Knepley   PetscValidPointer(patchSize, 2);
390392fa6c6SMatthew G Knepley   *patchSize = mesh->patchSize;
391392fa6c6SMatthew G Knepley   PetscFunctionReturn(0);
392392fa6c6SMatthew G Knepley }
393392fa6c6SMatthew G Knepley 
394392fa6c6SMatthew G Knepley PetscErrorCode DMPatchSetPatchSize(DM dm, MatStencil patchSize)
395392fa6c6SMatthew G Knepley {
396392fa6c6SMatthew G Knepley   DM_Patch *mesh = (DM_Patch*) dm->data;
397392fa6c6SMatthew G Knepley 
398392fa6c6SMatthew G Knepley   PetscFunctionBegin;
399392fa6c6SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
400392fa6c6SMatthew G Knepley   mesh->patchSize = patchSize;
401392fa6c6SMatthew G Knepley   PetscFunctionReturn(0);
402392fa6c6SMatthew G Knepley }
403bb71ef15SMatthew G Knepley 
404bb71ef15SMatthew G Knepley PetscErrorCode DMPatchGetCommSize(DM dm, MatStencil *commSize)
405bb71ef15SMatthew G Knepley {
406bb71ef15SMatthew G Knepley   DM_Patch *mesh = (DM_Patch*) dm->data;
407bb71ef15SMatthew G Knepley 
408bb71ef15SMatthew G Knepley   PetscFunctionBegin;
409bb71ef15SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
410bb71ef15SMatthew G Knepley   PetscValidPointer(commSize, 2);
411bb71ef15SMatthew G Knepley   *commSize = mesh->commSize;
412bb71ef15SMatthew G Knepley   PetscFunctionReturn(0);
413bb71ef15SMatthew G Knepley }
414bb71ef15SMatthew G Knepley 
415bb71ef15SMatthew G Knepley PetscErrorCode DMPatchSetCommSize(DM dm, MatStencil commSize)
416bb71ef15SMatthew G Knepley {
417bb71ef15SMatthew G Knepley   DM_Patch *mesh = (DM_Patch*) dm->data;
418bb71ef15SMatthew G Knepley 
419bb71ef15SMatthew G Knepley   PetscFunctionBegin;
420bb71ef15SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
421bb71ef15SMatthew G Knepley   mesh->commSize = commSize;
422bb71ef15SMatthew G Knepley   PetscFunctionReturn(0);
423bb71ef15SMatthew G Knepley }
424