Actual source code: nn.c
1: #include <../src/ksp/pc/impls/is/nn/nn.h>
3: /*
4: PCSetUp_NN - Prepares for the use of the NN preconditioner
5: by setting data structures and options.
7: Input Parameter:
8: . pc - the preconditioner context
10: Application Interface Routine: PCSetUp()
12: Note:
13: The interface routine PCSetUp() is not usually called directly by
14: the user, but instead is called by PCApply() if necessary.
15: */
16: static PetscErrorCode PCSetUp_NN(PC pc)
17: {
18: PetscFunctionBegin;
19: if (!pc->setupcalled) {
20: /* Set up all the "iterative substructuring" common block */
21: PetscCall(PCISSetUp(pc, PETSC_TRUE, PETSC_TRUE));
22: /* Create the coarse matrix. */
23: PetscCall(PCNNCreateCoarseMatrix(pc));
24: }
25: PetscFunctionReturn(PETSC_SUCCESS);
26: }
28: /*
29: PCApply_NN - Applies the NN preconditioner to a vector.
31: Input Parameters:
32: + pc - the preconditioner context
33: - r - input vector (global)
35: Output Parameter:
36: . z - output vector (global)
38: Application Interface Routine: PCApply()
39: */
40: static PetscErrorCode PCApply_NN(PC pc, Vec r, Vec z)
41: {
42: PC_IS *pcis = (PC_IS *)pc->data;
43: PetscScalar m_one = -1.0;
44: Vec w = pcis->vec1_global;
46: PetscFunctionBegin;
47: /*
48: Dirichlet solvers.
49: Solving $B_I^{(i)}r_I^{(i)}$ at each processor.
50: Storing the local results at vec2_D
51: */
52: PetscCall(VecScatterBegin(pcis->global_to_D, r, pcis->vec1_D, INSERT_VALUES, SCATTER_FORWARD));
53: PetscCall(VecScatterEnd(pcis->global_to_D, r, pcis->vec1_D, INSERT_VALUES, SCATTER_FORWARD));
54: PetscCall(KSPSolve(pcis->ksp_D, pcis->vec1_D, pcis->vec2_D));
56: /*
57: Computing $r_B - \sum_j \tilde R_j^T A_{BI}^{(j)} (B_I^{(j)}r_I^{(j)})$ .
58: Storing the result in the interface portion of the global vector w.
59: */
60: PetscCall(MatMult(pcis->A_BI, pcis->vec2_D, pcis->vec1_B));
61: PetscCall(VecScale(pcis->vec1_B, m_one));
62: PetscCall(VecCopy(r, w));
63: PetscCall(VecScatterBegin(pcis->global_to_B, pcis->vec1_B, w, ADD_VALUES, SCATTER_REVERSE));
64: PetscCall(VecScatterEnd(pcis->global_to_B, pcis->vec1_B, w, ADD_VALUES, SCATTER_REVERSE));
66: /*
67: Apply the interface preconditioner
68: */
69: PetscCall(PCNNApplyInterfacePreconditioner(pc, w, z, pcis->work_N, pcis->vec1_B, pcis->vec2_B, pcis->vec3_B, pcis->vec1_D, pcis->vec3_D, pcis->vec1_N, pcis->vec2_N));
71: /*
72: Computing $t_I^{(i)} = A_{IB}^{(i)} \tilde R_i z_B$
73: The result is stored in vec1_D.
74: */
75: PetscCall(VecScatterBegin(pcis->global_to_B, z, pcis->vec1_B, INSERT_VALUES, SCATTER_FORWARD));
76: PetscCall(VecScatterEnd(pcis->global_to_B, z, pcis->vec1_B, INSERT_VALUES, SCATTER_FORWARD));
77: PetscCall(MatMult(pcis->A_IB, pcis->vec1_B, pcis->vec1_D));
79: /*
80: Dirichlet solvers.
81: Computing $B_I^{(i)}t_I^{(i)}$ and sticking into the global vector the blocks
82: $B_I^{(i)}r_I^{(i)} - B_I^{(i)}t_I^{(i)}$.
83: */
84: PetscCall(VecScatterBegin(pcis->global_to_D, pcis->vec2_D, z, INSERT_VALUES, SCATTER_REVERSE));
85: PetscCall(VecScatterEnd(pcis->global_to_D, pcis->vec2_D, z, INSERT_VALUES, SCATTER_REVERSE));
86: PetscCall(KSPSolve(pcis->ksp_D, pcis->vec1_D, pcis->vec2_D));
87: PetscCall(VecScale(pcis->vec2_D, m_one));
88: PetscCall(VecScatterBegin(pcis->global_to_D, pcis->vec2_D, z, ADD_VALUES, SCATTER_REVERSE));
89: PetscCall(VecScatterEnd(pcis->global_to_D, pcis->vec2_D, z, ADD_VALUES, SCATTER_REVERSE));
90: PetscFunctionReturn(PETSC_SUCCESS);
91: }
93: /*
94: PCDestroy_NN - Destroys the private context for the NN preconditioner
95: that was created with PCCreate_NN().
97: Input Parameter:
98: . pc - the preconditioner context
100: Application Interface Routine: PCDestroy()
101: */
102: static PetscErrorCode PCDestroy_NN(PC pc)
103: {
104: PC_NN *pcnn = (PC_NN *)pc->data;
106: PetscFunctionBegin;
107: PetscCall(PCISReset(pc));
109: PetscCall(MatDestroy(&pcnn->coarse_mat));
110: PetscCall(VecDestroy(&pcnn->coarse_x));
111: PetscCall(VecDestroy(&pcnn->coarse_b));
112: PetscCall(KSPDestroy(&pcnn->ksp_coarse));
113: if (pcnn->DZ_IN) {
114: PetscCall(PetscFree(pcnn->DZ_IN[0]));
115: PetscCall(PetscFree(pcnn->DZ_IN));
116: }
118: /*
119: Free the private data structure that was hanging off the PC
120: */
121: PetscCall(PetscFree(pc->data));
122: PetscFunctionReturn(PETSC_SUCCESS);
123: }
125: /*MC
126: PCNN - Balancing Neumann-Neumann for scalar elliptic PDEs.
128: Options Database Keys:
129: + -pc_nn_turn_off_first_balancing (true|false) - do not balance the residual before solving the local Neumann problems
130: (this skips the first coarse grid solve in the preconditioner)
131: . -pc_nn_turn_off_second_balancing (true|false) - do not balance the solution solving the local Neumann problems
132: (this skips the second coarse grid solve in the preconditioner)
133: . -pc_is_damp_fixed [fact] - use shift amount `fact` for zero pivots when factoring the local Neumann matrix
134: of a subdomain that touches the Dirichlet boundary (default: the floating subdomain amount)
135: . -pc_is_remove_nullspace_fixed (true|false) - attach the constant null space to the local Neumann matrix
136: of a subdomain that touches the Dirichlet boundary
137: . -pc_is_set_damping_factor_floating fact - use shift amount `fact` for zero pivots when factoring the local Neumann matrix
138: of a floating subdomain (default 1.e-12)
139: . -pc_is_not_damp_floating (true|false) - do not shift the diagonal when factoring the local Neumann matrix
140: of a floating subdomain
141: - -pc_is_not_remove_nullspace_floating (true|false) - do not attach the constant null space to the local Neumann matrix
142: of a floating subdomain
144: Options Database prefixes for the subsolvers this preconditioner uses:
145: + -nn_coarse_pc_ - for the coarse grid preconditioner
146: . -is_localD_pc_ - for the Dirichlet subproblem preconditioner
147: - -is_localN_pc_ - for the Neumann subproblem preconditioner
149: Level: intermediate
151: Notes:
152: The matrix used with this preconditioner must be of type `MATIS`
154: A floating subdomain is one that does not touch the Dirichlet boundary, so its local Neumann matrix is singular.
155: The `-pc_is_` options apply to the Neumann subproblem and so require the `-is_localN_` prefix, for example `-is_localN_pc_is_damp_fixed`.
157: Unlike more 'conventional' Neumann-Neumann preconditioners this iterates over ALL the
158: degrees of freedom, NOT just those on the interface (this allows the use of approximate solvers
159: on the subdomains; though in our experience using approximate solvers is slower.).
161: Contributed by Paulo Goldfeld
163: .seealso: [](ch_ksp), `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `MATIS`, `PCBDDC`
164: M*/
166: PETSC_EXTERN PetscErrorCode PCCreate_NN(PC pc)
167: {
168: PC_NN *pcnn;
170: PetscFunctionBegin;
171: /*
172: Creates the private data structure for this preconditioner and
173: attach it to the PC object.
174: */
175: PetscCall(PetscNew(&pcnn));
176: pc->data = (void *)pcnn;
178: PetscCall(PCISInitialize(pc));
179: pcnn->coarse_mat = NULL;
180: pcnn->coarse_x = NULL;
181: pcnn->coarse_b = NULL;
182: pcnn->ksp_coarse = NULL;
183: pcnn->DZ_IN = NULL;
185: /*
186: Set the pointers for the functions that are provided above.
187: Now when the user-level routines (such as PCApply(), PCDestroy(), etc.)
188: are called, they will automatically call these functions. Note we
189: choose not to provide a couple of these functions since they are
190: not needed.
191: */
192: pc->ops->apply = PCApply_NN;
193: pc->ops->setup = PCSetUp_NN;
194: pc->ops->destroy = PCDestroy_NN;
195: PetscFunctionReturn(PETSC_SUCCESS);
196: }
198: /*
199: PCNNCreateCoarseMatrix -
200: */
201: PetscErrorCode PCNNCreateCoarseMatrix(PC pc)
202: {
203: MPI_Request *send_request, *recv_request;
204: PetscInt i, j, k;
205: PetscScalar *mat; /* Sub-matrix with this subdomain's contribution to the coarse matrix */
206: PetscScalar **DZ_OUT; /* proc[k].DZ_OUT[i][] = bit of vector to be sent from processor k to processor i */
208: PC_IS *pcis = (PC_IS *)pc->data;
209: PC_NN *pcnn = (PC_NN *)pc->data;
210: PetscInt *neigh = pcis->neigh;
211: PetscInt *n_shared = pcis->n_shared;
212: PetscInt **shared = pcis->shared;
213: PetscMPIInt n_neigh;
214: PetscScalar **DZ_IN; /* Must be initialized after memory allocation. */
216: PetscFunctionBegin;
217: PetscCall(PetscMPIIntCast(pcis->n_neigh, &n_neigh));
218: /* Allocate memory for mat (the +1 is to handle the case n_neigh equal to zero) */
219: PetscCall(PetscMalloc1(n_neigh * n_neigh + 1, &mat));
221: /* Allocate memory for DZ */
222: /* Notice that DZ_OUT[0] is allocated some space that is never used. */
223: /* This is just in order to DZ_OUT and DZ_IN to have exactly the same form. */
224: {
225: PetscInt size_of_Z = 0;
226: PetscCall(PetscMalloc((n_neigh + 1) * sizeof(PetscScalar *), &pcnn->DZ_IN));
227: DZ_IN = pcnn->DZ_IN;
228: PetscCall(PetscMalloc((n_neigh + 1) * sizeof(PetscScalar *), &DZ_OUT));
229: for (i = 0; i < n_neigh; i++) size_of_Z += n_shared[i];
230: PetscCall(PetscMalloc((size_of_Z + 1) * sizeof(PetscScalar), &DZ_IN[0]));
231: PetscCall(PetscMalloc((size_of_Z + 1) * sizeof(PetscScalar), &DZ_OUT[0]));
232: }
233: for (i = 1; i < n_neigh; i++) {
234: DZ_IN[i] = DZ_IN[i - 1] + n_shared[i - 1];
235: DZ_OUT[i] = DZ_OUT[i - 1] + n_shared[i - 1];
236: }
238: /* Set the values of DZ_OUT, in order to send this info to the neighbours */
239: /* First, set the auxiliary array pcis->work_N. */
240: PetscCall(PCISScatterArrayNToVecB(pc, pcis->work_N, pcis->D, INSERT_VALUES, SCATTER_REVERSE));
241: for (i = 1; i < n_neigh; i++) {
242: for (j = 0; j < n_shared[i]; j++) DZ_OUT[i][j] = pcis->work_N[shared[i][j]];
243: }
245: /* Non-blocking send/receive the common-interface chunks of scaled nullspaces */
246: /* Notice that send_request[] and recv_request[] could have one less element. */
247: /* We make them longer to have request[i] corresponding to neigh[i]. */
248: {
249: PetscMPIInt tag;
250: PetscCall(PetscObjectGetNewTag((PetscObject)pc, &tag));
251: PetscCall(PetscMalloc2(n_neigh + 1, &send_request, n_neigh + 1, &recv_request));
252: for (i = 1; i < n_neigh; i++) {
253: PetscMPIInt nn;
255: PetscCall(PetscMPIIntCast(neigh[i], &nn));
256: PetscCallMPI(MPIU_Isend(DZ_OUT[i], n_shared[i], MPIU_SCALAR, nn, tag, PetscObjectComm((PetscObject)pc), &send_request[i]));
257: PetscCallMPI(MPIU_Irecv(DZ_IN[i], n_shared[i], MPIU_SCALAR, nn, tag, PetscObjectComm((PetscObject)pc), &recv_request[i]));
258: }
259: }
261: /* Set DZ_IN[0][] (recall that neigh[0]==rank, always) */
262: for (j = 0; j < n_shared[0]; j++) DZ_IN[0][j] = pcis->work_N[shared[0][j]];
264: /* Start computing with local D*Z while communication goes on. */
265: /* Apply Schur complement. The result is "stored" in vec (more */
266: /* precisely, vec points to the result, stored in pc_nn->vec1_B) */
267: /* and also scattered to pcnn->work_N. */
268: PetscCall(PCNNApplySchurToChunk(pc, n_shared[0], shared[0], DZ_IN[0], pcis->work_N, pcis->vec1_B, pcis->vec2_B, pcis->vec1_D, pcis->vec2_D));
270: /* Compute the first column, while completing the receiving. */
271: for (i = 0; i < n_neigh; i++) {
272: MPI_Status stat;
273: PetscMPIInt ind = 0;
274: if (i > 0) {
275: PetscCallMPI(MPI_Waitany(n_neigh - 1, recv_request + 1, &ind, &stat));
276: ind++;
277: }
278: mat[ind * n_neigh + 0] = 0.0;
279: for (k = 0; k < n_shared[ind]; k++) mat[ind * n_neigh + 0] += DZ_IN[ind][k] * pcis->work_N[shared[ind][k]];
280: }
282: /* Compute the remaining of the columns */
283: for (j = 1; j < n_neigh; j++) {
284: PetscCall(PCNNApplySchurToChunk(pc, n_shared[j], shared[j], DZ_IN[j], pcis->work_N, pcis->vec1_B, pcis->vec2_B, pcis->vec1_D, pcis->vec2_D));
285: for (i = 0; i < n_neigh; i++) {
286: mat[i * n_neigh + j] = 0.0;
287: for (k = 0; k < n_shared[i]; k++) mat[i * n_neigh + j] += DZ_IN[i][k] * pcis->work_N[shared[i][k]];
288: }
289: }
291: /* Complete the sending. */
292: if (n_neigh > 1) {
293: MPI_Status *stat;
294: PetscCall(PetscMalloc1(n_neigh - 1, &stat));
295: if (n_neigh - 1) PetscCallMPI(MPI_Waitall(n_neigh - 1, &send_request[1], stat));
296: PetscCall(PetscFree(stat));
297: }
299: /* Free the memory for the MPI requests */
300: PetscCall(PetscFree2(send_request, recv_request));
302: /* Free the memory for DZ_OUT */
303: if (DZ_OUT) {
304: PetscCall(PetscFree(DZ_OUT[0]));
305: PetscCall(PetscFree(DZ_OUT));
306: }
308: {
309: PetscMPIInt size;
310: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
311: /* Create the global coarse vectors (rhs and solution). */
312: PetscCall(VecCreateMPI(PetscObjectComm((PetscObject)pc), 1, size, &pcnn->coarse_b));
313: PetscCall(VecDuplicate(pcnn->coarse_b, &pcnn->coarse_x));
314: /* Create and set the global coarse AIJ matrix. */
315: PetscCall(MatCreate(PetscObjectComm((PetscObject)pc), &pcnn->coarse_mat));
316: PetscCall(MatSetSizes(pcnn->coarse_mat, 1, 1, size, size));
317: PetscCall(MatSetType(pcnn->coarse_mat, MATAIJ));
318: PetscCall(MatSeqAIJSetPreallocation(pcnn->coarse_mat, 1, NULL));
319: PetscCall(MatMPIAIJSetPreallocation(pcnn->coarse_mat, 1, NULL, n_neigh, NULL));
320: PetscCall(MatSetOption(pcnn->coarse_mat, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
321: PetscCall(MatSetOption(pcnn->coarse_mat, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_FALSE));
322: PetscCall(MatSetValues(pcnn->coarse_mat, n_neigh, neigh, n_neigh, neigh, mat, ADD_VALUES));
323: PetscCall(MatAssemblyBegin(pcnn->coarse_mat, MAT_FINAL_ASSEMBLY));
324: PetscCall(MatAssemblyEnd(pcnn->coarse_mat, MAT_FINAL_ASSEMBLY));
325: }
327: {
328: PetscMPIInt rank;
329: PetscScalar one = 1.0;
330: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
331: /* "Zero out" rows of not-purely-Neumann subdomains */
332: if (pcis->pure_neumann) { /* does NOT zero the row; create an empty index set. The reason is that MatZeroRows() is collective. */
333: PetscCall(MatZeroRows(pcnn->coarse_mat, 0, NULL, one, NULL, NULL));
334: } else { /* here it DOES zero the row, since it's not a floating subdomain. */
335: PetscInt row = rank;
336: PetscCall(MatZeroRows(pcnn->coarse_mat, 1, &row, one, NULL, NULL));
337: }
338: }
340: /* Create the coarse linear solver context */
341: {
342: PC pc_ctx, inner_pc;
343: KSP inner_ksp;
345: PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc), &pcnn->ksp_coarse));
346: PetscCall(KSPSetNestLevel(pcnn->ksp_coarse, pc->kspnestlevel));
347: PetscCall(PetscObjectIncrementTabLevel((PetscObject)pcnn->ksp_coarse, (PetscObject)pc, 2));
348: PetscCall(KSPSetOperators(pcnn->ksp_coarse, pcnn->coarse_mat, pcnn->coarse_mat));
349: PetscCall(KSPGetPC(pcnn->ksp_coarse, &pc_ctx));
350: PetscCall(PCSetType(pc_ctx, PCREDUNDANT));
351: PetscCall(KSPSetType(pcnn->ksp_coarse, KSPPREONLY));
352: PetscCall(PCRedundantGetKSP(pc_ctx, &inner_ksp));
353: PetscCall(KSPGetPC(inner_ksp, &inner_pc));
354: PetscCall(PCSetType(inner_pc, PCLU));
355: PetscCall(KSPSetOptionsPrefix(pcnn->ksp_coarse, "nn_coarse_"));
356: PetscCall(KSPSetFromOptions(pcnn->ksp_coarse));
357: /* the vectors in the following line are dummy arguments, just telling the KSP the vector size. Values are not used */
358: PetscCall(KSPSetUp(pcnn->ksp_coarse));
359: }
361: /* Free the memory for mat */
362: PetscCall(PetscFree(mat));
364: /* for DEBUGGING, save the coarse matrix to a file. */
365: {
366: PetscBool flg = PETSC_FALSE;
367: PetscCall(PetscOptionsGetBool(NULL, NULL, "-pc_nn_save_coarse_matrix", &flg, NULL));
368: if (flg) {
369: PetscViewer viewer;
370: PetscCall(PetscViewerASCIIOpen(PETSC_COMM_WORLD, "coarse.m", &viewer));
371: PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_MATLAB));
372: PetscCall(MatView(pcnn->coarse_mat, viewer));
373: PetscCall(PetscViewerPopFormat(viewer));
374: PetscCall(PetscViewerDestroy(&viewer));
375: }
376: }
378: /* Set the variable pcnn->factor_coarse_rhs. */
379: pcnn->factor_coarse_rhs = pcis->pure_neumann ? 1.0 : 0.0;
381: /* See historical note 02, at the bottom of this file. */
382: PetscFunctionReturn(PETSC_SUCCESS);
383: }
385: /*
386: PCNNApplySchurToChunk -
388: Input parameters:
389: . pcnn
390: . n - size of chunk
391: . idx - indices of chunk
392: . chunk - values
394: Output parameters:
395: . array_N - result of Schur complement applied to chunk, scattered to big array
396: . vec1_B - result of Schur complement applied to chunk
397: . vec2_B - garbage (used as work space)
398: . vec1_D - garbage (used as work space)
399: . vec2_D - garbage (used as work space)
401: */
402: PetscErrorCode PCNNApplySchurToChunk(PC pc, PetscInt n, PetscInt *idx, PetscScalar *chunk, PetscScalar *array_N, Vec vec1_B, Vec vec2_B, Vec vec1_D, Vec vec2_D)
403: {
404: PC_IS *pcis = (PC_IS *)pc->data;
406: PetscFunctionBegin;
407: PetscCall(PetscArrayzero(array_N, pcis->n));
408: for (PetscInt i = 0; i < n; i++) array_N[idx[i]] = chunk[i];
409: PetscCall(PCISScatterArrayNToVecB(pc, array_N, vec2_B, INSERT_VALUES, SCATTER_FORWARD));
410: PetscCall(PCISApplySchur(pc, vec2_B, vec1_B, (Vec)0, vec1_D, vec2_D));
411: PetscCall(PCISScatterArrayNToVecB(pc, array_N, vec1_B, INSERT_VALUES, SCATTER_REVERSE));
412: PetscFunctionReturn(PETSC_SUCCESS);
413: }
415: /*
416: PCNNApplyInterfacePreconditioner - Apply the interface preconditioner, i.e.,
417: the preconditioner for the Schur complement.
419: Input parameter:
420: . r - global vector of interior and interface nodes. The values on the interior nodes are NOT used.
422: Output parameters:
423: . z - global vector of interior and interface nodes. The values on the interface are the result of
424: the application of the interface preconditioner to the interface part of r. The values on the
425: interior nodes are garbage.
426: . work_N - array of local nodes (interior and interface, including ghosts); returns garbage (used as work space)
427: . vec1_B - vector of local interface nodes (including ghosts); returns garbage (used as work space)
428: . vec2_B - vector of local interface nodes (including ghosts); returns garbage (used as work space)
429: . vec3_B - vector of local interface nodes (including ghosts); returns garbage (used as work space)
430: . vec1_D - vector of local interior nodes; returns garbage (used as work space)
431: . vec2_D - vector of local interior nodes; returns garbage (used as work space)
432: . vec1_N - vector of local nodes (interior and interface, including ghosts); returns garbage (used as work space)
433: . vec2_N - vector of local nodes (interior and interface, including ghosts); returns garbage (used as work space)
435: */
436: PetscErrorCode PCNNApplyInterfacePreconditioner(PC pc, Vec r, Vec z, PetscScalar *work_N, Vec vec1_B, Vec vec2_B, Vec vec3_B, Vec vec1_D, Vec vec2_D, Vec vec1_N, Vec vec2_N)
437: {
438: PC_IS *pcis = (PC_IS *)pc->data;
440: PetscFunctionBegin;
441: /*
442: First balancing step.
443: */
444: {
445: PetscBool flg = PETSC_FALSE;
446: PetscCall(PetscOptionsGetBool(NULL, NULL, "-pc_nn_turn_off_first_balancing", &flg, NULL));
447: if (!flg) {
448: PetscCall(PCNNBalancing(pc, r, (Vec)0, z, vec1_B, vec2_B, (Vec)0, vec1_D, vec2_D, work_N));
449: } else {
450: PetscCall(VecCopy(r, z));
451: }
452: }
454: /*
455: Extract the local interface part of z and scale it by D
456: */
457: PetscCall(VecScatterBegin(pcis->global_to_B, z, vec1_B, INSERT_VALUES, SCATTER_FORWARD));
458: PetscCall(VecScatterEnd(pcis->global_to_B, z, vec1_B, INSERT_VALUES, SCATTER_FORWARD));
459: PetscCall(VecPointwiseMult(vec2_B, pcis->D, vec1_B));
461: /* Neumann Solver */
462: PetscCall(PCISApplyInvSchur(pc, vec2_B, vec1_B, vec1_N, vec2_N));
464: /*
465: Second balancing step.
466: */
467: {
468: PetscBool flg = PETSC_FALSE;
470: PetscCall(PetscOptionsDeprecatedNoObject(PetscObjectComm((PetscObject)pc), NULL, "-pc_turn_off_second_balancing", "-pc_nn_turn_off_second_balancing", "3.27", NULL));
471: PetscCall(PetscOptionsGetBool(NULL, NULL, "-pc_nn_turn_off_second_balancing", &flg, NULL));
472: if (!flg) {
473: PetscCall(PCNNBalancing(pc, r, vec1_B, z, vec2_B, vec3_B, (Vec)0, vec1_D, vec2_D, work_N));
474: } else {
475: PetscCall(VecPointwiseMult(vec2_B, pcis->D, vec1_B));
476: PetscCall(VecSet(z, 0.0));
477: PetscCall(VecScatterBegin(pcis->global_to_B, vec2_B, z, ADD_VALUES, SCATTER_REVERSE));
478: PetscCall(VecScatterEnd(pcis->global_to_B, vec2_B, z, ADD_VALUES, SCATTER_REVERSE));
479: }
480: }
481: PetscFunctionReturn(PETSC_SUCCESS);
482: }
484: /*
485: PCNNBalancing - Computes z, as given in equations (15) and (16) (if the
486: input argument u is provided), or s, as given in equations
487: (12) and (13), if the input argument u is a null vector.
488: Notice that the input argument u plays the role of u_i in
489: equation (14). The equation numbers refer to [Man93].
491: Input Parameters:
492: + pcnn - NN preconditioner context.
493: . r - MPI vector of all nodes (interior and interface). It's preserved.
494: - u - (Optional) sequential vector of local interface nodes. It's preserved UNLESS vec3_B is null.
496: Output Parameters:
497: + z - MPI vector of interior and interface nodes. Returns s or z (see description above).
498: . vec1_B - Sequential vector of local interface nodes. Workspace.
499: . vec2_B - Sequential vector of local interface nodes. Workspace.
500: . vec3_B - (Optional) sequential vector of local interface nodes. Workspace.
501: . vec1_D - Sequential vector of local interior nodes. Workspace.
502: . vec2_D - Sequential vector of local interior nodes. Workspace.
503: - work_N - Array of all local nodes (interior and interface). Workspace.
505: */
506: PetscErrorCode PCNNBalancing(PC pc, Vec r, Vec u, Vec z, Vec vec1_B, Vec vec2_B, Vec vec3_B, Vec vec1_D, Vec vec2_D, PetscScalar *work_N)
507: {
508: PetscInt k;
509: PetscScalar value;
510: PetscScalar *lambda;
511: PC_NN *pcnn = (PC_NN *)pc->data;
512: PC_IS *pcis = (PC_IS *)pc->data;
514: PetscFunctionBegin;
515: PetscCall(PetscLogEventBegin(PC_ApplyCoarse, pc, 0, 0, 0));
516: if (u) {
517: if (!vec3_B) vec3_B = u;
518: PetscCall(VecPointwiseMult(vec1_B, pcis->D, u));
519: PetscCall(VecSet(z, 0.0));
520: PetscCall(VecScatterBegin(pcis->global_to_B, vec1_B, z, ADD_VALUES, SCATTER_REVERSE));
521: PetscCall(VecScatterEnd(pcis->global_to_B, vec1_B, z, ADD_VALUES, SCATTER_REVERSE));
522: PetscCall(VecScatterBegin(pcis->global_to_B, z, vec2_B, INSERT_VALUES, SCATTER_FORWARD));
523: PetscCall(VecScatterEnd(pcis->global_to_B, z, vec2_B, INSERT_VALUES, SCATTER_FORWARD));
524: PetscCall(PCISApplySchur(pc, vec2_B, vec3_B, (Vec)0, vec1_D, vec2_D));
525: PetscCall(VecScale(vec3_B, -1.0));
526: PetscCall(VecCopy(r, z));
527: PetscCall(VecScatterBegin(pcis->global_to_B, vec3_B, z, ADD_VALUES, SCATTER_REVERSE));
528: PetscCall(VecScatterEnd(pcis->global_to_B, vec3_B, z, ADD_VALUES, SCATTER_REVERSE));
529: } else {
530: PetscCall(VecCopy(r, z));
531: }
532: PetscCall(VecScatterBegin(pcis->global_to_B, z, vec2_B, INSERT_VALUES, SCATTER_FORWARD));
533: PetscCall(VecScatterEnd(pcis->global_to_B, z, vec2_B, INSERT_VALUES, SCATTER_FORWARD));
534: PetscCall(PCISScatterArrayNToVecB(pc, work_N, vec2_B, INSERT_VALUES, SCATTER_REVERSE));
535: for (k = 0, value = 0.0; k < pcis->n_shared[0]; k++) value += pcnn->DZ_IN[0][k] * work_N[pcis->shared[0][k]];
536: value *= pcnn->factor_coarse_rhs; /* This factor is set in CreateCoarseMatrix(). */
537: {
538: PetscMPIInt rank;
539: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)pc), &rank));
540: PetscCall(VecSetValue(pcnn->coarse_b, rank, value, INSERT_VALUES));
541: /*
542: Since we are only inserting local values (one value actually) we don't need to do the
543: reduction that tells us there is no data that needs to be moved. Hence we comment out these
544: PetscCall(VecAssemblyBegin(pcnn->coarse_b));
545: PetscCall(VecAssemblyEnd (pcnn->coarse_b));
546: */
547: }
548: PetscCall(KSPSolve(pcnn->ksp_coarse, pcnn->coarse_b, pcnn->coarse_x));
549: if (!u) PetscCall(VecScale(pcnn->coarse_x, -1.0));
550: PetscCall(VecGetArray(pcnn->coarse_x, &lambda));
551: for (k = 0; k < pcis->n_shared[0]; k++) work_N[pcis->shared[0][k]] = *lambda * pcnn->DZ_IN[0][k];
552: PetscCall(VecRestoreArray(pcnn->coarse_x, &lambda));
553: PetscCall(PCISScatterArrayNToVecB(pc, work_N, vec2_B, INSERT_VALUES, SCATTER_FORWARD));
554: PetscCall(VecSet(z, 0.0));
555: PetscCall(VecScatterBegin(pcis->global_to_B, vec2_B, z, ADD_VALUES, SCATTER_REVERSE));
556: PetscCall(VecScatterEnd(pcis->global_to_B, vec2_B, z, ADD_VALUES, SCATTER_REVERSE));
557: if (!u) {
558: PetscCall(VecScatterBegin(pcis->global_to_B, z, vec2_B, INSERT_VALUES, SCATTER_FORWARD));
559: PetscCall(VecScatterEnd(pcis->global_to_B, z, vec2_B, INSERT_VALUES, SCATTER_FORWARD));
560: PetscCall(PCISApplySchur(pc, vec2_B, vec1_B, (Vec)0, vec1_D, vec2_D));
561: PetscCall(VecCopy(r, z));
562: }
563: PetscCall(VecScatterBegin(pcis->global_to_B, vec1_B, z, ADD_VALUES, SCATTER_REVERSE));
564: PetscCall(VecScatterEnd(pcis->global_to_B, vec1_B, z, ADD_VALUES, SCATTER_REVERSE));
565: PetscCall(PetscLogEventEnd(PC_ApplyCoarse, pc, 0, 0, 0));
566: PetscFunctionReturn(PETSC_SUCCESS);
567: }
569: /* From now on, "footnotes" (or "historical notes"). */
570: /*
571: Historical note 01
573: We considered the possibility of an alternative D_i that would still
574: provide a partition of unity (i.e., $\sum_i N_i D_i N_i^T = I$).
575: The basic principle was still the pseudo-inverse of the counting
576: function; the difference was that we would not count subdomains
577: that do not contribute to the coarse space (i.e., not pure-Neumann
578: subdomains).
580: This turned out to be a bad idea: we would solve trivial Neumann
581: problems in the not pure-Neumann subdomains, since we would be scaling
582: the balanced residual by zero.
583: */
585: /*
586: Historical note 02
588: We tried an alternative coarse problem, that would eliminate exactly a
589: constant error. Turned out not to improve the overall convergence.
590: */