xref: /libCEED/examples/petsc/bps.c (revision d15030167e504f21b69a912dc34420b169f681b5)
1 // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 //                        libCEED + PETSc Example: CEED BPs
9 //
10 // This example demonstrates a simple usage of libCEED with PETSc to solve the CEED BP benchmark problems, see http://ceed.exascaleproject.org/bps.
11 //
12 // The code uses higher level communication protocols in DMPlex.
13 //
14 // Build with:
15 //
16 //     make bps [PETSC_DIR=</path/to/petsc>] [CEED_DIR=</path/to/libceed>]
17 //
18 // Sample runs:
19 //
20 //     ./bps -problem bp1 -degree 3
21 //     ./bps -problem bp2 -degree 3
22 //     ./bps -problem bp3 -degree 3
23 //     ./bps -problem bp4 -degree 3
24 //     ./bps -problem bp5 -degree 3 -ceed /cpu/self
25 //     ./bps -problem bp6 -degree 3 -ceed /gpu/cuda
26 //
27 //TESTARGS(name="BP3, tet elements") -ceed {ceed_resource} -test -problem bp3 -degree 3 -ksp_max_it_clip 50,50 -simplex
28 //TESTARGS(name="BP5, hex elements") -ceed {ceed_resource} -test -problem bp5 -degree 3 -ksp_max_it_clip 18,18
29 //TESTARGS(name="BP1+3, hex elements") -ceed {ceed_resource} -test -problem bp1_3 -degree 3 -ksp_max_it_clip 18,18
30 //TESTARGS(name="BP2+4, hex elements") -ceed {ceed_resource} -test -problem bp2_4 -degree 3 -ksp_max_it_clip 18,18
31 
32 /// @file
33 /// CEED BPs example using PETSc with DMPlex
34 /// See bpsraw.c for a "raw" implementation using a structured grid.
35 const char help[] = "Solve CEED BPs using PETSc with DMPlex\n";
36 
37 #include "bps.h"
38 
39 #include <ceed.h>
40 #include <petscdmplex.h>
41 #include <petscksp.h>
42 #include <stdbool.h>
43 #include <string.h>
44 
45 #include "include/bpsproblemdata.h"
46 #include "include/libceedsetup.h"
47 #include "include/matops.h"
48 #include "include/petscutils.h"
49 #include "include/petscversion.h"
50 #include "include/structs.h"
51 
52 // -----------------------------------------------------------------------------
53 // Main body of program, called in a loop for performance benchmarking purposes
54 // -----------------------------------------------------------------------------
55 static PetscErrorCode RunWithDM(RunParams rp, DM dm, const char *ceed_resource) {
56   double               my_rt_start, my_rt, rt_min, rt_max;
57   PetscInt             xl_size, l_size, g_size;
58   Vec                  X, X_loc, rhs, rhs_loc;
59   Mat                  mat_O;
60   KSP                  ksp;
61   OperatorApplyContext op_apply_ctx, op_error_ctx;
62   Ceed                 ceed;
63   CeedData             ceed_data;
64   CeedQFunction        qf_error;
65   CeedOperator         op_error;
66   CeedVector           rhs_ceed, target;
67   VecType              vec_type = VECSTANDARD;
68   PetscMemType         mem_type;
69 
70   PetscFunctionBeginUser;
71   // Set up libCEED
72   CeedInit(ceed_resource, &ceed);
73   CeedMemType mem_type_backend;
74   CeedGetPreferredMemType(ceed, &mem_type_backend);
75 
76   // Set mesh vec_type
77   switch (mem_type_backend) {
78     case CEED_MEM_HOST:
79       vec_type = VECSTANDARD;
80       break;
81     case CEED_MEM_DEVICE: {
82       const char *resolved;
83 
84       CeedGetResource(ceed, &resolved);
85       if (strstr(resolved, "/gpu/cuda")) vec_type = VECCUDA;
86       else if (strstr(resolved, "/gpu/hip")) vec_type = VECHIP;
87       else vec_type = VECSTANDARD;
88     }
89   }
90   PetscCall(DMSetVecType(dm, vec_type));
91   PetscCall(DMSetFromOptions(dm));
92 
93   // Create global and local solution vectors
94   PetscCall(DMCreateGlobalVector(dm, &X));
95   PetscCall(VecGetLocalSize(X, &l_size));
96   PetscCall(VecGetSize(X, &g_size));
97   PetscCall(DMCreateLocalVector(dm, &X_loc));
98   PetscCall(VecGetSize(X_loc, &xl_size));
99   PetscCall(VecDuplicate(X, &rhs));
100 
101   // Operator
102   PetscCall(PetscMalloc1(1, &op_apply_ctx));
103   PetscCall(PetscMalloc1(1, &op_error_ctx));
104   PetscCall(MatCreateShell(rp->comm, l_size, l_size, g_size, g_size, op_apply_ctx, &mat_O));
105   PetscCall(MatShellSetOperation(mat_O, MATOP_MULT, (void (*)(void))MatMult_Ceed));
106   PetscCall(MatShellSetOperation(mat_O, MATOP_GET_DIAGONAL, (void (*)(void))MatGetDiag));
107   PetscCall(MatShellSetVecType(mat_O, vec_type));
108 
109   // Print summary
110   if (!rp->test_mode) {
111     PetscInt P = rp->degree + 1, Q = P + rp->q_extra;
112 
113     const char *used_resource;
114     CeedGetResource(ceed, &used_resource);
115 
116     bool is_combined_bp = rp->bp_choice > CEED_BP6;
117     char bp_name[6]     = "";
118 
119     if (is_combined_bp) {
120       PetscCall(PetscSNPrintf(bp_name, 6, "%d + %d", rp->bp_choice % 2 ? 2 : 1, rp->bp_choice - CEED_BP4));
121     } else {
122       PetscCall(PetscSNPrintf(bp_name, 6, "%d", rp->bp_choice + 1));
123     }
124 
125     VecType vec_type;
126     PetscCall(VecGetType(X, &vec_type));
127 
128     PetscInt c_start, c_end;
129     PetscCall(DMPlexGetHeightStratum(dm, 0, &c_start, &c_end));
130     DMPolytopeType cell_type;
131     PetscCall(DMPlexGetCellType(dm, c_start, &cell_type));
132     CeedElemTopology elem_topo = ElemTopologyP2C(cell_type);
133     PetscMPIInt      comm_size;
134     PetscCall(MPI_Comm_size(rp->comm, &comm_size));
135     PetscCall(PetscPrintf(rp->comm,
136                           "\n-- CEED Benchmark Problem %s -- libCEED + PETSc --\n"
137                           "  MPI:\n"
138                           "    Hostname                                : %s\n"
139                           "    Total ranks                             : %d\n"
140                           "    Ranks per compute node                  : %d\n"
141                           "  PETSc:\n"
142                           "    PETSc Vec Type                          : %s\n"
143                           "  libCEED:\n"
144                           "    libCEED Backend                         : %s\n"
145                           "    libCEED Backend MemType                 : %s\n"
146                           "  Mesh:\n"
147                           "    Solution Order (P)                      : %" PetscInt_FMT "\n"
148                           "    Quadrature  Order (Q)                   : %" PetscInt_FMT "\n"
149                           "    Additional quadrature points (q_extra)  : %" PetscInt_FMT "\n"
150                           "    Global nodes                            : %" PetscInt_FMT "\n"
151                           "    Local Elements                          : %" PetscInt_FMT "\n"
152                           "    Element topology                        : %s\n"
153                           "    Owned nodes                             : %" PetscInt_FMT "\n"
154                           "    DoF per node                            : %" PetscInt_FMT "\n",
155                           bp_name, rp->hostname, comm_size, rp->ranks_per_node, vec_type, used_resource, CeedMemTypes[mem_type_backend], P, Q,
156                           rp->q_extra, g_size / rp->num_comp_u, c_end - c_start, CeedElemTopologies[elem_topo], l_size / rp->num_comp_u,
157                           rp->num_comp_u));
158   }
159 
160   // Create RHS vector
161   PetscCall(VecDuplicate(X_loc, &rhs_loc));
162   PetscCall(VecZeroEntries(rhs_loc));
163   CeedVectorCreate(ceed, xl_size, &rhs_ceed);
164   PetscCall(VecP2C(rhs_loc, &mem_type, rhs_ceed));
165 
166   PetscCall(PetscMalloc1(1, &ceed_data));
167   PetscCall(SetupLibceedByDegree(dm, ceed, rp->degree, rp->dim, rp->q_extra, rp->dim, rp->num_comp_u, g_size, xl_size, bp_options[rp->bp_choice],
168                                  ceed_data, true, true, rhs_ceed, &target));
169 
170   // Gather RHS
171   PetscCall(VecC2P(rhs_ceed, mem_type, rhs_loc));
172   PetscCall(VecZeroEntries(rhs));
173   PetscCall(DMLocalToGlobal(dm, rhs_loc, ADD_VALUES, rhs));
174   CeedVectorDestroy(&rhs_ceed);
175 
176   // Create the error QFunction
177   CeedQFunctionCreateInterior(ceed, 1, bp_options[rp->bp_choice].error, bp_options[rp->bp_choice].error_loc, &qf_error);
178   CeedQFunctionAddInput(qf_error, "u", rp->num_comp_u, CEED_EVAL_INTERP);
179   CeedQFunctionAddInput(qf_error, "true_soln", rp->num_comp_u, CEED_EVAL_NONE);
180   CeedQFunctionAddInput(qf_error, "qdata", ceed_data->q_data_size, CEED_EVAL_NONE);
181   CeedQFunctionAddOutput(qf_error, "error", rp->num_comp_u, CEED_EVAL_INTERP);
182 
183   // Create the error operator
184   CeedOperatorCreate(ceed, qf_error, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_error);
185   CeedOperatorSetField(op_error, "u", ceed_data->elem_restr_u, ceed_data->basis_u, CEED_VECTOR_ACTIVE);
186   CeedOperatorSetField(op_error, "true_soln", ceed_data->elem_restr_u_i, CEED_BASIS_NONE, target);
187   CeedOperatorSetField(op_error, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_NONE, ceed_data->q_data);
188   CeedOperatorSetField(op_error, "error", ceed_data->elem_restr_u, ceed_data->basis_u, CEED_VECTOR_ACTIVE);
189 
190   // Set up apply operator context
191   PetscCall(SetupApplyOperatorCtx(rp->comm, dm, ceed, ceed_data, X_loc, op_apply_ctx));
192   PetscCall(KSPCreate(rp->comm, &ksp));
193   {
194     PC pc;
195     PetscCall(KSPGetPC(ksp, &pc));
196     if (rp->bp_choice == CEED_BP1 || rp->bp_choice == CEED_BP2 || rp->bp_choice == CEED_BP13 || rp->bp_choice == CEED_BP24 ||
197         rp->bp_choice == CEED_BP15 || rp->bp_choice == CEED_BP26) {
198       PetscCall(PCSetType(pc, PCJACOBI));
199       if (rp->simplex || rp->bp_choice == CEED_BP13 || rp->bp_choice == CEED_BP24 || rp->bp_choice == CEED_BP15 || rp->bp_choice == CEED_BP26) {
200         PetscCall(PCJacobiSetType(pc, PC_JACOBI_DIAGONAL));
201       } else {
202         PetscCall(PCJacobiSetType(pc, PC_JACOBI_ROWSUM));
203       }
204     } else {
205       PetscCall(PCSetType(pc, PCNONE));
206     }
207     PetscCall(KSPSetType(ksp, KSPCG));
208     PetscCall(KSPSetNormType(ksp, KSP_NORM_NATURAL));
209     PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT));
210   }
211   PetscCall(KSPSetOperators(ksp, mat_O, mat_O));
212 
213   // First run's performance log is not considered for benchmarking purposes
214   PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, 1));
215   my_rt_start = MPI_Wtime();
216   PetscCall(KSPSolve(ksp, rhs, X));
217   my_rt = MPI_Wtime() - my_rt_start;
218   PetscCall(MPI_Allreduce(MPI_IN_PLACE, &my_rt, 1, MPI_DOUBLE, MPI_MIN, rp->comm));
219   // Set maxits based on first iteration timing
220   if (my_rt > 0.02) {
221     PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, rp->ksp_max_it_clip[0]));
222   } else {
223     PetscCall(KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, rp->ksp_max_it_clip[1]));
224   }
225   PetscCall(KSPSetFromOptions(ksp));
226 
227   // Timed solve
228   PetscCall(VecZeroEntries(X));
229   PetscCall(PetscBarrier((PetscObject)ksp));
230 
231   // -- Performance logging
232   PetscCall(PetscLogStagePush(rp->solve_stage));
233 
234   // -- Solve
235   my_rt_start = MPI_Wtime();
236   PetscCall(KSPSolve(ksp, rhs, X));
237   my_rt = MPI_Wtime() - my_rt_start;
238 
239   // -- Performance logging
240   PetscCall(PetscLogStagePop());
241 
242   // Output results
243   {
244     KSPType            ksp_type;
245     KSPConvergedReason reason;
246     PetscReal          rnorm;
247     PetscInt           its;
248     PetscCall(KSPGetType(ksp, &ksp_type));
249     PetscCall(KSPGetConvergedReason(ksp, &reason));
250     PetscCall(KSPGetIterationNumber(ksp, &its));
251     PetscCall(KSPGetResidualNorm(ksp, &rnorm));
252     if (!rp->test_mode || reason < 0 || rnorm > 1e-8) {
253       PetscCall(PetscPrintf(rp->comm,
254                             "  KSP:\n"
255                             "    KSP Type                                : %s\n"
256                             "    KSP Convergence                         : %s\n"
257                             "    Total KSP Iterations                    : %" PetscInt_FMT "\n"
258                             "    Final rnorm                             : %e\n",
259                             ksp_type, KSPConvergedReasons[reason], its, (double)rnorm));
260     }
261     if (!rp->test_mode) {
262       PetscCall(PetscPrintf(rp->comm, "  Performance:\n"));
263     }
264     {
265       // Set up error operator context
266       PetscCall(SetupErrorOperatorCtx(rp->comm, dm, ceed, ceed_data, X_loc, op_error, op_error_ctx));
267       PetscScalar l2_error;
268       PetscCall(ComputeL2Error(X, &l2_error, op_error_ctx));
269       // Tighter tol for BP1, BP2
270       // Looser tol for BP3, BP4, BP5, and BP6 with extra for vector valued problems
271       // BP1+3 and BP2+4 follow the pattern for BP3 and BP4
272       // BP1+5 and BP2+6 follow the pattern for BP5 and BP6
273       PetscReal tol = rp->bp_choice < CEED_BP3 ? 5e-4 : (5e-2 + (rp->bp_choice % 2 == 1 ? 5e-3 : 0));
274       if (!rp->test_mode || l2_error > tol) {
275         PetscCall(MPI_Allreduce(&my_rt, &rt_min, 1, MPI_DOUBLE, MPI_MIN, rp->comm));
276         PetscCall(MPI_Allreduce(&my_rt, &rt_max, 1, MPI_DOUBLE, MPI_MAX, rp->comm));
277         PetscCall(PetscPrintf(rp->comm,
278                               "    L2 Error                                : %e\n"
279                               "    CG Solve Time                           : %g (%g) sec\n",
280                               (double)l2_error, rt_max, rt_min));
281       }
282     }
283     if (!rp->test_mode) {
284       PetscCall(PetscPrintf(rp->comm, "    DoFs/Sec in CG                          : %g (%g) million\n", 1e-6 * g_size * its / rt_max,
285                             1e-6 * g_size * its / rt_min));
286     }
287   }
288 
289   if (rp->write_solution) {
290     PetscViewer vtk_viewer_soln;
291 
292     PetscCall(PetscViewerCreate(rp->comm, &vtk_viewer_soln));
293     PetscCall(PetscViewerSetType(vtk_viewer_soln, PETSCVIEWERVTK));
294     PetscCall(PetscViewerFileSetName(vtk_viewer_soln, "solution.vtu"));
295     PetscCall(VecView(X, vtk_viewer_soln));
296     PetscCall(PetscViewerDestroy(&vtk_viewer_soln));
297   }
298 
299   // Cleanup
300   PetscCall(VecDestroy(&X));
301   PetscCall(VecDestroy(&X_loc));
302   PetscCall(VecDestroy(&op_apply_ctx->Y_loc));
303   PetscCall(VecDestroy(&op_error_ctx->Y_loc));
304   PetscCall(MatDestroy(&mat_O));
305   PetscCall(PetscFree(op_apply_ctx));
306   PetscCall(PetscFree(op_error_ctx));
307   PetscCall(CeedDataDestroy(0, ceed_data));
308 
309   PetscCall(VecDestroy(&rhs));
310   PetscCall(VecDestroy(&rhs_loc));
311   PetscCall(KSPDestroy(&ksp));
312   CeedVectorDestroy(&target);
313   CeedQFunctionDestroy(&qf_error);
314   CeedOperatorDestroy(&op_error);
315   CeedDestroy(&ceed);
316   PetscFunctionReturn(PETSC_SUCCESS);
317 }
318 
319 static PetscErrorCode Run(RunParams rp, PetscInt num_resources, char *const *ceed_resources, PetscInt num_bp_choices, const BPType *bp_choices) {
320   DM dm;
321 
322   PetscFunctionBeginUser;
323   // Setup DM
324   PetscCall(CreateDistributedDM(rp, &dm));
325 
326   for (PetscInt b = 0; b < num_bp_choices; b++) {
327     DM       dm_deg;
328     VecType  vec_type;
329     PetscInt q_extra = rp->q_extra;
330     rp->bp_choice    = bp_choices[b];
331     rp->num_comp_u   = bp_options[rp->bp_choice].num_comp_u;
332     rp->q_extra      = q_extra < 0 ? bp_options[rp->bp_choice].q_extra : q_extra;
333     PetscCall(DMClone(dm, &dm_deg));
334     PetscCall(DMGetVecType(dm, &vec_type));
335     PetscCall(DMSetVecType(dm_deg, vec_type));
336     // Create DM
337     PetscInt dim;
338     PetscCall(DMGetDimension(dm_deg, &dim));
339     PetscCall(SetupDMByDegree(dm_deg, rp->degree, rp->q_extra, rp->num_comp_u, dim, bp_options[rp->bp_choice].enforce_bc));
340     for (PetscInt r = 0; r < num_resources; r++) {
341       PetscCall(RunWithDM(rp, dm_deg, ceed_resources[r]));
342     }
343     PetscCall(DMDestroy(&dm_deg));
344     rp->q_extra = q_extra;
345   }
346 
347   PetscCall(DMDestroy(&dm));
348   PetscFunctionReturn(PETSC_SUCCESS);
349 }
350 
351 int main(int argc, char **argv) {
352   PetscMPIInt comm_size;
353   RunParams   rp;
354   MPI_Comm    comm;
355   char        filename[PETSC_MAX_PATH_LEN];
356   char       *ceed_resources[30];
357   PetscInt    num_ceed_resources = 30;
358   char        hostname[PETSC_MAX_PATH_LEN];
359 
360   PetscInt    dim = 3, mesh_elem[3] = {3, 3, 3};
361   PetscInt    num_degrees = 30, degree[30] = {0}, num_local_nodes = 2, local_nodes[2] = {0};
362   PetscMPIInt ranks_per_node;
363   PetscBool   degree_set;
364   BPType      bp_choices[10];
365   PetscInt    num_bp_choices = 10;
366 
367   // Initialize PETSc
368   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
369   comm = PETSC_COMM_WORLD;
370   PetscCall(MPI_Comm_size(comm, &comm_size));
371 #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
372   {
373     MPI_Comm splitcomm;
374 
375     PetscCall(MPI_Comm_split_type(comm, MPI_COMM_TYPE_SHARED, 0, MPI_INFO_NULL, &splitcomm));
376     PetscCall(MPI_Comm_size(splitcomm, &ranks_per_node));
377     PetscCall(MPI_Comm_free(&splitcomm));
378   }
379 #else
380   ranks_per_node = -1;  // Unknown
381 #endif
382 
383   // Setup all parameters needed in Run()
384   PetscCall(PetscMalloc1(1, &rp));
385   rp->comm = comm;
386 
387   // Read command line options
388   PetscOptionsBegin(comm, NULL, "CEED BPs in PETSc", NULL);
389   {
390     PetscBool set;
391     PetscCall(PetscOptionsEnumArray("-problem", "CEED benchmark problem to solve", NULL, bp_types, (PetscEnum *)bp_choices, &num_bp_choices, &set));
392     if (!set) {
393       bp_choices[0]  = CEED_BP1;
394       num_bp_choices = 1;
395     }
396   }
397   rp->test_mode = PETSC_FALSE;
398   PetscCall(PetscOptionsBool("-test", "Testing mode (do not print unless error is large)", NULL, rp->test_mode, &rp->test_mode, NULL));
399   rp->write_solution = PETSC_FALSE;
400   PetscCall(PetscOptionsBool("-write_solution", "Write solution for visualization", NULL, rp->write_solution, &rp->write_solution, NULL));
401   rp->simplex = PETSC_FALSE;
402   PetscCall(PetscOptionsBool("-simplex", "Element topology (default:hex)", NULL, rp->simplex, &rp->simplex, NULL));
403   if ((bp_choices[0] == CEED_BP5 || bp_choices[0] == CEED_BP6) && (rp->simplex)) {
404     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "BP5/6 is not supported with simplex");
405   }
406   degree[0] = rp->test_mode ? 3 : 2;
407   PetscCall(PetscOptionsIntArray("-degree", "Polynomial degree of tensor product basis", NULL, degree, &num_degrees, &degree_set));
408   if (!degree_set) num_degrees = 1;
409   rp->q_extra = PETSC_DECIDE;
410   PetscCall(PetscOptionsInt("-q_extra", "Number of extra quadrature points (-1 for auto)", NULL, rp->q_extra, &rp->q_extra, NULL));
411   {
412     PetscBool set;
413     PetscCall(PetscOptionsStringArray("-ceed", "CEED resource specifier (comma-separated list)", NULL, ceed_resources, &num_ceed_resources, &set));
414     if (!set) {
415       PetscCall(PetscStrallocpy("/cpu/self", &ceed_resources[0]));
416       num_ceed_resources = 1;
417     }
418   }
419   PetscCall(PetscGetHostName(hostname, sizeof hostname));
420   PetscCall(PetscOptionsString("-hostname", "Hostname for output", NULL, hostname, hostname, sizeof(hostname), NULL));
421   rp->read_mesh = PETSC_FALSE;
422   PetscCall(PetscOptionsString("-mesh", "Read mesh from file", NULL, filename, filename, sizeof(filename), &rp->read_mesh));
423   rp->filename = filename;
424   if (!rp->read_mesh) {
425     PetscInt tmp = dim;
426     PetscCall(PetscOptionsIntArray("-cells", "Number of cells per dimension", NULL, mesh_elem, &tmp, NULL));
427   }
428   local_nodes[0] = 1000;
429   PetscCall(PetscOptionsIntArray("-local_nodes",
430                                  "Target number of locally owned nodes per "
431                                  "process (single value or min,max)",
432                                  NULL, local_nodes, &num_local_nodes, &rp->user_l_nodes));
433   if (num_local_nodes < 2) local_nodes[1] = 2 * local_nodes[0];
434   {
435     PetscInt two           = 2;
436     rp->ksp_max_it_clip[0] = 5;
437     rp->ksp_max_it_clip[1] = 20;
438     PetscCall(PetscOptionsIntArray("-ksp_max_it_clip", "Min and max number of iterations to use during benchmarking", NULL, rp->ksp_max_it_clip, &two,
439                                    NULL));
440   }
441   if (!degree_set) {
442     PetscInt max_degree = 8;
443     PetscCall(PetscOptionsInt("-max_degree", "Range of degrees [1, max_degree] to run with", NULL, max_degree, &max_degree, NULL));
444     for (PetscInt i = 0; i < max_degree; i++) degree[i] = i + 1;
445     num_degrees = max_degree;
446   }
447   {
448     PetscBool flg;
449     PetscInt  p = ranks_per_node;
450     PetscCall(PetscOptionsInt("-p", "Number of MPI ranks per node", NULL, p, &p, &flg));
451     if (flg) ranks_per_node = p;
452   }
453 
454   PetscOptionsEnd();
455 
456   // Register PETSc logging stage
457   PetscCall(PetscLogStageRegister("Solve Stage", &rp->solve_stage));
458 
459   rp->hostname       = hostname;
460   rp->dim            = dim;
461   rp->mesh_elem      = mesh_elem;
462   rp->ranks_per_node = ranks_per_node;
463 
464   for (PetscInt d = 0; d < num_degrees; d++) {
465     PetscInt deg = degree[d];
466     for (PetscInt n = local_nodes[0]; n < local_nodes[1]; n *= 2) {
467       rp->degree      = deg;
468       rp->local_nodes = n;
469       PetscCall(Run(rp, num_ceed_resources, ceed_resources, num_bp_choices, bp_choices));
470     }
471   }
472   // Clear memory
473   PetscCall(PetscFree(rp));
474   for (PetscInt i = 0; i < num_ceed_resources; i++) {
475     PetscCall(PetscFree(ceed_resources[i]));
476   }
477   return PetscFinalize();
478 }
479