xref: /petsc/src/dm/impls/patch/patch.c (revision d083f849a86f1f43e18d534ee43954e2786cb29a)
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 
24*d083f849SBarry 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;
5359583ba0SMatthew G Knepley   PetscErrorCode  ierr;
5459583ba0SMatthew G Knepley 
5559583ba0SMatthew G Knepley   PetscFunctionBegin;
5682f516ccSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
5759583ba0SMatthew G Knepley   /* Create patch DM */
58392fa6c6SMatthew G Knepley   ierr = DMDAGetInfo(dm, &dim, &M, &N, &P, 0,0,0, &dof, 0,0,0,0, &st);CHKERRQ(ierr);
59a07761baSMatthew G Knepley 
60a07761baSMatthew G Knepley   /* Get piece for rank r, expanded by halo */
61392fa6c6SMatthew G Knepley   bupper.i = PetscMin(M, upper.i + halo); blower.i = PetscMax(lower.i - halo, 0);
62392fa6c6SMatthew G Knepley   bupper.j = PetscMin(N, upper.j + halo); blower.j = PetscMax(lower.j - halo, 0);
63392fa6c6SMatthew G Knepley   bupper.k = PetscMin(P, upper.k + halo); blower.k = PetscMax(lower.k - halo, 0);
64392fa6c6SMatthew G Knepley   rM       = bupper.i - blower.i;
65392fa6c6SMatthew G Knepley   rN       = bupper.j - blower.j;
66392fa6c6SMatthew G Knepley   rP       = bupper.k - blower.k;
6759583ba0SMatthew G Knepley 
68bb71ef15SMatthew G Knepley   if (commz != MPI_COMM_NULL) {
6959583ba0SMatthew G Knepley     ierr = DMDACreate(commz, dmz);CHKERRQ(ierr);
70c73cfb54SMatthew G. Knepley     ierr = DMSetDimension(*dmz, dim);CHKERRQ(ierr);
71a07761baSMatthew G Knepley     ierr = DMDASetSizes(*dmz, rM, rN, rP);CHKERRQ(ierr);
7259583ba0SMatthew G Knepley     ierr = DMDASetNumProcs(*dmz, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE);CHKERRQ(ierr);
73bff4a2f0SMatthew G. Knepley     ierr = DMDASetBoundaryType(*dmz, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE);CHKERRQ(ierr);
7459583ba0SMatthew G Knepley     ierr = DMDASetDof(*dmz, dof);CHKERRQ(ierr);
7559583ba0SMatthew G Knepley     ierr = DMDASetStencilType(*dmz, st);CHKERRQ(ierr);
76392fa6c6SMatthew G Knepley     ierr = DMDASetStencilWidth(*dmz, 0);CHKERRQ(ierr);
770298fd71SBarry Smith     ierr = DMDASetOwnershipRanges(*dmz, NULL, NULL, NULL);CHKERRQ(ierr);
7859583ba0SMatthew G Knepley     ierr = DMSetFromOptions(*dmz);CHKERRQ(ierr);
7959583ba0SMatthew G Knepley     ierr = DMSetUp(*dmz);CHKERRQ(ierr);
80bd1050a3SMatthew G Knepley     ierr = DMDAGetCorners(*dmz, &sxb, &syb, &szb, &mxb, &myb, &mzb);CHKERRQ(ierr);
81bd1050a3SMatthew G Knepley     sxr  = PetscMax(sxb,     lower.i - blower.i);
82bd1050a3SMatthew G Knepley     syr  = PetscMax(syb,     lower.j - blower.j);
83bd1050a3SMatthew G Knepley     szr  = PetscMax(szb,     lower.k - blower.k);
84bd1050a3SMatthew G Knepley     exr  = PetscMin(sxb+mxb, upper.i - blower.i);
85bd1050a3SMatthew G Knepley     eyr  = PetscMin(syb+myb, upper.j - blower.j);
86bd1050a3SMatthew G Knepley     ezr  = PetscMin(szb+mzb, upper.k - blower.k);
87dcca6d9dSJed Brown     ierr = PetscMalloc2(rM*rN*rP,&localPoints,rM*rN*rP,&remotePoints);CHKERRQ(ierr);
88bb71ef15SMatthew G Knepley   } else {
89bb71ef15SMatthew G Knepley     sxr = syr = szr = exr = eyr = ezr = sxb = syb = szb = mxb = myb = mzb = 0;
90bb71ef15SMatthew G Knepley   }
91392fa6c6SMatthew G Knepley 
9259583ba0SMatthew G Knepley   /* Create SF for restricted map */
93077aedafSJed Brown   ierr = VecGetOwnershipRanges(X,&ranges);CHKERRQ(ierr);
948865f1eaSKarl Rupp 
95bd1050a3SMatthew G Knepley   loclower.i = blower.i + sxr; locupper.i = blower.i + exr;
96bd1050a3SMatthew G Knepley   loclower.j = blower.j + syr; locupper.j = blower.j + eyr;
97bd1050a3SMatthew G Knepley   loclower.k = blower.k + szr; locupper.k = blower.k + ezr;
988865f1eaSKarl Rupp 
99bd1050a3SMatthew G Knepley   ierr = DMDACreatePatchIS(dm, &loclower, &locupper, &is);CHKERRQ(ierr);
100bd1050a3SMatthew G Knepley   ierr = ISGetIndices(is, &indices);CHKERRQ(ierr);
1018865f1eaSKarl Rupp 
102392fa6c6SMatthew G Knepley   q = 0;
103bd1050a3SMatthew G Knepley   for (k = szb; k < szb+mzb; ++k) {
104bd1050a3SMatthew G Knepley     if ((k < szr) || (k >= ezr)) continue;
105bd1050a3SMatthew G Knepley     for (j = syb; j < syb+myb; ++j) {
106bd1050a3SMatthew G Knepley       if ((j < syr) || (j >= eyr)) continue;
107bd1050a3SMatthew G Knepley       for (i = sxb; i < sxb+mxb; ++i) {
108bd1050a3SMatthew G Knepley         const PetscInt lp = ((k-szb)*rN + (j-syb))*rM + i-sxb;
109bd1050a3SMatthew G Knepley         PetscInt       r;
110392fa6c6SMatthew G Knepley 
111bd1050a3SMatthew G Knepley         if ((i < sxr) || (i >= exr)) continue;
112392fa6c6SMatthew G Knepley         localPoints[q]        = lp;
113bd1050a3SMatthew G Knepley         ierr = PetscFindInt(indices[q], size+1, ranges, &r);CHKERRQ(ierr);
1148865f1eaSKarl Rupp 
115bd1050a3SMatthew G Knepley         remotePoints[q].rank  = r < 0 ? -(r+1) - 1 : r;
116bd1050a3SMatthew G Knepley         remotePoints[q].index = indices[q] - ranges[remotePoints[q].rank];
117bd1050a3SMatthew G Knepley         ++q;
118392fa6c6SMatthew G Knepley       }
119392fa6c6SMatthew G Knepley     }
120392fa6c6SMatthew G Knepley   }
121bd1050a3SMatthew G Knepley   ierr = ISRestoreIndices(is, &indices);CHKERRQ(ierr);
122bd1050a3SMatthew G Knepley   ierr = ISDestroy(&is);CHKERRQ(ierr);
12382f516ccSBarry Smith   ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm), sfzr);CHKERRQ(ierr);
124bd1050a3SMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) *sfzr, "Restricted Map");CHKERRQ(ierr);
125392fa6c6SMatthew G Knepley   ierr = PetscSFSetGraph(*sfzr, M*N*P, q, localPoints, PETSC_COPY_VALUES, remotePoints, PETSC_COPY_VALUES);CHKERRQ(ierr);
126392fa6c6SMatthew G Knepley 
127392fa6c6SMatthew G Knepley   /* Create SF for buffered map */
128bd1050a3SMatthew G Knepley   loclower.i = blower.i + sxb; locupper.i = blower.i + sxb+mxb;
129bd1050a3SMatthew G Knepley   loclower.j = blower.j + syb; locupper.j = blower.j + syb+myb;
130bd1050a3SMatthew G Knepley   loclower.k = blower.k + szb; locupper.k = blower.k + szb+mzb;
1318865f1eaSKarl Rupp 
132bd1050a3SMatthew G Knepley   ierr = DMDACreatePatchIS(dm, &loclower, &locupper, &is);CHKERRQ(ierr);
133bd1050a3SMatthew G Knepley   ierr = ISGetIndices(is, &indices);CHKERRQ(ierr);
1348865f1eaSKarl Rupp 
135392fa6c6SMatthew G Knepley   q = 0;
136392fa6c6SMatthew G Knepley   for (k = szb; k < szb+mzb; ++k) {
137392fa6c6SMatthew G Knepley     for (j = syb; j < syb+myb; ++j) {
138392fa6c6SMatthew G Knepley       for (i = sxb; i < sxb+mxb; ++i, ++q) {
139bd1050a3SMatthew G Knepley         PetscInt r;
140392fa6c6SMatthew G Knepley 
141bd1050a3SMatthew G Knepley         localPoints[q]        = q;
142bd1050a3SMatthew G Knepley         ierr = PetscFindInt(indices[q], size+1, ranges, &r);CHKERRQ(ierr);
143bd1050a3SMatthew G Knepley         remotePoints[q].rank  = r < 0 ? -(r+1) - 1 : r;
144bd1050a3SMatthew G Knepley         remotePoints[q].index = indices[q] - ranges[remotePoints[q].rank];
145392fa6c6SMatthew G Knepley       }
146392fa6c6SMatthew G Knepley     }
147392fa6c6SMatthew G Knepley   }
148bd1050a3SMatthew G Knepley   ierr = ISRestoreIndices(is, &indices);CHKERRQ(ierr);
149bd1050a3SMatthew G Knepley   ierr = ISDestroy(&is);CHKERRQ(ierr);
15082f516ccSBarry Smith   ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm), sfz);CHKERRQ(ierr);
151bd1050a3SMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) *sfz, "Buffered Map");CHKERRQ(ierr);
152392fa6c6SMatthew G Knepley   ierr = PetscSFSetGraph(*sfz, M*N*P, q, localPoints, PETSC_COPY_VALUES, remotePoints, PETSC_COPY_VALUES);CHKERRQ(ierr);
153392fa6c6SMatthew G Knepley 
154392fa6c6SMatthew G Knepley   ierr = PetscFree2(localPoints, remotePoints);CHKERRQ(ierr);
15559583ba0SMatthew G Knepley   PetscFunctionReturn(0);
15659583ba0SMatthew G Knepley }
15759583ba0SMatthew G Knepley 
158bd1050a3SMatthew G Knepley typedef enum {PATCH_COMM_TYPE_WORLD = 0, PATCH_COMM_TYPE_SELF = 1} PatchCommType;
159bd1050a3SMatthew G Knepley 
16059583ba0SMatthew G Knepley PetscErrorCode DMPatchSolve(DM dm)
16159583ba0SMatthew G Knepley {
16282f516ccSBarry Smith   MPI_Comm       comm;
163bb71ef15SMatthew G Knepley   MPI_Comm       commz;
164bb71ef15SMatthew G Knepley   DM             dmc;
165392fa6c6SMatthew G Knepley   PetscSF        sfz, sfzr;
166bb71ef15SMatthew G Knepley   Vec            XC;
167bb71ef15SMatthew G Knepley   MatStencil     patchSize, commSize, gridRank, lower, upper;
168bb71ef15SMatthew G Knepley   PetscInt       M, N, P, i, j, k, l, m, n, p = 0;
169bb71ef15SMatthew G Knepley   PetscMPIInt    rank, size;
170bb71ef15SMatthew G Knepley   PetscInt       debug = 0;
17159583ba0SMatthew G Knepley   PetscErrorCode ierr;
17259583ba0SMatthew G Knepley 
17359583ba0SMatthew G Knepley   PetscFunctionBegin;
17482f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
175bb71ef15SMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
176bb71ef15SMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
177bd1050a3SMatthew G Knepley   ierr = DMPatchGetCoarse(dm, &dmc);CHKERRQ(ierr);
178392fa6c6SMatthew G Knepley   ierr = DMPatchGetPatchSize(dm, &patchSize);CHKERRQ(ierr);
179bb71ef15SMatthew G Knepley   ierr = DMPatchGetCommSize(dm, &commSize);CHKERRQ(ierr);
180bb71ef15SMatthew G Knepley   ierr = DMPatchGetCommSize(dm, &commSize);CHKERRQ(ierr);
181bd1050a3SMatthew G Knepley   ierr = DMGetGlobalVector(dmc, &XC);CHKERRQ(ierr);
182bb71ef15SMatthew G Knepley   ierr = DMDAGetInfo(dmc, 0, &M, &N, &P, &l, &m, &n, 0,0,0,0,0,0);CHKERRQ(ierr);
183bb71ef15SMatthew G Knepley   M    = PetscMax(M, 1); l = PetscMax(l, 1);
184bb71ef15SMatthew G Knepley   N    = PetscMax(N, 1); m = PetscMax(m, 1);
185bb71ef15SMatthew G Knepley   P    = PetscMax(P, 1); n = PetscMax(n, 1);
1868865f1eaSKarl Rupp 
187bb71ef15SMatthew G Knepley   gridRank.i = rank       % l;
188bb71ef15SMatthew G Knepley   gridRank.j = rank/l     % m;
189bb71ef15SMatthew G Knepley   gridRank.k = rank/(l*m) % n;
1908865f1eaSKarl Rupp 
191bb71ef15SMatthew G Knepley   if (commSize.i*commSize.j*commSize.k == size || commSize.i*commSize.j*commSize.k == 0) {
192bb71ef15SMatthew G Knepley     commSize.i = l; commSize.j = m; commSize.k = n;
193bb71ef15SMatthew G Knepley     commz      = comm;
194bb71ef15SMatthew G Knepley   } else if (commSize.i*commSize.j*commSize.k == 1) {
195bb71ef15SMatthew G Knepley     commz = PETSC_COMM_SELF;
196bb71ef15SMatthew G Knepley   } else {
197bb71ef15SMatthew G Knepley     const PetscMPIInt newComm = ((gridRank.k/commSize.k)*(m/commSize.j) + gridRank.j/commSize.j)*(l/commSize.i) + (gridRank.i/commSize.i);
198bb71ef15SMatthew G Knepley     const PetscMPIInt newRank = ((gridRank.k%commSize.k)*commSize.j     + gridRank.j%commSize.j)*commSize.i     + (gridRank.i%commSize.i);
199bb71ef15SMatthew G Knepley 
200bb71ef15SMatthew G Knepley     ierr = MPI_Comm_split(comm, newComm, newRank, &commz);CHKERRQ(ierr);
201bf336ccaSMatthew 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);}
202bb71ef15SMatthew G Knepley   }
203bb71ef15SMatthew G Knepley   /*
204bb71ef15SMatthew G Knepley    Assumptions:
205bb71ef15SMatthew G Knepley      - patchSize divides gridSize
206bb71ef15SMatthew G Knepley      - commSize divides gridSize
207bb71ef15SMatthew G Knepley      - commSize divides l,m,n
208bb71ef15SMatthew G Knepley    Ignore multiple patches per rank for now
209bb71ef15SMatthew G Knepley 
210bb71ef15SMatthew G Knepley    Multiple ranks per patch:
211bb71ef15SMatthew G Knepley      - l,m,n divides patchSize
212bb71ef15SMatthew G Knepley      - commSize divides patchSize
213bb71ef15SMatthew G Knepley    */
214392fa6c6SMatthew G Knepley   for (k = 0; k < P; k += PetscMax(patchSize.k, 1)) {
215392fa6c6SMatthew G Knepley     for (j = 0; j < N; j += PetscMax(patchSize.j, 1)) {
216392fa6c6SMatthew G Knepley       for (i = 0; i < M; i += PetscMax(patchSize.i, 1), ++p) {
217bb71ef15SMatthew G Knepley         MPI_Comm commp = MPI_COMM_NULL;
2180298fd71SBarry Smith         DM       dmz   = NULL;
2192f857c34SHong Zhang #if 0
2200298fd71SBarry Smith         DM  dmf     = NULL;
2210298fd71SBarry Smith         Mat interpz = NULL;
2222f857c34SHong Zhang #endif
2230298fd71SBarry Smith         Vec         XZ       = NULL;
2240298fd71SBarry Smith         PetscScalar *xcarray = NULL;
2250298fd71SBarry Smith         PetscScalar *xzarray = NULL;
226bb87e006SMatthew G Knepley 
227bb71ef15SMatthew G Knepley         if ((gridRank.k/commSize.k == p/(l/commSize.i * m/commSize.j) % n/commSize.k) &&
228bb71ef15SMatthew G Knepley             (gridRank.j/commSize.j == p/(l/commSize.i)                % m/commSize.j) &&
229bb71ef15SMatthew G Knepley             (gridRank.i/commSize.i == p                               % l/commSize.i)) {
230bb71ef15SMatthew G Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "Rank %d is accepting Patch %d\n", rank, p);CHKERRQ(ierr);}
231bb71ef15SMatthew G Knepley           commp = commz;
232bb71ef15SMatthew G Knepley         }
233bb87e006SMatthew G Knepley         /* Zoom to coarse patch */
234392fa6c6SMatthew G Knepley         lower.i = i; lower.j = j; lower.k = k;
235392fa6c6SMatthew G Knepley         upper.i = i + patchSize.i; upper.j = j + patchSize.j; upper.k = k + patchSize.k;
236bb71ef15SMatthew G Knepley         ierr    = DMPatchZoom(dmc, XC, lower, upper, commp, &dmz, &sfz, &sfzr);CHKERRQ(ierr);
237da80777bSKarl Rupp         lower.c = 0; /* initialize member, otherwise compiler issues warnings */
238da80777bSKarl Rupp         upper.c = 0; /* initialize member, otherwise compiler issues warnings */
239bb87e006SMatthew G Knepley         /* Debug */
240392fa6c6SMatthew 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);
241bb71ef15SMatthew G Knepley         if (dmz) {ierr = DMView(dmz, PETSC_VIEWER_STDOUT_(commz));CHKERRQ(ierr);}
242bb71ef15SMatthew G Knepley         ierr = PetscSFView(sfz,  PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr);
243bb71ef15SMatthew G Knepley         ierr = PetscSFView(sfzr, PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr);
24459583ba0SMatthew G Knepley         /* Scatter Xcoarse -> Xzoom */
245bb71ef15SMatthew G Knepley         if (dmz) {ierr = DMGetGlobalVector(dmz, &XZ);CHKERRQ(ierr);}
246bb71ef15SMatthew G Knepley         if (XZ)  {ierr = VecGetArray(XZ, &xzarray);CHKERRQ(ierr);}
247bd1050a3SMatthew G Knepley         ierr = VecGetArray(XC, &xcarray);CHKERRQ(ierr);
24856668867SJed Brown         ierr = PetscSFBcastBegin(sfz, MPIU_SCALAR, xcarray, xzarray);CHKERRQ(ierr);
24956668867SJed Brown         ierr = PetscSFBcastEnd(sfz, MPIU_SCALAR, xcarray, xzarray);CHKERRQ(ierr);
250bd1050a3SMatthew G Knepley         ierr = VecRestoreArray(XC, &xcarray);CHKERRQ(ierr);
251bb71ef15SMatthew G Knepley         if (XZ)  {ierr = VecRestoreArray(XZ, &xzarray);CHKERRQ(ierr);}
252bd1050a3SMatthew G Knepley #if 0
25359583ba0SMatthew G Knepley         /* Interpolate Xzoom -> Xfine, note that this may be on subcomms */
25459583ba0SMatthew G Knepley         ierr = DMRefine(dmz, MPI_COMM_NULL, &dmf);CHKERRQ(ierr);
2550298fd71SBarry Smith         ierr = DMCreateInterpolation(dmz, dmf, &interpz, NULL);CHKERRQ(ierr);
25659583ba0SMatthew G Knepley         ierr = DMInterpolate(dmz, interpz, dmf);CHKERRQ(ierr);
25759583ba0SMatthew G Knepley         /* Smooth Xfine using two-step smoother, normal smoother plus Kaczmarz---moves back and forth from dmzoom to dmfine */
25859583ba0SMatthew G Knepley         /* Compute residual Rfine */
25959583ba0SMatthew G Knepley         /* Restrict Rfine to Rzoom_restricted */
260bd1050a3SMatthew G Knepley #endif
26159583ba0SMatthew G Knepley         /* Scatter Rzoom_restricted -> Rcoarse_restricted */
262bb71ef15SMatthew G Knepley         if (XZ)  {ierr = VecGetArray(XZ, &xzarray);CHKERRQ(ierr);}
263bd1050a3SMatthew G Knepley         ierr = VecGetArray(XC, &xcarray);CHKERRQ(ierr);
264a9b180a6SBarry Smith         ierr = PetscSFReduceBegin(sfzr, MPIU_SCALAR, xzarray, xcarray, MPIU_SUM);CHKERRQ(ierr);
265a9b180a6SBarry Smith         ierr = PetscSFReduceEnd(sfzr, MPIU_SCALAR, xzarray, xcarray, MPIU_SUM);CHKERRQ(ierr);
266bd1050a3SMatthew G Knepley         ierr = VecRestoreArray(XC, &xcarray);CHKERRQ(ierr);
267bb71ef15SMatthew G Knepley         if (XZ)  {ierr = VecRestoreArray(XZ, &xzarray);CHKERRQ(ierr);}
268bb71ef15SMatthew G Knepley         if (dmz) {ierr = DMRestoreGlobalVector(dmz, &XZ);CHKERRQ(ierr);}
26959583ba0SMatthew G Knepley         /* Compute global residual Rcoarse */
27059583ba0SMatthew G Knepley         /* TauCoarse = Rcoarse - Rcoarse_restricted */
271bb87e006SMatthew G Knepley 
2726fbb21edSJed Brown         ierr = PetscSFDestroy(&sfz);CHKERRQ(ierr);
2736fbb21edSJed Brown         ierr = PetscSFDestroy(&sfzr);CHKERRQ(ierr);
2746fbb21edSJed Brown         ierr = DMDestroy(&dmz);CHKERRQ(ierr);
275a07761baSMatthew G Knepley       }
276392fa6c6SMatthew G Knepley     }
277392fa6c6SMatthew G Knepley   }
278bd1050a3SMatthew G Knepley   ierr = DMRestoreGlobalVector(dmc, &XC);CHKERRQ(ierr);
27959583ba0SMatthew G Knepley   PetscFunctionReturn(0);
28059583ba0SMatthew G Knepley }
28159583ba0SMatthew G Knepley 
2823a19ef87SMatthew G Knepley PetscErrorCode DMPatchView_Ascii(DM dm, PetscViewer viewer)
2833a19ef87SMatthew G Knepley {
2843a19ef87SMatthew G Knepley   DM_Patch          *mesh = (DM_Patch*) dm->data;
2853a19ef87SMatthew G Knepley   PetscViewerFormat format;
286392fa6c6SMatthew G Knepley   const char        *name;
2873a19ef87SMatthew G Knepley   PetscErrorCode    ierr;
2883a19ef87SMatthew G Knepley 
2893a19ef87SMatthew G Knepley   PetscFunctionBegin;
2903a19ef87SMatthew G Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
2913a19ef87SMatthew G Knepley   /* if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) */
292392fa6c6SMatthew G Knepley   ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
293392fa6c6SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Patch DM %s\n", name);CHKERRQ(ierr);
2943a19ef87SMatthew G Knepley   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2953a19ef87SMatthew G Knepley   ierr = PetscViewerASCIIPrintf(viewer, "Coarse DM\n");CHKERRQ(ierr);
2963a19ef87SMatthew G Knepley   ierr = DMView(mesh->dmCoarse, viewer);CHKERRQ(ierr);
2973a19ef87SMatthew G Knepley   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2983a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
2993a19ef87SMatthew G Knepley }
3003a19ef87SMatthew G Knepley 
3013a19ef87SMatthew G Knepley PetscErrorCode DMView_Patch(DM dm, PetscViewer viewer)
3023a19ef87SMatthew G Knepley {
3033a19ef87SMatthew G Knepley   PetscBool      iascii, isbinary;
3043a19ef87SMatthew G Knepley   PetscErrorCode ierr;
3053a19ef87SMatthew G Knepley 
3063a19ef87SMatthew G Knepley   PetscFunctionBegin;
3073a19ef87SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3083a19ef87SMatthew G Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
3093a19ef87SMatthew G Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
3103a19ef87SMatthew G Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);CHKERRQ(ierr);
3113a19ef87SMatthew G Knepley   if (iascii) {
3123a19ef87SMatthew G Knepley     ierr = DMPatchView_Ascii(dm, viewer);CHKERRQ(ierr);
3133a19ef87SMatthew G Knepley #if 0
3143a19ef87SMatthew G Knepley   } else if (isbinary) {
3153a19ef87SMatthew G Knepley     ierr = DMPatchView_Binary(dm, viewer);CHKERRQ(ierr);
3163a19ef87SMatthew G Knepley #endif
31711aeaf0aSBarry Smith   }
3183a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3193a19ef87SMatthew G Knepley }
3203a19ef87SMatthew G Knepley 
3213a19ef87SMatthew G Knepley PetscErrorCode DMDestroy_Patch(DM dm)
3223a19ef87SMatthew G Knepley {
3233a19ef87SMatthew G Knepley   DM_Patch       *mesh = (DM_Patch*) dm->data;
3243a19ef87SMatthew G Knepley   PetscErrorCode ierr;
3253a19ef87SMatthew G Knepley 
3263a19ef87SMatthew G Knepley   PetscFunctionBegin;
3278865f1eaSKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
3283a19ef87SMatthew G Knepley   ierr = DMDestroy(&mesh->dmCoarse);CHKERRQ(ierr);
3293a19ef87SMatthew G Knepley   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
3303a19ef87SMatthew G Knepley   ierr = PetscFree(mesh);CHKERRQ(ierr);
3313a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3323a19ef87SMatthew G Knepley }
3333a19ef87SMatthew G Knepley 
3343a19ef87SMatthew G Knepley PetscErrorCode DMSetUp_Patch(DM dm)
3353a19ef87SMatthew G Knepley {
3363a19ef87SMatthew G Knepley   DM_Patch       *mesh = (DM_Patch*) dm->data;
3373a19ef87SMatthew G Knepley   PetscErrorCode ierr;
3383a19ef87SMatthew G Knepley 
3393a19ef87SMatthew G Knepley   PetscFunctionBegin;
3403a19ef87SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
341392fa6c6SMatthew G Knepley   ierr = DMSetUp(mesh->dmCoarse);CHKERRQ(ierr);
3423a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3433a19ef87SMatthew G Knepley }
3443a19ef87SMatthew G Knepley 
3453a19ef87SMatthew G Knepley PetscErrorCode DMCreateGlobalVector_Patch(DM dm, Vec *g)
3463a19ef87SMatthew G Knepley {
3473a19ef87SMatthew G Knepley   DM_Patch       *mesh = (DM_Patch*) dm->data;
3483a19ef87SMatthew G Knepley   PetscErrorCode ierr;
3493a19ef87SMatthew G Knepley 
3503a19ef87SMatthew G Knepley   PetscFunctionBegin;
3513a19ef87SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
352392fa6c6SMatthew G Knepley   ierr = DMCreateGlobalVector(mesh->dmCoarse, g);CHKERRQ(ierr);
3533a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3543a19ef87SMatthew G Knepley }
3553a19ef87SMatthew G Knepley 
3563a19ef87SMatthew G Knepley PetscErrorCode DMCreateLocalVector_Patch(DM dm, Vec *l)
3573a19ef87SMatthew G Knepley {
3583a19ef87SMatthew G Knepley   DM_Patch       *mesh = (DM_Patch*) dm->data;
3593a19ef87SMatthew G Knepley   PetscErrorCode ierr;
3603a19ef87SMatthew G Knepley 
3613a19ef87SMatthew G Knepley   PetscFunctionBegin;
3623a19ef87SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
363392fa6c6SMatthew G Knepley   ierr = DMCreateLocalVector(mesh->dmCoarse, l);CHKERRQ(ierr);
3643a19ef87SMatthew G Knepley   PetscFunctionReturn(0);
3653a19ef87SMatthew G Knepley }
3663a19ef87SMatthew G Knepley 
367276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Patch(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
3683a19ef87SMatthew G Knepley {
36982f516ccSBarry Smith   SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Tell me to code this");
3703a19ef87SMatthew G Knepley }
371392fa6c6SMatthew G Knepley 
372392fa6c6SMatthew G Knepley PetscErrorCode DMPatchGetCoarse(DM dm, DM *dmCoarse)
373392fa6c6SMatthew G Knepley {
374392fa6c6SMatthew G Knepley   DM_Patch *mesh = (DM_Patch*) dm->data;
375392fa6c6SMatthew G Knepley 
376392fa6c6SMatthew G Knepley   PetscFunctionBegin;
377392fa6c6SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
378392fa6c6SMatthew G Knepley   *dmCoarse = mesh->dmCoarse;
379392fa6c6SMatthew G Knepley   PetscFunctionReturn(0);
380392fa6c6SMatthew G Knepley }
381392fa6c6SMatthew G Knepley 
382392fa6c6SMatthew G Knepley PetscErrorCode DMPatchGetPatchSize(DM dm, MatStencil *patchSize)
383392fa6c6SMatthew G Knepley {
384392fa6c6SMatthew G Knepley   DM_Patch *mesh = (DM_Patch*) dm->data;
385392fa6c6SMatthew G Knepley 
386392fa6c6SMatthew G Knepley   PetscFunctionBegin;
387392fa6c6SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
388392fa6c6SMatthew G Knepley   PetscValidPointer(patchSize, 2);
389392fa6c6SMatthew G Knepley   *patchSize = mesh->patchSize;
390392fa6c6SMatthew G Knepley   PetscFunctionReturn(0);
391392fa6c6SMatthew G Knepley }
392392fa6c6SMatthew G Knepley 
393392fa6c6SMatthew G Knepley PetscErrorCode DMPatchSetPatchSize(DM dm, MatStencil patchSize)
394392fa6c6SMatthew G Knepley {
395392fa6c6SMatthew G Knepley   DM_Patch *mesh = (DM_Patch*) dm->data;
396392fa6c6SMatthew G Knepley 
397392fa6c6SMatthew G Knepley   PetscFunctionBegin;
398392fa6c6SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
399392fa6c6SMatthew G Knepley   mesh->patchSize = patchSize;
400392fa6c6SMatthew G Knepley   PetscFunctionReturn(0);
401392fa6c6SMatthew G Knepley }
402bb71ef15SMatthew G Knepley 
403bb71ef15SMatthew G Knepley PetscErrorCode DMPatchGetCommSize(DM dm, MatStencil *commSize)
404bb71ef15SMatthew G Knepley {
405bb71ef15SMatthew G Knepley   DM_Patch *mesh = (DM_Patch*) dm->data;
406bb71ef15SMatthew G Knepley 
407bb71ef15SMatthew G Knepley   PetscFunctionBegin;
408bb71ef15SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
409bb71ef15SMatthew G Knepley   PetscValidPointer(commSize, 2);
410bb71ef15SMatthew G Knepley   *commSize = mesh->commSize;
411bb71ef15SMatthew G Knepley   PetscFunctionReturn(0);
412bb71ef15SMatthew G Knepley }
413bb71ef15SMatthew G Knepley 
414bb71ef15SMatthew G Knepley PetscErrorCode DMPatchSetCommSize(DM dm, MatStencil commSize)
415bb71ef15SMatthew G Knepley {
416bb71ef15SMatthew G Knepley   DM_Patch *mesh = (DM_Patch*) dm->data;
417bb71ef15SMatthew G Knepley 
418bb71ef15SMatthew G Knepley   PetscFunctionBegin;
419bb71ef15SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
420bb71ef15SMatthew G Knepley   mesh->commSize = commSize;
421bb71ef15SMatthew G Knepley   PetscFunctionReturn(0);
422bb71ef15SMatthew G Knepley }
423