// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- // vi: set et ts=4 sw=2 sts=2: #ifndef DUNE_AMG_INDICESCOARSENER_HH #define DUNE_AMG_INDICESCOARSENER_HH #include #include #include "renumberer.hh" #if HAVE_MPI #include #endif #include "pinfo.hh" namespace Dune { namespace Amg { /** * @addtogroup ISTL_PAAMG * * @{ */ /** @file * @author Markus Blatt * @brief Provides a class for building the index set * and remote indices on the coarse level. */ template class IndicesCoarsener {}; #if HAVE_MPI template class ParallelIndicesCoarsener { public: /** * @brief The set of excluded attributes */ typedef E ExcludedAttributes; /** * @brief The type of the parallel information. */ typedef T ParallelInformation; typedef typename ParallelInformation::ParallelIndexSet ParallelIndexSet; /** * @brief The type of the global index. */ typedef typename ParallelIndexSet::GlobalIndex GlobalIndex; /** * @brief The type of the local index. */ typedef typename ParallelIndexSet::LocalIndex LocalIndex; /** * @brief The type of the attribute. */ typedef typename LocalIndex::Attribute Attribute; /** * @brief The type of the remote indices. */ typedef Dune::RemoteIndices RemoteIndices; /** * @brief Build the coarse index set after the aggregatio. * * @param fineInfo The parallel information at the fine level. * @param fineGraph The graph of the fine lecel, * @param visitedMap Map for marking vertices as visited. * @param aggregates The mapping of unknowns onto aggregates. * @param coarseInfo The information about the parallel data decomposition * on the coarse level. * @param noAggregates * @param useFixedOrder Flag indicating if creating indices for the coarser level * should be done in a fixed order, i.e., the order in which the rows were sent. * If set to true, this makes the runs reproducible but it might slow down performance. * @return The number of unknowns on the coarse level. */ template static typename Graph::VertexDescriptor coarsen(ParallelInformation& fineInfo, Graph& fineGraph, VM& visitedMap, AggregatesMap& aggregates, ParallelInformation& coarseInfo, typename Graph::VertexDescriptor noAggregates, bool useFixedOrder = false); private: template class ParallelAggregateRenumberer : public AggregateRenumberer { typedef typename G::VertexDescriptor Vertex; typedef I GlobalLookupIndexSet; typedef typename GlobalLookupIndexSet::IndexPair IndexPair; typedef typename IndexPair::GlobalIndex GlobalIndex; public: ParallelAggregateRenumberer(AggregatesMap& aggregates, const I& lookup) : AggregateRenumberer(aggregates), isPublic_(false), lookup_(lookup), globalIndex_(std::numeric_limits::max()) {} void operator()(const typename G::ConstEdgeIterator& edge) { AggregateRenumberer::operator()(edge); const IndexPair* pair= lookup_.pair(edge.target()); if(pair!=0) { globalIndex(pair->global()); attribute(pair->local().attribute()); isPublic(pair->local().isPublic()); } } Vertex operator()([[maybe_unused]] const GlobalIndex& global) { Vertex current = this->number_; this->operator++(); return current; } bool isPublic() { return isPublic_; } void isPublic(bool b) { isPublic_ = isPublic_ || b; } void reset() { globalIndex_ = std::numeric_limits::max(); isPublic_=false; } void attribute(const Attribute& attribute) { attribute_=attribute; } Attribute attribute() { return attribute_; } const GlobalIndex& globalIndex() const { return globalIndex_; } void globalIndex(const GlobalIndex& global) { globalIndex_ = global; } private: bool isPublic_; Attribute attribute_; const GlobalLookupIndexSet& lookup_; GlobalIndex globalIndex_; }; template static void buildCoarseIndexSet(const ParallelInformation& pinfo, Graph& fineGraph, VM& visitedMap, AggregatesMap& aggregates, ParallelIndexSet& coarseIndices, ParallelAggregateRenumberer& renumberer); template static void buildCoarseRemoteIndices(const RemoteIndices& fineRemote, const AggregatesMap& aggregates, ParallelIndexSet& coarseIndices, RemoteIndices& coarseRemote, ParallelAggregateRenumberer& renumberer, bool useFixedOrder); }; /** * @brief Coarsen Indices in the parallel case. */ template class IndicesCoarsener,E> : public ParallelIndicesCoarsener,E> {}; #endif /** * @brief Coarsen Indices in the sequential case. * * Nothing to be coarsened here. Just renumber the aggregates * consecutively */ template class IndicesCoarsener { public: template static typename Graph::VertexDescriptor coarsen(const SequentialInformation & fineInfo, Graph& fineGraph, VM& visitedMap, AggregatesMap& aggregates, SequentialInformation& coarseInfo, typename Graph::VertexDescriptor noAggregates, bool useFixedOrder = false); }; #if HAVE_MPI template template inline typename Graph::VertexDescriptor ParallelIndicesCoarsener::coarsen(ParallelInformation& fineInfo, Graph& fineGraph, VM& visitedMap, AggregatesMap& aggregates, ParallelInformation& coarseInfo, [[maybe_unused]] typename Graph::VertexDescriptor noAggregates, bool useFixedOrder) { ParallelAggregateRenumberer renumberer(aggregates, fineInfo.globalLookup()); buildCoarseIndexSet(fineInfo, fineGraph, visitedMap, aggregates, coarseInfo.indexSet(), renumberer); buildCoarseRemoteIndices(fineInfo.remoteIndices(), aggregates, coarseInfo.indexSet(), coarseInfo.remoteIndices(), renumberer, useFixedOrder); return renumberer; } template template void ParallelIndicesCoarsener::buildCoarseIndexSet(const ParallelInformation& pinfo, Graph& fineGraph, VM& visitedMap, AggregatesMap& aggregates, ParallelIndexSet& coarseIndices, ParallelAggregateRenumberer& renumberer) { // fineGraph is the local subgraph corresponding to the vertices the process owns. // i.e. no overlap/copy vertices can be visited traversing the graph typedef typename Graph::ConstVertexIterator Iterator; typedef typename ParallelInformation::GlobalLookupIndexSet GlobalLookupIndexSet; Iterator end = fineGraph.end(); const GlobalLookupIndexSet& lookup = pinfo.globalLookup(); coarseIndices.beginResize(); // Setup the coarse index set and renumber the aggregate consecutively // ascending from zero according to the minimum global index belonging // to the aggregate for(Iterator index = fineGraph.begin(); index != end; ++index) { if(aggregates[*index]!=AggregatesMap::ISOLATED) // Isolated vertices will not be represented on the next level. // These should only be there if skipIsolated is activiated in // the coarsening criterion as otherwise they will be aggregated // and should have real aggregate number in the map right now. if(!get(visitedMap, *index)) { // This vertex was not visited by breadthFirstSearch yet. typedef typename GlobalLookupIndexSet::IndexPair IndexPair; const IndexPair* pair= lookup.pair(*index); renumberer.reset(); // reset attribute and global index. if(pair!=0) { // vertex is in the index set. Note that not all vertices have // to be in the index set, just the ones where communication // will happen. assert(!ExcludedAttributes::contains(pair->local().attribute())); renumberer.attribute(pair->local().attribute()); renumberer.isPublic(pair->local().isPublic()); renumberer.globalIndex(pair->global()); } // Reconstruct aggregate and mark vertices as visited aggregates.template breadthFirstSearch(*index, aggregates[*index], fineGraph, renumberer, visitedMap); if(renumberer.globalIndex()!=std::numeric_limits::max()) { // vertex is in the index set. //std::cout <<" Adding global="<< renumberer.globalIndex()<<" local="<(renumberer)<(renumberer) >= coarseIndices.size()); // Reset the visited flags for(Iterator vertex=fineGraph.begin(); vertex != end; ++vertex) put(visitedMap, *vertex, false); } template template void ParallelIndicesCoarsener::buildCoarseRemoteIndices(const RemoteIndices& fineRemote, const AggregatesMap& aggregates, ParallelIndexSet& coarseIndices, RemoteIndices& coarseRemote, ParallelAggregateRenumberer& renumberer, bool useFixedOrder) { std::vector attributes(static_cast(renumberer)); GlobalLookupIndexSet coarseLookup(coarseIndices, static_cast(renumberer)); typedef typename RemoteIndices::const_iterator Iterator; Iterator end = fineRemote.end(); for(Iterator neighbour = fineRemote.begin(); neighbour != end; ++neighbour) { int process = neighbour->first; assert(neighbour->second.first==neighbour->second.second); // Mark all as not known typedef typename std::vector::iterator CIterator; for(CIterator iter=attributes.begin(); iter!= attributes.end(); ++iter) *iter = std::numeric_limits::max(); auto riEnd = neighbour->second.second->end(); for(auto index = neighbour->second.second->begin(); index != riEnd; ++index) { if(!E::contains(index->localIndexPair().local().attribute()) && aggregates[index->localIndexPair().local()] != AggregatesMap::ISOLATED) { assert(aggregates[index->localIndexPair().local()]localIndexPair().local()]] != 3) attributes[aggregates[index->localIndexPair().local()]] = index->attribute(); } } // Build remote index list typedef RemoteIndexListModifier Modifier; typedef typename RemoteIndices::RemoteIndex RemoteIndex; typedef typename ParallelIndexSet::const_iterator IndexIterator; Modifier coarseList = coarseRemote.template getModifier(process); IndexIterator iend = coarseIndices.end(); for(IndexIterator index = coarseIndices.begin(); index != iend; ++index) if(attributes[index->local()] != std::numeric_limits::max()) { // remote index is present coarseList.insert(RemoteIndex(Attribute(attributes[index->local()]), &(*index))); } //std::cout< syncer(coarseIndices, coarseRemote); syncer.sync(renumberer, useFixedOrder); } #endif template template typename Graph::VertexDescriptor IndicesCoarsener::coarsen( [[maybe_unused]] const SequentialInformation& fineInfo, [[maybe_unused]] Graph& fineGraph, [[maybe_unused]] VM& visitedMap, [[maybe_unused]] AggregatesMap& aggregates, [[maybe_unused]] SequentialInformation& coarseInfo, [[maybe_unused]] typename Graph::VertexDescriptor noAggregates, [[maybe_unused]] bool useFixedOrder) { return noAggregates; } } //namespace Amg } // namespace Dune #endif