xref: /petsc/src/ksp/pc/impls/gamg/gamg.c (revision e1cf14442cd30d139b39eb8d14a4fad780977f2e)
15b89ad90SMark F. Adams /*
20cd22d39SHong Zhang  GAMG geometric-algebric multigrid PC - Mark Adams 2011
35b89ad90SMark F. Adams  */
4af0996ceSBarry Smith #include <petsc/private/matimpl.h>
5389730f3SMark F. Adams #include <../src/ksp/pc/impls/gamg/gamg.h>            /*I "petscpc.h" I*/
618c3aa7eSMark #include <../src/ksp/ksp/impls/cheby/chebyshevimpl.h> /*I "petscksp.h" I*/
7f96513f1SMatthew G Knepley 
8c9567895SMark #if defined(PETSC_HAVE_CUDA)
9c9567895SMark   #include <cuda_runtime.h>
10c9567895SMark #endif
11c9567895SMark 
12c9567895SMark #if defined(PETSC_HAVE_HIP)
13c9567895SMark   #include <hip/hip_runtime.h>
14c9567895SMark #endif
15c9567895SMark 
16849bee69SMark Adams PetscLogEvent petsc_gamg_setup_events[GAMG_NUM_SET];
174555aa8cSStefano Zampini PetscLogEvent petsc_gamg_setup_matmat_events[PETSC_MG_MAXLEVELS][3];
180cbbd2e1SMark F. Adams 
19849bee69SMark Adams // #define GAMG_STAGES
204555aa8cSStefano Zampini #if defined(GAMG_STAGES)
2118c3aa7eSMark static PetscLogStage gamg_stages[PETSC_MG_MAXLEVELS];
22b4fbaa2aSMark F. Adams #endif
23f96513f1SMatthew G Knepley 
240a545947SLisandro Dalcin static PetscFunctionList GAMGList = NULL;
253e3471ccSMark Adams static PetscBool         PCGAMGPackageInitialized;
269d5b6da9SMark F. Adams 
2766976f2fSJacob Faibussowitsch static PetscErrorCode PCReset_GAMG(PC pc)
28d71ae5a4SJacob Faibussowitsch {
29d3d6bff4SMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
30d3d6bff4SMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
31d3d6bff4SMark F. Adams 
32d3d6bff4SMark F. Adams   PetscFunctionBegin;
339566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc_gamg->data));
341c1aac46SBarry Smith   pc_gamg->data_sz = 0;
359566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc_gamg->orig_data));
365f80ce2aSJacob Faibussowitsch   for (PetscInt level = 0; level < PETSC_MG_MAXLEVELS; level++) {
3718c3aa7eSMark     mg->min_eigen_DinvA[level] = 0;
3818c3aa7eSMark     mg->max_eigen_DinvA[level] = 0;
3918c3aa7eSMark   }
4018c3aa7eSMark   pc_gamg->emin = 0;
4118c3aa7eSMark   pc_gamg->emax = 0;
42978e3cbaSStefano Zampini   PetscCall(PCReset_MG(pc));
433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44a2f3521dSMark F. Adams }
45a2f3521dSMark F. Adams 
465b89ad90SMark F. Adams /*
47c238b0ebSToby Isaac    PCGAMGCreateLevel_GAMG: create coarse op with RAP.  repartition and/or reduce number
48a147abb0SMark F. Adams      of active processors.
495b89ad90SMark F. Adams 
505b89ad90SMark F. Adams    Input Parameter:
51a2f3521dSMark F. Adams    . pc - parameters + side effect: coarse data in 'pc_gamg->data' and
52a2f3521dSMark F. Adams           'pc_gamg->data_sz' are changed via repartitioning/reduction.
539d5b6da9SMark F. Adams    . Amat_fine - matrix on this fine (k) level
54c5bfad50SMark F. Adams    . cr_bs - coarse block size
553530afc2SMark F. Adams    In/Output Parameter:
56a2f3521dSMark F. Adams    . a_P_inout - prolongation operator to the next level (k-->k-1)
57afc97cdcSMark F. Adams    . a_nactive_proc - number of active procs
5811e60469SMark F. Adams    Output Parameter:
593530afc2SMark F. Adams    . a_Amat_crs - coarse matrix that is created (k-1)
605b89ad90SMark F. Adams */
61d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGCreateLevel_GAMG(PC pc, Mat Amat_fine, PetscInt cr_bs, Mat *a_P_inout, Mat *a_Amat_crs, PetscMPIInt *a_nactive_proc, IS *Pcolumnperm, PetscBool is_last)
62d71ae5a4SJacob Faibussowitsch {
639d5b6da9SMark F. Adams   PC_MG      *mg      = (PC_MG *)pc->data;
64486a8d0bSJed Brown   PC_GAMG    *pc_gamg = (PC_GAMG *)mg->innerctx;
65a2f3521dSMark F. Adams   Mat         Cmat, Pold = *a_P_inout;
663b4367a7SBarry Smith   MPI_Comm    comm;
67c5df96a5SBarry Smith   PetscMPIInt rank, size, new_size, nactive = *a_nactive_proc;
683ae0bb68SMark Adams   PetscInt    ncrs_eq, ncrs, f_bs;
695b89ad90SMark F. Adams 
705b89ad90SMark F. Adams   PetscFunctionBegin;
719566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)Amat_fine, &comm));
729566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
739566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
749566063dSJacob Faibussowitsch   PetscCall(MatGetBlockSize(Amat_fine, &f_bs));
75849bee69SMark Adams   PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[GAMG_PTAP], 0, 0, 0, 0));
769566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(petsc_gamg_setup_matmat_events[pc_gamg->current_level][1], 0, 0, 0, 0));
779566063dSJacob Faibussowitsch   PetscCall(MatPtAP(Amat_fine, Pold, MAT_INITIAL_MATRIX, 2.0, &Cmat));
789566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(petsc_gamg_setup_matmat_events[pc_gamg->current_level][1], 0, 0, 0, 0));
79849bee69SMark Adams   PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_PTAP], 0, 0, 0, 0));
80038e3b61SMark F. Adams 
81ce7c7f2fSMark Adams   if (Pcolumnperm) *Pcolumnperm = NULL;
82ce7c7f2fSMark Adams 
833ae0bb68SMark Adams   /* set 'ncrs' (nodes), 'ncrs_eq' (equations)*/
849566063dSJacob Faibussowitsch   PetscCall(MatGetLocalSize(Cmat, &ncrs_eq, NULL));
853ae0bb68SMark Adams   if (pc_gamg->data_cell_rows > 0) {
863ae0bb68SMark Adams     ncrs = pc_gamg->data_sz / pc_gamg->data_cell_cols / pc_gamg->data_cell_rows;
8773911c69SBarry Smith   } else {
883ae0bb68SMark Adams     PetscInt bs;
899566063dSJacob Faibussowitsch     PetscCall(MatGetBlockSize(Cmat, &bs));
903ae0bb68SMark Adams     ncrs = ncrs_eq / bs;
913ae0bb68SMark Adams   }
92c5df96a5SBarry Smith   /* get number of PEs to make active 'new_size', reduce, can be any integer 1-P */
93c9567895SMark   if (pc_gamg->level_reduction_factors[pc_gamg->current_level] == 0 && PetscDefined(HAVE_CUDA) && pc_gamg->current_level == 0) { /* 0 turns reducing to 1 process/device on; do for HIP, etc. */
94c9567895SMark #if defined(PETSC_HAVE_CUDA)
95c9567895SMark     PetscShmComm pshmcomm;
96c9567895SMark     PetscMPIInt  locrank;
97c9567895SMark     MPI_Comm     loccomm;
98c9567895SMark     PetscInt     s_nnodes, r_nnodes, new_new_size;
99c9567895SMark     cudaError_t  cerr;
100c9567895SMark     int          devCount;
1019566063dSJacob Faibussowitsch     PetscCall(PetscShmCommGet(comm, &pshmcomm));
1029566063dSJacob Faibussowitsch     PetscCall(PetscShmCommGetMpiShmComm(pshmcomm, &loccomm));
1039566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(loccomm, &locrank));
104c9567895SMark     s_nnodes = !locrank;
105712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(&s_nnodes, &r_nnodes, 1, MPIU_INT, MPI_SUM, comm));
10663a3b9bcSJacob Faibussowitsch     PetscCheck((size % r_nnodes) == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "odd number of nodes np=%d nnodes%" PetscInt_FMT, size, r_nnodes);
107c9567895SMark     devCount = 0;
108c9567895SMark     cerr     = cudaGetDeviceCount(&devCount);
109c9567895SMark     cudaGetLastError();                         /* Reset the last error */
110c9567895SMark     if (cerr == cudaSuccess && devCount >= 1) { /* There are devices, else go to heuristic */
111c9567895SMark       new_new_size = r_nnodes * devCount;
112c9567895SMark       new_size     = new_new_size;
11363a3b9bcSJacob Faibussowitsch       PetscCall(PetscInfo(pc, "%s: Fine grid with Cuda. %" PetscInt_FMT " nodes. Change new active set size %d --> %d (devCount=%d #nodes=%" PetscInt_FMT ")\n", ((PetscObject)pc)->prefix, r_nnodes, nactive, new_size, devCount, r_nnodes));
114c9567895SMark     } else {
1159d3446b2SPierre Jolivet       PetscCall(PetscInfo(pc, "%s: With Cuda but no device. Use heuristics.\n", ((PetscObject)pc)->prefix));
116c9567895SMark       goto HEURISTIC;
117c9567895SMark     }
118c9567895SMark #else
119c9567895SMark     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "should not be here");
120c9567895SMark #endif
121c9567895SMark   } else if (pc_gamg->level_reduction_factors[pc_gamg->current_level] > 0) {
12263a3b9bcSJacob Faibussowitsch     PetscCheck(nactive % pc_gamg->level_reduction_factors[pc_gamg->current_level] == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "odd number of active process %d wrt reduction factor %" PetscInt_FMT, nactive, pc_gamg->level_reduction_factors[pc_gamg->current_level]);
123c9567895SMark     new_size = nactive / pc_gamg->level_reduction_factors[pc_gamg->current_level];
12463a3b9bcSJacob Faibussowitsch     PetscCall(PetscInfo(pc, "%s: Manually setting reduction to %d active processes (%d/%" PetscInt_FMT ")\n", ((PetscObject)pc)->prefix, new_size, nactive, pc_gamg->level_reduction_factors[pc_gamg->current_level]));
125c9567895SMark   } else if (is_last && !pc_gamg->use_parallel_coarse_grid_solver) {
126c9567895SMark     new_size = 1;
1279566063dSJacob Faibussowitsch     PetscCall(PetscInfo(pc, "%s: Force coarsest grid reduction to %d active processes\n", ((PetscObject)pc)->prefix, new_size));
128c9567895SMark   } else {
129472110cdSMark F. Adams     PetscInt ncrs_eq_glob;
130c9567895SMark #if defined(PETSC_HAVE_CUDA)
131c9567895SMark   HEURISTIC:
132c9567895SMark #endif
1339566063dSJacob Faibussowitsch     PetscCall(MatGetSize(Cmat, &ncrs_eq_glob, NULL));
134a90e85d9SMark Adams     new_size = (PetscMPIInt)((float)ncrs_eq_glob / (float)pc_gamg->min_eq_proc + 0.5); /* hardwire min. number of eq/proc */
135da81f932SPierre Jolivet     if (!new_size) new_size = 1;                                                       /* not likely, possible? */
136c5df96a5SBarry Smith     else if (new_size >= nactive) new_size = nactive;                                  /* no change, rare */
1379566063dSJacob Faibussowitsch     PetscCall(PetscInfo(pc, "%s: Coarse grid reduction from %d to %d active processes\n", ((PetscObject)pc)->prefix, nactive, new_size));
138a2f3521dSMark F. Adams   }
1392e3501ffSMark Adams   if (new_size == nactive) {
140ef3f0257SMark Adams     *a_Amat_crs = Cmat; /* output - no repartitioning or reduction - could bail here */
141ce7c7f2fSMark Adams     if (new_size < size) {
142ce7c7f2fSMark Adams       /* odd case where multiple coarse grids are on one processor or no coarsening ... */
1439566063dSJacob Faibussowitsch       PetscCall(PetscInfo(pc, "%s: reduced grid using same number of processors (%d) as last grid (use larger coarse grid)\n", ((PetscObject)pc)->prefix, nactive));
144ce7c7f2fSMark Adams       if (pc_gamg->cpu_pin_coarse_grids) {
1459566063dSJacob Faibussowitsch         PetscCall(MatBindToCPU(*a_Amat_crs, PETSC_TRUE));
1469566063dSJacob Faibussowitsch         PetscCall(MatBindToCPU(*a_P_inout, PETSC_TRUE));
147ce7c7f2fSMark Adams       }
148ce7c7f2fSMark Adams     }
149ef3f0257SMark Adams     /* we know that the grid structure can be reused in MatPtAP */
1502e3501ffSMark Adams   } else { /* reduce active processors - we know that the grid structure can NOT be reused in MatPtAP */
151192c0e8bSMark Adams     PetscInt *counts, *newproc_idx, ii, jj, kk, strideNew, *tidx, ncrs_new, ncrs_eq_new, nloc_old, expand_factor = 1, rfactor = 1;
152885364a3SMark Adams     IS        is_eq_newproc, is_eq_num, is_eq_num_prim, new_eq_indices;
153849bee69SMark Adams     PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[GAMG_REDUCE], 0, 0, 0, 0));
15471959b99SBarry Smith     nloc_old = ncrs_eq / cr_bs;
15563a3b9bcSJacob Faibussowitsch     PetscCheck(ncrs_eq % cr_bs == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "ncrs_eq %" PetscInt_FMT " not divisible by cr_bs %" PetscInt_FMT, ncrs_eq, cr_bs);
156ce7c7f2fSMark Adams     /* get new_size and rfactor */
157ce7c7f2fSMark Adams     if (pc_gamg->layout_type == PCGAMG_LAYOUT_SPREAD || !pc_gamg->repart) {
158ce7c7f2fSMark Adams       /* find factor */
159ce7c7f2fSMark Adams       if (new_size == 1) rfactor = size; /* don't modify */
160ce7c7f2fSMark Adams       else {
161ce7c7f2fSMark Adams         PetscReal best_fact = 0.;
162ce7c7f2fSMark Adams         jj                  = -1;
163ce7c7f2fSMark Adams         for (kk = 1; kk <= size; kk++) {
164ce7c7f2fSMark Adams           if (!(size % kk)) { /* a candidate */
165ce7c7f2fSMark Adams             PetscReal nactpe = (PetscReal)size / (PetscReal)kk, fact = nactpe / (PetscReal)new_size;
166ce7c7f2fSMark Adams             if (fact > 1.0) fact = 1. / fact; /* keep fact < 1 */
167ce7c7f2fSMark Adams             if (fact > best_fact) {
1689371c9d4SSatish Balay               best_fact = fact;
1699371c9d4SSatish Balay               jj        = kk;
170ce7c7f2fSMark Adams             }
171ce7c7f2fSMark Adams           }
172ce7c7f2fSMark Adams         }
173ce7c7f2fSMark Adams         if (jj != -1) rfactor = jj;
174ce7c7f2fSMark Adams         else rfactor = 1; /* a prime */
175ce7c7f2fSMark Adams         if (pc_gamg->layout_type == PCGAMG_LAYOUT_COMPACT) expand_factor = 1;
176ce7c7f2fSMark Adams         else expand_factor = rfactor;
177ce7c7f2fSMark Adams       }
178ce7c7f2fSMark Adams       new_size = size / rfactor; /* make new size one that is factor */
1794cdfd227SMark       if (new_size == nactive) { /* no repartitioning or reduction, bail out because nested here (rare) */
1804cdfd227SMark         *a_Amat_crs = Cmat;
18163a3b9bcSJacob Faibussowitsch         PetscCall(PetscInfo(pc, "%s: Finding factorable processor set stopped reduction: new_size=%d, neq(loc)=%" PetscInt_FMT "\n", ((PetscObject)pc)->prefix, new_size, ncrs_eq));
182849bee69SMark Adams         PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_REDUCE], 0, 0, 0, 0));
1833ba16761SJacob Faibussowitsch         PetscFunctionReturn(PETSC_SUCCESS);
184ce7c7f2fSMark Adams       }
185ce7c7f2fSMark Adams     }
186a2f3521dSMark F. Adams     /* make 'is_eq_newproc' */
1879566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(size, &counts));
188849bee69SMark Adams     if (pc_gamg->repart) { /* Repartition Cmat_{k} and move columns of P^{k}_{k-1} and coordinates of primal part accordingly */
1895a9b9e01SMark F. Adams       Mat adj;
190849bee69SMark Adams       PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[GAMG_REPART], 0, 0, 0, 0));
19163a3b9bcSJacob Faibussowitsch       PetscCall(PetscInfo(pc, "%s: Repartition: size (active): %d --> %d, %" PetscInt_FMT " local equations, using %s process layout\n", ((PetscObject)pc)->prefix, *a_nactive_proc, new_size, ncrs_eq, (pc_gamg->layout_type == PCGAMG_LAYOUT_COMPACT) ? "compact" : "spread"));
192a2f3521dSMark F. Adams       /* get 'adj' */
193c5bfad50SMark F. Adams       if (cr_bs == 1) {
1949566063dSJacob Faibussowitsch         PetscCall(MatConvert(Cmat, MATMPIADJ, MAT_INITIAL_MATRIX, &adj));
195806fa848SBarry Smith       } else {
196a2f3521dSMark F. Adams         /* make a scalar matrix to partition (no Stokes here) */
197eb07cef2SMark F. Adams         Mat                tMat;
198a2f3521dSMark F. Adams         PetscInt           Istart_crs, Iend_crs, ncols, jj, Ii;
199b4fbaa2aSMark F. Adams         const PetscScalar *vals;
200b4fbaa2aSMark F. Adams         const PetscInt    *idx;
201a2f3521dSMark F. Adams         PetscInt          *d_nnz, *o_nnz, M, N;
20239d09545SMark Adams         static PetscInt    llev = 0; /* ugly but just used for debugging */
203d9558ea9SBarry Smith         MatType            mtype;
204b4fbaa2aSMark F. Adams 
2059566063dSJacob Faibussowitsch         PetscCall(PetscMalloc2(ncrs, &d_nnz, ncrs, &o_nnz));
2069566063dSJacob Faibussowitsch         PetscCall(MatGetOwnershipRange(Cmat, &Istart_crs, &Iend_crs));
2079566063dSJacob Faibussowitsch         PetscCall(MatGetSize(Cmat, &M, &N));
208c5bfad50SMark F. Adams         for (Ii = Istart_crs, jj = 0; Ii < Iend_crs; Ii += cr_bs, jj++) {
2099566063dSJacob Faibussowitsch           PetscCall(MatGetRow(Cmat, Ii, &ncols, NULL, NULL));
210c5bfad50SMark F. Adams           d_nnz[jj] = ncols / cr_bs;
211c5bfad50SMark F. Adams           o_nnz[jj] = ncols / cr_bs;
2129566063dSJacob Faibussowitsch           PetscCall(MatRestoreRow(Cmat, Ii, &ncols, NULL, NULL));
2133ae0bb68SMark Adams           if (d_nnz[jj] > ncrs) d_nnz[jj] = ncrs;
2143ae0bb68SMark Adams           if (o_nnz[jj] > (M / cr_bs - ncrs)) o_nnz[jj] = M / cr_bs - ncrs;
21558471d46SMark F. Adams         }
2166876a03eSMark F. Adams 
2179566063dSJacob Faibussowitsch         PetscCall(MatGetType(Amat_fine, &mtype));
2189566063dSJacob Faibussowitsch         PetscCall(MatCreate(comm, &tMat));
2199566063dSJacob Faibussowitsch         PetscCall(MatSetSizes(tMat, ncrs, ncrs, PETSC_DETERMINE, PETSC_DETERMINE));
2209566063dSJacob Faibussowitsch         PetscCall(MatSetType(tMat, mtype));
2219566063dSJacob Faibussowitsch         PetscCall(MatSeqAIJSetPreallocation(tMat, 0, d_nnz));
2229566063dSJacob Faibussowitsch         PetscCall(MatMPIAIJSetPreallocation(tMat, 0, d_nnz, 0, o_nnz));
2239566063dSJacob Faibussowitsch         PetscCall(PetscFree2(d_nnz, o_nnz));
224eb07cef2SMark F. Adams 
225a2f3521dSMark F. Adams         for (ii = Istart_crs; ii < Iend_crs; ii++) {
226c5bfad50SMark F. Adams           PetscInt dest_row = ii / cr_bs;
2279566063dSJacob Faibussowitsch           PetscCall(MatGetRow(Cmat, ii, &ncols, &idx, &vals));
228eb07cef2SMark F. Adams           for (jj = 0; jj < ncols; jj++) {
229c5bfad50SMark F. Adams             PetscInt    dest_col = idx[jj] / cr_bs;
230eb07cef2SMark F. Adams             PetscScalar v        = 1.0;
2319566063dSJacob Faibussowitsch             PetscCall(MatSetValues(tMat, 1, &dest_row, 1, &dest_col, &v, ADD_VALUES));
232eb07cef2SMark F. Adams           }
2339566063dSJacob Faibussowitsch           PetscCall(MatRestoreRow(Cmat, ii, &ncols, &idx, &vals));
234eb07cef2SMark F. Adams         }
2359566063dSJacob Faibussowitsch         PetscCall(MatAssemblyBegin(tMat, MAT_FINAL_ASSEMBLY));
2369566063dSJacob Faibussowitsch         PetscCall(MatAssemblyEnd(tMat, MAT_FINAL_ASSEMBLY));
237eb07cef2SMark F. Adams 
238b4fbaa2aSMark F. Adams         if (llev++ == -1) {
2399371c9d4SSatish Balay           PetscViewer viewer;
2409371c9d4SSatish Balay           char        fname[32];
24163a3b9bcSJacob Faibussowitsch           PetscCall(PetscSNPrintf(fname, sizeof(fname), "part_mat_%" PetscInt_FMT ".mat", llev));
2423ba16761SJacob Faibussowitsch           PetscCall(PetscViewerBinaryOpen(comm, fname, FILE_MODE_WRITE, &viewer));
2439566063dSJacob Faibussowitsch           PetscCall(MatView(tMat, viewer));
2449566063dSJacob Faibussowitsch           PetscCall(PetscViewerDestroy(&viewer));
245b4fbaa2aSMark F. Adams         }
2469566063dSJacob Faibussowitsch         PetscCall(MatConvert(tMat, MATMPIADJ, MAT_INITIAL_MATRIX, &adj));
2479566063dSJacob Faibussowitsch         PetscCall(MatDestroy(&tMat));
248a2f3521dSMark F. Adams       } /* create 'adj' */
249f150b916SMark F. Adams 
250a2f3521dSMark F. Adams       { /* partition: get newproc_idx */
2515a9b9e01SMark F. Adams         char            prefix[256];
2525a9b9e01SMark F. Adams         const char     *pcpre;
253b4fbaa2aSMark F. Adams         const PetscInt *is_idx;
254b4fbaa2aSMark F. Adams         MatPartitioning mpart;
255a4b7d37bSMark F. Adams         IS              proc_is;
2562f03bc48SMark F. Adams 
2579566063dSJacob Faibussowitsch         PetscCall(MatPartitioningCreate(comm, &mpart));
2589566063dSJacob Faibussowitsch         PetscCall(MatPartitioningSetAdjacency(mpart, adj));
2599566063dSJacob Faibussowitsch         PetscCall(PCGetOptionsPrefix(pc, &pcpre));
2609566063dSJacob Faibussowitsch         PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_gamg_", pcpre ? pcpre : ""));
2619566063dSJacob Faibussowitsch         PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mpart, prefix));
2629566063dSJacob Faibussowitsch         PetscCall(MatPartitioningSetFromOptions(mpart));
2639566063dSJacob Faibussowitsch         PetscCall(MatPartitioningSetNParts(mpart, new_size));
2649566063dSJacob Faibussowitsch         PetscCall(MatPartitioningApply(mpart, &proc_is));
2659566063dSJacob Faibussowitsch         PetscCall(MatPartitioningDestroy(&mpart));
2665a9b9e01SMark F. Adams 
2675ef31b24SMark F. Adams         /* collect IS info */
2689566063dSJacob Faibussowitsch         PetscCall(PetscMalloc1(ncrs_eq, &newproc_idx));
2699566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(proc_is, &is_idx));
270a2f3521dSMark F. Adams         for (kk = jj = 0; kk < nloc_old; kk++) {
2719371c9d4SSatish Balay           for (ii = 0; ii < cr_bs; ii++, jj++) { newproc_idx[jj] = is_idx[kk] * expand_factor; /* distribution */ }
2725ef31b24SMark F. Adams         }
2739566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(proc_is, &is_idx));
2749566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&proc_is));
2755ef31b24SMark F. Adams       }
2769566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&adj));
2775a9b9e01SMark F. Adams 
2789566063dSJacob Faibussowitsch       PetscCall(ISCreateGeneral(comm, ncrs_eq, newproc_idx, PETSC_COPY_VALUES, &is_eq_newproc));
2799566063dSJacob Faibussowitsch       PetscCall(PetscFree(newproc_idx));
280849bee69SMark Adams       PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_REPART], 0, 0, 0, 0));
28131cb4603SMark Adams     } else { /* simple aggregation of parts -- 'is_eq_newproc' */
282ce7c7f2fSMark Adams       PetscInt targetPE;
28308401ef6SPierre Jolivet       PetscCheck(new_size != nactive, PETSC_COMM_SELF, PETSC_ERR_PLIB, "new_size==nactive. Should not happen");
28463a3b9bcSJacob Faibussowitsch       PetscCall(PetscInfo(pc, "%s: Number of equations (loc) %" PetscInt_FMT " with simple aggregation\n", ((PetscObject)pc)->prefix, ncrs_eq));
285ce7c7f2fSMark Adams       targetPE = (rank / rfactor) * expand_factor;
2869566063dSJacob Faibussowitsch       PetscCall(ISCreateStride(comm, ncrs_eq, targetPE, 0, &is_eq_newproc));
287a2f3521dSMark F. Adams     } /* end simple 'is_eq_newproc' */
288e33ef3b1SMark F. Adams 
28911e60469SMark F. Adams     /*
290a2f3521dSMark F. Adams       Create an index set from the is_eq_newproc index set to indicate the mapping TO
29111e60469SMark F. Adams     */
2929566063dSJacob Faibussowitsch     PetscCall(ISPartitioningToNumbering(is_eq_newproc, &is_eq_num));
2937700e67bSMark Adams     is_eq_num_prim = is_eq_num;
29411e60469SMark F. Adams     /*
295a2f3521dSMark F. Adams       Determine how many equations/vertices are assigned to each processor
29611e60469SMark F. Adams     */
2979566063dSJacob Faibussowitsch     PetscCall(ISPartitioningCount(is_eq_newproc, size, counts));
298c5df96a5SBarry Smith     ncrs_eq_new = counts[rank];
2999566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&is_eq_newproc));
300ce7c7f2fSMark Adams     ncrs_new = ncrs_eq_new / cr_bs;
301a2f3521dSMark F. Adams 
3029566063dSJacob Faibussowitsch     PetscCall(PetscFree(counts));
3036aad120cSJose E. Roman     /* data movement scope -- this could be moved to subclasses so that we don't try to cram all auxiliary data into some complex abstracted thing */
304885364a3SMark Adams     {
305885364a3SMark Adams       Vec             src_crd, dest_crd;
306885364a3SMark Adams       const PetscInt *idx, ndata_rows = pc_gamg->data_cell_rows, ndata_cols = pc_gamg->data_cell_cols, node_data_sz = ndata_rows * ndata_cols;
307885364a3SMark Adams       VecScatter      vecscat;
308885364a3SMark Adams       PetscScalar    *array;
309885364a3SMark Adams       IS              isscat;
310a2f3521dSMark F. Adams       /* move data (for primal equations only) */
31122063be5SMark F. Adams       /* Create a vector to contain the newly ordered element information */
3129566063dSJacob Faibussowitsch       PetscCall(VecCreate(comm, &dest_crd));
3139566063dSJacob Faibussowitsch       PetscCall(VecSetSizes(dest_crd, node_data_sz * ncrs_new, PETSC_DECIDE));
3149566063dSJacob Faibussowitsch       PetscCall(VecSetType(dest_crd, VECSTANDARD)); /* this is needed! */
31511e60469SMark F. Adams       /*
3169d5b6da9SMark F. Adams         There are 'ndata_rows*ndata_cols' data items per node, (one can think of the vectors of having
317c5bfad50SMark F. Adams         a block size of ...).  Note, ISs are expanded into equation space by 'cr_bs'.
31811e60469SMark F. Adams       */
3199566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(ncrs * node_data_sz, &tidx));
3209566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(is_eq_num_prim, &idx));
3213ae0bb68SMark Adams       for (ii = 0, jj = 0; ii < ncrs; ii++) {
322c5bfad50SMark F. Adams         PetscInt id = idx[ii * cr_bs] / cr_bs; /* get node back */
323a2f3521dSMark F. Adams         for (kk = 0; kk < node_data_sz; kk++, jj++) tidx[jj] = id * node_data_sz + kk;
32411e60469SMark F. Adams       }
3259566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(is_eq_num_prim, &idx));
3269566063dSJacob Faibussowitsch       PetscCall(ISCreateGeneral(comm, node_data_sz * ncrs, tidx, PETSC_COPY_VALUES, &isscat));
3279566063dSJacob Faibussowitsch       PetscCall(PetscFree(tidx));
32811e60469SMark F. Adams       /*
32911e60469SMark F. Adams         Create a vector to contain the original vertex information for each element
33011e60469SMark F. Adams       */
3319566063dSJacob Faibussowitsch       PetscCall(VecCreateSeq(PETSC_COMM_SELF, node_data_sz * ncrs, &src_crd));
3329d5b6da9SMark F. Adams       for (jj = 0; jj < ndata_cols; jj++) {
3333ae0bb68SMark Adams         const PetscInt stride0 = ncrs * pc_gamg->data_cell_rows;
3343ae0bb68SMark Adams         for (ii = 0; ii < ncrs; ii++) {
3359d5b6da9SMark F. Adams           for (kk = 0; kk < ndata_rows; kk++) {
336a2f3521dSMark F. Adams             PetscInt    ix = ii * ndata_rows + kk + jj * stride0, jx = ii * node_data_sz + kk * ndata_cols + jj;
337c8b0795cSMark F. Adams             PetscScalar tt = (PetscScalar)pc_gamg->data[ix];
3389566063dSJacob Faibussowitsch             PetscCall(VecSetValues(src_crd, 1, &jx, &tt, INSERT_VALUES));
339d3d6bff4SMark F. Adams           }
340038e3b61SMark F. Adams         }
341eb07cef2SMark F. Adams       }
3429566063dSJacob Faibussowitsch       PetscCall(VecAssemblyBegin(src_crd));
3439566063dSJacob Faibussowitsch       PetscCall(VecAssemblyEnd(src_crd));
34411e60469SMark F. Adams       /*
34511e60469SMark F. Adams         Scatter the element vertex information (still in the original vertex ordering)
34611e60469SMark F. Adams         to the correct processor
34711e60469SMark F. Adams       */
3489566063dSJacob Faibussowitsch       PetscCall(VecScatterCreate(src_crd, NULL, dest_crd, isscat, &vecscat));
3499566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&isscat));
3509566063dSJacob Faibussowitsch       PetscCall(VecScatterBegin(vecscat, src_crd, dest_crd, INSERT_VALUES, SCATTER_FORWARD));
3519566063dSJacob Faibussowitsch       PetscCall(VecScatterEnd(vecscat, src_crd, dest_crd, INSERT_VALUES, SCATTER_FORWARD));
3529566063dSJacob Faibussowitsch       PetscCall(VecScatterDestroy(&vecscat));
3539566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&src_crd));
35411e60469SMark F. Adams       /*
35511e60469SMark F. Adams         Put the element vertex data into a new allocation of the gdata->ele
35611e60469SMark F. Adams       */
3579566063dSJacob Faibussowitsch       PetscCall(PetscFree(pc_gamg->data));
3589566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(node_data_sz * ncrs_new, &pc_gamg->data));
3592fa5cd67SKarl Rupp 
3603ae0bb68SMark Adams       pc_gamg->data_sz = node_data_sz * ncrs_new;
3613ae0bb68SMark Adams       strideNew        = ncrs_new * ndata_rows;
3622fa5cd67SKarl Rupp 
3639566063dSJacob Faibussowitsch       PetscCall(VecGetArray(dest_crd, &array));
3649d5b6da9SMark F. Adams       for (jj = 0; jj < ndata_cols; jj++) {
3653ae0bb68SMark Adams         for (ii = 0; ii < ncrs_new; ii++) {
3669d5b6da9SMark F. Adams           for (kk = 0; kk < ndata_rows; kk++) {
367a2f3521dSMark F. Adams             PetscInt ix = ii * ndata_rows + kk + jj * strideNew, jx = ii * node_data_sz + kk * ndata_cols + jj;
368c8b0795cSMark F. Adams             pc_gamg->data[ix] = PetscRealPart(array[jx]);
369d3d6bff4SMark F. Adams           }
370038e3b61SMark F. Adams         }
371038e3b61SMark F. Adams       }
3729566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(dest_crd, &array));
3739566063dSJacob Faibussowitsch       PetscCall(VecDestroy(&dest_crd));
374885364a3SMark Adams     }
375a2f3521dSMark F. Adams     /* move A and P (columns) with new layout */
376849bee69SMark Adams     /* PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[SET13],0,0,0,0)); */
37711e60469SMark F. Adams     /*
3787dae84e0SHong Zhang       Invert for MatCreateSubMatrix
37911e60469SMark F. Adams     */
3809566063dSJacob Faibussowitsch     PetscCall(ISInvertPermutation(is_eq_num, ncrs_eq_new, &new_eq_indices));
3819566063dSJacob Faibussowitsch     PetscCall(ISSort(new_eq_indices)); /* is this needed? */
3829566063dSJacob Faibussowitsch     PetscCall(ISSetBlockSize(new_eq_indices, cr_bs));
3839371c9d4SSatish Balay     if (is_eq_num != is_eq_num_prim) { PetscCall(ISDestroy(&is_eq_num_prim)); /* could be same as 'is_eq_num' */ }
3843cb8563fSToby Isaac     if (Pcolumnperm) {
3859566063dSJacob Faibussowitsch       PetscCall(PetscObjectReference((PetscObject)new_eq_indices));
3863cb8563fSToby Isaac       *Pcolumnperm = new_eq_indices;
3873cb8563fSToby Isaac     }
3889566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&is_eq_num));
389849bee69SMark Adams     /* PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[SET13],0,0,0,0)); */
390849bee69SMark Adams     /* PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[SET14],0,0,0,0)); */
391849bee69SMark Adams 
392a2f3521dSMark F. Adams     /* 'a_Amat_crs' output */
393a2f3521dSMark F. Adams     {
394a2f3521dSMark F. Adams       Mat       mat;
395b94d7dedSBarry Smith       PetscBool isset, isspd, isher;
39690db8557SMark Adams #if !defined(PETSC_USE_COMPLEX)
397b94d7dedSBarry Smith       PetscBool issym;
398b94d7dedSBarry Smith #endif
399b94d7dedSBarry Smith 
400b94d7dedSBarry Smith       PetscCall(MatCreateSubMatrix(Cmat, new_eq_indices, new_eq_indices, MAT_INITIAL_MATRIX, &mat));
401b94d7dedSBarry Smith       PetscCall(MatIsSPDKnown(Cmat, &isset, &isspd)); // like MatPropagateSymmetryOptions, but should set MAT_STRUCTURALLY_SYMMETRIC ?
402b94d7dedSBarry Smith       if (isset) PetscCall(MatSetOption(mat, MAT_SPD, isspd));
403b94d7dedSBarry Smith       else {
404b94d7dedSBarry Smith         PetscCall(MatIsHermitianKnown(Cmat, &isset, &isher));
405b94d7dedSBarry Smith         if (isset) PetscCall(MatSetOption(mat, MAT_HERMITIAN, isher));
406b94d7dedSBarry Smith         else {
407b94d7dedSBarry Smith #if !defined(PETSC_USE_COMPLEX)
408b94d7dedSBarry Smith           PetscCall(MatIsSymmetricKnown(Cmat, &isset, &issym));
409b94d7dedSBarry Smith           if (isset) PetscCall(MatSetOption(mat, MAT_SYMMETRIC, issym));
41090db8557SMark Adams #endif
41190db8557SMark Adams         }
41290db8557SMark Adams       }
413a2f3521dSMark F. Adams       *a_Amat_crs = mat;
414a2f3521dSMark F. Adams     }
4159566063dSJacob Faibussowitsch     PetscCall(MatDestroy(&Cmat));
416a2f3521dSMark F. Adams 
417849bee69SMark Adams     /* PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[SET14],0,0,0,0)); */
41811e60469SMark F. Adams     /* prolongator */
41911e60469SMark F. Adams     {
42011e60469SMark F. Adams       IS       findices;
421a2f3521dSMark F. Adams       PetscInt Istart, Iend;
422a2f3521dSMark F. Adams       Mat      Pnew;
42362294041SBarry Smith 
4249566063dSJacob Faibussowitsch       PetscCall(MatGetOwnershipRange(Pold, &Istart, &Iend));
425849bee69SMark Adams       /* PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[SET15],0,0,0,0)); */
4269566063dSJacob Faibussowitsch       PetscCall(ISCreateStride(comm, Iend - Istart, Istart, 1, &findices));
4279566063dSJacob Faibussowitsch       PetscCall(ISSetBlockSize(findices, f_bs));
4289566063dSJacob Faibussowitsch       PetscCall(MatCreateSubMatrix(Pold, findices, new_eq_indices, MAT_INITIAL_MATRIX, &Pnew));
4299566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&findices));
4309566063dSJacob Faibussowitsch       PetscCall(MatSetOption(Pnew, MAT_FORM_EXPLICIT_TRANSPOSE, PETSC_TRUE));
431c5bfad50SMark F. Adams 
432849bee69SMark Adams       /* PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[SET15],0,0,0,0)); */
4339566063dSJacob Faibussowitsch       PetscCall(MatDestroy(a_P_inout));
434a2f3521dSMark F. Adams 
435a2f3521dSMark F. Adams       /* output - repartitioned */
436a2f3521dSMark F. Adams       *a_P_inout = Pnew;
437e33ef3b1SMark F. Adams     }
4389566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&new_eq_indices));
4395b89ad90SMark F. Adams 
440c5df96a5SBarry Smith     *a_nactive_proc = new_size; /* output */
441ce7c7f2fSMark Adams 
442ce7c7f2fSMark Adams     /* pinning on reduced grids, not a bad heuristic and optimization gets folded into process reduction optimization */
443ce7c7f2fSMark Adams     if (pc_gamg->cpu_pin_coarse_grids) {
444ce7c7f2fSMark Adams #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
4458bca76a6SMark Adams       static PetscInt llev = 2;
44663a3b9bcSJacob Faibussowitsch       PetscCall(PetscInfo(pc, "%s: Pinning level %" PetscInt_FMT " to the CPU\n", ((PetscObject)pc)->prefix, llev++));
447ce7c7f2fSMark Adams #endif
4489566063dSJacob Faibussowitsch       PetscCall(MatBindToCPU(*a_Amat_crs, PETSC_TRUE));
4499566063dSJacob Faibussowitsch       PetscCall(MatBindToCPU(*a_P_inout, PETSC_TRUE));
450adf5291fSStefano Zampini       if (1) { /* HACK: move this to MatBindCPU_MPIAIJXXX; lvec is created, need to pin it, this is done in MatSetUpMultiply_MPIAIJ. Hack */
451ce7c7f2fSMark Adams         Mat         A = *a_Amat_crs, P = *a_P_inout;
452ce7c7f2fSMark Adams         PetscMPIInt size;
4539566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
454ce7c7f2fSMark Adams         if (size > 1) {
455ce7c7f2fSMark Adams           Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data, *p = (Mat_MPIAIJ *)P->data;
4569566063dSJacob Faibussowitsch           PetscCall(VecBindToCPU(a->lvec, PETSC_TRUE));
4579566063dSJacob Faibussowitsch           PetscCall(VecBindToCPU(p->lvec, PETSC_TRUE));
458ce7c7f2fSMark Adams         }
459ce7c7f2fSMark Adams       }
460ce7c7f2fSMark Adams     }
461849bee69SMark Adams     PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_REDUCE], 0, 0, 0, 0));
462849bee69SMark Adams   } // processor reduce
4633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4645b89ad90SMark F. Adams }
4655b89ad90SMark F. Adams 
466bae903cbSmarkadams4 // used in GEO
467d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSquareGraph_GAMG(PC a_pc, Mat Gmat1, Mat *Gmat2)
468d71ae5a4SJacob Faibussowitsch {
4694b1575e2SStefano Zampini   const char *prefix;
4704b1575e2SStefano Zampini   char        addp[32];
4714b1575e2SStefano Zampini   PC_MG      *mg      = (PC_MG *)a_pc->data;
4724b1575e2SStefano Zampini   PC_GAMG    *pc_gamg = (PC_GAMG *)mg->innerctx;
4734b1575e2SStefano Zampini 
4744b1575e2SStefano Zampini   PetscFunctionBegin;
4759566063dSJacob Faibussowitsch   PetscCall(PCGetOptionsPrefix(a_pc, &prefix));
47663a3b9bcSJacob Faibussowitsch   PetscCall(PetscInfo(a_pc, "%s: Square Graph on level %" PetscInt_FMT "\n", ((PetscObject)a_pc)->prefix, pc_gamg->current_level + 1));
4779566063dSJacob Faibussowitsch   PetscCall(MatProductCreate(Gmat1, Gmat1, NULL, Gmat2));
4789566063dSJacob Faibussowitsch   PetscCall(MatSetOptionsPrefix(*Gmat2, prefix));
47963a3b9bcSJacob Faibussowitsch   PetscCall(PetscSNPrintf(addp, sizeof(addp), "pc_gamg_square_%" PetscInt_FMT "_", pc_gamg->current_level));
4809566063dSJacob Faibussowitsch   PetscCall(MatAppendOptionsPrefix(*Gmat2, addp));
481b94d7dedSBarry Smith   if ((*Gmat2)->structurally_symmetric == PETSC_BOOL3_TRUE) {
4829566063dSJacob Faibussowitsch     PetscCall(MatProductSetType(*Gmat2, MATPRODUCT_AB));
483b4da3a1bSStefano Zampini   } else {
4849566063dSJacob Faibussowitsch     PetscCall(MatSetOption(Gmat1, MAT_FORM_EXPLICIT_TRANSPOSE, PETSC_TRUE));
4859566063dSJacob Faibussowitsch     PetscCall(MatProductSetType(*Gmat2, MATPRODUCT_AtB));
486b4da3a1bSStefano Zampini   }
4879566063dSJacob Faibussowitsch   PetscCall(MatProductSetFromOptions(*Gmat2));
4889566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(petsc_gamg_setup_matmat_events[pc_gamg->current_level][0], 0, 0, 0, 0));
4899566063dSJacob Faibussowitsch   PetscCall(MatProductSymbolic(*Gmat2));
4909566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(petsc_gamg_setup_matmat_events[pc_gamg->current_level][0], 0, 0, 0, 0));
4919566063dSJacob Faibussowitsch   PetscCall(MatProductClear(*Gmat2));
4924b1575e2SStefano Zampini   /* we only need the sparsity, cheat and tell PETSc the matrix has been assembled */
4934b1575e2SStefano Zampini   (*Gmat2)->assembled = PETSC_TRUE;
4943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4954b1575e2SStefano Zampini }
4964b1575e2SStefano Zampini 
4975b89ad90SMark F. Adams /*
4985b89ad90SMark F. Adams    PCSetUp_GAMG - Prepares for the use of the GAMG preconditioner
4995b89ad90SMark F. Adams                     by setting data structures and options.
5005b89ad90SMark F. Adams 
5015b89ad90SMark F. Adams    Input Parameter:
5025b89ad90SMark F. Adams .  pc - the preconditioner context
5035b89ad90SMark F. Adams 
5045b89ad90SMark F. Adams */
50566976f2fSJacob Faibussowitsch static PetscErrorCode PCSetUp_GAMG(PC pc)
506d71ae5a4SJacob Faibussowitsch {
5079d5b6da9SMark F. Adams   PC_MG      *mg      = (PC_MG *)pc->data;
5085b89ad90SMark F. Adams   PC_GAMG    *pc_gamg = (PC_GAMG *)mg->innerctx;
5092adcac29SMark F. Adams   Mat         Pmat    = pc->pmat;
51018c3aa7eSMark   PetscInt    fine_level, level, level1, bs, M, N, qq, lidx, nASMBlocksArr[PETSC_MG_MAXLEVELS];
5113b4367a7SBarry Smith   MPI_Comm    comm;
512c5df96a5SBarry Smith   PetscMPIInt rank, size, nactivepe;
51318c3aa7eSMark   Mat         Aarr[PETSC_MG_MAXLEVELS], Parr[PETSC_MG_MAXLEVELS];
51418c3aa7eSMark   IS         *ASMLocalIDsArr[PETSC_MG_MAXLEVELS];
5154279555eSSatish Balay   PetscBool   is_last = PETSC_FALSE;
5164279555eSSatish Balay #if defined(PETSC_USE_INFO)
517a2f3521dSMark F. Adams   PetscLogDouble nnz0 = 0., nnztot = 0.;
518569f4572SMark Adams   MatInfo        info;
5194279555eSSatish Balay #endif
5205ef31b24SMark F. Adams 
5215b89ad90SMark F. Adams   PetscFunctionBegin;
5229566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
5239566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
5249566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
525849bee69SMark Adams   PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[GAMG_SETUP], 0, 0, 0, 0));
5268abdc6daSStefano Zampini   if (pc->setupcalled) {
5278abdc6daSStefano Zampini     if (!pc_gamg->reuse_prol || pc->flag == DIFFERENT_NONZERO_PATTERN) {
528878e152fSMark F. Adams       /* reset everything */
5299566063dSJacob Faibussowitsch       PetscCall(PCReset_MG(pc));
530878e152fSMark F. Adams       pc->setupcalled = 0;
531806fa848SBarry Smith     } else {
53284d3f75bSMark F. Adams       PC_MG_Levels **mglevels = mg->levels;
53303a628feSMark F. Adams       /* just do Galerkin grids */
53458471d46SMark F. Adams       Mat B, dA, dB;
5359d5b6da9SMark F. Adams       if (pc_gamg->Nlevels > 1) {
5364555aa8cSStefano Zampini         PetscInt gl;
53758471d46SMark F. Adams         /* currently only handle case where mat and pmat are the same on coarser levels */
5389566063dSJacob Faibussowitsch         PetscCall(KSPGetOperators(mglevels[pc_gamg->Nlevels - 1]->smoothd, &dA, &dB));
53958471d46SMark F. Adams         /* (re)set to get dirty flag */
5409566063dSJacob Faibussowitsch         PetscCall(KSPSetOperators(mglevels[pc_gamg->Nlevels - 1]->smoothd, dA, dB));
54158471d46SMark F. Adams 
5424555aa8cSStefano Zampini         for (level = pc_gamg->Nlevels - 2, gl = 0; level >= 0; level--, gl++) {
5438abdc6daSStefano Zampini           MatReuse reuse = MAT_INITIAL_MATRIX;
544849bee69SMark Adams #if defined(GAMG_STAGES)
545849bee69SMark Adams           PetscCall(PetscLogStagePush(gamg_stages[gl]));
546849bee69SMark Adams #endif
5478abdc6daSStefano Zampini           /* matrix structure can change from repartitioning or process reduction but don't know if we have process reduction here. Should fix */
5489566063dSJacob Faibussowitsch           PetscCall(KSPGetOperators(mglevels[level]->smoothd, NULL, &B));
5498abdc6daSStefano Zampini           if (B->product) {
550ad540459SPierre Jolivet             if (B->product->A == dB && B->product->B == mglevels[level + 1]->interpolate) reuse = MAT_REUSE_MATRIX;
5518abdc6daSStefano Zampini           }
5529566063dSJacob Faibussowitsch           if (reuse == MAT_INITIAL_MATRIX) PetscCall(MatDestroy(&mglevels[level]->A));
5538abdc6daSStefano Zampini           if (reuse == MAT_REUSE_MATRIX) {
55463a3b9bcSJacob Faibussowitsch             PetscCall(PetscInfo(pc, "%s: RAP after first solve, reuse matrix level %" PetscInt_FMT "\n", ((PetscObject)pc)->prefix, level));
5558abdc6daSStefano Zampini           } else {
55663a3b9bcSJacob Faibussowitsch             PetscCall(PetscInfo(pc, "%s: RAP after first solve, new matrix level %" PetscInt_FMT "\n", ((PetscObject)pc)->prefix, level));
5578abdc6daSStefano Zampini           }
5589566063dSJacob Faibussowitsch           PetscCall(PetscLogEventBegin(petsc_gamg_setup_matmat_events[gl][1], 0, 0, 0, 0));
5599566063dSJacob Faibussowitsch           PetscCall(MatPtAP(dB, mglevels[level + 1]->interpolate, reuse, PETSC_DEFAULT, &B));
5609566063dSJacob Faibussowitsch           PetscCall(PetscLogEventEnd(petsc_gamg_setup_matmat_events[gl][1], 0, 0, 0, 0));
56163b77682SMark Adams           if (reuse == MAT_INITIAL_MATRIX) mglevels[level]->A = B;
5629566063dSJacob Faibussowitsch           PetscCall(KSPSetOperators(mglevels[level]->smoothd, B, B));
563*e1cf1444SMark Adams           // check for redoing eigen estimates
564*e1cf1444SMark Adams           if (pc_gamg->recompute_esteig) {
565*e1cf1444SMark Adams             PetscBool ischeb;
566*e1cf1444SMark Adams             KSP       smoother;
567*e1cf1444SMark Adams             PetscCall(PCMGGetSmoother(pc, level + 1, &smoother));
568*e1cf1444SMark Adams             PetscCall(PetscObjectTypeCompare((PetscObject)smoother, KSPCHEBYSHEV, &ischeb));
569*e1cf1444SMark Adams             if (ischeb) {
570*e1cf1444SMark Adams               KSP_Chebyshev *cheb = (KSP_Chebyshev *)smoother->data;
571*e1cf1444SMark Adams               cheb->emin_provided = 0;
572*e1cf1444SMark Adams               cheb->emax_provided = 0;
573*e1cf1444SMark Adams             }
574*e1cf1444SMark Adams             /* we could call PetscCall(KSPChebyshevSetEigenvalues(smoother, 0, 0)); but the logic does not work properly */
575*e1cf1444SMark Adams           }
576*e1cf1444SMark Adams           // inc
57758471d46SMark F. Adams           dB = B;
578849bee69SMark Adams #if defined(GAMG_STAGES)
579849bee69SMark Adams           PetscCall(PetscLogStagePop());
580849bee69SMark Adams #endif
58158471d46SMark F. Adams         }
5825f8cf99dSMark F. Adams       }
583d5280255SMark F. Adams 
5849566063dSJacob Faibussowitsch       PetscCall(PCSetUp_MG(pc));
585849bee69SMark Adams       PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_SETUP], 0, 0, 0, 0));
5863ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
587eb07cef2SMark F. Adams     }
588878e152fSMark F. Adams   }
589f6536408SMark F. Adams 
590878e152fSMark F. Adams   if (!pc_gamg->data) {
591878e152fSMark F. Adams     if (pc_gamg->orig_data) {
5929566063dSJacob Faibussowitsch       PetscCall(MatGetBlockSize(Pmat, &bs));
5939566063dSJacob Faibussowitsch       PetscCall(MatGetLocalSize(Pmat, &qq, NULL));
5942fa5cd67SKarl Rupp 
595878e152fSMark F. Adams       pc_gamg->data_sz        = (qq / bs) * pc_gamg->orig_data_cell_rows * pc_gamg->orig_data_cell_cols;
596878e152fSMark F. Adams       pc_gamg->data_cell_rows = pc_gamg->orig_data_cell_rows;
597878e152fSMark F. Adams       pc_gamg->data_cell_cols = pc_gamg->orig_data_cell_cols;
5982fa5cd67SKarl Rupp 
5999566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(pc_gamg->data_sz, &pc_gamg->data));
600878e152fSMark F. Adams       for (qq = 0; qq < pc_gamg->data_sz; qq++) pc_gamg->data[qq] = pc_gamg->orig_data[qq];
601806fa848SBarry Smith     } else {
6025f80ce2aSJacob Faibussowitsch       PetscCheck(pc_gamg->ops->createdefaultdata, comm, PETSC_ERR_PLIB, "'createdefaultdata' not set(?) need to support NULL data");
6039566063dSJacob Faibussowitsch       PetscCall(pc_gamg->ops->createdefaultdata(pc, Pmat));
6049d5b6da9SMark F. Adams     }
605878e152fSMark F. Adams   }
606878e152fSMark F. Adams 
607878e152fSMark F. Adams   /* cache original data for reuse */
6081c1aac46SBarry Smith   if (!pc_gamg->orig_data && (PetscBool)(!pc_gamg->reuse_prol)) {
6099566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(pc_gamg->data_sz, &pc_gamg->orig_data));
610878e152fSMark F. Adams     for (qq = 0; qq < pc_gamg->data_sz; qq++) pc_gamg->orig_data[qq] = pc_gamg->data[qq];
611878e152fSMark F. Adams     pc_gamg->orig_data_cell_rows = pc_gamg->data_cell_rows;
612878e152fSMark F. Adams     pc_gamg->orig_data_cell_cols = pc_gamg->data_cell_cols;
613878e152fSMark F. Adams   }
614038e3b61SMark F. Adams 
615302f38e8SMark F. Adams   /* get basic dims */
6169566063dSJacob Faibussowitsch   PetscCall(MatGetBlockSize(Pmat, &bs));
6179566063dSJacob Faibussowitsch   PetscCall(MatGetSize(Pmat, &M, &N));
61884d3f75bSMark F. Adams 
6194279555eSSatish Balay #if defined(PETSC_USE_INFO)
6209566063dSJacob Faibussowitsch   PetscCall(MatGetInfo(Pmat, MAT_GLOBAL_SUM, &info)); /* global reduction */
621569f4572SMark Adams   nnz0   = info.nz_used;
622569f4572SMark Adams   nnztot = info.nz_used;
6234279555eSSatish Balay #endif
624bae903cbSmarkadams4   PetscCall(PetscInfo(pc, "%s: level %d) N=%" PetscInt_FMT ", n data rows=%" PetscInt_FMT ", n data cols=%" PetscInt_FMT ", nnz/row (ave)=%" PetscInt_FMT ", np=%d\n", ((PetscObject)pc)->prefix, 0, M, pc_gamg->data_cell_rows, pc_gamg->data_cell_cols, (PetscInt)(nnz0 / (PetscReal)M + 0.5), size));
625569f4572SMark Adams 
626a2f3521dSMark F. Adams   /* Get A_i and R_i */
62762294041SBarry Smith   for (level = 0, Aarr[0] = Pmat, nactivepe = size; level < (pc_gamg->Nlevels - 1) && (!level || M > pc_gamg->coarse_eq_limit); level++) {
6289ab59c8bSMark Adams     pc_gamg->current_level = level;
62963a3b9bcSJacob Faibussowitsch     PetscCheck(level < PETSC_MG_MAXLEVELS, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Too many levels %" PetscInt_FMT, level);
6305b89ad90SMark F. Adams     level1 = level + 1;
6314555aa8cSStefano Zampini #if defined(GAMG_STAGES)
632849bee69SMark Adams     if (!gamg_stages[level]) {
633849bee69SMark Adams       char str[32];
634a364092eSJacob Faibussowitsch       PetscCall(PetscSNPrintf(str, PETSC_STATIC_ARRAY_LENGTH(str), "GAMG Level %d", (int)level));
635849bee69SMark Adams       PetscCall(PetscLogStageRegister(str, &gamg_stages[level]));
636849bee69SMark Adams     }
6379566063dSJacob Faibussowitsch     PetscCall(PetscLogStagePush(gamg_stages[level]));
638b4fbaa2aSMark F. Adams #endif
639c8b0795cSMark F. Adams     { /* construct prolongator */
640725b86d8SJed Brown       Mat               Gmat;
6410cbbd2e1SMark F. Adams       PetscCoarsenData *agg_lists;
6427700e67bSMark Adams       Mat               Prol11;
643c8b0795cSMark F. Adams 
6442d776b49SBarry Smith       PetscCall(PCGAMGCreateGraph(pc, Aarr[level], &Gmat));
6459566063dSJacob Faibussowitsch       PetscCall(pc_gamg->ops->coarsen(pc, &Gmat, &agg_lists));
6469566063dSJacob Faibussowitsch       PetscCall(pc_gamg->ops->prolongator(pc, Aarr[level], Gmat, agg_lists, &Prol11));
647c8b0795cSMark F. Adams 
648a2f3521dSMark F. Adams       /* could have failed to create new level */
649a2f3521dSMark F. Adams       if (Prol11) {
650f7df55f0SStefano Zampini         const char *prefix;
651f7df55f0SStefano Zampini         char        addp[32];
652f7df55f0SStefano Zampini 
6539d5b6da9SMark F. Adams         /* get new block size of coarse matrices */
6549566063dSJacob Faibussowitsch         PetscCall(MatGetBlockSizes(Prol11, NULL, &bs));
655a2f3521dSMark F. Adams 
656fd1112cbSBarry Smith         if (pc_gamg->ops->optprolongator) {
657c8b0795cSMark F. Adams           /* smooth */
6589566063dSJacob Faibussowitsch           PetscCall(pc_gamg->ops->optprolongator(pc, Aarr[level], &Prol11));
659c8b0795cSMark F. Adams         }
660c8b0795cSMark F. Adams 
6610c3bc534SBarry Smith         if (pc_gamg->use_aggs_in_asm) {
6621b18a24aSMark Adams           PetscInt bs;
663849bee69SMark Adams           PetscCall(MatGetBlockSizes(Prol11, &bs, NULL)); // not timed directly, ugly, could remove, but good ASM method
6649566063dSJacob Faibussowitsch           PetscCall(PetscCDGetASMBlocks(agg_lists, bs, Gmat, &nASMBlocksArr[level], &ASMLocalIDsArr[level]));
665ffc955d6SMark F. Adams         }
666ffc955d6SMark F. Adams 
6679566063dSJacob Faibussowitsch         PetscCall(PCGetOptionsPrefix(pc, &prefix));
6689566063dSJacob Faibussowitsch         PetscCall(MatSetOptionsPrefix(Prol11, prefix));
6699566063dSJacob Faibussowitsch         PetscCall(PetscSNPrintf(addp, sizeof(addp), "pc_gamg_prolongator_%d_", (int)level));
6709566063dSJacob Faibussowitsch         PetscCall(MatAppendOptionsPrefix(Prol11, addp));
67191f31d3dSStefano Zampini         /* Always generate the transpose with CUDA
672f7df55f0SStefano Zampini            Such behaviour can be adapted with -pc_gamg_prolongator_ prefixed options */
6739566063dSJacob Faibussowitsch         PetscCall(MatSetOption(Prol11, MAT_FORM_EXPLICIT_TRANSPOSE, PETSC_TRUE));
6749566063dSJacob Faibussowitsch         PetscCall(MatSetFromOptions(Prol11));
6754bde40a0SMark Adams         Parr[level1] = Prol11;
6764bde40a0SMark Adams       } else Parr[level1] = NULL; /* failed to coarsen */
6774bde40a0SMark Adams 
6789566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&Gmat));
6799566063dSJacob Faibussowitsch       PetscCall(PetscCDDestroy(agg_lists));
680a2f3521dSMark F. Adams     }                           /* construct prolongator scope */
6817f66b68fSMark Adams     if (!level) Aarr[0] = Pmat; /* use Pmat for finest level setup */
682171cca9aSMark Adams     if (!Parr[level1]) {        /* failed to coarsen */
68363a3b9bcSJacob Faibussowitsch       PetscCall(PetscInfo(pc, "%s: Stop gridding, level %" PetscInt_FMT "\n", ((PetscObject)pc)->prefix, level));
6844555aa8cSStefano Zampini #if defined(GAMG_STAGES)
6859566063dSJacob Faibussowitsch       PetscCall(PetscLogStagePop());
686a90e85d9SMark Adams #endif
687c8b0795cSMark F. Adams       break;
688c8b0795cSMark F. Adams     }
6899566063dSJacob Faibussowitsch     PetscCall(MatGetSize(Parr[level1], &M, &N)); /* N is next M, a loop test variables */
6902472a847SBarry Smith     PetscCheck(!is_last, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Is last ?");
691171cca9aSMark Adams     if (N <= pc_gamg->coarse_eq_limit) is_last = PETSC_TRUE;
6920e2909e1SMark Adams     if (level1 == pc_gamg->Nlevels - 1) is_last = PETSC_TRUE;
693849bee69SMark Adams     PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[GAMG_LEVEL], 0, 0, 0, 0));
6949566063dSJacob Faibussowitsch     PetscCall(pc_gamg->ops->createlevel(pc, Aarr[level], bs, &Parr[level1], &Aarr[level1], &nactivepe, NULL, is_last));
695849bee69SMark Adams     PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_LEVEL], 0, 0, 0, 0));
696a2f3521dSMark F. Adams 
6979566063dSJacob Faibussowitsch     PetscCall(MatGetSize(Aarr[level1], &M, &N)); /* M is loop test variables */
6984279555eSSatish Balay #if defined(PETSC_USE_INFO)
6999566063dSJacob Faibussowitsch     PetscCall(MatGetInfo(Aarr[level1], MAT_GLOBAL_SUM, &info));
700569f4572SMark Adams     nnztot += info.nz_used;
7014279555eSSatish Balay #endif
702bae903cbSmarkadams4     PetscCall(PetscInfo(pc, "%s: %d) N=%" PetscInt_FMT ", n data cols=%" PetscInt_FMT ", nnz/row (ave)=%" PetscInt_FMT ", %d active pes\n", ((PetscObject)pc)->prefix, (int)level1, M, pc_gamg->data_cell_cols, (PetscInt)(info.nz_used / (PetscReal)M), nactivepe));
703569f4572SMark Adams 
7044555aa8cSStefano Zampini #if defined(GAMG_STAGES)
7059566063dSJacob Faibussowitsch     PetscCall(PetscLogStagePop());
706b4fbaa2aSMark F. Adams #endif
707a90e85d9SMark Adams     /* stop if one node or one proc -- could pull back for singular problems */
7089ab59c8bSMark Adams     if ((pc_gamg->data_cell_cols && M / pc_gamg->data_cell_cols < 2) || (!pc_gamg->data_cell_cols && M / bs < 2)) {
70963a3b9bcSJacob Faibussowitsch       PetscCall(PetscInfo(pc, "%s: HARD stop of coarsening on level %" PetscInt_FMT ".  Grid too small: %" PetscInt_FMT " block nodes\n", ((PetscObject)pc)->prefix, level, M / bs));
710a90e85d9SMark Adams       level++;
711a90e85d9SMark Adams       break;
712a90e85d9SMark Adams     }
713c8b0795cSMark F. Adams   } /* levels */
7149566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc_gamg->data));
715c8b0795cSMark F. Adams 
716ba4ceb06Smarkadams4   PetscCall(PetscInfo(pc, "%s: %" PetscInt_FMT " levels, operator complexity = %g\n", ((PetscObject)pc)->prefix, level + 1, nnztot / nnz0));
7179d5b6da9SMark F. Adams   pc_gamg->Nlevels = level + 1;
7185b89ad90SMark F. Adams   fine_level       = level;
7199566063dSJacob Faibussowitsch   PetscCall(PCMGSetLevels(pc, pc_gamg->Nlevels, NULL));
7205b89ad90SMark F. Adams 
72162294041SBarry Smith   if (pc_gamg->Nlevels > 1) { /* don't setup MG if one level */
7220ed2132dSStefano Zampini 
723d5280255SMark F. Adams     /* set default smoothers & set operators */
72462294041SBarry Smith     for (lidx = 1, level = pc_gamg->Nlevels - 2; lidx <= fine_level; lidx++, level--) {
725ffc955d6SMark F. Adams       KSP smoother;
726ffc955d6SMark F. Adams       PC  subpc;
727a2f3521dSMark F. Adams 
7289566063dSJacob Faibussowitsch       PetscCall(PCMGGetSmoother(pc, lidx, &smoother));
7299566063dSJacob Faibussowitsch       PetscCall(KSPGetPC(smoother, &subpc));
730ffc955d6SMark F. Adams 
7319566063dSJacob Faibussowitsch       PetscCall(KSPSetNormType(smoother, KSP_NORM_NONE));
732a2f3521dSMark F. Adams       /* set ops */
7339566063dSJacob Faibussowitsch       PetscCall(KSPSetOperators(smoother, Aarr[level], Aarr[level]));
7349566063dSJacob Faibussowitsch       PetscCall(PCMGSetInterpolation(pc, lidx, Parr[level + 1]));
735a2f3521dSMark F. Adams 
736a2f3521dSMark F. Adams       /* set defaults */
7379566063dSJacob Faibussowitsch       PetscCall(KSPSetType(smoother, KSPCHEBYSHEV));
738a2f3521dSMark F. Adams 
7390c3bc534SBarry Smith       /* set blocks for ASM smoother that uses the 'aggregates' */
7400c3bc534SBarry Smith       if (pc_gamg->use_aggs_in_asm) {
7412d3561bbSSatish Balay         PetscInt sz;
7427a28f3e5SMark Adams         IS      *iss;
743a2f3521dSMark F. Adams 
7442d3561bbSSatish Balay         sz  = nASMBlocksArr[level];
7457a28f3e5SMark Adams         iss = ASMLocalIDsArr[level];
7469566063dSJacob Faibussowitsch         PetscCall(PCSetType(subpc, PCASM));
7479566063dSJacob Faibussowitsch         PetscCall(PCASMSetOverlap(subpc, 0));
7489566063dSJacob Faibussowitsch         PetscCall(PCASMSetType(subpc, PC_ASM_BASIC));
7497f66b68fSMark Adams         if (!sz) {
750ffc955d6SMark F. Adams           IS is;
7519566063dSJacob Faibussowitsch           PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_COPY_VALUES, &is));
7529566063dSJacob Faibussowitsch           PetscCall(PCASMSetLocalSubdomains(subpc, 1, NULL, &is));
7539566063dSJacob Faibussowitsch           PetscCall(ISDestroy(&is));
754806fa848SBarry Smith         } else {
755a94c3b12SMark F. Adams           PetscInt kk;
7562eab5cd7Smarkadams4           PetscCall(PCASMSetLocalSubdomains(subpc, sz, iss, NULL));
75748a46eb9SPierre Jolivet           for (kk = 0; kk < sz; kk++) PetscCall(ISDestroy(&iss[kk]));
7589566063dSJacob Faibussowitsch           PetscCall(PetscFree(iss));
759ffc955d6SMark F. Adams         }
7600298fd71SBarry Smith         ASMLocalIDsArr[level] = NULL;
761ffc955d6SMark F. Adams         nASMBlocksArr[level]  = 0;
762806fa848SBarry Smith       } else {
7639566063dSJacob Faibussowitsch         PetscCall(PCSetType(subpc, PCJACOBI));
764ffc955d6SMark F. Adams       }
765d5280255SMark F. Adams     }
766d5280255SMark F. Adams     {
767d5280255SMark F. Adams       /* coarse grid */
7689371c9d4SSatish Balay       KSP      smoother, *k2;
7699371c9d4SSatish Balay       PC       subpc, pc2;
7709371c9d4SSatish Balay       PetscInt ii, first;
7719371c9d4SSatish Balay       Mat      Lmat = Aarr[(level = pc_gamg->Nlevels - 1)];
7729371c9d4SSatish Balay       lidx          = 0;
7730ed2132dSStefano Zampini 
7749566063dSJacob Faibussowitsch       PetscCall(PCMGGetSmoother(pc, lidx, &smoother));
7759566063dSJacob Faibussowitsch       PetscCall(KSPSetOperators(smoother, Lmat, Lmat));
776cf8ae1d3SMark Adams       if (!pc_gamg->use_parallel_coarse_grid_solver) {
7779566063dSJacob Faibussowitsch         PetscCall(KSPSetNormType(smoother, KSP_NORM_NONE));
7789566063dSJacob Faibussowitsch         PetscCall(KSPGetPC(smoother, &subpc));
7799566063dSJacob Faibussowitsch         PetscCall(PCSetType(subpc, PCBJACOBI));
7809566063dSJacob Faibussowitsch         PetscCall(PCSetUp(subpc));
7819566063dSJacob Faibussowitsch         PetscCall(PCBJacobiGetSubKSP(subpc, &ii, &first, &k2));
78263a3b9bcSJacob Faibussowitsch         PetscCheck(ii == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "ii %" PetscInt_FMT " is not one", ii);
7839566063dSJacob Faibussowitsch         PetscCall(KSPGetPC(k2[0], &pc2));
7849566063dSJacob Faibussowitsch         PetscCall(PCSetType(pc2, PCLU));
7859566063dSJacob Faibussowitsch         PetscCall(PCFactorSetShiftType(pc2, MAT_SHIFT_INBLOCKS));
7869566063dSJacob Faibussowitsch         PetscCall(KSPSetTolerances(k2[0], PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT, 1));
7879566063dSJacob Faibussowitsch         PetscCall(KSPSetType(k2[0], KSPPREONLY));
788d5280255SMark F. Adams       }
789cf8ae1d3SMark Adams     }
790d5280255SMark F. Adams 
791d5280255SMark F. Adams     /* should be called in PCSetFromOptions_GAMG(), but cannot be called prior to PCMGSetLevels() */
792d0609cedSBarry Smith     PetscObjectOptionsBegin((PetscObject)pc);
793dbbe0bcdSBarry Smith     PetscCall(PCSetFromOptions_MG(pc, PetscOptionsObject));
794d0609cedSBarry Smith     PetscOptionsEnd();
7959566063dSJacob Faibussowitsch     PetscCall(PCMGSetGalerkin(pc, PC_MG_GALERKIN_EXTERNAL));
796d5280255SMark F. Adams 
797f1580f4eSBarry Smith     /* set cheby eigen estimates from SA to use in the solver */
7987e6512fdSJed Brown     if (pc_gamg->use_sa_esteig) {
79918c3aa7eSMark       for (lidx = 1, level = pc_gamg->Nlevels - 2; level >= 0; lidx++, level--) {
80018c3aa7eSMark         KSP       smoother;
80118c3aa7eSMark         PetscBool ischeb;
8020ed2132dSStefano Zampini 
8039566063dSJacob Faibussowitsch         PetscCall(PCMGGetSmoother(pc, lidx, &smoother));
8049566063dSJacob Faibussowitsch         PetscCall(PetscObjectTypeCompare((PetscObject)smoother, KSPCHEBYSHEV, &ischeb));
80518c3aa7eSMark         if (ischeb) {
80618c3aa7eSMark           KSP_Chebyshev *cheb = (KSP_Chebyshev *)smoother->data;
8070ed2132dSStefano Zampini 
8082de708cbSJed Brown           // The command line will override these settings because KSPSetFromOptions is called in PCSetUp_MG
809efe053fcSJed Brown           if (mg->max_eigen_DinvA[level] > 0) {
8107e6512fdSJed Brown             // SA uses Jacobi for P; we use SA estimates if the smoother is also Jacobi or if the user explicitly requested it.
8117e6512fdSJed Brown             // TODO: This should test whether it's the same Jacobi variant (DIAG, ROWSUM, etc.)
81218c3aa7eSMark             PetscReal emax, emin;
8130ed2132dSStefano Zampini 
81418c3aa7eSMark             emin = mg->min_eigen_DinvA[level];
81518c3aa7eSMark             emax = mg->max_eigen_DinvA[level];
81663a3b9bcSJacob Faibussowitsch             PetscCall(PetscInfo(pc, "%s: PCSetUp_GAMG: call KSPChebyshevSetEigenvalues on level %" PetscInt_FMT " (N=%" PetscInt_FMT ") with emax = %g emin = %g\n", ((PetscObject)pc)->prefix, level, Aarr[level]->rmap->N, (double)emax, (double)emin));
8170a94a983SJed Brown             cheb->emin_provided = emin;
8180a94a983SJed Brown             cheb->emax_provided = emax;
81918c3aa7eSMark           }
82018c3aa7eSMark         }
82118c3aa7eSMark       }
82218c3aa7eSMark     }
8230ed2132dSStefano Zampini 
8249566063dSJacob Faibussowitsch     PetscCall(PCSetUp_MG(pc));
8250ed2132dSStefano Zampini 
826d5280255SMark F. Adams     /* clean up */
827d5280255SMark F. Adams     for (level = 1; level < pc_gamg->Nlevels; level++) {
8289566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&Parr[level]));
8299566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&Aarr[level]));
8305b89ad90SMark F. Adams     }
831806fa848SBarry Smith   } else {
8325f8cf99dSMark F. Adams     KSP smoother;
8330ed2132dSStefano Zampini 
83463a3b9bcSJacob Faibussowitsch     PetscCall(PetscInfo(pc, "%s: One level solver used (system is seen as DD). Using default solver.\n", ((PetscObject)pc)->prefix));
8359566063dSJacob Faibussowitsch     PetscCall(PCMGGetSmoother(pc, 0, &smoother));
8369566063dSJacob Faibussowitsch     PetscCall(KSPSetOperators(smoother, Aarr[0], Aarr[0]));
8379566063dSJacob Faibussowitsch     PetscCall(KSPSetType(smoother, KSPPREONLY));
8389566063dSJacob Faibussowitsch     PetscCall(PCSetUp_MG(pc));
8395f8cf99dSMark F. Adams   }
840849bee69SMark Adams   PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_SETUP], 0, 0, 0, 0));
8413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8425b89ad90SMark F. Adams }
8435b89ad90SMark F. Adams 
8445b89ad90SMark F. Adams /*
8455b89ad90SMark F. Adams  PCDestroy_GAMG - Destroys the private context for the GAMG preconditioner
8465b89ad90SMark F. Adams    that was created with PCCreate_GAMG().
8475b89ad90SMark F. Adams 
8485b89ad90SMark F. Adams    Input Parameter:
8495b89ad90SMark F. Adams .  pc - the preconditioner context
8505b89ad90SMark F. Adams 
8515b89ad90SMark F. Adams    Application Interface Routine: PCDestroy()
8525b89ad90SMark F. Adams */
853d71ae5a4SJacob Faibussowitsch PetscErrorCode PCDestroy_GAMG(PC pc)
854d71ae5a4SJacob Faibussowitsch {
8555b89ad90SMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
8565b89ad90SMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
8575b89ad90SMark F. Adams 
8585b89ad90SMark F. Adams   PetscFunctionBegin;
8599566063dSJacob Faibussowitsch   PetscCall(PCReset_GAMG(pc));
8601baa6e33SBarry Smith   if (pc_gamg->ops->destroy) PetscCall((*pc_gamg->ops->destroy)(pc));
8619566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc_gamg->ops));
8629566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc_gamg->gamg_type_name));
8639566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc_gamg));
8642e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetProcEqLim_C", NULL));
8652e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetCoarseEqLim_C", NULL));
8662e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetRepartition_C", NULL));
8672e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetEigenvalues_C", NULL));
8682e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetUseSAEstEig_C", NULL));
869*e1cf1444SMark Adams   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetRecomputeEstEig_C", NULL));
8702e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetReuseInterpolation_C", NULL));
8712e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGASMSetUseAggs_C", NULL));
872d529f056Smarkadams4   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetParallelCoarseGridSolve_C", NULL));
8732e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetCpuPinCoarseGrids_C", NULL));
8742e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetCoarseGridLayoutType_C", NULL));
8752e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetThreshold_C", NULL));
8762e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetRankReductionFactors_C", NULL));
8772e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetThresholdScale_C", NULL));
8782e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetType_C", NULL));
8792e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGGetType_C", NULL));
8802e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetNlevels_C", NULL));
8819566063dSJacob Faibussowitsch   PetscCall(PCDestroy_MG(pc));
8823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8835b89ad90SMark F. Adams }
8845b89ad90SMark F. Adams 
885676e1743SMark F. Adams /*@
886f1580f4eSBarry Smith   PCGAMGSetProcEqLim - Set number of equations to aim for per process on the coarse grids via processor reduction in `PCGAMG`
887676e1743SMark F. Adams 
888c3339decSBarry Smith   Logically Collective
889676e1743SMark F. Adams 
890676e1743SMark F. Adams   Input Parameters:
8911cc46a46SBarry Smith + pc - the preconditioner context
8921cc46a46SBarry Smith - n  - the number of equations
893676e1743SMark F. Adams 
894676e1743SMark F. Adams   Options Database Key:
895147403d9SBarry Smith . -pc_gamg_process_eq_limit <limit> - set the limit
896676e1743SMark F. Adams 
89720f4b53cSBarry Smith   Level: intermediate
89820f4b53cSBarry Smith 
899f1580f4eSBarry Smith   Note:
900f1580f4eSBarry Smith   `PCGAMG` will reduce the number of MPI processes used directly on the coarse grids so that there are around <limit> equations on each process
901cab9ed1eSBarry Smith   that has degrees of freedom
902cab9ed1eSBarry Smith 
903f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCGAMGSetCoarseEqLim()`, `PCGAMGSetRankReductionFactors()`, `PCGAMGSetRepartition()`
904676e1743SMark F. Adams @*/
905d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetProcEqLim(PC pc, PetscInt n)
906d71ae5a4SJacob Faibussowitsch {
907676e1743SMark F. Adams   PetscFunctionBegin;
908676e1743SMark F. Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
909cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetProcEqLim_C", (PC, PetscInt), (pc, n));
9103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
911676e1743SMark F. Adams }
912676e1743SMark F. Adams 
913d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetProcEqLim_GAMG(PC pc, PetscInt n)
914d71ae5a4SJacob Faibussowitsch {
915c20e4228SMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
916c20e4228SMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
917676e1743SMark F. Adams 
918676e1743SMark F. Adams   PetscFunctionBegin;
9199d5b6da9SMark F. Adams   if (n > 0) pc_gamg->min_eq_proc = n;
9203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
921676e1743SMark F. Adams }
922676e1743SMark F. Adams 
923389730f3SMark F. Adams /*@
924f1580f4eSBarry Smith   PCGAMGSetCoarseEqLim - Set maximum number of equations on the coarsest grid of `PCGAMG`
925389730f3SMark F. Adams 
926c3339decSBarry Smith   Collective
927389730f3SMark F. Adams 
928389730f3SMark F. Adams   Input Parameters:
9291cc46a46SBarry Smith + pc - the preconditioner context
9301cc46a46SBarry Smith - n  - maximum number of equations to aim for
931389730f3SMark F. Adams 
932389730f3SMark F. Adams   Options Database Key:
933147403d9SBarry Smith . -pc_gamg_coarse_eq_limit <limit> - set the limit
934389730f3SMark F. Adams 
93520f4b53cSBarry Smith   Level: intermediate
93620f4b53cSBarry Smith 
937f1580f4eSBarry Smith   Note:
938f1580f4eSBarry Smith   For example -pc_gamg_coarse_eq_limit 1000 will stop coarsening once the coarse grid
93974329af1SBarry Smith   has less than 1000 unknowns.
94074329af1SBarry Smith 
941f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCGAMGSetProcEqLim()`, `PCGAMGSetRankReductionFactors()`, `PCGAMGSetRepartition()`
942389730f3SMark F. Adams @*/
943d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetCoarseEqLim(PC pc, PetscInt n)
944d71ae5a4SJacob Faibussowitsch {
945389730f3SMark F. Adams   PetscFunctionBegin;
946389730f3SMark F. Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
947cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetCoarseEqLim_C", (PC, PetscInt), (pc, n));
9483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
949389730f3SMark F. Adams }
950389730f3SMark F. Adams 
951d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetCoarseEqLim_GAMG(PC pc, PetscInt n)
952d71ae5a4SJacob Faibussowitsch {
953389730f3SMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
954389730f3SMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
955389730f3SMark F. Adams 
956389730f3SMark F. Adams   PetscFunctionBegin;
9579d5b6da9SMark F. Adams   if (n > 0) pc_gamg->coarse_eq_limit = n;
9583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
959389730f3SMark F. Adams }
960389730f3SMark F. Adams 
961676e1743SMark F. Adams /*@
962f1580f4eSBarry Smith   PCGAMGSetRepartition - Repartition the degrees of freedom across the processors on the coarser grids when reducing the number of MPI ranks to use
963676e1743SMark F. Adams 
964c3339decSBarry Smith   Collective
965676e1743SMark F. Adams 
966676e1743SMark F. Adams   Input Parameters:
9671cc46a46SBarry Smith + pc - the preconditioner context
968f1580f4eSBarry Smith - n  - `PETSC_TRUE` or `PETSC_FALSE`
969676e1743SMark F. Adams 
970676e1743SMark F. Adams   Options Database Key:
971147403d9SBarry Smith . -pc_gamg_repartition <true,false> - turn on the repartitioning
972676e1743SMark F. Adams 
97320f4b53cSBarry Smith   Level: intermediate
97420f4b53cSBarry Smith 
975f1580f4eSBarry Smith   Note:
976f1580f4eSBarry Smith   This will generally improve the loading balancing of the work on each level
977cab9ed1eSBarry Smith 
978f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCGAMGSetProcEqLim()`, `PCGAMGSetRankReductionFactors()`
979676e1743SMark F. Adams @*/
980d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetRepartition(PC pc, PetscBool n)
981d71ae5a4SJacob Faibussowitsch {
982676e1743SMark F. Adams   PetscFunctionBegin;
983676e1743SMark F. Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
984cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetRepartition_C", (PC, PetscBool), (pc, n));
9853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
986676e1743SMark F. Adams }
987676e1743SMark F. Adams 
988d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetRepartition_GAMG(PC pc, PetscBool n)
989d71ae5a4SJacob Faibussowitsch {
990c20e4228SMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
991c20e4228SMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
992676e1743SMark F. Adams 
993676e1743SMark F. Adams   PetscFunctionBegin;
9949d5b6da9SMark F. Adams   pc_gamg->repart = n;
9953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
996676e1743SMark F. Adams }
997676e1743SMark F. Adams 
998dfd5c07aSMark F. Adams /*@
999f1580f4eSBarry Smith   PCGAMGSetUseSAEstEig - Use the eigen estimate from smoothed aggregation for the Chebyshev smoother during the solution process
100018c3aa7eSMark 
1001c3339decSBarry Smith   Collective
100218c3aa7eSMark 
100318c3aa7eSMark   Input Parameters:
100418c3aa7eSMark + pc - the preconditioner context
1005*e1cf1444SMark Adams - b  - flag
100618c3aa7eSMark 
100718c3aa7eSMark   Options Database Key:
1008147403d9SBarry Smith . -pc_gamg_use_sa_esteig <true,false> - use the eigen estimate
100918c3aa7eSMark 
101020f4b53cSBarry Smith   Level: advanced
101120f4b53cSBarry Smith 
101218c3aa7eSMark   Notes:
10137e6512fdSJed Brown   Smoothed aggregation constructs the smoothed prolongator $P = (I - \omega D^{-1} A) T$ where $T$ is the tentative prolongator and $D$ is the diagonal of $A$.
1014f1580f4eSBarry Smith   Eigenvalue estimates (based on a few `PCCG` or `PCGMRES` iterations) are computed to choose $\omega$ so that this is a stable smoothing operation.
1015f1580f4eSBarry Smith   If Chebyshev with Jacobi (diagonal) preconditioning is used for smoothing, then the eigenvalue estimates can be reused during the solution process
1016f1580f4eSBarry Smith   This option is only used when the smoother uses Jacobi, and should be turned off if a different `PCJacobiType` is used.
1017efe053fcSJed Brown   It became default in PETSc 3.17.
101818c3aa7eSMark 
1019*e1cf1444SMark Adams .seealso: `PCGAMG`, `KSPChebyshevSetEigenvalues()`, `KSPChebyshevEstEigSet()`, `PCGAMGSetRecomputeEstEig()`
102018c3aa7eSMark @*/
1021*e1cf1444SMark Adams PetscErrorCode PCGAMGSetUseSAEstEig(PC pc, PetscBool b)
1022d71ae5a4SJacob Faibussowitsch {
102318c3aa7eSMark   PetscFunctionBegin;
102418c3aa7eSMark   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1025*e1cf1444SMark Adams   PetscTryMethod(pc, "PCGAMGSetUseSAEstEig_C", (PC, PetscBool), (pc, b));
10263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
102718c3aa7eSMark }
102818c3aa7eSMark 
1029*e1cf1444SMark Adams static PetscErrorCode PCGAMGSetUseSAEstEig_GAMG(PC pc, PetscBool b)
1030d71ae5a4SJacob Faibussowitsch {
103118c3aa7eSMark   PC_MG   *mg      = (PC_MG *)pc->data;
103218c3aa7eSMark   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
103318c3aa7eSMark 
103418c3aa7eSMark   PetscFunctionBegin;
1035*e1cf1444SMark Adams   pc_gamg->use_sa_esteig = b;
1036*e1cf1444SMark Adams   PetscFunctionReturn(PETSC_SUCCESS);
1037*e1cf1444SMark Adams }
1038*e1cf1444SMark Adams 
1039*e1cf1444SMark Adams /*@
1040*e1cf1444SMark Adams   PCGAMGSetRecomputeEstEig - Set flag for Chebyshev smoothers to recompute the eigen estimates
1041*e1cf1444SMark Adams 
1042*e1cf1444SMark Adams   Collective
1043*e1cf1444SMark Adams 
1044*e1cf1444SMark Adams   Input Parameters:
1045*e1cf1444SMark Adams + pc - the preconditioner context
1046*e1cf1444SMark Adams - b  - flag
1047*e1cf1444SMark Adams 
1048*e1cf1444SMark Adams   Options Database Key:
1049*e1cf1444SMark Adams . -pc_gamg_recompute_esteig <true> - use the eigen estimate
1050*e1cf1444SMark Adams 
1051*e1cf1444SMark Adams   Level: advanced
1052*e1cf1444SMark Adams 
1053*e1cf1444SMark Adams .seealso: `PCGAMG`, `KSPChebyshevSetEigenvalues()`, `KSPChebyshevEstEigSet()`
1054*e1cf1444SMark Adams @*/
1055*e1cf1444SMark Adams PetscErrorCode PCGAMGSetRecomputeEstEig(PC pc, PetscBool b)
1056*e1cf1444SMark Adams {
1057*e1cf1444SMark Adams   PetscFunctionBegin;
1058*e1cf1444SMark Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1059*e1cf1444SMark Adams   PetscTryMethod(pc, "PCGAMGSetRecomputeEstEig_C", (PC, PetscBool), (pc, b));
1060*e1cf1444SMark Adams   PetscFunctionReturn(PETSC_SUCCESS);
1061*e1cf1444SMark Adams }
1062*e1cf1444SMark Adams 
1063*e1cf1444SMark Adams static PetscErrorCode PCGAMGSetRecomputeEstEig_GAMG(PC pc, PetscBool b)
1064*e1cf1444SMark Adams {
1065*e1cf1444SMark Adams   PC_MG   *mg      = (PC_MG *)pc->data;
1066*e1cf1444SMark Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1067*e1cf1444SMark Adams 
1068*e1cf1444SMark Adams   PetscFunctionBegin;
1069*e1cf1444SMark Adams   pc_gamg->recompute_esteig = b;
10703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
107118c3aa7eSMark }
107218c3aa7eSMark 
107318c3aa7eSMark /*@
1074f1580f4eSBarry Smith   PCGAMGSetEigenvalues - Set WHAT eigenvalues WHY?
107518c3aa7eSMark 
1076c3339decSBarry Smith   Collective
107718c3aa7eSMark 
107818c3aa7eSMark   Input Parameters:
107918c3aa7eSMark + pc   - the preconditioner context
1080feefa0e1SJacob Faibussowitsch . emax - max eigenvalue
1081feefa0e1SJacob Faibussowitsch - emin - min eigenvalue
108218c3aa7eSMark 
108318c3aa7eSMark   Options Database Key:
1084147403d9SBarry Smith . -pc_gamg_eigenvalues <emin,emax> - estimates of the eigenvalues
108518c3aa7eSMark 
108618c3aa7eSMark   Level: intermediate
108718c3aa7eSMark 
1088f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCGAMGSetUseSAEstEig()`
108918c3aa7eSMark @*/
1090d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetEigenvalues(PC pc, PetscReal emax, PetscReal emin)
1091d71ae5a4SJacob Faibussowitsch {
109218c3aa7eSMark   PetscFunctionBegin;
109318c3aa7eSMark   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1094cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetEigenvalues_C", (PC, PetscReal, PetscReal), (pc, emax, emin));
10953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
109618c3aa7eSMark }
109741ffd417SStefano Zampini 
1098d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetEigenvalues_GAMG(PC pc, PetscReal emax, PetscReal emin)
1099d71ae5a4SJacob Faibussowitsch {
110018c3aa7eSMark   PC_MG   *mg      = (PC_MG *)pc->data;
110118c3aa7eSMark   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
110218c3aa7eSMark 
110318c3aa7eSMark   PetscFunctionBegin;
11045f80ce2aSJacob Faibussowitsch   PetscCheck(emax > emin, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "Maximum eigenvalue must be larger than minimum: max %g min %g", (double)emax, (double)emin);
11055f80ce2aSJacob Faibussowitsch   PetscCheck(emax * emin > 0.0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "Both eigenvalues must be of the same sign: max %g min %g", (double)emax, (double)emin);
110618c3aa7eSMark   pc_gamg->emax = emax;
110718c3aa7eSMark   pc_gamg->emin = emin;
11083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
110918c3aa7eSMark }
111018c3aa7eSMark 
111118c3aa7eSMark /*@
1112f1580f4eSBarry Smith   PCGAMGSetReuseInterpolation - Reuse prolongation when rebuilding a `PCGAMG` algebraic multigrid preconditioner
1113dfd5c07aSMark F. Adams 
1114c3339decSBarry Smith   Collective
1115dfd5c07aSMark F. Adams 
1116dfd5c07aSMark F. Adams   Input Parameters:
11171cc46a46SBarry Smith + pc - the preconditioner context
1118f1580f4eSBarry Smith - n  - `PETSC_TRUE` or `PETSC_FALSE`
1119dfd5c07aSMark F. Adams 
1120dfd5c07aSMark F. Adams   Options Database Key:
1121147403d9SBarry Smith . -pc_gamg_reuse_interpolation <true,false> - reuse the previous interpolation
1122dfd5c07aSMark F. Adams 
1123dfd5c07aSMark F. Adams   Level: intermediate
1124dfd5c07aSMark F. Adams 
1125f1580f4eSBarry Smith   Note:
1126147403d9SBarry Smith   May negatively affect the convergence rate of the method on new matrices if the matrix entries change a great deal, but allows
1127cab9ed1eSBarry Smith   rebuilding the preconditioner quicker.
1128cab9ed1eSBarry Smith 
1129f1580f4eSBarry Smith .seealso: `PCGAMG`
1130dfd5c07aSMark F. Adams @*/
1131d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetReuseInterpolation(PC pc, PetscBool n)
1132d71ae5a4SJacob Faibussowitsch {
1133dfd5c07aSMark F. Adams   PetscFunctionBegin;
1134dfd5c07aSMark F. Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1135cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetReuseInterpolation_C", (PC, PetscBool), (pc, n));
11363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1137dfd5c07aSMark F. Adams }
1138dfd5c07aSMark F. Adams 
1139d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetReuseInterpolation_GAMG(PC pc, PetscBool n)
1140d71ae5a4SJacob Faibussowitsch {
1141dfd5c07aSMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
1142dfd5c07aSMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1143dfd5c07aSMark F. Adams 
1144dfd5c07aSMark F. Adams   PetscFunctionBegin;
1145dfd5c07aSMark F. Adams   pc_gamg->reuse_prol = n;
11463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1147dfd5c07aSMark F. Adams }
1148dfd5c07aSMark F. Adams 
1149ffc955d6SMark F. Adams /*@
1150f1580f4eSBarry Smith   PCGAMGASMSetUseAggs - Have the `PCGAMG` smoother on each level use the aggregates defined by the coarsening process as the subdomains for the additive Schwarz preconditioner
1151f1580f4eSBarry Smith   used as the smoother
1152ffc955d6SMark F. Adams 
1153c3339decSBarry Smith   Collective
1154ffc955d6SMark F. Adams 
1155ffc955d6SMark F. Adams   Input Parameters:
1156cab9ed1eSBarry Smith + pc  - the preconditioner context
1157f1580f4eSBarry Smith - flg - `PETSC_TRUE` to use aggregates, `PETSC_FALSE` to not
1158ffc955d6SMark F. Adams 
1159ffc955d6SMark F. Adams   Options Database Key:
1160147403d9SBarry Smith . -pc_gamg_asm_use_agg <true,false> - use aggregates to define the additive Schwarz subdomains
1161ffc955d6SMark F. Adams 
1162ffc955d6SMark F. Adams   Level: intermediate
1163ffc955d6SMark F. Adams 
1164f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCASM`, `PCSetType`
1165ffc955d6SMark F. Adams @*/
1166d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGASMSetUseAggs(PC pc, PetscBool flg)
1167d71ae5a4SJacob Faibussowitsch {
1168ffc955d6SMark F. Adams   PetscFunctionBegin;
1169ffc955d6SMark F. Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1170cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGASMSetUseAggs_C", (PC, PetscBool), (pc, flg));
11713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1172ffc955d6SMark F. Adams }
1173ffc955d6SMark F. Adams 
1174d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGASMSetUseAggs_GAMG(PC pc, PetscBool flg)
1175d71ae5a4SJacob Faibussowitsch {
1176ffc955d6SMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
1177ffc955d6SMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1178ffc955d6SMark F. Adams 
1179ffc955d6SMark F. Adams   PetscFunctionBegin;
1180cab9ed1eSBarry Smith   pc_gamg->use_aggs_in_asm = flg;
11813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1182ffc955d6SMark F. Adams }
1183ffc955d6SMark F. Adams 
1184171cca9aSMark Adams /*@
1185d529f056Smarkadams4   PCGAMGSetParallelCoarseGridSolve - allow a parallel coarse grid solver
1186171cca9aSMark Adams 
1187c3339decSBarry Smith   Collective
1188171cca9aSMark Adams 
1189171cca9aSMark Adams   Input Parameters:
1190171cca9aSMark Adams + pc  - the preconditioner context
1191f1580f4eSBarry Smith - flg - `PETSC_TRUE` to not force coarse grid onto one processor
1192171cca9aSMark Adams 
1193171cca9aSMark Adams   Options Database Key:
1194d529f056Smarkadams4 . -pc_gamg_parallel_coarse_grid_solver - use a parallel coarse grid direct solver
1195171cca9aSMark Adams 
1196171cca9aSMark Adams   Level: intermediate
1197171cca9aSMark Adams 
1198f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCGAMGSetCoarseGridLayoutType()`, `PCGAMGSetCpuPinCoarseGrids()`
1199171cca9aSMark Adams @*/
1200d529f056Smarkadams4 PetscErrorCode PCGAMGSetParallelCoarseGridSolve(PC pc, PetscBool flg)
1201d71ae5a4SJacob Faibussowitsch {
1202171cca9aSMark Adams   PetscFunctionBegin;
1203171cca9aSMark Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1204d529f056Smarkadams4   PetscTryMethod(pc, "PCGAMGSetParallelCoarseGridSolve_C", (PC, PetscBool), (pc, flg));
12053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1206171cca9aSMark Adams }
1207171cca9aSMark Adams 
1208d529f056Smarkadams4 static PetscErrorCode PCGAMGSetParallelCoarseGridSolve_GAMG(PC pc, PetscBool flg)
1209d71ae5a4SJacob Faibussowitsch {
1210171cca9aSMark Adams   PC_MG   *mg      = (PC_MG *)pc->data;
1211171cca9aSMark Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1212171cca9aSMark Adams 
1213171cca9aSMark Adams   PetscFunctionBegin;
1214171cca9aSMark Adams   pc_gamg->use_parallel_coarse_grid_solver = flg;
12153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1216ffc955d6SMark F. Adams }
1217ffc955d6SMark F. Adams 
12184ef23d27SMark F. Adams /*@
1219f1580f4eSBarry Smith   PCGAMGSetCpuPinCoarseGrids - pin the coarse grids created in `PCGAMG` to run only on the CPU since the problems may be too small to run efficiently on the GPUs
1220ce7c7f2fSMark Adams 
1221c3339decSBarry Smith   Collective
1222ce7c7f2fSMark Adams 
1223ce7c7f2fSMark Adams   Input Parameters:
1224ce7c7f2fSMark Adams + pc  - the preconditioner context
1225f1580f4eSBarry Smith - flg - `PETSC_TRUE` to pin coarse grids to the CPU
1226ce7c7f2fSMark Adams 
1227ce7c7f2fSMark Adams   Options Database Key:
1228147403d9SBarry Smith . -pc_gamg_cpu_pin_coarse_grids - pin the coarse grids to the CPU
1229ce7c7f2fSMark Adams 
1230ce7c7f2fSMark Adams   Level: intermediate
1231ce7c7f2fSMark Adams 
1232d529f056Smarkadams4 .seealso: `PCGAMG`, `PCGAMGSetCoarseGridLayoutType()`, `PCGAMGSetParallelCoarseGridSolve()`
1233ce7c7f2fSMark Adams @*/
1234d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetCpuPinCoarseGrids(PC pc, PetscBool flg)
1235d71ae5a4SJacob Faibussowitsch {
1236ce7c7f2fSMark Adams   PetscFunctionBegin;
1237ce7c7f2fSMark Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1238cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetCpuPinCoarseGrids_C", (PC, PetscBool), (pc, flg));
12393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1240ce7c7f2fSMark Adams }
1241ce7c7f2fSMark Adams 
1242d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetCpuPinCoarseGrids_GAMG(PC pc, PetscBool flg)
1243d71ae5a4SJacob Faibussowitsch {
1244ce7c7f2fSMark Adams   PC_MG   *mg      = (PC_MG *)pc->data;
1245ce7c7f2fSMark Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1246ce7c7f2fSMark Adams 
1247ce7c7f2fSMark Adams   PetscFunctionBegin;
1248ce7c7f2fSMark Adams   pc_gamg->cpu_pin_coarse_grids = flg;
12493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1250ce7c7f2fSMark Adams }
1251ce7c7f2fSMark Adams 
1252ce7c7f2fSMark Adams /*@
1253147403d9SBarry Smith   PCGAMGSetCoarseGridLayoutType - place coarse grids on processors with natural order (compact type)
1254ce7c7f2fSMark Adams 
1255c3339decSBarry Smith   Collective
1256ce7c7f2fSMark Adams 
1257ce7c7f2fSMark Adams   Input Parameters:
1258ce7c7f2fSMark Adams + pc  - the preconditioner context
1259f1580f4eSBarry Smith - flg - `PCGAMGLayoutType` type, either `PCGAMG_LAYOUT_COMPACT` or `PCGAMG_LAYOUT_SPREAD`
1260ce7c7f2fSMark Adams 
1261ce7c7f2fSMark Adams   Options Database Key:
1262147403d9SBarry Smith . -pc_gamg_coarse_grid_layout_type - place the coarse grids with natural ordering
1263ce7c7f2fSMark Adams 
1264ce7c7f2fSMark Adams   Level: intermediate
1265ce7c7f2fSMark Adams 
1266d529f056Smarkadams4 .seealso: `PCGAMG`, `PCGAMGSetParallelCoarseGridSolve()`, `PCGAMGSetCpuPinCoarseGrids()`, `PCGAMGLayoutType`, `PCGAMG_LAYOUT_COMPACT`, `PCGAMG_LAYOUT_SPREAD`
1267ce7c7f2fSMark Adams @*/
1268d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetCoarseGridLayoutType(PC pc, PCGAMGLayoutType flg)
1269d71ae5a4SJacob Faibussowitsch {
1270ce7c7f2fSMark Adams   PetscFunctionBegin;
1271ce7c7f2fSMark Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1272cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetCoarseGridLayoutType_C", (PC, PCGAMGLayoutType), (pc, flg));
12733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1274ce7c7f2fSMark Adams }
1275ce7c7f2fSMark Adams 
1276d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetCoarseGridLayoutType_GAMG(PC pc, PCGAMGLayoutType flg)
1277d71ae5a4SJacob Faibussowitsch {
1278ce7c7f2fSMark Adams   PC_MG   *mg      = (PC_MG *)pc->data;
1279ce7c7f2fSMark Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1280ce7c7f2fSMark Adams 
1281ce7c7f2fSMark Adams   PetscFunctionBegin;
1282ce7c7f2fSMark Adams   pc_gamg->layout_type = flg;
12833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1284ce7c7f2fSMark Adams }
1285ce7c7f2fSMark Adams 
1286ce7c7f2fSMark Adams /*@
1287f1580f4eSBarry Smith   PCGAMGSetNlevels -  Sets the maximum number of levels `PCGAMG` will use
12884ef23d27SMark F. Adams 
128920f4b53cSBarry Smith   Not Collective
12904ef23d27SMark F. Adams 
12914ef23d27SMark F. Adams   Input Parameters:
12921cc46a46SBarry Smith + pc - the preconditioner
12931cc46a46SBarry Smith - n  - the maximum number of levels to use
12944ef23d27SMark F. Adams 
12954ef23d27SMark F. Adams   Options Database Key:
1296147403d9SBarry Smith . -pc_mg_levels <n> - set the maximum number of levels to allow
12974ef23d27SMark F. Adams 
12984ef23d27SMark F. Adams   Level: intermediate
12994ef23d27SMark F. Adams 
1300feefa0e1SJacob Faibussowitsch   Developer Notes:
1301f1580f4eSBarry Smith   Should be called `PCGAMGSetMaximumNumberlevels()` and possible be shared with `PCMG`
1302f1580f4eSBarry Smith 
1303f1580f4eSBarry Smith .seealso: `PCGAMG`
13044ef23d27SMark F. Adams @*/
1305d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetNlevels(PC pc, PetscInt n)
1306d71ae5a4SJacob Faibussowitsch {
13074ef23d27SMark F. Adams   PetscFunctionBegin;
13084ef23d27SMark F. Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1309cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetNlevels_C", (PC, PetscInt), (pc, n));
13103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13114ef23d27SMark F. Adams }
13124ef23d27SMark F. Adams 
1313d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetNlevels_GAMG(PC pc, PetscInt n)
1314d71ae5a4SJacob Faibussowitsch {
13154ef23d27SMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
13164ef23d27SMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
13174ef23d27SMark F. Adams 
13184ef23d27SMark F. Adams   PetscFunctionBegin;
13199d5b6da9SMark F. Adams   pc_gamg->Nlevels = n;
13203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13214ef23d27SMark F. Adams }
13224ef23d27SMark F. Adams 
13233542efc5SMark F. Adams /*@
13243542efc5SMark F. Adams   PCGAMGSetThreshold - Relative threshold to use for dropping edges in aggregation graph
13253542efc5SMark F. Adams 
132620f4b53cSBarry Smith   Not Collective
13273542efc5SMark F. Adams 
13283542efc5SMark F. Adams   Input Parameters:
13291cc46a46SBarry Smith + pc - the preconditioner context
1330c9567895SMark . v  - array of threshold values for finest n levels; 0.0 means keep all nonzero entries in the graph; negative means keep even zero entries in the graph
1331055c8bd0SJed Brown - n  - number of threshold values provided in array
13323542efc5SMark F. Adams 
13333542efc5SMark F. Adams   Options Database Key:
1334147403d9SBarry Smith . -pc_gamg_threshold <threshold> - the threshold to drop edges
13353542efc5SMark F. Adams 
133620f4b53cSBarry Smith   Level: intermediate
133720f4b53cSBarry Smith 
133895452b02SPatrick Sanan   Notes:
1339af3c827dSMark Adams   Increasing the threshold decreases the rate of coarsening. Conversely reducing the threshold increases the rate of coarsening (aggressive coarsening) and thereby reduces the complexity of the coarse grids, and generally results in slower solver converge rates. Reducing coarse grid complexity reduced the complexity of Galerkin coarse grid construction considerably.
1340f1580f4eSBarry Smith   Before coarsening or aggregating the graph, `PCGAMG` removes small values from the graph with this threshold, and thus reducing the coupling in the graph and a different (perhaps better) coarser set of points.
1341cab9ed1eSBarry Smith 
134220f4b53cSBarry Smith   If `n` is less than the total number of coarsenings (see `PCGAMGSetNlevels()`), then threshold scaling (see `PCGAMGSetThresholdScale()`) is used for each successive coarsening.
1343f1580f4eSBarry Smith   In this case, `PCGAMGSetThresholdScale()` must be called before `PCGAMGSetThreshold()`.
134420f4b53cSBarry Smith   If `n` is greater than the total number of levels, the excess entries in threshold will not be used.
13453542efc5SMark F. Adams 
1346d529f056Smarkadams4 .seealso: `PCGAMG`, `PCGAMGSetAggressiveLevels()`, `PCGAMGMISkSetAggressive()`, `PCGAMGSetMinDegreeOrderingMISk()`, `PCGAMGSetThresholdScale()`
13473542efc5SMark F. Adams @*/
1348d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetThreshold(PC pc, PetscReal v[], PetscInt n)
1349d71ae5a4SJacob Faibussowitsch {
13503542efc5SMark F. Adams   PetscFunctionBegin;
13513542efc5SMark F. Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
13524f572ea9SToby Isaac   if (n) PetscAssertPointer(v, 2);
1353cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetThreshold_C", (PC, PetscReal[], PetscInt), (pc, v, n));
13543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13553542efc5SMark F. Adams }
13563542efc5SMark F. Adams 
1357d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetThreshold_GAMG(PC pc, PetscReal v[], PetscInt n)
1358d71ae5a4SJacob Faibussowitsch {
1359c20e4228SMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
1360c20e4228SMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1361c1eae691SMark Adams   PetscInt i;
1362c1eae691SMark Adams   PetscFunctionBegin;
1363055c8bd0SJed Brown   for (i = 0; i < PetscMin(n, PETSC_MG_MAXLEVELS); i++) pc_gamg->threshold[i] = v[i];
1364055c8bd0SJed Brown   for (; i < PETSC_MG_MAXLEVELS; i++) pc_gamg->threshold[i] = pc_gamg->threshold[i - 1] * pc_gamg->threshold_scale;
13653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1366c1eae691SMark Adams }
1367c1eae691SMark Adams 
1368c1eae691SMark Adams /*@
1369f1580f4eSBarry Smith   PCGAMGSetRankReductionFactors - Set a manual schedule for MPI rank reduction on coarse grids
1370c9567895SMark 
1371c3339decSBarry Smith   Collective
1372c9567895SMark 
1373c9567895SMark   Input Parameters:
1374c9567895SMark + pc - the preconditioner context
1375f1580f4eSBarry Smith . v  - array of reduction factors. 0 for first value forces a reduction to one process/device on first level in CUDA
1376c9567895SMark - n  - number of values provided in array
1377c9567895SMark 
1378c9567895SMark   Options Database Key:
1379147403d9SBarry Smith . -pc_gamg_rank_reduction_factors <factors> - provide the schedule
1380c9567895SMark 
1381c9567895SMark   Level: intermediate
1382c9567895SMark 
1383f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCGAMGSetProcEqLim()`, `PCGAMGSetCoarseEqLim()`
1384c9567895SMark @*/
1385d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetRankReductionFactors(PC pc, PetscInt v[], PetscInt n)
1386d71ae5a4SJacob Faibussowitsch {
1387c9567895SMark   PetscFunctionBegin;
1388c9567895SMark   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
13894f572ea9SToby Isaac   if (n) PetscAssertPointer(v, 2);
1390cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetRankReductionFactors_C", (PC, PetscInt[], PetscInt), (pc, v, n));
13913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1392c9567895SMark }
1393c9567895SMark 
1394d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetRankReductionFactors_GAMG(PC pc, PetscInt v[], PetscInt n)
1395d71ae5a4SJacob Faibussowitsch {
1396c9567895SMark   PC_MG   *mg      = (PC_MG *)pc->data;
1397c9567895SMark   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1398c9567895SMark   PetscInt i;
1399c9567895SMark   PetscFunctionBegin;
1400c9567895SMark   for (i = 0; i < PetscMin(n, PETSC_MG_MAXLEVELS); i++) pc_gamg->level_reduction_factors[i] = v[i];
1401c9567895SMark   for (; i < PETSC_MG_MAXLEVELS; i++) pc_gamg->level_reduction_factors[i] = -1; /* 0 stop putting one process/device on first level */
14023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1403c9567895SMark }
1404c9567895SMark 
1405c9567895SMark /*@
1406c1eae691SMark Adams   PCGAMGSetThresholdScale - Relative threshold reduction at each level
1407c1eae691SMark Adams 
140820f4b53cSBarry Smith   Not Collective
1409c1eae691SMark Adams 
1410c1eae691SMark Adams   Input Parameters:
1411c1eae691SMark Adams + pc - the preconditioner context
1412feefa0e1SJacob Faibussowitsch - v  - the threshold value reduction, usually < 1.0
1413c1eae691SMark Adams 
1414c1eae691SMark Adams   Options Database Key:
1415147403d9SBarry Smith . -pc_gamg_threshold_scale <v> - set the relative threshold reduction on each level
1416c1eae691SMark Adams 
141720f4b53cSBarry Smith   Level: advanced
141820f4b53cSBarry Smith 
1419f1580f4eSBarry Smith   Note:
1420f1580f4eSBarry Smith   The initial threshold (for an arbitrary number of levels starting from the finest) can be set with `PCGAMGSetThreshold()`.
1421f1580f4eSBarry Smith   This scaling is used for each subsequent coarsening, but must be called before `PCGAMGSetThreshold()`.
1422055c8bd0SJed Brown 
1423f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCGAMGSetThreshold()`
1424c1eae691SMark Adams @*/
1425d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetThresholdScale(PC pc, PetscReal v)
1426d71ae5a4SJacob Faibussowitsch {
14273542efc5SMark F. Adams   PetscFunctionBegin;
1428c1eae691SMark Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1429cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetThresholdScale_C", (PC, PetscReal), (pc, v));
14303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1431c1eae691SMark Adams }
1432c1eae691SMark Adams 
1433d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetThresholdScale_GAMG(PC pc, PetscReal v)
1434d71ae5a4SJacob Faibussowitsch {
1435c1eae691SMark Adams   PC_MG   *mg      = (PC_MG *)pc->data;
1436c1eae691SMark Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1437c1eae691SMark Adams   PetscFunctionBegin;
1438c1eae691SMark Adams   pc_gamg->threshold_scale = v;
14393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
14403542efc5SMark F. Adams }
14413542efc5SMark F. Adams 
1442e20c40e8SBarry Smith /*@C
1443f1580f4eSBarry Smith   PCGAMGSetType - Set the type of algorithm `PCGAMG` should use
1444676e1743SMark F. Adams 
1445c3339decSBarry Smith   Collective
1446676e1743SMark F. Adams 
1447676e1743SMark F. Adams   Input Parameters:
1448c60c7ad4SBarry Smith + pc   - the preconditioner context
1449f1580f4eSBarry Smith - type - `PCGAMGAGG`, `PCGAMGGEO`, or `PCGAMGCLASSICAL`
1450676e1743SMark F. Adams 
1451676e1743SMark F. Adams   Options Database Key:
14525e4ac8c8Smarkadams4 . -pc_gamg_type <agg,geo,classical> - type of algebraic multigrid to apply - only agg supported
1453676e1743SMark F. Adams 
1454676e1743SMark F. Adams   Level: intermediate
1455676e1743SMark F. Adams 
1456db781477SPatrick Sanan .seealso: `PCGAMGGetType()`, `PCGAMG`, `PCGAMGType`
1457676e1743SMark F. Adams @*/
1458d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetType(PC pc, PCGAMGType type)
1459d71ae5a4SJacob Faibussowitsch {
1460676e1743SMark F. Adams   PetscFunctionBegin;
1461676e1743SMark F. Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1462cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetType_C", (PC, PCGAMGType), (pc, type));
14633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1464676e1743SMark F. Adams }
1465676e1743SMark F. Adams 
1466e20c40e8SBarry Smith /*@C
1467f1580f4eSBarry Smith   PCGAMGGetType - Get the type of algorithm `PCGAMG` will use
1468c60c7ad4SBarry Smith 
1469c3339decSBarry Smith   Collective
1470c60c7ad4SBarry Smith 
1471c60c7ad4SBarry Smith   Input Parameter:
1472c60c7ad4SBarry Smith . pc - the preconditioner context
1473c60c7ad4SBarry Smith 
1474c60c7ad4SBarry Smith   Output Parameter:
1475c60c7ad4SBarry Smith . type - the type of algorithm used
1476c60c7ad4SBarry Smith 
1477c60c7ad4SBarry Smith   Level: intermediate
1478c60c7ad4SBarry Smith 
1479f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCGAMGSetType()`, `PCGAMGType`
1480c60c7ad4SBarry Smith @*/
1481d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGGetType(PC pc, PCGAMGType *type)
1482d71ae5a4SJacob Faibussowitsch {
1483c60c7ad4SBarry Smith   PetscFunctionBegin;
1484c60c7ad4SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1485cac4c232SBarry Smith   PetscUseMethod(pc, "PCGAMGGetType_C", (PC, PCGAMGType *), (pc, type));
14863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1487c60c7ad4SBarry Smith }
1488c60c7ad4SBarry Smith 
1489d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGGetType_GAMG(PC pc, PCGAMGType *type)
1490d71ae5a4SJacob Faibussowitsch {
1491c60c7ad4SBarry Smith   PC_MG   *mg      = (PC_MG *)pc->data;
1492c60c7ad4SBarry Smith   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1493c60c7ad4SBarry Smith 
1494c60c7ad4SBarry Smith   PetscFunctionBegin;
1495c60c7ad4SBarry Smith   *type = pc_gamg->type;
14963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1497c60c7ad4SBarry Smith }
1498c60c7ad4SBarry Smith 
1499d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetType_GAMG(PC pc, PCGAMGType type)
1500d71ae5a4SJacob Faibussowitsch {
15011ab5ffc9SJed Brown   PC_MG   *mg      = (PC_MG *)pc->data;
15021ab5ffc9SJed Brown   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
15035f80ce2aSJacob Faibussowitsch   PetscErrorCode (*r)(PC);
1504676e1743SMark F. Adams 
1505676e1743SMark F. Adams   PetscFunctionBegin;
1506c60c7ad4SBarry Smith   pc_gamg->type = type;
15079566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(GAMGList, type, &r));
15086adde796SStefano Zampini   PetscCheck(r, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown GAMG type %s given", type);
15091ab5ffc9SJed Brown   if (pc_gamg->ops->destroy) {
15109566063dSJacob Faibussowitsch     PetscCall((*pc_gamg->ops->destroy)(pc));
15119566063dSJacob Faibussowitsch     PetscCall(PetscMemzero(pc_gamg->ops, sizeof(struct _PCGAMGOps)));
1512e616c208SToby Isaac     pc_gamg->ops->createlevel = PCGAMGCreateLevel_GAMG;
1513da81f932SPierre Jolivet     /* cleaning up common data in pc_gamg - this should disappear someday */
15143ae0bb68SMark Adams     pc_gamg->data_cell_cols      = 0;
15153ae0bb68SMark Adams     pc_gamg->data_cell_rows      = 0;
15163ae0bb68SMark Adams     pc_gamg->orig_data_cell_cols = 0;
15173ae0bb68SMark Adams     pc_gamg->orig_data_cell_rows = 0;
15189566063dSJacob Faibussowitsch     PetscCall(PetscFree(pc_gamg->data));
15193ae0bb68SMark Adams     pc_gamg->data_sz = 0;
15201ab5ffc9SJed Brown   }
15219566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc_gamg->gamg_type_name));
15229566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(type, &pc_gamg->gamg_type_name));
15239566063dSJacob Faibussowitsch   PetscCall((*r)(pc));
15243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1525676e1743SMark F. Adams }
1526676e1743SMark F. Adams 
1527d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCView_GAMG(PC pc, PetscViewer viewer)
1528d71ae5a4SJacob Faibussowitsch {
15295adeb434SBarry Smith   PC_MG    *mg      = (PC_MG *)pc->data;
15305adeb434SBarry Smith   PC_GAMG  *pc_gamg = (PC_GAMG *)mg->innerctx;
1531e7d4b4cbSMark Adams   PetscReal gc = 0, oc = 0;
153290db8557SMark Adams 
15335adeb434SBarry Smith   PetscFunctionBegin;
15349566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "    GAMG specific options\n"));
15359566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "      Threshold for dropping small values in graph on each level ="));
15369566063dSJacob Faibussowitsch   for (PetscInt i = 0; i < mg->nlevels; i++) PetscCall(PetscViewerASCIIPrintf(viewer, " %g", (double)pc_gamg->threshold[i]));
15379566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
15389566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "      Threshold scaling factor for each level not specified = %g\n", (double)pc_gamg->threshold_scale));
153948a46eb9SPierre Jolivet   if (pc_gamg->use_aggs_in_asm) PetscCall(PetscViewerASCIIPrintf(viewer, "      Using aggregates from coarsening process to define subdomains for PCASM\n"));
154048a46eb9SPierre Jolivet   if (pc_gamg->use_parallel_coarse_grid_solver) PetscCall(PetscViewerASCIIPrintf(viewer, "      Using parallel coarse grid solver (all coarse grid equations not put on one process)\n"));
15411baa6e33SBarry Smith   if (pc_gamg->ops->view) PetscCall((*pc_gamg->ops->view)(pc, viewer));
15429566063dSJacob Faibussowitsch   PetscCall(PCMGGetGridComplexity(pc, &gc, &oc));
154363a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "      Complexity:    grid = %g    operator = %g\n", (double)gc, (double)oc));
15443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
15455adeb434SBarry Smith }
15465adeb434SBarry Smith 
154766976f2fSJacob Faibussowitsch static PetscErrorCode PCSetFromOptions_GAMG(PC pc, PetscOptionItems *PetscOptionsObject)
1548d71ae5a4SJacob Faibussowitsch {
1549676e1743SMark F. Adams   PC_MG             *mg      = (PC_MG *)pc->data;
1550676e1743SMark F. Adams   PC_GAMG           *pc_gamg = (PC_GAMG *)mg->innerctx;
15517e6512fdSJed Brown   PetscBool          flag;
15523b4367a7SBarry Smith   MPI_Comm           comm;
155318c3aa7eSMark   char               prefix[256], tname[32];
1554c1eae691SMark Adams   PetscInt           i, n;
155514a9496bSBarry Smith   const char        *pcpre;
15560a545947SLisandro Dalcin   static const char *LayoutTypes[] = {"compact", "spread", "PCGAMGLayoutType", "PC_GAMG_LAYOUT", NULL};
15575b89ad90SMark F. Adams   PetscFunctionBegin;
15589566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
1559d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "GAMG options");
15605e4ac8c8Smarkadams4   PetscCall(PetscOptionsFList("-pc_gamg_type", "Type of AMG method (only 'agg' supported and useful)", "PCGAMGSetType", GAMGList, pc_gamg->gamg_type_name, tname, sizeof(tname), &flag));
15611baa6e33SBarry Smith   if (flag) PetscCall(PCGAMGSetType(pc, tname));
15629566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-pc_gamg_repartition", "Repartion coarse grids", "PCGAMGSetRepartition", pc_gamg->repart, &pc_gamg->repart, NULL));
1563f1580f4eSBarry Smith   PetscCall(PetscOptionsBool("-pc_gamg_use_sa_esteig", "Use eigen estimate from smoothed aggregation for smoother", "PCGAMGSetUseSAEstEig", pc_gamg->use_sa_esteig, &pc_gamg->use_sa_esteig, NULL));
1564*e1cf1444SMark Adams   PetscCall(PetscOptionsBool("-pc_gamg_recompute_esteig", "Set flag to recompute eigen estimates for Chebyshev when matrix changes", "PCGAMGSetRecomputeEstEig", pc_gamg->recompute_esteig, &pc_gamg->recompute_esteig, NULL));
15659566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-pc_gamg_reuse_interpolation", "Reuse prolongation operator", "PCGAMGReuseInterpolation", pc_gamg->reuse_prol, &pc_gamg->reuse_prol, NULL));
15669566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-pc_gamg_asm_use_agg", "Use aggregation aggregates for ASM smoother", "PCGAMGASMSetUseAggs", pc_gamg->use_aggs_in_asm, &pc_gamg->use_aggs_in_asm, NULL));
1567d529f056Smarkadams4   PetscCall(PetscOptionsBool("-pc_gamg_parallel_coarse_grid_solver", "Use parallel coarse grid solver (otherwise put last grid on one process)", "PCGAMGSetParallelCoarseGridSolve", pc_gamg->use_parallel_coarse_grid_solver, &pc_gamg->use_parallel_coarse_grid_solver, NULL));
15689566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-pc_gamg_cpu_pin_coarse_grids", "Pin coarse grids to the CPU", "PCGAMGSetCpuPinCoarseGrids", pc_gamg->cpu_pin_coarse_grids, &pc_gamg->cpu_pin_coarse_grids, NULL));
15699371c9d4SSatish Balay   PetscCall(PetscOptionsEnum("-pc_gamg_coarse_grid_layout_type", "compact: place reduced grids on processes in natural order; spread: distribute to whole machine for more memory bandwidth", "PCGAMGSetCoarseGridLayoutType", LayoutTypes,
15709371c9d4SSatish Balay                              (PetscEnum)pc_gamg->layout_type, (PetscEnum *)&pc_gamg->layout_type, NULL));
15719566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-pc_gamg_process_eq_limit", "Limit (goal) on number of equations per process on coarse grids", "PCGAMGSetProcEqLim", pc_gamg->min_eq_proc, &pc_gamg->min_eq_proc, NULL));
15729566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-pc_gamg_coarse_eq_limit", "Limit on number of equations for the coarse grid", "PCGAMGSetCoarseEqLim", pc_gamg->coarse_eq_limit, &pc_gamg->coarse_eq_limit, NULL));
15739566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-pc_gamg_threshold_scale", "Scaling of threshold for each level not specified", "PCGAMGSetThresholdScale", pc_gamg->threshold_scale, &pc_gamg->threshold_scale, NULL));
157418c3aa7eSMark   n = PETSC_MG_MAXLEVELS;
15759566063dSJacob Faibussowitsch   PetscCall(PetscOptionsRealArray("-pc_gamg_threshold", "Relative threshold to use for dropping edges in aggregation graph", "PCGAMGSetThreshold", pc_gamg->threshold, &n, &flag));
157618c3aa7eSMark   if (!flag || n < PETSC_MG_MAXLEVELS) {
1577efd3c5ceSMark Adams     if (!flag) n = 1;
1578c1eae691SMark Adams     i = n;
1579d71ae5a4SJacob Faibussowitsch     do {
1580d71ae5a4SJacob Faibussowitsch       pc_gamg->threshold[i] = pc_gamg->threshold[i - 1] * pc_gamg->threshold_scale;
1581d71ae5a4SJacob Faibussowitsch     } while (++i < PETSC_MG_MAXLEVELS);
1582c1eae691SMark Adams   }
1583c9567895SMark   n = PETSC_MG_MAXLEVELS;
15849566063dSJacob Faibussowitsch   PetscCall(PetscOptionsIntArray("-pc_gamg_rank_reduction_factors", "Manual schedule of coarse grid reduction factors that overrides internal heuristics (0 for first reduction puts one process/device)", "PCGAMGSetRankReductionFactors", pc_gamg->level_reduction_factors, &n, &flag));
1585c9567895SMark   if (!flag) i = 0;
1586c9567895SMark   else i = n;
1587d71ae5a4SJacob Faibussowitsch   do {
1588d71ae5a4SJacob Faibussowitsch     pc_gamg->level_reduction_factors[i] = -1;
1589d71ae5a4SJacob Faibussowitsch   } while (++i < PETSC_MG_MAXLEVELS);
15909566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-pc_mg_levels", "Set number of MG levels", "PCGAMGSetNlevels", pc_gamg->Nlevels, &pc_gamg->Nlevels, NULL));
159118c3aa7eSMark   {
159218c3aa7eSMark     PetscReal eminmax[2] = {0., 0.};
159318c3aa7eSMark     n                    = 2;
15949566063dSJacob Faibussowitsch     PetscCall(PetscOptionsRealArray("-pc_gamg_eigenvalues", "extreme eigenvalues for smoothed aggregation", "PCGAMGSetEigenvalues", eminmax, &n, &flag));
159518c3aa7eSMark     if (flag) {
159608401ef6SPierre Jolivet       PetscCheck(n == 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "-pc_gamg_eigenvalues: must specify 2 parameters, min and max eigenvalues");
15979566063dSJacob Faibussowitsch       PetscCall(PCGAMGSetEigenvalues(pc, eminmax[1], eminmax[0]));
159818c3aa7eSMark     }
159918c3aa7eSMark   }
1600b7cbab4eSMark Adams   /* set options for subtype */
1601dbbe0bcdSBarry Smith   PetscCall((*pc_gamg->ops->setfromoptions)(pc, PetscOptionsObject));
160218c3aa7eSMark 
16039566063dSJacob Faibussowitsch   PetscCall(PCGetOptionsPrefix(pc, &pcpre));
16049566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_gamg_", pcpre ? pcpre : ""));
1605d0609cedSBarry Smith   PetscOptionsHeadEnd();
16063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
16075b89ad90SMark F. Adams }
16085b89ad90SMark F. Adams 
16095b89ad90SMark F. Adams /*MC
16101cc46a46SBarry Smith   PCGAMG - Geometric algebraic multigrid (AMG) preconditioner
16115b89ad90SMark F. Adams 
1612280d9858SJed Brown   Options Database Keys:
16135e4ac8c8Smarkadams4 + -pc_gamg_type <type,default=agg> - one of agg, geo, or classical (only smoothed aggregation, agg, supported)
1614da81f932SPierre Jolivet . -pc_gamg_repartition  <bool,default=false> - repartition the degrees of freedom across the coarse grids as they are determined
161521d928e4Smarkadams4 . -pc_gamg_asm_use_agg <bool,default=false> - use the aggregates from the coasening process to defined the subdomains on each level for the PCASM smoother
1616da81f932SPierre Jolivet . -pc_gamg_process_eq_limit <limit, default=50> - `PCGAMG` will reduce the number of MPI ranks used directly on the coarse grids so that there are around <limit>
1617cab9ed1eSBarry Smith                                         equations on each process that has degrees of freedom
16182d776b49SBarry Smith . -pc_gamg_coarse_eq_limit <limit, default=50> - Set maximum number of equations on coarsest grid to aim for.
161921d928e4Smarkadams4 . -pc_gamg_reuse_interpolation <bool,default=true> - when rebuilding the algebraic multigrid preconditioner reuse the previously computed interpolations (should always be true)
162021d928e4Smarkadams4 . -pc_gamg_threshold[] <thresh,default=[-1,...]> - Before aggregating the graph `PCGAMG` will remove small values from the graph on each level (< 0 does no filtering)
16212d776b49SBarry Smith - -pc_gamg_threshold_scale <scale,default=1> - Scaling of threshold on each coarser grid if not specified
1622cab9ed1eSBarry Smith 
1623f1580f4eSBarry Smith   Options Database Keys for Aggregation:
1624cab9ed1eSBarry Smith + -pc_gamg_agg_nsmooths <nsmooth, default=1> - number of smoothing steps to use with smooth aggregation
1625d529f056Smarkadams4 . -pc_gamg_aggressive_coarsening <n,default=1> - number of aggressive coarsening (MIS-2) levels from finest.
1626d529f056Smarkadams4 . -pc_gamg_aggressive_square_graph <bool,default=false> - Use square graph (A'A) or MIS-k (k=2) for aggressive coarsening
1627d529f056Smarkadams4 . -pc_gamg_mis_k_minimum_degree_ordering <bool,default=true> - Use minimum degree ordering in greedy MIS algorithm
1628d529f056Smarkadams4 - -pc_gamg_aggressive_mis_k <n,default=2> - Number (k) distance in MIS coarsening (>2 is 'aggressive')
1629cab9ed1eSBarry Smith 
1630f1580f4eSBarry Smith   Options Database Keys for Multigrid:
1631a9f5add0SYANG Zongze + -pc_mg_cycle_type <v> - v or w, see `PCMGSetCycleType()`
1632db9745e2SBarry Smith . -pc_mg_distinct_smoothup - configure the up and down (pre and post) smoothers separately, see PCMGSetDistinctSmoothUp()
1633db9745e2SBarry Smith . -pc_mg_type <multiplicative> - (one of) additive multiplicative full kascade
163421d928e4Smarkadams4 - -pc_mg_levels <levels> - Number of levels of multigrid to use. GAMG has a heuristic so pc_mg_levels is not usually used with GAMG
16355b89ad90SMark F. Adams 
163620f4b53cSBarry Smith   Level: intermediate
163720f4b53cSBarry Smith 
163895452b02SPatrick Sanan   Notes:
1639f1580f4eSBarry Smith   To obtain good performance for `PCGAMG` for vector valued problems you must
1640f1580f4eSBarry Smith   call `MatSetBlockSize()` to indicate the number of degrees of freedom per grid point
1641f1580f4eSBarry Smith   call `MatSetNearNullSpace()` (or `PCSetCoordinates()` if solving the equations of elasticity) to indicate the near null space of the operator
1642f1580f4eSBarry Smith 
164304c3f3b8SBarry Smith   The many options for `PCMG` also work directly for `PCGAMG` such as controlling the smoothers on each level etc.
164404c3f3b8SBarry Smith 
164504c3f3b8SBarry Smith   See [the Users Manual section on PCGAMG](sec_amg) and [the Users Manual section on PCMG](sec_mg)for more details.
16461cc46a46SBarry Smith 
1647db781477SPatrick Sanan .seealso: `PCCreate()`, `PCSetType()`, `MatSetBlockSize()`, `PCMGType`, `PCSetCoordinates()`, `MatSetNearNullSpace()`, `PCGAMGSetType()`, `PCGAMGAGG`, `PCGAMGGEO`, `PCGAMGCLASSICAL`, `PCGAMGSetProcEqLim()`,
1648d529f056Smarkadams4           `PCGAMGSetCoarseEqLim()`, `PCGAMGSetRepartition()`, `PCGAMGRegister()`, `PCGAMGSetReuseInterpolation()`, `PCGAMGASMSetUseAggs()`, `PCGAMGSetParallelCoarseGridSolve()`, `PCGAMGSetNlevels()`, `PCGAMGSetThreshold()`, `PCGAMGGetType()`, `PCGAMGSetUseSAEstEig()`
16495b89ad90SMark F. Adams M*/
1650d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PCCreate_GAMG(PC pc)
1651d71ae5a4SJacob Faibussowitsch {
16525b89ad90SMark F. Adams   PC_GAMG *pc_gamg;
16535b89ad90SMark F. Adams   PC_MG   *mg;
16545b89ad90SMark F. Adams 
16555b89ad90SMark F. Adams   PetscFunctionBegin;
16561c1aac46SBarry Smith   /* register AMG type */
16579566063dSJacob Faibussowitsch   PetscCall(PCGAMGInitializePackage());
16581c1aac46SBarry Smith 
16595b89ad90SMark F. Adams   /* PCGAMG is an inherited class of PCMG. Initialize pc as PCMG */
16609566063dSJacob Faibussowitsch   PetscCall(PCSetType(pc, PCMG));
16619566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)pc, PCGAMG));
16625b89ad90SMark F. Adams 
16635b89ad90SMark F. Adams   /* create a supporting struct and attach it to pc */
16644dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&pc_gamg));
16659566063dSJacob Faibussowitsch   PetscCall(PCMGSetGalerkin(pc, PC_MG_GALERKIN_EXTERNAL));
16665b89ad90SMark F. Adams   mg           = (PC_MG *)pc->data;
16675b89ad90SMark F. Adams   mg->innerctx = pc_gamg;
16685b89ad90SMark F. Adams 
16694dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&pc_gamg->ops));
16701ab5ffc9SJed Brown 
16719d5b6da9SMark F. Adams   /* these should be in subctx but repartitioning needs simple arrays */
16729d5b6da9SMark F. Adams   pc_gamg->data_sz = 0;
16730a545947SLisandro Dalcin   pc_gamg->data    = NULL;
16745b89ad90SMark F. Adams 
16759d5b6da9SMark F. Adams   /* overwrite the pointers of PCMG by the functions of base class PCGAMG */
16765b89ad90SMark F. Adams   pc->ops->setfromoptions = PCSetFromOptions_GAMG;
16775b89ad90SMark F. Adams   pc->ops->setup          = PCSetUp_GAMG;
16785b89ad90SMark F. Adams   pc->ops->reset          = PCReset_GAMG;
16795b89ad90SMark F. Adams   pc->ops->destroy        = PCDestroy_GAMG;
16805adeb434SBarry Smith   mg->view                = PCView_GAMG;
16815b89ad90SMark F. Adams 
16829566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetProcEqLim_C", PCGAMGSetProcEqLim_GAMG));
16839566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetCoarseEqLim_C", PCGAMGSetCoarseEqLim_GAMG));
16849566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetRepartition_C", PCGAMGSetRepartition_GAMG));
16859566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetEigenvalues_C", PCGAMGSetEigenvalues_GAMG));
16869566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetUseSAEstEig_C", PCGAMGSetUseSAEstEig_GAMG));
1687*e1cf1444SMark Adams   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetRecomputeEstEig_C", PCGAMGSetRecomputeEstEig_GAMG));
16889566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetReuseInterpolation_C", PCGAMGSetReuseInterpolation_GAMG));
16899566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGASMSetUseAggs_C", PCGAMGASMSetUseAggs_GAMG));
1690d529f056Smarkadams4   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetParallelCoarseGridSolve_C", PCGAMGSetParallelCoarseGridSolve_GAMG));
16919566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetCpuPinCoarseGrids_C", PCGAMGSetCpuPinCoarseGrids_GAMG));
16929566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetCoarseGridLayoutType_C", PCGAMGSetCoarseGridLayoutType_GAMG));
16939566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetThreshold_C", PCGAMGSetThreshold_GAMG));
16949566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetRankReductionFactors_C", PCGAMGSetRankReductionFactors_GAMG));
16959566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetThresholdScale_C", PCGAMGSetThresholdScale_GAMG));
16969566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetType_C", PCGAMGSetType_GAMG));
16979566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGGetType_C", PCGAMGGetType_GAMG));
16989566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetNlevels_C", PCGAMGSetNlevels_GAMG));
16999d5b6da9SMark F. Adams   pc_gamg->repart                          = PETSC_FALSE;
170021d928e4Smarkadams4   pc_gamg->reuse_prol                      = PETSC_TRUE;
17010c3bc534SBarry Smith   pc_gamg->use_aggs_in_asm                 = PETSC_FALSE;
1702171cca9aSMark Adams   pc_gamg->use_parallel_coarse_grid_solver = PETSC_FALSE;
1703a0095786SMark   pc_gamg->cpu_pin_coarse_grids            = PETSC_FALSE;
1704a0095786SMark   pc_gamg->layout_type                     = PCGAMG_LAYOUT_SPREAD;
1705038f3aa4SMark F. Adams   pc_gamg->min_eq_proc                     = 50;
170625a145a7SMark Adams   pc_gamg->coarse_eq_limit                 = 50;
170753134ebeSMark Adams   for (int i = 0; i < PETSC_MG_MAXLEVELS; i++) pc_gamg->threshold[i] = -1;
1708c1eae691SMark Adams   pc_gamg->threshold_scale  = 1.;
170918c3aa7eSMark   pc_gamg->Nlevels          = PETSC_MG_MAXLEVELS;
17109ab59c8bSMark Adams   pc_gamg->current_level    = 0; /* don't need to init really */
17117e6512fdSJed Brown   pc_gamg->use_sa_esteig    = PETSC_TRUE;
1712*e1cf1444SMark Adams   pc_gamg->recompute_esteig = PETSC_TRUE;
171318c3aa7eSMark   pc_gamg->emin             = 0;
171418c3aa7eSMark   pc_gamg->emax             = 0;
171518c3aa7eSMark 
1716c238b0ebSToby Isaac   pc_gamg->ops->createlevel = PCGAMGCreateLevel_GAMG;
17179d5b6da9SMark F. Adams 
1718bd94a7aaSJed Brown   /* PCSetUp_GAMG assumes that the type has been set, so set it to the default now */
17199566063dSJacob Faibussowitsch   PetscCall(PCGAMGSetType(pc, PCGAMGAGG));
17203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17215b89ad90SMark F. Adams }
17223e3471ccSMark Adams 
17233e3471ccSMark Adams /*@C
1724f1580f4eSBarry Smith   PCGAMGInitializePackage - This function initializes everything in the `PCGAMG` package. It is called
1725f1580f4eSBarry Smith   from `PCInitializePackage()`.
17263e3471ccSMark Adams 
17273e3471ccSMark Adams   Level: developer
17283e3471ccSMark Adams 
1729db781477SPatrick Sanan .seealso: `PetscInitialize()`
17303e3471ccSMark Adams @*/
1731d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGInitializePackage(void)
1732d71ae5a4SJacob Faibussowitsch {
17334555aa8cSStefano Zampini   PetscInt l;
17343e3471ccSMark Adams 
17353e3471ccSMark Adams   PetscFunctionBegin;
17363ba16761SJacob Faibussowitsch   if (PCGAMGPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
17373e3471ccSMark Adams   PCGAMGPackageInitialized = PETSC_TRUE;
17389566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&GAMGList, PCGAMGGEO, PCCreateGAMG_GEO));
17399566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&GAMGList, PCGAMGAGG, PCCreateGAMG_AGG));
17409566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&GAMGList, PCGAMGCLASSICAL, PCCreateGAMG_Classical));
17419566063dSJacob Faibussowitsch   PetscCall(PetscRegisterFinalize(PCGAMGFinalizePackage));
1742c1c463dbSMark Adams 
1743c1c463dbSMark Adams   /* general events */
1744849bee69SMark Adams   PetscCall(PetscLogEventRegister("PCSetUp_GAMG+", PC_CLASSID, &petsc_gamg_setup_events[GAMG_SETUP]));
1745849bee69SMark Adams   PetscCall(PetscLogEventRegister(" PCGAMGCreateG", PC_CLASSID, &petsc_gamg_setup_events[GAMG_GRAPH]));
1746849bee69SMark Adams   PetscCall(PetscLogEventRegister(" GAMG Coarsen", PC_CLASSID, &petsc_gamg_setup_events[GAMG_COARSEN]));
1747849bee69SMark Adams   PetscCall(PetscLogEventRegister("  GAMG MIS/Agg", PC_CLASSID, &petsc_gamg_setup_events[GAMG_MIS]));
1748849bee69SMark Adams   PetscCall(PetscLogEventRegister(" PCGAMGProl", PC_CLASSID, &petsc_gamg_setup_events[GAMG_PROL]));
1749849bee69SMark Adams   PetscCall(PetscLogEventRegister("  GAMG Prol-col", PC_CLASSID, &petsc_gamg_setup_events[GAMG_PROLA]));
1750849bee69SMark Adams   PetscCall(PetscLogEventRegister("  GAMG Prol-lift", PC_CLASSID, &petsc_gamg_setup_events[GAMG_PROLB]));
1751849bee69SMark Adams   PetscCall(PetscLogEventRegister(" PCGAMGOptProl", PC_CLASSID, &petsc_gamg_setup_events[GAMG_OPT]));
1752849bee69SMark Adams   PetscCall(PetscLogEventRegister("  GAMG smooth", PC_CLASSID, &petsc_gamg_setup_events[GAMG_OPTSM]));
1753849bee69SMark Adams   PetscCall(PetscLogEventRegister(" PCGAMGCreateL", PC_CLASSID, &petsc_gamg_setup_events[GAMG_LEVEL]));
1754849bee69SMark Adams   PetscCall(PetscLogEventRegister("  GAMG PtAP", PC_CLASSID, &petsc_gamg_setup_events[GAMG_PTAP]));
1755849bee69SMark Adams   PetscCall(PetscLogEventRegister("  GAMG Reduce", PC_CLASSID, &petsc_gamg_setup_events[GAMG_REDUCE]));
1756849bee69SMark Adams   PetscCall(PetscLogEventRegister("   GAMG Repart", PC_CLASSID, &petsc_gamg_setup_events[GAMG_REPART]));
1757849bee69SMark Adams   /* PetscCall(PetscLogEventRegister("   GAMG Inv-Srt", PC_CLASSID, &petsc_gamg_setup_events[SET13])); */
1758849bee69SMark Adams   /* PetscCall(PetscLogEventRegister("   GAMG Move A", PC_CLASSID, &petsc_gamg_setup_events[SET14])); */
1759849bee69SMark Adams   /* PetscCall(PetscLogEventRegister("   GAMG Move P", PC_CLASSID, &petsc_gamg_setup_events[SET15])); */
17604555aa8cSStefano Zampini   for (l = 0; l < PETSC_MG_MAXLEVELS; l++) {
17614555aa8cSStefano Zampini     char ename[32];
17625b89ad90SMark F. Adams 
176363a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(ename, sizeof(ename), "PCGAMG Squ l%02" PetscInt_FMT, l));
17649566063dSJacob Faibussowitsch     PetscCall(PetscLogEventRegister(ename, PC_CLASSID, &petsc_gamg_setup_matmat_events[l][0]));
176563a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(ename, sizeof(ename), "PCGAMG Gal l%02" PetscInt_FMT, l));
17669566063dSJacob Faibussowitsch     PetscCall(PetscLogEventRegister(ename, PC_CLASSID, &petsc_gamg_setup_matmat_events[l][1]));
176763a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(ename, sizeof(ename), "PCGAMG Opt l%02" PetscInt_FMT, l));
17689566063dSJacob Faibussowitsch     PetscCall(PetscLogEventRegister(ename, PC_CLASSID, &petsc_gamg_setup_matmat_events[l][2]));
17694555aa8cSStefano Zampini   }
17704555aa8cSStefano Zampini #if defined(GAMG_STAGES)
1771849bee69SMark Adams   { /* create timer stages */
17725b89ad90SMark F. Adams     char str[32];
1773a364092eSJacob Faibussowitsch     PetscCall(PetscSNPrintf(str, PETSC_STATIC_ARRAY_LENGTH(str), "GAMG Level %d", 0));
17749566063dSJacob Faibussowitsch     PetscCall(PetscLogStageRegister(str, &gamg_stages[0]));
17755b89ad90SMark F. Adams   }
17765b89ad90SMark F. Adams #endif
17773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17783e3471ccSMark Adams }
17793e3471ccSMark Adams 
17803e3471ccSMark Adams /*@C
1781f1580f4eSBarry Smith   PCGAMGFinalizePackage - This function frees everything from the `PCGAMG` package. It is
1782f1580f4eSBarry Smith   called from `PetscFinalize()` automatically.
17833e3471ccSMark Adams 
17843e3471ccSMark Adams   Level: developer
17853e3471ccSMark Adams 
1786db781477SPatrick Sanan .seealso: `PetscFinalize()`
17873e3471ccSMark Adams @*/
1788d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGFinalizePackage(void)
1789d71ae5a4SJacob Faibussowitsch {
17903e3471ccSMark Adams   PetscFunctionBegin;
17913e3471ccSMark Adams   PCGAMGPackageInitialized = PETSC_FALSE;
17929566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListDestroy(&GAMGList));
17933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17943e3471ccSMark Adams }
1795a36cf38bSToby Isaac 
1796a36cf38bSToby Isaac /*@C
1797f1580f4eSBarry Smith   PCGAMGRegister - Register a `PCGAMG` implementation.
1798a36cf38bSToby Isaac 
1799a36cf38bSToby Isaac   Input Parameters:
1800f1580f4eSBarry Smith + type   - string that will be used as the name of the `PCGAMG` type.
1801a36cf38bSToby Isaac - create - function for creating the gamg context.
1802a36cf38bSToby Isaac 
1803f1580f4eSBarry Smith   Level: developer
1804a36cf38bSToby Isaac 
1805db781477SPatrick Sanan .seealso: `PCGAMGType`, `PCGAMG`, `PCGAMGSetType()`
1806a36cf38bSToby Isaac @*/
1807d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGRegister(PCGAMGType type, PetscErrorCode (*create)(PC))
1808d71ae5a4SJacob Faibussowitsch {
1809a36cf38bSToby Isaac   PetscFunctionBegin;
18109566063dSJacob Faibussowitsch   PetscCall(PCGAMGInitializePackage());
18119566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&GAMGList, type, create));
18123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1813a36cf38bSToby Isaac }
18142d776b49SBarry Smith 
18152d776b49SBarry Smith /*@C
18162d776b49SBarry Smith   PCGAMGCreateGraph - Creates a graph that is used by the ``PCGAMGType`` in the coarsening process
18172d776b49SBarry Smith 
18182d776b49SBarry Smith   Input Parameters:
18192d776b49SBarry Smith + pc - the `PCGAMG`
18202d776b49SBarry Smith - A  - the matrix, for any level
18212d776b49SBarry Smith 
18222d776b49SBarry Smith   Output Parameter:
18232d776b49SBarry Smith . G - the graph
18242d776b49SBarry Smith 
18252d776b49SBarry Smith   Level: advanced
18262d776b49SBarry Smith 
18272d776b49SBarry Smith .seealso: `PCGAMGType`, `PCGAMG`, `PCGAMGSetType()`
18282d776b49SBarry Smith @*/
18292d776b49SBarry Smith PetscErrorCode PCGAMGCreateGraph(PC pc, Mat A, Mat *G)
18302d776b49SBarry Smith {
18312d776b49SBarry Smith   PC_MG   *mg      = (PC_MG *)pc->data;
18322d776b49SBarry Smith   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
18332d776b49SBarry Smith 
18342d776b49SBarry Smith   PetscFunctionBegin;
18352d776b49SBarry Smith   PetscCall(pc_gamg->ops->creategraph(pc, A, G));
18363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
18372d776b49SBarry Smith }
1838