Proto
Proto_Copier.H
1 #pragma once
2 #ifndef _PROTO_COPIER_
3 #define _PROTO_COPIER_
4 
5 #include "Proto_DisjointBoxLayout.H"
6 #include "Proto.H"
7 #include "Proto_SPMD.H"
8 #include "Proto_LevelIterators.H"
9 #include <unordered_map>
10 #include <cstdint>
11 #include "Proto_MayDay.H"
12 
13 namespace Proto
14 
15 {
16 // =======================================================================
17 // DECLARATIONS
18 
19  struct BufferEntry;
20  struct MotionItem;
21  class CopierIterator;
22 
23 // =======================================================================
24 // ABSTRACT COPIER CLASS
25 
27 
51  template<class OP>
52  class Copier
53  {
54  friend class MotionItem;
55  friend class CopierIterator;
56 
57  public:
58 
59  enum MotionType
60  {
61  LOCAL,
62  FROM,
63  TO
64  };
65 
66  Copier();
67  Copier(OP a_op);
68 
69  // Destructor is virtual to handle potential polymorphic destruct
70  inline virtual ~Copier();
71  //
72  inline void define(OP a_op);
73  inline void clear();
74  virtual void buildMotionPlans(OP& a_op) = 0;
75 
76  inline Copier<OP>& operator=(const Copier<OP>& a_rhs);
77  inline bool operator==(const Copier<OP>& a_rhs) const;
78  inline void reverse();
79  inline void execute();
80  inline void sort();
81  inline CopierIterator begin(MotionType a_type) const;
82  protected:
83 
84  OP m_op;
85 
86  std::vector<MotionItem*> m_localMotionPlan;
87  std::vector<MotionItem*> m_fromMotionPlan;
88  std::vector<MotionItem*> m_toMotionPlan;
89 
90  bool m_isDefined = false;
91 
92  private:
93 
94  void makeItSo();
95  void makeItSoBegin();
96  void makeItSoLocal();
97  void makeItSoEnd();
98 
99  void completePendingSends() const;
100  void allocateBuffers();
101  void writeToSendBuffers();
102  void postSends() const;
103  void readFromRecvBuffers();
104  void postRecvs() const;
105 
106  // Copy buffers
107  void clearBuffers();
108 
109  mutable size_t m_sendCapacity;
110  mutable size_t m_recvCapacity;
111  mutable void* m_sendBuffer;
112  mutable void* m_recvBuffer;
113 
114  // Probably can't do stuff like this on the device...
115  //mutable std::unique_ptr<char[]> m_sendBuffer;
116  //mutable std::unique_ptr<char[]> m_recvBuffer;
117 
118  mutable std::vector<BufferEntry> m_fromMe;
119  mutable std::vector<BufferEntry> m_toMe;
120  mutable std::vector<std::vector<BufferEntry>> m_toMeUnpack;
121 
122  mutable int m_numSends;
123  mutable int m_numRecvs;
124 
125 #ifdef PR_MPI
126  mutable std::vector<MPI_Request> m_sendRequests, m_recvRequests;
127  mutable std::vector<MPI_Status> m_sendStatus, m_recvStatus;
128 #endif
129  }; // end class Copier
130 
131 // =======================================================================
132 // STRUCTURES
133 
135  {
136  public:
137  void* bufferPointer;
138  size_t size;
139  const MotionItem* item;
140  unsigned int procID;
141 
142  inline bool operator<(const BufferEntry& a_rhs) const;
143  }; // end struct BufferEntry
144 
146  {
147  public:
148  DataIndex fromIndex, toIndex;
149  Box fromRegion;
150  Box toRegion;
151  int procID;
152 
153  inline MotionItem(const DataIndex& a_fromIndex,
154  const DataIndex& a_toIndex,
155  const Box& a_fromRegion,
156  const Box& a_toRegion);
157 
158  inline bool operator==(const MotionItem& a_rhs) const;
159  inline bool operator<(const MotionItem& a_rhs) const;
160  }; // end struct MotionItem
161 
163  {
164  public:
165  inline bool operator()(MotionItem* const & a_lhs,
166  MotionItem* const& a_rhs) const
167  {
168  return ((*a_lhs) < (*a_rhs));
169  }
170  }; // end class MotionItemSorter
171 
172 // =======================================================================
173 // COPIER ITERATOR
174 
176  {
177  public:
178 
179  inline CopierIterator(const std::vector<MotionItem*>* a_motionPlan);
180  inline const MotionItem& operator()() const;
181  inline const MotionItem& operator[](size_t a_index) const;
182  inline const MotionItem& operator*() const;
183  inline void operator++();
184  inline bool ok();
185  inline void reset();
186  inline size_t size() const;
187 
188  private:
189 
190  const vector<MotionItem*>* m_motionPlan;
191  unsigned int m_current;
192  }; // end class CopierIterator
193 
194 
195 #include "implem/Proto_CopierImplem.H"
196 } // end namespace Proto
197 #endif //end include guard
Definition: Proto_Copier.H:134
Point operator*(int a_scale, Point a_pt)
Premultiplication by scalar.
Definition: Proto_Point.H:426
Definition: Proto_Copier.H:175
Definition: Proto_Copier.H:145
An interval in DIM dimensional space.
Definition: Proto_Box.H:26
int procID()
local process ID
Definition: Proto_SPMD.H:52
Definition: Proto_Box.H:11
Definition: Proto_Copier.H:162
Definition: Proto_DataIndex.H:17
Abstract Generic Parallel Copier.
Definition: Proto_Copier.H:52