SST  11.0.0
StructuralSimulationToolkit
sharedRegionImpl.h
1 // Copyright 2009-2021 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2021, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef SST_CORE_CORE_SHAREDREGIONIMPL_H
13 #define SST_CORE_CORE_SHAREDREGIONIMPL_H
14 
15 #include "sst/core/sst_types.h"
16 #include "sst/core/warnmacros.h"
17 
18 #include <string>
19 #include <vector>
20 #include <map>
21 #include <mutex>
22 
23 #include "sst/core/sharedRegion.h"
24 #include "sst/core/output.h"
25 #include "sst/core/serialization/serializable.h"
26 
27 namespace SST {
28 
29 class SharedRegionImpl;
30 
32 public:
33  ChangeSet() { }
34  size_t offset;
35  size_t length;
36  /*const*/ uint8_t *data;
37 
38  ChangeSet(size_t offset, size_t length, /*const*/ uint8_t *data = nullptr) : offset(offset), length(length), data(data) { }
39 
40  void serialize_order(SST::Core::Serialization::serializer &ser) override {
41  ser & offset;
42  // ser & length;
43  // if ( ser.mode() == SST::Core::Serialization::serializer::UNPACK ) {
44  // data = new uint8_t[length];
45  // }
46  // for ( int i = 0; i < length; i++ ) {
47  // ser & data[i];
48  // }
49  ser & SST::Core::Serialization::array(data,length);
50  // ser.binary(data,length);
51 
52  }
53 
54  ImplementSerializable(SST::ChangeSet)
55 
56 
57 };
58 
59 class RegionInfo {
60 public:
62 
63  protected:
64  int rank;
65  std::string key;
66  size_t length;
67  uint8_t initByte;
68 
69  virtual void check_size(RegionInfo *ri) {
70  // Check to see if sizes match.
71 
72  // If the merge info has zero length, then it doesn't know
73  // the size, so just return
74  if ( length == 0 ) return;
75 
76  ri->setSize(length, initByte);
77  }
78 
79  public:
80  RegionMergeInfo() {}
81  RegionMergeInfo(int rank, const std::string& key, size_t length, uint8_t initByte) :
82  rank(rank), key(key), length(length), initByte(initByte) { }
83  virtual ~RegionMergeInfo() { }
84 
85  virtual bool merge(RegionInfo *ri) {
86  check_size(ri);
87  return true;
88 
89  }
90  const std::string& getKey() const { return key; }
91  size_t getLength() const { return length; }
92 
93  void serialize_order(SST::Core::Serialization::serializer &ser) override {
94  ser & rank;
95  ser & key;
96  ser & length;
97  ser & initByte;
98  }
99 
100  ImplementSerializable(SST::RegionInfo::RegionMergeInfo)
101  };
102 
103 
105  protected:
106  void *data;
107 
108  public:
110  BulkMergeInfo(int rank, const std::string& key, void *data, size_t length, uint8_t initByte) :
111  RegionMergeInfo(rank, key, length, initByte),
112  data(data)
113  { }
114 
115  bool merge(RegionInfo *ri) override {
116  check_size(ri);
117  bool ret = ri->getMerger()->merge((uint8_t*)ri->getMemory(), (const uint8_t*)data, length);
118  free(data);
119  return ret;
120  }
121 
122  void serialize_order(SST::Core::Serialization::serializer &ser) override {
123  RegionInfo::RegionMergeInfo::serialize_order(ser);
124 
125  // if ( ser.mode() == SST::Core::Serialization::serializer::UNPACK ) {
126  // data = malloc(length);
127  // }
128  // for ( int i = 0; i < length; i++ ) {
129  // ser & ((uint8_t*)data)[i];
130  // }
131  ser & SST::Core::Serialization::array(data,length);
132  // ser.binary(data,length);
133  }
134 
135  ImplementSerializable(SST::RegionInfo::BulkMergeInfo)
136  };
137 
139  protected:
140  // std::vector<SharedRegionMerger::ChangeSet> changeSets;
141  std::vector<ChangeSet> changeSets;
142  public:
144  ChangeSetMergeInfo(int rank, const std::string& key, size_t length, uint8_t initByte,
145  std::vector<ChangeSet> & changeSets) :
146  RegionMergeInfo(rank, key, length, initByte),
147  changeSets(changeSets)
148  { }
149  bool merge(RegionInfo *ri) override {
150  check_size(ri);
151  return ri->getMerger()->merge((uint8_t*)ri->getMemory(), ri->getSize(), changeSets);
152  }
153 
154  void serialize_order(SST::Core::Serialization::serializer &ser) override {
155  RegionInfo::RegionMergeInfo::serialize_order(ser);
156  ser & changeSets;
157  }
158 
159  ImplementSerializable(SST::RegionInfo::ChangeSetMergeInfo)
160  };
161 
162 
163 private:
164  std::string myKey;
165  size_t realSize;
166  size_t apparentSize;
167  void *memory;
168 
169  size_t shareCount;
170  size_t publishCount;
171 
172  std::vector<SharedRegionImpl*> sharers;
173 
174  SharedRegionMerger *merger; // If null, no multi-rank merging
175  // std::vector<SharedRegionMerger::ChangeSet> changesets;
176  std::vector<ChangeSet> changesets;
177 
178  bool didBulk;
179  bool initialized;
180  bool ready;
181  uint8_t initByte;
182 
183 public:
184  RegionInfo() :
185  realSize(0), apparentSize(0), memory(nullptr),
186  shareCount(0), publishCount(0), merger(nullptr),
187  didBulk(false), initialized(false), ready(false), initByte(0)
188  { }
189  ~RegionInfo();
190  bool initialize(const std::string& key, size_t size, uint8_t initByte_in, SharedRegionMerger *mergeObj);
191  bool setSize(size_t size, uint8_t initByte_in);
192  bool isInitialized() const { return initialized; }
193  bool isReady() const { return ready; }
194 
195  SharedRegionImpl* addSharer(SharedRegionManager *manager);
196  void removeSharer(SharedRegionImpl *sri);
197 
198 
199  void modifyRegion(size_t offset, size_t length, const void *data);
200  void publish();
201 
202  void updateState(bool finalize);
203 
204  const std::string& getKey() const { return myKey; }
205  void* getMemory() { didBulk = true; return memory; }
206  const void* getConstPtr() const { return memory; }
207  size_t getSize() const { return apparentSize; }
208  size_t getNumSharers() const { return shareCount; }
209 
210  bool shouldMerge() const { return (nullptr != merger); }
211  SharedRegionMerger* getMerger() { return merger; }
212  /** Returns the size of the data to be transferred */
213  RegionMergeInfo* getMergeInfo();
214 
215  void setProtected(bool readOnly);
216 };
217 
218 
220  bool published;
221  RegionInfo *region;
222 
223 protected:
224  size_t getSize() { return region->getSize(); }
225 
226 public:
227  SharedRegionImpl(SharedRegionManager *manager, size_t id,
228  RegionInfo *region) : SharedRegion(manager, id),
229  published(false), region(region)
230  { }
231 
232  bool isPublished() const { return published; }
233  void setPublished() { published = true; }
234  RegionInfo* getRegion() const { return region; }
235  void notifySetSize() {
236  if ( deferred_pointer != nullptr ) {
237  deferred_pointer->setPointer(getPtr<const void*>());
238  delete deferred_pointer;
239  deferred_pointer = nullptr;
240  }
241  }
242 };
243 
244 
245 
247 
248  struct CommInfo_t {
249  RegionInfo *region;
250  std::vector<int> tgtRanks;
251  std::vector<char> sendBuffer;
252  };
253 
254  std::map<std::string, RegionInfo> regions;
255  std::mutex mtx;
256 
257 protected:
258  void modifyRegion(SharedRegion *sr, size_t offset, size_t length, const void *data) override;
259  void* getMemory(SharedRegion *sr) override;
260  const void* getConstPtr(const SharedRegion *sr) const override;
261  size_t getSize(const SharedRegion *sr) const override;
262 
263 public:
266 
267  virtual SharedRegion* getLocalSharedRegion(const std::string& key, size_t size, uint8_t initByte = 0) override;
268  virtual SharedRegion* getGlobalSharedRegion(const std::string& key, size_t size, SharedRegionMerger *merger = nullptr, uint8_t initByte = 0) override;
269 
270  virtual void publishRegion(SharedRegion*) override;
271  virtual bool isRegionReady(const SharedRegion*) override;
272  virtual void shutdownSharedRegion(SharedRegion*) override;
273 
274  void updateState(bool finalize) override;
275 };
276 
277 
278 }
279 #endif
Definition: sharedRegion.h:135
Definition: sharedRegionImpl.h:246
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
virtual SharedRegion * getGlobalSharedRegion(const std::string &key, size_t size, SharedRegionMerger *merger=nullptr, uint8_t initByte=0) override
Create a SharedRegion that will be shared with elements on the all ranks.
Definition: sharedRegion.cc:268
Definition: sharedRegionImpl.h:31
Definition: sharedRegionImpl.h:219
Utility class to define how to merge multiple pieces of shared memory regions Useful in the multi-MPI...
Definition: sharedRegion.h:34
RegionMergeInfo * getMergeInfo()
Returns the size of the data to be transferred.
Definition: sharedRegion.cc:226
Definition: serializable.h:109
virtual SharedRegion * getLocalSharedRegion(const std::string &key, size_t size, uint8_t initByte=0) override
Create a SharedRegion that will only be shared with elements on the current rank. ...
Definition: sharedRegion.cc:259
Definition: sharedRegionImpl.h:59
virtual bool merge(uint8_t *target, const uint8_t *newData, size_t size)
Merge the data from &#39;newData&#39; into &#39;target&#39;.
Definition: sharedRegionImpl.h:138
Definition: sharedRegionImpl.h:61
Definition: sharedRegion.h:64
Definition: sharedRegionImpl.h:104