xref: /petsc/src/ksp/pc/impls/gamg/gamg.c (revision d529f056d75cd16b380adc3e50ccb741df53d0de)
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));
56358471d46SMark F. Adams           dB = B;
564849bee69SMark Adams #if defined(GAMG_STAGES)
565849bee69SMark Adams           PetscCall(PetscLogStagePop());
566849bee69SMark Adams #endif
56758471d46SMark F. Adams         }
5685f8cf99dSMark F. Adams       }
569d5280255SMark F. Adams 
5709566063dSJacob Faibussowitsch       PetscCall(PCSetUp_MG(pc));
571849bee69SMark Adams       PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_SETUP], 0, 0, 0, 0));
5723ba16761SJacob Faibussowitsch       PetscFunctionReturn(PETSC_SUCCESS);
573eb07cef2SMark F. Adams     }
574878e152fSMark F. Adams   }
575f6536408SMark F. Adams 
576878e152fSMark F. Adams   if (!pc_gamg->data) {
577878e152fSMark F. Adams     if (pc_gamg->orig_data) {
5789566063dSJacob Faibussowitsch       PetscCall(MatGetBlockSize(Pmat, &bs));
5799566063dSJacob Faibussowitsch       PetscCall(MatGetLocalSize(Pmat, &qq, NULL));
5802fa5cd67SKarl Rupp 
581878e152fSMark F. Adams       pc_gamg->data_sz        = (qq / bs) * pc_gamg->orig_data_cell_rows * pc_gamg->orig_data_cell_cols;
582878e152fSMark F. Adams       pc_gamg->data_cell_rows = pc_gamg->orig_data_cell_rows;
583878e152fSMark F. Adams       pc_gamg->data_cell_cols = pc_gamg->orig_data_cell_cols;
5842fa5cd67SKarl Rupp 
5859566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(pc_gamg->data_sz, &pc_gamg->data));
586878e152fSMark F. Adams       for (qq = 0; qq < pc_gamg->data_sz; qq++) pc_gamg->data[qq] = pc_gamg->orig_data[qq];
587806fa848SBarry Smith     } else {
5885f80ce2aSJacob Faibussowitsch       PetscCheck(pc_gamg->ops->createdefaultdata, comm, PETSC_ERR_PLIB, "'createdefaultdata' not set(?) need to support NULL data");
5899566063dSJacob Faibussowitsch       PetscCall(pc_gamg->ops->createdefaultdata(pc, Pmat));
5909d5b6da9SMark F. Adams     }
591878e152fSMark F. Adams   }
592878e152fSMark F. Adams 
593878e152fSMark F. Adams   /* cache original data for reuse */
5941c1aac46SBarry Smith   if (!pc_gamg->orig_data && (PetscBool)(!pc_gamg->reuse_prol)) {
5959566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(pc_gamg->data_sz, &pc_gamg->orig_data));
596878e152fSMark F. Adams     for (qq = 0; qq < pc_gamg->data_sz; qq++) pc_gamg->orig_data[qq] = pc_gamg->data[qq];
597878e152fSMark F. Adams     pc_gamg->orig_data_cell_rows = pc_gamg->data_cell_rows;
598878e152fSMark F. Adams     pc_gamg->orig_data_cell_cols = pc_gamg->data_cell_cols;
599878e152fSMark F. Adams   }
600038e3b61SMark F. Adams 
601302f38e8SMark F. Adams   /* get basic dims */
6029566063dSJacob Faibussowitsch   PetscCall(MatGetBlockSize(Pmat, &bs));
6039566063dSJacob Faibussowitsch   PetscCall(MatGetSize(Pmat, &M, &N));
60484d3f75bSMark F. Adams 
6054279555eSSatish Balay #if defined(PETSC_USE_INFO)
6069566063dSJacob Faibussowitsch   PetscCall(MatGetInfo(Pmat, MAT_GLOBAL_SUM, &info)); /* global reduction */
607569f4572SMark Adams   nnz0   = info.nz_used;
608569f4572SMark Adams   nnztot = info.nz_used;
6094279555eSSatish Balay #endif
610bae903cbSmarkadams4   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));
611569f4572SMark Adams 
612a2f3521dSMark F. Adams   /* Get A_i and R_i */
61362294041SBarry Smith   for (level = 0, Aarr[0] = Pmat, nactivepe = size; level < (pc_gamg->Nlevels - 1) && (!level || M > pc_gamg->coarse_eq_limit); level++) {
6149ab59c8bSMark Adams     pc_gamg->current_level = level;
61563a3b9bcSJacob Faibussowitsch     PetscCheck(level < PETSC_MG_MAXLEVELS, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Too many levels %" PetscInt_FMT, level);
6165b89ad90SMark F. Adams     level1 = level + 1;
6174555aa8cSStefano Zampini #if defined(GAMG_STAGES)
618849bee69SMark Adams     if (!gamg_stages[level]) {
619849bee69SMark Adams       char str[32];
620a364092eSJacob Faibussowitsch       PetscCall(PetscSNPrintf(str, PETSC_STATIC_ARRAY_LENGTH(str), "GAMG Level %d", (int)level));
621849bee69SMark Adams       PetscCall(PetscLogStageRegister(str, &gamg_stages[level]));
622849bee69SMark Adams     }
6239566063dSJacob Faibussowitsch     PetscCall(PetscLogStagePush(gamg_stages[level]));
624b4fbaa2aSMark F. Adams #endif
625c8b0795cSMark F. Adams     { /* construct prolongator */
626725b86d8SJed Brown       Mat               Gmat;
6270cbbd2e1SMark F. Adams       PetscCoarsenData *agg_lists;
6287700e67bSMark Adams       Mat               Prol11;
629c8b0795cSMark F. Adams 
6302d776b49SBarry Smith       PetscCall(PCGAMGCreateGraph(pc, Aarr[level], &Gmat));
6319566063dSJacob Faibussowitsch       PetscCall(pc_gamg->ops->coarsen(pc, &Gmat, &agg_lists));
6329566063dSJacob Faibussowitsch       PetscCall(pc_gamg->ops->prolongator(pc, Aarr[level], Gmat, agg_lists, &Prol11));
633c8b0795cSMark F. Adams 
634a2f3521dSMark F. Adams       /* could have failed to create new level */
635a2f3521dSMark F. Adams       if (Prol11) {
636f7df55f0SStefano Zampini         const char *prefix;
637f7df55f0SStefano Zampini         char        addp[32];
638f7df55f0SStefano Zampini 
6399d5b6da9SMark F. Adams         /* get new block size of coarse matrices */
6409566063dSJacob Faibussowitsch         PetscCall(MatGetBlockSizes(Prol11, NULL, &bs));
641a2f3521dSMark F. Adams 
642fd1112cbSBarry Smith         if (pc_gamg->ops->optprolongator) {
643c8b0795cSMark F. Adams           /* smooth */
6449566063dSJacob Faibussowitsch           PetscCall(pc_gamg->ops->optprolongator(pc, Aarr[level], &Prol11));
645c8b0795cSMark F. Adams         }
646c8b0795cSMark F. Adams 
6470c3bc534SBarry Smith         if (pc_gamg->use_aggs_in_asm) {
6481b18a24aSMark Adams           PetscInt bs;
649849bee69SMark Adams           PetscCall(MatGetBlockSizes(Prol11, &bs, NULL)); // not timed directly, ugly, could remove, but good ASM method
6509566063dSJacob Faibussowitsch           PetscCall(PetscCDGetASMBlocks(agg_lists, bs, Gmat, &nASMBlocksArr[level], &ASMLocalIDsArr[level]));
651ffc955d6SMark F. Adams         }
652ffc955d6SMark F. Adams 
6539566063dSJacob Faibussowitsch         PetscCall(PCGetOptionsPrefix(pc, &prefix));
6549566063dSJacob Faibussowitsch         PetscCall(MatSetOptionsPrefix(Prol11, prefix));
6559566063dSJacob Faibussowitsch         PetscCall(PetscSNPrintf(addp, sizeof(addp), "pc_gamg_prolongator_%d_", (int)level));
6569566063dSJacob Faibussowitsch         PetscCall(MatAppendOptionsPrefix(Prol11, addp));
65791f31d3dSStefano Zampini         /* Always generate the transpose with CUDA
658f7df55f0SStefano Zampini            Such behaviour can be adapted with -pc_gamg_prolongator_ prefixed options */
6599566063dSJacob Faibussowitsch         PetscCall(MatSetOption(Prol11, MAT_FORM_EXPLICIT_TRANSPOSE, PETSC_TRUE));
6609566063dSJacob Faibussowitsch         PetscCall(MatSetFromOptions(Prol11));
6614bde40a0SMark Adams         Parr[level1] = Prol11;
6624bde40a0SMark Adams       } else Parr[level1] = NULL; /* failed to coarsen */
6634bde40a0SMark Adams 
6649566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&Gmat));
6659566063dSJacob Faibussowitsch       PetscCall(PetscCDDestroy(agg_lists));
666a2f3521dSMark F. Adams     }                           /* construct prolongator scope */
6677f66b68fSMark Adams     if (!level) Aarr[0] = Pmat; /* use Pmat for finest level setup */
668171cca9aSMark Adams     if (!Parr[level1]) {        /* failed to coarsen */
66963a3b9bcSJacob Faibussowitsch       PetscCall(PetscInfo(pc, "%s: Stop gridding, level %" PetscInt_FMT "\n", ((PetscObject)pc)->prefix, level));
6704555aa8cSStefano Zampini #if defined(GAMG_STAGES)
6719566063dSJacob Faibussowitsch       PetscCall(PetscLogStagePop());
672a90e85d9SMark Adams #endif
673c8b0795cSMark F. Adams       break;
674c8b0795cSMark F. Adams     }
6759566063dSJacob Faibussowitsch     PetscCall(MatGetSize(Parr[level1], &M, &N)); /* N is next M, a loop test variables */
6762472a847SBarry Smith     PetscCheck(!is_last, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Is last ?");
677171cca9aSMark Adams     if (N <= pc_gamg->coarse_eq_limit) is_last = PETSC_TRUE;
6780e2909e1SMark Adams     if (level1 == pc_gamg->Nlevels - 1) is_last = PETSC_TRUE;
679849bee69SMark Adams     PetscCall(PetscLogEventBegin(petsc_gamg_setup_events[GAMG_LEVEL], 0, 0, 0, 0));
6809566063dSJacob Faibussowitsch     PetscCall(pc_gamg->ops->createlevel(pc, Aarr[level], bs, &Parr[level1], &Aarr[level1], &nactivepe, NULL, is_last));
681849bee69SMark Adams     PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_LEVEL], 0, 0, 0, 0));
682a2f3521dSMark F. Adams 
6839566063dSJacob Faibussowitsch     PetscCall(MatGetSize(Aarr[level1], &M, &N)); /* M is loop test variables */
6844279555eSSatish Balay #if defined(PETSC_USE_INFO)
6859566063dSJacob Faibussowitsch     PetscCall(MatGetInfo(Aarr[level1], MAT_GLOBAL_SUM, &info));
686569f4572SMark Adams     nnztot += info.nz_used;
6874279555eSSatish Balay #endif
688bae903cbSmarkadams4     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));
689569f4572SMark Adams 
6904555aa8cSStefano Zampini #if defined(GAMG_STAGES)
6919566063dSJacob Faibussowitsch     PetscCall(PetscLogStagePop());
692b4fbaa2aSMark F. Adams #endif
693a90e85d9SMark Adams     /* stop if one node or one proc -- could pull back for singular problems */
6949ab59c8bSMark Adams     if ((pc_gamg->data_cell_cols && M / pc_gamg->data_cell_cols < 2) || (!pc_gamg->data_cell_cols && M / bs < 2)) {
69563a3b9bcSJacob 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));
696a90e85d9SMark Adams       level++;
697a90e85d9SMark Adams       break;
698a90e85d9SMark Adams     }
699c8b0795cSMark F. Adams   } /* levels */
7009566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc_gamg->data));
701c8b0795cSMark F. Adams 
702ba4ceb06Smarkadams4   PetscCall(PetscInfo(pc, "%s: %" PetscInt_FMT " levels, operator complexity = %g\n", ((PetscObject)pc)->prefix, level + 1, nnztot / nnz0));
7039d5b6da9SMark F. Adams   pc_gamg->Nlevels = level + 1;
7045b89ad90SMark F. Adams   fine_level       = level;
7059566063dSJacob Faibussowitsch   PetscCall(PCMGSetLevels(pc, pc_gamg->Nlevels, NULL));
7065b89ad90SMark F. Adams 
70762294041SBarry Smith   if (pc_gamg->Nlevels > 1) { /* don't setup MG if one level */
7080ed2132dSStefano Zampini 
709d5280255SMark F. Adams     /* set default smoothers & set operators */
71062294041SBarry Smith     for (lidx = 1, level = pc_gamg->Nlevels - 2; lidx <= fine_level; lidx++, level--) {
711ffc955d6SMark F. Adams       KSP smoother;
712ffc955d6SMark F. Adams       PC  subpc;
713a2f3521dSMark F. Adams 
7149566063dSJacob Faibussowitsch       PetscCall(PCMGGetSmoother(pc, lidx, &smoother));
7159566063dSJacob Faibussowitsch       PetscCall(KSPGetPC(smoother, &subpc));
716ffc955d6SMark F. Adams 
7179566063dSJacob Faibussowitsch       PetscCall(KSPSetNormType(smoother, KSP_NORM_NONE));
718a2f3521dSMark F. Adams       /* set ops */
7199566063dSJacob Faibussowitsch       PetscCall(KSPSetOperators(smoother, Aarr[level], Aarr[level]));
7209566063dSJacob Faibussowitsch       PetscCall(PCMGSetInterpolation(pc, lidx, Parr[level + 1]));
721a2f3521dSMark F. Adams 
722a2f3521dSMark F. Adams       /* set defaults */
7239566063dSJacob Faibussowitsch       PetscCall(KSPSetType(smoother, KSPCHEBYSHEV));
724a2f3521dSMark F. Adams 
7250c3bc534SBarry Smith       /* set blocks for ASM smoother that uses the 'aggregates' */
7260c3bc534SBarry Smith       if (pc_gamg->use_aggs_in_asm) {
7272d3561bbSSatish Balay         PetscInt sz;
7287a28f3e5SMark Adams         IS      *iss;
729a2f3521dSMark F. Adams 
7302d3561bbSSatish Balay         sz  = nASMBlocksArr[level];
7317a28f3e5SMark Adams         iss = ASMLocalIDsArr[level];
7329566063dSJacob Faibussowitsch         PetscCall(PCSetType(subpc, PCASM));
7339566063dSJacob Faibussowitsch         PetscCall(PCASMSetOverlap(subpc, 0));
7349566063dSJacob Faibussowitsch         PetscCall(PCASMSetType(subpc, PC_ASM_BASIC));
7357f66b68fSMark Adams         if (!sz) {
736ffc955d6SMark F. Adams           IS is;
7379566063dSJacob Faibussowitsch           PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 0, NULL, PETSC_COPY_VALUES, &is));
7389566063dSJacob Faibussowitsch           PetscCall(PCASMSetLocalSubdomains(subpc, 1, NULL, &is));
7399566063dSJacob Faibussowitsch           PetscCall(ISDestroy(&is));
740806fa848SBarry Smith         } else {
741a94c3b12SMark F. Adams           PetscInt kk;
7422eab5cd7Smarkadams4           PetscCall(PCASMSetLocalSubdomains(subpc, sz, iss, NULL));
74348a46eb9SPierre Jolivet           for (kk = 0; kk < sz; kk++) PetscCall(ISDestroy(&iss[kk]));
7449566063dSJacob Faibussowitsch           PetscCall(PetscFree(iss));
745ffc955d6SMark F. Adams         }
7460298fd71SBarry Smith         ASMLocalIDsArr[level] = NULL;
747ffc955d6SMark F. Adams         nASMBlocksArr[level]  = 0;
748806fa848SBarry Smith       } else {
7499566063dSJacob Faibussowitsch         PetscCall(PCSetType(subpc, PCJACOBI));
750ffc955d6SMark F. Adams       }
751d5280255SMark F. Adams     }
752d5280255SMark F. Adams     {
753d5280255SMark F. Adams       /* coarse grid */
7549371c9d4SSatish Balay       KSP      smoother, *k2;
7559371c9d4SSatish Balay       PC       subpc, pc2;
7569371c9d4SSatish Balay       PetscInt ii, first;
7579371c9d4SSatish Balay       Mat      Lmat = Aarr[(level = pc_gamg->Nlevels - 1)];
7589371c9d4SSatish Balay       lidx          = 0;
7590ed2132dSStefano Zampini 
7609566063dSJacob Faibussowitsch       PetscCall(PCMGGetSmoother(pc, lidx, &smoother));
7619566063dSJacob Faibussowitsch       PetscCall(KSPSetOperators(smoother, Lmat, Lmat));
762cf8ae1d3SMark Adams       if (!pc_gamg->use_parallel_coarse_grid_solver) {
7639566063dSJacob Faibussowitsch         PetscCall(KSPSetNormType(smoother, KSP_NORM_NONE));
7649566063dSJacob Faibussowitsch         PetscCall(KSPGetPC(smoother, &subpc));
7659566063dSJacob Faibussowitsch         PetscCall(PCSetType(subpc, PCBJACOBI));
7669566063dSJacob Faibussowitsch         PetscCall(PCSetUp(subpc));
7679566063dSJacob Faibussowitsch         PetscCall(PCBJacobiGetSubKSP(subpc, &ii, &first, &k2));
76863a3b9bcSJacob Faibussowitsch         PetscCheck(ii == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "ii %" PetscInt_FMT " is not one", ii);
7699566063dSJacob Faibussowitsch         PetscCall(KSPGetPC(k2[0], &pc2));
7709566063dSJacob Faibussowitsch         PetscCall(PCSetType(pc2, PCLU));
7719566063dSJacob Faibussowitsch         PetscCall(PCFactorSetShiftType(pc2, MAT_SHIFT_INBLOCKS));
7729566063dSJacob Faibussowitsch         PetscCall(KSPSetTolerances(k2[0], PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT, 1));
7739566063dSJacob Faibussowitsch         PetscCall(KSPSetType(k2[0], KSPPREONLY));
774d5280255SMark F. Adams       }
775cf8ae1d3SMark Adams     }
776d5280255SMark F. Adams 
777d5280255SMark F. Adams     /* should be called in PCSetFromOptions_GAMG(), but cannot be called prior to PCMGSetLevels() */
778d0609cedSBarry Smith     PetscObjectOptionsBegin((PetscObject)pc);
779dbbe0bcdSBarry Smith     PetscCall(PCSetFromOptions_MG(pc, PetscOptionsObject));
780d0609cedSBarry Smith     PetscOptionsEnd();
7819566063dSJacob Faibussowitsch     PetscCall(PCMGSetGalerkin(pc, PC_MG_GALERKIN_EXTERNAL));
782d5280255SMark F. Adams 
783f1580f4eSBarry Smith     /* set cheby eigen estimates from SA to use in the solver */
7847e6512fdSJed Brown     if (pc_gamg->use_sa_esteig) {
78518c3aa7eSMark       for (lidx = 1, level = pc_gamg->Nlevels - 2; level >= 0; lidx++, level--) {
78618c3aa7eSMark         KSP       smoother;
78718c3aa7eSMark         PetscBool ischeb;
7880ed2132dSStefano Zampini 
7899566063dSJacob Faibussowitsch         PetscCall(PCMGGetSmoother(pc, lidx, &smoother));
7909566063dSJacob Faibussowitsch         PetscCall(PetscObjectTypeCompare((PetscObject)smoother, KSPCHEBYSHEV, &ischeb));
79118c3aa7eSMark         if (ischeb) {
79218c3aa7eSMark           KSP_Chebyshev *cheb = (KSP_Chebyshev *)smoother->data;
7930ed2132dSStefano Zampini 
7942de708cbSJed Brown           // The command line will override these settings because KSPSetFromOptions is called in PCSetUp_MG
795efe053fcSJed Brown           if (mg->max_eigen_DinvA[level] > 0) {
7967e6512fdSJed Brown             // SA uses Jacobi for P; we use SA estimates if the smoother is also Jacobi or if the user explicitly requested it.
7977e6512fdSJed Brown             // TODO: This should test whether it's the same Jacobi variant (DIAG, ROWSUM, etc.)
79818c3aa7eSMark             PetscReal emax, emin;
7990ed2132dSStefano Zampini 
80018c3aa7eSMark             emin = mg->min_eigen_DinvA[level];
80118c3aa7eSMark             emax = mg->max_eigen_DinvA[level];
80263a3b9bcSJacob 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));
8030a94a983SJed Brown             cheb->emin_provided = emin;
8040a94a983SJed Brown             cheb->emax_provided = emax;
80518c3aa7eSMark           }
80618c3aa7eSMark         }
80718c3aa7eSMark       }
80818c3aa7eSMark     }
8090ed2132dSStefano Zampini 
8109566063dSJacob Faibussowitsch     PetscCall(PCSetUp_MG(pc));
8110ed2132dSStefano Zampini 
812d5280255SMark F. Adams     /* clean up */
813d5280255SMark F. Adams     for (level = 1; level < pc_gamg->Nlevels; level++) {
8149566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&Parr[level]));
8159566063dSJacob Faibussowitsch       PetscCall(MatDestroy(&Aarr[level]));
8165b89ad90SMark F. Adams     }
817806fa848SBarry Smith   } else {
8185f8cf99dSMark F. Adams     KSP smoother;
8190ed2132dSStefano Zampini 
82063a3b9bcSJacob Faibussowitsch     PetscCall(PetscInfo(pc, "%s: One level solver used (system is seen as DD). Using default solver.\n", ((PetscObject)pc)->prefix));
8219566063dSJacob Faibussowitsch     PetscCall(PCMGGetSmoother(pc, 0, &smoother));
8229566063dSJacob Faibussowitsch     PetscCall(KSPSetOperators(smoother, Aarr[0], Aarr[0]));
8239566063dSJacob Faibussowitsch     PetscCall(KSPSetType(smoother, KSPPREONLY));
8249566063dSJacob Faibussowitsch     PetscCall(PCSetUp_MG(pc));
8255f8cf99dSMark F. Adams   }
826849bee69SMark Adams   PetscCall(PetscLogEventEnd(petsc_gamg_setup_events[GAMG_SETUP], 0, 0, 0, 0));
8273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8285b89ad90SMark F. Adams }
8295b89ad90SMark F. Adams 
8305b89ad90SMark F. Adams /*
8315b89ad90SMark F. Adams  PCDestroy_GAMG - Destroys the private context for the GAMG preconditioner
8325b89ad90SMark F. Adams    that was created with PCCreate_GAMG().
8335b89ad90SMark F. Adams 
8345b89ad90SMark F. Adams    Input Parameter:
8355b89ad90SMark F. Adams .  pc - the preconditioner context
8365b89ad90SMark F. Adams 
8375b89ad90SMark F. Adams    Application Interface Routine: PCDestroy()
8385b89ad90SMark F. Adams */
839d71ae5a4SJacob Faibussowitsch PetscErrorCode PCDestroy_GAMG(PC pc)
840d71ae5a4SJacob Faibussowitsch {
8415b89ad90SMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
8425b89ad90SMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
8435b89ad90SMark F. Adams 
8445b89ad90SMark F. Adams   PetscFunctionBegin;
8459566063dSJacob Faibussowitsch   PetscCall(PCReset_GAMG(pc));
8461baa6e33SBarry Smith   if (pc_gamg->ops->destroy) PetscCall((*pc_gamg->ops->destroy)(pc));
8479566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc_gamg->ops));
8489566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc_gamg->gamg_type_name));
8499566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc_gamg));
8502e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetProcEqLim_C", NULL));
8512e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetCoarseEqLim_C", NULL));
8522e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetRepartition_C", NULL));
8532e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetEigenvalues_C", NULL));
8542e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetUseSAEstEig_C", NULL));
8552e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetReuseInterpolation_C", NULL));
8562e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGASMSetUseAggs_C", NULL));
857*d529f056Smarkadams4   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetParallelCoarseGridSolve_C", NULL));
8582e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetCpuPinCoarseGrids_C", NULL));
8592e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetCoarseGridLayoutType_C", NULL));
8602e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetThreshold_C", NULL));
8612e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetRankReductionFactors_C", NULL));
8622e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetThresholdScale_C", NULL));
8632e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetType_C", NULL));
8642e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGGetType_C", NULL));
8652e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetNlevels_C", NULL));
8669566063dSJacob Faibussowitsch   PetscCall(PCDestroy_MG(pc));
8673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8685b89ad90SMark F. Adams }
8695b89ad90SMark F. Adams 
870676e1743SMark F. Adams /*@
871f1580f4eSBarry Smith   PCGAMGSetProcEqLim - Set number of equations to aim for per process on the coarse grids via processor reduction in `PCGAMG`
872676e1743SMark F. Adams 
873c3339decSBarry Smith   Logically Collective
874676e1743SMark F. Adams 
875676e1743SMark F. Adams   Input Parameters:
8761cc46a46SBarry Smith + pc - the preconditioner context
8771cc46a46SBarry Smith - n  - the number of equations
878676e1743SMark F. Adams 
879676e1743SMark F. Adams   Options Database Key:
880147403d9SBarry Smith . -pc_gamg_process_eq_limit <limit> - set the limit
881676e1743SMark F. Adams 
88220f4b53cSBarry Smith   Level: intermediate
88320f4b53cSBarry Smith 
884f1580f4eSBarry Smith   Note:
885f1580f4eSBarry 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
886cab9ed1eSBarry Smith   that has degrees of freedom
887cab9ed1eSBarry Smith 
888f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCGAMGSetCoarseEqLim()`, `PCGAMGSetRankReductionFactors()`, `PCGAMGSetRepartition()`
889676e1743SMark F. Adams @*/
890d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetProcEqLim(PC pc, PetscInt n)
891d71ae5a4SJacob Faibussowitsch {
892676e1743SMark F. Adams   PetscFunctionBegin;
893676e1743SMark F. Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
894cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetProcEqLim_C", (PC, PetscInt), (pc, n));
8953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
896676e1743SMark F. Adams }
897676e1743SMark F. Adams 
898d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetProcEqLim_GAMG(PC pc, PetscInt n)
899d71ae5a4SJacob Faibussowitsch {
900c20e4228SMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
901c20e4228SMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
902676e1743SMark F. Adams 
903676e1743SMark F. Adams   PetscFunctionBegin;
9049d5b6da9SMark F. Adams   if (n > 0) pc_gamg->min_eq_proc = n;
9053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
906676e1743SMark F. Adams }
907676e1743SMark F. Adams 
908389730f3SMark F. Adams /*@
909f1580f4eSBarry Smith   PCGAMGSetCoarseEqLim - Set maximum number of equations on the coarsest grid of `PCGAMG`
910389730f3SMark F. Adams 
911c3339decSBarry Smith   Collective
912389730f3SMark F. Adams 
913389730f3SMark F. Adams   Input Parameters:
9141cc46a46SBarry Smith + pc - the preconditioner context
9151cc46a46SBarry Smith - n  - maximum number of equations to aim for
916389730f3SMark F. Adams 
917389730f3SMark F. Adams   Options Database Key:
918147403d9SBarry Smith . -pc_gamg_coarse_eq_limit <limit> - set the limit
919389730f3SMark F. Adams 
92020f4b53cSBarry Smith   Level: intermediate
92120f4b53cSBarry Smith 
922f1580f4eSBarry Smith   Note:
923f1580f4eSBarry Smith   For example -pc_gamg_coarse_eq_limit 1000 will stop coarsening once the coarse grid
92474329af1SBarry Smith   has less than 1000 unknowns.
92574329af1SBarry Smith 
926f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCGAMGSetProcEqLim()`, `PCGAMGSetRankReductionFactors()`, `PCGAMGSetRepartition()`
927389730f3SMark F. Adams @*/
928d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetCoarseEqLim(PC pc, PetscInt n)
929d71ae5a4SJacob Faibussowitsch {
930389730f3SMark F. Adams   PetscFunctionBegin;
931389730f3SMark F. Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
932cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetCoarseEqLim_C", (PC, PetscInt), (pc, n));
9333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
934389730f3SMark F. Adams }
935389730f3SMark F. Adams 
936d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetCoarseEqLim_GAMG(PC pc, PetscInt n)
937d71ae5a4SJacob Faibussowitsch {
938389730f3SMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
939389730f3SMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
940389730f3SMark F. Adams 
941389730f3SMark F. Adams   PetscFunctionBegin;
9429d5b6da9SMark F. Adams   if (n > 0) pc_gamg->coarse_eq_limit = n;
9433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
944389730f3SMark F. Adams }
945389730f3SMark F. Adams 
946676e1743SMark F. Adams /*@
947f1580f4eSBarry Smith   PCGAMGSetRepartition - Repartition the degrees of freedom across the processors on the coarser grids when reducing the number of MPI ranks to use
948676e1743SMark F. Adams 
949c3339decSBarry Smith   Collective
950676e1743SMark F. Adams 
951676e1743SMark F. Adams   Input Parameters:
9521cc46a46SBarry Smith + pc - the preconditioner context
953f1580f4eSBarry Smith - n  - `PETSC_TRUE` or `PETSC_FALSE`
954676e1743SMark F. Adams 
955676e1743SMark F. Adams   Options Database Key:
956147403d9SBarry Smith . -pc_gamg_repartition <true,false> - turn on the repartitioning
957676e1743SMark F. Adams 
95820f4b53cSBarry Smith   Level: intermediate
95920f4b53cSBarry Smith 
960f1580f4eSBarry Smith   Note:
961f1580f4eSBarry Smith   This will generally improve the loading balancing of the work on each level
962cab9ed1eSBarry Smith 
963f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCGAMGSetProcEqLim()`, `PCGAMGSetRankReductionFactors()`
964676e1743SMark F. Adams @*/
965d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetRepartition(PC pc, PetscBool n)
966d71ae5a4SJacob Faibussowitsch {
967676e1743SMark F. Adams   PetscFunctionBegin;
968676e1743SMark F. Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
969cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetRepartition_C", (PC, PetscBool), (pc, n));
9703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
971676e1743SMark F. Adams }
972676e1743SMark F. Adams 
973d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetRepartition_GAMG(PC pc, PetscBool n)
974d71ae5a4SJacob Faibussowitsch {
975c20e4228SMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
976c20e4228SMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
977676e1743SMark F. Adams 
978676e1743SMark F. Adams   PetscFunctionBegin;
9799d5b6da9SMark F. Adams   pc_gamg->repart = n;
9803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
981676e1743SMark F. Adams }
982676e1743SMark F. Adams 
983dfd5c07aSMark F. Adams /*@
984f1580f4eSBarry Smith   PCGAMGSetUseSAEstEig - Use the eigen estimate from smoothed aggregation for the Chebyshev smoother during the solution process
98518c3aa7eSMark 
986c3339decSBarry Smith   Collective
98718c3aa7eSMark 
98818c3aa7eSMark   Input Parameters:
98918c3aa7eSMark + pc - the preconditioner context
99018c3aa7eSMark - n  - number of its
99118c3aa7eSMark 
99218c3aa7eSMark   Options Database Key:
993147403d9SBarry Smith . -pc_gamg_use_sa_esteig <true,false> - use the eigen estimate
99418c3aa7eSMark 
99520f4b53cSBarry Smith   Level: advanced
99620f4b53cSBarry Smith 
99718c3aa7eSMark   Notes:
9987e6512fdSJed 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$.
999f1580f4eSBarry Smith   Eigenvalue estimates (based on a few `PCCG` or `PCGMRES` iterations) are computed to choose $\omega$ so that this is a stable smoothing operation.
1000f1580f4eSBarry Smith   If Chebyshev with Jacobi (diagonal) preconditioning is used for smoothing, then the eigenvalue estimates can be reused during the solution process
1001f1580f4eSBarry Smith   This option is only used when the smoother uses Jacobi, and should be turned off if a different `PCJacobiType` is used.
1002efe053fcSJed Brown   It became default in PETSc 3.17.
100318c3aa7eSMark 
1004f1580f4eSBarry Smith .seealso: `PCGAMG`, `KSPChebyshevSetEigenvalues()`, `KSPChebyshevEstEigSet()`
100518c3aa7eSMark @*/
1006d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetUseSAEstEig(PC pc, PetscBool n)
1007d71ae5a4SJacob Faibussowitsch {
100818c3aa7eSMark   PetscFunctionBegin;
100918c3aa7eSMark   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1010cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetUseSAEstEig_C", (PC, PetscBool), (pc, n));
10113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
101218c3aa7eSMark }
101318c3aa7eSMark 
1014d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetUseSAEstEig_GAMG(PC pc, PetscBool n)
1015d71ae5a4SJacob Faibussowitsch {
101618c3aa7eSMark   PC_MG   *mg      = (PC_MG *)pc->data;
101718c3aa7eSMark   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
101818c3aa7eSMark 
101918c3aa7eSMark   PetscFunctionBegin;
10207e6512fdSJed Brown   pc_gamg->use_sa_esteig = n;
10213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
102218c3aa7eSMark }
102318c3aa7eSMark 
102418c3aa7eSMark /*@
1025f1580f4eSBarry Smith   PCGAMGSetEigenvalues - Set WHAT eigenvalues WHY?
102618c3aa7eSMark 
1027c3339decSBarry Smith   Collective
102818c3aa7eSMark 
102918c3aa7eSMark   Input Parameters:
103018c3aa7eSMark + pc   - the preconditioner context
1031feefa0e1SJacob Faibussowitsch . emax - max eigenvalue
1032feefa0e1SJacob Faibussowitsch - emin - min eigenvalue
103318c3aa7eSMark 
103418c3aa7eSMark   Options Database Key:
1035147403d9SBarry Smith . -pc_gamg_eigenvalues <emin,emax> - estimates of the eigenvalues
103618c3aa7eSMark 
103718c3aa7eSMark   Level: intermediate
103818c3aa7eSMark 
1039f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCGAMGSetUseSAEstEig()`
104018c3aa7eSMark @*/
1041d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetEigenvalues(PC pc, PetscReal emax, PetscReal emin)
1042d71ae5a4SJacob Faibussowitsch {
104318c3aa7eSMark   PetscFunctionBegin;
104418c3aa7eSMark   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1045cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetEigenvalues_C", (PC, PetscReal, PetscReal), (pc, emax, emin));
10463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
104718c3aa7eSMark }
104841ffd417SStefano Zampini 
1049d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetEigenvalues_GAMG(PC pc, PetscReal emax, PetscReal emin)
1050d71ae5a4SJacob Faibussowitsch {
105118c3aa7eSMark   PC_MG   *mg      = (PC_MG *)pc->data;
105218c3aa7eSMark   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
105318c3aa7eSMark 
105418c3aa7eSMark   PetscFunctionBegin;
10555f80ce2aSJacob 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);
10565f80ce2aSJacob 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);
105718c3aa7eSMark   pc_gamg->emax = emax;
105818c3aa7eSMark   pc_gamg->emin = emin;
10593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
106018c3aa7eSMark }
106118c3aa7eSMark 
106218c3aa7eSMark /*@
1063f1580f4eSBarry Smith   PCGAMGSetReuseInterpolation - Reuse prolongation when rebuilding a `PCGAMG` algebraic multigrid preconditioner
1064dfd5c07aSMark F. Adams 
1065c3339decSBarry Smith   Collective
1066dfd5c07aSMark F. Adams 
1067dfd5c07aSMark F. Adams   Input Parameters:
10681cc46a46SBarry Smith + pc - the preconditioner context
1069f1580f4eSBarry Smith - n  - `PETSC_TRUE` or `PETSC_FALSE`
1070dfd5c07aSMark F. Adams 
1071dfd5c07aSMark F. Adams   Options Database Key:
1072147403d9SBarry Smith . -pc_gamg_reuse_interpolation <true,false> - reuse the previous interpolation
1073dfd5c07aSMark F. Adams 
1074dfd5c07aSMark F. Adams   Level: intermediate
1075dfd5c07aSMark F. Adams 
1076f1580f4eSBarry Smith   Note:
1077147403d9SBarry Smith   May negatively affect the convergence rate of the method on new matrices if the matrix entries change a great deal, but allows
1078cab9ed1eSBarry Smith   rebuilding the preconditioner quicker.
1079cab9ed1eSBarry Smith 
1080f1580f4eSBarry Smith .seealso: `PCGAMG`
1081dfd5c07aSMark F. Adams @*/
1082d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetReuseInterpolation(PC pc, PetscBool n)
1083d71ae5a4SJacob Faibussowitsch {
1084dfd5c07aSMark F. Adams   PetscFunctionBegin;
1085dfd5c07aSMark F. Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1086cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetReuseInterpolation_C", (PC, PetscBool), (pc, n));
10873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1088dfd5c07aSMark F. Adams }
1089dfd5c07aSMark F. Adams 
1090d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetReuseInterpolation_GAMG(PC pc, PetscBool n)
1091d71ae5a4SJacob Faibussowitsch {
1092dfd5c07aSMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
1093dfd5c07aSMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1094dfd5c07aSMark F. Adams 
1095dfd5c07aSMark F. Adams   PetscFunctionBegin;
1096dfd5c07aSMark F. Adams   pc_gamg->reuse_prol = n;
10973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1098dfd5c07aSMark F. Adams }
1099dfd5c07aSMark F. Adams 
1100ffc955d6SMark F. Adams /*@
1101f1580f4eSBarry 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
1102f1580f4eSBarry Smith   used as the smoother
1103ffc955d6SMark F. Adams 
1104c3339decSBarry Smith   Collective
1105ffc955d6SMark F. Adams 
1106ffc955d6SMark F. Adams   Input Parameters:
1107cab9ed1eSBarry Smith + pc  - the preconditioner context
1108f1580f4eSBarry Smith - flg - `PETSC_TRUE` to use aggregates, `PETSC_FALSE` to not
1109ffc955d6SMark F. Adams 
1110ffc955d6SMark F. Adams   Options Database Key:
1111147403d9SBarry Smith . -pc_gamg_asm_use_agg <true,false> - use aggregates to define the additive Schwarz subdomains
1112ffc955d6SMark F. Adams 
1113ffc955d6SMark F. Adams   Level: intermediate
1114ffc955d6SMark F. Adams 
1115f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCASM`, `PCSetType`
1116ffc955d6SMark F. Adams @*/
1117d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGASMSetUseAggs(PC pc, PetscBool flg)
1118d71ae5a4SJacob Faibussowitsch {
1119ffc955d6SMark F. Adams   PetscFunctionBegin;
1120ffc955d6SMark F. Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1121cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGASMSetUseAggs_C", (PC, PetscBool), (pc, flg));
11223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1123ffc955d6SMark F. Adams }
1124ffc955d6SMark F. Adams 
1125d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGASMSetUseAggs_GAMG(PC pc, PetscBool flg)
1126d71ae5a4SJacob Faibussowitsch {
1127ffc955d6SMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
1128ffc955d6SMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1129ffc955d6SMark F. Adams 
1130ffc955d6SMark F. Adams   PetscFunctionBegin;
1131cab9ed1eSBarry Smith   pc_gamg->use_aggs_in_asm = flg;
11323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1133ffc955d6SMark F. Adams }
1134ffc955d6SMark F. Adams 
1135171cca9aSMark Adams /*@
1136*d529f056Smarkadams4   PCGAMGSetParallelCoarseGridSolve - allow a parallel coarse grid solver
1137171cca9aSMark Adams 
1138c3339decSBarry Smith   Collective
1139171cca9aSMark Adams 
1140171cca9aSMark Adams   Input Parameters:
1141171cca9aSMark Adams + pc  - the preconditioner context
1142f1580f4eSBarry Smith - flg - `PETSC_TRUE` to not force coarse grid onto one processor
1143171cca9aSMark Adams 
1144171cca9aSMark Adams   Options Database Key:
1145*d529f056Smarkadams4 . -pc_gamg_parallel_coarse_grid_solver - use a parallel coarse grid direct solver
1146171cca9aSMark Adams 
1147171cca9aSMark Adams   Level: intermediate
1148171cca9aSMark Adams 
1149f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCGAMGSetCoarseGridLayoutType()`, `PCGAMGSetCpuPinCoarseGrids()`
1150171cca9aSMark Adams @*/
1151*d529f056Smarkadams4 PetscErrorCode PCGAMGSetParallelCoarseGridSolve(PC pc, PetscBool flg)
1152d71ae5a4SJacob Faibussowitsch {
1153171cca9aSMark Adams   PetscFunctionBegin;
1154171cca9aSMark Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1155*d529f056Smarkadams4   PetscTryMethod(pc, "PCGAMGSetParallelCoarseGridSolve_C", (PC, PetscBool), (pc, flg));
11563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1157171cca9aSMark Adams }
1158171cca9aSMark Adams 
1159*d529f056Smarkadams4 static PetscErrorCode PCGAMGSetParallelCoarseGridSolve_GAMG(PC pc, PetscBool flg)
1160d71ae5a4SJacob Faibussowitsch {
1161171cca9aSMark Adams   PC_MG   *mg      = (PC_MG *)pc->data;
1162171cca9aSMark Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1163171cca9aSMark Adams 
1164171cca9aSMark Adams   PetscFunctionBegin;
1165171cca9aSMark Adams   pc_gamg->use_parallel_coarse_grid_solver = flg;
11663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1167ffc955d6SMark F. Adams }
1168ffc955d6SMark F. Adams 
11694ef23d27SMark F. Adams /*@
1170f1580f4eSBarry 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
1171ce7c7f2fSMark Adams 
1172c3339decSBarry Smith   Collective
1173ce7c7f2fSMark Adams 
1174ce7c7f2fSMark Adams   Input Parameters:
1175ce7c7f2fSMark Adams + pc  - the preconditioner context
1176f1580f4eSBarry Smith - flg - `PETSC_TRUE` to pin coarse grids to the CPU
1177ce7c7f2fSMark Adams 
1178ce7c7f2fSMark Adams   Options Database Key:
1179147403d9SBarry Smith . -pc_gamg_cpu_pin_coarse_grids - pin the coarse grids to the CPU
1180ce7c7f2fSMark Adams 
1181ce7c7f2fSMark Adams   Level: intermediate
1182ce7c7f2fSMark Adams 
1183*d529f056Smarkadams4 .seealso: `PCGAMG`, `PCGAMGSetCoarseGridLayoutType()`, `PCGAMGSetParallelCoarseGridSolve()`
1184ce7c7f2fSMark Adams @*/
1185d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetCpuPinCoarseGrids(PC pc, PetscBool flg)
1186d71ae5a4SJacob Faibussowitsch {
1187ce7c7f2fSMark Adams   PetscFunctionBegin;
1188ce7c7f2fSMark Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1189cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetCpuPinCoarseGrids_C", (PC, PetscBool), (pc, flg));
11903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1191ce7c7f2fSMark Adams }
1192ce7c7f2fSMark Adams 
1193d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetCpuPinCoarseGrids_GAMG(PC pc, PetscBool flg)
1194d71ae5a4SJacob Faibussowitsch {
1195ce7c7f2fSMark Adams   PC_MG   *mg      = (PC_MG *)pc->data;
1196ce7c7f2fSMark Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1197ce7c7f2fSMark Adams 
1198ce7c7f2fSMark Adams   PetscFunctionBegin;
1199ce7c7f2fSMark Adams   pc_gamg->cpu_pin_coarse_grids = flg;
12003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1201ce7c7f2fSMark Adams }
1202ce7c7f2fSMark Adams 
1203ce7c7f2fSMark Adams /*@
1204147403d9SBarry Smith   PCGAMGSetCoarseGridLayoutType - place coarse grids on processors with natural order (compact type)
1205ce7c7f2fSMark Adams 
1206c3339decSBarry Smith   Collective
1207ce7c7f2fSMark Adams 
1208ce7c7f2fSMark Adams   Input Parameters:
1209ce7c7f2fSMark Adams + pc  - the preconditioner context
1210f1580f4eSBarry Smith - flg - `PCGAMGLayoutType` type, either `PCGAMG_LAYOUT_COMPACT` or `PCGAMG_LAYOUT_SPREAD`
1211ce7c7f2fSMark Adams 
1212ce7c7f2fSMark Adams   Options Database Key:
1213147403d9SBarry Smith . -pc_gamg_coarse_grid_layout_type - place the coarse grids with natural ordering
1214ce7c7f2fSMark Adams 
1215ce7c7f2fSMark Adams   Level: intermediate
1216ce7c7f2fSMark Adams 
1217*d529f056Smarkadams4 .seealso: `PCGAMG`, `PCGAMGSetParallelCoarseGridSolve()`, `PCGAMGSetCpuPinCoarseGrids()`, `PCGAMGLayoutType`, `PCGAMG_LAYOUT_COMPACT`, `PCGAMG_LAYOUT_SPREAD`
1218ce7c7f2fSMark Adams @*/
1219d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetCoarseGridLayoutType(PC pc, PCGAMGLayoutType flg)
1220d71ae5a4SJacob Faibussowitsch {
1221ce7c7f2fSMark Adams   PetscFunctionBegin;
1222ce7c7f2fSMark Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1223cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetCoarseGridLayoutType_C", (PC, PCGAMGLayoutType), (pc, flg));
12243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1225ce7c7f2fSMark Adams }
1226ce7c7f2fSMark Adams 
1227d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetCoarseGridLayoutType_GAMG(PC pc, PCGAMGLayoutType flg)
1228d71ae5a4SJacob Faibussowitsch {
1229ce7c7f2fSMark Adams   PC_MG   *mg      = (PC_MG *)pc->data;
1230ce7c7f2fSMark Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1231ce7c7f2fSMark Adams 
1232ce7c7f2fSMark Adams   PetscFunctionBegin;
1233ce7c7f2fSMark Adams   pc_gamg->layout_type = flg;
12343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1235ce7c7f2fSMark Adams }
1236ce7c7f2fSMark Adams 
1237ce7c7f2fSMark Adams /*@
1238f1580f4eSBarry Smith   PCGAMGSetNlevels -  Sets the maximum number of levels `PCGAMG` will use
12394ef23d27SMark F. Adams 
124020f4b53cSBarry Smith   Not Collective
12414ef23d27SMark F. Adams 
12424ef23d27SMark F. Adams   Input Parameters:
12431cc46a46SBarry Smith + pc - the preconditioner
12441cc46a46SBarry Smith - n  - the maximum number of levels to use
12454ef23d27SMark F. Adams 
12464ef23d27SMark F. Adams   Options Database Key:
1247147403d9SBarry Smith . -pc_mg_levels <n> - set the maximum number of levels to allow
12484ef23d27SMark F. Adams 
12494ef23d27SMark F. Adams   Level: intermediate
12504ef23d27SMark F. Adams 
1251feefa0e1SJacob Faibussowitsch   Developer Notes:
1252f1580f4eSBarry Smith   Should be called `PCGAMGSetMaximumNumberlevels()` and possible be shared with `PCMG`
1253f1580f4eSBarry Smith 
1254f1580f4eSBarry Smith .seealso: `PCGAMG`
12554ef23d27SMark F. Adams @*/
1256d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetNlevels(PC pc, PetscInt n)
1257d71ae5a4SJacob Faibussowitsch {
12584ef23d27SMark F. Adams   PetscFunctionBegin;
12594ef23d27SMark F. Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1260cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetNlevels_C", (PC, PetscInt), (pc, n));
12613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12624ef23d27SMark F. Adams }
12634ef23d27SMark F. Adams 
1264d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetNlevels_GAMG(PC pc, PetscInt n)
1265d71ae5a4SJacob Faibussowitsch {
12664ef23d27SMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
12674ef23d27SMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
12684ef23d27SMark F. Adams 
12694ef23d27SMark F. Adams   PetscFunctionBegin;
12709d5b6da9SMark F. Adams   pc_gamg->Nlevels = n;
12713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12724ef23d27SMark F. Adams }
12734ef23d27SMark F. Adams 
12743542efc5SMark F. Adams /*@
12753542efc5SMark F. Adams   PCGAMGSetThreshold - Relative threshold to use for dropping edges in aggregation graph
12763542efc5SMark F. Adams 
127720f4b53cSBarry Smith   Not Collective
12783542efc5SMark F. Adams 
12793542efc5SMark F. Adams   Input Parameters:
12801cc46a46SBarry Smith + pc - the preconditioner context
1281c9567895SMark . 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
1282055c8bd0SJed Brown - n  - number of threshold values provided in array
12833542efc5SMark F. Adams 
12843542efc5SMark F. Adams   Options Database Key:
1285147403d9SBarry Smith . -pc_gamg_threshold <threshold> - the threshold to drop edges
12863542efc5SMark F. Adams 
128720f4b53cSBarry Smith   Level: intermediate
128820f4b53cSBarry Smith 
128995452b02SPatrick Sanan   Notes:
1290af3c827dSMark 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.
1291f1580f4eSBarry 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.
1292cab9ed1eSBarry Smith 
129320f4b53cSBarry Smith   If `n` is less than the total number of coarsenings (see `PCGAMGSetNlevels()`), then threshold scaling (see `PCGAMGSetThresholdScale()`) is used for each successive coarsening.
1294f1580f4eSBarry Smith   In this case, `PCGAMGSetThresholdScale()` must be called before `PCGAMGSetThreshold()`.
129520f4b53cSBarry Smith   If `n` is greater than the total number of levels, the excess entries in threshold will not be used.
12963542efc5SMark F. Adams 
1297*d529f056Smarkadams4 .seealso: `PCGAMG`, `PCGAMGSetAggressiveLevels()`, `PCGAMGMISkSetAggressive()`, `PCGAMGSetMinDegreeOrderingMISk()`, `PCGAMGSetThresholdScale()`
12983542efc5SMark F. Adams @*/
1299d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetThreshold(PC pc, PetscReal v[], PetscInt n)
1300d71ae5a4SJacob Faibussowitsch {
13013542efc5SMark F. Adams   PetscFunctionBegin;
13023542efc5SMark F. Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
13034f572ea9SToby Isaac   if (n) PetscAssertPointer(v, 2);
1304cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetThreshold_C", (PC, PetscReal[], PetscInt), (pc, v, n));
13053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13063542efc5SMark F. Adams }
13073542efc5SMark F. Adams 
1308d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetThreshold_GAMG(PC pc, PetscReal v[], PetscInt n)
1309d71ae5a4SJacob Faibussowitsch {
1310c20e4228SMark F. Adams   PC_MG   *mg      = (PC_MG *)pc->data;
1311c20e4228SMark F. Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1312c1eae691SMark Adams   PetscInt i;
1313c1eae691SMark Adams   PetscFunctionBegin;
1314055c8bd0SJed Brown   for (i = 0; i < PetscMin(n, PETSC_MG_MAXLEVELS); i++) pc_gamg->threshold[i] = v[i];
1315055c8bd0SJed Brown   for (; i < PETSC_MG_MAXLEVELS; i++) pc_gamg->threshold[i] = pc_gamg->threshold[i - 1] * pc_gamg->threshold_scale;
13163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1317c1eae691SMark Adams }
1318c1eae691SMark Adams 
1319c1eae691SMark Adams /*@
1320f1580f4eSBarry Smith   PCGAMGSetRankReductionFactors - Set a manual schedule for MPI rank reduction on coarse grids
1321c9567895SMark 
1322c3339decSBarry Smith   Collective
1323c9567895SMark 
1324c9567895SMark   Input Parameters:
1325c9567895SMark + pc - the preconditioner context
1326f1580f4eSBarry Smith . v  - array of reduction factors. 0 for first value forces a reduction to one process/device on first level in CUDA
1327c9567895SMark - n  - number of values provided in array
1328c9567895SMark 
1329c9567895SMark   Options Database Key:
1330147403d9SBarry Smith . -pc_gamg_rank_reduction_factors <factors> - provide the schedule
1331c9567895SMark 
1332c9567895SMark   Level: intermediate
1333c9567895SMark 
1334f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCGAMGSetProcEqLim()`, `PCGAMGSetCoarseEqLim()`
1335c9567895SMark @*/
1336d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetRankReductionFactors(PC pc, PetscInt v[], PetscInt n)
1337d71ae5a4SJacob Faibussowitsch {
1338c9567895SMark   PetscFunctionBegin;
1339c9567895SMark   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
13404f572ea9SToby Isaac   if (n) PetscAssertPointer(v, 2);
1341cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetRankReductionFactors_C", (PC, PetscInt[], PetscInt), (pc, v, n));
13423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1343c9567895SMark }
1344c9567895SMark 
1345d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetRankReductionFactors_GAMG(PC pc, PetscInt v[], PetscInt n)
1346d71ae5a4SJacob Faibussowitsch {
1347c9567895SMark   PC_MG   *mg      = (PC_MG *)pc->data;
1348c9567895SMark   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1349c9567895SMark   PetscInt i;
1350c9567895SMark   PetscFunctionBegin;
1351c9567895SMark   for (i = 0; i < PetscMin(n, PETSC_MG_MAXLEVELS); i++) pc_gamg->level_reduction_factors[i] = v[i];
1352c9567895SMark   for (; i < PETSC_MG_MAXLEVELS; i++) pc_gamg->level_reduction_factors[i] = -1; /* 0 stop putting one process/device on first level */
13533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1354c9567895SMark }
1355c9567895SMark 
1356c9567895SMark /*@
1357c1eae691SMark Adams   PCGAMGSetThresholdScale - Relative threshold reduction at each level
1358c1eae691SMark Adams 
135920f4b53cSBarry Smith   Not Collective
1360c1eae691SMark Adams 
1361c1eae691SMark Adams   Input Parameters:
1362c1eae691SMark Adams + pc - the preconditioner context
1363feefa0e1SJacob Faibussowitsch - v  - the threshold value reduction, usually < 1.0
1364c1eae691SMark Adams 
1365c1eae691SMark Adams   Options Database Key:
1366147403d9SBarry Smith . -pc_gamg_threshold_scale <v> - set the relative threshold reduction on each level
1367c1eae691SMark Adams 
136820f4b53cSBarry Smith   Level: advanced
136920f4b53cSBarry Smith 
1370f1580f4eSBarry Smith   Note:
1371f1580f4eSBarry Smith   The initial threshold (for an arbitrary number of levels starting from the finest) can be set with `PCGAMGSetThreshold()`.
1372f1580f4eSBarry Smith   This scaling is used for each subsequent coarsening, but must be called before `PCGAMGSetThreshold()`.
1373055c8bd0SJed Brown 
1374f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCGAMGSetThreshold()`
1375c1eae691SMark Adams @*/
1376d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetThresholdScale(PC pc, PetscReal v)
1377d71ae5a4SJacob Faibussowitsch {
13783542efc5SMark F. Adams   PetscFunctionBegin;
1379c1eae691SMark Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1380cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetThresholdScale_C", (PC, PetscReal), (pc, v));
13813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1382c1eae691SMark Adams }
1383c1eae691SMark Adams 
1384d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetThresholdScale_GAMG(PC pc, PetscReal v)
1385d71ae5a4SJacob Faibussowitsch {
1386c1eae691SMark Adams   PC_MG   *mg      = (PC_MG *)pc->data;
1387c1eae691SMark Adams   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1388c1eae691SMark Adams   PetscFunctionBegin;
1389c1eae691SMark Adams   pc_gamg->threshold_scale = v;
13903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13913542efc5SMark F. Adams }
13923542efc5SMark F. Adams 
1393e20c40e8SBarry Smith /*@C
1394f1580f4eSBarry Smith   PCGAMGSetType - Set the type of algorithm `PCGAMG` should use
1395676e1743SMark F. Adams 
1396c3339decSBarry Smith   Collective
1397676e1743SMark F. Adams 
1398676e1743SMark F. Adams   Input Parameters:
1399c60c7ad4SBarry Smith + pc   - the preconditioner context
1400f1580f4eSBarry Smith - type - `PCGAMGAGG`, `PCGAMGGEO`, or `PCGAMGCLASSICAL`
1401676e1743SMark F. Adams 
1402676e1743SMark F. Adams   Options Database Key:
14035e4ac8c8Smarkadams4 . -pc_gamg_type <agg,geo,classical> - type of algebraic multigrid to apply - only agg supported
1404676e1743SMark F. Adams 
1405676e1743SMark F. Adams   Level: intermediate
1406676e1743SMark F. Adams 
1407db781477SPatrick Sanan .seealso: `PCGAMGGetType()`, `PCGAMG`, `PCGAMGType`
1408676e1743SMark F. Adams @*/
1409d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGSetType(PC pc, PCGAMGType type)
1410d71ae5a4SJacob Faibussowitsch {
1411676e1743SMark F. Adams   PetscFunctionBegin;
1412676e1743SMark F. Adams   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1413cac4c232SBarry Smith   PetscTryMethod(pc, "PCGAMGSetType_C", (PC, PCGAMGType), (pc, type));
14143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1415676e1743SMark F. Adams }
1416676e1743SMark F. Adams 
1417e20c40e8SBarry Smith /*@C
1418f1580f4eSBarry Smith   PCGAMGGetType - Get the type of algorithm `PCGAMG` will use
1419c60c7ad4SBarry Smith 
1420c3339decSBarry Smith   Collective
1421c60c7ad4SBarry Smith 
1422c60c7ad4SBarry Smith   Input Parameter:
1423c60c7ad4SBarry Smith . pc - the preconditioner context
1424c60c7ad4SBarry Smith 
1425c60c7ad4SBarry Smith   Output Parameter:
1426c60c7ad4SBarry Smith . type - the type of algorithm used
1427c60c7ad4SBarry Smith 
1428c60c7ad4SBarry Smith   Level: intermediate
1429c60c7ad4SBarry Smith 
1430f1580f4eSBarry Smith .seealso: `PCGAMG`, `PCGAMGSetType()`, `PCGAMGType`
1431c60c7ad4SBarry Smith @*/
1432d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGGetType(PC pc, PCGAMGType *type)
1433d71ae5a4SJacob Faibussowitsch {
1434c60c7ad4SBarry Smith   PetscFunctionBegin;
1435c60c7ad4SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1436cac4c232SBarry Smith   PetscUseMethod(pc, "PCGAMGGetType_C", (PC, PCGAMGType *), (pc, type));
14373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1438c60c7ad4SBarry Smith }
1439c60c7ad4SBarry Smith 
1440d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGGetType_GAMG(PC pc, PCGAMGType *type)
1441d71ae5a4SJacob Faibussowitsch {
1442c60c7ad4SBarry Smith   PC_MG   *mg      = (PC_MG *)pc->data;
1443c60c7ad4SBarry Smith   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
1444c60c7ad4SBarry Smith 
1445c60c7ad4SBarry Smith   PetscFunctionBegin;
1446c60c7ad4SBarry Smith   *type = pc_gamg->type;
14473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1448c60c7ad4SBarry Smith }
1449c60c7ad4SBarry Smith 
1450d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCGAMGSetType_GAMG(PC pc, PCGAMGType type)
1451d71ae5a4SJacob Faibussowitsch {
14521ab5ffc9SJed Brown   PC_MG   *mg      = (PC_MG *)pc->data;
14531ab5ffc9SJed Brown   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
14545f80ce2aSJacob Faibussowitsch   PetscErrorCode (*r)(PC);
1455676e1743SMark F. Adams 
1456676e1743SMark F. Adams   PetscFunctionBegin;
1457c60c7ad4SBarry Smith   pc_gamg->type = type;
14589566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(GAMGList, type, &r));
14596adde796SStefano Zampini   PetscCheck(r, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown GAMG type %s given", type);
14601ab5ffc9SJed Brown   if (pc_gamg->ops->destroy) {
14619566063dSJacob Faibussowitsch     PetscCall((*pc_gamg->ops->destroy)(pc));
14629566063dSJacob Faibussowitsch     PetscCall(PetscMemzero(pc_gamg->ops, sizeof(struct _PCGAMGOps)));
1463e616c208SToby Isaac     pc_gamg->ops->createlevel = PCGAMGCreateLevel_GAMG;
1464da81f932SPierre Jolivet     /* cleaning up common data in pc_gamg - this should disappear someday */
14653ae0bb68SMark Adams     pc_gamg->data_cell_cols      = 0;
14663ae0bb68SMark Adams     pc_gamg->data_cell_rows      = 0;
14673ae0bb68SMark Adams     pc_gamg->orig_data_cell_cols = 0;
14683ae0bb68SMark Adams     pc_gamg->orig_data_cell_rows = 0;
14699566063dSJacob Faibussowitsch     PetscCall(PetscFree(pc_gamg->data));
14703ae0bb68SMark Adams     pc_gamg->data_sz = 0;
14711ab5ffc9SJed Brown   }
14729566063dSJacob Faibussowitsch   PetscCall(PetscFree(pc_gamg->gamg_type_name));
14739566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(type, &pc_gamg->gamg_type_name));
14749566063dSJacob Faibussowitsch   PetscCall((*r)(pc));
14753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1476676e1743SMark F. Adams }
1477676e1743SMark F. Adams 
1478d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCView_GAMG(PC pc, PetscViewer viewer)
1479d71ae5a4SJacob Faibussowitsch {
14805adeb434SBarry Smith   PC_MG    *mg      = (PC_MG *)pc->data;
14815adeb434SBarry Smith   PC_GAMG  *pc_gamg = (PC_GAMG *)mg->innerctx;
1482e7d4b4cbSMark Adams   PetscReal gc = 0, oc = 0;
148390db8557SMark Adams 
14845adeb434SBarry Smith   PetscFunctionBegin;
14859566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "    GAMG specific options\n"));
14869566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "      Threshold for dropping small values in graph on each level ="));
14879566063dSJacob Faibussowitsch   for (PetscInt i = 0; i < mg->nlevels; i++) PetscCall(PetscViewerASCIIPrintf(viewer, " %g", (double)pc_gamg->threshold[i]));
14889566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
14899566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "      Threshold scaling factor for each level not specified = %g\n", (double)pc_gamg->threshold_scale));
149048a46eb9SPierre Jolivet   if (pc_gamg->use_aggs_in_asm) PetscCall(PetscViewerASCIIPrintf(viewer, "      Using aggregates from coarsening process to define subdomains for PCASM\n"));
149148a46eb9SPierre 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"));
14921baa6e33SBarry Smith   if (pc_gamg->ops->view) PetscCall((*pc_gamg->ops->view)(pc, viewer));
14939566063dSJacob Faibussowitsch   PetscCall(PCMGGetGridComplexity(pc, &gc, &oc));
149463a3b9bcSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "      Complexity:    grid = %g    operator = %g\n", (double)gc, (double)oc));
14953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
14965adeb434SBarry Smith }
14975adeb434SBarry Smith 
149866976f2fSJacob Faibussowitsch static PetscErrorCode PCSetFromOptions_GAMG(PC pc, PetscOptionItems *PetscOptionsObject)
1499d71ae5a4SJacob Faibussowitsch {
1500676e1743SMark F. Adams   PC_MG             *mg      = (PC_MG *)pc->data;
1501676e1743SMark F. Adams   PC_GAMG           *pc_gamg = (PC_GAMG *)mg->innerctx;
15027e6512fdSJed Brown   PetscBool          flag;
15033b4367a7SBarry Smith   MPI_Comm           comm;
150418c3aa7eSMark   char               prefix[256], tname[32];
1505c1eae691SMark Adams   PetscInt           i, n;
150614a9496bSBarry Smith   const char        *pcpre;
15070a545947SLisandro Dalcin   static const char *LayoutTypes[] = {"compact", "spread", "PCGAMGLayoutType", "PC_GAMG_LAYOUT", NULL};
15085b89ad90SMark F. Adams   PetscFunctionBegin;
15099566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
1510d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "GAMG options");
15115e4ac8c8Smarkadams4   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));
15121baa6e33SBarry Smith   if (flag) PetscCall(PCGAMGSetType(pc, tname));
15139566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-pc_gamg_repartition", "Repartion coarse grids", "PCGAMGSetRepartition", pc_gamg->repart, &pc_gamg->repart, NULL));
1514f1580f4eSBarry 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));
15159566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-pc_gamg_reuse_interpolation", "Reuse prolongation operator", "PCGAMGReuseInterpolation", pc_gamg->reuse_prol, &pc_gamg->reuse_prol, NULL));
15169566063dSJacob 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));
1517*d529f056Smarkadams4   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));
15189566063dSJacob 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));
15199371c9d4SSatish 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,
15209371c9d4SSatish Balay                              (PetscEnum)pc_gamg->layout_type, (PetscEnum *)&pc_gamg->layout_type, NULL));
15219566063dSJacob 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));
15229566063dSJacob 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));
15239566063dSJacob 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));
152418c3aa7eSMark   n = PETSC_MG_MAXLEVELS;
15259566063dSJacob Faibussowitsch   PetscCall(PetscOptionsRealArray("-pc_gamg_threshold", "Relative threshold to use for dropping edges in aggregation graph", "PCGAMGSetThreshold", pc_gamg->threshold, &n, &flag));
152618c3aa7eSMark   if (!flag || n < PETSC_MG_MAXLEVELS) {
1527efd3c5ceSMark Adams     if (!flag) n = 1;
1528c1eae691SMark Adams     i = n;
1529d71ae5a4SJacob Faibussowitsch     do {
1530d71ae5a4SJacob Faibussowitsch       pc_gamg->threshold[i] = pc_gamg->threshold[i - 1] * pc_gamg->threshold_scale;
1531d71ae5a4SJacob Faibussowitsch     } while (++i < PETSC_MG_MAXLEVELS);
1532c1eae691SMark Adams   }
1533c9567895SMark   n = PETSC_MG_MAXLEVELS;
15349566063dSJacob 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));
1535c9567895SMark   if (!flag) i = 0;
1536c9567895SMark   else i = n;
1537d71ae5a4SJacob Faibussowitsch   do {
1538d71ae5a4SJacob Faibussowitsch     pc_gamg->level_reduction_factors[i] = -1;
1539d71ae5a4SJacob Faibussowitsch   } while (++i < PETSC_MG_MAXLEVELS);
15409566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-pc_mg_levels", "Set number of MG levels", "PCGAMGSetNlevels", pc_gamg->Nlevels, &pc_gamg->Nlevels, NULL));
154118c3aa7eSMark   {
154218c3aa7eSMark     PetscReal eminmax[2] = {0., 0.};
154318c3aa7eSMark     n                    = 2;
15449566063dSJacob Faibussowitsch     PetscCall(PetscOptionsRealArray("-pc_gamg_eigenvalues", "extreme eigenvalues for smoothed aggregation", "PCGAMGSetEigenvalues", eminmax, &n, &flag));
154518c3aa7eSMark     if (flag) {
154608401ef6SPierre Jolivet       PetscCheck(n == 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "-pc_gamg_eigenvalues: must specify 2 parameters, min and max eigenvalues");
15479566063dSJacob Faibussowitsch       PetscCall(PCGAMGSetEigenvalues(pc, eminmax[1], eminmax[0]));
154818c3aa7eSMark     }
154918c3aa7eSMark   }
1550b7cbab4eSMark Adams   /* set options for subtype */
1551dbbe0bcdSBarry Smith   PetscCall((*pc_gamg->ops->setfromoptions)(pc, PetscOptionsObject));
155218c3aa7eSMark 
15539566063dSJacob Faibussowitsch   PetscCall(PCGetOptionsPrefix(pc, &pcpre));
15549566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%spc_gamg_", pcpre ? pcpre : ""));
1555d0609cedSBarry Smith   PetscOptionsHeadEnd();
15563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
15575b89ad90SMark F. Adams }
15585b89ad90SMark F. Adams 
15595b89ad90SMark F. Adams /*MC
15601cc46a46SBarry Smith   PCGAMG - Geometric algebraic multigrid (AMG) preconditioner
15615b89ad90SMark F. Adams 
1562280d9858SJed Brown   Options Database Keys:
15635e4ac8c8Smarkadams4 + -pc_gamg_type <type,default=agg> - one of agg, geo, or classical (only smoothed aggregation, agg, supported)
1564da81f932SPierre Jolivet . -pc_gamg_repartition  <bool,default=false> - repartition the degrees of freedom across the coarse grids as they are determined
156521d928e4Smarkadams4 . -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
1566da81f932SPierre 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>
1567cab9ed1eSBarry Smith                                         equations on each process that has degrees of freedom
15682d776b49SBarry Smith . -pc_gamg_coarse_eq_limit <limit, default=50> - Set maximum number of equations on coarsest grid to aim for.
156921d928e4Smarkadams4 . -pc_gamg_reuse_interpolation <bool,default=true> - when rebuilding the algebraic multigrid preconditioner reuse the previously computed interpolations (should always be true)
157021d928e4Smarkadams4 . -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)
15712d776b49SBarry Smith - -pc_gamg_threshold_scale <scale,default=1> - Scaling of threshold on each coarser grid if not specified
1572cab9ed1eSBarry Smith 
1573f1580f4eSBarry Smith   Options Database Keys for Aggregation:
1574cab9ed1eSBarry Smith + -pc_gamg_agg_nsmooths <nsmooth, default=1> - number of smoothing steps to use with smooth aggregation
1575*d529f056Smarkadams4 . -pc_gamg_aggressive_coarsening <n,default=1> - number of aggressive coarsening (MIS-2) levels from finest.
1576*d529f056Smarkadams4 . -pc_gamg_aggressive_square_graph <bool,default=false> - Use square graph (A'A) or MIS-k (k=2) for aggressive coarsening
1577*d529f056Smarkadams4 . -pc_gamg_mis_k_minimum_degree_ordering <bool,default=true> - Use minimum degree ordering in greedy MIS algorithm
1578*d529f056Smarkadams4 - -pc_gamg_aggressive_mis_k <n,default=2> - Number (k) distance in MIS coarsening (>2 is 'aggressive')
1579cab9ed1eSBarry Smith 
1580f1580f4eSBarry Smith   Options Database Keys for Multigrid:
1581a9f5add0SYANG Zongze + -pc_mg_cycle_type <v> - v or w, see `PCMGSetCycleType()`
1582db9745e2SBarry Smith . -pc_mg_distinct_smoothup - configure the up and down (pre and post) smoothers separately, see PCMGSetDistinctSmoothUp()
1583db9745e2SBarry Smith . -pc_mg_type <multiplicative> - (one of) additive multiplicative full kascade
158421d928e4Smarkadams4 - -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
15855b89ad90SMark F. Adams 
158620f4b53cSBarry Smith   Level: intermediate
158720f4b53cSBarry Smith 
158895452b02SPatrick Sanan   Notes:
1589f1580f4eSBarry Smith   To obtain good performance for `PCGAMG` for vector valued problems you must
1590f1580f4eSBarry Smith   call `MatSetBlockSize()` to indicate the number of degrees of freedom per grid point
1591f1580f4eSBarry Smith   call `MatSetNearNullSpace()` (or `PCSetCoordinates()` if solving the equations of elasticity) to indicate the near null space of the operator
1592f1580f4eSBarry Smith 
159304c3f3b8SBarry Smith   The many options for `PCMG` also work directly for `PCGAMG` such as controlling the smoothers on each level etc.
159404c3f3b8SBarry Smith 
159504c3f3b8SBarry Smith   See [the Users Manual section on PCGAMG](sec_amg) and [the Users Manual section on PCMG](sec_mg)for more details.
15961cc46a46SBarry Smith 
1597db781477SPatrick Sanan .seealso: `PCCreate()`, `PCSetType()`, `MatSetBlockSize()`, `PCMGType`, `PCSetCoordinates()`, `MatSetNearNullSpace()`, `PCGAMGSetType()`, `PCGAMGAGG`, `PCGAMGGEO`, `PCGAMGCLASSICAL`, `PCGAMGSetProcEqLim()`,
1598*d529f056Smarkadams4           `PCGAMGSetCoarseEqLim()`, `PCGAMGSetRepartition()`, `PCGAMGRegister()`, `PCGAMGSetReuseInterpolation()`, `PCGAMGASMSetUseAggs()`, `PCGAMGSetParallelCoarseGridSolve()`, `PCGAMGSetNlevels()`, `PCGAMGSetThreshold()`, `PCGAMGGetType()`, `PCGAMGSetUseSAEstEig()`
15995b89ad90SMark F. Adams M*/
1600d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PCCreate_GAMG(PC pc)
1601d71ae5a4SJacob Faibussowitsch {
16025b89ad90SMark F. Adams   PC_GAMG *pc_gamg;
16035b89ad90SMark F. Adams   PC_MG   *mg;
16045b89ad90SMark F. Adams 
16055b89ad90SMark F. Adams   PetscFunctionBegin;
16061c1aac46SBarry Smith   /* register AMG type */
16079566063dSJacob Faibussowitsch   PetscCall(PCGAMGInitializePackage());
16081c1aac46SBarry Smith 
16095b89ad90SMark F. Adams   /* PCGAMG is an inherited class of PCMG. Initialize pc as PCMG */
16109566063dSJacob Faibussowitsch   PetscCall(PCSetType(pc, PCMG));
16119566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)pc, PCGAMG));
16125b89ad90SMark F. Adams 
16135b89ad90SMark F. Adams   /* create a supporting struct and attach it to pc */
16144dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&pc_gamg));
16159566063dSJacob Faibussowitsch   PetscCall(PCMGSetGalerkin(pc, PC_MG_GALERKIN_EXTERNAL));
16165b89ad90SMark F. Adams   mg           = (PC_MG *)pc->data;
16175b89ad90SMark F. Adams   mg->innerctx = pc_gamg;
16185b89ad90SMark F. Adams 
16194dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&pc_gamg->ops));
16201ab5ffc9SJed Brown 
16219d5b6da9SMark F. Adams   /* these should be in subctx but repartitioning needs simple arrays */
16229d5b6da9SMark F. Adams   pc_gamg->data_sz = 0;
16230a545947SLisandro Dalcin   pc_gamg->data    = NULL;
16245b89ad90SMark F. Adams 
16259d5b6da9SMark F. Adams   /* overwrite the pointers of PCMG by the functions of base class PCGAMG */
16265b89ad90SMark F. Adams   pc->ops->setfromoptions = PCSetFromOptions_GAMG;
16275b89ad90SMark F. Adams   pc->ops->setup          = PCSetUp_GAMG;
16285b89ad90SMark F. Adams   pc->ops->reset          = PCReset_GAMG;
16295b89ad90SMark F. Adams   pc->ops->destroy        = PCDestroy_GAMG;
16305adeb434SBarry Smith   mg->view                = PCView_GAMG;
16315b89ad90SMark F. Adams 
16329566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetProcEqLim_C", PCGAMGSetProcEqLim_GAMG));
16339566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetCoarseEqLim_C", PCGAMGSetCoarseEqLim_GAMG));
16349566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetRepartition_C", PCGAMGSetRepartition_GAMG));
16359566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetEigenvalues_C", PCGAMGSetEigenvalues_GAMG));
16369566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetUseSAEstEig_C", PCGAMGSetUseSAEstEig_GAMG));
16379566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetReuseInterpolation_C", PCGAMGSetReuseInterpolation_GAMG));
16389566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGASMSetUseAggs_C", PCGAMGASMSetUseAggs_GAMG));
1639*d529f056Smarkadams4   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetParallelCoarseGridSolve_C", PCGAMGSetParallelCoarseGridSolve_GAMG));
16409566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetCpuPinCoarseGrids_C", PCGAMGSetCpuPinCoarseGrids_GAMG));
16419566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetCoarseGridLayoutType_C", PCGAMGSetCoarseGridLayoutType_GAMG));
16429566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetThreshold_C", PCGAMGSetThreshold_GAMG));
16439566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetRankReductionFactors_C", PCGAMGSetRankReductionFactors_GAMG));
16449566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetThresholdScale_C", PCGAMGSetThresholdScale_GAMG));
16459566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetType_C", PCGAMGSetType_GAMG));
16469566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGGetType_C", PCGAMGGetType_GAMG));
16479566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGAMGSetNlevels_C", PCGAMGSetNlevels_GAMG));
16489d5b6da9SMark F. Adams   pc_gamg->repart                          = PETSC_FALSE;
164921d928e4Smarkadams4   pc_gamg->reuse_prol                      = PETSC_TRUE;
16500c3bc534SBarry Smith   pc_gamg->use_aggs_in_asm                 = PETSC_FALSE;
1651171cca9aSMark Adams   pc_gamg->use_parallel_coarse_grid_solver = PETSC_FALSE;
1652a0095786SMark   pc_gamg->cpu_pin_coarse_grids            = PETSC_FALSE;
1653a0095786SMark   pc_gamg->layout_type                     = PCGAMG_LAYOUT_SPREAD;
1654038f3aa4SMark F. Adams   pc_gamg->min_eq_proc                     = 50;
165525a145a7SMark Adams   pc_gamg->coarse_eq_limit                 = 50;
165653134ebeSMark Adams   for (int i = 0; i < PETSC_MG_MAXLEVELS; i++) pc_gamg->threshold[i] = -1;
1657c1eae691SMark Adams   pc_gamg->threshold_scale = 1.;
165818c3aa7eSMark   pc_gamg->Nlevels         = PETSC_MG_MAXLEVELS;
16599ab59c8bSMark Adams   pc_gamg->current_level   = 0; /* don't need to init really */
16607e6512fdSJed Brown   pc_gamg->use_sa_esteig   = PETSC_TRUE;
166118c3aa7eSMark   pc_gamg->emin            = 0;
166218c3aa7eSMark   pc_gamg->emax            = 0;
166318c3aa7eSMark 
1664c238b0ebSToby Isaac   pc_gamg->ops->createlevel = PCGAMGCreateLevel_GAMG;
16659d5b6da9SMark F. Adams 
1666bd94a7aaSJed Brown   /* PCSetUp_GAMG assumes that the type has been set, so set it to the default now */
16679566063dSJacob Faibussowitsch   PetscCall(PCGAMGSetType(pc, PCGAMGAGG));
16683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
16695b89ad90SMark F. Adams }
16703e3471ccSMark Adams 
16713e3471ccSMark Adams /*@C
1672f1580f4eSBarry Smith   PCGAMGInitializePackage - This function initializes everything in the `PCGAMG` package. It is called
1673f1580f4eSBarry Smith   from `PCInitializePackage()`.
16743e3471ccSMark Adams 
16753e3471ccSMark Adams   Level: developer
16763e3471ccSMark Adams 
1677db781477SPatrick Sanan .seealso: `PetscInitialize()`
16783e3471ccSMark Adams @*/
1679d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGInitializePackage(void)
1680d71ae5a4SJacob Faibussowitsch {
16814555aa8cSStefano Zampini   PetscInt l;
16823e3471ccSMark Adams 
16833e3471ccSMark Adams   PetscFunctionBegin;
16843ba16761SJacob Faibussowitsch   if (PCGAMGPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
16853e3471ccSMark Adams   PCGAMGPackageInitialized = PETSC_TRUE;
16869566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&GAMGList, PCGAMGGEO, PCCreateGAMG_GEO));
16879566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&GAMGList, PCGAMGAGG, PCCreateGAMG_AGG));
16889566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&GAMGList, PCGAMGCLASSICAL, PCCreateGAMG_Classical));
16899566063dSJacob Faibussowitsch   PetscCall(PetscRegisterFinalize(PCGAMGFinalizePackage));
1690c1c463dbSMark Adams 
1691c1c463dbSMark Adams   /* general events */
1692849bee69SMark Adams   PetscCall(PetscLogEventRegister("PCSetUp_GAMG+", PC_CLASSID, &petsc_gamg_setup_events[GAMG_SETUP]));
1693849bee69SMark Adams   PetscCall(PetscLogEventRegister(" PCGAMGCreateG", PC_CLASSID, &petsc_gamg_setup_events[GAMG_GRAPH]));
1694849bee69SMark Adams   PetscCall(PetscLogEventRegister(" GAMG Coarsen", PC_CLASSID, &petsc_gamg_setup_events[GAMG_COARSEN]));
1695849bee69SMark Adams   PetscCall(PetscLogEventRegister("  GAMG MIS/Agg", PC_CLASSID, &petsc_gamg_setup_events[GAMG_MIS]));
1696849bee69SMark Adams   PetscCall(PetscLogEventRegister(" PCGAMGProl", PC_CLASSID, &petsc_gamg_setup_events[GAMG_PROL]));
1697849bee69SMark Adams   PetscCall(PetscLogEventRegister("  GAMG Prol-col", PC_CLASSID, &petsc_gamg_setup_events[GAMG_PROLA]));
1698849bee69SMark Adams   PetscCall(PetscLogEventRegister("  GAMG Prol-lift", PC_CLASSID, &petsc_gamg_setup_events[GAMG_PROLB]));
1699849bee69SMark Adams   PetscCall(PetscLogEventRegister(" PCGAMGOptProl", PC_CLASSID, &petsc_gamg_setup_events[GAMG_OPT]));
1700849bee69SMark Adams   PetscCall(PetscLogEventRegister("  GAMG smooth", PC_CLASSID, &petsc_gamg_setup_events[GAMG_OPTSM]));
1701849bee69SMark Adams   PetscCall(PetscLogEventRegister(" PCGAMGCreateL", PC_CLASSID, &petsc_gamg_setup_events[GAMG_LEVEL]));
1702849bee69SMark Adams   PetscCall(PetscLogEventRegister("  GAMG PtAP", PC_CLASSID, &petsc_gamg_setup_events[GAMG_PTAP]));
1703849bee69SMark Adams   PetscCall(PetscLogEventRegister("  GAMG Reduce", PC_CLASSID, &petsc_gamg_setup_events[GAMG_REDUCE]));
1704849bee69SMark Adams   PetscCall(PetscLogEventRegister("   GAMG Repart", PC_CLASSID, &petsc_gamg_setup_events[GAMG_REPART]));
1705849bee69SMark Adams   /* PetscCall(PetscLogEventRegister("   GAMG Inv-Srt", PC_CLASSID, &petsc_gamg_setup_events[SET13])); */
1706849bee69SMark Adams   /* PetscCall(PetscLogEventRegister("   GAMG Move A", PC_CLASSID, &petsc_gamg_setup_events[SET14])); */
1707849bee69SMark Adams   /* PetscCall(PetscLogEventRegister("   GAMG Move P", PC_CLASSID, &petsc_gamg_setup_events[SET15])); */
17084555aa8cSStefano Zampini   for (l = 0; l < PETSC_MG_MAXLEVELS; l++) {
17094555aa8cSStefano Zampini     char ename[32];
17105b89ad90SMark F. Adams 
171163a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(ename, sizeof(ename), "PCGAMG Squ l%02" PetscInt_FMT, l));
17129566063dSJacob Faibussowitsch     PetscCall(PetscLogEventRegister(ename, PC_CLASSID, &petsc_gamg_setup_matmat_events[l][0]));
171363a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(ename, sizeof(ename), "PCGAMG Gal l%02" PetscInt_FMT, l));
17149566063dSJacob Faibussowitsch     PetscCall(PetscLogEventRegister(ename, PC_CLASSID, &petsc_gamg_setup_matmat_events[l][1]));
171563a3b9bcSJacob Faibussowitsch     PetscCall(PetscSNPrintf(ename, sizeof(ename), "PCGAMG Opt l%02" PetscInt_FMT, l));
17169566063dSJacob Faibussowitsch     PetscCall(PetscLogEventRegister(ename, PC_CLASSID, &petsc_gamg_setup_matmat_events[l][2]));
17174555aa8cSStefano Zampini   }
17184555aa8cSStefano Zampini #if defined(GAMG_STAGES)
1719849bee69SMark Adams   { /* create timer stages */
17205b89ad90SMark F. Adams     char str[32];
1721a364092eSJacob Faibussowitsch     PetscCall(PetscSNPrintf(str, PETSC_STATIC_ARRAY_LENGTH(str), "GAMG Level %d", 0));
17229566063dSJacob Faibussowitsch     PetscCall(PetscLogStageRegister(str, &gamg_stages[0]));
17235b89ad90SMark F. Adams   }
17245b89ad90SMark F. Adams #endif
17253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17263e3471ccSMark Adams }
17273e3471ccSMark Adams 
17283e3471ccSMark Adams /*@C
1729f1580f4eSBarry Smith   PCGAMGFinalizePackage - This function frees everything from the `PCGAMG` package. It is
1730f1580f4eSBarry Smith   called from `PetscFinalize()` automatically.
17313e3471ccSMark Adams 
17323e3471ccSMark Adams   Level: developer
17333e3471ccSMark Adams 
1734db781477SPatrick Sanan .seealso: `PetscFinalize()`
17353e3471ccSMark Adams @*/
1736d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGFinalizePackage(void)
1737d71ae5a4SJacob Faibussowitsch {
17383e3471ccSMark Adams   PetscFunctionBegin;
17393e3471ccSMark Adams   PCGAMGPackageInitialized = PETSC_FALSE;
17409566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListDestroy(&GAMGList));
17413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17423e3471ccSMark Adams }
1743a36cf38bSToby Isaac 
1744a36cf38bSToby Isaac /*@C
1745f1580f4eSBarry Smith   PCGAMGRegister - Register a `PCGAMG` implementation.
1746a36cf38bSToby Isaac 
1747a36cf38bSToby Isaac   Input Parameters:
1748f1580f4eSBarry Smith + type   - string that will be used as the name of the `PCGAMG` type.
1749a36cf38bSToby Isaac - create - function for creating the gamg context.
1750a36cf38bSToby Isaac 
1751f1580f4eSBarry Smith   Level: developer
1752a36cf38bSToby Isaac 
1753db781477SPatrick Sanan .seealso: `PCGAMGType`, `PCGAMG`, `PCGAMGSetType()`
1754a36cf38bSToby Isaac @*/
1755d71ae5a4SJacob Faibussowitsch PetscErrorCode PCGAMGRegister(PCGAMGType type, PetscErrorCode (*create)(PC))
1756d71ae5a4SJacob Faibussowitsch {
1757a36cf38bSToby Isaac   PetscFunctionBegin;
17589566063dSJacob Faibussowitsch   PetscCall(PCGAMGInitializePackage());
17599566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&GAMGList, type, create));
17603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1761a36cf38bSToby Isaac }
17622d776b49SBarry Smith 
17632d776b49SBarry Smith /*@C
17642d776b49SBarry Smith   PCGAMGCreateGraph - Creates a graph that is used by the ``PCGAMGType`` in the coarsening process
17652d776b49SBarry Smith 
17662d776b49SBarry Smith   Input Parameters:
17672d776b49SBarry Smith + pc - the `PCGAMG`
17682d776b49SBarry Smith - A  - the matrix, for any level
17692d776b49SBarry Smith 
17702d776b49SBarry Smith   Output Parameter:
17712d776b49SBarry Smith . G - the graph
17722d776b49SBarry Smith 
17732d776b49SBarry Smith   Level: advanced
17742d776b49SBarry Smith 
17752d776b49SBarry Smith .seealso: `PCGAMGType`, `PCGAMG`, `PCGAMGSetType()`
17762d776b49SBarry Smith @*/
17772d776b49SBarry Smith PetscErrorCode PCGAMGCreateGraph(PC pc, Mat A, Mat *G)
17782d776b49SBarry Smith {
17792d776b49SBarry Smith   PC_MG   *mg      = (PC_MG *)pc->data;
17802d776b49SBarry Smith   PC_GAMG *pc_gamg = (PC_GAMG *)mg->innerctx;
17812d776b49SBarry Smith 
17822d776b49SBarry Smith   PetscFunctionBegin;
17832d776b49SBarry Smith   PetscCall(pc_gamg->ops->creategraph(pc, A, G));
17843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17852d776b49SBarry Smith }
1786