SST  10.1.0
StructuralSimulationToolkit
sharedRegionImpl.h
1 // Copyright 2009-2020 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-2020, 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/serialization/serializable.h"
25 
26 namespace SST {
27 
28 class SharedRegionImpl;
29 
31 public:
32  ChangeSet() { }
33  size_t offset;
34  size_t length;
35  /*const*/ uint8_t *data;
36 
37  ChangeSet(size_t offset, size_t length, /*const*/ uint8_t *data = nullptr) : offset(offset), length(length), data(data) { }
38 
39  void serialize_order(SST::Core::Serialization::serializer &ser) override {
40  ser & offset;
41  // ser & length;
42  // if ( ser.mode() == SST::Core::Serialization::serializer::UNPACK ) {
43  // data = new uint8_t[length];
44  // }
45  // for ( int i = 0; i < length; i++ ) {
46  // ser & data[i];
47  // }
48  ser & SST::Core::Serialization::array(data,length);
49  // ser.binary(data,length);
50 
51  }
52 
53  ImplementSerializable(SST::ChangeSet)
54 
55 
56 };
57 
58 class RegionInfo {
59 public:
61 
62  protected:
63  int rank;
64  std::string key;
65 
66  public:
67  RegionMergeInfo() {}
68  RegionMergeInfo(int rank, const std::string& key) : rank(rank), key(key) { }
69  virtual ~RegionMergeInfo() { }
70 
71  virtual bool merge(RegionInfo *UNUSED(ri)) { return true; }
72  const std::string& getKey() const { return key; }
73 
74  void serialize_order(SST::Core::Serialization::serializer &ser) override {
75  ser & rank;
76  ser & key;
77  }
78 
79  ImplementSerializable(SST::RegionInfo::RegionMergeInfo)
80  };
81 
82 
83  class BulkMergeInfo : public RegionMergeInfo {
84  protected:
85  size_t length;
86  void *data;
87 
88  public:
90  BulkMergeInfo(int rank, const std::string& key, void *data, size_t length) : RegionMergeInfo(rank, key),
91  length(length), data(data)
92  { }
93 
94  bool merge(RegionInfo *ri) override {
95  bool ret = ri->getMerger()->merge((uint8_t*)ri->getMemory(), (const uint8_t*)data, length);
96  free(data);
97  return ret;
98  }
99 
100  void serialize_order(SST::Core::Serialization::serializer &ser) override {
101  RegionInfo::RegionMergeInfo::serialize_order(ser);
102 
103  ser & length;
104  // if ( ser.mode() == SST::Core::Serialization::serializer::UNPACK ) {
105  // data = malloc(length);
106  // }
107  // for ( int i = 0; i < length; i++ ) {
108  // ser & ((uint8_t*)data)[i];
109  // }
110  ser & SST::Core::Serialization::array(data,length);
111  // ser.binary(data,length);
112  }
113 
114  ImplementSerializable(SST::RegionInfo::BulkMergeInfo)
115  };
116 
118  protected:
119  // std::vector<SharedRegionMerger::ChangeSet> changeSets;
120  std::vector<ChangeSet> changeSets;
121  public:
123  ChangeSetMergeInfo(int rank, const std::string& key,
124  // std::vector<SharedRegionMerger::ChangeSet> & changeSets) : RegionMergeInfo(rank, key),
125  std::vector<ChangeSet> & changeSets) : RegionMergeInfo(rank, key),
126  changeSets(changeSets)
127  { }
128  bool merge(RegionInfo *ri) override {
129  return ri->getMerger()->merge((uint8_t*)ri->getMemory(), ri->getSize(), changeSets);
130  }
131 
132  void serialize_order(SST::Core::Serialization::serializer &ser) override {
133  RegionInfo::RegionMergeInfo::serialize_order(ser);
134  ser & changeSets;
135  }
136 
137  ImplementSerializable(SST::RegionInfo::ChangeSetMergeInfo)
138  };
139 
140 
141 private:
142  std::string myKey;
143  size_t realSize;
144  size_t apparentSize;
145  void *memory;
146 
147  size_t shareCount;
148  size_t publishCount;
149 
150  std::vector<SharedRegionImpl*> sharers;
151 
152  SharedRegionMerger *merger; // If null, no multi-rank merging
153  // std::vector<SharedRegionMerger::ChangeSet> changesets;
154  std::vector<ChangeSet> changesets;
155 
156  bool didBulk;
157  bool initialized;
158  bool ready;
159 
160 
161 public:
162  RegionInfo() : realSize(0), apparentSize(0), memory(nullptr),
163  shareCount(0), publishCount(0), merger(nullptr),
164  didBulk(false), initialized(false), ready(false)
165  { }
166  ~RegionInfo();
167  bool initialize(const std::string& key, size_t size, uint8_t initByte, SharedRegionMerger *mergeObj);
168  bool isInitialized() const { return initialized; }
169  bool isReady() const { return ready; }
170 
171  SharedRegionImpl* addSharer(SharedRegionManager *manager);
172  void removeSharer(SharedRegionImpl *sri);
173 
174 
175  void modifyRegion(size_t offset, size_t length, const void *data);
176  void publish();
177 
178  void updateState(bool finalize);
179 
180  const std::string& getKey() const { return myKey; }
181  void* getMemory() { didBulk = true; return memory; }
182  const void* getConstPtr() const { return memory; }
183  size_t getSize() const { return apparentSize; }
184  size_t getNumSharers() const { return shareCount; }
185 
186  bool shouldMerge() const { return (nullptr != merger); }
187  SharedRegionMerger* getMerger() { return merger; }
188  /** Returns the size of the data to be transferred */
189  RegionMergeInfo* getMergeInfo();
190 
191  void setProtected(bool readOnly);
192 };
193 
194 
196  bool published;
197  RegionInfo *region;
198 public:
199  SharedRegionImpl(SharedRegionManager *manager, size_t id,
200  size_t size, RegionInfo *region) : SharedRegion(manager, id, size),
201  published(false), region(region)
202  { }
203 
204  bool isPublished() const { return published; }
205  void setPublished() { published = true; }
206  RegionInfo* getRegion() const { return region; }
207 };
208 
209 
210 
212 
213  struct CommInfo_t {
214  RegionInfo *region;
215  std::vector<int> tgtRanks;
216  std::vector<char> sendBuffer;
217  };
218 
219  std::map<std::string, RegionInfo> regions;
220  std::mutex mtx;
221 
222 protected:
223  void modifyRegion(SharedRegion *sr, size_t offset, size_t length, const void *data) override;
224  void* getMemory(SharedRegion *sr) override;
225  const void* getConstPtr(const SharedRegion *sr) const override;
226 
227 public:
230 
231  virtual SharedRegion* getLocalSharedRegion(const std::string& key, size_t size, uint8_t initByte = 0) override;
232  virtual SharedRegion* getGlobalSharedRegion(const std::string& key, size_t size, SharedRegionMerger *merger = nullptr, uint8_t initByte = 0) override;
233 
234  virtual void publishRegion(SharedRegion*) override;
235  virtual bool isRegionReady(const SharedRegion*) override;
236  virtual void shutdownSharedRegion(SharedRegion*) override;
237 
238  void updateState(bool finalize) override;
239 };
240 
241 
242 }
243 #endif
Definition: sharedRegion.h:81
Definition: sharedRegionImpl.h:211
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
Definition: sharedRegionImpl.h:30
Definition: sharedRegionImpl.h:195
Utility class to define how to merge multiple pieces of shared memory regions Useful in the multi-MPI...
Definition: sharedRegion.h:30
RegionMergeInfo * getMergeInfo()
Returns the size of the data to be transferred.
Definition: sharedRegion.cc:189
Definition: serializable.h:109
Definition: sharedRegionImpl.h:58
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:117
Definition: sharedRegionImpl.h:60
Definition: sharedRegion.h:60
Definition: sharedRegionImpl.h:83