4 #ifndef DUNE_OWNEROVERLAPCOPY_HH
5 #define DUNE_OWNEROVERLAPCOPY_HH
22 #include <dune/common/tuples.hh>
23 #include <dune/common/enumset.hh>
26 #include <dune/common/parallel/indexset.hh>
27 #include <dune/common/parallel/communicator.hh>
28 #include <dune/common/parallel/remoteindices.hh>
29 #include <dune/common/parallel/mpicollectivecommunication.hh>
34 #include <dune/common/parallel/collectivecommunication.hh>
36 template<
int dim,
template<
class,
class>
class Comm>
75 template <
class G,
class L>
110 DUNE_THROW(
ISTLError,
"OwnerOverlapCopyCommunication: global index not in index set");
111 localindices.insert(x);
124 DUNE_THROW(
ISTLError,
"OwnerOverlapCopyCommunication: global index not in index set");
125 remoteindices.insert(x);
143 return remoteindices;
151 localindices.clear();
152 remoteindices.clear();
157 std::set<IndexTripel> localindices;
159 std::set<RemoteIndexTripel> remoteindices;
171 template <
class GlobalIdType,
class LocalIdType=
int>
174 template<
typename M,
typename G,
typename L>
182 typedef typename std::set<IndexTripel>::const_iterator localindex_iterator;
183 typedef typename std::set<RemoteIndexTripel>::const_iterator remoteindex_iterator;
185 typedef Dune::ParallelLocalIndex<AttributeSet> LI;
187 typedef Dune::ParallelIndexSet<GlobalIdType,LI,512>
PIS;
188 typedef Dune::RemoteIndices<PIS>
RI;
189 typedef Dune::RemoteIndexListModifier<PIS,typename RI::Allocator,false>
RILM;
190 typedef typename RI::RemoteIndex
RX;
191 typedef Dune::BufferedCommunicator
BC;
192 typedef Dune::Interface
IF;
193 typedef EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::owner>
OwnerSet;
194 typedef EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::copy>
CopySet;
195 typedef Combine<EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::owner>,EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::overlap>,
AttributeSet>
OwnerOverlapSet;
196 typedef Dune::AllSet<AttributeSet>
AllSet;
204 typedef typename CommPolicy<T>::IndexedType
V;
219 typedef typename CommPolicy<T>::IndexedType
V;
234 if (OwnerOverlapToAllInterfaceBuilt)
235 OwnerOverlapToAllInterface.free();
236 typedef Combine<EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::owner>,EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::overlap>,
AttributeSet>
OwnerOverlapSet;
237 typedef Combine<OwnerOverlapSet,EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::copy>,
AttributeSet>
AllSet;
238 OwnerOverlapSet sourceFlags;
240 OwnerOverlapToAllInterface.build(ri,sourceFlags,destFlags);
241 OwnerOverlapToAllInterfaceBuilt =
true;
246 if (OwnerToAllInterfaceBuilt)
247 OwnerToAllInterface.free();
250 OwnerToAllInterface.build(ri,sourceFlags,destFlags);
251 OwnerToAllInterfaceBuilt =
true;
256 if (OwnerCopyToAllInterfaceBuilt)
257 OwnerCopyToAllInterface.free();
258 typedef Combine<EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::owner>,EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::copy>,
AttributeSet> OwnerCopySet;
259 typedef Combine<OwnerCopySet,EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::overlap>,
AttributeSet>
AllSet;
260 OwnerCopySet sourceFlags;
262 OwnerCopyToAllInterface.build(ri,sourceFlags,destFlags);
263 OwnerCopyToAllInterfaceBuilt =
true;
268 if (OwnerCopyToOwnerCopyInterfaceBuilt)
269 OwnerCopyToOwnerCopyInterface.free();
270 typedef Combine<EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::owner>,EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::copy>,
AttributeSet> OwnerCopySet;
271 OwnerCopySet sourceFlags;
272 OwnerCopySet destFlags;
273 OwnerCopyToOwnerCopyInterface.build(ri,sourceFlags,destFlags);
274 OwnerCopyToOwnerCopyInterfaceBuilt =
true;
279 if (CopyToAllInterfaceBuilt)
280 CopyToAllInterface.free();
283 CopyToAllInterface.build(ri,sourceFlags,destFlags);
284 CopyToAllInterfaceBuilt =
true;
320 if (!OwnerToAllInterfaceBuilt)
323 communicator.template build<T>(OwnerToAllInterface);
324 communicator.template forward<CopyGatherScatter<T> >(source,dest);
337 if (!CopyToAllInterfaceBuilt)
340 communicator.template build<T>(CopyToAllInterface);
341 communicator.template forward<CopyGatherScatter<T> >(source,dest);
354 if (!OwnerOverlapToAllInterfaceBuilt)
357 communicator.template build<T>(OwnerOverlapToAllInterface);
358 communicator.template forward<AddGatherScatter<T> >(source,dest);
371 if (!OwnerCopyToAllInterfaceBuilt)
374 communicator.template build<T>(OwnerCopyToAllInterface);
375 communicator.template forward<AddGatherScatter<T> >(source,dest);
388 if (!OwnerCopyToOwnerCopyInterfaceBuilt)
391 communicator.template build<T>(OwnerCopyToOwnerCopyInterface);
392 communicator.template forward<AddGatherScatter<T> >(source,dest);
404 template<
class T1,
class T2>
405 void dot (
const T1& x,
const T1& y, T2& result)
const
408 if (mask.size()!=
static_cast<typename std::vector<double>::size_type
>(x.size()))
410 mask.resize(x.size());
411 for (
typename std::vector<double>::size_type i=0; i<mask.size(); i++)
413 for (
typename PIS::const_iterator i=pis.begin(); i!=pis.end(); ++i)
415 mask[i->local().local()] = 0;
419 for (
typename T1::size_type i=0; i<x.size(); i++)
420 result += x[i]*(y[i])*mask[i];
421 result = cc.sum(result);
432 double norm (
const T1& x)
const
435 if (mask.size()!=
static_cast<typename std::vector<double>::size_type
>(x.size()))
437 mask.resize(x.size());
438 for (
typename std::vector<double>::size_type i=0; i<mask.size(); i++)
440 for (
typename PIS::const_iterator i=pis.begin(); i!=pis.end(); ++i)
442 mask[i->local().local()] = 0;
445 for (
typename T1::size_type i=0; i<x.size(); i++)
446 result += x[i].two_norm2()*mask[i];
447 return sqrt(cc.sum(result));
450 typedef Dune::EnumItem<AttributeSet,OwnerOverlapCopyAttributeSet::copy>
CopyFlags;
502 if(pis.seqNo()==oldseqNo)
505 delete globalLookup_;
509 oldseqNo = pis.seqNo();
515 if(pis.seqNo()==oldseqNo)
518 delete globalLookup_;
521 oldseqNo = pis.seqNo();
526 delete globalLookup_;
532 assert(globalLookup_ != 0);
533 return *globalLookup_;
544 for (
typename PIS::const_iterator i=pis.begin(); i!=pis.end(); ++i)
546 x[i->local().local()] = 0;
560 bool freecomm_ =
false)
561 : comm(comm_), cc(comm_), pis(), ri(pis,pis,comm_),
562 OwnerToAllInterfaceBuilt(false), OwnerOverlapToAllInterfaceBuilt(false),
563 OwnerCopyToAllInterfaceBuilt(false), OwnerCopyToOwnerCopyInterfaceBuilt(false),
564 CopyToAllInterfaceBuilt(false), globalLookup_(0), category(cat_),
577 : comm(MPI_COMM_WORLD), cc(MPI_COMM_WORLD), pis(), ri(pis,pis,MPI_COMM_WORLD),
578 OwnerToAllInterfaceBuilt(false), OwnerOverlapToAllInterfaceBuilt(false),
579 OwnerCopyToAllInterfaceBuilt(false), OwnerCopyToOwnerCopyInterfaceBuilt(false),
580 CopyToAllInterfaceBuilt(false), globalLookup_(0), category(cat_), freecomm(false)
593 bool freecomm_ =
false)
594 : comm(comm_), cc(comm_), OwnerToAllInterfaceBuilt(false),
595 OwnerOverlapToAllInterfaceBuilt(false), OwnerCopyToAllInterfaceBuilt(false),
596 OwnerCopyToOwnerCopyInterfaceBuilt(false), CopyToAllInterfaceBuilt(false),
597 globalLookup_(0), category(cat_), freecomm(freecomm_)
615 ri.setIndexSets(pis,pis,cc);
620 RILM modifier = ri.template getModifier<false,true>(p);
621 typename PIS::const_iterator pi=pis.begin();
628 modifier = ri.template getModifier<false,true>(p);
633 while (pi->global()!=get<1>(*i) && pi!=pis.end())
636 DUNE_THROW(
ISTLError,
"OwnerOverlapCopyCommunication: global index not in index set");
649 ri.template getModifier<false,true>(0);
657 if (OwnerToAllInterfaceBuilt) OwnerToAllInterface.free();
658 if (OwnerOverlapToAllInterfaceBuilt) OwnerOverlapToAllInterface.free();
659 if (OwnerCopyToAllInterfaceBuilt) OwnerCopyToAllInterface.free();
660 if (OwnerCopyToOwnerCopyInterfaceBuilt) OwnerCopyToOwnerCopyInterface.free();
661 if (CopyToAllInterfaceBuilt) CopyToAllInterface.free();
662 if (globalLookup_)
delete globalLookup_;
664 if(comm!=MPI_COMM_NULL)
670 int wasFinalized = 0;
671 MPI_Finalized( &wasFinalized );
674 MPI_Comm_free(&comm);
682 CollectiveCommunication<MPI_Comm> cc;
685 mutable IF OwnerToAllInterface;
686 mutable bool OwnerToAllInterfaceBuilt;
687 mutable IF OwnerOverlapToAllInterface;
688 mutable bool OwnerOverlapToAllInterfaceBuilt;
689 mutable IF OwnerCopyToAllInterface;
690 mutable bool OwnerCopyToAllInterfaceBuilt;
691 mutable IF OwnerCopyToOwnerCopyInterface;
692 mutable bool OwnerCopyToOwnerCopyInterfaceBuilt;
693 mutable IF CopyToAllInterface;
694 mutable bool CopyToAllInterfaceBuilt;
695 mutable std::vector<double> mask;