21 #include <opm/simulators/linalg/ParallelOverlappingILU0.hpp> 23 #include <dune/common/version.hh> 25 #include <dune/istl/ilu.hh> 26 #include <dune/istl/owneroverlapcopy.hh> 28 #include <opm/common/ErrorMacros.hpp> 29 #include <opm/common/TimingMacros.hpp> 31 #include <opm/simulators/linalg/GraphColoring.hpp> 32 #include <opm/simulators/linalg/matrixblock.hh> 43 void ghost_last_bilu0_decomposition (M& A, std::size_t interiorSize)
45 OPM_TIMEBLOCK(GhostLastBlockILU0Decomp);
47 assert(interiorSize <= A.N());
48 using rowiterator =
typename M::RowIterator;
49 using coliterator =
typename M::ColIterator;
50 using block =
typename M::block_type;
53 for (rowiterator i = A.begin(); i.index() < interiorSize; ++i)
56 coliterator endij=(*i).end();
60 for (ij=(*i).begin(); ij.index()<i.index(); ++ij)
63 coliterator jj = A[ij.index()].find(ij.index());
66 (*ij).rightmultiply(*jj);
69 coliterator endjk=A[ij.index()].end();
70 coliterator jk=jj; ++jk;
71 coliterator ik=ij; ++ik;
72 while (ik!=endij && jk!=endjk)
73 if (ik.index()==jk.index())
82 if (ik.index()<jk.index())
90 if (ij.index()!=i.index())
91 DUNE_THROW(Dune::ISTLError,
"diagonal entry missing");
95 catch (Dune::FMatrixError & e) {
96 DUNE_THROW(Dune::ISTLError,
"ILU failed to invert matrix block");
102 template<
class M,
class CRS,
class InvVector>
103 void convertToCRS(
const M& A, CRS& lower, CRS& upper, InvVector& inv)
105 OPM_TIMEBLOCK(convertToCRS);
113 using size_type =
typename M :: size_type;
118 lower.resize( A.N() );
119 upper.resize( A.N() );
123 size_type numLower = 0;
124 size_type numUpper = 0;
125 const auto endi = A.end();
126 for (
auto i = A.begin(); i != endi; ++i) {
127 const size_type iIndex = i.index();
128 size_type numLowerRow = 0;
129 for (
auto j = (*i).begin(); j.index() < iIndex; ++j) {
132 numLower += numLowerRow;
133 numUpper += (*i).size() - numLowerRow - 1;
135 assert(numLower + numUpper + A.N() == A.nonzeroes());
137 lower.reserveAdditional( numLower );
141 size_type colcount = 0;
142 lower.rows_[ 0 ] = colcount;
143 for (
auto i=A.begin(); i!=endi; ++i, ++row)
145 const size_type iIndex = i.index();
148 for (
auto j=(*i).begin(); j.index() < iIndex; ++j )
150 lower.push_back( (*j), j.index() );
153 lower.rows_[ iIndex+1 ] = colcount;
156 assert(colcount == numLower);
158 const auto rbegini = std::make_reverse_iterator(A.begin());
161 upper.rows_[ 0 ] = colcount ;
163 upper.reserveAdditional( numUpper );
167 auto rindex = [](
auto it) {
return std::prev(it.base()).index(); };
168 for (
auto i=std::make_reverse_iterator(A.end()); i!=rbegini; ++i, ++ row )
170 const size_type iIndex = rindex(i);
174 for (
auto j=std::make_reverse_iterator(i->end()); rindex(j)>=iIndex; ++j )
176 const size_type jIndex = rindex(j);
177 if( rindex(j) == iIndex )
182 else if ( rindex(j) >= rindex(i) )
184 upper.push_back( (*j), jIndex );
188 upper.rows_[ row+1 ] = colcount;
190 assert(colcount == numUpper);
194 size_t set_interiorSize( [[maybe_unused]]
size_t N,
size_t interiorSize, [[maybe_unused]]
const PI& comm)
201 size_t set_interiorSize(
size_t N,
size_t interiorSize,
const Dune::OwnerOverlapCopyCommunication<int,int>& comm)
205 auto indexSet = comm.indexSet();
206 if (indexSet.size() == 0)
210 for (
auto idx = indexSet.begin(); idx!=indexSet.end(); ++idx)
212 if (idx->local().attribute()==1)
214 auto loc = idx->local().local();
227 template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
228 Dune::SolverCategory::Category
229 ParallelOverlappingILU0<Matrix,Domain,Range,ParallelInfoT>::category()
const 231 return std::is_same_v<ParallelInfoT, Dune::Amg::SequentialInformation> ?
232 Dune::SolverCategory::sequential : Dune::SolverCategory::overlapping;
235 template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
244 comm_(nullptr), w_(w),
245 relaxation_( std::abs( w - 1.0 ) > 1e-15 ),
246 A_(&reinterpret_cast<const Matrix&>(A)), iluIteration_(n),
247 milu_(milu), redBlack_(redblack), reorderSphere_(reorder_sphere)
249 interiorSize_ = A.N();
255 template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
258 const ParallelInfo& comm,
const int n,
const field_type w,
265 relaxation_( std::abs( w - 1.0 ) > 1e-15 ),
266 A_(&reinterpret_cast<const Matrix&>(A)), iluIteration_(n),
267 milu_(milu), redBlack_(redblack), reorderSphere_(reorder_sphere)
269 interiorSize_ = A.N();
275 template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
283 template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
293 relaxation_( std::abs( w - 1.0 ) > 1e-15 ),
294 A_(&reinterpret_cast<const Matrix&>(A)), iluIteration_(0),
295 milu_(milu), redBlack_(redblack), reorderSphere_(reorder_sphere)
297 interiorSize_ = A.N();
303 template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
306 const ParallelInfo& comm,
308 size_type interiorSize,
bool redblack,
314 relaxation_( std::abs( w - 1.0 ) > 1e-15 ),
315 interiorSize_(interiorSize),
316 A_(&reinterpret_cast<const Matrix&>(A)), iluIteration_(0),
317 milu_(milu), redBlack_(redblack), reorderSphere_(reorder_sphere)
321 assert(interiorSize <= A_->N());
325 template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
329 OPM_TIMEBLOCK(apply);
330 Range& md = reorderD(d);
331 Domain& mv = reorderV(v);
334 using dblock =
typename Range ::block_type;
335 using vblock =
typename Domain::block_type;
337 const size_type iEnd = lower_.rows();
338 const size_type lastRow = iEnd - 1;
339 size_type upperLoopStart = iEnd - interiorSize_;
340 size_type lowerLoopEnd = interiorSize_;
341 if (iEnd != upper_.rows())
343 OPM_THROW(std::logic_error,
"ILU: number of lower and upper rows must be the same");
347 for (size_type i = 0; i < lowerLoopEnd; ++i)
349 dblock rhs( md[ i ] );
350 const size_type rowI = lower_.rows_[ i ];
351 const size_type rowINext = lower_.rows_[ i+1 ];
353 for (size_type col = rowI; col < rowINext; ++col)
355 lower_.values_[ col ].mmv( mv[ lower_.cols_[ col ] ], rhs );
361 for (size_type i = upperLoopStart; i < iEnd; ++i)
363 vblock& vBlock = mv[ lastRow - i ];
364 vblock rhs ( vBlock );
365 const size_type rowI = upper_.rows_[ i ];
366 const size_type rowINext = upper_.rows_[ i+1 ];
368 for (size_type col = rowI; col < rowINext; ++col)
370 upper_.values_[ col ].mmv( mv[ upper_.cols_[ col ] ], rhs );
374 inv_[ i ].mv( rhs, vBlock);
377 copyOwnerToAll( mv );
385 template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
391 comm_->copyOwnerToAll(v, v);
395 template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
396 void ParallelOverlappingILU0<Matrix,Domain,Range,ParallelInfoT>::
399 OPM_TIMEBLOCK(update);
403 if (comm_ && comm_->communicator().size() <= 0)
407 OPM_THROW(std::logic_error,
"Expected a matrix with zero rows for an invalid communicator.");
416 int ilu_setup_successful = 1;
418 const int rank = comm_ ? comm_->communicator().rank() : 0;
422 using Graph = Dune::Amg::MatrixGraph<const Matrix>;
425 const auto& colors = std::get<0>(colorsTuple);
426 const auto& verticesPerColor = std::get<2>(colorsTuple);
427 auto noColors = std::get<1>(colorsTuple);
428 if ( reorderSphere_ )
440 std::vector<std::size_t> inverseOrdering(ordering_.size());
442 OPM_TIMEBLOCK(createInverseOrdering);
443 std::size_t index = 0;
444 for (
const auto newIndex : ordering_)
446 inverseOrdering[newIndex] = index++;
452 OPM_TIMEBLOCK(iluDecomposition);
453 if (iluIteration_ == 0) {
456 interiorSize_ = detail::set_interiorSize(A_->N(), interiorSize_, *comm_);
457 assert(interiorSize_ <= A_->N());
461 if (ordering_.empty())
463 OPM_TIMEBLOCK(iluDecompositionUpdateMatrix);
465 OPM_TIMEBLOCK(iluDecompositionCopyEntries);
469 for (std::size_t row = 0; row < A_->N(); ++row) {
470 const auto& Arow = (*A_)[row];
471 auto Ait = Arow.begin();
472 auto Iit = (*ILU_)[row].begin();
473 for (; Ait != Arow.end(); ++Ait, ++Iit) {
478 OPM_TIMEBLOCK(iluDecompositionDuplicateMatrix);
480 ILU_ = std::make_unique<Matrix>(*A_);
485 ILU_ = std::make_unique<Matrix>(A_->N(), A_->M(),
486 A_->nonzeroes(), Matrix::row_wise);
489 auto endcreateA = newA.createend();
490 for (
auto iter = newA.createbegin(); iter != endcreateA; ++iter)
492 const auto& row = (*A_)[inverseOrdering[iter.index()]];
493 for (
auto col = row.begin(), cend = row.end(); col != cend; ++col)
495 iter.insert(ordering_[col.index()]);
499 for (
auto iter = A_->begin(); iter != A_->end(); ++iter)
501 auto newRow = newA.begin() + ordering_[iter.index()];
502 for (
auto&& [A_ij, j] : sparseRange(*newRow))
504 (*newRow)[ordering_[j]] = A_ij;
512 detail::milu0_decomposition ( *ILU_);
515 detail::milu0_decomposition ( *ILU_, detail::identityFunctor<typename Matrix::field_type>,
516 detail::signFunctor<typename Matrix::field_type> );
519 detail::milu0_decomposition ( *ILU_, detail::absFunctor<typename Matrix::field_type>,
520 detail::signFunctor<typename Matrix::field_type> );
523 detail::milu0_decomposition ( *ILU_, detail::identityFunctor<typename Matrix::field_type>,
524 detail::isPositiveFunctor<typename Matrix::field_type> );
527 if (interiorSize_ == A_->N())
528 Dune::ILU::blockILU0Decomposition( *ILU_ );
530 detail::ghost_last_bilu0_decomposition(*ILU_, interiorSize_);
536 ILU_ = std::make_unique<Matrix>(A_->N(), A_->M(), Matrix::row_wise);
537 std::unique_ptr<detail::Reorderer> reorderer, inverseReorderer;
538 if (ordering_.empty())
540 reorderer.reset(
new detail::NoReorderer());
541 inverseReorderer.reset(
new detail::NoReorderer());
545 reorderer.reset(
new detail::RealReorderer(ordering_));
546 inverseReorderer.reset(
new detail::RealReorderer(inverseOrdering));
549 milun_decomposition( *A_, iluIteration_, milu_, *ILU_, *reorderer, *inverseReorderer );
552 catch (
const Dune::MatrixBlockError& error)
554 message = error.what();
555 std::cerr <<
"Exception occurred on process " << rank <<
" during " <<
556 "setup of ILU0 preconditioner with message: " 557 << message<<std::endl;
558 ilu_setup_successful = 0;
563 OPM_TIMEBLOCK(checkParallelFailure);
564 const bool parallel_failure = comm_ && comm_->communicator().min(ilu_setup_successful) == 0;
565 const bool local_failure = ilu_setup_successful == 0;
566 if (local_failure || parallel_failure)
568 throw Dune::MatrixBlockError();
573 detail::convertToCRS(*ILU_, lower_, upper_, inv_);
576 template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
580 if (ordering_.empty())
586 return const_cast<Range&
>(d);
590 reorderedD_.resize(d.size());
592 for (
const auto index : ordering_)
594 reorderedD_[index] = d[i++];
600 template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
604 if (ordering_.empty())
610 reorderedV_.resize(v.size());
612 for (
const auto index : ordering_)
614 reorderedV_[index] = v[i++];
620 template<
class Matrix,
class Domain,
class Range,
class ParallelInfoT>
624 if (!ordering_.empty())
627 for (
const auto index : ordering_)
629 v[i++] = reorderedV[index];
A two-step version of an overlapping Schwarz preconditioner using one step ILU0 as.
Definition: ParallelOverlappingILU0.hpp:37
MILU_VARIANT
Definition: MILU.hpp:34
std::vector< std::size_t > reorderVerticesPreserving(const std::vector< int > &colors, int noColors, const std::vector< std::size_t > &verticesPerColor, const Graph &graph)
! Reorder colored graph preserving order of vertices with the same color.
Definition: GraphColoring.hpp:169
std::tuple< std::vector< int >, int, std::vector< std::size_t > > colorVerticesWelshPowell(const Graph &graph)
Color the vertices of graph.
Definition: GraphColoring.hpp:113
Range & reorderD(const Range &d)
Reorder D if needed and return a reference to it.
Definition: ParallelOverlappingILU0_impl.hpp:578
void apply(Domain &v, const Range &d) override
Apply the preconditoner.
Definition: ParallelOverlappingILU0_impl.hpp:327
Structs needed for tpfalinearizer and its gpuparams struct extracted to be defined in one place that ...
Definition: blackoilbioeffectsmodules.hh:45
typename Domain::field_type field_type
The field type of the preconditioner.
Definition: ParallelOverlappingILU0.hpp:142
std::vector< std::size_t > reorderVerticesSpheres(const std::vector< int > &colors, int noColors, const std::vector< std::size_t > &verticesPerColor, const Graph &graph, typename Graph::VertexDescriptor root)
! Reorder Vetrices in spheres
Definition: GraphColoring.hpp:189
Domain & reorderV(Domain &v)
Reorder V if needed and return a reference to it.
Definition: ParallelOverlappingILU0_impl.hpp:602
ParallelOverlappingILU0(const Matrix &A, const int n, const field_type w, MILU_VARIANT milu, bool redblack=false, bool reorder_sphere=true)
Constructor.
Definition: ParallelOverlappingILU0_impl.hpp:237