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
218//
319// 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 ).
723//
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.
1025//
1126// Build with:
1227//
2742
2843const char help [] = "Solve the shallow-water equations using PETSc and libCEED\n" ;
2944
30- #include <petscts.h>
31- #include <petscdmplex.h>
32- #include <ceed.h>
33- #include <stdbool.h>
34- #include <petscsys.h>
3545#include "sw_headers.h"
3646
3747int main (int argc , char * * argv ) {
@@ -57,11 +67,21 @@ int main(int argc, char **argv) {
5767 filename [PETSC_MAX_PATH_LEN ];
5868 Ceed ceed ;
5969 CeedData ceeddata ;
70+ CeedMemType memtyperequested ;
6071 PetscScalar meter = 1e-2 ; // 1 meter in scaled length units
6172 PetscScalar second = 1e-2 ; // 1 second in scaled time units
6273 PetscScalar f = 0.0001 ; // mid-latitude Coriolis parameter
6374 PetscScalar g = 9.81 ; // m/s^2
6475 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*
6585 // Performance context
6686 double start , cpu_time_used ;
6787
@@ -73,10 +93,6 @@ int main(int argc, char **argv) {
7393 ierr = PetscMalloc1 (1 , & user ); CHKERRQ (ierr );
7494 ierr = PetscMalloc1 (1 , & units ); CHKERRQ (ierr );
7595
76- // Set up problem type command line option
77- //PetscFunctionListAdd(&icsflist, "sphere", &ICsSW);
78- //PetscFunctionListAdd(&qflist, "shallow-water", &SW);
79-
8096 // Parse command line options
8197 comm = PETSC_COMM_WORLD ;
8298 ierr = PetscOptionsBegin (comm , NULL , "Shallow-water equations in PETSc with libCEED" ,
@@ -123,6 +139,12 @@ int main(int argc, char **argv) {
123139 simplex = PETSC_FALSE ;
124140 ierr = PetscOptionsBool ("-simplex" , "Use simplices, or tensor product cells" ,
125141 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 );
126148 ierr = PetscOptionsEnd (); CHKERRQ (ierr );
127149
128150 // Define derived units
@@ -216,35 +238,69 @@ int main(int argc, char **argv) {
216238 ierr = VecGetSize (Qloc , & lnodes ); CHKERRQ (ierr );
217239 lnodes /= ncompq ;
218240
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+
219254 // Print grid information
220255 PetscInt numP = degree + 1 , numQ = numP + qextra ;
221256 PetscInt gdofs , odofs ;
222257 {
258+ const char * usedresource ;
259+ CeedGetResource (ceed , & usedresource );
260+
223261 int comm_size ;
262+ ierr = MPI_Comm_size (comm , & comm_size ); CHKERRQ (ierr );
263+
224264 ierr = VecGetSize (Q , & gdofs ); CHKERRQ (ierr );
225265 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+
227272 if (!test ) {
228273 ierr = PetscPrintf (comm ,
229274 "\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"
230278 " libCEED:\n"
231279 " libCEED Backend : %s\n"
280+ " libCEED Backend MemType : %s\n"
281+ " libCEED User Requested MemType : %s\n"
232282 " 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 );
238297 }
239298
240299 }
241300
242301 // Set up global mass vector
243302 ierr = VecDuplicate (Q , & user -> M ); CHKERRQ (ierr );
244303
245- // Initialize CEED
246- CeedInit (ceedresource , & ceed );
247-
248304 // Setup libCEED's objects
249305 ierr = PetscMalloc1 (1 , & ceeddata ); CHKERRQ (ierr );
250306 ierr = SetupLibceed (dm , ceed , degree , topodim , qextra ,
@@ -287,14 +343,16 @@ int main(int argc, char **argv) {
287343 & ctxSetup , 0.0 );
288344
289345 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+ }
291349 // Gather initial Q values
292350 // In case of continuation of simulation, set up initial values from binary file
293351 if (contsteps ) { // continue from existent solution
294352 PetscViewer viewer ;
295353 char filepath [PETSC_MAX_PATH_LEN ];
296354 // Read input
297- ierr = PetscSNPrintf (filepath , sizeof filepath , "%s/ns -solution.bin" ,
355+ ierr = PetscSNPrintf (filepath , sizeof filepath , "%s/swe -solution.bin" ,
298356 user -> outputfolder );
299357 CHKERRQ (ierr );
300358 ierr = PetscViewerBinaryOpen (comm , filepath , FILE_MODE_READ , & viewer );
@@ -305,7 +363,7 @@ int main(int argc, char **argv) {
305363 ierr = DMRestoreLocalVector (dm , & Qloc ); CHKERRQ (ierr );
306364
307365 // 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 ,
309367 PETSC_DETERMINE , user , & J ); CHKERRQ (ierr );
310368 // Set the MatShell user context
311369// ierr = MatShellSetContext(J, user); CHKERRQ(ierr);
0 commit comments