xref: /petsc/include/petsc/private/pcgamgimpl.h (revision 7fa8f2d3c229ab24d2648dd75b628cc252fb1e95)
1 #if !defined(__GAMG_IMPL)
2 #define __GAMG_IMPL
3 #include <petsc/private/pcimpl.h>
4 #include <petsc/private/pcmgimpl.h>                    /*I "petscksp.h" I*/
5 #include <petscmatcoarsen.h>                           /*I "petscmatcoarsen.h" I*/
6 
7 struct _PCGAMGOps {
8   PetscErrorCode (*graph)(PC, Mat, Mat*);
9   PetscErrorCode (*coarsen)(PC, Mat*, PetscCoarsenData**);
10   PetscErrorCode (*prolongator)(PC, Mat, Mat, PetscCoarsenData*, Mat*);
11   PetscErrorCode (*optprolongator)(PC, Mat, Mat*);
12   PetscErrorCode (*createlevel)(PC, Mat, PetscInt, Mat *, Mat *, PetscMPIInt *, IS *, PetscBool);
13   PetscErrorCode (*createdefaultdata)(PC, Mat); /* for data methods that have a default (SA) */
14   PetscErrorCode (*setfromoptions)(PetscOptionItems*,PC);
15   PetscErrorCode (*destroy)(PC);
16   PetscErrorCode (*view)(PC,PetscViewer);
17 };
18 
19 /* Private context for the GAMG preconditioner */
20 typedef struct gamg_TAG {
21   PCGAMGType type;
22   PetscInt  Nlevels;
23   PetscInt  setup_count;
24   PetscBool repart;
25   PetscBool reuse_prol;
26   PetscBool use_aggs_in_asm;
27   PetscBool use_parallel_coarse_grid_solver;
28   PetscInt  min_eq_proc;
29   PetscInt  coarse_eq_limit;
30   PetscReal threshold;      /* common quatity to many AMG methods so keep it up here */
31   PetscInt  current_level; /* stash construction state */
32 
33   /* these 4 are all related to the method data and should be in the subctx */
34   PetscInt  data_sz;      /* nloc*data_rows*data_cols */
35   PetscInt  data_cell_rows;
36   PetscInt  data_cell_cols;
37   PetscInt  orig_data_cell_rows;
38   PetscInt  orig_data_cell_cols;
39   PetscReal *data;          /* [data_sz] blocked vector of vertex data on fine grid (coordinates/nullspace) */
40   PetscReal *orig_data;          /* cache data */
41 
42   struct _PCGAMGOps *ops;
43   char *gamg_type_name;
44 
45   void *subctx;
46 } PC_GAMG;
47 
48 PetscErrorCode PCReset_MG(PC);
49 
50 /* hooks create derivied classes */
51 PetscErrorCode  PCCreateGAMG_GEO(PC);
52 PetscErrorCode  PCCreateGAMG_AGG(PC);
53 PetscErrorCode  PCCreateGAMG_Classical(PC);
54 
55 PetscErrorCode PCDestroy_GAMG(PC);
56 
57 /* helper methods */
58 PetscErrorCode PCGAMGCreateGraph(Mat, Mat*);
59 PetscErrorCode PCGAMGFilterGraph(Mat*, PetscReal, PetscBool);
60 PetscErrorCode PCGAMGGetDataWithGhosts(Mat, PetscInt, PetscReal[],PetscInt*, PetscReal **);
61 
62 #if defined PETSC_USE_LOG
63 #define PETSC_GAMG_USE_LOG
64 enum tag {SET1,SET2,GRAPH,GRAPH_MAT,GRAPH_FILTER,GRAPH_SQR,SET4,SET5,SET6,FIND_V,SET7,SET8,SET9,SET10,SET11,SET12,SET13,SET14,SET15,SET16,NUM_SET};
65 #if defined PETSC_GAMG_USE_LOG
66 PETSC_EXTERN PetscLogEvent petsc_gamg_setup_events[NUM_SET];
67 #endif
68 PETSC_EXTERN PetscLogEvent PC_GAMGGraph_AGG;
69 PETSC_EXTERN PetscLogEvent PC_GAMGGraph_GEO;
70 PETSC_EXTERN PetscLogEvent PC_GAMGCoarsen_AGG;
71 PETSC_EXTERN PetscLogEvent PC_GAMGCoarsen_GEO;
72 PETSC_EXTERN PetscLogEvent PC_GAMGProlongator_AGG;
73 PETSC_EXTERN PetscLogEvent PC_GAMGProlongator_GEO;
74 PETSC_EXTERN PetscLogEvent PC_GAMGOptProlongator_AGG;
75 #endif
76 
77 typedef struct _PCGAMGHashTable {
78   PetscInt *table;
79   PetscInt *data;
80   PetscInt size;
81 } PCGAMGHashTable;
82 
83 
84 PETSC_INTERN PetscErrorCode PCGAMGHashTableCreate(PetscInt, PCGAMGHashTable*);
85 PETSC_INTERN PetscErrorCode PCGAMGHashTableDestroy(PCGAMGHashTable*);
86 PETSC_INTERN PetscErrorCode PCGAMGHashTableAdd(PCGAMGHashTable*,PetscInt,PetscInt);
87 
88 #define GAMG_HASH(key) ((((PetscInt)7)*key)%a_tab->size)
89 #undef __FUNCT__
90 #define __FUNCT__ "PCGAMGHashTableFind"
91 PETSC_STATIC_INLINE PetscErrorCode PCGAMGHashTableFind(PCGAMGHashTable *a_tab, PetscInt a_key, PetscInt *a_data)
92 {
93   PetscInt kk,idx;
94 
95   PetscFunctionBegin;
96   if (a_key<0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Negative key %d.",a_key);
97   for (kk = 0, idx = GAMG_HASH(a_key); kk < a_tab->size; kk++, idx = (idx==(a_tab->size-1)) ? 0 : idx + 1) {
98     if (a_tab->table[idx] == a_key) {
99       *a_data = a_tab->data[idx];
100       break;
101     } else if (a_tab->table[idx] == -1) {
102       /* not here */
103       *a_data = -1;
104       break;
105     }
106   }
107   if (kk==a_tab->size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"key %d not found in table",a_key);
108   PetscFunctionReturn(0);
109 }
110 
111 #endif
112 
113