dune-composites (2.5.1)

multicommdatahandle.hh
1#ifndef DUNE_MULTICOMMDATAHANDLE_HH
2#define DUNE_MULTICOMMDATAHANDLE_HH
3
4#include <dune/pdelab/gridfunctionspace/entityindexcache.hh>
5
6
7namespace Dune {
8 namespace Geneo {
9
13 template<typename GFS, typename RankIndex, typename V>
15 {
16
17 typedef Dune::PDELab::EntityIndexCache<GFS> IndexCache;
18 typedef typename V::template LocalView<IndexCache> LocalView;
19
20 public:
21
22 template<typename MessageBuffer, typename Entity, typename LocalView>
23 bool gather(MessageBuffer& buff, const Entity& e, LocalView& local_view) const
24 {
25 // Write rank
26 buff.write((double)_rank);
27
28 // Write values
29 for (std::size_t i = 0; i < local_view.size(); ++i) {
30 buff.write (local_view[i]);
31 }
32 return true;
33 }
34
35 template<typename MessageBuffer, typename Entity, typename LocalView>
36 bool scatter(MessageBuffer& buff, std::size_t n, const Entity& e, LocalView& local_view) const
37 {
38 double received_rank;
39 buff.read(received_rank); // read in original type!
40 RankIndex remote_rank = received_rank;
41
42 // Get neighbor index from rank
43 int remote_index = -1;
44 for (unsigned int i = 0; i < neighbor_ranks.size(); i++) {
45 if (neighbor_ranks[i] == remote_rank) {
46 remote_index = i;
47 break;
48 }
49 }
50 if (remote_index == -1)
51 DUNE_THROW(Exception,"Received remote rank " << remote_rank << ", but it's not in the given neighbor set!");
52
53 // Get values
54 auto target_view = LocalView(*target_vectors[remote_index]);
55 _index_cache.update(e);
56 target_view.bind(_index_cache);
57
58
59 if (target_view.cache().gridFunctionSpace().entitySet().partitions().contains(e.partitionType())) {
60 if (target_view.size() != n - 1) // 1st entry for rank, n-1 for DOFs
61 DUNE_THROW(Exception,"size mismatch in GridFunctionSpace data handle, have " << target_view.size() << "DOFs, but received " << n);
62
63 for (std::size_t i = 0; i < target_view.size(); ++i) {
64 typename LocalView::ElementType x;
65 buff.read(x);
66 target_view[i] = x;
67 }
68 target_view.unbind();
69 return true;
70
71 } else {
72
73 if (target_view.size() != 0)
74 DUNE_THROW(Exception,"expected no DOFs in partition '" << e.partitionType() << "', but have " << target_view.size());
75
76 for (std::size_t i = 0; i < target_view.size(); ++i) {
77 typename LocalView::ElementType dummy;
78 buff.read(dummy);
79 }
80 target_view.unbind();
81 return false;
82 }
83 }
84
86
89 MultiCommGatherScatter(const GFS& gfs, RankIndex rank, std::vector<std::shared_ptr<V> > _target_vectors, std::vector<RankIndex> _neighbor_ranks)
90 : _rank(rank), target_vectors(_target_vectors), neighbor_ranks(_neighbor_ranks), _index_cache(gfs)
91 {}
92
93 private:
94
95
96 const RankIndex _rank;
97 // TODO: std::map?
98 std::vector<std::shared_ptr<V> > target_vectors;
99 std::vector<RankIndex> neighbor_ranks;
100 mutable IndexCache _index_cache;
101 };
102
103 template<class GFS, class V, typename RankIndex,int dim>
104 class MultiCommDataHandle
105 : public Dune::PDELab::GFSDataHandle<GFS,
106 V,
107 MultiCommGatherScatter<
108 GFS, RankIndex, V
109 >,
110 Dune::PDELab::EntityDataCommunicationDescriptor<
111 typename V::ElementType
112 >
113 >
114 {
115 typedef Dune::PDELab::GFSDataHandle<
116 GFS,
117 V,
118 MultiCommGatherScatter<
119 GFS, RankIndex, V
120 >,
121 Dune::PDELab::EntityDataCommunicationDescriptor<
122 typename V::ElementType
123 >
124 > BaseT;
125
126 public:
127
129
138 MultiCommDataHandle(const GFS& gfs_, V& v_, std::vector<std::shared_ptr<V> > target_vectors, std::vector<RankIndex> neighbor_ranks)
139 : BaseT(gfs_,v_,MultiCommGatherScatter<GFS, RankIndex, V>(gfs_, gfs_.gridView().comm().rank(), target_vectors, neighbor_ranks),
140 Dune::PDELab::EntityDataCommunicationDescriptor<typename V::ElementType>(dim+1)) // Request size 2 to store rank + DOF
141 {
142 }
143 };
144
145 }
146}
147#endif //DUNE_MULTICOMMDATAHANDLE_HH
Definition: multicommdatahandle.hh:15
MultiCommGatherScatter(const GFS &gfs, RankIndex rank, std::vector< std::shared_ptr< V > > _target_vectors, std::vector< RankIndex > _neighbor_ranks)
Create a DisjointPartitioningGatherScatter object.
Definition: multicommdatahandle.hh:89
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Sep 5, 22:35, 2025)