xref: /petsc/src/dm/dt/fe/tests/ex1.c (revision eb23ec828dce5d2018966dde62ea131297bcf5f7)
1 static const char help[] = "Performance Tests for FE Integration";
2 
3 #include <petscdmplex.h>
4 #include <petscfe.h>
5 #include <petscds.h>
6 
7 typedef struct {
8   PetscInt  dim;     /* The topological dimension */
9   PetscBool simplex; /* True for simplices, false for hexes */
10   PetscInt  its;     /* Number of replications for timing */
11   PetscInt  cbs;     /* Number of cells in an integration block */
12 } AppCtx;
13 
14 static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
15 {
16   PetscErrorCode ierr;
17 
18   PetscFunctionBeginUser;
19   options->dim     = 2;
20   options->simplex = PETSC_TRUE;
21   options->its     = 1;
22   options->cbs     = 8;
23 
24   ierr = PetscOptionsBegin(comm, "", "FE Integration Performance Options", "PETSCFE");CHKERRQ(ierr);
25   CHKERRQ(PetscOptionsInt("-dim", "The topological dimension", "ex1.c", options->dim, &options->dim, NULL));
26   CHKERRQ(PetscOptionsBool("-simplex", "Simplex or hex cells", "ex1.c", options->simplex, &options->simplex, NULL));
27   CHKERRQ(PetscOptionsInt("-its", "The number of replications for timing", "ex1.c", options->its, &options->its, NULL));
28   CHKERRQ(PetscOptionsInt("-cbs", "The number of cells in an integration block", "ex1.c", options->cbs, &options->cbs, NULL));
29   ierr = PetscOptionsEnd();CHKERRQ(ierr);
30   PetscFunctionReturn(0);
31 }
32 
33 static PetscErrorCode trig_u(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx)
34 {
35   PetscInt d;
36   *u = 0.0;
37   for (d = 0; d < dim; ++d) *u += PetscSinReal(2.0*PETSC_PI*x[d]);
38   return 0;
39 }
40 
41 static void f0_trig_u(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, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
45 {
46   PetscInt d;
47   for (d = 0; d < dim; ++d) f0[0] += -4.0*PetscSqr(PETSC_PI)*PetscSinReal(2.0*PETSC_PI*x[d]);
48 }
49 
50 static void f1_u(PetscInt dim, PetscInt Nf, PetscInt NfAux,
51                  const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
52                  const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
53                  PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[])
54 {
55   PetscInt d;
56   for (d = 0; d < dim; ++d) f1[d] = u_x[d];
57 }
58 
59 static void g3_uu(PetscInt dim, PetscInt Nf, PetscInt NfAux,
60                   const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
61                   const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
62                   PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[])
63 {
64   PetscInt d;
65   for (d = 0; d < dim; ++d) g3[d*dim+d] = 1.0;
66 }
67 
68 static PetscErrorCode SetupPrimalProblem(DM dm, AppCtx *user)
69 {
70   PetscDS        prob;
71   DMLabel        label;
72   const PetscInt id = 1;
73 
74   PetscFunctionBeginUser;
75   CHKERRQ(DMGetDS(dm, &prob));
76   CHKERRQ(PetscDSSetResidual(prob, 0, f0_trig_u, f1_u));
77   CHKERRQ(PetscDSSetJacobian(prob, 0, 0, NULL, NULL, NULL, g3_uu));
78   CHKERRQ(PetscDSSetExactSolution(prob, 0, trig_u, user));
79   CHKERRQ(DMGetLabel(dm, "marker", &label));
80   CHKERRQ(DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, 1, &id, 0, 0, NULL, (void (*)(void)) trig_u, NULL, user, NULL));
81   PetscFunctionReturn(0);
82 }
83 
84 static PetscErrorCode SetupDiscretization(DM dm, const char name[], PetscErrorCode (*setup)(DM, AppCtx *), AppCtx *user)
85 {
86   DM             cdm = dm;
87   PetscFE        fe;
88   char           prefix[PETSC_MAX_PATH_LEN];
89 
90   PetscFunctionBeginUser;
91   /* Create finite element */
92   CHKERRQ(PetscSNPrintf(prefix, PETSC_MAX_PATH_LEN, "%s_", name));
93   CHKERRQ(PetscFECreateDefault(PetscObjectComm((PetscObject) dm), user->dim, 1, user->simplex, name ? prefix : NULL, -1, &fe));
94   CHKERRQ(PetscObjectSetName((PetscObject) fe, name));
95   /* Set discretization and boundary conditions for each mesh */
96   CHKERRQ(DMSetField(dm, 0, NULL, (PetscObject) fe));
97   CHKERRQ(DMCreateDS(dm));
98   CHKERRQ((*setup)(dm, user));
99   while (cdm) {
100     CHKERRQ(DMCopyDisc(dm,cdm));
101     /* TODO: Check whether the boundary of coarse meshes is marked */
102     CHKERRQ(DMGetCoarseDM(cdm, &cdm));
103   }
104   CHKERRQ(PetscFEDestroy(&fe));
105   PetscFunctionReturn(0);
106 }
107 
108 static PetscErrorCode PetscContainerUserDestroy_PetscFEGeom(void *ctx)
109 {
110   PetscFEGeom   *geom = (PetscFEGeom *) ctx;
111 
112   PetscFunctionBegin;
113   CHKERRQ(PetscFEGeomDestroy(&geom));
114   PetscFunctionReturn(0);
115 }
116 
117 PetscErrorCode CellRangeGetFEGeom(IS cellIS, DMField coordField, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
118 {
119   char            composeStr[33] = {0};
120   PetscObjectId   id;
121   PetscContainer  container;
122 
123   PetscFunctionBegin;
124   CHKERRQ(PetscObjectGetId((PetscObject) quad, &id));
125   CHKERRQ(PetscSNPrintf(composeStr, 32, "CellRangeGetFEGeom_%x\n", id));
126   CHKERRQ(PetscObjectQuery((PetscObject) cellIS, composeStr, (PetscObject *) &container));
127   if (container) {
128     CHKERRQ(PetscContainerGetPointer(container, (void **) geom));
129   } else {
130     CHKERRQ(DMFieldCreateFEGeom(coordField, cellIS, quad, faceData, geom));
131     CHKERRQ(PetscContainerCreate(PETSC_COMM_SELF, &container));
132     CHKERRQ(PetscContainerSetPointer(container, (void *) *geom));
133     CHKERRQ(PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom));
134     CHKERRQ(PetscObjectCompose((PetscObject) cellIS, composeStr, (PetscObject) container));
135     CHKERRQ(PetscContainerDestroy(&container));
136   }
137   PetscFunctionReturn(0);
138 }
139 
140 PetscErrorCode CellRangeRestoreFEGeom(IS cellIS, DMField coordField, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
141 {
142   PetscFunctionBegin;
143   *geom = NULL;
144   PetscFunctionReturn(0);
145 }
146 
147 static PetscErrorCode CreateFEGeometry(DM dm, PetscDS ds, IS cellIS, PetscQuadrature *affineQuad, PetscFEGeom **affineGeom, PetscQuadrature **quads, PetscFEGeom ***geoms)
148 {
149   DMField        coordField;
150   PetscInt       Nf, f, maxDegree;
151 
152   PetscFunctionBeginUser;
153   *affineQuad = NULL;
154   *affineGeom = NULL;
155   *quads      = NULL;
156   *geoms      = NULL;
157   CHKERRQ(PetscDSGetNumFields(ds, &Nf));
158   CHKERRQ(DMGetCoordinateField(dm, &coordField));
159   CHKERRQ(DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree));
160   if (maxDegree <= 1) {
161     CHKERRQ(DMFieldCreateDefaultQuadrature(coordField, cellIS, affineQuad));
162     if (*affineQuad) CHKERRQ(CellRangeGetFEGeom(cellIS, coordField, *affineQuad, PETSC_FALSE, affineGeom));
163   } else {
164     CHKERRQ(PetscCalloc2(Nf, quads, Nf, geoms));
165     for (f = 0; f < Nf; ++f) {
166       PetscFE fe;
167 
168       CHKERRQ(PetscDSGetDiscretization(ds, f, (PetscObject *) &fe));
169       CHKERRQ(PetscFEGetQuadrature(fe, &(*quads)[f]));
170       CHKERRQ(PetscObjectReference((PetscObject) (*quads)[f]));
171       CHKERRQ(CellRangeGetFEGeom(cellIS, coordField, (*quads)[f], PETSC_FALSE, &(*geoms)[f]));
172     }
173   }
174   PetscFunctionReturn(0);
175 }
176 
177 static PetscErrorCode DestroyFEGeometry(DM dm, PetscDS ds, IS cellIS, PetscQuadrature *affineQuad, PetscFEGeom **affineGeom, PetscQuadrature **quads, PetscFEGeom ***geoms)
178 {
179   DMField        coordField;
180   PetscInt       Nf, f;
181 
182   PetscFunctionBeginUser;
183   CHKERRQ(PetscDSGetNumFields(ds, &Nf));
184   CHKERRQ(DMGetCoordinateField(dm, &coordField));
185   if (*affineQuad) {
186     CHKERRQ(CellRangeRestoreFEGeom(cellIS, coordField, *affineQuad, PETSC_FALSE, affineGeom));
187     CHKERRQ(PetscQuadratureDestroy(affineQuad));
188   } else {
189     for (f = 0; f < Nf; ++f) {
190       CHKERRQ(CellRangeRestoreFEGeom(cellIS, coordField, (*quads)[f], PETSC_FALSE, &(*geoms)[f]));
191       CHKERRQ(PetscQuadratureDestroy(&(*quads)[f]));
192     }
193     CHKERRQ(PetscFree2(*quads, *geoms));
194   }
195   PetscFunctionReturn(0);
196 }
197 
198 static PetscErrorCode TestIntegration(DM dm, PetscInt cbs, PetscInt its)
199 {
200   PetscDS         ds;
201   PetscFEGeom    *chunkGeom = NULL;
202   PetscQuadrature affineQuad,  *quads = NULL;
203   PetscFEGeom    *affineGeom, **geoms = NULL;
204   PetscScalar    *u, *elemVec;
205   IS              cellIS;
206   PetscInt        depth, cStart, cEnd, cell, chunkSize = cbs, Nch = 0, Nf, f, totDim, i, k;
207 #if defined(PETSC_USE_LOG)
208   PetscLogStage   stage;
209   PetscLogEvent   event;
210 #endif
211 
212   PetscFunctionBeginUser;
213   CHKERRQ(PetscLogStageRegister("PetscFE Residual Integration Test", &stage));
214   CHKERRQ(PetscLogEventRegister("FEIntegRes", PETSCFE_CLASSID, &event));
215   CHKERRQ(PetscLogStagePush(stage));
216   CHKERRQ(DMPlexGetDepth(dm, &depth));
217   CHKERRQ(DMGetStratumIS(dm, "depth", depth, &cellIS));
218   CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
219   CHKERRQ(DMGetCellDS(dm, cStart, &ds));
220   CHKERRQ(PetscDSGetNumFields(ds, &Nf));
221   CHKERRQ(PetscDSGetTotalDimension(ds, &totDim));
222   CHKERRQ(CreateFEGeometry(dm, ds, cellIS, &affineQuad, &affineGeom, &quads, &geoms));
223   CHKERRQ(PetscMalloc2(chunkSize*totDim, &u, chunkSize*totDim, &elemVec));
224   /* Assumptions:
225     - Single field
226     - No input data
227     - No auxiliary data
228     - No time-dependence
229   */
230   for (i = 0; i < its; ++i) {
231     for (cell = cStart; cell < cEnd; cell += chunkSize, ++Nch) {
232       const PetscInt cS = cell, cE = PetscMin(cS + chunkSize, cEnd), Ne = cE - cS;
233 
234       CHKERRQ(PetscArrayzero(elemVec, chunkSize*totDim));
235       /* TODO Replace with DMPlexGetCellFields() */
236       for (k = 0; k < chunkSize*totDim; ++k) u[k] = 1.0;
237       for (f = 0; f < Nf; ++f) {
238         PetscFormKey key;
239         PetscFEGeom     *geom = affineGeom ? affineGeom : geoms[f];
240         /* PetscQuadrature quad = affineQuad ? affineQuad : quads[f]; */
241 
242         key.label = NULL; key.value = 0; key.field = f;
243         CHKERRQ(PetscFEGeomGetChunk(geom, cS, cE, &chunkGeom));
244         CHKERRQ(PetscLogEventBegin(event,0,0,0,0));
245         CHKERRQ(PetscFEIntegrateResidual(ds, key, Ne, chunkGeom, u, NULL, NULL, NULL, 0.0, elemVec));
246         CHKERRQ(PetscLogEventEnd(event,0,0,0,0));
247       }
248     }
249   }
250   CHKERRQ(PetscFEGeomRestoreChunk(affineGeom, cStart, cEnd, &chunkGeom));
251   CHKERRQ(DestroyFEGeometry(dm, ds, cellIS, &affineQuad, &affineGeom, &quads, &geoms));
252   CHKERRQ(ISDestroy(&cellIS));
253   CHKERRQ(PetscFree2(u, elemVec));
254   CHKERRQ(PetscLogStagePop());
255 #if defined(PETSC_USE_LOG)
256   {
257     const char        *title = "Petsc FE Residual Integration";
258     PetscEventPerfInfo eventInfo;
259     PetscInt           N = (cEnd - cStart)*Nf*its;
260     PetscReal          flopRate, cellRate;
261 
262     CHKERRQ(PetscLogEventGetPerfInfo(stage, event, &eventInfo));
263     flopRate = eventInfo.time != 0.0 ? eventInfo.flops/eventInfo.time : 0.0;
264     cellRate = eventInfo.time != 0.0 ? N/eventInfo.time : 0.0;
265     CHKERRQ(PetscPrintf(PetscObjectComm((PetscObject) dm), "%s: %D integrals %D chunks %D reps\n  Cell rate: %.2f/s flop rate: %.2f MF/s\n", title, N, Nch, its, (double)cellRate, (double)(flopRate/1.e6)));
266   }
267 #endif
268   PetscFunctionReturn(0);
269 }
270 
271 static PetscErrorCode TestIntegration2(DM dm, PetscInt cbs, PetscInt its)
272 {
273   Vec             X, F;
274 #if defined(PETSC_USE_LOG)
275   PetscLogStage   stage;
276 #endif
277   PetscInt        i;
278 
279   PetscFunctionBeginUser;
280   CHKERRQ(PetscLogStageRegister("DMPlex Residual Integration Test", &stage));
281   CHKERRQ(PetscLogStagePush(stage));
282   CHKERRQ(DMGetLocalVector(dm, &X));
283   CHKERRQ(DMGetLocalVector(dm, &F));
284   for (i = 0; i < its; ++i) {
285     CHKERRQ(DMPlexSNESComputeResidualFEM(dm, X, F, NULL));
286   }
287   CHKERRQ(DMRestoreLocalVector(dm, &X));
288   CHKERRQ(DMRestoreLocalVector(dm, &F));
289   CHKERRQ(PetscLogStagePop());
290 #if defined(PETSC_USE_LOG)
291   {
292     const char         *title = "DMPlex Residual Integration";
293     PetscEventPerfInfo eventInfo;
294     PetscReal          flopRate, cellRate;
295     PetscInt           cStart, cEnd, Nf, N;
296     PetscLogEvent      event;
297 
298     CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
299     CHKERRQ(DMGetNumFields(dm, &Nf));
300     CHKERRQ(PetscLogEventGetId("DMPlexResidualFE", &event));
301     CHKERRQ(PetscLogEventGetPerfInfo(stage, event, &eventInfo));
302     N        = (cEnd - cStart)*Nf*eventInfo.count;
303     flopRate = eventInfo.time != 0.0 ? eventInfo.flops/eventInfo.time : 0.0;
304     cellRate = eventInfo.time != 0.0 ? N/eventInfo.time : 0.0;
305     CHKERRQ(PetscPrintf(PetscObjectComm((PetscObject) dm), "%s: %D integrals %D reps\n  Cell rate: %.2f/s flop rate: %.2f MF/s\n", title, N, eventInfo.count, (double)cellRate, (double)(flopRate/1.e6)));
306   }
307 #endif
308   PetscFunctionReturn(0);
309 }
310 
311 int main(int argc, char **argv)
312 {
313   DM             dm;
314   AppCtx         ctx;
315   PetscMPIInt    size;
316   PetscErrorCode ierr;
317 
318   ierr = PetscInitialize(&argc, &argv, NULL, help); if (ierr) return ierr;
319   CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
320   PetscCheckFalse(size > 1,PETSC_COMM_WORLD, PETSC_ERR_SUP, "This is a uniprocessor example only.");
321   CHKERRQ(ProcessOptions(PETSC_COMM_WORLD, &ctx));
322   CHKERRQ(PetscLogDefaultBegin());
323   CHKERRQ(DMCreate(PETSC_COMM_WORLD, &dm));
324   CHKERRQ(DMSetType(dm, DMPLEX));
325   CHKERRQ(DMSetFromOptions(dm));
326   CHKERRQ(PetscObjectSetName((PetscObject) dm, "Mesh"));
327   CHKERRQ(PetscObjectViewFromOptions((PetscObject) dm, NULL, "-dm_view"));
328   CHKERRQ(SetupDiscretization(dm, "potential", SetupPrimalProblem, &ctx));
329   CHKERRQ(TestIntegration(dm, ctx.cbs, ctx.its));
330   CHKERRQ(TestIntegration2(dm, ctx.cbs, ctx.its));
331   CHKERRQ(DMDestroy(&dm));
332   ierr = PetscFinalize();
333   return ierr;
334 }
335 
336 /*TEST
337   test:
338     suffix: 0
339     requires: triangle
340     args: -dm_view
341 
342   test:
343     suffix: 1
344     requires: triangle
345     args: -dm_view -potential_petscspace_degree 1
346 
347   test:
348     suffix: 2
349     requires: triangle
350     args: -dm_view -potential_petscspace_degree 2
351 
352   test:
353     suffix: 3
354     requires: triangle
355     args: -dm_view -potential_petscspace_degree 3
356 TEST*/
357