-
Notifications
You must be signed in to change notification settings - Fork 51
/
Cabana_Grid_HypreStructuredSolver.hpp
1611 lines (1407 loc) · 54.3 KB
/
Cabana_Grid_HypreStructuredSolver.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
* Copyright (c) 2018-2023 by the Cabana authors *
* All rights reserved. *
* *
* This file is part of the Cabana library. Cabana is distributed under a *
* BSD 3-clause license. For the licensing terms see the LICENSE file in *
* the top-level directory. *
* *
* SPDX-License-Identifier: BSD-3-Clause *
****************************************************************************/
/*!
\file Cabana_Grid_HypreStructuredSolver.hpp
\brief HYPRE structured solver interface
*/
#ifndef CABANA_GRID_HYPRESTRUCTUREDSOLVER_HPP
#define CABANA_GRID_HYPRESTRUCTUREDSOLVER_HPP
#include <Cabana_Grid_Array.hpp>
#include <Cabana_Grid_GlobalGrid.hpp>
#include <Cabana_Grid_Hypre.hpp>
#include <Cabana_Grid_IndexSpace.hpp>
#include <Cabana_Grid_LocalGrid.hpp>
#include <Cabana_Grid_Types.hpp>
#include <Cabana_Utils.hpp> // FIXME: remove after next release.
#include <HYPRE_config.h>
#include <HYPRE_struct_ls.h>
#include <HYPRE_struct_mv.h>
#include <Kokkos_Core.hpp>
#include <Kokkos_Profiling_ScopedRegion.hpp>
#include <array>
#include <memory>
#include <numeric>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>
namespace Cabana
{
namespace Grid
{
//---------------------------------------------------------------------------//
//! Hypre structured solver interface for scalar fields.
template <class Scalar, class EntityType, class MemorySpace>
class HypreStructuredSolver
{
public:
//! Entity type.
using entity_type = EntityType;
//! Kokkos memory space..
using memory_space = MemorySpace;
//! Scalar value type.
using value_type = Scalar;
//! Hypre memory space compatibility check.
static_assert( HypreIsCompatibleWithMemorySpace<memory_space>::value,
"HYPRE not compatible with solver memory space" );
/*!
\brief Constructor.
\param layout The array layout defining the vector space of the solver.
\param is_preconditioner Flag indicating if this solver will be used as
a preconditioner.
*/
template <class ArrayLayout_t>
HypreStructuredSolver( const ArrayLayout_t& layout,
const bool is_preconditioner = false )
: _comm( layout.localGrid()->globalGrid().comm() )
, _is_preconditioner( is_preconditioner )
{
static_assert( is_array_layout<ArrayLayout_t>::value,
"Must use an array layout" );
static_assert(
std::is_same<typename ArrayLayout_t::entity_type,
entity_type>::value,
"Array layout entity type mush match solver entity type" );
// Spatial dimension.
const std::size_t num_space_dim = ArrayLayout_t::num_space_dim;
// Only create data structures if this is not a preconditioner.
if ( !_is_preconditioner )
{
// Create the grid.
auto error = HYPRE_StructGridCreate( _comm, num_space_dim, &_grid );
checkHypreError( error );
// Get the global index space spanned by the local grid on this
// rank. Note that the upper bound is not a bound but rather the
// last index as this is what Hypre wants. Note that we reordered
// this to KJI from IJK to be consistent with HYPRE ordering. By
// setting up the grid like this, HYPRE will then want layout-right
// data indexed as (i,j,k) or (i,j,k,l) which will allow us to
// directly use Kokkos::deep_copy to move data between Cajita arrays
// and HYPRE data structures.
auto global_space = layout.indexSpace( Own(), Global() );
_lower.resize( num_space_dim );
_upper.resize( num_space_dim );
for ( std::size_t d = 0; d < num_space_dim; ++d )
{
_lower[d] = static_cast<HYPRE_Int>(
global_space.min( num_space_dim - d - 1 ) );
_upper[d] = static_cast<HYPRE_Int>(
global_space.max( num_space_dim - d - 1 ) - 1 );
}
error = HYPRE_StructGridSetExtents( _grid, _lower.data(),
_upper.data() );
checkHypreError( error );
// Get periodicity. Note we invert the order of this to KJI as well.
const auto& global_grid = layout.localGrid()->globalGrid();
HYPRE_Int periodic[num_space_dim];
for ( std::size_t d = 0; d < num_space_dim; ++d )
periodic[num_space_dim - 1 - d] =
global_grid.isPeriodic( d )
? layout.localGrid()->globalGrid().globalNumEntity(
EntityType(), d )
: 0;
error = HYPRE_StructGridSetPeriodic( _grid, periodic );
checkHypreError( error );
// Assemble the grid.
error = HYPRE_StructGridAssemble( _grid );
checkHypreError( error );
// Allocate LHS and RHS vectors and initialize to zero. Note that we
// are fixing the views under these vectors to layout-right.
std::array<long, num_space_dim> reorder_size;
for ( std::size_t d = 0; d < num_space_dim; ++d )
{
reorder_size[d] = global_space.extent( d );
}
IndexSpace<num_space_dim> reorder_space( reorder_size );
auto vector_values =
createView<HYPRE_Complex, Kokkos::LayoutRight, memory_space>(
"vector_values", reorder_space );
Kokkos::deep_copy( vector_values, 0.0 );
error = HYPRE_StructVectorCreate( _comm, _grid, &_b );
checkHypreError( error );
error = HYPRE_StructVectorInitialize( _b );
checkHypreError( error );
error = HYPRE_StructVectorSetBoxValues(
_b, _lower.data(), _upper.data(), vector_values.data() );
checkHypreError( error );
error = HYPRE_StructVectorAssemble( _b );
checkHypreError( error );
error = HYPRE_StructVectorCreate( _comm, _grid, &_x );
checkHypreError( error );
error = HYPRE_StructVectorInitialize( _x );
checkHypreError( error );
error = HYPRE_StructVectorSetBoxValues(
_x, _lower.data(), _upper.data(), vector_values.data() );
checkHypreError( error );
error = HYPRE_StructVectorAssemble( _x );
checkHypreError( error );
}
}
// Destructor.
virtual ~HypreStructuredSolver()
{
// We only make data if this is not a preconditioner.
if ( !_is_preconditioner )
{
HYPRE_StructVectorDestroy( _x );
HYPRE_StructVectorDestroy( _b );
HYPRE_StructMatrixDestroy( _A );
HYPRE_StructStencilDestroy( _stencil );
HYPRE_StructGridDestroy( _grid );
}
}
//! Return if this solver is a preconditioner.
bool isPreconditioner() const { return _is_preconditioner; }
/*!
\brief Set the operator stencil.
\param stencil The (i,j,k) offsets describing the structured matrix
entries at each grid point. Offsets are defined relative to an index.
\param is_symmetric If true the matrix is designated as symmetric. The
stencil entries should only contain one entry from each symmetric
component if this is true.
*/
template <std::size_t NumSpaceDim>
void
setMatrixStencil( const std::vector<std::array<int, NumSpaceDim>>& stencil,
const bool is_symmetric = false )
{
// This function is only valid for non-preconditioners.
if ( _is_preconditioner )
throw std::logic_error(
"Cannot call setMatrixStencil() on preconditioners" );
// Create the stencil.
_stencil_size = stencil.size();
auto error =
HYPRE_StructStencilCreate( NumSpaceDim, _stencil_size, &_stencil );
checkHypreError( error );
std::array<HYPRE_Int, NumSpaceDim> offset;
for ( unsigned n = 0; n < stencil.size(); ++n )
{
for ( std::size_t d = 0; d < NumSpaceDim; ++d )
offset[d] = stencil[n][d];
error = HYPRE_StructStencilSetElement( _stencil, n, offset.data() );
checkHypreError( error );
}
// Create the matrix object. Must be done after the stencil is setup
error = HYPRE_StructMatrixCreate( _comm, _grid, _stencil, &_A );
checkHypreError( error );
error = HYPRE_StructMatrixSetSymmetric( _A, is_symmetric );
checkHypreError( error );
error = HYPRE_StructMatrixInitialize( _A );
checkHypreError( error );
}
/*!
\brief Set the matrix values.
\param values The matrix entry values. For each entity over which the
vector space is defined an entry for each stencil element is
required. The order of the stencil elements is that same as that in the
stencil definition. Note that values corresponding to stencil entries
outside of the domain should be set to zero.
*/
template <class Array_t>
void setMatrixValues( const Array_t& values )
{
static_assert( is_array<Array_t>::value, "Must use an array" );
static_assert(
std::is_same<typename Array_t::entity_type, entity_type>::value,
"Array entity type mush match solver entity type" );
static_assert(
std::is_same<typename Array_t::memory_space, MemorySpace>::value,
"Array memory space and solver memory space are different." );
static_assert(
std::is_same<typename Array_t::value_type, value_type>::value,
"Array value type and solver value type are different." );
// This function is only valid for non-preconditioners.
if ( _is_preconditioner )
throw std::logic_error(
"Cannot call setMatrixValues() on preconditioners" );
if ( values.layout()->dofsPerEntity() !=
static_cast<int>( _stencil_size ) )
throw std::runtime_error(
"Number of matrix values does not match stencil size" );
// Spatial dimension.
const std::size_t num_space_dim = Array_t::num_space_dim;
// Copy the matrix entries into HYPRE. The HYPRE layout is fixed as
// layout-right.
auto owned_space = values.layout()->indexSpace( Own(), Local() );
std::array<long, num_space_dim + 1> reorder_size;
for ( std::size_t d = 0; d < num_space_dim; ++d )
{
reorder_size[d] = owned_space.extent( d );
}
reorder_size.back() = _stencil_size;
IndexSpace<num_space_dim + 1> reorder_space( reorder_size );
auto a_values =
createView<HYPRE_Complex, Kokkos::LayoutRight, memory_space>(
"a_values", reorder_space );
auto values_subv = createSubview( values.view(), owned_space );
Kokkos::deep_copy( a_values, values_subv );
// Insert values into the HYPRE matrix.
std::vector<HYPRE_Int> indices( _stencil_size );
std::iota( indices.begin(), indices.end(), 0 );
auto error = HYPRE_StructMatrixSetBoxValues(
_A, _lower.data(), _upper.data(), indices.size(), indices.data(),
a_values.data() );
checkHypreError( error );
error = HYPRE_StructMatrixAssemble( _A );
checkHypreError( error );
}
/*!
\brief Print the hypre matrix to output file
\param prefix File prefix for where hypre output is written
*/
void printMatrix( const char* prefix )
{
HYPRE_StructMatrixPrint( prefix, _A, 0 );
}
/*!
\brief Print the hypre LHS to output file
\param prefix File prefix for where hypre output is written
*/
void printLHS( const char* prefix )
{
HYPRE_StructVectorPrint( prefix, _x, 0 );
}
/*!
\brief Print the hypre RHS to output file
\param prefix File prefix for where hypre output is written
*/
void printRHS( const char* prefix )
{
HYPRE_StructVectorPrint( prefix, _b, 0 );
}
//! Set convergence tolerance implementation.
void setTolerance( const double tol ) { this->setToleranceImpl( tol ); }
//! Set maximum iteration implementation.
void setMaxIter( const int max_iter ) { this->setMaxIterImpl( max_iter ); }
//! Set the output level.
void setPrintLevel( const int print_level )
{
this->setPrintLevelImpl( print_level );
}
//! Set a preconditioner.
void
setPreconditioner( const std::shared_ptr<HypreStructuredSolver<
Scalar, EntityType, MemorySpace>>& preconditioner )
{
// This function is only valid for non-preconditioners.
if ( _is_preconditioner )
throw std::logic_error(
"Cannot call setPreconditioner() on a preconditioner" );
// Only a preconditioner can be used as a preconditioner.
if ( !preconditioner->isPreconditioner() )
throw std::logic_error( "Not a preconditioner" );
_preconditioner = preconditioner;
this->setPreconditionerImpl( *_preconditioner );
}
//! Setup the problem.
void setup()
{
// This function is only valid for non-preconditioners.
if ( _is_preconditioner )
throw std::logic_error( "Cannot call setup() on preconditioners" );
// FIXME: appears to be a memory issue in the call to this function
this->setupImpl();
}
/*!
\brief Solve the problem Ax = b for x.
\param b The forcing term.
\param x The solution.
*/
template <class Array_t>
void solve( const Array_t& b, Array_t& x )
{
Kokkos::Profiling::ScopedRegion region(
"Cabana::Grid::HypreStructuredSolver::solve" );
static_assert( is_array<Array_t>::value, "Must use an array" );
static_assert(
std::is_same<typename Array_t::entity_type, entity_type>::value,
"Array entity type mush match solver entity type" );
static_assert(
std::is_same<typename Array_t::memory_space, MemorySpace>::value,
"Array memory space and solver memory space are different." );
static_assert(
std::is_same<typename Array_t::value_type, value_type>::value,
"Array value type and solver value type are different." );
// This function is only valid for non-preconditioners.
if ( _is_preconditioner )
throw std::logic_error( "Cannot call solve() on preconditioners" );
if ( b.layout()->dofsPerEntity() != 1 ||
x.layout()->dofsPerEntity() != 1 )
throw std::runtime_error(
"Structured solver only for scalar fields" );
// Spatial dimension.
const std::size_t num_space_dim = Array_t::num_space_dim;
// Copy the RHS into HYPRE. The HYPRE layout is fixed as layout-right.
auto owned_space = b.layout()->indexSpace( Own(), Local() );
std::array<long, num_space_dim + 1> reorder_size;
for ( std::size_t d = 0; d < num_space_dim; ++d )
{
reorder_size[d] = owned_space.extent( d );
}
reorder_size.back() = 1;
IndexSpace<num_space_dim + 1> reorder_space( reorder_size );
auto vector_values =
createView<HYPRE_Complex, Kokkos::LayoutRight, memory_space>(
"vector_values", reorder_space );
auto b_subv = createSubview( b.view(), owned_space );
Kokkos::deep_copy( vector_values, b_subv );
// Insert b values into the HYPRE vector.
auto error = HYPRE_StructVectorSetBoxValues(
_b, _lower.data(), _upper.data(), vector_values.data() );
checkHypreError( error );
error = HYPRE_StructVectorAssemble( _b );
checkHypreError( error );
// Solve the problem
this->solveImpl();
// Extract the solution from the LHS
error = HYPRE_StructVectorGetBoxValues(
_x, _lower.data(), _upper.data(), vector_values.data() );
checkHypreError( error );
// Copy the HYPRE solution to the LHS.
auto x_subv = createSubview( x.view(), owned_space );
Kokkos::deep_copy( x_subv, vector_values );
}
//! Get the number of iterations taken on the last solve.
int getNumIter() { return this->getNumIterImpl(); }
//! Get the relative residual norm achieved on the last solve.
double getFinalRelativeResidualNorm()
{
return this->getFinalRelativeResidualNormImpl();
}
//! Get the preconditioner.
virtual HYPRE_StructSolver getHypreSolver() const = 0;
//! Get the preconditioner setup function.
virtual HYPRE_PtrToStructSolverFcn getHypreSetupFunction() const = 0;
//! Get the preconditioner solve function.
virtual HYPRE_PtrToStructSolverFcn getHypreSolveFunction() const = 0;
protected:
//! Set convergence tolerance implementation.
virtual void setToleranceImpl( const double tol ) = 0;
//! Set maximum iteration implementation.
virtual void setMaxIterImpl( const int max_iter ) = 0;
//! Set the output level.
virtual void setPrintLevelImpl( const int print_level ) = 0;
//! Setup implementation.
virtual void setupImpl() = 0;
//! Solver implementation.
virtual void solveImpl() = 0;
//! Get the number of iterations taken on the last solve.
virtual int getNumIterImpl() = 0;
//! Get the relative residual norm achieved on the last solve.
virtual double getFinalRelativeResidualNormImpl() = 0;
//! Set a preconditioner.
virtual void setPreconditionerImpl(
const HypreStructuredSolver<Scalar, EntityType, MemorySpace>&
preconditioner ) = 0;
//! Check a hypre error.
void checkHypreError( const int error ) const
{
if ( error > 0 )
{
char error_msg[256];
HYPRE_DescribeError( error, error_msg );
std::stringstream out;
out << "HYPRE structured solver error: ";
out << error << " " << error_msg;
HYPRE_ClearError( error );
throw std::runtime_error( out.str() );
}
}
protected:
//! Matrix for the problem Ax = b.
HYPRE_StructMatrix _A;
//! Forcing term for the problem Ax = b.
HYPRE_StructVector _b;
//! Solution to the problem Ax = b.
HYPRE_StructVector _x;
private:
MPI_Comm _comm;
bool _is_preconditioner;
HYPRE_StructGrid _grid;
std::vector<HYPRE_Int> _lower;
std::vector<HYPRE_Int> _upper;
HYPRE_StructStencil _stencil;
unsigned _stencil_size;
std::shared_ptr<HypreStructuredSolver<Scalar, EntityType, MemorySpace>>
_preconditioner;
};
//---------------------------------------------------------------------------//
//! PCG solver.
template <class Scalar, class EntityType, class MemorySpace>
class HypreStructPCG
: public HypreStructuredSolver<Scalar, EntityType, MemorySpace>
{
public:
//! Base HYPRE structured solver type.
using base_type = HypreStructuredSolver<Scalar, EntityType, MemorySpace>;
//! Constructor
template <class ArrayLayout_t>
HypreStructPCG( const ArrayLayout_t& layout,
const bool is_preconditioner = false )
: base_type( layout, is_preconditioner )
{
if ( is_preconditioner )
throw std::logic_error(
"HYPRE PCG cannot be used as a preconditioner" );
auto error = HYPRE_StructPCGCreate(
layout.localGrid()->globalGrid().comm(), &_solver );
this->checkHypreError( error );
HYPRE_StructPCGSetTwoNorm( _solver, 1 );
}
~HypreStructPCG() { HYPRE_StructPCGDestroy( _solver ); }
// PCG SETTINGS
//! Set the absolute tolerance
void setAbsoluteTol( const double tol )
{
auto error = HYPRE_StructPCGSetAbsoluteTol( _solver, tol );
this->checkHypreError( error );
}
//! Additionally require that the relative difference in successive
//! iterates be small.
void setRelChange( const int rel_change )
{
auto error = HYPRE_StructPCGSetRelChange( _solver, rel_change );
this->checkHypreError( error );
}
//! Set the amount of logging to do.
void setLogging( const int logging )
{
auto error = HYPRE_StructPCGSetLogging( _solver, logging );
this->checkHypreError( error );
}
HYPRE_StructSolver getHypreSolver() const override { return _solver; }
HYPRE_PtrToStructSolverFcn getHypreSetupFunction() const override
{
return HYPRE_StructPCGSetup;
}
HYPRE_PtrToStructSolverFcn getHypreSolveFunction() const override
{
return HYPRE_StructPCGSolve;
}
protected:
void setToleranceImpl( const double tol ) override
{
auto error = HYPRE_StructPCGSetTol( _solver, tol );
this->checkHypreError( error );
}
void setMaxIterImpl( const int max_iter ) override
{
auto error = HYPRE_StructPCGSetMaxIter( _solver, max_iter );
this->checkHypreError( error );
}
void setPrintLevelImpl( const int print_level ) override
{
auto error = HYPRE_StructPCGSetPrintLevel( _solver, print_level );
this->checkHypreError( error );
}
void setupImpl() override
{
auto error = HYPRE_StructPCGSetup( _solver, _A, _b, _x );
this->checkHypreError( error );
}
void solveImpl() override
{
auto error = HYPRE_StructPCGSolve( _solver, _A, _b, _x );
this->checkHypreError( error );
}
int getNumIterImpl() override
{
HYPRE_Int num_iter;
auto error = HYPRE_StructPCGGetNumIterations( _solver, &num_iter );
this->checkHypreError( error );
return num_iter;
}
double getFinalRelativeResidualNormImpl() override
{
HYPRE_Real norm;
auto error =
HYPRE_StructPCGGetFinalRelativeResidualNorm( _solver, &norm );
this->checkHypreError( error );
return norm;
}
void setPreconditionerImpl(
const HypreStructuredSolver<Scalar, EntityType, MemorySpace>&
preconditioner ) override
{
auto error = HYPRE_StructPCGSetPrecond(
_solver, preconditioner.getHypreSolveFunction(),
preconditioner.getHypreSetupFunction(),
preconditioner.getHypreSolver() );
this->checkHypreError( error );
}
private:
HYPRE_StructSolver _solver;
using base_type::_A;
using base_type::_b;
using base_type::_x;
};
//---------------------------------------------------------------------------//
//! GMRES solver.
template <class Scalar, class EntityType, class MemorySpace>
class HypreStructGMRES
: public HypreStructuredSolver<Scalar, EntityType, MemorySpace>
{
public:
//! Base HYPRE structured solver type.
using base_type = HypreStructuredSolver<Scalar, EntityType, MemorySpace>;
//! Constructor
template <class ArrayLayout_t>
HypreStructGMRES( const ArrayLayout_t& layout,
const bool is_preconditioner = false )
: base_type( layout, is_preconditioner )
{
if ( is_preconditioner )
throw std::logic_error(
"HYPRE GMRES cannot be used as a preconditioner" );
auto error = HYPRE_StructGMRESCreate(
layout.localGrid()->globalGrid().comm(), &_solver );
this->checkHypreError( error );
}
~HypreStructGMRES() { HYPRE_StructGMRESDestroy( _solver ); }
// GMRES SETTINGS
//! Set the absolute tolerance
void setAbsoluteTol( const double tol )
{
auto error = HYPRE_StructGMRESSetAbsoluteTol( _solver, tol );
this->checkHypreError( error );
}
//! Set the max size of the Krylov space.
void setKDim( const int k_dim )
{
auto error = HYPRE_StructGMRESSetKDim( _solver, k_dim );
this->checkHypreError( error );
}
//! Set the amount of logging to do.
void setLogging( const int logging )
{
auto error = HYPRE_StructGMRESSetLogging( _solver, logging );
this->checkHypreError( error );
}
HYPRE_StructSolver getHypreSolver() const override { return _solver; }
HYPRE_PtrToStructSolverFcn getHypreSetupFunction() const override
{
return HYPRE_StructGMRESSetup;
}
HYPRE_PtrToStructSolverFcn getHypreSolveFunction() const override
{
return HYPRE_StructGMRESSolve;
}
protected:
void setToleranceImpl( const double tol ) override
{
auto error = HYPRE_StructGMRESSetTol( _solver, tol );
this->checkHypreError( error );
}
void setMaxIterImpl( const int max_iter ) override
{
auto error = HYPRE_StructGMRESSetMaxIter( _solver, max_iter );
this->checkHypreError( error );
}
void setPrintLevelImpl( const int print_level ) override
{
auto error = HYPRE_StructGMRESSetPrintLevel( _solver, print_level );
this->checkHypreError( error );
}
void setupImpl() override
{
auto error = HYPRE_StructGMRESSetup( _solver, _A, _b, _x );
this->checkHypreError( error );
}
void solveImpl() override
{
auto error = HYPRE_StructGMRESSolve( _solver, _A, _b, _x );
this->checkHypreError( error );
}
int getNumIterImpl() override
{
HYPRE_Int num_iter;
auto error = HYPRE_StructGMRESGetNumIterations( _solver, &num_iter );
this->checkHypreError( error );
return num_iter;
}
double getFinalRelativeResidualNormImpl() override
{
HYPRE_Real norm;
auto error =
HYPRE_StructGMRESGetFinalRelativeResidualNorm( _solver, &norm );
this->checkHypreError( error );
return norm;
}
void setPreconditionerImpl(
const HypreStructuredSolver<Scalar, EntityType, MemorySpace>&
preconditioner ) override
{
auto error = HYPRE_StructGMRESSetPrecond(
_solver, preconditioner.getHypreSolveFunction(),
preconditioner.getHypreSetupFunction(),
preconditioner.getHypreSolver() );
this->checkHypreError( error );
}
private:
HYPRE_StructSolver _solver;
using base_type::_A;
using base_type::_b;
using base_type::_x;
};
//---------------------------------------------------------------------------//
//! BiCGSTAB solver.
template <class Scalar, class EntityType, class MemorySpace>
class HypreStructBiCGSTAB
: public HypreStructuredSolver<Scalar, EntityType, MemorySpace>
{
public:
//! Base HYPRE structured solver type.
using base_type = HypreStructuredSolver<Scalar, EntityType, MemorySpace>;
//! Constructor
template <class ArrayLayout_t>
HypreStructBiCGSTAB( const ArrayLayout_t& layout,
const bool is_preconditioner = false )
: base_type( layout, is_preconditioner )
{
if ( is_preconditioner )
throw std::logic_error(
"HYPRE BiCGSTAB cannot be used as a preconditioner" );
auto error = HYPRE_StructBiCGSTABCreate(
layout.localGrid()->globalGrid().comm(), &_solver );
this->checkHypreError( error );
}
~HypreStructBiCGSTAB() { HYPRE_StructBiCGSTABDestroy( _solver ); }
// BiCGSTAB SETTINGS
//! Set the absolute tolerance
void setAbsoluteTol( const double tol )
{
auto error = HYPRE_StructBiCGSTABSetAbsoluteTol( _solver, tol );
this->checkHypreError( error );
}
//! Set the amount of logging to do.
void setLogging( const int logging )
{
auto error = HYPRE_StructBiCGSTABSetLogging( _solver, logging );
this->checkHypreError( error );
}
HYPRE_StructSolver getHypreSolver() const override { return _solver; }
HYPRE_PtrToStructSolverFcn getHypreSetupFunction() const override
{
return HYPRE_StructBiCGSTABSetup;
}
HYPRE_PtrToStructSolverFcn getHypreSolveFunction() const override
{
return HYPRE_StructBiCGSTABSolve;
}
protected:
void setToleranceImpl( const double tol ) override
{
auto error = HYPRE_StructBiCGSTABSetTol( _solver, tol );
this->checkHypreError( error );
}
void setMaxIterImpl( const int max_iter ) override
{
auto error = HYPRE_StructBiCGSTABSetMaxIter( _solver, max_iter );
this->checkHypreError( error );
}
void setPrintLevelImpl( const int print_level ) override
{
auto error = HYPRE_StructBiCGSTABSetPrintLevel( _solver, print_level );
this->checkHypreError( error );
}
void setupImpl() override
{
auto error = HYPRE_StructBiCGSTABSetup( _solver, _A, _b, _x );
this->checkHypreError( error );
}
void solveImpl() override
{
auto error = HYPRE_StructBiCGSTABSolve( _solver, _A, _b, _x );
this->checkHypreError( error );
}
int getNumIterImpl() override
{
HYPRE_Int num_iter;
auto error = HYPRE_StructBiCGSTABGetNumIterations( _solver, &num_iter );
this->checkHypreError( error );
return num_iter;
}
double getFinalRelativeResidualNormImpl() override
{
HYPRE_Real norm;
auto error =
HYPRE_StructBiCGSTABGetFinalRelativeResidualNorm( _solver, &norm );
this->checkHypreError( error );
return norm;
}
void setPreconditionerImpl(
const HypreStructuredSolver<Scalar, EntityType, MemorySpace>&
preconditioner ) override
{
auto error = HYPRE_StructBiCGSTABSetPrecond(
_solver, preconditioner.getHypreSolveFunction(),
preconditioner.getHypreSetupFunction(),
preconditioner.getHypreSolver() );
this->checkHypreError( error );
}
private:
HYPRE_StructSolver _solver;
using base_type::_A;
using base_type::_b;
using base_type::_x;
};
//---------------------------------------------------------------------------//
//! PFMG solver.
template <class Scalar, class EntityType, class MemorySpace>
class HypreStructPFMG
: public HypreStructuredSolver<Scalar, EntityType, MemorySpace>
{
public:
//! Base HYPRE structured solver type.
using base_type = HypreStructuredSolver<Scalar, EntityType, MemorySpace>;
//! Constructor
template <class ArrayLayout_t>
HypreStructPFMG( const ArrayLayout_t& layout,
const bool is_preconditioner = false )
: base_type( layout, is_preconditioner )
{
auto error = HYPRE_StructPFMGCreate(
layout.localGrid()->globalGrid().comm(), &_solver );
this->checkHypreError( error );
if ( is_preconditioner )
{
error = HYPRE_StructPFMGSetZeroGuess( _solver );
this->checkHypreError( error );
}
}
~HypreStructPFMG() { HYPRE_StructPFMGDestroy( _solver ); }
// PFMG SETTINGS
//! Set the maximum number of multigrid levels.
void setMaxLevels( const int max_levels )
{
auto error = HYPRE_StructPFMGSetMaxLevels( _solver, max_levels );
this->checkHypreError( error );
}
//! Additionally require that the relative difference in successive
//! iterates be small.
void setRelChange( const int rel_change )
{
auto error = HYPRE_StructPFMGSetRelChange( _solver, rel_change );
this->checkHypreError( error );
}
/*!
\brief Set relaxation type.
0 - Jacobi
1 - Weighted Jacobi (default)
2 - Red/Black Gauss-Seidel (symmetric: RB pre-relaxation, BR
post-relaxation)
3 - Red/Black Gauss-Seidel (nonsymmetric: RB pre- and post-relaxation)
*/
void setRelaxType( const int relax_type )
{
auto error = HYPRE_StructPFMGSetRelaxType( _solver, relax_type );
this->checkHypreError( error );
}
//! Set the Jacobi weight
void setJacobiWeight( const double weight )
{
auto error = HYPRE_StructPFMGSetJacobiWeight( _solver, weight );
this->checkHypreError( error );
}
/*!
\brief Set type of coarse-grid operator to use.
0 - Galerkin (default)
1 - non-Galerkin 5-pt or 7-pt stencils
Both operators are constructed algebraically. The non-Galerkin option
maintains a 5-pt stencil in 2D and a 7-pt stencil in 3D on all grid
levels. The stencil coefficients are computed by averaging techniques.
*/
void setRAPType( const int rap_type )
{
auto error = HYPRE_StructPFMGSetRAPType( _solver, rap_type );
this->checkHypreError( error );
}
//! Set number of relaxation sweeps before coarse-grid correction.
void setNumPreRelax( const int num_pre_relax )
{
auto error = HYPRE_StructPFMGSetNumPreRelax( _solver, num_pre_relax );
this->checkHypreError( error );
}
//! Set number of relaxation sweeps before coarse-grid correction.
void setNumPostRelax( const int num_post_relax )
{
auto error = HYPRE_StructPFMGSetNumPostRelax( _solver, num_post_relax );
this->checkHypreError( error );
}
//! Skip relaxation on certain grids for isotropic problems. This can
//! greatly improve efficiency by eliminating unnecessary relaxations when
//! the underlying problem is isotropic.
void setSkipRelax( const int skip_relax )
{
auto error = HYPRE_StructPFMGSetSkipRelax( _solver, skip_relax );
this->checkHypreError( error );
}
//! Set the amount of logging to do.
void setLogging( const int logging )
{
auto error = HYPRE_StructPFMGSetLogging( _solver, logging );
this->checkHypreError( error );
}
HYPRE_StructSolver getHypreSolver() const override { return _solver; }
HYPRE_PtrToStructSolverFcn getHypreSetupFunction() const override
{
return HYPRE_StructPFMGSetup;
}
HYPRE_PtrToStructSolverFcn getHypreSolveFunction() const override
{
return HYPRE_StructPFMGSolve;
}
protected:
void setToleranceImpl( const double tol ) override
{
auto error = HYPRE_StructPFMGSetTol( _solver, tol );
this->checkHypreError( error );
}