1 static char help[] = "Benchmark Poisson Problem in 2d and 3d with finite elements.\n\ 2 We solve the Poisson problem in a rectangular domain\n\ 3 using a parallel unstructured mesh (DMPLEX) to discretize it.\n\n\n"; 4 5 #include <petscdmplex.h> 6 #include <petscsnes.h> 7 #include <petscds.h> 8 #include <petscconvest.h> 9 10 typedef struct { 11 PetscInt nit; /* Number of benchmark iterations */ 12 PetscBool strong; /* Do not integrate the Laplacian by parts */ 13 } AppCtx; 14 15 static PetscErrorCode trig_u(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx) 16 { 17 PetscInt d; 18 *u = 0.0; 19 for (d = 0; d < dim; ++d) *u += PetscSinReal(2.0*PETSC_PI*x[d]); 20 return 0; 21 } 22 23 static void f0_trig_u(PetscInt dim, PetscInt Nf, PetscInt NfAux, 24 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 25 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 26 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[]) 27 { 28 PetscInt d; 29 for (d = 0; d < dim; ++d) f0[0] += -4.0*PetscSqr(PETSC_PI)*PetscSinReal(2.0*PETSC_PI*x[d]); 30 } 31 32 static void f1_u(PetscInt dim, PetscInt Nf, PetscInt NfAux, 33 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 34 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 35 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[]) 36 { 37 PetscInt d; 38 for (d = 0; d < dim; ++d) f1[d] = u_x[d]; 39 } 40 41 static void g3_uu(PetscInt dim, PetscInt Nf, PetscInt NfAux, 42 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 43 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 44 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[]) 45 { 46 PetscInt d; 47 for (d = 0; d < dim; ++d) g3[d*dim+d] = 1.0; 48 } 49 50 static PetscErrorCode quadratic_u(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx) 51 { 52 *u = PetscSqr(x[0]) + PetscSqr(x[1]); 53 return 0; 54 } 55 56 static void f0_strong_u(PetscInt dim, PetscInt Nf, PetscInt NfAux, 57 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 58 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 59 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[]) 60 { 61 PetscInt d; 62 for (d = 0; d < dim; ++d) f0[0] -= u_x[dim + d*dim+d]; 63 f0[0] += 4.0; 64 } 65 66 static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options) 67 { 68 PetscErrorCode ierr; 69 70 PetscFunctionBeginUser; 71 options->nit = 10; 72 options->strong = PETSC_FALSE; 73 ierr = PetscOptionsBegin(comm, "", "Poisson Problem Options", "DMPLEX");CHKERRQ(ierr); 74 ierr = PetscOptionsInt("-benchmark_it", "Solve the benchmark problem this many times", "ex13.c", options->nit, &options->nit, NULL);CHKERRQ(ierr); 75 ierr = PetscOptionsBool("-strong", "Do not integrate the Laplacian by parts", "ex13.c", options->strong, &options->strong, NULL);CHKERRQ(ierr); 76 ierr = PetscOptionsEnd();CHKERRQ(ierr); 77 PetscFunctionReturn(0); 78 } 79 80 static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm) 81 { 82 PetscErrorCode ierr; 83 84 PetscFunctionBeginUser; 85 ierr = DMCreate(comm, dm);CHKERRQ(ierr); 86 ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr); 87 ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); 88 ierr = DMSetApplicationContext(*dm, user);CHKERRQ(ierr); 89 ierr = DMViewFromOptions(*dm, NULL, "-dm_view");CHKERRQ(ierr); 90 PetscFunctionReturn(0); 91 } 92 93 static PetscErrorCode SetupPrimalProblem(DM dm, AppCtx *user) 94 { 95 PetscDS ds; 96 DMLabel label; 97 const PetscInt id = 1; 98 PetscErrorCode ierr; 99 100 PetscFunctionBeginUser; 101 ierr = DMGetDS(dm, &ds);CHKERRQ(ierr); 102 ierr = DMGetLabel(dm, "marker", &label);CHKERRQ(ierr); 103 if (user->strong) { 104 ierr = PetscDSSetResidual(ds, 0, f0_strong_u, NULL);CHKERRQ(ierr); 105 ierr = PetscDSSetExactSolution(ds, 0, quadratic_u, user);CHKERRQ(ierr); 106 ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, 1, &id, 0, 0, NULL, (void (*)(void)) quadratic_u, NULL, user, NULL);CHKERRQ(ierr); 107 } else { 108 ierr = PetscDSSetResidual(ds, 0, f0_trig_u, f1_u);CHKERRQ(ierr); 109 ierr = PetscDSSetJacobian(ds, 0, 0, NULL, NULL, NULL, g3_uu);CHKERRQ(ierr); 110 ierr = PetscDSSetExactSolution(ds, 0, trig_u, user);CHKERRQ(ierr); 111 ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, 1, &id, 0, 0, NULL, (void (*)(void)) trig_u, NULL, user, NULL);CHKERRQ(ierr); 112 } 113 PetscFunctionReturn(0); 114 } 115 116 static PetscErrorCode SetupDiscretization(DM dm, const char name[], PetscErrorCode (*setup)(DM, AppCtx *), AppCtx *user) 117 { 118 DM cdm = dm; 119 PetscFE fe; 120 DMPolytopeType ct; 121 PetscBool simplex; 122 PetscInt dim, cStart; 123 char prefix[PETSC_MAX_PATH_LEN]; 124 PetscErrorCode ierr; 125 126 PetscFunctionBeginUser; 127 ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr); 128 ierr = DMPlexGetHeightStratum(dm, 0, &cStart, NULL);CHKERRQ(ierr); 129 ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr); 130 simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE; 131 /* Create finite element */ 132 ierr = PetscSNPrintf(prefix, PETSC_MAX_PATH_LEN, "%s_", name);CHKERRQ(ierr); 133 ierr = PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, name ? prefix : NULL, -1, &fe);CHKERRQ(ierr); 134 ierr = PetscObjectSetName((PetscObject) fe, name);CHKERRQ(ierr); 135 /* Set discretization and boundary conditions for each mesh */ 136 ierr = DMSetField(dm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr); 137 ierr = DMCreateDS(dm);CHKERRQ(ierr); 138 ierr = (*setup)(dm, user);CHKERRQ(ierr); 139 while (cdm) { 140 ierr = DMCopyDisc(dm,cdm);CHKERRQ(ierr); 141 /* TODO: Check whether the boundary of coarse meshes is marked */ 142 ierr = DMGetCoarseDM(cdm, &cdm);CHKERRQ(ierr); 143 } 144 ierr = PetscFEDestroy(&fe);CHKERRQ(ierr); 145 PetscFunctionReturn(0); 146 } 147 148 int main(int argc, char **argv) 149 { 150 DM dm; /* Problem specification */ 151 SNES snes; /* Nonlinear solver */ 152 Vec u; /* Solutions */ 153 AppCtx user; /* User-defined work context */ 154 PetscErrorCode ierr; 155 156 ierr = PetscInitialize(&argc, &argv, NULL,help);if (ierr) return ierr; 157 ierr = ProcessOptions(PETSC_COMM_WORLD, &user);CHKERRQ(ierr); 158 /* Primal system */ 159 ierr = SNESCreate(PETSC_COMM_WORLD, &snes);CHKERRQ(ierr); 160 ierr = CreateMesh(PETSC_COMM_WORLD, &user, &dm);CHKERRQ(ierr); 161 ierr = SNESSetDM(snes, dm);CHKERRQ(ierr); 162 ierr = SetupDiscretization(dm, "potential", SetupPrimalProblem, &user);CHKERRQ(ierr); 163 ierr = DMCreateGlobalVector(dm, &u);CHKERRQ(ierr); 164 ierr = VecSet(u, 0.0);CHKERRQ(ierr); 165 ierr = PetscObjectSetName((PetscObject) u, "potential");CHKERRQ(ierr); 166 ierr = DMPlexSetSNESLocalFEM(dm, &user, &user, &user);CHKERRQ(ierr); 167 ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); 168 ierr = DMSNESCheckFromOptions(snes, u);CHKERRQ(ierr); 169 ierr = SNESSolve(snes, NULL, u);CHKERRQ(ierr); 170 /* Benchmark system */ 171 if (user.nit) { 172 #if defined(PETSC_USE_LOG) 173 PetscLogStage kspstage,pcstage; 174 #endif 175 KSP ksp; 176 PC pc; 177 Mat A,P; 178 Vec b; 179 PetscInt i; 180 ierr = PetscOptionsClearValue(NULL,"-ksp_monitor");CHKERRQ(ierr); 181 ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr); 182 ierr = SNESGetSolution(snes, &u);CHKERRQ(ierr); 183 ierr = SNESGetJacobian(snes, &A, &P, NULL, NULL);CHKERRQ(ierr); 184 ierr = VecSet(u, 0.0);CHKERRQ(ierr); 185 ierr = SNESGetFunction(snes, &b, NULL, NULL);CHKERRQ(ierr); 186 ierr = SNESComputeFunction(snes, u, b);CHKERRQ(ierr); 187 ierr = SNESComputeJacobian(snes, u, A, P);CHKERRQ(ierr); 188 ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 189 ierr = PetscLogStageRegister("PCSetUp", &pcstage);CHKERRQ(ierr); 190 ierr = PetscLogStagePush(pcstage);CHKERRQ(ierr); 191 ierr = PCSetUp(pc);CHKERRQ(ierr); 192 ierr = PetscLogStagePop();CHKERRQ(ierr); 193 ierr = PetscLogStageRegister("KSP Solve only", &kspstage);CHKERRQ(ierr); 194 ierr = PetscLogStagePush(kspstage);CHKERRQ(ierr); 195 for (i=0;i<user.nit;i++) { 196 ierr = VecZeroEntries(u);CHKERRQ(ierr); 197 ierr = KSPSolve(ksp, b, u);CHKERRQ(ierr); 198 } 199 ierr = PetscLogStagePop();CHKERRQ(ierr); 200 } 201 ierr = SNESGetSolution(snes, &u);CHKERRQ(ierr); 202 ierr = VecViewFromOptions(u, NULL, "-potential_view");CHKERRQ(ierr); 203 /* Cleanup */ 204 ierr = VecDestroy(&u);CHKERRQ(ierr); 205 ierr = SNESDestroy(&snes);CHKERRQ(ierr); 206 ierr = DMDestroy(&dm);CHKERRQ(ierr); 207 ierr = PetscFinalize(); 208 return ierr; 209 } 210 211 /*TEST 212 213 test: 214 suffix: strong 215 requires: triangle 216 args: -dm_plex_dim 2 -dm_refine 1 -benchmark_it 0 -dmsnes_check \ 217 -potential_petscspace_degree 2 -dm_ds_jet_degree 2 -strong 218 219 test: 220 suffix: bench 221 nsize: 4 222 args: -dm_plex_dim 3 -dm_plex_simplex 0 -dm_plex_box_faces 2,2,8 -dm_refine 1 -dm_distribute \ 223 -petscpartitioner_type simple -petscpartitioner_simple_process_grid 1,1,2 -petscpartitioner_simple_node_grid 1,1,2 \ 224 -potential_petscspace_degree 2 -ksp_type cg -pc_type gamg -benchmark_it 1 -dm_view -snes_rtol 1.e-4 225 226 test: 227 suffix: comparison 228 nsize: 4 229 args: -dm_plex_dim 2 -dm_plex_box_faces 4,4 -dm_refine 3 -petscpartitioner_simple_process_grid 2,2 \ 230 -petscpartitioner_simple_node_grid 1,1 -potential_petscspace_degree 2 -dm_distribute -petscpartitioner_type simple \ 231 -dm_plex_simplex 0 -snes_monitor_short -snes_type ksponly -dm_view -pc_type gamg -pc_gamg_process_eq_limit 400 -ksp_norm_type unpreconditioned \ 232 -pc_gamg_coarse_eq_limit 10 -snes_converged_reason -ksp_converged_reason -snes_rtol 1.e-4 233 234 test: 235 suffix: cuda 236 nsize: 4 237 requires: cuda 238 output_file: output/ex13_comparison.out 239 args: -dm_plex_dim 2 -dm_plex_box_faces 4,4 -dm_refine 3 -petscpartitioner_simple_process_grid 2,2 \ 240 -petscpartitioner_simple_node_grid 1,1 -potential_petscspace_degree 2 -dm_distribute -petscpartitioner_type simple \ 241 -dm_plex_simplex 0 -snes_monitor_short -snes_type ksponly -dm_view -pc_type gamg -pc_gamg_process_eq_limit 400 -ksp_norm_type unpreconditioned \ 242 -pc_gamg_coarse_eq_limit 10 -snes_converged_reason -ksp_converged_reason -snes_rtol 1.e-4 -dm_mat_type aijcusparse -dm_vec_type cuda 243 244 test: 245 suffix: kokkos_comp 246 nsize: 4 247 requires: kokkos_kernels 248 output_file: output/ex13_comparison.out 249 args: -dm_plex_dim 2 -dm_plex_box_faces 4,4 -dm_refine 3 -petscpartitioner_simple_process_grid 2,2 \ 250 -petscpartitioner_simple_node_grid 1,1 -potential_petscspace_degree 2 -dm_distribute -petscpartitioner_type simple \ 251 -dm_plex_simplex 0 -snes_monitor_short -snes_type ksponly -dm_view -pc_type gamg -pc_gamg_process_eq_limit 400 -ksp_norm_type unpreconditioned \ 252 -pc_gamg_coarse_eq_limit 10 -snes_converged_reason -ksp_converged_reason -snes_rtol 1.e-4 -dm_mat_type aijkokkos -dm_vec_type kokkos 253 254 test: 255 nsize: 4 256 requires: kokkos_kernels 257 suffix: kokkos 258 args: -dm_plex_dim 2 -dm_plex_box_faces 2,8 -dm_distribute -petscpartitioner_type simple -petscpartitioner_simple_process_grid 2,1 \ 259 -petscpartitioner_simple_node_grid 2,1 -dm_plex_simplex 0 -potential_petscspace_degree 1 -dm_refine 1 -ksp_type cg -pc_type gamg -ksp_norm_type unpreconditioned \ 260 -mg_levels_esteig_ksp_type cg -mg_levels_pc_type jacobi -ksp_converged_reason -snes_monitor_short -snes_rtol 1.e-4 -dm_view -dm_mat_type aijkokkos -dm_vec_type kokkos 261 262 test: 263 suffix: aijmkl_comp 264 nsize: 4 265 requires: mkl_sparse 266 output_file: output/ex13_comparison.out 267 args: -dm_plex_dim 2 -dm_plex_box_faces 4,4 -dm_refine 3 -petscpartitioner_simple_process_grid 2,2 \ 268 -petscpartitioner_simple_node_grid 1,1 -potential_petscspace_degree 2 -dm_distribute -petscpartitioner_type simple \ 269 -dm_plex_simplex 0 -snes_monitor_short -snes_type ksponly -dm_view -pc_type gamg -pc_gamg_process_eq_limit 400 -ksp_norm_type unpreconditioned \ 270 -pc_gamg_coarse_eq_limit 10 -snes_converged_reason -ksp_converged_reason -snes_rtol 1.e-4 -dm_mat_type aijmkl 271 272 test: 273 suffix: aijmkl_seq 274 nsize: 1 275 requires: mkl_sparse 276 TODO: broken (INDEFINITE PC) 277 args: -dm_plex_dim 3 -dm_plex_box_faces 4,4,4 -dm_refine 1 -petscpartitioner_type simple -potential_petscspace_degree 1 -dm_distribute -dm_plex_simplex 0 -snes_monitor_short -snes_type ksponly -dm_view -pc_type gamg -pc_gamg_sym_graph 0 -pc_gamg_threshold -1 -pc_gamg_square_graph 10 -pc_gamg_process_eq_limit 400 -pc_gamg_reuse_interpolation -pc_gamg_coarse_eq_limit 10 -mg_levels_esteig_ksp_type cg -mg_levels_pc_type jacobi -ksp_type cg -ksp_norm_type unpreconditioned -snes_converged_reason -ksp_converged_reason -snes_rtol 1.e-4 -dm_mat_type aijmkl -dm_vec_type standard 278 279 TEST*/ 280