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