1
- // libCEED + PETSc Example: Shallow-water equations
1
+ // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2
+ // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3
+ // reserved. See files LICENSE and NOTICE for details.
4
+ //
5
+ // This file is part of CEED, a collection of benchmarks, miniapps, software
6
+ // libraries and APIs for efficient high-order finite element and spectral
7
+ // element discretizations for exascale applications. For more information and
8
+ // source code availability see http://github.com/ceed.
9
+ //
10
+ // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11
+ // a collaborative effort of two U.S. Department of Energy organizations (Office
12
+ // of Science and the National Nuclear Security Administration) responsible for
13
+ // the planning and preparation of a capable exascale ecosystem, including
14
+ // software, applications, hardware, advanced system engineering and early
15
+ // testbed platforms, in support of the nation's exascale computing imperative.
16
+
17
+ // libCEED + PETSc Example: Shallow-water equations
2
18
//
3
19
// This example demonstrates a simple usage of libCEED with PETSc to solve the
4
- // shallow-water equations on a cubed-sphere (i.e., a spherical surface
5
- // tessellated by quadrilaterals , obtained by projecting the sides
6
- // of a circumscribed cube onto a spherical surface ).
20
+ // shallow-water equations on a cubed-sphere (i.e., a tensor-product discrete
21
+ // sphere , obtained by projecting a cube inscribed in a sphere onto the surface
22
+ // of the sphere ).
7
23
//
8
- // The code is intentionally "raw", using only low-level communication
9
- // primitives.
24
+ // The code uses higher level communication protocols in PETSc's DMPlex.
10
25
//
11
26
// Build with:
12
27
//
27
42
28
43
const char help [] = "Solve the shallow-water equations using PETSc and libCEED\n" ;
29
44
30
- #include <petscts.h>
31
- #include <petscdmplex.h>
32
- #include <ceed.h>
33
- #include <stdbool.h>
34
- #include <petscsys.h>
35
45
#include "sw_headers.h"
36
46
37
47
int main (int argc , char * * argv ) {
@@ -57,11 +67,21 @@ int main(int argc, char **argv) {
57
67
filename [PETSC_MAX_PATH_LEN ];
58
68
Ceed ceed ;
59
69
CeedData ceeddata ;
70
+ CeedMemType memtyperequested ;
60
71
PetscScalar meter = 1e-2 ; // 1 meter in scaled length units
61
72
PetscScalar second = 1e-2 ; // 1 second in scaled time units
62
73
PetscScalar f = 0.0001 ; // mid-latitude Coriolis parameter
63
74
PetscScalar g = 9.81 ; // m/s^2
64
75
PetscScalar mpersquareds ;
76
+ // Check PETSc CUDA support
77
+ PetscBool petschavecuda , setmemtyperequest = PETSC_FALSE ;
78
+ // *INDENT-OFF*
79
+ #ifdef PETSC_HAVE_CUDA
80
+ petschavecuda = PETSC_TRUE ;
81
+ #else
82
+ petschavecuda = PETSC_FALSE ;
83
+ #endif
84
+ // *INDENT-ON*
65
85
// Performance context
66
86
double start , cpu_time_used ;
67
87
@@ -73,10 +93,6 @@ int main(int argc, char **argv) {
73
93
ierr = PetscMalloc1 (1 , & user ); CHKERRQ (ierr );
74
94
ierr = PetscMalloc1 (1 , & units ); CHKERRQ (ierr );
75
95
76
- // Set up problem type command line option
77
- //PetscFunctionListAdd(&icsflist, "sphere", &ICsSW);
78
- //PetscFunctionListAdd(&qflist, "shallow-water", &SW);
79
-
80
96
// Parse command line options
81
97
comm = PETSC_COMM_WORLD ;
82
98
ierr = PetscOptionsBegin (comm , NULL , "Shallow-water equations in PETSc with libCEED" ,
@@ -123,6 +139,12 @@ int main(int argc, char **argv) {
123
139
simplex = PETSC_FALSE ;
124
140
ierr = PetscOptionsBool ("-simplex" , "Use simplices, or tensor product cells" ,
125
141
NULL , simplex , & simplex , NULL ); CHKERRQ (ierr );
142
+ memtyperequested = petschavecuda ? CEED_MEM_DEVICE : CEED_MEM_HOST ;
143
+ ierr = PetscOptionsEnum ("-memtype" ,
144
+ "CEED MemType requested" , NULL ,
145
+ memTypes , (PetscEnum )memtyperequested ,
146
+ (PetscEnum * )& memtyperequested , & setmemtyperequest );
147
+ CHKERRQ (ierr );
126
148
ierr = PetscOptionsEnd (); CHKERRQ (ierr );
127
149
128
150
// Define derived units
@@ -216,35 +238,69 @@ int main(int argc, char **argv) {
216
238
ierr = VecGetSize (Qloc , & lnodes ); CHKERRQ (ierr );
217
239
lnodes /= ncompq ;
218
240
241
+ // Initialize CEED
242
+ CeedInit (ceedresource , & ceed );
243
+ // Set memtype
244
+ CeedMemType memtypebackend ;
245
+ CeedGetPreferredMemType (ceed , & memtypebackend );
246
+ // Check memtype compatibility
247
+ if (!setmemtyperequest )
248
+ memtyperequested = memtypebackend ;
249
+ else if (!petschavecuda && memtyperequested == CEED_MEM_DEVICE )
250
+ SETERRQ1 (PETSC_COMM_WORLD , PETSC_ERR_SUP_SYS ,
251
+ "PETSc was not built with CUDA. "
252
+ "Requested MemType CEED_MEM_DEVICE is not supported." , NULL );
253
+
219
254
// Print grid information
220
255
PetscInt numP = degree + 1 , numQ = numP + qextra ;
221
256
PetscInt gdofs , odofs ;
222
257
{
258
+ const char * usedresource ;
259
+ CeedGetResource (ceed , & usedresource );
260
+
223
261
int comm_size ;
262
+ ierr = MPI_Comm_size (comm , & comm_size ); CHKERRQ (ierr );
263
+
224
264
ierr = VecGetSize (Q , & gdofs ); CHKERRQ (ierr );
225
265
ierr = VecGetLocalSize (Q , & odofs ); CHKERRQ (ierr );
226
- ierr = MPI_Comm_size (comm , & comm_size ); CHKERRQ (ierr );
266
+ VecType vectype ;
267
+ ierr = VecGetType (Q , & vectype ); CHKERRQ (ierr );
268
+
269
+ PetscInt cStart , cEnd ;
270
+ ierr = DMPlexGetHeightStratum (dm , 0 , & cStart , & cEnd ); CHKERRQ (ierr );
271
+
227
272
if (!test ) {
228
273
ierr = PetscPrintf (comm ,
229
274
"\n-- CEED Shallow-water equations solver on the cubed-sphere -- libCEED + PETSc --\n"
275
+ " rank(s) : %d\n"
276
+ " PETSc:\n"
277
+ " PETSc Vec Type : %s\n"
230
278
" libCEED:\n"
231
279
" libCEED Backend : %s\n"
280
+ " libCEED Backend MemType : %s\n"
281
+ " libCEED User Requested MemType : %s\n"
232
282
" FEM space:\n"
233
- " Number of 1D Basis Nodes (p) : %d\n"
234
- " Number of 1D Quadrature Points (q) : %d\n"
235
- " Global FEM dofs: %D (%D owned) on %d rank(s)\n"
236
- " Local FEM nodes: %D\n" ,
237
- ceedresource , numP , numQ , gdofs , odofs , comm_size , lnodes ); CHKERRQ (ierr );
283
+ " Number of 1D Basis Nodes (P) : %d\n"
284
+ " Number of 1D Quadrature Points (Q) : %d\n"
285
+ " Local Elements : %D\n"
286
+ " Global nodes : %D\n"
287
+ " Owned nodes : %D\n"
288
+ " DoFs per node : %D\n"
289
+ " Global DoFs : %D\n"
290
+ " Owned DoFs : %D\n" ,
291
+ comm_size , ceedresource , usedresource ,
292
+ CeedMemTypes [memtypebackend ],
293
+ (setmemtyperequest ) ?
294
+ CeedMemTypes [memtyperequested ] : "none" , numP , numQ ,
295
+ cEnd - cStart , gdofs /ncompq , odofs /ncompq , ncompq ,
296
+ gdofs , odofs ); CHKERRQ (ierr );
238
297
}
239
298
240
299
}
241
300
242
301
// Set up global mass vector
243
302
ierr = VecDuplicate (Q , & user -> M ); CHKERRQ (ierr );
244
303
245
- // Initialize CEED
246
- CeedInit (ceedresource , & ceed );
247
-
248
304
// Setup libCEED's objects
249
305
ierr = PetscMalloc1 (1 , & ceeddata ); CHKERRQ (ierr );
250
306
ierr = SetupLibceed (dm , ceed , degree , topodim , qextra ,
@@ -287,14 +343,16 @@ int main(int argc, char **argv) {
287
343
& ctxSetup , 0.0 );
288
344
289
345
MPI_Comm_rank (comm , & rank );
290
- if (!rank ) {ierr = PetscMkdir (user -> outputfolder ); CHKERRQ (ierr );}
346
+ if (!rank ) {
347
+ ierr = PetscMkdir (user -> outputfolder ); CHKERRQ (ierr );
348
+ }
291
349
// Gather initial Q values
292
350
// In case of continuation of simulation, set up initial values from binary file
293
351
if (contsteps ) { // continue from existent solution
294
352
PetscViewer viewer ;
295
353
char filepath [PETSC_MAX_PATH_LEN ];
296
354
// Read input
297
- ierr = PetscSNPrintf (filepath , sizeof filepath , "%s/ns -solution.bin" ,
355
+ ierr = PetscSNPrintf (filepath , sizeof filepath , "%s/swe -solution.bin" ,
298
356
user -> outputfolder );
299
357
CHKERRQ (ierr );
300
358
ierr = PetscViewerBinaryOpen (comm , filepath , FILE_MODE_READ , & viewer );
@@ -305,7 +363,7 @@ int main(int argc, char **argv) {
305
363
ierr = DMRestoreLocalVector (dm , & Qloc ); CHKERRQ (ierr );
306
364
307
365
// Set up the MatShell for the associated Jacobian operator
308
- ierr = MatCreateShell (PETSC_COMM_SELF , 3 * odofs , 3 * odofs , PETSC_DETERMINE ,
366
+ ierr = MatCreateShell (PETSC_COMM_SELF , 5 * odofs , 5 * odofs , PETSC_DETERMINE ,
309
367
PETSC_DETERMINE , user , & J ); CHKERRQ (ierr );
310
368
// Set the MatShell user context
311
369
// ierr = MatShellSetContext(J, user); CHKERRQ(ierr);
0 commit comments