dune-fem 2.12-git
Loading...
Searching...
No Matches
petsccommon.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_PETSCCOMMON_HH
2#define DUNE_FEM_PETSCCOMMON_HH
3
4/*
5 * This file should be the first include in every dune-fem-petsc related header
6 * It contains some common code in order to deal with PETSc in a consistent, object oriented
7 * fashion
8 */
9
10#include <string>
11#include <iostream>
12#include <iomanip>
13
17
19
20
21#if HAVE_PETSC
22
23 /*
24 * This turns off PETSc logging of MPI calls. If it is on, PETSc redifines MPI functions as macros,
25 * which breaks some code. E.g. MPI_Allreduce is redefined
26 */
27#define PETSC_HAVE_BROKEN_RECURSIVE_MACRO
28
29#include <petsc.h>
30
31// newer versions of PETSc use PETSC_NULLPTR instread of PETSC_NULL
32#ifndef PETSC_NULLPTR
33#define PETSC_NULLPTR PETSC_NULL
34#endif
35
36namespace Dune
37{
38
39 namespace Petsc
40 {
41 // PETSc #defines VecType to be char* - this can cause problems with e.g. ALUGrid 1.50. So we #undef
42 // it here and use a typedef instead. The following trick is really nasty, but conforming to the standard and
43 // - most important of all - working :)
44 typedef VecType
45#undef VecType
46 VecType;
47
48 /*
49 * exceptions
50 */
51 class Exception : public Dune::Exception {};
52
53 /*
54 * types
55 */
56 typedef ::PetscErrorCode ErrorCode;
57
58
59
60 /* The following 2 methods are needed to make the code work with the CHKERRQ
61 * macro (which we want to use because it can print useful diagnostic output in
62 * case of errors)
63 */
64 inline static ErrorCode ErrorCheckHelper ( ErrorCode errorCode ) { CHKERRQ( errorCode ); return 0; }
65
66 inline ErrorCode ErrorHandler ( MPI_Comm comm, int line, const char *function, const char *file,
67#if PETSC_VERSION_MAJOR <= 3 && PETSC_VERSION_MINOR < 5
68 const char *dir,
69#endif
70 ErrorCode errorCode, PetscErrorType p, const char *message, void *context )
71 {
72 std::ostringstream msgout;
73 msgout << "PETSc Error in the PETSc function '" << function << "' at " << file << ":" << line << ":";
74
75 const char *text;
76 PetscErrorMessage( errorCode, &text, PETSC_NULLPTR );
77 if( text )
78 msgout << " '" << text << "'. Error message: '" << message << "'";
79 else
80 msgout << " Unable to retrieve error text";
81
82 DUNE_THROW( Exception, msgout.str() );
83
84 return errorCode;
85 }
86
87 inline static void ErrorCheck ( ErrorCode errorCode )
88 {
89 if( errorCode )
90 {
91 DUNE_THROW( Exception, "Generic PETSC exception" );
92 }
93 }
94
95 /*
96 * This should be called right after the initialization of the MPI manager. It expects the same arguments
97 * as PetscInitialize
98 */
99 inline bool initialize( const bool verbose, int &argc, char **&args, const char file[] = 0 , const char help[] = 0, bool ownHandler = true )
100 {
101 bool wasInitializedHere = false ;
102 PetscBool petscInitialized = PETSC_FALSE;
103
104 // check whether PETSc had been initialized elsewhere
105 ::PetscInitialized( &petscInitialized );
106
107 if( ! petscInitialized )
108 {
109 ::PetscInitialize( &argc, &args, file, help );
110 wasInitializedHere = true;
111 }
112
113 if( ownHandler )
114 {
115 // set up our error handler
116 if( verbose )
117 {
118 dverb << "INFORMATION: Setting up an own error handler for PETSc errors. If you want the default PETSc handler,\n"
119 << "INFORMATION: set the last argument of Dune::Petsc::initialize(...) to 'false'.\n";
120 }
121 ::PetscPushErrorHandler( &ErrorHandler, 0 );
122 }
123 return wasInitializedHere;
124 }
125
126 /*
127 * This should be called just before the termination of the program.
128 */
129 inline bool finalize ()
130 {
131 // TODO: test here if we are using our own handler
132 ::PetscPopErrorHandler();
133 PetscBool finalized = PETSC_FALSE;
134 ErrorCheck( ::PetscFinalized( &finalized ) );
135
136 if( ! finalized )
137 {
138 ::PetscFinalize();
139 }
140 ErrorCheck( ::PetscFinalized( &finalized ) );
141 return finalized;
142 }
143
144 template <class Comm>
145 inline auto castToPetscComm( const Comm& comm )
146 {
147 // this is needed because Dune::No_Comm or
148 // Dune::Communication< No_Comm > does not cast into MPI_Comm
149 if constexpr ( std::is_same< Dune::No_Comm, Comm > :: value ||
151 {
152 return PETSC_COMM_SELF;
153 }
154 else
155 {
156 return comm;
157 }
158 }
159
160 /*
161 * ==================================================
162 * These are simple mappings to PETSc's C-routines. (Maybe some of them are not needed...).
163 *
164 * The PETSC_VERSION_... customizations are not very well tested yet
165 */
166 template <class Comm>
167 inline static void KSPCreate ( const Comm& comm, KSP *inksp )
168 {
169 ErrorCheck( ::KSPCreate( castToPetscComm( comm ), inksp ) );
170 }
171
172 inline static void KSPDestroy ( KSP *ksp ) {
173#if PETSC_VERSION_MAJOR <= 3 && PETSC_VERSION_MINOR < 2
174 ErrorCheck( ::KSPDestroy( *ksp ) );
175#else
176 ErrorCheck( ::KSPDestroy( ksp ) );
177#endif // PETSC_PETSC_VERSION_MAJOR <= 3 && PETSC_VERSION_MINOR < 2
178 }
179 inline static void KSPGetPC ( KSP ksp, PC *pc ) { ErrorCheck( ::KSPGetPC( ksp, pc ) ); }
180 inline static void KSPSetFromOptions ( KSP ksp ) { ErrorCheck( ::KSPSetFromOptions( ksp ) ); }
181 inline static void KSPSetUp ( KSP ksp ) { ErrorCheck( ::KSPSetUp( ksp ) ); }
182 inline static void KSPSetType ( KSP ksp, const KSPType type ) { ErrorCheck( ::KSPSetType( ksp, type ) ); }
183 inline static void KSPGMRESSetRestart ( KSP ksp, PetscInt restart ) { ErrorCheck( ::KSPGMRESSetRestart( ksp, restart ) ); }
184
185 template <class Comm>
186 inline static void KSPView ( const Comm& comm,
187 KSP ksp,
188 PetscViewer viewer )
189 {
190 ErrorCheck( ::KSPView( ksp, viewer ) );
191 }
192
193 template <class Comm>
194 inline static void KSPView ( const Comm& comm,
195 KSP ksp )
196 {
197 KSPView( comm, ksp, PETSC_VIEWER_STDOUT_( castToPetscComm(comm) ) );
198 }
199
200 inline static void KSPMonitorSet (KSP ksp, PetscErrorCode (*monitor)(KSP,PetscInt,PetscReal,void*),
201#if PETSC_VERSION_MAJOR <= 3 && PETSC_VERSION_MINOR < 2
202 void *mctx,PetscErrorCode (*monitordestroy)(void*)
203#elif PETSC_VERSION_MAJOR >=3 && PETSC_VERSION_MINOR < 25 // PETSc 3.2 to 3.24
204 void *mctx,PetscErrorCode (*monitordestroy)(void**)
205#else // PETSc 3.25 and beyond
206 PetscCtx mctx, PetscCtxDestroyFn *monitordestroy
207#endif
208 )
209 {
210 ErrorCheck( ::KSPMonitorSet( ksp, monitor, mctx, monitordestroy ) );
211 }
212 inline static void KSPGetIterationNumber( KSP ksp, PetscInt* its )
213 { ErrorCheck( ::KSPGetIterationNumber( ksp, its ) ); }
214 inline static void KSPGetConvergedReason(KSP ksp, KSPConvergedReason *reason)
215 { ErrorCheck( ::KSPGetConvergedReason( ksp, reason ) ); }
216
217
218#if PETSC_VERSION_MAJOR <= 3 && PETSC_VERSION_MINOR < 5
219 inline static void KSPSetOperators (KSP ksp, Mat Amat, Mat Pmat, MatStructure flag ) { ErrorCheck( ::KSPSetOperators( ksp, Amat, Pmat, flag ) ); }
220#else
221 inline static void KSPSetOperators (KSP ksp, Mat Amat, Mat Pmat ) { ErrorCheck( ::KSPSetOperators( ksp, Amat, Pmat ) ); }
222#endif
223 inline static void KSPSetTolerances ( KSP ksp, PetscReal rtol, PetscReal abstol, PetscReal dtol, PetscInt maxits )
224 { ErrorCheck( ::KSPSetTolerances( ksp, rtol, abstol, dtol, maxits ) ); }
225 inline static void KSPSetInitialGuessNonzero( KSP ksp, PetscBool flg ) { ErrorCheck( ::KSPSetInitialGuessNonzero( ksp, flg ) ); };
226 inline static void KSPSetInitialGuessKnoll( KSP ksp, PetscBool flg ) { ErrorCheck( ::KSPSetInitialGuessKnoll( ksp, flg ) ); };
227 inline static void KSPSolve ( KSP ksp, Vec b, Vec x ) { ErrorCheck( ::KSPSolve( ksp, b, x ) ); }
228 inline static void KSPSetPC ( KSP ksp, PC pc ) { ErrorCheck( ::KSPSetPC( ksp, pc ) ); }
229
230 // preconditioning
231 template <class Comm>
232 inline static void PCCreate ( const Comm& comm, PC* pc)
233 {
234 ErrorCheck( ::PCCreate( castToPetscComm( comm ), pc ) );
235 }
236
237 inline static void PCDestroy ( PC* pc) {
238#if PETSC_VERSION_MAJOR <= 3 && PETSC_VERSION_MINOR < 2
239 ErrorCheck( ::PCDestroy( *pc ) );
240#else
241 ErrorCheck( ::PCDestroy( pc ) );
242#endif
243 }
244 inline static void PCSetType ( PC pc, const PCType type ) { ErrorCheck( ::PCSetType( pc, type ) ); }
245 inline static void PCSetFromOptions ( PC pc ) { ErrorCheck( ::PCSetFromOptions( pc ) ); }
246 inline static void PCSetUp ( PC pc ) { ErrorCheck( ::PCSetUp( pc ) ); }
247 inline static void PCFactorSetLevels( PC pc, PetscInt level ) { ErrorCheck( ::PCFactorSetLevels( pc, level ) ); }
248 inline static void PCSORSetOmega( PC pc, PetscReal omega ) { ErrorCheck( ::PCSORSetOmega( pc, omega ) ); }
249 inline static void PCSORSetSymmetric( PC pc, MatSORType flag ) { ErrorCheck( ::PCSORSetSymmetric( pc, flag ) ); }
250#if PETSC_VERSION_MAJOR <= 3 && PETSC_VERSION_MINOR < 9
251 inline static void PCFactorSetMatSolverPackage( PC pc, const MatSolverPackage type )
252 {
253 ErrorCheck( ::PCFactorSetMatSolverPackage( pc, type ) );
254 }
255#else
256 inline static void PCFactorSetMatSolverPackage( PC pc, const MatSolverType type )
257 {
258 ErrorCheck( ::PCFactorSetMatSolverType( pc, type ) );
259 }
260#endif
261 inline static void PCHYPRESetType( PC pc, const char* type )
262 {
263 ErrorCheck( ::PCHYPRESetType( pc, type ) );
264 }
265
266 // matrix routines
267 inline static void MatAssemblyBegin ( Mat mat, MatAssemblyType type ) { ErrorCheck( ::MatAssemblyBegin( mat, type ) ); }
268 inline static void MatAssemblyEnd ( Mat mat, MatAssemblyType type ) { ErrorCheck( ::MatAssemblyEnd( mat, type ) ); }
269 inline static void MatAssembled( Mat mat, PetscBool* assembled ) { ErrorCheck( ::MatAssembled( mat, assembled ) ); }
270
271 template <class Comm>
272 inline static void MatCreate ( const Comm& comm, Mat *A )
273 {
274 ErrorCheck( ::MatCreate( castToPetscComm( comm ), A) );
275 }
276
277 template <class Comm>
278 inline static void MatCreateBlockMat ( const Comm& comm,
279 Mat *A,
280 PetscInt m, PetscInt n, PetscInt bs, PetscInt nz, PetscInt* nnz )
281 {
282 ErrorCheck( ::MatCreateBlockMat( castToPetscComm( comm ), n, m, bs, nz, nnz, A) );
283 }
284 inline static void MatDestroy ( Mat *A ) {
285 #if PETSC_VERSION_MAJOR <= 3 && PETSC_VERSION_MINOR < 2
286 ErrorCheck( ::MatDestroy( *A ) );
287 #else
288 ErrorCheck( ::MatDestroy( A ) );
289 #endif // PETSC_PETSC_VERSION_MAJOR <= 3 && PETSC_VERSION_MINOR < 2
290 }
291 inline static void MatSetUp( Mat mat )
292 {
293 ErrorCheck( ::MatSetUp(mat));
294 }
295 inline static void MatSetUp( Mat mat, PetscInt bs, PetscInt nz )
296 {
297 if (bs == 1)
298 {
299 ErrorCheck( ::MatSeqAIJSetPreallocation(mat,nz,PETSC_NULLPTR) );
300 ErrorCheck( ::MatMPIAIJSetPreallocation(mat,nz,PETSC_NULLPTR,nz/2,PETSC_NULLPTR) );
301 }
302 else
303 {
304 ErrorCheck( ::MatSeqBAIJSetPreallocation(mat,bs,nz,PETSC_NULLPTR) );
305 ErrorCheck( ::MatMPIBAIJSetPreallocation(mat,bs,nz,PETSC_NULLPTR,nz/2,PETSC_NULLPTR) );
306 }
307 // the following seems not to work for block matrix
308 ErrorCheck( ::MatSetOption(mat, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE) );
309
310 // when using MatZeroRows only set entries to zero
311 // and do not change the sparsity pattern
312 ErrorCheck( ::MatSetOption(mat, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE) );
313
314 // the following only works for block matrix
315 // but should be used with MAT_NEW_NONZERO_LOCATIONS...
316 // ErrorCheck( ::MatSetOption(mat, MAT_USE_HASH_TABLE,PETSC_FALSE) );
317 ErrorCheck( ::MatSetUp(mat));
318 }
319 inline static void MatSetUp( Mat mat, PetscInt bs, const PetscInt *d_nnz, const PetscInt *o_nnz )
320 {
321 if (bs == 1)
322 {
323 ErrorCheck( ::MatSeqAIJSetPreallocation(mat,0,d_nnz ) );
324 ErrorCheck( ::MatMPIAIJSetPreallocation(mat,0,d_nnz,5,o_nnz) );
325 }
326 else
327 {
328 ErrorCheck( ::MatSeqBAIJSetPreallocation(mat,bs,0,d_nnz ) );
329 ErrorCheck( ::MatMPIBAIJSetPreallocation(mat,bs,0,d_nnz,5,PETSC_NULLPTR) );
330 }
331 // see previous comments
332 ErrorCheck( ::MatSetOption(mat, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE) );
333 ErrorCheck( ::MatSetUp(mat));
334 }
335
336 inline static void MatGetOwnershipRange ( Mat mat, PetscInt *m, PetscInt* n ) { ErrorCheck( ::MatGetOwnershipRange( mat, m, n ) ); }
337 inline static void MatGetSize ( Mat mat, PetscInt *m, PetscInt* n ) { ErrorCheck( ::MatGetSize( mat, m, n ) ); }
338 inline static void MatMult ( Mat mat, Vec x, Vec y ) { ErrorCheck( ::MatMult( mat, x, y ) ); }
339 inline static void MatSetBlockSize ( Mat A, PetscInt bs ) { ErrorCheck( ::MatSetBlockSize( A, bs ) ); }
340 inline static void MatSetSizes ( Mat A, PetscInt m, PetscInt n, PetscInt M, PetscInt N ) { ErrorCheck( ::MatSetSizes( A, m, n, M, N ) ); }
341 inline static void MatSetFromOptions ( Mat B ) { ErrorCheck( ::MatSetFromOptions( B ) ); }
342 inline static void MatSetType ( Mat mat, const MatType matype ) { ErrorCheck( ::MatSetType( mat, matype ) ); }
343
344 inline static void MatSetValue ( Mat v, PetscInt i, PetscInt j, PetscScalar va, InsertMode mode )
345 {
346#ifdef _OPENMP
347#pragma omp critical
348#endif
349 {
350 ErrorCheck( ::MatSetValue( v, i, j, va, mode ) );
351 }
352 }
353
354 inline static void MatSetValues ( Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], const PetscScalar v[], InsertMode addv )
355 {
356#ifdef _OPENMP
357#pragma omp critical
358#endif
359 {
360 ErrorCheck( ::MatSetValues( mat, m, idxm, n, idxn, v, addv ) );
361 }
362 }
363
364 inline static void MatSetValuesBlocked ( Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], const PetscScalar v[], InsertMode addv )
365 {
366#ifdef _OPENMP
367#pragma omp critical
368#endif
369 {
370 ErrorCheck( ::MatSetValuesBlocked( mat, m, idxm, n, idxn, v, addv ) );
371 }
372 }
373
374 inline static void MatGetValues ( Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], PetscScalar v[] )
375 { ErrorCheck( ::MatGetValues( mat, m, idxm, n, idxn, v ) ); }
376
377 inline static void MatZeroRows ( Mat mat, PetscInt m, const PetscInt idxm[], const PetscScalar v )
378 {
379#ifdef _OPENMP
380#pragma omp critical
381#endif
382 {
383 ErrorCheck( ::MatZeroRows( mat, m, idxm, v, 0, 0 ) );
384 }
385 }
386
387 inline static void MatView ( Mat mat, PetscViewer viewer ) { ErrorCheck( ::MatView( mat, viewer ) ); }
388 inline static void MatZeroEntries ( Mat mat )
389 {
390#ifdef _OPENMP
391#pragma omp critical
392#endif
393 {
394 ErrorCheck( ::MatZeroEntries( mat ) );
395 }
396 }
397
398 inline static void PetscOptionsSetValue(const std::string& key, const std::string& value)
399 {
400 // add parameter to global data base (nullptr)
401 // TODO, add PetscOptions object to have several data bases
402 ErrorCheck( ::PetscOptionsSetValue(PETSC_NULLPTR, key.c_str(), value.c_str() ));
403 }
404
405 inline static void PetscOptionsClear()
406 {
407 // clear global data base (nullptr)
408 // TODO, add PetscOptions object to have several data bases
409 ErrorCheck( ::PetscOptionsClear(PETSC_NULLPTR) );
410 }
411
412 inline static void PetscBarrier ( PetscObject obj ) { ErrorCheck( ::PetscBarrier( obj ) ); }
413 inline static void PetscFinalize () { ErrorCheck( ::PetscFinalize() ); }
414 inline static void PetscInitialize( int *argc, char ***args, const char file[], const char help[] ) { ErrorCheck( ::PetscInitialize( argc, args, file, help ) ); }
415 inline static void PetscViewerASCIIOpen ( MPI_Comm comm, const char name[], PetscViewer *lab ) { ErrorCheck( ::PetscViewerASCIIOpen( comm, name, lab ) ); }
416 inline static void PetscViewerDestroy ( PetscViewer *viewer ) {
417 #if PETSC_VERSION_MAJOR <= 3 && PETSC_VERSION_MINOR < 2
418 ErrorCheck( ::PetscViewerDestroy( *viewer ) );
419 #else
420 ErrorCheck( ::PetscViewerDestroy( viewer ) );
421 #endif
422 }
423 inline static void PetscViewerSetFormat ( PetscViewer viewer, PetscViewerFormat format )
424 {
425 #if PETSC_VERSION_MAJOR <= 3 && PETSC_VERSION_MINOR < 5
426 ErrorCheck( ::PetscViewerSetFormat( viewer, format ) );
427 #else
428 ErrorCheck( ::PetscViewerPushFormat( viewer, format ) );
429 #endif
430 }
431 inline static void VecAssemblyBegin ( Vec vec ) { ErrorCheck( ::VecAssemblyBegin( vec ) ); }
432 inline static void VecAssemblyEnd ( Vec vec ) { ErrorCheck( ::VecAssemblyEnd( vec ) ); }
433 inline static void VecAXPY ( Vec y, PetscScalar alpha, Vec x) { ErrorCheck( ::VecAXPY( y, alpha, x ) ); }
434 inline static void VecCopy ( Vec x, Vec y ) { ErrorCheck( ::VecCopy( x, y ) ); }
435
436 template <class Comm>
437 inline static void VecCreate ( const Comm& comm, Vec *vec )
438 {
439 ErrorCheck( ::VecCreate( castToPetscComm( comm ), vec ) );
440 }
441
442 template <class Comm>
443 inline static void VecCreateGhost ( const Comm& comm, PetscInt n, PetscInt N, PetscInt nghost, const PetscInt ghosts[], Vec *vv )
444 { ErrorCheck( ::VecCreateGhost( castToPetscComm( comm ), n, N, nghost, ghosts, vv ) ); }
445
446 template <class Comm>
447 inline static void VecCreateGhostBlock ( const Comm& comm, PetscInt bs, PetscInt n, PetscInt N, PetscInt nghost, const PetscInt ghosts[], Vec *vv )
448 {
449#if PETSC_VERSION_MAJOR < 3 || (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR <= 2)
450 std::unique_ptr< PetscInt[] > ghostsCopy( new PetscInt[ nghost * bs ] );
451 for( PetscInt i = 0; i < nghost; ++i )
452 {
453 for( PetscInt j = 0; j < bs; ++j )
454 ghostsCopy[ i*bs + j ] = ghosts[ i ]*bs + j;
455 }
456 VecCreateGhost( n, N, nghost * bs, ghostsCopy.get(), vv );
457#else // #if PETSC_VERSION_MAJOR < 3 || (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR <= 2)
458 ErrorCheck( ::VecCreateGhostBlock( castToPetscComm( comm ), bs, n, N, nghost, ghosts, vv ) );
459#endif // #else // #if PETSC_VERSION_MAJOR < 3 || (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR <= 2)
460 }
461
462 inline static void VecDestroy ( Vec *v ) {
463 #if PETSC_VERSION_MAJOR <= 3 && PETSC_VERSION_MINOR < 2
464 ErrorCheck( ::VecDestroy( *v ) );
465 #else
466 ErrorCheck( ::VecDestroy( v ) );
467 #endif // PETSC_PETSC_VERSION_MAJOR <= 3 && PETSC_VERSION_MINOR < 2
468 }
469 inline static void VecDot ( Vec x, Vec y, PetscScalar *val ) { ErrorCheck( ::VecDot( x, y, val ) ); }
470 inline static void VecDuplicate ( Vec v, Vec *newv ) { ErrorCheck( ::VecDuplicate( v, newv ) ); }
471 inline static void VecGetBlockSize ( Vec x, PetscInt* bs ) { ErrorCheck( ::VecGetBlockSize( x, bs ) ); }
472 inline static void VecGetLocalSize ( Vec x, PetscInt *size ) { ErrorCheck( ::VecGetLocalSize( x, size ) ); }
473 inline static void VecGetOwnershipRange ( Vec x, PetscInt *low, PetscInt *high ) { ErrorCheck( ::VecGetOwnershipRange( x, low, high ) ); }
474 inline static void VecGetSize ( Vec x, PetscInt *size ) { ErrorCheck( ::VecGetSize( x, size ) ); }
475 inline static void VecGetValues ( Vec x, PetscInt ni, const PetscInt ix[], PetscScalar y[] ) { ErrorCheck( ::VecGetValues( x, ni, ix, y ) ); }
476 inline static void VecGhostGetLocalForm ( Vec g, Vec *l ) { ErrorCheck( ::VecGhostGetLocalForm( g, l ) ); }
477 inline static void VecGhostRestoreLocalForm ( Vec g, Vec *l ) { ErrorCheck( ::VecGhostRestoreLocalForm( g, l ) ); }
478 inline static void VecGhostUpdateBegin ( Vec g, InsertMode insertmode, ScatterMode scattermode ) { ErrorCheck( ::VecGhostUpdateBegin( g, insertmode, scattermode ) ); }
479 inline static void VecGhostUpdateEnd ( Vec g, InsertMode insertmode, ScatterMode scattermode ) { ErrorCheck( ::VecGhostUpdateEnd( g, insertmode, scattermode ) ); }
480 inline static void VecNorm ( Vec x, NormType type, PetscReal *val ) { ErrorCheck( ::VecNorm( x, type, val ) ); }
481 inline static void VecScale ( Vec x, PetscScalar alpha ) { ErrorCheck( ::VecScale( x, alpha ) ); }
482 inline static void VecSet ( Vec x, PetscScalar alpha ) { ErrorCheck( ::VecSet( x, alpha ) ); }
483 inline static void VecSetBlockSize ( Vec x, PetscInt bs ) { ErrorCheck( ::VecSetBlockSize( x, bs ) ); }
484 inline static void VecSetFromOptions ( Vec vec ) { ErrorCheck( ::VecSetFromOptions( vec ) ); }
485 inline static void VecSetType ( Vec vec, const VecType method ) { ErrorCheck( ::VecSetType( vec, method ) ); }
486 inline static void VecGetType ( Vec vec, VecType* method ) { ErrorCheck( ::VecGetType( vec, method ) ); }
487 inline static void VecSetSizes ( Vec v, PetscInt n, PetscInt N ) { ErrorCheck( ::VecSetSizes( v, n, N ) ); }
488 inline static void VecSetValue ( Vec v, int row, PetscScalar value, InsertMode mode ) { ErrorCheck( ::VecSetValue( v, row, value, mode ) ); }
489 inline static void VecSetValuesBlocked ( Vec v, PetscInt ni, const PetscInt xi[], const PetscScalar values[], InsertMode mode ) { ErrorCheck( ::VecSetValuesBlocked( v, ni, xi, values, mode ) ); }
490 inline static void VecView ( Vec vec, PetscViewer viewer ) { ErrorCheck( ::VecView( vec, viewer ) ); }
491
492 /* ========================================
493 * class Backend
494 *
495 * This class defined different backends for Petsc Matrices and Vectors.
496 * The backend id allows to convert back and forth to Petsc VecType and
497 * MatType.
498 */
499 class Backend
500 {
501 public:
503 block = 0, // MATSEQBAIJ and MATMPIBAIJ as well as MATBAIJ | VECSEQ and VECMPI as well as VECSTANDARD
504 scalar = 1, // MATSEQAIJ and MATMPIAIJ as well as MATAIJ | VECSEQ and VECMPI as well as VECSTANDARD
505 kokkos = 2, // MATSEQAIJKOKKOS and MATMPIAIJKOKKOS as well as MATAIJKOKKOS | VECSEQKOKKOS and VECMPIKOKKOS as well as VECKOKKOS
506 cuda = 3 // MATSEQAIJCUDA and MATMPIAIJCUDA as well as MATAIJCUDA | VECSEQCUDA and VECMPICUDA as well as VECCUDA
507 );
508
510 static const int defaultBackend = block;
511
513 static VecType toVecType( const int id )
514 {
515 switch (id)
516 {
517 case kokkos:
518 return VECKOKKOS;
519 case cuda:
520#if PETSC_VERSION_MAJOR <= 3 && PETSC_VERSION_MINOR >= 20
521 return VECCUDA;
522#else
523 DUNE_THROW(NotImplemented,"Update PETSc version to >= 3.20.x");
524 return VECSTANDARD;
525#endif
526 default: // black and scalar
527 return VECSTANDARD;
528 }
529 }
530
532 static MatType toMatType( const int id )
533 {
534 switch (id)
535 {
536 case block:
537 return MATBAIJ;
538 case scalar:
539 return MATAIJ;
540 case kokkos:
541 return MATAIJKOKKOS;
542#if PETSC_VERSION_MAJOR <= 3 && PETSC_VERSION_MINOR >= 20
543 case cuda:
544 return MATSELLCUDA;
545#else
546 DUNE_THROW(NotImplemented,"Update PETSc version to >= 3.20.x");
547 return MATBAIJ;
548#endif
549 default:
550 return MATBAIJ;
551 }
552 }
553
555 static int toVecId( VecType vectype )
556 {
557 std::string t( vectype );
558 std::size_t notFound = (-1);
559 if ( t.find("kokkos") != notFound )
560 return kokkos;
561 else if ( t.find("cuda") != notFound )
562 return cuda;
563 else
564 return block;
565 }
566
568 static int toMatId( MatType mattype )
569 {
570 std::string t( mattype );
571 const std::size_t notFound = (-1);
572 if ( t.find("kokkos") != notFound )
573 return kokkos;
574 else if ( t.find("cuda") != notFound )
575 return cuda;
576 else if ( t.find("baij") != notFound )
577 return block;
578 else
579 return scalar;
580 }
581
583 static const std::vector< std::string >& availableBackend()
584 {
585 static std::vector< std::string > mthds;
586 if ( mthds.empty() )
587 {
588 for ( int i=0; i<=cuda; ++i )
589 {
590 mthds.push_back( BT::to_string( i ) );
591 }
592 }
593 return mthds;
594 }
595 };
596
597
598 } // namespace Petsc
599
600} // namespace Dune
601
602#endif // #if HAVE_PETSC
603
604#endif // DUNE_FEM_PETSCCOMMON_HH
const char * name()
double alpha() const
int size() const
static constexpr size_type M()
static constexpr size_type N()
Exception()
void message(const std::string &msg)
#define DUNE_THROW(E,...)
DVerbType dverb
const Communication & comm() const
#define LIST_OF_INT_FORWARDED(ListName,...)
Definition staticlistofint.hh:71
T c_str(T... args)
T empty(T... args)
T push_back(T... args)
T str(T... args)