3 #ifndef DUNE_ISTL_OWNEROVERLAPCOPY_HH
4 #define DUNE_ISTL_OWNEROVERLAPCOPY_HH
21 #include <dune/common/enumset.hh>
24 #include <dune/common/parallel/indexset.hh>
25 #include <dune/common/parallel/communicator.hh>
26 #include <dune/common/parallel/remoteindices.hh>
27 #include <dune/common/parallel/mpicollectivecommunication.hh>
32 #include <dune/common/parallel/collectivecommunication.hh>
35 template<
int dim,
template<
class,
class>
class Comm>
74 template <
class G,
class L>
109 DUNE_THROW(
ISTLError,
"OwnerOverlapCopyCommunication: global index not in index set");
110 localindices.insert(x);
123 DUNE_THROW(
ISTLError,
"OwnerOverlapCopyCommunication: global index not in index set");
124 remoteindices.insert(x);
142 return remoteindices;
150 localindices.clear();
151 remoteindices.clear();
156 std::set<IndexTripel> localindices;
158 std::set<RemoteIndexTripel> remoteindices;
170 template <
class GlobalIdType,
class LocalIdType=
int>
171 class OwnerOverlapCopyCommunication
173 template<
typename M,
typename G,
typename L>
176 OwnerOverlapCopyCommunication<G,L>&,
181 typedef typename std::set<IndexTripel>::const_iterator localindex_iterator;
182 typedef typename std::set<RemoteIndexTripel>::const_iterator remoteindex_iterator;
184 typedef Dune::ParallelLocalIndex<AttributeSet> LI;
186 typedef Dune::ParallelIndexSet<GlobalIdType,LI,512> PIS;
187 typedef Dune::RemoteIndices<PIS> RI;
188 typedef Dune::RemoteIndexListModifier<PIS,typename RI::Allocator,false> RILM;
189 typedef typename RI::RemoteIndex RX;
190 typedef Dune::BufferedCommunicator BC;
191 typedef Dune::Interface IF;
192 typedef EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::owner> OwnerSet;
193 typedef EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::copy> CopySet;
194 typedef Combine<EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::owner>,EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::overlap>,AttributeSet> OwnerOverlapSet;
195 typedef Dune::AllSet<AttributeSet> AllSet;
201 struct CopyGatherScatter
203 typedef typename CommPolicy<T>::IndexedType V;
205 static V gather(
const T& a, std::size_t i)
210 static void scatter(T& a, V v, std::size_t i)
216 struct AddGatherScatter
218 typedef typename CommPolicy<T>::IndexedType V;
220 static V gather(
const T& a, std::size_t i)
225 static void scatter(T& a, V v, std::size_t i)
231 void buildOwnerOverlapToAllInterface ()
const
233 if (OwnerOverlapToAllInterfaceBuilt)
234 OwnerOverlapToAllInterface.free();
235 typedef Combine<EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::owner>,EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::overlap>,AttributeSet> OwnerOverlapSet;
236 typedef Combine<OwnerOverlapSet,EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::copy>,AttributeSet> AllSet;
237 OwnerOverlapSet sourceFlags;
239 OwnerOverlapToAllInterface.build(ri,sourceFlags,destFlags);
240 OwnerOverlapToAllInterfaceBuilt =
true;
243 void buildOwnerToAllInterface ()
const
245 if (OwnerToAllInterfaceBuilt)
246 OwnerToAllInterface.free();
247 OwnerSet sourceFlags;
249 OwnerToAllInterface.build(ri,sourceFlags,destFlags);
250 OwnerToAllInterfaceBuilt =
true;
253 void buildOwnerCopyToAllInterface ()
const
255 if (OwnerCopyToAllInterfaceBuilt)
256 OwnerCopyToAllInterface.free();
257 typedef Combine<EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::owner>,EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::copy>,AttributeSet> OwnerCopySet;
258 typedef Combine<OwnerCopySet,EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::overlap>,AttributeSet> AllSet;
259 OwnerCopySet sourceFlags;
261 OwnerCopyToAllInterface.build(ri,sourceFlags,destFlags);
262 OwnerCopyToAllInterfaceBuilt =
true;
265 void buildOwnerCopyToOwnerCopyInterface ()
const
267 if (OwnerCopyToOwnerCopyInterfaceBuilt)
268 OwnerCopyToOwnerCopyInterface.free();
269 typedef Combine<EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::owner>,EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::copy>,AttributeSet> OwnerCopySet;
270 OwnerCopySet sourceFlags;
271 OwnerCopySet destFlags;
272 OwnerCopyToOwnerCopyInterface.build(ri,sourceFlags,destFlags);
273 OwnerCopyToOwnerCopyInterfaceBuilt =
true;
276 void buildCopyToAllInterface ()
const
278 if (CopyToAllInterfaceBuilt)
279 CopyToAllInterface.free();
282 CopyToAllInterface.build(ri,sourceFlags,destFlags);
283 CopyToAllInterfaceBuilt =
true;
291 void setSolverCategory (SolverCategory set) {
303 const CollectiveCommunication<MPI_Comm>& communicator()
const
315 void copyOwnerToAll (
const T& source, T& dest)
const
317 if (!OwnerToAllInterfaceBuilt)
318 buildOwnerToAllInterface ();
320 communicator.template build<T>(OwnerToAllInterface);
321 communicator.template forward<CopyGatherScatter<T> >(source,dest);
332 void copyCopyToAll (
const T& source, T& dest)
const
334 if (!CopyToAllInterfaceBuilt)
335 buildCopyToAllInterface ();
337 communicator.template build<T>(CopyToAllInterface);
338 communicator.template forward<CopyGatherScatter<T> >(source,dest);
349 void addOwnerOverlapToAll (
const T& source, T& dest)
const
351 if (!OwnerOverlapToAllInterfaceBuilt)
352 buildOwnerOverlapToAllInterface ();
354 communicator.template build<T>(OwnerOverlapToAllInterface);
355 communicator.template forward<AddGatherScatter<T> >(source,dest);
366 void addOwnerCopyToAll (
const T& source, T& dest)
const
368 if (!OwnerCopyToAllInterfaceBuilt)
369 buildOwnerCopyToAllInterface ();
371 communicator.template build<T>(OwnerCopyToAllInterface);
372 communicator.template forward<AddGatherScatter<T> >(source,dest);
383 void addOwnerCopyToOwnerCopy (
const T& source, T& dest)
const
385 if (!OwnerCopyToOwnerCopyInterfaceBuilt)
386 buildOwnerCopyToOwnerCopyInterface ();
388 communicator.template build<T>(OwnerCopyToOwnerCopyInterface);
389 communicator.template forward<AddGatherScatter<T> >(source,dest);
401 template<
class T1,
class T2>
402 void dot (
const T1& x,
const T1& y, T2& result)
const
405 if (mask.size()!=
static_cast<typename std::vector<double>::size_type
>(x.size()))
407 mask.resize(x.size());
408 for (
typename std::vector<double>::size_type i=0; i<mask.size(); i++)
410 for (
typename PIS::const_iterator i=pis.begin(); i!=pis.end(); ++i)
412 mask[i->local().local()] = 0;
416 for (
typename T1::size_type i=0; i<x.size(); i++)
417 result += x[i]*(y[i])*mask[i];
418 result = cc.sum(result);
429 typename FieldTraits<typename T1::field_type>::real_type norm (
const T1& x)
const
432 if (mask.size()!=
static_cast<typename std::vector<double>::size_type
>(x.size()))
434 mask.resize(x.size());
435 for (
typename std::vector<double>::size_type i=0; i<mask.size(); i++)
437 for (
typename PIS::const_iterator i=pis.begin(); i!=pis.end(); ++i)
439 mask[i->local().local()] = 0;
441 typename T1::field_type result =
typename T1::field_type(0.0);
442 for (
typename T1::size_type i=0; i<x.size(); i++)
443 result += x[i].two_norm2()*mask[i];
444 return static_cast<double>(sqrt(cc.sum(result)));
447 typedef Dune::EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::copy> CopyFlags;
450 typedef Dune::ParallelIndexSet<GlobalIdType,LI,512> ParallelIndexSet;
453 typedef Dune::RemoteIndices<PIS> RemoteIndices;
457 typedef Dune::GlobalLookupIndexSet<ParallelIndexSet> GlobalLookupIndexSet;
463 const ParallelIndexSet& indexSet()
const
472 const RemoteIndices& remoteIndices()
const
481 ParallelIndexSet& indexSet()
491 RemoteIndices& remoteIndices()
496 void buildGlobalLookup()
499 if(pis.seqNo()==oldseqNo)
502 delete globalLookup_;
505 globalLookup_ =
new GlobalLookupIndexSet(pis);
506 oldseqNo = pis.seqNo();
509 void buildGlobalLookup(std::size_t size)
512 if(pis.seqNo()==oldseqNo)
515 delete globalLookup_;
517 globalLookup_ =
new GlobalLookupIndexSet(pis, size);
518 oldseqNo = pis.seqNo();
521 void freeGlobalLookup()
523 delete globalLookup_;
527 const GlobalLookupIndexSet& globalLookup()
const
529 assert(globalLookup_ != 0);
530 return *globalLookup_;
539 void project (T1& x)
const
541 for (
typename PIS::const_iterator i=pis.begin(); i!=pis.end(); ++i)
543 x[i->local().local()] = 0;
555 OwnerOverlapCopyCommunication (MPI_Comm comm_,
557 bool freecomm_ =
false)
558 : comm(comm_), cc(comm_), pis(), ri(pis,pis,comm_),
559 OwnerToAllInterfaceBuilt(false), OwnerOverlapToAllInterfaceBuilt(false),
560 OwnerCopyToAllInterfaceBuilt(false), OwnerCopyToOwnerCopyInterfaceBuilt(false),
561 CopyToAllInterfaceBuilt(false), globalLookup_(0), category(cat_),
574 : comm(MPI_COMM_WORLD), cc(MPI_COMM_WORLD), pis(), ri(pis,pis,MPI_COMM_WORLD),
575 OwnerToAllInterfaceBuilt(false), OwnerOverlapToAllInterfaceBuilt(false),
576 OwnerCopyToAllInterfaceBuilt(false), OwnerCopyToOwnerCopyInterfaceBuilt(false),
577 CopyToAllInterfaceBuilt(false), globalLookup_(0), category(cat_), freecomm(false)
587 OwnerOverlapCopyCommunication (
const IndexInfoFromGrid<GlobalIdType, LocalIdType>& indexinfo,
590 bool freecomm_ =
false)
591 : comm(comm_), cc(comm_), OwnerToAllInterfaceBuilt(false),
592 OwnerOverlapToAllInterfaceBuilt(false), OwnerCopyToAllInterfaceBuilt(false),
593 OwnerCopyToOwnerCopyInterfaceBuilt(false), CopyToAllInterfaceBuilt(false),
594 globalLookup_(0), category(cat_), freecomm(freecomm_)
598 for (localindex_iterator i=indexinfo.localIndices().begin(); i!=indexinfo.localIndices().end(); ++i)
612 ri.setIndexSets(pis,pis,cc);
613 if (indexinfo.remoteIndices().size()>0)
615 remoteindex_iterator i=indexinfo.remoteIndices().begin();
616 int p = std::get<0>(*i);
617 RILM modifier = ri.template getModifier<false,true>(p);
618 typename PIS::const_iterator pi=pis.begin();
619 for ( ; i!=indexinfo.remoteIndices().end(); ++i)
622 if (p!=std::get<0>(*i))
625 modifier = ri.template getModifier<false,true>(p);
630 while (pi->global()!=std::get<1>(*i) && pi!=pis.end())
633 DUNE_THROW(ISTLError,
"OwnerOverlapCopyCommunication: global index not in index set");
646 ri.template getModifier<false,true>(0);
651 ~OwnerOverlapCopyCommunication ()
654 if (OwnerToAllInterfaceBuilt) OwnerToAllInterface.free();
655 if (OwnerOverlapToAllInterfaceBuilt) OwnerOverlapToAllInterface.free();
656 if (OwnerCopyToAllInterfaceBuilt) OwnerCopyToAllInterface.free();
657 if (OwnerCopyToOwnerCopyInterfaceBuilt) OwnerCopyToOwnerCopyInterface.free();
658 if (CopyToAllInterfaceBuilt) CopyToAllInterface.free();
659 if (globalLookup_)
delete globalLookup_;
661 if(comm!=MPI_COMM_NULL)
667 int wasFinalized = 0;
668 MPI_Finalized( &wasFinalized );
671 MPI_Comm_free(&comm);
676 OwnerOverlapCopyCommunication (
const OwnerOverlapCopyCommunication&)
679 CollectiveCommunication<MPI_Comm> cc;
682 mutable IF OwnerToAllInterface;
683 mutable bool OwnerToAllInterfaceBuilt;
684 mutable IF OwnerOverlapToAllInterface;
685 mutable bool OwnerOverlapToAllInterfaceBuilt;
686 mutable IF OwnerCopyToAllInterface;
687 mutable bool OwnerCopyToAllInterfaceBuilt;
688 mutable IF OwnerCopyToOwnerCopyInterface;
689 mutable bool OwnerCopyToOwnerCopyInterfaceBuilt;
690 mutable IF CopyToAllInterface;
691 mutable bool CopyToAllInterfaceBuilt;
692 mutable std::vector<double> mask;
694 GlobalLookupIndexSet* globalLookup_;